1 /* $OpenBSD: smtpd.c,v 1.338 2021/04/21 07:54:10 eric Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> 5 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> 6 * Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/types.h> 22 #include <sys/queue.h> 23 #include <sys/tree.h> 24 #include <sys/socket.h> 25 #include <sys/wait.h> 26 #include <sys/stat.h> 27 #include <sys/uio.h> 28 #include <sys/mman.h> 29 30 #include <bsd_auth.h> 31 #include <dirent.h> 32 #include <err.h> 33 #include <errno.h> 34 #include <event.h> 35 #include <fcntl.h> 36 #include <fts.h> 37 #include <grp.h> 38 #include <imsg.h> 39 #include <inttypes.h> 40 #include <login_cap.h> 41 #include <paths.h> 42 #include <poll.h> 43 #include <pwd.h> 44 #include <signal.h> 45 #include <stdio.h> 46 #include <syslog.h> 47 #include <limits.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <sysexits.h> 51 #include <time.h> 52 #include <tls.h> 53 #include <unistd.h> 54 55 #include <openssl/ssl.h> 56 #include <openssl/evp.h> 57 58 #include "smtpd.h" 59 #include "log.h" 60 #include "ssl.h" 61 62 #define SMTPD_MAXARG 32 63 64 static void parent_imsg(struct mproc *, struct imsg *); 65 static void usage(void); 66 static int smtpd(void); 67 static void parent_shutdown(void); 68 static void parent_send_config(int, short, void *); 69 static void parent_send_config_lka(void); 70 static void parent_send_config_dispatcher(void); 71 static void parent_send_config_ca(void); 72 static void parent_sig_handler(int, short, void *); 73 static void forkmda(struct mproc *, uint64_t, struct deliver *); 74 static int parent_forward_open(char *, char *, uid_t, gid_t); 75 static struct child *child_add(pid_t, int, const char *); 76 static struct mproc *start_child(int, char **, char *); 77 static struct mproc *setup_peer(enum smtp_proc_type, pid_t, int); 78 static void setup_peers(struct mproc *, struct mproc *); 79 static void setup_done(struct mproc *); 80 static void setup_proc(void); 81 static struct mproc *setup_peer(enum smtp_proc_type, pid_t, int); 82 static int imsg_wait(struct imsgbuf *, struct imsg *, int); 83 84 static void offline_scan(int, short, void *); 85 static int offline_add(char *, uid_t, gid_t); 86 static void offline_done(void); 87 static int offline_enqueue(char *, uid_t, gid_t); 88 89 static void purge_task(void); 90 static int parent_auth_user(const char *, const char *); 91 static void load_pki_tree(void); 92 static void load_pki_keys(void); 93 94 static void fork_filter_processes(void); 95 static void fork_filter_process(const char *, const char *, const char *, const char *, const char *, uint32_t); 96 97 enum child_type { 98 CHILD_DAEMON, 99 CHILD_MDA, 100 CHILD_PROCESSOR, 101 CHILD_ENQUEUE_OFFLINE, 102 }; 103 104 struct child { 105 pid_t pid; 106 enum child_type type; 107 const char *title; 108 int mda_out; 109 uint64_t mda_id; 110 char *path; 111 char *cause; 112 }; 113 114 struct offline { 115 TAILQ_ENTRY(offline) entry; 116 uid_t uid; 117 gid_t gid; 118 char *path; 119 }; 120 121 #define OFFLINE_READMAX 20 122 #define OFFLINE_QUEUEMAX 5 123 static size_t offline_running = 0; 124 TAILQ_HEAD(, offline) offline_q; 125 126 static struct event config_ev; 127 static struct event offline_ev; 128 static struct timeval offline_timeout; 129 130 static pid_t purge_pid = -1; 131 132 extern char **environ; 133 void (*imsg_callback)(struct mproc *, struct imsg *); 134 135 enum smtp_proc_type smtpd_process; 136 137 struct smtpd *env = NULL; 138 139 struct mproc *p_control = NULL; 140 struct mproc *p_lka = NULL; 141 struct mproc *p_parent = NULL; 142 struct mproc *p_queue = NULL; 143 struct mproc *p_scheduler = NULL; 144 struct mproc *p_dispatcher = NULL; 145 struct mproc *p_ca = NULL; 146 147 const char *backend_queue = "fs"; 148 const char *backend_scheduler = "ramqueue"; 149 const char *backend_stat = "ram"; 150 151 int profiling = 0; 152 int debug = 0; 153 int foreground = 0; 154 int control_socket = -1; 155 156 struct tree children; 157 158 static void 159 parent_imsg(struct mproc *p, struct imsg *imsg) 160 { 161 struct forward_req *fwreq; 162 struct filter_proc *processor; 163 struct deliver deliver; 164 struct child *c; 165 struct msg m; 166 const void *data; 167 const char *username, *password, *cause, *procname; 168 uint64_t reqid; 169 size_t sz; 170 void *i; 171 int fd, n, v, ret; 172 173 if (imsg == NULL) 174 fatalx("process %s socket closed", p->name); 175 176 switch (imsg->hdr.type) { 177 case IMSG_LKA_OPEN_FORWARD: 178 CHECK_IMSG_DATA_SIZE(imsg, sizeof *fwreq); 179 fwreq = imsg->data; 180 fd = parent_forward_open(fwreq->user, fwreq->directory, 181 fwreq->uid, fwreq->gid); 182 fwreq->status = 0; 183 if (fd == -1 && errno != ENOENT) { 184 if (errno == EAGAIN) 185 fwreq->status = -1; 186 } 187 else 188 fwreq->status = 1; 189 m_compose(p, IMSG_LKA_OPEN_FORWARD, 0, 0, fd, 190 fwreq, sizeof *fwreq); 191 return; 192 193 case IMSG_LKA_AUTHENTICATE: 194 /* 195 * If we reached here, it means we want root to lookup 196 * system user. 197 */ 198 m_msg(&m, imsg); 199 m_get_id(&m, &reqid); 200 m_get_string(&m, &username); 201 m_get_string(&m, &password); 202 m_end(&m); 203 204 ret = parent_auth_user(username, password); 205 206 m_create(p, IMSG_LKA_AUTHENTICATE, 0, 0, -1); 207 m_add_id(p, reqid); 208 m_add_int(p, ret); 209 m_close(p); 210 return; 211 212 case IMSG_MDA_FORK: 213 m_msg(&m, imsg); 214 m_get_id(&m, &reqid); 215 m_get_data(&m, &data, &sz); 216 m_end(&m); 217 if (sz != sizeof(deliver)) 218 fatalx("expected deliver"); 219 memmove(&deliver, data, sz); 220 forkmda(p, reqid, &deliver); 221 return; 222 223 case IMSG_MDA_KILL: 224 m_msg(&m, imsg); 225 m_get_id(&m, &reqid); 226 m_get_string(&m, &cause); 227 m_end(&m); 228 229 i = NULL; 230 while ((n = tree_iter(&children, &i, NULL, (void**)&c))) 231 if (c->type == CHILD_MDA && 232 c->mda_id == reqid && 233 c->cause == NULL) 234 break; 235 if (!n) { 236 log_debug("debug: smtpd: " 237 "kill request: proc not found"); 238 return; 239 } 240 241 c->cause = xstrdup(cause); 242 log_debug("debug: smtpd: kill requested for %u: %s", 243 c->pid, c->cause); 244 kill(c->pid, SIGTERM); 245 return; 246 247 case IMSG_CTL_VERBOSE: 248 m_msg(&m, imsg); 249 m_get_int(&m, &v); 250 m_end(&m); 251 log_trace_verbose(v); 252 return; 253 254 case IMSG_CTL_PROFILE: 255 m_msg(&m, imsg); 256 m_get_int(&m, &v); 257 m_end(&m); 258 profiling = v; 259 return; 260 261 case IMSG_LKA_PROCESSOR_ERRFD: 262 m_msg(&m, imsg); 263 m_get_string(&m, &procname); 264 m_end(&m); 265 266 processor = dict_xget(env->sc_filter_processes_dict, procname); 267 m_create(p_lka, IMSG_LKA_PROCESSOR_ERRFD, 0, 0, processor->errfd); 268 m_add_string(p_lka, procname); 269 m_close(p_lka); 270 return; 271 } 272 273 errx(1, "parent_imsg: unexpected %s imsg from %s", 274 imsg_to_str(imsg->hdr.type), proc_title(p->proc)); 275 } 276 277 static void 278 usage(void) 279 { 280 extern char *__progname; 281 282 fprintf(stderr, "usage: %s [-dFhnv] [-D macro=value] " 283 "[-f file] [-P system] [-T trace]\n", __progname); 284 exit(1); 285 } 286 287 static void 288 parent_shutdown(void) 289 { 290 pid_t pid; 291 292 mproc_clear(p_ca); 293 mproc_clear(p_dispatcher); 294 mproc_clear(p_control); 295 mproc_clear(p_lka); 296 mproc_clear(p_scheduler); 297 mproc_clear(p_queue); 298 299 do { 300 pid = waitpid(WAIT_MYPGRP, NULL, 0); 301 } while (pid != -1 || (pid == -1 && errno == EINTR)); 302 303 unlink(SMTPD_SOCKET); 304 305 log_info("Exiting"); 306 exit(0); 307 } 308 309 static void 310 parent_send_config(int fd, short event, void *p) 311 { 312 parent_send_config_lka(); 313 parent_send_config_dispatcher(); 314 parent_send_config_ca(); 315 purge_config(PURGE_PKI); 316 } 317 318 static void 319 parent_send_config_dispatcher(void) 320 { 321 log_debug("debug: parent_send_config: configuring dispatcher process"); 322 m_compose(p_dispatcher, IMSG_CONF_START, 0, 0, -1, NULL, 0); 323 m_compose(p_dispatcher, IMSG_CONF_END, 0, 0, -1, NULL, 0); 324 } 325 326 void 327 parent_send_config_lka() 328 { 329 log_debug("debug: parent_send_config_ruleset: reloading"); 330 m_compose(p_lka, IMSG_CONF_START, 0, 0, -1, NULL, 0); 331 m_compose(p_lka, IMSG_CONF_END, 0, 0, -1, NULL, 0); 332 } 333 334 static void 335 parent_send_config_ca(void) 336 { 337 log_debug("debug: parent_send_config: configuring ca process"); 338 m_compose(p_ca, IMSG_CONF_START, 0, 0, -1, NULL, 0); 339 m_compose(p_ca, IMSG_CONF_END, 0, 0, -1, NULL, 0); 340 } 341 342 static void 343 parent_sig_handler(int sig, short event, void *p) 344 { 345 struct child *child; 346 int status, fail; 347 pid_t pid; 348 char *cause; 349 350 switch (sig) { 351 case SIGTERM: 352 case SIGINT: 353 log_debug("debug: got signal %d", sig); 354 parent_shutdown(); 355 /* NOT REACHED */ 356 357 case SIGCHLD: 358 do { 359 int len; 360 enum mda_resp_status mda_status; 361 int mda_sysexit; 362 363 pid = waitpid(-1, &status, WNOHANG); 364 if (pid <= 0) 365 continue; 366 367 fail = 0; 368 if (WIFSIGNALED(status)) { 369 fail = 1; 370 len = asprintf(&cause, "terminated; signal %d", 371 WTERMSIG(status)); 372 mda_status = MDA_TEMPFAIL; 373 mda_sysexit = 0; 374 } else if (WIFEXITED(status)) { 375 if (WEXITSTATUS(status) != 0) { 376 fail = 1; 377 len = asprintf(&cause, 378 "exited abnormally"); 379 mda_sysexit = WEXITSTATUS(status); 380 if (mda_sysexit == EX_OSERR || 381 mda_sysexit == EX_TEMPFAIL) 382 mda_status = MDA_TEMPFAIL; 383 else 384 mda_status = MDA_PERMFAIL; 385 } else { 386 len = asprintf(&cause, "exited okay"); 387 mda_status = MDA_OK; 388 mda_sysexit = 0; 389 } 390 } else 391 /* WIFSTOPPED or WIFCONTINUED */ 392 continue; 393 394 if (len == -1) 395 fatal("asprintf"); 396 397 if (pid == purge_pid) 398 purge_pid = -1; 399 400 child = tree_pop(&children, pid); 401 if (child == NULL) 402 goto skip; 403 404 switch (child->type) { 405 case CHILD_PROCESSOR: 406 if (fail) { 407 log_warnx("warn: lost processor: %s %s", 408 child->title, cause); 409 parent_shutdown(); 410 } 411 break; 412 413 case CHILD_DAEMON: 414 if (fail) 415 log_warnx("warn: lost child: %s %s", 416 child->title, cause); 417 break; 418 419 case CHILD_MDA: 420 if (WIFSIGNALED(status) && 421 WTERMSIG(status) == SIGALRM) { 422 char *tmp; 423 if (asprintf(&tmp, 424 "terminated; timeout") != -1) { 425 free(cause); 426 cause = tmp; 427 } 428 } 429 else if (child->cause && 430 WIFSIGNALED(status) && 431 WTERMSIG(status) == SIGTERM) { 432 free(cause); 433 cause = child->cause; 434 child->cause = NULL; 435 } 436 free(child->cause); 437 log_debug("debug: smtpd: mda process done " 438 "for session %016"PRIx64 ": %s", 439 child->mda_id, cause); 440 441 m_create(p_dispatcher, IMSG_MDA_DONE, 0, 0, 442 child->mda_out); 443 m_add_id(p_dispatcher, child->mda_id); 444 m_add_int(p_dispatcher, mda_status); 445 m_add_int(p_dispatcher, mda_sysexit); 446 m_add_string(p_dispatcher, cause); 447 m_close(p_dispatcher); 448 449 break; 450 451 case CHILD_ENQUEUE_OFFLINE: 452 if (fail) 453 log_warnx("warn: smtpd: " 454 "couldn't enqueue offline " 455 "message %s; smtpctl %s", 456 child->path, cause); 457 else 458 unlink(child->path); 459 free(child->path); 460 offline_done(); 461 break; 462 463 default: 464 fatalx("smtpd: unexpected child type"); 465 } 466 free(child); 467 skip: 468 free(cause); 469 } while (pid > 0 || (pid == -1 && errno == EINTR)); 470 471 break; 472 default: 473 fatalx("smtpd: unexpected signal"); 474 } 475 } 476 477 int 478 main(int argc, char *argv[]) 479 { 480 int c, i; 481 int opts, flags; 482 const char *conffile = CONF_FILE; 483 int save_argc = argc; 484 char **save_argv = argv; 485 char *rexec = NULL; 486 struct smtpd *conf; 487 488 if ((conf = config_default()) == NULL) 489 err(1, NULL); 490 491 env = conf; 492 493 flags = 0; 494 opts = 0; 495 debug = 0; 496 tracing = 0; 497 498 log_init(1, LOG_MAIL); 499 500 TAILQ_INIT(&offline_q); 501 502 while ((c = getopt(argc, argv, "B:dD:hnP:f:FT:vx:")) != -1) { 503 switch (c) { 504 case 'B': 505 if (strstr(optarg, "queue=") == optarg) 506 backend_queue = strchr(optarg, '=') + 1; 507 else if (strstr(optarg, "scheduler=") == optarg) 508 backend_scheduler = strchr(optarg, '=') + 1; 509 else if (strstr(optarg, "stat=") == optarg) 510 backend_stat = strchr(optarg, '=') + 1; 511 else 512 log_warnx("warn: " 513 "invalid backend specifier %s", 514 optarg); 515 break; 516 case 'd': 517 foreground = 1; 518 foreground_log = 1; 519 break; 520 case 'D': 521 if (cmdline_symset(optarg) < 0) 522 log_warnx("warn: " 523 "could not parse macro definition %s", 524 optarg); 525 break; 526 case 'h': 527 log_info("version: " SMTPD_NAME " " SMTPD_VERSION); 528 usage(); 529 break; 530 case 'n': 531 debug = 2; 532 opts |= SMTPD_OPT_NOACTION; 533 break; 534 case 'f': 535 conffile = optarg; 536 break; 537 case 'F': 538 foreground = 1; 539 break; 540 541 case 'T': 542 if (!strcmp(optarg, "imsg")) 543 tracing |= TRACE_IMSG; 544 else if (!strcmp(optarg, "io")) 545 tracing |= TRACE_IO; 546 else if (!strcmp(optarg, "smtp")) 547 tracing |= TRACE_SMTP; 548 else if (!strcmp(optarg, "filters")) 549 tracing |= TRACE_FILTERS; 550 else if (!strcmp(optarg, "mta") || 551 !strcmp(optarg, "transfer")) 552 tracing |= TRACE_MTA; 553 else if (!strcmp(optarg, "bounce") || 554 !strcmp(optarg, "bounces")) 555 tracing |= TRACE_BOUNCE; 556 else if (!strcmp(optarg, "scheduler")) 557 tracing |= TRACE_SCHEDULER; 558 else if (!strcmp(optarg, "lookup")) 559 tracing |= TRACE_LOOKUP; 560 else if (!strcmp(optarg, "stat") || 561 !strcmp(optarg, "stats")) 562 tracing |= TRACE_STAT; 563 else if (!strcmp(optarg, "rules")) 564 tracing |= TRACE_RULES; 565 else if (!strcmp(optarg, "mproc")) 566 tracing |= TRACE_MPROC; 567 else if (!strcmp(optarg, "expand")) 568 tracing |= TRACE_EXPAND; 569 else if (!strcmp(optarg, "table") || 570 !strcmp(optarg, "tables")) 571 tracing |= TRACE_TABLES; 572 else if (!strcmp(optarg, "queue")) 573 tracing |= TRACE_QUEUE; 574 else if (!strcmp(optarg, "all")) 575 tracing |= ~TRACE_DEBUG; 576 else if (!strcmp(optarg, "profstat")) 577 profiling |= PROFILE_TOSTAT; 578 else if (!strcmp(optarg, "profile-imsg")) 579 profiling |= PROFILE_IMSG; 580 else if (!strcmp(optarg, "profile-queue")) 581 profiling |= PROFILE_QUEUE; 582 else 583 log_warnx("warn: unknown trace flag \"%s\"", 584 optarg); 585 break; 586 case 'P': 587 if (!strcmp(optarg, "smtp")) 588 flags |= SMTPD_SMTP_PAUSED; 589 else if (!strcmp(optarg, "mta")) 590 flags |= SMTPD_MTA_PAUSED; 591 else if (!strcmp(optarg, "mda")) 592 flags |= SMTPD_MDA_PAUSED; 593 break; 594 case 'v': 595 tracing |= TRACE_DEBUG; 596 break; 597 case 'x': 598 rexec = optarg; 599 break; 600 default: 601 usage(); 602 } 603 } 604 605 argv += optind; 606 argc -= optind; 607 608 if (argc || *argv) 609 usage(); 610 611 env->sc_opts |= opts; 612 613 tls_init(); 614 615 if (parse_config(conf, conffile, opts)) 616 exit(1); 617 618 if (strlcpy(env->sc_conffile, conffile, PATH_MAX) 619 >= PATH_MAX) 620 errx(1, "config file exceeds PATH_MAX"); 621 622 if (env->sc_opts & SMTPD_OPT_NOACTION) { 623 if (env->sc_queue_key && 624 crypto_setup(env->sc_queue_key, 625 strlen(env->sc_queue_key)) == 0) { 626 fatalx("crypto_setup:" 627 "invalid key for queue encryption"); 628 } 629 load_pki_tree(); 630 load_pki_keys(); 631 fprintf(stderr, "configuration OK\n"); 632 exit(0); 633 } 634 635 env->sc_flags |= flags; 636 637 /* check for root privileges */ 638 if (geteuid()) 639 errx(1, "need root privileges"); 640 641 log_init(foreground_log, LOG_MAIL); 642 log_trace_verbose(tracing); 643 load_pki_tree(); 644 load_pki_keys(); 645 646 log_debug("debug: using \"%s\" queue backend", backend_queue); 647 log_debug("debug: using \"%s\" scheduler backend", backend_scheduler); 648 log_debug("debug: using \"%s\" stat backend", backend_stat); 649 650 if (env->sc_hostname[0] == '\0') 651 errx(1, "machine does not have a hostname set"); 652 env->sc_uptime = time(NULL); 653 654 if (rexec == NULL) { 655 smtpd_process = PROC_PARENT; 656 657 if (env->sc_queue_flags & QUEUE_ENCRYPTION) { 658 if (env->sc_queue_key == NULL) { 659 char *password; 660 661 password = getpass("queue key: "); 662 if (password == NULL) 663 err(1, "getpass"); 664 665 env->sc_queue_key = strdup(password); 666 explicit_bzero(password, strlen(password)); 667 if (env->sc_queue_key == NULL) 668 err(1, "strdup"); 669 } 670 else { 671 char *buf = NULL; 672 size_t sz = 0; 673 ssize_t len; 674 675 if (strcasecmp(env->sc_queue_key, "stdin") == 0) { 676 if ((len = getline(&buf, &sz, stdin)) == -1) 677 err(1, "getline"); 678 if (buf[len - 1] == '\n') 679 buf[len - 1] = '\0'; 680 env->sc_queue_key = buf; 681 } 682 } 683 } 684 685 log_info("info: %s %s starting", SMTPD_NAME, SMTPD_VERSION); 686 687 if (!foreground) 688 if (daemon(0, 0) == -1) 689 err(1, "failed to daemonize"); 690 691 /* setup all processes */ 692 693 p_ca = start_child(save_argc, save_argv, "ca"); 694 p_ca->proc = PROC_CA; 695 696 p_control = start_child(save_argc, save_argv, "control"); 697 p_control->proc = PROC_CONTROL; 698 699 p_lka = start_child(save_argc, save_argv, "lka"); 700 p_lka->proc = PROC_LKA; 701 702 p_dispatcher = start_child(save_argc, save_argv, "dispatcher"); 703 p_dispatcher->proc = PROC_DISPATCHER; 704 705 p_queue = start_child(save_argc, save_argv, "queue"); 706 p_queue->proc = PROC_QUEUE; 707 708 p_scheduler = start_child(save_argc, save_argv, "scheduler"); 709 p_scheduler->proc = PROC_SCHEDULER; 710 711 setup_peers(p_control, p_ca); 712 setup_peers(p_control, p_lka); 713 setup_peers(p_control, p_dispatcher); 714 setup_peers(p_control, p_queue); 715 setup_peers(p_control, p_scheduler); 716 setup_peers(p_dispatcher, p_ca); 717 setup_peers(p_dispatcher, p_lka); 718 setup_peers(p_dispatcher, p_queue); 719 setup_peers(p_queue, p_lka); 720 setup_peers(p_queue, p_scheduler); 721 722 if (env->sc_queue_key) { 723 if (imsg_compose(&p_queue->imsgbuf, IMSG_SETUP_KEY, 0, 724 0, -1, env->sc_queue_key, strlen(env->sc_queue_key) 725 + 1) == -1) 726 fatal("imsg_compose"); 727 if (imsg_flush(&p_queue->imsgbuf) == -1) 728 fatal("imsg_flush"); 729 } 730 731 setup_done(p_ca); 732 setup_done(p_control); 733 setup_done(p_lka); 734 setup_done(p_dispatcher); 735 setup_done(p_queue); 736 setup_done(p_scheduler); 737 738 log_debug("smtpd: setup done"); 739 740 return smtpd(); 741 } 742 743 if (!strcmp(rexec, "ca")) { 744 smtpd_process = PROC_CA; 745 setup_proc(); 746 747 return ca(); 748 } 749 750 else if (!strcmp(rexec, "control")) { 751 smtpd_process = PROC_CONTROL; 752 setup_proc(); 753 754 /* the control socket ensures that only one smtpd instance is running */ 755 control_socket = control_create_socket(); 756 757 env->sc_stat = stat_backend_lookup(backend_stat); 758 if (env->sc_stat == NULL) 759 errx(1, "could not find stat backend \"%s\"", backend_stat); 760 761 return control(); 762 } 763 764 else if (!strcmp(rexec, "lka")) { 765 smtpd_process = PROC_LKA; 766 setup_proc(); 767 768 return lka(); 769 } 770 771 else if (!strcmp(rexec, "dispatcher")) { 772 smtpd_process = PROC_DISPATCHER; 773 setup_proc(); 774 775 return dispatcher(); 776 } 777 778 else if (!strcmp(rexec, "queue")) { 779 smtpd_process = PROC_QUEUE; 780 setup_proc(); 781 782 if (env->sc_queue_flags & QUEUE_COMPRESSION) 783 env->sc_comp = compress_backend_lookup("gzip"); 784 785 if (!queue_init(backend_queue, 1)) 786 errx(1, "could not initialize queue backend"); 787 788 return queue(); 789 } 790 791 else if (!strcmp(rexec, "scheduler")) { 792 smtpd_process = PROC_SCHEDULER; 793 setup_proc(); 794 795 for (i = 0; i < MAX_BOUNCE_WARN; i++) { 796 if (env->sc_bounce_warn[i] == 0) 797 break; 798 log_debug("debug: bounce warning after %s", 799 duration_to_text(env->sc_bounce_warn[i])); 800 } 801 802 return scheduler(); 803 } 804 805 fatalx("bad rexec: %s", rexec); 806 807 return (1); 808 } 809 810 static struct mproc * 811 start_child(int save_argc, char **save_argv, char *rexec) 812 { 813 struct mproc *p; 814 char *argv[SMTPD_MAXARG]; 815 int sp[2], argc = 0; 816 pid_t pid; 817 818 if (save_argc >= SMTPD_MAXARG - 2) 819 fatalx("too many arguments"); 820 821 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) 822 fatal("socketpair"); 823 824 io_set_nonblocking(sp[0]); 825 io_set_nonblocking(sp[1]); 826 827 switch (pid = fork()) { 828 case -1: 829 fatal("%s: fork", save_argv[0]); 830 case 0: 831 break; 832 default: 833 close(sp[0]); 834 p = calloc(1, sizeof(*p)); 835 if (p == NULL) 836 fatal("calloc"); 837 if((p->name = strdup(rexec)) == NULL) 838 fatal("strdup"); 839 mproc_init(p, sp[1]); 840 p->pid = pid; 841 p->handler = parent_imsg; 842 return p; 843 } 844 845 if (sp[0] != 3) { 846 if (dup2(sp[0], 3) == -1) 847 fatal("%s: dup2", rexec); 848 } else if (fcntl(sp[0], F_SETFD, 0) == -1) 849 fatal("%s: fcntl", rexec); 850 851 if (closefrom(4) == -1) 852 fatal("%s: closefrom", rexec); 853 854 for (argc = 0; argc < save_argc; argc++) 855 argv[argc] = save_argv[argc]; 856 argv[argc++] = "-x"; 857 argv[argc++] = rexec; 858 argv[argc++] = NULL; 859 860 execvp(argv[0], argv); 861 fatal("%s: execvp", rexec); 862 } 863 864 static void 865 setup_peers(struct mproc *a, struct mproc *b) 866 { 867 int sp[2]; 868 869 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) 870 fatal("socketpair"); 871 872 io_set_nonblocking(sp[0]); 873 io_set_nonblocking(sp[1]); 874 875 if (imsg_compose(&a->imsgbuf, IMSG_SETUP_PEER, b->proc, b->pid, sp[0], 876 NULL, 0) == -1) 877 fatal("imsg_compose"); 878 if (imsg_flush(&a->imsgbuf) == -1) 879 fatal("imsg_flush"); 880 881 if (imsg_compose(&b->imsgbuf, IMSG_SETUP_PEER, a->proc, a->pid, sp[1], 882 NULL, 0) == -1) 883 fatal("imsg_compose"); 884 if (imsg_flush(&b->imsgbuf) == -1) 885 fatal("imsg_flush"); 886 } 887 888 static void 889 setup_done(struct mproc *p) 890 { 891 struct imsg imsg; 892 893 if (imsg_compose(&p->imsgbuf, IMSG_SETUP_DONE, 0, 0, -1, NULL, 0) == -1) 894 fatal("imsg_compose"); 895 if (imsg_flush(&p->imsgbuf) == -1) 896 fatal("imsg_flush"); 897 898 if (imsg_wait(&p->imsgbuf, &imsg, 10000) == -1) 899 fatal("imsg_wait"); 900 901 if (imsg.hdr.type != IMSG_SETUP_DONE) 902 fatalx("expect IMSG_SETUP_DONE"); 903 904 log_debug("setup_done: %s[%d] done", p->name, p->pid); 905 906 imsg_free(&imsg); 907 } 908 909 static void 910 setup_proc(void) 911 { 912 struct imsgbuf *ibuf; 913 struct imsg imsg; 914 int setup = 1; 915 916 log_procinit(proc_title(smtpd_process)); 917 918 p_parent = calloc(1, sizeof(*p_parent)); 919 if (p_parent == NULL) 920 fatal("calloc"); 921 if((p_parent->name = strdup("parent")) == NULL) 922 fatal("strdup"); 923 p_parent->proc = PROC_PARENT; 924 p_parent->handler = imsg_dispatch; 925 mproc_init(p_parent, 3); 926 927 ibuf = &p_parent->imsgbuf; 928 929 while (setup) { 930 if (imsg_wait(ibuf, &imsg, 10000) == -1) 931 fatal("imsg_wait"); 932 933 switch (imsg.hdr.type) { 934 case IMSG_SETUP_KEY: 935 env->sc_queue_key = strdup(imsg.data); 936 break; 937 case IMSG_SETUP_PEER: 938 setup_peer(imsg.hdr.peerid, imsg.hdr.pid, imsg.fd); 939 break; 940 case IMSG_SETUP_DONE: 941 setup = 0; 942 break; 943 default: 944 fatal("bad imsg %d", imsg.hdr.type); 945 } 946 imsg_free(&imsg); 947 } 948 949 if (imsg_compose(ibuf, IMSG_SETUP_DONE, 0, 0, -1, NULL, 0) == -1) 950 fatal("imsg_compose"); 951 952 if (imsg_flush(ibuf) == -1) 953 fatal("imsg_flush"); 954 955 log_debug("setup_proc: %s done", proc_title(smtpd_process)); 956 } 957 958 static struct mproc * 959 setup_peer(enum smtp_proc_type proc, pid_t pid, int sock) 960 { 961 struct mproc *p, **pp; 962 963 log_debug("setup_peer: %s -> %s[%u] fd=%d", proc_title(smtpd_process), 964 proc_title(proc), pid, sock); 965 966 if (sock == -1) 967 fatalx("peer socket not received"); 968 969 switch (proc) { 970 case PROC_LKA: 971 pp = &p_lka; 972 break; 973 case PROC_QUEUE: 974 pp = &p_queue; 975 break; 976 case PROC_CONTROL: 977 pp = &p_control; 978 break; 979 case PROC_SCHEDULER: 980 pp = &p_scheduler; 981 break; 982 case PROC_DISPATCHER: 983 pp = &p_dispatcher; 984 break; 985 case PROC_CA: 986 pp = &p_ca; 987 break; 988 default: 989 fatalx("unknown peer"); 990 } 991 992 if (*pp) 993 fatalx("peer already set"); 994 995 p = calloc(1, sizeof(*p)); 996 if (p == NULL) 997 fatal("calloc"); 998 if((p->name = strdup(proc_title(proc))) == NULL) 999 fatal("strdup"); 1000 mproc_init(p, sock); 1001 p->pid = pid; 1002 p->proc = proc; 1003 p->handler = imsg_dispatch; 1004 1005 *pp = p; 1006 1007 return p; 1008 } 1009 1010 static int 1011 imsg_wait(struct imsgbuf *ibuf, struct imsg *imsg, int timeout) 1012 { 1013 struct pollfd pfd[1]; 1014 ssize_t n; 1015 1016 pfd[0].fd = ibuf->fd; 1017 pfd[0].events = POLLIN; 1018 1019 while (1) { 1020 if ((n = imsg_get(ibuf, imsg)) == -1) 1021 return -1; 1022 if (n) 1023 return 1; 1024 1025 n = poll(pfd, 1, timeout); 1026 if (n == -1) 1027 return -1; 1028 if (n == 0) { 1029 errno = ETIMEDOUT; 1030 return -1; 1031 } 1032 1033 if (((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) || n == 0) 1034 return -1; 1035 } 1036 } 1037 1038 int 1039 smtpd(void) { 1040 struct event ev_sigint; 1041 struct event ev_sigterm; 1042 struct event ev_sigchld; 1043 struct event ev_sighup; 1044 struct timeval tv; 1045 1046 imsg_callback = parent_imsg; 1047 1048 tree_init(&children); 1049 1050 child_add(p_queue->pid, CHILD_DAEMON, proc_title(PROC_QUEUE)); 1051 child_add(p_control->pid, CHILD_DAEMON, proc_title(PROC_CONTROL)); 1052 child_add(p_lka->pid, CHILD_DAEMON, proc_title(PROC_LKA)); 1053 child_add(p_scheduler->pid, CHILD_DAEMON, proc_title(PROC_SCHEDULER)); 1054 child_add(p_dispatcher->pid, CHILD_DAEMON, proc_title(PROC_DISPATCHER)); 1055 child_add(p_ca->pid, CHILD_DAEMON, proc_title(PROC_CA)); 1056 1057 event_init(); 1058 1059 signal_set(&ev_sigint, SIGINT, parent_sig_handler, NULL); 1060 signal_set(&ev_sigterm, SIGTERM, parent_sig_handler, NULL); 1061 signal_set(&ev_sigchld, SIGCHLD, parent_sig_handler, NULL); 1062 signal_set(&ev_sighup, SIGHUP, parent_sig_handler, NULL); 1063 signal_add(&ev_sigint, NULL); 1064 signal_add(&ev_sigterm, NULL); 1065 signal_add(&ev_sigchld, NULL); 1066 signal_add(&ev_sighup, NULL); 1067 signal(SIGPIPE, SIG_IGN); 1068 1069 config_peer(PROC_CONTROL); 1070 config_peer(PROC_LKA); 1071 config_peer(PROC_QUEUE); 1072 config_peer(PROC_CA); 1073 config_peer(PROC_DISPATCHER); 1074 1075 evtimer_set(&config_ev, parent_send_config, NULL); 1076 memset(&tv, 0, sizeof(tv)); 1077 evtimer_add(&config_ev, &tv); 1078 1079 /* defer offline scanning for a second */ 1080 evtimer_set(&offline_ev, offline_scan, NULL); 1081 offline_timeout.tv_sec = 1; 1082 offline_timeout.tv_usec = 0; 1083 evtimer_add(&offline_ev, &offline_timeout); 1084 1085 fork_filter_processes(); 1086 1087 purge_task(); 1088 1089 if (pledge("stdio rpath wpath cpath fattr tmppath " 1090 "getpw sendfd proc exec id inet chown unix", NULL) == -1) 1091 err(1, "pledge"); 1092 1093 event_dispatch(); 1094 fatalx("exited event loop"); 1095 1096 return (0); 1097 } 1098 1099 static void 1100 load_pki_tree(void) 1101 { 1102 struct pki *pki; 1103 struct ca *sca; 1104 const char *k; 1105 void *iter_dict; 1106 1107 log_debug("debug: init ssl-tree"); 1108 iter_dict = NULL; 1109 while (dict_iter(env->sc_pki_dict, &iter_dict, &k, (void **)&pki)) { 1110 log_debug("info: loading pki information for %s", k); 1111 if (pki->pki_cert_file == NULL) 1112 fatalx("load_pki_tree: missing certificate file"); 1113 if (pki->pki_key_file == NULL) 1114 fatalx("load_pki_tree: missing key file"); 1115 1116 if (!ssl_load_certificate(pki, pki->pki_cert_file)) 1117 fatalx("load_pki_tree: failed to load certificate file"); 1118 } 1119 1120 log_debug("debug: init ca-tree"); 1121 iter_dict = NULL; 1122 while (dict_iter(env->sc_ca_dict, &iter_dict, &k, (void **)&sca)) { 1123 log_debug("info: loading CA information for %s", k); 1124 if (!ssl_load_cafile(sca, sca->ca_cert_file)) 1125 fatalx("load_pki_tree: failed to load CA file"); 1126 } 1127 } 1128 1129 void 1130 load_pki_keys(void) 1131 { 1132 struct pki *pki; 1133 const char *k; 1134 void *iter_dict; 1135 1136 log_debug("debug: init ssl-tree"); 1137 iter_dict = NULL; 1138 while (dict_iter(env->sc_pki_dict, &iter_dict, &k, (void **)&pki)) { 1139 log_debug("info: loading pki keys for %s", k); 1140 1141 if (!ssl_load_keyfile(pki, pki->pki_key_file, k)) 1142 fatalx("load_pki_keys: failed to load key file"); 1143 } 1144 } 1145 1146 int 1147 fork_proc_backend(const char *key, const char *conf, const char *procname) 1148 { 1149 pid_t pid; 1150 int sp[2]; 1151 char path[PATH_MAX]; 1152 char name[PATH_MAX]; 1153 char *arg; 1154 1155 if (strlcpy(name, conf, sizeof(name)) >= sizeof(name)) { 1156 log_warnx("warn: %s-proc: conf too long", key); 1157 return (0); 1158 } 1159 1160 arg = strchr(name, ':'); 1161 if (arg) 1162 *arg++ = '\0'; 1163 1164 if (snprintf(path, sizeof(path), PATH_LIBEXEC "/%s-%s", key, name) >= 1165 (ssize_t)sizeof(path)) { 1166 log_warn("warn: %s-proc: exec path too long", key); 1167 return (-1); 1168 } 1169 1170 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) { 1171 log_warn("warn: %s-proc: socketpair", key); 1172 return (-1); 1173 } 1174 1175 if ((pid = fork()) == -1) { 1176 log_warn("warn: %s-proc: fork", key); 1177 close(sp[0]); 1178 close(sp[1]); 1179 return (-1); 1180 } 1181 1182 if (pid == 0) { 1183 /* child process */ 1184 dup2(sp[0], STDIN_FILENO); 1185 if (closefrom(STDERR_FILENO + 1) == -1) 1186 exit(1); 1187 1188 if (procname == NULL) 1189 procname = name; 1190 1191 execl(path, procname, arg, (char *)NULL); 1192 err(1, "execl: %s", path); 1193 } 1194 1195 /* parent process */ 1196 close(sp[0]); 1197 1198 return (sp[1]); 1199 } 1200 1201 struct child * 1202 child_add(pid_t pid, int type, const char *title) 1203 { 1204 struct child *child; 1205 1206 if ((child = calloc(1, sizeof(*child))) == NULL) 1207 fatal("smtpd: child_add: calloc"); 1208 1209 child->pid = pid; 1210 child->type = type; 1211 child->title = title; 1212 1213 tree_xset(&children, pid, child); 1214 1215 return (child); 1216 } 1217 1218 static void 1219 purge_task(void) 1220 { 1221 struct passwd *pw; 1222 DIR *d; 1223 int n; 1224 uid_t uid; 1225 gid_t gid; 1226 1227 n = 0; 1228 if ((d = opendir(PATH_SPOOL PATH_PURGE))) { 1229 while (readdir(d) != NULL) 1230 n++; 1231 closedir(d); 1232 } else 1233 log_warn("warn: purge_task: opendir"); 1234 1235 if (n > 2) { 1236 switch (purge_pid = fork()) { 1237 case -1: 1238 log_warn("warn: purge_task: fork"); 1239 break; 1240 case 0: 1241 if ((pw = getpwnam(SMTPD_QUEUE_USER)) == NULL) 1242 fatalx("unknown user " SMTPD_QUEUE_USER); 1243 if (chroot(PATH_SPOOL PATH_PURGE) == -1) 1244 fatal("smtpd: chroot"); 1245 if (chdir("/") == -1) 1246 fatal("smtpd: chdir"); 1247 uid = pw->pw_uid; 1248 gid = pw->pw_gid; 1249 if (setgroups(1, &gid) || 1250 setresgid(gid, gid, gid) || 1251 setresuid(uid, uid, uid)) 1252 fatal("smtpd: cannot drop privileges"); 1253 rmtree("/", 1); 1254 _exit(0); 1255 break; 1256 default: 1257 break; 1258 } 1259 } 1260 } 1261 1262 static void 1263 fork_filter_processes(void) 1264 { 1265 const char *name; 1266 void *iter; 1267 const char *fn; 1268 struct filter_config *fc; 1269 struct filter_config *fcs; 1270 struct filter_proc *fp; 1271 size_t i; 1272 1273 /* For each filter chain, assign the registered subsystem to subfilters */ 1274 iter = NULL; 1275 while (dict_iter(env->sc_filters_dict, &iter, (const char **)&fn, (void **)&fc)) { 1276 if (fc->chain) { 1277 for (i = 0; i < fc->chain_size; ++i) { 1278 fcs = dict_xget(env->sc_filters_dict, fc->chain[i]); 1279 fcs->filter_subsystem |= fc->filter_subsystem; 1280 } 1281 } 1282 } 1283 1284 /* For each filter, assign the registered subsystem to underlying proc */ 1285 iter = NULL; 1286 while (dict_iter(env->sc_filters_dict, &iter, (const char **)&fn, (void **)&fc)) { 1287 if (fc->proc) { 1288 fp = dict_xget(env->sc_filter_processes_dict, fc->proc); 1289 fp->filter_subsystem |= fc->filter_subsystem; 1290 } 1291 } 1292 1293 iter = NULL; 1294 while (dict_iter(env->sc_filter_processes_dict, &iter, &name, (void **)&fp)) 1295 fork_filter_process(name, fp->command, fp->user, fp->group, fp->chroot, fp->filter_subsystem); 1296 } 1297 1298 static void 1299 fork_filter_process(const char *name, const char *command, const char *user, const char *group, const char *chroot_path, uint32_t subsystems) 1300 { 1301 pid_t pid; 1302 struct filter_proc *processor; 1303 char buf; 1304 int sp[2], errfd[2]; 1305 struct passwd *pw; 1306 struct group *gr; 1307 char exec[_POSIX_ARG_MAX]; 1308 int execr; 1309 1310 if (user == NULL) 1311 user = SMTPD_USER; 1312 if ((pw = getpwnam(user)) == NULL) 1313 err(1, "getpwnam"); 1314 1315 if (group) { 1316 if ((gr = getgrnam(group)) == NULL) 1317 err(1, "getgrnam"); 1318 } 1319 else { 1320 if ((gr = getgrgid(pw->pw_gid)) == NULL) 1321 err(1, "getgrgid"); 1322 } 1323 1324 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) 1325 err(1, "socketpair"); 1326 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, errfd) == -1) 1327 err(1, "socketpair"); 1328 1329 if ((pid = fork()) == -1) 1330 err(1, "fork"); 1331 1332 /* parent passes the child fd over to lka */ 1333 if (pid > 0) { 1334 processor = dict_xget(env->sc_filter_processes_dict, name); 1335 processor->errfd = errfd[1]; 1336 child_add(pid, CHILD_PROCESSOR, name); 1337 close(sp[0]); 1338 close(errfd[0]); 1339 m_create(p_lka, IMSG_LKA_PROCESSOR_FORK, 0, 0, sp[1]); 1340 m_add_string(p_lka, name); 1341 m_add_u32(p_lka, (uint32_t)subsystems); 1342 m_close(p_lka); 1343 return; 1344 } 1345 1346 close(sp[1]); 1347 close(errfd[1]); 1348 dup2(sp[0], STDIN_FILENO); 1349 dup2(sp[0], STDOUT_FILENO); 1350 dup2(errfd[0], STDERR_FILENO); 1351 1352 if (chroot_path) { 1353 if (chroot(chroot_path) != 0 || chdir("/") != 0) 1354 err(1, "chroot: %s", chroot_path); 1355 } 1356 1357 if (setgroups(1, &gr->gr_gid) || 1358 setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid) || 1359 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 1360 err(1, "fork_filter_process: cannot drop privileges"); 1361 1362 if (closefrom(STDERR_FILENO + 1) == -1) 1363 err(1, "closefrom"); 1364 if (setsid() == -1) 1365 err(1, "setsid"); 1366 if (signal(SIGPIPE, SIG_DFL) == SIG_ERR || 1367 signal(SIGINT, SIG_DFL) == SIG_ERR || 1368 signal(SIGTERM, SIG_DFL) == SIG_ERR || 1369 signal(SIGCHLD, SIG_DFL) == SIG_ERR || 1370 signal(SIGHUP, SIG_DFL) == SIG_ERR) 1371 err(1, "signal"); 1372 1373 if (command[0] == '/') 1374 execr = snprintf(exec, sizeof(exec), "exec %s", command); 1375 else 1376 execr = snprintf(exec, sizeof(exec), "exec %s/%s", 1377 PATH_LIBEXEC, command); 1378 if (execr >= (int) sizeof(exec)) 1379 errx(1, "%s: exec path too long", name); 1380 1381 /* 1382 * Wait for lka to acknowledge that it received the fd. 1383 * This prevents a race condition between the filter sending an error 1384 * message, and exiting and lka not being able to log it because of 1385 * SIGCHLD. 1386 * (Ab)use read to determine if the fd is installed; since stderr is 1387 * never going to be read from we can shutdown(2) the write-end in lka. 1388 */ 1389 if (read(STDERR_FILENO, &buf, 1) != 0) 1390 errx(1, "lka didn't properly close write end of error socket"); 1391 if (system(exec) == -1) 1392 err(1, NULL); 1393 1394 /* there's no successful exit from a processor */ 1395 _exit(1); 1396 } 1397 1398 static void 1399 forkmda(struct mproc *p, uint64_t id, struct deliver *deliver) 1400 { 1401 char ebuf[128], sfn[32]; 1402 struct dispatcher *dsp; 1403 struct child *child; 1404 pid_t pid; 1405 int allout, pipefd[2]; 1406 struct passwd *pw; 1407 const char *pw_name; 1408 uid_t pw_uid; 1409 gid_t pw_gid; 1410 const char *pw_dir; 1411 1412 dsp = dict_xget(env->sc_dispatchers, deliver->dispatcher); 1413 if (dsp->type != DISPATCHER_LOCAL) 1414 fatalx("non-local dispatcher called from forkmda()"); 1415 1416 log_debug("debug: smtpd: forking mda for session %016"PRIx64 1417 ": %s as %s", id, deliver->userinfo.username, 1418 dsp->u.local.user ? dsp->u.local.user : deliver->userinfo.username); 1419 1420 if (dsp->u.local.user) { 1421 if ((pw = getpwnam(dsp->u.local.user)) == NULL) { 1422 (void)snprintf(ebuf, sizeof ebuf, 1423 "delivery user '%s' does not exist", 1424 dsp->u.local.user); 1425 m_create(p_dispatcher, IMSG_MDA_DONE, 0, 0, -1); 1426 m_add_id(p_dispatcher, id); 1427 m_add_int(p_dispatcher, MDA_PERMFAIL); 1428 m_add_int(p_dispatcher, EX_NOUSER); 1429 m_add_string(p_dispatcher, ebuf); 1430 m_close(p_dispatcher); 1431 return; 1432 } 1433 pw_name = pw->pw_name; 1434 pw_uid = pw->pw_uid; 1435 pw_gid = pw->pw_gid; 1436 pw_dir = pw->pw_dir; 1437 } 1438 else { 1439 pw_name = deliver->userinfo.username; 1440 pw_uid = deliver->userinfo.uid; 1441 pw_gid = deliver->userinfo.gid; 1442 pw_dir = deliver->userinfo.directory; 1443 } 1444 1445 if (pw_uid == 0 && deliver->mda_exec[0]) { 1446 pw_name = deliver->userinfo.username; 1447 pw_uid = deliver->userinfo.uid; 1448 pw_gid = deliver->userinfo.gid; 1449 pw_dir = deliver->userinfo.directory; 1450 } 1451 1452 if (pw_uid == 0 && !dsp->u.local.is_mbox) { 1453 (void)snprintf(ebuf, sizeof ebuf, "not allowed to deliver to: %s", 1454 deliver->userinfo.username); 1455 m_create(p_dispatcher, IMSG_MDA_DONE, 0, 0, -1); 1456 m_add_id(p_dispatcher, id); 1457 m_add_int(p_dispatcher, MDA_PERMFAIL); 1458 m_add_int(p_dispatcher, EX_NOPERM); 1459 m_add_string(p_dispatcher, ebuf); 1460 m_close(p_dispatcher); 1461 return; 1462 } 1463 1464 if (pipe(pipefd) == -1) { 1465 (void)snprintf(ebuf, sizeof ebuf, "pipe: %s", strerror(errno)); 1466 m_create(p_dispatcher, IMSG_MDA_DONE, 0, 0, -1); 1467 m_add_id(p_dispatcher, id); 1468 m_add_int(p_dispatcher, MDA_TEMPFAIL); 1469 m_add_int(p_dispatcher, EX_OSERR); 1470 m_add_string(p_dispatcher, ebuf); 1471 m_close(p_dispatcher); 1472 return; 1473 } 1474 1475 /* prepare file which captures stdout and stderr */ 1476 (void)strlcpy(sfn, "/tmp/smtpd.out.XXXXXXXXXXX", sizeof(sfn)); 1477 allout = mkstemp(sfn); 1478 if (allout == -1) { 1479 (void)snprintf(ebuf, sizeof ebuf, "mkstemp: %s", strerror(errno)); 1480 m_create(p_dispatcher, IMSG_MDA_DONE, 0, 0, -1); 1481 m_add_id(p_dispatcher, id); 1482 m_add_int(p_dispatcher, MDA_TEMPFAIL); 1483 m_add_int(p_dispatcher, EX_OSERR); 1484 m_add_string(p_dispatcher, ebuf); 1485 m_close(p_dispatcher); 1486 close(pipefd[0]); 1487 close(pipefd[1]); 1488 return; 1489 } 1490 unlink(sfn); 1491 1492 pid = fork(); 1493 if (pid == -1) { 1494 (void)snprintf(ebuf, sizeof ebuf, "fork: %s", strerror(errno)); 1495 m_create(p_dispatcher, IMSG_MDA_DONE, 0, 0, -1); 1496 m_add_id(p_dispatcher, id); 1497 m_add_int(p_dispatcher, MDA_TEMPFAIL); 1498 m_add_int(p_dispatcher, EX_OSERR); 1499 m_add_string(p_dispatcher, ebuf); 1500 m_close(p_dispatcher); 1501 close(pipefd[0]); 1502 close(pipefd[1]); 1503 close(allout); 1504 return; 1505 } 1506 1507 /* parent passes the child fd over to mda */ 1508 if (pid > 0) { 1509 child = child_add(pid, CHILD_MDA, NULL); 1510 child->mda_out = allout; 1511 child->mda_id = id; 1512 close(pipefd[0]); 1513 m_create(p, IMSG_MDA_FORK, 0, 0, pipefd[1]); 1514 m_add_id(p, id); 1515 m_close(p); 1516 return; 1517 } 1518 1519 /* mbox helper, create mailbox before privdrop if it doesn't exist */ 1520 if (dsp->u.local.is_mbox) 1521 mda_mbox_init(deliver); 1522 1523 if (chdir(pw_dir) == -1 && chdir("/") == -1) 1524 err(1, "chdir"); 1525 if (setgroups(1, &pw_gid) || 1526 setresgid(pw_gid, pw_gid, pw_gid) || 1527 setresuid(pw_uid, pw_uid, pw_uid)) 1528 err(1, "forkmda: cannot drop privileges"); 1529 if (dup2(pipefd[0], STDIN_FILENO) == -1 || 1530 dup2(allout, STDOUT_FILENO) == -1 || 1531 dup2(allout, STDERR_FILENO) == -1) 1532 err(1, "forkmda: dup2"); 1533 if (closefrom(STDERR_FILENO + 1) == -1) 1534 err(1, "closefrom"); 1535 if (setsid() == -1) 1536 err(1, "setsid"); 1537 if (signal(SIGPIPE, SIG_DFL) == SIG_ERR || 1538 signal(SIGINT, SIG_DFL) == SIG_ERR || 1539 signal(SIGTERM, SIG_DFL) == SIG_ERR || 1540 signal(SIGCHLD, SIG_DFL) == SIG_ERR || 1541 signal(SIGHUP, SIG_DFL) == SIG_ERR) 1542 err(1, "signal"); 1543 1544 /* avoid hangs by setting 5m timeout */ 1545 alarm(300); 1546 1547 if (dsp->u.local.is_mbox && 1548 dsp->u.local.mda_wrapper == NULL && 1549 deliver->mda_exec[0] == '\0') 1550 mda_mbox(deliver); 1551 else 1552 mda_unpriv(dsp, deliver, pw_name, pw_dir); 1553 } 1554 1555 static void 1556 offline_scan(int fd, short ev, void *arg) 1557 { 1558 char *path_argv[2]; 1559 FTS *fts = arg; 1560 FTSENT *e; 1561 int n = 0; 1562 1563 path_argv[0] = PATH_SPOOL PATH_OFFLINE; 1564 path_argv[1] = NULL; 1565 1566 if (fts == NULL) { 1567 log_debug("debug: smtpd: scanning offline queue..."); 1568 fts = fts_open(path_argv, FTS_PHYSICAL | FTS_NOCHDIR, NULL); 1569 if (fts == NULL) { 1570 log_warn("fts_open: %s", path_argv[0]); 1571 return; 1572 } 1573 } 1574 1575 while ((e = fts_read(fts)) != NULL) { 1576 if (e->fts_info != FTS_F) 1577 continue; 1578 1579 /* offline files must be at depth 1 */ 1580 if (e->fts_level != 1) 1581 continue; 1582 1583 /* offline file group must match parent directory group */ 1584 if (e->fts_statp->st_gid != e->fts_parent->fts_statp->st_gid) 1585 continue; 1586 1587 if (e->fts_statp->st_size == 0) { 1588 if (unlink(e->fts_accpath) == -1) 1589 log_warnx("warn: smtpd: could not unlink %s", e->fts_accpath); 1590 continue; 1591 } 1592 1593 if (offline_add(e->fts_name, e->fts_statp->st_uid, 1594 e->fts_statp->st_gid)) { 1595 log_warnx("warn: smtpd: " 1596 "could not add offline message %s", e->fts_name); 1597 continue; 1598 } 1599 1600 if ((n++) == OFFLINE_READMAX) { 1601 evtimer_set(&offline_ev, offline_scan, fts); 1602 offline_timeout.tv_sec = 0; 1603 offline_timeout.tv_usec = 100000; 1604 evtimer_add(&offline_ev, &offline_timeout); 1605 return; 1606 } 1607 } 1608 1609 log_debug("debug: smtpd: offline scanning done"); 1610 fts_close(fts); 1611 } 1612 1613 static int 1614 offline_enqueue(char *name, uid_t uid, gid_t gid) 1615 { 1616 char *path; 1617 struct stat sb; 1618 pid_t pid; 1619 struct child *child; 1620 struct passwd *pw; 1621 int pathlen; 1622 1623 pathlen = asprintf(&path, "%s/%s", PATH_SPOOL PATH_OFFLINE, name); 1624 if (pathlen == -1) { 1625 log_warnx("warn: smtpd: asprintf"); 1626 return (-1); 1627 } 1628 1629 if (pathlen >= PATH_MAX) { 1630 log_warnx("warn: smtpd: pathname exceeds PATH_MAX"); 1631 free(path); 1632 return (-1); 1633 } 1634 1635 log_debug("debug: smtpd: enqueueing offline message %s", path); 1636 1637 if ((pid = fork()) == -1) { 1638 log_warn("warn: smtpd: fork"); 1639 free(path); 1640 return (-1); 1641 } 1642 1643 if (pid == 0) { 1644 char *envp[2], *p = NULL, *tmp; 1645 int fd; 1646 FILE *fp; 1647 size_t sz = 0; 1648 ssize_t len; 1649 arglist args; 1650 1651 if (closefrom(STDERR_FILENO + 1) == -1) 1652 _exit(1); 1653 1654 memset(&args, 0, sizeof(args)); 1655 1656 if ((fd = open(path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK)) == -1) { 1657 log_warn("warn: smtpd: open: %s", path); 1658 _exit(1); 1659 } 1660 1661 if (fstat(fd, &sb) == -1) { 1662 log_warn("warn: smtpd: fstat: %s", path); 1663 _exit(1); 1664 } 1665 1666 if (!S_ISREG(sb.st_mode)) { 1667 log_warnx("warn: smtpd: file %s (uid %d) not regular", 1668 path, sb.st_uid); 1669 _exit(1); 1670 } 1671 1672 if (sb.st_nlink != 1) { 1673 log_warnx("warn: smtpd: file %s is hard-link", path); 1674 _exit(1); 1675 } 1676 1677 if (sb.st_uid != uid) { 1678 log_warnx("warn: smtpd: file %s has bad uid %d", 1679 path, sb.st_uid); 1680 _exit(1); 1681 } 1682 1683 if (sb.st_gid != gid) { 1684 log_warnx("warn: smtpd: file %s has bad gid %d", 1685 path, sb.st_gid); 1686 _exit(1); 1687 } 1688 1689 pw = getpwuid(sb.st_uid); 1690 if (pw == NULL) { 1691 log_warnx("warn: smtpd: getpwuid for uid %d failed", 1692 sb.st_uid); 1693 _exit(1); 1694 } 1695 1696 if (setgroups(1, &pw->pw_gid) || 1697 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 1698 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 1699 _exit(1); 1700 1701 if ((fp = fdopen(fd, "r")) == NULL) 1702 _exit(1); 1703 1704 if (chdir(pw->pw_dir) == -1 && chdir("/") == -1) 1705 _exit(1); 1706 1707 if (setsid() == -1 || 1708 signal(SIGPIPE, SIG_DFL) == SIG_ERR || 1709 dup2(fileno(fp), STDIN_FILENO) == -1) 1710 _exit(1); 1711 1712 if ((len = getline(&p, &sz, fp)) == -1) 1713 _exit(1); 1714 1715 if (p[len - 1] != '\n') 1716 _exit(1); 1717 p[len - 1] = '\0'; 1718 1719 addargs(&args, "%s", "sendmail"); 1720 addargs(&args, "%s", "-S"); 1721 1722 while ((tmp = strsep(&p, "|")) != NULL) 1723 addargs(&args, "%s", tmp); 1724 1725 free(p); 1726 if (lseek(fileno(fp), len, SEEK_SET) == -1) 1727 _exit(1); 1728 1729 envp[0] = "PATH=" _PATH_DEFPATH; 1730 envp[1] = (char *)NULL; 1731 environ = envp; 1732 1733 execvp(PATH_SMTPCTL, args.list); 1734 _exit(1); 1735 } 1736 1737 offline_running++; 1738 child = child_add(pid, CHILD_ENQUEUE_OFFLINE, NULL); 1739 child->path = path; 1740 1741 return (0); 1742 } 1743 1744 static int 1745 offline_add(char *path, uid_t uid, gid_t gid) 1746 { 1747 struct offline *q; 1748 1749 if (offline_running < OFFLINE_QUEUEMAX) 1750 /* skip queue */ 1751 return offline_enqueue(path, uid, gid); 1752 1753 q = malloc(sizeof(*q) + strlen(path) + 1); 1754 if (q == NULL) 1755 return (-1); 1756 q->uid = uid; 1757 q->gid = gid; 1758 q->path = (char *)q + sizeof(*q); 1759 memmove(q->path, path, strlen(path) + 1); 1760 TAILQ_INSERT_TAIL(&offline_q, q, entry); 1761 1762 return (0); 1763 } 1764 1765 static void 1766 offline_done(void) 1767 { 1768 struct offline *q; 1769 1770 offline_running--; 1771 1772 while (offline_running < OFFLINE_QUEUEMAX) { 1773 if ((q = TAILQ_FIRST(&offline_q)) == NULL) 1774 break; /* all done */ 1775 TAILQ_REMOVE(&offline_q, q, entry); 1776 offline_enqueue(q->path, q->uid, q->gid); 1777 free(q); 1778 } 1779 } 1780 1781 static int 1782 parent_forward_open(char *username, char *directory, uid_t uid, gid_t gid) 1783 { 1784 char pathname[PATH_MAX]; 1785 int fd; 1786 struct stat sb; 1787 1788 if (!bsnprintf(pathname, sizeof (pathname), "%s/.forward", 1789 directory)) { 1790 log_warnx("warn: smtpd: %s: pathname too large", pathname); 1791 return -1; 1792 } 1793 1794 if (stat(directory, &sb) == -1) { 1795 log_warn("warn: smtpd: parent_forward_open: %s", directory); 1796 return -1; 1797 } 1798 if (sb.st_mode & S_ISVTX) { 1799 log_warnx("warn: smtpd: parent_forward_open: %s is sticky", 1800 directory); 1801 errno = EAGAIN; 1802 return -1; 1803 } 1804 1805 do { 1806 fd = open(pathname, O_RDONLY|O_NOFOLLOW|O_NONBLOCK); 1807 } while (fd == -1 && errno == EINTR); 1808 if (fd == -1) { 1809 if (errno == ENOENT) 1810 return -1; 1811 if (errno == EMFILE || errno == ENFILE || errno == EIO) { 1812 errno = EAGAIN; 1813 return -1; 1814 } 1815 if (errno == ELOOP) 1816 log_warnx("warn: smtpd: parent_forward_open: %s: " 1817 "cannot follow symbolic links", pathname); 1818 else 1819 log_warn("warn: smtpd: parent_forward_open: %s", pathname); 1820 return -1; 1821 } 1822 1823 if (!secure_file(fd, pathname, directory, uid, 1)) { 1824 log_warnx("warn: smtpd: %s: unsecure file", pathname); 1825 close(fd); 1826 return -1; 1827 } 1828 1829 return fd; 1830 } 1831 1832 void 1833 imsg_dispatch(struct mproc *p, struct imsg *imsg) 1834 { 1835 struct timespec t0, t1, dt; 1836 int msg; 1837 1838 if (imsg == NULL) { 1839 imsg_callback(p, imsg); 1840 return; 1841 } 1842 1843 log_imsg(smtpd_process, p->proc, imsg); 1844 1845 if (profiling & PROFILE_IMSG) 1846 clock_gettime(CLOCK_MONOTONIC, &t0); 1847 1848 msg = imsg->hdr.type; 1849 imsg_callback(p, imsg); 1850 1851 if (profiling & PROFILE_IMSG) { 1852 clock_gettime(CLOCK_MONOTONIC, &t1); 1853 timespecsub(&t1, &t0, &dt); 1854 1855 log_debug("profile-imsg: %s %s %s %d %lld.%09ld", 1856 proc_name(smtpd_process), 1857 proc_name(p->proc), 1858 imsg_to_str(msg), 1859 (int)imsg->hdr.len, 1860 (long long)dt.tv_sec, 1861 dt.tv_nsec); 1862 1863 if (profiling & PROFILE_TOSTAT) { 1864 char key[STAT_KEY_SIZE]; 1865 /* can't profstat control process yet */ 1866 if (smtpd_process == PROC_CONTROL) 1867 return; 1868 1869 if (!bsnprintf(key, sizeof key, 1870 "profiling.imsg.%s.%s.%s", 1871 proc_name(smtpd_process), 1872 proc_name(p->proc), 1873 imsg_to_str(msg))) 1874 return; 1875 stat_set(key, stat_timespec(&dt)); 1876 } 1877 } 1878 } 1879 1880 void 1881 log_imsg(int to, int from, struct imsg *imsg) 1882 { 1883 1884 if (to == PROC_CONTROL && imsg->hdr.type == IMSG_STAT_SET) 1885 return; 1886 1887 if (imsg->fd != -1) 1888 log_trace(TRACE_IMSG, "imsg: %s <- %s: %s (len=%zu, fd=%d)", 1889 proc_name(to), 1890 proc_name(from), 1891 imsg_to_str(imsg->hdr.type), 1892 imsg->hdr.len - IMSG_HEADER_SIZE, 1893 imsg->fd); 1894 else 1895 log_trace(TRACE_IMSG, "imsg: %s <- %s: %s (len=%zu)", 1896 proc_name(to), 1897 proc_name(from), 1898 imsg_to_str(imsg->hdr.type), 1899 imsg->hdr.len - IMSG_HEADER_SIZE); 1900 } 1901 1902 const char * 1903 proc_title(enum smtp_proc_type proc) 1904 { 1905 switch (proc) { 1906 case PROC_PARENT: 1907 return "[priv]"; 1908 case PROC_LKA: 1909 return "lookup"; 1910 case PROC_QUEUE: 1911 return "queue"; 1912 case PROC_CONTROL: 1913 return "control"; 1914 case PROC_SCHEDULER: 1915 return "scheduler"; 1916 case PROC_DISPATCHER: 1917 return "dispatcher"; 1918 case PROC_CA: 1919 return "crypto"; 1920 case PROC_CLIENT: 1921 return "client"; 1922 case PROC_PROCESSOR: 1923 return "processor"; 1924 } 1925 return "unknown"; 1926 } 1927 1928 const char * 1929 proc_name(enum smtp_proc_type proc) 1930 { 1931 switch (proc) { 1932 case PROC_PARENT: 1933 return "parent"; 1934 case PROC_LKA: 1935 return "lka"; 1936 case PROC_QUEUE: 1937 return "queue"; 1938 case PROC_CONTROL: 1939 return "control"; 1940 case PROC_SCHEDULER: 1941 return "scheduler"; 1942 case PROC_DISPATCHER: 1943 return "dispatcher"; 1944 case PROC_CA: 1945 return "ca"; 1946 case PROC_CLIENT: 1947 return "client-proc"; 1948 default: 1949 return "unknown"; 1950 } 1951 } 1952 1953 #define CASE(x) case x : return #x 1954 1955 const char * 1956 imsg_to_str(int type) 1957 { 1958 static char buf[32]; 1959 1960 switch (type) { 1961 CASE(IMSG_NONE); 1962 1963 CASE(IMSG_CTL_OK); 1964 CASE(IMSG_CTL_FAIL); 1965 1966 CASE(IMSG_CTL_GET_DIGEST); 1967 CASE(IMSG_CTL_GET_STATS); 1968 CASE(IMSG_CTL_LIST_MESSAGES); 1969 CASE(IMSG_CTL_LIST_ENVELOPES); 1970 CASE(IMSG_CTL_MTA_SHOW_HOSTS); 1971 CASE(IMSG_CTL_MTA_SHOW_RELAYS); 1972 CASE(IMSG_CTL_MTA_SHOW_ROUTES); 1973 CASE(IMSG_CTL_MTA_SHOW_HOSTSTATS); 1974 CASE(IMSG_CTL_MTA_BLOCK); 1975 CASE(IMSG_CTL_MTA_UNBLOCK); 1976 CASE(IMSG_CTL_MTA_SHOW_BLOCK); 1977 CASE(IMSG_CTL_PAUSE_EVP); 1978 CASE(IMSG_CTL_PAUSE_MDA); 1979 CASE(IMSG_CTL_PAUSE_MTA); 1980 CASE(IMSG_CTL_PAUSE_SMTP); 1981 CASE(IMSG_CTL_PROFILE); 1982 CASE(IMSG_CTL_PROFILE_DISABLE); 1983 CASE(IMSG_CTL_PROFILE_ENABLE); 1984 CASE(IMSG_CTL_RESUME_EVP); 1985 CASE(IMSG_CTL_RESUME_MDA); 1986 CASE(IMSG_CTL_RESUME_MTA); 1987 CASE(IMSG_CTL_RESUME_SMTP); 1988 CASE(IMSG_CTL_RESUME_ROUTE); 1989 CASE(IMSG_CTL_REMOVE); 1990 CASE(IMSG_CTL_SCHEDULE); 1991 CASE(IMSG_CTL_SHOW_STATUS); 1992 CASE(IMSG_CTL_TRACE_DISABLE); 1993 CASE(IMSG_CTL_TRACE_ENABLE); 1994 CASE(IMSG_CTL_UPDATE_TABLE); 1995 CASE(IMSG_CTL_VERBOSE); 1996 CASE(IMSG_CTL_DISCOVER_EVPID); 1997 CASE(IMSG_CTL_DISCOVER_MSGID); 1998 1999 CASE(IMSG_CTL_SMTP_SESSION); 2000 2001 CASE(IMSG_GETADDRINFO); 2002 CASE(IMSG_GETADDRINFO_END); 2003 CASE(IMSG_GETNAMEINFO); 2004 CASE(IMSG_RES_QUERY); 2005 2006 CASE(IMSG_SETUP_KEY); 2007 CASE(IMSG_SETUP_PEER); 2008 CASE(IMSG_SETUP_DONE); 2009 2010 CASE(IMSG_CONF_START); 2011 CASE(IMSG_CONF_END); 2012 2013 CASE(IMSG_STAT_INCREMENT); 2014 CASE(IMSG_STAT_DECREMENT); 2015 CASE(IMSG_STAT_SET); 2016 2017 CASE(IMSG_LKA_AUTHENTICATE); 2018 CASE(IMSG_LKA_OPEN_FORWARD); 2019 CASE(IMSG_LKA_ENVELOPE_SUBMIT); 2020 CASE(IMSG_LKA_ENVELOPE_COMMIT); 2021 2022 CASE(IMSG_QUEUE_DELIVER); 2023 CASE(IMSG_QUEUE_DELIVERY_OK); 2024 CASE(IMSG_QUEUE_DELIVERY_TEMPFAIL); 2025 CASE(IMSG_QUEUE_DELIVERY_PERMFAIL); 2026 CASE(IMSG_QUEUE_DELIVERY_LOOP); 2027 CASE(IMSG_QUEUE_DISCOVER_EVPID); 2028 CASE(IMSG_QUEUE_DISCOVER_MSGID); 2029 CASE(IMSG_QUEUE_ENVELOPE_ACK); 2030 CASE(IMSG_QUEUE_ENVELOPE_COMMIT); 2031 CASE(IMSG_QUEUE_ENVELOPE_REMOVE); 2032 CASE(IMSG_QUEUE_ENVELOPE_SCHEDULE); 2033 CASE(IMSG_QUEUE_ENVELOPE_SUBMIT); 2034 CASE(IMSG_QUEUE_HOLDQ_HOLD); 2035 CASE(IMSG_QUEUE_HOLDQ_RELEASE); 2036 CASE(IMSG_QUEUE_MESSAGE_COMMIT); 2037 CASE(IMSG_QUEUE_MESSAGE_ROLLBACK); 2038 CASE(IMSG_QUEUE_SMTP_SESSION); 2039 CASE(IMSG_QUEUE_TRANSFER); 2040 2041 CASE(IMSG_MDA_DELIVERY_OK); 2042 CASE(IMSG_MDA_DELIVERY_TEMPFAIL); 2043 CASE(IMSG_MDA_DELIVERY_PERMFAIL); 2044 CASE(IMSG_MDA_DELIVERY_LOOP); 2045 CASE(IMSG_MDA_DELIVERY_HOLD); 2046 CASE(IMSG_MDA_DONE); 2047 CASE(IMSG_MDA_FORK); 2048 CASE(IMSG_MDA_HOLDQ_RELEASE); 2049 CASE(IMSG_MDA_LOOKUP_USERINFO); 2050 CASE(IMSG_MDA_KILL); 2051 CASE(IMSG_MDA_OPEN_MESSAGE); 2052 2053 CASE(IMSG_MTA_DELIVERY_OK); 2054 CASE(IMSG_MTA_DELIVERY_TEMPFAIL); 2055 CASE(IMSG_MTA_DELIVERY_PERMFAIL); 2056 CASE(IMSG_MTA_DELIVERY_LOOP); 2057 CASE(IMSG_MTA_DELIVERY_HOLD); 2058 CASE(IMSG_MTA_DNS_HOST); 2059 CASE(IMSG_MTA_DNS_HOST_END); 2060 CASE(IMSG_MTA_DNS_MX); 2061 CASE(IMSG_MTA_DNS_MX_PREFERENCE); 2062 CASE(IMSG_MTA_HOLDQ_RELEASE); 2063 CASE(IMSG_MTA_LOOKUP_CREDENTIALS); 2064 CASE(IMSG_MTA_LOOKUP_SOURCE); 2065 CASE(IMSG_MTA_LOOKUP_HELO); 2066 CASE(IMSG_MTA_LOOKUP_SMARTHOST); 2067 CASE(IMSG_MTA_OPEN_MESSAGE); 2068 CASE(IMSG_MTA_SCHEDULE); 2069 2070 CASE(IMSG_SCHED_ENVELOPE_BOUNCE); 2071 CASE(IMSG_SCHED_ENVELOPE_DELIVER); 2072 CASE(IMSG_SCHED_ENVELOPE_EXPIRE); 2073 CASE(IMSG_SCHED_ENVELOPE_INJECT); 2074 CASE(IMSG_SCHED_ENVELOPE_REMOVE); 2075 CASE(IMSG_SCHED_ENVELOPE_TRANSFER); 2076 2077 CASE(IMSG_SMTP_AUTHENTICATE); 2078 CASE(IMSG_SMTP_MESSAGE_COMMIT); 2079 CASE(IMSG_SMTP_MESSAGE_CREATE); 2080 CASE(IMSG_SMTP_MESSAGE_ROLLBACK); 2081 CASE(IMSG_SMTP_MESSAGE_OPEN); 2082 CASE(IMSG_SMTP_CHECK_SENDER); 2083 CASE(IMSG_SMTP_EXPAND_RCPT); 2084 CASE(IMSG_SMTP_LOOKUP_HELO); 2085 2086 CASE(IMSG_SMTP_REQ_CONNECT); 2087 CASE(IMSG_SMTP_REQ_HELO); 2088 CASE(IMSG_SMTP_REQ_MAIL); 2089 CASE(IMSG_SMTP_REQ_RCPT); 2090 CASE(IMSG_SMTP_REQ_DATA); 2091 CASE(IMSG_SMTP_REQ_EOM); 2092 CASE(IMSG_SMTP_EVENT_RSET); 2093 CASE(IMSG_SMTP_EVENT_COMMIT); 2094 CASE(IMSG_SMTP_EVENT_ROLLBACK); 2095 CASE(IMSG_SMTP_EVENT_DISCONNECT); 2096 2097 CASE(IMSG_LKA_PROCESSOR_FORK); 2098 CASE(IMSG_LKA_PROCESSOR_ERRFD); 2099 2100 CASE(IMSG_REPORT_SMTP_LINK_CONNECT); 2101 CASE(IMSG_REPORT_SMTP_LINK_DISCONNECT); 2102 CASE(IMSG_REPORT_SMTP_LINK_TLS); 2103 CASE(IMSG_REPORT_SMTP_LINK_GREETING); 2104 CASE(IMSG_REPORT_SMTP_LINK_IDENTIFY); 2105 CASE(IMSG_REPORT_SMTP_LINK_AUTH); 2106 2107 CASE(IMSG_REPORT_SMTP_TX_RESET); 2108 CASE(IMSG_REPORT_SMTP_TX_BEGIN); 2109 CASE(IMSG_REPORT_SMTP_TX_ENVELOPE); 2110 CASE(IMSG_REPORT_SMTP_TX_COMMIT); 2111 CASE(IMSG_REPORT_SMTP_TX_ROLLBACK); 2112 2113 CASE(IMSG_REPORT_SMTP_PROTOCOL_CLIENT); 2114 CASE(IMSG_REPORT_SMTP_PROTOCOL_SERVER); 2115 2116 CASE(IMSG_FILTER_SMTP_BEGIN); 2117 CASE(IMSG_FILTER_SMTP_END); 2118 CASE(IMSG_FILTER_SMTP_PROTOCOL); 2119 CASE(IMSG_FILTER_SMTP_DATA_BEGIN); 2120 CASE(IMSG_FILTER_SMTP_DATA_END); 2121 2122 CASE(IMSG_CA_RSA_PRIVENC); 2123 CASE(IMSG_CA_RSA_PRIVDEC); 2124 CASE(IMSG_CA_ECDSA_SIGN); 2125 default: 2126 (void)snprintf(buf, sizeof(buf), "IMSG_??? (%d)", type); 2127 2128 return buf; 2129 } 2130 } 2131 2132 int 2133 parent_auth_user(const char *username, const char *password) 2134 { 2135 char user[LOGIN_NAME_MAX]; 2136 char pass[LINE_MAX]; 2137 int ret; 2138 2139 (void)strlcpy(user, username, sizeof(user)); 2140 (void)strlcpy(pass, password, sizeof(pass)); 2141 2142 ret = auth_userokay(user, NULL, "auth-smtp", pass); 2143 if (ret) 2144 return LKA_OK; 2145 return LKA_PERMFAIL; 2146 } 2147