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