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