06_add_compile_time_defaults.dpatch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #! /bin/sh -e
  2. ## 06_add_compile_time_defaults.dpatch by <weasel@debian.org>
  3. ##
  4. ## All lines beginning with `## DP:' are a description of the patch.
  5. ## DP: No description.
  6. if [ $# -lt 1 ]; then
  7. echo "`basename $0`: script expects -patch|-unpatch as argument" >&2
  8. exit 1
  9. fi
  10. [ -f debian/patches/00patch-opts ] && . debian/patches/00patch-opts
  11. patch_opts="${patch_opts:--f --no-backup-if-mismatch} ${2:+-d $2}"
  12. case "$1" in
  13. -patch) patch -p1 ${patch_opts} < $0;;
  14. -unpatch) patch -R -p1 ${patch_opts} < $0;;
  15. *)
  16. echo "`basename $0`: script expects -patch|-unpatch as argument" >&2
  17. exit 1;;
  18. esac
  19. exit 0
  20. @DPATCH@
  21. diff -urNad trunk~/src/or/config.c trunk/src/or/config.c
  22. --- trunk~/src/or/config.c 2008-02-23 11:08:40.000000000 +0100
  23. +++ trunk/src/or/config.c 2008-02-23 13:43:05.000000000 +0100
  24. @@ -15,6 +15,7 @@
  25. #define CONFIG_PRIVATE
  26. #include "or.h"
  27. +#include <pwd.h>
  28. #ifdef MS_WINDOWS
  29. #include <shlobj.h>
  30. #endif
  31. @@ -660,6 +661,8 @@
  32. #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD)
  33. static void check_libevent_version(const char *m, int server);
  34. #endif
  35. +static int debian_running_as_debiantor();
  36. +static int debian_config_fix_defaults();
  37. /** Magic value for or_options_t. */
  38. #define OR_OPTIONS_MAGIC 9090909
  39. @@ -3522,7 +3525,7 @@
  40. int
  41. options_init_from_torrc(int argc, char **argv)
  42. {
  43. - or_options_t *oldoptions, *newoptions;
  44. + or_options_t *oldoptions, *newoptions = NULL;
  45. config_line_t *cl;
  46. char *cf=NULL, *fname=NULL, *errmsg=NULL;
  47. int i, retval;
  48. @@ -3531,6 +3534,9 @@
  49. static char **backup_argv;
  50. static int backup_argc;
  51. + if (debian_config_fix_defaults() < 0)
  52. + goto err;
  53. +
  54. if (argv) { /* first time we're called. save commandline args */
  55. backup_argv = argv;
  56. backup_argc = argc;
  57. @@ -3666,7 +3672,8 @@
  58. err:
  59. tor_free(fname);
  60. torrc_fname = NULL;
  61. - config_free(&options_format, newoptions);
  62. + if (newoptions)
  63. + config_free(&options_format, newoptions);
  64. if (errmsg) {
  65. log(LOG_WARN,LD_CONFIG,"Failed to parse/validate config: %s", errmsg);
  66. tor_free(errmsg);
  67. @@ -5011,3 +5018,64 @@
  68. puts(routerparse_c_id);
  69. }
  70. +/* Checks whether we are running as the debian-tor user.
  71. + * Returns -1 on error, 1 if we are debian-tor, 0 if not */
  72. +static int
  73. +debian_running_as_debiantor()
  74. +{
  75. + struct passwd *pw = NULL;
  76. + int uid;
  77. +
  78. + uid = getuid();
  79. + /* If we run as root we also apply our debian defaults. */
  80. + if (uid == 0)
  81. + return 1;
  82. +
  83. + pw = getpwuid(uid);
  84. + if (!pw) {
  85. + log(LOG_WARN, LD_GENERAL, "Could not get passwd information for uid %d.", uid);
  86. + return -1;
  87. + }
  88. + assert(pw->pw_name);
  89. + if (strcmp(pw->pw_name, "debian-tor") == 0)
  90. + return 1;
  91. + else
  92. + return 0;
  93. +}
  94. +
  95. +static int
  96. +debian_config_fix_defaults()
  97. +{
  98. + config_var_t *var;
  99. + static int fixed = 0;
  100. + int running_as_debian;
  101. +
  102. + if (fixed) return 0;
  103. + fixed = 1;
  104. +
  105. + running_as_debian = debian_running_as_debiantor();
  106. + if (running_as_debian < 0) return -1;
  107. + if (!running_as_debian) return 0;
  108. +
  109. + var = config_find_option(&options_format, "DataDirectory");
  110. + tor_assert(var);
  111. + var->initvalue = tor_strdup("/var/lib/tor");
  112. +
  113. + var = config_find_option(&options_format, "PidFile");
  114. + tor_assert(var);
  115. + var->initvalue = tor_strdup("/var/run/tor/tor.pid");
  116. +
  117. + var = config_find_option(&options_format, "RunAsDaemon");
  118. + tor_assert(var);
  119. + var->initvalue = tor_strdup("1");
  120. +
  121. + var = config_find_option(&options_format, "User");
  122. + tor_assert(var);
  123. + var->initvalue = tor_strdup("debian-tor");
  124. +
  125. + var = config_find_option(&options_format, "Group");
  126. + tor_assert(var);
  127. + var->initvalue = tor_strdup("debian-tor");
  128. +
  129. + return 0;
  130. +}