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