env.c 6.9 KB

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