sync up to hp300/hp300/trap.c 7.26
[unix-history] / usr / src / usr.sbin / sendmail / src / main.c
CommitLineData
aeb2545d 1/*
dc45ba8c 2 * Copyright (c) 1983 Eric P. Allman
bee79b64
KB
3 * Copyright (c) 1988 Regents of the University of California.
4 * All rights reserved.
5 *
417f7a11 6 * %sccs.include.redist.c%
bee79b64 7 */
aeb2545d
DF
8
9#ifndef lint
10char copyright[] =
bee79b64 11"@(#) Copyright (c) 1988 Regents of the University of California.\n\
aeb2545d 12 All rights reserved.\n";
bee79b64 13#endif /* not lint */
aeb2545d
DF
14
15#ifndef lint
1c8ae8e7 16static char sccsid[] = "@(#)main.c 6.3 (Berkeley) %G%";
bee79b64 17#endif /* not lint */
aeb2545d 18
137c2d1f
KB
19#define _DEFINE
20
5bec1980 21#include <sys/file.h>
f2e44ded 22#include <sys/stat.h>
137c2d1f
KB
23#include <signal.h>
24#include <sgtty.h>
25#include "sendmail.h"
26#include <arpa/nameser.h>
27#include <resolv.h>
b3cbe40f 28
17a67c62 29# ifdef lint
2e3062fe 30char edata, end;
17a67c62
EA
31# endif lint
32
b3cbe40f 33/*
96faada8 34** SENDMAIL -- Post mail to a set of destinations.
b3cbe40f
EA
35**
36** This is the basic mail router. All user mail programs should
96faada8 37** call this routine to actually deliver mail. Sendmail in
b3cbe40f
EA
38** turn calls a bunch of mail servers that do the real work of
39** delivering the mail.
40**
3199e4f3
EA
41** Sendmail is driven by tables read in from /usr/lib/sendmail.cf
42** (read by readcf.c). Some more static configuration info,
43** including some code that you may want to tailor for your
44** installation, is in conf.c. You may also want to touch
45** daemon.c (if you have some other IPC mechanism), acct.c
46** (to change your accounting), names.c (to adjust the name
47** server mechanism).
b3cbe40f
EA
48**
49** Usage:
7338e3d4 50** /usr/lib/sendmail [flags] addr ...
b3cbe40f 51**
7338e3d4 52** See the associated documentation for details.
b3cbe40f 53**
b3cbe40f 54** Author:
7338e3d4
EA
55** Eric Allman, UCB/INGRES (until 10/81)
56** Britton-Lee, Inc., purveyors of fine
57** database computers (from 11/81)
ce46a48a 58** Now back at UCB at the Mammoth project.
7338e3d4
EA
59** The support of the INGRES Project and Britton-Lee is
60** gratefully acknowledged. Britton-Lee in
61** particular had absolutely nothing to gain from
62** my involvement in this project.
b3cbe40f
EA
63*/
64
65
83f674d8 66int NextMailer; /* "free" index into Mailer struct */
912acb74 67char *FullName; /* sender's full name */
be2fcca9 68ENVELOPE BlankEnvelope; /* a "blank" envelope */
2654b031 69ENVELOPE MainEnvelope; /* the envelope around the basic letter */
2e3062fe 70ADDRESS NullAddress = /* a null address */
599d88d0 71 { "", "", NULL, "" };
2e3062fe
EA
72
73/*
74** Pointers for setproctitle.
75** This allows "ps" listings to give more useful information.
76** These must be kept out of BSS for frozen configuration files
77** to work.
78*/
79
80# ifdef SETPROCTITLE
81char **Argv = NULL; /* pointer to argument vector */
82char *LastArgv = NULL; /* end of argv */
f3d8f6d6 83# endif /* SETPROCTITLE */
b3cbe40f 84
ce46a48a
EA
85/*
86** The file in which to log raw recipient information.
87** This is logged before aliasing, forwarding, and so forth so we
88** can see how our addresses are being used. For example, this
89** would give us the names of aliases (instead of what they alias
90** to), the pre-MX hostnames, and so forth.
91**
92** This is specified on the command line, not in the config file,
93** and is therefore really only useful for logging SMTP RCPTs.
94*/
95
96char *RcptLogFile = NULL; /* file name */
97
f96fa7de
EA
98static void obsolete();
99
4aebfe5d
EA
100#ifdef DAEMON
101#ifndef SMTP
102ERROR %%%% Cannot have daemon mode without SMTP %%%% ERROR
f3d8f6d6
EA
103#endif /* SMTP */
104#endif /* DAEMON */
4aebfe5d 105
2281a453
EA
106#define MAXCONFIGLEVEL 3 /* highest config version level known */
107
e36705df 108main(argc, argv, envp)
b3cbe40f
EA
109 int argc;
110 char **argv;
e36705df 111 char **envp;
b3cbe40f
EA
112{
113 register char *p;
af04f6e4 114 register char *q;
acae5a9d 115 char **av;
17df0fcb 116 char *locname;
b3cbe40f 117 extern int finis();
b3cbe40f 118 extern char Version[];
b2d4b27a 119 char *ep, *from;
b3cbe40f 120 typedef int (*fnptr)();
be2fcca9 121 STAB *st;
9e3c0a28 122 register int i;
d0a69620 123 int j;
4388720d 124 bool readconfig = TRUE;
aba51985 125 bool queuemode = FALSE; /* process queue requests */
43e0af62 126 bool nothaw;
66391bf0 127 bool safecf = TRUE;
be2fcca9 128 static bool reenter = FALSE;
b2d4b27a 129 char jbuf[MAXHOSTNAMELEN]; /* holds MyHostName */
f96fa7de
EA
130 extern int DtableSize;
131 extern int optind;
f6a0cc15 132 extern bool safefile();
ed45aae1 133 extern time_t convtime();
dd1fe05b 134 extern putheader(), putbody();
be2fcca9 135 extern ENVELOPE *newenvelope();
0df908a9 136 extern void intsig();
1dbda134 137 extern char **myhostname();
22e6d6b8 138 extern char *arpadate();
e36705df 139 extern char **environ;
f96fa7de 140 extern char *optarg;
b72377c6 141
22659072
EA
142 /*
143 ** Check to see if we reentered.
144 ** This would normally happen if e_putheader or e_putbody
145 ** were NULL when invoked.
146 */
147
b72377c6
EA
148 if (reenter)
149 {
150 syserr("main: reentered!");
151 abort();
152 }
153 reenter = TRUE;
abae7b2d 154 extern ADDRESS *recipient();
17df0fcb 155 bool canrename;
b3cbe40f 156
07c63e56 157#ifndef SYS5TZ
140717b5
EA
158 /* enforce use of kernel-supplied time zone information */
159 unsetenv("TZ");
160#endif
60c0b7b2 161
3eb4fac4
EA
162 /* in 4.4BSD, the table can be huge; impose a reasonable limit */
163 DtableSize = getdtablesize();
164 if (DtableSize > 256)
165 DtableSize = 256;
166
d15bd559
EA
167 /*
168 ** Be sure we have enough file descriptors.
59b65d0f 169 ** But also be sure that 0, 1, & 2 are open.
d15bd559
EA
170 */
171
e1098041 172 i = open("/dev/null", O_RDWR);
59b65d0f
EA
173 while (i >= 0 && i < 2)
174 i = dup(i);
66391bf0 175
3eb4fac4 176 i = DtableSize;
66391bf0 177 while (--i > 2)
d15bd559
EA
178 (void) close(i);
179 errno = 0;
180
07adc4f5
RA
181#ifdef LOG_MAIL
182 openlog("sendmail", LOG_PID, LOG_MAIL);
183#else
184 openlog("sendmail", LOG_PID);
185#endif
186
8ff78b51
EA
187 /*
188 ** Set default values for variables.
189 ** These cannot be in initialized data space.
190 */
191
192 setdefaults();
193
bb09c502
EA
194 /* set up the blank envelope */
195 BlankEnvelope.e_puthdr = putheader;
196 BlankEnvelope.e_putbody = putbody;
197 BlankEnvelope.e_xfp = NULL;
2e3062fe 198 STRUCTCOPY(NullAddress, BlankEnvelope.e_from);
bd575ea9
EA
199 STRUCTCOPY(BlankEnvelope, MainEnvelope);
200 CurEnv = &MainEnvelope;
bb09c502 201
b2d4b27a
KB
202 /* Handle any non-getoptable constructions. */
203 obsolete(argv);
204
22659072
EA
205 /*
206 ** Do a quick prescan of the argument list.
207 ** We do this to find out if we can potentially thaw the
208 ** configuration file. If not, we do the thaw now so that
209 ** the argument processing applies to this run rather than
210 ** to the run that froze the configuration.
211 */
43e0af62 212 nothaw = FALSE;
a0431b8d
EA
213#define OPTIONS "b:C:cd:eF:f:h:Iimno:p:q:R:r:sTtv"
214 while ((j = getopt(argc, argv, OPTIONS)) != EOF)
f96fa7de
EA
215 {
216 switch (j)
560a80d9 217 {
b2d4b27a
KB
218 case 'b':
219 if (optarg[0] == 'z' && optarg[1] == '\0')
220 nothaw = TRUE;
221 break;
222
223 case 'C':
224 ConfFile = optarg;
0e306e7f
EA
225 (void) setgid(getrgid());
226 (void) setuid(getruid());
66391bf0 227 safecf = FALSE;
43e0af62 228 nothaw = TRUE;
b2d4b27a
KB
229 break;
230
231 case 'd':
43e0af62 232 tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
b2d4b27a 233 tTflag(optarg);
43e0af62
EA
234 setbuf(stdout, (char *) NULL);
235 printf("Version %s\n", Version);
b2d4b27a 236 break;
43e0af62 237 }
f96fa7de 238 }
2bc15c4e
KB
239
240 InChannel = stdin;
241 OutChannel = stdout;
242
43e0af62 243 if (!nothaw)
22659072
EA
244 readconfig = !thaw(FreezeFile);
245
b2d4b27a
KB
246 /* strip out "dangerous" environment variables */
247 (void) unsetenv("FS");
248 for (i = 1; (p = envp[i++]) != NULL;)
d0a69620 249 {
b2d4b27a
KB
250 if (strncmp(p, "LD_", 3) == 0)
251 {
f96fa7de
EA
252 /* hack: change this name to be non-special */
253 p[0] = '\201';
d0a69620 254 continue;
b2d4b27a 255 }
d0a69620 256 }
b2d4b27a 257 environ = envp;
42bbf376
EA
258
259# ifdef SETPROCTITLE
260 /*
261 ** Save start and extent of argv for setproctitle.
262 */
263
264 Argv = argv;
3251a23d 265 if (--i > 0)
577bce94
EA
266 LastArgv = envp[i - 1] + strlen(envp[i - 1]);
267 else
268 LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
f3d8f6d6 269# endif /* SETPROCTITLE */
e36705df 270
b3cbe40f 271 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
6e2f38be 272 (void) signal(SIGINT, intsig);
eb211e2c 273 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
6e2f38be
EA
274 (void) signal(SIGHUP, intsig);
275 (void) signal(SIGTERM, intsig);
ed7382d3 276 (void) signal(SIGPIPE, SIG_IGN);
a0554f81 277 OldUmask = umask(0);
75f95954 278 OpMode = MD_DELIVER;
e673aad7 279 MotherPid = getpid();
561c7c50 280 FullName = getenv("NAME");
dd1fe05b 281
b3cbe40f
EA
282 errno = 0;
283 from = NULL;
378e8da7 284
4388720d 285 if (readconfig)
1dbda134 286 {
4388720d
EA
287 /* initialize some macros, etc. */
288 initmacros();
289
290 /* hostname */
291 av = myhostname(jbuf, sizeof jbuf);
292 if (jbuf[0] != '\0')
293 {
dec0d30e
EA
294 extern char *strchr();
295
43e0af62
EA
296 if (tTd(0, 4))
297 printf("canonical name: %s\n", jbuf);
4388720d
EA
298 p = newstr(jbuf);
299 define('w', p, CurEnv);
dec0d30e
EA
300
301 q = strchr(jbuf, '.');
302 if (q != NULL)
303 {
1bc035b0 304 *q++ = '\0';
dec0d30e 305 p = newstr(jbuf);
1bc035b0 306 define('m', q, CurEnv);
dec0d30e 307 }
4388720d
EA
308 setclass('w', p);
309 }
310 while (av != NULL && *av != NULL)
43e0af62 311 {
43e0af62
EA
312 if (tTd(0, 4))
313 printf("\ta.k.a.: %s\n", *av);
4388720d 314 setclass('w', *av++);
43e0af62 315 }
4388720d
EA
316
317 /* version */
318 define('v', Version, CurEnv);
1dbda134 319 }
378e8da7
EA
320
321 /* current time */
22e6d6b8 322 define('b', arpadate((char *) NULL), CurEnv);
378e8da7 323
a691a4a6 324 /*
c1e24818 325 ** Crack argv.
a691a4a6
EA
326 */
327
acae5a9d 328 av = argv;
f3d8f6d6 329 p = strrchr(*av, '/');
26a3626c
EA
330 if (p++ == NULL)
331 p = *av;
332 if (strcmp(p, "newaliases") == 0)
75f95954 333 OpMode = MD_INITALIAS;
26a3626c 334 else if (strcmp(p, "mailq") == 0)
75f95954 335 OpMode = MD_PRINT;
34fe0a9b
EA
336 else if (strcmp(p, "smtpd") == 0)
337 OpMode = MD_DAEMON;
b2d4b27a
KB
338
339 optind = 1;
a0431b8d 340 while ((j = getopt(argc, argv, OPTIONS)) != EOF)
f96fa7de
EA
341 {
342 switch (j)
a691a4a6 343 {
75f95954 344 case 'b': /* operations mode */
f96fa7de 345 switch (j = *optarg)
c1e24818 346 {
75f95954 347 case MD_DAEMON:
908dc8db
MK
348# ifdef DAEMON
349 if (getuid() != 0) {
350 usrerr("Permission denied");
351 exit (EX_USAGE);
352 }
353 (void) unsetenv("HOSTALIASES");
354# else
2e15a2d8
MK
355 usrerr("Daemon mode not implemented");
356 ExitStat = EX_USAGE;
75f95954 357 break;
f3d8f6d6 358# endif /* DAEMON */
75f95954
EA
359 case MD_SMTP:
360# ifndef SMTP
2e15a2d8
MK
361 usrerr("I don't speak SMTP");
362 ExitStat = EX_USAGE;
75f95954 363 break;
f3d8f6d6 364# endif /* SMTP */
75f95954
EA
365 case MD_DELIVER:
366 case MD_VERIFY:
367 case MD_TEST:
368 case MD_INITALIAS:
369 case MD_PRINT:
370 case MD_FREEZE:
b2d4b27a 371 OpMode = j;
75f95954
EA
372 break;
373
374 default:
b2d4b27a 375 usrerr("Invalid operation mode %c", j);
2e15a2d8 376 ExitStat = EX_USAGE;
75f95954 377 break;
c1e24818
EA
378 }
379 break;
380
560a80d9 381 case 'C': /* select configuration file (already done) */
c1e24818 382 break;
a691a4a6 383
43e0af62 384 case 'd': /* debugging -- redo in case frozen */
c1e24818 385 tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
b2d4b27a 386 tTflag(optarg);
c1e24818 387 setbuf(stdout, (char *) NULL);
c1e24818 388 break;
b3cbe40f 389
b3cbe40f 390 case 'f': /* from address */
c1e24818 391 case 'r': /* obsolete -f flag */
22659072 392 if (from != NULL)
b3cbe40f 393 {
2e15a2d8
MK
394 usrerr("More than one \"from\" person");
395 ExitStat = EX_USAGE;
b3cbe40f
EA
396 break;
397 }
b2d4b27a 398 from = newstr(optarg);
b3cbe40f
EA
399 break;
400
6da7b890 401 case 'F': /* set full name */
b2d4b27a 402 FullName = newstr(optarg);
6da7b890
EA
403 break;
404
b3cbe40f 405 case 'h': /* hop count */
b2d4b27a
KB
406 CurEnv->e_hopcount = strtol(optarg, &ep, 10);
407 if (*ep)
b3cbe40f 408 {
b2d4b27a 409 usrerr("Bad hop count (%s)", optarg);
2e15a2d8 410 ExitStat = EX_USAGE;
3110074f 411 break;
b3cbe40f 412 }
b3cbe40f 413 break;
b3cbe40f 414
c1e24818
EA
415 case 'n': /* don't alias */
416 NoAlias = TRUE;
d59b067a
EA
417 break;
418
c1e24818 419 case 'o': /* set option */
b2d4b27a 420 setoption(*optarg, optarg + 1, FALSE, TRUE);
14a39063 421 break;
cbdb7357 422
b1e36c23 423 case 'p': /* set protocol */
b2d4b27a 424 q = strchr(optarg, ':');
af04f6e4
EA
425 if (q != NULL)
426 *q++ = '\0';
b2d4b27a
KB
427 if (*optarg != '\0')
428 define('r', newstr(optarg), CurEnv);
af04f6e4
EA
429 if (*q != '\0')
430 define('s', newstr(q), CurEnv);
b1e36c23
EA
431 break;
432
ed45aae1 433 case 'q': /* run queue files at intervals */
884a20cb 434# ifdef QUEUE
f2e44ded
EA
435 if (getuid() != 0)
436 {
437 struct stat stbuf;
438
439 /* check to see if we own the queue directory */
440 if (stat(QueueDir, &stbuf) < 0)
441 syserr("main: cannot stat %s", QueueDir);
442 if (stbuf.st_uid != getuid())
443 {
444 /* nope, really a botch */
445 usrerr("Permission denied");
446 exit (EX_NOPERM);
447 }
908dc8db
MK
448 }
449 (void) unsetenv("HOSTALIASES");
d0a69620 450 FullName = NULL;
aba51985 451 queuemode = TRUE;
b2d4b27a 452 QueueIntvl = convtime(optarg);
f3d8f6d6 453# else /* QUEUE */
2e15a2d8
MK
454 usrerr("I don't know about queues");
455 ExitStat = EX_USAGE;
f3d8f6d6 456# endif /* QUEUE */
ed45aae1
EA
457 break;
458
c1e24818
EA
459 case 't': /* read recipients from message */
460 GrabTo = TRUE;
461 break;
462
463 /* compatibility flags */
c1e24818
EA
464 case 'c': /* connect to non-local mailers */
465 case 'e': /* error message disposition */
466 case 'i': /* don't let dot stop me */
467 case 'm': /* send to me too */
468 case 'T': /* set timeout interval */
469 case 'v': /* give blow-by-blow description */
a0431b8d 470 setoption(j, "T", FALSE, TRUE);
35cc3fad
EA
471 break;
472
c1e24818 473 case 's': /* save From lines in headers */
a0431b8d 474 setoption('f', "T", FALSE, TRUE);
b3cbe40f 475 break;
26a3626c
EA
476
477# ifdef DBM
478 case 'I': /* initialize alias DBM file */
75f95954 479 OpMode = MD_INITALIAS;
26a3626c 480 break;
ce46a48a
EA
481# endif /* DBM */
482
483 case 'R': /* log raw recipient info */
b2d4b27a
KB
484 RcptLogFile = newstr(optarg);
485 break;
486
487 default:
488 ExitStat = EX_USAGE;
489 finis();
ce46a48a 490 break;
b3cbe40f 491 }
f96fa7de 492 }
b2d4b27a 493 av += optind;
b3cbe40f 494
836a1487
EA
495#ifdef NAMED_BIND
496 if (tTd(8, 1))
497 _res.options |= RES_DEBUG;
498#endif
499
9e3c0a28 500 /*
2cce0c26
EA
501 ** Do basic initialization.
502 ** Read system control file.
179c1218 503 ** Extract special fields for local use.
9e3c0a28
EA
504 */
505
46f6ec52 506 if (OpMode == MD_FREEZE || readconfig)
a4076aed 507 readcf(ConfFile, safecf, CurEnv);
22659072 508
07c63e56 509#ifdef SYS5TZ
ca4d0c0b
EA
510 /* Enforce use of local time (null string overrides this) */
511 if (TimeZoneSpec == NULL)
512 unsetenv("TZ");
513 else if (TimeZoneSpec[0] != '\0')
514 {
515 p = xalloc(strlen(TimeZoneSpec) + 4);
516 (void) strcpy(p, "TZ=");
517 (void) strcat(p, TimeZoneSpec);
518 putenv(p);
519 }
520#endif
521
2281a453
EA
522 if (ConfigLevel > MAXCONFIGLEVEL)
523 {
524 syserr("Warning: .cf version level (%d) exceeds program functionality (%d)",
525 ConfigLevel, MAXCONFIGLEVEL);
526 }
75f95954 527 switch (OpMode)
acae5a9d 528 {
26a3626c 529 case MD_FREEZE:
a9621daf 530 /* this is critical to avoid forgeries of the frozen config */
0e306e7f
EA
531 (void) setgid(getgid());
532 (void) setuid(getuid());
a9621daf
EA
533
534 /* freeze the configuration */
8fe4fb9b 535 freeze(FreezeFile);
acae5a9d 536 exit(EX_OK);
26a3626c
EA
537
538 case MD_INITALIAS:
539 Verbose = TRUE;
540 break;
13dfebea
EA
541
542 case MD_DAEMON:
543 /* remove things that don't make sense in daemon mode */
544 FullName = NULL;
545 break;
acae5a9d 546 }
179c1218 547
6130649c
EA
548 /* do heuristic mode adjustment */
549 if (Verbose)
75f95954
EA
550 {
551 /* turn off noconnect option */
552 setoption('c', "F", TRUE, FALSE);
553
554 /* turn on interactive delivery */
555 setoption('d', "", TRUE, FALSE);
556 }
6130649c 557
179c1218 558 /* our name for SMTP codes */
a73ae8ac 559 expand("\001j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
57c97d4a 560 MyHostName = jbuf;
d6a28dd8 561
179c1218
EA
562 /* the indices of local and program mailers */
563 st = stab("local", ST_MAILER, ST_FIND);
564 if (st == NULL)
565 syserr("No local mailer defined");
566 else
567 LocalMailer = st->s_mailer;
568 st = stab("prog", ST_MAILER, ST_FIND);
569 if (st == NULL)
570 syserr("No prog mailer defined");
571 else
572 ProgMailer = st->s_mailer;
573
6bbaf971
EA
574 /* operate in queue directory */
575 if (chdir(QueueDir) < 0)
576 {
577 syserr("cannot chdir(%s)", QueueDir);
578 exit(EX_SOFTWARE);
579 }
580
64912e7e 581 /*
55f0da62 582 ** Do operation-mode-dependent initialization.
64912e7e
EA
583 */
584
55f0da62 585 switch (OpMode)
64912e7e 586 {
55f0da62
EA
587 case MD_PRINT:
588 /* print the queue */
74f37936 589#ifdef QUEUE
64912e7e
EA
590 dropenvelope(CurEnv);
591 printqueue();
592 exit(EX_OK);
f3d8f6d6 593#else /* QUEUE */
74f37936
EA
594 usrerr("No queue to print");
595 finis();
f3d8f6d6 596#endif /* QUEUE */
8acb5142 597
55f0da62
EA
598 case MD_INITALIAS:
599 /* initialize alias database */
a4076aed 600 initaliases(AliasFile, TRUE, CurEnv);
f4dbf345 601 exit(EX_OK);
cdb17311 602
55f0da62
EA
603 case MD_DAEMON:
604 /* don't open alias database -- done in srvrsmtp */
605 break;
606
607 default:
608 /* open the alias database */
a4076aed 609 initaliases(AliasFile, FALSE, CurEnv);
55f0da62
EA
610 break;
611 }
612
9678c96d 613 if (tTd(0, 15))
9c6d4c70 614 {
f6a0cc15 615 /* print configuration table (or at least part of it) */
9c6d4c70
EA
616 printrules();
617 for (i = 0; i < MAXMAILERS; i++)
618 {
619 register struct mailer *m = Mailer[i];
1dbda134 620 int j;
9c6d4c70
EA
621
622 if (m == NULL)
623 continue;
97ad25b6
EA
624 printf("mailer %d (%s): P=%s S=%d R=%d M=%ld F=", i, m->m_name,
625 m->m_mailer, m->m_s_rwset, m->m_r_rwset,
626 m->m_maxsize);
1dbda134
EA
627 for (j = '\0'; j <= '\177'; j++)
628 if (bitnset(j, m->m_flags))
0e306e7f 629 (void) putchar(j);
1dbda134 630 printf(" E=");
b3ef02a2 631 xputs(m->m_eol);
72f91c2e
EA
632 if (m->m_argv != NULL)
633 {
634 char **a = m->m_argv;
635
636 printf(" A=");
637 while (*a != NULL)
638 {
639 if (a != m->m_argv)
640 printf(" ");
641 xputs(*a++);
642 }
643 }
b3ef02a2 644 printf("\n");
9c6d4c70
EA
645 }
646 }
9c6d4c70 647
be2fcca9
EA
648 /*
649 ** Switch to the main envelope.
650 */
651
652 CurEnv = newenvelope(&MainEnvelope);
e6f08ab1 653 MainEnvelope.e_flags = BlankEnvelope.e_flags;
be2fcca9 654
cf69a203
EA
655 /*
656 ** If test mode, read addresses from stdin and process.
657 */
658
75f95954 659 if (OpMode == MD_TEST)
cf69a203 660 {
dab2b390 661 bool terminal = isatty(fileno(stdin));
cf69a203
EA
662 char buf[MAXLINE];
663
dab2b390
EA
664 if (terminal)
665 {
666 printf("ADDRESS TEST MODE (ruleset 3 NOT automatically invoked)\n");
667 printf("Enter <ruleset> <address>\n");
668 }
cf69a203
EA
669 for (;;)
670 {
671 register char **pvp;
50435450 672 char *q;
50435450 673 extern char *DelimChar;
2ee7e9e0 674 extern bool invalidaddr();
cf69a203 675
dab2b390
EA
676 if (terminal)
677 printf("> ");
0e306e7f 678 (void) fflush(stdout);
cf69a203
EA
679 if (fgets(buf, sizeof buf, stdin) == NULL)
680 finis();
dab2b390
EA
681 if (!terminal)
682 printf("> %s", buf);
683 if (buf[0] == '#')
684 continue;
f90b4150 685 for (p = buf; isspace(*p); p++)
cf69a203 686 continue;
ecfd2c8e
EA
687 q = p;
688 while (*p != '\0' && !isspace(*p))
689 p++;
cf69a203 690 if (*p == '\0')
2ee7e9e0
EA
691 {
692 printf("No address!\n");
cf69a203 693 continue;
2ee7e9e0 694 }
50435450 695 *p = '\0';
2ee7e9e0 696 if (invalidaddr(p + 1))
2ee7e9e0 697 continue;
50435450 698 do
ecfd2c8e 699 {
217a0102
EA
700 extern char **prescan();
701 char pvpbuf[PSBUFSIZE];
702
703 pvp = prescan(++p, ',', pvpbuf);
50435450 704 if (pvp == NULL)
ecfd2c8e 705 continue;
50435450
EA
706 p = q;
707 while (*p != '\0')
708 {
709 rewrite(pvp, atoi(p));
710 while (*p != '\0' && *p++ != ',')
711 continue;
712 }
713 } while (*(p = DelimChar) != '\0');
cf69a203
EA
714 }
715 }
716
e3cd595c
EA
717# ifdef QUEUE
718 /*
719 ** If collecting stuff from the queue, go start doing that.
720 */
721
7b21425b 722 if (queuemode && OpMode != MD_DAEMON && QueueIntvl == 0)
e3cd595c 723 {
ca4d0c0b 724 runqueue(FALSE);
e3cd595c
EA
725 finis();
726 }
f3d8f6d6 727# endif /* QUEUE */
e3cd595c 728
f6a0cc15
EA
729 /*
730 ** If a daemon, wait for a request.
731 ** getrequests will always return in a child.
25b9d645 732 ** If we should also be processing the queue, start
19147b2d
EA
733 ** doing it in background.
734 ** We check for any errors that might have happened
735 ** during startup.
f6a0cc15
EA
736 */
737
75f95954 738 if (OpMode == MD_DAEMON || QueueIntvl != 0)
25b9d645 739 {
9678c96d 740 if (!tTd(0, 1))
58b27aa4 741 {
fcdf8200 742 /* put us in background */
58b27aa4
EA
743 i = fork();
744 if (i < 0)
745 syserr("daemon: cannot fork");
746 if (i != 0)
747 exit(0);
fcdf8200
EA
748
749 /* get our pid right */
d9162460 750 MotherPid = getpid();
fcdf8200
EA
751
752 /* disconnect from our controlling tty */
d188728a 753 disconnect(TRUE);
58b27aa4 754 }
7338e3d4 755
25b9d645
EA
756# ifdef QUEUE
757 if (queuemode)
f309127e 758 {
ca4d0c0b 759 runqueue(TRUE);
75f95954 760 if (OpMode != MD_DAEMON)
f309127e
EA
761 for (;;)
762 pause();
763 }
f3d8f6d6 764# endif /* QUEUE */
7338e3d4
EA
765 dropenvelope(CurEnv);
766
767#ifdef DAEMON
f6a0cc15 768 getrequests();
2a16bae3
EA
769
770 /* at this point we are in a child: reset state */
75f95954 771 OpMode = MD_SMTP;
7338e3d4 772 (void) newenvelope(CurEnv);
912acb74 773 openxscript(CurEnv);
f3d8f6d6 774#endif /* DAEMON */
7338e3d4 775 }
88039044
EA
776
777# ifdef SMTP
778 /*
779 ** If running SMTP protocol, start collecting and executing
780 ** commands. This will never return.
781 */
782
75f95954 783 if (OpMode == MD_SMTP)
a4076aed 784 smtp(CurEnv);
f3d8f6d6 785# endif /* SMTP */
88039044 786
f6a0cc15 787 /*
e6f08ab1 788 ** Do basic system initialization and set the sender
f6a0cc15
EA
789 */
790
a4076aed 791 initsys(CurEnv);
b5958391 792 setsender(from, CurEnv);
a9e0e597 793
0e1aa71e 794 if (*av == NULL && !GrabTo)
e863b1fa 795 {
2e3062fe
EA
796 usrerr("Recipient names must be specified");
797
798 /* collect body for UUCP return */
799 if (OpMode != MD_VERIFY)
a4076aed 800 collect(FALSE, CurEnv);
e863b1fa
EA
801 finis();
802 }
75f95954
EA
803 if (OpMode == MD_VERIFY)
804 SendMode = SM_VERIFY;
b3cbe40f 805
b3cbe40f 806 /*
d6b27179 807 ** Scan argv and deliver the message to everyone.
b3cbe40f
EA
808 */
809
a4076aed 810 sendtoargv(av, CurEnv);
b3cbe40f 811
72e9b3cc 812 /* if we have had errors sofar, arrange a meaningful exit stat */
d916f0ca 813 if (Errors > 0 && ExitStat == EX_OK)
a4b004a6 814 ExitStat = EX_USAGE;
a4b004a6 815
dc39c568
EA
816 /*
817 ** Read the input mail.
818 */
819
2654b031 820 CurEnv->e_to = NULL;
75f95954 821 if (OpMode != MD_VERIFY || GrabTo)
a4076aed 822 collect(FALSE, CurEnv);
d829793b 823 errno = 0;
35cc3fad 824
4e1f4d4b 825 /* collect statistics */
7338e3d4
EA
826 if (OpMode != MD_VERIFY)
827 markstats(CurEnv, (ADDRESS *) NULL);
b3cbe40f 828
9678c96d 829 if (tTd(1, 1))
2654b031 830 printf("From person = \"%s\"\n", CurEnv->e_from.q_paddr);
d6b27179 831
b3cbe40f
EA
832 /*
833 ** Actually send everything.
d6b27179 834 ** If verifying, just ack.
b3cbe40f
EA
835 */
836
7338e3d4
EA
837 CurEnv->e_from.q_flags |= QDONTSEND;
838 CurEnv->e_to = NULL;
f7e74083 839 sendall(CurEnv, SM_DEFAULT);
b3cbe40f
EA
840
841 /*
842 ** All done.
843 */
844
845 finis();
846}
847\f/*
848** FINIS -- Clean up and exit.
849**
b3cbe40f
EA
850** Parameters:
851** none
852**
853** Returns:
854** never
855**
856** Side Effects:
96faada8 857** exits sendmail
b3cbe40f
EA
858*/
859
860finis()
861{
9678c96d 862 if (tTd(2, 1))
e6f08ab1 863 printf("\n====finis: stat %d e_flags %o\n", ExitStat, CurEnv->e_flags);
aba51985 864
7338e3d4 865 /* clean up temp files */
912acb74 866 CurEnv->e_to = NULL;
e6f08ab1 867 dropenvelope(CurEnv);
b3cbe40f 868
f2e44ded
EA
869 /* flush any cached connections */
870 mci_flush();
871
7338e3d4
EA
872 /* post statistics */
873 poststats(StatFile);
68f0b54c 874
7338e3d4 875 /* and exit */
36a4e219
EA
876# ifdef LOG
877 if (LogLevel > 11)
878 syslog(LOG_DEBUG, "finis, pid=%d", getpid());
f3d8f6d6 879# endif /* LOG */
c8ec8736
EA
880 if (ExitStat == EX_TEMPFAIL)
881 ExitStat = EX_OK;
b3cbe40f
EA
882 exit(ExitStat);
883}
884\f/*
6e2f38be
EA
885** INTSIG -- clean up on interrupt
886**
7338e3d4
EA
887** This just arranges to exit. It pessimises in that it
888** may resend a message.
6e2f38be
EA
889**
890** Parameters:
891** none.
892**
893** Returns:
894** none.
895**
896** Side Effects:
7338e3d4 897** Unlocks the current job.
6e2f38be
EA
898*/
899
0df908a9 900void
6e2f38be
EA
901intsig()
902{
7338e3d4
EA
903 FileName = NULL;
904 unlockqueue(CurEnv);
905 exit(EX_OK);
6e2f38be
EA
906}
907\f/*
721fad23
EA
908** INITMACROS -- initialize the macro system
909**
910** This just involves defining some macros that are actually
911** used internally as metasymbols to be themselves.
912**
913** Parameters:
914** none.
915**
916** Returns:
917** none.
918**
919** Side Effects:
920** initializes several macros to be themselves.
921*/
922
9dbc8d99
EA
923struct metamac MetaMacros[] =
924{
eca244ca 925 /* LHS pattern matching characters */
42fa5d67
EA
926 '*', MATCHZANY, '+', MATCHANY, '-', MATCHONE,
927 '=', MATCHCLASS, '~', MATCHNCLASS,
721fad23
EA
928
929 /* these are RHS metasymbols */
42fa5d67
EA
930 '#', CANONNET, '@', CANONHOST, ':', CANONUSER,
931 '>', CALLSUBR,
41173b8f 932 '{', MATCHLOOKUP, '}', MATCHELOOKUP,
721fad23 933
eca244ca 934 /* the conditional operations */
42fa5d67 935 '?', CONDIF, '|', CONDELSE, '.', CONDFI,
9dbc8d99 936
eca244ca 937 /* and finally the hostname lookup characters */
42fa5d67
EA
938 '[', HOSTBEGIN, ']', HOSTEND,
939 '(', LOOKUPBEGIN, ')', LOOKUPEND,
eca244ca 940
9dbc8d99 941 '\0'
721fad23
EA
942};
943
944initmacros()
945{
9dbc8d99
EA
946 register struct metamac *m;
947 char buf[5];
948 register int c;
721fad23 949
9dbc8d99
EA
950 for (m = MetaMacros; m->metaname != '\0'; m++)
951 {
952 buf[0] = m->metaval;
953 buf[1] = '\0';
7338e3d4 954 define(m->metaname, newstr(buf), CurEnv);
9dbc8d99
EA
955 }
956 buf[0] = MATCHREPL;
957 buf[2] = '\0';
958 for (c = '0'; c <= '9'; c++)
959 {
960 buf[1] = c;
7338e3d4 961 define(c, newstr(buf), CurEnv);
9dbc8d99 962 }
721fad23 963}
dd1fe05b 964\f/*
acae5a9d
EA
965** FREEZE -- freeze BSS & allocated memory
966**
967** This will be used to efficiently load the configuration file.
968**
969** Parameters:
8fe4fb9b 970** freezefile -- the name of the file to freeze to.
acae5a9d
EA
971**
972** Returns:
973** none.
974**
975** Side Effects:
8fe4fb9b 976** Writes BSS and malloc'ed memory to freezefile
acae5a9d
EA
977*/
978
7338e3d4 979union frz
acae5a9d 980{
7338e3d4
EA
981 char frzpad[BUFSIZ]; /* insure we are on a BUFSIZ boundary */
982 struct
983 {
984 time_t frzstamp; /* timestamp on this freeze */
985 char *frzbrk; /* the current break */
2e3062fe
EA
986 char *frzedata; /* address of edata */
987 char *frzend; /* address of end */
7338e3d4
EA
988 char frzver[252]; /* sendmail version */
989 } frzinfo;
acae5a9d
EA
990};
991
8fe4fb9b
EA
992freeze(freezefile)
993 char *freezefile;
acae5a9d
EA
994{
995 int f;
7338e3d4 996 union frz fhdr;
2e3062fe 997 extern char edata, end;
acae5a9d 998 extern char *sbrk();
912acb74 999 extern char Version[];
acae5a9d 1000
8fe4fb9b 1001 if (freezefile == NULL)
acae5a9d
EA
1002 return;
1003
1004 /* try to open the freeze file */
8fe4fb9b 1005 f = creat(freezefile, FileMode);
acae5a9d
EA
1006 if (f < 0)
1007 {
2e15a2d8 1008 syserr("Cannot freeze %s", freezefile);
acae5a9d
EA
1009 errno = 0;
1010 return;
1011 }
1012
1013 /* build the freeze header */
7338e3d4
EA
1014 fhdr.frzinfo.frzstamp = curtime();
1015 fhdr.frzinfo.frzbrk = sbrk(0);
2e3062fe
EA
1016 fhdr.frzinfo.frzedata = &edata;
1017 fhdr.frzinfo.frzend = &end;
0e306e7f 1018 (void) strcpy(fhdr.frzinfo.frzver, Version);
acae5a9d
EA
1019
1020 /* write out the freeze header */
611b763d 1021 if (write(f, (char *) &fhdr, sizeof fhdr) != sizeof fhdr ||
34fe0a9b
EA
1022 write(f, (char *) &edata, (int) (fhdr.frzinfo.frzbrk - &edata)) !=
1023 (int) (fhdr.frzinfo.frzbrk - &edata))
7338e3d4 1024 {
2e15a2d8 1025 syserr("Cannot freeze %s", freezefile);
7338e3d4 1026 }
acae5a9d
EA
1027
1028 /* fine, clean up */
1029 (void) close(f);
1030}
1031\f/*
1032** THAW -- read in the frozen configuration file.
1033**
1034** Parameters:
8fe4fb9b 1035** freezefile -- the name of the file to thaw from.
acae5a9d
EA
1036**
1037** Returns:
1038** TRUE if it successfully read the freeze file.
1039** FALSE otherwise.
1040**
1041** Side Effects:
8fe4fb9b 1042** reads freezefile in to BSS area.
acae5a9d
EA
1043*/
1044
8fe4fb9b
EA
1045thaw(freezefile)
1046 char *freezefile;
acae5a9d
EA
1047{
1048 int f;
ccfed53c 1049 register char *p;
7338e3d4 1050 union frz fhdr;
ccfed53c 1051 char hbuf[60];
a59237a4 1052 extern char edata, end;
912acb74 1053 extern char Version[];
17a67c62 1054 extern caddr_t brk();
ccfed53c
EA
1055 extern char **myhostname();
1056 extern char *macvalue();
acae5a9d 1057
8fe4fb9b 1058 if (freezefile == NULL)
acae5a9d
EA
1059 return (FALSE);
1060
1061 /* open the freeze file */
8fe4fb9b 1062 f = open(freezefile, 0);
acae5a9d
EA
1063 if (f < 0)
1064 {
1065 errno = 0;
1066 return (FALSE);
1067 }
1068
1069 /* read in the header */
07adc4f5
RA
1070 if (read(f, (char *) &fhdr, sizeof fhdr) < sizeof fhdr)
1071 {
db281fbf 1072 syslog(LOG_WARNING, "Cannot read frozen config file");
07adc4f5
RA
1073 (void) close(f);
1074 return (FALSE);
1075 }
db281fbf 1076 if (fhdr.frzinfo.frzedata != &edata ||
2e3062fe 1077 fhdr.frzinfo.frzend != &end ||
7338e3d4 1078 strcmp(fhdr.frzinfo.frzver, Version) != 0)
acae5a9d 1079 {
07adc4f5 1080 syslog(LOG_WARNING, "Wrong version of frozen config file");
acae5a9d
EA
1081 (void) close(f);
1082 return (FALSE);
1083 }
1084
1085 /* arrange to have enough space */
17a67c62 1086 if (brk(fhdr.frzinfo.frzbrk) == (caddr_t) -1)
acae5a9d 1087 {
7338e3d4 1088 syserr("Cannot break to %x", fhdr.frzinfo.frzbrk);
acae5a9d
EA
1089 (void) close(f);
1090 return (FALSE);
1091 }
1092
1093 /* now read in the freeze file */
34fe0a9b
EA
1094 if (read(f, (char *) &edata, (int) (fhdr.frzinfo.frzbrk - &edata)) !=
1095 (int) (fhdr.frzinfo.frzbrk - &edata))
acae5a9d 1096 {
07adc4f5 1097 syserr("Cannot read frozen config file");
acae5a9d 1098 /* oops! we have trashed memory..... */
0e306e7f 1099 (void) write(2, "Cannot read freeze file\n", 24);
7338e3d4 1100 _exit(EX_SOFTWARE);
acae5a9d
EA
1101 }
1102
1103 (void) close(f);
ccfed53c
EA
1104
1105 /* verify that the host name was correct on the freeze */
1106 (void) myhostname(hbuf, sizeof hbuf);
1107 p = macvalue('w', CurEnv);
1108 if (p == NULL)
1109 p = "";
1110 if (strcmp(hbuf, macvalue('w', CurEnv)) == 0)
1111 return (TRUE);
1112 syslog(LOG_WARNING, "Hostname changed since freeze (%s => %s)",
1113 p, hbuf);
1114 return (FALSE);
acae5a9d 1115}
813d8709
EA
1116\f/*
1117** DISCONNECT -- remove our connection with any foreground process
1118**
1119** Parameters:
d188728a
EA
1120** fulldrop -- if set, we should also drop the controlling
1121** TTY if possible -- this should only be done when
1122** setting up the daemon since otherwise UUCP can
1123** leave us trying to open a dialin, and we will
1124** wait for the carrier.
813d8709
EA
1125**
1126** Returns:
1127** none
1128**
1129** Side Effects:
1130** Trys to insure that we are immune to vagaries of
1131** the controlling tty.
1132*/
1133
d188728a
EA
1134disconnect(fulldrop)
1135 bool fulldrop;
813d8709
EA
1136{
1137 int fd;
1138
e6f08ab1 1139 if (tTd(52, 1))
b9accadd
EA
1140 printf("disconnect: In %d Out %d\n", fileno(InChannel),
1141 fileno(OutChannel));
e6f08ab1 1142 if (tTd(52, 5))
813d8709 1143 {
e6f08ab1
EA
1144 printf("don't\n");
1145 return;
813d8709 1146 }
813d8709
EA
1147
1148 /* be sure we don't get nasty signals */
0e306e7f
EA
1149 (void) signal(SIGHUP, SIG_IGN);
1150 (void) signal(SIGINT, SIG_IGN);
1151 (void) signal(SIGQUIT, SIG_IGN);
813d8709
EA
1152
1153 /* we can't communicate with our caller, so.... */
7338e3d4
EA
1154 HoldErrs = TRUE;
1155 ErrorMode = EM_MAIL;
813d8709
EA
1156 Verbose = FALSE;
1157
1158 /* all input from /dev/null */
813d8709 1159 if (InChannel != stdin)
b9accadd
EA
1160 {
1161 (void) fclose(InChannel);
1162 InChannel = stdin;
1163 }
1164 (void) freopen("/dev/null", "r", stdin);
813d8709
EA
1165
1166 /* output to the transcript */
b9accadd 1167 if (OutChannel != stdout)
813d8709 1168 {
b9accadd
EA
1169 (void) fclose(OutChannel);
1170 OutChannel = stdout;
813d8709 1171 }
912acb74
EA
1172 if (CurEnv->e_xfp == NULL)
1173 CurEnv->e_xfp = fopen("/dev/null", "w");
b9accadd
EA
1174 (void) fflush(stdout);
1175 (void) close(1);
1176 (void) close(2);
912acb74 1177 while ((fd = dup(fileno(CurEnv->e_xfp))) < 2 && fd > 0)
b9accadd 1178 continue;
813d8709 1179
e6f08ab1 1180 /* drop our controlling TTY completely if possible */
d188728a 1181 if (fulldrop)
e6f08ab1 1182 {
5229f34d 1183 (void) setsid();
afe907a4 1184#ifdef TIOCNOTTY
d188728a
EA
1185 fd = open("/dev/tty", 2);
1186 if (fd >= 0)
1187 {
17a67c62 1188 (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
d188728a
EA
1189 (void) close(fd);
1190 }
17a67c62 1191 (void) setpgrp(0, 0);
afe907a4 1192#endif /* TIOCNOTTY */
70faa7c8 1193 errno = 0;
e6f08ab1 1194 }
e6f08ab1 1195
813d8709
EA
1196# ifdef LOG
1197 if (LogLevel > 11)
1198 syslog(LOG_DEBUG, "in background, pid=%d", getpid());
f3d8f6d6 1199# endif /* LOG */
813d8709
EA
1200
1201 errno = 0;
1202}
b2d4b27a
KB
1203
1204static void
1205obsolete(argv)
1206 char *argv[];
1207{
1208 char *ap;
1209
1210 while (ap = *++argv)
1211 {
1212 /* Return if "--" or not an option of any form. */
1213 if (ap[0] != '-' || ap[1] == '-')
1214 return;
1215
1216 /* If -C doesn't have an argument, use sendmail.cf. */
1217#define __DEFPATH "sendmail.cf"
f96fa7de
EA
1218 if (ap[1] == 'C' && ap[2] == '\0' &&
1219 (argv[1] == NULL || argv[1][0] == '-'))
b2d4b27a
KB
1220 {
1221 *argv = xalloc(sizeof(__DEFPATH) + 2);
1222 argv[0][0] = '-';
1223 argv[0][1] = 'C';
1224 (void)strcpy(&argv[0][2], __DEFPATH);
1225 }
f96fa7de
EA
1226
1227 /* If -q doesn't have an argument, run it once. */
1228 if (ap[1] == 'q' && ap[2] == '\0' &&
1229 (argv[1] == NULL || argv[1][0] == '-'))
1230 *argv = "-q0";
a0431b8d
EA
1231
1232 /* if -d doesn't have an argument, use 0-99.1 */
1233 if (ap[1] == 'd' && ap[2] == '\0' &&
1234 (argv[1] == NULL || argv[1][0] == '-'))
1235 *argv = "-d0-99.1";
b2d4b27a
KB
1236 }
1237}