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