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