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