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