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