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