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*a6ce3008Seric static char sccsid[] = "@(#)deliver.c 8.88 (Berkeley) 07/23/94"; 11e70a7521Sbostic #endif /* not lint */ 124227346bSdist 13fcde9cc8Sbostic #include "sendmail.h" 14f28da541Smiriam #include <netdb.h> 15911693bfSbostic #include <errno.h> 169d4a8008Seric #if NAMED_BIND 17912a731aSbostic #include <resolv.h> 18f170942cSeric 19f170942cSeric extern int h_errno; 20134746fbSeric #endif 2125a99e2eSeric 225d437c4dSeric extern char SmtpError[]; 235d437c4dSeric 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; 49b056abd0Seric register ENVELOPE *ee; 50b056abd0Seric ENVELOPE *splitenv = NULL; 516103d9b4Seric bool announcequeueup; 529c9e68d9Seric 537880f3e4Seric /* 547880f3e4Seric ** If we have had global, fatal errors, don't bother sending 557880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 567880f3e4Seric ** (e.g., a single address failing) will still cause the other 577880f3e4Seric ** addresses to be sent. 587880f3e4Seric */ 597880f3e4Seric 608d19a23dSeric if (bitset(EF_FATALERRS, e->e_flags) && 618d19a23dSeric (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 62896b16f6Seric { 63896b16f6Seric e->e_flags |= EF_CLRQUEUE; 64896b16f6Seric return; 65896b16f6Seric } 66896b16f6Seric 679c9e68d9Seric /* determine actual delivery mode */ 68fbe2f963Seric CurrentLA = getla(); 699c9e68d9Seric if (mode == SM_DEFAULT) 709c9e68d9Seric { 719c9e68d9Seric mode = e->e_sendmode; 729c9e68d9Seric if (mode != SM_VERIFY && 739c9e68d9Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 749c9e68d9Seric mode = SM_QUEUE; 756103d9b4Seric announcequeueup = mode == SM_QUEUE; 769c9e68d9Seric } 776103d9b4Seric else 786103d9b4Seric announcequeueup = FALSE; 799c9e68d9Seric 809c9e68d9Seric if (tTd(13, 1)) 819c9e68d9Seric { 820e484fe5Seric printf("\n===== SENDALL: mode %c, id %s, e_from ", 830e484fe5Seric mode, e->e_id); 849c9e68d9Seric printaddr(&e->e_from, FALSE); 859c9e68d9Seric printf("sendqueue:\n"); 869c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 879c9e68d9Seric } 889c9e68d9Seric 899c9e68d9Seric /* 909c9e68d9Seric ** Do any preprocessing necessary for the mode we are running. 919c9e68d9Seric ** Check to make sure the hop count is reasonable. 929c9e68d9Seric ** Delete sends to the sender in mailing lists. 939c9e68d9Seric */ 949c9e68d9Seric 959c9e68d9Seric CurEnv = e; 969c9e68d9Seric 979c9e68d9Seric if (e->e_hopcount > MaxHopCount) 989c9e68d9Seric { 999c9e68d9Seric errno = 0; 10027607809Seric e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE; 1014bb8b0fdSeric syserr("554 too many hops %d (%d max): from %s via %s, to %s", 1029c9e68d9Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 103f7869e68Seric RealHostName == NULL ? "localhost" : RealHostName, 104f7869e68Seric e->e_sendqueue->q_paddr); 1059c9e68d9Seric return; 1069c9e68d9Seric } 1079c9e68d9Seric 108b8004690Seric /* 109b8004690Seric ** Do sender deletion. 110b8004690Seric ** 111b8004690Seric ** If the sender has the QQUEUEUP flag set, skip this. 112b8004690Seric ** This can happen if the name server is hosed when you 113b8004690Seric ** are trying to send mail. The result is that the sender 114b8004690Seric ** is instantiated in the queue as a recipient. 115b8004690Seric */ 116b8004690Seric 117e76a6a8fSeric if (!bitset(EF_METOO, e->e_flags) && 118e76a6a8fSeric !bitset(QQUEUEUP, e->e_from.q_flags)) 1199c9e68d9Seric { 1209c9e68d9Seric if (tTd(13, 5)) 1219c9e68d9Seric { 1229c9e68d9Seric printf("sendall: QDONTSEND "); 1239c9e68d9Seric printaddr(&e->e_from, FALSE); 1249c9e68d9Seric } 1259c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 1269c9e68d9Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 1279c9e68d9Seric } 1289c9e68d9Seric 129ce5531bdSeric /* 130ce5531bdSeric ** Handle alias owners. 131ce5531bdSeric ** 132ce5531bdSeric ** We scan up the q_alias chain looking for owners. 133ce5531bdSeric ** We discard owners that are the same as the return path. 134ce5531bdSeric */ 135ce5531bdSeric 136ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 137ce5531bdSeric { 138ce5531bdSeric register struct address *a; 139ce5531bdSeric 140ce5531bdSeric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 141ce5531bdSeric continue; 142ce5531bdSeric if (a != NULL) 143ce5531bdSeric q->q_owner = a->q_owner; 144ce5531bdSeric 145ce5531bdSeric if (q->q_owner != NULL && 146ce5531bdSeric !bitset(QDONTSEND, q->q_flags) && 147ce5531bdSeric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 148ce5531bdSeric q->q_owner = NULL; 149ce5531bdSeric } 150ce5531bdSeric 151ce5531bdSeric owner = ""; 152ce5531bdSeric otherowners = 1; 153ce5531bdSeric while (owner != NULL && otherowners > 0) 154ce5531bdSeric { 155ce5531bdSeric owner = NULL; 156ce5531bdSeric otherowners = 0; 157ce5531bdSeric 158ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 159ce5531bdSeric { 160ce5531bdSeric if (bitset(QDONTSEND, q->q_flags)) 161ce5531bdSeric continue; 162ce5531bdSeric 163ce5531bdSeric if (q->q_owner != NULL) 164ce5531bdSeric { 165ce5531bdSeric if (owner == NULL) 166ce5531bdSeric owner = q->q_owner; 167ce5531bdSeric else if (owner != q->q_owner) 168ce5531bdSeric { 169ce5531bdSeric if (strcmp(owner, q->q_owner) == 0) 170ce5531bdSeric { 171ce5531bdSeric /* make future comparisons cheap */ 172ce5531bdSeric q->q_owner = owner; 173ce5531bdSeric } 174ce5531bdSeric else 175ce5531bdSeric { 176ce5531bdSeric otherowners++; 177ce5531bdSeric } 178ce5531bdSeric owner = q->q_owner; 179ce5531bdSeric } 180ce5531bdSeric } 181ce5531bdSeric else 182ce5531bdSeric { 183ce5531bdSeric otherowners++; 184ce5531bdSeric } 185ce5531bdSeric } 186ce5531bdSeric 187ce5531bdSeric if (owner != NULL && otherowners > 0) 188ce5531bdSeric { 189ce5531bdSeric extern HDR *copyheader(); 190ce5531bdSeric extern ADDRESS *copyqueue(); 191ce5531bdSeric 192ce5531bdSeric /* 193ce5531bdSeric ** Split this envelope into two. 194ce5531bdSeric */ 195ce5531bdSeric 196ce5531bdSeric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 197ce5531bdSeric *ee = *e; 198ce5531bdSeric ee->e_id = NULL; 199ce5531bdSeric (void) queuename(ee, '\0'); 200ce5531bdSeric 201ce5531bdSeric if (tTd(13, 1)) 202ce5531bdSeric printf("sendall: split %s into %s\n", 203ce5531bdSeric e->e_id, ee->e_id); 204ce5531bdSeric 205ce5531bdSeric ee->e_header = copyheader(e->e_header); 206ce5531bdSeric ee->e_sendqueue = copyqueue(e->e_sendqueue); 207ce5531bdSeric ee->e_errorqueue = copyqueue(e->e_errorqueue); 208fce3c07bSeric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT); 209fce3c07bSeric ee->e_flags |= EF_NORECEIPT; 210ce5531bdSeric setsender(owner, ee, NULL, TRUE); 211ce5531bdSeric if (tTd(13, 5)) 212ce5531bdSeric { 213ce5531bdSeric printf("sendall(split): QDONTSEND "); 214ce5531bdSeric printaddr(&ee->e_from, FALSE); 215ce5531bdSeric } 216ce5531bdSeric ee->e_from.q_flags |= QDONTSEND; 217ce5531bdSeric ee->e_dfp = NULL; 218ce5531bdSeric ee->e_xfp = NULL; 219ce5531bdSeric ee->e_df = NULL; 220ce5531bdSeric ee->e_errormode = EM_MAIL; 221b056abd0Seric ee->e_sibling = splitenv; 222b056abd0Seric splitenv = ee; 223ce5531bdSeric 224ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 225ce5531bdSeric if (q->q_owner == owner) 226de1a6b1bSeric { 227ce5531bdSeric q->q_flags |= QDONTSEND; 228de1a6b1bSeric q->q_flags &= ~QQUEUEUP; 229de1a6b1bSeric } 230ce5531bdSeric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 231ce5531bdSeric if (q->q_owner != owner) 232de1a6b1bSeric { 233ce5531bdSeric q->q_flags |= QDONTSEND; 234de1a6b1bSeric q->q_flags &= ~QQUEUEUP; 235de1a6b1bSeric } 236ce5531bdSeric 237ce5531bdSeric if (e->e_df != NULL && mode != SM_VERIFY) 238ce5531bdSeric { 239ce5531bdSeric ee->e_dfp = NULL; 240da662164Seric ee->e_df = queuename(ee, 'd'); 241da662164Seric ee->e_df = newstr(ee->e_df); 242ce5531bdSeric if (link(e->e_df, ee->e_df) < 0) 243ce5531bdSeric { 244ce5531bdSeric syserr("sendall: link(%s, %s)", 245ce5531bdSeric e->e_df, ee->e_df); 246ce5531bdSeric } 247ce5531bdSeric } 248ce5531bdSeric #ifdef LOG 249ce5531bdSeric if (LogLevel > 4) 250c8b70947Seric syslog(LOG_INFO, "%s: clone %s, owner=%s", 251c8b70947Seric ee->e_id, e->e_id, owner); 252ce5531bdSeric #endif 253ce5531bdSeric } 254ce5531bdSeric } 255ce5531bdSeric 256ce5531bdSeric if (owner != NULL) 257ce5531bdSeric { 258ce5531bdSeric setsender(owner, e, NULL, TRUE); 259ce5531bdSeric if (tTd(13, 5)) 260ce5531bdSeric { 261ce5531bdSeric printf("sendall(owner): QDONTSEND "); 262ce5531bdSeric printaddr(&e->e_from, FALSE); 263ce5531bdSeric } 264ce5531bdSeric e->e_from.q_flags |= QDONTSEND; 265ce5531bdSeric e->e_errormode = EM_MAIL; 266fce3c07bSeric e->e_flags |= EF_NORECEIPT; 267ce5531bdSeric } 268ce5531bdSeric 269c1ac89b1Seric # ifdef QUEUE 270c1ac89b1Seric if ((mode == SM_QUEUE || mode == SM_FORK || 271c1ac89b1Seric (mode != SM_VERIFY && SuperSafe)) && 272c1ac89b1Seric !bitset(EF_INQUEUE, e->e_flags)) 273c1ac89b1Seric { 274c1ac89b1Seric /* be sure everything is instantiated in the queue */ 2756103d9b4Seric queueup(e, TRUE, announcequeueup); 276b056abd0Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 277b056abd0Seric queueup(ee, TRUE, announcequeueup); 278c1ac89b1Seric } 279c1ac89b1Seric #endif /* QUEUE */ 280c1ac89b1Seric 281b056abd0Seric if (splitenv != NULL) 282b056abd0Seric { 283b056abd0Seric if (tTd(13, 1)) 284b056abd0Seric { 285b056abd0Seric printf("\nsendall: Split queue; remaining queue:\n"); 286b056abd0Seric printaddr(e->e_sendqueue, TRUE); 287b056abd0Seric } 288b056abd0Seric 289b056abd0Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 290b056abd0Seric { 291b056abd0Seric CurEnv = ee; 292562ec147Seric if (mode != SM_VERIFY) 293562ec147Seric openxscript(ee); 294b056abd0Seric sendenvelope(ee, mode); 295562ec147Seric dropenvelope(ee); 296b056abd0Seric } 297b056abd0Seric 298b056abd0Seric CurEnv = e; 299b056abd0Seric } 300b056abd0Seric sendenvelope(e, mode); 301b056abd0Seric } 302b056abd0Seric 303b056abd0Seric sendenvelope(e, mode) 304b056abd0Seric register ENVELOPE *e; 305b056abd0Seric char mode; 306b056abd0Seric { 307b056abd0Seric bool oldverbose; 308b056abd0Seric int pid; 309b056abd0Seric register ADDRESS *q; 310b056abd0Seric char *qf; 311b056abd0Seric char *id; 312b056abd0Seric 3137880f3e4Seric /* 3147880f3e4Seric ** If we have had global, fatal errors, don't bother sending 3157880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 3167880f3e4Seric ** (e.g., a single address failing) will still cause the other 3177880f3e4Seric ** addresses to be sent. 3187880f3e4Seric */ 3197880f3e4Seric 3208d19a23dSeric if (bitset(EF_FATALERRS, e->e_flags) && 3218d19a23dSeric (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 3227880f3e4Seric { 3237880f3e4Seric e->e_flags |= EF_CLRQUEUE; 3247880f3e4Seric return; 3257880f3e4Seric } 3267880f3e4Seric 327ce5531bdSeric oldverbose = Verbose; 328c1ac89b1Seric switch (mode) 329c1ac89b1Seric { 330c1ac89b1Seric case SM_VERIFY: 331c1ac89b1Seric Verbose = TRUE; 332c1ac89b1Seric break; 333c1ac89b1Seric 334c1ac89b1Seric case SM_QUEUE: 335c1ac89b1Seric queueonly: 336c1ac89b1Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 337c1ac89b1Seric return; 338c1ac89b1Seric 339c1ac89b1Seric case SM_FORK: 340c1ac89b1Seric if (e->e_xfp != NULL) 341c1ac89b1Seric (void) fflush(e->e_xfp); 342c1ac89b1Seric 343b269aa8eSeric # if !HASFLOCK 344c1ac89b1Seric /* 3456b2765c6Seric ** Since fcntl locking has the interesting semantic that 3466b2765c6Seric ** the lock is owned by a process, not by an open file 3476b2765c6Seric ** descriptor, we have to flush this to the queue, and 3486b2765c6Seric ** then restart from scratch in the child. 349c1ac89b1Seric */ 350c1ac89b1Seric 3516b2765c6Seric /* save id for future use */ 3526b2765c6Seric id = e->e_id; 3536b2765c6Seric 3546b2765c6Seric /* now drop the envelope in the parent */ 3556b2765c6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 3566b2765c6Seric dropenvelope(e); 3576b2765c6Seric 3586b2765c6Seric /* and reacquire in the child */ 3596b2765c6Seric (void) dowork(id, TRUE, FALSE, e); 3606b2765c6Seric 3616b2765c6Seric return; 3626b2765c6Seric 3636b2765c6Seric # else /* HASFLOCK */ 364c1ac89b1Seric 365c1ac89b1Seric pid = fork(); 366c1ac89b1Seric if (pid < 0) 367c1ac89b1Seric { 368c1ac89b1Seric goto queueonly; 369c1ac89b1Seric } 370c1ac89b1Seric else if (pid > 0) 371c1ac89b1Seric { 3720e484fe5Seric /* be sure we leave the temp files to our child */ 3730e484fe5Seric /* can't call unlockqueue to avoid unlink of xfp */ 3740e484fe5Seric if (e->e_lockfp != NULL) 3750e484fe5Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 3760e484fe5Seric e->e_lockfp = NULL; 3770e484fe5Seric 3780e484fe5Seric /* close any random open files in the envelope */ 3790e484fe5Seric closexscript(e); 3800e484fe5Seric if (e->e_dfp != NULL) 3810e484fe5Seric (void) xfclose(e->e_dfp, "sendenvelope", e->e_df); 3820e484fe5Seric e->e_dfp = NULL; 3830e484fe5Seric e->e_id = e->e_df = NULL; 3849f9b003eSeric 3859f9b003eSeric /* catch intermediate zombie */ 3869f9b003eSeric (void) waitfor(pid); 387c1ac89b1Seric return; 388c1ac89b1Seric } 389c1ac89b1Seric 390c1ac89b1Seric /* double fork to avoid zombies */ 3919f9b003eSeric pid = fork(); 3929f9b003eSeric if (pid > 0) 393c1ac89b1Seric exit(EX_OK); 394c1ac89b1Seric 395c1ac89b1Seric /* be sure we are immune from the terminal */ 3967880f3e4Seric disconnect(1, e); 397c1ac89b1Seric 3989f9b003eSeric /* prevent parent from waiting if there was an error */ 3999f9b003eSeric if (pid < 0) 4009f9b003eSeric { 4019f9b003eSeric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 4029f9b003eSeric finis(); 4039f9b003eSeric } 4049f9b003eSeric 405c1ac89b1Seric /* 406c1ac89b1Seric ** Close any cached connections. 407c1ac89b1Seric ** 408c1ac89b1Seric ** We don't send the QUIT protocol because the parent 409c1ac89b1Seric ** still knows about the connection. 410c1ac89b1Seric ** 411c1ac89b1Seric ** This should only happen when delivering an error 412c1ac89b1Seric ** message. 413c1ac89b1Seric */ 414c1ac89b1Seric 415c1ac89b1Seric mci_flush(FALSE, NULL); 416c1ac89b1Seric 4176b2765c6Seric # endif /* HASFLOCK */ 4186b2765c6Seric 419c1ac89b1Seric break; 420c1ac89b1Seric } 421c1ac89b1Seric 422c1ac89b1Seric /* 4239c9e68d9Seric ** Run through the list and send everything. 4245288e21aSeric ** 4255288e21aSeric ** Set EF_GLOBALERRS so that error messages during delivery 4265288e21aSeric ** result in returned mail. 4279c9e68d9Seric */ 4289c9e68d9Seric 4299c9e68d9Seric e->e_nsent = 0; 4305288e21aSeric e->e_flags |= EF_GLOBALERRS; 431faad2b16Seric 432faad2b16Seric /* now run through the queue */ 4339c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 4349c9e68d9Seric { 435fdc75a0fSeric #ifdef XDEBUG 436fdc75a0fSeric char wbuf[MAXNAME + 20]; 437fdc75a0fSeric 438fdc75a0fSeric (void) sprintf(wbuf, "sendall(%s)", q->q_paddr); 439fdc75a0fSeric checkfd012(wbuf); 440fdc75a0fSeric #endif 4419c9e68d9Seric if (mode == SM_VERIFY) 4429c9e68d9Seric { 4439c9e68d9Seric e->e_to = q->q_paddr; 4449c9e68d9Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4458dfca105Seric { 446fafe46e1Seric if (q->q_host != NULL && q->q_host[0] != '\0') 4478dfca105Seric message("deliverable: mailer %s, host %s, user %s", 4488dfca105Seric q->q_mailer->m_name, 4498dfca105Seric q->q_host, 4508dfca105Seric q->q_user); 451fafe46e1Seric else 452fafe46e1Seric message("deliverable: mailer %s, user %s", 453fafe46e1Seric q->q_mailer->m_name, 454fafe46e1Seric q->q_user); 4558dfca105Seric } 4569c9e68d9Seric } 4579c9e68d9Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4589c9e68d9Seric { 4599c9e68d9Seric # ifdef QUEUE 4609c9e68d9Seric /* 4619c9e68d9Seric ** Checkpoint the send list every few addresses 4629c9e68d9Seric */ 4639c9e68d9Seric 4649c9e68d9Seric if (e->e_nsent >= CheckpointInterval) 4659c9e68d9Seric { 4669c9e68d9Seric queueup(e, TRUE, FALSE); 4679c9e68d9Seric e->e_nsent = 0; 4689c9e68d9Seric } 4699c9e68d9Seric # endif /* QUEUE */ 4709c9e68d9Seric (void) deliver(e, q); 4719c9e68d9Seric } 4729c9e68d9Seric } 4739c9e68d9Seric Verbose = oldverbose; 4749c9e68d9Seric 475fdc75a0fSeric #ifdef XDEBUG 476fdc75a0fSeric checkfd012("end of sendenvelope"); 477fdc75a0fSeric #endif 478fdc75a0fSeric 4799c9e68d9Seric if (mode == SM_FORK) 4809c9e68d9Seric finis(); 4819c9e68d9Seric } 4829c9e68d9Seric /* 4839c9e68d9Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4849c9e68d9Seric ** 4859c9e68d9Seric ** This MUST be a macro, since after a vfork we are running 4869c9e68d9Seric ** two processes on the same stack!!! 4879c9e68d9Seric ** 4889c9e68d9Seric ** Parameters: 4899c9e68d9Seric ** none. 4909c9e68d9Seric ** 4919c9e68d9Seric ** Returns: 4929c9e68d9Seric ** From a macro??? You've got to be kidding! 4939c9e68d9Seric ** 4949c9e68d9Seric ** Side Effects: 4959c9e68d9Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4969c9e68d9Seric ** pid of child in parent, zero in child. 4979c9e68d9Seric ** -1 on unrecoverable error. 4989c9e68d9Seric ** 4999c9e68d9Seric ** Notes: 5009c9e68d9Seric ** I'm awfully sorry this looks so awful. That's 5019c9e68d9Seric ** vfork for you..... 5029c9e68d9Seric */ 5039c9e68d9Seric 5049c9e68d9Seric # define NFORKTRIES 5 5059c9e68d9Seric 5069c9e68d9Seric # ifndef FORK 5079c9e68d9Seric # define FORK fork 5089c9e68d9Seric # endif 5099c9e68d9Seric 5109c9e68d9Seric # define DOFORK(fORKfN) \ 5119c9e68d9Seric {\ 5129c9e68d9Seric register int i;\ 5139c9e68d9Seric \ 5149c9e68d9Seric for (i = NFORKTRIES; --i >= 0; )\ 5159c9e68d9Seric {\ 5169c9e68d9Seric pid = fORKfN();\ 5179c9e68d9Seric if (pid >= 0)\ 5189c9e68d9Seric break;\ 5199c9e68d9Seric if (i > 0)\ 5209c9e68d9Seric sleep((unsigned) NFORKTRIES - i);\ 5219c9e68d9Seric }\ 5229c9e68d9Seric } 5239c9e68d9Seric /* 5249c9e68d9Seric ** DOFORK -- simple fork interface to DOFORK. 5259c9e68d9Seric ** 5269c9e68d9Seric ** Parameters: 5279c9e68d9Seric ** none. 5289c9e68d9Seric ** 5299c9e68d9Seric ** Returns: 5309c9e68d9Seric ** pid of child in parent. 5319c9e68d9Seric ** zero in child. 5329c9e68d9Seric ** -1 on error. 5339c9e68d9Seric ** 5349c9e68d9Seric ** Side Effects: 5359c9e68d9Seric ** returns twice, once in parent and once in child. 5369c9e68d9Seric */ 5379c9e68d9Seric 5389c9e68d9Seric dofork() 5399c9e68d9Seric { 5409c9e68d9Seric register int pid; 5419c9e68d9Seric 5429c9e68d9Seric DOFORK(fork); 5439c9e68d9Seric return (pid); 5449c9e68d9Seric } 5459c9e68d9Seric /* 54613bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 54713bbc08cSeric ** 54813bbc08cSeric ** This routine delivers to everyone on the same host as the 54913bbc08cSeric ** user on the head of the list. It is clever about mailers 55013bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 55113bbc08cSeric ** that it will deliver to all these addresses however -- so 55213bbc08cSeric ** deliver should be called once for each address on the 55313bbc08cSeric ** list. 55425a99e2eSeric ** 55525a99e2eSeric ** Parameters: 556588cad61Seric ** e -- the envelope to deliver. 557c77d1c25Seric ** firstto -- head of the address list to deliver to. 55825a99e2eSeric ** 55925a99e2eSeric ** Returns: 56025a99e2eSeric ** zero -- successfully delivered. 56125a99e2eSeric ** else -- some failure, see ExitStat for more info. 56225a99e2eSeric ** 56325a99e2eSeric ** Side Effects: 56425a99e2eSeric ** The standard input is passed off to someone. 56525a99e2eSeric */ 56625a99e2eSeric 567588cad61Seric deliver(e, firstto) 568588cad61Seric register ENVELOPE *e; 569c77d1c25Seric ADDRESS *firstto; 57025a99e2eSeric { 57178442df3Seric char *host; /* host being sent to */ 57278442df3Seric char *user; /* user being sent to */ 57325a99e2eSeric char **pvp; 5745dfc646bSeric register char **mvp; 57525a99e2eSeric register char *p; 576588cad61Seric register MAILER *m; /* mailer for this recipient */ 5776259796dSeric ADDRESS *ctladdr; 578b31e7f2bSeric register MCI *mci; 579c77d1c25Seric register ADDRESS *to = firstto; 580c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 581772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 582911693bfSbostic int rcode; /* response code */ 583e103b48fSeric char *firstsig; /* signature of firstto */ 5849c9e68d9Seric int pid; 5859c9e68d9Seric char *curhost; 5869c9e68d9Seric int mpvect[2]; 5879c9e68d9Seric int rpvect[2]; 588ee6bf8dfSeric char *pv[MAXPV+1]; 589579ef0ddSeric char tobuf[TOBUFSIZE]; /* text line of to people */ 590ee6bf8dfSeric char buf[MAXNAME]; 591c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 592fabb3bd4Seric extern int checkcompat(); 59325a99e2eSeric 59435490626Seric errno = 0; 595ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5965dfc646bSeric return (0); 59725a99e2eSeric 5989d4a8008Seric #if NAMED_BIND 599912a731aSbostic /* unless interactive, try twice, over a minute */ 6008d19a23dSeric if (OpMode == MD_DAEMON || OpMode == MD_SMTP) 6018d19a23dSeric { 602912a731aSbostic _res.retrans = 30; 603912a731aSbostic _res.retry = 2; 604912a731aSbostic } 605d4bd8f0eSbostic #endif 606912a731aSbostic 60751552439Seric m = to->q_mailer; 60851552439Seric host = to->q_host; 609c9be6216Seric CurEnv = e; /* just in case */ 6104384d521Seric e->e_statmsg = NULL; 611df106f0bSeric SmtpError[0] = '\0'; 61251552439Seric 6136ef48975Seric if (tTd(10, 1)) 614562ec147Seric printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n", 615562ec147Seric e->e_id, m->m_name, host, to->q_user); 616ab81ee53Seric if (tTd(10, 100)) 617ab81ee53Seric printopenfds(FALSE); 618f3dbc832Seric 619f3dbc832Seric /* 620f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 621f3dbc832Seric ** connections now, just mark these addresses and return. 622f3dbc832Seric ** This is useful if we want to batch connections to 623f3dbc832Seric ** reduce load. This will cause the messages to be 624f3dbc832Seric ** queued up, and a daemon will come along to send the 625f3dbc832Seric ** messages later. 626f3dbc832Seric ** This should be on a per-mailer basis. 627f3dbc832Seric */ 628f3dbc832Seric 62979aa5155Seric if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 630f3dbc832Seric { 631f3dbc832Seric for (; to != NULL; to = to->q_next) 632f4560e80Seric { 633ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 634c6e18ac5Seric to->q_mailer != m) 635f4560e80Seric continue; 636268a25e1Seric to->q_flags |= QQUEUEUP; 637588cad61Seric e->e_to = to->q_paddr; 63808b25121Seric message("queued"); 6392f624c86Seric if (LogLevel > 8) 6404dee0003Seric logdelivery(m, NULL, "queued", NULL, e); 641f4560e80Seric } 642588cad61Seric e->e_to = NULL; 643f3dbc832Seric return (0); 644f3dbc832Seric } 645f3dbc832Seric 64625a99e2eSeric /* 6475dfc646bSeric ** Do initial argv setup. 6485dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 6495dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 6505dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 6515dfc646bSeric ** code does not check for 'pv' overflow; this places a 6525dfc646bSeric ** manifest lower limit of 4 for MAXPV. 6533bea8136Seric ** The from address rewrite is expected to make 6543bea8136Seric ** the address relative to the other end. 6555dfc646bSeric */ 6565dfc646bSeric 65778442df3Seric /* rewrite from address, using rewriting rules */ 658efe54562Seric rcode = EX_OK; 659efe54562Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, 660efe54562Seric RF_SENDERADDR|RF_CANONICAL, 661efe54562Seric &rcode, e)); 662ee4b0922Seric define('g', rpathbuf, e); /* translated return path */ 663588cad61Seric define('h', host, e); /* to host */ 6645dfc646bSeric Errors = 0; 6655dfc646bSeric pvp = pv; 6665dfc646bSeric *pvp++ = m->m_argv[0]; 6675dfc646bSeric 6685dfc646bSeric /* insert -f or -r flag as appropriate */ 66957fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6705dfc646bSeric { 67157fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 6725dfc646bSeric *pvp++ = "-f"; 6735dfc646bSeric else 6745dfc646bSeric *pvp++ = "-r"; 675c23ed322Seric *pvp++ = newstr(rpathbuf); 6765dfc646bSeric } 6775dfc646bSeric 6785dfc646bSeric /* 6795dfc646bSeric ** Append the other fixed parts of the argv. These run 6805dfc646bSeric ** up to the first entry containing "$u". There can only 6815dfc646bSeric ** be one of these, and there are only a few more slots 6825dfc646bSeric ** in the pv after it. 6835dfc646bSeric */ 6845dfc646bSeric 6855dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 6865dfc646bSeric { 6872bc47524Seric /* can't use strchr here because of sign extension problems */ 6882bc47524Seric while (*p != '\0') 6892bc47524Seric { 6902bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 6912bc47524Seric { 6922bc47524Seric if (*p == 'u') 6935dfc646bSeric break; 6942bc47524Seric } 6952bc47524Seric } 6962bc47524Seric 6972bc47524Seric if (*p != '\0') 6985dfc646bSeric break; 6995dfc646bSeric 7005dfc646bSeric /* this entry is safe -- go ahead and process it */ 701588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 7025dfc646bSeric *pvp++ = newstr(buf); 7035dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 7045dfc646bSeric { 70508b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 7065dfc646bSeric return (-1); 7075dfc646bSeric } 7085dfc646bSeric } 709c579ef51Seric 71033db8731Seric /* 71133db8731Seric ** If we have no substitution for the user name in the argument 71233db8731Seric ** list, we know that we must supply the names otherwise -- and 71333db8731Seric ** SMTP is the answer!! 71433db8731Seric */ 71533db8731Seric 7165dfc646bSeric if (*mvp == NULL) 717c579ef51Seric { 718c579ef51Seric /* running SMTP */ 7192c7e1b8dSeric # ifdef SMTP 720c579ef51Seric clever = TRUE; 721c579ef51Seric *pvp = NULL; 7226c2c3107Seric # else /* SMTP */ 72333db8731Seric /* oops! we don't implement SMTP */ 724df106f0bSeric syserr("554 SMTP style mailer not implemented"); 7252c7e1b8dSeric return (EX_SOFTWARE); 7266c2c3107Seric # endif /* SMTP */ 727c579ef51Seric } 7285dfc646bSeric 7295dfc646bSeric /* 7305dfc646bSeric ** At this point *mvp points to the argument with $u. We 7315dfc646bSeric ** run through our address list and append all the addresses 7325dfc646bSeric ** we can. If we run out of space, do not fret! We can 7335dfc646bSeric ** always send another copy later. 7345dfc646bSeric */ 7355dfc646bSeric 7365dfc646bSeric tobuf[0] = '\0'; 737588cad61Seric e->e_to = tobuf; 7386259796dSeric ctladdr = NULL; 739e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7405dfc646bSeric for (; to != NULL; to = to->q_next) 7415dfc646bSeric { 7425dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 74357fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7445dfc646bSeric break; 7455dfc646bSeric 7465dfc646bSeric /* if already sent or not for this host, don't send */ 747ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 748e103b48fSeric to->q_mailer != firstto->q_mailer || 749e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7505dfc646bSeric continue; 7516259796dSeric 7524b22ea87Seric /* avoid overflowing tobuf */ 753aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7544b22ea87Seric break; 7554b22ea87Seric 7566ef48975Seric if (tTd(10, 1)) 757772e6e50Seric { 758772e6e50Seric printf("\nsend to "); 759772e6e50Seric printaddr(to, FALSE); 760772e6e50Seric } 761772e6e50Seric 7626259796dSeric /* compute effective uid/gid when sending */ 7630ed512e3Seric if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags)) 7646259796dSeric ctladdr = getctladdr(to); 7656259796dSeric 7665dfc646bSeric user = to->q_user; 767588cad61Seric e->e_to = to->q_paddr; 76875f1ade9Seric if (tTd(10, 5)) 76975f1ade9Seric { 77075f1ade9Seric printf("deliver: QDONTSEND "); 77175f1ade9Seric printaddr(to, FALSE); 77275f1ade9Seric } 773ee4b0922Seric to->q_flags |= QDONTSEND; 7745dfc646bSeric 7755dfc646bSeric /* 7765dfc646bSeric ** Check to see that these people are allowed to 7775dfc646bSeric ** talk to each other. 7782a6e0786Seric */ 7792a6e0786Seric 78069582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 78169582d2fSeric { 7820ed512e3Seric e->e_flags |= EF_NORETURN; 78308b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 7844dee0003Seric giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e); 78569582d2fSeric continue; 78669582d2fSeric } 7879bfcea71Seric #if NAMED_BIND 7889bfcea71Seric h_errno = 0; 7899bfcea71Seric #endif 790fabb3bd4Seric rcode = checkcompat(to, e); 7911793c9c5Seric if (rcode != EX_OK) 7925dfc646bSeric { 7931000c37aSeric markfailure(e, to, rcode); 7944dee0003Seric giveresponse(rcode, m, NULL, ctladdr, e); 7955dfc646bSeric continue; 7965dfc646bSeric } 7972a6e0786Seric 7982a6e0786Seric /* 7999ec9501bSeric ** Strip quote bits from names if the mailer is dumb 8009ec9501bSeric ** about them. 80125a99e2eSeric */ 80225a99e2eSeric 80357fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 80425a99e2eSeric { 8051d8f1806Seric stripquotes(user); 8061d8f1806Seric stripquotes(host); 80725a99e2eSeric } 80825a99e2eSeric 809cdb828c5Seric /* hack attack -- delivermail compatibility */ 810cdb828c5Seric if (m == ProgMailer && *user == '|') 811cdb828c5Seric user++; 812cdb828c5Seric 81325a99e2eSeric /* 8143efaed6eSeric ** If an error message has already been given, don't 8153efaed6eSeric ** bother to send to this address. 8163efaed6eSeric ** 8173efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 8183efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 8193efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 8203efaed6eSeric */ 8213efaed6eSeric 8226cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 8233efaed6eSeric continue; 8243efaed6eSeric 825f2fec898Seric /* save statistics.... */ 826588cad61Seric markstats(e, to); 827f2fec898Seric 8283efaed6eSeric /* 82925a99e2eSeric ** See if this user name is "special". 83025a99e2eSeric ** If the user name has a slash in it, assume that this 83151552439Seric ** is a file -- send it off without further ado. Note 83251552439Seric ** that this type of addresses is not processed along 83351552439Seric ** with the others, so we fudge on the To person. 83425a99e2eSeric */ 83525a99e2eSeric 8362c017f8dSeric if (m == FileMailer) 83725a99e2eSeric { 83803463a75Seric rcode = mailfile(user, ctladdr, e); 83903463a75Seric giveresponse(rcode, m, NULL, ctladdr, e); 840dde5acadSeric if (rcode == EX_OK) 841dde5acadSeric to->q_flags |= QSENT; 8425dfc646bSeric continue; 84325a99e2eSeric } 84425a99e2eSeric 84513bbc08cSeric /* 84613bbc08cSeric ** Address is verified -- add this user to mailer 84713bbc08cSeric ** argv, and add it to the print list of recipients. 84813bbc08cSeric */ 84913bbc08cSeric 850508daeccSeric /* link together the chain of recipients */ 851508daeccSeric to->q_tchain = tochain; 852508daeccSeric tochain = to; 853508daeccSeric 8545dfc646bSeric /* create list of users for error messages */ 855db8841e9Seric (void) strcat(tobuf, ","); 856db8841e9Seric (void) strcat(tobuf, to->q_paddr); 857588cad61Seric define('u', user, e); /* to user */ 858b6f89e6eSeric p = to->q_home; 859b6f89e6eSeric if (p == NULL && ctladdr != NULL) 860b6f89e6eSeric p = ctladdr->q_home; 861b6f89e6eSeric define('z', p, e); /* user's home */ 8625dfc646bSeric 863c579ef51Seric /* 864508daeccSeric ** Expand out this user into argument list. 865c579ef51Seric */ 866c579ef51Seric 867508daeccSeric if (!clever) 868c579ef51Seric { 869588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8705dfc646bSeric *pvp++ = newstr(buf); 8715dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 8725dfc646bSeric { 8735dfc646bSeric /* allow some space for trailing parms */ 8745dfc646bSeric break; 8755dfc646bSeric } 8765dfc646bSeric } 877c579ef51Seric } 8785dfc646bSeric 879145b49b1Seric /* see if any addresses still exist */ 880145b49b1Seric if (tobuf[0] == '\0') 881c579ef51Seric { 882588cad61Seric define('g', (char *) NULL, e); 883145b49b1Seric return (0); 884c579ef51Seric } 885145b49b1Seric 8865dfc646bSeric /* print out messages as full list */ 88763780dbdSeric e->e_to = tobuf + 1; 8885dfc646bSeric 8895dfc646bSeric /* 8905dfc646bSeric ** Fill out any parameters after the $u parameter. 8915dfc646bSeric */ 8925dfc646bSeric 893c579ef51Seric while (!clever && *++mvp != NULL) 8945dfc646bSeric { 895588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8965dfc646bSeric *pvp++ = newstr(buf); 8975dfc646bSeric if (pvp >= &pv[MAXPV]) 89808b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 8995dfc646bSeric } 9005dfc646bSeric *pvp++ = NULL; 9015dfc646bSeric 90225a99e2eSeric /* 90325a99e2eSeric ** Call the mailer. 9046328bdf7Seric ** The argument vector gets built, pipes 90525a99e2eSeric ** are created as necessary, and we fork & exec as 9066328bdf7Seric ** appropriate. 907c579ef51Seric ** If we are running SMTP, we just need to clean up. 90825a99e2eSeric */ 90925a99e2eSeric 910960e665aSeric /*XXX this seems a bit wierd */ 9114899f6b5Seric if (ctladdr == NULL && m != ProgMailer && 9124899f6b5Seric bitset(QGOODUID, e->e_from.q_flags)) 913960e665aSeric ctladdr = &e->e_from; 914960e665aSeric 9159d4a8008Seric #if NAMED_BIND 9162bcc6d2dSeric if (ConfigLevel < 2) 917912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 918134746fbSeric #endif 9199c9e68d9Seric 9209c9e68d9Seric if (tTd(11, 1)) 921134746fbSeric { 9229c9e68d9Seric printf("openmailer:"); 9239c9e68d9Seric printav(pv); 9249c9e68d9Seric } 9259c9e68d9Seric errno = 0; 9269bfcea71Seric #if NAMED_BIND 9279bfcea71Seric h_errno = 0; 9289bfcea71Seric #endif 9299c9e68d9Seric 9309c9e68d9Seric CurHostName = m->m_mailer; 9319c9e68d9Seric 9329c9e68d9Seric /* 9339c9e68d9Seric ** Deal with the special case of mail handled through an IPC 9349c9e68d9Seric ** connection. 9359c9e68d9Seric ** In this case we don't actually fork. We must be 9369c9e68d9Seric ** running SMTP for this to work. We will return a 9379c9e68d9Seric ** zero pid to indicate that we are running IPC. 9389c9e68d9Seric ** We also handle a debug version that just talks to stdin/out. 9399c9e68d9Seric */ 9409c9e68d9Seric 9419c9e68d9Seric curhost = NULL; 942c931b82bSeric SmtpPhase = NULL; 943df106f0bSeric mci = NULL; 9449c9e68d9Seric 9457febfd66Seric #ifdef XDEBUG 9467febfd66Seric { 9477febfd66Seric char wbuf[MAXLINE]; 9487febfd66Seric 9497febfd66Seric /* make absolutely certain 0, 1, and 2 are in use */ 9507febfd66Seric sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 9517febfd66Seric checkfd012(wbuf); 9527febfd66Seric } 9537febfd66Seric #endif 9547febfd66Seric 9559c9e68d9Seric /* check for Local Person Communication -- not for mortals!!! */ 9569c9e68d9Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 9579c9e68d9Seric { 9589c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 9599c9e68d9Seric bzero((char *) mci, sizeof *mci); 9609c9e68d9Seric mci->mci_in = stdin; 9619c9e68d9Seric mci->mci_out = stdout; 9629c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9639c9e68d9Seric mci->mci_mailer = m; 9649c9e68d9Seric } 9659c9e68d9Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 9669c9e68d9Seric strcmp(m->m_mailer, "[TCP]") == 0) 9679c9e68d9Seric { 9689c9e68d9Seric #ifdef DAEMON 9699c9e68d9Seric register int i; 9709c9e68d9Seric register u_short port; 9719c9e68d9Seric 972a5d44642Seric if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0') 973a5d44642Seric { 974a5d44642Seric syserr("null host name for %s mailer", m->m_mailer); 975a5d44642Seric rcode = EX_CONFIG; 976a5d44642Seric goto give_up; 977a5d44642Seric } 978a5d44642Seric 9799c9e68d9Seric CurHostName = pv[1]; 9809c9e68d9Seric curhost = hostsignature(m, pv[1], e); 9819c9e68d9Seric 9829c9e68d9Seric if (curhost == NULL || curhost[0] == '\0') 9839c9e68d9Seric { 984a5d44642Seric syserr("null host signature for %s", pv[1]); 985dfebe401Seric rcode = EX_CONFIG; 986b31e7f2bSeric goto give_up; 987b31e7f2bSeric } 9889c9e68d9Seric 9899c9e68d9Seric if (!clever) 9909c9e68d9Seric { 9919c9e68d9Seric syserr("554 non-clever IPC"); 992df106f0bSeric rcode = EX_CONFIG; 9939c9e68d9Seric goto give_up; 9949c9e68d9Seric } 9959c9e68d9Seric if (pv[2] != NULL) 9969c9e68d9Seric port = atoi(pv[2]); 9979c9e68d9Seric else 9989c9e68d9Seric port = 0; 9999c9e68d9Seric tryhost: 10009c9e68d9Seric while (*curhost != '\0') 10019c9e68d9Seric { 10029c9e68d9Seric register char *p; 10039c9e68d9Seric static char hostbuf[MAXNAME]; 10049c9e68d9Seric 10059c9e68d9Seric /* pull the next host from the signature */ 10069c9e68d9Seric p = strchr(curhost, ':'); 10079c9e68d9Seric if (p == NULL) 10089c9e68d9Seric p = &curhost[strlen(curhost)]; 1009df106f0bSeric if (p == curhost) 1010df106f0bSeric { 1011df106f0bSeric syserr("deliver: null host name in signature"); 10128087428aSeric curhost++; 1013df106f0bSeric continue; 1014df106f0bSeric } 10159c9e68d9Seric strncpy(hostbuf, curhost, p - curhost); 10169c9e68d9Seric hostbuf[p - curhost] = '\0'; 10179c9e68d9Seric if (*p != '\0') 10189c9e68d9Seric p++; 10199c9e68d9Seric curhost = p; 10209c9e68d9Seric 10219c9e68d9Seric /* see if we already know that this host is fried */ 10229c9e68d9Seric CurHostName = hostbuf; 10239c9e68d9Seric mci = mci_get(hostbuf, m); 10249c9e68d9Seric if (mci->mci_state != MCIS_CLOSED) 10259c9e68d9Seric { 10269c9e68d9Seric if (tTd(11, 1)) 10279c9e68d9Seric { 10289c9e68d9Seric printf("openmailer: "); 10294052916eSeric mci_dump(mci, FALSE); 10309c9e68d9Seric } 10319c9e68d9Seric CurHostName = mci->mci_host; 10329c9e68d9Seric break; 10339c9e68d9Seric } 10349c9e68d9Seric mci->mci_mailer = m; 10359c9e68d9Seric if (mci->mci_exitstat != EX_OK) 10369c9e68d9Seric continue; 10379c9e68d9Seric 10389c9e68d9Seric /* try the connection */ 10399c9e68d9Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 10409c9e68d9Seric message("Connecting to %s (%s)...", 10419c9e68d9Seric hostbuf, m->m_name); 10429c9e68d9Seric i = makeconnection(hostbuf, port, mci, 10439c9e68d9Seric bitnset(M_SECURE_PORT, m->m_flags)); 10449c9e68d9Seric mci->mci_exitstat = i; 10459c9e68d9Seric mci->mci_errno = errno; 10469d4a8008Seric #if NAMED_BIND 1047f170942cSeric mci->mci_herrno = h_errno; 1048f170942cSeric #endif 10499c9e68d9Seric if (i == EX_OK) 10509c9e68d9Seric { 10519c9e68d9Seric mci->mci_state = MCIS_OPENING; 10529c9e68d9Seric mci_cache(mci); 1053f170942cSeric if (TrafficLogFile != NULL) 1054f170942cSeric fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1055f170942cSeric getpid(), hostbuf); 10569c9e68d9Seric break; 10579c9e68d9Seric } 10589c9e68d9Seric else if (tTd(11, 1)) 10599c9e68d9Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 10609c9e68d9Seric i, errno); 10619c9e68d9Seric 10629c9e68d9Seric /* enter status of this host */ 10639c9e68d9Seric setstat(i); 1064df106f0bSeric 1065df106f0bSeric /* should print some message here for -v mode */ 1066df106f0bSeric } 1067df106f0bSeric if (mci == NULL) 1068df106f0bSeric { 1069df106f0bSeric syserr("deliver: no host name"); 1070df106f0bSeric rcode = EX_OSERR; 1071df106f0bSeric goto give_up; 10729c9e68d9Seric } 10739c9e68d9Seric mci->mci_pid = 0; 10749c9e68d9Seric #else /* no DAEMON */ 10759c9e68d9Seric syserr("554 openmailer: no IPC"); 10769c9e68d9Seric if (tTd(11, 1)) 10779c9e68d9Seric printf("openmailer: NULL\n"); 1078df106f0bSeric rcode = EX_UNAVAILABLE; 1079df106f0bSeric goto give_up; 10809c9e68d9Seric #endif /* DAEMON */ 10819c9e68d9Seric } 10829c9e68d9Seric else 10839c9e68d9Seric { 1084f170942cSeric if (TrafficLogFile != NULL) 10850e3bfef5Seric { 1086f170942cSeric char **av; 1087f170942cSeric 1088f170942cSeric fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1089f170942cSeric for (av = pv; *av != NULL; av++) 1090f170942cSeric fprintf(TrafficLogFile, " %s", *av); 1091f170942cSeric fprintf(TrafficLogFile, "\n"); 10926fe8c3bcSeric } 10936fe8c3bcSeric 10949c9e68d9Seric /* create a pipe to shove the mail through */ 10959c9e68d9Seric if (pipe(mpvect) < 0) 10969c9e68d9Seric { 10970e3bfef5Seric syserr("%s... openmailer(%s): pipe (to mailer)", 10980e3bfef5Seric e->e_to, m->m_name); 10999c9e68d9Seric if (tTd(11, 1)) 11009c9e68d9Seric printf("openmailer: NULL\n"); 11019c9e68d9Seric rcode = EX_OSERR; 11029c9e68d9Seric goto give_up; 11039c9e68d9Seric } 11049c9e68d9Seric 11059c9e68d9Seric /* if this mailer speaks smtp, create a return pipe */ 11069c9e68d9Seric if (clever && pipe(rpvect) < 0) 11079c9e68d9Seric { 11080e3bfef5Seric syserr("%s... openmailer(%s): pipe (from mailer)", 11090e3bfef5Seric e->e_to, m->m_name); 11109c9e68d9Seric (void) close(mpvect[0]); 11119c9e68d9Seric (void) close(mpvect[1]); 11129c9e68d9Seric if (tTd(11, 1)) 11139c9e68d9Seric printf("openmailer: NULL\n"); 11149c9e68d9Seric rcode = EX_OSERR; 11159c9e68d9Seric goto give_up; 11169c9e68d9Seric } 11179c9e68d9Seric 11189c9e68d9Seric /* 11199c9e68d9Seric ** Actually fork the mailer process. 11209c9e68d9Seric ** DOFORK is clever about retrying. 11219c9e68d9Seric ** 11229c9e68d9Seric ** Dispose of SIGCHLD signal catchers that may be laying 11239c9e68d9Seric ** around so that endmail will get it. 11249c9e68d9Seric */ 11259c9e68d9Seric 11269c9e68d9Seric if (e->e_xfp != NULL) 11279c9e68d9Seric (void) fflush(e->e_xfp); /* for debugging */ 11289c9e68d9Seric (void) fflush(stdout); 11299c9e68d9Seric # ifdef SIGCHLD 11302b9178d3Seric (void) setsignal(SIGCHLD, SIG_DFL); 11319c9e68d9Seric # endif /* SIGCHLD */ 11329c9e68d9Seric DOFORK(FORK); 11339c9e68d9Seric /* pid is set by DOFORK */ 11349c9e68d9Seric if (pid < 0) 11359c9e68d9Seric { 11369c9e68d9Seric /* failure */ 11370e3bfef5Seric syserr("%s... openmailer(%s): cannot fork", 11380e3bfef5Seric e->e_to, m->m_name); 11399c9e68d9Seric (void) close(mpvect[0]); 11409c9e68d9Seric (void) close(mpvect[1]); 11419c9e68d9Seric if (clever) 11429c9e68d9Seric { 11439c9e68d9Seric (void) close(rpvect[0]); 11449c9e68d9Seric (void) close(rpvect[1]); 11459c9e68d9Seric } 11469c9e68d9Seric if (tTd(11, 1)) 11479c9e68d9Seric printf("openmailer: NULL\n"); 11489c9e68d9Seric rcode = EX_OSERR; 11499c9e68d9Seric goto give_up; 11509c9e68d9Seric } 11519c9e68d9Seric else if (pid == 0) 11529c9e68d9Seric { 11539c9e68d9Seric int i; 11549c9e68d9Seric int saveerrno; 11559c9e68d9Seric char **ep; 11569c9e68d9Seric char *env[MAXUSERENVIRON]; 11579c9e68d9Seric extern char **environ; 11589c9e68d9Seric extern int DtableSize; 11599c9e68d9Seric 11609c9e68d9Seric /* child -- set up input & exec mailer */ 11612b9178d3Seric (void) setsignal(SIGINT, SIG_IGN); 11622b9178d3Seric (void) setsignal(SIGHUP, SIG_IGN); 11632b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 11649c9e68d9Seric 116544f2317fSeric /* reset user and group */ 116644f2317fSeric if (!bitnset(M_RESTR, m->m_flags)) 116744f2317fSeric { 116844f2317fSeric if (ctladdr == NULL || ctladdr->q_uid == 0) 116944f2317fSeric { 117044f2317fSeric (void) initgroups(DefUser, DefGid); 1171fc354902Seric (void) setgid(DefGid); 117244f2317fSeric (void) setuid(DefUid); 117344f2317fSeric } 117444f2317fSeric else 117544f2317fSeric { 117644f2317fSeric (void) initgroups(ctladdr->q_ruser? 117744f2317fSeric ctladdr->q_ruser: ctladdr->q_user, 117844f2317fSeric ctladdr->q_gid); 117951cfe111Seric (void) setgid(ctladdr->q_gid); 118044f2317fSeric (void) setuid(ctladdr->q_uid); 118144f2317fSeric } 118244f2317fSeric } 118344f2317fSeric 118444f2317fSeric if (tTd(11, 2)) 118544f2317fSeric printf("openmailer: running as r/euid=%d/%d\n", 118644f2317fSeric getuid(), geteuid()); 118744f2317fSeric 1188b986f6aaSeric /* move into some "safe" directory */ 1189b986f6aaSeric if (m->m_execdir != NULL) 1190b986f6aaSeric { 1191b986f6aaSeric char *p, *q; 1192b986f6aaSeric char buf[MAXLINE]; 1193b986f6aaSeric 1194b986f6aaSeric for (p = m->m_execdir; p != NULL; p = q) 1195b986f6aaSeric { 1196b986f6aaSeric q = strchr(p, ':'); 1197b986f6aaSeric if (q != NULL) 1198b986f6aaSeric *q = '\0'; 1199b986f6aaSeric expand(p, buf, &buf[sizeof buf] - 1, e); 1200b986f6aaSeric if (q != NULL) 1201b986f6aaSeric *q++ = ':'; 1202b986f6aaSeric if (tTd(11, 20)) 1203b986f6aaSeric printf("openmailer: trydir %s\n", 1204b986f6aaSeric buf); 1205b986f6aaSeric if (buf[0] != '\0' && chdir(buf) >= 0) 1206b986f6aaSeric break; 1207b986f6aaSeric } 1208b986f6aaSeric } 1209b986f6aaSeric 12109c9e68d9Seric /* arrange to filter std & diag output of command */ 12119c9e68d9Seric if (clever) 12129c9e68d9Seric { 12139c9e68d9Seric (void) close(rpvect[0]); 12146fe8c3bcSeric if (dup2(rpvect[1], STDOUT_FILENO) < 0) 12156fe8c3bcSeric { 12160e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 12170e3bfef5Seric e->e_to, m->m_name, rpvect[1]); 12186fe8c3bcSeric _exit(EX_OSERR); 12196fe8c3bcSeric } 12209c9e68d9Seric (void) close(rpvect[1]); 12219c9e68d9Seric } 12228b40b0a4Seric else if (OpMode == MD_SMTP || OpMode == MD_DAEMON || 12238b40b0a4Seric HoldErrs || DisConnected) 12249c9e68d9Seric { 12259c9e68d9Seric /* put mailer output in transcript */ 12266fe8c3bcSeric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 12276fe8c3bcSeric { 12280e3bfef5Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 12290e3bfef5Seric e->e_to, m->m_name, 12306fe8c3bcSeric fileno(e->e_xfp)); 12316fe8c3bcSeric _exit(EX_OSERR); 12329c9e68d9Seric } 12336fe8c3bcSeric } 12346fe8c3bcSeric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 12356fe8c3bcSeric { 12360e3bfef5Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr", 12370e3bfef5Seric e->e_to, m->m_name); 12386fe8c3bcSeric _exit(EX_OSERR); 12396fe8c3bcSeric } 12409c9e68d9Seric 12419c9e68d9Seric /* arrange to get standard input */ 12429c9e68d9Seric (void) close(mpvect[1]); 12439c9e68d9Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 12449c9e68d9Seric { 12450e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 12460e3bfef5Seric e->e_to, m->m_name, mpvect[0]); 12479c9e68d9Seric _exit(EX_OSERR); 12489c9e68d9Seric } 12499c9e68d9Seric (void) close(mpvect[0]); 12509c9e68d9Seric 12519c9e68d9Seric /* arrange for all the files to be closed */ 12529c9e68d9Seric for (i = 3; i < DtableSize; i++) 12539c9e68d9Seric { 12549c9e68d9Seric register int j; 125544f2317fSeric 12569c9e68d9Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 12579c9e68d9Seric (void) fcntl(i, F_SETFD, j | 1); 12589c9e68d9Seric } 12599c9e68d9Seric 12609f9b003eSeric /* 12619f9b003eSeric ** Set up the mailer environment 12629f9b003eSeric ** TZ is timezone information. 12639f9b003eSeric ** SYSTYPE is Apollo software sys type (required). 12649f9b003eSeric ** ISP is Apollo hardware system type (required). 12659f9b003eSeric */ 12669f9b003eSeric 12679c9e68d9Seric i = 0; 12689c9e68d9Seric env[i++] = "AGENT=sendmail"; 12699c9e68d9Seric for (ep = environ; *ep != NULL; ep++) 12709c9e68d9Seric { 12719f9b003eSeric if (strncmp(*ep, "TZ=", 3) == 0 || 12729f9b003eSeric strncmp(*ep, "ISP=", 4) == 0 || 12739f9b003eSeric strncmp(*ep, "SYSTYPE=", 8) == 0) 12749c9e68d9Seric env[i++] = *ep; 12759c9e68d9Seric } 12769c9e68d9Seric env[i++] = NULL; 12779c9e68d9Seric 1278929277f2Seric /* run disconnected from terminal */ 1279929277f2Seric (void) setsid(); 1280929277f2Seric 12819c9e68d9Seric /* try to execute the mailer */ 12829c9e68d9Seric execve(m->m_mailer, pv, env); 12839c9e68d9Seric saveerrno = errno; 12849c9e68d9Seric syserr("Cannot exec %s", m->m_mailer); 12850ed512e3Seric if (bitnset(M_LOCALMAILER, m->m_flags) || 12860ed512e3Seric transienterror(saveerrno)) 12877c941fd2Seric _exit(EX_OSERR); 12889c9e68d9Seric _exit(EX_UNAVAILABLE); 12899c9e68d9Seric } 12909c9e68d9Seric 12919c9e68d9Seric /* 12929c9e68d9Seric ** Set up return value. 12939c9e68d9Seric */ 12949c9e68d9Seric 12959c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 12969c9e68d9Seric bzero((char *) mci, sizeof *mci); 12979c9e68d9Seric mci->mci_mailer = m; 12989c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 12999c9e68d9Seric mci->mci_pid = pid; 13009c9e68d9Seric (void) close(mpvect[0]); 13019c9e68d9Seric mci->mci_out = fdopen(mpvect[1], "w"); 1302335eae58Seric if (mci->mci_out == NULL) 1303335eae58Seric { 1304335eae58Seric syserr("deliver: cannot create mailer output channel, fd=%d", 1305335eae58Seric mpvect[1]); 1306335eae58Seric (void) close(mpvect[1]); 1307335eae58Seric if (clever) 1308335eae58Seric { 1309335eae58Seric (void) close(rpvect[0]); 1310335eae58Seric (void) close(rpvect[1]); 1311335eae58Seric } 1312335eae58Seric rcode = EX_OSERR; 1313335eae58Seric goto give_up; 1314335eae58Seric } 13159c9e68d9Seric if (clever) 13169c9e68d9Seric { 13179c9e68d9Seric (void) close(rpvect[1]); 13189c9e68d9Seric mci->mci_in = fdopen(rpvect[0], "r"); 1319335eae58Seric if (mci->mci_in == NULL) 1320335eae58Seric { 1321335eae58Seric syserr("deliver: cannot create mailer input channel, fd=%d", 1322335eae58Seric mpvect[1]); 1323335eae58Seric (void) close(rpvect[0]); 1324335eae58Seric fclose(mci->mci_out); 1325335eae58Seric mci->mci_out = NULL; 1326335eae58Seric rcode = EX_OSERR; 1327335eae58Seric goto give_up; 1328335eae58Seric } 13299c9e68d9Seric } 13309c9e68d9Seric else 13319c9e68d9Seric { 13329c9e68d9Seric mci->mci_flags |= MCIF_TEMP; 13339c9e68d9Seric mci->mci_in = NULL; 13349c9e68d9Seric } 13359c9e68d9Seric } 13369c9e68d9Seric 13379c9e68d9Seric /* 13389c9e68d9Seric ** If we are in SMTP opening state, send initial protocol. 13399c9e68d9Seric */ 13409c9e68d9Seric 13419c9e68d9Seric if (clever && mci->mci_state != MCIS_CLOSED) 13429c9e68d9Seric { 13439c9e68d9Seric smtpinit(m, mci, e); 13449c9e68d9Seric } 13459c9e68d9Seric if (tTd(11, 1)) 13469c9e68d9Seric { 13479c9e68d9Seric printf("openmailer: "); 13484052916eSeric mci_dump(mci, FALSE); 13499c9e68d9Seric } 13509c9e68d9Seric 13519c9e68d9Seric if (mci->mci_state != MCIS_OPEN) 1352b31e7f2bSeric { 1353b31e7f2bSeric /* couldn't open the mailer */ 1354b31e7f2bSeric rcode = mci->mci_exitstat; 13552a6bc25bSeric errno = mci->mci_errno; 13569d4a8008Seric #if NAMED_BIND 1357f170942cSeric h_errno = mci->mci_herrno; 1358f170942cSeric #endif 1359b31e7f2bSeric if (rcode == EX_OK) 1360b31e7f2bSeric { 1361b31e7f2bSeric /* shouldn't happen */ 136208b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 13636b0f339dSeric rcode, mci->mci_state, firstsig); 1364b31e7f2bSeric rcode = EX_SOFTWARE; 1365b31e7f2bSeric } 1366cb082c5bSeric else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0') 136790891494Seric { 136890891494Seric /* try next MX site */ 136990891494Seric goto tryhost; 137090891494Seric } 1371b31e7f2bSeric } 1372b31e7f2bSeric else if (!clever) 1373b31e7f2bSeric { 1374b31e7f2bSeric /* 1375b31e7f2bSeric ** Format and send message. 1376b31e7f2bSeric */ 137715d084d5Seric 13785aa0f353Seric putfromline(mci, e); 1379*a6ce3008Seric (*e->e_puthdr)(mci, e->e_header, e); 13805aa0f353Seric putline("\n", mci); 13815aa0f353Seric (*e->e_putbody)(mci, e, NULL); 1382b31e7f2bSeric 1383b31e7f2bSeric /* get the exit status */ 1384c9be6216Seric rcode = endmailer(mci, e, pv); 1385134746fbSeric } 1386134746fbSeric else 1387b31e7f2bSeric #ifdef SMTP 1388134746fbSeric { 1389b31e7f2bSeric /* 1390b31e7f2bSeric ** Send the MAIL FROM: protocol 1391b31e7f2bSeric */ 139215d084d5Seric 1393b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 1394b31e7f2bSeric if (rcode == EX_OK) 139575889e88Seric { 1396ded0d3daSkarels register char *t = tobuf; 1397ded0d3daSkarels register int i; 1398ded0d3daSkarels 1399588cad61Seric /* send the recipient list */ 140063780dbdSeric tobuf[0] = '\0'; 140175889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 140275889e88Seric { 140363780dbdSeric e->e_to = to->q_paddr; 140415d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 140575889e88Seric { 140683b7ddc9Seric markfailure(e, to, i); 14074dee0003Seric giveresponse(i, m, mci, ctladdr, e); 140863780dbdSeric } 140975889e88Seric else 141075889e88Seric { 1411911693bfSbostic *t++ = ','; 1412b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 1413b31e7f2bSeric continue; 1414ee39df61Seric *t = '\0'; 1415588cad61Seric } 1416588cad61Seric } 1417588cad61Seric 141863780dbdSeric /* now send the data */ 141963780dbdSeric if (tobuf[0] == '\0') 1420b31e7f2bSeric { 14219c9e68d9Seric rcode = EX_OK; 142263780dbdSeric e->e_to = NULL; 1423b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 1424b31e7f2bSeric smtprset(m, mci, e); 1425b31e7f2bSeric } 142675889e88Seric else 142775889e88Seric { 142863780dbdSeric e->e_to = tobuf + 1; 142975889e88Seric rcode = smtpdata(m, mci, e); 143063780dbdSeric } 143163780dbdSeric 143263780dbdSeric /* now close the connection */ 1433b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 143415d084d5Seric smtpquit(m, mci, e); 143563780dbdSeric } 1436cb082c5bSeric if (rcode != EX_OK && curhost != NULL && *curhost != '\0') 14379c9e68d9Seric { 14389c9e68d9Seric /* try next MX site */ 14399c9e68d9Seric goto tryhost; 14409c9e68d9Seric } 1441c579ef51Seric } 1442b31e7f2bSeric #else /* not SMTP */ 1443a05b3449Sbostic { 144408b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 1445845e533cSeric rcode = EX_CONFIG; 1446b31e7f2bSeric goto give_up; 1447a05b3449Sbostic } 1448b31e7f2bSeric #endif /* SMTP */ 14499d4a8008Seric #if NAMED_BIND 14502bcc6d2dSeric if (ConfigLevel < 2) 1451912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1452134746fbSeric #endif 14535dfc646bSeric 1454b31e7f2bSeric /* arrange a return receipt if requested */ 1455df106f0bSeric if (rcode == EX_OK && e->e_receiptto != NULL && 1456df106f0bSeric bitnset(M_LOCALMAILER, m->m_flags)) 1457b31e7f2bSeric { 1458b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 1459b31e7f2bSeric /* do we want to send back more info? */ 1460b31e7f2bSeric } 1461b31e7f2bSeric 1462c77d1c25Seric /* 146363780dbdSeric ** Do final status disposal. 146463780dbdSeric ** We check for something in tobuf for the SMTP case. 1465c77d1c25Seric ** If we got a temporary failure, arrange to queue the 1466c77d1c25Seric ** addressees. 1467c77d1c25Seric */ 1468c77d1c25Seric 1469b31e7f2bSeric give_up: 147063780dbdSeric if (tobuf[0] != '\0') 14714dee0003Seric giveresponse(rcode, m, mci, ctladdr, e); 1472772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 1473b31e7f2bSeric { 1474dde5acadSeric if (rcode != EX_OK) 147583b7ddc9Seric markfailure(e, to, rcode); 1476dde5acadSeric else 1477655518ecSeric { 1478dde5acadSeric to->q_flags |= QSENT; 1479655518ecSeric e->e_nsent++; 1480df106f0bSeric if (e->e_receiptto != NULL && 1481df106f0bSeric bitnset(M_LOCALMAILER, m->m_flags)) 1482df106f0bSeric { 1483df106f0bSeric fprintf(e->e_xfp, "%s... Successfully delivered\n", 1484df106f0bSeric to->q_paddr); 1485df106f0bSeric } 1486655518ecSeric } 1487b31e7f2bSeric } 1488b31e7f2bSeric 1489b31e7f2bSeric /* 1490b31e7f2bSeric ** Restore state and return. 1491b31e7f2bSeric */ 1492c77d1c25Seric 14937febfd66Seric #ifdef XDEBUG 14947febfd66Seric { 14957febfd66Seric char wbuf[MAXLINE]; 14967febfd66Seric 14977febfd66Seric /* make absolutely certain 0, 1, and 2 are in use */ 149844622ea3Seric sprintf(wbuf, "%s... end of deliver(%s)", 149944622ea3Seric e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 150044622ea3Seric m->m_name); 15017febfd66Seric checkfd012(wbuf); 15027febfd66Seric } 15037febfd66Seric #endif 15047febfd66Seric 150535490626Seric errno = 0; 1506588cad61Seric define('g', (char *) NULL, e); 15075826d9d3Seric return (rcode); 150825a99e2eSeric } 15095dfc646bSeric /* 151083b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 151183b7ddc9Seric ** 151283b7ddc9Seric ** Parameters: 151383b7ddc9Seric ** e -- the envelope we are sending. 151483b7ddc9Seric ** q -- the address to mark. 151583b7ddc9Seric ** rcode -- the code signifying the particular failure. 151683b7ddc9Seric ** 151783b7ddc9Seric ** Returns: 151883b7ddc9Seric ** none. 151983b7ddc9Seric ** 152083b7ddc9Seric ** Side Effects: 152183b7ddc9Seric ** marks the address (and possibly the envelope) with the 152283b7ddc9Seric ** failure so that an error will be returned or 152383b7ddc9Seric ** the message will be queued, as appropriate. 152483b7ddc9Seric */ 152583b7ddc9Seric 152683b7ddc9Seric markfailure(e, q, rcode) 152783b7ddc9Seric register ENVELOPE *e; 152883b7ddc9Seric register ADDRESS *q; 152983b7ddc9Seric int rcode; 153083b7ddc9Seric { 153119c47125Seric char buf[MAXLINE]; 153219c47125Seric 1533d5aafa3cSeric switch (rcode) 1534d5aafa3cSeric { 1535d5aafa3cSeric case EX_OK: 1536d5aafa3cSeric break; 1537d5aafa3cSeric 1538d5aafa3cSeric case EX_TEMPFAIL: 1539d5aafa3cSeric case EX_IOERR: 1540d5aafa3cSeric case EX_OSERR: 154183b7ddc9Seric q->q_flags |= QQUEUEUP; 1542d5aafa3cSeric break; 1543d5aafa3cSeric 1544d5aafa3cSeric default: 1545f170942cSeric q->q_flags |= QBADADDR; 1546d5aafa3cSeric break; 1547d5aafa3cSeric } 154883b7ddc9Seric } 154983b7ddc9Seric /* 1550c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 1551c579ef51Seric ** 1552c579ef51Seric ** We should never get fatal errors (e.g., segmentation 1553c579ef51Seric ** violation), so we report those specially. For other 1554c579ef51Seric ** errors, we choose a status message (into statmsg), 1555c579ef51Seric ** and if it represents an error, we print it. 1556c579ef51Seric ** 1557c579ef51Seric ** Parameters: 1558c579ef51Seric ** pid -- pid of mailer. 1559c9be6216Seric ** e -- the current envelope. 1560c9be6216Seric ** pv -- the parameter vector that invoked the mailer 1561c9be6216Seric ** (for error messages). 1562c579ef51Seric ** 1563c579ef51Seric ** Returns: 1564c579ef51Seric ** exit code of mailer. 1565c579ef51Seric ** 1566c579ef51Seric ** Side Effects: 1567c579ef51Seric ** none. 1568c579ef51Seric */ 1569c579ef51Seric 1570c9be6216Seric endmailer(mci, e, pv) 1571b31e7f2bSeric register MCI *mci; 1572c9be6216Seric register ENVELOPE *e; 1573c9be6216Seric char **pv; 1574c579ef51Seric { 1575588cad61Seric int st; 1576c579ef51Seric 157775889e88Seric /* close any connections */ 157875889e88Seric if (mci->mci_in != NULL) 157976f3d53fSeric (void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in"); 158075889e88Seric if (mci->mci_out != NULL) 158176f3d53fSeric (void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out"); 158275889e88Seric mci->mci_in = mci->mci_out = NULL; 158375889e88Seric mci->mci_state = MCIS_CLOSED; 158475889e88Seric 158533db8731Seric /* in the IPC case there is nothing to wait for */ 158675889e88Seric if (mci->mci_pid == 0) 158733db8731Seric return (EX_OK); 158833db8731Seric 158933db8731Seric /* wait for the mailer process to die and collect status */ 159075889e88Seric st = waitfor(mci->mci_pid); 1591588cad61Seric if (st == -1) 159278de67c1Seric { 1593c9be6216Seric syserr("endmailer %s: wait", pv[0]); 1594588cad61Seric return (EX_SOFTWARE); 1595c579ef51Seric } 159633db8731Seric 1597bf9bc890Seric if (WIFEXITED(st)) 1598c579ef51Seric { 1599bf9bc890Seric /* normal death -- return status */ 1600bf9bc890Seric return (WEXITSTATUS(st)); 1601bf9bc890Seric } 1602bf9bc890Seric 1603bf9bc890Seric /* it died a horrid death */ 1604550092c3Seric syserr("451 mailer %s died with signal %o", 1605550092c3Seric mci->mci_mailer->m_name, st); 1606c9be6216Seric 1607c9be6216Seric /* log the arguments */ 16083c930db3Seric if (pv != NULL && e->e_xfp != NULL) 1609c9be6216Seric { 1610c9be6216Seric register char **av; 1611c9be6216Seric 1612c9be6216Seric fprintf(e->e_xfp, "Arguments:"); 1613c9be6216Seric for (av = pv; *av != NULL; av++) 1614c9be6216Seric fprintf(e->e_xfp, " %s", *av); 1615c9be6216Seric fprintf(e->e_xfp, "\n"); 1616c9be6216Seric } 1617c9be6216Seric 16185f73204aSeric ExitStat = EX_TEMPFAIL; 16195f73204aSeric return (EX_TEMPFAIL); 1620c579ef51Seric } 1621c579ef51Seric /* 162225a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 162325a99e2eSeric ** 162425a99e2eSeric ** Parameters: 162525a99e2eSeric ** stat -- the status code from the mailer (high byte 162625a99e2eSeric ** only; core dumps must have been taken care of 162725a99e2eSeric ** already). 162881161401Seric ** m -- the mailer info for this mailer. 162981161401Seric ** mci -- the mailer connection info -- can be NULL if the 163081161401Seric ** response is given before the connection is made. 16314dee0003Seric ** ctladdr -- the controlling address for the recipient 16324dee0003Seric ** address(es). 163381161401Seric ** e -- the current envelope. 163425a99e2eSeric ** 163525a99e2eSeric ** Returns: 1636db8841e9Seric ** none. 163725a99e2eSeric ** 163825a99e2eSeric ** Side Effects: 1639c1f9df2cSeric ** Errors may be incremented. 164025a99e2eSeric ** ExitStat may be set. 164125a99e2eSeric */ 164225a99e2eSeric 16434dee0003Seric giveresponse(stat, m, mci, ctladdr, e) 164425a99e2eSeric int stat; 1645588cad61Seric register MAILER *m; 164681161401Seric register MCI *mci; 16474dee0003Seric ADDRESS *ctladdr; 1648198d9be0Seric ENVELOPE *e; 164925a99e2eSeric { 16509c16475dSeric register const char *statmsg; 165125a99e2eSeric extern char *SysExMsg[]; 165225a99e2eSeric register int i; 1653d4bd8f0eSbostic extern int N_SysEx; 1654198d9be0Seric char buf[MAXLINE]; 165525a99e2eSeric 165613bbc08cSeric /* 165713bbc08cSeric ** Compute status message from code. 165813bbc08cSeric */ 165913bbc08cSeric 166025a99e2eSeric i = stat - EX__BASE; 1661588cad61Seric if (stat == 0) 16626fe8c3bcSeric { 1663588cad61Seric statmsg = "250 Sent"; 1664ce5531bdSeric if (e->e_statmsg != NULL) 16656fe8c3bcSeric { 1666ce5531bdSeric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 16676fe8c3bcSeric statmsg = buf; 16686fe8c3bcSeric } 16696fe8c3bcSeric } 1670588cad61Seric else if (i < 0 || i > N_SysEx) 1671588cad61Seric { 1672588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1673588cad61Seric stat = EX_UNAVAILABLE; 1674588cad61Seric statmsg = buf; 1675588cad61Seric } 1676198d9be0Seric else if (stat == EX_TEMPFAIL) 1677198d9be0Seric { 16787d55540cSeric (void) strcpy(buf, SysExMsg[i] + 1); 16799d4a8008Seric #if NAMED_BIND 1680f28da541Smiriam if (h_errno == TRY_AGAIN) 16814d50702aSeric statmsg = errstring(h_errno+E_DNSBASE); 1682f28da541Smiriam else 1683d4bd8f0eSbostic #endif 1684f28da541Smiriam { 16858557d168Seric if (errno != 0) 1686d87e85f3Seric statmsg = errstring(errno); 1687d87e85f3Seric else 1688d87e85f3Seric { 1689d87e85f3Seric #ifdef SMTP 1690d87e85f3Seric statmsg = SmtpError; 16916c2c3107Seric #else /* SMTP */ 1692d87e85f3Seric statmsg = NULL; 16936c2c3107Seric #endif /* SMTP */ 1694d87e85f3Seric } 1695f28da541Smiriam } 1696d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1697d87e85f3Seric { 169887c9b3e7Seric (void) strcat(buf, ": "); 1699d87e85f3Seric (void) strcat(buf, statmsg); 17008557d168Seric } 1701198d9be0Seric statmsg = buf; 1702198d9be0Seric } 17039d4a8008Seric #if NAMED_BIND 1704f170942cSeric else if (stat == EX_NOHOST && h_errno != 0) 1705f170942cSeric { 17064d50702aSeric statmsg = errstring(h_errno + E_DNSBASE); 1707862934b2Seric (void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg); 1708f170942cSeric statmsg = buf; 1709f170942cSeric } 1710f170942cSeric #endif 171125a99e2eSeric else 1712d87e85f3Seric { 171325a99e2eSeric statmsg = SysExMsg[i]; 17147d55540cSeric if (*statmsg++ == ':') 17157d55540cSeric { 17167d55540cSeric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 17177d55540cSeric statmsg = buf; 17187d55540cSeric } 1719d87e85f3Seric } 1720588cad61Seric 1721588cad61Seric /* 1722588cad61Seric ** Print the message as appropriate 1723588cad61Seric */ 1724588cad61Seric 1725198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 1726df106f0bSeric { 1727df106f0bSeric extern char MsgBuf[]; 1728df106f0bSeric 17291f96bd0bSeric message("%s", &statmsg[4]); 1730df106f0bSeric if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 1731df106f0bSeric fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 1732df106f0bSeric } 173325a99e2eSeric else 173425a99e2eSeric { 1735862934b2Seric char mbuf[8]; 1736862934b2Seric 1737c1f9df2cSeric Errors++; 1738862934b2Seric sprintf(mbuf, "%.3s %%s", statmsg); 1739862934b2Seric usrerr(mbuf, &statmsg[4]); 174025a99e2eSeric } 174125a99e2eSeric 174225a99e2eSeric /* 174325a99e2eSeric ** Final cleanup. 174425a99e2eSeric ** Log a record of the transaction. Compute the new 174525a99e2eSeric ** ExitStat -- if we already had an error, stick with 174625a99e2eSeric ** that. 174725a99e2eSeric */ 174825a99e2eSeric 17492f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 17504dee0003Seric logdelivery(m, mci, &statmsg[4], ctladdr, e); 1751eb238f8cSeric 17525d3f0ed4Seric if (tTd(11, 2)) 17535d3f0ed4Seric printf("giveresponse: stat=%d, e->e_message=%s\n", 17545d3f0ed4Seric stat, e->e_message); 17555d3f0ed4Seric 1756eb238f8cSeric if (stat != EX_TEMPFAIL) 1757eb238f8cSeric setstat(stat); 17581097d4c9Seric if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL)) 1759198d9be0Seric { 1760198d9be0Seric if (e->e_message != NULL) 1761198d9be0Seric free(e->e_message); 1762198d9be0Seric e->e_message = newstr(&statmsg[4]); 1763198d9be0Seric } 17648557d168Seric errno = 0; 17659d4a8008Seric #if NAMED_BIND 1766f28da541Smiriam h_errno = 0; 1767d4bd8f0eSbostic #endif 1768eb238f8cSeric } 1769eb238f8cSeric /* 1770eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1771eb238f8cSeric ** 17722c9b4f99Seric ** Care is taken to avoid logging lines that are too long, because 17732c9b4f99Seric ** some versions of syslog have an unfortunate proclivity for core 17742c9b4f99Seric ** dumping. This is a hack, to be sure, that is at best empirical. 17752c9b4f99Seric ** 1776eb238f8cSeric ** Parameters: 177781161401Seric ** m -- the mailer info. Can be NULL for initial queue. 177881161401Seric ** mci -- the mailer connection info -- can be NULL if the 177981161401Seric ** log is occuring when no connection is active. 178081161401Seric ** stat -- the message to print for the status. 17814dee0003Seric ** ctladdr -- the controlling address for the to list. 178281161401Seric ** e -- the current envelope. 1783eb238f8cSeric ** 1784eb238f8cSeric ** Returns: 1785eb238f8cSeric ** none 1786eb238f8cSeric ** 1787eb238f8cSeric ** Side Effects: 1788eb238f8cSeric ** none 1789eb238f8cSeric */ 1790eb238f8cSeric 17914dee0003Seric logdelivery(m, mci, stat, ctladdr, e) 179281161401Seric MAILER *m; 179381161401Seric register MCI *mci; 1794eb238f8cSeric char *stat; 17954dee0003Seric ADDRESS *ctladdr; 1796b31e7f2bSeric register ENVELOPE *e; 17975cf56be3Seric { 1798eb238f8cSeric # ifdef LOG 17994dee0003Seric register char *bp; 18002c9b4f99Seric register char *p; 18012c9b4f99Seric int l; 1802d6acf3eeSeric char buf[512]; 18039507d1f9Seric 18043a100e8bSeric # if (SYSLOG_BUFSIZE) >= 256 18054dee0003Seric bp = buf; 18064dee0003Seric if (ctladdr != NULL) 18074dee0003Seric { 18084dee0003Seric strcpy(bp, ", ctladdr="); 1809f62f08c7Seric strcat(bp, shortenstring(ctladdr->q_paddr, 83)); 18104dee0003Seric bp += strlen(bp); 18114dee0003Seric if (bitset(QGOODUID, ctladdr->q_flags)) 18124dee0003Seric { 18134dee0003Seric (void) sprintf(bp, " (%d/%d)", 18144dee0003Seric ctladdr->q_uid, ctladdr->q_gid); 18154dee0003Seric bp += strlen(bp); 18164dee0003Seric } 18174dee0003Seric } 18184dee0003Seric 18194dee0003Seric (void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 18204dee0003Seric bp += strlen(bp); 182181161401Seric 182248ed5d33Seric if (m != NULL) 182371ff6caaSeric { 18244dee0003Seric (void) strcpy(bp, ", mailer="); 18254dee0003Seric (void) strcat(bp, m->m_name); 18264dee0003Seric bp += strlen(bp); 182771ff6caaSeric } 182848ed5d33Seric 182948ed5d33Seric if (mci != NULL && mci->mci_host != NULL) 183071ff6caaSeric { 183171ff6caaSeric # ifdef DAEMON 1832e2f2f828Seric extern SOCKADDR CurHostAddr; 183348ed5d33Seric # endif 183471ff6caaSeric 18354dee0003Seric (void) strcpy(bp, ", relay="); 18364dee0003Seric (void) strcat(bp, mci->mci_host); 183748ed5d33Seric 183848ed5d33Seric # ifdef DAEMON 18392bdfc6b9Seric (void) strcat(bp, " ["); 18404dee0003Seric (void) strcat(bp, anynet_ntoa(&CurHostAddr)); 18412bdfc6b9Seric (void) strcat(bp, "]"); 184271ff6caaSeric # endif 184371ff6caaSeric } 1844bcb9b028Seric else if (strcmp(stat, "queued") != 0) 184548ed5d33Seric { 184648ed5d33Seric char *p = macvalue('h', e); 18479507d1f9Seric 184848ed5d33Seric if (p != NULL && p[0] != '\0') 184948ed5d33Seric { 18504dee0003Seric (void) strcpy(bp, ", relay="); 18514dee0003Seric (void) strcat(bp, p); 185248ed5d33Seric } 185348ed5d33Seric } 1854cb082c5bSeric bp += strlen(bp); 1855d6acf3eeSeric 18563a100e8bSeric #define STATLEN (((SYSLOG_BUFSIZE) - 100) / 4) 18573a100e8bSeric #if (STATLEN) < 63 18583a100e8bSeric # undef STATLEN 18593a100e8bSeric # define STATLEN 63 18603a100e8bSeric #endif 18613a100e8bSeric #if (STATLEN) > 203 18623a100e8bSeric # undef STATLEN 18633a100e8bSeric # define STATLEN 203 18643a100e8bSeric #endif 18653a100e8bSeric 18663a100e8bSeric if ((bp - buf) > (sizeof buf - ((STATLEN) + 20))) 18672c9b4f99Seric { 18682c9b4f99Seric /* desperation move -- truncate data */ 18693a100e8bSeric bp = buf + sizeof buf - ((STATLEN) + 17); 18702c9b4f99Seric strcpy(bp, "..."); 18712c9b4f99Seric bp += 3; 18722c9b4f99Seric } 18732c9b4f99Seric 18742c9b4f99Seric (void) strcpy(bp, ", stat="); 18752c9b4f99Seric bp += strlen(bp); 18763a100e8bSeric 18773a100e8bSeric (void) strcpy(bp, shortenstring(stat, (STATLEN))); 18782c9b4f99Seric 18792c9b4f99Seric l = SYSLOG_BUFSIZE - 100 - strlen(buf); 18802c9b4f99Seric p = e->e_to; 18812c9b4f99Seric while (strlen(p) >= l) 18822c9b4f99Seric { 18832c9b4f99Seric register char *q = strchr(p + l, ','); 18842c9b4f99Seric 18852a1b6b73Seric if (q == NULL) 18862c9b4f99Seric break; 18872c9b4f99Seric syslog(LOG_INFO, "%s: to=%.*s [more]%s", 18882c9b4f99Seric e->e_id, ++q - p, p, buf); 18892c9b4f99Seric p = q; 18902c9b4f99Seric } 18912c9b4f99Seric syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf); 18923a100e8bSeric 18933a100e8bSeric # else /* we have a very short log buffer size */ 18943a100e8bSeric 189527607809Seric l = SYSLOG_BUFSIZE - 85; 18963a100e8bSeric p = e->e_to; 18973a100e8bSeric while (strlen(p) >= l) 18983a100e8bSeric { 18993a100e8bSeric register char *q = strchr(p + l, ','); 19003a100e8bSeric 19013a100e8bSeric if (q == NULL) 19023a100e8bSeric break; 19033a100e8bSeric syslog(LOG_INFO, "%s: to=%.*s [more]", 19043a100e8bSeric e->e_id, ++q - p, p); 19053a100e8bSeric p = q; 19063a100e8bSeric } 19073a100e8bSeric syslog(LOG_INFO, "%s: to=%s", e->e_id, p); 19083a100e8bSeric 19093a100e8bSeric if (ctladdr != NULL) 19103a100e8bSeric { 19113a100e8bSeric bp = buf; 19123a100e8bSeric strcpy(buf, "ctladdr="); 19133a100e8bSeric bp += strlen(buf); 19143a100e8bSeric strcpy(bp, shortenstring(ctladdr->q_paddr, 83)); 19153a100e8bSeric bp += strlen(buf); 19163a100e8bSeric if (bitset(QGOODUID, ctladdr->q_flags)) 19173a100e8bSeric { 19183a100e8bSeric (void) sprintf(bp, " (%d/%d)", 19193a100e8bSeric ctladdr->q_uid, ctladdr->q_gid); 19203a100e8bSeric bp += strlen(bp); 19213a100e8bSeric } 19223a100e8bSeric syslog(LOG_INFO, "%s: %s", e->e_id, buf); 19233a100e8bSeric } 19244e797715Seric bp = buf; 19254e797715Seric sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 19264e797715Seric bp += strlen(bp); 19273a100e8bSeric 19283a100e8bSeric if (m != NULL) 19294e797715Seric { 19304e797715Seric sprintf(bp, ", mailer=%s", m->m_name); 19314e797715Seric bp += strlen(bp); 19324e797715Seric } 1933b056abd0Seric syslog(LOG_INFO, "%s: %s", e->e_id, buf); 19343a100e8bSeric 1935b056abd0Seric buf[0] = '\0'; 19363a100e8bSeric if (mci != NULL && mci->mci_host != NULL) 19373a100e8bSeric { 19383a100e8bSeric # ifdef DAEMON 19393a100e8bSeric extern SOCKADDR CurHostAddr; 19403a100e8bSeric # endif 19413a100e8bSeric 1942d2ece200Seric sprintf(buf, "relay=%s", mci->mci_host); 19433a100e8bSeric 19443a100e8bSeric # ifdef DAEMON 1945b056abd0Seric (void) strcat(buf, " ["); 1946b056abd0Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 1947b056abd0Seric (void) strcat(buf, "]"); 19483a100e8bSeric # endif 19493a100e8bSeric } 1950bcb9b028Seric else if (strcmp(stat, "queued") != 0) 19513a100e8bSeric { 19523a100e8bSeric char *p = macvalue('h', e); 19533a100e8bSeric 19543a100e8bSeric if (p != NULL && p[0] != '\0') 1955d2ece200Seric sprintf(buf, "relay=%s", p); 19563a100e8bSeric } 1957b056abd0Seric if (buf[0] != '\0') 19584e797715Seric syslog(LOG_INFO, "%s: %s", e->e_id, buf); 19593a100e8bSeric 19603a100e8bSeric syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63)); 19613a100e8bSeric # endif /* short log buffer */ 19626c2c3107Seric # endif /* LOG */ 196325a99e2eSeric } 196425a99e2eSeric /* 196551552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 196625a99e2eSeric ** 196751552439Seric ** This can be made an arbitrary message separator by changing $l 196851552439Seric ** 19699b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 19709b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 19719b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 19729b6c17a6Seric ** this kind of antique garbage???? 197325a99e2eSeric ** 197425a99e2eSeric ** Parameters: 19755aa0f353Seric ** mci -- the connection information. 19765aa0f353Seric ** e -- the envelope. 197725a99e2eSeric ** 197825a99e2eSeric ** Returns: 197951552439Seric ** none 198025a99e2eSeric ** 198125a99e2eSeric ** Side Effects: 198251552439Seric ** outputs some text to fp. 198325a99e2eSeric */ 198425a99e2eSeric 19855aa0f353Seric putfromline(mci, e) 19865aa0f353Seric register MCI *mci; 1987b31e7f2bSeric ENVELOPE *e; 198825a99e2eSeric { 19892bc47524Seric char *template = "\201l\n"; 199051552439Seric char buf[MAXLINE]; 199125a99e2eSeric 19925aa0f353Seric if (bitnset(M_NHDR, mci->mci_mailer->m_flags)) 199351552439Seric return; 199413bbc08cSeric 19952c7e1b8dSeric # ifdef UGLYUUCP 19965aa0f353Seric if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags)) 199774b6e67bSeric { 1998ea09d6edSeric char *bang; 1999ea09d6edSeric char xbuf[MAXLINE]; 200074b6e67bSeric 2001ee4b0922Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 20026c2c3107Seric bang = strchr(buf, '!'); 200374b6e67bSeric if (bang == NULL) 200434fcca25Seric { 200534fcca25Seric errno = 0; 200634fcca25Seric syserr("554 No ! in UUCP From address! (%s given)", buf); 200734fcca25Seric } 200874b6e67bSeric else 2009588cad61Seric { 2010ea09d6edSeric *bang++ = '\0'; 20112bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 2012ea09d6edSeric template = xbuf; 201374b6e67bSeric } 2014588cad61Seric } 20156c2c3107Seric # endif /* UGLYUUCP */ 2016b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 20175aa0f353Seric putline(buf, mci); 2018bc6e2962Seric } 2019bc6e2962Seric /* 202051552439Seric ** PUTBODY -- put the body of a message. 202151552439Seric ** 202251552439Seric ** Parameters: 20235aa0f353Seric ** mci -- the connection information. 20249a6a5f55Seric ** e -- the envelope to put out. 202503c02fdeSeric ** separator -- if non-NULL, a message separator that must 202603c02fdeSeric ** not be permitted in the resulting message. 202751552439Seric ** 202851552439Seric ** Returns: 202951552439Seric ** none. 203051552439Seric ** 203151552439Seric ** Side Effects: 203251552439Seric ** The message is written onto fp. 203351552439Seric */ 203451552439Seric 20355aa0f353Seric putbody(mci, e, separator) 20365aa0f353Seric register MCI *mci; 20379a6a5f55Seric register ENVELOPE *e; 203803c02fdeSeric char *separator; 203951552439Seric { 204077b52738Seric char buf[MAXLINE]; 204151552439Seric 204251552439Seric /* 204351552439Seric ** Output the body of the message 204451552439Seric */ 204551552439Seric 20469a6a5f55Seric if (e->e_dfp == NULL) 204751552439Seric { 20489a6a5f55Seric if (e->e_df != NULL) 20499a6a5f55Seric { 20509a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 20519a6a5f55Seric if (e->e_dfp == NULL) 20528f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 2053e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 20549a6a5f55Seric } 20559a6a5f55Seric else 2056*a6ce3008Seric { 2057*a6ce3008Seric if (bitset(MCIF_INHEADER, mci->mci_flags)) 2058*a6ce3008Seric { 2059*a6ce3008Seric putline("", mci); 2060*a6ce3008Seric mci->mci_flags &= ~MCIF_INHEADER; 2061*a6ce3008Seric } 20625aa0f353Seric putline("<<< No Message Collected >>>", mci); 20639a6a5f55Seric } 2064*a6ce3008Seric } 20659a6a5f55Seric if (e->e_dfp != NULL) 20669a6a5f55Seric { 20679a6a5f55Seric rewind(e->e_dfp); 2068*a6ce3008Seric 2069*a6ce3008Seric if (bitset(MCIF_CVT8TO7, mci->mci_flags)) 2070*a6ce3008Seric { 2071*a6ce3008Seric /* do 8 to 7 bit MIME conversion */ 2072*a6ce3008Seric if (hvalue("MIME-Version", e->e_header) == NULL) 2073*a6ce3008Seric putline("MIME-Version: 1.0", mci); 2074*a6ce3008Seric mime8to7(mci, e->e_header, e, NULL); 2075*a6ce3008Seric } 2076*a6ce3008Seric else 2077*a6ce3008Seric { 2078*a6ce3008Seric /* we can pass it through unmodified */ 2079*a6ce3008Seric if (bitset(MCIF_INHEADER, mci->mci_flags)) 2080*a6ce3008Seric { 2081*a6ce3008Seric putline("", mci); 2082*a6ce3008Seric mci->mci_flags &= ~MCIF_INHEADER; 2083*a6ce3008Seric } 2084*a6ce3008Seric while (!ferror(mci->mci_out) && 2085*a6ce3008Seric fgets(buf, sizeof buf, e->e_dfp) != NULL) 208624fc8aeeSeric { 20875aa0f353Seric if (buf[0] == 'F' && 20885aa0f353Seric bitnset(M_ESCFROM, mci->mci_mailer->m_flags) && 2089d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 20905aa0f353Seric (void) putc('>', mci->mci_out); 2091*a6ce3008Seric if (buf[0] == '-' && buf[1] == '-' && 2092*a6ce3008Seric separator != NULL) 209303c02fdeSeric { 209403c02fdeSeric /* possible separator */ 209503c02fdeSeric int sl = strlen(separator); 209603c02fdeSeric 209703c02fdeSeric if (strncmp(&buf[2], separator, sl) == 0) 20985aa0f353Seric (void) putc(' ', mci->mci_out); 209903c02fdeSeric } 21005aa0f353Seric putline(buf, mci); 210124fc8aeeSeric } 2102*a6ce3008Seric } 210351552439Seric 21049a6a5f55Seric if (ferror(e->e_dfp)) 210551552439Seric { 2106df106f0bSeric syserr("putbody: %s: read error", e->e_df); 210751552439Seric ExitStat = EX_IOERR; 210851552439Seric } 210951552439Seric } 211051552439Seric 21110890ba1fSeric /* some mailers want extra blank line at end of message */ 21125aa0f353Seric if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) && 21135aa0f353Seric buf[0] != '\0' && buf[0] != '\n') 21145aa0f353Seric putline("", mci); 21150890ba1fSeric 21165aa0f353Seric (void) fflush(mci->mci_out); 21175aa0f353Seric if (ferror(mci->mci_out) && errno != EPIPE) 211851552439Seric { 211951552439Seric syserr("putbody: write error"); 212051552439Seric ExitStat = EX_IOERR; 212151552439Seric } 212251552439Seric errno = 0; 212325a99e2eSeric } 212425a99e2eSeric /* 212525a99e2eSeric ** MAILFILE -- Send a message to a file. 212625a99e2eSeric ** 2127f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 2128f129ec7dSeric ** bits, sendmail will try to become the owner of that file 2129f129ec7dSeric ** rather than the real user. Obviously, this only works if 2130f129ec7dSeric ** sendmail runs as root. 2131f129ec7dSeric ** 2132588cad61Seric ** This could be done as a subordinate mailer, except that it 2133588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 2134588cad61Seric ** view this as being sufficiently important as to include it 2135588cad61Seric ** here. For example, if the system is dying, we shouldn't have 2136588cad61Seric ** to create another process plus some pipes to save the message. 2137588cad61Seric ** 213825a99e2eSeric ** Parameters: 213925a99e2eSeric ** filename -- the name of the file to send to. 21406259796dSeric ** ctladdr -- the controlling address header -- includes 21416259796dSeric ** the userid/groupid to be when sending. 214225a99e2eSeric ** 214325a99e2eSeric ** Returns: 214425a99e2eSeric ** The exit code associated with the operation. 214525a99e2eSeric ** 214625a99e2eSeric ** Side Effects: 214725a99e2eSeric ** none. 214825a99e2eSeric */ 214925a99e2eSeric 2150b31e7f2bSeric mailfile(filename, ctladdr, e) 215125a99e2eSeric char *filename; 21526259796dSeric ADDRESS *ctladdr; 2153b31e7f2bSeric register ENVELOPE *e; 215425a99e2eSeric { 215525a99e2eSeric register FILE *f; 215632d19d43Seric register int pid; 215715d084d5Seric int mode; 215825a99e2eSeric 2159671745f3Seric if (tTd(11, 1)) 2160671745f3Seric { 2161671745f3Seric printf("mailfile %s\n ctladdr=", filename); 2162671745f3Seric printaddr(ctladdr, FALSE); 2163671745f3Seric } 2164671745f3Seric 2165f170942cSeric if (e->e_xfp != NULL) 2166f170942cSeric fflush(e->e_xfp); 2167f170942cSeric 216832d19d43Seric /* 216932d19d43Seric ** Fork so we can change permissions here. 217032d19d43Seric ** Note that we MUST use fork, not vfork, because of 217132d19d43Seric ** the complications of calling subroutines, etc. 217232d19d43Seric */ 217332d19d43Seric 217432d19d43Seric DOFORK(fork); 217532d19d43Seric 217632d19d43Seric if (pid < 0) 217732d19d43Seric return (EX_OSERR); 217832d19d43Seric else if (pid == 0) 217932d19d43Seric { 218032d19d43Seric /* child -- actually write to file */ 2181f129ec7dSeric struct stat stb; 21825aa0f353Seric MCI mcibuf; 2183f129ec7dSeric 21842b9178d3Seric (void) setsignal(SIGINT, SIG_DFL); 21852b9178d3Seric (void) setsignal(SIGHUP, SIG_DFL); 21862b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 21873462ad9eSeric (void) umask(OldUmask); 218895f16dc0Seric 2189f129ec7dSeric if (stat(filename, &stb) < 0) 21903a98e7eaSeric stb.st_mode = FileMode; 219115d084d5Seric mode = stb.st_mode; 219295f16dc0Seric 219395f16dc0Seric /* limit the errors to those actually caused in the child */ 219495f16dc0Seric errno = 0; 219595f16dc0Seric ExitStat = EX_OK; 219695f16dc0Seric 2197f129ec7dSeric if (bitset(0111, stb.st_mode)) 2198f129ec7dSeric exit(EX_CANTCREAT); 219919428781Seric if (ctladdr != NULL) 220015d084d5Seric { 220115d084d5Seric /* ignore setuid and setgid bits */ 220215d084d5Seric mode &= ~(S_ISGID|S_ISUID); 220315d084d5Seric } 220415d084d5Seric 22058f9146b0Srick /* we have to open the dfile BEFORE setuid */ 22068f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 22078f9146b0Srick { 22088f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 220995f16dc0Seric if (e->e_dfp == NULL) 221095f16dc0Seric { 22118f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 2212e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 22138f9146b0Srick } 22148f9146b0Srick } 22158f9146b0Srick 221615d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 2217e36b99e2Seric { 221819428781Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 221995f16dc0Seric { 2220898a126bSbostic (void) initgroups(DefUser, DefGid); 222195f16dc0Seric } 222295f16dc0Seric else 222395f16dc0Seric { 2224898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 2225898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 2226898a126bSbostic ctladdr->q_gid); 2227898a126bSbostic } 2228e36b99e2Seric } 222915d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 2230e36b99e2Seric { 223119428781Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 2232e36b99e2Seric (void) setuid(DefUid); 2233e36b99e2Seric else 22346259796dSeric (void) setuid(ctladdr->q_uid); 2235e36b99e2Seric } 223695f16dc0Seric FileName = filename; 223795f16dc0Seric LineNumber = 0; 22383a98e7eaSeric f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 223925a99e2eSeric if (f == NULL) 224095f16dc0Seric { 2241b6a0de9dSeric message("554 cannot open: %s", errstring(errno)); 224232d19d43Seric exit(EX_CANTCREAT); 224395f16dc0Seric } 224425a99e2eSeric 22455aa0f353Seric bzero(&mcibuf, sizeof mcibuf); 22465aa0f353Seric mcibuf.mci_mailer = FileMailer; 22475aa0f353Seric mcibuf.mci_out = f; 22485aa0f353Seric if (bitnset(M_7BITS, FileMailer->m_flags)) 22495aa0f353Seric mcibuf.mci_flags |= MCIF_7BIT; 22505aa0f353Seric 22515aa0f353Seric putfromline(&mcibuf, e); 2252*a6ce3008Seric (*e->e_puthdr)(&mcibuf, e->e_header, e); 22535aa0f353Seric putline("\n", &mcibuf); 22545aa0f353Seric (*e->e_putbody)(&mcibuf, e, NULL); 22555aa0f353Seric putline("\n", &mcibuf); 225695f16dc0Seric if (ferror(f)) 225795f16dc0Seric { 2258b6a0de9dSeric message("451 I/O error: %s", errstring(errno)); 225995f16dc0Seric setstat(EX_IOERR); 226095f16dc0Seric } 2261ee4b0922Seric (void) xfclose(f, "mailfile", filename); 226232d19d43Seric (void) fflush(stdout); 2263e36b99e2Seric 226427628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 2265c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 226695f16dc0Seric exit(ExitStat); 226713bbc08cSeric /*NOTREACHED*/ 226832d19d43Seric } 226932d19d43Seric else 227032d19d43Seric { 227132d19d43Seric /* parent -- wait for exit status */ 2272588cad61Seric int st; 227332d19d43Seric 2274588cad61Seric st = waitfor(pid); 2275bf9bc890Seric if (WIFEXITED(st)) 2276bf9bc890Seric return (WEXITSTATUS(st)); 2277588cad61Seric else 2278b6a0de9dSeric { 2279b6a0de9dSeric syserr("child died on signal %d", st); 2280bf9bc890Seric return (EX_UNAVAILABLE); 2281b6a0de9dSeric } 22828f9146b0Srick /*NOTREACHED*/ 228332d19d43Seric } 228425a99e2eSeric } 2285ea4dc939Seric /* 2286e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 2287e103b48fSeric ** 2288e103b48fSeric ** The signature describes how we are going to send this -- it 2289e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 2290e103b48fSeric ** an ordered list of MX hosts. 2291e103b48fSeric ** 2292e103b48fSeric ** Parameters: 2293e103b48fSeric ** m -- the mailer describing this host. 2294e103b48fSeric ** host -- the host name. 2295e103b48fSeric ** e -- the current envelope. 2296e103b48fSeric ** 2297e103b48fSeric ** Returns: 2298e103b48fSeric ** The signature for this host. 2299e103b48fSeric ** 2300e103b48fSeric ** Side Effects: 2301e103b48fSeric ** Can tweak the symbol table. 2302e103b48fSeric */ 2303e103b48fSeric 2304e103b48fSeric char * 2305e103b48fSeric hostsignature(m, host, e) 2306e103b48fSeric register MAILER *m; 2307e103b48fSeric char *host; 2308e103b48fSeric ENVELOPE *e; 2309e103b48fSeric { 2310e103b48fSeric register char *p; 2311e103b48fSeric register STAB *s; 2312e103b48fSeric int i; 2313e103b48fSeric int len; 23149d4a8008Seric #if NAMED_BIND 2315e103b48fSeric int nmx; 2316e103b48fSeric auto int rcode; 2317bafdc4e5Seric char *hp; 2318bafdc4e5Seric char *endp; 2319516782b4Seric int oldoptions; 2320e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 2321e103b48fSeric #endif 2322e103b48fSeric 2323e103b48fSeric /* 2324e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 2325e103b48fSeric */ 2326e103b48fSeric 2327e103b48fSeric p = m->m_mailer; 2328e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2329e103b48fSeric { 2330e103b48fSeric /* just an ordinary mailer */ 2331e103b48fSeric return host; 2332e103b48fSeric } 2333e103b48fSeric 2334e103b48fSeric /* 2335e103b48fSeric ** Look it up in the symbol table. 2336e103b48fSeric */ 2337e103b48fSeric 2338e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 2339e103b48fSeric if (s->s_hostsig != NULL) 2340e103b48fSeric return s->s_hostsig; 2341e103b48fSeric 2342e103b48fSeric /* 2343e103b48fSeric ** Not already there -- create a signature. 2344e103b48fSeric */ 2345e103b48fSeric 23469d4a8008Seric #if NAMED_BIND 2347516782b4Seric if (ConfigLevel < 2) 2348516782b4Seric { 2349516782b4Seric oldoptions = _res.options; 2350516782b4Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2351516782b4Seric } 2352516782b4Seric 2353bafdc4e5Seric for (hp = host; hp != NULL; hp = endp) 2354bafdc4e5Seric { 2355bafdc4e5Seric endp = strchr(hp, ':'); 2356bafdc4e5Seric if (endp != NULL) 2357bafdc4e5Seric *endp = '\0'; 2358bafdc4e5Seric 23597bf809e6Seric nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 23607d55540cSeric 2361e103b48fSeric if (nmx <= 0) 2362e103b48fSeric { 2363e103b48fSeric register MCI *mci; 2364e103b48fSeric 2365e103b48fSeric /* update the connection info for this host */ 2366bafdc4e5Seric mci = mci_get(hp, m); 2367e103b48fSeric mci->mci_exitstat = rcode; 2368e103b48fSeric mci->mci_errno = errno; 23699d4a8008Seric #if NAMED_BIND 2370f170942cSeric mci->mci_herrno = h_errno; 2371f170942cSeric #endif 2372e103b48fSeric 2373e103b48fSeric /* and return the original host name as the signature */ 2374bafdc4e5Seric nmx = 1; 2375bafdc4e5Seric mxhosts[0] = hp; 2376e103b48fSeric } 2377e103b48fSeric 2378e103b48fSeric len = 0; 2379e103b48fSeric for (i = 0; i < nmx; i++) 2380e103b48fSeric { 2381e103b48fSeric len += strlen(mxhosts[i]) + 1; 2382e103b48fSeric } 2383bafdc4e5Seric if (s->s_hostsig != NULL) 2384bafdc4e5Seric len += strlen(s->s_hostsig) + 1; 2385bafdc4e5Seric p = xalloc(len); 2386bafdc4e5Seric if (s->s_hostsig != NULL) 2387bafdc4e5Seric { 2388bafdc4e5Seric (void) strcpy(p, s->s_hostsig); 2389bafdc4e5Seric free(s->s_hostsig); 2390bafdc4e5Seric s->s_hostsig = p; 2391bafdc4e5Seric p += strlen(p); 2392bafdc4e5Seric *p++ = ':'; 2393bafdc4e5Seric } 2394bafdc4e5Seric else 2395bafdc4e5Seric s->s_hostsig = p; 2396e103b48fSeric for (i = 0; i < nmx; i++) 2397e103b48fSeric { 2398e103b48fSeric if (i != 0) 2399e103b48fSeric *p++ = ':'; 2400e103b48fSeric strcpy(p, mxhosts[i]); 2401e103b48fSeric p += strlen(p); 2402e103b48fSeric } 2403bafdc4e5Seric if (endp != NULL) 2404bafdc4e5Seric *endp++ = ':'; 2405bafdc4e5Seric } 2406e103b48fSeric makelower(s->s_hostsig); 2407516782b4Seric if (ConfigLevel < 2) 2408516782b4Seric _res.options = oldoptions; 2409e103b48fSeric #else 2410e103b48fSeric /* not using BIND -- the signature is just the host name */ 2411e103b48fSeric s->s_hostsig = host; 2412e103b48fSeric #endif 2413e103b48fSeric if (tTd(17, 1)) 2414e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2415e103b48fSeric return s->s_hostsig; 2416e103b48fSeric } 2417