1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
|
/*
* Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
* Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "got_compat.h"
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>
#include <ctype.h>
#include "got_opentemp.h"
#include "got_reference.h"
#include "got_object.h"
#include "gotwebd.h"
#include "log.h"
__dead void usage(void);
int main(int, char **);
int gotwebd_configure(struct gotwebd *, uid_t, gid_t);
void gotwebd_configure_done(struct gotwebd *);
void gotwebd_sighdlr(int sig, short event, void *arg);
void gotwebd_shutdown(void);
void gotwebd_dispatch_server(int, short, void *);
void gotwebd_dispatch_fcgi(int, short, void *);
void gotwebd_dispatch_login(int, short, void *);
void gotwebd_dispatch_auth(int, short, void *);
void gotwebd_dispatch_gotweb(int, short, void *);
struct gotwebd *gotwebd_env;
void
imsg_event_add(struct imsgev *iev)
{
if (iev->handler == NULL) {
imsgbuf_flush(&iev->ibuf);
return;
}
iev->events = EV_READ;
if (imsgbuf_queuelen(&iev->ibuf))
iev->events |= EV_WRITE;
event_del(&iev->ev);
event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
event_add(&iev->ev, NULL);
}
int
imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
pid_t pid, int fd, const void *data, size_t datalen)
{
int ret;
ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
if (ret == -1)
return (ret);
imsg_event_add(iev);
return (ret);
}
static int
send_imsg(struct imsgev *iev, uint32_t type, int fd, const void *data,
uint16_t len)
{
int ret, d = -1;
if (fd != -1 && (d = dup(fd)) == -1)
goto err;
ret = imsg_compose_event(iev, type, 0, -1, d, data, len);
if (ret == -1)
goto err;
if (d != -1) {
d = -1;
/* Flush imsg to prevent fd exhaustion. 'd' will be closed. */
if (imsgbuf_flush(&iev->ibuf) == -1)
goto err;
imsg_event_add(iev);
}
return 0;
err:
if (d != -1)
close(d);
return -1;
}
int
main_compose_sockets(struct gotwebd *env, uint32_t type, int fd,
const void *data, uint16_t len)
{
return send_imsg(env->iev_sockets, type, fd, data, len);
}
int
main_compose_login(struct gotwebd *env, uint32_t type, int fd,
const void *data, uint16_t len)
{
return send_imsg(env->iev_login, type, fd, data, len);
}
int
main_compose_gotweb(struct gotwebd *env, uint32_t type, int fd,
const void *data, uint16_t len)
{
size_t i;
int ret = 0;
for (i = 0; i < env->prefork; i++) {
ret = send_imsg(&env->iev_gotweb[i], type, fd, data, len);
if (ret)
break;
}
return ret;
}
int
main_compose_auth(struct gotwebd *env, uint32_t type, int fd,
const void *data, uint16_t len)
{
size_t i;
int ret = 0;
for (i = 0; i < env->prefork; i++) {
ret = send_imsg(&env->iev_auth[i], type, fd, data, len);
if (ret)
break;
}
return ret;
}
int
sockets_compose_main(struct gotwebd *env, uint32_t type, const void *d,
uint16_t len)
{
return (imsg_compose_event(env->iev_parent, type, 0, -1, -1, d, len));
}
void
gotwebd_dispatch_login(int fd, short event, void *arg)
{
struct imsgev *iev = arg;
struct imsgbuf *ibuf;
struct imsg imsg;
ssize_t n;
int shut = 0;
ibuf = &iev->ibuf;
if (event & EV_READ) {
if ((n = imsgbuf_read(ibuf)) == -1)
fatal("imsgbuf_read error");
if (n == 0) /* Connection closed */
shut = 1;
}
if (event & EV_WRITE) {
if (imsgbuf_write(ibuf) == -1)
fatal("imsgbuf_write");
}
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("imsg_get");
if (n == 0) /* No more messages. */
break;
switch (imsg.hdr.type) {
default:
fatalx("%s: unknown imsg type %d", __func__,
imsg.hdr.type);
}
imsg_free(&imsg);
}
if (!shut)
imsg_event_add(iev);
else {
/* This pipe is dead. Remove its event handler */
event_del(&iev->ev);
event_loopexit(NULL);
}
}
void
gotwebd_dispatch_server(int fd, short event, void *arg)
{
struct imsgev *iev = arg;
struct imsgbuf *ibuf;
struct imsg imsg;
struct gotwebd *env = gotwebd_env;
ssize_t n;
int shut = 0;
ibuf = &iev->ibuf;
if (event & EV_READ) {
if ((n = imsgbuf_read(ibuf)) == -1)
fatal("imsgbuf_read error");
if (n == 0) /* Connection closed */
shut = 1;
}
if (event & EV_WRITE) {
if (imsgbuf_write(ibuf) == -1)
fatal("imsgbuf_write");
}
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("imsg_get");
if (n == 0) /* No more messages. */
break;
switch (imsg.hdr.type) {
case GOTWEBD_IMSG_CFG_DONE:
gotwebd_configure_done(env);
break;
default:
fatalx("%s: unknown imsg type %d", __func__,
imsg.hdr.type);
}
imsg_free(&imsg);
}
if (!shut)
imsg_event_add(iev);
else {
/* This pipe is dead. Remove its event handler */
event_del(&iev->ev);
event_loopexit(NULL);
}
}
void
gotwebd_dispatch_fcgi(int fd, short event, void *arg)
{
struct imsgev *iev = arg;
struct imsgbuf *ibuf;
struct imsg imsg;
struct gotwebd *env = gotwebd_env;
ssize_t n;
int shut = 0;
ibuf = &iev->ibuf;
if (event & EV_READ) {
if ((n = imsgbuf_read(ibuf)) == -1)
fatal("imsgbuf_read error");
if (n == 0) /* Connection closed */
shut = 1;
}
if (event & EV_WRITE) {
if (imsgbuf_write(ibuf) == -1)
fatal("imsgbuf_write");
}
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("imsg_get");
if (n == 0) /* No more messages. */
break;
switch (imsg.hdr.type) {
case GOTWEBD_IMSG_CFG_DONE:
gotwebd_configure_done(env);
break;
default:
fatalx("%s: unknown imsg type %d", __func__,
imsg.hdr.type);
}
imsg_free(&imsg);
}
if (!shut)
imsg_event_add(iev);
else {
/* This pipe is dead. Remove its event handler */
event_del(&iev->ev);
event_loopexit(NULL);
}
}
void
gotwebd_dispatch_auth(int fd, short event, void *arg)
{
struct imsgev *iev = arg;
struct imsgbuf *ibuf;
struct imsg imsg;
struct gotwebd *env = gotwebd_env;
ssize_t n;
int shut = 0;
ibuf = &iev->ibuf;
if (event & EV_READ) {
if ((n = imsgbuf_read(ibuf)) == -1)
fatal("imsgbuf_read error");
if (n == 0) /* Connection closed */
shut = 1;
}
if (event & EV_WRITE) {
if (imsgbuf_write(ibuf) == -1)
fatal("imsgbuf_write");
}
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("imsg_get");
if (n == 0) /* No more messages. */
break;
switch (imsg.hdr.type) {
case GOTWEBD_IMSG_CFG_DONE:
gotwebd_configure_done(env);
break;
default:
fatalx("%s: unknown imsg type %d", __func__,
imsg.hdr.type);
}
imsg_free(&imsg);
}
if (!shut)
imsg_event_add(iev);
else {
/* This pipe is dead. Remove its event handler */
event_del(&iev->ev);
event_loopexit(NULL);
}
}
void
gotwebd_dispatch_gotweb(int fd, short event, void *arg)
{
struct imsgev *iev = arg;
struct imsgbuf *ibuf;
struct imsg imsg;
struct gotwebd *env = gotwebd_env;
ssize_t n;
int shut = 0;
ibuf = &iev->ibuf;
if (event & EV_READ) {
if ((n = imsgbuf_read(ibuf)) == -1)
fatal("imsgbuf_read error");
if (n == 0) /* Connection closed */
shut = 1;
}
if (event & EV_WRITE) {
if (imsgbuf_write(ibuf) == -1)
fatal("imsgbuf_write");
}
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("imsg_get");
if (n == 0) /* No more messages. */
break;
switch (imsg.hdr.type) {
case GOTWEBD_IMSG_CFG_DONE:
gotwebd_configure_done(env);
break;
default:
fatalx("%s: unknown imsg type %d", __func__,
imsg.hdr.type);
}
imsg_free(&imsg);
}
if (!shut)
imsg_event_add(iev);
else {
/* This pipe is dead. Remove its event handler */
event_del(&iev->ev);
event_loopexit(NULL);
}
}
void
gotwebd_sighdlr(int sig, short event, void *arg)
{
/* struct privsep *ps = arg; */
switch (sig) {
case SIGHUP:
log_info("%s: ignoring SIGHUP", __func__);
break;
case SIGPIPE:
log_info("%s: ignoring SIGPIPE", __func__);
break;
case SIGUSR1:
log_info("%s: ignoring SIGUSR1", __func__);
break;
case SIGTERM:
case SIGINT:
gotwebd_shutdown();
break;
default:
fatalx("unexpected signal");
}
}
static void
spawn_process(struct gotwebd *env, const char *argv0, struct imsgev *iev,
enum gotwebd_proc_type proc_type, const char *username,
const char *www_user, void (*handler)(int, short, void *))
{
const char *argv[10];
int argc = 0;
int p[2];
pid_t pid;
char usernames[_PW_NAME_LEN * 2 + 1 + 1];
int ret;
int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
#ifdef SOCK_CLOEXEC
sock_flags |= SOCK_CLOEXEC;
#endif
ret = snprintf(usernames, sizeof(usernames), "%s:%s",
username, www_user);
if (ret == -1)
fatal("snprintf");
if ((size_t)ret >= sizeof(usernames))
fatalx("usernames too long");
if (socketpair(AF_UNIX,
sock_flags, PF_UNSPEC, p) == -1)
fatal("socketpair");
switch (pid = fork()) {
case -1:
fatal("fork");
case 0: /* child */
break;
default: /* parent */
close(p[0]);
if (imsgbuf_init(&iev->ibuf, p[1]) == -1)
fatal("imsgbuf_init");
imsgbuf_allow_fdpass(&iev->ibuf);
iev->handler = handler;
iev->data = iev;
event_set(&iev->ev, p[1], EV_READ, handler, iev);
event_add(&iev->ev, NULL);
return;
}
close(p[1]);
argv[argc++] = argv0;
if (proc_type == GOTWEBD_PROC_SOCKETS) {
char *s;
argv[argc++] = "-S";
argv[argc++] = usernames;
if (asprintf(&s, "-S%d", env->prefork) == -1)
fatal("asprintf");
argv[argc++] = s;
} else if (proc_type == GOTWEBD_PROC_LOGIN) {
argv[argc++] = "-L";
argv[argc++] = usernames;
} else if (proc_type == GOTWEBD_PROC_FCGI) {
argv[argc++] = "-F";
argv[argc++] = usernames;
} else if (proc_type == GOTWEBD_PROC_AUTH) {
argv[argc++] = "-A";
argv[argc++] = usernames;
} else if (proc_type == GOTWEBD_PROC_GOTWEB) {
argv[argc++] = "-G";
argv[argc++] = usernames;
}
if (strcmp(env->gotwebd_conffile, GOTWEBD_CONF) != 0) {
argv[argc++] = "-f";
argv[argc++] = env->gotwebd_conffile;
}
if (env->gotwebd_debug)
argv[argc++] = "-d";
if (env->gotwebd_verbose > 0)
argv[argc++] = "-v";
if (env->gotwebd_verbose > 1)
argv[argc++] = "-v";
argv[argc] = NULL;
if (p[0] != GOTWEBD_SOCK_FILENO) {
if (dup2(p[0], GOTWEBD_SOCK_FILENO) == -1)
fatal("dup2");
} else if (fcntl(p[0], F_SETFD, 0) == -1)
fatal("fcntl");
/* obnoxious cast */
execvp(argv0, (char * const *)argv);
fatal("execvp %s", argv0);
}
__dead void
usage(void)
{
fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
getprogname());
exit(1);
}
static void
get_usernames(const char **gotwebd_username, const char **www_username,
char *optarg)
{
static char usernames[_PW_NAME_LEN * 2 + 1 + 1];
char *colon;
if (strlcpy(usernames, optarg, sizeof(usernames)) >=
sizeof(usernames))
fatalx("usernames too long");
colon = strchr(usernames, ':');
if (colon == NULL)
fatalx("bad username option parameter");
*colon = '\0';
*gotwebd_username = &usernames[0];
*www_username = colon + 1;
}
int
main(int argc, char **argv)
{
struct event sigint, sigterm, sighup, sigpipe, sigusr1;
struct event_base *evb;
struct gotwebd *env;
struct passwd *pw;
int ch, i, gotwebd_ngroups = NGROUPS_MAX;
int no_action = 0;
int proc_type = GOTWEBD_PROC_PARENT;
const char *conffile = GOTWEBD_CONF;
const char *gotwebd_username = GOTWEBD_DEFAULT_USER;
const char *www_username = GOTWEBD_WWW_USER;
gid_t gotwebd_groups[NGROUPS_MAX];
gid_t www_gid;
const char *argv0, *errstr;
if ((argv0 = argv[0]) == NULL)
argv0 = "gotwebd";
/* log to stderr until daemonized */
log_init(1, LOG_DAEMON);
env = calloc(1, sizeof(*env));
if (env == NULL)
fatal("%s: calloc", __func__);
config_init(env);
while ((ch = getopt(argc, argv, "A:D:dG:f:F:L:nS:vW:")) != -1) {
switch (ch) {
case 'A':
proc_type = GOTWEBD_PROC_AUTH;
get_usernames(&gotwebd_username, &www_username, optarg);
break;
case 'D':
if (cmdline_symset(optarg) < 0)
log_warnx("could not parse macro definition %s",
optarg);
break;
case 'd':
env->gotwebd_debug = 1;
break;
case 'G':
proc_type = GOTWEBD_PROC_GOTWEB;
get_usernames(&gotwebd_username, &www_username, optarg);
break;
case 'f':
conffile = optarg;
break;
case 'F':
proc_type = GOTWEBD_PROC_FCGI;
get_usernames(&gotwebd_username, &www_username, optarg);
break;
case 'L':
proc_type = GOTWEBD_PROC_LOGIN;
get_usernames(&gotwebd_username, &www_username, optarg);
break;
case 'n':
no_action = 1;
break;
case 'S':
proc_type = GOTWEBD_PROC_SOCKETS;
i = strtonum(optarg, 1, INT_MAX, &errstr);
if (errstr) {
get_usernames(&gotwebd_username,
&www_username, optarg);
} else
env->prefork = i;
break;
case 'v':
if (env->gotwebd_verbose < 3)
env->gotwebd_verbose++;
break;
default:
usage();
}
}
argc -= optind;
if (argc > 0)
usage();
gotwebd_env = env;
env->gotwebd_conffile = conffile;
if (proc_type == GOTWEBD_PROC_PARENT) {
if (parse_config(env->gotwebd_conffile, env) == -1)
exit(1);
if (no_action) {
fprintf(stderr, "configuration OK\n");
exit(0);
}
if (env->user)
gotwebd_username = env->user;
if (env->www_user)
www_username = env->www_user;
}
if (proc_type == GOTWEBD_PROC_SOCKETS) {
env->worker_load = calloc(env->prefork,
sizeof(env->worker_load[0]));
if (env->worker_load == NULL)
fatal("calloc");
}
pw = getpwnam(www_username);
if (pw == NULL)
fatalx("unknown user %s", www_username);
env->www_uid = pw->pw_uid;
www_gid = pw->pw_gid;
pw = getpwnam(gotwebd_username);
if (pw == NULL)
fatalx("unknown user %s", gotwebd_username);
if (getgrouplist(gotwebd_username, pw->pw_gid, gotwebd_groups,
&gotwebd_ngroups) == -1)
fatalx("too many groups for user %s", gotwebd_username);
/* check for root privileges */
if (geteuid())
fatalx("need root privileges");
log_init(env->gotwebd_debug, LOG_DAEMON);
log_setverbose(env->gotwebd_verbose);
switch (proc_type) {
case GOTWEBD_PROC_LOGIN:
setproctitle("login");
log_procinit("login");
if (setgroups(1, &pw->pw_gid) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
fatal("failed to drop privileges");
gotwebd_login(env, GOTWEBD_SOCK_FILENO);
return 1;
case GOTWEBD_PROC_SOCKETS:
setproctitle("sockets");
log_procinit("sockets");
if (chroot(env->httpd_chroot) == -1)
fatal("chroot %s", env->httpd_chroot);
if (chdir("/") == -1)
fatal("chdir /");
if (setgroups(1, &pw->pw_gid) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
fatal("failed to drop privileges");
sockets(env, GOTWEBD_SOCK_FILENO);
return 1;
case GOTWEBD_PROC_FCGI:
setproctitle("fcgi");
log_procinit("fcgi");
if (chroot(env->httpd_chroot) == -1)
fatal("chroot %s", env->httpd_chroot);
if (chdir("/") == -1)
fatal("chdir /");
if (setgroups(gotwebd_ngroups, gotwebd_groups) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
fatal("failed to drop privileges");
gotwebd_fcgi(env, GOTWEBD_SOCK_FILENO);
return 1;
case GOTWEBD_PROC_AUTH:
setproctitle("auth");
log_procinit("auth");
if (setgroups(1, &pw->pw_gid) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
fatal("failed to drop privileges");
gotwebd_auth(env, GOTWEBD_SOCK_FILENO);
return 1;
case GOTWEBD_PROC_GOTWEB:
setproctitle("gotweb");
log_procinit("gotweb");
if (setgroups(gotwebd_ngroups, gotwebd_groups) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
fatal("failed to drop privileges");
gotweb(env, GOTWEBD_SOCK_FILENO);
return 1;
default:
break;
}
if (!env->gotwebd_debug && daemon(1, 0) == -1)
fatal("daemon");
evb = event_init();
env->iev_sockets = calloc(1, sizeof(*env->iev_sockets));
if (env->iev_sockets == NULL)
fatal("calloc");
env->iev_login = calloc(1, sizeof(*env->iev_login));
if (env->iev_login == NULL)
fatal("calloc");
env->iev_fcgi = calloc(1, sizeof(*env->iev_fcgi));
if (env->iev_fcgi == NULL)
fatal("calloc");
env->iev_auth = calloc(env->prefork, sizeof(*env->iev_auth));
if (env->iev_auth == NULL)
fatal("calloc");
env->iev_gotweb = calloc(env->prefork, sizeof(*env->iev_gotweb));
if (env->iev_gotweb == NULL)
fatal("calloc");
spawn_process(env, argv0, env->iev_sockets,
GOTWEBD_PROC_SOCKETS, gotwebd_username, www_username,
gotwebd_dispatch_server);
spawn_process(env, argv0, env->iev_login, GOTWEBD_PROC_LOGIN,
gotwebd_username, www_username, gotwebd_dispatch_login);
spawn_process(env, argv0, env->iev_fcgi,
GOTWEBD_PROC_FCGI, gotwebd_username, www_username,
gotwebd_dispatch_fcgi);
for (i = 0; i < env->prefork; ++i) {
spawn_process(env, argv0, &env->iev_auth[i],
GOTWEBD_PROC_AUTH, gotwebd_username, www_username,
gotwebd_dispatch_auth);
spawn_process(env, argv0, &env->iev_gotweb[i],
GOTWEBD_PROC_GOTWEB, gotwebd_username, www_username,
gotwebd_dispatch_gotweb);
}
if (chdir("/") == -1)
fatal("chdir /");
log_procinit("gotwebd");
log_info("%s startup", getprogname());
signal_set(&sigint, SIGINT, gotwebd_sighdlr, env);
signal_set(&sigterm, SIGTERM, gotwebd_sighdlr, env);
signal_set(&sighup, SIGHUP, gotwebd_sighdlr, env);
signal_set(&sigpipe, SIGPIPE, gotwebd_sighdlr, env);
signal_set(&sigusr1, SIGUSR1, gotwebd_sighdlr, env);
signal_add(&sigint, NULL);
signal_add(&sigterm, NULL);
signal_add(&sighup, NULL);
signal_add(&sigpipe, NULL);
signal_add(&sigusr1, NULL);
if (gotwebd_configure(env, pw->pw_uid, www_gid) == -1)
fatalx("configuration failed");
if (setgroups(1, &pw->pw_gid) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
fatal("failed to drop privileges");
#ifdef PROFILE
if (unveil("gmon.out", "rwc") != 0)
err(1, "gmon.out");
#endif
if (unveil(GOTWEBD_CONF, "r") == -1)
err(1, "unveil");
if (unveil(NULL, NULL) != 0)
err(1, "unveil");
#ifndef PROFILE
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
#endif
event_dispatch();
event_base_free(evb);
log_debug("%s gotwebd exiting", getprogname());
return (0);
}
static void
connect_children(struct gotwebd *env)
{
struct imsgev *iev_gotweb, *iev_auth;
int pipe[2];
int i;
int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
#ifdef SOCK_CLOEXEC
sock_flags |= SOCK_CLOEXEC;
#endif
if (socketpair(AF_UNIX, sock_flags,
PF_UNSPEC, pipe) == -1)
fatal("socketpair");
if (main_compose_sockets(env, GOTWEBD_IMSG_CTL_PIPE, pipe[0], NULL, 0))
fatal("main_compose_sockets");
if (send_imsg(env->iev_fcgi, GOTWEBD_IMSG_CTL_PIPE, pipe[1], NULL, 0))
fatal("send_imsg");
for (i = 0; i < env->prefork; i++) {
iev_gotweb = &env->iev_gotweb[i];
iev_auth = &env->iev_auth[i];
if (socketpair(AF_UNIX,
SOCK_STREAM | sock_flags,
PF_UNSPEC, pipe) == -1)
fatal("socketpair");
if (main_compose_sockets(env, GOTWEBD_IMSG_CTL_PIPE,
pipe[0], NULL, 0))
fatal("send_imsg");
if (send_imsg(iev_auth, GOTWEBD_IMSG_CTL_PIPE,
pipe[1], NULL, 0))
fatal("send_imsg");
if (socketpair(AF_UNIX,
SOCK_STREAM | sock_flags,
PF_UNSPEC, pipe) == -1)
fatal("socketpair");
if (send_imsg(iev_auth, GOTWEBD_IMSG_CTL_PIPE,
pipe[0], NULL, 0))
fatal("send_imsg");
if (send_imsg(iev_gotweb, GOTWEBD_IMSG_CTL_PIPE,
pipe[1], NULL, 0))
fatal("send_imsg");
}
}
int
gotwebd_configure(struct gotwebd *env, uid_t uid, gid_t gid)
{
struct server *srv;
struct socket *sock;
struct gotwebd_repo *repo;
char secret[32];
int i;
/* gotweb need to reload its config. */
env->gotweb_pending = env->prefork;
env->auth_pending = env->prefork;
/* send global access rules */
for (i = 0; i < env->prefork; ++i) {
config_set_access_rules(&env->iev_auth[i],
&env->access_rules);
config_set_access_rules(&env->iev_gotweb[i],
&env->access_rules);
}
/* send our gotweb servers */
TAILQ_FOREACH(srv, &env->servers, entry) {
if (main_compose_sockets(env, GOTWEBD_IMSG_CFG_SRV,
-1, srv, sizeof(*srv)) == -1)
fatal("send_imsg GOTWEBD_IMSG_CFG_SRV");
if (main_compose_auth(env, GOTWEBD_IMSG_CFG_SRV,
-1, srv, sizeof(*srv)) == -1)
fatal("main_compose_gotweb GOTWEBD_IMSG_CFG_SRV");
if (main_compose_gotweb(env, GOTWEBD_IMSG_CFG_SRV,
-1, srv, sizeof(*srv)) == -1)
fatal("main_compose_gotweb GOTWEBD_IMSG_CFG_SRV");
if (main_compose_login(env, GOTWEBD_IMSG_CFG_SRV,
-1, srv, sizeof(*srv)) == -1)
fatal("main_compose_gotweb GOTWEBD_IMSG_CFG_SRV");
/* send per-server access rules */
for (i = 0; i < env->prefork; ++i) {
config_set_access_rules(&env->iev_auth[i],
&srv->access_rules);
config_set_access_rules(&env->iev_gotweb[i],
&srv->access_rules);
}
/* send repositories and per-repository access rules */
TAILQ_FOREACH(repo, &srv->repos, entry) {
for (i = 0; i < env->prefork; i++) {
config_set_repository(&env->iev_auth[i],
repo);
config_set_repository(&env->iev_gotweb[i],
repo);
config_set_access_rules(&env->iev_auth[i],
&repo->access_rules);
config_set_access_rules(&env->iev_gotweb[i],
&repo->access_rules);
}
}
for (i = 0; i < env->prefork; i++) {
if (imsgbuf_flush(&env->iev_auth[i].ibuf) == -1)
fatal("imsgbuf_flush");
imsg_event_add(&env->iev_auth[i]);
if (imsgbuf_flush(&env->iev_gotweb[i].ibuf) == -1)
fatal("imsgbuf_flush");
imsg_event_add(&env->iev_gotweb[i]);
}
}
/* send our sockets */
TAILQ_FOREACH(sock, &env->sockets, entry) {
if (config_setsock(env, sock, uid, gid) == -1)
fatalx("%s: send socket error", __func__);
}
/* send the temp files */
if (config_setfd(env) == -1)
fatalx("%s: send priv_fd error", __func__);
/* Connect servers and gotwebs. */
connect_children(env);
if (main_compose_auth(env, GOTWEBD_IMSG_AUTH_CONF, -1,
&env->auth_config, sizeof(env->auth_config)) == -1)
fatal("send_imsg GOTWEB_IMSG_AUTH_CONF");
if (main_compose_gotweb(env, GOTWEBD_IMSG_AUTH_CONF, -1,
&env->auth_config, sizeof(env->auth_config)) == -1)
fatal("main_compose_gotweb GOTWEB_IMSG_AUTH_CONF");
if (main_compose_auth(env, GOTWEBD_IMSG_WWW_UID, -1,
&env->www_uid, sizeof(env->www_uid)) == -1)
fatal("main_compose_auth GOTWEB_IMSG_WWW_UID");
if (main_compose_gotweb(env, GOTWEBD_IMSG_WWW_UID, -1,
&env->www_uid, sizeof(env->www_uid)) == -1)
fatal("main_compose_gotweb GOTWEB_IMSG_WWW_UID");
arc4random_buf(secret, sizeof(secret));
if (main_compose_login(env, GOTWEBD_IMSG_LOGIN_SECRET, -1,
secret, sizeof(secret)) == -1)
fatal("main_compose_login GOTWEB_IMSG_LOGIN_SECRET");
if (main_compose_auth(env, GOTWEBD_IMSG_LOGIN_SECRET, -1,
secret, sizeof(secret)) == -1)
fatal("main_compose_auth GOTWEB_IMSG_LOGIN_SECRET");
arc4random_buf(secret, sizeof(secret));
if (main_compose_auth(env, GOTWEBD_IMSG_AUTH_SECRET, -1,
secret, sizeof(secret)) == -1)
fatal("main_compose_auth GOTWEB_IMSG_AUTH_SECRET");
#ifdef __APPLE__
memset_s(secret, sizeof(*secret), 0, sizeof(*secret));
#elif defined(__NetBSD__)
explicit_memset(secret, sizeof(*secret), 0);
#else
explicit_bzero(secret, sizeof(secret));
#endif
if (login_privinit(env, uid, gid) == -1)
fatalx("cannot open authentication socket");
if (main_compose_login(env, GOTWEBD_IMSG_CFG_SOCK, env->login_sock->fd,
NULL, 0) == -1)
fatal("main_compose_login GOTWEBD_IMSG_CFG_SOCK");
if (main_compose_sockets(env, GOTWEBD_IMSG_CFG_DONE, -1,
NULL, 0) == -1)
fatal("send_imsg GOTWEBD_IMSG_CFG_DONE");
return (0);
}
void
gotwebd_configure_done(struct gotwebd *env)
{
if (main_compose_sockets(env, GOTWEBD_IMSG_CTL_START,
-1, NULL, 0) == -1)
fatal("send_imsg GOTWEBD_IMSG_CTL_START");
if (env->gotweb_pending > 0) {
env->gotweb_pending--;
if (env->gotweb_pending == 0 &&
main_compose_gotweb(env, GOTWEBD_IMSG_CTL_START,
-1, NULL, 0) == -1)
fatal("main_compose_gotweb GOTWEBD_IMSG_CTL_START");
}
if (env->auth_pending > 0) {
env->auth_pending--;
if (env->auth_pending == 0 &&
main_compose_auth(env, GOTWEBD_IMSG_CTL_START,
-1, NULL, 0) == -1)
fatal("main_compose_auth GOTWEBD_IMSG_CTL_START");
}
if (main_compose_login(env, GOTWEBD_IMSG_CTL_START,
-1, NULL, 0) == -1)
fatal("send_imsg GOTWEBD_IMSG_CTL_START");
}
void
gotwebd_shutdown(void)
{
struct gotwebd *env = gotwebd_env;
pid_t pid;
int i, status;
event_del(&env->iev_login->ev);
imsgbuf_clear(&env->iev_login->ibuf);
close(env->iev_login->ibuf.fd);
env->iev_login->ibuf.fd = -1;
free(env->iev_login);
event_del(&env->iev_sockets->ev);
imsgbuf_clear(&env->iev_sockets->ibuf);
close(env->iev_sockets->ibuf.fd);
env->iev_sockets->ibuf.fd = -1;
free(env->iev_sockets);
event_del(&env->iev_fcgi->ev);
imsgbuf_clear(&env->iev_fcgi->ibuf);
close(env->iev_fcgi->ibuf.fd);
env->iev_fcgi->ibuf.fd = -1;
free(env->iev_fcgi);
for (i = 0; i < env->prefork; ++i) {
event_del(&env->iev_auth[i].ev);
imsgbuf_clear(&env->iev_auth[i].ibuf);
close(env->iev_auth[i].ibuf.fd);
env->iev_auth[i].ibuf.fd = -1;
event_del(&env->iev_gotweb[i].ev);
imsgbuf_clear(&env->iev_gotweb[i].ibuf);
close(env->iev_gotweb[i].ibuf.fd);
env->iev_gotweb[i].ibuf.fd = -1;
}
free(env->iev_auth);
free(env->iev_gotweb);
free(env->login_sock);
do {
pid = waitpid(WAIT_ANY, &status, 0);
if (pid <= 0)
continue;
if (WIFSIGNALED(status))
log_warnx("lost child: pid %u terminated; signal %d",
pid, WTERMSIG(status));
else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
log_warnx("lost child: pid %u exited abnormally",
pid);
} while (pid != -1 || (pid == -1 && errno == EINTR));
while (!TAILQ_EMPTY(&gotwebd_env->addresses)) {
struct address *h;
h = TAILQ_FIRST(&gotwebd_env->addresses);
TAILQ_REMOVE(&gotwebd_env->addresses, h, entry);
free(h);
}
while (!TAILQ_EMPTY(&gotwebd_env->sockets)) {
struct socket *sock;
sock = TAILQ_FIRST(&gotwebd_env->sockets);
TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
free(sock);
}
while (!TAILQ_EMPTY(&gotwebd_env->servers)) {
struct server *srv;
srv = TAILQ_FIRST(&gotwebd_env->servers);
TAILQ_REMOVE(&gotwebd_env->servers, srv, entry);
free(srv);
}
free(gotwebd_env);
log_warnx("gotwebd terminating");
exit(0);
}
|