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