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