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