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