06_add_compile_time_defaults.dpatch 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 tor-debian~/src/or/config.c tor-debian/src/or/config.c
  22. --- tor-debian~/src/or/config.c 2007-03-06 21:52:33.000000000 +0100
  23. +++ tor-debian/src/or/config.c 2007-04-27 13:05:42.420147495 +0200
  24. @@ -12,6 +12,7 @@
  25. **/
  26. #include "or.h"
  27. +#include <pwd.h>
  28. #ifdef MS_WINDOWS
  29. #include <shlobj.h>
  30. #endif
  31. @@ -592,6 +593,10 @@
  32. static void check_libevent_version(const char *m, int server);
  33. #endif
  34. +static int debian_running_as_debiantor();
  35. +static int debian_config_fix_defaults();
  36. +
  37. +
  38. /*static*/ or_options_t *options_new(void);
  39. /** Magic value for or_options_t. */
  40. @@ -2982,7 +2987,7 @@
  41. int
  42. options_init_from_torrc(int argc, char **argv)
  43. {
  44. - or_options_t *oldoptions, *newoptions;
  45. + or_options_t *oldoptions, *newoptions = NULL;
  46. config_line_t *cl;
  47. char *cf=NULL, *fname=NULL, *errmsg=NULL;
  48. int i, retval;
  49. @@ -2991,6 +2996,9 @@
  50. static char **backup_argv;
  51. static int backup_argc;
  52. + if (debian_config_fix_defaults() < 0)
  53. + goto err;
  54. +
  55. if (argv) { /* first time we're called. save commandline args */
  56. backup_argv = argv;
  57. backup_argc = argc;
  58. @@ -3120,7 +3128,8 @@
  59. err:
  60. tor_free(fname);
  61. torrc_fname = NULL;
  62. - config_free(&options_format, newoptions);
  63. + if (newoptions)
  64. + config_free(&options_format, newoptions);
  65. if (errmsg) {
  66. log(LOG_WARN,LD_CONFIG,"Failed to parse/validate config: %s", errmsg);
  67. tor_free(errmsg);
  68. @@ -4306,3 +4315,52 @@
  69. puts(routerparse_c_id);
  70. }
  71. +/* Checks whether we are running as the debian-tor user.
  72. + * Returns -1 on error, 1 if we are debian-tor, 0 if not */
  73. +static int
  74. +debian_running_as_debiantor()
  75. +{
  76. + struct passwd *pw = NULL;
  77. + int uid;
  78. +
  79. + uid = getuid();
  80. + pw = getpwuid(uid);
  81. + if (!pw) {
  82. + log(LOG_WARN, LD_GENERAL, "Could not get passwd information for uid %d.", uid);
  83. + return -1;
  84. + }
  85. + assert(pw->pw_name);
  86. + if (strcmp(pw->pw_name, "debian-tor") == 0)
  87. + return 1;
  88. + else
  89. + return 0;
  90. +}
  91. +
  92. +static int
  93. +debian_config_fix_defaults()
  94. +{
  95. + config_var_t *var;
  96. + static int fixed = 0;
  97. + int running_as_debian;
  98. +
  99. + if (fixed) return 0;
  100. + fixed = 1;
  101. +
  102. + running_as_debian = debian_running_as_debiantor();
  103. + if (running_as_debian < 0) return -1;
  104. + if (!running_as_debian) return 0;
  105. +
  106. + var = config_find_option(&options_format, "DataDirectory");
  107. + tor_assert(var);
  108. + var->initvalue = tor_strdup("/var/lib/tor");
  109. +
  110. + var = config_find_option(&options_format, "PidFile");
  111. + tor_assert(var);
  112. + var->initvalue = tor_strdup("/var/run/tor/tor.pid");
  113. +
  114. + var = config_find_option(&options_format, "RunAsDaemon");
  115. + tor_assert(var);
  116. + var->initvalue = tor_strdup("1");
  117. +
  118. + return 0;
  119. +}