1 /* 2 * Copyright (c) 1998-2005 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved. 5 * Copyright (c) 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * By using this file, you agree to the terms and conditions set 9 * forth in the LICENSE file which can be found at the top level of 10 * the sendmail distribution. 11 * 12 */ 13 14 /* 15 * Copyright 1996-2006 Sun Microsystems, Inc. All rights reserved. 16 * Use is subject to license terms. 17 */ 18 19 #define _DEFINE 20 #include <sendmail.h> 21 #include <sm/xtrap.h> 22 #include <sm/signal.h> 23 24 #ifndef lint 25 SM_UNUSED(static char copyright[]) = 26 "@(#) Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.\n\ 27 @(#) All rights reserved.\n\ 28 @(#) Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.\n\ 29 @(#) Copyright (c) 1988, 1993\n\ 30 @(#) The Regents of the University of California. All rights reserved.\n\ 31 @(#) Copyright 1996-2006 Sun Microsystems, Inc. All rights reserved.\n\ 32 @(#) Use is subject to license terms.\n"; 33 #endif /* ! lint */ 34 35 #pragma ident "%Z%%M% %I% %E% SMI" 36 37 SM_RCSID("@(#)$Id: main.c,v 8.942 2005/12/26 04:39:13 ca Exp $") 38 SM_IDSTR(i2, "%W% (Sun) %G%") 39 40 #if NETINET || NETINET6 41 # include <arpa/inet.h> 42 #endif /* NETINET || NETINET6 */ 43 44 /* for getcfname() */ 45 #include <sendmail/pathnames.h> 46 47 static SM_DEBUG_T 48 DebugNoPRestart = SM_DEBUG_INITIALIZER("no_persistent_restart", 49 "@(#)$Debug: no_persistent_restart - don't restart, log only $"); 50 51 static void dump_class __P((STAB *, int)); 52 static void obsolete __P((char **)); 53 static void testmodeline __P((char *, ENVELOPE *)); 54 static char *getextenv __P((const char *)); 55 static void sm_printoptions __P((char **)); 56 static SIGFUNC_DECL intindebug __P((int)); 57 static SIGFUNC_DECL sighup __P((int)); 58 static SIGFUNC_DECL sigpipe __P((int)); 59 static SIGFUNC_DECL sigterm __P((int)); 60 #ifdef SIGUSR1 61 static SIGFUNC_DECL sigusr1 __P((int)); 62 #endif /* SIGUSR1 */ 63 64 /* 65 ** SENDMAIL -- Post mail to a set of destinations. 66 ** 67 ** This is the basic mail router. All user mail programs should 68 ** call this routine to actually deliver mail. Sendmail in 69 ** turn calls a bunch of mail servers that do the real work of 70 ** delivering the mail. 71 ** 72 ** Sendmail is driven by settings read in from /etc/mail/sendmail.cf 73 ** (read by readcf.c). 74 ** 75 ** Usage: 76 ** /usr/lib/sendmail [flags] addr ... 77 ** 78 ** See the associated documentation for details. 79 ** 80 ** Authors: 81 ** Eric Allman, UCB/INGRES (until 10/81). 82 ** Britton-Lee, Inc., purveyors of fine 83 ** database computers (11/81 - 10/88). 84 ** International Computer Science Institute 85 ** (11/88 - 9/89). 86 ** UCB/Mammoth Project (10/89 - 7/95). 87 ** InReference, Inc. (8/95 - 1/97). 88 ** Sendmail, Inc. (1/98 - present). 89 ** The support of my employers is gratefully acknowledged. 90 ** Few of them (Britton-Lee in particular) have had 91 ** anything to gain from my involvement in this project. 92 ** 93 ** Gregory Neil Shapiro, 94 ** Worcester Polytechnic Institute (until 3/98). 95 ** Sendmail, Inc. (3/98 - present). 96 ** 97 ** Claus Assmann, 98 ** Sendmail, Inc. (12/98 - present). 99 */ 100 101 char *FullName; /* sender's full name */ 102 ENVELOPE BlankEnvelope; /* a "blank" envelope */ 103 static ENVELOPE MainEnvelope; /* the envelope around the basic letter */ 104 ADDRESS NullAddress = /* a null address */ 105 { "", "", NULL, "" }; 106 char *CommandLineArgs; /* command line args for pid file */ 107 bool Warn_Q_option = false; /* warn about Q option use */ 108 static int MissingFds = 0; /* bit map of fds missing on startup */ 109 char *Mbdb = "pw"; /* mailbox database defaults to /etc/passwd */ 110 111 #ifdef NGROUPS_MAX 112 GIDSET_T InitialGidSet[NGROUPS_MAX]; 113 #endif /* NGROUPS_MAX */ 114 115 #define MAXCONFIGLEVEL 10 /* highest config version level known */ 116 117 #if SASL 118 static sasl_callback_t srvcallbacks[] = 119 { 120 { SASL_CB_VERIFYFILE, &safesaslfile, NULL }, 121 { SASL_CB_PROXY_POLICY, &proxy_policy, NULL }, 122 { SASL_CB_LIST_END, NULL, NULL } 123 }; 124 #endif /* SASL */ 125 126 unsigned int SubmitMode; 127 int SyslogPrefixLen; /* estimated length of syslog prefix */ 128 #define PIDLEN 6 /* pid length for computing SyslogPrefixLen */ 129 #ifndef SL_FUDGE 130 # define SL_FUDGE 10 /* fudge offset for SyslogPrefixLen */ 131 #endif /* ! SL_FUDGE */ 132 #define SLDLL 8 /* est. length of default syslog label */ 133 134 135 /* Some options are dangerous to allow users to use in non-submit mode */ 136 #define CHECK_AGAINST_OPMODE(cmd) \ 137 { \ 138 if (extraprivs && \ 139 OpMode != MD_DELIVER && OpMode != MD_SMTP && \ 140 OpMode != MD_ARPAFTP && \ 141 OpMode != MD_VERIFY && OpMode != MD_TEST) \ 142 { \ 143 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, \ 144 "WARNING: Ignoring submission mode -%c option (not in submission mode)\n", \ 145 (cmd)); \ 146 break; \ 147 } \ 148 if (extraprivs && queuerun) \ 149 { \ 150 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, \ 151 "WARNING: Ignoring submission mode -%c option with -q\n", \ 152 (cmd)); \ 153 break; \ 154 } \ 155 } 156 157 int 158 main(argc, argv, envp) 159 int argc; 160 char **argv; 161 char **envp; 162 { 163 register char *p; 164 char **av; 165 extern char Version[]; 166 char *ep, *from; 167 STAB *st; 168 register int i; 169 int j; 170 int dp; 171 int fill_errno; 172 int qgrp = NOQGRP; /* queue group to process */ 173 bool safecf = true; 174 BITMAP256 *p_flags = NULL; /* daemon flags */ 175 bool warn_C_flag = false; 176 bool auth = true; /* whether to set e_auth_param */ 177 char warn_f_flag = '\0'; 178 bool run_in_foreground = false; /* -bD mode */ 179 bool queuerun = false, debug = false; 180 struct passwd *pw; 181 struct hostent *hp; 182 char *nullserver = NULL; 183 char *authinfo = NULL; 184 char *sysloglabel = NULL; /* label for syslog */ 185 char *conffile = NULL; /* name of .cf file */ 186 char *queuegroup = NULL; /* queue group to process */ 187 char *quarantining = NULL; /* quarantine queue items? */ 188 bool extraprivs; 189 bool forged, negate; 190 bool queuepersistent = false; /* queue runner process runs forever */ 191 bool foregroundqueue = false; /* queue run in foreground */ 192 bool save_val; /* to save some bool var. */ 193 int cftype; /* which cf file to use? */ 194 SM_FILE_T *smdebug; 195 static time_t starttime = 0; /* when was process started */ 196 struct stat traf_st; /* for TrafficLog FIFO check */ 197 char buf[MAXLINE]; 198 char jbuf[MAXHOSTNAMELEN]; /* holds MyHostName */ 199 static char rnamebuf[MAXNAME]; /* holds RealUserName */ 200 char *emptyenviron[1]; 201 #if STARTTLS 202 bool tls_ok; 203 #endif /* STARTTLS */ 204 QUEUE_CHAR *new; 205 ENVELOPE *e; 206 extern int DtableSize; 207 extern int optind; 208 extern int opterr; 209 extern char *optarg; 210 extern char **environ; 211 #if SASL 212 extern void sm_sasl_init __P((void)); 213 #endif /* SASL */ 214 215 #if USE_ENVIRON 216 envp = environ; 217 #endif /* USE_ENVIRON */ 218 219 /* turn off profiling */ 220 SM_PROF(0); 221 222 /* install default exception handler */ 223 sm_exc_newthread(fatal_error); 224 225 /* set the default in/out channel so errors reported to screen */ 226 InChannel = smioin; 227 OutChannel = smioout; 228 229 /* 230 ** Check to see if we reentered. 231 ** This would normally happen if e_putheader or e_putbody 232 ** were NULL when invoked. 233 */ 234 235 if (starttime != 0) 236 { 237 syserr("main: reentered!"); 238 abort(); 239 } 240 starttime = curtime(); 241 242 /* avoid null pointer dereferences */ 243 TermEscape.te_rv_on = TermEscape.te_rv_off = ""; 244 245 RealUid = getuid(); 246 RealGid = getgid(); 247 248 /* Check if sendmail is running with extra privs */ 249 extraprivs = (RealUid != 0 && 250 (geteuid() != getuid() || getegid() != getgid())); 251 252 CurrentPid = getpid(); 253 254 /* get whatever .cf file is right for the opmode */ 255 cftype = SM_GET_RIGHT_CF; 256 257 /* in 4.4BSD, the table can be huge; impose a reasonable limit */ 258 DtableSize = getdtsize(); 259 if (DtableSize > 256) 260 DtableSize = 256; 261 262 /* 263 ** Be sure we have enough file descriptors. 264 ** But also be sure that 0, 1, & 2 are open. 265 */ 266 267 /* reset errno and fill_errno; the latter is used way down below */ 268 errno = fill_errno = 0; 269 fill_fd(STDIN_FILENO, NULL); 270 if (errno != 0) 271 fill_errno = errno; 272 fill_fd(STDOUT_FILENO, NULL); 273 if (errno != 0) 274 fill_errno = errno; 275 fill_fd(STDERR_FILENO, NULL); 276 if (errno != 0) 277 fill_errno = errno; 278 279 sm_closefrom(STDERR_FILENO + 1, DtableSize); 280 errno = 0; 281 smdebug = NULL; 282 283 #if LOG 284 # ifndef SM_LOG_STR 285 # define SM_LOG_STR "sendmail" 286 # endif /* ! SM_LOG_STR */ 287 # ifdef LOG_MAIL 288 openlog(SM_LOG_STR, LOG_PID, LOG_MAIL); 289 # else /* LOG_MAIL */ 290 openlog(SM_LOG_STR, LOG_PID); 291 # endif /* LOG_MAIL */ 292 #endif /* LOG */ 293 294 /* 295 ** Seed the random number generator. 296 ** Used for queue file names, picking a queue directory, and 297 ** MX randomization. 298 */ 299 300 seed_random(); 301 302 /* do machine-dependent initializations */ 303 init_md(argc, argv); 304 305 306 SyslogPrefixLen = PIDLEN + (MAXQFNAME - 3) + SL_FUDGE + SLDLL; 307 308 /* reset status from syserr() calls for missing file descriptors */ 309 Errors = 0; 310 ExitStat = EX_OK; 311 312 SubmitMode = SUBMIT_UNKNOWN; 313 #if XDEBUG 314 checkfd012("after openlog"); 315 #endif /* XDEBUG */ 316 317 tTsetup(tTdvect, sizeof tTdvect, "0-99.1,*_trace_*.1"); 318 319 #ifdef NGROUPS_MAX 320 /* save initial group set for future checks */ 321 i = getgroups(NGROUPS_MAX, InitialGidSet); 322 if (i <= 0) 323 { 324 InitialGidSet[0] = (GID_T) -1; 325 i = 0; 326 } 327 while (i < NGROUPS_MAX) 328 InitialGidSet[i++] = InitialGidSet[0]; 329 #endif /* NGROUPS_MAX */ 330 331 /* drop group id privileges (RunAsUser not yet set) */ 332 dp = drop_privileges(false); 333 setstat(dp); 334 335 #ifdef SIGUSR1 336 /* Only allow root (or non-set-*-ID binaries) to use SIGUSR1 */ 337 if (!extraprivs) 338 { 339 /* arrange to dump state on user-1 signal */ 340 (void) sm_signal(SIGUSR1, sigusr1); 341 } 342 else 343 { 344 /* ignore user-1 signal */ 345 (void) sm_signal(SIGUSR1, SIG_IGN); 346 } 347 #endif /* SIGUSR1 */ 348 349 /* initialize for setproctitle */ 350 initsetproctitle(argc, argv, envp); 351 352 /* Handle any non-getoptable constructions. */ 353 obsolete(argv); 354 355 /* 356 ** Do a quick prescan of the argument list. 357 */ 358 359 360 /* find initial opMode */ 361 OpMode = MD_DELIVER; 362 av = argv; 363 p = strrchr(*av, '/'); 364 if (p++ == NULL) 365 p = *av; 366 if (strcmp(p, "newaliases") == 0) 367 OpMode = MD_INITALIAS; 368 else if (strcmp(p, "mailq") == 0) 369 OpMode = MD_PRINT; 370 else if (strcmp(p, "smtpd") == 0) 371 OpMode = MD_DAEMON; 372 else if (strcmp(p, "hoststat") == 0) 373 OpMode = MD_HOSTSTAT; 374 else if (strcmp(p, "purgestat") == 0) 375 OpMode = MD_PURGESTAT; 376 377 #if defined(__osf__) || defined(_AIX3) 378 # define OPTIONS "A:B:b:C:cD:d:e:F:f:Gh:IiL:M:mN:nO:o:p:Q:q:R:r:sTtV:vX:x" 379 #endif /* defined(__osf__) || defined(_AIX3) */ 380 #if defined(sony_news) 381 # define OPTIONS "A:B:b:C:cD:d:E:e:F:f:Gh:IiJ:L:M:mN:nO:o:p:Q:q:R:r:sTtV:vX:" 382 #endif /* defined(sony_news) */ 383 #ifndef OPTIONS 384 # define OPTIONS "A:B:b:C:cD:d:e:F:f:Gh:IiL:M:mN:nO:o:p:Q:q:R:r:sTtV:vX:" 385 #endif /* ! OPTIONS */ 386 387 /* Set to 0 to allow -b; need to check optarg before using it! */ 388 opterr = 0; 389 while ((j = getopt(argc, argv, OPTIONS)) != -1) 390 { 391 switch (j) 392 { 393 case 'b': /* operations mode */ 394 j = (optarg == NULL) ? ' ' : *optarg; 395 switch (j) 396 { 397 case MD_DAEMON: 398 case MD_FGDAEMON: 399 case MD_SMTP: 400 case MD_INITALIAS: 401 case MD_DELIVER: 402 case MD_VERIFY: 403 case MD_TEST: 404 case MD_PRINT: 405 case MD_PRINTNQE: 406 case MD_HOSTSTAT: 407 case MD_PURGESTAT: 408 case MD_ARPAFTP: 409 OpMode = j; 410 break; 411 412 case MD_FREEZE: 413 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 414 "Frozen configurations unsupported\n"); 415 return EX_USAGE; 416 417 default: 418 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 419 "Invalid operation mode %c\n", 420 j); 421 return EX_USAGE; 422 } 423 break; 424 425 case 'D': 426 if (debug) 427 { 428 errno = 0; 429 syserr("-D file must be before -d"); 430 ExitStat = EX_USAGE; 431 break; 432 } 433 dp = drop_privileges(true); 434 setstat(dp); 435 smdebug = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, 436 optarg, SM_IO_APPEND, NULL); 437 if (smdebug == NULL) 438 { 439 syserr("cannot open %s", optarg); 440 ExitStat = EX_CANTCREAT; 441 break; 442 } 443 sm_debug_setfile(smdebug); 444 break; 445 446 case 'd': 447 debug = true; 448 tTflag(optarg); 449 (void) sm_io_setvbuf(sm_debug_file(), SM_TIME_DEFAULT, 450 (char *) NULL, SM_IO_NBF, 451 SM_IO_BUFSIZ); 452 break; 453 454 case 'G': /* relay (gateway) submission */ 455 SubmitMode = SUBMIT_MTA; 456 break; 457 458 case 'L': 459 if (optarg == NULL) 460 { 461 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 462 "option requires an argument -- '%c'", 463 (char) j); 464 return EX_USAGE; 465 } 466 j = SM_MIN(strlen(optarg), 32) + 1; 467 sysloglabel = xalloc(j); 468 (void) sm_strlcpy(sysloglabel, optarg, j); 469 SyslogPrefixLen = PIDLEN + (MAXQFNAME - 3) + 470 SL_FUDGE + j; 471 break; 472 473 case 'Q': 474 case 'q': 475 /* just check if it is there */ 476 queuerun = true; 477 break; 478 } 479 } 480 opterr = 1; 481 482 /* Don't leak queue information via debug flags */ 483 if (extraprivs && queuerun && debug) 484 { 485 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 486 "WARNING: Can not use -d with -q. Disabling debugging.\n"); 487 sm_debug_close(); 488 sm_debug_setfile(NULL); 489 (void) memset(tTdvect, '\0', sizeof tTdvect); 490 } 491 492 #if LOG 493 if (sysloglabel != NULL) 494 { 495 /* Sanitize the string */ 496 for (p = sysloglabel; *p != '\0'; p++) 497 { 498 if (!isascii(*p) || !isprint(*p) || *p == '%') 499 *p = '*'; 500 } 501 closelog(); 502 # ifdef LOG_MAIL 503 openlog(sysloglabel, LOG_PID, LOG_MAIL); 504 # else /* LOG_MAIL */ 505 openlog(sysloglabel, LOG_PID); 506 # endif /* LOG_MAIL */ 507 } 508 #endif /* LOG */ 509 510 /* set up the blank envelope */ 511 BlankEnvelope.e_puthdr = putheader; 512 BlankEnvelope.e_putbody = putbody; 513 BlankEnvelope.e_xfp = NULL; 514 STRUCTCOPY(NullAddress, BlankEnvelope.e_from); 515 CurEnv = &BlankEnvelope; 516 STRUCTCOPY(NullAddress, MainEnvelope.e_from); 517 518 /* 519 ** Set default values for variables. 520 ** These cannot be in initialized data space. 521 */ 522 523 setdefaults(&BlankEnvelope); 524 initmacros(&BlankEnvelope); 525 526 /* reset macro */ 527 set_op_mode(OpMode); 528 529 pw = sm_getpwuid(RealUid); 530 if (pw != NULL) 531 (void) sm_strlcpy(rnamebuf, pw->pw_name, sizeof rnamebuf); 532 else 533 (void) sm_snprintf(rnamebuf, sizeof rnamebuf, "Unknown UID %d", 534 (int) RealUid); 535 536 RealUserName = rnamebuf; 537 538 if (tTd(0, 101)) 539 { 540 sm_dprintf("Version %s\n", Version); 541 finis(false, true, EX_OK); 542 /* NOTREACHED */ 543 } 544 545 /* 546 ** if running non-set-user-ID binary as non-root, pretend 547 ** we are the RunAsUid 548 */ 549 550 if (RealUid != 0 && geteuid() == RealUid) 551 { 552 if (tTd(47, 1)) 553 sm_dprintf("Non-set-user-ID binary: RunAsUid = RealUid = %d\n", 554 (int) RealUid); 555 RunAsUid = RealUid; 556 } 557 else if (geteuid() != 0) 558 RunAsUid = geteuid(); 559 560 EffGid = getegid(); 561 if (RealUid != 0 && EffGid == RealGid) 562 RunAsGid = RealGid; 563 564 if (tTd(47, 5)) 565 { 566 sm_dprintf("main: e/ruid = %d/%d e/rgid = %d/%d\n", 567 (int) geteuid(), (int) getuid(), 568 (int) getegid(), (int) getgid()); 569 sm_dprintf("main: RunAsUser = %d:%d\n", 570 (int) RunAsUid, (int) RunAsGid); 571 } 572 573 /* save command line arguments */ 574 j = 0; 575 for (av = argv; *av != NULL; ) 576 j += strlen(*av++) + 1; 577 SaveArgv = (char **) xalloc(sizeof (char *) * (argc + 1)); 578 CommandLineArgs = xalloc(j); 579 p = CommandLineArgs; 580 for (av = argv, i = 0; *av != NULL; ) 581 { 582 int h; 583 584 SaveArgv[i++] = newstr(*av); 585 if (av != argv) 586 *p++ = ' '; 587 (void) sm_strlcpy(p, *av++, j); 588 h = strlen(p); 589 p += h; 590 j -= h + 1; 591 } 592 SaveArgv[i] = NULL; 593 594 if (tTd(0, 1)) 595 { 596 extern char *CompileOptions[]; 597 598 sm_dprintf("Version %s\n Compiled with:", Version); 599 sm_printoptions(CompileOptions); 600 } 601 if (tTd(0, 10)) 602 { 603 extern char *OsCompileOptions[]; 604 605 sm_dprintf(" OS Defines:"); 606 sm_printoptions(OsCompileOptions); 607 #ifdef _PATH_UNIX 608 sm_dprintf("Kernel symbols:\t%s\n", _PATH_UNIX); 609 #endif /* _PATH_UNIX */ 610 611 sm_dprintf(" Conf file:\t%s (default for MSP)\n", 612 getcfname(OpMode, SubmitMode, SM_GET_SUBMIT_CF, 613 conffile)); 614 sm_dprintf(" Conf file:\t%s (default for MTA)\n", 615 getcfname(OpMode, SubmitMode, SM_GET_SENDMAIL_CF, 616 conffile)); 617 sm_dprintf(" Pid file:\t%s (default)\n", PidFile); 618 } 619 620 if (tTd(0, 12)) 621 { 622 extern char *SmCompileOptions[]; 623 624 sm_dprintf(" libsm Defines:"); 625 sm_printoptions(SmCompileOptions); 626 } 627 628 if (tTd(0, 13)) 629 { 630 extern char *FFRCompileOptions[]; 631 632 sm_dprintf(" FFR Defines:"); 633 sm_printoptions(FFRCompileOptions); 634 } 635 636 /* clear sendmail's environment */ 637 ExternalEnviron = environ; 638 emptyenviron[0] = NULL; 639 environ = emptyenviron; 640 641 /* 642 ** restore any original TZ setting until TimeZoneSpec has been 643 ** determined - or early log messages may get bogus time stamps 644 */ 645 646 if ((p = getextenv("TZ")) != NULL) 647 { 648 char *tz; 649 int tzlen; 650 651 /* XXX check for reasonable length? */ 652 tzlen = strlen(p) + 4; 653 tz = xalloc(tzlen); 654 (void) sm_strlcpyn(tz, tzlen, 2, "TZ=", p); 655 656 /* XXX check return code? */ 657 (void) putenv(tz); 658 } 659 660 /* prime the child environment */ 661 sm_setuserenv("AGENT", "sendmail"); 662 663 (void) sm_signal(SIGPIPE, SIG_IGN); 664 OldUmask = umask(022); 665 FullName = getextenv("NAME"); 666 if (FullName != NULL) 667 FullName = newstr(FullName); 668 669 /* 670 ** Initialize name server if it is going to be used. 671 */ 672 673 #if NAMED_BIND 674 if (!bitset(RES_INIT, _res.options)) 675 (void) res_init(); 676 if (tTd(8, 8)) 677 _res.options |= RES_DEBUG; 678 else 679 _res.options &= ~RES_DEBUG; 680 # ifdef RES_NOALIASES 681 _res.options |= RES_NOALIASES; 682 # endif /* RES_NOALIASES */ 683 TimeOuts.res_retry[RES_TO_DEFAULT] = _res.retry; 684 TimeOuts.res_retry[RES_TO_FIRST] = _res.retry; 685 TimeOuts.res_retry[RES_TO_NORMAL] = _res.retry; 686 TimeOuts.res_retrans[RES_TO_DEFAULT] = _res.retrans; 687 TimeOuts.res_retrans[RES_TO_FIRST] = _res.retrans; 688 TimeOuts.res_retrans[RES_TO_NORMAL] = _res.retrans; 689 #endif /* NAMED_BIND */ 690 691 errno = 0; 692 from = NULL; 693 694 /* initialize some macros, etc. */ 695 init_vendor_macros(&BlankEnvelope); 696 697 /* version */ 698 macdefine(&BlankEnvelope.e_macro, A_PERM, 'v', Version); 699 700 /* hostname */ 701 hp = myhostname(jbuf, sizeof jbuf); 702 if (jbuf[0] != '\0') 703 { 704 struct utsname utsname; 705 706 if (tTd(0, 4)) 707 sm_dprintf("Canonical name: %s\n", jbuf); 708 macdefine(&BlankEnvelope.e_macro, A_TEMP, 'w', jbuf); 709 macdefine(&BlankEnvelope.e_macro, A_TEMP, 'j', jbuf); 710 setclass('w', jbuf); 711 712 p = strchr(jbuf, '.'); 713 if (p != NULL && p[1] != '\0') 714 macdefine(&BlankEnvelope.e_macro, A_TEMP, 'm', &p[1]); 715 716 if (uname(&utsname) >= 0) 717 p = utsname.nodename; 718 else 719 { 720 if (tTd(0, 22)) 721 sm_dprintf("uname failed (%s)\n", 722 sm_errstring(errno)); 723 makelower(jbuf); 724 p = jbuf; 725 } 726 if (tTd(0, 4)) 727 sm_dprintf(" UUCP nodename: %s\n", p); 728 macdefine(&BlankEnvelope.e_macro, A_TEMP, 'k', p); 729 setclass('k', p); 730 setclass('w', p); 731 } 732 if (hp != NULL) 733 { 734 for (av = hp->h_aliases; av != NULL && *av != NULL; av++) 735 { 736 if (tTd(0, 4)) 737 sm_dprintf("\ta.k.a.: %s\n", *av); 738 setclass('w', *av); 739 } 740 #if NETINET || NETINET6 741 for (i = 0; i >= 0 && hp->h_addr_list[i] != NULL; i++) 742 { 743 # if NETINET6 744 char *addr; 745 char buf6[INET6_ADDRSTRLEN]; 746 struct in6_addr ia6; 747 # endif /* NETINET6 */ 748 # if NETINET 749 struct in_addr ia; 750 # endif /* NETINET */ 751 char ipbuf[103]; 752 753 ipbuf[0] = '\0'; 754 switch (hp->h_addrtype) 755 { 756 # if NETINET 757 case AF_INET: 758 if (hp->h_length != INADDRSZ) 759 break; 760 761 memmove(&ia, hp->h_addr_list[i], INADDRSZ); 762 (void) sm_snprintf(ipbuf, sizeof ipbuf, 763 "[%.100s]", inet_ntoa(ia)); 764 break; 765 # endif /* NETINET */ 766 767 # if NETINET6 768 case AF_INET6: 769 if (hp->h_length != IN6ADDRSZ) 770 break; 771 772 memmove(&ia6, hp->h_addr_list[i], IN6ADDRSZ); 773 addr = anynet_ntop(&ia6, buf6, sizeof buf6); 774 if (addr != NULL) 775 (void) sm_snprintf(ipbuf, sizeof ipbuf, 776 "[%.100s]", addr); 777 break; 778 # endif /* NETINET6 */ 779 } 780 if (ipbuf[0] == '\0') 781 break; 782 783 if (tTd(0, 4)) 784 sm_dprintf("\ta.k.a.: %s\n", ipbuf); 785 setclass('w', ipbuf); 786 } 787 #endif /* NETINET || NETINET6 */ 788 #if NETINET6 789 freehostent(hp); 790 hp = NULL; 791 #endif /* NETINET6 */ 792 } 793 794 /* current time */ 795 macdefine(&BlankEnvelope.e_macro, A_TEMP, 'b', arpadate((char *) NULL)); 796 797 /* current load average */ 798 sm_getla(); 799 800 QueueLimitRecipient = (QUEUE_CHAR *) NULL; 801 QueueLimitSender = (QUEUE_CHAR *) NULL; 802 QueueLimitId = (QUEUE_CHAR *) NULL; 803 QueueLimitQuarantine = (QUEUE_CHAR *) NULL; 804 805 /* 806 ** Crack argv. 807 */ 808 809 optind = 1; 810 while ((j = getopt(argc, argv, OPTIONS)) != -1) 811 { 812 switch (j) 813 { 814 case 'b': /* operations mode */ 815 /* already done */ 816 break; 817 818 case 'A': /* use Alternate sendmail/submit.cf */ 819 cftype = optarg[0] == 'c' ? SM_GET_SUBMIT_CF 820 : SM_GET_SENDMAIL_CF; 821 break; 822 823 case 'B': /* body type */ 824 CHECK_AGAINST_OPMODE(j); 825 BlankEnvelope.e_bodytype = newstr(optarg); 826 break; 827 828 case 'C': /* select configuration file (already done) */ 829 if (RealUid != 0) 830 warn_C_flag = true; 831 conffile = newstr(optarg); 832 dp = drop_privileges(true); 833 setstat(dp); 834 safecf = false; 835 break; 836 837 case 'D': 838 case 'd': /* debugging */ 839 /* already done */ 840 break; 841 842 case 'f': /* from address */ 843 case 'r': /* obsolete -f flag */ 844 CHECK_AGAINST_OPMODE(j); 845 if (from != NULL) 846 { 847 usrerr("More than one \"from\" person"); 848 ExitStat = EX_USAGE; 849 break; 850 } 851 if (optarg[0] == '\0') 852 from = newstr("<>"); 853 else 854 from = newstr(denlstring(optarg, true, true)); 855 if (strcmp(RealUserName, from) != 0) 856 warn_f_flag = j; 857 break; 858 859 case 'F': /* set full name */ 860 CHECK_AGAINST_OPMODE(j); 861 FullName = newstr(optarg); 862 break; 863 864 case 'G': /* relay (gateway) submission */ 865 /* already set */ 866 CHECK_AGAINST_OPMODE(j); 867 break; 868 869 case 'h': /* hop count */ 870 CHECK_AGAINST_OPMODE(j); 871 BlankEnvelope.e_hopcount = (short) strtol(optarg, &ep, 872 10); 873 (void) sm_snprintf(buf, sizeof buf, "%d", 874 BlankEnvelope.e_hopcount); 875 macdefine(&BlankEnvelope.e_macro, A_TEMP, 'c', buf); 876 877 if (*ep) 878 { 879 usrerr("Bad hop count (%s)", optarg); 880 ExitStat = EX_USAGE; 881 } 882 break; 883 884 case 'L': /* program label */ 885 /* already set */ 886 break; 887 888 case 'n': /* don't alias */ 889 CHECK_AGAINST_OPMODE(j); 890 NoAlias = true; 891 break; 892 893 case 'N': /* delivery status notifications */ 894 CHECK_AGAINST_OPMODE(j); 895 DefaultNotify |= QHASNOTIFY; 896 macdefine(&BlankEnvelope.e_macro, A_TEMP, 897 macid("{dsn_notify}"), optarg); 898 if (sm_strcasecmp(optarg, "never") == 0) 899 break; 900 for (p = optarg; p != NULL; optarg = p) 901 { 902 p = strchr(p, ','); 903 if (p != NULL) 904 *p++ = '\0'; 905 if (sm_strcasecmp(optarg, "success") == 0) 906 DefaultNotify |= QPINGONSUCCESS; 907 else if (sm_strcasecmp(optarg, "failure") == 0) 908 DefaultNotify |= QPINGONFAILURE; 909 else if (sm_strcasecmp(optarg, "delay") == 0) 910 DefaultNotify |= QPINGONDELAY; 911 else 912 { 913 usrerr("Invalid -N argument"); 914 ExitStat = EX_USAGE; 915 } 916 } 917 break; 918 919 case 'o': /* set option */ 920 setoption(*optarg, optarg + 1, false, true, 921 &BlankEnvelope); 922 break; 923 924 case 'O': /* set option (long form) */ 925 setoption(' ', optarg, false, true, &BlankEnvelope); 926 break; 927 928 case 'p': /* set protocol */ 929 CHECK_AGAINST_OPMODE(j); 930 p = strchr(optarg, ':'); 931 if (p != NULL) 932 { 933 *p++ = '\0'; 934 if (*p != '\0') 935 { 936 i = strlen(p) + 1; 937 ep = sm_malloc_x(i); 938 cleanstrcpy(ep, p, i); 939 macdefine(&BlankEnvelope.e_macro, 940 A_HEAP, 's', ep); 941 } 942 } 943 if (*optarg != '\0') 944 { 945 i = strlen(optarg) + 1; 946 ep = sm_malloc_x(i); 947 cleanstrcpy(ep, optarg, i); 948 macdefine(&BlankEnvelope.e_macro, A_HEAP, 949 'r', ep); 950 } 951 break; 952 953 case 'Q': /* change quarantining on queued items */ 954 /* sanity check */ 955 if (OpMode != MD_DELIVER && 956 OpMode != MD_QUEUERUN) 957 { 958 usrerr("Can not use -Q with -b%c", OpMode); 959 ExitStat = EX_USAGE; 960 break; 961 } 962 963 if (OpMode == MD_DELIVER) 964 set_op_mode(MD_QUEUERUN); 965 966 FullName = NULL; 967 968 quarantining = newstr(optarg); 969 break; 970 971 case 'q': /* run queue files at intervals */ 972 /* sanity check */ 973 if (OpMode != MD_DELIVER && 974 OpMode != MD_DAEMON && 975 OpMode != MD_FGDAEMON && 976 OpMode != MD_PRINT && 977 OpMode != MD_PRINTNQE && 978 OpMode != MD_QUEUERUN) 979 { 980 usrerr("Can not use -q with -b%c", OpMode); 981 ExitStat = EX_USAGE; 982 break; 983 } 984 985 /* don't override -bd, -bD or -bp */ 986 if (OpMode == MD_DELIVER) 987 set_op_mode(MD_QUEUERUN); 988 989 FullName = NULL; 990 negate = optarg[0] == '!'; 991 if (negate) 992 { 993 /* negate meaning of pattern match */ 994 optarg++; /* skip '!' for next switch */ 995 } 996 997 switch (optarg[0]) 998 { 999 case 'G': /* Limit by queue group name */ 1000 if (negate) 1001 { 1002 usrerr("Can not use -q!G"); 1003 ExitStat = EX_USAGE; 1004 break; 1005 } 1006 if (queuegroup != NULL) 1007 { 1008 usrerr("Can not use multiple -qG options"); 1009 ExitStat = EX_USAGE; 1010 break; 1011 } 1012 queuegroup = newstr(&optarg[1]); 1013 break; 1014 1015 case 'I': /* Limit by ID */ 1016 new = (QUEUE_CHAR *) xalloc(sizeof *new); 1017 new->queue_match = newstr(&optarg[1]); 1018 new->queue_negate = negate; 1019 new->queue_next = QueueLimitId; 1020 QueueLimitId = new; 1021 break; 1022 1023 case 'R': /* Limit by recipient */ 1024 new = (QUEUE_CHAR *) xalloc(sizeof *new); 1025 new->queue_match = newstr(&optarg[1]); 1026 new->queue_negate = negate; 1027 new->queue_next = QueueLimitRecipient; 1028 QueueLimitRecipient = new; 1029 break; 1030 1031 case 'S': /* Limit by sender */ 1032 new = (QUEUE_CHAR *) xalloc(sizeof *new); 1033 new->queue_match = newstr(&optarg[1]); 1034 new->queue_negate = negate; 1035 new->queue_next = QueueLimitSender; 1036 QueueLimitSender = new; 1037 break; 1038 1039 case 'f': /* foreground queue run */ 1040 foregroundqueue = true; 1041 break; 1042 1043 case 'Q': /* Limit by quarantine message */ 1044 if (optarg[1] != '\0') 1045 { 1046 new = (QUEUE_CHAR *) xalloc(sizeof *new); 1047 new->queue_match = newstr(&optarg[1]); 1048 new->queue_negate = negate; 1049 new->queue_next = QueueLimitQuarantine; 1050 QueueLimitQuarantine = new; 1051 } 1052 QueueMode = QM_QUARANTINE; 1053 break; 1054 1055 case 'L': /* act on lost items */ 1056 QueueMode = QM_LOST; 1057 break; 1058 1059 case 'p': /* Persistent queue */ 1060 queuepersistent = true; 1061 if (QueueIntvl == 0) 1062 QueueIntvl = 1; 1063 if (optarg[1] == '\0') 1064 break; 1065 ++optarg; 1066 /* FALLTHROUGH */ 1067 1068 default: 1069 i = Errors; 1070 QueueIntvl = convtime(optarg, 'm'); 1071 if (QueueIntvl < 0) 1072 { 1073 usrerr("Invalid -q value"); 1074 ExitStat = EX_USAGE; 1075 } 1076 1077 /* check for bad conversion */ 1078 if (i < Errors) 1079 ExitStat = EX_USAGE; 1080 break; 1081 } 1082 break; 1083 1084 case 'R': /* DSN RET: what to return */ 1085 CHECK_AGAINST_OPMODE(j); 1086 if (bitset(EF_RET_PARAM, BlankEnvelope.e_flags)) 1087 { 1088 usrerr("Duplicate -R flag"); 1089 ExitStat = EX_USAGE; 1090 break; 1091 } 1092 BlankEnvelope.e_flags |= EF_RET_PARAM; 1093 if (sm_strcasecmp(optarg, "hdrs") == 0) 1094 BlankEnvelope.e_flags |= EF_NO_BODY_RETN; 1095 else if (sm_strcasecmp(optarg, "full") != 0) 1096 { 1097 usrerr("Invalid -R value"); 1098 ExitStat = EX_USAGE; 1099 } 1100 macdefine(&BlankEnvelope.e_macro, A_TEMP, 1101 macid("{dsn_ret}"), optarg); 1102 break; 1103 1104 case 't': /* read recipients from message */ 1105 CHECK_AGAINST_OPMODE(j); 1106 GrabTo = true; 1107 break; 1108 1109 case 'V': /* DSN ENVID: set "original" envelope id */ 1110 CHECK_AGAINST_OPMODE(j); 1111 if (!xtextok(optarg)) 1112 { 1113 usrerr("Invalid syntax in -V flag"); 1114 ExitStat = EX_USAGE; 1115 } 1116 else 1117 { 1118 BlankEnvelope.e_envid = newstr(optarg); 1119 macdefine(&BlankEnvelope.e_macro, A_TEMP, 1120 macid("{dsn_envid}"), optarg); 1121 } 1122 break; 1123 1124 case 'X': /* traffic log file */ 1125 dp = drop_privileges(true); 1126 setstat(dp); 1127 if (stat(optarg, &traf_st) == 0 && 1128 S_ISFIFO(traf_st.st_mode)) 1129 TrafficLogFile = sm_io_open(SmFtStdio, 1130 SM_TIME_DEFAULT, 1131 optarg, 1132 SM_IO_WRONLY, NULL); 1133 else 1134 TrafficLogFile = sm_io_open(SmFtStdio, 1135 SM_TIME_DEFAULT, 1136 optarg, 1137 SM_IO_APPEND, NULL); 1138 if (TrafficLogFile == NULL) 1139 { 1140 syserr("cannot open %s", optarg); 1141 ExitStat = EX_CANTCREAT; 1142 break; 1143 } 1144 (void) sm_io_setvbuf(TrafficLogFile, SM_TIME_DEFAULT, 1145 NULL, SM_IO_LBF, 0); 1146 break; 1147 1148 /* compatibility flags */ 1149 case 'c': /* connect to non-local mailers */ 1150 case 'i': /* don't let dot stop me */ 1151 case 'm': /* send to me too */ 1152 case 'T': /* set timeout interval */ 1153 case 'v': /* give blow-by-blow description */ 1154 setoption(j, "T", false, true, &BlankEnvelope); 1155 break; 1156 1157 case 'e': /* error message disposition */ 1158 case 'M': /* define macro */ 1159 setoption(j, optarg, false, true, &BlankEnvelope); 1160 break; 1161 1162 case 's': /* save From lines in headers */ 1163 setoption('f', "T", false, true, &BlankEnvelope); 1164 break; 1165 1166 #ifdef DBM 1167 case 'I': /* initialize alias DBM file */ 1168 set_op_mode(MD_INITALIAS); 1169 break; 1170 #endif /* DBM */ 1171 1172 #if defined(__osf__) || defined(_AIX3) 1173 case 'x': /* random flag that OSF/1 & AIX mailx passes */ 1174 break; 1175 #endif /* defined(__osf__) || defined(_AIX3) */ 1176 #if defined(sony_news) 1177 case 'E': 1178 case 'J': /* ignore flags for Japanese code conversion 1179 implemented on Sony NEWS */ 1180 break; 1181 #endif /* defined(sony_news) */ 1182 1183 default: 1184 finis(true, true, EX_USAGE); 1185 /* NOTREACHED */ 1186 break; 1187 } 1188 } 1189 1190 /* if we've had errors so far, exit now */ 1191 if ((ExitStat != EX_OK && OpMode != MD_TEST) || 1192 ExitStat == EX_OSERR) 1193 { 1194 finis(false, true, ExitStat); 1195 /* NOTREACHED */ 1196 } 1197 1198 if (bitset(SUBMIT_MTA, SubmitMode)) 1199 { 1200 /* If set daemon_flags on command line, don't reset it */ 1201 if (macvalue(macid("{daemon_flags}"), &BlankEnvelope) == NULL) 1202 macdefine(&BlankEnvelope.e_macro, A_PERM, 1203 macid("{daemon_flags}"), "CC f"); 1204 } 1205 else if (OpMode == MD_DELIVER || OpMode == MD_SMTP) 1206 { 1207 SubmitMode = SUBMIT_MSA; 1208 1209 /* If set daemon_flags on command line, don't reset it */ 1210 if (macvalue(macid("{daemon_flags}"), &BlankEnvelope) == NULL) 1211 macdefine(&BlankEnvelope.e_macro, A_PERM, 1212 macid("{daemon_flags}"), "c u"); 1213 } 1214 1215 /* 1216 ** Do basic initialization. 1217 ** Read system control file. 1218 ** Extract special fields for local use. 1219 */ 1220 1221 #if XDEBUG 1222 checkfd012("before readcf"); 1223 #endif /* XDEBUG */ 1224 vendor_pre_defaults(&BlankEnvelope); 1225 1226 readcf(getcfname(OpMode, SubmitMode, cftype, conffile), 1227 safecf, &BlankEnvelope); 1228 #if !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_) 1229 ConfigFileRead = true; 1230 #endif /* !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_) */ 1231 vendor_post_defaults(&BlankEnvelope); 1232 1233 /* now we can complain about missing fds */ 1234 if (MissingFds != 0 && LogLevel > 8) 1235 { 1236 char mbuf[MAXLINE]; 1237 1238 mbuf[0] = '\0'; 1239 if (bitset(1 << STDIN_FILENO, MissingFds)) 1240 (void) sm_strlcat(mbuf, ", stdin", sizeof mbuf); 1241 if (bitset(1 << STDOUT_FILENO, MissingFds)) 1242 (void) sm_strlcat(mbuf, ", stdout", sizeof mbuf); 1243 if (bitset(1 << STDERR_FILENO, MissingFds)) 1244 (void) sm_strlcat(mbuf, ", stderr", sizeof mbuf); 1245 1246 /* Notice: fill_errno is from high above: fill_fd() */ 1247 sm_syslog(LOG_WARNING, NOQID, 1248 "File descriptors missing on startup: %s; %s", 1249 &mbuf[2], sm_errstring(fill_errno)); 1250 } 1251 1252 /* Remove the ability for a normal user to send signals */ 1253 if (RealUid != 0 && RealUid != geteuid()) 1254 { 1255 uid_t new_uid = geteuid(); 1256 1257 #if HASSETREUID 1258 /* 1259 ** Since we can differentiate between uid and euid, 1260 ** make the uid a different user so the real user 1261 ** can't send signals. However, it doesn't need to be 1262 ** root (euid has root). 1263 */ 1264 1265 if (new_uid == 0) 1266 new_uid = DefUid; 1267 if (tTd(47, 5)) 1268 sm_dprintf("Changing real uid to %d\n", (int) new_uid); 1269 if (setreuid(new_uid, geteuid()) < 0) 1270 { 1271 syserr("main: setreuid(%d, %d) failed", 1272 (int) new_uid, (int) geteuid()); 1273 finis(false, true, EX_OSERR); 1274 /* NOTREACHED */ 1275 } 1276 if (tTd(47, 10)) 1277 sm_dprintf("Now running as e/ruid %d:%d\n", 1278 (int) geteuid(), (int) getuid()); 1279 #else /* HASSETREUID */ 1280 /* 1281 ** Have to change both effective and real so need to 1282 ** change them both to effective to keep privs. 1283 */ 1284 1285 if (tTd(47, 5)) 1286 sm_dprintf("Changing uid to %d\n", (int) new_uid); 1287 if (setuid(new_uid) < 0) 1288 { 1289 syserr("main: setuid(%d) failed", (int) new_uid); 1290 finis(false, true, EX_OSERR); 1291 /* NOTREACHED */ 1292 } 1293 if (tTd(47, 10)) 1294 sm_dprintf("Now running as e/ruid %d:%d\n", 1295 (int) geteuid(), (int) getuid()); 1296 #endif /* HASSETREUID */ 1297 } 1298 1299 #if NAMED_BIND 1300 if (FallbackMX != NULL) 1301 (void) getfallbackmxrr(FallbackMX); 1302 #endif /* NAMED_BIND */ 1303 1304 if (SuperSafe == SAFE_INTERACTIVE && CurEnv->e_sendmode != SM_DELIVER) 1305 { 1306 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 1307 "WARNING: SuperSafe=interactive should only be used with\n DeliveryMode=interactive\n"); 1308 } 1309 1310 if (UseMSP && (OpMode == MD_DAEMON || OpMode == MD_FGDAEMON)) 1311 { 1312 usrerr("Mail submission program cannot be used as daemon"); 1313 finis(false, true, EX_USAGE); 1314 } 1315 1316 if (OpMode == MD_DELIVER || OpMode == MD_SMTP || 1317 OpMode == MD_QUEUERUN || OpMode == MD_ARPAFTP || 1318 OpMode == MD_DAEMON || OpMode == MD_FGDAEMON) 1319 makeworkgroups(); 1320 1321 /* set up the basic signal handlers */ 1322 if (sm_signal(SIGINT, SIG_IGN) != SIG_IGN) 1323 (void) sm_signal(SIGINT, intsig); 1324 (void) sm_signal(SIGTERM, intsig); 1325 1326 /* Enforce use of local time (null string overrides this) */ 1327 if (TimeZoneSpec == NULL) 1328 unsetenv("TZ"); 1329 else if (TimeZoneSpec[0] != '\0') 1330 sm_setuserenv("TZ", TimeZoneSpec); 1331 else 1332 sm_setuserenv("TZ", NULL); 1333 tzset(); 1334 1335 /* initialize mailbox database */ 1336 i = sm_mbdb_initialize(Mbdb); 1337 if (i != EX_OK) 1338 { 1339 usrerr("Can't initialize mailbox database \"%s\": %s", 1340 Mbdb, sm_strexit(i)); 1341 ExitStat = i; 1342 } 1343 1344 /* avoid denial-of-service attacks */ 1345 resetlimits(); 1346 1347 if (OpMode == MD_TEST) 1348 { 1349 /* can't be done after readcf if RunAs* is used */ 1350 dp = drop_privileges(true); 1351 if (dp != EX_OK) 1352 { 1353 finis(false, true, dp); 1354 /* NOTREACHED */ 1355 } 1356 } 1357 else if (OpMode != MD_DAEMON && OpMode != MD_FGDAEMON) 1358 { 1359 /* drop privileges -- daemon mode done after socket/bind */ 1360 dp = drop_privileges(false); 1361 setstat(dp); 1362 if (dp == EX_OK && UseMSP && (geteuid() == 0 || getuid() == 0)) 1363 { 1364 usrerr("Mail submission program must have RunAsUser set to non root user"); 1365 finis(false, true, EX_CONFIG); 1366 /* NOTREACHED */ 1367 } 1368 } 1369 1370 #if NAMED_BIND 1371 _res.retry = TimeOuts.res_retry[RES_TO_DEFAULT]; 1372 _res.retrans = TimeOuts.res_retrans[RES_TO_DEFAULT]; 1373 #endif /* NAMED_BIND */ 1374 1375 /* 1376 ** Find our real host name for future logging. 1377 */ 1378 1379 authinfo = getauthinfo(STDIN_FILENO, &forged); 1380 macdefine(&BlankEnvelope.e_macro, A_TEMP, '_', authinfo); 1381 1382 /* suppress error printing if errors mailed back or whatever */ 1383 if (BlankEnvelope.e_errormode != EM_PRINT) 1384 HoldErrs = true; 1385 1386 /* set up the $=m class now, after .cf has a chance to redefine $m */ 1387 expand("\201m", jbuf, sizeof jbuf, &BlankEnvelope); 1388 if (jbuf[0] != '\0') 1389 setclass('m', jbuf); 1390 1391 /* probe interfaces and locate any additional names */ 1392 if (DontProbeInterfaces != DPI_PROBENONE) 1393 load_if_names(); 1394 1395 if (tTd(0, 10)) 1396 { 1397 char pidpath[MAXPATHLEN]; 1398 1399 /* Now we know which .cf file we use */ 1400 sm_dprintf(" Conf file:\t%s (selected)\n", 1401 getcfname(OpMode, SubmitMode, cftype, conffile)); 1402 expand(PidFile, pidpath, sizeof pidpath, &BlankEnvelope); 1403 sm_dprintf(" Pid file:\t%s (selected)\n", pidpath); 1404 } 1405 1406 if (tTd(0, 1)) 1407 { 1408 sm_dprintf("\n============ SYSTEM IDENTITY (after readcf) ============"); 1409 sm_dprintf("\n (short domain name) $w = "); 1410 xputs(sm_debug_file(), macvalue('w', &BlankEnvelope)); 1411 sm_dprintf("\n (canonical domain name) $j = "); 1412 xputs(sm_debug_file(), macvalue('j', &BlankEnvelope)); 1413 sm_dprintf("\n (subdomain name) $m = "); 1414 xputs(sm_debug_file(), macvalue('m', &BlankEnvelope)); 1415 sm_dprintf("\n (node name) $k = "); 1416 xputs(sm_debug_file(), macvalue('k', &BlankEnvelope)); 1417 sm_dprintf("\n========================================================\n\n"); 1418 } 1419 1420 /* 1421 ** Do more command line checking -- these are things that 1422 ** have to modify the results of reading the config file. 1423 */ 1424 1425 /* process authorization warnings from command line */ 1426 if (warn_C_flag) 1427 auth_warning(&BlankEnvelope, "Processed by %s with -C %s", 1428 RealUserName, conffile); 1429 if (Warn_Q_option && !wordinclass(RealUserName, 't')) 1430 auth_warning(&BlankEnvelope, "Processed from queue %s", 1431 QueueDir); 1432 if (sysloglabel != NULL && !wordinclass(RealUserName, 't') && 1433 RealUid != 0 && RealUid != TrustedUid && LogLevel > 1) 1434 sm_syslog(LOG_WARNING, NOQID, "user %d changed syslog label", 1435 (int) RealUid); 1436 1437 /* check body type for legality */ 1438 i = check_bodytype(BlankEnvelope.e_bodytype); 1439 if (i == BODYTYPE_ILLEGAL) 1440 { 1441 usrerr("Illegal body type %s", BlankEnvelope.e_bodytype); 1442 BlankEnvelope.e_bodytype = NULL; 1443 } 1444 else if (i != BODYTYPE_NONE) 1445 SevenBitInput = (i == BODYTYPE_7BIT); 1446 1447 /* tweak default DSN notifications */ 1448 if (DefaultNotify == 0) 1449 DefaultNotify = QPINGONFAILURE|QPINGONDELAY; 1450 1451 /* check for sane configuration level */ 1452 if (ConfigLevel > MAXCONFIGLEVEL) 1453 { 1454 syserr("Warning: .cf version level (%d) exceeds sendmail version %s functionality (%d)", 1455 ConfigLevel, Version, MAXCONFIGLEVEL); 1456 } 1457 1458 /* need MCI cache to have persistence */ 1459 if (HostStatDir != NULL && MaxMciCache == 0) 1460 { 1461 HostStatDir = NULL; 1462 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 1463 "Warning: HostStatusDirectory disabled with ConnectionCacheSize = 0\n"); 1464 } 1465 1466 /* need HostStatusDir in order to have SingleThreadDelivery */ 1467 if (SingleThreadDelivery && HostStatDir == NULL) 1468 { 1469 SingleThreadDelivery = false; 1470 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 1471 "Warning: HostStatusDirectory required for SingleThreadDelivery\n"); 1472 } 1473 1474 #if _FFR_MEMSTAT 1475 j = sm_memstat_open(); 1476 if (j < 0 && (RefuseLowMem > 0 || QueueLowMem > 0) && LogLevel > 4) 1477 { 1478 sm_syslog(LOG_WARNING, NOQID, 1479 "cannot get memory statistics, settings ignored, error=%d" 1480 , j); 1481 } 1482 #endif /* _FFR_MEMSTAT */ 1483 1484 /* check for permissions */ 1485 if (RealUid != 0 && 1486 RealUid != TrustedUid) 1487 { 1488 char *action = NULL; 1489 1490 switch (OpMode) 1491 { 1492 case MD_QUEUERUN: 1493 if (quarantining != NULL) 1494 action = "quarantine jobs"; 1495 else 1496 { 1497 /* Normal users can do a single queue run */ 1498 if (QueueIntvl == 0) 1499 break; 1500 } 1501 1502 /* but not persistent queue runners */ 1503 if (action == NULL) 1504 action = "start a queue runner daemon"; 1505 /* FALLTHROUGH */ 1506 1507 case MD_PURGESTAT: 1508 if (action == NULL) 1509 action = "purge host status"; 1510 /* FALLTHROUGH */ 1511 1512 case MD_DAEMON: 1513 case MD_FGDAEMON: 1514 if (action == NULL) 1515 action = "run daemon"; 1516 1517 if (tTd(65, 1)) 1518 sm_dprintf("Deny user %d attempt to %s\n", 1519 (int) RealUid, action); 1520 1521 if (LogLevel > 1) 1522 sm_syslog(LOG_ALERT, NOQID, 1523 "user %d attempted to %s", 1524 (int) RealUid, action); 1525 HoldErrs = false; 1526 usrerr("Permission denied (real uid not trusted)"); 1527 finis(false, true, EX_USAGE); 1528 /* NOTREACHED */ 1529 break; 1530 1531 case MD_VERIFY: 1532 if (bitset(PRIV_RESTRICTEXPAND, PrivacyFlags)) 1533 { 1534 /* 1535 ** If -bv and RestrictExpand, 1536 ** drop privs to prevent normal 1537 ** users from reading private 1538 ** aliases/forwards/:include:s 1539 */ 1540 1541 if (tTd(65, 1)) 1542 sm_dprintf("Drop privs for user %d attempt to expand (RestrictExpand)\n", 1543 (int) RealUid); 1544 1545 dp = drop_privileges(true); 1546 1547 /* Fake address safety */ 1548 if (tTd(65, 1)) 1549 sm_dprintf("Faking DontBlameSendmail=NonRootSafeAddr\n"); 1550 setbitn(DBS_NONROOTSAFEADDR, DontBlameSendmail); 1551 1552 if (dp != EX_OK) 1553 { 1554 if (tTd(65, 1)) 1555 sm_dprintf("Failed to drop privs for user %d attempt to expand, exiting\n", 1556 (int) RealUid); 1557 CurEnv->e_id = NULL; 1558 finis(true, true, dp); 1559 /* NOTREACHED */ 1560 } 1561 } 1562 break; 1563 1564 case MD_TEST: 1565 case MD_PRINT: 1566 case MD_PRINTNQE: 1567 case MD_FREEZE: 1568 case MD_HOSTSTAT: 1569 /* Nothing special to check */ 1570 break; 1571 1572 case MD_INITALIAS: 1573 if (!wordinclass(RealUserName, 't')) 1574 { 1575 if (tTd(65, 1)) 1576 sm_dprintf("Deny user %d attempt to rebuild the alias map\n", 1577 (int) RealUid); 1578 if (LogLevel > 1) 1579 sm_syslog(LOG_ALERT, NOQID, 1580 "user %d attempted to rebuild the alias map", 1581 (int) RealUid); 1582 HoldErrs = false; 1583 usrerr("Permission denied (real uid not trusted)"); 1584 finis(false, true, EX_USAGE); 1585 /* NOTREACHED */ 1586 } 1587 if (UseMSP) 1588 { 1589 HoldErrs = false; 1590 usrerr("User %d cannot rebuild aliases in mail submission program", 1591 (int) RealUid); 1592 finis(false, true, EX_USAGE); 1593 /* NOTREACHED */ 1594 } 1595 /* FALLTHROUGH */ 1596 1597 default: 1598 if (bitset(PRIV_RESTRICTEXPAND, PrivacyFlags) && 1599 Verbose != 0) 1600 { 1601 /* 1602 ** If -v and RestrictExpand, reset 1603 ** Verbose to prevent normal users 1604 ** from seeing the expansion of 1605 ** aliases/forwards/:include:s 1606 */ 1607 1608 if (tTd(65, 1)) 1609 sm_dprintf("Dropping verbosity for user %d (RestrictExpand)\n", 1610 (int) RealUid); 1611 Verbose = 0; 1612 } 1613 break; 1614 } 1615 } 1616 1617 if (MeToo) 1618 BlankEnvelope.e_flags |= EF_METOO; 1619 1620 switch (OpMode) 1621 { 1622 case MD_TEST: 1623 /* don't have persistent host status in test mode */ 1624 HostStatDir = NULL; 1625 if (Verbose == 0) 1626 Verbose = 2; 1627 BlankEnvelope.e_errormode = EM_PRINT; 1628 HoldErrs = false; 1629 break; 1630 1631 case MD_VERIFY: 1632 BlankEnvelope.e_errormode = EM_PRINT; 1633 HoldErrs = false; 1634 /* arrange to exit cleanly on hangup signal */ 1635 if (sm_signal(SIGHUP, SIG_IGN) == (sigfunc_t) SIG_DFL) 1636 (void) sm_signal(SIGHUP, intsig); 1637 if (geteuid() != 0) 1638 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 1639 "Notice: -bv may give misleading output for non-privileged user\n"); 1640 break; 1641 1642 case MD_FGDAEMON: 1643 run_in_foreground = true; 1644 set_op_mode(MD_DAEMON); 1645 /* FALLTHROUGH */ 1646 1647 case MD_DAEMON: 1648 vendor_daemon_setup(&BlankEnvelope); 1649 1650 /* remove things that don't make sense in daemon mode */ 1651 FullName = NULL; 1652 GrabTo = false; 1653 1654 /* arrange to restart on hangup signal */ 1655 if (SaveArgv[0] == NULL || SaveArgv[0][0] != '/') 1656 sm_syslog(LOG_WARNING, NOQID, 1657 "daemon invoked without full pathname; kill -1 won't work"); 1658 break; 1659 1660 case MD_INITALIAS: 1661 Verbose = 2; 1662 BlankEnvelope.e_errormode = EM_PRINT; 1663 HoldErrs = false; 1664 /* FALLTHROUGH */ 1665 1666 default: 1667 /* arrange to exit cleanly on hangup signal */ 1668 if (sm_signal(SIGHUP, SIG_IGN) == (sigfunc_t) SIG_DFL) 1669 (void) sm_signal(SIGHUP, intsig); 1670 break; 1671 } 1672 1673 /* special considerations for FullName */ 1674 if (FullName != NULL) 1675 { 1676 char *full = NULL; 1677 1678 /* full names can't have newlines */ 1679 if (strchr(FullName, '\n') != NULL) 1680 { 1681 full = newstr(denlstring(FullName, true, true)); 1682 FullName = full; 1683 } 1684 1685 /* check for characters that may have to be quoted */ 1686 if (!rfc822_string(FullName)) 1687 { 1688 /* 1689 ** Quote a full name with special characters 1690 ** as a comment so crackaddr() doesn't destroy 1691 ** the name portion of the address. 1692 */ 1693 1694 FullName = addquotes(FullName, NULL); 1695 if (full != NULL) 1696 sm_free(full); /* XXX */ 1697 } 1698 } 1699 1700 /* do heuristic mode adjustment */ 1701 if (Verbose) 1702 { 1703 /* turn off noconnect option */ 1704 setoption('c', "F", true, false, &BlankEnvelope); 1705 1706 /* turn on interactive delivery */ 1707 setoption('d', "", true, false, &BlankEnvelope); 1708 } 1709 1710 #ifdef VENDOR_CODE 1711 /* check for vendor mismatch */ 1712 if (VendorCode != VENDOR_CODE) 1713 { 1714 message("Warning: .cf file vendor code mismatch: sendmail expects vendor %s, .cf file vendor is %s", 1715 getvendor(VENDOR_CODE), getvendor(VendorCode)); 1716 } 1717 #endif /* VENDOR_CODE */ 1718 1719 /* check for out of date configuration level */ 1720 if (ConfigLevel < MAXCONFIGLEVEL) 1721 { 1722 message("Warning: .cf file is out of date: sendmail %s supports version %d, .cf file is version %d", 1723 Version, MAXCONFIGLEVEL, ConfigLevel); 1724 } 1725 1726 if (ConfigLevel < 3) 1727 UseErrorsTo = true; 1728 1729 /* set options that were previous macros */ 1730 if (SmtpGreeting == NULL) 1731 { 1732 if (ConfigLevel < 7 && 1733 (p = macvalue('e', &BlankEnvelope)) != NULL) 1734 SmtpGreeting = newstr(p); 1735 else 1736 SmtpGreeting = "\201j Sendmail \201v ready at \201b"; 1737 } 1738 if (UnixFromLine == NULL) 1739 { 1740 if (ConfigLevel < 7 && 1741 (p = macvalue('l', &BlankEnvelope)) != NULL) 1742 UnixFromLine = newstr(p); 1743 else 1744 UnixFromLine = "From \201g \201d"; 1745 } 1746 SmtpError[0] = '\0'; 1747 1748 /* our name for SMTP codes */ 1749 expand("\201j", jbuf, sizeof jbuf, &BlankEnvelope); 1750 if (jbuf[0] == '\0') 1751 PSTRSET(MyHostName, "localhost"); 1752 else 1753 PSTRSET(MyHostName, jbuf); 1754 if (strchr(MyHostName, '.') == NULL) 1755 message("WARNING: local host name (%s) is not qualified; see cf/README: WHO AM I?", 1756 MyHostName); 1757 1758 /* make certain that this name is part of the $=w class */ 1759 setclass('w', MyHostName); 1760 1761 /* fill in the structure of the *default* queue */ 1762 st = stab("mqueue", ST_QUEUE, ST_FIND); 1763 if (st == NULL) 1764 syserr("No default queue (mqueue) defined"); 1765 else 1766 set_def_queueval(st->s_quegrp, true); 1767 1768 /* the indices of built-in mailers */ 1769 st = stab("local", ST_MAILER, ST_FIND); 1770 if (st != NULL) 1771 LocalMailer = st->s_mailer; 1772 else if (OpMode != MD_TEST || !warn_C_flag) 1773 syserr("No local mailer defined"); 1774 1775 st = stab("prog", ST_MAILER, ST_FIND); 1776 if (st == NULL) 1777 syserr("No prog mailer defined"); 1778 else 1779 { 1780 ProgMailer = st->s_mailer; 1781 clrbitn(M_MUSER, ProgMailer->m_flags); 1782 } 1783 1784 st = stab("*file*", ST_MAILER, ST_FIND); 1785 if (st == NULL) 1786 syserr("No *file* mailer defined"); 1787 else 1788 { 1789 FileMailer = st->s_mailer; 1790 clrbitn(M_MUSER, FileMailer->m_flags); 1791 } 1792 1793 st = stab("*include*", ST_MAILER, ST_FIND); 1794 if (st == NULL) 1795 syserr("No *include* mailer defined"); 1796 else 1797 InclMailer = st->s_mailer; 1798 1799 if (ConfigLevel < 6) 1800 { 1801 /* heuristic tweaking of local mailer for back compat */ 1802 if (LocalMailer != NULL) 1803 { 1804 setbitn(M_ALIASABLE, LocalMailer->m_flags); 1805 setbitn(M_HASPWENT, LocalMailer->m_flags); 1806 setbitn(M_TRYRULESET5, LocalMailer->m_flags); 1807 setbitn(M_CHECKINCLUDE, LocalMailer->m_flags); 1808 setbitn(M_CHECKPROG, LocalMailer->m_flags); 1809 setbitn(M_CHECKFILE, LocalMailer->m_flags); 1810 setbitn(M_CHECKUDB, LocalMailer->m_flags); 1811 } 1812 if (ProgMailer != NULL) 1813 setbitn(M_RUNASRCPT, ProgMailer->m_flags); 1814 if (FileMailer != NULL) 1815 setbitn(M_RUNASRCPT, FileMailer->m_flags); 1816 } 1817 if (ConfigLevel < 7) 1818 { 1819 if (LocalMailer != NULL) 1820 setbitn(M_VRFY250, LocalMailer->m_flags); 1821 if (ProgMailer != NULL) 1822 setbitn(M_VRFY250, ProgMailer->m_flags); 1823 if (FileMailer != NULL) 1824 setbitn(M_VRFY250, FileMailer->m_flags); 1825 } 1826 1827 /* MIME Content-Types that cannot be transfer encoded */ 1828 setclass('n', "multipart/signed"); 1829 1830 /* MIME message/xxx subtypes that can be treated as messages */ 1831 setclass('s', "rfc822"); 1832 1833 /* MIME Content-Transfer-Encodings that can be encoded */ 1834 setclass('e', "7bit"); 1835 setclass('e', "8bit"); 1836 setclass('e', "binary"); 1837 1838 #ifdef USE_B_CLASS 1839 /* MIME Content-Types that should be treated as binary */ 1840 setclass('b', "image"); 1841 setclass('b', "audio"); 1842 setclass('b', "video"); 1843 setclass('b', "application/octet-stream"); 1844 #endif /* USE_B_CLASS */ 1845 1846 /* MIME headers which have fields to check for overflow */ 1847 setclass(macid("{checkMIMEFieldHeaders}"), "content-disposition"); 1848 setclass(macid("{checkMIMEFieldHeaders}"), "content-type"); 1849 1850 /* MIME headers to check for length overflow */ 1851 setclass(macid("{checkMIMETextHeaders}"), "content-description"); 1852 1853 /* MIME headers to check for overflow and rebalance */ 1854 setclass(macid("{checkMIMEHeaders}"), "content-disposition"); 1855 setclass(macid("{checkMIMEHeaders}"), "content-id"); 1856 setclass(macid("{checkMIMEHeaders}"), "content-transfer-encoding"); 1857 setclass(macid("{checkMIMEHeaders}"), "content-type"); 1858 setclass(macid("{checkMIMEHeaders}"), "mime-version"); 1859 1860 /* Macros to save in the queue file -- don't remove any */ 1861 setclass(macid("{persistentMacros}"), "r"); 1862 setclass(macid("{persistentMacros}"), "s"); 1863 setclass(macid("{persistentMacros}"), "_"); 1864 setclass(macid("{persistentMacros}"), "{if_addr}"); 1865 setclass(macid("{persistentMacros}"), "{daemon_flags}"); 1866 1867 /* operate in queue directory */ 1868 if (QueueDir == NULL || *QueueDir == '\0') 1869 { 1870 if (OpMode != MD_TEST) 1871 { 1872 syserr("QueueDirectory (Q) option must be set"); 1873 ExitStat = EX_CONFIG; 1874 } 1875 } 1876 else 1877 { 1878 if (OpMode != MD_TEST) 1879 setup_queues(OpMode == MD_DAEMON); 1880 } 1881 1882 /* check host status directory for validity */ 1883 if (HostStatDir != NULL && !path_is_dir(HostStatDir, false)) 1884 { 1885 /* cannot use this value */ 1886 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 1887 "Warning: Cannot use HostStatusDirectory = %s: %s\n", 1888 HostStatDir, sm_errstring(errno)); 1889 HostStatDir = NULL; 1890 } 1891 1892 if (OpMode == MD_QUEUERUN && 1893 RealUid != 0 && bitset(PRIV_RESTRICTQRUN, PrivacyFlags)) 1894 { 1895 struct stat stbuf; 1896 1897 /* check to see if we own the queue directory */ 1898 if (stat(".", &stbuf) < 0) 1899 syserr("main: cannot stat %s", QueueDir); 1900 if (stbuf.st_uid != RealUid) 1901 { 1902 /* nope, really a botch */ 1903 HoldErrs = false; 1904 usrerr("You do not have permission to process the queue"); 1905 finis(false, true, EX_NOPERM); 1906 /* NOTREACHED */ 1907 } 1908 } 1909 1910 #if MILTER 1911 /* sanity checks on milter filters */ 1912 if (OpMode == MD_DAEMON || OpMode == MD_SMTP) 1913 { 1914 milter_config(InputFilterList, InputFilters, MAXFILTERS); 1915 setup_daemon_milters(); 1916 } 1917 #endif /* MILTER */ 1918 1919 /* Convert queuegroup string to qgrp number */ 1920 if (queuegroup != NULL) 1921 { 1922 qgrp = name2qid(queuegroup); 1923 if (qgrp == NOQGRP) 1924 { 1925 HoldErrs = false; 1926 usrerr("Queue group %s unknown", queuegroup); 1927 finis(false, true, ExitStat); 1928 /* NOTREACHED */ 1929 } 1930 } 1931 1932 /* if we've had errors so far, exit now */ 1933 if (ExitStat != EX_OK && OpMode != MD_TEST) 1934 { 1935 finis(false, true, ExitStat); 1936 /* NOTREACHED */ 1937 } 1938 1939 #if SASL 1940 /* sendmail specific SASL initialization */ 1941 sm_sasl_init(); 1942 #endif /* SASL */ 1943 1944 #if XDEBUG 1945 checkfd012("before main() initmaps"); 1946 #endif /* XDEBUG */ 1947 1948 /* 1949 ** Do operation-mode-dependent initialization. 1950 */ 1951 1952 switch (OpMode) 1953 { 1954 case MD_PRINT: 1955 /* print the queue */ 1956 HoldErrs = false; 1957 dropenvelope(&BlankEnvelope, true, false); 1958 (void) sm_signal(SIGPIPE, sigpipe); 1959 if (qgrp != NOQGRP) 1960 { 1961 int j; 1962 1963 /* Selecting a particular queue group to run */ 1964 for (j = 0; j < Queue[qgrp]->qg_numqueues; j++) 1965 { 1966 if (StopRequest) 1967 stop_sendmail(); 1968 (void) print_single_queue(qgrp, j); 1969 } 1970 finis(false, true, EX_OK); 1971 /* NOTREACHED */ 1972 } 1973 printqueue(); 1974 finis(false, true, EX_OK); 1975 /* NOTREACHED */ 1976 break; 1977 1978 case MD_PRINTNQE: 1979 /* print number of entries in queue */ 1980 dropenvelope(&BlankEnvelope, true, false); 1981 (void) sm_signal(SIGPIPE, sigpipe); 1982 printnqe(smioout, NULL); 1983 finis(false, true, EX_OK); 1984 /* NOTREACHED */ 1985 break; 1986 1987 case MD_QUEUERUN: 1988 /* only handle quarantining here */ 1989 if (quarantining == NULL) 1990 break; 1991 1992 if (QueueMode != QM_QUARANTINE && 1993 QueueMode != QM_NORMAL) 1994 { 1995 HoldErrs = false; 1996 usrerr("Can not use -Q with -q%c", QueueMode); 1997 ExitStat = EX_USAGE; 1998 finis(false, true, ExitStat); 1999 /* NOTREACHED */ 2000 } 2001 quarantine_queue(quarantining, qgrp); 2002 finis(false, true, EX_OK); 2003 break; 2004 2005 case MD_HOSTSTAT: 2006 (void) sm_signal(SIGPIPE, sigpipe); 2007 (void) mci_traverse_persistent(mci_print_persistent, NULL); 2008 finis(false, true, EX_OK); 2009 /* NOTREACHED */ 2010 break; 2011 2012 case MD_PURGESTAT: 2013 (void) mci_traverse_persistent(mci_purge_persistent, NULL); 2014 finis(false, true, EX_OK); 2015 /* NOTREACHED */ 2016 break; 2017 2018 case MD_INITALIAS: 2019 /* initialize maps */ 2020 initmaps(); 2021 finis(false, true, ExitStat); 2022 /* NOTREACHED */ 2023 break; 2024 2025 case MD_SMTP: 2026 case MD_DAEMON: 2027 /* reset DSN parameters */ 2028 DefaultNotify = QPINGONFAILURE|QPINGONDELAY; 2029 macdefine(&BlankEnvelope.e_macro, A_PERM, 2030 macid("{dsn_notify}"), NULL); 2031 BlankEnvelope.e_envid = NULL; 2032 macdefine(&BlankEnvelope.e_macro, A_PERM, 2033 macid("{dsn_envid}"), NULL); 2034 BlankEnvelope.e_flags &= ~(EF_RET_PARAM|EF_NO_BODY_RETN); 2035 macdefine(&BlankEnvelope.e_macro, A_PERM, 2036 macid("{dsn_ret}"), NULL); 2037 2038 /* don't open maps for daemon -- done below in child */ 2039 break; 2040 } 2041 2042 if (tTd(0, 15)) 2043 { 2044 /* print configuration table (or at least part of it) */ 2045 if (tTd(0, 90)) 2046 printrules(); 2047 for (i = 0; i < MAXMAILERS; i++) 2048 { 2049 if (Mailer[i] != NULL) 2050 printmailer(sm_debug_file(), Mailer[i]); 2051 } 2052 } 2053 2054 /* 2055 ** Switch to the main envelope. 2056 */ 2057 2058 CurEnv = newenvelope(&MainEnvelope, &BlankEnvelope, 2059 sm_rpool_new_x(NULL)); 2060 MainEnvelope.e_flags = BlankEnvelope.e_flags; 2061 2062 /* 2063 ** If test mode, read addresses from stdin and process. 2064 */ 2065 2066 if (OpMode == MD_TEST) 2067 { 2068 if (isatty(sm_io_getinfo(smioin, SM_IO_WHAT_FD, NULL))) 2069 Verbose = 2; 2070 2071 if (Verbose) 2072 { 2073 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 2074 "ADDRESS TEST MODE (ruleset 3 NOT automatically invoked)\n"); 2075 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 2076 "Enter <ruleset> <address>\n"); 2077 } 2078 macdefine(&(MainEnvelope.e_macro), A_PERM, 2079 macid("{addr_type}"), "e r"); 2080 for (;;) 2081 { 2082 SM_TRY 2083 { 2084 (void) sm_signal(SIGINT, intindebug); 2085 (void) sm_releasesignal(SIGINT); 2086 if (Verbose == 2) 2087 (void) sm_io_fprintf(smioout, 2088 SM_TIME_DEFAULT, 2089 "> "); 2090 (void) sm_io_flush(smioout, SM_TIME_DEFAULT); 2091 if (sm_io_fgets(smioin, SM_TIME_DEFAULT, buf, 2092 sizeof buf) == NULL) 2093 testmodeline("/quit", &MainEnvelope); 2094 p = strchr(buf, '\n'); 2095 if (p != NULL) 2096 *p = '\0'; 2097 if (Verbose < 2) 2098 (void) sm_io_fprintf(smioout, 2099 SM_TIME_DEFAULT, 2100 "> %s\n", buf); 2101 testmodeline(buf, &MainEnvelope); 2102 } 2103 SM_EXCEPT(exc, "[!F]*") 2104 { 2105 /* 2106 ** 8.10 just prints \n on interrupt. 2107 ** I'm printing the exception here in case 2108 ** sendmail is extended to raise additional 2109 ** exceptions in this context. 2110 */ 2111 2112 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 2113 "\n"); 2114 sm_exc_print(exc, smioout); 2115 } 2116 SM_END_TRY 2117 } 2118 } 2119 2120 #if STARTTLS 2121 tls_ok = true; 2122 if (OpMode == MD_QUEUERUN || OpMode == MD_DELIVER) 2123 { 2124 /* check whether STARTTLS is turned off for the client */ 2125 if (chkclientmodifiers(D_NOTLS)) 2126 tls_ok = false; 2127 } 2128 else if (OpMode == MD_DAEMON || OpMode == MD_FGDAEMON || 2129 OpMode == MD_SMTP) 2130 { 2131 /* check whether STARTTLS is turned off for the server */ 2132 if (chkdaemonmodifiers(D_NOTLS)) 2133 tls_ok = false; 2134 } 2135 else /* other modes don't need STARTTLS */ 2136 tls_ok = false; 2137 2138 if (tls_ok) 2139 { 2140 /* basic TLS initialization */ 2141 tls_ok = init_tls_library(); 2142 } 2143 2144 if (!tls_ok && (OpMode == MD_QUEUERUN || OpMode == MD_DELIVER)) 2145 { 2146 /* disable TLS for client */ 2147 setclttls(false); 2148 } 2149 #endif /* STARTTLS */ 2150 2151 /* 2152 ** If collecting stuff from the queue, go start doing that. 2153 */ 2154 2155 if (OpMode == MD_QUEUERUN && QueueIntvl == 0) 2156 { 2157 pid_t pid = -1; 2158 2159 #if STARTTLS 2160 /* init TLS for client, ignore result for now */ 2161 (void) initclttls(tls_ok); 2162 #endif /* STARTTLS */ 2163 2164 /* 2165 ** The parent process of the caller of runqueue() needs 2166 ** to stay around for a possible SIGTERM. The SIGTERM will 2167 ** tell this process that all of the queue runners children 2168 ** need to be sent SIGTERM as well. At the same time, we 2169 ** want to return control to the command line. So we do an 2170 ** extra fork(). 2171 */ 2172 2173 if (Verbose || foregroundqueue || (pid = fork()) <= 0) 2174 { 2175 /* 2176 ** If the fork() failed we should still try to do 2177 ** the queue run. If it succeeded then the child 2178 ** is going to start the run and wait for all 2179 ** of the children to finish. 2180 */ 2181 2182 if (pid == 0) 2183 { 2184 /* Reset global flags */ 2185 RestartRequest = NULL; 2186 ShutdownRequest = NULL; 2187 PendingSignal = 0; 2188 2189 /* disconnect from terminal */ 2190 disconnect(2, CurEnv); 2191 } 2192 2193 CurrentPid = getpid(); 2194 if (qgrp != NOQGRP) 2195 { 2196 int rwgflags = RWG_NONE; 2197 2198 /* 2199 ** To run a specific queue group mark it to 2200 ** be run, select the work group it's in and 2201 ** increment the work counter. 2202 */ 2203 2204 for (i = 0; i < NumQueue && Queue[i] != NULL; 2205 i++) 2206 Queue[i]->qg_nextrun = (time_t) -1; 2207 Queue[qgrp]->qg_nextrun = 0; 2208 if (Verbose) 2209 rwgflags |= RWG_VERBOSE; 2210 if (queuepersistent) 2211 rwgflags |= RWG_PERSISTENT; 2212 rwgflags |= RWG_FORCE; 2213 (void) run_work_group(Queue[qgrp]->qg_wgrp, 2214 rwgflags); 2215 } 2216 else 2217 (void) runqueue(false, Verbose, 2218 queuepersistent, true); 2219 2220 /* set the title to make it easier to find */ 2221 sm_setproctitle(true, CurEnv, "Queue control"); 2222 (void) sm_signal(SIGCHLD, SIG_DFL); 2223 while (CurChildren > 0) 2224 { 2225 int status; 2226 pid_t ret; 2227 2228 errno = 0; 2229 while ((ret = sm_wait(&status)) <= 0) 2230 { 2231 if (errno == ECHILD) 2232 { 2233 /* 2234 ** Oops... something got messed 2235 ** up really bad. Waiting for 2236 ** non-existent children 2237 ** shouldn't happen. Let's get 2238 ** out of here. 2239 */ 2240 2241 CurChildren = 0; 2242 break; 2243 } 2244 continue; 2245 } 2246 2247 /* something is really really wrong */ 2248 if (errno == ECHILD) 2249 { 2250 sm_syslog(LOG_ERR, NOQID, 2251 "queue control process: lost all children: wait returned ECHILD"); 2252 break; 2253 } 2254 2255 /* Only drop when a child gives status */ 2256 if (WIFSTOPPED(status)) 2257 continue; 2258 2259 proc_list_drop(ret, status, NULL); 2260 } 2261 } 2262 finis(true, true, ExitStat); 2263 /* NOTREACHED */ 2264 } 2265 2266 # if SASL 2267 if (OpMode == MD_SMTP || OpMode == MD_DAEMON) 2268 { 2269 /* check whether AUTH is turned off for the server */ 2270 if (!chkdaemonmodifiers(D_NOAUTH) && 2271 (i = sasl_server_init(srvcallbacks, "Sendmail")) != SASL_OK) 2272 syserr("!sasl_server_init failed! [%s]", 2273 sasl_errstring(i, NULL, NULL)); 2274 } 2275 # endif /* SASL */ 2276 2277 if (OpMode == MD_SMTP) 2278 { 2279 proc_list_add(CurrentPid, "Sendmail SMTP Agent", 2280 PROC_DAEMON, 0, -1, NULL); 2281 2282 /* clean up background delivery children */ 2283 (void) sm_signal(SIGCHLD, reapchild); 2284 } 2285 2286 /* 2287 ** If a daemon, wait for a request. 2288 ** getrequests will always return in a child. 2289 ** If we should also be processing the queue, start 2290 ** doing it in background. 2291 ** We check for any errors that might have happened 2292 ** during startup. 2293 */ 2294 2295 if (OpMode == MD_DAEMON || QueueIntvl > 0) 2296 { 2297 char dtype[200]; 2298 2299 if (!run_in_foreground && !tTd(99, 100)) 2300 { 2301 /* put us in background */ 2302 i = fork(); 2303 if (i < 0) 2304 syserr("daemon: cannot fork"); 2305 if (i != 0) 2306 { 2307 finis(false, true, EX_OK); 2308 /* NOTREACHED */ 2309 } 2310 2311 /* 2312 ** Initialize exception stack and default exception 2313 ** handler for child process. 2314 */ 2315 2316 /* Reset global flags */ 2317 RestartRequest = NULL; 2318 RestartWorkGroup = false; 2319 ShutdownRequest = NULL; 2320 PendingSignal = 0; 2321 CurrentPid = getpid(); 2322 2323 sm_exc_newthread(fatal_error); 2324 2325 /* disconnect from our controlling tty */ 2326 disconnect(2, &MainEnvelope); 2327 } 2328 2329 dtype[0] = '\0'; 2330 if (OpMode == MD_DAEMON) 2331 { 2332 (void) sm_strlcat(dtype, "+SMTP", sizeof dtype); 2333 DaemonPid = CurrentPid; 2334 } 2335 if (QueueIntvl > 0) 2336 { 2337 (void) sm_strlcat2(dtype, 2338 queuepersistent 2339 ? "+persistent-queueing@" 2340 : "+queueing@", 2341 pintvl(QueueIntvl, true), 2342 sizeof dtype); 2343 } 2344 if (tTd(0, 1)) 2345 (void) sm_strlcat(dtype, "+debugging", sizeof dtype); 2346 2347 sm_syslog(LOG_INFO, NOQID, 2348 "starting daemon (%s): %s", Version, dtype + 1); 2349 #if XLA 2350 xla_create_file(); 2351 #endif /* XLA */ 2352 2353 /* save daemon type in a macro for possible PidFile use */ 2354 macdefine(&BlankEnvelope.e_macro, A_TEMP, 2355 macid("{daemon_info}"), dtype + 1); 2356 2357 /* save queue interval in a macro for possible PidFile use */ 2358 macdefine(&MainEnvelope.e_macro, A_TEMP, 2359 macid("{queue_interval}"), pintvl(QueueIntvl, true)); 2360 2361 /* workaround: can't seem to release the signal in the parent */ 2362 (void) sm_signal(SIGHUP, sighup); 2363 (void) sm_releasesignal(SIGHUP); 2364 (void) sm_signal(SIGTERM, sigterm); 2365 2366 if (QueueIntvl > 0) 2367 { 2368 (void) runqueue(true, false, queuepersistent, true); 2369 2370 /* 2371 ** If queuepersistent but not in daemon mode then 2372 ** we're going to do the queue runner monitoring here. 2373 ** If in daemon mode then the monitoring will happen 2374 ** elsewhere. 2375 */ 2376 2377 if (OpMode != MD_DAEMON && queuepersistent) 2378 { 2379 /* 2380 ** Write the pid to file 2381 ** XXX Overwrites sendmail.pid 2382 */ 2383 2384 log_sendmail_pid(&MainEnvelope); 2385 2386 /* set the title to make it easier to find */ 2387 sm_setproctitle(true, CurEnv, "Queue control"); 2388 (void) sm_signal(SIGCHLD, SIG_DFL); 2389 while (CurChildren > 0) 2390 { 2391 int status; 2392 pid_t ret; 2393 int group; 2394 2395 CHECK_RESTART; 2396 errno = 0; 2397 while ((ret = sm_wait(&status)) <= 0) 2398 { 2399 /* 2400 ** Waiting for non-existent 2401 ** children shouldn't happen. 2402 ** Let's get out of here if 2403 ** it occurs. 2404 */ 2405 2406 if (errno == ECHILD) 2407 { 2408 CurChildren = 0; 2409 break; 2410 } 2411 continue; 2412 } 2413 2414 /* something is really really wrong */ 2415 if (errno == ECHILD) 2416 { 2417 sm_syslog(LOG_ERR, NOQID, 2418 "persistent queue runner control process: lost all children: wait returned ECHILD"); 2419 break; 2420 } 2421 2422 if (WIFSTOPPED(status)) 2423 continue; 2424 2425 /* Probe only on a child status */ 2426 proc_list_drop(ret, status, &group); 2427 2428 if (WIFSIGNALED(status)) 2429 { 2430 if (WCOREDUMP(status)) 2431 { 2432 sm_syslog(LOG_ERR, NOQID, 2433 "persistent queue runner=%d core dumped, signal=%d", 2434 group, WTERMSIG(status)); 2435 2436 /* don't restart this */ 2437 mark_work_group_restart( 2438 group, -1); 2439 continue; 2440 } 2441 2442 sm_syslog(LOG_ERR, NOQID, 2443 "persistent queue runner=%d died, signal=%d", 2444 group, WTERMSIG(status)); 2445 } 2446 2447 /* 2448 ** When debugging active, don't 2449 ** restart the persistent queues. 2450 ** But do log this as info. 2451 */ 2452 2453 if (sm_debug_active(&DebugNoPRestart, 2454 1)) 2455 { 2456 sm_syslog(LOG_DEBUG, NOQID, 2457 "persistent queue runner=%d, exited", 2458 group); 2459 mark_work_group_restart(group, 2460 -1); 2461 } 2462 } 2463 finis(true, true, ExitStat); 2464 /* NOTREACHED */ 2465 } 2466 2467 if (OpMode != MD_DAEMON) 2468 { 2469 char qtype[200]; 2470 2471 /* 2472 ** Write the pid to file 2473 ** XXX Overwrites sendmail.pid 2474 */ 2475 2476 log_sendmail_pid(&MainEnvelope); 2477 2478 /* set the title to make it easier to find */ 2479 qtype[0] = '\0'; 2480 (void) sm_strlcpyn(qtype, sizeof qtype, 4, 2481 "Queue runner@", 2482 pintvl(QueueIntvl, true), 2483 " for ", 2484 QueueDir); 2485 sm_setproctitle(true, CurEnv, qtype); 2486 for (;;) 2487 { 2488 (void) pause(); 2489 2490 CHECK_RESTART; 2491 2492 if (doqueuerun()) 2493 (void) runqueue(true, false, 2494 false, false); 2495 } 2496 } 2497 } 2498 dropenvelope(&MainEnvelope, true, false); 2499 2500 #if STARTTLS 2501 /* init TLS for server, ignore result for now */ 2502 (void) initsrvtls(tls_ok); 2503 #endif /* STARTTLS */ 2504 2505 nextreq: 2506 p_flags = getrequests(&MainEnvelope); 2507 2508 /* drop privileges */ 2509 (void) drop_privileges(false); 2510 2511 /* 2512 ** Get authentication data 2513 ** Set _ macro in BlankEnvelope before calling newenvelope(). 2514 */ 2515 2516 authinfo = getauthinfo(sm_io_getinfo(InChannel, SM_IO_WHAT_FD, 2517 NULL), &forged); 2518 macdefine(&BlankEnvelope.e_macro, A_TEMP, '_', authinfo); 2519 2520 /* at this point we are in a child: reset state */ 2521 sm_rpool_free(MainEnvelope.e_rpool); 2522 (void) newenvelope(&MainEnvelope, &MainEnvelope, 2523 sm_rpool_new_x(NULL)); 2524 } 2525 2526 if (LogLevel > 9) 2527 { 2528 /* log connection information */ 2529 sm_syslog(LOG_INFO, NULL, "connect from %s", authinfo); 2530 } 2531 2532 /* 2533 ** If running SMTP protocol, start collecting and executing 2534 ** commands. This will never return. 2535 */ 2536 2537 if (OpMode == MD_SMTP || OpMode == MD_DAEMON) 2538 { 2539 char pbuf[20]; 2540 2541 /* 2542 ** Save some macros for check_* rulesets. 2543 */ 2544 2545 if (forged) 2546 { 2547 char ipbuf[103]; 2548 2549 (void) sm_snprintf(ipbuf, sizeof ipbuf, "[%.100s]", 2550 anynet_ntoa(&RealHostAddr)); 2551 macdefine(&BlankEnvelope.e_macro, A_TEMP, 2552 macid("{client_name}"), ipbuf); 2553 } 2554 else 2555 macdefine(&BlankEnvelope.e_macro, A_PERM, 2556 macid("{client_name}"), RealHostName); 2557 macdefine(&BlankEnvelope.e_macro, A_PERM, 2558 macid("{client_ptr}"), RealHostName); 2559 macdefine(&BlankEnvelope.e_macro, A_TEMP, 2560 macid("{client_addr}"), anynet_ntoa(&RealHostAddr)); 2561 sm_getla(); 2562 2563 switch (RealHostAddr.sa.sa_family) 2564 { 2565 #if NETINET 2566 case AF_INET: 2567 (void) sm_snprintf(pbuf, sizeof pbuf, "%d", 2568 RealHostAddr.sin.sin_port); 2569 break; 2570 #endif /* NETINET */ 2571 #if NETINET6 2572 case AF_INET6: 2573 (void) sm_snprintf(pbuf, sizeof pbuf, "%d", 2574 RealHostAddr.sin6.sin6_port); 2575 break; 2576 #endif /* NETINET6 */ 2577 default: 2578 (void) sm_snprintf(pbuf, sizeof pbuf, "0"); 2579 break; 2580 } 2581 macdefine(&BlankEnvelope.e_macro, A_TEMP, 2582 macid("{client_port}"), pbuf); 2583 2584 if (OpMode == MD_DAEMON) 2585 { 2586 /* validate the connection */ 2587 HoldErrs = true; 2588 nullserver = validate_connection(&RealHostAddr, 2589 macvalue(macid("{client_name}"), 2590 &MainEnvelope), 2591 &MainEnvelope); 2592 HoldErrs = false; 2593 } 2594 else if (p_flags == NULL) 2595 { 2596 p_flags = (BITMAP256 *) xalloc(sizeof *p_flags); 2597 clrbitmap(p_flags); 2598 } 2599 #if STARTTLS 2600 if (OpMode == MD_SMTP) 2601 (void) initsrvtls(tls_ok); 2602 #endif /* STARTTLS */ 2603 2604 /* turn off profiling */ 2605 SM_PROF(1); 2606 smtp(nullserver, *p_flags, &MainEnvelope); 2607 2608 if (tTd(93, 100)) 2609 { 2610 /* turn off profiling */ 2611 SM_PROF(0); 2612 if (OpMode == MD_DAEMON) 2613 goto nextreq; 2614 } 2615 } 2616 2617 sm_rpool_free(MainEnvelope.e_rpool); 2618 clearenvelope(&MainEnvelope, false, sm_rpool_new_x(NULL)); 2619 if (OpMode == MD_VERIFY) 2620 { 2621 set_delivery_mode(SM_VERIFY, &MainEnvelope); 2622 PostMasterCopy = NULL; 2623 } 2624 else 2625 { 2626 /* interactive -- all errors are global */ 2627 MainEnvelope.e_flags |= EF_GLOBALERRS|EF_LOGSENDER; 2628 } 2629 2630 /* 2631 ** Do basic system initialization and set the sender 2632 */ 2633 2634 initsys(&MainEnvelope); 2635 macdefine(&MainEnvelope.e_macro, A_PERM, macid("{ntries}"), "0"); 2636 macdefine(&MainEnvelope.e_macro, A_PERM, macid("{nrcpts}"), "0"); 2637 setsender(from, &MainEnvelope, NULL, '\0', false); 2638 if (warn_f_flag != '\0' && !wordinclass(RealUserName, 't') && 2639 (!bitnset(M_LOCALMAILER, MainEnvelope.e_from.q_mailer->m_flags) || 2640 strcmp(MainEnvelope.e_from.q_user, RealUserName) != 0)) 2641 { 2642 auth_warning(&MainEnvelope, "%s set sender to %s using -%c", 2643 RealUserName, from, warn_f_flag); 2644 #if SASL 2645 auth = false; 2646 #endif /* SASL */ 2647 } 2648 if (auth) 2649 { 2650 char *fv; 2651 2652 /* set the initial sender for AUTH= to $f@$j */ 2653 fv = macvalue('f', &MainEnvelope); 2654 if (fv == NULL || *fv == '\0') 2655 MainEnvelope.e_auth_param = NULL; 2656 else 2657 { 2658 if (strchr(fv, '@') == NULL) 2659 { 2660 i = strlen(fv) + strlen(macvalue('j', 2661 &MainEnvelope)) + 2; 2662 p = sm_malloc_x(i); 2663 (void) sm_strlcpyn(p, i, 3, fv, "@", 2664 macvalue('j', 2665 &MainEnvelope)); 2666 } 2667 else 2668 p = sm_strdup_x(fv); 2669 MainEnvelope.e_auth_param = sm_rpool_strdup_x(MainEnvelope.e_rpool, 2670 xtextify(p, "=")); 2671 sm_free(p); /* XXX */ 2672 } 2673 } 2674 if (macvalue('s', &MainEnvelope) == NULL) 2675 macdefine(&MainEnvelope.e_macro, A_PERM, 's', RealHostName); 2676 2677 av = argv + optind; 2678 if (*av == NULL && !GrabTo) 2679 { 2680 MainEnvelope.e_to = NULL; 2681 MainEnvelope.e_flags |= EF_GLOBALERRS; 2682 HoldErrs = false; 2683 SuperSafe = SAFE_NO; 2684 usrerr("Recipient names must be specified"); 2685 2686 /* collect body for UUCP return */ 2687 if (OpMode != MD_VERIFY) 2688 collect(InChannel, false, NULL, &MainEnvelope, true); 2689 finis(true, true, EX_USAGE); 2690 /* NOTREACHED */ 2691 } 2692 2693 /* 2694 ** Scan argv and deliver the message to everyone. 2695 */ 2696 2697 save_val = LogUsrErrs; 2698 LogUsrErrs = true; 2699 sendtoargv(av, &MainEnvelope); 2700 LogUsrErrs = save_val; 2701 2702 /* if we have had errors sofar, arrange a meaningful exit stat */ 2703 if (Errors > 0 && ExitStat == EX_OK) 2704 ExitStat = EX_USAGE; 2705 2706 #if _FFR_FIX_DASHT 2707 /* 2708 ** If using -t, force not sending to argv recipients, even 2709 ** if they are mentioned in the headers. 2710 */ 2711 2712 if (GrabTo) 2713 { 2714 ADDRESS *q; 2715 2716 for (q = MainEnvelope.e_sendqueue; q != NULL; q = q->q_next) 2717 q->q_state = QS_REMOVED; 2718 } 2719 #endif /* _FFR_FIX_DASHT */ 2720 2721 /* 2722 ** Read the input mail. 2723 */ 2724 2725 MainEnvelope.e_to = NULL; 2726 if (OpMode != MD_VERIFY || GrabTo) 2727 { 2728 int savederrors; 2729 unsigned long savedflags; 2730 2731 /* 2732 ** workaround for compiler warning on Irix: 2733 ** do not initialize variable in the definition, but 2734 ** later on: 2735 ** warning(1548): transfer of control bypasses 2736 ** initialization of: 2737 ** variable "savederrors" (declared at line 2570) 2738 ** variable "savedflags" (declared at line 2571) 2739 ** goto giveup; 2740 */ 2741 2742 savederrors = Errors; 2743 savedflags = MainEnvelope.e_flags & EF_FATALERRS; 2744 MainEnvelope.e_flags |= EF_GLOBALERRS; 2745 MainEnvelope.e_flags &= ~EF_FATALERRS; 2746 Errors = 0; 2747 buffer_errors(); 2748 collect(InChannel, false, NULL, &MainEnvelope, true); 2749 2750 /* header checks failed */ 2751 if (Errors > 0) 2752 { 2753 giveup: 2754 if (!GrabTo) 2755 { 2756 /* Log who the mail would have gone to */ 2757 logundelrcpts(&MainEnvelope, 2758 MainEnvelope.e_message, 2759 8, false); 2760 } 2761 flush_errors(true); 2762 finis(true, true, ExitStat); 2763 /* NOTREACHED */ 2764 return -1; 2765 } 2766 2767 /* bail out if message too large */ 2768 if (bitset(EF_CLRQUEUE, MainEnvelope.e_flags)) 2769 { 2770 finis(true, true, ExitStat != EX_OK ? ExitStat 2771 : EX_DATAERR); 2772 /* NOTREACHED */ 2773 return -1; 2774 } 2775 2776 /* set message size */ 2777 (void) sm_snprintf(buf, sizeof buf, "%ld", 2778 MainEnvelope.e_msgsize); 2779 macdefine(&MainEnvelope.e_macro, A_TEMP, 2780 macid("{msg_size}"), buf); 2781 2782 Errors = savederrors; 2783 MainEnvelope.e_flags |= savedflags; 2784 } 2785 errno = 0; 2786 2787 if (tTd(1, 1)) 2788 sm_dprintf("From person = \"%s\"\n", 2789 MainEnvelope.e_from.q_paddr); 2790 2791 /* Check if quarantining stats should be updated */ 2792 if (MainEnvelope.e_quarmsg != NULL) 2793 markstats(&MainEnvelope, NULL, STATS_QUARANTINE); 2794 2795 /* 2796 ** Actually send everything. 2797 ** If verifying, just ack. 2798 */ 2799 2800 if (Errors == 0) 2801 { 2802 if (!split_by_recipient(&MainEnvelope) && 2803 bitset(EF_FATALERRS, MainEnvelope.e_flags)) 2804 goto giveup; 2805 } 2806 2807 /* make sure we deliver at least the first envelope */ 2808 i = FastSplit > 0 ? 0 : -1; 2809 for (e = &MainEnvelope; e != NULL; e = e->e_sibling, i++) 2810 { 2811 ENVELOPE *next; 2812 2813 e->e_from.q_state = QS_SENDER; 2814 if (tTd(1, 5)) 2815 { 2816 sm_dprintf("main[%d]: QS_SENDER ", i); 2817 printaddr(sm_debug_file(), &e->e_from, false); 2818 } 2819 e->e_to = NULL; 2820 sm_getla(); 2821 GrabTo = false; 2822 #if NAMED_BIND 2823 _res.retry = TimeOuts.res_retry[RES_TO_FIRST]; 2824 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST]; 2825 #endif /* NAMED_BIND */ 2826 next = e->e_sibling; 2827 e->e_sibling = NULL; 2828 2829 /* after FastSplit envelopes: queue up */ 2830 sendall(e, i >= FastSplit ? SM_QUEUE : SM_DEFAULT); 2831 e->e_sibling = next; 2832 } 2833 2834 /* 2835 ** All done. 2836 ** Don't send return error message if in VERIFY mode. 2837 */ 2838 2839 finis(true, true, ExitStat); 2840 /* NOTREACHED */ 2841 return ExitStat; 2842 } 2843 /* 2844 ** STOP_SENDMAIL -- Stop the running program 2845 ** 2846 ** Parameters: 2847 ** none. 2848 ** 2849 ** Returns: 2850 ** none. 2851 ** 2852 ** Side Effects: 2853 ** exits. 2854 */ 2855 2856 void 2857 stop_sendmail() 2858 { 2859 /* reset uid for process accounting */ 2860 endpwent(); 2861 (void) setuid(RealUid); 2862 exit(EX_OK); 2863 } 2864 /* 2865 ** FINIS -- Clean up and exit. 2866 ** 2867 ** Parameters: 2868 ** drop -- whether or not to drop CurEnv envelope 2869 ** cleanup -- call exit() or _exit()? 2870 ** exitstat -- exit status to use for exit() call 2871 ** 2872 ** Returns: 2873 ** never 2874 ** 2875 ** Side Effects: 2876 ** exits sendmail 2877 */ 2878 2879 void 2880 finis(drop, cleanup, exitstat) 2881 bool drop; 2882 bool cleanup; 2883 volatile int exitstat; 2884 { 2885 char pidpath[MAXPATHLEN]; 2886 2887 /* Still want to process new timeouts added below */ 2888 sm_clear_events(); 2889 (void) sm_releasesignal(SIGALRM); 2890 2891 if (tTd(2, 1)) 2892 { 2893 sm_dprintf("\n====finis: stat %d e_id=%s e_flags=", 2894 exitstat, 2895 CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id); 2896 printenvflags(CurEnv); 2897 } 2898 if (tTd(2, 9)) 2899 printopenfds(false); 2900 2901 SM_TRY 2902 /* 2903 ** Clean up. This might raise E:mta.quickabort 2904 */ 2905 2906 /* clean up temp files */ 2907 CurEnv->e_to = NULL; 2908 if (drop) 2909 { 2910 if (CurEnv->e_id != NULL) 2911 { 2912 dropenvelope(CurEnv, true, false); 2913 sm_rpool_free(CurEnv->e_rpool); 2914 CurEnv->e_rpool = NULL; 2915 } 2916 else 2917 poststats(StatFile); 2918 } 2919 2920 /* flush any cached connections */ 2921 mci_flush(true, NULL); 2922 2923 /* close maps belonging to this pid */ 2924 closemaps(false); 2925 2926 #if USERDB 2927 /* close UserDatabase */ 2928 _udbx_close(); 2929 #endif /* USERDB */ 2930 2931 #if SASL 2932 stop_sasl_client(); 2933 #endif /* SASL */ 2934 2935 #if XLA 2936 /* clean up extended load average stuff */ 2937 xla_all_end(); 2938 #endif /* XLA */ 2939 2940 SM_FINALLY 2941 /* 2942 ** And exit. 2943 */ 2944 2945 if (LogLevel > 78) 2946 sm_syslog(LOG_DEBUG, CurEnv->e_id, "finis, pid=%d", 2947 (int) CurrentPid); 2948 if (exitstat == EX_TEMPFAIL || 2949 CurEnv->e_errormode == EM_BERKNET) 2950 exitstat = EX_OK; 2951 2952 /* XXX clean up queues and related data structures */ 2953 cleanup_queues(); 2954 #if SM_CONF_SHM 2955 cleanup_shm(DaemonPid == getpid()); 2956 #endif /* SM_CONF_SHM */ 2957 2958 /* close locked pid file */ 2959 close_sendmail_pid(); 2960 2961 if (DaemonPid == getpid() || PidFilePid == getpid()) 2962 { 2963 /* blow away the pid file */ 2964 expand(PidFile, pidpath, sizeof pidpath, CurEnv); 2965 (void) unlink(pidpath); 2966 } 2967 2968 /* reset uid for process accounting */ 2969 endpwent(); 2970 sm_mbdb_terminate(); 2971 #if _FFR_MEMSTAT 2972 (void) sm_memstat_close(); 2973 #endif /* _FFR_MEMSTAT */ 2974 (void) setuid(RealUid); 2975 #if SM_HEAP_CHECK 2976 /* dump the heap, if we are checking for memory leaks */ 2977 if (sm_debug_active(&SmHeapCheck, 2)) 2978 sm_heap_report(smioout, 2979 sm_debug_level(&SmHeapCheck) - 1); 2980 #endif /* SM_HEAP_CHECK */ 2981 if (sm_debug_active(&SmXtrapReport, 1)) 2982 sm_dprintf("xtrap count = %d\n", SmXtrapCount); 2983 if (cleanup) 2984 exit(exitstat); 2985 else 2986 _exit(exitstat); 2987 SM_END_TRY 2988 } 2989 /* 2990 ** INTINDEBUG -- signal handler for SIGINT in -bt mode 2991 ** 2992 ** Parameters: 2993 ** sig -- incoming signal. 2994 ** 2995 ** Returns: 2996 ** none. 2997 ** 2998 ** Side Effects: 2999 ** longjmps back to test mode loop. 3000 ** 3001 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3002 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3003 ** DOING. 3004 */ 3005 3006 /* Type of an exception generated on SIGINT during address test mode. */ 3007 static const SM_EXC_TYPE_T EtypeInterrupt = 3008 { 3009 SmExcTypeMagic, 3010 "S:mta.interrupt", 3011 "", 3012 sm_etype_printf, 3013 "interrupt", 3014 }; 3015 3016 /* ARGSUSED */ 3017 static SIGFUNC_DECL 3018 intindebug(sig) 3019 int sig; 3020 { 3021 int save_errno = errno; 3022 3023 FIX_SYSV_SIGNAL(sig, intindebug); 3024 errno = save_errno; 3025 CHECK_CRITICAL(sig); 3026 errno = save_errno; 3027 sm_exc_raisenew_x(&EtypeInterrupt); 3028 errno = save_errno; 3029 return SIGFUNC_RETURN; 3030 } 3031 /* 3032 ** SIGTERM -- SIGTERM handler for the daemon 3033 ** 3034 ** Parameters: 3035 ** sig -- signal number. 3036 ** 3037 ** Returns: 3038 ** none. 3039 ** 3040 ** Side Effects: 3041 ** Sets ShutdownRequest which will hopefully trigger 3042 ** the daemon to exit. 3043 ** 3044 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3045 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3046 ** DOING. 3047 */ 3048 3049 /* ARGSUSED */ 3050 static SIGFUNC_DECL 3051 sigterm(sig) 3052 int sig; 3053 { 3054 int save_errno = errno; 3055 3056 FIX_SYSV_SIGNAL(sig, sigterm); 3057 ShutdownRequest = "signal"; 3058 errno = save_errno; 3059 return SIGFUNC_RETURN; 3060 } 3061 /* 3062 ** SIGHUP -- handle a SIGHUP signal 3063 ** 3064 ** Parameters: 3065 ** sig -- incoming signal. 3066 ** 3067 ** Returns: 3068 ** none. 3069 ** 3070 ** Side Effects: 3071 ** Sets RestartRequest which should cause the daemon 3072 ** to restart. 3073 ** 3074 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3075 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3076 ** DOING. 3077 */ 3078 3079 /* ARGSUSED */ 3080 static SIGFUNC_DECL 3081 sighup(sig) 3082 int sig; 3083 { 3084 int save_errno = errno; 3085 3086 FIX_SYSV_SIGNAL(sig, sighup); 3087 RestartRequest = "signal"; 3088 errno = save_errno; 3089 return SIGFUNC_RETURN; 3090 } 3091 /* 3092 ** SIGPIPE -- signal handler for SIGPIPE 3093 ** 3094 ** Parameters: 3095 ** sig -- incoming signal. 3096 ** 3097 ** Returns: 3098 ** none. 3099 ** 3100 ** Side Effects: 3101 ** Sets StopRequest which should cause the mailq/hoststatus 3102 ** display to stop. 3103 ** 3104 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3105 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3106 ** DOING. 3107 */ 3108 3109 /* ARGSUSED */ 3110 static SIGFUNC_DECL 3111 sigpipe(sig) 3112 int sig; 3113 { 3114 int save_errno = errno; 3115 3116 FIX_SYSV_SIGNAL(sig, sigpipe); 3117 StopRequest = true; 3118 errno = save_errno; 3119 return SIGFUNC_RETURN; 3120 } 3121 /* 3122 ** INTSIG -- clean up on interrupt 3123 ** 3124 ** This just arranges to exit. It pessimizes in that it 3125 ** may resend a message. 3126 ** 3127 ** Parameters: 3128 ** none. 3129 ** 3130 ** Returns: 3131 ** none. 3132 ** 3133 ** Side Effects: 3134 ** Unlocks the current job. 3135 ** 3136 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3137 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3138 ** DOING. 3139 ** 3140 ** XXX: More work is needed for this signal handler. 3141 */ 3142 3143 /* ARGSUSED */ 3144 SIGFUNC_DECL 3145 intsig(sig) 3146 int sig; 3147 { 3148 bool drop = false; 3149 int save_errno = errno; 3150 3151 FIX_SYSV_SIGNAL(sig, intsig); 3152 errno = save_errno; 3153 CHECK_CRITICAL(sig); 3154 sm_allsignals(true); 3155 3156 if (sig != 0 && LogLevel > 79) 3157 sm_syslog(LOG_DEBUG, CurEnv->e_id, "interrupt"); 3158 FileName = NULL; 3159 3160 /* Clean-up on aborted stdin message submission */ 3161 if (CurEnv->e_id != NULL && 3162 (OpMode == MD_SMTP || 3163 OpMode == MD_DELIVER || 3164 OpMode == MD_ARPAFTP)) 3165 { 3166 register ADDRESS *q; 3167 3168 /* don't return an error indication */ 3169 CurEnv->e_to = NULL; 3170 CurEnv->e_flags &= ~EF_FATALERRS; 3171 CurEnv->e_flags |= EF_CLRQUEUE; 3172 3173 /* 3174 ** Spin through the addresses and 3175 ** mark them dead to prevent bounces 3176 */ 3177 3178 for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 3179 q->q_state = QS_DONTSEND; 3180 3181 drop = true; 3182 } 3183 else if (OpMode != MD_TEST) 3184 { 3185 unlockqueue(CurEnv); 3186 } 3187 3188 finis(drop, false, EX_OK); 3189 /* NOTREACHED */ 3190 } 3191 /* 3192 ** DISCONNECT -- remove our connection with any foreground process 3193 ** 3194 ** Parameters: 3195 ** droplev -- how "deeply" we should drop the line. 3196 ** 0 -- ignore signals, mail back errors, make sure 3197 ** output goes to stdout. 3198 ** 1 -- also, make stdout go to /dev/null. 3199 ** 2 -- also, disconnect from controlling terminal 3200 ** (only for daemon mode). 3201 ** e -- the current envelope. 3202 ** 3203 ** Returns: 3204 ** none 3205 ** 3206 ** Side Effects: 3207 ** Trys to insure that we are immune to vagaries of 3208 ** the controlling tty. 3209 */ 3210 3211 void 3212 disconnect(droplev, e) 3213 int droplev; 3214 register ENVELOPE *e; 3215 { 3216 int fd; 3217 3218 if (tTd(52, 1)) 3219 sm_dprintf("disconnect: In %d Out %d, e=%p\n", 3220 sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL), 3221 sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL), e); 3222 if (tTd(52, 100)) 3223 { 3224 sm_dprintf("don't\n"); 3225 return; 3226 } 3227 if (LogLevel > 93) 3228 sm_syslog(LOG_DEBUG, e->e_id, 3229 "disconnect level %d", 3230 droplev); 3231 3232 /* be sure we don't get nasty signals */ 3233 (void) sm_signal(SIGINT, SIG_IGN); 3234 (void) sm_signal(SIGQUIT, SIG_IGN); 3235 3236 /* we can't communicate with our caller, so.... */ 3237 HoldErrs = true; 3238 CurEnv->e_errormode = EM_MAIL; 3239 Verbose = 0; 3240 DisConnected = true; 3241 3242 /* all input from /dev/null */ 3243 if (InChannel != smioin) 3244 { 3245 (void) sm_io_close(InChannel, SM_TIME_DEFAULT); 3246 InChannel = smioin; 3247 } 3248 if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL, 3249 SM_IO_RDONLY, NULL, smioin) == NULL) 3250 sm_syslog(LOG_ERR, e->e_id, 3251 "disconnect: sm_io_reopen(\"%s\") failed: %s", 3252 SM_PATH_DEVNULL, sm_errstring(errno)); 3253 3254 /* 3255 ** output to the transcript 3256 ** We also compare the fd numbers here since OutChannel 3257 ** might be a layer on top of smioout due to encryption 3258 ** (see sfsasl.c). 3259 */ 3260 3261 if (OutChannel != smioout && 3262 sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL) != 3263 sm_io_getinfo(smioout, SM_IO_WHAT_FD, NULL)) 3264 { 3265 (void) sm_io_close(OutChannel, SM_TIME_DEFAULT); 3266 OutChannel = smioout; 3267 3268 #if 0 3269 /* 3270 ** Has smioout been closed? Reopen it. 3271 ** This shouldn't happen anymore, the code is here 3272 ** just as a reminder. 3273 */ 3274 3275 if (smioout->sm_magic == NULL && 3276 sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL, 3277 SM_IO_WRONLY, NULL, smioout) == NULL) 3278 sm_syslog(LOG_ERR, e->e_id, 3279 "disconnect: sm_io_reopen(\"%s\") failed: %s", 3280 SM_PATH_DEVNULL, sm_errstring(errno)); 3281 #endif /* 0 */ 3282 } 3283 if (droplev > 0) 3284 { 3285 fd = open(SM_PATH_DEVNULL, O_WRONLY, 0666); 3286 if (fd == -1) 3287 sm_syslog(LOG_ERR, e->e_id, 3288 "disconnect: open(\"%s\") failed: %s", 3289 SM_PATH_DEVNULL, sm_errstring(errno)); 3290 (void) sm_io_flush(smioout, SM_TIME_DEFAULT); 3291 (void) dup2(fd, STDOUT_FILENO); 3292 (void) dup2(fd, STDERR_FILENO); 3293 (void) close(fd); 3294 } 3295 3296 /* drop our controlling TTY completely if possible */ 3297 if (droplev > 1) 3298 { 3299 (void) setsid(); 3300 errno = 0; 3301 } 3302 3303 #if XDEBUG 3304 checkfd012("disconnect"); 3305 #endif /* XDEBUG */ 3306 3307 if (LogLevel > 71) 3308 sm_syslog(LOG_DEBUG, e->e_id, "in background, pid=%d", 3309 (int) CurrentPid); 3310 3311 errno = 0; 3312 } 3313 3314 static void 3315 obsolete(argv) 3316 char *argv[]; 3317 { 3318 register char *ap; 3319 register char *op; 3320 3321 while ((ap = *++argv) != NULL) 3322 { 3323 /* Return if "--" or not an option of any form. */ 3324 if (ap[0] != '-' || ap[1] == '-') 3325 return; 3326 3327 /* Don't allow users to use "-Q." or "-Q ." */ 3328 if ((ap[1] == 'Q' && ap[2] == '.') || 3329 (ap[1] == 'Q' && argv[1] != NULL && 3330 argv[1][0] == '.' && argv[1][1] == '\0')) 3331 { 3332 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 3333 "Can not use -Q.\n"); 3334 exit(EX_USAGE); 3335 } 3336 3337 /* skip over options that do have a value */ 3338 op = strchr(OPTIONS, ap[1]); 3339 if (op != NULL && *++op == ':' && ap[2] == '\0' && 3340 ap[1] != 'd' && 3341 #if defined(sony_news) 3342 ap[1] != 'E' && ap[1] != 'J' && 3343 #endif /* defined(sony_news) */ 3344 argv[1] != NULL && argv[1][0] != '-') 3345 { 3346 argv++; 3347 continue; 3348 } 3349 3350 /* If -C doesn't have an argument, use sendmail.cf. */ 3351 #define __DEFPATH "sendmail.cf" 3352 if (ap[1] == 'C' && ap[2] == '\0') 3353 { 3354 *argv = xalloc(sizeof(__DEFPATH) + 2); 3355 (void) sm_strlcpyn(argv[0], sizeof(__DEFPATH) + 2, 2, 3356 "-C", __DEFPATH); 3357 } 3358 3359 /* If -q doesn't have an argument, run it once. */ 3360 if (ap[1] == 'q' && ap[2] == '\0') 3361 *argv = "-q0"; 3362 3363 /* If -Q doesn't have an argument, disable quarantining */ 3364 if (ap[1] == 'Q' && ap[2] == '\0') 3365 *argv = "-Q."; 3366 3367 /* if -d doesn't have an argument, use 0-99.1 */ 3368 if (ap[1] == 'd' && ap[2] == '\0') 3369 *argv = "-d0-99.1"; 3370 3371 #if defined(sony_news) 3372 /* if -E doesn't have an argument, use -EC */ 3373 if (ap[1] == 'E' && ap[2] == '\0') 3374 *argv = "-EC"; 3375 3376 /* if -J doesn't have an argument, use -JJ */ 3377 if (ap[1] == 'J' && ap[2] == '\0') 3378 *argv = "-JJ"; 3379 #endif /* defined(sony_news) */ 3380 } 3381 } 3382 /* 3383 ** AUTH_WARNING -- specify authorization warning 3384 ** 3385 ** Parameters: 3386 ** e -- the current envelope. 3387 ** msg -- the text of the message. 3388 ** args -- arguments to the message. 3389 ** 3390 ** Returns: 3391 ** none. 3392 */ 3393 3394 void 3395 #ifdef __STDC__ 3396 auth_warning(register ENVELOPE *e, const char *msg, ...) 3397 #else /* __STDC__ */ 3398 auth_warning(e, msg, va_alist) 3399 register ENVELOPE *e; 3400 const char *msg; 3401 va_dcl 3402 #endif /* __STDC__ */ 3403 { 3404 char buf[MAXLINE]; 3405 SM_VA_LOCAL_DECL 3406 3407 if (bitset(PRIV_AUTHWARNINGS, PrivacyFlags)) 3408 { 3409 register char *p; 3410 static char hostbuf[48]; 3411 3412 if (hostbuf[0] == '\0') 3413 { 3414 struct hostent *hp; 3415 3416 hp = myhostname(hostbuf, sizeof hostbuf); 3417 #if NETINET6 3418 if (hp != NULL) 3419 { 3420 freehostent(hp); 3421 hp = NULL; 3422 } 3423 #endif /* NETINET6 */ 3424 } 3425 3426 (void) sm_strlcpyn(buf, sizeof buf, 2, hostbuf, ": "); 3427 p = &buf[strlen(buf)]; 3428 SM_VA_START(ap, msg); 3429 (void) sm_vsnprintf(p, SPACELEFT(buf, p), msg, ap); 3430 SM_VA_END(ap); 3431 addheader("X-Authentication-Warning", buf, 0, e); 3432 if (LogLevel > 3) 3433 sm_syslog(LOG_INFO, e->e_id, 3434 "Authentication-Warning: %.400s", 3435 buf); 3436 } 3437 } 3438 /* 3439 ** GETEXTENV -- get from external environment 3440 ** 3441 ** Parameters: 3442 ** envar -- the name of the variable to retrieve 3443 ** 3444 ** Returns: 3445 ** The value, if any. 3446 */ 3447 3448 static char * 3449 getextenv(envar) 3450 const char *envar; 3451 { 3452 char **envp; 3453 int l; 3454 3455 l = strlen(envar); 3456 for (envp = ExternalEnviron; envp != NULL && *envp != NULL; envp++) 3457 { 3458 if (strncmp(*envp, envar, l) == 0 && (*envp)[l] == '=') 3459 return &(*envp)[l + 1]; 3460 } 3461 return NULL; 3462 } 3463 /* 3464 ** SM_SETUSERENV -- set an environment variable in the propagated environment 3465 ** 3466 ** Parameters: 3467 ** envar -- the name of the environment variable. 3468 ** value -- the value to which it should be set. If 3469 ** null, this is extracted from the incoming 3470 ** environment. If that is not set, the call 3471 ** to sm_setuserenv is ignored. 3472 ** 3473 ** Returns: 3474 ** none. 3475 */ 3476 3477 void 3478 sm_setuserenv(envar, value) 3479 const char *envar; 3480 const char *value; 3481 { 3482 int i, l; 3483 char **evp = UserEnviron; 3484 char *p; 3485 3486 if (value == NULL) 3487 { 3488 value = getextenv(envar); 3489 if (value == NULL) 3490 return; 3491 } 3492 3493 /* XXX enforce reasonable size? */ 3494 i = strlen(envar) + 1; 3495 l = strlen(value) + i + 1; 3496 p = (char *) xalloc(l); 3497 (void) sm_strlcpyn(p, l, 3, envar, "=", value); 3498 3499 while (*evp != NULL && strncmp(*evp, p, i) != 0) 3500 evp++; 3501 if (*evp != NULL) 3502 { 3503 *evp++ = p; 3504 } 3505 else if (evp < &UserEnviron[MAXUSERENVIRON]) 3506 { 3507 *evp++ = p; 3508 *evp = NULL; 3509 } 3510 3511 /* make sure it is in our environment as well */ 3512 if (putenv(p) < 0) 3513 syserr("sm_setuserenv: putenv(%s) failed", p); 3514 } 3515 /* 3516 ** DUMPSTATE -- dump state 3517 ** 3518 ** For debugging. 3519 */ 3520 3521 void 3522 dumpstate(when) 3523 char *when; 3524 { 3525 register char *j = macvalue('j', CurEnv); 3526 int rs; 3527 extern int NextMacroId; 3528 3529 sm_syslog(LOG_DEBUG, CurEnv->e_id, 3530 "--- dumping state on %s: $j = %s ---", 3531 when, 3532 j == NULL ? "<NULL>" : j); 3533 if (j != NULL) 3534 { 3535 if (!wordinclass(j, 'w')) 3536 sm_syslog(LOG_DEBUG, CurEnv->e_id, 3537 "*** $j not in $=w ***"); 3538 } 3539 sm_syslog(LOG_DEBUG, CurEnv->e_id, "CurChildren = %d", CurChildren); 3540 sm_syslog(LOG_DEBUG, CurEnv->e_id, "NextMacroId = %d (Max %d)", 3541 NextMacroId, MAXMACROID); 3542 sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- open file descriptors: ---"); 3543 printopenfds(true); 3544 sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- connection cache: ---"); 3545 mci_dump_all(smioout, true); 3546 rs = strtorwset("debug_dumpstate", NULL, ST_FIND); 3547 if (rs > 0) 3548 { 3549 int status; 3550 register char **pvp; 3551 char *pv[MAXATOM + 1]; 3552 3553 pv[0] = NULL; 3554 status = REWRITE(pv, rs, CurEnv); 3555 sm_syslog(LOG_DEBUG, CurEnv->e_id, 3556 "--- ruleset debug_dumpstate returns stat %d, pv: ---", 3557 status); 3558 for (pvp = pv; *pvp != NULL; pvp++) 3559 sm_syslog(LOG_DEBUG, CurEnv->e_id, "%s", *pvp); 3560 } 3561 sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- end of state dump ---"); 3562 } 3563 3564 #ifdef SIGUSR1 3565 /* 3566 ** SIGUSR1 -- Signal a request to dump state. 3567 ** 3568 ** Parameters: 3569 ** sig -- calling signal. 3570 ** 3571 ** Returns: 3572 ** none. 3573 ** 3574 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3575 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3576 ** DOING. 3577 ** 3578 ** XXX: More work is needed for this signal handler. 3579 */ 3580 3581 /* ARGSUSED */ 3582 static SIGFUNC_DECL 3583 sigusr1(sig) 3584 int sig; 3585 { 3586 int save_errno = errno; 3587 # if SM_HEAP_CHECK 3588 extern void dumpstab __P((void)); 3589 # endif /* SM_HEAP_CHECK */ 3590 3591 FIX_SYSV_SIGNAL(sig, sigusr1); 3592 errno = save_errno; 3593 CHECK_CRITICAL(sig); 3594 dumpstate("user signal"); 3595 # if SM_HEAP_CHECK 3596 dumpstab(); 3597 # endif /* SM_HEAP_CHECK */ 3598 errno = save_errno; 3599 return SIGFUNC_RETURN; 3600 } 3601 #endif /* SIGUSR1 */ 3602 3603 /* 3604 ** DROP_PRIVILEGES -- reduce privileges to those of the RunAsUser option 3605 ** 3606 ** Parameters: 3607 ** to_real_uid -- if set, drop to the real uid instead 3608 ** of the RunAsUser. 3609 ** 3610 ** Returns: 3611 ** EX_OSERR if the setuid failed. 3612 ** EX_OK otherwise. 3613 */ 3614 3615 int 3616 drop_privileges(to_real_uid) 3617 bool to_real_uid; 3618 { 3619 int rval = EX_OK; 3620 GIDSET_T emptygidset[1]; 3621 3622 if (tTd(47, 1)) 3623 sm_dprintf("drop_privileges(%d): Real[UG]id=%d:%d, get[ug]id=%d:%d, gete[ug]id=%d:%d, RunAs[UG]id=%d:%d\n", 3624 (int) to_real_uid, 3625 (int) RealUid, (int) RealGid, 3626 (int) getuid(), (int) getgid(), 3627 (int) geteuid(), (int) getegid(), 3628 (int) RunAsUid, (int) RunAsGid); 3629 3630 if (to_real_uid) 3631 { 3632 RunAsUserName = RealUserName; 3633 RunAsUid = RealUid; 3634 RunAsGid = RealGid; 3635 EffGid = RunAsGid; 3636 } 3637 3638 /* make sure no one can grab open descriptors for secret files */ 3639 endpwent(); 3640 sm_mbdb_terminate(); 3641 3642 /* reset group permissions; these can be set later */ 3643 emptygidset[0] = (to_real_uid || RunAsGid != 0) ? RunAsGid : getegid(); 3644 3645 /* 3646 ** Notice: on some OS (Linux...) the setgroups() call causes 3647 ** a logfile entry if sendmail is not run by root. 3648 ** However, it is unclear (no POSIX standard) whether 3649 ** setgroups() can only succeed if executed by root. 3650 ** So for now we keep it as it is; if you want to change it, use 3651 ** if (geteuid() == 0 && setgroups(1, emptygidset) == -1) 3652 */ 3653 3654 if (setgroups(1, emptygidset) == -1 && geteuid() == 0) 3655 { 3656 syserr("drop_privileges: setgroups(1, %d) failed", 3657 (int) emptygidset[0]); 3658 rval = EX_OSERR; 3659 } 3660 3661 /* reset primary group id */ 3662 if (to_real_uid) 3663 { 3664 /* 3665 ** Drop gid to real gid. 3666 ** On some OS we must reset the effective[/real[/saved]] gid, 3667 ** and then use setgid() to finally drop all group privileges. 3668 ** Later on we check whether we can get back the 3669 ** effective gid. 3670 */ 3671 3672 #if HASSETEGID 3673 if (setegid(RunAsGid) < 0) 3674 { 3675 syserr("drop_privileges: setegid(%d) failed", 3676 (int) RunAsGid); 3677 rval = EX_OSERR; 3678 } 3679 #else /* HASSETEGID */ 3680 # if HASSETREGID 3681 if (setregid(RunAsGid, RunAsGid) < 0) 3682 { 3683 syserr("drop_privileges: setregid(%d, %d) failed", 3684 (int) RunAsGid, (int) RunAsGid); 3685 rval = EX_OSERR; 3686 } 3687 # else /* HASSETREGID */ 3688 # if HASSETRESGID 3689 if (setresgid(RunAsGid, RunAsGid, RunAsGid) < 0) 3690 { 3691 syserr("drop_privileges: setresgid(%d, %d, %d) failed", 3692 (int) RunAsGid, (int) RunAsGid, (int) RunAsGid); 3693 rval = EX_OSERR; 3694 } 3695 # endif /* HASSETRESGID */ 3696 # endif /* HASSETREGID */ 3697 #endif /* HASSETEGID */ 3698 } 3699 if (rval == EX_OK && (to_real_uid || RunAsGid != 0)) 3700 { 3701 if (setgid(RunAsGid) < 0 && (!UseMSP || getegid() != RunAsGid)) 3702 { 3703 syserr("drop_privileges: setgid(%d) failed", 3704 (int) RunAsGid); 3705 rval = EX_OSERR; 3706 } 3707 errno = 0; 3708 if (rval == EX_OK && getegid() != RunAsGid) 3709 { 3710 syserr("drop_privileges: Unable to set effective gid=%d to RunAsGid=%d", 3711 (int) getegid(), (int) RunAsGid); 3712 rval = EX_OSERR; 3713 } 3714 } 3715 3716 /* fiddle with uid */ 3717 if (to_real_uid || RunAsUid != 0) 3718 { 3719 uid_t euid; 3720 3721 /* 3722 ** Try to setuid(RunAsUid). 3723 ** euid must be RunAsUid, 3724 ** ruid must be RunAsUid unless (e|r)uid wasn't 0 3725 ** and we didn't have to drop privileges to the real uid. 3726 */ 3727 3728 if (setuid(RunAsUid) < 0 || 3729 geteuid() != RunAsUid || 3730 (getuid() != RunAsUid && 3731 (to_real_uid || geteuid() == 0 || getuid() == 0))) 3732 { 3733 #if HASSETREUID 3734 /* 3735 ** if ruid != RunAsUid, euid == RunAsUid, then 3736 ** try resetting just the real uid, then using 3737 ** setuid() to drop the saved-uid as well. 3738 */ 3739 3740 if (geteuid() == RunAsUid) 3741 { 3742 if (setreuid(RunAsUid, -1) < 0) 3743 { 3744 syserr("drop_privileges: setreuid(%d, -1) failed", 3745 (int) RunAsUid); 3746 rval = EX_OSERR; 3747 } 3748 if (setuid(RunAsUid) < 0) 3749 { 3750 syserr("drop_privileges: second setuid(%d) attempt failed", 3751 (int) RunAsUid); 3752 rval = EX_OSERR; 3753 } 3754 } 3755 else 3756 #endif /* HASSETREUID */ 3757 { 3758 syserr("drop_privileges: setuid(%d) failed", 3759 (int) RunAsUid); 3760 rval = EX_OSERR; 3761 } 3762 } 3763 euid = geteuid(); 3764 if (RunAsUid != 0 && setuid(0) == 0) 3765 { 3766 /* 3767 ** Believe it or not, the Linux capability model 3768 ** allows a non-root process to override setuid() 3769 ** on a process running as root and prevent that 3770 ** process from dropping privileges. 3771 */ 3772 3773 syserr("drop_privileges: setuid(0) succeeded (when it should not)"); 3774 rval = EX_OSERR; 3775 } 3776 else if (RunAsUid != euid && setuid(euid) == 0) 3777 { 3778 /* 3779 ** Some operating systems will keep the saved-uid 3780 ** if a non-root effective-uid calls setuid(real-uid) 3781 ** making it possible to set it back again later. 3782 */ 3783 3784 syserr("drop_privileges: Unable to drop non-root set-user-ID privileges"); 3785 rval = EX_OSERR; 3786 } 3787 } 3788 3789 if ((to_real_uid || RunAsGid != 0) && 3790 rval == EX_OK && RunAsGid != EffGid && 3791 getuid() != 0 && geteuid() != 0) 3792 { 3793 errno = 0; 3794 if (setgid(EffGid) == 0) 3795 { 3796 syserr("drop_privileges: setgid(%d) succeeded (when it should not)", 3797 (int) EffGid); 3798 rval = EX_OSERR; 3799 } 3800 } 3801 3802 if (tTd(47, 5)) 3803 { 3804 sm_dprintf("drop_privileges: e/ruid = %d/%d e/rgid = %d/%d\n", 3805 (int) geteuid(), (int) getuid(), 3806 (int) getegid(), (int) getgid()); 3807 sm_dprintf("drop_privileges: RunAsUser = %d:%d\n", 3808 (int) RunAsUid, (int) RunAsGid); 3809 if (tTd(47, 10)) 3810 sm_dprintf("drop_privileges: rval = %d\n", rval); 3811 } 3812 return rval; 3813 } 3814 /* 3815 ** FILL_FD -- make sure a file descriptor has been properly allocated 3816 ** 3817 ** Used to make sure that stdin/out/err are allocated on startup 3818 ** 3819 ** Parameters: 3820 ** fd -- the file descriptor to be filled. 3821 ** where -- a string used for logging. If NULL, this is 3822 ** being called on startup, and logging should 3823 ** not be done. 3824 ** 3825 ** Returns: 3826 ** none 3827 ** 3828 ** Side Effects: 3829 ** possibly changes MissingFds 3830 */ 3831 3832 void 3833 fill_fd(fd, where) 3834 int fd; 3835 char *where; 3836 { 3837 int i; 3838 struct stat stbuf; 3839 3840 if (fstat(fd, &stbuf) >= 0 || errno != EBADF) 3841 return; 3842 3843 if (where != NULL) 3844 syserr("fill_fd: %s: fd %d not open", where, fd); 3845 else 3846 MissingFds |= 1 << fd; 3847 i = open(SM_PATH_DEVNULL, fd == 0 ? O_RDONLY : O_WRONLY, 0666); 3848 if (i < 0) 3849 { 3850 syserr("!fill_fd: %s: cannot open %s", 3851 where == NULL ? "startup" : where, SM_PATH_DEVNULL); 3852 } 3853 if (fd != i) 3854 { 3855 (void) dup2(i, fd); 3856 (void) close(i); 3857 } 3858 } 3859 /* 3860 ** SM_PRINTOPTIONS -- print options 3861 ** 3862 ** Parameters: 3863 ** options -- array of options. 3864 ** 3865 ** Returns: 3866 ** none. 3867 */ 3868 3869 static void 3870 sm_printoptions(options) 3871 char **options; 3872 { 3873 int ll; 3874 char **av; 3875 3876 av = options; 3877 ll = 7; 3878 while (*av != NULL) 3879 { 3880 if (ll + strlen(*av) > 63) 3881 { 3882 sm_dprintf("\n"); 3883 ll = 0; 3884 } 3885 if (ll == 0) 3886 sm_dprintf("\t\t"); 3887 else 3888 sm_dprintf(" "); 3889 sm_dprintf("%s", *av); 3890 ll += strlen(*av++) + 1; 3891 } 3892 sm_dprintf("\n"); 3893 } 3894 /* 3895 ** TESTMODELINE -- process a test mode input line 3896 ** 3897 ** Parameters: 3898 ** line -- the input line. 3899 ** e -- the current environment. 3900 ** Syntax: 3901 ** # a comment 3902 ** .X process X as a configuration line 3903 ** =X dump a configuration item (such as mailers) 3904 ** $X dump a macro or class 3905 ** /X try an activity 3906 ** X normal process through rule set X 3907 */ 3908 3909 static void 3910 testmodeline(line, e) 3911 char *line; 3912 ENVELOPE *e; 3913 { 3914 register char *p; 3915 char *q; 3916 auto char *delimptr; 3917 int mid; 3918 int i, rs; 3919 STAB *map; 3920 char **s; 3921 struct rewrite *rw; 3922 ADDRESS a; 3923 static int tryflags = RF_COPYNONE; 3924 char exbuf[MAXLINE]; 3925 extern unsigned char TokTypeNoC[]; 3926 3927 /* skip leading spaces */ 3928 while (*line == ' ') 3929 line++; 3930 3931 switch (line[0]) 3932 { 3933 case '#': 3934 case '\0': 3935 return; 3936 3937 case '?': 3938 help("-bt", e); 3939 return; 3940 3941 case '.': /* config-style settings */ 3942 switch (line[1]) 3943 { 3944 case 'D': 3945 mid = macid_parse(&line[2], &delimptr); 3946 if (mid == 0) 3947 return; 3948 translate_dollars(delimptr); 3949 macdefine(&e->e_macro, A_TEMP, mid, delimptr); 3950 break; 3951 3952 case 'C': 3953 if (line[2] == '\0') /* not to call syserr() */ 3954 return; 3955 3956 mid = macid_parse(&line[2], &delimptr); 3957 if (mid == 0) 3958 return; 3959 translate_dollars(delimptr); 3960 expand(delimptr, exbuf, sizeof exbuf, e); 3961 p = exbuf; 3962 while (*p != '\0') 3963 { 3964 register char *wd; 3965 char delim; 3966 3967 while (*p != '\0' && isascii(*p) && isspace(*p)) 3968 p++; 3969 wd = p; 3970 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 3971 p++; 3972 delim = *p; 3973 *p = '\0'; 3974 if (wd[0] != '\0') 3975 setclass(mid, wd); 3976 *p = delim; 3977 } 3978 break; 3979 3980 case '\0': 3981 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 3982 "Usage: .[DC]macro value(s)\n"); 3983 break; 3984 3985 default: 3986 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 3987 "Unknown \".\" command %s\n", line); 3988 break; 3989 } 3990 return; 3991 3992 case '=': /* config-style settings */ 3993 switch (line[1]) 3994 { 3995 case 'S': /* dump rule set */ 3996 rs = strtorwset(&line[2], NULL, ST_FIND); 3997 if (rs < 0) 3998 { 3999 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4000 "Undefined ruleset %s\n", &line[2]); 4001 return; 4002 } 4003 rw = RewriteRules[rs]; 4004 if (rw == NULL) 4005 return; 4006 do 4007 { 4008 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, 4009 'R'); 4010 s = rw->r_lhs; 4011 while (*s != NULL) 4012 { 4013 xputs(smioout, *s++); 4014 (void) sm_io_putc(smioout, 4015 SM_TIME_DEFAULT, ' '); 4016 } 4017 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, 4018 '\t'); 4019 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, 4020 '\t'); 4021 s = rw->r_rhs; 4022 while (*s != NULL) 4023 { 4024 xputs(smioout, *s++); 4025 (void) sm_io_putc(smioout, 4026 SM_TIME_DEFAULT, ' '); 4027 } 4028 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, 4029 '\n'); 4030 } while ((rw = rw->r_next) != NULL); 4031 break; 4032 4033 case 'M': 4034 for (i = 0; i < MAXMAILERS; i++) 4035 { 4036 if (Mailer[i] != NULL) 4037 printmailer(smioout, Mailer[i]); 4038 } 4039 break; 4040 4041 case '\0': 4042 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4043 "Usage: =Sruleset or =M\n"); 4044 break; 4045 4046 default: 4047 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4048 "Unknown \"=\" command %s\n", line); 4049 break; 4050 } 4051 return; 4052 4053 case '-': /* set command-line-like opts */ 4054 switch (line[1]) 4055 { 4056 case 'd': 4057 tTflag(&line[2]); 4058 break; 4059 4060 case '\0': 4061 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4062 "Usage: -d{debug arguments}\n"); 4063 break; 4064 4065 default: 4066 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4067 "Unknown \"-\" command %s\n", line); 4068 break; 4069 } 4070 return; 4071 4072 case '$': 4073 if (line[1] == '=') 4074 { 4075 mid = macid(&line[2]); 4076 if (mid != 0) 4077 stabapply(dump_class, mid); 4078 return; 4079 } 4080 mid = macid(&line[1]); 4081 if (mid == 0) 4082 return; 4083 p = macvalue(mid, e); 4084 if (p == NULL) 4085 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4086 "Undefined\n"); 4087 else 4088 { 4089 xputs(smioout, p); 4090 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4091 "\n"); 4092 } 4093 return; 4094 4095 case '/': /* miscellaneous commands */ 4096 p = &line[strlen(line)]; 4097 while (--p >= line && isascii(*p) && isspace(*p)) 4098 *p = '\0'; 4099 p = strpbrk(line, " \t"); 4100 if (p != NULL) 4101 { 4102 while (isascii(*p) && isspace(*p)) 4103 *p++ = '\0'; 4104 } 4105 else 4106 p = ""; 4107 if (line[1] == '\0') 4108 { 4109 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4110 "Usage: /[canon|map|mx|parse|try|tryflags]\n"); 4111 return; 4112 } 4113 if (sm_strcasecmp(&line[1], "quit") == 0) 4114 { 4115 CurEnv->e_id = NULL; 4116 finis(true, true, ExitStat); 4117 /* NOTREACHED */ 4118 } 4119 if (sm_strcasecmp(&line[1], "mx") == 0) 4120 { 4121 #if NAMED_BIND 4122 /* look up MX records */ 4123 int nmx; 4124 auto int rcode; 4125 char *mxhosts[MAXMXHOSTS + 1]; 4126 4127 if (*p == '\0') 4128 { 4129 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4130 "Usage: /mx address\n"); 4131 return; 4132 } 4133 nmx = getmxrr(p, mxhosts, NULL, false, &rcode, true, 4134 NULL); 4135 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4136 "getmxrr(%s) returns %d value(s):\n", 4137 p, nmx); 4138 for (i = 0; i < nmx; i++) 4139 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4140 "\t%s\n", mxhosts[i]); 4141 #else /* NAMED_BIND */ 4142 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4143 "No MX code compiled in\n"); 4144 #endif /* NAMED_BIND */ 4145 } 4146 else if (sm_strcasecmp(&line[1], "canon") == 0) 4147 { 4148 char host[MAXHOSTNAMELEN]; 4149 4150 if (*p == '\0') 4151 { 4152 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4153 "Usage: /canon address\n"); 4154 return; 4155 } 4156 else if (sm_strlcpy(host, p, sizeof host) >= sizeof host) 4157 { 4158 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4159 "Name too long\n"); 4160 return; 4161 } 4162 (void) getcanonname(host, sizeof host, !HasWildcardMX, 4163 NULL); 4164 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4165 "getcanonname(%s) returns %s\n", 4166 p, host); 4167 } 4168 else if (sm_strcasecmp(&line[1], "map") == 0) 4169 { 4170 auto int rcode = EX_OK; 4171 char *av[2]; 4172 4173 if (*p == '\0') 4174 { 4175 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4176 "Usage: /map mapname key\n"); 4177 return; 4178 } 4179 for (q = p; *q != '\0' && !(isascii(*q) && isspace(*q)); q++) 4180 continue; 4181 if (*q == '\0') 4182 { 4183 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4184 "No key specified\n"); 4185 return; 4186 } 4187 *q++ = '\0'; 4188 map = stab(p, ST_MAP, ST_FIND); 4189 if (map == NULL) 4190 { 4191 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4192 "Map named \"%s\" not found\n", p); 4193 return; 4194 } 4195 if (!bitset(MF_OPEN, map->s_map.map_mflags) && 4196 !openmap(&(map->s_map))) 4197 { 4198 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4199 "Map named \"%s\" not open\n", p); 4200 return; 4201 } 4202 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4203 "map_lookup: %s (%s) ", p, q); 4204 av[0] = q; 4205 av[1] = NULL; 4206 p = (*map->s_map.map_class->map_lookup) 4207 (&map->s_map, q, av, &rcode); 4208 if (p == NULL) 4209 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4210 "no match (%d)\n", 4211 rcode); 4212 else 4213 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4214 "returns %s (%d)\n", p, 4215 rcode); 4216 } 4217 else if (sm_strcasecmp(&line[1], "try") == 0) 4218 { 4219 MAILER *m; 4220 STAB *st; 4221 auto int rcode = EX_OK; 4222 4223 q = strpbrk(p, " \t"); 4224 if (q != NULL) 4225 { 4226 while (isascii(*q) && isspace(*q)) 4227 *q++ = '\0'; 4228 } 4229 if (q == NULL || *q == '\0') 4230 { 4231 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4232 "Usage: /try mailer address\n"); 4233 return; 4234 } 4235 st = stab(p, ST_MAILER, ST_FIND); 4236 if (st == NULL) 4237 { 4238 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4239 "Unknown mailer %s\n", p); 4240 return; 4241 } 4242 m = st->s_mailer; 4243 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4244 "Trying %s %s address %s for mailer %s\n", 4245 bitset(RF_HEADERADDR, tryflags) ? "header" 4246 : "envelope", 4247 bitset(RF_SENDERADDR, tryflags) ? "sender" 4248 : "recipient", q, p); 4249 p = remotename(q, m, tryflags, &rcode, CurEnv); 4250 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4251 "Rcode = %d, addr = %s\n", 4252 rcode, p == NULL ? "<NULL>" : p); 4253 e->e_to = NULL; 4254 } 4255 else if (sm_strcasecmp(&line[1], "tryflags") == 0) 4256 { 4257 if (*p == '\0') 4258 { 4259 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4260 "Usage: /tryflags [Hh|Ee][Ss|Rr]\n"); 4261 return; 4262 } 4263 for (; *p != '\0'; p++) 4264 { 4265 switch (*p) 4266 { 4267 case 'H': 4268 case 'h': 4269 tryflags |= RF_HEADERADDR; 4270 break; 4271 4272 case 'E': 4273 case 'e': 4274 tryflags &= ~RF_HEADERADDR; 4275 break; 4276 4277 case 'S': 4278 case 's': 4279 tryflags |= RF_SENDERADDR; 4280 break; 4281 4282 case 'R': 4283 case 'r': 4284 tryflags &= ~RF_SENDERADDR; 4285 break; 4286 } 4287 } 4288 exbuf[0] = bitset(RF_HEADERADDR, tryflags) ? 'h' : 'e'; 4289 exbuf[1] = ' '; 4290 exbuf[2] = bitset(RF_SENDERADDR, tryflags) ? 's' : 'r'; 4291 exbuf[3] = '\0'; 4292 macdefine(&e->e_macro, A_TEMP, 4293 macid("{addr_type}"), exbuf); 4294 } 4295 else if (sm_strcasecmp(&line[1], "parse") == 0) 4296 { 4297 if (*p == '\0') 4298 { 4299 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4300 "Usage: /parse address\n"); 4301 return; 4302 } 4303 q = crackaddr(p, e); 4304 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4305 "Cracked address = "); 4306 xputs(smioout, q); 4307 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4308 "\nParsing %s %s address\n", 4309 bitset(RF_HEADERADDR, tryflags) ? 4310 "header" : "envelope", 4311 bitset(RF_SENDERADDR, tryflags) ? 4312 "sender" : "recipient"); 4313 if (parseaddr(p, &a, tryflags, '\0', NULL, e, true) 4314 == NULL) 4315 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4316 "Cannot parse\n"); 4317 else if (a.q_host != NULL && a.q_host[0] != '\0') 4318 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4319 "mailer %s, host %s, user %s\n", 4320 a.q_mailer->m_name, 4321 a.q_host, 4322 a.q_user); 4323 else 4324 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4325 "mailer %s, user %s\n", 4326 a.q_mailer->m_name, 4327 a.q_user); 4328 e->e_to = NULL; 4329 } 4330 else 4331 { 4332 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4333 "Unknown \"/\" command %s\n", 4334 line); 4335 } 4336 return; 4337 } 4338 4339 for (p = line; isascii(*p) && isspace(*p); p++) 4340 continue; 4341 q = p; 4342 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 4343 p++; 4344 if (*p == '\0') 4345 { 4346 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4347 "No address!\n"); 4348 return; 4349 } 4350 *p = '\0'; 4351 if (invalidaddr(p + 1, NULL, true)) 4352 return; 4353 do 4354 { 4355 register char **pvp; 4356 char pvpbuf[PSBUFSIZE]; 4357 4358 pvp = prescan(++p, ',', pvpbuf, sizeof pvpbuf, &delimptr, 4359 ConfigLevel >= 9 ? TokTypeNoC : NULL, false); 4360 if (pvp == NULL) 4361 continue; 4362 p = q; 4363 while (*p != '\0') 4364 { 4365 int status; 4366 4367 rs = strtorwset(p, NULL, ST_FIND); 4368 if (rs < 0) 4369 { 4370 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4371 "Undefined ruleset %s\n", 4372 p); 4373 break; 4374 } 4375 status = REWRITE(pvp, rs, e); 4376 if (status != EX_OK) 4377 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4378 "== Ruleset %s (%d) status %d\n", 4379 p, rs, status); 4380 while (*p != '\0' && *p++ != ',') 4381 continue; 4382 } 4383 } while (*(p = delimptr) != '\0'); 4384 } 4385 4386 static void 4387 dump_class(s, id) 4388 register STAB *s; 4389 int id; 4390 { 4391 if (s->s_symtype != ST_CLASS) 4392 return; 4393 if (bitnset(bitidx(id), s->s_class)) 4394 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4395 "%s\n", s->s_name); 4396 } 4397 4398 /* 4399 ** An exception type used to create QuickAbort exceptions. 4400 ** This is my first cut at converting QuickAbort from longjmp to exceptions. 4401 ** These exceptions have a single integer argument, which is the argument 4402 ** to longjmp in the original code (either 1 or 2). I don't know the 4403 ** significance of 1 vs 2: the calls to setjmp don't care. 4404 */ 4405 4406 const SM_EXC_TYPE_T EtypeQuickAbort = 4407 { 4408 SmExcTypeMagic, 4409 "E:mta.quickabort", 4410 "i", 4411 sm_etype_printf, 4412 "quick abort %0", 4413 }; 4414