from Jan Pendry
[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 *
417f7a11 6 * %sccs.include.redist.c%
bee79b64 7 */
aeb2545d
DF
8
9#ifndef lint
10char copyright[] =
bee79b64 11"@(#) Copyright (c) 1988 Regents of the University of California.\n\
aeb2545d 12 All rights reserved.\n";
bee79b64 13#endif /* not lint */
aeb2545d
DF
14
15#ifndef lint
afe907a4 16static char sccsid[] = "@(#)main.c 5.30 (Berkeley) %G%";
bee79b64 17#endif /* not lint */
aeb2545d 18
137c2d1f
KB
19#define _DEFINE
20
afe907a4 21#include <sys/param.h>
5bec1980 22#include <sys/file.h>
137c2d1f
KB
23#include <signal.h>
24#include <sgtty.h>
25#include "sendmail.h"
26#include <arpa/nameser.h>
27#include <resolv.h>
b3cbe40f 28
17a67c62 29# ifdef lint
2e3062fe 30char edata, end;
17a67c62
EA
31# endif lint
32
b3cbe40f 33/*
96faada8 34** SENDMAIL -- Post mail to a set of destinations.
b3cbe40f
EA
35**
36** This is the basic mail router. All user mail programs should
96faada8 37** call this routine to actually deliver mail. Sendmail in
b3cbe40f
EA
38** turn calls a bunch of mail servers that do the real work of
39** delivering the mail.
40**
3199e4f3
EA
41** Sendmail is driven by tables read in from /usr/lib/sendmail.cf
42** (read by readcf.c). Some more static configuration info,
43** including some code that you may want to tailor for your
44** installation, is in conf.c. You may also want to touch
45** daemon.c (if you have some other IPC mechanism), acct.c
46** (to change your accounting), names.c (to adjust the name
47** server mechanism).
b3cbe40f
EA
48**
49** Usage:
7338e3d4 50** /usr/lib/sendmail [flags] addr ...
b3cbe40f 51**
7338e3d4 52** See the associated documentation for details.
b3cbe40f 53**
b3cbe40f 54** Author:
7338e3d4
EA
55** Eric Allman, UCB/INGRES (until 10/81)
56** Britton-Lee, Inc., purveyors of fine
57** database computers (from 11/81)
58** The support of the INGRES Project and Britton-Lee is
59** gratefully acknowledged. Britton-Lee in
60** particular had absolutely nothing to gain from
61** my involvement in this project.
b3cbe40f
EA
62*/
63
64
83f674d8 65int NextMailer; /* "free" index into Mailer struct */
912acb74 66char *FullName; /* sender's full name */
be2fcca9 67ENVELOPE BlankEnvelope; /* a "blank" envelope */
2654b031 68ENVELOPE MainEnvelope; /* the envelope around the basic letter */
2e3062fe
EA
69ADDRESS NullAddress = /* a null address */
70 { "", "", "" };
71
72/*
73** Pointers for setproctitle.
74** This allows "ps" listings to give more useful information.
75** These must be kept out of BSS for frozen configuration files
76** to work.
77*/
78
79# ifdef SETPROCTITLE
80char **Argv = NULL; /* pointer to argument vector */
81char *LastArgv = NULL; /* end of argv */
82# endif SETPROCTITLE
b3cbe40f 83
4aebfe5d
EA
84#ifdef DAEMON
85#ifndef SMTP
86ERROR %%%% Cannot have daemon mode without SMTP %%%% ERROR
87#endif SMTP
88#endif DAEMON
89
e36705df 90main(argc, argv, envp)
b3cbe40f
EA
91 int argc;
92 char **argv;
e36705df 93 char **envp;
b3cbe40f
EA
94{
95 register char *p;
acae5a9d 96 char **av;
17df0fcb 97 char *locname;
b3cbe40f 98 extern int finis();
b3cbe40f 99 extern char Version[];
b3cbe40f 100 char *from;
b3cbe40f 101 typedef int (*fnptr)();
be2fcca9 102 STAB *st;
9e3c0a28 103 register int i;
4388720d 104 bool readconfig = TRUE;
aba51985 105 bool queuemode = FALSE; /* process queue requests */
43e0af62 106 bool nothaw;
be2fcca9 107 static bool reenter = FALSE;
57c97d4a 108 char jbuf[30]; /* holds MyHostName */
f6a0cc15 109 extern bool safefile();
ed45aae1 110 extern time_t convtime();
dd1fe05b 111 extern putheader(), putbody();
be2fcca9 112 extern ENVELOPE *newenvelope();
6e2f38be 113 extern intsig();
1dbda134 114 extern char **myhostname();
22e6d6b8 115 extern char *arpadate();
e36705df 116 extern char **environ;
b72377c6 117
22659072
EA
118 /*
119 ** Check to see if we reentered.
120 ** This would normally happen if e_putheader or e_putbody
121 ** were NULL when invoked.
122 */
123
b72377c6
EA
124 if (reenter)
125 {
126 syserr("main: reentered!");
127 abort();
128 }
129 reenter = TRUE;
abae7b2d 130 extern ADDRESS *recipient();
17df0fcb 131 bool canrename;
b3cbe40f 132
60c0b7b2
KB
133 /* Enforce use of local time */
134 unsetenv("TZ");
135
d15bd559
EA
136 /*
137 ** Be sure we have enough file descriptors.
59b65d0f 138 ** But also be sure that 0, 1, & 2 are open.
d15bd559
EA
139 */
140
e1098041 141 i = open("/dev/null", O_RDWR);
59b65d0f
EA
142 while (i >= 0 && i < 2)
143 i = dup(i);
e1098041 144 for (i = getdtablesize(); i > 2; --i)
d15bd559
EA
145 (void) close(i);
146 errno = 0;
147
07adc4f5
RA
148#ifdef LOG_MAIL
149 openlog("sendmail", LOG_PID, LOG_MAIL);
150#else
151 openlog("sendmail", LOG_PID);
152#endif
153
8ff78b51
EA
154 /*
155 ** Set default values for variables.
156 ** These cannot be in initialized data space.
157 */
158
159 setdefaults();
160
bb09c502
EA
161 /* set up the blank envelope */
162 BlankEnvelope.e_puthdr = putheader;
163 BlankEnvelope.e_putbody = putbody;
164 BlankEnvelope.e_xfp = NULL;
2e3062fe 165 STRUCTCOPY(NullAddress, BlankEnvelope.e_from);
bd575ea9
EA
166 STRUCTCOPY(BlankEnvelope, MainEnvelope);
167 CurEnv = &MainEnvelope;
bb09c502 168
22659072
EA
169 /*
170 ** Do a quick prescan of the argument list.
171 ** We do this to find out if we can potentially thaw the
172 ** configuration file. If not, we do the thaw now so that
173 ** the argument processing applies to this run rather than
174 ** to the run that froze the configuration.
175 */
176
d6b27179 177 argv[argc] = NULL;
22659072 178 av = argv;
43e0af62 179 nothaw = FALSE;
560a80d9 180 while ((p = *++av) != NULL)
22659072 181 {
560a80d9
EA
182 if (strncmp(p, "-C", 2) == 0)
183 {
184 ConfFile = &p[2];
185 if (ConfFile[0] == '\0')
186 ConfFile = "sendmail.cf";
0e306e7f
EA
187 (void) setgid(getrgid());
188 (void) setuid(getruid());
43e0af62 189 nothaw = TRUE;
560a80d9
EA
190 }
191 else if (strncmp(p, "-bz", 3) == 0)
43e0af62 192 nothaw = TRUE;
43e0af62
EA
193 else if (strncmp(p, "-d", 2) == 0)
194 {
195 tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
196 tTflag(&p[2]);
197 setbuf(stdout, (char *) NULL);
198 printf("Version %s\n", Version);
199 }
22659072 200 }
2bc15c4e
KB
201
202 InChannel = stdin;
203 OutChannel = stdout;
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
b3cbe40f 226 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
6e2f38be 227 (void) signal(SIGINT, intsig);
eb211e2c 228 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
6e2f38be
EA
229 (void) signal(SIGHUP, intsig);
230 (void) signal(SIGTERM, intsig);
ed7382d3 231 (void) signal(SIGPIPE, SIG_IGN);
a0554f81 232 OldUmask = umask(0);
75f95954 233 OpMode = MD_DELIVER;
e673aad7 234 MotherPid = getpid();
561c7c50 235 FullName = getenv("NAME");
dd1fe05b 236
b3cbe40f
EA
237 errno = 0;
238 from = NULL;
378e8da7 239
4388720d 240 if (readconfig)
1dbda134 241 {
4388720d
EA
242 /* initialize some macros, etc. */
243 initmacros();
244
245 /* hostname */
246 av = myhostname(jbuf, sizeof jbuf);
247 if (jbuf[0] != '\0')
248 {
43e0af62
EA
249 if (tTd(0, 4))
250 printf("canonical name: %s\n", jbuf);
4388720d
EA
251 p = newstr(jbuf);
252 define('w', p, CurEnv);
253 setclass('w', p);
254 }
255 while (av != NULL && *av != NULL)
43e0af62 256 {
43e0af62
EA
257 if (tTd(0, 4))
258 printf("\ta.k.a.: %s\n", *av);
4388720d 259 setclass('w', *av++);
43e0af62 260 }
4388720d
EA
261
262 /* version */
263 define('v', Version, CurEnv);
1dbda134 264 }
378e8da7
EA
265
266 /* current time */
22e6d6b8 267 define('b', arpadate((char *) NULL), CurEnv);
378e8da7 268
a691a4a6 269 /*
c1e24818 270 ** Crack argv.
a691a4a6
EA
271 */
272
acae5a9d 273 av = argv;
26a3626c
EA
274 p = rindex(*av, '/');
275 if (p++ == NULL)
276 p = *av;
277 if (strcmp(p, "newaliases") == 0)
75f95954 278 OpMode = MD_INITALIAS;
26a3626c 279 else if (strcmp(p, "mailq") == 0)
75f95954 280 OpMode = MD_PRINT;
34fe0a9b
EA
281 else if (strcmp(p, "smtpd") == 0)
282 OpMode = MD_DAEMON;
3110074f 283 while ((p = *++av) != NULL && p[0] == '-')
a691a4a6 284 {
c1e24818 285 switch (p[1])
a691a4a6 286 {
75f95954
EA
287 case 'b': /* operations mode */
288 switch (p[2])
c1e24818 289 {
75f95954 290 case MD_DAEMON:
908dc8db
MK
291# ifdef DAEMON
292 if (getuid() != 0) {
293 usrerr("Permission denied");
294 exit (EX_USAGE);
295 }
296 (void) unsetenv("HOSTALIASES");
297# else
2e15a2d8
MK
298 usrerr("Daemon mode not implemented");
299 ExitStat = EX_USAGE;
75f95954
EA
300 break;
301# endif DAEMON
302 case MD_SMTP:
303# ifndef SMTP
2e15a2d8
MK
304 usrerr("I don't speak SMTP");
305 ExitStat = EX_USAGE;
75f95954 306 break;
c1e24818 307# endif SMTP
75f95954
EA
308 case MD_ARPAFTP:
309 case MD_DELIVER:
310 case MD_VERIFY:
311 case MD_TEST:
312 case MD_INITALIAS:
313 case MD_PRINT:
314 case MD_FREEZE:
315 OpMode = p[2];
316 break;
317
318 default:
2e15a2d8
MK
319 usrerr("Invalid operation mode %c", p[2]);
320 ExitStat = EX_USAGE;
75f95954 321 break;
c1e24818
EA
322 }
323 break;
324
560a80d9 325 case 'C': /* select configuration file (already done) */
c1e24818 326 break;
a691a4a6 327
43e0af62 328 case 'd': /* debugging -- redo in case frozen */
c1e24818
EA
329 tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
330 tTflag(&p[2]);
331 setbuf(stdout, (char *) NULL);
a29056d8 332#ifdef NAMED_BIND
137c2d1f 333 _res.options |= RES_DEBUG;
a29056d8 334#endif
c1e24818 335 break;
b3cbe40f 336
b3cbe40f 337 case 'f': /* from address */
c1e24818 338 case 'r': /* obsolete -f flag */
b3cbe40f 339 p += 2;
3110074f 340 if (*p == '\0' && ((p = *++av) == NULL || *p == '-'))
b3cbe40f 341 {
acae5a9d 342 p = *++av;
3110074f 343 if (p == NULL || *p == '-')
b3cbe40f 344 {
2e15a2d8
MK
345 usrerr("No \"from\" person");
346 ExitStat = EX_USAGE;
acae5a9d 347 av--;
b3cbe40f
EA
348 break;
349 }
350 }
22659072 351 if (from != NULL)
b3cbe40f 352 {
2e15a2d8
MK
353 usrerr("More than one \"from\" person");
354 ExitStat = EX_USAGE;
b3cbe40f
EA
355 break;
356 }
2e3062fe 357 from = newstr(p);
b3cbe40f
EA
358 break;
359
6da7b890 360 case 'F': /* set full name */
74c5fe7c 361 p += 2;
3110074f 362 if (*p == '\0' && ((p = *++av) == NULL || *p == '-'))
74c5fe7c 363 {
2e15a2d8
MK
364 usrerr("Bad -F flag");
365 ExitStat = EX_USAGE;
3110074f
EA
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 {
2e15a2d8
MK
376 usrerr("Bad hop count (%s)", p);
377 ExitStat = EX_USAGE;
3110074f
EA
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
908dc8db
MK
394 if (getuid() != 0) {
395 usrerr("Permission denied");
396 exit (EX_USAGE);
397 }
398 (void) unsetenv("HOSTALIASES");
aba51985 399 queuemode = TRUE;
25b9d645 400 QueueIntvl = convtime(&p[2]);
884a20cb 401# else QUEUE
2e15a2d8
MK
402 usrerr("I don't know about queues");
403 ExitStat = EX_USAGE;
884a20cb 404# endif QUEUE
ed45aae1
EA
405 break;
406
c1e24818
EA
407 case 't': /* read recipients from message */
408 GrabTo = TRUE;
409 break;
410
411 /* compatibility flags */
c1e24818
EA
412 case 'c': /* connect to non-local mailers */
413 case 'e': /* error message disposition */
414 case 'i': /* don't let dot stop me */
415 case 'm': /* send to me too */
416 case 'T': /* set timeout interval */
417 case 'v': /* give blow-by-blow description */
418 setoption(p[1], &p[2], FALSE, TRUE);
35cc3fad
EA
419 break;
420
c1e24818
EA
421 case 's': /* save From lines in headers */
422 setoption('f', &p[2], FALSE, TRUE);
b3cbe40f 423 break;
26a3626c
EA
424
425# ifdef DBM
426 case 'I': /* initialize alias DBM file */
75f95954 427 OpMode = MD_INITALIAS;
26a3626c
EA
428 break;
429# endif DBM
b3cbe40f
EA
430 }
431 }
432
9e3c0a28 433 /*
2cce0c26
EA
434 ** Do basic initialization.
435 ** Read system control file.
179c1218 436 ** Extract special fields for local use.
9e3c0a28
EA
437 */
438
46f6ec52
EA
439 if (OpMode == MD_FREEZE || readconfig)
440 readcf(ConfFile);
22659072 441
75f95954 442 switch (OpMode)
acae5a9d 443 {
26a3626c 444 case MD_FREEZE:
a9621daf 445 /* this is critical to avoid forgeries of the frozen config */
0e306e7f
EA
446 (void) setgid(getgid());
447 (void) setuid(getuid());
a9621daf
EA
448
449 /* freeze the configuration */
8fe4fb9b 450 freeze(FreezeFile);
acae5a9d 451 exit(EX_OK);
26a3626c
EA
452
453 case MD_INITALIAS:
454 Verbose = TRUE;
455 break;
acae5a9d 456 }
179c1218 457
6130649c
EA
458 /* do heuristic mode adjustment */
459 if (Verbose)
75f95954
EA
460 {
461 /* turn off noconnect option */
462 setoption('c', "F", TRUE, FALSE);
463
464 /* turn on interactive delivery */
465 setoption('d', "", TRUE, FALSE);
466 }
6130649c 467
179c1218 468 /* our name for SMTP codes */
a73ae8ac 469 expand("\001j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
57c97d4a 470 MyHostName = jbuf;
d6a28dd8 471
179c1218
EA
472 /* the indices of local and program mailers */
473 st = stab("local", ST_MAILER, ST_FIND);
474 if (st == NULL)
475 syserr("No local mailer defined");
476 else
477 LocalMailer = st->s_mailer;
478 st = stab("prog", ST_MAILER, ST_FIND);
479 if (st == NULL)
480 syserr("No prog mailer defined");
481 else
482 ProgMailer = st->s_mailer;
483
6bbaf971
EA
484 /* operate in queue directory */
485 if (chdir(QueueDir) < 0)
486 {
487 syserr("cannot chdir(%s)", QueueDir);
488 exit(EX_SOFTWARE);
489 }
490
64912e7e 491 /*
55f0da62 492 ** Do operation-mode-dependent initialization.
64912e7e
EA
493 */
494
55f0da62 495 switch (OpMode)
64912e7e 496 {
55f0da62
EA
497 case MD_PRINT:
498 /* print the queue */
74f37936 499#ifdef QUEUE
64912e7e
EA
500 dropenvelope(CurEnv);
501 printqueue();
502 exit(EX_OK);
74f37936
EA
503#else QUEUE
504 usrerr("No queue to print");
505 finis();
506#endif QUEUE
8acb5142 507
55f0da62
EA
508 case MD_INITALIAS:
509 /* initialize alias database */
510 initaliases(AliasFile, TRUE);
f4dbf345 511 exit(EX_OK);
cdb17311 512
55f0da62
EA
513 case MD_DAEMON:
514 /* don't open alias database -- done in srvrsmtp */
515 break;
516
517 default:
518 /* open the alias database */
519 initaliases(AliasFile, FALSE);
520 break;
521 }
522
9678c96d 523 if (tTd(0, 15))
9c6d4c70 524 {
f6a0cc15 525 /* print configuration table (or at least part of it) */
9c6d4c70
EA
526 printrules();
527 for (i = 0; i < MAXMAILERS; i++)
528 {
529 register struct mailer *m = Mailer[i];
1dbda134 530 int j;
9c6d4c70
EA
531
532 if (m == NULL)
533 continue;
97ad25b6
EA
534 printf("mailer %d (%s): P=%s S=%d R=%d M=%ld F=", i, m->m_name,
535 m->m_mailer, m->m_s_rwset, m->m_r_rwset,
536 m->m_maxsize);
1dbda134
EA
537 for (j = '\0'; j <= '\177'; j++)
538 if (bitnset(j, m->m_flags))
0e306e7f 539 (void) putchar(j);
1dbda134 540 printf(" E=");
b3ef02a2
EA
541 xputs(m->m_eol);
542 printf("\n");
9c6d4c70
EA
543 }
544 }
9c6d4c70 545
be2fcca9
EA
546 /*
547 ** Switch to the main envelope.
548 */
549
550 CurEnv = newenvelope(&MainEnvelope);
e6f08ab1 551 MainEnvelope.e_flags = BlankEnvelope.e_flags;
be2fcca9 552
cf69a203
EA
553 /*
554 ** If test mode, read addresses from stdin and process.
555 */
556
75f95954 557 if (OpMode == MD_TEST)
cf69a203
EA
558 {
559 char buf[MAXLINE];
560
50435450 561 printf("ADDRESS TEST MODE\nEnter <ruleset> <address>\n");
cf69a203
EA
562 for (;;)
563 {
564 register char **pvp;
50435450 565 char *q;
50435450 566 extern char *DelimChar;
cf69a203
EA
567
568 printf("> ");
0e306e7f 569 (void) fflush(stdout);
cf69a203
EA
570 if (fgets(buf, sizeof buf, stdin) == NULL)
571 finis();
f90b4150 572 for (p = buf; isspace(*p); p++)
cf69a203 573 continue;
ecfd2c8e
EA
574 q = p;
575 while (*p != '\0' && !isspace(*p))
576 p++;
cf69a203
EA
577 if (*p == '\0')
578 continue;
50435450
EA
579 *p = '\0';
580 do
ecfd2c8e 581 {
217a0102
EA
582 extern char **prescan();
583 char pvpbuf[PSBUFSIZE];
584
585 pvp = prescan(++p, ',', pvpbuf);
50435450 586 if (pvp == NULL)
ecfd2c8e 587 continue;
50435450
EA
588 rewrite(pvp, 3);
589 p = q;
590 while (*p != '\0')
591 {
592 rewrite(pvp, atoi(p));
593 while (*p != '\0' && *p++ != ',')
594 continue;
595 }
596 } while (*(p = DelimChar) != '\0');
cf69a203
EA
597 }
598 }
599
e3cd595c
EA
600# ifdef QUEUE
601 /*
602 ** If collecting stuff from the queue, go start doing that.
603 */
604
7b21425b 605 if (queuemode && OpMode != MD_DAEMON && QueueIntvl == 0)
e3cd595c
EA
606 {
607 runqueue(FALSE);
608 finis();
609 }
610# endif QUEUE
611
f6a0cc15
EA
612 /*
613 ** If a daemon, wait for a request.
614 ** getrequests will always return in a child.
25b9d645 615 ** If we should also be processing the queue, start
19147b2d
EA
616 ** doing it in background.
617 ** We check for any errors that might have happened
618 ** during startup.
f6a0cc15
EA
619 */
620
75f95954 621 if (OpMode == MD_DAEMON || QueueIntvl != 0)
25b9d645 622 {
9678c96d 623 if (!tTd(0, 1))
58b27aa4 624 {
fcdf8200 625 /* put us in background */
58b27aa4
EA
626 i = fork();
627 if (i < 0)
628 syserr("daemon: cannot fork");
629 if (i != 0)
630 exit(0);
fcdf8200
EA
631
632 /* get our pid right */
d9162460 633 MotherPid = getpid();
fcdf8200
EA
634
635 /* disconnect from our controlling tty */
d188728a 636 disconnect(TRUE);
58b27aa4 637 }
7338e3d4 638
25b9d645
EA
639# ifdef QUEUE
640 if (queuemode)
f309127e 641 {
25b9d645 642 runqueue(TRUE);
75f95954 643 if (OpMode != MD_DAEMON)
f309127e
EA
644 for (;;)
645 pause();
646 }
25b9d645 647# endif QUEUE
7338e3d4
EA
648 dropenvelope(CurEnv);
649
650#ifdef DAEMON
f6a0cc15 651 getrequests();
2a16bae3
EA
652
653 /* at this point we are in a child: reset state */
75f95954 654 OpMode = MD_SMTP;
7338e3d4 655 (void) newenvelope(CurEnv);
912acb74 656 openxscript(CurEnv);
14a39063 657#endif DAEMON
7338e3d4 658 }
88039044
EA
659
660# ifdef SMTP
661 /*
662 ** If running SMTP protocol, start collecting and executing
663 ** commands. This will never return.
664 */
665
75f95954 666 if (OpMode == MD_SMTP)
88039044
EA
667 smtp();
668# endif SMTP
669
f6a0cc15 670 /*
e6f08ab1 671 ** Do basic system initialization and set the sender
f6a0cc15
EA
672 */
673
e6f08ab1 674 initsys();
cbdb7357 675 setsender(from);
a9e0e597 676
3110074f 677 if (OpMode != MD_ARPAFTP && *av == NULL && !GrabTo)
e863b1fa 678 {
2e3062fe
EA
679 usrerr("Recipient names must be specified");
680
681 /* collect body for UUCP return */
682 if (OpMode != MD_VERIFY)
683 collect(FALSE);
e863b1fa
EA
684 finis();
685 }
75f95954
EA
686 if (OpMode == MD_VERIFY)
687 SendMode = SM_VERIFY;
b3cbe40f 688
b3cbe40f 689 /*
d6b27179 690 ** Scan argv and deliver the message to everyone.
b3cbe40f
EA
691 */
692
acae5a9d 693 sendtoargv(av);
b3cbe40f 694
72e9b3cc 695 /* if we have had errors sofar, arrange a meaningful exit stat */
d916f0ca 696 if (Errors > 0 && ExitStat == EX_OK)
a4b004a6 697 ExitStat = EX_USAGE;
a4b004a6 698
dc39c568
EA
699 /*
700 ** Read the input mail.
701 */
702
2654b031 703 CurEnv->e_to = NULL;
75f95954 704 if (OpMode != MD_VERIFY || GrabTo)
49086753 705 collect(FALSE);
d829793b 706 errno = 0;
35cc3fad 707
4e1f4d4b 708 /* collect statistics */
7338e3d4
EA
709 if (OpMode != MD_VERIFY)
710 markstats(CurEnv, (ADDRESS *) NULL);
b3cbe40f 711
9678c96d 712 if (tTd(1, 1))
2654b031 713 printf("From person = \"%s\"\n", CurEnv->e_from.q_paddr);
d6b27179 714
b3cbe40f
EA
715 /*
716 ** Actually send everything.
d6b27179 717 ** If verifying, just ack.
b3cbe40f
EA
718 */
719
7338e3d4
EA
720 CurEnv->e_from.q_flags |= QDONTSEND;
721 CurEnv->e_to = NULL;
f7e74083 722 sendall(CurEnv, SM_DEFAULT);
b3cbe40f
EA
723
724 /*
725 ** All done.
726 */
727
728 finis();
729}
730\f/*
731** FINIS -- Clean up and exit.
732**
b3cbe40f
EA
733** Parameters:
734** none
735**
736** Returns:
737** never
738**
739** Side Effects:
96faada8 740** exits sendmail
b3cbe40f
EA
741*/
742
743finis()
744{
9678c96d 745 if (tTd(2, 1))
e6f08ab1 746 printf("\n====finis: stat %d e_flags %o\n", ExitStat, CurEnv->e_flags);
aba51985 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 {
2e15a2d8 891 syserr("Cannot freeze %s", freezefile);
acae5a9d
EA
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 {
2e15a2d8 908 syserr("Cannot freeze %s", freezefile);
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 {
07adc4f5
RA
944 syslog(LOG_WARNING, "Cannot open frozen config file %s: %m",
945 freezefile);
acae5a9d
EA
946 errno = 0;
947 return (FALSE);
948 }
949
950 /* read in the header */
07adc4f5
RA
951 if (read(f, (char *) &fhdr, sizeof fhdr) < sizeof fhdr)
952 {
953 syserr("Cannot read frozen config file");
954 (void) close(f);
955 return (FALSE);
956 }
957 if ( fhdr.frzinfo.frzedata != &edata ||
2e3062fe 958 fhdr.frzinfo.frzend != &end ||
7338e3d4 959 strcmp(fhdr.frzinfo.frzver, Version) != 0)
acae5a9d 960 {
07adc4f5 961 syslog(LOG_WARNING, "Wrong version of frozen config file");
acae5a9d
EA
962 (void) close(f);
963 return (FALSE);
964 }
965
966 /* arrange to have enough space */
17a67c62 967 if (brk(fhdr.frzinfo.frzbrk) == (caddr_t) -1)
acae5a9d 968 {
7338e3d4 969 syserr("Cannot break to %x", fhdr.frzinfo.frzbrk);
acae5a9d
EA
970 (void) close(f);
971 return (FALSE);
972 }
973
974 /* now read in the freeze file */
34fe0a9b
EA
975 if (read(f, (char *) &edata, (int) (fhdr.frzinfo.frzbrk - &edata)) !=
976 (int) (fhdr.frzinfo.frzbrk - &edata))
acae5a9d 977 {
07adc4f5 978 syserr("Cannot read frozen config file");
acae5a9d 979 /* oops! we have trashed memory..... */
0e306e7f 980 (void) write(2, "Cannot read freeze file\n", 24);
7338e3d4 981 _exit(EX_SOFTWARE);
acae5a9d
EA
982 }
983
984 (void) close(f);
985 return (TRUE);
986}
813d8709
EA
987\f/*
988** DISCONNECT -- remove our connection with any foreground process
989**
990** Parameters:
d188728a
EA
991** fulldrop -- if set, we should also drop the controlling
992** TTY if possible -- this should only be done when
993** setting up the daemon since otherwise UUCP can
994** leave us trying to open a dialin, and we will
995** wait for the carrier.
813d8709
EA
996**
997** Returns:
998** none
999**
1000** Side Effects:
1001** Trys to insure that we are immune to vagaries of
1002** the controlling tty.
1003*/
1004
d188728a
EA
1005disconnect(fulldrop)
1006 bool fulldrop;
813d8709
EA
1007{
1008 int fd;
1009
e6f08ab1 1010 if (tTd(52, 1))
b9accadd
EA
1011 printf("disconnect: In %d Out %d\n", fileno(InChannel),
1012 fileno(OutChannel));
e6f08ab1 1013 if (tTd(52, 5))
813d8709 1014 {
e6f08ab1
EA
1015 printf("don't\n");
1016 return;
813d8709 1017 }
813d8709
EA
1018
1019 /* be sure we don't get nasty signals */
0e306e7f
EA
1020 (void) signal(SIGHUP, SIG_IGN);
1021 (void) signal(SIGINT, SIG_IGN);
1022 (void) signal(SIGQUIT, SIG_IGN);
813d8709
EA
1023
1024 /* we can't communicate with our caller, so.... */
7338e3d4
EA
1025 HoldErrs = TRUE;
1026 ErrorMode = EM_MAIL;
813d8709
EA
1027 Verbose = FALSE;
1028
1029 /* all input from /dev/null */
813d8709 1030 if (InChannel != stdin)
b9accadd
EA
1031 {
1032 (void) fclose(InChannel);
1033 InChannel = stdin;
1034 }
1035 (void) freopen("/dev/null", "r", stdin);
813d8709
EA
1036
1037 /* output to the transcript */
b9accadd 1038 if (OutChannel != stdout)
813d8709 1039 {
b9accadd
EA
1040 (void) fclose(OutChannel);
1041 OutChannel = stdout;
813d8709 1042 }
912acb74
EA
1043 if (CurEnv->e_xfp == NULL)
1044 CurEnv->e_xfp = fopen("/dev/null", "w");
b9accadd
EA
1045 (void) fflush(stdout);
1046 (void) close(1);
1047 (void) close(2);
912acb74 1048 while ((fd = dup(fileno(CurEnv->e_xfp))) < 2 && fd > 0)
b9accadd 1049 continue;
813d8709 1050
e6f08ab1 1051 /* drop our controlling TTY completely if possible */
d188728a 1052 if (fulldrop)
e6f08ab1 1053 {
afe907a4
MK
1054#if BSD > 43
1055 daemon(1, 1);
1056#else
1057#ifdef TIOCNOTTY
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);
afe907a4
MK
1065#endif /* TIOCNOTTY */
1066#endif /* BSD */
70faa7c8 1067 errno = 0;
e6f08ab1 1068 }
e6f08ab1 1069
813d8709
EA
1070# ifdef LOG
1071 if (LogLevel > 11)
1072 syslog(LOG_DEBUG, "in background, pid=%d", getpid());
1073# endif LOG
1074
1075 errno = 0;
1076}