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