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*f2e4d5e8Seric static char sccsid[] = "@(#)deliver.c 8.84.1.2 (Berkeley) 03/05/95"; 11e70a7521Sbostic #endif /* not lint */ 124227346bSdist 13fcde9cc8Sbostic #include "sendmail.h" 14f28da541Smiriam #include <netdb.h> 15911693bfSbostic #include <errno.h> 169d4a8008Seric #if NAMED_BIND 17*f2e4d5e8Seric #include <arpa/nameser.h> 18912a731aSbostic #include <resolv.h> 19f170942cSeric 20f170942cSeric extern int h_errno; 21134746fbSeric #endif 2225a99e2eSeric 235d437c4dSeric extern char SmtpError[]; 245d437c4dSeric 2525a99e2eSeric /* 269c9e68d9Seric ** SENDALL -- actually send all the messages. 279c9e68d9Seric ** 289c9e68d9Seric ** Parameters: 299c9e68d9Seric ** e -- the envelope to send. 309c9e68d9Seric ** mode -- the delivery mode to use. If SM_DEFAULT, use 319c9e68d9Seric ** the current e->e_sendmode. 329c9e68d9Seric ** 339c9e68d9Seric ** Returns: 349c9e68d9Seric ** none. 359c9e68d9Seric ** 369c9e68d9Seric ** Side Effects: 379c9e68d9Seric ** Scans the send lists and sends everything it finds. 389c9e68d9Seric ** Delivers any appropriate error messages. 399c9e68d9Seric ** If we are running in a non-interactive mode, takes the 409c9e68d9Seric ** appropriate action. 419c9e68d9Seric */ 429c9e68d9Seric 439c9e68d9Seric sendall(e, mode) 449c9e68d9Seric ENVELOPE *e; 459c9e68d9Seric char mode; 469c9e68d9Seric { 479c9e68d9Seric register ADDRESS *q; 489c9e68d9Seric char *owner; 499c9e68d9Seric int otherowners; 50b056abd0Seric register ENVELOPE *ee; 51b056abd0Seric ENVELOPE *splitenv = NULL; 526103d9b4Seric bool announcequeueup; 539c9e68d9Seric 547880f3e4Seric /* 557880f3e4Seric ** If we have had global, fatal errors, don't bother sending 567880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 577880f3e4Seric ** (e.g., a single address failing) will still cause the other 587880f3e4Seric ** addresses to be sent. 597880f3e4Seric */ 607880f3e4Seric 618d19a23dSeric if (bitset(EF_FATALERRS, e->e_flags) && 628d19a23dSeric (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 63896b16f6Seric { 64896b16f6Seric e->e_flags |= EF_CLRQUEUE; 65896b16f6Seric return; 66896b16f6Seric } 67896b16f6Seric 689c9e68d9Seric /* determine actual delivery mode */ 69fbe2f963Seric CurrentLA = getla(); 709c9e68d9Seric if (mode == SM_DEFAULT) 719c9e68d9Seric { 729c9e68d9Seric mode = e->e_sendmode; 739c9e68d9Seric if (mode != SM_VERIFY && 749c9e68d9Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 759c9e68d9Seric mode = SM_QUEUE; 766103d9b4Seric announcequeueup = mode == SM_QUEUE; 779c9e68d9Seric } 786103d9b4Seric else 796103d9b4Seric announcequeueup = FALSE; 809c9e68d9Seric 819c9e68d9Seric if (tTd(13, 1)) 829c9e68d9Seric { 830e484fe5Seric printf("\n===== SENDALL: mode %c, id %s, e_from ", 840e484fe5Seric mode, e->e_id); 859c9e68d9Seric printaddr(&e->e_from, FALSE); 869c9e68d9Seric printf("sendqueue:\n"); 879c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 889c9e68d9Seric } 899c9e68d9Seric 909c9e68d9Seric /* 919c9e68d9Seric ** Do any preprocessing necessary for the mode we are running. 929c9e68d9Seric ** Check to make sure the hop count is reasonable. 939c9e68d9Seric ** Delete sends to the sender in mailing lists. 949c9e68d9Seric */ 959c9e68d9Seric 969c9e68d9Seric CurEnv = e; 979c9e68d9Seric 989c9e68d9Seric if (e->e_hopcount > MaxHopCount) 999c9e68d9Seric { 1009c9e68d9Seric errno = 0; 101f356687bSeric queueup(e, TRUE, announcequeueup); 10227607809Seric e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE; 1034bb8b0fdSeric syserr("554 too many hops %d (%d max): from %s via %s, to %s", 1049c9e68d9Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 105f7869e68Seric RealHostName == NULL ? "localhost" : RealHostName, 106f7869e68Seric e->e_sendqueue->q_paddr); 1079c9e68d9Seric return; 1089c9e68d9Seric } 1099c9e68d9Seric 110b8004690Seric /* 111b8004690Seric ** Do sender deletion. 112b8004690Seric ** 113b8004690Seric ** If the sender has the QQUEUEUP flag set, skip this. 114b8004690Seric ** This can happen if the name server is hosed when you 115b8004690Seric ** are trying to send mail. The result is that the sender 116b8004690Seric ** is instantiated in the queue as a recipient. 117b8004690Seric */ 118b8004690Seric 119e76a6a8fSeric if (!bitset(EF_METOO, e->e_flags) && 120e76a6a8fSeric !bitset(QQUEUEUP, e->e_from.q_flags)) 1219c9e68d9Seric { 1229c9e68d9Seric if (tTd(13, 5)) 1239c9e68d9Seric { 1249c9e68d9Seric printf("sendall: QDONTSEND "); 1259c9e68d9Seric printaddr(&e->e_from, FALSE); 1269c9e68d9Seric } 1279c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 128*f2e4d5e8Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 1299c9e68d9Seric } 1309c9e68d9Seric 131ce5531bdSeric /* 132ce5531bdSeric ** Handle alias owners. 133ce5531bdSeric ** 134ce5531bdSeric ** We scan up the q_alias chain looking for owners. 135ce5531bdSeric ** We discard owners that are the same as the return path. 136ce5531bdSeric */ 137ce5531bdSeric 138ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 139ce5531bdSeric { 140ce5531bdSeric register struct address *a; 141ce5531bdSeric 142ce5531bdSeric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 143ce5531bdSeric continue; 144ce5531bdSeric if (a != NULL) 145ce5531bdSeric q->q_owner = a->q_owner; 146ce5531bdSeric 147ce5531bdSeric if (q->q_owner != NULL && 148ce5531bdSeric !bitset(QDONTSEND, q->q_flags) && 149ce5531bdSeric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 150ce5531bdSeric q->q_owner = NULL; 151ce5531bdSeric } 152ce5531bdSeric 153ce5531bdSeric owner = ""; 154ce5531bdSeric otherowners = 1; 155ce5531bdSeric while (owner != NULL && otherowners > 0) 156ce5531bdSeric { 157ce5531bdSeric owner = NULL; 158ce5531bdSeric otherowners = 0; 159ce5531bdSeric 160ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 161ce5531bdSeric { 162ce5531bdSeric if (bitset(QDONTSEND, q->q_flags)) 163ce5531bdSeric continue; 164ce5531bdSeric 165ce5531bdSeric if (q->q_owner != NULL) 166ce5531bdSeric { 167ce5531bdSeric if (owner == NULL) 168ce5531bdSeric owner = q->q_owner; 169ce5531bdSeric else if (owner != q->q_owner) 170ce5531bdSeric { 171ce5531bdSeric if (strcmp(owner, q->q_owner) == 0) 172ce5531bdSeric { 173ce5531bdSeric /* make future comparisons cheap */ 174ce5531bdSeric q->q_owner = owner; 175ce5531bdSeric } 176ce5531bdSeric else 177ce5531bdSeric { 178ce5531bdSeric otherowners++; 179ce5531bdSeric } 180ce5531bdSeric owner = q->q_owner; 181ce5531bdSeric } 182ce5531bdSeric } 183ce5531bdSeric else 184ce5531bdSeric { 185ce5531bdSeric otherowners++; 186ce5531bdSeric } 187ce5531bdSeric } 188ce5531bdSeric 189ce5531bdSeric if (owner != NULL && otherowners > 0) 190ce5531bdSeric { 191ce5531bdSeric extern HDR *copyheader(); 192ce5531bdSeric extern ADDRESS *copyqueue(); 193ce5531bdSeric 194ce5531bdSeric /* 195ce5531bdSeric ** Split this envelope into two. 196ce5531bdSeric */ 197ce5531bdSeric 198ce5531bdSeric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 199ce5531bdSeric *ee = *e; 200ce5531bdSeric ee->e_id = NULL; 201ce5531bdSeric (void) queuename(ee, '\0'); 202ce5531bdSeric 203ce5531bdSeric if (tTd(13, 1)) 204ce5531bdSeric printf("sendall: split %s into %s\n", 205ce5531bdSeric e->e_id, ee->e_id); 206ce5531bdSeric 207ce5531bdSeric ee->e_header = copyheader(e->e_header); 208ce5531bdSeric ee->e_sendqueue = copyqueue(e->e_sendqueue); 209ce5531bdSeric ee->e_errorqueue = copyqueue(e->e_errorqueue); 210fce3c07bSeric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT); 211fce3c07bSeric ee->e_flags |= EF_NORECEIPT; 212ce5531bdSeric setsender(owner, ee, NULL, TRUE); 213ce5531bdSeric if (tTd(13, 5)) 214ce5531bdSeric { 215ce5531bdSeric printf("sendall(split): QDONTSEND "); 216ce5531bdSeric printaddr(&ee->e_from, FALSE); 217ce5531bdSeric } 218ce5531bdSeric ee->e_from.q_flags |= QDONTSEND; 219ce5531bdSeric ee->e_dfp = NULL; 220ce5531bdSeric ee->e_xfp = NULL; 221ce5531bdSeric ee->e_df = NULL; 222ce5531bdSeric ee->e_errormode = EM_MAIL; 223b056abd0Seric ee->e_sibling = splitenv; 224b056abd0Seric splitenv = ee; 225ce5531bdSeric 226ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 227ce5531bdSeric if (q->q_owner == owner) 228de1a6b1bSeric { 229ce5531bdSeric q->q_flags |= QDONTSEND; 230de1a6b1bSeric q->q_flags &= ~QQUEUEUP; 231de1a6b1bSeric } 232ce5531bdSeric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 233ce5531bdSeric if (q->q_owner != owner) 234de1a6b1bSeric { 235ce5531bdSeric q->q_flags |= QDONTSEND; 236de1a6b1bSeric q->q_flags &= ~QQUEUEUP; 237de1a6b1bSeric } 238ce5531bdSeric 239ce5531bdSeric if (e->e_df != NULL && mode != SM_VERIFY) 240ce5531bdSeric { 241ce5531bdSeric ee->e_dfp = NULL; 242da662164Seric ee->e_df = queuename(ee, 'd'); 243da662164Seric ee->e_df = newstr(ee->e_df); 244ce5531bdSeric if (link(e->e_df, ee->e_df) < 0) 245ce5531bdSeric { 246ce5531bdSeric syserr("sendall: link(%s, %s)", 247ce5531bdSeric e->e_df, ee->e_df); 248ce5531bdSeric } 249ce5531bdSeric } 250ce5531bdSeric #ifdef LOG 251ce5531bdSeric if (LogLevel > 4) 252c8b70947Seric syslog(LOG_INFO, "%s: clone %s, owner=%s", 253c8b70947Seric ee->e_id, e->e_id, owner); 254ce5531bdSeric #endif 255ce5531bdSeric } 256ce5531bdSeric } 257ce5531bdSeric 258ce5531bdSeric if (owner != NULL) 259ce5531bdSeric { 260ce5531bdSeric setsender(owner, e, NULL, TRUE); 261ce5531bdSeric if (tTd(13, 5)) 262ce5531bdSeric { 263ce5531bdSeric printf("sendall(owner): QDONTSEND "); 264ce5531bdSeric printaddr(&e->e_from, FALSE); 265ce5531bdSeric } 266ce5531bdSeric e->e_from.q_flags |= QDONTSEND; 267ce5531bdSeric e->e_errormode = EM_MAIL; 268fce3c07bSeric e->e_flags |= EF_NORECEIPT; 269ce5531bdSeric } 270ce5531bdSeric 271c1ac89b1Seric # ifdef QUEUE 272c1ac89b1Seric if ((mode == SM_QUEUE || mode == SM_FORK || 273c1ac89b1Seric (mode != SM_VERIFY && SuperSafe)) && 274c1ac89b1Seric !bitset(EF_INQUEUE, e->e_flags)) 275c1ac89b1Seric { 276c1ac89b1Seric /* be sure everything is instantiated in the queue */ 2776103d9b4Seric queueup(e, TRUE, announcequeueup); 278b056abd0Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 279b056abd0Seric queueup(ee, TRUE, announcequeueup); 280c1ac89b1Seric } 281c1ac89b1Seric #endif /* QUEUE */ 282c1ac89b1Seric 283*f2e4d5e8Seric if (splitenv != NULL) 284*f2e4d5e8Seric { 285*f2e4d5e8Seric if (tTd(13, 1)) 286*f2e4d5e8Seric { 287*f2e4d5e8Seric printf("\nsendall: Split queue; remaining queue:\n"); 288*f2e4d5e8Seric printaddr(e->e_sendqueue, TRUE); 289*f2e4d5e8Seric } 290*f2e4d5e8Seric 291*f2e4d5e8Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 292*f2e4d5e8Seric { 293*f2e4d5e8Seric CurEnv = ee; 294*f2e4d5e8Seric if (mode != SM_VERIFY) 295*f2e4d5e8Seric openxscript(ee); 296*f2e4d5e8Seric sendenvelope(ee, mode); 297*f2e4d5e8Seric dropenvelope(ee); 298*f2e4d5e8Seric } 299*f2e4d5e8Seric 300*f2e4d5e8Seric CurEnv = e; 301*f2e4d5e8Seric } 302*f2e4d5e8Seric sendenvelope(e, mode); 303*f2e4d5e8Seric } 304*f2e4d5e8Seric 305*f2e4d5e8Seric sendenvelope(e, mode) 306*f2e4d5e8Seric register ENVELOPE *e; 307*f2e4d5e8Seric char mode; 308*f2e4d5e8Seric { 309*f2e4d5e8Seric bool oldverbose; 310*f2e4d5e8Seric int pid; 311*f2e4d5e8Seric register ADDRESS *q; 312*f2e4d5e8Seric char *qf; 313*f2e4d5e8Seric char *id; 314*f2e4d5e8Seric 3157880f3e4Seric /* 316*f2e4d5e8Seric ** If we have had global, fatal errors, don't bother sending 317*f2e4d5e8Seric ** the message at all if we are in SMTP mode. Local errors 318*f2e4d5e8Seric ** (e.g., a single address failing) will still cause the other 319*f2e4d5e8Seric ** addresses to be sent. 3207880f3e4Seric */ 3217880f3e4Seric 322*f2e4d5e8Seric if (bitset(EF_FATALERRS, e->e_flags) && 323*f2e4d5e8Seric (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 324*f2e4d5e8Seric { 325*f2e4d5e8Seric e->e_flags |= EF_CLRQUEUE; 326*f2e4d5e8Seric return; 327*f2e4d5e8Seric } 328*f2e4d5e8Seric 329*f2e4d5e8Seric oldverbose = Verbose; 330c1ac89b1Seric switch (mode) 331c1ac89b1Seric { 332c1ac89b1Seric case SM_VERIFY: 333c1ac89b1Seric Verbose = TRUE; 334c1ac89b1Seric break; 335c1ac89b1Seric 336c1ac89b1Seric case SM_QUEUE: 337c1ac89b1Seric queueonly: 338c1ac89b1Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 339c1ac89b1Seric return; 340c1ac89b1Seric 341c1ac89b1Seric case SM_FORK: 342c1ac89b1Seric if (e->e_xfp != NULL) 343c1ac89b1Seric (void) fflush(e->e_xfp); 344c1ac89b1Seric 345b269aa8eSeric # if !HASFLOCK 346c1ac89b1Seric /* 3476b2765c6Seric ** Since fcntl locking has the interesting semantic that 3486b2765c6Seric ** the lock is owned by a process, not by an open file 3496b2765c6Seric ** descriptor, we have to flush this to the queue, and 3506b2765c6Seric ** then restart from scratch in the child. 351c1ac89b1Seric */ 352c1ac89b1Seric 3536b2765c6Seric /* save id for future use */ 354*f2e4d5e8Seric id = e->e_id; 3556b2765c6Seric 3566b2765c6Seric /* now drop the envelope in the parent */ 3576b2765c6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 3586b2765c6Seric dropenvelope(e); 3596b2765c6Seric 3606b2765c6Seric /* and reacquire in the child */ 361*f2e4d5e8Seric (void) dowork(id, TRUE, FALSE, e); 3626b2765c6Seric 3636b2765c6Seric return; 3646b2765c6Seric 3656b2765c6Seric # else /* HASFLOCK */ 366c1ac89b1Seric 367c1ac89b1Seric pid = fork(); 368c1ac89b1Seric if (pid < 0) 369c1ac89b1Seric { 370c1ac89b1Seric goto queueonly; 371c1ac89b1Seric } 372c1ac89b1Seric else if (pid > 0) 373c1ac89b1Seric { 3740e484fe5Seric /* be sure we leave the temp files to our child */ 3750e484fe5Seric /* can't call unlockqueue to avoid unlink of xfp */ 3760e484fe5Seric if (e->e_lockfp != NULL) 3770e484fe5Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 3780e484fe5Seric e->e_lockfp = NULL; 3790e484fe5Seric 3800e484fe5Seric /* close any random open files in the envelope */ 3810e484fe5Seric closexscript(e); 3820e484fe5Seric if (e->e_dfp != NULL) 3830e484fe5Seric (void) xfclose(e->e_dfp, "sendenvelope", e->e_df); 3840e484fe5Seric e->e_dfp = NULL; 3850e484fe5Seric e->e_id = e->e_df = NULL; 3869f9b003eSeric 3879f9b003eSeric /* catch intermediate zombie */ 3889f9b003eSeric (void) waitfor(pid); 389c1ac89b1Seric return; 390c1ac89b1Seric } 391c1ac89b1Seric 392c1ac89b1Seric /* double fork to avoid zombies */ 3939f9b003eSeric pid = fork(); 3949f9b003eSeric if (pid > 0) 395c1ac89b1Seric exit(EX_OK); 396c1ac89b1Seric 397c1ac89b1Seric /* be sure we are immune from the terminal */ 3987880f3e4Seric disconnect(1, e); 399c1ac89b1Seric 4009f9b003eSeric /* prevent parent from waiting if there was an error */ 4019f9b003eSeric if (pid < 0) 4029f9b003eSeric { 4039f9b003eSeric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 4049f9b003eSeric finis(); 4059f9b003eSeric } 4069f9b003eSeric 407c1ac89b1Seric /* 408c1ac89b1Seric ** Close any cached connections. 409c1ac89b1Seric ** 410c1ac89b1Seric ** We don't send the QUIT protocol because the parent 411c1ac89b1Seric ** still knows about the connection. 412c1ac89b1Seric ** 413c1ac89b1Seric ** This should only happen when delivering an error 414c1ac89b1Seric ** message. 415c1ac89b1Seric */ 416c1ac89b1Seric 417c1ac89b1Seric mci_flush(FALSE, NULL); 418c1ac89b1Seric 4196b2765c6Seric # endif /* HASFLOCK */ 4206b2765c6Seric 421c1ac89b1Seric break; 422c1ac89b1Seric } 423c1ac89b1Seric 424c1ac89b1Seric /* 4259c9e68d9Seric ** Run through the list and send everything. 4265288e21aSeric ** 4275288e21aSeric ** Set EF_GLOBALERRS so that error messages during delivery 4285288e21aSeric ** result in returned mail. 4299c9e68d9Seric */ 4309c9e68d9Seric 4319c9e68d9Seric e->e_nsent = 0; 4325288e21aSeric e->e_flags |= EF_GLOBALERRS; 433faad2b16Seric 434faad2b16Seric /* now run through the queue */ 4359c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 4369c9e68d9Seric { 437fdc75a0fSeric #ifdef XDEBUG 438fdc75a0fSeric char wbuf[MAXNAME + 20]; 439fdc75a0fSeric 440fdc75a0fSeric (void) sprintf(wbuf, "sendall(%s)", q->q_paddr); 441fdc75a0fSeric checkfd012(wbuf); 442fdc75a0fSeric #endif 4439c9e68d9Seric if (mode == SM_VERIFY) 4449c9e68d9Seric { 4459c9e68d9Seric e->e_to = q->q_paddr; 4469c9e68d9Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4478dfca105Seric { 448fafe46e1Seric if (q->q_host != NULL && q->q_host[0] != '\0') 4498dfca105Seric message("deliverable: mailer %s, host %s, user %s", 4508dfca105Seric q->q_mailer->m_name, 4518dfca105Seric q->q_host, 4528dfca105Seric q->q_user); 453fafe46e1Seric else 454fafe46e1Seric message("deliverable: mailer %s, user %s", 455fafe46e1Seric q->q_mailer->m_name, 456fafe46e1Seric q->q_user); 4578dfca105Seric } 4589c9e68d9Seric } 4599c9e68d9Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4609c9e68d9Seric { 4619c9e68d9Seric # ifdef QUEUE 4629c9e68d9Seric /* 4639c9e68d9Seric ** Checkpoint the send list every few addresses 4649c9e68d9Seric */ 4659c9e68d9Seric 4669c9e68d9Seric if (e->e_nsent >= CheckpointInterval) 4679c9e68d9Seric { 4689c9e68d9Seric queueup(e, TRUE, FALSE); 4699c9e68d9Seric e->e_nsent = 0; 4709c9e68d9Seric } 4719c9e68d9Seric # endif /* QUEUE */ 4729c9e68d9Seric (void) deliver(e, q); 4739c9e68d9Seric } 4749c9e68d9Seric } 475*f2e4d5e8Seric Verbose = oldverbose; 4769c9e68d9Seric 477fdc75a0fSeric #ifdef XDEBUG 478fdc75a0fSeric checkfd012("end of sendenvelope"); 479fdc75a0fSeric #endif 480fdc75a0fSeric 4819c9e68d9Seric if (mode == SM_FORK) 4829c9e68d9Seric finis(); 4839c9e68d9Seric } 4849c9e68d9Seric /* 4859c9e68d9Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4869c9e68d9Seric ** 4879c9e68d9Seric ** This MUST be a macro, since after a vfork we are running 4889c9e68d9Seric ** two processes on the same stack!!! 4899c9e68d9Seric ** 4909c9e68d9Seric ** Parameters: 4919c9e68d9Seric ** none. 4929c9e68d9Seric ** 4939c9e68d9Seric ** Returns: 4949c9e68d9Seric ** From a macro??? You've got to be kidding! 4959c9e68d9Seric ** 4969c9e68d9Seric ** Side Effects: 4979c9e68d9Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4989c9e68d9Seric ** pid of child in parent, zero in child. 4999c9e68d9Seric ** -1 on unrecoverable error. 5009c9e68d9Seric ** 5019c9e68d9Seric ** Notes: 5029c9e68d9Seric ** I'm awfully sorry this looks so awful. That's 5039c9e68d9Seric ** vfork for you..... 5049c9e68d9Seric */ 5059c9e68d9Seric 5069c9e68d9Seric # define NFORKTRIES 5 5079c9e68d9Seric 5089c9e68d9Seric # ifndef FORK 5099c9e68d9Seric # define FORK fork 5109c9e68d9Seric # endif 5119c9e68d9Seric 5129c9e68d9Seric # define DOFORK(fORKfN) \ 5139c9e68d9Seric {\ 5149c9e68d9Seric register int i;\ 5159c9e68d9Seric \ 5169c9e68d9Seric for (i = NFORKTRIES; --i >= 0; )\ 5179c9e68d9Seric {\ 5189c9e68d9Seric pid = fORKfN();\ 5199c9e68d9Seric if (pid >= 0)\ 5209c9e68d9Seric break;\ 5219c9e68d9Seric if (i > 0)\ 5229c9e68d9Seric sleep((unsigned) NFORKTRIES - i);\ 5239c9e68d9Seric }\ 5249c9e68d9Seric } 5259c9e68d9Seric /* 5269c9e68d9Seric ** DOFORK -- simple fork interface to DOFORK. 5279c9e68d9Seric ** 5289c9e68d9Seric ** Parameters: 5299c9e68d9Seric ** none. 5309c9e68d9Seric ** 5319c9e68d9Seric ** Returns: 5329c9e68d9Seric ** pid of child in parent. 5339c9e68d9Seric ** zero in child. 5349c9e68d9Seric ** -1 on error. 5359c9e68d9Seric ** 5369c9e68d9Seric ** Side Effects: 5379c9e68d9Seric ** returns twice, once in parent and once in child. 5389c9e68d9Seric */ 5399c9e68d9Seric 5409c9e68d9Seric dofork() 5419c9e68d9Seric { 542*f2e4d5e8Seric register int pid; 5439c9e68d9Seric 5449c9e68d9Seric DOFORK(fork); 5459c9e68d9Seric return (pid); 5469c9e68d9Seric } 5479c9e68d9Seric /* 54813bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 54913bbc08cSeric ** 55013bbc08cSeric ** This routine delivers to everyone on the same host as the 55113bbc08cSeric ** user on the head of the list. It is clever about mailers 55213bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 55313bbc08cSeric ** that it will deliver to all these addresses however -- so 55413bbc08cSeric ** deliver should be called once for each address on the 55513bbc08cSeric ** list. 55625a99e2eSeric ** 55725a99e2eSeric ** Parameters: 558588cad61Seric ** e -- the envelope to deliver. 559c77d1c25Seric ** firstto -- head of the address list to deliver to. 56025a99e2eSeric ** 56125a99e2eSeric ** Returns: 56225a99e2eSeric ** zero -- successfully delivered. 56325a99e2eSeric ** else -- some failure, see ExitStat for more info. 56425a99e2eSeric ** 56525a99e2eSeric ** Side Effects: 56625a99e2eSeric ** The standard input is passed off to someone. 56725a99e2eSeric */ 56825a99e2eSeric 569588cad61Seric deliver(e, firstto) 570588cad61Seric register ENVELOPE *e; 571c77d1c25Seric ADDRESS *firstto; 57225a99e2eSeric { 57378442df3Seric char *host; /* host being sent to */ 57478442df3Seric char *user; /* user being sent to */ 57525a99e2eSeric char **pvp; 5765dfc646bSeric register char **mvp; 57725a99e2eSeric register char *p; 578588cad61Seric register MAILER *m; /* mailer for this recipient */ 5796259796dSeric ADDRESS *ctladdr; 580b31e7f2bSeric register MCI *mci; 581c77d1c25Seric register ADDRESS *to = firstto; 582c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 583*f2e4d5e8Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 584911693bfSbostic int rcode; /* response code */ 585e103b48fSeric char *firstsig; /* signature of firstto */ 586*f2e4d5e8Seric int pid; 5879c9e68d9Seric char *curhost; 5889c9e68d9Seric int mpvect[2]; 5899c9e68d9Seric int rpvect[2]; 590ee6bf8dfSeric char *pv[MAXPV+1]; 591579ef0ddSeric char tobuf[TOBUFSIZE]; /* text line of to people */ 592ee6bf8dfSeric char buf[MAXNAME]; 593c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 594fabb3bd4Seric extern int checkcompat(); 59525a99e2eSeric 59635490626Seric errno = 0; 597ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5985dfc646bSeric return (0); 59925a99e2eSeric 6009d4a8008Seric #if NAMED_BIND 601912a731aSbostic /* unless interactive, try twice, over a minute */ 6028d19a23dSeric if (OpMode == MD_DAEMON || OpMode == MD_SMTP) 6038d19a23dSeric { 604912a731aSbostic _res.retrans = 30; 605912a731aSbostic _res.retry = 2; 606912a731aSbostic } 607d4bd8f0eSbostic #endif 608912a731aSbostic 60951552439Seric m = to->q_mailer; 61051552439Seric host = to->q_host; 611c9be6216Seric CurEnv = e; /* just in case */ 6124384d521Seric e->e_statmsg = NULL; 613df106f0bSeric SmtpError[0] = '\0'; 61451552439Seric 6156ef48975Seric if (tTd(10, 1)) 616562ec147Seric printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n", 617562ec147Seric e->e_id, m->m_name, host, to->q_user); 618ab81ee53Seric if (tTd(10, 100)) 619ab81ee53Seric printopenfds(FALSE); 620f3dbc832Seric 621f3dbc832Seric /* 622f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 623f3dbc832Seric ** connections now, just mark these addresses and return. 624f3dbc832Seric ** This is useful if we want to batch connections to 625f3dbc832Seric ** reduce load. This will cause the messages to be 626f3dbc832Seric ** queued up, and a daemon will come along to send the 627f3dbc832Seric ** messages later. 628f3dbc832Seric ** This should be on a per-mailer basis. 629f3dbc832Seric */ 630f3dbc832Seric 63179aa5155Seric if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 632f3dbc832Seric { 633f3dbc832Seric for (; to != NULL; to = to->q_next) 634f4560e80Seric { 635ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 636c6e18ac5Seric to->q_mailer != m) 637f4560e80Seric continue; 638268a25e1Seric to->q_flags |= QQUEUEUP; 639588cad61Seric e->e_to = to->q_paddr; 64008b25121Seric message("queued"); 6412f624c86Seric if (LogLevel > 8) 642*f2e4d5e8Seric logdelivery(m, NULL, "queued", NULL, e); 643f4560e80Seric } 644588cad61Seric e->e_to = NULL; 645f3dbc832Seric return (0); 646f3dbc832Seric } 647f3dbc832Seric 64825a99e2eSeric /* 6495dfc646bSeric ** Do initial argv setup. 6505dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 6515dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 6525dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 6535dfc646bSeric ** code does not check for 'pv' overflow; this places a 6545dfc646bSeric ** manifest lower limit of 4 for MAXPV. 6553bea8136Seric ** The from address rewrite is expected to make 6563bea8136Seric ** the address relative to the other end. 6575dfc646bSeric */ 6585dfc646bSeric 65978442df3Seric /* rewrite from address, using rewriting rules */ 660efe54562Seric rcode = EX_OK; 661*f2e4d5e8Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, 662efe54562Seric RF_SENDERADDR|RF_CANONICAL, 663efe54562Seric &rcode, e)); 664ee4b0922Seric define('g', rpathbuf, e); /* translated return path */ 665588cad61Seric define('h', host, e); /* to host */ 6665dfc646bSeric Errors = 0; 6675dfc646bSeric pvp = pv; 6685dfc646bSeric *pvp++ = m->m_argv[0]; 6695dfc646bSeric 6705dfc646bSeric /* insert -f or -r flag as appropriate */ 67157fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6725dfc646bSeric { 67357fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 6745dfc646bSeric *pvp++ = "-f"; 6755dfc646bSeric else 6765dfc646bSeric *pvp++ = "-r"; 677c23ed322Seric *pvp++ = newstr(rpathbuf); 6785dfc646bSeric } 6795dfc646bSeric 6805dfc646bSeric /* 6815dfc646bSeric ** Append the other fixed parts of the argv. These run 6825dfc646bSeric ** up to the first entry containing "$u". There can only 6835dfc646bSeric ** be one of these, and there are only a few more slots 6845dfc646bSeric ** in the pv after it. 6855dfc646bSeric */ 6865dfc646bSeric 6875dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 6885dfc646bSeric { 6892bc47524Seric /* can't use strchr here because of sign extension problems */ 6902bc47524Seric while (*p != '\0') 6912bc47524Seric { 6922bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 6932bc47524Seric { 6942bc47524Seric if (*p == 'u') 6955dfc646bSeric break; 6962bc47524Seric } 6972bc47524Seric } 6982bc47524Seric 6992bc47524Seric if (*p != '\0') 7005dfc646bSeric break; 7015dfc646bSeric 7025dfc646bSeric /* this entry is safe -- go ahead and process it */ 703588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 7045dfc646bSeric *pvp++ = newstr(buf); 7055dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 7065dfc646bSeric { 70708b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 7085dfc646bSeric return (-1); 7095dfc646bSeric } 7105dfc646bSeric } 711c579ef51Seric 71233db8731Seric /* 71333db8731Seric ** If we have no substitution for the user name in the argument 71433db8731Seric ** list, we know that we must supply the names otherwise -- and 71533db8731Seric ** SMTP is the answer!! 71633db8731Seric */ 71733db8731Seric 7185dfc646bSeric if (*mvp == NULL) 719c579ef51Seric { 720c579ef51Seric /* running SMTP */ 7212c7e1b8dSeric # ifdef SMTP 722c579ef51Seric clever = TRUE; 723c579ef51Seric *pvp = NULL; 7246c2c3107Seric # else /* SMTP */ 72533db8731Seric /* oops! we don't implement SMTP */ 726df106f0bSeric syserr("554 SMTP style mailer not implemented"); 7272c7e1b8dSeric return (EX_SOFTWARE); 7286c2c3107Seric # endif /* SMTP */ 729c579ef51Seric } 7305dfc646bSeric 7315dfc646bSeric /* 7325dfc646bSeric ** At this point *mvp points to the argument with $u. We 7335dfc646bSeric ** run through our address list and append all the addresses 7345dfc646bSeric ** we can. If we run out of space, do not fret! We can 7355dfc646bSeric ** always send another copy later. 7365dfc646bSeric */ 7375dfc646bSeric 7385dfc646bSeric tobuf[0] = '\0'; 739588cad61Seric e->e_to = tobuf; 7406259796dSeric ctladdr = NULL; 741e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7425dfc646bSeric for (; to != NULL; to = to->q_next) 7435dfc646bSeric { 7445dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 74557fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7465dfc646bSeric break; 7475dfc646bSeric 7485dfc646bSeric /* if already sent or not for this host, don't send */ 749ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 750e103b48fSeric to->q_mailer != firstto->q_mailer || 751e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7525dfc646bSeric continue; 7536259796dSeric 7544b22ea87Seric /* avoid overflowing tobuf */ 755aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7564b22ea87Seric break; 7574b22ea87Seric 7586ef48975Seric if (tTd(10, 1)) 759772e6e50Seric { 760772e6e50Seric printf("\nsend to "); 761772e6e50Seric printaddr(to, FALSE); 762772e6e50Seric } 763772e6e50Seric 7646259796dSeric /* compute effective uid/gid when sending */ 765*f2e4d5e8Seric /* XXX perhaps this should be to->q_mailer != LocalMailer ?? */ 766*f2e4d5e8Seric /* XXX perhaps it should be a mailer flag? */ 767*f2e4d5e8Seric if (to->q_mailer == ProgMailer || to->q_mailer == FileMailer) 7686259796dSeric ctladdr = getctladdr(to); 7696259796dSeric 7705dfc646bSeric user = to->q_user; 771588cad61Seric e->e_to = to->q_paddr; 77275f1ade9Seric if (tTd(10, 5)) 77375f1ade9Seric { 77475f1ade9Seric printf("deliver: QDONTSEND "); 77575f1ade9Seric printaddr(to, FALSE); 77675f1ade9Seric } 777ee4b0922Seric to->q_flags |= QDONTSEND; 7785dfc646bSeric 7795dfc646bSeric /* 7805dfc646bSeric ** Check to see that these people are allowed to 7815dfc646bSeric ** talk to each other. 7822a6e0786Seric */ 7832a6e0786Seric 78469582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 78569582d2fSeric { 786*f2e4d5e8Seric NoReturn = TRUE; 78708b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 788*f2e4d5e8Seric giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e); 78969582d2fSeric continue; 79069582d2fSeric } 791fabb3bd4Seric rcode = checkcompat(to, e); 7921793c9c5Seric if (rcode != EX_OK) 7935dfc646bSeric { 794*f2e4d5e8Seric markfailure(e, to, rcode); 795*f2e4d5e8Seric giveresponse(rcode, m, NULL, ctladdr, e); 7965dfc646bSeric continue; 7975dfc646bSeric } 7982a6e0786Seric 7992a6e0786Seric /* 8009ec9501bSeric ** Strip quote bits from names if the mailer is dumb 8019ec9501bSeric ** about them. 80225a99e2eSeric */ 80325a99e2eSeric 80457fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 80525a99e2eSeric { 8061d8f1806Seric stripquotes(user); 8071d8f1806Seric stripquotes(host); 80825a99e2eSeric } 80925a99e2eSeric 810cdb828c5Seric /* hack attack -- delivermail compatibility */ 811cdb828c5Seric if (m == ProgMailer && *user == '|') 812cdb828c5Seric user++; 813cdb828c5Seric 81425a99e2eSeric /* 8153efaed6eSeric ** If an error message has already been given, don't 8163efaed6eSeric ** bother to send to this address. 8173efaed6eSeric ** 8183efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 8193efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 8203efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 8213efaed6eSeric */ 8223efaed6eSeric 8236cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 8243efaed6eSeric continue; 8253efaed6eSeric 826f2fec898Seric /* save statistics.... */ 827588cad61Seric markstats(e, to); 828f2fec898Seric 8293efaed6eSeric /* 83025a99e2eSeric ** See if this user name is "special". 83125a99e2eSeric ** If the user name has a slash in it, assume that this 83251552439Seric ** is a file -- send it off without further ado. Note 83351552439Seric ** that this type of addresses is not processed along 83451552439Seric ** with the others, so we fudge on the To person. 83525a99e2eSeric */ 83625a99e2eSeric 8372c017f8dSeric if (m == FileMailer) 83825a99e2eSeric { 83903463a75Seric rcode = mailfile(user, ctladdr, e); 840*f2e4d5e8Seric giveresponse(rcode, m, NULL, ctladdr, e); 841dde5acadSeric if (rcode == EX_OK) 842dde5acadSeric to->q_flags |= QSENT; 8435dfc646bSeric continue; 84425a99e2eSeric } 84525a99e2eSeric 84613bbc08cSeric /* 84713bbc08cSeric ** Address is verified -- add this user to mailer 84813bbc08cSeric ** argv, and add it to the print list of recipients. 84913bbc08cSeric */ 85013bbc08cSeric 851508daeccSeric /* link together the chain of recipients */ 852508daeccSeric to->q_tchain = tochain; 853508daeccSeric tochain = to; 854508daeccSeric 8555dfc646bSeric /* create list of users for error messages */ 856db8841e9Seric (void) strcat(tobuf, ","); 857db8841e9Seric (void) strcat(tobuf, to->q_paddr); 858588cad61Seric define('u', user, e); /* to user */ 859b6f89e6eSeric p = to->q_home; 860b6f89e6eSeric if (p == NULL && ctladdr != NULL) 861b6f89e6eSeric p = ctladdr->q_home; 862b6f89e6eSeric define('z', p, e); /* user's home */ 8635dfc646bSeric 864c579ef51Seric /* 865508daeccSeric ** Expand out this user into argument list. 866c579ef51Seric */ 867c579ef51Seric 868508daeccSeric if (!clever) 869c579ef51Seric { 870588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8715dfc646bSeric *pvp++ = newstr(buf); 8725dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 8735dfc646bSeric { 8745dfc646bSeric /* allow some space for trailing parms */ 8755dfc646bSeric break; 8765dfc646bSeric } 8775dfc646bSeric } 878c579ef51Seric } 8795dfc646bSeric 880145b49b1Seric /* see if any addresses still exist */ 881145b49b1Seric if (tobuf[0] == '\0') 882c579ef51Seric { 883588cad61Seric define('g', (char *) NULL, e); 884145b49b1Seric return (0); 885c579ef51Seric } 886145b49b1Seric 8875dfc646bSeric /* print out messages as full list */ 88863780dbdSeric e->e_to = tobuf + 1; 8895dfc646bSeric 8905dfc646bSeric /* 8915dfc646bSeric ** Fill out any parameters after the $u parameter. 8925dfc646bSeric */ 8935dfc646bSeric 894c579ef51Seric while (!clever && *++mvp != NULL) 8955dfc646bSeric { 896588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8975dfc646bSeric *pvp++ = newstr(buf); 8985dfc646bSeric if (pvp >= &pv[MAXPV]) 89908b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 9005dfc646bSeric } 9015dfc646bSeric *pvp++ = NULL; 9025dfc646bSeric 90325a99e2eSeric /* 90425a99e2eSeric ** Call the mailer. 9056328bdf7Seric ** The argument vector gets built, pipes 90625a99e2eSeric ** are created as necessary, and we fork & exec as 9076328bdf7Seric ** appropriate. 908c579ef51Seric ** If we are running SMTP, we just need to clean up. 90925a99e2eSeric */ 91025a99e2eSeric 911960e665aSeric /*XXX this seems a bit wierd */ 9124899f6b5Seric if (ctladdr == NULL && m != ProgMailer && 9134899f6b5Seric bitset(QGOODUID, e->e_from.q_flags)) 914960e665aSeric ctladdr = &e->e_from; 915960e665aSeric 9169d4a8008Seric #if NAMED_BIND 9172bcc6d2dSeric if (ConfigLevel < 2) 918912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 919134746fbSeric #endif 9209c9e68d9Seric 9219c9e68d9Seric if (tTd(11, 1)) 922134746fbSeric { 9239c9e68d9Seric printf("openmailer:"); 9249c9e68d9Seric printav(pv); 9259c9e68d9Seric } 9269c9e68d9Seric errno = 0; 9279c9e68d9Seric 928*f2e4d5e8Seric CurHostName = m->m_mailer; 9299c9e68d9Seric 9309c9e68d9Seric /* 9319c9e68d9Seric ** Deal with the special case of mail handled through an IPC 9329c9e68d9Seric ** connection. 9339c9e68d9Seric ** In this case we don't actually fork. We must be 9349c9e68d9Seric ** running SMTP for this to work. We will return a 9359c9e68d9Seric ** zero pid to indicate that we are running IPC. 9369c9e68d9Seric ** We also handle a debug version that just talks to stdin/out. 9379c9e68d9Seric */ 9389c9e68d9Seric 9399c9e68d9Seric curhost = NULL; 940c931b82bSeric SmtpPhase = NULL; 941df106f0bSeric mci = NULL; 9429c9e68d9Seric 9437febfd66Seric #ifdef XDEBUG 9447febfd66Seric { 9457febfd66Seric char wbuf[MAXLINE]; 9467febfd66Seric 9477febfd66Seric /* make absolutely certain 0, 1, and 2 are in use */ 9487febfd66Seric sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 9497febfd66Seric checkfd012(wbuf); 9507febfd66Seric } 9517febfd66Seric #endif 9527febfd66Seric 9539c9e68d9Seric /* check for Local Person Communication -- not for mortals!!! */ 9549c9e68d9Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 9559c9e68d9Seric { 9569c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 9579c9e68d9Seric bzero((char *) mci, sizeof *mci); 9589c9e68d9Seric mci->mci_in = stdin; 9599c9e68d9Seric mci->mci_out = stdout; 9609c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9619c9e68d9Seric mci->mci_mailer = m; 9629c9e68d9Seric } 9639c9e68d9Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 9649c9e68d9Seric strcmp(m->m_mailer, "[TCP]") == 0) 9659c9e68d9Seric { 9669c9e68d9Seric #ifdef DAEMON 9679c9e68d9Seric register int i; 968*f2e4d5e8Seric register u_short port; 9699c9e68d9Seric 970a5d44642Seric if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0') 971a5d44642Seric { 972a5d44642Seric syserr("null host name for %s mailer", m->m_mailer); 973a5d44642Seric rcode = EX_CONFIG; 974a5d44642Seric goto give_up; 975a5d44642Seric } 976a5d44642Seric 9779c9e68d9Seric CurHostName = pv[1]; 9789c9e68d9Seric curhost = hostsignature(m, pv[1], e); 9799c9e68d9Seric 9809c9e68d9Seric if (curhost == NULL || curhost[0] == '\0') 9819c9e68d9Seric { 982a5d44642Seric syserr("null host signature for %s", pv[1]); 983dfebe401Seric rcode = EX_CONFIG; 984b31e7f2bSeric goto give_up; 985b31e7f2bSeric } 9869c9e68d9Seric 9879c9e68d9Seric if (!clever) 9889c9e68d9Seric { 9899c9e68d9Seric syserr("554 non-clever IPC"); 990df106f0bSeric rcode = EX_CONFIG; 9919c9e68d9Seric goto give_up; 9929c9e68d9Seric } 9939c9e68d9Seric if (pv[2] != NULL) 9949c9e68d9Seric port = atoi(pv[2]); 995*f2e4d5e8Seric else 996*f2e4d5e8Seric port = 0; 9979c9e68d9Seric tryhost: 9989c9e68d9Seric while (*curhost != '\0') 9999c9e68d9Seric { 10009c9e68d9Seric register char *p; 10019c9e68d9Seric static char hostbuf[MAXNAME]; 10029c9e68d9Seric 10039c9e68d9Seric /* pull the next host from the signature */ 10049c9e68d9Seric p = strchr(curhost, ':'); 10059c9e68d9Seric if (p == NULL) 10069c9e68d9Seric p = &curhost[strlen(curhost)]; 1007df106f0bSeric if (p == curhost) 1008df106f0bSeric { 1009df106f0bSeric syserr("deliver: null host name in signature"); 10108087428aSeric curhost++; 1011df106f0bSeric continue; 1012df106f0bSeric } 10139c9e68d9Seric strncpy(hostbuf, curhost, p - curhost); 10149c9e68d9Seric hostbuf[p - curhost] = '\0'; 10159c9e68d9Seric if (*p != '\0') 10169c9e68d9Seric p++; 10179c9e68d9Seric curhost = p; 10189c9e68d9Seric 10199c9e68d9Seric /* see if we already know that this host is fried */ 10209c9e68d9Seric CurHostName = hostbuf; 10219c9e68d9Seric mci = mci_get(hostbuf, m); 10229c9e68d9Seric if (mci->mci_state != MCIS_CLOSED) 10239c9e68d9Seric { 10249c9e68d9Seric if (tTd(11, 1)) 10259c9e68d9Seric { 10269c9e68d9Seric printf("openmailer: "); 10274052916eSeric mci_dump(mci, FALSE); 10289c9e68d9Seric } 10299c9e68d9Seric CurHostName = mci->mci_host; 10309c9e68d9Seric break; 10319c9e68d9Seric } 10329c9e68d9Seric mci->mci_mailer = m; 10339c9e68d9Seric if (mci->mci_exitstat != EX_OK) 10349c9e68d9Seric continue; 10359c9e68d9Seric 10369c9e68d9Seric /* try the connection */ 10379c9e68d9Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 1038*f2e4d5e8Seric message("Connecting to %s (%s)...", 10399c9e68d9Seric hostbuf, m->m_name); 10409c9e68d9Seric i = makeconnection(hostbuf, port, mci, 10419c9e68d9Seric bitnset(M_SECURE_PORT, m->m_flags)); 10429c9e68d9Seric mci->mci_exitstat = i; 10439c9e68d9Seric mci->mci_errno = errno; 10449d4a8008Seric #if NAMED_BIND 1045f170942cSeric mci->mci_herrno = h_errno; 1046f170942cSeric #endif 10479c9e68d9Seric if (i == EX_OK) 10489c9e68d9Seric { 10499c9e68d9Seric mci->mci_state = MCIS_OPENING; 10509c9e68d9Seric mci_cache(mci); 1051f170942cSeric if (TrafficLogFile != NULL) 1052f170942cSeric fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1053f170942cSeric getpid(), hostbuf); 10549c9e68d9Seric break; 10559c9e68d9Seric } 10569c9e68d9Seric else if (tTd(11, 1)) 10579c9e68d9Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 10589c9e68d9Seric i, errno); 10599c9e68d9Seric 10609c9e68d9Seric /* enter status of this host */ 10619c9e68d9Seric setstat(i); 1062df106f0bSeric 1063df106f0bSeric /* should print some message here for -v mode */ 1064df106f0bSeric } 1065df106f0bSeric if (mci == NULL) 1066df106f0bSeric { 1067df106f0bSeric syserr("deliver: no host name"); 1068df106f0bSeric rcode = EX_OSERR; 1069df106f0bSeric goto give_up; 10709c9e68d9Seric } 10719c9e68d9Seric mci->mci_pid = 0; 10729c9e68d9Seric #else /* no DAEMON */ 10739c9e68d9Seric syserr("554 openmailer: no IPC"); 10749c9e68d9Seric if (tTd(11, 1)) 10759c9e68d9Seric printf("openmailer: NULL\n"); 1076df106f0bSeric rcode = EX_UNAVAILABLE; 1077df106f0bSeric goto give_up; 10789c9e68d9Seric #endif /* DAEMON */ 10799c9e68d9Seric } 10809c9e68d9Seric else 10819c9e68d9Seric { 1082f170942cSeric if (TrafficLogFile != NULL) 10830e3bfef5Seric { 1084f170942cSeric char **av; 1085f170942cSeric 1086f170942cSeric fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1087f170942cSeric for (av = pv; *av != NULL; av++) 1088f170942cSeric fprintf(TrafficLogFile, " %s", *av); 1089f170942cSeric fprintf(TrafficLogFile, "\n"); 10906fe8c3bcSeric } 10916fe8c3bcSeric 10929c9e68d9Seric /* create a pipe to shove the mail through */ 10939c9e68d9Seric if (pipe(mpvect) < 0) 10949c9e68d9Seric { 10950e3bfef5Seric syserr("%s... openmailer(%s): pipe (to mailer)", 10960e3bfef5Seric e->e_to, m->m_name); 10979c9e68d9Seric if (tTd(11, 1)) 10989c9e68d9Seric printf("openmailer: NULL\n"); 10999c9e68d9Seric rcode = EX_OSERR; 11009c9e68d9Seric goto give_up; 11019c9e68d9Seric } 11029c9e68d9Seric 11039c9e68d9Seric /* if this mailer speaks smtp, create a return pipe */ 11049c9e68d9Seric if (clever && pipe(rpvect) < 0) 11059c9e68d9Seric { 11060e3bfef5Seric syserr("%s... openmailer(%s): pipe (from mailer)", 11070e3bfef5Seric e->e_to, m->m_name); 11089c9e68d9Seric (void) close(mpvect[0]); 11099c9e68d9Seric (void) close(mpvect[1]); 11109c9e68d9Seric if (tTd(11, 1)) 11119c9e68d9Seric printf("openmailer: NULL\n"); 11129c9e68d9Seric rcode = EX_OSERR; 11139c9e68d9Seric goto give_up; 11149c9e68d9Seric } 11159c9e68d9Seric 11169c9e68d9Seric /* 11179c9e68d9Seric ** Actually fork the mailer process. 11189c9e68d9Seric ** DOFORK is clever about retrying. 11199c9e68d9Seric ** 11209c9e68d9Seric ** Dispose of SIGCHLD signal catchers that may be laying 11219c9e68d9Seric ** around so that endmail will get it. 11229c9e68d9Seric */ 11239c9e68d9Seric 11249c9e68d9Seric if (e->e_xfp != NULL) 11259c9e68d9Seric (void) fflush(e->e_xfp); /* for debugging */ 11269c9e68d9Seric (void) fflush(stdout); 11279c9e68d9Seric # ifdef SIGCHLD 11282b9178d3Seric (void) setsignal(SIGCHLD, SIG_DFL); 11299c9e68d9Seric # endif /* SIGCHLD */ 11309c9e68d9Seric DOFORK(FORK); 11319c9e68d9Seric /* pid is set by DOFORK */ 11329c9e68d9Seric if (pid < 0) 11339c9e68d9Seric { 11349c9e68d9Seric /* failure */ 11350e3bfef5Seric syserr("%s... openmailer(%s): cannot fork", 11360e3bfef5Seric e->e_to, m->m_name); 11379c9e68d9Seric (void) close(mpvect[0]); 11389c9e68d9Seric (void) close(mpvect[1]); 11399c9e68d9Seric if (clever) 11409c9e68d9Seric { 11419c9e68d9Seric (void) close(rpvect[0]); 11429c9e68d9Seric (void) close(rpvect[1]); 11439c9e68d9Seric } 11449c9e68d9Seric if (tTd(11, 1)) 11459c9e68d9Seric printf("openmailer: NULL\n"); 11469c9e68d9Seric rcode = EX_OSERR; 11479c9e68d9Seric goto give_up; 11489c9e68d9Seric } 11499c9e68d9Seric else if (pid == 0) 11509c9e68d9Seric { 11519c9e68d9Seric int i; 11529c9e68d9Seric int saveerrno; 11539c9e68d9Seric char **ep; 11549c9e68d9Seric char *env[MAXUSERENVIRON]; 11559c9e68d9Seric extern char **environ; 11569c9e68d9Seric extern int DtableSize; 11579c9e68d9Seric 1158*f2e4d5e8Seric if (e->e_lockfp != NULL) 1159*f2e4d5e8Seric { 1160*f2e4d5e8Seric fclose(e->e_lockfp); 1161*f2e4d5e8Seric e->e_lockfp = NULL; 1162*f2e4d5e8Seric } 1163*f2e4d5e8Seric 11649c9e68d9Seric /* child -- set up input & exec mailer */ 11652b9178d3Seric (void) setsignal(SIGINT, SIG_IGN); 11662b9178d3Seric (void) setsignal(SIGHUP, SIG_IGN); 11672b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 11689c9e68d9Seric 116944f2317fSeric /* reset user and group */ 1170*f2e4d5e8Seric if (!bitnset(M_RESTR, m->m_flags)) 1171acc57dbfSeric { 1172*f2e4d5e8Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 1173*f2e4d5e8Seric { 1174*f2e4d5e8Seric (void) initgroups(DefUser, DefGid); 1175*f2e4d5e8Seric (void) setgid(DefGid); 1176*f2e4d5e8Seric (void) setuid(DefUid); 1177acc57dbfSeric } 1178*f2e4d5e8Seric else 117944f2317fSeric { 118044f2317fSeric (void) initgroups(ctladdr->q_ruser? 118144f2317fSeric ctladdr->q_ruser: ctladdr->q_user, 118244f2317fSeric ctladdr->q_gid); 118351cfe111Seric (void) setgid(ctladdr->q_gid); 118444f2317fSeric (void) setuid(ctladdr->q_uid); 118544f2317fSeric } 118644f2317fSeric } 118744f2317fSeric 118844f2317fSeric if (tTd(11, 2)) 118944f2317fSeric printf("openmailer: running as r/euid=%d/%d\n", 119044f2317fSeric getuid(), geteuid()); 119144f2317fSeric 1192b986f6aaSeric /* move into some "safe" directory */ 1193b986f6aaSeric if (m->m_execdir != NULL) 1194b986f6aaSeric { 1195b986f6aaSeric char *p, *q; 1196b986f6aaSeric char buf[MAXLINE]; 1197b986f6aaSeric 1198b986f6aaSeric for (p = m->m_execdir; p != NULL; p = q) 1199b986f6aaSeric { 1200b986f6aaSeric q = strchr(p, ':'); 1201b986f6aaSeric if (q != NULL) 1202b986f6aaSeric *q = '\0'; 1203b986f6aaSeric expand(p, buf, &buf[sizeof buf] - 1, e); 1204b986f6aaSeric if (q != NULL) 1205b986f6aaSeric *q++ = ':'; 1206b986f6aaSeric if (tTd(11, 20)) 1207b986f6aaSeric printf("openmailer: trydir %s\n", 1208b986f6aaSeric buf); 1209b986f6aaSeric if (buf[0] != '\0' && chdir(buf) >= 0) 1210b986f6aaSeric break; 1211b986f6aaSeric } 1212b986f6aaSeric } 1213b986f6aaSeric 12149c9e68d9Seric /* arrange to filter std & diag output of command */ 12159c9e68d9Seric if (clever) 12169c9e68d9Seric { 12179c9e68d9Seric (void) close(rpvect[0]); 12186fe8c3bcSeric if (dup2(rpvect[1], STDOUT_FILENO) < 0) 12196fe8c3bcSeric { 12200e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 12210e3bfef5Seric e->e_to, m->m_name, rpvect[1]); 12226fe8c3bcSeric _exit(EX_OSERR); 12236fe8c3bcSeric } 12249c9e68d9Seric (void) close(rpvect[1]); 12259c9e68d9Seric } 12268b40b0a4Seric else if (OpMode == MD_SMTP || OpMode == MD_DAEMON || 12278b40b0a4Seric HoldErrs || DisConnected) 12289c9e68d9Seric { 12299c9e68d9Seric /* put mailer output in transcript */ 12306fe8c3bcSeric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 12316fe8c3bcSeric { 12320e3bfef5Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 12330e3bfef5Seric e->e_to, m->m_name, 12346fe8c3bcSeric fileno(e->e_xfp)); 12356fe8c3bcSeric _exit(EX_OSERR); 12369c9e68d9Seric } 12376fe8c3bcSeric } 12386fe8c3bcSeric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 12396fe8c3bcSeric { 12400e3bfef5Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr", 12410e3bfef5Seric e->e_to, m->m_name); 12426fe8c3bcSeric _exit(EX_OSERR); 12436fe8c3bcSeric } 12449c9e68d9Seric 12459c9e68d9Seric /* arrange to get standard input */ 12469c9e68d9Seric (void) close(mpvect[1]); 12479c9e68d9Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 12489c9e68d9Seric { 12490e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 12500e3bfef5Seric e->e_to, m->m_name, mpvect[0]); 12519c9e68d9Seric _exit(EX_OSERR); 12529c9e68d9Seric } 12539c9e68d9Seric (void) close(mpvect[0]); 12549c9e68d9Seric 12559c9e68d9Seric /* arrange for all the files to be closed */ 12569c9e68d9Seric for (i = 3; i < DtableSize; i++) 12579c9e68d9Seric { 12589c9e68d9Seric register int j; 125944f2317fSeric 12609c9e68d9Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 12619c9e68d9Seric (void) fcntl(i, F_SETFD, j | 1); 12629c9e68d9Seric } 12639c9e68d9Seric 12649f9b003eSeric /* 12659f9b003eSeric ** Set up the mailer environment 12669f9b003eSeric ** TZ is timezone information. 12679f9b003eSeric ** SYSTYPE is Apollo software sys type (required). 12689f9b003eSeric ** ISP is Apollo hardware system type (required). 12699f9b003eSeric */ 12709f9b003eSeric 12719c9e68d9Seric i = 0; 12729c9e68d9Seric env[i++] = "AGENT=sendmail"; 12739c9e68d9Seric for (ep = environ; *ep != NULL; ep++) 12749c9e68d9Seric { 12759f9b003eSeric if (strncmp(*ep, "TZ=", 3) == 0 || 12769f9b003eSeric strncmp(*ep, "ISP=", 4) == 0 || 12779f9b003eSeric strncmp(*ep, "SYSTYPE=", 8) == 0) 12789c9e68d9Seric env[i++] = *ep; 12799c9e68d9Seric } 1280*f2e4d5e8Seric env[i++] = NULL; 12819c9e68d9Seric 1282929277f2Seric /* run disconnected from terminal */ 1283929277f2Seric (void) setsid(); 1284929277f2Seric 12859c9e68d9Seric /* try to execute the mailer */ 12869c9e68d9Seric execve(m->m_mailer, pv, env); 12879c9e68d9Seric saveerrno = errno; 12889c9e68d9Seric syserr("Cannot exec %s", m->m_mailer); 1289*f2e4d5e8Seric if (m == LocalMailer || transienterror(saveerrno)) 12907c941fd2Seric _exit(EX_OSERR); 12919c9e68d9Seric _exit(EX_UNAVAILABLE); 12929c9e68d9Seric } 12939c9e68d9Seric 12949c9e68d9Seric /* 12959c9e68d9Seric ** Set up return value. 12969c9e68d9Seric */ 12979c9e68d9Seric 12989c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 12999c9e68d9Seric bzero((char *) mci, sizeof *mci); 13009c9e68d9Seric mci->mci_mailer = m; 13019c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 13029c9e68d9Seric mci->mci_pid = pid; 13039c9e68d9Seric (void) close(mpvect[0]); 13049c9e68d9Seric mci->mci_out = fdopen(mpvect[1], "w"); 1305335eae58Seric if (mci->mci_out == NULL) 1306335eae58Seric { 1307335eae58Seric syserr("deliver: cannot create mailer output channel, fd=%d", 1308335eae58Seric mpvect[1]); 1309335eae58Seric (void) close(mpvect[1]); 1310335eae58Seric if (clever) 1311335eae58Seric { 1312335eae58Seric (void) close(rpvect[0]); 1313335eae58Seric (void) close(rpvect[1]); 1314335eae58Seric } 1315335eae58Seric rcode = EX_OSERR; 1316335eae58Seric goto give_up; 1317335eae58Seric } 13189c9e68d9Seric if (clever) 13199c9e68d9Seric { 13209c9e68d9Seric (void) close(rpvect[1]); 13219c9e68d9Seric mci->mci_in = fdopen(rpvect[0], "r"); 1322335eae58Seric if (mci->mci_in == NULL) 1323335eae58Seric { 1324335eae58Seric syserr("deliver: cannot create mailer input channel, fd=%d", 1325335eae58Seric mpvect[1]); 1326335eae58Seric (void) close(rpvect[0]); 1327335eae58Seric fclose(mci->mci_out); 1328335eae58Seric mci->mci_out = NULL; 1329335eae58Seric rcode = EX_OSERR; 1330335eae58Seric goto give_up; 1331335eae58Seric } 13329c9e68d9Seric } 13339c9e68d9Seric else 13349c9e68d9Seric { 13359c9e68d9Seric mci->mci_flags |= MCIF_TEMP; 13369c9e68d9Seric mci->mci_in = NULL; 13379c9e68d9Seric } 13389c9e68d9Seric } 13399c9e68d9Seric 13409c9e68d9Seric /* 13419c9e68d9Seric ** If we are in SMTP opening state, send initial protocol. 13429c9e68d9Seric */ 13439c9e68d9Seric 13449c9e68d9Seric if (clever && mci->mci_state != MCIS_CLOSED) 13459c9e68d9Seric { 13469c9e68d9Seric smtpinit(m, mci, e); 13479c9e68d9Seric } 13489c9e68d9Seric if (tTd(11, 1)) 13499c9e68d9Seric { 13509c9e68d9Seric printf("openmailer: "); 13514052916eSeric mci_dump(mci, FALSE); 13529c9e68d9Seric } 13539c9e68d9Seric 13549c9e68d9Seric if (mci->mci_state != MCIS_OPEN) 1355b31e7f2bSeric { 1356b31e7f2bSeric /* couldn't open the mailer */ 1357b31e7f2bSeric rcode = mci->mci_exitstat; 13582a6bc25bSeric errno = mci->mci_errno; 13599d4a8008Seric #if NAMED_BIND 1360f170942cSeric h_errno = mci->mci_herrno; 1361f170942cSeric #endif 1362b31e7f2bSeric if (rcode == EX_OK) 1363b31e7f2bSeric { 1364b31e7f2bSeric /* shouldn't happen */ 136508b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 13666b0f339dSeric rcode, mci->mci_state, firstsig); 1367b31e7f2bSeric rcode = EX_SOFTWARE; 1368b31e7f2bSeric } 1369cb082c5bSeric else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0') 137090891494Seric { 137190891494Seric /* try next MX site */ 137290891494Seric goto tryhost; 137390891494Seric } 1374b31e7f2bSeric } 1375b31e7f2bSeric else if (!clever) 1376b31e7f2bSeric { 1377b31e7f2bSeric /* 1378b31e7f2bSeric ** Format and send message. 1379b31e7f2bSeric */ 138015d084d5Seric 13815aa0f353Seric putfromline(mci, e); 1382*f2e4d5e8Seric (*e->e_puthdr)(mci, e); 1383*f2e4d5e8Seric putline("\n", mci); 13846ffc2dd1Seric (*e->e_putbody)(mci, e, NULL); 1385b31e7f2bSeric 1386b31e7f2bSeric /* get the exit status */ 1387c9be6216Seric rcode = endmailer(mci, e, pv); 1388134746fbSeric } 1389134746fbSeric else 1390b31e7f2bSeric #ifdef SMTP 1391134746fbSeric { 1392b31e7f2bSeric /* 1393b31e7f2bSeric ** Send the MAIL FROM: protocol 1394b31e7f2bSeric */ 139515d084d5Seric 1396b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 1397b31e7f2bSeric if (rcode == EX_OK) 139875889e88Seric { 1399ded0d3daSkarels register char *t = tobuf; 1400ded0d3daSkarels register int i; 1401ded0d3daSkarels 1402588cad61Seric /* send the recipient list */ 140363780dbdSeric tobuf[0] = '\0'; 140475889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 140575889e88Seric { 140663780dbdSeric e->e_to = to->q_paddr; 140715d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 140875889e88Seric { 1409*f2e4d5e8Seric markfailure(e, to, i); 1410*f2e4d5e8Seric giveresponse(i, m, mci, ctladdr, e); 141163780dbdSeric } 141275889e88Seric else 141375889e88Seric { 1414911693bfSbostic *t++ = ','; 1415b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 1416b31e7f2bSeric continue; 1417ee39df61Seric *t = '\0'; 1418588cad61Seric } 1419588cad61Seric } 1420588cad61Seric 142163780dbdSeric /* now send the data */ 142263780dbdSeric if (tobuf[0] == '\0') 1423b31e7f2bSeric { 14249c9e68d9Seric rcode = EX_OK; 142563780dbdSeric e->e_to = NULL; 1426b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 1427b31e7f2bSeric smtprset(m, mci, e); 1428b31e7f2bSeric } 142975889e88Seric else 143075889e88Seric { 143163780dbdSeric e->e_to = tobuf + 1; 143275889e88Seric rcode = smtpdata(m, mci, e); 143363780dbdSeric } 143463780dbdSeric 143563780dbdSeric /* now close the connection */ 1436b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 143715d084d5Seric smtpquit(m, mci, e); 143863780dbdSeric } 1439cb082c5bSeric if (rcode != EX_OK && curhost != NULL && *curhost != '\0') 14409c9e68d9Seric { 14419c9e68d9Seric /* try next MX site */ 14429c9e68d9Seric goto tryhost; 14439c9e68d9Seric } 1444c579ef51Seric } 1445b31e7f2bSeric #else /* not SMTP */ 1446a05b3449Sbostic { 144708b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 1448845e533cSeric rcode = EX_CONFIG; 1449b31e7f2bSeric goto give_up; 1450a05b3449Sbostic } 1451b31e7f2bSeric #endif /* SMTP */ 14529d4a8008Seric #if NAMED_BIND 14532bcc6d2dSeric if (ConfigLevel < 2) 1454912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1455134746fbSeric #endif 14565dfc646bSeric 1457b31e7f2bSeric /* arrange a return receipt if requested */ 1458df106f0bSeric if (rcode == EX_OK && e->e_receiptto != NULL && 1459df106f0bSeric bitnset(M_LOCALMAILER, m->m_flags)) 1460b31e7f2bSeric { 1461b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 1462b31e7f2bSeric /* do we want to send back more info? */ 1463b31e7f2bSeric } 1464b31e7f2bSeric 1465c77d1c25Seric /* 146663780dbdSeric ** Do final status disposal. 146763780dbdSeric ** We check for something in tobuf for the SMTP case. 1468c77d1c25Seric ** If we got a temporary failure, arrange to queue the 1469c77d1c25Seric ** addressees. 1470c77d1c25Seric */ 1471c77d1c25Seric 1472b31e7f2bSeric give_up: 147363780dbdSeric if (tobuf[0] != '\0') 1474*f2e4d5e8Seric giveresponse(rcode, m, mci, ctladdr, e); 1475772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 1476b31e7f2bSeric { 1477dde5acadSeric if (rcode != EX_OK) 1478*f2e4d5e8Seric markfailure(e, to, rcode); 1479*f2e4d5e8Seric else 1480655518ecSeric { 1481dde5acadSeric to->q_flags |= QSENT; 1482655518ecSeric e->e_nsent++; 1483*f2e4d5e8Seric if (e->e_receiptto != NULL && 1484*f2e4d5e8Seric bitnset(M_LOCALMAILER, m->m_flags)) 1485df106f0bSeric { 1486df106f0bSeric fprintf(e->e_xfp, "%s... Successfully delivered\n", 1487df106f0bSeric to->q_paddr); 1488df106f0bSeric } 1489655518ecSeric } 1490b31e7f2bSeric } 1491b31e7f2bSeric 1492b31e7f2bSeric /* 1493b31e7f2bSeric ** Restore state and return. 1494b31e7f2bSeric */ 1495c77d1c25Seric 14967febfd66Seric #ifdef XDEBUG 14977febfd66Seric { 14987febfd66Seric char wbuf[MAXLINE]; 14997febfd66Seric 15007febfd66Seric /* make absolutely certain 0, 1, and 2 are in use */ 150144622ea3Seric sprintf(wbuf, "%s... end of deliver(%s)", 150244622ea3Seric e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 150344622ea3Seric m->m_name); 15047febfd66Seric checkfd012(wbuf); 15057febfd66Seric } 15067febfd66Seric #endif 15077febfd66Seric 150835490626Seric errno = 0; 1509588cad61Seric define('g', (char *) NULL, e); 15105826d9d3Seric return (rcode); 151125a99e2eSeric } 15125dfc646bSeric /* 151383b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 151483b7ddc9Seric ** 151583b7ddc9Seric ** Parameters: 151683b7ddc9Seric ** e -- the envelope we are sending. 151783b7ddc9Seric ** q -- the address to mark. 151883b7ddc9Seric ** rcode -- the code signifying the particular failure. 151983b7ddc9Seric ** 152083b7ddc9Seric ** Returns: 152183b7ddc9Seric ** none. 152283b7ddc9Seric ** 152383b7ddc9Seric ** Side Effects: 152483b7ddc9Seric ** marks the address (and possibly the envelope) with the 152583b7ddc9Seric ** failure so that an error will be returned or 152683b7ddc9Seric ** the message will be queued, as appropriate. 152783b7ddc9Seric */ 152883b7ddc9Seric 1529*f2e4d5e8Seric markfailure(e, q, rcode) 153083b7ddc9Seric register ENVELOPE *e; 153183b7ddc9Seric register ADDRESS *q; 153283b7ddc9Seric int rcode; 153383b7ddc9Seric { 1534*f2e4d5e8Seric char buf[MAXLINE]; 153519c47125Seric 1536d5aafa3cSeric switch (rcode) 1537d5aafa3cSeric { 1538d5aafa3cSeric case EX_OK: 1539d5aafa3cSeric break; 1540d5aafa3cSeric 1541d5aafa3cSeric case EX_TEMPFAIL: 1542d5aafa3cSeric case EX_IOERR: 1543d5aafa3cSeric case EX_OSERR: 154483b7ddc9Seric q->q_flags |= QQUEUEUP; 1545d5aafa3cSeric break; 1546d5aafa3cSeric 1547d5aafa3cSeric default: 1548f170942cSeric q->q_flags |= QBADADDR; 1549d5aafa3cSeric break; 1550d5aafa3cSeric } 155183b7ddc9Seric } 155283b7ddc9Seric /* 1553c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 1554c579ef51Seric ** 1555c579ef51Seric ** We should never get fatal errors (e.g., segmentation 1556c579ef51Seric ** violation), so we report those specially. For other 1557c579ef51Seric ** errors, we choose a status message (into statmsg), 1558c579ef51Seric ** and if it represents an error, we print it. 1559c579ef51Seric ** 1560c579ef51Seric ** Parameters: 1561c579ef51Seric ** pid -- pid of mailer. 1562c9be6216Seric ** e -- the current envelope. 1563c9be6216Seric ** pv -- the parameter vector that invoked the mailer 1564c9be6216Seric ** (for error messages). 1565c579ef51Seric ** 1566c579ef51Seric ** Returns: 1567c579ef51Seric ** exit code of mailer. 1568c579ef51Seric ** 1569c579ef51Seric ** Side Effects: 1570c579ef51Seric ** none. 1571c579ef51Seric */ 1572c579ef51Seric 1573c9be6216Seric endmailer(mci, e, pv) 1574b31e7f2bSeric register MCI *mci; 1575c9be6216Seric register ENVELOPE *e; 1576c9be6216Seric char **pv; 1577c579ef51Seric { 1578588cad61Seric int st; 1579c579ef51Seric 158075889e88Seric /* close any connections */ 158175889e88Seric if (mci->mci_in != NULL) 158276f3d53fSeric (void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in"); 158375889e88Seric if (mci->mci_out != NULL) 158476f3d53fSeric (void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out"); 158575889e88Seric mci->mci_in = mci->mci_out = NULL; 158675889e88Seric mci->mci_state = MCIS_CLOSED; 158775889e88Seric 158833db8731Seric /* in the IPC case there is nothing to wait for */ 158975889e88Seric if (mci->mci_pid == 0) 159033db8731Seric return (EX_OK); 159133db8731Seric 159233db8731Seric /* wait for the mailer process to die and collect status */ 159375889e88Seric st = waitfor(mci->mci_pid); 1594588cad61Seric if (st == -1) 159578de67c1Seric { 1596c9be6216Seric syserr("endmailer %s: wait", pv[0]); 1597588cad61Seric return (EX_SOFTWARE); 1598c579ef51Seric } 159933db8731Seric 1600bf9bc890Seric if (WIFEXITED(st)) 1601c579ef51Seric { 1602bf9bc890Seric /* normal death -- return status */ 1603bf9bc890Seric return (WEXITSTATUS(st)); 1604bf9bc890Seric } 1605bf9bc890Seric 1606bf9bc890Seric /* it died a horrid death */ 1607550092c3Seric syserr("451 mailer %s died with signal %o", 1608550092c3Seric mci->mci_mailer->m_name, st); 1609c9be6216Seric 1610c9be6216Seric /* log the arguments */ 16113c930db3Seric if (pv != NULL && e->e_xfp != NULL) 1612c9be6216Seric { 1613c9be6216Seric register char **av; 1614c9be6216Seric 1615c9be6216Seric fprintf(e->e_xfp, "Arguments:"); 1616c9be6216Seric for (av = pv; *av != NULL; av++) 1617c9be6216Seric fprintf(e->e_xfp, " %s", *av); 1618c9be6216Seric fprintf(e->e_xfp, "\n"); 1619c9be6216Seric } 1620c9be6216Seric 16215f73204aSeric ExitStat = EX_TEMPFAIL; 16225f73204aSeric return (EX_TEMPFAIL); 1623c579ef51Seric } 1624c579ef51Seric /* 162525a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 162625a99e2eSeric ** 162725a99e2eSeric ** Parameters: 162825a99e2eSeric ** stat -- the status code from the mailer (high byte 162925a99e2eSeric ** only; core dumps must have been taken care of 163025a99e2eSeric ** already). 163181161401Seric ** m -- the mailer info for this mailer. 163281161401Seric ** mci -- the mailer connection info -- can be NULL if the 163381161401Seric ** response is given before the connection is made. 16344dee0003Seric ** ctladdr -- the controlling address for the recipient 16354dee0003Seric ** address(es). 163681161401Seric ** e -- the current envelope. 163725a99e2eSeric ** 163825a99e2eSeric ** Returns: 1639db8841e9Seric ** none. 164025a99e2eSeric ** 164125a99e2eSeric ** Side Effects: 1642c1f9df2cSeric ** Errors may be incremented. 164325a99e2eSeric ** ExitStat may be set. 164425a99e2eSeric */ 164525a99e2eSeric 1646*f2e4d5e8Seric giveresponse(stat, m, mci, ctladdr, e) 164725a99e2eSeric int stat; 1648588cad61Seric register MAILER *m; 164981161401Seric register MCI *mci; 16504dee0003Seric ADDRESS *ctladdr; 1651198d9be0Seric ENVELOPE *e; 165225a99e2eSeric { 16539c16475dSeric register const char *statmsg; 165425a99e2eSeric extern char *SysExMsg[]; 165525a99e2eSeric register int i; 1656d4bd8f0eSbostic extern int N_SysEx; 1657198d9be0Seric char buf[MAXLINE]; 165825a99e2eSeric 165913bbc08cSeric /* 166013bbc08cSeric ** Compute status message from code. 166113bbc08cSeric */ 166213bbc08cSeric 166325a99e2eSeric i = stat - EX__BASE; 1664588cad61Seric if (stat == 0) 16656fe8c3bcSeric { 1666588cad61Seric statmsg = "250 Sent"; 1667ce5531bdSeric if (e->e_statmsg != NULL) 16686fe8c3bcSeric { 1669ce5531bdSeric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 16706fe8c3bcSeric statmsg = buf; 16716fe8c3bcSeric } 16726fe8c3bcSeric } 1673588cad61Seric else if (i < 0 || i > N_SysEx) 1674588cad61Seric { 1675588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1676588cad61Seric stat = EX_UNAVAILABLE; 1677588cad61Seric statmsg = buf; 1678588cad61Seric } 1679198d9be0Seric else if (stat == EX_TEMPFAIL) 1680198d9be0Seric { 16817d55540cSeric (void) strcpy(buf, SysExMsg[i] + 1); 16829d4a8008Seric #if NAMED_BIND 1683f28da541Smiriam if (h_errno == TRY_AGAIN) 16844d50702aSeric statmsg = errstring(h_errno+E_DNSBASE); 1685f28da541Smiriam else 1686d4bd8f0eSbostic #endif 1687f28da541Smiriam { 16888557d168Seric if (errno != 0) 1689d87e85f3Seric statmsg = errstring(errno); 1690d87e85f3Seric else 1691d87e85f3Seric { 1692d87e85f3Seric #ifdef SMTP 1693d87e85f3Seric statmsg = SmtpError; 16946c2c3107Seric #else /* SMTP */ 1695d87e85f3Seric statmsg = NULL; 16966c2c3107Seric #endif /* SMTP */ 1697d87e85f3Seric } 1698f28da541Smiriam } 1699d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1700d87e85f3Seric { 170187c9b3e7Seric (void) strcat(buf, ": "); 1702d87e85f3Seric (void) strcat(buf, statmsg); 17038557d168Seric } 1704198d9be0Seric statmsg = buf; 1705198d9be0Seric } 17069d4a8008Seric #if NAMED_BIND 1707f170942cSeric else if (stat == EX_NOHOST && h_errno != 0) 1708f170942cSeric { 17094d50702aSeric statmsg = errstring(h_errno + E_DNSBASE); 1710862934b2Seric (void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg); 1711f170942cSeric statmsg = buf; 1712f170942cSeric } 1713f170942cSeric #endif 171425a99e2eSeric else 1715d87e85f3Seric { 171625a99e2eSeric statmsg = SysExMsg[i]; 17177d55540cSeric if (*statmsg++ == ':') 17187d55540cSeric { 17197d55540cSeric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 17207d55540cSeric statmsg = buf; 17217d55540cSeric } 1722d87e85f3Seric } 1723588cad61Seric 1724588cad61Seric /* 1725588cad61Seric ** Print the message as appropriate 1726588cad61Seric */ 1727588cad61Seric 1728198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 1729df106f0bSeric { 1730df106f0bSeric extern char MsgBuf[]; 1731df106f0bSeric 17321f96bd0bSeric message("%s", &statmsg[4]); 1733df106f0bSeric if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 1734df106f0bSeric fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 1735df106f0bSeric } 173625a99e2eSeric else 173725a99e2eSeric { 1738862934b2Seric char mbuf[8]; 1739862934b2Seric 1740c1f9df2cSeric Errors++; 1741862934b2Seric sprintf(mbuf, "%.3s %%s", statmsg); 1742862934b2Seric usrerr(mbuf, &statmsg[4]); 174325a99e2eSeric } 174425a99e2eSeric 174525a99e2eSeric /* 174625a99e2eSeric ** Final cleanup. 174725a99e2eSeric ** Log a record of the transaction. Compute the new 174825a99e2eSeric ** ExitStat -- if we already had an error, stick with 174925a99e2eSeric ** that. 175025a99e2eSeric */ 175125a99e2eSeric 17522f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 1753*f2e4d5e8Seric logdelivery(m, mci, &statmsg[4], ctladdr, e); 1754eb238f8cSeric 17555d3f0ed4Seric if (tTd(11, 2)) 17565d3f0ed4Seric printf("giveresponse: stat=%d, e->e_message=%s\n", 17572c8ac7e0Seric stat, e->e_message == NULL ? "<NULL>" : e->e_message); 17585d3f0ed4Seric 1759eb238f8cSeric if (stat != EX_TEMPFAIL) 1760eb238f8cSeric setstat(stat); 17611097d4c9Seric if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL)) 1762198d9be0Seric { 1763198d9be0Seric if (e->e_message != NULL) 1764198d9be0Seric free(e->e_message); 1765198d9be0Seric e->e_message = newstr(&statmsg[4]); 1766198d9be0Seric } 17678557d168Seric errno = 0; 17689d4a8008Seric #if NAMED_BIND 1769f28da541Smiriam h_errno = 0; 1770d4bd8f0eSbostic #endif 1771eb238f8cSeric } 1772eb238f8cSeric /* 1773eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1774eb238f8cSeric ** 17752c9b4f99Seric ** Care is taken to avoid logging lines that are too long, because 17762c9b4f99Seric ** some versions of syslog have an unfortunate proclivity for core 17772c9b4f99Seric ** dumping. This is a hack, to be sure, that is at best empirical. 17782c9b4f99Seric ** 1779eb238f8cSeric ** Parameters: 178081161401Seric ** m -- the mailer info. Can be NULL for initial queue. 178181161401Seric ** mci -- the mailer connection info -- can be NULL if the 178281161401Seric ** log is occuring when no connection is active. 178381161401Seric ** stat -- the message to print for the status. 17844dee0003Seric ** ctladdr -- the controlling address for the to list. 178581161401Seric ** e -- the current envelope. 1786eb238f8cSeric ** 1787eb238f8cSeric ** Returns: 1788eb238f8cSeric ** none 1789eb238f8cSeric ** 1790eb238f8cSeric ** Side Effects: 1791eb238f8cSeric ** none 1792eb238f8cSeric */ 1793eb238f8cSeric 1794*f2e4d5e8Seric logdelivery(m, mci, stat, ctladdr, e) 179581161401Seric MAILER *m; 179681161401Seric register MCI *mci; 1797eb238f8cSeric char *stat; 17984dee0003Seric ADDRESS *ctladdr; 1799b31e7f2bSeric register ENVELOPE *e; 18005cf56be3Seric { 1801eb238f8cSeric # ifdef LOG 18024dee0003Seric register char *bp; 18032c9b4f99Seric register char *p; 18042c9b4f99Seric int l; 1805d6acf3eeSeric char buf[512]; 18069507d1f9Seric 18073a100e8bSeric # if (SYSLOG_BUFSIZE) >= 256 18084dee0003Seric bp = buf; 18094dee0003Seric if (ctladdr != NULL) 18104dee0003Seric { 18114dee0003Seric strcpy(bp, ", ctladdr="); 1812f62f08c7Seric strcat(bp, shortenstring(ctladdr->q_paddr, 83)); 18134dee0003Seric bp += strlen(bp); 18144dee0003Seric if (bitset(QGOODUID, ctladdr->q_flags)) 18154dee0003Seric { 18164dee0003Seric (void) sprintf(bp, " (%d/%d)", 18174dee0003Seric ctladdr->q_uid, ctladdr->q_gid); 18184dee0003Seric bp += strlen(bp); 18194dee0003Seric } 18204dee0003Seric } 18214dee0003Seric 1822*f2e4d5e8Seric (void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 18234dee0003Seric bp += strlen(bp); 182481161401Seric 182548ed5d33Seric if (m != NULL) 182671ff6caaSeric { 18274dee0003Seric (void) strcpy(bp, ", mailer="); 18284dee0003Seric (void) strcat(bp, m->m_name); 18294dee0003Seric bp += strlen(bp); 183071ff6caaSeric } 183148ed5d33Seric 183248ed5d33Seric if (mci != NULL && mci->mci_host != NULL) 183371ff6caaSeric { 183471ff6caaSeric # ifdef DAEMON 1835e2f2f828Seric extern SOCKADDR CurHostAddr; 183648ed5d33Seric # endif 183771ff6caaSeric 18384dee0003Seric (void) strcpy(bp, ", relay="); 18394dee0003Seric (void) strcat(bp, mci->mci_host); 184048ed5d33Seric 184148ed5d33Seric # ifdef DAEMON 18422bdfc6b9Seric (void) strcat(bp, " ["); 18434dee0003Seric (void) strcat(bp, anynet_ntoa(&CurHostAddr)); 18442bdfc6b9Seric (void) strcat(bp, "]"); 184571ff6caaSeric # endif 184671ff6caaSeric } 1847bcb9b028Seric else if (strcmp(stat, "queued") != 0) 184848ed5d33Seric { 184948ed5d33Seric char *p = macvalue('h', e); 18509507d1f9Seric 185148ed5d33Seric if (p != NULL && p[0] != '\0') 185248ed5d33Seric { 18534dee0003Seric (void) strcpy(bp, ", relay="); 18544dee0003Seric (void) strcat(bp, p); 185548ed5d33Seric } 185648ed5d33Seric } 1857cb082c5bSeric bp += strlen(bp); 1858d6acf3eeSeric 18593a100e8bSeric #define STATLEN (((SYSLOG_BUFSIZE) - 100) / 4) 18603a100e8bSeric #if (STATLEN) < 63 18613a100e8bSeric # undef STATLEN 18623a100e8bSeric # define STATLEN 63 18633a100e8bSeric #endif 18643a100e8bSeric #if (STATLEN) > 203 18653a100e8bSeric # undef STATLEN 18663a100e8bSeric # define STATLEN 203 18673a100e8bSeric #endif 18683a100e8bSeric 18693a100e8bSeric if ((bp - buf) > (sizeof buf - ((STATLEN) + 20))) 18702c9b4f99Seric { 18712c9b4f99Seric /* desperation move -- truncate data */ 18723a100e8bSeric bp = buf + sizeof buf - ((STATLEN) + 17); 18732c9b4f99Seric strcpy(bp, "..."); 18742c9b4f99Seric bp += 3; 18752c9b4f99Seric } 18762c9b4f99Seric 18772c9b4f99Seric (void) strcpy(bp, ", stat="); 18782c9b4f99Seric bp += strlen(bp); 18793a100e8bSeric 18803a100e8bSeric (void) strcpy(bp, shortenstring(stat, (STATLEN))); 18812c9b4f99Seric 18822c9b4f99Seric l = SYSLOG_BUFSIZE - 100 - strlen(buf); 18832c9b4f99Seric p = e->e_to; 18842c9b4f99Seric while (strlen(p) >= l) 18852c9b4f99Seric { 18862c9b4f99Seric register char *q = strchr(p + l, ','); 18872c9b4f99Seric 18882a1b6b73Seric if (q == NULL) 18892c9b4f99Seric break; 18902c9b4f99Seric syslog(LOG_INFO, "%s: to=%.*s [more]%s", 18912c9b4f99Seric e->e_id, ++q - p, p, buf); 18922c9b4f99Seric p = q; 18932c9b4f99Seric } 18942c9b4f99Seric syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf); 18953a100e8bSeric 18963a100e8bSeric # else /* we have a very short log buffer size */ 18973a100e8bSeric 189827607809Seric l = SYSLOG_BUFSIZE - 85; 18993a100e8bSeric p = e->e_to; 19003a100e8bSeric while (strlen(p) >= l) 19013a100e8bSeric { 19023a100e8bSeric register char *q = strchr(p + l, ','); 19033a100e8bSeric 19043a100e8bSeric if (q == NULL) 19053a100e8bSeric break; 19063a100e8bSeric syslog(LOG_INFO, "%s: to=%.*s [more]", 19073a100e8bSeric e->e_id, ++q - p, p); 19083a100e8bSeric p = q; 19093a100e8bSeric } 19103a100e8bSeric syslog(LOG_INFO, "%s: to=%s", e->e_id, p); 19113a100e8bSeric 19123a100e8bSeric if (ctladdr != NULL) 19133a100e8bSeric { 19143a100e8bSeric bp = buf; 19153a100e8bSeric strcpy(buf, "ctladdr="); 19163a100e8bSeric bp += strlen(buf); 19173a100e8bSeric strcpy(bp, shortenstring(ctladdr->q_paddr, 83)); 19183a100e8bSeric bp += strlen(buf); 19193a100e8bSeric if (bitset(QGOODUID, ctladdr->q_flags)) 19203a100e8bSeric { 19213a100e8bSeric (void) sprintf(bp, " (%d/%d)", 19223a100e8bSeric ctladdr->q_uid, ctladdr->q_gid); 19233a100e8bSeric bp += strlen(bp); 19243a100e8bSeric } 19253a100e8bSeric syslog(LOG_INFO, "%s: %s", e->e_id, buf); 19263a100e8bSeric } 19274e797715Seric bp = buf; 19284e797715Seric sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 19294e797715Seric bp += strlen(bp); 19303a100e8bSeric 19313a100e8bSeric if (m != NULL) 19324e797715Seric { 19334e797715Seric sprintf(bp, ", mailer=%s", m->m_name); 19344e797715Seric bp += strlen(bp); 19354e797715Seric } 1936b056abd0Seric syslog(LOG_INFO, "%s: %s", e->e_id, buf); 19373a100e8bSeric 1938b056abd0Seric buf[0] = '\0'; 19393a100e8bSeric if (mci != NULL && mci->mci_host != NULL) 19403a100e8bSeric { 19413a100e8bSeric # ifdef DAEMON 19423a100e8bSeric extern SOCKADDR CurHostAddr; 19433a100e8bSeric # endif 19443a100e8bSeric 1945d2ece200Seric sprintf(buf, "relay=%s", mci->mci_host); 19463a100e8bSeric 19473a100e8bSeric # ifdef DAEMON 1948b056abd0Seric (void) strcat(buf, " ["); 1949b056abd0Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 1950b056abd0Seric (void) strcat(buf, "]"); 19513a100e8bSeric # endif 19523a100e8bSeric } 1953bcb9b028Seric else if (strcmp(stat, "queued") != 0) 19543a100e8bSeric { 19553a100e8bSeric char *p = macvalue('h', e); 19563a100e8bSeric 19573a100e8bSeric if (p != NULL && p[0] != '\0') 1958d2ece200Seric sprintf(buf, "relay=%s", p); 19593a100e8bSeric } 1960b056abd0Seric if (buf[0] != '\0') 19614e797715Seric syslog(LOG_INFO, "%s: %s", e->e_id, buf); 19623a100e8bSeric 19633a100e8bSeric syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63)); 19643a100e8bSeric # endif /* short log buffer */ 19656c2c3107Seric # endif /* LOG */ 196625a99e2eSeric } 196725a99e2eSeric /* 196851552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 196925a99e2eSeric ** 197051552439Seric ** This can be made an arbitrary message separator by changing $l 197151552439Seric ** 19729b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 19739b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 19749b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 19759b6c17a6Seric ** this kind of antique garbage???? 197625a99e2eSeric ** 197725a99e2eSeric ** Parameters: 19785aa0f353Seric ** mci -- the connection information. 19795aa0f353Seric ** e -- the envelope. 198025a99e2eSeric ** 198125a99e2eSeric ** Returns: 198251552439Seric ** none 198325a99e2eSeric ** 198425a99e2eSeric ** Side Effects: 198551552439Seric ** outputs some text to fp. 198625a99e2eSeric */ 198725a99e2eSeric 19885aa0f353Seric putfromline(mci, e) 19895aa0f353Seric register MCI *mci; 1990b31e7f2bSeric ENVELOPE *e; 199125a99e2eSeric { 19922bc47524Seric char *template = "\201l\n"; 199351552439Seric char buf[MAXLINE]; 199425a99e2eSeric 19955aa0f353Seric if (bitnset(M_NHDR, mci->mci_mailer->m_flags)) 199651552439Seric return; 199713bbc08cSeric 19982c7e1b8dSeric # ifdef UGLYUUCP 19995aa0f353Seric if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags)) 200074b6e67bSeric { 2001ea09d6edSeric char *bang; 2002ea09d6edSeric char xbuf[MAXLINE]; 200374b6e67bSeric 2004ee4b0922Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 20056c2c3107Seric bang = strchr(buf, '!'); 200674b6e67bSeric if (bang == NULL) 200734fcca25Seric { 200834fcca25Seric errno = 0; 200934fcca25Seric syserr("554 No ! in UUCP From address! (%s given)", buf); 201034fcca25Seric } 201174b6e67bSeric else 2012588cad61Seric { 2013ea09d6edSeric *bang++ = '\0'; 20142bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 2015ea09d6edSeric template = xbuf; 201674b6e67bSeric } 2017588cad61Seric } 20186c2c3107Seric # endif /* UGLYUUCP */ 2019b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 20205aa0f353Seric putline(buf, mci); 2021bc6e2962Seric } 2022bc6e2962Seric /* 202351552439Seric ** PUTBODY -- put the body of a message. 202451552439Seric ** 202551552439Seric ** Parameters: 20265aa0f353Seric ** mci -- the connection information. 20279a6a5f55Seric ** e -- the envelope to put out. 202803c02fdeSeric ** separator -- if non-NULL, a message separator that must 202903c02fdeSeric ** not be permitted in the resulting message. 203051552439Seric ** 203151552439Seric ** Returns: 203251552439Seric ** none. 203351552439Seric ** 203451552439Seric ** Side Effects: 203551552439Seric ** The message is written onto fp. 203651552439Seric */ 203751552439Seric 20386ffc2dd1Seric putbody(mci, e, separator) 20395aa0f353Seric register MCI *mci; 20409a6a5f55Seric register ENVELOPE *e; 204103c02fdeSeric char *separator; 204251552439Seric { 204377b52738Seric char buf[MAXLINE]; 204451552439Seric 204551552439Seric /* 204651552439Seric ** Output the body of the message 204751552439Seric */ 204851552439Seric 2049*f2e4d5e8Seric if (e->e_dfp == NULL) 2050*f2e4d5e8Seric { 2051*f2e4d5e8Seric if (e->e_df != NULL) 20529a6a5f55Seric { 20539a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 20549a6a5f55Seric if (e->e_dfp == NULL) 20558f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 2056e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 20579a6a5f55Seric } 2058*f2e4d5e8Seric else 2059c80705fdSeric putline("<<< No Message Collected >>>", mci); 2060c80705fdSeric } 2061*f2e4d5e8Seric if (e->e_dfp != NULL) 2062519e7d80Seric { 20639a6a5f55Seric rewind(e->e_dfp); 2064*f2e4d5e8Seric while (!ferror(mci->mci_out) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 2065a6ce3008Seric { 20665aa0f353Seric if (buf[0] == 'F' && 20675aa0f353Seric bitnset(M_ESCFROM, mci->mci_mailer->m_flags) && 2068d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 2069*f2e4d5e8Seric (void) putc('>', mci->mci_out); 2070*f2e4d5e8Seric if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 207103c02fdeSeric { 207203c02fdeSeric /* possible separator */ 207303c02fdeSeric int sl = strlen(separator); 207403c02fdeSeric 207503c02fdeSeric if (strncmp(&buf[2], separator, sl) == 0) 2076*f2e4d5e8Seric (void) putc(' ', mci->mci_out); 207703c02fdeSeric } 2078*f2e4d5e8Seric putline(buf, mci); 2079a6ce3008Seric } 208051552439Seric 20819a6a5f55Seric if (ferror(e->e_dfp)) 208251552439Seric { 2083df106f0bSeric syserr("putbody: %s: read error", e->e_df); 208451552439Seric ExitStat = EX_IOERR; 208551552439Seric } 2086*f2e4d5e8Seric } 208751552439Seric 20880890ba1fSeric /* some mailers want extra blank line at end of message */ 20895aa0f353Seric if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) && 20905aa0f353Seric buf[0] != '\0' && buf[0] != '\n') 20915aa0f353Seric putline("", mci); 20920890ba1fSeric 20935aa0f353Seric (void) fflush(mci->mci_out); 20945aa0f353Seric if (ferror(mci->mci_out) && errno != EPIPE) 209551552439Seric { 209651552439Seric syserr("putbody: write error"); 209751552439Seric ExitStat = EX_IOERR; 209851552439Seric } 209951552439Seric errno = 0; 210025a99e2eSeric } 210125a99e2eSeric /* 210225a99e2eSeric ** MAILFILE -- Send a message to a file. 210325a99e2eSeric ** 2104f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 2105f129ec7dSeric ** bits, sendmail will try to become the owner of that file 2106f129ec7dSeric ** rather than the real user. Obviously, this only works if 2107f129ec7dSeric ** sendmail runs as root. 2108f129ec7dSeric ** 2109588cad61Seric ** This could be done as a subordinate mailer, except that it 2110588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 2111588cad61Seric ** view this as being sufficiently important as to include it 2112588cad61Seric ** here. For example, if the system is dying, we shouldn't have 2113588cad61Seric ** to create another process plus some pipes to save the message. 2114588cad61Seric ** 211525a99e2eSeric ** Parameters: 211625a99e2eSeric ** filename -- the name of the file to send to. 21176259796dSeric ** ctladdr -- the controlling address header -- includes 21186259796dSeric ** the userid/groupid to be when sending. 211925a99e2eSeric ** 212025a99e2eSeric ** Returns: 212125a99e2eSeric ** The exit code associated with the operation. 212225a99e2eSeric ** 212325a99e2eSeric ** Side Effects: 212425a99e2eSeric ** none. 212525a99e2eSeric */ 212625a99e2eSeric 2127b31e7f2bSeric mailfile(filename, ctladdr, e) 212825a99e2eSeric char *filename; 21296259796dSeric ADDRESS *ctladdr; 2130b31e7f2bSeric register ENVELOPE *e; 213125a99e2eSeric { 213225a99e2eSeric register FILE *f; 2133*f2e4d5e8Seric register int pid; 213415d084d5Seric int mode; 213525a99e2eSeric 2136671745f3Seric if (tTd(11, 1)) 2137671745f3Seric { 2138671745f3Seric printf("mailfile %s\n ctladdr=", filename); 2139671745f3Seric printaddr(ctladdr, FALSE); 2140671745f3Seric } 2141671745f3Seric 2142f170942cSeric if (e->e_xfp != NULL) 2143f170942cSeric fflush(e->e_xfp); 2144f170942cSeric 214532d19d43Seric /* 214632d19d43Seric ** Fork so we can change permissions here. 214732d19d43Seric ** Note that we MUST use fork, not vfork, because of 214832d19d43Seric ** the complications of calling subroutines, etc. 214932d19d43Seric */ 215032d19d43Seric 215132d19d43Seric DOFORK(fork); 215232d19d43Seric 215332d19d43Seric if (pid < 0) 215432d19d43Seric return (EX_OSERR); 215532d19d43Seric else if (pid == 0) 215632d19d43Seric { 215732d19d43Seric /* child -- actually write to file */ 2158f129ec7dSeric struct stat stb; 2159*f2e4d5e8Seric struct stat fsb; 21605aa0f353Seric MCI mcibuf; 2161f129ec7dSeric 2162*f2e4d5e8Seric if (e->e_lockfp != NULL) 2163*f2e4d5e8Seric { 2164*f2e4d5e8Seric fclose(e->e_lockfp); 2165*f2e4d5e8Seric e->e_lockfp = NULL; 2166*f2e4d5e8Seric } 2167*f2e4d5e8Seric 21682b9178d3Seric (void) setsignal(SIGINT, SIG_DFL); 21692b9178d3Seric (void) setsignal(SIGHUP, SIG_DFL); 21702b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 21713462ad9eSeric (void) umask(OldUmask); 217295f16dc0Seric 2173f129ec7dSeric if (stat(filename, &stb) < 0) 21743a98e7eaSeric stb.st_mode = FileMode; 2175*f2e4d5e8Seric else if (bitset(0111, stb.st_mode) || stb.st_nlink != 1) 2176*f2e4d5e8Seric exit(EX_CANTCREAT); 217715d084d5Seric mode = stb.st_mode; 217895f16dc0Seric 217995f16dc0Seric /* limit the errors to those actually caused in the child */ 218095f16dc0Seric errno = 0; 218195f16dc0Seric ExitStat = EX_OK; 218295f16dc0Seric 218319428781Seric if (ctladdr != NULL) 218415d084d5Seric { 218515d084d5Seric /* ignore setuid and setgid bits */ 218615d084d5Seric mode &= ~(S_ISGID|S_ISUID); 218715d084d5Seric } 218815d084d5Seric 21898f9146b0Srick /* we have to open the dfile BEFORE setuid */ 21908f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 21918f9146b0Srick { 21928f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 219395f16dc0Seric if (e->e_dfp == NULL) 219495f16dc0Seric { 21958f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 2196e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 21978f9146b0Srick } 21988f9146b0Srick } 21998f9146b0Srick 220015d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 2201e36b99e2Seric { 2202*f2e4d5e8Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 2203*f2e4d5e8Seric { 2204*f2e4d5e8Seric (void) initgroups(DefUser, DefGid); 2205*f2e4d5e8Seric } 2206*f2e4d5e8Seric else 2207*f2e4d5e8Seric { 2208898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 2209898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 2210898a126bSbostic ctladdr->q_gid); 2211*f2e4d5e8Seric } 2212e36b99e2Seric } 221315d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 2214e36b99e2Seric { 2215*f2e4d5e8Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 2216c80705fdSeric (void) setuid(DefUid); 2217*f2e4d5e8Seric else 2218*f2e4d5e8Seric (void) setuid(ctladdr->q_uid); 2219e36b99e2Seric } 222095f16dc0Seric FileName = filename; 222195f16dc0Seric LineNumber = 0; 22223a98e7eaSeric f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 222325a99e2eSeric if (f == NULL) 222495f16dc0Seric { 2225b6a0de9dSeric message("554 cannot open: %s", errstring(errno)); 222632d19d43Seric exit(EX_CANTCREAT); 222795f16dc0Seric } 2228*f2e4d5e8Seric if (fstat(fileno(f), &fsb) < 0 || 2229*f2e4d5e8Seric stb.st_nlink != fsb.st_nlink || 2230*f2e4d5e8Seric stb.st_dev != fsb.st_dev || 2231*f2e4d5e8Seric stb.st_ino != fsb.st_ino || 2232*f2e4d5e8Seric stb.st_uid != fsb.st_uid) 2233*f2e4d5e8Seric { 2234*f2e4d5e8Seric message("554 cannot write: file changed after open"); 2235*f2e4d5e8Seric exit(EX_CANTCREAT); 2236*f2e4d5e8Seric } 223725a99e2eSeric 22385aa0f353Seric bzero(&mcibuf, sizeof mcibuf); 22395aa0f353Seric mcibuf.mci_mailer = FileMailer; 22405aa0f353Seric mcibuf.mci_out = f; 22415aa0f353Seric if (bitnset(M_7BITS, FileMailer->m_flags)) 22425aa0f353Seric mcibuf.mci_flags |= MCIF_7BIT; 22435aa0f353Seric 22445aa0f353Seric putfromline(&mcibuf, e); 2245*f2e4d5e8Seric (*e->e_puthdr)(&mcibuf, e); 2246*f2e4d5e8Seric putline("\n", &mcibuf); 22476ffc2dd1Seric (*e->e_putbody)(&mcibuf, e, NULL); 22485aa0f353Seric putline("\n", &mcibuf); 224995f16dc0Seric if (ferror(f)) 225095f16dc0Seric { 2251b6a0de9dSeric message("451 I/O error: %s", errstring(errno)); 225295f16dc0Seric setstat(EX_IOERR); 225395f16dc0Seric } 2254ee4b0922Seric (void) xfclose(f, "mailfile", filename); 225532d19d43Seric (void) fflush(stdout); 2256e36b99e2Seric 225727628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 2258c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 225995f16dc0Seric exit(ExitStat); 226013bbc08cSeric /*NOTREACHED*/ 226132d19d43Seric } 226232d19d43Seric else 226332d19d43Seric { 226432d19d43Seric /* parent -- wait for exit status */ 2265588cad61Seric int st; 226632d19d43Seric 2267588cad61Seric st = waitfor(pid); 2268bf9bc890Seric if (WIFEXITED(st)) 2269bf9bc890Seric return (WEXITSTATUS(st)); 2270588cad61Seric else 2271b6a0de9dSeric { 2272b6a0de9dSeric syserr("child died on signal %d", st); 2273bf9bc890Seric return (EX_UNAVAILABLE); 2274b6a0de9dSeric } 22758f9146b0Srick /*NOTREACHED*/ 227632d19d43Seric } 227725a99e2eSeric } 2278ea4dc939Seric /* 2279e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 2280e103b48fSeric ** 2281e103b48fSeric ** The signature describes how we are going to send this -- it 2282e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 2283e103b48fSeric ** an ordered list of MX hosts. 2284e103b48fSeric ** 2285e103b48fSeric ** Parameters: 2286e103b48fSeric ** m -- the mailer describing this host. 2287e103b48fSeric ** host -- the host name. 2288e103b48fSeric ** e -- the current envelope. 2289e103b48fSeric ** 2290e103b48fSeric ** Returns: 2291e103b48fSeric ** The signature for this host. 2292e103b48fSeric ** 2293e103b48fSeric ** Side Effects: 2294e103b48fSeric ** Can tweak the symbol table. 2295e103b48fSeric */ 2296e103b48fSeric 2297e103b48fSeric char * 2298e103b48fSeric hostsignature(m, host, e) 2299e103b48fSeric register MAILER *m; 2300e103b48fSeric char *host; 2301e103b48fSeric ENVELOPE *e; 2302e103b48fSeric { 2303e103b48fSeric register char *p; 2304e103b48fSeric register STAB *s; 2305e103b48fSeric int i; 2306e103b48fSeric int len; 23079d4a8008Seric #if NAMED_BIND 2308e103b48fSeric int nmx; 2309e103b48fSeric auto int rcode; 2310bafdc4e5Seric char *hp; 2311bafdc4e5Seric char *endp; 2312*f2e4d5e8Seric int oldoptions; 2313e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 2314e103b48fSeric #endif 2315e103b48fSeric 2316e103b48fSeric /* 2317e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 2318e103b48fSeric */ 2319e103b48fSeric 2320e103b48fSeric p = m->m_mailer; 2321e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2322e103b48fSeric { 2323e103b48fSeric /* just an ordinary mailer */ 2324e103b48fSeric return host; 2325e103b48fSeric } 2326e103b48fSeric 2327e103b48fSeric /* 2328e103b48fSeric ** Look it up in the symbol table. 2329e103b48fSeric */ 2330e103b48fSeric 2331e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 2332e103b48fSeric if (s->s_hostsig != NULL) 2333e103b48fSeric return s->s_hostsig; 2334e103b48fSeric 2335e103b48fSeric /* 2336e103b48fSeric ** Not already there -- create a signature. 2337e103b48fSeric */ 2338e103b48fSeric 23399d4a8008Seric #if NAMED_BIND 2340516782b4Seric if (ConfigLevel < 2) 2341*f2e4d5e8Seric { 2342*f2e4d5e8Seric oldoptions = _res.options; 2343516782b4Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2344*f2e4d5e8Seric } 2345516782b4Seric 2346bafdc4e5Seric for (hp = host; hp != NULL; hp = endp) 2347bafdc4e5Seric { 2348bafdc4e5Seric endp = strchr(hp, ':'); 2349bafdc4e5Seric if (endp != NULL) 2350bafdc4e5Seric *endp = '\0'; 2351bafdc4e5Seric 23527bf809e6Seric nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 23537d55540cSeric 2354e103b48fSeric if (nmx <= 0) 2355e103b48fSeric { 2356e103b48fSeric register MCI *mci; 2357e103b48fSeric 2358e103b48fSeric /* update the connection info for this host */ 2359bafdc4e5Seric mci = mci_get(hp, m); 2360e103b48fSeric mci->mci_exitstat = rcode; 2361e103b48fSeric mci->mci_errno = errno; 2362*f2e4d5e8Seric #if NAMED_BIND 2363f170942cSeric mci->mci_herrno = h_errno; 2364*f2e4d5e8Seric #endif 2365e103b48fSeric 2366e103b48fSeric /* and return the original host name as the signature */ 2367bafdc4e5Seric nmx = 1; 2368bafdc4e5Seric mxhosts[0] = hp; 2369e103b48fSeric } 2370e103b48fSeric 2371e103b48fSeric len = 0; 2372e103b48fSeric for (i = 0; i < nmx; i++) 2373e103b48fSeric { 2374e103b48fSeric len += strlen(mxhosts[i]) + 1; 2375e103b48fSeric } 2376bafdc4e5Seric if (s->s_hostsig != NULL) 2377bafdc4e5Seric len += strlen(s->s_hostsig) + 1; 2378bafdc4e5Seric p = xalloc(len); 2379bafdc4e5Seric if (s->s_hostsig != NULL) 2380bafdc4e5Seric { 2381bafdc4e5Seric (void) strcpy(p, s->s_hostsig); 2382bafdc4e5Seric free(s->s_hostsig); 2383bafdc4e5Seric s->s_hostsig = p; 2384bafdc4e5Seric p += strlen(p); 2385bafdc4e5Seric *p++ = ':'; 2386bafdc4e5Seric } 2387bafdc4e5Seric else 2388bafdc4e5Seric s->s_hostsig = p; 2389e103b48fSeric for (i = 0; i < nmx; i++) 2390e103b48fSeric { 2391e103b48fSeric if (i != 0) 2392e103b48fSeric *p++ = ':'; 2393e103b48fSeric strcpy(p, mxhosts[i]); 2394e103b48fSeric p += strlen(p); 2395e103b48fSeric } 2396bafdc4e5Seric if (endp != NULL) 2397bafdc4e5Seric *endp++ = ':'; 2398bafdc4e5Seric } 2399e103b48fSeric makelower(s->s_hostsig); 2400516782b4Seric if (ConfigLevel < 2) 2401516782b4Seric _res.options = oldoptions; 2402e103b48fSeric #else 2403e103b48fSeric /* not using BIND -- the signature is just the host name */ 2404e103b48fSeric s->s_hostsig = host; 2405e103b48fSeric #endif 2406e103b48fSeric if (tTd(17, 1)) 2407e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2408e103b48fSeric return s->s_hostsig; 2409e103b48fSeric } 2410