fix botch in YPCOMPAT mode
[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
dab2b390 16static char sccsid[] = "@(#)main.c 5.58 (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 664 {
dab2b390 665 bool terminal = isatty(fileno(stdin));
cf69a203
EA
666 char buf[MAXLINE];
667
dab2b390
EA
668 if (terminal)
669 {
670 printf("ADDRESS TEST MODE (ruleset 3 NOT automatically invoked)\n");
671 printf("Enter <ruleset> <address>\n");
672 }
cf69a203
EA
673 for (;;)
674 {
675 register char **pvp;
50435450 676 char *q;
50435450 677 extern char *DelimChar;
cf69a203 678
dab2b390
EA
679 if (terminal)
680 printf("> ");
0e306e7f 681 (void) fflush(stdout);
cf69a203
EA
682 if (fgets(buf, sizeof buf, stdin) == NULL)
683 finis();
dab2b390
EA
684 if (!terminal)
685 printf("> %s", buf);
686 if (buf[0] == '#')
687 continue;
f90b4150 688 for (p = buf; isspace(*p); p++)
cf69a203 689 continue;
ecfd2c8e
EA
690 q = p;
691 while (*p != '\0' && !isspace(*p))
692 p++;
cf69a203
EA
693 if (*p == '\0')
694 continue;
50435450
EA
695 *p = '\0';
696 do
ecfd2c8e 697 {
217a0102
EA
698 extern char **prescan();
699 char pvpbuf[PSBUFSIZE];
700
701 pvp = prescan(++p, ',', pvpbuf);
50435450 702 if (pvp == NULL)
ecfd2c8e 703 continue;
50435450
EA
704 p = q;
705 while (*p != '\0')
706 {
707 rewrite(pvp, atoi(p));
708 while (*p != '\0' && *p++ != ',')
709 continue;
710 }
711 } while (*(p = DelimChar) != '\0');
cf69a203
EA
712 }
713 }
714
e3cd595c
EA
715# ifdef QUEUE
716 /*
717 ** If collecting stuff from the queue, go start doing that.
718 */
719
7b21425b 720 if (queuemode && OpMode != MD_DAEMON && QueueIntvl == 0)
e3cd595c 721 {
ca4d0c0b 722 runqueue(FALSE);
e3cd595c
EA
723 finis();
724 }
725# endif QUEUE
726
f6a0cc15
EA
727 /*
728 ** If a daemon, wait for a request.
729 ** getrequests will always return in a child.
25b9d645 730 ** If we should also be processing the queue, start
19147b2d
EA
731 ** doing it in background.
732 ** We check for any errors that might have happened
733 ** during startup.
f6a0cc15
EA
734 */
735
75f95954 736 if (OpMode == MD_DAEMON || QueueIntvl != 0)
25b9d645 737 {
9678c96d 738 if (!tTd(0, 1))
58b27aa4 739 {
fcdf8200 740 /* put us in background */
58b27aa4
EA
741 i = fork();
742 if (i < 0)
743 syserr("daemon: cannot fork");
744 if (i != 0)
745 exit(0);
fcdf8200
EA
746
747 /* get our pid right */
d9162460 748 MotherPid = getpid();
fcdf8200
EA
749
750 /* disconnect from our controlling tty */
d188728a 751 disconnect(TRUE);
58b27aa4 752 }
7338e3d4 753
25b9d645
EA
754# ifdef QUEUE
755 if (queuemode)
f309127e 756 {
ca4d0c0b 757 runqueue(TRUE);
75f95954 758 if (OpMode != MD_DAEMON)
f309127e
EA
759 for (;;)
760 pause();
761 }
25b9d645 762# endif QUEUE
7338e3d4
EA
763 dropenvelope(CurEnv);
764
765#ifdef DAEMON
f6a0cc15 766 getrequests();
2a16bae3
EA
767
768 /* at this point we are in a child: reset state */
75f95954 769 OpMode = MD_SMTP;
7338e3d4 770 (void) newenvelope(CurEnv);
912acb74 771 openxscript(CurEnv);
14a39063 772#endif DAEMON
7338e3d4 773 }
88039044
EA
774
775# ifdef SMTP
776 /*
777 ** If running SMTP protocol, start collecting and executing
778 ** commands. This will never return.
779 */
780
75f95954 781 if (OpMode == MD_SMTP)
a4076aed 782 smtp(CurEnv);
88039044
EA
783# endif SMTP
784
f6a0cc15 785 /*
e6f08ab1 786 ** Do basic system initialization and set the sender
f6a0cc15
EA
787 */
788
a4076aed 789 initsys(CurEnv);
b5958391 790 setsender(from, CurEnv);
a9e0e597 791
0e1aa71e 792 if (*av == NULL && !GrabTo)
e863b1fa 793 {
2e3062fe
EA
794 usrerr("Recipient names must be specified");
795
796 /* collect body for UUCP return */
797 if (OpMode != MD_VERIFY)
a4076aed 798 collect(FALSE, CurEnv);
e863b1fa
EA
799 finis();
800 }
75f95954
EA
801 if (OpMode == MD_VERIFY)
802 SendMode = SM_VERIFY;
b3cbe40f 803
b3cbe40f 804 /*
d6b27179 805 ** Scan argv and deliver the message to everyone.
b3cbe40f
EA
806 */
807
a4076aed 808 sendtoargv(av, CurEnv);
b3cbe40f 809
72e9b3cc 810 /* if we have had errors sofar, arrange a meaningful exit stat */
d916f0ca 811 if (Errors > 0 && ExitStat == EX_OK)
a4b004a6 812 ExitStat = EX_USAGE;
a4b004a6 813
dc39c568
EA
814 /*
815 ** Read the input mail.
816 */
817
2654b031 818 CurEnv->e_to = NULL;
75f95954 819 if (OpMode != MD_VERIFY || GrabTo)
a4076aed 820 collect(FALSE, CurEnv);
d829793b 821 errno = 0;
35cc3fad 822
4e1f4d4b 823 /* collect statistics */
7338e3d4
EA
824 if (OpMode != MD_VERIFY)
825 markstats(CurEnv, (ADDRESS *) NULL);
b3cbe40f 826
9678c96d 827 if (tTd(1, 1))
2654b031 828 printf("From person = \"%s\"\n", CurEnv->e_from.q_paddr);
d6b27179 829
b3cbe40f
EA
830 /*
831 ** Actually send everything.
d6b27179 832 ** If verifying, just ack.
b3cbe40f
EA
833 */
834
7338e3d4
EA
835 CurEnv->e_from.q_flags |= QDONTSEND;
836 CurEnv->e_to = NULL;
f7e74083 837 sendall(CurEnv, SM_DEFAULT);
b3cbe40f
EA
838
839 /*
840 ** All done.
841 */
842
843 finis();
844}
845\f/*
846** FINIS -- Clean up and exit.
847**
b3cbe40f
EA
848** Parameters:
849** none
850**
851** Returns:
852** never
853**
854** Side Effects:
96faada8 855** exits sendmail
b3cbe40f
EA
856*/
857
858finis()
859{
9678c96d 860 if (tTd(2, 1))
e6f08ab1 861 printf("\n====finis: stat %d e_flags %o\n", ExitStat, CurEnv->e_flags);
aba51985 862
7338e3d4 863 /* clean up temp files */
912acb74 864 CurEnv->e_to = NULL;
e6f08ab1 865 dropenvelope(CurEnv);
b3cbe40f 866
f2e44ded
EA
867 /* flush any cached connections */
868 mci_flush();
869
7338e3d4
EA
870 /* post statistics */
871 poststats(StatFile);
68f0b54c 872
7338e3d4 873 /* and exit */
36a4e219
EA
874# ifdef LOG
875 if (LogLevel > 11)
876 syslog(LOG_DEBUG, "finis, pid=%d", getpid());
877# endif LOG
c8ec8736
EA
878 if (ExitStat == EX_TEMPFAIL)
879 ExitStat = EX_OK;
b3cbe40f
EA
880 exit(ExitStat);
881}
882\f/*
6e2f38be
EA
883** INTSIG -- clean up on interrupt
884**
7338e3d4
EA
885** This just arranges to exit. It pessimises in that it
886** may resend a message.
6e2f38be
EA
887**
888** Parameters:
889** none.
890**
891** Returns:
892** none.
893**
894** Side Effects:
7338e3d4 895** Unlocks the current job.
6e2f38be
EA
896*/
897
0df908a9 898void
6e2f38be
EA
899intsig()
900{
7338e3d4
EA
901 FileName = NULL;
902 unlockqueue(CurEnv);
903 exit(EX_OK);
6e2f38be
EA
904}
905\f/*
721fad23
EA
906** INITMACROS -- initialize the macro system
907**
908** This just involves defining some macros that are actually
909** used internally as metasymbols to be themselves.
910**
911** Parameters:
912** none.
913**
914** Returns:
915** none.
916**
917** Side Effects:
918** initializes several macros to be themselves.
919*/
920
9dbc8d99
EA
921struct metamac MetaMacros[] =
922{
eca244ca 923 /* LHS pattern matching characters */
42fa5d67
EA
924 '*', MATCHZANY, '+', MATCHANY, '-', MATCHONE,
925 '=', MATCHCLASS, '~', MATCHNCLASS,
721fad23
EA
926
927 /* these are RHS metasymbols */
42fa5d67
EA
928 '#', CANONNET, '@', CANONHOST, ':', CANONUSER,
929 '>', CALLSUBR,
41173b8f 930 '{', MATCHLOOKUP, '}', MATCHELOOKUP,
721fad23 931
eca244ca 932 /* the conditional operations */
42fa5d67 933 '?', CONDIF, '|', CONDELSE, '.', CONDFI,
9dbc8d99 934
eca244ca 935 /* and finally the hostname lookup characters */
42fa5d67
EA
936 '[', HOSTBEGIN, ']', HOSTEND,
937 '(', LOOKUPBEGIN, ')', LOOKUPEND,
eca244ca 938
9dbc8d99 939 '\0'
721fad23
EA
940};
941
942initmacros()
943{
9dbc8d99
EA
944 register struct metamac *m;
945 char buf[5];
946 register int c;
721fad23 947
9dbc8d99
EA
948 for (m = MetaMacros; m->metaname != '\0'; m++)
949 {
950 buf[0] = m->metaval;
951 buf[1] = '\0';
7338e3d4 952 define(m->metaname, newstr(buf), CurEnv);
9dbc8d99
EA
953 }
954 buf[0] = MATCHREPL;
955 buf[2] = '\0';
956 for (c = '0'; c <= '9'; c++)
957 {
958 buf[1] = c;
7338e3d4 959 define(c, newstr(buf), CurEnv);
9dbc8d99 960 }
721fad23 961}
dd1fe05b 962\f/*
acae5a9d
EA
963** FREEZE -- freeze BSS & allocated memory
964**
965** This will be used to efficiently load the configuration file.
966**
967** Parameters:
8fe4fb9b 968** freezefile -- the name of the file to freeze to.
acae5a9d
EA
969**
970** Returns:
971** none.
972**
973** Side Effects:
8fe4fb9b 974** Writes BSS and malloc'ed memory to freezefile
acae5a9d
EA
975*/
976
7338e3d4 977union frz
acae5a9d 978{
7338e3d4
EA
979 char frzpad[BUFSIZ]; /* insure we are on a BUFSIZ boundary */
980 struct
981 {
982 time_t frzstamp; /* timestamp on this freeze */
983 char *frzbrk; /* the current break */
2e3062fe
EA
984 char *frzedata; /* address of edata */
985 char *frzend; /* address of end */
7338e3d4
EA
986 char frzver[252]; /* sendmail version */
987 } frzinfo;
acae5a9d
EA
988};
989
8fe4fb9b
EA
990freeze(freezefile)
991 char *freezefile;
acae5a9d
EA
992{
993 int f;
7338e3d4 994 union frz fhdr;
2e3062fe 995 extern char edata, end;
acae5a9d 996 extern char *sbrk();
912acb74 997 extern char Version[];
acae5a9d 998
8fe4fb9b 999 if (freezefile == NULL)
acae5a9d
EA
1000 return;
1001
1002 /* try to open the freeze file */
8fe4fb9b 1003 f = creat(freezefile, FileMode);
acae5a9d
EA
1004 if (f < 0)
1005 {
2e15a2d8 1006 syserr("Cannot freeze %s", freezefile);
acae5a9d
EA
1007 errno = 0;
1008 return;
1009 }
1010
1011 /* build the freeze header */
7338e3d4
EA
1012 fhdr.frzinfo.frzstamp = curtime();
1013 fhdr.frzinfo.frzbrk = sbrk(0);
2e3062fe
EA
1014 fhdr.frzinfo.frzedata = &edata;
1015 fhdr.frzinfo.frzend = &end;
0e306e7f 1016 (void) strcpy(fhdr.frzinfo.frzver, Version);
acae5a9d
EA
1017
1018 /* write out the freeze header */
611b763d 1019 if (write(f, (char *) &fhdr, sizeof fhdr) != sizeof fhdr ||
34fe0a9b
EA
1020 write(f, (char *) &edata, (int) (fhdr.frzinfo.frzbrk - &edata)) !=
1021 (int) (fhdr.frzinfo.frzbrk - &edata))
7338e3d4 1022 {
2e15a2d8 1023 syserr("Cannot freeze %s", freezefile);
7338e3d4 1024 }
acae5a9d
EA
1025
1026 /* fine, clean up */
1027 (void) close(f);
1028}
1029\f/*
1030** THAW -- read in the frozen configuration file.
1031**
1032** Parameters:
8fe4fb9b 1033** freezefile -- the name of the file to thaw from.
acae5a9d
EA
1034**
1035** Returns:
1036** TRUE if it successfully read the freeze file.
1037** FALSE otherwise.
1038**
1039** Side Effects:
8fe4fb9b 1040** reads freezefile in to BSS area.
acae5a9d
EA
1041*/
1042
8fe4fb9b
EA
1043thaw(freezefile)
1044 char *freezefile;
acae5a9d
EA
1045{
1046 int f;
ccfed53c 1047 register char *p;
7338e3d4 1048 union frz fhdr;
ccfed53c 1049 char hbuf[60];
a59237a4 1050 extern char edata, end;
912acb74 1051 extern char Version[];
17a67c62 1052 extern caddr_t brk();
ccfed53c
EA
1053 extern char **myhostname();
1054 extern char *macvalue();
acae5a9d 1055
8fe4fb9b 1056 if (freezefile == NULL)
acae5a9d
EA
1057 return (FALSE);
1058
1059 /* open the freeze file */
8fe4fb9b 1060 f = open(freezefile, 0);
acae5a9d
EA
1061 if (f < 0)
1062 {
1063 errno = 0;
1064 return (FALSE);
1065 }
1066
1067 /* read in the header */
07adc4f5
RA
1068 if (read(f, (char *) &fhdr, sizeof fhdr) < sizeof fhdr)
1069 {
db281fbf 1070 syslog(LOG_WARNING, "Cannot read frozen config file");
07adc4f5
RA
1071 (void) close(f);
1072 return (FALSE);
1073 }
db281fbf 1074 if (fhdr.frzinfo.frzedata != &edata ||
2e3062fe 1075 fhdr.frzinfo.frzend != &end ||
7338e3d4 1076 strcmp(fhdr.frzinfo.frzver, Version) != 0)
acae5a9d 1077 {
07adc4f5 1078 syslog(LOG_WARNING, "Wrong version of frozen config file");
acae5a9d
EA
1079 (void) close(f);
1080 return (FALSE);
1081 }
1082
1083 /* arrange to have enough space */
17a67c62 1084 if (brk(fhdr.frzinfo.frzbrk) == (caddr_t) -1)
acae5a9d 1085 {
7338e3d4 1086 syserr("Cannot break to %x", fhdr.frzinfo.frzbrk);
acae5a9d
EA
1087 (void) close(f);
1088 return (FALSE);
1089 }
1090
1091 /* now read in the freeze file */
34fe0a9b
EA
1092 if (read(f, (char *) &edata, (int) (fhdr.frzinfo.frzbrk - &edata)) !=
1093 (int) (fhdr.frzinfo.frzbrk - &edata))
acae5a9d 1094 {
07adc4f5 1095 syserr("Cannot read frozen config file");
acae5a9d 1096 /* oops! we have trashed memory..... */
0e306e7f 1097 (void) write(2, "Cannot read freeze file\n", 24);
7338e3d4 1098 _exit(EX_SOFTWARE);
acae5a9d
EA
1099 }
1100
1101 (void) close(f);
ccfed53c
EA
1102
1103 /* verify that the host name was correct on the freeze */
1104 (void) myhostname(hbuf, sizeof hbuf);
1105 p = macvalue('w', CurEnv);
1106 if (p == NULL)
1107 p = "";
1108 if (strcmp(hbuf, macvalue('w', CurEnv)) == 0)
1109 return (TRUE);
1110 syslog(LOG_WARNING, "Hostname changed since freeze (%s => %s)",
1111 p, hbuf);
1112 return (FALSE);
acae5a9d 1113}
813d8709
EA
1114\f/*
1115** DISCONNECT -- remove our connection with any foreground process
1116**
1117** Parameters:
d188728a
EA
1118** fulldrop -- if set, we should also drop the controlling
1119** TTY if possible -- this should only be done when
1120** setting up the daemon since otherwise UUCP can
1121** leave us trying to open a dialin, and we will
1122** wait for the carrier.
813d8709
EA
1123**
1124** Returns:
1125** none
1126**
1127** Side Effects:
1128** Trys to insure that we are immune to vagaries of
1129** the controlling tty.
1130*/
1131
d188728a
EA
1132disconnect(fulldrop)
1133 bool fulldrop;
813d8709
EA
1134{
1135 int fd;
1136
e6f08ab1 1137 if (tTd(52, 1))
b9accadd
EA
1138 printf("disconnect: In %d Out %d\n", fileno(InChannel),
1139 fileno(OutChannel));
e6f08ab1 1140 if (tTd(52, 5))
813d8709 1141 {
e6f08ab1
EA
1142 printf("don't\n");
1143 return;
813d8709 1144 }
813d8709
EA
1145
1146 /* be sure we don't get nasty signals */
0e306e7f
EA
1147 (void) signal(SIGHUP, SIG_IGN);
1148 (void) signal(SIGINT, SIG_IGN);
1149 (void) signal(SIGQUIT, SIG_IGN);
813d8709
EA
1150
1151 /* we can't communicate with our caller, so.... */
7338e3d4
EA
1152 HoldErrs = TRUE;
1153 ErrorMode = EM_MAIL;
813d8709
EA
1154 Verbose = FALSE;
1155
1156 /* all input from /dev/null */
813d8709 1157 if (InChannel != stdin)
b9accadd
EA
1158 {
1159 (void) fclose(InChannel);
1160 InChannel = stdin;
1161 }
1162 (void) freopen("/dev/null", "r", stdin);
813d8709
EA
1163
1164 /* output to the transcript */
b9accadd 1165 if (OutChannel != stdout)
813d8709 1166 {
b9accadd
EA
1167 (void) fclose(OutChannel);
1168 OutChannel = stdout;
813d8709 1169 }
912acb74
EA
1170 if (CurEnv->e_xfp == NULL)
1171 CurEnv->e_xfp = fopen("/dev/null", "w");
b9accadd
EA
1172 (void) fflush(stdout);
1173 (void) close(1);
1174 (void) close(2);
912acb74 1175 while ((fd = dup(fileno(CurEnv->e_xfp))) < 2 && fd > 0)
b9accadd 1176 continue;
813d8709 1177
e6f08ab1 1178 /* drop our controlling TTY completely if possible */
d188728a 1179 if (fulldrop)
e6f08ab1 1180 {
afe907a4
MK
1181#if BSD > 43
1182 daemon(1, 1);
1183#else
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
MK
1192#endif /* TIOCNOTTY */
1193#endif /* BSD */
70faa7c8 1194 errno = 0;
e6f08ab1 1195 }
e6f08ab1 1196
813d8709
EA
1197# ifdef LOG
1198 if (LogLevel > 11)
1199 syslog(LOG_DEBUG, "in background, pid=%d", getpid());
1200# endif LOG
1201
1202 errno = 0;
1203}