Randal L. Schwartz | 979e32f | 2005-10-25 16:29:09 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 2 | #include "pkt-line.h" |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 3 | #include "exec_cmd.h" |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 4 | #include "interpolate.h" |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 5 | |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 6 | #include <syslog.h> |
| 7 | |
Johannes Schindelin | 695dffe | 2006-09-28 12:00:35 +0200 | [diff] [blame] | 8 | #ifndef HOST_NAME_MAX |
| 9 | #define HOST_NAME_MAX 256 |
| 10 | #endif |
| 11 | |
Patrick Welche | 415e7b8 | 2007-10-18 18:17:39 +0100 | [diff] [blame] | 12 | #ifndef NI_MAXSERV |
| 13 | #define NI_MAXSERV 32 |
| 14 | #endif |
| 15 | |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 16 | static int log_syslog; |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 17 | static int verbose; |
Mark Wooding | 1955fab | 2006-02-03 20:27:04 +0000 | [diff] [blame] | 18 | static int reuseaddr; |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 19 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 20 | static const char daemon_usage[] = |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 21 | "git-daemon [--verbose] [--syslog] [--export-all]\n" |
Petr Baudis | b21c31c | 2005-12-23 02:27:40 +0100 | [diff] [blame] | 22 | " [--timeout=n] [--init-timeout=n] [--strict-paths]\n" |
Jens Axboe | 73a7a65 | 2007-07-27 14:00:29 -0700 | [diff] [blame] | 23 | " [--base-path=path] [--base-path-relaxed]\n" |
| 24 | " [--user-path | --user-path=path]\n" |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 25 | " [--interpolated-path=path]\n" |
Tilman Sauerbeck | 678dac6 | 2006-08-22 19:37:41 +0200 | [diff] [blame] | 26 | " [--reuseaddr] [--detach] [--pid-file=file]\n" |
Junio C Hamano | d9edcbd | 2006-09-07 01:40:04 -0700 | [diff] [blame] | 27 | " [--[enable|disable|allow-override|forbid-override]=service]\n" |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 28 | " [--inetd | [--listen=host_or_ipaddr] [--port=n]\n" |
| 29 | " [--user=user [--group=group]]\n" |
| 30 | " [directory...]"; |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 31 | |
| 32 | /* List of acceptable pathname prefixes */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 33 | static char **ok_paths; |
| 34 | static int strict_paths; |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 35 | |
| 36 | /* If this is set, git-daemon-export-ok is not required */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 37 | static int export_all_trees; |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 38 | |
Petr Baudis | b21c31c | 2005-12-23 02:27:40 +0100 | [diff] [blame] | 39 | /* Take all paths relative to this one if non-NULL */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 40 | static char *base_path; |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 41 | static char *interpolated_path; |
Jens Axboe | 73a7a65 | 2007-07-27 14:00:29 -0700 | [diff] [blame] | 42 | static int base_path_relaxed; |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 43 | |
| 44 | /* Flag indicating client sent extra args. */ |
| 45 | static int saw_extended_args; |
Petr Baudis | b21c31c | 2005-12-23 02:27:40 +0100 | [diff] [blame] | 46 | |
Junio C Hamano | 603968d | 2006-02-04 22:27:29 -0800 | [diff] [blame] | 47 | /* If defined, ~user notation is allowed and the string is inserted |
| 48 | * after ~user/. E.g. a request to git://host/~alice/frotz would |
| 49 | * go to /home/alice/pub_git/frotz with --user-path=pub_git. |
| 50 | */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 51 | static const char *user_path; |
Junio C Hamano | 603968d | 2006-02-04 22:27:29 -0800 | [diff] [blame] | 52 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 53 | /* Timeout, and initial timeout */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 54 | static unsigned int timeout; |
| 55 | static unsigned int init_timeout; |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 56 | |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 57 | /* |
| 58 | * Static table for now. Ugh. |
| 59 | * Feel free to make dynamic as needed. |
| 60 | */ |
| 61 | #define INTERP_SLOT_HOST (0) |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 62 | #define INTERP_SLOT_CANON_HOST (1) |
| 63 | #define INTERP_SLOT_IP (2) |
| 64 | #define INTERP_SLOT_PORT (3) |
| 65 | #define INTERP_SLOT_DIR (4) |
| 66 | #define INTERP_SLOT_PERCENT (5) |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 67 | |
| 68 | static struct interp interp_table[] = { |
| 69 | { "%H", 0}, |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 70 | { "%CH", 0}, |
| 71 | { "%IP", 0}, |
| 72 | { "%P", 0}, |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 73 | { "%D", 0}, |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 74 | { "%%", 0}, |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 78 | static void logreport(int priority, const char *err, va_list params) |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 79 | { |
| 80 | /* We should do a single write so that it is atomic and output |
| 81 | * of several processes do not get intermingled. */ |
| 82 | char buf[1024]; |
| 83 | int buflen; |
| 84 | int maxlen, msglen; |
| 85 | |
| 86 | /* sizeof(buf) should be big enough for "[pid] \n" */ |
Junio C Hamano | 1bedd4c | 2005-09-23 23:26:55 -0700 | [diff] [blame] | 87 | buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid()); |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 88 | |
| 89 | maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */ |
| 90 | msglen = vsnprintf(buf + buflen, maxlen, err, params); |
| 91 | |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 92 | if (log_syslog) { |
| 93 | syslog(priority, "%s", buf); |
| 94 | return; |
| 95 | } |
| 96 | |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 97 | /* maxlen counted our own LF but also counts space given to |
| 98 | * vsnprintf for the terminating NUL. We want to make sure that |
| 99 | * we have space for our own LF and NUL after the "meat" of the |
| 100 | * message, so truncate it at maxlen - 1. |
| 101 | */ |
| 102 | if (msglen > maxlen - 1) |
| 103 | msglen = maxlen - 1; |
| 104 | else if (msglen < 0) |
| 105 | msglen = 0; /* Protect against weird return values. */ |
| 106 | buflen += msglen; |
| 107 | |
| 108 | buf[buflen++] = '\n'; |
| 109 | buf[buflen] = '\0'; |
| 110 | |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 111 | write_in_full(2, buf, buflen); |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Pavel Roskin | cdda474 | 2005-09-29 16:53:14 -0400 | [diff] [blame] | 114 | static void logerror(const char *err, ...) |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 115 | { |
| 116 | va_list params; |
| 117 | va_start(params, err); |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 118 | logreport(LOG_ERR, err, params); |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 119 | va_end(params); |
| 120 | } |
| 121 | |
Pavel Roskin | cdda474 | 2005-09-29 16:53:14 -0400 | [diff] [blame] | 122 | static void loginfo(const char *err, ...) |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 123 | { |
| 124 | va_list params; |
| 125 | if (!verbose) |
| 126 | return; |
| 127 | va_start(params, err); |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 128 | logreport(LOG_INFO, err, params); |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 129 | va_end(params); |
| 130 | } |
| 131 | |
Matthias Lederhofer | ad8b4f5 | 2006-07-13 12:02:29 +0200 | [diff] [blame] | 132 | static void NORETURN daemon_die(const char *err, va_list params) |
| 133 | { |
| 134 | logreport(LOG_ERR, err, params); |
| 135 | exit(1); |
| 136 | } |
| 137 | |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 138 | static int avoid_alias(char *p) |
| 139 | { |
| 140 | int sl, ndot; |
| 141 | |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 142 | /* |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 143 | * This resurrects the belts and suspenders paranoia check by HPA |
| 144 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() |
| 145 | * does not do getcwd() based path canonicalizations. |
| 146 | * |
| 147 | * sl becomes true immediately after seeing '/' and continues to |
| 148 | * be true as long as dots continue after that without intervening |
| 149 | * non-dot character. |
| 150 | */ |
| 151 | if (!p || (*p != '/' && *p != '~')) |
| 152 | return -1; |
| 153 | sl = 1; ndot = 0; |
| 154 | p++; |
| 155 | |
| 156 | while (1) { |
| 157 | char ch = *p++; |
| 158 | if (sl) { |
| 159 | if (ch == '.') |
| 160 | ndot++; |
| 161 | else if (ch == '/') { |
| 162 | if (ndot < 3) |
| 163 | /* reject //, /./ and /../ */ |
| 164 | return -1; |
| 165 | ndot = 0; |
| 166 | } |
| 167 | else if (ch == 0) { |
| 168 | if (0 < ndot && ndot < 3) |
| 169 | /* reject /.$ and /..$ */ |
| 170 | return -1; |
| 171 | return 0; |
| 172 | } |
| 173 | else |
| 174 | sl = ndot = 0; |
| 175 | } |
| 176 | else if (ch == 0) |
| 177 | return 0; |
| 178 | else if (ch == '/') { |
| 179 | sl = 1; |
| 180 | ndot = 0; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 185 | static char *path_ok(struct interp *itable) |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 186 | { |
Junio C Hamano | 603968d | 2006-02-04 22:27:29 -0800 | [diff] [blame] | 187 | static char rpath[PATH_MAX]; |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 188 | static char interp_path[PATH_MAX]; |
Jens Axboe | 73a7a65 | 2007-07-27 14:00:29 -0700 | [diff] [blame] | 189 | int retried_path = 0; |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 190 | char *path; |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 191 | char *dir; |
| 192 | |
| 193 | dir = itable[INTERP_SLOT_DIR].value; |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 194 | |
| 195 | if (avoid_alias(dir)) { |
| 196 | logerror("'%s': aliased", dir); |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Junio C Hamano | 603968d | 2006-02-04 22:27:29 -0800 | [diff] [blame] | 200 | if (*dir == '~') { |
| 201 | if (!user_path) { |
| 202 | logerror("'%s': User-path not allowed", dir); |
| 203 | return NULL; |
| 204 | } |
| 205 | if (*user_path) { |
| 206 | /* Got either "~alice" or "~alice/foo"; |
| 207 | * rewrite them to "~alice/%s" or |
| 208 | * "~alice/%s/foo". |
| 209 | */ |
| 210 | int namlen, restlen = strlen(dir); |
| 211 | char *slash = strchr(dir, '/'); |
| 212 | if (!slash) |
| 213 | slash = dir + restlen; |
| 214 | namlen = slash - dir; |
| 215 | restlen -= namlen; |
| 216 | loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash); |
| 217 | snprintf(rpath, PATH_MAX, "%.*s/%s%.*s", |
| 218 | namlen, dir, user_path, restlen, slash); |
| 219 | dir = rpath; |
| 220 | } |
| 221 | } |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 222 | else if (interpolated_path && saw_extended_args) { |
| 223 | if (*dir != '/') { |
| 224 | /* Allow only absolute */ |
| 225 | logerror("'%s': Non-absolute path denied (interpolated-path active)", dir); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | interpolate(interp_path, PATH_MAX, interpolated_path, |
| 230 | interp_table, ARRAY_SIZE(interp_table)); |
| 231 | loginfo("Interpolated dir '%s'", interp_path); |
| 232 | |
| 233 | dir = interp_path; |
| 234 | } |
Junio C Hamano | 603968d | 2006-02-04 22:27:29 -0800 | [diff] [blame] | 235 | else if (base_path) { |
| 236 | if (*dir != '/') { |
| 237 | /* Allow only absolute */ |
Mark Wooding | 1fda3d5 | 2006-02-03 20:27:02 +0000 | [diff] [blame] | 238 | logerror("'%s': Non-absolute path denied (base-path active)", dir); |
Petr Baudis | b21c31c | 2005-12-23 02:27:40 +0100 | [diff] [blame] | 239 | return NULL; |
| 240 | } |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 241 | snprintf(rpath, PATH_MAX, "%s%s", base_path, dir); |
| 242 | dir = rpath; |
Petr Baudis | b21c31c | 2005-12-23 02:27:40 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Jens Axboe | 73a7a65 | 2007-07-27 14:00:29 -0700 | [diff] [blame] | 245 | do { |
| 246 | path = enter_repo(dir, strict_paths); |
| 247 | if (path) |
| 248 | break; |
| 249 | |
| 250 | /* |
| 251 | * if we fail and base_path_relaxed is enabled, try without |
| 252 | * prefixing the base path |
| 253 | */ |
| 254 | if (base_path && base_path_relaxed && !retried_path) { |
| 255 | dir = itable[INTERP_SLOT_DIR].value; |
| 256 | retried_path = 1; |
| 257 | continue; |
| 258 | } |
| 259 | break; |
| 260 | } while (1); |
H. Peter Anvin | 3e04c62 | 2005-10-18 18:26:52 -0700 | [diff] [blame] | 261 | |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 262 | if (!path) { |
| 263 | logerror("'%s': unable to chdir or not a git archive", dir); |
| 264 | return NULL; |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if ( ok_paths && *ok_paths ) { |
Junio C Hamano | ce335fe | 2005-11-21 01:21:18 -0800 | [diff] [blame] | 268 | char **pp; |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 269 | int pathlen = strlen(path); |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 270 | |
Junio C Hamano | ce335fe | 2005-11-21 01:21:18 -0800 | [diff] [blame] | 271 | /* The validation is done on the paths after enter_repo |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 272 | * appends optional {.git,.git/.git} and friends, but |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 273 | * it does not use getcwd(). So if your /pub is |
| 274 | * a symlink to /mnt/pub, you can whitelist /pub and |
| 275 | * do not have to say /mnt/pub. |
| 276 | * Do not say /pub/. |
Junio C Hamano | ce335fe | 2005-11-21 01:21:18 -0800 | [diff] [blame] | 277 | */ |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 278 | for ( pp = ok_paths ; *pp ; pp++ ) { |
| 279 | int len = strlen(*pp); |
Junio C Hamano | ce335fe | 2005-11-21 01:21:18 -0800 | [diff] [blame] | 280 | if (len <= pathlen && |
| 281 | !memcmp(*pp, path, len) && |
| 282 | (path[len] == '\0' || |
| 283 | (!strict_paths && path[len] == '/'))) |
| 284 | return path; |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 285 | } |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 286 | } |
| 287 | else { |
| 288 | /* be backwards compatible */ |
| 289 | if (!strict_paths) |
| 290 | return path; |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 293 | logerror("'%s': not in whitelist", path); |
| 294 | return NULL; /* Fallthrough. Deny by default */ |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 295 | } |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 296 | |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 297 | typedef int (*daemon_service_fn)(void); |
| 298 | struct daemon_service { |
| 299 | const char *name; |
| 300 | const char *config_name; |
| 301 | daemon_service_fn fn; |
| 302 | int enabled; |
| 303 | int overridable; |
| 304 | }; |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 305 | |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 306 | static struct daemon_service *service_looking_at; |
| 307 | static int service_enabled; |
| 308 | |
| 309 | static int git_daemon_config(const char *var, const char *value) |
| 310 | { |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 311 | if (!prefixcmp(var, "daemon.") && |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 312 | !strcmp(var + 7, service_looking_at->config_name)) { |
| 313 | service_enabled = git_config_bool(var, value); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* we are not interested in parsing any other configuration here */ |
| 318 | return 0; |
| 319 | } |
| 320 | |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 321 | static int run_service(struct interp *itable, struct daemon_service *service) |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 322 | { |
| 323 | const char *path; |
| 324 | int enabled = service->enabled; |
| 325 | |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 326 | loginfo("Request %s for '%s'", |
| 327 | service->name, |
| 328 | itable[INTERP_SLOT_DIR].value); |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 329 | |
| 330 | if (!enabled && !service->overridable) { |
| 331 | logerror("'%s': service not enabled.", service->name); |
| 332 | errno = EACCES; |
| 333 | return -1; |
| 334 | } |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 335 | |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 336 | if (!(path = path_ok(itable))) |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 337 | return -1; |
H. Peter Anvin | 47888f0 | 2005-09-27 08:49:40 -0700 | [diff] [blame] | 338 | |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 339 | /* |
| 340 | * Security on the cheap. |
| 341 | * |
Junio C Hamano | a935c39 | 2005-10-20 23:19:36 -0700 | [diff] [blame] | 342 | * We want a readable HEAD, usable "objects" directory, and |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 343 | * a "git-daemon-export-ok" flag that says that the other side |
| 344 | * is ok with us doing this. |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 345 | * |
| 346 | * path_ok() uses enter_repo() and does whitelist checking. |
| 347 | * We only need to make sure the repository is exported. |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 348 | */ |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 349 | |
H. Peter Anvin | 3e04c62 | 2005-10-18 18:26:52 -0700 | [diff] [blame] | 350 | if (!export_all_trees && access("git-daemon-export-ok", F_OK)) { |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 351 | logerror("'%s': repository not exported.", path); |
H. Peter Anvin | 3e04c62 | 2005-10-18 18:26:52 -0700 | [diff] [blame] | 352 | errno = EACCES; |
| 353 | return -1; |
| 354 | } |
| 355 | |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 356 | if (service->overridable) { |
| 357 | service_looking_at = service; |
| 358 | service_enabled = -1; |
| 359 | git_config(git_daemon_config); |
| 360 | if (0 <= service_enabled) |
| 361 | enabled = service_enabled; |
| 362 | } |
| 363 | if (!enabled) { |
| 364 | logerror("'%s': service not enabled for '%s'", |
| 365 | service->name, path); |
| 366 | errno = EACCES; |
| 367 | return -1; |
| 368 | } |
| 369 | |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 370 | /* |
| 371 | * We'll ignore SIGTERM from now on, we have a |
| 372 | * good client. |
| 373 | */ |
| 374 | signal(SIGTERM, SIG_IGN); |
| 375 | |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 376 | return service->fn(); |
| 377 | } |
| 378 | |
| 379 | static int upload_pack(void) |
| 380 | { |
| 381 | /* Timeout as string */ |
| 382 | char timeout_buf[64]; |
| 383 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 384 | snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout); |
| 385 | |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 386 | /* git-upload-pack only ever reads stuff, so this is safe */ |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 387 | execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL); |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 388 | return -1; |
| 389 | } |
| 390 | |
Franck Bui-Huu | 39345a2 | 2006-09-07 15:12:05 +0200 | [diff] [blame] | 391 | static int upload_archive(void) |
| 392 | { |
| 393 | execl_git_cmd("upload-archive", ".", NULL); |
| 394 | return -1; |
| 395 | } |
| 396 | |
Linus Torvalds | 4b3b1e1 | 2007-01-21 11:04:13 -0800 | [diff] [blame] | 397 | static int receive_pack(void) |
| 398 | { |
| 399 | execl_git_cmd("receive-pack", ".", NULL); |
| 400 | return -1; |
| 401 | } |
| 402 | |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 403 | static struct daemon_service daemon_service[] = { |
Franck Bui-Huu | 39345a2 | 2006-09-07 15:12:05 +0200 | [diff] [blame] | 404 | { "upload-archive", "uploadarch", upload_archive, 0, 1 }, |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 405 | { "upload-pack", "uploadpack", upload_pack, 1, 1 }, |
Linus Torvalds | 4b3b1e1 | 2007-01-21 11:04:13 -0800 | [diff] [blame] | 406 | { "receive-pack", "receivepack", receive_pack, 0, 1 }, |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 407 | }; |
| 408 | |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 409 | static void enable_service(const char *name, int ena) |
| 410 | { |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 411 | int i; |
| 412 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { |
| 413 | if (!strcmp(daemon_service[i].name, name)) { |
| 414 | daemon_service[i].enabled = ena; |
| 415 | return; |
| 416 | } |
| 417 | } |
| 418 | die("No such service %s", name); |
| 419 | } |
| 420 | |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 421 | static void make_service_overridable(const char *name, int ena) |
| 422 | { |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 423 | int i; |
| 424 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { |
| 425 | if (!strcmp(daemon_service[i].name, name)) { |
| 426 | daemon_service[i].overridable = ena; |
| 427 | return; |
| 428 | } |
| 429 | } |
| 430 | die("No such service %s", name); |
| 431 | } |
| 432 | |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 433 | /* |
| 434 | * Separate the "extra args" information as supplied by the client connection. |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 435 | * Any resulting data is squirreled away in the given interpolation table. |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 436 | */ |
| 437 | static void parse_extra_args(struct interp *table, char *extra_args, int buflen) |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 438 | { |
| 439 | char *val; |
| 440 | int vallen; |
| 441 | char *end = extra_args + buflen; |
| 442 | |
| 443 | while (extra_args < end && *extra_args) { |
| 444 | saw_extended_args = 1; |
| 445 | if (strncasecmp("host=", extra_args, 5) == 0) { |
| 446 | val = extra_args + 5; |
| 447 | vallen = strlen(val) + 1; |
| 448 | if (*val) { |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 449 | /* Split <host>:<port> at colon. */ |
| 450 | char *host = val; |
| 451 | char *port = strrchr(host, ':'); |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 452 | if (port) { |
| 453 | *port = 0; |
| 454 | port++; |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 455 | interp_set_entry(table, INTERP_SLOT_PORT, port); |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 456 | } |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 457 | interp_set_entry(table, INTERP_SLOT_HOST, host); |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 458 | } |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 459 | |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 460 | /* On to the next one */ |
| 461 | extra_args = val + vallen; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
Pierre Habouzit | 52fae7d | 2007-06-07 22:45:00 +0200 | [diff] [blame] | 466 | static void fill_in_extra_table_entries(struct interp *itable) |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 467 | { |
| 468 | char *hp; |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 469 | |
| 470 | /* |
| 471 | * Replace literal host with lowercase-ized hostname. |
| 472 | */ |
| 473 | hp = interp_table[INTERP_SLOT_HOST].value; |
Junio C Hamano | 83543a2 | 2006-10-23 18:26:05 -0700 | [diff] [blame] | 474 | if (!hp) |
| 475 | return; |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 476 | for ( ; *hp; hp++) |
| 477 | *hp = tolower(*hp); |
| 478 | |
| 479 | /* |
| 480 | * Locate canonical hostname and its IP address. |
| 481 | */ |
| 482 | #ifndef NO_IPV6 |
| 483 | { |
| 484 | struct addrinfo hints; |
| 485 | struct addrinfo *ai, *ai0; |
| 486 | int gai; |
| 487 | static char addrbuf[HOST_NAME_MAX + 1]; |
| 488 | |
| 489 | memset(&hints, 0, sizeof(hints)); |
| 490 | hints.ai_flags = AI_CANONNAME; |
| 491 | |
| 492 | gai = getaddrinfo(interp_table[INTERP_SLOT_HOST].value, 0, &hints, &ai0); |
| 493 | if (!gai) { |
| 494 | for (ai = ai0; ai; ai = ai->ai_next) { |
| 495 | struct sockaddr_in *sin_addr = (void *)ai->ai_addr; |
| 496 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 497 | inet_ntop(AF_INET, &sin_addr->sin_addr, |
| 498 | addrbuf, sizeof(addrbuf)); |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 499 | interp_set_entry(interp_table, |
| 500 | INTERP_SLOT_CANON_HOST, ai->ai_canonname); |
| 501 | interp_set_entry(interp_table, |
| 502 | INTERP_SLOT_IP, addrbuf); |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | freeaddrinfo(ai0); |
| 506 | } |
| 507 | } |
| 508 | #else |
| 509 | { |
| 510 | struct hostent *hent; |
| 511 | struct sockaddr_in sa; |
| 512 | char **ap; |
| 513 | static char addrbuf[HOST_NAME_MAX + 1]; |
| 514 | |
| 515 | hent = gethostbyname(interp_table[INTERP_SLOT_HOST].value); |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 516 | |
| 517 | ap = hent->h_addr_list; |
| 518 | memset(&sa, 0, sizeof sa); |
| 519 | sa.sin_family = hent->h_addrtype; |
| 520 | sa.sin_port = htons(0); |
| 521 | memcpy(&sa.sin_addr, *ap, hent->h_length); |
| 522 | |
| 523 | inet_ntop(hent->h_addrtype, &sa.sin_addr, |
| 524 | addrbuf, sizeof(addrbuf)); |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 525 | |
| 526 | interp_set_entry(interp_table, INTERP_SLOT_CANON_HOST, hent->h_name); |
| 527 | interp_set_entry(interp_table, INTERP_SLOT_IP, addrbuf); |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 528 | } |
| 529 | #endif |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
David Woodhouse | 5b276ee | 2006-06-20 15:38:13 +0100 | [diff] [blame] | 533 | static int execute(struct sockaddr *addr) |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 534 | { |
Linus Torvalds | 7d80694 | 2005-07-15 09:27:05 -0700 | [diff] [blame] | 535 | static char line[1000]; |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 536 | int pktlen, len, i; |
Linus Torvalds | 7d80694 | 2005-07-15 09:27:05 -0700 | [diff] [blame] | 537 | |
David Woodhouse | 5b276ee | 2006-06-20 15:38:13 +0100 | [diff] [blame] | 538 | if (addr) { |
| 539 | char addrbuf[256] = ""; |
| 540 | int port = -1; |
| 541 | |
| 542 | if (addr->sa_family == AF_INET) { |
| 543 | struct sockaddr_in *sin_addr = (void *) addr; |
| 544 | inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf)); |
Gerrit Pape | c67359b | 2007-11-05 09:16:22 +0000 | [diff] [blame] | 545 | port = ntohs(sin_addr->sin_port); |
David Woodhouse | 5b276ee | 2006-06-20 15:38:13 +0100 | [diff] [blame] | 546 | #ifndef NO_IPV6 |
| 547 | } else if (addr && addr->sa_family == AF_INET6) { |
| 548 | struct sockaddr_in6 *sin6_addr = (void *) addr; |
| 549 | |
| 550 | char *buf = addrbuf; |
| 551 | *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */ |
| 552 | inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1); |
| 553 | strcat(buf, "]"); |
| 554 | |
Gerrit Pape | c67359b | 2007-11-05 09:16:22 +0000 | [diff] [blame] | 555 | port = ntohs(sin6_addr->sin6_port); |
David Woodhouse | 5b276ee | 2006-06-20 15:38:13 +0100 | [diff] [blame] | 556 | #endif |
| 557 | } |
| 558 | loginfo("Connection from %s:%d", addrbuf, port); |
| 559 | } |
| 560 | |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 561 | alarm(init_timeout ? init_timeout : timeout); |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 562 | pktlen = packet_read_line(0, line, sizeof(line)); |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 563 | alarm(0); |
Linus Torvalds | 7d80694 | 2005-07-15 09:27:05 -0700 | [diff] [blame] | 564 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 565 | len = strlen(line); |
| 566 | if (pktlen != len) |
| 567 | loginfo("Extended attributes (%d bytes) exist <%.*s>", |
| 568 | (int) pktlen - len, |
| 569 | (int) pktlen - len, line + len + 1); |
Junio C Hamano | 83543a2 | 2006-10-23 18:26:05 -0700 | [diff] [blame] | 570 | if (len && line[len-1] == '\n') { |
Linus Torvalds | 7d80694 | 2005-07-15 09:27:05 -0700 | [diff] [blame] | 571 | line[--len] = 0; |
Junio C Hamano | 83543a2 | 2006-10-23 18:26:05 -0700 | [diff] [blame] | 572 | pktlen--; |
| 573 | } |
Linus Torvalds | 7d80694 | 2005-07-15 09:27:05 -0700 | [diff] [blame] | 574 | |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 575 | /* |
| 576 | * Initialize the path interpolation table for this connection. |
| 577 | */ |
| 578 | interp_clear_table(interp_table, ARRAY_SIZE(interp_table)); |
| 579 | interp_set_entry(interp_table, INTERP_SLOT_PERCENT, "%"); |
| 580 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 581 | if (len != pktlen) { |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 582 | parse_extra_args(interp_table, line + len + 1, pktlen - len - 1); |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 583 | fill_in_extra_table_entries(interp_table); |
| 584 | } |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 585 | |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 586 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { |
| 587 | struct daemon_service *s = &(daemon_service[i]); |
| 588 | int namelen = strlen(s->name); |
Junio C Hamano | 599065a | 2007-02-20 01:54:00 -0800 | [diff] [blame] | 589 | if (!prefixcmp(line, "git-") && |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 590 | !strncmp(s->name, line + 4, namelen) && |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 591 | line[namelen + 4] == ' ') { |
Jon Loeliger | eb30aed | 2006-09-27 11:16:10 -0500 | [diff] [blame] | 592 | /* |
| 593 | * Note: The directory here is probably context sensitive, |
| 594 | * and might depend on the actual service being performed. |
| 595 | */ |
| 596 | interp_set_entry(interp_table, |
| 597 | INTERP_SLOT_DIR, line + namelen + 5); |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 598 | return run_service(interp_table, s); |
| 599 | } |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 600 | } |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 601 | |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 602 | logerror("Protocol error: '%s'", line); |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 603 | return -1; |
| 604 | } |
| 605 | |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 606 | |
| 607 | /* |
| 608 | * We count spawned/reaped separately, just to avoid any |
| 609 | * races when updating them from signals. The SIGCHLD handler |
| 610 | * will only update children_reaped, and the fork logic will |
| 611 | * only update children_spawned. |
| 612 | * |
| 613 | * MAX_CHILDREN should be a power-of-two to make the modulus |
| 614 | * operation cheap. It should also be at least twice |
| 615 | * the maximum number of connections we will ever allow. |
| 616 | */ |
| 617 | #define MAX_CHILDREN 128 |
| 618 | |
| 619 | static int max_connections = 25; |
| 620 | |
| 621 | /* These are updated by the signal handler */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 622 | static volatile unsigned int children_reaped; |
Linus Torvalds | 4d8fa91 | 2005-08-01 12:11:53 -0700 | [diff] [blame] | 623 | static pid_t dead_child[MAX_CHILDREN]; |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 624 | |
| 625 | /* These are updated by the main loop */ |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 626 | static unsigned int children_spawned; |
| 627 | static unsigned int children_deleted; |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 628 | |
Linus Torvalds | 4d8fa91 | 2005-08-01 12:11:53 -0700 | [diff] [blame] | 629 | static struct child { |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 630 | pid_t pid; |
Junio C Hamano | 7fa0908 | 2005-09-11 13:58:41 -0700 | [diff] [blame] | 631 | int addrlen; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 632 | struct sockaddr_storage address; |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 633 | } live_child[MAX_CHILDREN]; |
| 634 | |
Junio C Hamano | 7fa0908 | 2005-09-11 13:58:41 -0700 | [diff] [blame] | 635 | static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen) |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 636 | { |
| 637 | live_child[idx].pid = pid; |
| 638 | live_child[idx].addrlen = addrlen; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 639 | memcpy(&live_child[idx].address, addr, addrlen); |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Walk from "deleted" to "spawned", and remove child "pid". |
| 644 | * |
| 645 | * We move everything up by one, since the new "deleted" will |
| 646 | * be one higher. |
| 647 | */ |
| 648 | static void remove_child(pid_t pid, unsigned deleted, unsigned spawned) |
| 649 | { |
| 650 | struct child n; |
| 651 | |
| 652 | deleted %= MAX_CHILDREN; |
| 653 | spawned %= MAX_CHILDREN; |
| 654 | if (live_child[deleted].pid == pid) { |
| 655 | live_child[deleted].pid = -1; |
| 656 | return; |
| 657 | } |
| 658 | n = live_child[deleted]; |
| 659 | for (;;) { |
| 660 | struct child m; |
| 661 | deleted = (deleted + 1) % MAX_CHILDREN; |
| 662 | if (deleted == spawned) |
| 663 | die("could not find dead child %d\n", pid); |
| 664 | m = live_child[deleted]; |
| 665 | live_child[deleted] = n; |
| 666 | if (m.pid == pid) |
| 667 | return; |
| 668 | n = m; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * This gets called if the number of connections grows |
| 674 | * past "max_connections". |
| 675 | * |
| 676 | * We _should_ start off by searching for connections |
| 677 | * from the same IP, and if there is some address wth |
| 678 | * multiple connections, we should kill that first. |
| 679 | * |
| 680 | * As it is, we just "randomly" kill 25% of the connections, |
| 681 | * and our pseudo-random generator sucks too. I have no |
| 682 | * shame. |
| 683 | * |
| 684 | * Really, this is just a place-holder for a _real_ algorithm. |
| 685 | */ |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 686 | static void kill_some_children(int signo, unsigned start, unsigned stop) |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 687 | { |
| 688 | start %= MAX_CHILDREN; |
| 689 | stop %= MAX_CHILDREN; |
| 690 | while (start != stop) { |
| 691 | if (!(start & 3)) |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 692 | kill(live_child[start].pid, signo); |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 693 | start = (start + 1) % MAX_CHILDREN; |
| 694 | } |
| 695 | } |
| 696 | |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 697 | static void check_max_connections(void) |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 698 | { |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 699 | for (;;) { |
Linus Torvalds | eaa9491 | 2005-07-15 20:42:28 -0700 | [diff] [blame] | 700 | int active; |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 701 | unsigned spawned, reaped, deleted; |
Linus Torvalds | eaa9491 | 2005-07-15 20:42:28 -0700 | [diff] [blame] | 702 | |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 703 | spawned = children_spawned; |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 704 | reaped = children_reaped; |
| 705 | deleted = children_deleted; |
| 706 | |
| 707 | while (deleted < reaped) { |
| 708 | pid_t pid = dead_child[deleted % MAX_CHILDREN]; |
| 709 | remove_child(pid, deleted, spawned); |
| 710 | deleted++; |
Linus Torvalds | eaa9491 | 2005-07-15 20:42:28 -0700 | [diff] [blame] | 711 | } |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 712 | children_deleted = deleted; |
| 713 | |
| 714 | active = spawned - deleted; |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 715 | if (active <= max_connections) |
| 716 | break; |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 717 | |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 718 | /* Kill some unstarted connections with SIGTERM */ |
| 719 | kill_some_children(SIGTERM, deleted, spawned); |
| 720 | if (active <= max_connections << 1) |
| 721 | break; |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 722 | |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 723 | /* If the SIGTERM thing isn't helping use SIGKILL */ |
| 724 | kill_some_children(SIGKILL, deleted, spawned); |
| 725 | sleep(1); |
| 726 | } |
| 727 | } |
| 728 | |
Junio C Hamano | 7fa0908 | 2005-09-11 13:58:41 -0700 | [diff] [blame] | 729 | static void handle(int incoming, struct sockaddr *addr, int addrlen) |
Linus Torvalds | 02d57da | 2005-07-15 22:53:31 -0700 | [diff] [blame] | 730 | { |
| 731 | pid_t pid = fork(); |
| 732 | |
| 733 | if (pid) { |
| 734 | unsigned idx; |
| 735 | |
| 736 | close(incoming); |
| 737 | if (pid < 0) |
| 738 | return; |
| 739 | |
| 740 | idx = children_spawned % MAX_CHILDREN; |
| 741 | children_spawned++; |
| 742 | add_child(idx, pid, addr, addrlen); |
| 743 | |
| 744 | check_max_connections(); |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 745 | return; |
| 746 | } |
| 747 | |
| 748 | dup2(incoming, 0); |
| 749 | dup2(incoming, 1); |
| 750 | close(incoming); |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 751 | |
David Woodhouse | 5b276ee | 2006-06-20 15:38:13 +0100 | [diff] [blame] | 752 | exit(execute(addr)); |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Linus Torvalds | eaa9491 | 2005-07-15 20:42:28 -0700 | [diff] [blame] | 755 | static void child_handler(int signo) |
| 756 | { |
| 757 | for (;;) { |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 758 | int status; |
| 759 | pid_t pid = waitpid(-1, &status, WNOHANG); |
Linus Torvalds | 66e631d | 2005-07-15 21:51:57 -0700 | [diff] [blame] | 760 | |
| 761 | if (pid > 0) { |
| 762 | unsigned reaped = children_reaped; |
| 763 | dead_child[reaped % MAX_CHILDREN] = pid; |
| 764 | children_reaped = reaped + 1; |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 765 | /* XXX: Custom logging, since we don't wanna getpid() */ |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 766 | if (verbose) { |
Timo Hirvonen | 554fe20 | 2006-06-28 12:04:39 +0300 | [diff] [blame] | 767 | const char *dead = ""; |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 768 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) |
| 769 | dead = " (with error)"; |
| 770 | if (log_syslog) |
| 771 | syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead); |
| 772 | else |
| 773 | fprintf(stderr, "[%d] Disconnected%s\n", pid, dead); |
| 774 | } |
Linus Torvalds | eaa9491 | 2005-07-15 20:42:28 -0700 | [diff] [blame] | 775 | continue; |
| 776 | } |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | |
Mark Wooding | 1955fab | 2006-02-03 20:27:04 +0000 | [diff] [blame] | 781 | static int set_reuse_addr(int sockfd) |
| 782 | { |
| 783 | int on = 1; |
| 784 | |
| 785 | if (!reuseaddr) |
| 786 | return 0; |
| 787 | return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, |
| 788 | &on, sizeof(on)); |
| 789 | } |
| 790 | |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 791 | #ifndef NO_IPV6 |
| 792 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 793 | static int socksetup(char *listen_addr, int listen_port, int **socklist_p) |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 794 | { |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 795 | int socknum = 0, *socklist = NULL; |
| 796 | int maxfd = -1; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 797 | char pbuf[NI_MAXSERV]; |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 798 | struct addrinfo hints, *ai0, *ai; |
| 799 | int gai; |
Alexandre Julliard | 2027688 | 2007-02-14 18:10:26 +0100 | [diff] [blame] | 800 | long flags; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 801 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 802 | sprintf(pbuf, "%d", listen_port); |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 803 | memset(&hints, 0, sizeof(hints)); |
| 804 | hints.ai_family = AF_UNSPEC; |
| 805 | hints.ai_socktype = SOCK_STREAM; |
| 806 | hints.ai_protocol = IPPROTO_TCP; |
| 807 | hints.ai_flags = AI_PASSIVE; |
| 808 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 809 | gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0); |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 810 | if (gai) |
| 811 | die("getaddrinfo() failed: %s\n", gai_strerror(gai)); |
| 812 | |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 813 | for (ai = ai0; ai; ai = ai->ai_next) { |
| 814 | int sockfd; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 815 | |
| 816 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 817 | if (sockfd < 0) |
| 818 | continue; |
| 819 | if (sockfd >= FD_SETSIZE) { |
| 820 | error("too large socket descriptor."); |
| 821 | close(sockfd); |
| 822 | continue; |
| 823 | } |
| 824 | |
| 825 | #ifdef IPV6_V6ONLY |
| 826 | if (ai->ai_family == AF_INET6) { |
| 827 | int on = 1; |
| 828 | setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, |
| 829 | &on, sizeof(on)); |
| 830 | /* Note: error is not fatal */ |
| 831 | } |
| 832 | #endif |
| 833 | |
Mark Wooding | 1955fab | 2006-02-03 20:27:04 +0000 | [diff] [blame] | 834 | if (set_reuse_addr(sockfd)) { |
| 835 | close(sockfd); |
Serge E. Hallyn | 0032d54 | 2006-04-18 08:11:06 -0500 | [diff] [blame] | 836 | continue; |
Mark Wooding | 1955fab | 2006-02-03 20:27:04 +0000 | [diff] [blame] | 837 | } |
| 838 | |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 839 | if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 840 | close(sockfd); |
| 841 | continue; /* not fatal */ |
| 842 | } |
| 843 | if (listen(sockfd, 5) < 0) { |
| 844 | close(sockfd); |
| 845 | continue; /* not fatal */ |
| 846 | } |
| 847 | |
Alexandre Julliard | 2027688 | 2007-02-14 18:10:26 +0100 | [diff] [blame] | 848 | flags = fcntl(sockfd, F_GETFD, 0); |
| 849 | if (flags >= 0) |
| 850 | fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC); |
| 851 | |
Jonas Fonseca | 83572c1 | 2006-08-26 16:16:18 +0200 | [diff] [blame] | 852 | socklist = xrealloc(socklist, sizeof(int) * (socknum + 1)); |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 853 | socklist[socknum++] = sockfd; |
| 854 | |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 855 | if (maxfd < sockfd) |
| 856 | maxfd = sockfd; |
| 857 | } |
| 858 | |
| 859 | freeaddrinfo(ai0); |
| 860 | |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 861 | *socklist_p = socklist; |
| 862 | return socknum; |
| 863 | } |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 864 | |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 865 | #else /* NO_IPV6 */ |
| 866 | |
Alex Riesen | 100690b | 2006-09-28 20:48:14 +0200 | [diff] [blame] | 867 | static int socksetup(char *listen_addr, int listen_port, int **socklist_p) |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 868 | { |
| 869 | struct sockaddr_in sin; |
| 870 | int sockfd; |
Alexandre Julliard | 2027688 | 2007-02-14 18:10:26 +0100 | [diff] [blame] | 871 | long flags; |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 872 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 873 | memset(&sin, 0, sizeof sin); |
| 874 | sin.sin_family = AF_INET; |
| 875 | sin.sin_port = htons(listen_port); |
| 876 | |
| 877 | if (listen_addr) { |
| 878 | /* Well, host better be an IP address here. */ |
| 879 | if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0) |
| 880 | return 0; |
| 881 | } else { |
| 882 | sin.sin_addr.s_addr = htonl(INADDR_ANY); |
| 883 | } |
| 884 | |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 885 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 886 | if (sockfd < 0) |
| 887 | return 0; |
| 888 | |
Mark Wooding | 1955fab | 2006-02-03 20:27:04 +0000 | [diff] [blame] | 889 | if (set_reuse_addr(sockfd)) { |
| 890 | close(sockfd); |
| 891 | return 0; |
| 892 | } |
| 893 | |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 894 | if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) { |
| 895 | close(sockfd); |
| 896 | return 0; |
| 897 | } |
| 898 | |
Paul Serice | f35230f | 2005-11-21 11:07:23 -0600 | [diff] [blame] | 899 | if (listen(sockfd, 5) < 0) { |
| 900 | close(sockfd); |
| 901 | return 0; |
| 902 | } |
| 903 | |
Alexandre Julliard | 2027688 | 2007-02-14 18:10:26 +0100 | [diff] [blame] | 904 | flags = fcntl(sockfd, F_GETFD, 0); |
| 905 | if (flags >= 0) |
| 906 | fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC); |
| 907 | |
H. Peter Anvin | 1b4713f | 2005-09-30 10:47:50 -0700 | [diff] [blame] | 908 | *socklist_p = xmalloc(sizeof(int)); |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 909 | **socklist_p = sockfd; |
Paul Serice | f35230f | 2005-11-21 11:07:23 -0600 | [diff] [blame] | 910 | return 1; |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | #endif |
| 914 | |
| 915 | static int service_loop(int socknum, int *socklist) |
| 916 | { |
| 917 | struct pollfd *pfd; |
| 918 | int i; |
| 919 | |
H. Peter Anvin | 1b4713f | 2005-09-30 10:47:50 -0700 | [diff] [blame] | 920 | pfd = xcalloc(socknum, sizeof(struct pollfd)); |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 921 | |
| 922 | for (i = 0; i < socknum; i++) { |
| 923 | pfd[i].fd = socklist[i]; |
| 924 | pfd[i].events = POLLIN; |
| 925 | } |
H. Peter Anvin | 9220282 | 2005-09-30 11:01:57 -0700 | [diff] [blame] | 926 | |
| 927 | signal(SIGCHLD, child_handler); |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 928 | |
| 929 | for (;;) { |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 930 | int i; |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 931 | |
Jens Axboe | 7872e05 | 2005-10-20 00:52:32 -0700 | [diff] [blame] | 932 | if (poll(pfd, socknum, -1) < 0) { |
Junio C Hamano | 1eef0b3 | 2005-07-26 13:26:52 -0700 | [diff] [blame] | 933 | if (errno != EINTR) { |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 934 | error("poll failed, resuming: %s", |
Junio C Hamano | 1eef0b3 | 2005-07-26 13:26:52 -0700 | [diff] [blame] | 935 | strerror(errno)); |
| 936 | sleep(1); |
| 937 | } |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 938 | continue; |
| 939 | } |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 940 | |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 941 | for (i = 0; i < socknum; i++) { |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 942 | if (pfd[i].revents & POLLIN) { |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 943 | struct sockaddr_storage ss; |
H. Peter Anvin | 7626e49 | 2005-09-30 10:48:21 -0700 | [diff] [blame] | 944 | unsigned int sslen = sizeof(ss); |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 945 | int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen); |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 04:24:59 -0400 | [diff] [blame] | 946 | if (incoming < 0) { |
| 947 | switch (errno) { |
| 948 | case EAGAIN: |
| 949 | case EINTR: |
| 950 | case ECONNABORTED: |
| 951 | continue; |
| 952 | default: |
| 953 | die("accept returned %s", strerror(errno)); |
| 954 | } |
| 955 | } |
| 956 | handle(incoming, (struct sockaddr *)&ss, sslen); |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 957 | } |
| 958 | } |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
Matthias Lederhofer | 258e93a | 2006-07-13 18:32:11 +0200 | [diff] [blame] | 962 | /* if any standard file descriptor is missing open it to /dev/null */ |
| 963 | static void sanitize_stdfds(void) |
| 964 | { |
| 965 | int fd = open("/dev/null", O_RDWR, 0); |
| 966 | while (fd != -1 && fd < 2) |
| 967 | fd = dup(fd); |
| 968 | if (fd == -1) |
| 969 | die("open /dev/null or dup failed: %s", strerror(errno)); |
| 970 | if (fd > 2) |
| 971 | close(fd); |
| 972 | } |
| 973 | |
Matthias Lederhofer | a526276 | 2006-07-13 18:47:13 +0200 | [diff] [blame] | 974 | static void daemonize(void) |
| 975 | { |
| 976 | switch (fork()) { |
| 977 | case 0: |
| 978 | break; |
| 979 | case -1: |
| 980 | die("fork failed: %s", strerror(errno)); |
| 981 | default: |
| 982 | exit(0); |
| 983 | } |
| 984 | if (setsid() == -1) |
| 985 | die("setsid failed: %s", strerror(errno)); |
| 986 | close(0); |
| 987 | close(1); |
| 988 | close(2); |
| 989 | sanitize_stdfds(); |
| 990 | } |
| 991 | |
Matthias Lederhofer | 45ed5d7 | 2006-07-13 12:18:08 +0200 | [diff] [blame] | 992 | static void store_pid(const char *path) |
| 993 | { |
| 994 | FILE *f = fopen(path, "w"); |
| 995 | if (!f) |
| 996 | die("cannot open pid file %s: %s", path, strerror(errno)); |
Jim Meyering | bc4e7d0 | 2007-05-21 09:58:01 +0200 | [diff] [blame] | 997 | if (fprintf(f, "%d\n", getpid()) < 0 || fclose(f) != 0) |
| 998 | die("failed to write pid file %s: %s", path, strerror(errno)); |
Matthias Lederhofer | 45ed5d7 | 2006-07-13 12:18:08 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 1001 | static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t gid) |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 1002 | { |
| 1003 | int socknum, *socklist; |
Junio C Hamano | 4ae22d9 | 2005-10-20 23:21:50 -0700 | [diff] [blame] | 1004 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 1005 | socknum = socksetup(listen_addr, listen_port, &socklist); |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 1006 | if (socknum == 0) |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 1007 | die("unable to allocate any listen sockets on host %s port %u", |
| 1008 | listen_addr, listen_port); |
Junio C Hamano | 4ae22d9 | 2005-10-20 23:21:50 -0700 | [diff] [blame] | 1009 | |
Tilman Sauerbeck | 678dac6 | 2006-08-22 19:37:41 +0200 | [diff] [blame] | 1010 | if (pass && gid && |
| 1011 | (initgroups(pass->pw_name, gid) || setgid (gid) || |
| 1012 | setuid(pass->pw_uid))) |
| 1013 | die("cannot drop privileges"); |
| 1014 | |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 1015 | return service_loop(socknum, socklist); |
Junio C Hamano | 4ae22d9 | 2005-10-20 23:21:50 -0700 | [diff] [blame] | 1016 | } |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 1017 | |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 1018 | int main(int argc, char **argv) |
| 1019 | { |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 1020 | int listen_port = 0; |
| 1021 | char *listen_addr = NULL; |
Linus Torvalds | e64e1b7 | 2005-07-15 09:32:16 -0700 | [diff] [blame] | 1022 | int inetd_mode = 0; |
Tilman Sauerbeck | 678dac6 | 2006-08-22 19:37:41 +0200 | [diff] [blame] | 1023 | const char *pid_file = NULL, *user_name = NULL, *group_name = NULL; |
Matthias Lederhofer | a526276 | 2006-07-13 18:47:13 +0200 | [diff] [blame] | 1024 | int detach = 0; |
Tilman Sauerbeck | 678dac6 | 2006-08-22 19:37:41 +0200 | [diff] [blame] | 1025 | struct passwd *pass = NULL; |
| 1026 | struct group *group; |
| 1027 | gid_t gid = 0; |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 1028 | int i; |
| 1029 | |
Junio C Hamano | f0b7367 | 2006-06-19 18:25:21 -0700 | [diff] [blame] | 1030 | /* Without this we cannot rely on waitpid() to tell |
| 1031 | * what happened to our children. |
| 1032 | */ |
| 1033 | signal(SIGCHLD, SIG_DFL); |
| 1034 | |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 1035 | for (i = 1; i < argc; i++) { |
| 1036 | char *arg = argv[i]; |
| 1037 | |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1038 | if (!prefixcmp(arg, "--listen=")) { |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 1039 | char *p = arg + 9; |
| 1040 | char *ph = listen_addr = xmalloc(strlen(arg + 9) + 1); |
| 1041 | while (*p) |
| 1042 | *ph++ = tolower(*p++); |
| 1043 | *ph = 0; |
| 1044 | continue; |
| 1045 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1046 | if (!prefixcmp(arg, "--port=")) { |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 1047 | char *end; |
| 1048 | unsigned long n; |
| 1049 | n = strtoul(arg+7, &end, 0); |
| 1050 | if (arg[7] && !*end) { |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 1051 | listen_port = n; |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 1052 | continue; |
| 1053 | } |
| 1054 | } |
Linus Torvalds | e64e1b7 | 2005-07-15 09:32:16 -0700 | [diff] [blame] | 1055 | if (!strcmp(arg, "--inetd")) { |
| 1056 | inetd_mode = 1; |
Andreas Ericsson | a888328 | 2005-11-17 00:38:29 +0100 | [diff] [blame] | 1057 | log_syslog = 1; |
Linus Torvalds | e64e1b7 | 2005-07-15 09:32:16 -0700 | [diff] [blame] | 1058 | continue; |
| 1059 | } |
Petr Baudis | f8ff0c0 | 2005-09-22 11:25:28 +0200 | [diff] [blame] | 1060 | if (!strcmp(arg, "--verbose")) { |
| 1061 | verbose = 1; |
| 1062 | continue; |
| 1063 | } |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 1064 | if (!strcmp(arg, "--syslog")) { |
| 1065 | log_syslog = 1; |
Petr Baudis | 9048fe1 | 2005-09-24 16:13:01 +0200 | [diff] [blame] | 1066 | continue; |
| 1067 | } |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 1068 | if (!strcmp(arg, "--export-all")) { |
| 1069 | export_all_trees = 1; |
| 1070 | continue; |
| 1071 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1072 | if (!prefixcmp(arg, "--timeout=")) { |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 1073 | timeout = atoi(arg+10); |
Andreas Ericsson | a888328 | 2005-11-17 00:38:29 +0100 | [diff] [blame] | 1074 | continue; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 1075 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1076 | if (!prefixcmp(arg, "--init-timeout=")) { |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 1077 | init_timeout = atoi(arg+15); |
Andreas Ericsson | a888328 | 2005-11-17 00:38:29 +0100 | [diff] [blame] | 1078 | continue; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 1079 | } |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1080 | if (!strcmp(arg, "--strict-paths")) { |
| 1081 | strict_paths = 1; |
| 1082 | continue; |
| 1083 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1084 | if (!prefixcmp(arg, "--base-path=")) { |
Petr Baudis | b21c31c | 2005-12-23 02:27:40 +0100 | [diff] [blame] | 1085 | base_path = arg+12; |
| 1086 | continue; |
| 1087 | } |
Jens Axboe | 73a7a65 | 2007-07-27 14:00:29 -0700 | [diff] [blame] | 1088 | if (!strcmp(arg, "--base-path-relaxed")) { |
| 1089 | base_path_relaxed = 1; |
| 1090 | continue; |
| 1091 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1092 | if (!prefixcmp(arg, "--interpolated-path=")) { |
Jon Loeliger | 49ba83f | 2006-09-19 20:31:51 -0500 | [diff] [blame] | 1093 | interpolated_path = arg+20; |
| 1094 | continue; |
| 1095 | } |
Mark Wooding | 1955fab | 2006-02-03 20:27:04 +0000 | [diff] [blame] | 1096 | if (!strcmp(arg, "--reuseaddr")) { |
| 1097 | reuseaddr = 1; |
| 1098 | continue; |
| 1099 | } |
Junio C Hamano | 603968d | 2006-02-04 22:27:29 -0800 | [diff] [blame] | 1100 | if (!strcmp(arg, "--user-path")) { |
| 1101 | user_path = ""; |
| 1102 | continue; |
| 1103 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1104 | if (!prefixcmp(arg, "--user-path=")) { |
Junio C Hamano | 603968d | 2006-02-04 22:27:29 -0800 | [diff] [blame] | 1105 | user_path = arg + 12; |
| 1106 | continue; |
| 1107 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1108 | if (!prefixcmp(arg, "--pid-file=")) { |
Matthias Lederhofer | 45ed5d7 | 2006-07-13 12:18:08 +0200 | [diff] [blame] | 1109 | pid_file = arg + 11; |
| 1110 | continue; |
| 1111 | } |
Matthias Lederhofer | a526276 | 2006-07-13 18:47:13 +0200 | [diff] [blame] | 1112 | if (!strcmp(arg, "--detach")) { |
| 1113 | detach = 1; |
| 1114 | log_syslog = 1; |
| 1115 | continue; |
| 1116 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1117 | if (!prefixcmp(arg, "--user=")) { |
Tilman Sauerbeck | 678dac6 | 2006-08-22 19:37:41 +0200 | [diff] [blame] | 1118 | user_name = arg + 7; |
| 1119 | continue; |
| 1120 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1121 | if (!prefixcmp(arg, "--group=")) { |
Tilman Sauerbeck | 678dac6 | 2006-08-22 19:37:41 +0200 | [diff] [blame] | 1122 | group_name = arg + 8; |
| 1123 | continue; |
| 1124 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1125 | if (!prefixcmp(arg, "--enable=")) { |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 1126 | enable_service(arg + 9, 1); |
| 1127 | continue; |
| 1128 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1129 | if (!prefixcmp(arg, "--disable=")) { |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 1130 | enable_service(arg + 10, 0); |
| 1131 | continue; |
| 1132 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1133 | if (!prefixcmp(arg, "--allow-override=")) { |
Junio C Hamano | 74c0cc2 | 2006-08-20 19:03:50 -0700 | [diff] [blame] | 1134 | make_service_overridable(arg + 17, 1); |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 1135 | continue; |
| 1136 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 1137 | if (!prefixcmp(arg, "--forbid-override=")) { |
Junio C Hamano | 74c0cc2 | 2006-08-20 19:03:50 -0700 | [diff] [blame] | 1138 | make_service_overridable(arg + 18, 0); |
Junio C Hamano | d819e4e | 2006-08-20 19:03:13 -0700 | [diff] [blame] | 1139 | continue; |
| 1140 | } |
H. Peter Anvin | 4ae9568 | 2005-09-26 19:10:55 -0700 | [diff] [blame] | 1141 | if (!strcmp(arg, "--")) { |
| 1142 | ok_paths = &argv[i+1]; |
| 1143 | break; |
| 1144 | } else if (arg[0] != '-') { |
| 1145 | ok_paths = &argv[i]; |
| 1146 | break; |
| 1147 | } |
Linus Torvalds | e64e1b7 | 2005-07-15 09:32:16 -0700 | [diff] [blame] | 1148 | |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 1149 | usage(daemon_usage); |
| 1150 | } |
| 1151 | |
Tilman Sauerbeck | 678dac6 | 2006-08-22 19:37:41 +0200 | [diff] [blame] | 1152 | if (inetd_mode && (group_name || user_name)) |
| 1153 | die("--user and --group are incompatible with --inetd"); |
| 1154 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 1155 | if (inetd_mode && (listen_port || listen_addr)) |
| 1156 | die("--listen= and --port= are incompatible with --inetd"); |
| 1157 | else if (listen_port == 0) |
| 1158 | listen_port = DEFAULT_GIT_PORT; |
| 1159 | |
Tilman Sauerbeck | 678dac6 | 2006-08-22 19:37:41 +0200 | [diff] [blame] | 1160 | if (group_name && !user_name) |
| 1161 | die("--group supplied without --user"); |
| 1162 | |
| 1163 | if (user_name) { |
| 1164 | pass = getpwnam(user_name); |
| 1165 | if (!pass) |
| 1166 | die("user not found - %s", user_name); |
| 1167 | |
| 1168 | if (!group_name) |
| 1169 | gid = pass->pw_gid; |
| 1170 | else { |
| 1171 | group = getgrnam(group_name); |
| 1172 | if (!group) |
| 1173 | die("group not found - %s", group_name); |
| 1174 | |
| 1175 | gid = group->gr_gid; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Matthias Lederhofer | ad8b4f5 | 2006-07-13 12:02:29 +0200 | [diff] [blame] | 1179 | if (log_syslog) { |
Andreas Ericsson | a888328 | 2005-11-17 00:38:29 +0100 | [diff] [blame] | 1180 | openlog("git-daemon", 0, LOG_DAEMON); |
Matthias Lederhofer | ad8b4f5 | 2006-07-13 12:02:29 +0200 | [diff] [blame] | 1181 | set_die_routine(daemon_die); |
Andreas Ericsson | 4dbd135 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
Matthias Lederhofer | ad8b4f5 | 2006-07-13 12:02:29 +0200 | [diff] [blame] | 1184 | if (strict_paths && (!ok_paths || !*ok_paths)) |
| 1185 | die("option --strict-paths requires a whitelist"); |
| 1186 | |
lars.doelle@on-line.de | 7c3693f | 2005-09-08 03:50:01 +0200 | [diff] [blame] | 1187 | if (inetd_mode) { |
David Woodhouse | 5b276ee | 2006-06-20 15:38:13 +0100 | [diff] [blame] | 1188 | struct sockaddr_storage ss; |
| 1189 | struct sockaddr *peer = (struct sockaddr *)&ss; |
| 1190 | socklen_t slen = sizeof(ss); |
| 1191 | |
Junio C Hamano | ba0012c | 2006-06-21 16:37:48 -0700 | [diff] [blame] | 1192 | freopen("/dev/null", "w", stderr); |
David Woodhouse | 5b276ee | 2006-06-20 15:38:13 +0100 | [diff] [blame] | 1193 | |
| 1194 | if (getpeername(0, peer, &slen)) |
| 1195 | peer = NULL; |
| 1196 | |
| 1197 | return execute(peer); |
lars.doelle@on-line.de | 7c3693f | 2005-09-08 03:50:01 +0200 | [diff] [blame] | 1198 | } |
Andreas Ericsson | bce8230 | 2005-11-14 17:41:01 +0100 | [diff] [blame] | 1199 | |
Matthias Lederhofer | a526276 | 2006-07-13 18:47:13 +0200 | [diff] [blame] | 1200 | if (detach) |
| 1201 | daemonize(); |
| 1202 | else |
| 1203 | sanitize_stdfds(); |
Matthias Lederhofer | 258e93a | 2006-07-13 18:32:11 +0200 | [diff] [blame] | 1204 | |
Matthias Lederhofer | 45ed5d7 | 2006-07-13 12:18:08 +0200 | [diff] [blame] | 1205 | if (pid_file) |
| 1206 | store_pid(pid_file); |
| 1207 | |
Jon Loeliger | dd46762 | 2006-09-26 09:47:43 -0500 | [diff] [blame] | 1208 | return serve(listen_addr, listen_port, pass, gid); |
Linus Torvalds | a87e8be | 2005-07-13 19:45:26 -0700 | [diff] [blame] | 1209 | } |