06_add_compile_time_defaults.dpatch 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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: Change a few compile time defaults so that Tor is better integrated on a Debian system
  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-trunk~/src/or/config.c tor-trunk/src/or/config.c
  22. --- tor-trunk~/src/or/config.c 2009-01-18 01:47:33.000000000 +0100
  23. +++ tor-trunk/src/or/config.c 2009-02-05 00:25:17.614844812 +0100
  24. @@ -12,6 +12,7 @@
  25. #define CONFIG_PRIVATE
  26. #include "or.h"
  27. +#include <pwd.h>
  28. #ifdef MS_WINDOWS
  29. #include <shlobj.h>
  30. #endif
  31. @@ -711,6 +712,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. @@ -3917,6 +3920,9 @@
  40. char *command_arg = NULL;
  41. char *errmsg=NULL;
  42. + if (debian_config_fix_defaults() < 0)
  43. + goto err;
  44. +
  45. if (argv) { /* first time we're called. save commandline args */
  46. backup_argv = argv;
  47. backup_argc = argc;
  48. @@ -5307,3 +5313,62 @@
  49. return 0;
  50. }
  51. +/* Checks whether we are running as the debian-tor user.
  52. + * Returns -1 on error, 2 if we are root, 1 if we are debian-tor, 0 if we are any normal user */
  53. +static int
  54. +debian_running_as_debiantor()
  55. +{
  56. + struct passwd *pw = NULL;
  57. + int uid;
  58. +
  59. + uid = getuid();
  60. + /* If we run as root we also apply our debian defaults. */
  61. + if (uid == 0)
  62. + return 2;
  63. +
  64. + pw = getpwuid(uid);
  65. + if (!pw) {
  66. + log(LOG_WARN, LD_GENERAL, "Could not get passwd information for uid %d.", uid);
  67. + return -1;
  68. + }
  69. + assert(pw->pw_name);
  70. + if (strcmp(pw->pw_name, "debian-tor") == 0)
  71. + return 1;
  72. + else
  73. + return 0;
  74. +}
  75. +
  76. +static int
  77. +debian_config_fix_defaults()
  78. +{
  79. + config_var_t *var;
  80. + static int fixed = 0;
  81. + int running_as_debian;
  82. +
  83. + if (fixed) return 0;
  84. + fixed = 1;
  85. +
  86. + running_as_debian = debian_running_as_debiantor();
  87. + if (running_as_debian < 0) return -1;
  88. + if (!running_as_debian) return 0;
  89. +
  90. + var = config_find_option(&options_format, "DataDirectory");
  91. + tor_assert(var);
  92. + var->initvalue = tor_strdup("/var/lib/tor");
  93. +
  94. + var = config_find_option(&options_format, "PidFile");
  95. + tor_assert(var);
  96. + var->initvalue = tor_strdup("/var/run/tor/tor.pid");
  97. +
  98. + var = config_find_option(&options_format, "RunAsDaemon");
  99. + tor_assert(var);
  100. + var->initvalue = tor_strdup("1");
  101. +
  102. + if (running_as_debian == 2) {
  103. + var = config_find_option(&options_format, "User");
  104. + tor_assert(var);
  105. + var->initvalue = tor_strdup("debian-tor");
  106. + };
  107. +
  108. + return 0;
  109. +}