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