06_add_compile_time_defaults.dpatch 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 git-trunk-conf~/src/or/config.c git-trunk-conf/src/or/config.c
  22. --- git-trunk-conf~/src/or/config.c 2008-03-11 15:23:01.000000000 +0100
  23. +++ git-trunk-conf/src/or/config.c 2008-03-11 15:30:34.139280421 +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. @@ -658,6 +659,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. @@ -3634,6 +3637,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. @@ -5051,3 +5057,64 @@
  49. puts(routerparse_c_id);
  50. }
  51. +/* Checks whether we are running as the debian-tor user.
  52. + * Returns -1 on error, 1 if we are debian-tor, 0 if not */
  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 1;
  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. + var = config_find_option(&options_format, "User");
  103. + tor_assert(var);
  104. + var->initvalue = tor_strdup("debian-tor");
  105. +
  106. + var = config_find_option(&options_format, "Group");
  107. + tor_assert(var);
  108. + var->initvalue = tor_strdup("debian-tor");
  109. +
  110. + return 0;
  111. +}