14227346bSdist /* 20942ea6aSbostic * Copyright (c) 1983 Eric P. Allman 32b191fa3Sbostic * Copyright (c) 1988, 1993 42b191fa3Sbostic * The Regents of the University of California. All rights reserved. 5e70a7521Sbostic * 63bc94712Sbostic * %sccs.include.redist.c% 74227346bSdist */ 84227346bSdist 94227346bSdist #ifndef lint 10*7880f3e4Seric static char sccsid[] = "@(#)deliver.c 8.4 (Berkeley) 07/16/93"; 11e70a7521Sbostic #endif /* not lint */ 124227346bSdist 13fcde9cc8Sbostic #include "sendmail.h" 14a8c080f0Seric #include <signal.h> 15f28da541Smiriam #include <netdb.h> 16911693bfSbostic #include <errno.h> 17134746fbSeric #ifdef NAMED_BIND 18912a731aSbostic #include <arpa/nameser.h> 19912a731aSbostic #include <resolv.h> 20f170942cSeric 21f170942cSeric extern int h_errno; 22134746fbSeric #endif 2325a99e2eSeric 2425a99e2eSeric /* 259c9e68d9Seric ** SENDALL -- actually send all the messages. 269c9e68d9Seric ** 279c9e68d9Seric ** Parameters: 289c9e68d9Seric ** e -- the envelope to send. 299c9e68d9Seric ** mode -- the delivery mode to use. If SM_DEFAULT, use 309c9e68d9Seric ** the current e->e_sendmode. 319c9e68d9Seric ** 329c9e68d9Seric ** Returns: 339c9e68d9Seric ** none. 349c9e68d9Seric ** 359c9e68d9Seric ** Side Effects: 369c9e68d9Seric ** Scans the send lists and sends everything it finds. 379c9e68d9Seric ** Delivers any appropriate error messages. 389c9e68d9Seric ** If we are running in a non-interactive mode, takes the 399c9e68d9Seric ** appropriate action. 409c9e68d9Seric */ 419c9e68d9Seric 429c9e68d9Seric sendall(e, mode) 439c9e68d9Seric ENVELOPE *e; 449c9e68d9Seric char mode; 459c9e68d9Seric { 469c9e68d9Seric register ADDRESS *q; 479c9e68d9Seric char *owner; 489c9e68d9Seric int otherowners; 499c9e68d9Seric register ENVELOPE *ee; 509c9e68d9Seric ENVELOPE *splitenv = NULL; 516103d9b4Seric bool announcequeueup; 529c9e68d9Seric 53*7880f3e4Seric /* 54*7880f3e4Seric ** If we have had global, fatal errors, don't bother sending 55*7880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 56*7880f3e4Seric ** (e.g., a single address failing) will still cause the other 57*7880f3e4Seric ** addresses to be sent. 58*7880f3e4Seric */ 59*7880f3e4Seric 60*7880f3e4Seric if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 61896b16f6Seric { 62896b16f6Seric e->e_flags |= EF_CLRQUEUE; 63896b16f6Seric return; 64896b16f6Seric } 65896b16f6Seric 669c9e68d9Seric /* determine actual delivery mode */ 679c9e68d9Seric if (mode == SM_DEFAULT) 689c9e68d9Seric { 699c9e68d9Seric mode = e->e_sendmode; 709c9e68d9Seric if (mode != SM_VERIFY && 719c9e68d9Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 729c9e68d9Seric mode = SM_QUEUE; 736103d9b4Seric announcequeueup = mode == SM_QUEUE; 749c9e68d9Seric } 756103d9b4Seric else 766103d9b4Seric announcequeueup = FALSE; 779c9e68d9Seric 789c9e68d9Seric if (tTd(13, 1)) 799c9e68d9Seric { 809c9e68d9Seric printf("\nSENDALL: mode %c, e_from ", mode); 819c9e68d9Seric printaddr(&e->e_from, FALSE); 829c9e68d9Seric printf("sendqueue:\n"); 839c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 849c9e68d9Seric } 859c9e68d9Seric 869c9e68d9Seric /* 879c9e68d9Seric ** Do any preprocessing necessary for the mode we are running. 889c9e68d9Seric ** Check to make sure the hop count is reasonable. 899c9e68d9Seric ** Delete sends to the sender in mailing lists. 909c9e68d9Seric */ 919c9e68d9Seric 929c9e68d9Seric CurEnv = e; 939c9e68d9Seric 949c9e68d9Seric if (e->e_hopcount > MaxHopCount) 959c9e68d9Seric { 969c9e68d9Seric errno = 0; 979c9e68d9Seric syserr("554 too many hops %d (%d max): from %s, to %s", 989c9e68d9Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 999c9e68d9Seric e->e_sendqueue->q_paddr); 1009c9e68d9Seric return; 1019c9e68d9Seric } 1029c9e68d9Seric 103b8004690Seric /* 104b8004690Seric ** Do sender deletion. 105b8004690Seric ** 106b8004690Seric ** If the sender has the QQUEUEUP flag set, skip this. 107b8004690Seric ** This can happen if the name server is hosed when you 108b8004690Seric ** are trying to send mail. The result is that the sender 109b8004690Seric ** is instantiated in the queue as a recipient. 110b8004690Seric */ 111b8004690Seric 112b8004690Seric if (!MeToo && !bitset(QQUEUEUP, e->e_from.q_flags)) 1139c9e68d9Seric { 1149c9e68d9Seric if (tTd(13, 5)) 1159c9e68d9Seric { 1169c9e68d9Seric printf("sendall: QDONTSEND "); 1179c9e68d9Seric printaddr(&e->e_from, FALSE); 1189c9e68d9Seric } 1199c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 1209c9e68d9Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 1219c9e68d9Seric } 1229c9e68d9Seric 123ce5531bdSeric /* 124ce5531bdSeric ** Handle alias owners. 125ce5531bdSeric ** 126ce5531bdSeric ** We scan up the q_alias chain looking for owners. 127ce5531bdSeric ** We discard owners that are the same as the return path. 128ce5531bdSeric */ 129ce5531bdSeric 130ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 131ce5531bdSeric { 132ce5531bdSeric register struct address *a; 133ce5531bdSeric 134ce5531bdSeric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 135ce5531bdSeric continue; 136ce5531bdSeric if (a != NULL) 137ce5531bdSeric q->q_owner = a->q_owner; 138ce5531bdSeric 139ce5531bdSeric if (q->q_owner != NULL && 140ce5531bdSeric !bitset(QDONTSEND, q->q_flags) && 141ce5531bdSeric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 142ce5531bdSeric q->q_owner = NULL; 143ce5531bdSeric } 144ce5531bdSeric 145ce5531bdSeric owner = ""; 146ce5531bdSeric otherowners = 1; 147ce5531bdSeric while (owner != NULL && otherowners > 0) 148ce5531bdSeric { 149ce5531bdSeric owner = NULL; 150ce5531bdSeric otherowners = 0; 151ce5531bdSeric 152ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 153ce5531bdSeric { 154ce5531bdSeric if (bitset(QDONTSEND, q->q_flags)) 155ce5531bdSeric continue; 156ce5531bdSeric 157ce5531bdSeric if (q->q_owner != NULL) 158ce5531bdSeric { 159ce5531bdSeric if (owner == NULL) 160ce5531bdSeric owner = q->q_owner; 161ce5531bdSeric else if (owner != q->q_owner) 162ce5531bdSeric { 163ce5531bdSeric if (strcmp(owner, q->q_owner) == 0) 164ce5531bdSeric { 165ce5531bdSeric /* make future comparisons cheap */ 166ce5531bdSeric q->q_owner = owner; 167ce5531bdSeric } 168ce5531bdSeric else 169ce5531bdSeric { 170ce5531bdSeric otherowners++; 171ce5531bdSeric } 172ce5531bdSeric owner = q->q_owner; 173ce5531bdSeric } 174ce5531bdSeric } 175ce5531bdSeric else 176ce5531bdSeric { 177ce5531bdSeric otherowners++; 178ce5531bdSeric } 179ce5531bdSeric } 180ce5531bdSeric 181ce5531bdSeric if (owner != NULL && otherowners > 0) 182ce5531bdSeric { 183ce5531bdSeric extern HDR *copyheader(); 184ce5531bdSeric extern ADDRESS *copyqueue(); 185ce5531bdSeric 186ce5531bdSeric /* 187ce5531bdSeric ** Split this envelope into two. 188ce5531bdSeric */ 189ce5531bdSeric 190ce5531bdSeric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 191ce5531bdSeric *ee = *e; 192ce5531bdSeric ee->e_id = NULL; 193ce5531bdSeric (void) queuename(ee, '\0'); 194ce5531bdSeric 195ce5531bdSeric if (tTd(13, 1)) 196ce5531bdSeric printf("sendall: split %s into %s\n", 197ce5531bdSeric e->e_id, ee->e_id); 198ce5531bdSeric 199ce5531bdSeric ee->e_header = copyheader(e->e_header); 200ce5531bdSeric ee->e_sendqueue = copyqueue(e->e_sendqueue); 201ce5531bdSeric ee->e_errorqueue = copyqueue(e->e_errorqueue); 202ce5531bdSeric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS); 203ce5531bdSeric setsender(owner, ee, NULL, TRUE); 204ce5531bdSeric if (tTd(13, 5)) 205ce5531bdSeric { 206ce5531bdSeric printf("sendall(split): QDONTSEND "); 207ce5531bdSeric printaddr(&ee->e_from, FALSE); 208ce5531bdSeric } 209ce5531bdSeric ee->e_from.q_flags |= QDONTSEND; 210ce5531bdSeric ee->e_dfp = NULL; 211ce5531bdSeric ee->e_xfp = NULL; 212ce5531bdSeric ee->e_lockfp = NULL; 213ce5531bdSeric ee->e_df = NULL; 214ce5531bdSeric ee->e_errormode = EM_MAIL; 215ce5531bdSeric ee->e_sibling = splitenv; 216ce5531bdSeric splitenv = ee; 217ce5531bdSeric 218ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 219ce5531bdSeric if (q->q_owner == owner) 220ce5531bdSeric q->q_flags |= QDONTSEND; 221ce5531bdSeric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 222ce5531bdSeric if (q->q_owner != owner) 223ce5531bdSeric q->q_flags |= QDONTSEND; 224ce5531bdSeric 225ce5531bdSeric if (e->e_df != NULL && mode != SM_VERIFY) 226ce5531bdSeric { 227ce5531bdSeric ee->e_dfp = NULL; 228ce5531bdSeric ee->e_df = newstr(queuename(ee, 'd')); 229ce5531bdSeric if (link(e->e_df, ee->e_df) < 0) 230ce5531bdSeric { 231ce5531bdSeric syserr("sendall: link(%s, %s)", 232ce5531bdSeric e->e_df, ee->e_df); 233ce5531bdSeric } 234ce5531bdSeric } 235ce5531bdSeric 236ce5531bdSeric if (mode != SM_VERIFY) 237ce5531bdSeric openxscript(ee); 238ce5531bdSeric #ifdef LOG 239ce5531bdSeric if (LogLevel > 4) 240ce5531bdSeric syslog(LOG_INFO, "%s: clone %s", 241ce5531bdSeric ee->e_id, e->e_id); 242ce5531bdSeric #endif 243ce5531bdSeric } 244ce5531bdSeric } 245ce5531bdSeric 246ce5531bdSeric if (owner != NULL) 247ce5531bdSeric { 248ce5531bdSeric setsender(owner, e, NULL, TRUE); 249ce5531bdSeric if (tTd(13, 5)) 250ce5531bdSeric { 251ce5531bdSeric printf("sendall(owner): QDONTSEND "); 252ce5531bdSeric printaddr(&e->e_from, FALSE); 253ce5531bdSeric } 254ce5531bdSeric e->e_from.q_flags |= QDONTSEND; 255ce5531bdSeric e->e_errormode = EM_MAIL; 256ce5531bdSeric } 257ce5531bdSeric 258c1ac89b1Seric # ifdef QUEUE 259c1ac89b1Seric if ((mode == SM_QUEUE || mode == SM_FORK || 260c1ac89b1Seric (mode != SM_VERIFY && SuperSafe)) && 261c1ac89b1Seric !bitset(EF_INQUEUE, e->e_flags)) 262c1ac89b1Seric { 263c1ac89b1Seric /* be sure everything is instantiated in the queue */ 2646103d9b4Seric queueup(e, TRUE, announcequeueup); 265ce5531bdSeric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 2666103d9b4Seric queueup(ee, TRUE, announcequeueup); 267c1ac89b1Seric } 268c1ac89b1Seric #endif /* QUEUE */ 269c1ac89b1Seric 270ce5531bdSeric if (splitenv != NULL) 271ce5531bdSeric { 272ce5531bdSeric if (tTd(13, 1)) 273ce5531bdSeric { 274ce5531bdSeric printf("\nsendall: Split queue; remaining queue:\n"); 275ce5531bdSeric printaddr(e->e_sendqueue, TRUE); 276ce5531bdSeric } 277ce5531bdSeric 278ce5531bdSeric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 279ce5531bdSeric { 280ce5531bdSeric CurEnv = ee; 281ce5531bdSeric sendenvelope(ee, mode); 282ce5531bdSeric } 283ce5531bdSeric 284ce5531bdSeric CurEnv = e; 285ce5531bdSeric } 286ce5531bdSeric sendenvelope(e, mode); 287ce5531bdSeric 288ce5531bdSeric for (; splitenv != NULL; splitenv = splitenv->e_sibling) 289ce5531bdSeric dropenvelope(splitenv); 290ce5531bdSeric } 291ce5531bdSeric 292ce5531bdSeric sendenvelope(e, mode) 293ce5531bdSeric register ENVELOPE *e; 294ce5531bdSeric char mode; 295ce5531bdSeric { 296ce5531bdSeric bool oldverbose; 297ce5531bdSeric int pid; 298ce5531bdSeric register ADDRESS *q; 299ce5531bdSeric #ifdef LOCKF 300ce5531bdSeric struct flock lfd; 301ce5531bdSeric #endif 302ce5531bdSeric 303*7880f3e4Seric /* 304*7880f3e4Seric ** If we have had global, fatal errors, don't bother sending 305*7880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 306*7880f3e4Seric ** (e.g., a single address failing) will still cause the other 307*7880f3e4Seric ** addresses to be sent. 308*7880f3e4Seric */ 309*7880f3e4Seric 310*7880f3e4Seric if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 311*7880f3e4Seric { 312*7880f3e4Seric e->e_flags |= EF_CLRQUEUE; 313*7880f3e4Seric return; 314*7880f3e4Seric } 315*7880f3e4Seric 316ce5531bdSeric oldverbose = Verbose; 317c1ac89b1Seric switch (mode) 318c1ac89b1Seric { 319c1ac89b1Seric case SM_VERIFY: 320c1ac89b1Seric Verbose = TRUE; 321c1ac89b1Seric break; 322c1ac89b1Seric 323c1ac89b1Seric case SM_QUEUE: 324c1ac89b1Seric queueonly: 325c1ac89b1Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 326c1ac89b1Seric return; 327c1ac89b1Seric 328c1ac89b1Seric case SM_FORK: 329c1ac89b1Seric if (e->e_xfp != NULL) 330c1ac89b1Seric (void) fflush(e->e_xfp); 331c1ac89b1Seric 332c1ac89b1Seric # ifdef LOCKF 333c1ac89b1Seric /* 334c1ac89b1Seric ** Since lockf has the interesting semantic that the 335c1ac89b1Seric ** lock is lost when we fork, we have to risk losing 336c1ac89b1Seric ** the lock here by closing before the fork, and then 337c1ac89b1Seric ** trying to get it back in the child. 338c1ac89b1Seric */ 339c1ac89b1Seric 340c1ac89b1Seric if (e->e_lockfp != NULL) 341c1ac89b1Seric { 342c1ac89b1Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 343c1ac89b1Seric e->e_lockfp = NULL; 344c1ac89b1Seric } 345c1ac89b1Seric # endif /* LOCKF */ 346c1ac89b1Seric 347c1ac89b1Seric pid = fork(); 348c1ac89b1Seric if (pid < 0) 349c1ac89b1Seric { 350c1ac89b1Seric goto queueonly; 351c1ac89b1Seric } 352c1ac89b1Seric else if (pid > 0) 353c1ac89b1Seric { 354c1ac89b1Seric /* be sure we leave the temp files to our child */ 355c1ac89b1Seric e->e_id = e->e_df = NULL; 356c1ac89b1Seric # ifndef LOCKF 357c1ac89b1Seric if (e->e_lockfp != NULL) 358c1ac89b1Seric { 359c1ac89b1Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 360c1ac89b1Seric e->e_lockfp = NULL; 361c1ac89b1Seric } 362c1ac89b1Seric # endif 363c1ac89b1Seric 364c1ac89b1Seric /* close any random open files in the envelope */ 365c1ac89b1Seric if (e->e_dfp != NULL) 366c1ac89b1Seric { 367c1ac89b1Seric (void) xfclose(e->e_dfp, "sendenvelope", "dfp"); 368c1ac89b1Seric e->e_dfp = NULL; 369c1ac89b1Seric } 370c1ac89b1Seric if (e->e_xfp != NULL) 371c1ac89b1Seric { 372c1ac89b1Seric (void) xfclose(e->e_xfp, "sendenvelope", "xfp"); 373c1ac89b1Seric e->e_xfp = NULL; 374c1ac89b1Seric } 375c1ac89b1Seric return; 376c1ac89b1Seric } 377c1ac89b1Seric 378c1ac89b1Seric /* double fork to avoid zombies */ 379c1ac89b1Seric if (fork() > 0) 380c1ac89b1Seric exit(EX_OK); 381c1ac89b1Seric 382c1ac89b1Seric /* be sure we are immune from the terminal */ 383*7880f3e4Seric disconnect(1, e); 384c1ac89b1Seric 385c1ac89b1Seric # ifdef LOCKF 386c1ac89b1Seric /* 387c1ac89b1Seric ** Now try to get our lock back. 388c1ac89b1Seric */ 389c1ac89b1Seric 390c1ac89b1Seric lfd.l_type = F_WRLCK; 391c1ac89b1Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 392c1ac89b1Seric e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 393c1ac89b1Seric if (e->e_lockfp == NULL || 394c1ac89b1Seric fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 395c1ac89b1Seric { 396c1ac89b1Seric /* oops.... lost it */ 397c1ac89b1Seric if (tTd(13, 1)) 398c1ac89b1Seric printf("sendenvelope: %s lost lock: lockfp=%x, %s\n", 399c1ac89b1Seric e->e_id, e->e_lockfp, errstring(errno)); 400c1ac89b1Seric 401c1ac89b1Seric # ifdef LOG 402c1ac89b1Seric if (LogLevel > 29) 403c1ac89b1Seric syslog(LOG_NOTICE, "%s: lost lock: %m", 404c1ac89b1Seric e->e_id); 405c1ac89b1Seric # endif /* LOG */ 406c1ac89b1Seric exit(EX_OK); 407c1ac89b1Seric } 408c1ac89b1Seric # endif /* LOCKF */ 409c1ac89b1Seric 410c1ac89b1Seric /* 411c1ac89b1Seric ** Close any cached connections. 412c1ac89b1Seric ** 413c1ac89b1Seric ** We don't send the QUIT protocol because the parent 414c1ac89b1Seric ** still knows about the connection. 415c1ac89b1Seric ** 416c1ac89b1Seric ** This should only happen when delivering an error 417c1ac89b1Seric ** message. 418c1ac89b1Seric */ 419c1ac89b1Seric 420c1ac89b1Seric mci_flush(FALSE, NULL); 421c1ac89b1Seric 422c1ac89b1Seric break; 423c1ac89b1Seric } 424c1ac89b1Seric 425c1ac89b1Seric /* 4269c9e68d9Seric ** Run through the list and send everything. 4279c9e68d9Seric */ 4289c9e68d9Seric 4299c9e68d9Seric e->e_nsent = 0; 4309c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 4319c9e68d9Seric { 4329c9e68d9Seric if (mode == SM_VERIFY) 4339c9e68d9Seric { 4349c9e68d9Seric e->e_to = q->q_paddr; 4359c9e68d9Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4368dfca105Seric { 4378dfca105Seric message("deliverable: mailer %s, host %s, user %s", 4388dfca105Seric q->q_mailer->m_name, 4398dfca105Seric q->q_host, 4408dfca105Seric q->q_user); 4418dfca105Seric } 4429c9e68d9Seric } 4439c9e68d9Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4449c9e68d9Seric { 4459c9e68d9Seric # ifdef QUEUE 4469c9e68d9Seric /* 4479c9e68d9Seric ** Checkpoint the send list every few addresses 4489c9e68d9Seric */ 4499c9e68d9Seric 4509c9e68d9Seric if (e->e_nsent >= CheckpointInterval) 4519c9e68d9Seric { 4529c9e68d9Seric queueup(e, TRUE, FALSE); 4539c9e68d9Seric e->e_nsent = 0; 4549c9e68d9Seric } 4559c9e68d9Seric # endif /* QUEUE */ 4569c9e68d9Seric (void) deliver(e, q); 4579c9e68d9Seric } 4589c9e68d9Seric } 4599c9e68d9Seric Verbose = oldverbose; 4609c9e68d9Seric 4619c9e68d9Seric if (mode == SM_FORK) 4629c9e68d9Seric finis(); 4639c9e68d9Seric } 4649c9e68d9Seric /* 4659c9e68d9Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4669c9e68d9Seric ** 4679c9e68d9Seric ** This MUST be a macro, since after a vfork we are running 4689c9e68d9Seric ** two processes on the same stack!!! 4699c9e68d9Seric ** 4709c9e68d9Seric ** Parameters: 4719c9e68d9Seric ** none. 4729c9e68d9Seric ** 4739c9e68d9Seric ** Returns: 4749c9e68d9Seric ** From a macro??? You've got to be kidding! 4759c9e68d9Seric ** 4769c9e68d9Seric ** Side Effects: 4779c9e68d9Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4789c9e68d9Seric ** pid of child in parent, zero in child. 4799c9e68d9Seric ** -1 on unrecoverable error. 4809c9e68d9Seric ** 4819c9e68d9Seric ** Notes: 4829c9e68d9Seric ** I'm awfully sorry this looks so awful. That's 4839c9e68d9Seric ** vfork for you..... 4849c9e68d9Seric */ 4859c9e68d9Seric 4869c9e68d9Seric # define NFORKTRIES 5 4879c9e68d9Seric 4889c9e68d9Seric # ifndef FORK 4899c9e68d9Seric # define FORK fork 4909c9e68d9Seric # endif 4919c9e68d9Seric 4929c9e68d9Seric # define DOFORK(fORKfN) \ 4939c9e68d9Seric {\ 4949c9e68d9Seric register int i;\ 4959c9e68d9Seric \ 4969c9e68d9Seric for (i = NFORKTRIES; --i >= 0; )\ 4979c9e68d9Seric {\ 4989c9e68d9Seric pid = fORKfN();\ 4999c9e68d9Seric if (pid >= 0)\ 5009c9e68d9Seric break;\ 5019c9e68d9Seric if (i > 0)\ 5029c9e68d9Seric sleep((unsigned) NFORKTRIES - i);\ 5039c9e68d9Seric }\ 5049c9e68d9Seric } 5059c9e68d9Seric /* 5069c9e68d9Seric ** DOFORK -- simple fork interface to DOFORK. 5079c9e68d9Seric ** 5089c9e68d9Seric ** Parameters: 5099c9e68d9Seric ** none. 5109c9e68d9Seric ** 5119c9e68d9Seric ** Returns: 5129c9e68d9Seric ** pid of child in parent. 5139c9e68d9Seric ** zero in child. 5149c9e68d9Seric ** -1 on error. 5159c9e68d9Seric ** 5169c9e68d9Seric ** Side Effects: 5179c9e68d9Seric ** returns twice, once in parent and once in child. 5189c9e68d9Seric */ 5199c9e68d9Seric 5209c9e68d9Seric dofork() 5219c9e68d9Seric { 5229c9e68d9Seric register int pid; 5239c9e68d9Seric 5249c9e68d9Seric DOFORK(fork); 5259c9e68d9Seric return (pid); 5269c9e68d9Seric } 5279c9e68d9Seric /* 52813bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 52913bbc08cSeric ** 53013bbc08cSeric ** This routine delivers to everyone on the same host as the 53113bbc08cSeric ** user on the head of the list. It is clever about mailers 53213bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 53313bbc08cSeric ** that it will deliver to all these addresses however -- so 53413bbc08cSeric ** deliver should be called once for each address on the 53513bbc08cSeric ** list. 53625a99e2eSeric ** 53725a99e2eSeric ** Parameters: 538588cad61Seric ** e -- the envelope to deliver. 539c77d1c25Seric ** firstto -- head of the address list to deliver to. 54025a99e2eSeric ** 54125a99e2eSeric ** Returns: 54225a99e2eSeric ** zero -- successfully delivered. 54325a99e2eSeric ** else -- some failure, see ExitStat for more info. 54425a99e2eSeric ** 54525a99e2eSeric ** Side Effects: 54625a99e2eSeric ** The standard input is passed off to someone. 54725a99e2eSeric */ 54825a99e2eSeric 549588cad61Seric deliver(e, firstto) 550588cad61Seric register ENVELOPE *e; 551c77d1c25Seric ADDRESS *firstto; 55225a99e2eSeric { 55378442df3Seric char *host; /* host being sent to */ 55478442df3Seric char *user; /* user being sent to */ 55525a99e2eSeric char **pvp; 5565dfc646bSeric register char **mvp; 55725a99e2eSeric register char *p; 558588cad61Seric register MAILER *m; /* mailer for this recipient */ 5596259796dSeric ADDRESS *ctladdr; 560b31e7f2bSeric register MCI *mci; 561c77d1c25Seric register ADDRESS *to = firstto; 562c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 563772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 564911693bfSbostic int rcode; /* response code */ 565e103b48fSeric char *firstsig; /* signature of firstto */ 5669c9e68d9Seric int pid; 5679c9e68d9Seric char *curhost; 5689c9e68d9Seric int mpvect[2]; 5699c9e68d9Seric int rpvect[2]; 570ee6bf8dfSeric char *pv[MAXPV+1]; 571579ef0ddSeric char tobuf[TOBUFSIZE]; /* text line of to people */ 572ee6bf8dfSeric char buf[MAXNAME]; 573c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 574fabb3bd4Seric extern int checkcompat(); 5759c9e68d9Seric extern FILE *fdopen(); 57625a99e2eSeric 57735490626Seric errno = 0; 578ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5795dfc646bSeric return (0); 58025a99e2eSeric 581134746fbSeric #ifdef NAMED_BIND 582912a731aSbostic /* unless interactive, try twice, over a minute */ 583912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 584912a731aSbostic _res.retrans = 30; 585912a731aSbostic _res.retry = 2; 586912a731aSbostic } 587d4bd8f0eSbostic #endif 588912a731aSbostic 58951552439Seric m = to->q_mailer; 59051552439Seric host = to->q_host; 591c9be6216Seric CurEnv = e; /* just in case */ 5924384d521Seric e->e_statmsg = NULL; 59351552439Seric 5946ef48975Seric if (tTd(10, 1)) 5955dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 59651552439Seric m->m_mno, host, to->q_user); 597f3dbc832Seric 598f3dbc832Seric /* 599f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 600f3dbc832Seric ** connections now, just mark these addresses and return. 601f3dbc832Seric ** This is useful if we want to batch connections to 602f3dbc832Seric ** reduce load. This will cause the messages to be 603f3dbc832Seric ** queued up, and a daemon will come along to send the 604f3dbc832Seric ** messages later. 605f3dbc832Seric ** This should be on a per-mailer basis. 606f3dbc832Seric */ 607f3dbc832Seric 60819c47125Seric if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) && 60919c47125Seric bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 610f3dbc832Seric { 611f3dbc832Seric for (; to != NULL; to = to->q_next) 612f4560e80Seric { 613ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 614c6e18ac5Seric to->q_mailer != m) 615f4560e80Seric continue; 616f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 617588cad61Seric e->e_to = to->q_paddr; 61808b25121Seric message("queued"); 6192f624c86Seric if (LogLevel > 8) 62081161401Seric logdelivery(m, NULL, "queued", e); 621f4560e80Seric } 622588cad61Seric e->e_to = NULL; 623f3dbc832Seric return (0); 624f3dbc832Seric } 625f3dbc832Seric 62625a99e2eSeric /* 6275dfc646bSeric ** Do initial argv setup. 6285dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 6295dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 6305dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 6315dfc646bSeric ** code does not check for 'pv' overflow; this places a 6325dfc646bSeric ** manifest lower limit of 4 for MAXPV. 6333bea8136Seric ** The from address rewrite is expected to make 6343bea8136Seric ** the address relative to the other end. 6355dfc646bSeric */ 6365dfc646bSeric 63778442df3Seric /* rewrite from address, using rewriting rules */ 638efe54562Seric rcode = EX_OK; 639efe54562Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, 640efe54562Seric RF_SENDERADDR|RF_CANONICAL, 641efe54562Seric &rcode, e)); 642ee4b0922Seric define('g', rpathbuf, e); /* translated return path */ 643588cad61Seric define('h', host, e); /* to host */ 6445dfc646bSeric Errors = 0; 6455dfc646bSeric pvp = pv; 6465dfc646bSeric *pvp++ = m->m_argv[0]; 6475dfc646bSeric 6485dfc646bSeric /* insert -f or -r flag as appropriate */ 64957fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6505dfc646bSeric { 65157fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 6525dfc646bSeric *pvp++ = "-f"; 6535dfc646bSeric else 6545dfc646bSeric *pvp++ = "-r"; 655c23ed322Seric *pvp++ = newstr(rpathbuf); 6565dfc646bSeric } 6575dfc646bSeric 6585dfc646bSeric /* 6595dfc646bSeric ** Append the other fixed parts of the argv. These run 6605dfc646bSeric ** up to the first entry containing "$u". There can only 6615dfc646bSeric ** be one of these, and there are only a few more slots 6625dfc646bSeric ** in the pv after it. 6635dfc646bSeric */ 6645dfc646bSeric 6655dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 6665dfc646bSeric { 6672bc47524Seric /* can't use strchr here because of sign extension problems */ 6682bc47524Seric while (*p != '\0') 6692bc47524Seric { 6702bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 6712bc47524Seric { 6722bc47524Seric if (*p == 'u') 6735dfc646bSeric break; 6742bc47524Seric } 6752bc47524Seric } 6762bc47524Seric 6772bc47524Seric if (*p != '\0') 6785dfc646bSeric break; 6795dfc646bSeric 6805dfc646bSeric /* this entry is safe -- go ahead and process it */ 681588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 6825dfc646bSeric *pvp++ = newstr(buf); 6835dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 6845dfc646bSeric { 68508b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 6865dfc646bSeric return (-1); 6875dfc646bSeric } 6885dfc646bSeric } 689c579ef51Seric 69033db8731Seric /* 69133db8731Seric ** If we have no substitution for the user name in the argument 69233db8731Seric ** list, we know that we must supply the names otherwise -- and 69333db8731Seric ** SMTP is the answer!! 69433db8731Seric */ 69533db8731Seric 6965dfc646bSeric if (*mvp == NULL) 697c579ef51Seric { 698c579ef51Seric /* running SMTP */ 6992c7e1b8dSeric # ifdef SMTP 700c579ef51Seric clever = TRUE; 701c579ef51Seric *pvp = NULL; 7026c2c3107Seric # else /* SMTP */ 70333db8731Seric /* oops! we don't implement SMTP */ 70408b25121Seric syserr("554 SMTP style mailer"); 7052c7e1b8dSeric return (EX_SOFTWARE); 7066c2c3107Seric # endif /* SMTP */ 707c579ef51Seric } 7085dfc646bSeric 7095dfc646bSeric /* 7105dfc646bSeric ** At this point *mvp points to the argument with $u. We 7115dfc646bSeric ** run through our address list and append all the addresses 7125dfc646bSeric ** we can. If we run out of space, do not fret! We can 7135dfc646bSeric ** always send another copy later. 7145dfc646bSeric */ 7155dfc646bSeric 7165dfc646bSeric tobuf[0] = '\0'; 717588cad61Seric e->e_to = tobuf; 7186259796dSeric ctladdr = NULL; 719e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7205dfc646bSeric for (; to != NULL; to = to->q_next) 7215dfc646bSeric { 7225dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 72357fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7245dfc646bSeric break; 7255dfc646bSeric 7265dfc646bSeric /* if already sent or not for this host, don't send */ 727ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 728e103b48fSeric to->q_mailer != firstto->q_mailer || 729e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7305dfc646bSeric continue; 7316259796dSeric 7324b22ea87Seric /* avoid overflowing tobuf */ 733aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7344b22ea87Seric break; 7354b22ea87Seric 7366ef48975Seric if (tTd(10, 1)) 737772e6e50Seric { 738772e6e50Seric printf("\nsend to "); 739772e6e50Seric printaddr(to, FALSE); 740772e6e50Seric } 741772e6e50Seric 7426259796dSeric /* compute effective uid/gid when sending */ 7437da1035fSeric if (to->q_mailer == ProgMailer) 7446259796dSeric ctladdr = getctladdr(to); 7456259796dSeric 7465dfc646bSeric user = to->q_user; 747588cad61Seric e->e_to = to->q_paddr; 74875f1ade9Seric if (tTd(10, 5)) 74975f1ade9Seric { 75075f1ade9Seric printf("deliver: QDONTSEND "); 75175f1ade9Seric printaddr(to, FALSE); 75275f1ade9Seric } 753ee4b0922Seric to->q_flags |= QDONTSEND; 7545dfc646bSeric 7555dfc646bSeric /* 7565dfc646bSeric ** Check to see that these people are allowed to 7575dfc646bSeric ** talk to each other. 7582a6e0786Seric */ 7592a6e0786Seric 76069582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 76169582d2fSeric { 76269582d2fSeric NoReturn = TRUE; 76308b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 76481161401Seric giveresponse(EX_UNAVAILABLE, m, NULL, e); 76569582d2fSeric continue; 76669582d2fSeric } 767fabb3bd4Seric rcode = checkcompat(to, e); 7681793c9c5Seric if (rcode != EX_OK) 7695dfc646bSeric { 7701000c37aSeric markfailure(e, to, rcode); 77181161401Seric giveresponse(rcode, m, NULL, e); 7725dfc646bSeric continue; 7735dfc646bSeric } 7742a6e0786Seric 7752a6e0786Seric /* 7769ec9501bSeric ** Strip quote bits from names if the mailer is dumb 7779ec9501bSeric ** about them. 77825a99e2eSeric */ 77925a99e2eSeric 78057fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 78125a99e2eSeric { 7821d8f1806Seric stripquotes(user); 7831d8f1806Seric stripquotes(host); 78425a99e2eSeric } 78525a99e2eSeric 786cdb828c5Seric /* hack attack -- delivermail compatibility */ 787cdb828c5Seric if (m == ProgMailer && *user == '|') 788cdb828c5Seric user++; 789cdb828c5Seric 79025a99e2eSeric /* 7913efaed6eSeric ** If an error message has already been given, don't 7923efaed6eSeric ** bother to send to this address. 7933efaed6eSeric ** 7943efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 7953efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 7963efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 7973efaed6eSeric */ 7983efaed6eSeric 7996cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 8003efaed6eSeric continue; 8013efaed6eSeric 802f2fec898Seric /* save statistics.... */ 803588cad61Seric markstats(e, to); 804f2fec898Seric 8053efaed6eSeric /* 80625a99e2eSeric ** See if this user name is "special". 80725a99e2eSeric ** If the user name has a slash in it, assume that this 80851552439Seric ** is a file -- send it off without further ado. Note 80951552439Seric ** that this type of addresses is not processed along 81051552439Seric ** with the others, so we fudge on the To person. 81125a99e2eSeric */ 81225a99e2eSeric 8132c017f8dSeric if (m == FileMailer) 81425a99e2eSeric { 815b31e7f2bSeric rcode = mailfile(user, getctladdr(to), e); 81681161401Seric giveresponse(rcode, m, NULL, e); 817dde5acadSeric if (rcode == EX_OK) 818dde5acadSeric to->q_flags |= QSENT; 8195dfc646bSeric continue; 82025a99e2eSeric } 82125a99e2eSeric 82213bbc08cSeric /* 82313bbc08cSeric ** Address is verified -- add this user to mailer 82413bbc08cSeric ** argv, and add it to the print list of recipients. 82513bbc08cSeric */ 82613bbc08cSeric 827508daeccSeric /* link together the chain of recipients */ 828508daeccSeric to->q_tchain = tochain; 829508daeccSeric tochain = to; 830508daeccSeric 8315dfc646bSeric /* create list of users for error messages */ 832db8841e9Seric (void) strcat(tobuf, ","); 833db8841e9Seric (void) strcat(tobuf, to->q_paddr); 834588cad61Seric define('u', user, e); /* to user */ 835588cad61Seric define('z', to->q_home, e); /* user's home */ 8365dfc646bSeric 837c579ef51Seric /* 838508daeccSeric ** Expand out this user into argument list. 839c579ef51Seric */ 840c579ef51Seric 841508daeccSeric if (!clever) 842c579ef51Seric { 843588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8445dfc646bSeric *pvp++ = newstr(buf); 8455dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 8465dfc646bSeric { 8475dfc646bSeric /* allow some space for trailing parms */ 8485dfc646bSeric break; 8495dfc646bSeric } 8505dfc646bSeric } 851c579ef51Seric } 8525dfc646bSeric 853145b49b1Seric /* see if any addresses still exist */ 854145b49b1Seric if (tobuf[0] == '\0') 855c579ef51Seric { 856588cad61Seric define('g', (char *) NULL, e); 857145b49b1Seric return (0); 858c579ef51Seric } 859145b49b1Seric 8605dfc646bSeric /* print out messages as full list */ 86163780dbdSeric e->e_to = tobuf + 1; 8625dfc646bSeric 8635dfc646bSeric /* 8645dfc646bSeric ** Fill out any parameters after the $u parameter. 8655dfc646bSeric */ 8665dfc646bSeric 867c579ef51Seric while (!clever && *++mvp != NULL) 8685dfc646bSeric { 869588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8705dfc646bSeric *pvp++ = newstr(buf); 8715dfc646bSeric if (pvp >= &pv[MAXPV]) 87208b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 8735dfc646bSeric } 8745dfc646bSeric *pvp++ = NULL; 8755dfc646bSeric 87625a99e2eSeric /* 87725a99e2eSeric ** Call the mailer. 8786328bdf7Seric ** The argument vector gets built, pipes 87925a99e2eSeric ** are created as necessary, and we fork & exec as 8806328bdf7Seric ** appropriate. 881c579ef51Seric ** If we are running SMTP, we just need to clean up. 88225a99e2eSeric */ 88325a99e2eSeric 8847ee87e5fSeric if (ctladdr == NULL && m != ProgMailer) 88586b26461Seric ctladdr = &e->e_from; 886134746fbSeric #ifdef NAMED_BIND 8872bcc6d2dSeric if (ConfigLevel < 2) 888912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 889134746fbSeric #endif 8909c9e68d9Seric 8919c9e68d9Seric if (tTd(11, 1)) 892134746fbSeric { 8939c9e68d9Seric printf("openmailer:"); 8949c9e68d9Seric printav(pv); 8959c9e68d9Seric } 8969c9e68d9Seric errno = 0; 8979c9e68d9Seric 8989c9e68d9Seric CurHostName = m->m_mailer; 8999c9e68d9Seric 9009c9e68d9Seric /* 9019c9e68d9Seric ** Deal with the special case of mail handled through an IPC 9029c9e68d9Seric ** connection. 9039c9e68d9Seric ** In this case we don't actually fork. We must be 9049c9e68d9Seric ** running SMTP for this to work. We will return a 9059c9e68d9Seric ** zero pid to indicate that we are running IPC. 9069c9e68d9Seric ** We also handle a debug version that just talks to stdin/out. 9079c9e68d9Seric */ 9089c9e68d9Seric 9099c9e68d9Seric curhost = NULL; 9109c9e68d9Seric 9119c9e68d9Seric /* check for Local Person Communication -- not for mortals!!! */ 9129c9e68d9Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 9139c9e68d9Seric { 9149c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 9159c9e68d9Seric bzero((char *) mci, sizeof *mci); 9169c9e68d9Seric mci->mci_in = stdin; 9179c9e68d9Seric mci->mci_out = stdout; 9189c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9199c9e68d9Seric mci->mci_mailer = m; 9209c9e68d9Seric } 9219c9e68d9Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 9229c9e68d9Seric strcmp(m->m_mailer, "[TCP]") == 0) 9239c9e68d9Seric { 9249c9e68d9Seric #ifdef DAEMON 9259c9e68d9Seric register int i; 9269c9e68d9Seric register u_short port; 9279c9e68d9Seric 9289c9e68d9Seric CurHostName = pv[1]; 9299c9e68d9Seric curhost = hostsignature(m, pv[1], e); 9309c9e68d9Seric 9319c9e68d9Seric if (curhost == NULL || curhost[0] == '\0') 9329c9e68d9Seric { 9339c9e68d9Seric syserr("null signature"); 934845e533cSeric rcode = EX_OSERR; 935b31e7f2bSeric goto give_up; 936b31e7f2bSeric } 9379c9e68d9Seric 9389c9e68d9Seric if (!clever) 9399c9e68d9Seric { 9409c9e68d9Seric syserr("554 non-clever IPC"); 9419c9e68d9Seric rcode = EX_OSERR; 9429c9e68d9Seric goto give_up; 9439c9e68d9Seric } 9449c9e68d9Seric if (pv[2] != NULL) 9459c9e68d9Seric port = atoi(pv[2]); 9469c9e68d9Seric else 9479c9e68d9Seric port = 0; 9489c9e68d9Seric tryhost: 9499c9e68d9Seric mci = NULL; 9509c9e68d9Seric while (*curhost != '\0') 9519c9e68d9Seric { 9529c9e68d9Seric register char *p; 9539c9e68d9Seric static char hostbuf[MAXNAME]; 9549c9e68d9Seric 9559c9e68d9Seric mci = NULL; 9569c9e68d9Seric 9579c9e68d9Seric /* pull the next host from the signature */ 9589c9e68d9Seric p = strchr(curhost, ':'); 9599c9e68d9Seric if (p == NULL) 9609c9e68d9Seric p = &curhost[strlen(curhost)]; 9619c9e68d9Seric strncpy(hostbuf, curhost, p - curhost); 9629c9e68d9Seric hostbuf[p - curhost] = '\0'; 9639c9e68d9Seric if (*p != '\0') 9649c9e68d9Seric p++; 9659c9e68d9Seric curhost = p; 9669c9e68d9Seric 9679c9e68d9Seric /* see if we already know that this host is fried */ 9689c9e68d9Seric CurHostName = hostbuf; 9699c9e68d9Seric mci = mci_get(hostbuf, m); 9709c9e68d9Seric if (mci->mci_state != MCIS_CLOSED) 9719c9e68d9Seric { 9729c9e68d9Seric if (tTd(11, 1)) 9739c9e68d9Seric { 9749c9e68d9Seric printf("openmailer: "); 9759c9e68d9Seric mci_dump(mci); 9769c9e68d9Seric } 9779c9e68d9Seric CurHostName = mci->mci_host; 9789c9e68d9Seric break; 9799c9e68d9Seric } 9809c9e68d9Seric mci->mci_mailer = m; 9819c9e68d9Seric if (mci->mci_exitstat != EX_OK) 9829c9e68d9Seric continue; 9839c9e68d9Seric 9849c9e68d9Seric /* try the connection */ 9859c9e68d9Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 9869c9e68d9Seric message("Connecting to %s (%s)...", 9879c9e68d9Seric hostbuf, m->m_name); 9889c9e68d9Seric i = makeconnection(hostbuf, port, mci, 9899c9e68d9Seric bitnset(M_SECURE_PORT, m->m_flags)); 9909c9e68d9Seric mci->mci_exitstat = i; 9919c9e68d9Seric mci->mci_errno = errno; 992f170942cSeric #ifdef NAMED_BIND 993f170942cSeric mci->mci_herrno = h_errno; 994f170942cSeric #endif 9959c9e68d9Seric if (i == EX_OK) 9969c9e68d9Seric { 9979c9e68d9Seric mci->mci_state = MCIS_OPENING; 9989c9e68d9Seric mci_cache(mci); 999f170942cSeric if (TrafficLogFile != NULL) 1000f170942cSeric fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1001f170942cSeric getpid(), hostbuf); 10029c9e68d9Seric break; 10039c9e68d9Seric } 10049c9e68d9Seric else if (tTd(11, 1)) 10059c9e68d9Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 10069c9e68d9Seric i, errno); 10079c9e68d9Seric 10089c9e68d9Seric 10099c9e68d9Seric /* enter status of this host */ 10109c9e68d9Seric setstat(i); 10119c9e68d9Seric } 10129c9e68d9Seric mci->mci_pid = 0; 10139c9e68d9Seric #else /* no DAEMON */ 10149c9e68d9Seric syserr("554 openmailer: no IPC"); 10159c9e68d9Seric if (tTd(11, 1)) 10169c9e68d9Seric printf("openmailer: NULL\n"); 10179c9e68d9Seric return NULL; 10189c9e68d9Seric #endif /* DAEMON */ 10199c9e68d9Seric } 10209c9e68d9Seric else 10219c9e68d9Seric { 1022f170942cSeric #ifdef XDEBUG 1023f170942cSeric char wbuf[MAXLINE]; 10246fe8c3bcSeric 10256fe8c3bcSeric /* make absolutely certain 0, 1, and 2 are in use */ 1026f170942cSeric sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 1027f170942cSeric checkfd012(wbuf); 1028f170942cSeric #endif 10290e3bfef5Seric 1030f170942cSeric if (TrafficLogFile != NULL) 10310e3bfef5Seric { 1032f170942cSeric char **av; 1033f170942cSeric 1034f170942cSeric fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1035f170942cSeric for (av = pv; *av != NULL; av++) 1036f170942cSeric fprintf(TrafficLogFile, " %s", *av); 1037f170942cSeric fprintf(TrafficLogFile, "\n"); 10386fe8c3bcSeric } 10396fe8c3bcSeric 10409c9e68d9Seric /* create a pipe to shove the mail through */ 10419c9e68d9Seric if (pipe(mpvect) < 0) 10429c9e68d9Seric { 10430e3bfef5Seric syserr("%s... openmailer(%s): pipe (to mailer)", 10440e3bfef5Seric e->e_to, m->m_name); 10459c9e68d9Seric if (tTd(11, 1)) 10469c9e68d9Seric printf("openmailer: NULL\n"); 10479c9e68d9Seric rcode = EX_OSERR; 10489c9e68d9Seric goto give_up; 10499c9e68d9Seric } 10509c9e68d9Seric 10519c9e68d9Seric /* if this mailer speaks smtp, create a return pipe */ 10529c9e68d9Seric if (clever && pipe(rpvect) < 0) 10539c9e68d9Seric { 10540e3bfef5Seric syserr("%s... openmailer(%s): pipe (from mailer)", 10550e3bfef5Seric e->e_to, m->m_name); 10569c9e68d9Seric (void) close(mpvect[0]); 10579c9e68d9Seric (void) close(mpvect[1]); 10589c9e68d9Seric if (tTd(11, 1)) 10599c9e68d9Seric printf("openmailer: NULL\n"); 10609c9e68d9Seric rcode = EX_OSERR; 10619c9e68d9Seric goto give_up; 10629c9e68d9Seric } 10639c9e68d9Seric 10649c9e68d9Seric /* 10659c9e68d9Seric ** Actually fork the mailer process. 10669c9e68d9Seric ** DOFORK is clever about retrying. 10679c9e68d9Seric ** 10689c9e68d9Seric ** Dispose of SIGCHLD signal catchers that may be laying 10699c9e68d9Seric ** around so that endmail will get it. 10709c9e68d9Seric */ 10719c9e68d9Seric 10729c9e68d9Seric if (e->e_xfp != NULL) 10739c9e68d9Seric (void) fflush(e->e_xfp); /* for debugging */ 10749c9e68d9Seric (void) fflush(stdout); 10759c9e68d9Seric # ifdef SIGCHLD 10769c9e68d9Seric (void) signal(SIGCHLD, SIG_DFL); 10779c9e68d9Seric # endif /* SIGCHLD */ 10789c9e68d9Seric DOFORK(FORK); 10799c9e68d9Seric /* pid is set by DOFORK */ 10809c9e68d9Seric if (pid < 0) 10819c9e68d9Seric { 10829c9e68d9Seric /* failure */ 10830e3bfef5Seric syserr("%s... openmailer(%s): cannot fork", 10840e3bfef5Seric e->e_to, m->m_name); 10859c9e68d9Seric (void) close(mpvect[0]); 10869c9e68d9Seric (void) close(mpvect[1]); 10879c9e68d9Seric if (clever) 10889c9e68d9Seric { 10899c9e68d9Seric (void) close(rpvect[0]); 10909c9e68d9Seric (void) close(rpvect[1]); 10919c9e68d9Seric } 10929c9e68d9Seric if (tTd(11, 1)) 10939c9e68d9Seric printf("openmailer: NULL\n"); 10949c9e68d9Seric rcode = EX_OSERR; 10959c9e68d9Seric goto give_up; 10969c9e68d9Seric } 10979c9e68d9Seric else if (pid == 0) 10989c9e68d9Seric { 10999c9e68d9Seric int i; 11009c9e68d9Seric int saveerrno; 11019c9e68d9Seric char **ep; 11029c9e68d9Seric char *env[MAXUSERENVIRON]; 11039c9e68d9Seric extern char **environ; 11049c9e68d9Seric extern int DtableSize; 11059c9e68d9Seric 11069c9e68d9Seric /* child -- set up input & exec mailer */ 11079c9e68d9Seric /* make diagnostic output be standard output */ 11089c9e68d9Seric (void) signal(SIGINT, SIG_IGN); 11099c9e68d9Seric (void) signal(SIGHUP, SIG_IGN); 11109c9e68d9Seric (void) signal(SIGTERM, SIG_DFL); 11119c9e68d9Seric 11129c9e68d9Seric /* close any other cached connections */ 11139c9e68d9Seric mci_flush(FALSE, mci); 11149c9e68d9Seric 1115b986f6aaSeric /* move into some "safe" directory */ 1116b986f6aaSeric if (m->m_execdir != NULL) 1117b986f6aaSeric { 1118b986f6aaSeric char *p, *q; 1119b986f6aaSeric char buf[MAXLINE]; 1120b986f6aaSeric 1121b986f6aaSeric for (p = m->m_execdir; p != NULL; p = q) 1122b986f6aaSeric { 1123b986f6aaSeric q = strchr(p, ':'); 1124b986f6aaSeric if (q != NULL) 1125b986f6aaSeric *q = '\0'; 1126b986f6aaSeric expand(p, buf, &buf[sizeof buf] - 1, e); 1127b986f6aaSeric if (q != NULL) 1128b986f6aaSeric *q++ = ':'; 1129b986f6aaSeric if (tTd(11, 20)) 1130b986f6aaSeric printf("openmailer: trydir %s\n", 1131b986f6aaSeric buf); 1132b986f6aaSeric if (buf[0] != '\0' && chdir(buf) >= 0) 1133b986f6aaSeric break; 1134b986f6aaSeric } 1135b986f6aaSeric } 1136b986f6aaSeric 11379c9e68d9Seric /* arrange to filter std & diag output of command */ 11389c9e68d9Seric if (clever) 11399c9e68d9Seric { 11409c9e68d9Seric (void) close(rpvect[0]); 11416fe8c3bcSeric if (dup2(rpvect[1], STDOUT_FILENO) < 0) 11426fe8c3bcSeric { 11430e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 11440e3bfef5Seric e->e_to, m->m_name, rpvect[1]); 11456fe8c3bcSeric _exit(EX_OSERR); 11466fe8c3bcSeric } 11479c9e68d9Seric (void) close(rpvect[1]); 11489c9e68d9Seric } 11499c9e68d9Seric else if (OpMode == MD_SMTP || HoldErrs) 11509c9e68d9Seric { 11519c9e68d9Seric /* put mailer output in transcript */ 11526fe8c3bcSeric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 11536fe8c3bcSeric { 11540e3bfef5Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 11550e3bfef5Seric e->e_to, m->m_name, 11566fe8c3bcSeric fileno(e->e_xfp)); 11576fe8c3bcSeric _exit(EX_OSERR); 11589c9e68d9Seric } 11596fe8c3bcSeric } 11606fe8c3bcSeric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 11616fe8c3bcSeric { 11620e3bfef5Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr", 11630e3bfef5Seric e->e_to, m->m_name); 11646fe8c3bcSeric _exit(EX_OSERR); 11656fe8c3bcSeric } 11669c9e68d9Seric 11679c9e68d9Seric /* arrange to get standard input */ 11689c9e68d9Seric (void) close(mpvect[1]); 11699c9e68d9Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 11709c9e68d9Seric { 11710e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 11720e3bfef5Seric e->e_to, m->m_name, mpvect[0]); 11739c9e68d9Seric _exit(EX_OSERR); 11749c9e68d9Seric } 11759c9e68d9Seric (void) close(mpvect[0]); 11769c9e68d9Seric if (!bitnset(M_RESTR, m->m_flags)) 11779c9e68d9Seric { 11789c9e68d9Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 11799c9e68d9Seric { 11809c9e68d9Seric (void) setgid(DefGid); 11819c9e68d9Seric (void) initgroups(DefUser, DefGid); 11829c9e68d9Seric (void) setuid(DefUid); 11839c9e68d9Seric } 11849c9e68d9Seric else 11859c9e68d9Seric { 11869c9e68d9Seric (void) setgid(ctladdr->q_gid); 11879c9e68d9Seric (void) initgroups(ctladdr->q_ruser? 11889c9e68d9Seric ctladdr->q_ruser: ctladdr->q_user, 11899c9e68d9Seric ctladdr->q_gid); 11909c9e68d9Seric (void) setuid(ctladdr->q_uid); 11919c9e68d9Seric } 11929c9e68d9Seric } 11939c9e68d9Seric 11949c9e68d9Seric /* arrange for all the files to be closed */ 11959c9e68d9Seric for (i = 3; i < DtableSize; i++) 11969c9e68d9Seric { 11979c9e68d9Seric register int j; 11989c9e68d9Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 11999c9e68d9Seric (void)fcntl(i, F_SETFD, j|1); 12009c9e68d9Seric } 12019c9e68d9Seric 12029c9e68d9Seric /* set up the mailer environment */ 12039c9e68d9Seric i = 0; 12049c9e68d9Seric env[i++] = "AGENT=sendmail"; 12059c9e68d9Seric for (ep = environ; *ep != NULL; ep++) 12069c9e68d9Seric { 12079c9e68d9Seric if (strncmp(*ep, "TZ=", 3) == 0) 12089c9e68d9Seric env[i++] = *ep; 12099c9e68d9Seric } 12109c9e68d9Seric env[i++] = NULL; 12119c9e68d9Seric 12129c9e68d9Seric /* try to execute the mailer */ 12139c9e68d9Seric execve(m->m_mailer, pv, env); 12149c9e68d9Seric saveerrno = errno; 12159c9e68d9Seric syserr("Cannot exec %s", m->m_mailer); 12167c941fd2Seric if (m == LocalMailer || transienterror(saveerrno)) 12177c941fd2Seric _exit(EX_OSERR); 12189c9e68d9Seric _exit(EX_UNAVAILABLE); 12199c9e68d9Seric } 12209c9e68d9Seric 12219c9e68d9Seric /* 12229c9e68d9Seric ** Set up return value. 12239c9e68d9Seric */ 12249c9e68d9Seric 12259c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 12269c9e68d9Seric bzero((char *) mci, sizeof *mci); 12279c9e68d9Seric mci->mci_mailer = m; 12289c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 12299c9e68d9Seric mci->mci_pid = pid; 12309c9e68d9Seric (void) close(mpvect[0]); 12319c9e68d9Seric mci->mci_out = fdopen(mpvect[1], "w"); 12329c9e68d9Seric if (clever) 12339c9e68d9Seric { 12349c9e68d9Seric (void) close(rpvect[1]); 12359c9e68d9Seric mci->mci_in = fdopen(rpvect[0], "r"); 12369c9e68d9Seric } 12379c9e68d9Seric else 12389c9e68d9Seric { 12399c9e68d9Seric mci->mci_flags |= MCIF_TEMP; 12409c9e68d9Seric mci->mci_in = NULL; 12419c9e68d9Seric } 12429c9e68d9Seric } 12439c9e68d9Seric 12449c9e68d9Seric /* 12459c9e68d9Seric ** If we are in SMTP opening state, send initial protocol. 12469c9e68d9Seric */ 12479c9e68d9Seric 12489c9e68d9Seric if (clever && mci->mci_state != MCIS_CLOSED) 12499c9e68d9Seric { 12509c9e68d9Seric smtpinit(m, mci, e); 12519c9e68d9Seric } 12529c9e68d9Seric if (tTd(11, 1)) 12539c9e68d9Seric { 12549c9e68d9Seric printf("openmailer: "); 12559c9e68d9Seric mci_dump(mci); 12569c9e68d9Seric } 12579c9e68d9Seric 12589c9e68d9Seric if (mci->mci_state != MCIS_OPEN) 1259b31e7f2bSeric { 1260b31e7f2bSeric /* couldn't open the mailer */ 1261b31e7f2bSeric rcode = mci->mci_exitstat; 12622a6bc25bSeric errno = mci->mci_errno; 1263f170942cSeric #ifdef NAMED_BIND 1264f170942cSeric h_errno = mci->mci_herrno; 1265f170942cSeric #endif 1266b31e7f2bSeric if (rcode == EX_OK) 1267b31e7f2bSeric { 1268b31e7f2bSeric /* shouldn't happen */ 126908b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 12706b0f339dSeric rcode, mci->mci_state, firstsig); 1271b31e7f2bSeric rcode = EX_SOFTWARE; 1272b31e7f2bSeric } 1273e9277e33Seric else if (rcode == EX_TEMPFAIL && *curhost != '\0') 127490891494Seric { 127590891494Seric /* try next MX site */ 127690891494Seric goto tryhost; 127790891494Seric } 1278b31e7f2bSeric } 1279b31e7f2bSeric else if (!clever) 1280b31e7f2bSeric { 1281b31e7f2bSeric /* 1282b31e7f2bSeric ** Format and send message. 1283b31e7f2bSeric */ 128415d084d5Seric 1285b31e7f2bSeric putfromline(mci->mci_out, m, e); 1286b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 1287b31e7f2bSeric putline("\n", mci->mci_out, m); 128803c02fdeSeric (*e->e_putbody)(mci->mci_out, m, e, NULL); 1289b31e7f2bSeric 1290b31e7f2bSeric /* get the exit status */ 1291c9be6216Seric rcode = endmailer(mci, e, pv); 1292134746fbSeric } 1293134746fbSeric else 1294b31e7f2bSeric #ifdef SMTP 1295134746fbSeric { 1296b31e7f2bSeric /* 1297b31e7f2bSeric ** Send the MAIL FROM: protocol 1298b31e7f2bSeric */ 129915d084d5Seric 1300b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 1301b31e7f2bSeric if (rcode == EX_OK) 130275889e88Seric { 1303ded0d3daSkarels register char *t = tobuf; 1304ded0d3daSkarels register int i; 1305ded0d3daSkarels 1306588cad61Seric /* send the recipient list */ 130763780dbdSeric tobuf[0] = '\0'; 130875889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 130975889e88Seric { 131063780dbdSeric e->e_to = to->q_paddr; 131115d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 131275889e88Seric { 131383b7ddc9Seric markfailure(e, to, i); 131481161401Seric giveresponse(i, m, mci, e); 131563780dbdSeric } 131675889e88Seric else 131775889e88Seric { 1318911693bfSbostic *t++ = ','; 1319b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 1320b31e7f2bSeric continue; 1321588cad61Seric } 1322588cad61Seric } 1323588cad61Seric 132463780dbdSeric /* now send the data */ 132563780dbdSeric if (tobuf[0] == '\0') 1326b31e7f2bSeric { 13279c9e68d9Seric rcode = EX_OK; 132863780dbdSeric e->e_to = NULL; 1329b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 1330b31e7f2bSeric smtprset(m, mci, e); 1331b31e7f2bSeric } 133275889e88Seric else 133375889e88Seric { 133463780dbdSeric e->e_to = tobuf + 1; 133575889e88Seric rcode = smtpdata(m, mci, e); 133663780dbdSeric } 133763780dbdSeric 133863780dbdSeric /* now close the connection */ 1339b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 134015d084d5Seric smtpquit(m, mci, e); 134163780dbdSeric } 13429c9e68d9Seric if (rcode != EX_OK && *curhost != '\0') 13439c9e68d9Seric { 13449c9e68d9Seric /* try next MX site */ 13459c9e68d9Seric goto tryhost; 13469c9e68d9Seric } 1347c579ef51Seric } 1348b31e7f2bSeric #else /* not SMTP */ 1349a05b3449Sbostic { 135008b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 1351845e533cSeric rcode = EX_CONFIG; 1352b31e7f2bSeric goto give_up; 1353a05b3449Sbostic } 1354b31e7f2bSeric #endif /* SMTP */ 1355134746fbSeric #ifdef NAMED_BIND 13562bcc6d2dSeric if (ConfigLevel < 2) 1357912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1358134746fbSeric #endif 13595dfc646bSeric 1360b31e7f2bSeric /* arrange a return receipt if requested */ 13618c13bf07Seric if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags)) 1362b31e7f2bSeric { 1363b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 1364b31e7f2bSeric /* do we want to send back more info? */ 1365b31e7f2bSeric } 1366b31e7f2bSeric 1367c77d1c25Seric /* 136863780dbdSeric ** Do final status disposal. 136963780dbdSeric ** We check for something in tobuf for the SMTP case. 1370c77d1c25Seric ** If we got a temporary failure, arrange to queue the 1371c77d1c25Seric ** addressees. 1372c77d1c25Seric */ 1373c77d1c25Seric 1374b31e7f2bSeric give_up: 137563780dbdSeric if (tobuf[0] != '\0') 137681161401Seric giveresponse(rcode, m, mci, e); 1377772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 1378b31e7f2bSeric { 1379dde5acadSeric if (rcode != EX_OK) 138083b7ddc9Seric markfailure(e, to, rcode); 1381dde5acadSeric else 1382655518ecSeric { 1383dde5acadSeric to->q_flags |= QSENT; 1384655518ecSeric e->e_nsent++; 1385655518ecSeric } 1386b31e7f2bSeric } 1387b31e7f2bSeric 1388b31e7f2bSeric /* 1389b31e7f2bSeric ** Restore state and return. 1390b31e7f2bSeric */ 1391c77d1c25Seric 139235490626Seric errno = 0; 1393588cad61Seric define('g', (char *) NULL, e); 13945826d9d3Seric return (rcode); 139525a99e2eSeric } 13965dfc646bSeric /* 139783b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 139883b7ddc9Seric ** 139983b7ddc9Seric ** Parameters: 140083b7ddc9Seric ** e -- the envelope we are sending. 140183b7ddc9Seric ** q -- the address to mark. 140283b7ddc9Seric ** rcode -- the code signifying the particular failure. 140383b7ddc9Seric ** 140483b7ddc9Seric ** Returns: 140583b7ddc9Seric ** none. 140683b7ddc9Seric ** 140783b7ddc9Seric ** Side Effects: 140883b7ddc9Seric ** marks the address (and possibly the envelope) with the 140983b7ddc9Seric ** failure so that an error will be returned or 141083b7ddc9Seric ** the message will be queued, as appropriate. 141183b7ddc9Seric */ 141283b7ddc9Seric 141383b7ddc9Seric markfailure(e, q, rcode) 141483b7ddc9Seric register ENVELOPE *e; 141583b7ddc9Seric register ADDRESS *q; 141683b7ddc9Seric int rcode; 141783b7ddc9Seric { 141819c47125Seric char buf[MAXLINE]; 141919c47125Seric 142083b7ddc9Seric if (rcode == EX_OK) 142183b7ddc9Seric return; 1422f170942cSeric else if (rcode == EX_TEMPFAIL) 142383b7ddc9Seric q->q_flags |= QQUEUEUP; 1424f170942cSeric else if (rcode != EX_IOERR && rcode != EX_OSERR) 1425f170942cSeric q->q_flags |= QBADADDR; 142683b7ddc9Seric } 142783b7ddc9Seric /* 1428c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 1429c579ef51Seric ** 1430c579ef51Seric ** We should never get fatal errors (e.g., segmentation 1431c579ef51Seric ** violation), so we report those specially. For other 1432c579ef51Seric ** errors, we choose a status message (into statmsg), 1433c579ef51Seric ** and if it represents an error, we print it. 1434c579ef51Seric ** 1435c579ef51Seric ** Parameters: 1436c579ef51Seric ** pid -- pid of mailer. 1437c9be6216Seric ** e -- the current envelope. 1438c9be6216Seric ** pv -- the parameter vector that invoked the mailer 1439c9be6216Seric ** (for error messages). 1440c579ef51Seric ** 1441c579ef51Seric ** Returns: 1442c579ef51Seric ** exit code of mailer. 1443c579ef51Seric ** 1444c579ef51Seric ** Side Effects: 1445c579ef51Seric ** none. 1446c579ef51Seric */ 1447c579ef51Seric 1448c9be6216Seric endmailer(mci, e, pv) 1449b31e7f2bSeric register MCI *mci; 1450c9be6216Seric register ENVELOPE *e; 1451c9be6216Seric char **pv; 1452c579ef51Seric { 1453588cad61Seric int st; 1454c579ef51Seric 145575889e88Seric /* close any connections */ 145675889e88Seric if (mci->mci_in != NULL) 1457c9be6216Seric (void) xfclose(mci->mci_in, pv[0], "mci_in"); 145875889e88Seric if (mci->mci_out != NULL) 1459c9be6216Seric (void) xfclose(mci->mci_out, pv[0], "mci_out"); 146075889e88Seric mci->mci_in = mci->mci_out = NULL; 146175889e88Seric mci->mci_state = MCIS_CLOSED; 146275889e88Seric 146333db8731Seric /* in the IPC case there is nothing to wait for */ 146475889e88Seric if (mci->mci_pid == 0) 146533db8731Seric return (EX_OK); 146633db8731Seric 146733db8731Seric /* wait for the mailer process to die and collect status */ 146875889e88Seric st = waitfor(mci->mci_pid); 1469588cad61Seric if (st == -1) 147078de67c1Seric { 1471c9be6216Seric syserr("endmailer %s: wait", pv[0]); 1472588cad61Seric return (EX_SOFTWARE); 1473c579ef51Seric } 147433db8731Seric 147533db8731Seric /* see if it died a horrid death */ 1476c579ef51Seric if ((st & 0377) != 0) 1477c579ef51Seric { 1478c9be6216Seric syserr("mailer %s died with signal %o", pv[0], st); 1479c9be6216Seric 1480c9be6216Seric /* log the arguments */ 1481c9be6216Seric if (e->e_xfp != NULL) 1482c9be6216Seric { 1483c9be6216Seric register char **av; 1484c9be6216Seric 1485c9be6216Seric fprintf(e->e_xfp, "Arguments:"); 1486c9be6216Seric for (av = pv; *av != NULL; av++) 1487c9be6216Seric fprintf(e->e_xfp, " %s", *av); 1488c9be6216Seric fprintf(e->e_xfp, "\n"); 1489c9be6216Seric } 1490c9be6216Seric 14915f73204aSeric ExitStat = EX_TEMPFAIL; 14925f73204aSeric return (EX_TEMPFAIL); 1493c579ef51Seric } 149433db8731Seric 149533db8731Seric /* normal death -- return status */ 1496588cad61Seric st = (st >> 8) & 0377; 1497588cad61Seric return (st); 1498c579ef51Seric } 1499c579ef51Seric /* 150025a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 150125a99e2eSeric ** 150225a99e2eSeric ** Parameters: 150325a99e2eSeric ** stat -- the status code from the mailer (high byte 150425a99e2eSeric ** only; core dumps must have been taken care of 150525a99e2eSeric ** already). 150681161401Seric ** m -- the mailer info for this mailer. 150781161401Seric ** mci -- the mailer connection info -- can be NULL if the 150881161401Seric ** response is given before the connection is made. 150981161401Seric ** e -- the current envelope. 151025a99e2eSeric ** 151125a99e2eSeric ** Returns: 1512db8841e9Seric ** none. 151325a99e2eSeric ** 151425a99e2eSeric ** Side Effects: 1515c1f9df2cSeric ** Errors may be incremented. 151625a99e2eSeric ** ExitStat may be set. 151725a99e2eSeric */ 151825a99e2eSeric 151981161401Seric giveresponse(stat, m, mci, e) 152025a99e2eSeric int stat; 1521588cad61Seric register MAILER *m; 152281161401Seric register MCI *mci; 1523198d9be0Seric ENVELOPE *e; 152425a99e2eSeric { 15259c16475dSeric register const char *statmsg; 152625a99e2eSeric extern char *SysExMsg[]; 152725a99e2eSeric register int i; 1528d4bd8f0eSbostic extern int N_SysEx; 1529198d9be0Seric char buf[MAXLINE]; 153025a99e2eSeric 153113bbc08cSeric /* 153213bbc08cSeric ** Compute status message from code. 153313bbc08cSeric */ 153413bbc08cSeric 153525a99e2eSeric i = stat - EX__BASE; 1536588cad61Seric if (stat == 0) 15376fe8c3bcSeric { 1538588cad61Seric statmsg = "250 Sent"; 1539ce5531bdSeric if (e->e_statmsg != NULL) 15406fe8c3bcSeric { 1541ce5531bdSeric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 15426fe8c3bcSeric statmsg = buf; 15436fe8c3bcSeric } 15446fe8c3bcSeric } 1545588cad61Seric else if (i < 0 || i > N_SysEx) 1546588cad61Seric { 1547588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1548588cad61Seric stat = EX_UNAVAILABLE; 1549588cad61Seric statmsg = buf; 1550588cad61Seric } 1551198d9be0Seric else if (stat == EX_TEMPFAIL) 1552198d9be0Seric { 15537d55540cSeric (void) strcpy(buf, SysExMsg[i] + 1); 1554d4bd8f0eSbostic #ifdef NAMED_BIND 1555f28da541Smiriam if (h_errno == TRY_AGAIN) 1556f28da541Smiriam statmsg = errstring(h_errno+MAX_ERRNO); 1557f28da541Smiriam else 1558d4bd8f0eSbostic #endif 1559f28da541Smiriam { 15608557d168Seric if (errno != 0) 1561d87e85f3Seric statmsg = errstring(errno); 1562d87e85f3Seric else 1563d87e85f3Seric { 1564d87e85f3Seric #ifdef SMTP 1565d87e85f3Seric extern char SmtpError[]; 1566d87e85f3Seric 1567d87e85f3Seric statmsg = SmtpError; 15686c2c3107Seric #else /* SMTP */ 1569d87e85f3Seric statmsg = NULL; 15706c2c3107Seric #endif /* SMTP */ 1571d87e85f3Seric } 1572f28da541Smiriam } 1573d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1574d87e85f3Seric { 157587c9b3e7Seric (void) strcat(buf, ": "); 1576d87e85f3Seric (void) strcat(buf, statmsg); 15778557d168Seric } 1578198d9be0Seric statmsg = buf; 1579198d9be0Seric } 1580f170942cSeric #ifdef NAMED_BIND 1581f170942cSeric else if (stat == EX_NOHOST && h_errno != 0) 1582f170942cSeric { 1583f170942cSeric statmsg = errstring(h_errno + MAX_ERRNO); 1584f170942cSeric (void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg); 1585f170942cSeric statmsg = buf; 1586f170942cSeric } 1587f170942cSeric #endif 158825a99e2eSeric else 1589d87e85f3Seric { 159025a99e2eSeric statmsg = SysExMsg[i]; 15917d55540cSeric if (*statmsg++ == ':') 15927d55540cSeric { 15937d55540cSeric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 15947d55540cSeric statmsg = buf; 15957d55540cSeric } 1596d87e85f3Seric } 1597588cad61Seric 1598588cad61Seric /* 1599588cad61Seric ** Print the message as appropriate 1600588cad61Seric */ 1601588cad61Seric 1602198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 1603e12d03eeSeric message(&statmsg[4], errstring(errno)); 160425a99e2eSeric else 160525a99e2eSeric { 1606c1f9df2cSeric Errors++; 1607e12d03eeSeric usrerr(statmsg, errstring(errno)); 160825a99e2eSeric } 160925a99e2eSeric 161025a99e2eSeric /* 161125a99e2eSeric ** Final cleanup. 161225a99e2eSeric ** Log a record of the transaction. Compute the new 161325a99e2eSeric ** ExitStat -- if we already had an error, stick with 161425a99e2eSeric ** that. 161525a99e2eSeric */ 161625a99e2eSeric 16172f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 161881161401Seric logdelivery(m, mci, &statmsg[4], e); 1619eb238f8cSeric 1620eb238f8cSeric if (stat != EX_TEMPFAIL) 1621eb238f8cSeric setstat(stat); 1622198d9be0Seric if (stat != EX_OK) 1623198d9be0Seric { 1624198d9be0Seric if (e->e_message != NULL) 1625198d9be0Seric free(e->e_message); 1626198d9be0Seric e->e_message = newstr(&statmsg[4]); 1627198d9be0Seric } 16288557d168Seric errno = 0; 1629d4bd8f0eSbostic #ifdef NAMED_BIND 1630f28da541Smiriam h_errno = 0; 1631d4bd8f0eSbostic #endif 1632eb238f8cSeric } 1633eb238f8cSeric /* 1634eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1635eb238f8cSeric ** 1636eb238f8cSeric ** Parameters: 163781161401Seric ** m -- the mailer info. Can be NULL for initial queue. 163881161401Seric ** mci -- the mailer connection info -- can be NULL if the 163981161401Seric ** log is occuring when no connection is active. 164081161401Seric ** stat -- the message to print for the status. 164181161401Seric ** e -- the current envelope. 1642eb238f8cSeric ** 1643eb238f8cSeric ** Returns: 1644eb238f8cSeric ** none 1645eb238f8cSeric ** 1646eb238f8cSeric ** Side Effects: 1647eb238f8cSeric ** none 1648eb238f8cSeric */ 1649eb238f8cSeric 165081161401Seric logdelivery(m, mci, stat, e) 165181161401Seric MAILER *m; 165281161401Seric register MCI *mci; 1653eb238f8cSeric char *stat; 1654b31e7f2bSeric register ENVELOPE *e; 16555cf56be3Seric { 1656eb238f8cSeric # ifdef LOG 1657d6acf3eeSeric char buf[512]; 16589507d1f9Seric 165948ed5d33Seric (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 166081161401Seric 166148ed5d33Seric if (m != NULL) 166271ff6caaSeric { 166348ed5d33Seric (void) strcat(buf, ", mailer="); 166448ed5d33Seric (void) strcat(buf, m->m_name); 166571ff6caaSeric } 166648ed5d33Seric 166748ed5d33Seric if (mci != NULL && mci->mci_host != NULL) 166871ff6caaSeric { 166971ff6caaSeric # ifdef DAEMON 1670e2f2f828Seric extern SOCKADDR CurHostAddr; 167148ed5d33Seric # endif 167271ff6caaSeric 167348ed5d33Seric (void) strcat(buf, ", relay="); 167448ed5d33Seric (void) strcat(buf, mci->mci_host); 167548ed5d33Seric 167648ed5d33Seric # ifdef DAEMON 167748ed5d33Seric (void) strcat(buf, " ("); 1678e2f2f828Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 167948ed5d33Seric (void) strcat(buf, ")"); 168071ff6caaSeric # endif 168171ff6caaSeric } 16829507d1f9Seric else 168348ed5d33Seric { 168448ed5d33Seric char *p = macvalue('h', e); 16859507d1f9Seric 168648ed5d33Seric if (p != NULL && p[0] != '\0') 168748ed5d33Seric { 168848ed5d33Seric (void) strcat(buf, ", relay="); 168948ed5d33Seric (void) strcat(buf, p); 169048ed5d33Seric } 169148ed5d33Seric } 1692d6acf3eeSeric 1693d6acf3eeSeric syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1694d6acf3eeSeric e->e_id, e->e_to, buf, stat); 16956c2c3107Seric # endif /* LOG */ 169625a99e2eSeric } 169725a99e2eSeric /* 169851552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 169925a99e2eSeric ** 170051552439Seric ** This can be made an arbitrary message separator by changing $l 170151552439Seric ** 17029b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 17039b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 17049b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 17059b6c17a6Seric ** this kind of antique garbage???? 170625a99e2eSeric ** 170725a99e2eSeric ** Parameters: 170851552439Seric ** fp -- the file to output to. 170951552439Seric ** m -- the mailer describing this entry. 171025a99e2eSeric ** 171125a99e2eSeric ** Returns: 171251552439Seric ** none 171325a99e2eSeric ** 171425a99e2eSeric ** Side Effects: 171551552439Seric ** outputs some text to fp. 171625a99e2eSeric */ 171725a99e2eSeric 1718b31e7f2bSeric putfromline(fp, m, e) 171951552439Seric register FILE *fp; 172051552439Seric register MAILER *m; 1721b31e7f2bSeric ENVELOPE *e; 172225a99e2eSeric { 17232bc47524Seric char *template = "\201l\n"; 172451552439Seric char buf[MAXLINE]; 172525a99e2eSeric 172657fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 172751552439Seric return; 172813bbc08cSeric 17292c7e1b8dSeric # ifdef UGLYUUCP 173057fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 173174b6e67bSeric { 1732ea09d6edSeric char *bang; 1733ea09d6edSeric char xbuf[MAXLINE]; 173474b6e67bSeric 1735ee4b0922Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 17366c2c3107Seric bang = strchr(buf, '!'); 173774b6e67bSeric if (bang == NULL) 173808b25121Seric syserr("554 No ! in UUCP! (%s)", buf); 173974b6e67bSeric else 1740588cad61Seric { 1741ea09d6edSeric *bang++ = '\0'; 17422bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1743ea09d6edSeric template = xbuf; 174474b6e67bSeric } 1745588cad61Seric } 17466c2c3107Seric # endif /* UGLYUUCP */ 1747b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 174877b52738Seric putline(buf, fp, m); 1749bc6e2962Seric } 1750bc6e2962Seric /* 175151552439Seric ** PUTBODY -- put the body of a message. 175251552439Seric ** 175351552439Seric ** Parameters: 175451552439Seric ** fp -- file to output onto. 175577b52738Seric ** m -- a mailer descriptor to control output format. 17569a6a5f55Seric ** e -- the envelope to put out. 175703c02fdeSeric ** separator -- if non-NULL, a message separator that must 175803c02fdeSeric ** not be permitted in the resulting message. 175951552439Seric ** 176051552439Seric ** Returns: 176151552439Seric ** none. 176251552439Seric ** 176351552439Seric ** Side Effects: 176451552439Seric ** The message is written onto fp. 176551552439Seric */ 176651552439Seric 176703c02fdeSeric putbody(fp, m, e, separator) 176851552439Seric FILE *fp; 1769588cad61Seric MAILER *m; 17709a6a5f55Seric register ENVELOPE *e; 177103c02fdeSeric char *separator; 177251552439Seric { 177377b52738Seric char buf[MAXLINE]; 177451552439Seric 177551552439Seric /* 177651552439Seric ** Output the body of the message 177751552439Seric */ 177851552439Seric 17799a6a5f55Seric if (e->e_dfp == NULL) 178051552439Seric { 17819a6a5f55Seric if (e->e_df != NULL) 17829a6a5f55Seric { 17839a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 17849a6a5f55Seric if (e->e_dfp == NULL) 17858f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 17868f9146b0Srick e->e_df, e->e_to, e->e_from); 17879a6a5f55Seric } 17889a6a5f55Seric else 178977b52738Seric putline("<<< No Message Collected >>>", fp, m); 17909a6a5f55Seric } 17919a6a5f55Seric if (e->e_dfp != NULL) 17929a6a5f55Seric { 17939a6a5f55Seric rewind(e->e_dfp); 179477b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 179524fc8aeeSeric { 179624fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1797d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 17983462ad9eSeric (void) putc('>', fp); 179903c02fdeSeric if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 180003c02fdeSeric { 180103c02fdeSeric /* possible separator */ 180203c02fdeSeric int sl = strlen(separator); 180303c02fdeSeric 180403c02fdeSeric if (strncmp(&buf[2], separator, sl) == 0) 180503c02fdeSeric (void) putc(' ', fp); 180603c02fdeSeric } 180777b52738Seric putline(buf, fp, m); 180824fc8aeeSeric } 180951552439Seric 18109a6a5f55Seric if (ferror(e->e_dfp)) 181151552439Seric { 181251552439Seric syserr("putbody: read error"); 181351552439Seric ExitStat = EX_IOERR; 181451552439Seric } 181551552439Seric } 181651552439Seric 18170890ba1fSeric /* some mailers want extra blank line at end of message */ 18180890ba1fSeric if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 18190890ba1fSeric putline("", fp, m); 18200890ba1fSeric 182151552439Seric (void) fflush(fp); 182251552439Seric if (ferror(fp) && errno != EPIPE) 182351552439Seric { 182451552439Seric syserr("putbody: write error"); 182551552439Seric ExitStat = EX_IOERR; 182651552439Seric } 182751552439Seric errno = 0; 182825a99e2eSeric } 182925a99e2eSeric /* 183025a99e2eSeric ** MAILFILE -- Send a message to a file. 183125a99e2eSeric ** 1832f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1833f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1834f129ec7dSeric ** rather than the real user. Obviously, this only works if 1835f129ec7dSeric ** sendmail runs as root. 1836f129ec7dSeric ** 1837588cad61Seric ** This could be done as a subordinate mailer, except that it 1838588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1839588cad61Seric ** view this as being sufficiently important as to include it 1840588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1841588cad61Seric ** to create another process plus some pipes to save the message. 1842588cad61Seric ** 184325a99e2eSeric ** Parameters: 184425a99e2eSeric ** filename -- the name of the file to send to. 18456259796dSeric ** ctladdr -- the controlling address header -- includes 18466259796dSeric ** the userid/groupid to be when sending. 184725a99e2eSeric ** 184825a99e2eSeric ** Returns: 184925a99e2eSeric ** The exit code associated with the operation. 185025a99e2eSeric ** 185125a99e2eSeric ** Side Effects: 185225a99e2eSeric ** none. 185325a99e2eSeric */ 185425a99e2eSeric 1855b31e7f2bSeric mailfile(filename, ctladdr, e) 185625a99e2eSeric char *filename; 18576259796dSeric ADDRESS *ctladdr; 1858b31e7f2bSeric register ENVELOPE *e; 185925a99e2eSeric { 186025a99e2eSeric register FILE *f; 186132d19d43Seric register int pid; 186215d084d5Seric int mode; 186325a99e2eSeric 1864671745f3Seric if (tTd(11, 1)) 1865671745f3Seric { 1866671745f3Seric printf("mailfile %s\n ctladdr=", filename); 1867671745f3Seric printaddr(ctladdr, FALSE); 1868671745f3Seric } 1869671745f3Seric 1870f170942cSeric if (e->e_xfp != NULL) 1871f170942cSeric fflush(e->e_xfp); 1872f170942cSeric 187332d19d43Seric /* 187432d19d43Seric ** Fork so we can change permissions here. 187532d19d43Seric ** Note that we MUST use fork, not vfork, because of 187632d19d43Seric ** the complications of calling subroutines, etc. 187732d19d43Seric */ 187832d19d43Seric 187932d19d43Seric DOFORK(fork); 188032d19d43Seric 188132d19d43Seric if (pid < 0) 188232d19d43Seric return (EX_OSERR); 188332d19d43Seric else if (pid == 0) 188432d19d43Seric { 188532d19d43Seric /* child -- actually write to file */ 1886f129ec7dSeric struct stat stb; 1887f129ec7dSeric 18880984da9fSeric (void) signal(SIGINT, SIG_DFL); 18890984da9fSeric (void) signal(SIGHUP, SIG_DFL); 18900984da9fSeric (void) signal(SIGTERM, SIG_DFL); 18913462ad9eSeric (void) umask(OldUmask); 189295f16dc0Seric 1893f129ec7dSeric if (stat(filename, &stb) < 0) 18943a98e7eaSeric stb.st_mode = FileMode; 189515d084d5Seric mode = stb.st_mode; 189695f16dc0Seric 189795f16dc0Seric /* limit the errors to those actually caused in the child */ 189895f16dc0Seric errno = 0; 189995f16dc0Seric ExitStat = EX_OK; 190095f16dc0Seric 1901f129ec7dSeric if (bitset(0111, stb.st_mode)) 1902f129ec7dSeric exit(EX_CANTCREAT); 190303827b5fSeric if (ctladdr == NULL) 19048f9146b0Srick ctladdr = &e->e_from; 190515d084d5Seric else 190615d084d5Seric { 190715d084d5Seric /* ignore setuid and setgid bits */ 190815d084d5Seric mode &= ~(S_ISGID|S_ISUID); 190915d084d5Seric } 191015d084d5Seric 19118f9146b0Srick /* we have to open the dfile BEFORE setuid */ 19128f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 19138f9146b0Srick { 19148f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 191595f16dc0Seric if (e->e_dfp == NULL) 191695f16dc0Seric { 19178f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 19188f9146b0Srick e->e_df, e->e_to, e->e_from); 19198f9146b0Srick } 19208f9146b0Srick } 19218f9146b0Srick 192215d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1923e36b99e2Seric { 192495f16dc0Seric if (ctladdr->q_uid == 0) 192595f16dc0Seric { 1926e36b99e2Seric (void) setgid(DefGid); 1927898a126bSbostic (void) initgroups(DefUser, DefGid); 192895f16dc0Seric } 192995f16dc0Seric else 193095f16dc0Seric { 19316259796dSeric (void) setgid(ctladdr->q_gid); 1932898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 1933898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 1934898a126bSbostic ctladdr->q_gid); 1935898a126bSbostic } 1936e36b99e2Seric } 193715d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1938e36b99e2Seric { 1939e36b99e2Seric if (ctladdr->q_uid == 0) 1940e36b99e2Seric (void) setuid(DefUid); 1941e36b99e2Seric else 19426259796dSeric (void) setuid(ctladdr->q_uid); 1943e36b99e2Seric } 194495f16dc0Seric FileName = filename; 194595f16dc0Seric LineNumber = 0; 19463a98e7eaSeric f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 194725a99e2eSeric if (f == NULL) 194895f16dc0Seric { 194908b25121Seric message("554 cannot open"); 195032d19d43Seric exit(EX_CANTCREAT); 195195f16dc0Seric } 195225a99e2eSeric 19530331ce05Seric putfromline(f, FileMailer, e); 19540331ce05Seric (*e->e_puthdr)(f, FileMailer, e); 19550331ce05Seric putline("\n", f, FileMailer); 195603c02fdeSeric (*e->e_putbody)(f, FileMailer, e, NULL); 19570331ce05Seric putline("\n", f, FileMailer); 195895f16dc0Seric if (ferror(f)) 195995f16dc0Seric { 196008b25121Seric message("451 I/O error"); 196195f16dc0Seric setstat(EX_IOERR); 196295f16dc0Seric } 1963ee4b0922Seric (void) xfclose(f, "mailfile", filename); 196432d19d43Seric (void) fflush(stdout); 1965e36b99e2Seric 196627628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1967c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 196895f16dc0Seric exit(ExitStat); 196913bbc08cSeric /*NOTREACHED*/ 197032d19d43Seric } 197132d19d43Seric else 197232d19d43Seric { 197332d19d43Seric /* parent -- wait for exit status */ 1974588cad61Seric int st; 197532d19d43Seric 1976588cad61Seric st = waitfor(pid); 1977588cad61Seric if ((st & 0377) != 0) 1978588cad61Seric return (EX_UNAVAILABLE); 1979588cad61Seric else 1980588cad61Seric return ((st >> 8) & 0377); 19818f9146b0Srick /*NOTREACHED*/ 198232d19d43Seric } 198325a99e2eSeric } 1984ea4dc939Seric /* 1985e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 1986e103b48fSeric ** 1987e103b48fSeric ** The signature describes how we are going to send this -- it 1988e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 1989e103b48fSeric ** an ordered list of MX hosts. 1990e103b48fSeric ** 1991e103b48fSeric ** Parameters: 1992e103b48fSeric ** m -- the mailer describing this host. 1993e103b48fSeric ** host -- the host name. 1994e103b48fSeric ** e -- the current envelope. 1995e103b48fSeric ** 1996e103b48fSeric ** Returns: 1997e103b48fSeric ** The signature for this host. 1998e103b48fSeric ** 1999e103b48fSeric ** Side Effects: 2000e103b48fSeric ** Can tweak the symbol table. 2001e103b48fSeric */ 2002e103b48fSeric 2003e103b48fSeric char * 2004e103b48fSeric hostsignature(m, host, e) 2005e103b48fSeric register MAILER *m; 2006e103b48fSeric char *host; 2007e103b48fSeric ENVELOPE *e; 2008e103b48fSeric { 2009e103b48fSeric register char *p; 2010e103b48fSeric register STAB *s; 2011e103b48fSeric int i; 2012e103b48fSeric int len; 2013e103b48fSeric #ifdef NAMED_BIND 2014e103b48fSeric int nmx; 2015e103b48fSeric auto int rcode; 2016bafdc4e5Seric char *hp; 2017bafdc4e5Seric char *endp; 2018516782b4Seric int oldoptions; 2019e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 2020e103b48fSeric #endif 2021e103b48fSeric 2022e103b48fSeric /* 2023e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 2024e103b48fSeric */ 2025e103b48fSeric 2026e103b48fSeric p = m->m_mailer; 2027e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2028e103b48fSeric { 2029e103b48fSeric /* just an ordinary mailer */ 2030e103b48fSeric return host; 2031e103b48fSeric } 2032e103b48fSeric 2033e103b48fSeric /* 2034e103b48fSeric ** If it is a numeric address, just return it. 2035e103b48fSeric */ 2036e103b48fSeric 2037e103b48fSeric if (host[0] == '[') 2038e103b48fSeric return host; 2039e103b48fSeric 2040e103b48fSeric /* 2041e103b48fSeric ** Look it up in the symbol table. 2042e103b48fSeric */ 2043e103b48fSeric 2044e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 2045e103b48fSeric if (s->s_hostsig != NULL) 2046e103b48fSeric return s->s_hostsig; 2047e103b48fSeric 2048e103b48fSeric /* 2049e103b48fSeric ** Not already there -- create a signature. 2050e103b48fSeric */ 2051e103b48fSeric 2052e103b48fSeric #ifdef NAMED_BIND 2053516782b4Seric if (ConfigLevel < 2) 2054516782b4Seric { 2055516782b4Seric oldoptions = _res.options; 2056516782b4Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2057516782b4Seric } 2058516782b4Seric 2059bafdc4e5Seric for (hp = host; hp != NULL; hp = endp) 2060bafdc4e5Seric { 2061bafdc4e5Seric endp = strchr(hp, ':'); 2062bafdc4e5Seric if (endp != NULL) 2063bafdc4e5Seric *endp = '\0'; 2064bafdc4e5Seric 20657bf809e6Seric nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 20667d55540cSeric 2067e103b48fSeric if (nmx <= 0) 2068e103b48fSeric { 2069e103b48fSeric register MCI *mci; 2070e103b48fSeric extern int errno; 2071e103b48fSeric 2072e103b48fSeric /* update the connection info for this host */ 2073bafdc4e5Seric mci = mci_get(hp, m); 2074e103b48fSeric mci->mci_exitstat = rcode; 2075e103b48fSeric mci->mci_errno = errno; 2076f170942cSeric #ifdef NAMED_BIND 2077f170942cSeric mci->mci_herrno = h_errno; 2078f170942cSeric #endif 2079e103b48fSeric 2080e103b48fSeric /* and return the original host name as the signature */ 2081bafdc4e5Seric nmx = 1; 2082bafdc4e5Seric mxhosts[0] = hp; 2083e103b48fSeric } 2084e103b48fSeric 2085e103b48fSeric len = 0; 2086e103b48fSeric for (i = 0; i < nmx; i++) 2087e103b48fSeric { 2088e103b48fSeric len += strlen(mxhosts[i]) + 1; 2089e103b48fSeric } 2090bafdc4e5Seric if (s->s_hostsig != NULL) 2091bafdc4e5Seric len += strlen(s->s_hostsig) + 1; 2092bafdc4e5Seric p = xalloc(len); 2093bafdc4e5Seric if (s->s_hostsig != NULL) 2094bafdc4e5Seric { 2095bafdc4e5Seric (void) strcpy(p, s->s_hostsig); 2096bafdc4e5Seric free(s->s_hostsig); 2097bafdc4e5Seric s->s_hostsig = p; 2098bafdc4e5Seric p += strlen(p); 2099bafdc4e5Seric *p++ = ':'; 2100bafdc4e5Seric } 2101bafdc4e5Seric else 2102bafdc4e5Seric s->s_hostsig = p; 2103e103b48fSeric for (i = 0; i < nmx; i++) 2104e103b48fSeric { 2105e103b48fSeric if (i != 0) 2106e103b48fSeric *p++ = ':'; 2107e103b48fSeric strcpy(p, mxhosts[i]); 2108e103b48fSeric p += strlen(p); 2109e103b48fSeric } 2110bafdc4e5Seric if (endp != NULL) 2111bafdc4e5Seric *endp++ = ':'; 2112bafdc4e5Seric } 2113e103b48fSeric makelower(s->s_hostsig); 2114516782b4Seric if (ConfigLevel < 2) 2115516782b4Seric _res.options = oldoptions; 2116e103b48fSeric #else 2117e103b48fSeric /* not using BIND -- the signature is just the host name */ 2118e103b48fSeric s->s_hostsig = host; 2119e103b48fSeric #endif 2120e103b48fSeric if (tTd(17, 1)) 2121e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2122e103b48fSeric return s->s_hostsig; 2123e103b48fSeric } 2124