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