env.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "nssm.h"
  2. /*
  3. Environment block is of the form:
  4. KEY1=VALUE1 NULL
  5. KEY2=VALUE2 NULL
  6. NULL
  7. A single variable KEY=VALUE has length 15:
  8. KEY=VALUE (13) NULL (1)
  9. NULL (1)
  10. Environment variable names are case-insensitive!
  11. */
  12. /* Find the length in characters of an environment block. */
  13. size_t environment_length(TCHAR *env) {
  14. size_t len = 0;
  15. TCHAR *s;
  16. for (s = env; ; s++) {
  17. len++;
  18. if (*s == _T('\0')) {
  19. if (*(s + 1) == _T('\0')) {
  20. len++;
  21. break;
  22. }
  23. }
  24. }
  25. return len;
  26. }
  27. /* Copy an environment block. */
  28. TCHAR *copy_environment_block(TCHAR *env) {
  29. TCHAR *newenv;
  30. if (copy_double_null(env, (unsigned long) environment_length(env), &newenv)) return 0;
  31. return newenv;
  32. }
  33. /*
  34. The environment block starts with variables of the form
  35. =C:=C:\Windows\System32 which we ignore.
  36. */
  37. TCHAR *useful_environment(TCHAR *rawenv) {
  38. TCHAR *env = rawenv;
  39. if (env) {
  40. while (*env == _T('=')) {
  41. for ( ; *env; env++);
  42. env++;
  43. }
  44. }
  45. return env;
  46. }
  47. /* Expand an environment variable. Must call HeapFree() on the result. */
  48. TCHAR *expand_environment_string(TCHAR *string) {
  49. unsigned long len;
  50. len = ExpandEnvironmentStrings(string, 0, 0);
  51. if (! len) {
  52. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, string, error_string(GetLastError()), 0);
  53. return 0;
  54. }
  55. TCHAR *ret = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  56. if (! ret) {
  57. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("ExpandEnvironmentStrings()"), _T("expand_environment_string"), 0);
  58. return 0;
  59. }
  60. if (! ExpandEnvironmentStrings(string, ret, len)) {
  61. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, string, error_string(GetLastError()), 0);
  62. HeapFree(GetProcessHeap(), 0, ret);
  63. return 0;
  64. }
  65. return ret;
  66. }
  67. /*
  68. Set all the environment variables from an environment block in the current
  69. environment or remove all the variables in the block from the current
  70. environment.
  71. */
  72. static int set_environment_block(TCHAR *env, bool set) {
  73. int ret = 0;
  74. TCHAR *s, *t;
  75. for (s = env; *s; s++) {
  76. for (t = s; *t && *t != _T('='); t++);
  77. if (*t == _T('=')) {
  78. *t = _T('\0');
  79. if (set) {
  80. TCHAR *expanded = expand_environment_string(++t);
  81. if (expanded) {
  82. if (! SetEnvironmentVariable(s, expanded)) ret++;
  83. HeapFree(GetProcessHeap(), 0, expanded);
  84. }
  85. else {
  86. if (! SetEnvironmentVariable(s, t)) ret++;
  87. }
  88. }
  89. else {
  90. if (! SetEnvironmentVariable(s, NULL)) ret++;
  91. }
  92. for (t++; *t; t++);
  93. }
  94. s = t;
  95. }
  96. return ret;
  97. }
  98. int set_environment_block(TCHAR *env) {
  99. return set_environment_block(env, true);
  100. }
  101. static int unset_environment_block(TCHAR *env) {
  102. return set_environment_block(env, false);
  103. }
  104. /* Remove all variables from the process environment. */
  105. int clear_environment() {
  106. TCHAR *rawenv = GetEnvironmentStrings();
  107. TCHAR *env = useful_environment(rawenv);
  108. int ret = unset_environment_block(env);
  109. if (rawenv) FreeEnvironmentStrings(rawenv);
  110. return ret;
  111. }
  112. /* Set the current environment to exactly duplicate an environment block. */
  113. int duplicate_environment(TCHAR *rawenv) {
  114. int ret = clear_environment();
  115. TCHAR *env = useful_environment(rawenv);
  116. ret += set_environment_block(env);
  117. return ret;
  118. }
  119. /*
  120. Verify an environment block.
  121. Returns: 1 if environment is invalid.
  122. 0 if environment is OK.
  123. -1 on error.
  124. */
  125. int test_environment(TCHAR *env) {
  126. TCHAR *path = (TCHAR *) nssm_imagepath();
  127. STARTUPINFO si;
  128. ZeroMemory(&si, sizeof(si));
  129. si.cb = sizeof(si);
  130. PROCESS_INFORMATION pi;
  131. ZeroMemory(&pi, sizeof(pi));
  132. unsigned long flags = CREATE_SUSPENDED;
  133. #ifdef UNICODE
  134. flags |= CREATE_UNICODE_ENVIRONMENT;
  135. #endif
  136. /*
  137. Try to relaunch ourselves but with the candidate environment set.
  138. Assuming no solar flare activity, the only reason this would fail is if
  139. the environment were invalid.
  140. */
  141. if (CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) {
  142. TerminateProcess(pi.hProcess, 0);
  143. }
  144. else {
  145. unsigned long error = GetLastError();
  146. if (error == ERROR_INVALID_PARAMETER) return 1;
  147. else return -1;
  148. }
  149. return 0;
  150. }
  151. /*
  152. Duplicate an environment block returned by GetEnvironmentStrings().
  153. Since such a block is by definition readonly, and duplicate_environment()
  154. modifies its inputs, this function takes a copy of the input and operates
  155. on that.
  156. */
  157. void duplicate_environment_strings(TCHAR *env) {
  158. TCHAR *newenv = copy_environment_block(env);
  159. if (! newenv) return;
  160. duplicate_environment(newenv);
  161. HeapFree(GetProcessHeap(), 0, newenv);
  162. }
  163. /* Safely get a copy of the current environment. */
  164. TCHAR *copy_environment() {
  165. TCHAR *rawenv = GetEnvironmentStrings();
  166. if (! rawenv) return NULL;
  167. TCHAR *env = copy_environment_block(rawenv);
  168. FreeEnvironmentStrings(rawenv);
  169. return env;
  170. }
  171. /*
  172. Create a new block with all the strings of the first block plus a new string.
  173. If the key is already present its value will be overwritten in place.
  174. If the key is blank or empty the new block will still be allocated and have
  175. non-zero length.
  176. */
  177. int append_to_environment_block(TCHAR *env, unsigned long envlen, TCHAR *string, TCHAR **newenv, unsigned long *newlen) {
  178. size_t keylen = 0;
  179. if (string && string[0]) {
  180. for (; string[keylen]; keylen++) {
  181. if (string[keylen] == _T('=')) {
  182. keylen++;
  183. break;
  184. }
  185. }
  186. }
  187. return append_to_double_null(env, envlen, newenv, newlen, string, keylen, false);
  188. }
  189. /*
  190. Create a new block with all the strings of the first block minus the given
  191. string.
  192. If the key is not present the new block will be a copy of the original.
  193. If the string is KEY=VALUE the key will only be removed if its value is
  194. VALUE.
  195. If the string is just KEY the key will unconditionally be removed.
  196. If removing the string results in an empty list the new block will still be
  197. allocated and have non-zero length.
  198. */
  199. int remove_from_environment_block(TCHAR *env, unsigned long envlen, TCHAR *string, TCHAR **newenv, unsigned long *newlen) {
  200. if (! string || ! string[0] || string[0] == _T('=')) return 1;
  201. TCHAR *key = 0;
  202. size_t len = _tcslen(string);
  203. size_t i;
  204. for (i = 0; i < len; i++) if (string[i] == _T('=')) break;
  205. /* Rewrite KEY to KEY= but leave KEY=VALUE alone. */
  206. size_t keylen = len;
  207. if (i == len) keylen++;
  208. key = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (keylen + 1) * sizeof(TCHAR));
  209. if (! key) {
  210. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("key"), _T("remove_from_environment_block()"), 0);
  211. return 2;
  212. }
  213. memmove(key, string, len * sizeof(TCHAR));
  214. if (keylen > len) key[keylen - 1] = _T('=');
  215. key[keylen] = _T('\0');
  216. int ret = remove_from_double_null(env, envlen, newenv, newlen, key, keylen, false);
  217. HeapFree(GetProcessHeap(), 0, key);
  218. return ret;
  219. }