1 /* 2 * Copyright (c) 1998-2004 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-2004 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-2004 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.939 2004/06/17 16:39:21 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 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 setuserenv("TZ", TimeZoneSpec); 1331 else 1332 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 /* check for permissions */ 1475 if (RealUid != 0 && 1476 RealUid != TrustedUid) 1477 { 1478 char *action = NULL; 1479 1480 switch (OpMode) 1481 { 1482 case MD_QUEUERUN: 1483 if (quarantining != NULL) 1484 action = "quarantine jobs"; 1485 else 1486 { 1487 /* Normal users can do a single queue run */ 1488 if (QueueIntvl == 0) 1489 break; 1490 } 1491 1492 /* but not persistent queue runners */ 1493 if (action == NULL) 1494 action = "start a queue runner daemon"; 1495 /* FALLTHROUGH */ 1496 1497 case MD_PURGESTAT: 1498 if (action == NULL) 1499 action = "purge host status"; 1500 /* FALLTHROUGH */ 1501 1502 case MD_DAEMON: 1503 case MD_FGDAEMON: 1504 if (action == NULL) 1505 action = "run daemon"; 1506 1507 if (tTd(65, 1)) 1508 sm_dprintf("Deny user %d attempt to %s\n", 1509 (int) RealUid, action); 1510 1511 if (LogLevel > 1) 1512 sm_syslog(LOG_ALERT, NOQID, 1513 "user %d attempted to %s", 1514 (int) RealUid, action); 1515 HoldErrs = false; 1516 usrerr("Permission denied (real uid not trusted)"); 1517 finis(false, true, EX_USAGE); 1518 /* NOTREACHED */ 1519 break; 1520 1521 case MD_VERIFY: 1522 if (bitset(PRIV_RESTRICTEXPAND, PrivacyFlags)) 1523 { 1524 /* 1525 ** If -bv and RestrictExpand, 1526 ** drop privs to prevent normal 1527 ** users from reading private 1528 ** aliases/forwards/:include:s 1529 */ 1530 1531 if (tTd(65, 1)) 1532 sm_dprintf("Drop privs for user %d attempt to expand (RestrictExpand)\n", 1533 (int) RealUid); 1534 1535 dp = drop_privileges(true); 1536 1537 /* Fake address safety */ 1538 if (tTd(65, 1)) 1539 sm_dprintf("Faking DontBlameSendmail=NonRootSafeAddr\n"); 1540 setbitn(DBS_NONROOTSAFEADDR, DontBlameSendmail); 1541 1542 if (dp != EX_OK) 1543 { 1544 if (tTd(65, 1)) 1545 sm_dprintf("Failed to drop privs for user %d attempt to expand, exiting\n", 1546 (int) RealUid); 1547 CurEnv->e_id = NULL; 1548 finis(true, true, dp); 1549 /* NOTREACHED */ 1550 } 1551 } 1552 break; 1553 1554 case MD_TEST: 1555 case MD_PRINT: 1556 case MD_PRINTNQE: 1557 case MD_FREEZE: 1558 case MD_HOSTSTAT: 1559 /* Nothing special to check */ 1560 break; 1561 1562 case MD_INITALIAS: 1563 if (!wordinclass(RealUserName, 't')) 1564 { 1565 if (tTd(65, 1)) 1566 sm_dprintf("Deny user %d attempt to rebuild the alias map\n", 1567 (int) RealUid); 1568 if (LogLevel > 1) 1569 sm_syslog(LOG_ALERT, NOQID, 1570 "user %d attempted to rebuild the alias map", 1571 (int) RealUid); 1572 HoldErrs = false; 1573 usrerr("Permission denied (real uid not trusted)"); 1574 finis(false, true, EX_USAGE); 1575 /* NOTREACHED */ 1576 } 1577 if (UseMSP) 1578 { 1579 HoldErrs = false; 1580 usrerr("User %d cannot rebuild aliases in mail submission program", 1581 (int) RealUid); 1582 finis(false, true, EX_USAGE); 1583 /* NOTREACHED */ 1584 } 1585 /* FALLTHROUGH */ 1586 1587 default: 1588 if (bitset(PRIV_RESTRICTEXPAND, PrivacyFlags) && 1589 Verbose != 0) 1590 { 1591 /* 1592 ** If -v and RestrictExpand, reset 1593 ** Verbose to prevent normal users 1594 ** from seeing the expansion of 1595 ** aliases/forwards/:include:s 1596 */ 1597 1598 if (tTd(65, 1)) 1599 sm_dprintf("Dropping verbosity for user %d (RestrictExpand)\n", 1600 (int) RealUid); 1601 Verbose = 0; 1602 } 1603 break; 1604 } 1605 } 1606 1607 if (MeToo) 1608 BlankEnvelope.e_flags |= EF_METOO; 1609 1610 switch (OpMode) 1611 { 1612 case MD_TEST: 1613 /* don't have persistent host status in test mode */ 1614 HostStatDir = NULL; 1615 if (Verbose == 0) 1616 Verbose = 2; 1617 BlankEnvelope.e_errormode = EM_PRINT; 1618 HoldErrs = false; 1619 break; 1620 1621 case MD_VERIFY: 1622 BlankEnvelope.e_errormode = EM_PRINT; 1623 HoldErrs = false; 1624 /* arrange to exit cleanly on hangup signal */ 1625 if (sm_signal(SIGHUP, SIG_IGN) == (sigfunc_t) SIG_DFL) 1626 (void) sm_signal(SIGHUP, intsig); 1627 if (geteuid() != 0) 1628 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 1629 "Notice: -bv may give misleading output for non-privileged user\n"); 1630 break; 1631 1632 case MD_FGDAEMON: 1633 run_in_foreground = true; 1634 set_op_mode(MD_DAEMON); 1635 /* FALLTHROUGH */ 1636 1637 case MD_DAEMON: 1638 vendor_daemon_setup(&BlankEnvelope); 1639 1640 /* remove things that don't make sense in daemon mode */ 1641 FullName = NULL; 1642 GrabTo = false; 1643 1644 /* arrange to restart on hangup signal */ 1645 if (SaveArgv[0] == NULL || SaveArgv[0][0] != '/') 1646 sm_syslog(LOG_WARNING, NOQID, 1647 "daemon invoked without full pathname; kill -1 won't work"); 1648 break; 1649 1650 case MD_INITALIAS: 1651 Verbose = 2; 1652 BlankEnvelope.e_errormode = EM_PRINT; 1653 HoldErrs = false; 1654 /* FALLTHROUGH */ 1655 1656 default: 1657 /* arrange to exit cleanly on hangup signal */ 1658 if (sm_signal(SIGHUP, SIG_IGN) == (sigfunc_t) SIG_DFL) 1659 (void) sm_signal(SIGHUP, intsig); 1660 break; 1661 } 1662 1663 /* special considerations for FullName */ 1664 if (FullName != NULL) 1665 { 1666 char *full = NULL; 1667 1668 /* full names can't have newlines */ 1669 if (strchr(FullName, '\n') != NULL) 1670 { 1671 full = newstr(denlstring(FullName, true, true)); 1672 FullName = full; 1673 } 1674 1675 /* check for characters that may have to be quoted */ 1676 if (!rfc822_string(FullName)) 1677 { 1678 /* 1679 ** Quote a full name with special characters 1680 ** as a comment so crackaddr() doesn't destroy 1681 ** the name portion of the address. 1682 */ 1683 1684 FullName = addquotes(FullName, NULL); 1685 if (full != NULL) 1686 sm_free(full); /* XXX */ 1687 } 1688 } 1689 1690 /* do heuristic mode adjustment */ 1691 if (Verbose) 1692 { 1693 /* turn off noconnect option */ 1694 setoption('c', "F", true, false, &BlankEnvelope); 1695 1696 /* turn on interactive delivery */ 1697 setoption('d', "", true, false, &BlankEnvelope); 1698 } 1699 1700 #ifdef VENDOR_CODE 1701 /* check for vendor mismatch */ 1702 if (VendorCode != VENDOR_CODE) 1703 { 1704 message("Warning: .cf file vendor code mismatch: sendmail expects vendor %s, .cf file vendor is %s", 1705 getvendor(VENDOR_CODE), getvendor(VendorCode)); 1706 } 1707 #endif /* VENDOR_CODE */ 1708 1709 /* check for out of date configuration level */ 1710 if (ConfigLevel < MAXCONFIGLEVEL) 1711 { 1712 message("Warning: .cf file is out of date: sendmail %s supports version %d, .cf file is version %d", 1713 Version, MAXCONFIGLEVEL, ConfigLevel); 1714 } 1715 1716 if (ConfigLevel < 3) 1717 UseErrorsTo = true; 1718 1719 /* set options that were previous macros */ 1720 if (SmtpGreeting == NULL) 1721 { 1722 if (ConfigLevel < 7 && 1723 (p = macvalue('e', &BlankEnvelope)) != NULL) 1724 SmtpGreeting = newstr(p); 1725 else 1726 SmtpGreeting = "\201j Sendmail \201v ready at \201b"; 1727 } 1728 if (UnixFromLine == NULL) 1729 { 1730 if (ConfigLevel < 7 && 1731 (p = macvalue('l', &BlankEnvelope)) != NULL) 1732 UnixFromLine = newstr(p); 1733 else 1734 UnixFromLine = "From \201g \201d"; 1735 } 1736 SmtpError[0] = '\0'; 1737 1738 /* our name for SMTP codes */ 1739 expand("\201j", jbuf, sizeof jbuf, &BlankEnvelope); 1740 if (jbuf[0] == '\0') 1741 PSTRSET(MyHostName, "localhost"); 1742 else 1743 PSTRSET(MyHostName, jbuf); 1744 if (strchr(MyHostName, '.') == NULL) 1745 message("WARNING: local host name (%s) is not qualified; see cf/README: WHO AM I?", 1746 MyHostName); 1747 1748 /* make certain that this name is part of the $=w class */ 1749 setclass('w', MyHostName); 1750 1751 /* fill in the structure of the *default* queue */ 1752 st = stab("mqueue", ST_QUEUE, ST_FIND); 1753 if (st == NULL) 1754 syserr("No default queue (mqueue) defined"); 1755 else 1756 set_def_queueval(st->s_quegrp, true); 1757 1758 /* the indices of built-in mailers */ 1759 st = stab("local", ST_MAILER, ST_FIND); 1760 if (st != NULL) 1761 LocalMailer = st->s_mailer; 1762 else if (OpMode != MD_TEST || !warn_C_flag) 1763 syserr("No local mailer defined"); 1764 1765 st = stab("prog", ST_MAILER, ST_FIND); 1766 if (st == NULL) 1767 syserr("No prog mailer defined"); 1768 else 1769 { 1770 ProgMailer = st->s_mailer; 1771 clrbitn(M_MUSER, ProgMailer->m_flags); 1772 } 1773 1774 st = stab("*file*", ST_MAILER, ST_FIND); 1775 if (st == NULL) 1776 syserr("No *file* mailer defined"); 1777 else 1778 { 1779 FileMailer = st->s_mailer; 1780 clrbitn(M_MUSER, FileMailer->m_flags); 1781 } 1782 1783 st = stab("*include*", ST_MAILER, ST_FIND); 1784 if (st == NULL) 1785 syserr("No *include* mailer defined"); 1786 else 1787 InclMailer = st->s_mailer; 1788 1789 if (ConfigLevel < 6) 1790 { 1791 /* heuristic tweaking of local mailer for back compat */ 1792 if (LocalMailer != NULL) 1793 { 1794 setbitn(M_ALIASABLE, LocalMailer->m_flags); 1795 setbitn(M_HASPWENT, LocalMailer->m_flags); 1796 setbitn(M_TRYRULESET5, LocalMailer->m_flags); 1797 setbitn(M_CHECKINCLUDE, LocalMailer->m_flags); 1798 setbitn(M_CHECKPROG, LocalMailer->m_flags); 1799 setbitn(M_CHECKFILE, LocalMailer->m_flags); 1800 setbitn(M_CHECKUDB, LocalMailer->m_flags); 1801 } 1802 if (ProgMailer != NULL) 1803 setbitn(M_RUNASRCPT, ProgMailer->m_flags); 1804 if (FileMailer != NULL) 1805 setbitn(M_RUNASRCPT, FileMailer->m_flags); 1806 } 1807 if (ConfigLevel < 7) 1808 { 1809 if (LocalMailer != NULL) 1810 setbitn(M_VRFY250, LocalMailer->m_flags); 1811 if (ProgMailer != NULL) 1812 setbitn(M_VRFY250, ProgMailer->m_flags); 1813 if (FileMailer != NULL) 1814 setbitn(M_VRFY250, FileMailer->m_flags); 1815 } 1816 1817 /* MIME Content-Types that cannot be transfer encoded */ 1818 setclass('n', "multipart/signed"); 1819 1820 /* MIME message/xxx subtypes that can be treated as messages */ 1821 setclass('s', "rfc822"); 1822 1823 /* MIME Content-Transfer-Encodings that can be encoded */ 1824 setclass('e', "7bit"); 1825 setclass('e', "8bit"); 1826 setclass('e', "binary"); 1827 1828 #ifdef USE_B_CLASS 1829 /* MIME Content-Types that should be treated as binary */ 1830 setclass('b', "image"); 1831 setclass('b', "audio"); 1832 setclass('b', "video"); 1833 setclass('b', "application/octet-stream"); 1834 #endif /* USE_B_CLASS */ 1835 1836 /* MIME headers which have fields to check for overflow */ 1837 setclass(macid("{checkMIMEFieldHeaders}"), "content-disposition"); 1838 setclass(macid("{checkMIMEFieldHeaders}"), "content-type"); 1839 1840 /* MIME headers to check for length overflow */ 1841 setclass(macid("{checkMIMETextHeaders}"), "content-description"); 1842 1843 /* MIME headers to check for overflow and rebalance */ 1844 setclass(macid("{checkMIMEHeaders}"), "content-disposition"); 1845 setclass(macid("{checkMIMEHeaders}"), "content-id"); 1846 setclass(macid("{checkMIMEHeaders}"), "content-transfer-encoding"); 1847 setclass(macid("{checkMIMEHeaders}"), "content-type"); 1848 setclass(macid("{checkMIMEHeaders}"), "mime-version"); 1849 1850 /* Macros to save in the queue file -- don't remove any */ 1851 setclass(macid("{persistentMacros}"), "r"); 1852 setclass(macid("{persistentMacros}"), "s"); 1853 setclass(macid("{persistentMacros}"), "_"); 1854 setclass(macid("{persistentMacros}"), "{if_addr}"); 1855 setclass(macid("{persistentMacros}"), "{daemon_flags}"); 1856 1857 /* operate in queue directory */ 1858 if (QueueDir == NULL || *QueueDir == '\0') 1859 { 1860 if (OpMode != MD_TEST) 1861 { 1862 syserr("QueueDirectory (Q) option must be set"); 1863 ExitStat = EX_CONFIG; 1864 } 1865 } 1866 else 1867 { 1868 if (OpMode != MD_TEST) 1869 setup_queues(OpMode == MD_DAEMON); 1870 } 1871 1872 /* check host status directory for validity */ 1873 if (HostStatDir != NULL && !path_is_dir(HostStatDir, false)) 1874 { 1875 /* cannot use this value */ 1876 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 1877 "Warning: Cannot use HostStatusDirectory = %s: %s\n", 1878 HostStatDir, sm_errstring(errno)); 1879 HostStatDir = NULL; 1880 } 1881 1882 if (OpMode == MD_QUEUERUN && 1883 RealUid != 0 && bitset(PRIV_RESTRICTQRUN, PrivacyFlags)) 1884 { 1885 struct stat stbuf; 1886 1887 /* check to see if we own the queue directory */ 1888 if (stat(".", &stbuf) < 0) 1889 syserr("main: cannot stat %s", QueueDir); 1890 if (stbuf.st_uid != RealUid) 1891 { 1892 /* nope, really a botch */ 1893 HoldErrs = false; 1894 usrerr("You do not have permission to process the queue"); 1895 finis(false, true, EX_NOPERM); 1896 /* NOTREACHED */ 1897 } 1898 } 1899 1900 #if MILTER 1901 /* sanity checks on milter filters */ 1902 if (OpMode == MD_DAEMON || OpMode == MD_SMTP) 1903 { 1904 milter_config(InputFilterList, InputFilters, MAXFILTERS); 1905 setup_daemon_milters(); 1906 } 1907 #endif /* MILTER */ 1908 1909 /* Convert queuegroup string to qgrp number */ 1910 if (queuegroup != NULL) 1911 { 1912 qgrp = name2qid(queuegroup); 1913 if (qgrp == NOQGRP) 1914 { 1915 HoldErrs = false; 1916 usrerr("Queue group %s unknown", queuegroup); 1917 finis(false, true, ExitStat); 1918 /* NOTREACHED */ 1919 } 1920 } 1921 1922 /* if we've had errors so far, exit now */ 1923 if (ExitStat != EX_OK && OpMode != MD_TEST) 1924 { 1925 finis(false, true, ExitStat); 1926 /* NOTREACHED */ 1927 } 1928 1929 #if SASL 1930 /* sendmail specific SASL initialization */ 1931 sm_sasl_init(); 1932 #endif /* SASL */ 1933 1934 #if XDEBUG 1935 checkfd012("before main() initmaps"); 1936 #endif /* XDEBUG */ 1937 1938 /* 1939 ** Do operation-mode-dependent initialization. 1940 */ 1941 1942 switch (OpMode) 1943 { 1944 case MD_PRINT: 1945 /* print the queue */ 1946 HoldErrs = false; 1947 dropenvelope(&BlankEnvelope, true, false); 1948 (void) sm_signal(SIGPIPE, sigpipe); 1949 if (qgrp != NOQGRP) 1950 { 1951 int j; 1952 1953 /* Selecting a particular queue group to run */ 1954 for (j = 0; j < Queue[qgrp]->qg_numqueues; j++) 1955 { 1956 if (StopRequest) 1957 stop_sendmail(); 1958 (void) print_single_queue(qgrp, j); 1959 } 1960 finis(false, true, EX_OK); 1961 /* NOTREACHED */ 1962 } 1963 printqueue(); 1964 finis(false, true, EX_OK); 1965 /* NOTREACHED */ 1966 break; 1967 1968 case MD_PRINTNQE: 1969 /* print number of entries in queue */ 1970 dropenvelope(&BlankEnvelope, true, false); 1971 (void) sm_signal(SIGPIPE, sigpipe); 1972 printnqe(smioout, NULL); 1973 finis(false, true, EX_OK); 1974 /* NOTREACHED */ 1975 break; 1976 1977 case MD_QUEUERUN: 1978 /* only handle quarantining here */ 1979 if (quarantining == NULL) 1980 break; 1981 1982 if (QueueMode != QM_QUARANTINE && 1983 QueueMode != QM_NORMAL) 1984 { 1985 HoldErrs = false; 1986 usrerr("Can not use -Q with -q%c", QueueMode); 1987 ExitStat = EX_USAGE; 1988 finis(false, true, ExitStat); 1989 /* NOTREACHED */ 1990 } 1991 quarantine_queue(quarantining, qgrp); 1992 finis(false, true, EX_OK); 1993 break; 1994 1995 case MD_HOSTSTAT: 1996 (void) sm_signal(SIGPIPE, sigpipe); 1997 (void) mci_traverse_persistent(mci_print_persistent, NULL); 1998 finis(false, true, EX_OK); 1999 /* NOTREACHED */ 2000 break; 2001 2002 case MD_PURGESTAT: 2003 (void) mci_traverse_persistent(mci_purge_persistent, NULL); 2004 finis(false, true, EX_OK); 2005 /* NOTREACHED */ 2006 break; 2007 2008 case MD_INITALIAS: 2009 /* initialize maps */ 2010 initmaps(); 2011 finis(false, true, ExitStat); 2012 /* NOTREACHED */ 2013 break; 2014 2015 case MD_SMTP: 2016 case MD_DAEMON: 2017 /* reset DSN parameters */ 2018 DefaultNotify = QPINGONFAILURE|QPINGONDELAY; 2019 macdefine(&BlankEnvelope.e_macro, A_PERM, 2020 macid("{dsn_notify}"), NULL); 2021 BlankEnvelope.e_envid = NULL; 2022 macdefine(&BlankEnvelope.e_macro, A_PERM, 2023 macid("{dsn_envid}"), NULL); 2024 BlankEnvelope.e_flags &= ~(EF_RET_PARAM|EF_NO_BODY_RETN); 2025 macdefine(&BlankEnvelope.e_macro, A_PERM, 2026 macid("{dsn_ret}"), NULL); 2027 2028 /* don't open maps for daemon -- done below in child */ 2029 break; 2030 } 2031 2032 if (tTd(0, 15)) 2033 { 2034 /* print configuration table (or at least part of it) */ 2035 if (tTd(0, 90)) 2036 printrules(); 2037 for (i = 0; i < MAXMAILERS; i++) 2038 { 2039 if (Mailer[i] != NULL) 2040 printmailer(sm_debug_file(), Mailer[i]); 2041 } 2042 } 2043 2044 /* 2045 ** Switch to the main envelope. 2046 */ 2047 2048 CurEnv = newenvelope(&MainEnvelope, &BlankEnvelope, 2049 sm_rpool_new_x(NULL)); 2050 MainEnvelope.e_flags = BlankEnvelope.e_flags; 2051 2052 /* 2053 ** If test mode, read addresses from stdin and process. 2054 */ 2055 2056 if (OpMode == MD_TEST) 2057 { 2058 if (isatty(sm_io_getinfo(smioin, SM_IO_WHAT_FD, NULL))) 2059 Verbose = 2; 2060 2061 if (Verbose) 2062 { 2063 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 2064 "ADDRESS TEST MODE (ruleset 3 NOT automatically invoked)\n"); 2065 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 2066 "Enter <ruleset> <address>\n"); 2067 } 2068 macdefine(&(MainEnvelope.e_macro), A_PERM, 2069 macid("{addr_type}"), "e r"); 2070 for (;;) 2071 { 2072 SM_TRY 2073 { 2074 (void) sm_signal(SIGINT, intindebug); 2075 (void) sm_releasesignal(SIGINT); 2076 if (Verbose == 2) 2077 (void) sm_io_fprintf(smioout, 2078 SM_TIME_DEFAULT, 2079 "> "); 2080 (void) sm_io_flush(smioout, SM_TIME_DEFAULT); 2081 if (sm_io_fgets(smioin, SM_TIME_DEFAULT, buf, 2082 sizeof buf) == NULL) 2083 testmodeline("/quit", &MainEnvelope); 2084 p = strchr(buf, '\n'); 2085 if (p != NULL) 2086 *p = '\0'; 2087 if (Verbose < 2) 2088 (void) sm_io_fprintf(smioout, 2089 SM_TIME_DEFAULT, 2090 "> %s\n", buf); 2091 testmodeline(buf, &MainEnvelope); 2092 } 2093 SM_EXCEPT(exc, "[!F]*") 2094 { 2095 /* 2096 ** 8.10 just prints \n on interrupt. 2097 ** I'm printing the exception here in case 2098 ** sendmail is extended to raise additional 2099 ** exceptions in this context. 2100 */ 2101 2102 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 2103 "\n"); 2104 sm_exc_print(exc, smioout); 2105 } 2106 SM_END_TRY 2107 } 2108 } 2109 2110 #if STARTTLS 2111 tls_ok = true; 2112 if (OpMode == MD_QUEUERUN || OpMode == MD_DELIVER) 2113 { 2114 /* check whether STARTTLS is turned off for the client */ 2115 if (chkclientmodifiers(D_NOTLS)) 2116 tls_ok = false; 2117 } 2118 else if (OpMode == MD_DAEMON || OpMode == MD_FGDAEMON || 2119 OpMode == MD_SMTP) 2120 { 2121 /* check whether STARTTLS is turned off for the server */ 2122 if (chkdaemonmodifiers(D_NOTLS)) 2123 tls_ok = false; 2124 } 2125 else /* other modes don't need STARTTLS */ 2126 tls_ok = false; 2127 2128 if (tls_ok) 2129 { 2130 /* basic TLS initialization */ 2131 tls_ok = init_tls_library(); 2132 } 2133 2134 if (!tls_ok && (OpMode == MD_QUEUERUN || OpMode == MD_DELIVER)) 2135 { 2136 /* disable TLS for client */ 2137 setclttls(false); 2138 } 2139 #endif /* STARTTLS */ 2140 2141 /* 2142 ** If collecting stuff from the queue, go start doing that. 2143 */ 2144 2145 if (OpMode == MD_QUEUERUN && QueueIntvl == 0) 2146 { 2147 pid_t pid = -1; 2148 2149 #if STARTTLS 2150 /* init TLS for client, ignore result for now */ 2151 (void) initclttls(tls_ok); 2152 #endif /* STARTTLS */ 2153 2154 /* 2155 ** The parent process of the caller of runqueue() needs 2156 ** to stay around for a possible SIGTERM. The SIGTERM will 2157 ** tell this process that all of the queue runners children 2158 ** need to be sent SIGTERM as well. At the same time, we 2159 ** want to return control to the command line. So we do an 2160 ** extra fork(). 2161 */ 2162 2163 if (Verbose || foregroundqueue || (pid = fork()) <= 0) 2164 { 2165 /* 2166 ** If the fork() failed we should still try to do 2167 ** the queue run. If it succeeded then the child 2168 ** is going to start the run and wait for all 2169 ** of the children to finish. 2170 */ 2171 2172 if (pid == 0) 2173 { 2174 /* Reset global flags */ 2175 RestartRequest = NULL; 2176 ShutdownRequest = NULL; 2177 PendingSignal = 0; 2178 2179 /* disconnect from terminal */ 2180 disconnect(2, CurEnv); 2181 } 2182 2183 CurrentPid = getpid(); 2184 if (qgrp != NOQGRP) 2185 { 2186 int rwgflags = RWG_NONE; 2187 2188 /* 2189 ** To run a specific queue group mark it to 2190 ** be run, select the work group it's in and 2191 ** increment the work counter. 2192 */ 2193 2194 for (i = 0; i < NumQueue && Queue[i] != NULL; 2195 i++) 2196 Queue[i]->qg_nextrun = (time_t) -1; 2197 Queue[qgrp]->qg_nextrun = 0; 2198 if (Verbose) 2199 rwgflags |= RWG_VERBOSE; 2200 if (queuepersistent) 2201 rwgflags |= RWG_PERSISTENT; 2202 rwgflags |= RWG_FORCE; 2203 (void) run_work_group(Queue[qgrp]->qg_wgrp, 2204 rwgflags); 2205 } 2206 else 2207 (void) runqueue(false, Verbose, 2208 queuepersistent, true); 2209 2210 /* set the title to make it easier to find */ 2211 sm_setproctitle(true, CurEnv, "Queue control"); 2212 (void) sm_signal(SIGCHLD, SIG_DFL); 2213 while (CurChildren > 0) 2214 { 2215 int status; 2216 pid_t ret; 2217 2218 errno = 0; 2219 while ((ret = sm_wait(&status)) <= 0) 2220 { 2221 if (errno == ECHILD) 2222 { 2223 /* 2224 ** Oops... something got messed 2225 ** up really bad. Waiting for 2226 ** non-existent children 2227 ** shouldn't happen. Let's get 2228 ** out of here. 2229 */ 2230 2231 CurChildren = 0; 2232 break; 2233 } 2234 continue; 2235 } 2236 2237 /* something is really really wrong */ 2238 if (errno == ECHILD) 2239 { 2240 sm_syslog(LOG_ERR, NOQID, 2241 "queue control process: lost all children: wait returned ECHILD"); 2242 break; 2243 } 2244 2245 /* Only drop when a child gives status */ 2246 if (WIFSTOPPED(status)) 2247 continue; 2248 2249 proc_list_drop(ret, status, NULL); 2250 } 2251 } 2252 finis(true, true, ExitStat); 2253 /* NOTREACHED */ 2254 } 2255 2256 # if SASL 2257 if (OpMode == MD_SMTP || OpMode == MD_DAEMON) 2258 { 2259 /* check whether AUTH is turned off for the server */ 2260 if (!chkdaemonmodifiers(D_NOAUTH) && 2261 (i = sasl_server_init(srvcallbacks, "Sendmail")) != SASL_OK) 2262 syserr("!sasl_server_init failed! [%s]", 2263 sasl_errstring(i, NULL, NULL)); 2264 } 2265 # endif /* SASL */ 2266 2267 if (OpMode == MD_SMTP) 2268 { 2269 proc_list_add(CurrentPid, "Sendmail SMTP Agent", 2270 PROC_DAEMON, 0, -1, NULL); 2271 2272 /* clean up background delivery children */ 2273 (void) sm_signal(SIGCHLD, reapchild); 2274 } 2275 2276 /* 2277 ** If a daemon, wait for a request. 2278 ** getrequests will always return in a child. 2279 ** If we should also be processing the queue, start 2280 ** doing it in background. 2281 ** We check for any errors that might have happened 2282 ** during startup. 2283 */ 2284 2285 if (OpMode == MD_DAEMON || QueueIntvl > 0) 2286 { 2287 char dtype[200]; 2288 2289 if (!run_in_foreground && !tTd(99, 100)) 2290 { 2291 /* put us in background */ 2292 i = fork(); 2293 if (i < 0) 2294 syserr("daemon: cannot fork"); 2295 if (i != 0) 2296 { 2297 finis(false, true, EX_OK); 2298 /* NOTREACHED */ 2299 } 2300 2301 /* 2302 ** Initialize exception stack and default exception 2303 ** handler for child process. 2304 */ 2305 2306 /* Reset global flags */ 2307 RestartRequest = NULL; 2308 RestartWorkGroup = false; 2309 ShutdownRequest = NULL; 2310 PendingSignal = 0; 2311 CurrentPid = getpid(); 2312 2313 sm_exc_newthread(fatal_error); 2314 2315 /* disconnect from our controlling tty */ 2316 disconnect(2, &MainEnvelope); 2317 } 2318 2319 dtype[0] = '\0'; 2320 if (OpMode == MD_DAEMON) 2321 { 2322 (void) sm_strlcat(dtype, "+SMTP", sizeof dtype); 2323 DaemonPid = CurrentPid; 2324 } 2325 if (QueueIntvl > 0) 2326 { 2327 (void) sm_strlcat2(dtype, 2328 queuepersistent 2329 ? "+persistent-queueing@" 2330 : "+queueing@", 2331 pintvl(QueueIntvl, true), 2332 sizeof dtype); 2333 } 2334 if (tTd(0, 1)) 2335 (void) sm_strlcat(dtype, "+debugging", sizeof dtype); 2336 2337 sm_syslog(LOG_INFO, NOQID, 2338 "starting daemon (%s): %s", Version, dtype + 1); 2339 #if XLA 2340 xla_create_file(); 2341 #endif /* XLA */ 2342 2343 /* save daemon type in a macro for possible PidFile use */ 2344 macdefine(&BlankEnvelope.e_macro, A_TEMP, 2345 macid("{daemon_info}"), dtype + 1); 2346 2347 /* save queue interval in a macro for possible PidFile use */ 2348 macdefine(&MainEnvelope.e_macro, A_TEMP, 2349 macid("{queue_interval}"), pintvl(QueueIntvl, true)); 2350 2351 /* workaround: can't seem to release the signal in the parent */ 2352 (void) sm_signal(SIGHUP, sighup); 2353 (void) sm_releasesignal(SIGHUP); 2354 (void) sm_signal(SIGTERM, sigterm); 2355 2356 if (QueueIntvl > 0) 2357 { 2358 (void) runqueue(true, false, queuepersistent, true); 2359 2360 /* 2361 ** If queuepersistent but not in daemon mode then 2362 ** we're going to do the queue runner monitoring here. 2363 ** If in daemon mode then the monitoring will happen 2364 ** elsewhere. 2365 */ 2366 2367 if (OpMode != MD_DAEMON && queuepersistent) 2368 { 2369 /* 2370 ** Write the pid to file 2371 ** XXX Overwrites sendmail.pid 2372 */ 2373 2374 log_sendmail_pid(&MainEnvelope); 2375 2376 /* set the title to make it easier to find */ 2377 sm_setproctitle(true, CurEnv, "Queue control"); 2378 (void) sm_signal(SIGCHLD, SIG_DFL); 2379 while (CurChildren > 0) 2380 { 2381 int status; 2382 pid_t ret; 2383 int group; 2384 2385 CHECK_RESTART; 2386 errno = 0; 2387 while ((ret = sm_wait(&status)) <= 0) 2388 { 2389 /* 2390 ** Waiting for non-existent 2391 ** children shouldn't happen. 2392 ** Let's get out of here if 2393 ** it occurs. 2394 */ 2395 2396 if (errno == ECHILD) 2397 { 2398 CurChildren = 0; 2399 break; 2400 } 2401 continue; 2402 } 2403 2404 /* something is really really wrong */ 2405 if (errno == ECHILD) 2406 { 2407 sm_syslog(LOG_ERR, NOQID, 2408 "persistent queue runner control process: lost all children: wait returned ECHILD"); 2409 break; 2410 } 2411 2412 if (WIFSTOPPED(status)) 2413 continue; 2414 2415 /* Probe only on a child status */ 2416 proc_list_drop(ret, status, &group); 2417 2418 if (WIFSIGNALED(status)) 2419 { 2420 if (WCOREDUMP(status)) 2421 { 2422 sm_syslog(LOG_ERR, NOQID, 2423 "persistent queue runner=%d core dumped, signal=%d", 2424 group, WTERMSIG(status)); 2425 2426 /* don't restart this */ 2427 mark_work_group_restart( 2428 group, -1); 2429 continue; 2430 } 2431 2432 sm_syslog(LOG_ERR, NOQID, 2433 "persistent queue runner=%d died, signal=%d", 2434 group, WTERMSIG(status)); 2435 } 2436 2437 /* 2438 ** When debugging active, don't 2439 ** restart the persistent queues. 2440 ** But do log this as info. 2441 */ 2442 2443 if (sm_debug_active(&DebugNoPRestart, 2444 1)) 2445 { 2446 sm_syslog(LOG_DEBUG, NOQID, 2447 "persistent queue runner=%d, exited", 2448 group); 2449 mark_work_group_restart(group, 2450 -1); 2451 } 2452 } 2453 finis(true, true, ExitStat); 2454 /* NOTREACHED */ 2455 } 2456 2457 if (OpMode != MD_DAEMON) 2458 { 2459 char qtype[200]; 2460 2461 /* 2462 ** Write the pid to file 2463 ** XXX Overwrites sendmail.pid 2464 */ 2465 2466 log_sendmail_pid(&MainEnvelope); 2467 2468 /* set the title to make it easier to find */ 2469 qtype[0] = '\0'; 2470 (void) sm_strlcpyn(qtype, sizeof qtype, 4, 2471 "Queue runner@", 2472 pintvl(QueueIntvl, true), 2473 " for ", 2474 QueueDir); 2475 sm_setproctitle(true, CurEnv, qtype); 2476 for (;;) 2477 { 2478 (void) pause(); 2479 2480 CHECK_RESTART; 2481 2482 if (doqueuerun()) 2483 (void) runqueue(true, false, 2484 false, false); 2485 } 2486 } 2487 } 2488 dropenvelope(&MainEnvelope, true, false); 2489 2490 #if STARTTLS 2491 /* init TLS for server, ignore result for now */ 2492 (void) initsrvtls(tls_ok); 2493 #endif /* STARTTLS */ 2494 2495 nextreq: 2496 p_flags = getrequests(&MainEnvelope); 2497 2498 /* drop privileges */ 2499 (void) drop_privileges(false); 2500 2501 /* 2502 ** Get authentication data 2503 ** Set _ macro in BlankEnvelope before calling newenvelope(). 2504 */ 2505 2506 authinfo = getauthinfo(sm_io_getinfo(InChannel, SM_IO_WHAT_FD, 2507 NULL), &forged); 2508 macdefine(&BlankEnvelope.e_macro, A_TEMP, '_', authinfo); 2509 2510 /* at this point we are in a child: reset state */ 2511 sm_rpool_free(MainEnvelope.e_rpool); 2512 (void) newenvelope(&MainEnvelope, &MainEnvelope, 2513 sm_rpool_new_x(NULL)); 2514 } 2515 2516 if (LogLevel > 9) 2517 { 2518 /* log connection information */ 2519 sm_syslog(LOG_INFO, NULL, "connect from %s", authinfo); 2520 } 2521 2522 /* 2523 ** If running SMTP protocol, start collecting and executing 2524 ** commands. This will never return. 2525 */ 2526 2527 if (OpMode == MD_SMTP || OpMode == MD_DAEMON) 2528 { 2529 char pbuf[20]; 2530 2531 /* 2532 ** Save some macros for check_* rulesets. 2533 */ 2534 2535 if (forged) 2536 { 2537 char ipbuf[103]; 2538 2539 (void) sm_snprintf(ipbuf, sizeof ipbuf, "[%.100s]", 2540 anynet_ntoa(&RealHostAddr)); 2541 macdefine(&BlankEnvelope.e_macro, A_TEMP, 2542 macid("{client_name}"), ipbuf); 2543 } 2544 else 2545 macdefine(&BlankEnvelope.e_macro, A_PERM, 2546 macid("{client_name}"), RealHostName); 2547 macdefine(&BlankEnvelope.e_macro, A_PERM, 2548 macid("{client_ptr}"), RealHostName); 2549 macdefine(&BlankEnvelope.e_macro, A_TEMP, 2550 macid("{client_addr}"), anynet_ntoa(&RealHostAddr)); 2551 sm_getla(); 2552 2553 switch (RealHostAddr.sa.sa_family) 2554 { 2555 #if NETINET 2556 case AF_INET: 2557 (void) sm_snprintf(pbuf, sizeof pbuf, "%d", 2558 RealHostAddr.sin.sin_port); 2559 break; 2560 #endif /* NETINET */ 2561 #if NETINET6 2562 case AF_INET6: 2563 (void) sm_snprintf(pbuf, sizeof pbuf, "%d", 2564 RealHostAddr.sin6.sin6_port); 2565 break; 2566 #endif /* NETINET6 */ 2567 default: 2568 (void) sm_snprintf(pbuf, sizeof pbuf, "0"); 2569 break; 2570 } 2571 macdefine(&BlankEnvelope.e_macro, A_TEMP, 2572 macid("{client_port}"), pbuf); 2573 2574 if (OpMode == MD_DAEMON) 2575 { 2576 /* validate the connection */ 2577 HoldErrs = true; 2578 nullserver = validate_connection(&RealHostAddr, 2579 macvalue(macid("{client_name}"), 2580 &MainEnvelope), 2581 &MainEnvelope); 2582 HoldErrs = false; 2583 } 2584 else if (p_flags == NULL) 2585 { 2586 p_flags = (BITMAP256 *) xalloc(sizeof *p_flags); 2587 clrbitmap(p_flags); 2588 } 2589 #if STARTTLS 2590 if (OpMode == MD_SMTP) 2591 (void) initsrvtls(tls_ok); 2592 #endif /* STARTTLS */ 2593 2594 /* turn off profiling */ 2595 SM_PROF(1); 2596 smtp(nullserver, *p_flags, &MainEnvelope); 2597 2598 if (tTd(93, 100)) 2599 { 2600 /* turn off profiling */ 2601 SM_PROF(0); 2602 if (OpMode == MD_DAEMON) 2603 goto nextreq; 2604 } 2605 } 2606 2607 sm_rpool_free(MainEnvelope.e_rpool); 2608 clearenvelope(&MainEnvelope, false, sm_rpool_new_x(NULL)); 2609 if (OpMode == MD_VERIFY) 2610 { 2611 set_delivery_mode(SM_VERIFY, &MainEnvelope); 2612 PostMasterCopy = NULL; 2613 } 2614 else 2615 { 2616 /* interactive -- all errors are global */ 2617 MainEnvelope.e_flags |= EF_GLOBALERRS|EF_LOGSENDER; 2618 } 2619 2620 /* 2621 ** Do basic system initialization and set the sender 2622 */ 2623 2624 initsys(&MainEnvelope); 2625 macdefine(&MainEnvelope.e_macro, A_PERM, macid("{ntries}"), "0"); 2626 macdefine(&MainEnvelope.e_macro, A_PERM, macid("{nrcpts}"), "0"); 2627 setsender(from, &MainEnvelope, NULL, '\0', false); 2628 if (warn_f_flag != '\0' && !wordinclass(RealUserName, 't') && 2629 (!bitnset(M_LOCALMAILER, MainEnvelope.e_from.q_mailer->m_flags) || 2630 strcmp(MainEnvelope.e_from.q_user, RealUserName) != 0)) 2631 { 2632 auth_warning(&MainEnvelope, "%s set sender to %s using -%c", 2633 RealUserName, from, warn_f_flag); 2634 #if SASL 2635 auth = false; 2636 #endif /* SASL */ 2637 } 2638 if (auth) 2639 { 2640 char *fv; 2641 2642 /* set the initial sender for AUTH= to $f@$j */ 2643 fv = macvalue('f', &MainEnvelope); 2644 if (fv == NULL || *fv == '\0') 2645 MainEnvelope.e_auth_param = NULL; 2646 else 2647 { 2648 if (strchr(fv, '@') == NULL) 2649 { 2650 i = strlen(fv) + strlen(macvalue('j', 2651 &MainEnvelope)) + 2; 2652 p = sm_malloc_x(i); 2653 (void) sm_strlcpyn(p, i, 3, fv, "@", 2654 macvalue('j', 2655 &MainEnvelope)); 2656 } 2657 else 2658 p = sm_strdup_x(fv); 2659 MainEnvelope.e_auth_param = sm_rpool_strdup_x(MainEnvelope.e_rpool, 2660 xtextify(p, "=")); 2661 sm_free(p); /* XXX */ 2662 } 2663 } 2664 if (macvalue('s', &MainEnvelope) == NULL) 2665 macdefine(&MainEnvelope.e_macro, A_PERM, 's', RealHostName); 2666 2667 av = argv + optind; 2668 if (*av == NULL && !GrabTo) 2669 { 2670 MainEnvelope.e_to = NULL; 2671 MainEnvelope.e_flags |= EF_GLOBALERRS; 2672 HoldErrs = false; 2673 SuperSafe = SAFE_NO; 2674 usrerr("Recipient names must be specified"); 2675 2676 /* collect body for UUCP return */ 2677 if (OpMode != MD_VERIFY) 2678 collect(InChannel, false, NULL, &MainEnvelope, true); 2679 finis(true, true, EX_USAGE); 2680 /* NOTREACHED */ 2681 } 2682 2683 /* 2684 ** Scan argv and deliver the message to everyone. 2685 */ 2686 2687 save_val = LogUsrErrs; 2688 LogUsrErrs = true; 2689 sendtoargv(av, &MainEnvelope); 2690 LogUsrErrs = save_val; 2691 2692 /* if we have had errors sofar, arrange a meaningful exit stat */ 2693 if (Errors > 0 && ExitStat == EX_OK) 2694 ExitStat = EX_USAGE; 2695 2696 #if _FFR_FIX_DASHT 2697 /* 2698 ** If using -t, force not sending to argv recipients, even 2699 ** if they are mentioned in the headers. 2700 */ 2701 2702 if (GrabTo) 2703 { 2704 ADDRESS *q; 2705 2706 for (q = MainEnvelope.e_sendqueue; q != NULL; q = q->q_next) 2707 q->q_state = QS_REMOVED; 2708 } 2709 #endif /* _FFR_FIX_DASHT */ 2710 2711 /* 2712 ** Read the input mail. 2713 */ 2714 2715 MainEnvelope.e_to = NULL; 2716 if (OpMode != MD_VERIFY || GrabTo) 2717 { 2718 int savederrors; 2719 unsigned long savedflags; 2720 2721 /* 2722 ** workaround for compiler warning on Irix: 2723 ** do not initialize variable in the definition, but 2724 ** later on: 2725 ** warning(1548): transfer of control bypasses 2726 ** initialization of: 2727 ** variable "savederrors" (declared at line 2570) 2728 ** variable "savedflags" (declared at line 2571) 2729 ** goto giveup; 2730 */ 2731 2732 savederrors = Errors; 2733 savedflags = MainEnvelope.e_flags & EF_FATALERRS; 2734 MainEnvelope.e_flags |= EF_GLOBALERRS; 2735 MainEnvelope.e_flags &= ~EF_FATALERRS; 2736 Errors = 0; 2737 buffer_errors(); 2738 collect(InChannel, false, NULL, &MainEnvelope, true); 2739 2740 /* header checks failed */ 2741 if (Errors > 0) 2742 { 2743 giveup: 2744 if (!GrabTo) 2745 { 2746 /* Log who the mail would have gone to */ 2747 logundelrcpts(&MainEnvelope, 2748 MainEnvelope.e_message, 2749 8, false); 2750 } 2751 flush_errors(true); 2752 finis(true, true, ExitStat); 2753 /* NOTREACHED */ 2754 return -1; 2755 } 2756 2757 /* bail out if message too large */ 2758 if (bitset(EF_CLRQUEUE, MainEnvelope.e_flags)) 2759 { 2760 finis(true, true, ExitStat != EX_OK ? ExitStat 2761 : EX_DATAERR); 2762 /* NOTREACHED */ 2763 return -1; 2764 } 2765 2766 /* set message size */ 2767 (void) sm_snprintf(buf, sizeof buf, "%ld", 2768 MainEnvelope.e_msgsize); 2769 macdefine(&MainEnvelope.e_macro, A_TEMP, 2770 macid("{msg_size}"), buf); 2771 2772 Errors = savederrors; 2773 MainEnvelope.e_flags |= savedflags; 2774 } 2775 errno = 0; 2776 2777 if (tTd(1, 1)) 2778 sm_dprintf("From person = \"%s\"\n", 2779 MainEnvelope.e_from.q_paddr); 2780 2781 /* Check if quarantining stats should be updated */ 2782 if (MainEnvelope.e_quarmsg != NULL) 2783 markstats(&MainEnvelope, NULL, STATS_QUARANTINE); 2784 2785 /* 2786 ** Actually send everything. 2787 ** If verifying, just ack. 2788 */ 2789 2790 if (Errors == 0) 2791 { 2792 if (!split_by_recipient(&MainEnvelope) && 2793 bitset(EF_FATALERRS, MainEnvelope.e_flags)) 2794 goto giveup; 2795 } 2796 2797 /* make sure we deliver at least the first envelope */ 2798 i = FastSplit > 0 ? 0 : -1; 2799 for (e = &MainEnvelope; e != NULL; e = e->e_sibling, i++) 2800 { 2801 ENVELOPE *next; 2802 2803 e->e_from.q_state = QS_SENDER; 2804 if (tTd(1, 5)) 2805 { 2806 sm_dprintf("main[%d]: QS_SENDER ", i); 2807 printaddr(sm_debug_file(), &e->e_from, false); 2808 } 2809 e->e_to = NULL; 2810 sm_getla(); 2811 GrabTo = false; 2812 #if NAMED_BIND 2813 _res.retry = TimeOuts.res_retry[RES_TO_FIRST]; 2814 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST]; 2815 #endif /* NAMED_BIND */ 2816 next = e->e_sibling; 2817 e->e_sibling = NULL; 2818 2819 /* after FastSplit envelopes: queue up */ 2820 sendall(e, i >= FastSplit ? SM_QUEUE : SM_DEFAULT); 2821 e->e_sibling = next; 2822 } 2823 2824 /* 2825 ** All done. 2826 ** Don't send return error message if in VERIFY mode. 2827 */ 2828 2829 finis(true, true, ExitStat); 2830 /* NOTREACHED */ 2831 return ExitStat; 2832 } 2833 /* 2834 ** STOP_SENDMAIL -- Stop the running program 2835 ** 2836 ** Parameters: 2837 ** none. 2838 ** 2839 ** Returns: 2840 ** none. 2841 ** 2842 ** Side Effects: 2843 ** exits. 2844 */ 2845 2846 void 2847 stop_sendmail() 2848 { 2849 /* reset uid for process accounting */ 2850 endpwent(); 2851 (void) setuid(RealUid); 2852 exit(EX_OK); 2853 } 2854 /* 2855 ** FINIS -- Clean up and exit. 2856 ** 2857 ** Parameters: 2858 ** drop -- whether or not to drop CurEnv envelope 2859 ** cleanup -- call exit() or _exit()? 2860 ** exitstat -- exit status to use for exit() call 2861 ** 2862 ** Returns: 2863 ** never 2864 ** 2865 ** Side Effects: 2866 ** exits sendmail 2867 */ 2868 2869 void 2870 finis(drop, cleanup, exitstat) 2871 bool drop; 2872 bool cleanup; 2873 volatile int exitstat; 2874 { 2875 char pidpath[MAXPATHLEN]; 2876 2877 /* Still want to process new timeouts added below */ 2878 sm_clear_events(); 2879 (void) sm_releasesignal(SIGALRM); 2880 2881 if (tTd(2, 1)) 2882 { 2883 sm_dprintf("\n====finis: stat %d e_id=%s e_flags=", 2884 exitstat, 2885 CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id); 2886 printenvflags(CurEnv); 2887 } 2888 if (tTd(2, 9)) 2889 printopenfds(false); 2890 2891 SM_TRY 2892 /* 2893 ** Clean up. This might raise E:mta.quickabort 2894 */ 2895 2896 /* clean up temp files */ 2897 CurEnv->e_to = NULL; 2898 if (drop) 2899 { 2900 if (CurEnv->e_id != NULL) 2901 { 2902 dropenvelope(CurEnv, true, false); 2903 sm_rpool_free(CurEnv->e_rpool); 2904 CurEnv->e_rpool = NULL; 2905 } 2906 else 2907 poststats(StatFile); 2908 } 2909 2910 /* flush any cached connections */ 2911 mci_flush(true, NULL); 2912 2913 /* close maps belonging to this pid */ 2914 closemaps(false); 2915 2916 #if USERDB 2917 /* close UserDatabase */ 2918 _udbx_close(); 2919 #endif /* USERDB */ 2920 2921 #if SASL 2922 stop_sasl_client(); 2923 #endif /* SASL */ 2924 2925 #if XLA 2926 /* clean up extended load average stuff */ 2927 xla_all_end(); 2928 #endif /* XLA */ 2929 2930 SM_FINALLY 2931 /* 2932 ** And exit. 2933 */ 2934 2935 if (LogLevel > 78) 2936 sm_syslog(LOG_DEBUG, CurEnv->e_id, "finis, pid=%d", 2937 (int) CurrentPid); 2938 if (exitstat == EX_TEMPFAIL || 2939 CurEnv->e_errormode == EM_BERKNET) 2940 exitstat = EX_OK; 2941 2942 /* XXX clean up queues and related data structures */ 2943 cleanup_queues(); 2944 #if SM_CONF_SHM 2945 cleanup_shm(DaemonPid == getpid()); 2946 #endif /* SM_CONF_SHM */ 2947 2948 /* close locked pid file */ 2949 close_sendmail_pid(); 2950 2951 if (DaemonPid == getpid() || PidFilePid == getpid()) 2952 { 2953 /* blow away the pid file */ 2954 expand(PidFile, pidpath, sizeof pidpath, CurEnv); 2955 (void) unlink(pidpath); 2956 } 2957 2958 /* reset uid for process accounting */ 2959 endpwent(); 2960 sm_mbdb_terminate(); 2961 (void) setuid(RealUid); 2962 #if SM_HEAP_CHECK 2963 /* dump the heap, if we are checking for memory leaks */ 2964 if (sm_debug_active(&SmHeapCheck, 2)) 2965 sm_heap_report(smioout, 2966 sm_debug_level(&SmHeapCheck) - 1); 2967 #endif /* SM_HEAP_CHECK */ 2968 if (sm_debug_active(&SmXtrapReport, 1)) 2969 sm_dprintf("xtrap count = %d\n", SmXtrapCount); 2970 if (cleanup) 2971 exit(exitstat); 2972 else 2973 _exit(exitstat); 2974 SM_END_TRY 2975 } 2976 /* 2977 ** INTINDEBUG -- signal handler for SIGINT in -bt mode 2978 ** 2979 ** Parameters: 2980 ** sig -- incoming signal. 2981 ** 2982 ** Returns: 2983 ** none. 2984 ** 2985 ** Side Effects: 2986 ** longjmps back to test mode loop. 2987 ** 2988 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 2989 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 2990 ** DOING. 2991 */ 2992 2993 /* Type of an exception generated on SIGINT during address test mode. */ 2994 static const SM_EXC_TYPE_T EtypeInterrupt = 2995 { 2996 SmExcTypeMagic, 2997 "S:mta.interrupt", 2998 "", 2999 sm_etype_printf, 3000 "interrupt", 3001 }; 3002 3003 /* ARGSUSED */ 3004 static SIGFUNC_DECL 3005 intindebug(sig) 3006 int sig; 3007 { 3008 int save_errno = errno; 3009 3010 FIX_SYSV_SIGNAL(sig, intindebug); 3011 errno = save_errno; 3012 CHECK_CRITICAL(sig); 3013 errno = save_errno; 3014 sm_exc_raisenew_x(&EtypeInterrupt); 3015 errno = save_errno; 3016 return SIGFUNC_RETURN; 3017 } 3018 /* 3019 ** SIGTERM -- SIGTERM handler for the daemon 3020 ** 3021 ** Parameters: 3022 ** sig -- signal number. 3023 ** 3024 ** Returns: 3025 ** none. 3026 ** 3027 ** Side Effects: 3028 ** Sets ShutdownRequest which will hopefully trigger 3029 ** the daemon to exit. 3030 ** 3031 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3032 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3033 ** DOING. 3034 */ 3035 3036 /* ARGSUSED */ 3037 static SIGFUNC_DECL 3038 sigterm(sig) 3039 int sig; 3040 { 3041 int save_errno = errno; 3042 3043 FIX_SYSV_SIGNAL(sig, sigterm); 3044 ShutdownRequest = "signal"; 3045 errno = save_errno; 3046 return SIGFUNC_RETURN; 3047 } 3048 /* 3049 ** SIGHUP -- handle a SIGHUP signal 3050 ** 3051 ** Parameters: 3052 ** sig -- incoming signal. 3053 ** 3054 ** Returns: 3055 ** none. 3056 ** 3057 ** Side Effects: 3058 ** Sets RestartRequest which should cause the daemon 3059 ** to restart. 3060 ** 3061 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3062 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3063 ** DOING. 3064 */ 3065 3066 /* ARGSUSED */ 3067 static SIGFUNC_DECL 3068 sighup(sig) 3069 int sig; 3070 { 3071 int save_errno = errno; 3072 3073 FIX_SYSV_SIGNAL(sig, sighup); 3074 RestartRequest = "signal"; 3075 errno = save_errno; 3076 return SIGFUNC_RETURN; 3077 } 3078 /* 3079 ** SIGPIPE -- signal handler for SIGPIPE 3080 ** 3081 ** Parameters: 3082 ** sig -- incoming signal. 3083 ** 3084 ** Returns: 3085 ** none. 3086 ** 3087 ** Side Effects: 3088 ** Sets StopRequest which should cause the mailq/hoststatus 3089 ** display to stop. 3090 ** 3091 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3092 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3093 ** DOING. 3094 */ 3095 3096 /* ARGSUSED */ 3097 static SIGFUNC_DECL 3098 sigpipe(sig) 3099 int sig; 3100 { 3101 int save_errno = errno; 3102 3103 FIX_SYSV_SIGNAL(sig, sigpipe); 3104 StopRequest = true; 3105 errno = save_errno; 3106 return SIGFUNC_RETURN; 3107 } 3108 /* 3109 ** INTSIG -- clean up on interrupt 3110 ** 3111 ** This just arranges to exit. It pessimizes in that it 3112 ** may resend a message. 3113 ** 3114 ** Parameters: 3115 ** none. 3116 ** 3117 ** Returns: 3118 ** none. 3119 ** 3120 ** Side Effects: 3121 ** Unlocks the current job. 3122 ** 3123 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3124 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3125 ** DOING. 3126 ** 3127 ** XXX: More work is needed for this signal handler. 3128 */ 3129 3130 /* ARGSUSED */ 3131 SIGFUNC_DECL 3132 intsig(sig) 3133 int sig; 3134 { 3135 bool drop = false; 3136 int save_errno = errno; 3137 3138 FIX_SYSV_SIGNAL(sig, intsig); 3139 errno = save_errno; 3140 CHECK_CRITICAL(sig); 3141 sm_allsignals(true); 3142 3143 if (sig != 0 && LogLevel > 79) 3144 sm_syslog(LOG_DEBUG, CurEnv->e_id, "interrupt"); 3145 FileName = NULL; 3146 3147 /* Clean-up on aborted stdin message submission */ 3148 if (CurEnv->e_id != NULL && 3149 (OpMode == MD_SMTP || 3150 OpMode == MD_DELIVER || 3151 OpMode == MD_ARPAFTP)) 3152 { 3153 register ADDRESS *q; 3154 3155 /* don't return an error indication */ 3156 CurEnv->e_to = NULL; 3157 CurEnv->e_flags &= ~EF_FATALERRS; 3158 CurEnv->e_flags |= EF_CLRQUEUE; 3159 3160 /* 3161 ** Spin through the addresses and 3162 ** mark them dead to prevent bounces 3163 */ 3164 3165 for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 3166 q->q_state = QS_DONTSEND; 3167 3168 drop = true; 3169 } 3170 else if (OpMode != MD_TEST) 3171 { 3172 unlockqueue(CurEnv); 3173 } 3174 3175 finis(drop, false, EX_OK); 3176 /* NOTREACHED */ 3177 } 3178 /* 3179 ** DISCONNECT -- remove our connection with any foreground process 3180 ** 3181 ** Parameters: 3182 ** droplev -- how "deeply" we should drop the line. 3183 ** 0 -- ignore signals, mail back errors, make sure 3184 ** output goes to stdout. 3185 ** 1 -- also, make stdout go to /dev/null. 3186 ** 2 -- also, disconnect from controlling terminal 3187 ** (only for daemon mode). 3188 ** e -- the current envelope. 3189 ** 3190 ** Returns: 3191 ** none 3192 ** 3193 ** Side Effects: 3194 ** Trys to insure that we are immune to vagaries of 3195 ** the controlling tty. 3196 */ 3197 3198 void 3199 disconnect(droplev, e) 3200 int droplev; 3201 register ENVELOPE *e; 3202 { 3203 int fd; 3204 3205 if (tTd(52, 1)) 3206 sm_dprintf("disconnect: In %d Out %d, e=%p\n", 3207 sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL), 3208 sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL), e); 3209 if (tTd(52, 100)) 3210 { 3211 sm_dprintf("don't\n"); 3212 return; 3213 } 3214 if (LogLevel > 93) 3215 sm_syslog(LOG_DEBUG, e->e_id, 3216 "disconnect level %d", 3217 droplev); 3218 3219 /* be sure we don't get nasty signals */ 3220 (void) sm_signal(SIGINT, SIG_IGN); 3221 (void) sm_signal(SIGQUIT, SIG_IGN); 3222 3223 /* we can't communicate with our caller, so.... */ 3224 HoldErrs = true; 3225 CurEnv->e_errormode = EM_MAIL; 3226 Verbose = 0; 3227 DisConnected = true; 3228 3229 /* all input from /dev/null */ 3230 if (InChannel != smioin) 3231 { 3232 (void) sm_io_close(InChannel, SM_TIME_DEFAULT); 3233 InChannel = smioin; 3234 } 3235 if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL, 3236 SM_IO_RDONLY, NULL, smioin) == NULL) 3237 sm_syslog(LOG_ERR, e->e_id, 3238 "disconnect: sm_io_reopen(\"%s\") failed: %s", 3239 SM_PATH_DEVNULL, sm_errstring(errno)); 3240 3241 /* 3242 ** output to the transcript 3243 ** We also compare the fd numbers here since OutChannel 3244 ** might be a layer on top of smioout due to encryption 3245 ** (see sfsasl.c). 3246 */ 3247 3248 if (OutChannel != smioout && 3249 sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL) != 3250 sm_io_getinfo(smioout, SM_IO_WHAT_FD, NULL)) 3251 { 3252 (void) sm_io_close(OutChannel, SM_TIME_DEFAULT); 3253 OutChannel = smioout; 3254 3255 #if 0 3256 /* 3257 ** Has smioout been closed? Reopen it. 3258 ** This shouldn't happen anymore, the code is here 3259 ** just as a reminder. 3260 */ 3261 3262 if (smioout->sm_magic == NULL && 3263 sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL, 3264 SM_IO_WRONLY, NULL, smioout) == NULL) 3265 sm_syslog(LOG_ERR, e->e_id, 3266 "disconnect: sm_io_reopen(\"%s\") failed: %s", 3267 SM_PATH_DEVNULL, sm_errstring(errno)); 3268 #endif /* 0 */ 3269 } 3270 if (droplev > 0) 3271 { 3272 fd = open(SM_PATH_DEVNULL, O_WRONLY, 0666); 3273 if (fd == -1) 3274 sm_syslog(LOG_ERR, e->e_id, 3275 "disconnect: open(\"%s\") failed: %s", 3276 SM_PATH_DEVNULL, sm_errstring(errno)); 3277 (void) sm_io_flush(smioout, SM_TIME_DEFAULT); 3278 (void) dup2(fd, STDOUT_FILENO); 3279 (void) dup2(fd, STDERR_FILENO); 3280 (void) close(fd); 3281 } 3282 3283 /* drop our controlling TTY completely if possible */ 3284 if (droplev > 1) 3285 { 3286 (void) setsid(); 3287 errno = 0; 3288 } 3289 3290 #if XDEBUG 3291 checkfd012("disconnect"); 3292 #endif /* XDEBUG */ 3293 3294 if (LogLevel > 71) 3295 sm_syslog(LOG_DEBUG, e->e_id, "in background, pid=%d", 3296 (int) CurrentPid); 3297 3298 errno = 0; 3299 } 3300 3301 static void 3302 obsolete(argv) 3303 char *argv[]; 3304 { 3305 register char *ap; 3306 register char *op; 3307 3308 while ((ap = *++argv) != NULL) 3309 { 3310 /* Return if "--" or not an option of any form. */ 3311 if (ap[0] != '-' || ap[1] == '-') 3312 return; 3313 3314 /* Don't allow users to use "-Q." or "-Q ." */ 3315 if ((ap[1] == 'Q' && ap[2] == '.') || 3316 (ap[1] == 'Q' && argv[1] != NULL && 3317 argv[1][0] == '.' && argv[1][1] == '\0')) 3318 { 3319 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 3320 "Can not use -Q.\n"); 3321 exit(EX_USAGE); 3322 } 3323 3324 /* skip over options that do have a value */ 3325 op = strchr(OPTIONS, ap[1]); 3326 if (op != NULL && *++op == ':' && ap[2] == '\0' && 3327 ap[1] != 'd' && 3328 #if defined(sony_news) 3329 ap[1] != 'E' && ap[1] != 'J' && 3330 #endif /* defined(sony_news) */ 3331 argv[1] != NULL && argv[1][0] != '-') 3332 { 3333 argv++; 3334 continue; 3335 } 3336 3337 /* If -C doesn't have an argument, use sendmail.cf. */ 3338 #define __DEFPATH "sendmail.cf" 3339 if (ap[1] == 'C' && ap[2] == '\0') 3340 { 3341 *argv = xalloc(sizeof(__DEFPATH) + 2); 3342 (void) sm_strlcpyn(argv[0], sizeof(__DEFPATH) + 2, 2, 3343 "-C", __DEFPATH); 3344 } 3345 3346 /* If -q doesn't have an argument, run it once. */ 3347 if (ap[1] == 'q' && ap[2] == '\0') 3348 *argv = "-q0"; 3349 3350 /* If -Q doesn't have an argument, disable quarantining */ 3351 if (ap[1] == 'Q' && ap[2] == '\0') 3352 *argv = "-Q."; 3353 3354 /* if -d doesn't have an argument, use 0-99.1 */ 3355 if (ap[1] == 'd' && ap[2] == '\0') 3356 *argv = "-d0-99.1"; 3357 3358 #if defined(sony_news) 3359 /* if -E doesn't have an argument, use -EC */ 3360 if (ap[1] == 'E' && ap[2] == '\0') 3361 *argv = "-EC"; 3362 3363 /* if -J doesn't have an argument, use -JJ */ 3364 if (ap[1] == 'J' && ap[2] == '\0') 3365 *argv = "-JJ"; 3366 #endif /* defined(sony_news) */ 3367 } 3368 } 3369 /* 3370 ** AUTH_WARNING -- specify authorization warning 3371 ** 3372 ** Parameters: 3373 ** e -- the current envelope. 3374 ** msg -- the text of the message. 3375 ** args -- arguments to the message. 3376 ** 3377 ** Returns: 3378 ** none. 3379 */ 3380 3381 void 3382 #ifdef __STDC__ 3383 auth_warning(register ENVELOPE *e, const char *msg, ...) 3384 #else /* __STDC__ */ 3385 auth_warning(e, msg, va_alist) 3386 register ENVELOPE *e; 3387 const char *msg; 3388 va_dcl 3389 #endif /* __STDC__ */ 3390 { 3391 char buf[MAXLINE]; 3392 SM_VA_LOCAL_DECL 3393 3394 if (bitset(PRIV_AUTHWARNINGS, PrivacyFlags)) 3395 { 3396 register char *p; 3397 static char hostbuf[48]; 3398 3399 if (hostbuf[0] == '\0') 3400 { 3401 struct hostent *hp; 3402 3403 hp = myhostname(hostbuf, sizeof hostbuf); 3404 #if NETINET6 3405 if (hp != NULL) 3406 { 3407 freehostent(hp); 3408 hp = NULL; 3409 } 3410 #endif /* NETINET6 */ 3411 } 3412 3413 (void) sm_strlcpyn(buf, sizeof buf, 2, hostbuf, ": "); 3414 p = &buf[strlen(buf)]; 3415 SM_VA_START(ap, msg); 3416 (void) sm_vsnprintf(p, SPACELEFT(buf, p), msg, ap); 3417 SM_VA_END(ap); 3418 addheader("X-Authentication-Warning", buf, 0, e); 3419 if (LogLevel > 3) 3420 sm_syslog(LOG_INFO, e->e_id, 3421 "Authentication-Warning: %.400s", 3422 buf); 3423 } 3424 } 3425 /* 3426 ** GETEXTENV -- get from external environment 3427 ** 3428 ** Parameters: 3429 ** envar -- the name of the variable to retrieve 3430 ** 3431 ** Returns: 3432 ** The value, if any. 3433 */ 3434 3435 static char * 3436 getextenv(envar) 3437 const char *envar; 3438 { 3439 char **envp; 3440 int l; 3441 3442 l = strlen(envar); 3443 for (envp = ExternalEnviron; envp != NULL && *envp != NULL; envp++) 3444 { 3445 if (strncmp(*envp, envar, l) == 0 && (*envp)[l] == '=') 3446 return &(*envp)[l + 1]; 3447 } 3448 return NULL; 3449 } 3450 /* 3451 ** SETUSERENV -- set an environment in the propagated environment 3452 ** 3453 ** Parameters: 3454 ** envar -- the name of the environment variable. 3455 ** value -- the value to which it should be set. If 3456 ** null, this is extracted from the incoming 3457 ** environment. If that is not set, the call 3458 ** to setuserenv is ignored. 3459 ** 3460 ** Returns: 3461 ** none. 3462 */ 3463 3464 void 3465 setuserenv(envar, value) 3466 const char *envar; 3467 const char *value; 3468 { 3469 int i, l; 3470 char **evp = UserEnviron; 3471 char *p; 3472 3473 if (value == NULL) 3474 { 3475 value = getextenv(envar); 3476 if (value == NULL) 3477 return; 3478 } 3479 3480 /* XXX enforce reasonable size? */ 3481 i = strlen(envar) + 1; 3482 l = strlen(value) + i + 1; 3483 p = (char *) xalloc(l); 3484 (void) sm_strlcpyn(p, l, 3, envar, "=", value); 3485 3486 while (*evp != NULL && strncmp(*evp, p, i) != 0) 3487 evp++; 3488 if (*evp != NULL) 3489 { 3490 *evp++ = p; 3491 } 3492 else if (evp < &UserEnviron[MAXUSERENVIRON]) 3493 { 3494 *evp++ = p; 3495 *evp = NULL; 3496 } 3497 3498 /* make sure it is in our environment as well */ 3499 if (putenv(p) < 0) 3500 syserr("setuserenv: putenv(%s) failed", p); 3501 } 3502 /* 3503 ** DUMPSTATE -- dump state 3504 ** 3505 ** For debugging. 3506 */ 3507 3508 void 3509 dumpstate(when) 3510 char *when; 3511 { 3512 register char *j = macvalue('j', CurEnv); 3513 int rs; 3514 extern int NextMacroId; 3515 3516 sm_syslog(LOG_DEBUG, CurEnv->e_id, 3517 "--- dumping state on %s: $j = %s ---", 3518 when, 3519 j == NULL ? "<NULL>" : j); 3520 if (j != NULL) 3521 { 3522 if (!wordinclass(j, 'w')) 3523 sm_syslog(LOG_DEBUG, CurEnv->e_id, 3524 "*** $j not in $=w ***"); 3525 } 3526 sm_syslog(LOG_DEBUG, CurEnv->e_id, "CurChildren = %d", CurChildren); 3527 sm_syslog(LOG_DEBUG, CurEnv->e_id, "NextMacroId = %d (Max %d)", 3528 NextMacroId, MAXMACROID); 3529 sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- open file descriptors: ---"); 3530 printopenfds(true); 3531 sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- connection cache: ---"); 3532 mci_dump_all(smioout, true); 3533 rs = strtorwset("debug_dumpstate", NULL, ST_FIND); 3534 if (rs > 0) 3535 { 3536 int status; 3537 register char **pvp; 3538 char *pv[MAXATOM + 1]; 3539 3540 pv[0] = NULL; 3541 status = REWRITE(pv, rs, CurEnv); 3542 sm_syslog(LOG_DEBUG, CurEnv->e_id, 3543 "--- ruleset debug_dumpstate returns stat %d, pv: ---", 3544 status); 3545 for (pvp = pv; *pvp != NULL; pvp++) 3546 sm_syslog(LOG_DEBUG, CurEnv->e_id, "%s", *pvp); 3547 } 3548 sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- end of state dump ---"); 3549 } 3550 3551 #ifdef SIGUSR1 3552 /* 3553 ** SIGUSR1 -- Signal a request to dump state. 3554 ** 3555 ** Parameters: 3556 ** sig -- calling signal. 3557 ** 3558 ** Returns: 3559 ** none. 3560 ** 3561 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 3562 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 3563 ** DOING. 3564 ** 3565 ** XXX: More work is needed for this signal handler. 3566 */ 3567 3568 /* ARGSUSED */ 3569 static SIGFUNC_DECL 3570 sigusr1(sig) 3571 int sig; 3572 { 3573 int save_errno = errno; 3574 # if SM_HEAP_CHECK 3575 extern void dumpstab __P((void)); 3576 # endif /* SM_HEAP_CHECK */ 3577 3578 FIX_SYSV_SIGNAL(sig, sigusr1); 3579 errno = save_errno; 3580 CHECK_CRITICAL(sig); 3581 dumpstate("user signal"); 3582 # if SM_HEAP_CHECK 3583 dumpstab(); 3584 # endif /* SM_HEAP_CHECK */ 3585 errno = save_errno; 3586 return SIGFUNC_RETURN; 3587 } 3588 #endif /* SIGUSR1 */ 3589 3590 /* 3591 ** DROP_PRIVILEGES -- reduce privileges to those of the RunAsUser option 3592 ** 3593 ** Parameters: 3594 ** to_real_uid -- if set, drop to the real uid instead 3595 ** of the RunAsUser. 3596 ** 3597 ** Returns: 3598 ** EX_OSERR if the setuid failed. 3599 ** EX_OK otherwise. 3600 */ 3601 3602 int 3603 drop_privileges(to_real_uid) 3604 bool to_real_uid; 3605 { 3606 int rval = EX_OK; 3607 GIDSET_T emptygidset[1]; 3608 3609 if (tTd(47, 1)) 3610 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", 3611 (int) to_real_uid, 3612 (int) RealUid, (int) RealGid, 3613 (int) getuid(), (int) getgid(), 3614 (int) geteuid(), (int) getegid(), 3615 (int) RunAsUid, (int) RunAsGid); 3616 3617 if (to_real_uid) 3618 { 3619 RunAsUserName = RealUserName; 3620 RunAsUid = RealUid; 3621 RunAsGid = RealGid; 3622 EffGid = RunAsGid; 3623 } 3624 3625 /* make sure no one can grab open descriptors for secret files */ 3626 endpwent(); 3627 sm_mbdb_terminate(); 3628 3629 /* reset group permissions; these can be set later */ 3630 emptygidset[0] = (to_real_uid || RunAsGid != 0) ? RunAsGid : getegid(); 3631 3632 /* 3633 ** Notice: on some OS (Linux...) the setgroups() call causes 3634 ** a logfile entry if sendmail is not run by root. 3635 ** However, it is unclear (no POSIX standard) whether 3636 ** setgroups() can only succeed if executed by root. 3637 ** So for now we keep it as it is; if you want to change it, use 3638 ** if (geteuid() == 0 && setgroups(1, emptygidset) == -1) 3639 */ 3640 3641 if (setgroups(1, emptygidset) == -1 && geteuid() == 0) 3642 { 3643 syserr("drop_privileges: setgroups(1, %d) failed", 3644 (int) emptygidset[0]); 3645 rval = EX_OSERR; 3646 } 3647 3648 /* reset primary group id */ 3649 if (to_real_uid) 3650 { 3651 /* 3652 ** Drop gid to real gid. 3653 ** On some OS we must reset the effective[/real[/saved]] gid, 3654 ** and then use setgid() to finally drop all group privileges. 3655 ** Later on we check whether we can get back the 3656 ** effective gid. 3657 */ 3658 3659 #if HASSETEGID 3660 if (setegid(RunAsGid) < 0) 3661 { 3662 syserr("drop_privileges: setegid(%d) failed", 3663 (int) RunAsGid); 3664 rval = EX_OSERR; 3665 } 3666 #else /* HASSETEGID */ 3667 # if HASSETREGID 3668 if (setregid(RunAsGid, RunAsGid) < 0) 3669 { 3670 syserr("drop_privileges: setregid(%d, %d) failed", 3671 (int) RunAsGid, (int) RunAsGid); 3672 rval = EX_OSERR; 3673 } 3674 # else /* HASSETREGID */ 3675 # if HASSETRESGID 3676 if (setresgid(RunAsGid, RunAsGid, RunAsGid) < 0) 3677 { 3678 syserr("drop_privileges: setresgid(%d, %d, %d) failed", 3679 (int) RunAsGid, (int) RunAsGid, (int) RunAsGid); 3680 rval = EX_OSERR; 3681 } 3682 # endif /* HASSETRESGID */ 3683 # endif /* HASSETREGID */ 3684 #endif /* HASSETEGID */ 3685 } 3686 if (rval == EX_OK && (to_real_uid || RunAsGid != 0)) 3687 { 3688 if (setgid(RunAsGid) < 0 && (!UseMSP || getegid() != RunAsGid)) 3689 { 3690 syserr("drop_privileges: setgid(%d) failed", 3691 (int) RunAsGid); 3692 rval = EX_OSERR; 3693 } 3694 errno = 0; 3695 if (rval == EX_OK && getegid() != RunAsGid) 3696 { 3697 syserr("drop_privileges: Unable to set effective gid=%d to RunAsGid=%d", 3698 (int) getegid(), (int) RunAsGid); 3699 rval = EX_OSERR; 3700 } 3701 } 3702 3703 /* fiddle with uid */ 3704 if (to_real_uid || RunAsUid != 0) 3705 { 3706 uid_t euid; 3707 3708 /* 3709 ** Try to setuid(RunAsUid). 3710 ** euid must be RunAsUid, 3711 ** ruid must be RunAsUid unless (e|r)uid wasn't 0 3712 ** and we didn't have to drop privileges to the real uid. 3713 */ 3714 3715 if (setuid(RunAsUid) < 0 || 3716 geteuid() != RunAsUid || 3717 (getuid() != RunAsUid && 3718 (to_real_uid || geteuid() == 0 || getuid() == 0))) 3719 { 3720 #if HASSETREUID 3721 /* 3722 ** if ruid != RunAsUid, euid == RunAsUid, then 3723 ** try resetting just the real uid, then using 3724 ** setuid() to drop the saved-uid as well. 3725 */ 3726 3727 if (geteuid() == RunAsUid) 3728 { 3729 if (setreuid(RunAsUid, -1) < 0) 3730 { 3731 syserr("drop_privileges: setreuid(%d, -1) failed", 3732 (int) RunAsUid); 3733 rval = EX_OSERR; 3734 } 3735 if (setuid(RunAsUid) < 0) 3736 { 3737 syserr("drop_privileges: second setuid(%d) attempt failed", 3738 (int) RunAsUid); 3739 rval = EX_OSERR; 3740 } 3741 } 3742 else 3743 #endif /* HASSETREUID */ 3744 { 3745 syserr("drop_privileges: setuid(%d) failed", 3746 (int) RunAsUid); 3747 rval = EX_OSERR; 3748 } 3749 } 3750 euid = geteuid(); 3751 if (RunAsUid != 0 && setuid(0) == 0) 3752 { 3753 /* 3754 ** Believe it or not, the Linux capability model 3755 ** allows a non-root process to override setuid() 3756 ** on a process running as root and prevent that 3757 ** process from dropping privileges. 3758 */ 3759 3760 syserr("drop_privileges: setuid(0) succeeded (when it should not)"); 3761 rval = EX_OSERR; 3762 } 3763 else if (RunAsUid != euid && setuid(euid) == 0) 3764 { 3765 /* 3766 ** Some operating systems will keep the saved-uid 3767 ** if a non-root effective-uid calls setuid(real-uid) 3768 ** making it possible to set it back again later. 3769 */ 3770 3771 syserr("drop_privileges: Unable to drop non-root set-user-ID privileges"); 3772 rval = EX_OSERR; 3773 } 3774 } 3775 3776 if ((to_real_uid || RunAsGid != 0) && 3777 rval == EX_OK && RunAsGid != EffGid && 3778 getuid() != 0 && geteuid() != 0) 3779 { 3780 errno = 0; 3781 if (setgid(EffGid) == 0) 3782 { 3783 syserr("drop_privileges: setgid(%d) succeeded (when it should not)", 3784 (int) EffGid); 3785 rval = EX_OSERR; 3786 } 3787 } 3788 3789 if (tTd(47, 5)) 3790 { 3791 sm_dprintf("drop_privileges: e/ruid = %d/%d e/rgid = %d/%d\n", 3792 (int) geteuid(), (int) getuid(), 3793 (int) getegid(), (int) getgid()); 3794 sm_dprintf("drop_privileges: RunAsUser = %d:%d\n", 3795 (int) RunAsUid, (int) RunAsGid); 3796 if (tTd(47, 10)) 3797 sm_dprintf("drop_privileges: rval = %d\n", rval); 3798 } 3799 return rval; 3800 } 3801 /* 3802 ** FILL_FD -- make sure a file descriptor has been properly allocated 3803 ** 3804 ** Used to make sure that stdin/out/err are allocated on startup 3805 ** 3806 ** Parameters: 3807 ** fd -- the file descriptor to be filled. 3808 ** where -- a string used for logging. If NULL, this is 3809 ** being called on startup, and logging should 3810 ** not be done. 3811 ** 3812 ** Returns: 3813 ** none 3814 ** 3815 ** Side Effects: 3816 ** possibly changes MissingFds 3817 */ 3818 3819 void 3820 fill_fd(fd, where) 3821 int fd; 3822 char *where; 3823 { 3824 int i; 3825 struct stat stbuf; 3826 3827 if (fstat(fd, &stbuf) >= 0 || errno != EBADF) 3828 return; 3829 3830 if (where != NULL) 3831 syserr("fill_fd: %s: fd %d not open", where, fd); 3832 else 3833 MissingFds |= 1 << fd; 3834 i = open(SM_PATH_DEVNULL, fd == 0 ? O_RDONLY : O_WRONLY, 0666); 3835 if (i < 0) 3836 { 3837 syserr("!fill_fd: %s: cannot open %s", 3838 where == NULL ? "startup" : where, SM_PATH_DEVNULL); 3839 } 3840 if (fd != i) 3841 { 3842 (void) dup2(i, fd); 3843 (void) close(i); 3844 } 3845 } 3846 /* 3847 ** SM_PRINTOPTIONS -- print options 3848 ** 3849 ** Parameters: 3850 ** options -- array of options. 3851 ** 3852 ** Returns: 3853 ** none. 3854 */ 3855 3856 static void 3857 sm_printoptions(options) 3858 char **options; 3859 { 3860 int ll; 3861 char **av; 3862 3863 av = options; 3864 ll = 7; 3865 while (*av != NULL) 3866 { 3867 if (ll + strlen(*av) > 63) 3868 { 3869 sm_dprintf("\n"); 3870 ll = 0; 3871 } 3872 if (ll == 0) 3873 sm_dprintf("\t\t"); 3874 else 3875 sm_dprintf(" "); 3876 sm_dprintf("%s", *av); 3877 ll += strlen(*av++) + 1; 3878 } 3879 sm_dprintf("\n"); 3880 } 3881 /* 3882 ** TESTMODELINE -- process a test mode input line 3883 ** 3884 ** Parameters: 3885 ** line -- the input line. 3886 ** e -- the current environment. 3887 ** Syntax: 3888 ** # a comment 3889 ** .X process X as a configuration line 3890 ** =X dump a configuration item (such as mailers) 3891 ** $X dump a macro or class 3892 ** /X try an activity 3893 ** X normal process through rule set X 3894 */ 3895 3896 static void 3897 testmodeline(line, e) 3898 char *line; 3899 ENVELOPE *e; 3900 { 3901 register char *p; 3902 char *q; 3903 auto char *delimptr; 3904 int mid; 3905 int i, rs; 3906 STAB *map; 3907 char **s; 3908 struct rewrite *rw; 3909 ADDRESS a; 3910 static int tryflags = RF_COPYNONE; 3911 char exbuf[MAXLINE]; 3912 extern unsigned char TokTypeNoC[]; 3913 3914 /* skip leading spaces */ 3915 while (*line == ' ') 3916 line++; 3917 3918 switch (line[0]) 3919 { 3920 case '#': 3921 case '\0': 3922 return; 3923 3924 case '?': 3925 help("-bt", e); 3926 return; 3927 3928 case '.': /* config-style settings */ 3929 switch (line[1]) 3930 { 3931 case 'D': 3932 mid = macid_parse(&line[2], &delimptr); 3933 if (mid == 0) 3934 return; 3935 translate_dollars(delimptr); 3936 macdefine(&e->e_macro, A_TEMP, mid, delimptr); 3937 break; 3938 3939 case 'C': 3940 if (line[2] == '\0') /* not to call syserr() */ 3941 return; 3942 3943 mid = macid_parse(&line[2], &delimptr); 3944 if (mid == 0) 3945 return; 3946 translate_dollars(delimptr); 3947 expand(delimptr, exbuf, sizeof exbuf, e); 3948 p = exbuf; 3949 while (*p != '\0') 3950 { 3951 register char *wd; 3952 char delim; 3953 3954 while (*p != '\0' && isascii(*p) && isspace(*p)) 3955 p++; 3956 wd = p; 3957 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 3958 p++; 3959 delim = *p; 3960 *p = '\0'; 3961 if (wd[0] != '\0') 3962 setclass(mid, wd); 3963 *p = delim; 3964 } 3965 break; 3966 3967 case '\0': 3968 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 3969 "Usage: .[DC]macro value(s)\n"); 3970 break; 3971 3972 default: 3973 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 3974 "Unknown \".\" command %s\n", line); 3975 break; 3976 } 3977 return; 3978 3979 case '=': /* config-style settings */ 3980 switch (line[1]) 3981 { 3982 case 'S': /* dump rule set */ 3983 rs = strtorwset(&line[2], NULL, ST_FIND); 3984 if (rs < 0) 3985 { 3986 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 3987 "Undefined ruleset %s\n", &line[2]); 3988 return; 3989 } 3990 rw = RewriteRules[rs]; 3991 if (rw == NULL) 3992 return; 3993 do 3994 { 3995 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, 3996 'R'); 3997 s = rw->r_lhs; 3998 while (*s != NULL) 3999 { 4000 xputs(smioout, *s++); 4001 (void) sm_io_putc(smioout, 4002 SM_TIME_DEFAULT, ' '); 4003 } 4004 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, 4005 '\t'); 4006 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, 4007 '\t'); 4008 s = rw->r_rhs; 4009 while (*s != NULL) 4010 { 4011 xputs(smioout, *s++); 4012 (void) sm_io_putc(smioout, 4013 SM_TIME_DEFAULT, ' '); 4014 } 4015 (void) sm_io_putc(smioout, SM_TIME_DEFAULT, 4016 '\n'); 4017 } while ((rw = rw->r_next) != NULL); 4018 break; 4019 4020 case 'M': 4021 for (i = 0; i < MAXMAILERS; i++) 4022 { 4023 if (Mailer[i] != NULL) 4024 printmailer(smioout, Mailer[i]); 4025 } 4026 break; 4027 4028 case '\0': 4029 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4030 "Usage: =Sruleset or =M\n"); 4031 break; 4032 4033 default: 4034 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4035 "Unknown \"=\" command %s\n", line); 4036 break; 4037 } 4038 return; 4039 4040 case '-': /* set command-line-like opts */ 4041 switch (line[1]) 4042 { 4043 case 'd': 4044 tTflag(&line[2]); 4045 break; 4046 4047 case '\0': 4048 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4049 "Usage: -d{debug arguments}\n"); 4050 break; 4051 4052 default: 4053 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4054 "Unknown \"-\" command %s\n", line); 4055 break; 4056 } 4057 return; 4058 4059 case '$': 4060 if (line[1] == '=') 4061 { 4062 mid = macid(&line[2]); 4063 if (mid != 0) 4064 stabapply(dump_class, mid); 4065 return; 4066 } 4067 mid = macid(&line[1]); 4068 if (mid == 0) 4069 return; 4070 p = macvalue(mid, e); 4071 if (p == NULL) 4072 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4073 "Undefined\n"); 4074 else 4075 { 4076 xputs(smioout, p); 4077 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4078 "\n"); 4079 } 4080 return; 4081 4082 case '/': /* miscellaneous commands */ 4083 p = &line[strlen(line)]; 4084 while (--p >= line && isascii(*p) && isspace(*p)) 4085 *p = '\0'; 4086 p = strpbrk(line, " \t"); 4087 if (p != NULL) 4088 { 4089 while (isascii(*p) && isspace(*p)) 4090 *p++ = '\0'; 4091 } 4092 else 4093 p = ""; 4094 if (line[1] == '\0') 4095 { 4096 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4097 "Usage: /[canon|map|mx|parse|try|tryflags]\n"); 4098 return; 4099 } 4100 if (sm_strcasecmp(&line[1], "quit") == 0) 4101 { 4102 CurEnv->e_id = NULL; 4103 finis(true, true, ExitStat); 4104 /* NOTREACHED */ 4105 } 4106 if (sm_strcasecmp(&line[1], "mx") == 0) 4107 { 4108 #if NAMED_BIND 4109 /* look up MX records */ 4110 int nmx; 4111 auto int rcode; 4112 char *mxhosts[MAXMXHOSTS + 1]; 4113 4114 if (*p == '\0') 4115 { 4116 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4117 "Usage: /mx address\n"); 4118 return; 4119 } 4120 nmx = getmxrr(p, mxhosts, NULL, false, &rcode, true, 4121 NULL); 4122 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4123 "getmxrr(%s) returns %d value(s):\n", 4124 p, nmx); 4125 for (i = 0; i < nmx; i++) 4126 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4127 "\t%s\n", mxhosts[i]); 4128 #else /* NAMED_BIND */ 4129 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4130 "No MX code compiled in\n"); 4131 #endif /* NAMED_BIND */ 4132 } 4133 else if (sm_strcasecmp(&line[1], "canon") == 0) 4134 { 4135 char host[MAXHOSTNAMELEN]; 4136 4137 if (*p == '\0') 4138 { 4139 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4140 "Usage: /canon address\n"); 4141 return; 4142 } 4143 else if (sm_strlcpy(host, p, sizeof host) >= sizeof host) 4144 { 4145 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4146 "Name too long\n"); 4147 return; 4148 } 4149 (void) getcanonname(host, sizeof host, !HasWildcardMX, 4150 NULL); 4151 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4152 "getcanonname(%s) returns %s\n", 4153 p, host); 4154 } 4155 else if (sm_strcasecmp(&line[1], "map") == 0) 4156 { 4157 auto int rcode = EX_OK; 4158 char *av[2]; 4159 4160 if (*p == '\0') 4161 { 4162 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4163 "Usage: /map mapname key\n"); 4164 return; 4165 } 4166 for (q = p; *q != '\0' && !(isascii(*q) && isspace(*q)); q++) 4167 continue; 4168 if (*q == '\0') 4169 { 4170 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4171 "No key specified\n"); 4172 return; 4173 } 4174 *q++ = '\0'; 4175 map = stab(p, ST_MAP, ST_FIND); 4176 if (map == NULL) 4177 { 4178 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4179 "Map named \"%s\" not found\n", p); 4180 return; 4181 } 4182 if (!bitset(MF_OPEN, map->s_map.map_mflags) && 4183 !openmap(&(map->s_map))) 4184 { 4185 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4186 "Map named \"%s\" not open\n", p); 4187 return; 4188 } 4189 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4190 "map_lookup: %s (%s) ", p, q); 4191 av[0] = q; 4192 av[1] = NULL; 4193 p = (*map->s_map.map_class->map_lookup) 4194 (&map->s_map, q, av, &rcode); 4195 if (p == NULL) 4196 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4197 "no match (%d)\n", 4198 rcode); 4199 else 4200 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4201 "returns %s (%d)\n", p, 4202 rcode); 4203 } 4204 else if (sm_strcasecmp(&line[1], "try") == 0) 4205 { 4206 MAILER *m; 4207 STAB *st; 4208 auto int rcode = EX_OK; 4209 4210 q = strpbrk(p, " \t"); 4211 if (q != NULL) 4212 { 4213 while (isascii(*q) && isspace(*q)) 4214 *q++ = '\0'; 4215 } 4216 if (q == NULL || *q == '\0') 4217 { 4218 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4219 "Usage: /try mailer address\n"); 4220 return; 4221 } 4222 st = stab(p, ST_MAILER, ST_FIND); 4223 if (st == NULL) 4224 { 4225 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4226 "Unknown mailer %s\n", p); 4227 return; 4228 } 4229 m = st->s_mailer; 4230 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4231 "Trying %s %s address %s for mailer %s\n", 4232 bitset(RF_HEADERADDR, tryflags) ? "header" 4233 : "envelope", 4234 bitset(RF_SENDERADDR, tryflags) ? "sender" 4235 : "recipient", q, p); 4236 p = remotename(q, m, tryflags, &rcode, CurEnv); 4237 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4238 "Rcode = %d, addr = %s\n", 4239 rcode, p == NULL ? "<NULL>" : p); 4240 e->e_to = NULL; 4241 } 4242 else if (sm_strcasecmp(&line[1], "tryflags") == 0) 4243 { 4244 if (*p == '\0') 4245 { 4246 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4247 "Usage: /tryflags [Hh|Ee][Ss|Rr]\n"); 4248 return; 4249 } 4250 for (; *p != '\0'; p++) 4251 { 4252 switch (*p) 4253 { 4254 case 'H': 4255 case 'h': 4256 tryflags |= RF_HEADERADDR; 4257 break; 4258 4259 case 'E': 4260 case 'e': 4261 tryflags &= ~RF_HEADERADDR; 4262 break; 4263 4264 case 'S': 4265 case 's': 4266 tryflags |= RF_SENDERADDR; 4267 break; 4268 4269 case 'R': 4270 case 'r': 4271 tryflags &= ~RF_SENDERADDR; 4272 break; 4273 } 4274 } 4275 exbuf[0] = bitset(RF_HEADERADDR, tryflags) ? 'h' : 'e'; 4276 exbuf[1] = ' '; 4277 exbuf[2] = bitset(RF_SENDERADDR, tryflags) ? 's' : 'r'; 4278 exbuf[3] = '\0'; 4279 macdefine(&e->e_macro, A_TEMP, 4280 macid("{addr_type}"), exbuf); 4281 } 4282 else if (sm_strcasecmp(&line[1], "parse") == 0) 4283 { 4284 if (*p == '\0') 4285 { 4286 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4287 "Usage: /parse address\n"); 4288 return; 4289 } 4290 q = crackaddr(p, e); 4291 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4292 "Cracked address = "); 4293 xputs(smioout, q); 4294 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4295 "\nParsing %s %s address\n", 4296 bitset(RF_HEADERADDR, tryflags) ? 4297 "header" : "envelope", 4298 bitset(RF_SENDERADDR, tryflags) ? 4299 "sender" : "recipient"); 4300 if (parseaddr(p, &a, tryflags, '\0', NULL, e, true) 4301 == NULL) 4302 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4303 "Cannot parse\n"); 4304 else if (a.q_host != NULL && a.q_host[0] != '\0') 4305 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4306 "mailer %s, host %s, user %s\n", 4307 a.q_mailer->m_name, 4308 a.q_host, 4309 a.q_user); 4310 else 4311 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4312 "mailer %s, user %s\n", 4313 a.q_mailer->m_name, 4314 a.q_user); 4315 e->e_to = NULL; 4316 } 4317 else 4318 { 4319 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4320 "Unknown \"/\" command %s\n", 4321 line); 4322 } 4323 return; 4324 } 4325 4326 for (p = line; isascii(*p) && isspace(*p); p++) 4327 continue; 4328 q = p; 4329 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 4330 p++; 4331 if (*p == '\0') 4332 { 4333 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4334 "No address!\n"); 4335 return; 4336 } 4337 *p = '\0'; 4338 if (invalidaddr(p + 1, NULL, true)) 4339 return; 4340 do 4341 { 4342 register char **pvp; 4343 char pvpbuf[PSBUFSIZE]; 4344 4345 pvp = prescan(++p, ',', pvpbuf, sizeof pvpbuf, &delimptr, 4346 ConfigLevel >= 9 ? TokTypeNoC : NULL, false); 4347 if (pvp == NULL) 4348 continue; 4349 p = q; 4350 while (*p != '\0') 4351 { 4352 int status; 4353 4354 rs = strtorwset(p, NULL, ST_FIND); 4355 if (rs < 0) 4356 { 4357 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4358 "Undefined ruleset %s\n", 4359 p); 4360 break; 4361 } 4362 status = REWRITE(pvp, rs, e); 4363 if (status != EX_OK) 4364 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4365 "== Ruleset %s (%d) status %d\n", 4366 p, rs, status); 4367 while (*p != '\0' && *p++ != ',') 4368 continue; 4369 } 4370 } while (*(p = delimptr) != '\0'); 4371 } 4372 4373 static void 4374 dump_class(s, id) 4375 register STAB *s; 4376 int id; 4377 { 4378 if (s->s_symtype != ST_CLASS) 4379 return; 4380 if (bitnset(bitidx(id), s->s_class)) 4381 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 4382 "%s\n", s->s_name); 4383 } 4384 4385 /* 4386 ** An exception type used to create QuickAbort exceptions. 4387 ** This is my first cut at converting QuickAbort from longjmp to exceptions. 4388 ** These exceptions have a single integer argument, which is the argument 4389 ** to longjmp in the original code (either 1 or 2). I don't know the 4390 ** significance of 1 vs 2: the calls to setjmp don't care. 4391 */ 4392 4393 const SM_EXC_TYPE_T EtypeQuickAbort = 4394 { 4395 SmExcTypeMagic, 4396 "E:mta.quickabort", 4397 "i", 4398 sm_etype_printf, 4399 "quick abort %0", 4400 }; 4401