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