minor cleanups for cray
[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
f90b4150 26static char sccsid[] = "@(#)main.c 5.24 (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 303 case MD_DAEMON:
908dc8db
MK
304# ifdef DAEMON
305 if (getuid() != 0) {
306 usrerr("Permission denied");
307 exit (EX_USAGE);
308 }
309 (void) unsetenv("HOSTALIASES");
310# else
2e15a2d8
MK
311 usrerr("Daemon mode not implemented");
312 ExitStat = EX_USAGE;
75f95954
EA
313 break;
314# endif DAEMON
315 case MD_SMTP:
316# ifndef SMTP
2e15a2d8
MK
317 usrerr("I don't speak SMTP");
318 ExitStat = EX_USAGE;
75f95954 319 break;
c1e24818 320# endif SMTP
75f95954
EA
321 case MD_ARPAFTP:
322 case MD_DELIVER:
323 case MD_VERIFY:
324 case MD_TEST:
325 case MD_INITALIAS:
326 case MD_PRINT:
327 case MD_FREEZE:
328 OpMode = p[2];
329 break;
330
331 default:
2e15a2d8
MK
332 usrerr("Invalid operation mode %c", p[2]);
333 ExitStat = EX_USAGE;
75f95954 334 break;
c1e24818
EA
335 }
336 break;
337
560a80d9 338 case 'C': /* select configuration file (already done) */
c1e24818 339 break;
a691a4a6 340
137c2d1f 341#ifdef DEBUG
43e0af62 342 case 'd': /* debugging -- redo in case frozen */
c1e24818
EA
343 tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
344 tTflag(&p[2]);
345 setbuf(stdout, (char *) NULL);
a29056d8 346#ifdef NAMED_BIND
137c2d1f 347 _res.options |= RES_DEBUG;
a29056d8 348#endif
c1e24818 349 break;
137c2d1f 350#endif
b3cbe40f 351
b3cbe40f 352 case 'f': /* from address */
c1e24818 353 case 'r': /* obsolete -f flag */
b3cbe40f 354 p += 2;
3110074f 355 if (*p == '\0' && ((p = *++av) == NULL || *p == '-'))
b3cbe40f 356 {
acae5a9d 357 p = *++av;
3110074f 358 if (p == NULL || *p == '-')
b3cbe40f 359 {
2e15a2d8
MK
360 usrerr("No \"from\" person");
361 ExitStat = EX_USAGE;
acae5a9d 362 av--;
b3cbe40f
EA
363 break;
364 }
365 }
22659072 366 if (from != NULL)
b3cbe40f 367 {
2e15a2d8
MK
368 usrerr("More than one \"from\" person");
369 ExitStat = EX_USAGE;
b3cbe40f
EA
370 break;
371 }
2e3062fe 372 from = newstr(p);
b3cbe40f
EA
373 break;
374
6da7b890 375 case 'F': /* set full name */
74c5fe7c 376 p += 2;
3110074f 377 if (*p == '\0' && ((p = *++av) == NULL || *p == '-'))
74c5fe7c 378 {
2e15a2d8
MK
379 usrerr("Bad -F flag");
380 ExitStat = EX_USAGE;
3110074f
EA
381 av--;
382 break;
74c5fe7c 383 }
2e3062fe 384 FullName = newstr(p);
6da7b890
EA
385 break;
386
b3cbe40f
EA
387 case 'h': /* hop count */
388 p += 2;
3110074f 389 if (*p == '\0' && ((p = *++av) == NULL || !isdigit(*p)))
b3cbe40f 390 {
2e15a2d8
MK
391 usrerr("Bad hop count (%s)", p);
392 ExitStat = EX_USAGE;
3110074f
EA
393 av--;
394 break;
b3cbe40f 395 }
7338e3d4 396 CurEnv->e_hopcount = atoi(p);
b3cbe40f 397 break;
b3cbe40f 398
c1e24818
EA
399 case 'n': /* don't alias */
400 NoAlias = TRUE;
d59b067a
EA
401 break;
402
c1e24818
EA
403 case 'o': /* set option */
404 setoption(p[2], &p[3], FALSE, TRUE);
14a39063 405 break;
cbdb7357 406
ed45aae1 407 case 'q': /* run queue files at intervals */
884a20cb 408# ifdef QUEUE
908dc8db
MK
409 if (getuid() != 0) {
410 usrerr("Permission denied");
411 exit (EX_USAGE);
412 }
413 (void) unsetenv("HOSTALIASES");
aba51985 414 queuemode = TRUE;
25b9d645 415 QueueIntvl = convtime(&p[2]);
884a20cb 416# else QUEUE
2e15a2d8
MK
417 usrerr("I don't know about queues");
418 ExitStat = EX_USAGE;
884a20cb 419# endif QUEUE
ed45aae1
EA
420 break;
421
c1e24818
EA
422 case 't': /* read recipients from message */
423 GrabTo = TRUE;
424 break;
425
426 /* compatibility flags */
c1e24818
EA
427 case 'c': /* connect to non-local mailers */
428 case 'e': /* error message disposition */
429 case 'i': /* don't let dot stop me */
430 case 'm': /* send to me too */
431 case 'T': /* set timeout interval */
432 case 'v': /* give blow-by-blow description */
433 setoption(p[1], &p[2], FALSE, TRUE);
35cc3fad
EA
434 break;
435
c1e24818
EA
436 case 's': /* save From lines in headers */
437 setoption('f', &p[2], FALSE, TRUE);
b3cbe40f 438 break;
26a3626c
EA
439
440# ifdef DBM
441 case 'I': /* initialize alias DBM file */
75f95954 442 OpMode = MD_INITALIAS;
26a3626c
EA
443 break;
444# endif DBM
b3cbe40f
EA
445 }
446 }
447
9e3c0a28 448 /*
2cce0c26
EA
449 ** Do basic initialization.
450 ** Read system control file.
179c1218 451 ** Extract special fields for local use.
9e3c0a28
EA
452 */
453
46f6ec52
EA
454 if (OpMode == MD_FREEZE || readconfig)
455 readcf(ConfFile);
22659072 456
75f95954 457 switch (OpMode)
acae5a9d 458 {
26a3626c 459 case MD_FREEZE:
a9621daf 460 /* this is critical to avoid forgeries of the frozen config */
0e306e7f
EA
461 (void) setgid(getgid());
462 (void) setuid(getuid());
a9621daf
EA
463
464 /* freeze the configuration */
8fe4fb9b 465 freeze(FreezeFile);
acae5a9d 466 exit(EX_OK);
26a3626c
EA
467
468 case MD_INITALIAS:
469 Verbose = TRUE;
470 break;
acae5a9d 471 }
179c1218 472
6130649c
EA
473 /* do heuristic mode adjustment */
474 if (Verbose)
75f95954
EA
475 {
476 /* turn off noconnect option */
477 setoption('c', "F", TRUE, FALSE);
478
479 /* turn on interactive delivery */
480 setoption('d', "", TRUE, FALSE);
481 }
6130649c 482
179c1218 483 /* our name for SMTP codes */
a73ae8ac 484 expand("\001j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
57c97d4a 485 MyHostName = jbuf;
d6a28dd8 486
179c1218
EA
487 /* the indices of local and program mailers */
488 st = stab("local", ST_MAILER, ST_FIND);
489 if (st == NULL)
490 syserr("No local mailer defined");
491 else
492 LocalMailer = st->s_mailer;
493 st = stab("prog", ST_MAILER, ST_FIND);
494 if (st == NULL)
495 syserr("No prog mailer defined");
496 else
497 ProgMailer = st->s_mailer;
498
6bbaf971
EA
499 /* operate in queue directory */
500 if (chdir(QueueDir) < 0)
501 {
502 syserr("cannot chdir(%s)", QueueDir);
503 exit(EX_SOFTWARE);
504 }
505
64912e7e 506 /*
55f0da62 507 ** Do operation-mode-dependent initialization.
64912e7e
EA
508 */
509
55f0da62 510 switch (OpMode)
64912e7e 511 {
55f0da62
EA
512 case MD_PRINT:
513 /* print the queue */
74f37936 514#ifdef QUEUE
64912e7e
EA
515 dropenvelope(CurEnv);
516 printqueue();
517 exit(EX_OK);
74f37936
EA
518#else QUEUE
519 usrerr("No queue to print");
520 finis();
521#endif QUEUE
8acb5142 522
55f0da62
EA
523 case MD_INITALIAS:
524 /* initialize alias database */
525 initaliases(AliasFile, TRUE);
f4dbf345 526 exit(EX_OK);
cdb17311 527
55f0da62
EA
528 case MD_DAEMON:
529 /* don't open alias database -- done in srvrsmtp */
530 break;
531
532 default:
533 /* open the alias database */
534 initaliases(AliasFile, FALSE);
535 break;
536 }
537
9c6d4c70 538# ifdef DEBUG
9678c96d 539 if (tTd(0, 15))
9c6d4c70 540 {
f6a0cc15 541 /* print configuration table (or at least part of it) */
9c6d4c70
EA
542 printrules();
543 for (i = 0; i < MAXMAILERS; i++)
544 {
545 register struct mailer *m = Mailer[i];
1dbda134 546 int j;
9c6d4c70
EA
547
548 if (m == NULL)
549 continue;
97ad25b6
EA
550 printf("mailer %d (%s): P=%s S=%d R=%d M=%ld F=", i, m->m_name,
551 m->m_mailer, m->m_s_rwset, m->m_r_rwset,
552 m->m_maxsize);
1dbda134
EA
553 for (j = '\0'; j <= '\177'; j++)
554 if (bitnset(j, m->m_flags))
0e306e7f 555 (void) putchar(j);
1dbda134 556 printf(" E=");
b3ef02a2
EA
557 xputs(m->m_eol);
558 printf("\n");
9c6d4c70
EA
559 }
560 }
561# endif DEBUG
562
be2fcca9
EA
563 /*
564 ** Switch to the main envelope.
565 */
566
567 CurEnv = newenvelope(&MainEnvelope);
e6f08ab1 568 MainEnvelope.e_flags = BlankEnvelope.e_flags;
be2fcca9 569
cf69a203
EA
570 /*
571 ** If test mode, read addresses from stdin and process.
572 */
573
75f95954 574 if (OpMode == MD_TEST)
cf69a203
EA
575 {
576 char buf[MAXLINE];
577
50435450 578 printf("ADDRESS TEST MODE\nEnter <ruleset> <address>\n");
cf69a203
EA
579 for (;;)
580 {
581 register char **pvp;
50435450 582 char *q;
50435450 583 extern char *DelimChar;
cf69a203
EA
584
585 printf("> ");
0e306e7f 586 (void) fflush(stdout);
cf69a203
EA
587 if (fgets(buf, sizeof buf, stdin) == NULL)
588 finis();
f90b4150 589 for (p = buf; isspace(*p); p++)
cf69a203 590 continue;
ecfd2c8e
EA
591 q = p;
592 while (*p != '\0' && !isspace(*p))
593 p++;
cf69a203
EA
594 if (*p == '\0')
595 continue;
50435450
EA
596 *p = '\0';
597 do
ecfd2c8e 598 {
217a0102
EA
599 extern char **prescan();
600 char pvpbuf[PSBUFSIZE];
601
602 pvp = prescan(++p, ',', pvpbuf);
50435450 603 if (pvp == NULL)
ecfd2c8e 604 continue;
50435450
EA
605 rewrite(pvp, 3);
606 p = q;
607 while (*p != '\0')
608 {
609 rewrite(pvp, atoi(p));
610 while (*p != '\0' && *p++ != ',')
611 continue;
612 }
613 } while (*(p = DelimChar) != '\0');
cf69a203
EA
614 }
615 }
616
e3cd595c
EA
617# ifdef QUEUE
618 /*
619 ** If collecting stuff from the queue, go start doing that.
620 */
621
7b21425b 622 if (queuemode && OpMode != MD_DAEMON && QueueIntvl == 0)
e3cd595c
EA
623 {
624 runqueue(FALSE);
625 finis();
626 }
627# endif QUEUE
628
f6a0cc15
EA
629 /*
630 ** If a daemon, wait for a request.
631 ** getrequests will always return in a child.
25b9d645 632 ** If we should also be processing the queue, start
19147b2d
EA
633 ** doing it in background.
634 ** We check for any errors that might have happened
635 ** during startup.
f6a0cc15
EA
636 */
637
75f95954 638 if (OpMode == MD_DAEMON || QueueIntvl != 0)
25b9d645 639 {
9678c96d 640 if (!tTd(0, 1))
58b27aa4 641 {
fcdf8200 642 /* put us in background */
58b27aa4
EA
643 i = fork();
644 if (i < 0)
645 syserr("daemon: cannot fork");
646 if (i != 0)
647 exit(0);
fcdf8200
EA
648
649 /* get our pid right */
d9162460 650 MotherPid = getpid();
fcdf8200
EA
651
652 /* disconnect from our controlling tty */
d188728a 653 disconnect(TRUE);
58b27aa4 654 }
7338e3d4 655
25b9d645
EA
656# ifdef QUEUE
657 if (queuemode)
f309127e 658 {
25b9d645 659 runqueue(TRUE);
75f95954 660 if (OpMode != MD_DAEMON)
f309127e
EA
661 for (;;)
662 pause();
663 }
25b9d645 664# endif QUEUE
7338e3d4
EA
665 dropenvelope(CurEnv);
666
667#ifdef DAEMON
f6a0cc15 668 getrequests();
2a16bae3
EA
669
670 /* at this point we are in a child: reset state */
75f95954 671 OpMode = MD_SMTP;
7338e3d4 672 (void) newenvelope(CurEnv);
912acb74 673 openxscript(CurEnv);
14a39063 674#endif DAEMON
7338e3d4 675 }
88039044
EA
676
677# ifdef SMTP
678 /*
679 ** If running SMTP protocol, start collecting and executing
680 ** commands. This will never return.
681 */
682
75f95954 683 if (OpMode == MD_SMTP)
88039044
EA
684 smtp();
685# endif SMTP
686
f6a0cc15 687 /*
e6f08ab1 688 ** Do basic system initialization and set the sender
f6a0cc15
EA
689 */
690
e6f08ab1 691 initsys();
cbdb7357 692 setsender(from);
a9e0e597 693
3110074f 694 if (OpMode != MD_ARPAFTP && *av == NULL && !GrabTo)
e863b1fa 695 {
2e3062fe
EA
696 usrerr("Recipient names must be specified");
697
698 /* collect body for UUCP return */
699 if (OpMode != MD_VERIFY)
700 collect(FALSE);
e863b1fa
EA
701 finis();
702 }
75f95954
EA
703 if (OpMode == MD_VERIFY)
704 SendMode = SM_VERIFY;
b3cbe40f 705
b3cbe40f 706 /*
d6b27179 707 ** Scan argv and deliver the message to everyone.
b3cbe40f
EA
708 */
709
acae5a9d 710 sendtoargv(av);
b3cbe40f 711
72e9b3cc 712 /* if we have had errors sofar, arrange a meaningful exit stat */
d916f0ca 713 if (Errors > 0 && ExitStat == EX_OK)
a4b004a6 714 ExitStat = EX_USAGE;
a4b004a6 715
dc39c568
EA
716 /*
717 ** Read the input mail.
718 */
719
2654b031 720 CurEnv->e_to = NULL;
75f95954 721 if (OpMode != MD_VERIFY || GrabTo)
49086753 722 collect(FALSE);
d829793b 723 errno = 0;
35cc3fad 724
4e1f4d4b 725 /* collect statistics */
7338e3d4
EA
726 if (OpMode != MD_VERIFY)
727 markstats(CurEnv, (ADDRESS *) NULL);
b3cbe40f 728
d6b27179 729# ifdef DEBUG
9678c96d 730 if (tTd(1, 1))
2654b031 731 printf("From person = \"%s\"\n", CurEnv->e_from.q_paddr);
d6b27179
EA
732# endif DEBUG
733
b3cbe40f
EA
734 /*
735 ** Actually send everything.
d6b27179 736 ** If verifying, just ack.
b3cbe40f
EA
737 */
738
7338e3d4
EA
739 CurEnv->e_from.q_flags |= QDONTSEND;
740 CurEnv->e_to = NULL;
f7e74083 741 sendall(CurEnv, SM_DEFAULT);
b3cbe40f
EA
742
743 /*
744 ** All done.
745 */
746
747 finis();
748}
749\f/*
750** FINIS -- Clean up and exit.
751**
b3cbe40f
EA
752** Parameters:
753** none
754**
755** Returns:
756** never
757**
758** Side Effects:
96faada8 759** exits sendmail
b3cbe40f
EA
760*/
761
762finis()
763{
aba51985 764# ifdef DEBUG
9678c96d 765 if (tTd(2, 1))
e6f08ab1 766 printf("\n====finis: stat %d e_flags %o\n", ExitStat, CurEnv->e_flags);
aba51985
EA
767# endif DEBUG
768
7338e3d4 769 /* clean up temp files */
912acb74 770 CurEnv->e_to = NULL;
e6f08ab1 771 dropenvelope(CurEnv);
b3cbe40f 772
7338e3d4
EA
773 /* post statistics */
774 poststats(StatFile);
68f0b54c 775
7338e3d4 776 /* and exit */
36a4e219
EA
777# ifdef LOG
778 if (LogLevel > 11)
779 syslog(LOG_DEBUG, "finis, pid=%d", getpid());
780# endif LOG
c8ec8736
EA
781 if (ExitStat == EX_TEMPFAIL)
782 ExitStat = EX_OK;
b3cbe40f
EA
783 exit(ExitStat);
784}
785\f/*
6e2f38be
EA
786** INTSIG -- clean up on interrupt
787**
7338e3d4
EA
788** This just arranges to exit. It pessimises in that it
789** may resend a message.
6e2f38be
EA
790**
791** Parameters:
792** none.
793**
794** Returns:
795** none.
796**
797** Side Effects:
7338e3d4 798** Unlocks the current job.
6e2f38be
EA
799*/
800
801intsig()
802{
7338e3d4
EA
803 FileName = NULL;
804 unlockqueue(CurEnv);
805 exit(EX_OK);
6e2f38be
EA
806}
807\f/*
721fad23
EA
808** INITMACROS -- initialize the macro system
809**
810** This just involves defining some macros that are actually
811** used internally as metasymbols to be themselves.
812**
813** Parameters:
814** none.
815**
816** Returns:
817** none.
818**
819** Side Effects:
820** initializes several macros to be themselves.
821*/
822
9dbc8d99 823struct metamac
721fad23 824{
9dbc8d99
EA
825 char metaname;
826 char metaval;
827};
721fad23 828
9dbc8d99
EA
829struct metamac MetaMacros[] =
830{
eca244ca 831 /* LHS pattern matching characters */
2018e82d 832 '*', MATCHZANY, '+', MATCHANY, '-', MATCHONE, '=', MATCHCLASS,
9f39d7cd 833 '~', MATCHNCLASS,
721fad23
EA
834
835 /* these are RHS metasymbols */
2018e82d 836 '#', CANONNET, '@', CANONHOST, ':', CANONUSER, '>', CALLSUBR,
41173b8f 837 '{', MATCHLOOKUP, '}', MATCHELOOKUP,
721fad23 838
eca244ca 839 /* the conditional operations */
2018e82d 840 '?', CONDIF, '|', CONDELSE, '.', CONDFI,
9dbc8d99 841
eca244ca 842 /* and finally the hostname lookup characters */
217a0102 843 '[', HOSTBEGIN, ']', HOSTEND,
eca244ca 844
9dbc8d99 845 '\0'
721fad23
EA
846};
847
848initmacros()
849{
9dbc8d99
EA
850 register struct metamac *m;
851 char buf[5];
852 register int c;
721fad23 853
9dbc8d99
EA
854 for (m = MetaMacros; m->metaname != '\0'; m++)
855 {
856 buf[0] = m->metaval;
857 buf[1] = '\0';
7338e3d4 858 define(m->metaname, newstr(buf), CurEnv);
9dbc8d99
EA
859 }
860 buf[0] = MATCHREPL;
861 buf[2] = '\0';
862 for (c = '0'; c <= '9'; c++)
863 {
864 buf[1] = c;
7338e3d4 865 define(c, newstr(buf), CurEnv);
9dbc8d99 866 }
721fad23 867}
dd1fe05b 868\f/*
acae5a9d
EA
869** FREEZE -- freeze BSS & allocated memory
870**
871** This will be used to efficiently load the configuration file.
872**
873** Parameters:
8fe4fb9b 874** freezefile -- the name of the file to freeze to.
acae5a9d
EA
875**
876** Returns:
877** none.
878**
879** Side Effects:
8fe4fb9b 880** Writes BSS and malloc'ed memory to freezefile
acae5a9d
EA
881*/
882
7338e3d4 883union frz
acae5a9d 884{
7338e3d4
EA
885 char frzpad[BUFSIZ]; /* insure we are on a BUFSIZ boundary */
886 struct
887 {
888 time_t frzstamp; /* timestamp on this freeze */
889 char *frzbrk; /* the current break */
2e3062fe
EA
890 char *frzedata; /* address of edata */
891 char *frzend; /* address of end */
7338e3d4
EA
892 char frzver[252]; /* sendmail version */
893 } frzinfo;
acae5a9d
EA
894};
895
8fe4fb9b
EA
896freeze(freezefile)
897 char *freezefile;
acae5a9d
EA
898{
899 int f;
7338e3d4 900 union frz fhdr;
2e3062fe 901 extern char edata, end;
acae5a9d 902 extern char *sbrk();
912acb74 903 extern char Version[];
acae5a9d 904
8fe4fb9b 905 if (freezefile == NULL)
acae5a9d
EA
906 return;
907
908 /* try to open the freeze file */
8fe4fb9b 909 f = creat(freezefile, FileMode);
acae5a9d
EA
910 if (f < 0)
911 {
2e15a2d8 912 syserr("Cannot freeze %s", freezefile);
acae5a9d
EA
913 errno = 0;
914 return;
915 }
916
917 /* build the freeze header */
7338e3d4
EA
918 fhdr.frzinfo.frzstamp = curtime();
919 fhdr.frzinfo.frzbrk = sbrk(0);
2e3062fe
EA
920 fhdr.frzinfo.frzedata = &edata;
921 fhdr.frzinfo.frzend = &end;
0e306e7f 922 (void) strcpy(fhdr.frzinfo.frzver, Version);
acae5a9d
EA
923
924 /* write out the freeze header */
611b763d 925 if (write(f, (char *) &fhdr, sizeof fhdr) != sizeof fhdr ||
34fe0a9b
EA
926 write(f, (char *) &edata, (int) (fhdr.frzinfo.frzbrk - &edata)) !=
927 (int) (fhdr.frzinfo.frzbrk - &edata))
7338e3d4 928 {
2e15a2d8 929 syserr("Cannot freeze %s", freezefile);
7338e3d4 930 }
acae5a9d
EA
931
932 /* fine, clean up */
933 (void) close(f);
934}
935\f/*
936** THAW -- read in the frozen configuration file.
937**
938** Parameters:
8fe4fb9b 939** freezefile -- the name of the file to thaw from.
acae5a9d
EA
940**
941** Returns:
942** TRUE if it successfully read the freeze file.
943** FALSE otherwise.
944**
945** Side Effects:
8fe4fb9b 946** reads freezefile in to BSS area.
acae5a9d
EA
947*/
948
8fe4fb9b
EA
949thaw(freezefile)
950 char *freezefile;
acae5a9d
EA
951{
952 int f;
7338e3d4 953 union frz fhdr;
a59237a4 954 extern char edata, end;
912acb74 955 extern char Version[];
17a67c62 956 extern caddr_t brk();
acae5a9d 957
8fe4fb9b 958 if (freezefile == NULL)
acae5a9d
EA
959 return (FALSE);
960
961 /* open the freeze file */
8fe4fb9b 962 f = open(freezefile, 0);
acae5a9d
EA
963 if (f < 0)
964 {
965 errno = 0;
966 return (FALSE);
967 }
968
969 /* read in the header */
611b763d 970 if (read(f, (char *) &fhdr, sizeof fhdr) < sizeof fhdr ||
2e3062fe
EA
971 fhdr.frzinfo.frzedata != &edata ||
972 fhdr.frzinfo.frzend != &end ||
7338e3d4 973 strcmp(fhdr.frzinfo.frzver, Version) != 0)
acae5a9d
EA
974 {
975 (void) close(f);
976 return (FALSE);
977 }
978
979 /* arrange to have enough space */
17a67c62 980 if (brk(fhdr.frzinfo.frzbrk) == (caddr_t) -1)
acae5a9d 981 {
7338e3d4 982 syserr("Cannot break to %x", fhdr.frzinfo.frzbrk);
acae5a9d
EA
983 (void) close(f);
984 return (FALSE);
985 }
986
987 /* now read in the freeze file */
34fe0a9b
EA
988 if (read(f, (char *) &edata, (int) (fhdr.frzinfo.frzbrk - &edata)) !=
989 (int) (fhdr.frzinfo.frzbrk - &edata))
acae5a9d
EA
990 {
991 /* oops! we have trashed memory..... */
0e306e7f 992 (void) write(2, "Cannot read freeze file\n", 24);
7338e3d4 993 _exit(EX_SOFTWARE);
acae5a9d
EA
994 }
995
996 (void) close(f);
997 return (TRUE);
998}
813d8709
EA
999\f/*
1000** DISCONNECT -- remove our connection with any foreground process
1001**
1002** Parameters:
d188728a
EA
1003** fulldrop -- if set, we should also drop the controlling
1004** TTY if possible -- this should only be done when
1005** setting up the daemon since otherwise UUCP can
1006** leave us trying to open a dialin, and we will
1007** wait for the carrier.
813d8709
EA
1008**
1009** Returns:
1010** none
1011**
1012** Side Effects:
1013** Trys to insure that we are immune to vagaries of
1014** the controlling tty.
1015*/
1016
d188728a
EA
1017disconnect(fulldrop)
1018 bool fulldrop;
813d8709
EA
1019{
1020 int fd;
1021
e6f08ab1
EA
1022#ifdef DEBUG
1023 if (tTd(52, 1))
b9accadd
EA
1024 printf("disconnect: In %d Out %d\n", fileno(InChannel),
1025 fileno(OutChannel));
e6f08ab1 1026 if (tTd(52, 5))
813d8709 1027 {
e6f08ab1
EA
1028 printf("don't\n");
1029 return;
813d8709 1030 }
e6f08ab1 1031#endif DEBUG
813d8709
EA
1032
1033 /* be sure we don't get nasty signals */
0e306e7f
EA
1034 (void) signal(SIGHUP, SIG_IGN);
1035 (void) signal(SIGINT, SIG_IGN);
1036 (void) signal(SIGQUIT, SIG_IGN);
813d8709
EA
1037
1038 /* we can't communicate with our caller, so.... */
7338e3d4
EA
1039 HoldErrs = TRUE;
1040 ErrorMode = EM_MAIL;
813d8709
EA
1041 Verbose = FALSE;
1042
1043 /* all input from /dev/null */
813d8709 1044 if (InChannel != stdin)
b9accadd
EA
1045 {
1046 (void) fclose(InChannel);
1047 InChannel = stdin;
1048 }
1049 (void) freopen("/dev/null", "r", stdin);
813d8709
EA
1050
1051 /* output to the transcript */
b9accadd 1052 if (OutChannel != stdout)
813d8709 1053 {
b9accadd
EA
1054 (void) fclose(OutChannel);
1055 OutChannel = stdout;
813d8709 1056 }
912acb74
EA
1057 if (CurEnv->e_xfp == NULL)
1058 CurEnv->e_xfp = fopen("/dev/null", "w");
b9accadd
EA
1059 (void) fflush(stdout);
1060 (void) close(1);
1061 (void) close(2);
912acb74 1062 while ((fd = dup(fileno(CurEnv->e_xfp))) < 2 && fd > 0)
b9accadd 1063 continue;
813d8709 1064
e6f08ab1
EA
1065#ifdef TIOCNOTTY
1066 /* drop our controlling TTY completely if possible */
d188728a 1067 if (fulldrop)
e6f08ab1 1068 {
d188728a
EA
1069 fd = open("/dev/tty", 2);
1070 if (fd >= 0)
1071 {
17a67c62 1072 (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
d188728a
EA
1073 (void) close(fd);
1074 }
17a67c62 1075 (void) setpgrp(0, 0);
70faa7c8 1076 errno = 0;
e6f08ab1
EA
1077 }
1078#endif TIOCNOTTY
1079
813d8709
EA
1080# ifdef LOG
1081 if (LogLevel > 11)
1082 syslog(LOG_DEBUG, "in background, pid=%d", getpid());
1083# endif LOG
1084
1085 errno = 0;
1086}