env.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/process/env.h"
  7. #include "lib/malloc/util_malloc.h"
  8. #include "lib/ctime/di_ops.h"
  9. #include "lib/container/smartlist.h"
  10. #include "lib/log/util_bug.h"
  11. #include "lib/log/torlog.h"
  12. #include "lib/malloc/util_malloc.h"
  13. #ifdef HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #ifdef HAVE_CRT_EXTERNS_H
  19. /* For _NSGetEnviron on macOS */
  20. #include <crt_externs.h>
  21. #endif
  22. #ifndef HAVE__NSGETENVIRON
  23. #ifndef HAVE_EXTERN_ENVIRON_DECLARED
  24. /* Some platforms declare environ under some circumstances, others don't. */
  25. #ifndef RUNNING_DOXYGEN
  26. extern char **environ;
  27. #endif
  28. #endif /* !defined(HAVE_EXTERN_ENVIRON_DECLARED) */
  29. #endif /* !defined(HAVE__NSGETENVIRON) */
  30. /** Return the current environment. This is a portable replacement for
  31. * 'environ'. */
  32. char **
  33. get_environment(void)
  34. {
  35. #ifdef HAVE__NSGETENVIRON
  36. /* This is for compatibility between OSX versions. Otherwise (for example)
  37. * when we do a mostly-static build on OSX 10.7, the resulting binary won't
  38. * work on OSX 10.6. */
  39. return *_NSGetEnviron();
  40. #else /* !(defined(HAVE__NSGETENVIRON)) */
  41. return environ;
  42. #endif /* defined(HAVE__NSGETENVIRON) */
  43. }
  44. /** Helper: return the number of characters in <b>s</b> preceding the first
  45. * occurrence of <b>ch</b>. If <b>ch</b> does not occur in <b>s</b>, return
  46. * the length of <b>s</b>. Should be equivalent to strspn(s, "ch"). */
  47. static inline size_t
  48. str_num_before(const char *s, char ch)
  49. {
  50. const char *cp = strchr(s, ch);
  51. if (cp)
  52. return cp - s;
  53. else
  54. return strlen(s);
  55. }
  56. /** Return non-zero iff getenv would consider <b>s1</b> and <b>s2</b>
  57. * to have the same name as strings in a process's environment. */
  58. int
  59. environment_variable_names_equal(const char *s1, const char *s2)
  60. {
  61. size_t s1_name_len = str_num_before(s1, '=');
  62. size_t s2_name_len = str_num_before(s2, '=');
  63. return (s1_name_len == s2_name_len &&
  64. tor_memeq(s1, s2, s1_name_len));
  65. }
  66. /** Free <b>env</b> (assuming it was produced by
  67. * process_environment_make). */
  68. void
  69. process_environment_free_(process_environment_t *env)
  70. {
  71. if (env == NULL) return;
  72. /* As both an optimization hack to reduce consing on Unixoid systems
  73. * and a nice way to ensure that some otherwise-Windows-specific
  74. * code will always get tested before changes to it get merged, the
  75. * strings which env->unixoid_environment_block points to are packed
  76. * into env->windows_environment_block. */
  77. tor_free(env->unixoid_environment_block);
  78. tor_free(env->windows_environment_block);
  79. tor_free(env);
  80. }
  81. /** Make a process_environment_t containing the environment variables
  82. * specified in <b>env_vars</b> (as C strings of the form
  83. * "NAME=VALUE"). */
  84. process_environment_t *
  85. process_environment_make(struct smartlist_t *env_vars)
  86. {
  87. process_environment_t *env = tor_malloc_zero(sizeof(process_environment_t));
  88. int n_env_vars = smartlist_len(env_vars);
  89. int i;
  90. size_t total_env_length;
  91. smartlist_t *env_vars_sorted;
  92. tor_assert(n_env_vars + 1 != 0);
  93. env->unixoid_environment_block = tor_calloc(n_env_vars + 1, sizeof(char *));
  94. /* env->unixoid_environment_block is already NULL-terminated,
  95. * because we assume that NULL == 0 (and check that during compilation). */
  96. total_env_length = 1; /* terminating NUL of terminating empty string */
  97. for (i = 0; i < n_env_vars; ++i) {
  98. const char *s = smartlist_get(env_vars, (int)i);
  99. size_t slen = strlen(s);
  100. tor_assert(slen + 1 != 0);
  101. tor_assert(slen + 1 < SIZE_MAX - total_env_length);
  102. total_env_length += slen + 1;
  103. }
  104. env->windows_environment_block = tor_malloc_zero(total_env_length);
  105. /* env->windows_environment_block is already
  106. * (NUL-terminated-empty-string)-terminated. */
  107. /* Some versions of Windows supposedly require that environment
  108. * blocks be sorted. Or maybe some Windows programs (or their
  109. * runtime libraries) fail to look up strings in non-sorted
  110. * environment blocks.
  111. *
  112. * Also, sorting strings makes it easy to find duplicate environment
  113. * variables and environment-variable strings without an '=' on all
  114. * OSes, and they can cause badness. Let's complain about those. */
  115. env_vars_sorted = smartlist_new();
  116. smartlist_add_all(env_vars_sorted, env_vars);
  117. smartlist_sort_strings(env_vars_sorted);
  118. /* Now copy the strings into the environment blocks. */
  119. {
  120. char *cp = env->windows_environment_block;
  121. const char *prev_env_var = NULL;
  122. for (i = 0; i < n_env_vars; ++i) {
  123. const char *s = smartlist_get(env_vars_sorted, (int)i);
  124. size_t slen = strlen(s);
  125. size_t s_name_len = str_num_before(s, '=');
  126. if (s_name_len == slen) {
  127. log_warn(LD_GENERAL,
  128. "Preparing an environment containing a variable "
  129. "without a value: %s",
  130. s);
  131. }
  132. if (prev_env_var != NULL &&
  133. environment_variable_names_equal(s, prev_env_var)) {
  134. log_warn(LD_GENERAL,
  135. "Preparing an environment containing two variables "
  136. "with the same name: %s and %s",
  137. prev_env_var, s);
  138. }
  139. prev_env_var = s;
  140. /* Actually copy the string into the environment. */
  141. memcpy(cp, s, slen+1);
  142. env->unixoid_environment_block[i] = cp;
  143. cp += slen+1;
  144. }
  145. tor_assert(cp == env->windows_environment_block + total_env_length - 1);
  146. }
  147. smartlist_free(env_vars_sorted);
  148. return env;
  149. }
  150. /** Return a newly allocated smartlist containing every variable in
  151. * this process's environment, as a NUL-terminated string of the form
  152. * "NAME=VALUE". Note that on some/many/most/all OSes, the parent
  153. * process can put strings not of that form in our environment;
  154. * callers should try to not get crashed by that.
  155. *
  156. * The returned strings are heap-allocated, and must be freed by the
  157. * caller. */
  158. struct smartlist_t *
  159. get_current_process_environment_variables(void)
  160. {
  161. smartlist_t *sl = smartlist_new();
  162. char **environ_tmp; /* Not const char ** ? Really? */
  163. for (environ_tmp = get_environment(); *environ_tmp; ++environ_tmp) {
  164. smartlist_add_strdup(sl, *environ_tmp);
  165. }
  166. return sl;
  167. }
  168. /** For each string s in <b>env_vars</b> such that
  169. * environment_variable_names_equal(s, <b>new_var</b>), remove it; if
  170. * <b>free_p</b> is non-zero, call <b>free_old</b>(s). If
  171. * <b>new_var</b> contains '=', insert it into <b>env_vars</b>. */
  172. void
  173. set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
  174. const char *new_var,
  175. void (*free_old)(void*),
  176. int free_p)
  177. {
  178. SMARTLIST_FOREACH_BEGIN(env_vars, const char *, s) {
  179. if (environment_variable_names_equal(s, new_var)) {
  180. SMARTLIST_DEL_CURRENT(env_vars, s);
  181. if (free_p) {
  182. free_old((void *)s);
  183. }
  184. }
  185. } SMARTLIST_FOREACH_END(s);
  186. if (strchr(new_var, '=') != NULL) {
  187. smartlist_add(env_vars, (void *)new_var);
  188. }
  189. }