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