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