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