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*51cfe111Seric static char sccsid[] = "@(#)deliver.c 8.46 (Berkeley) 11/30/93"; 11e70a7521Sbostic #endif /* not lint */ 124227346bSdist 13fcde9cc8Sbostic #include "sendmail.h" 14f28da541Smiriam #include <netdb.h> 15911693bfSbostic #include <errno.h> 16134746fbSeric #ifdef NAMED_BIND 17912a731aSbostic #include <arpa/nameser.h> 18912a731aSbostic #include <resolv.h> 19f170942cSeric 20f170942cSeric extern int h_errno; 21134746fbSeric #endif 2225a99e2eSeric 2325a99e2eSeric /* 249c9e68d9Seric ** SENDALL -- actually send all the messages. 259c9e68d9Seric ** 269c9e68d9Seric ** Parameters: 279c9e68d9Seric ** e -- the envelope to send. 289c9e68d9Seric ** mode -- the delivery mode to use. If SM_DEFAULT, use 299c9e68d9Seric ** the current e->e_sendmode. 309c9e68d9Seric ** 319c9e68d9Seric ** Returns: 329c9e68d9Seric ** none. 339c9e68d9Seric ** 349c9e68d9Seric ** Side Effects: 359c9e68d9Seric ** Scans the send lists and sends everything it finds. 369c9e68d9Seric ** Delivers any appropriate error messages. 379c9e68d9Seric ** If we are running in a non-interactive mode, takes the 389c9e68d9Seric ** appropriate action. 399c9e68d9Seric */ 409c9e68d9Seric 419c9e68d9Seric sendall(e, mode) 429c9e68d9Seric ENVELOPE *e; 439c9e68d9Seric char mode; 449c9e68d9Seric { 459c9e68d9Seric register ADDRESS *q; 469c9e68d9Seric char *owner; 479c9e68d9Seric int otherowners; 489c9e68d9Seric register ENVELOPE *ee; 499c9e68d9Seric ENVELOPE *splitenv = NULL; 506103d9b4Seric bool announcequeueup; 519c9e68d9Seric 527880f3e4Seric /* 537880f3e4Seric ** If we have had global, fatal errors, don't bother sending 547880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 557880f3e4Seric ** (e.g., a single address failing) will still cause the other 567880f3e4Seric ** addresses to be sent. 577880f3e4Seric */ 587880f3e4Seric 597880f3e4Seric if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 60896b16f6Seric { 61896b16f6Seric e->e_flags |= EF_CLRQUEUE; 62896b16f6Seric return; 63896b16f6Seric } 64896b16f6Seric 659c9e68d9Seric /* determine actual delivery mode */ 66fbe2f963Seric CurrentLA = getla(); 679c9e68d9Seric if (mode == SM_DEFAULT) 689c9e68d9Seric { 699c9e68d9Seric mode = e->e_sendmode; 709c9e68d9Seric if (mode != SM_VERIFY && 719c9e68d9Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 729c9e68d9Seric mode = SM_QUEUE; 736103d9b4Seric announcequeueup = mode == SM_QUEUE; 749c9e68d9Seric } 756103d9b4Seric else 766103d9b4Seric announcequeueup = FALSE; 779c9e68d9Seric 789c9e68d9Seric if (tTd(13, 1)) 799c9e68d9Seric { 800e484fe5Seric printf("\n===== SENDALL: mode %c, id %s, e_from ", 810e484fe5Seric mode, e->e_id); 829c9e68d9Seric printaddr(&e->e_from, FALSE); 839c9e68d9Seric printf("sendqueue:\n"); 849c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 859c9e68d9Seric } 869c9e68d9Seric 879c9e68d9Seric /* 889c9e68d9Seric ** Do any preprocessing necessary for the mode we are running. 899c9e68d9Seric ** Check to make sure the hop count is reasonable. 909c9e68d9Seric ** Delete sends to the sender in mailing lists. 919c9e68d9Seric */ 929c9e68d9Seric 939c9e68d9Seric CurEnv = e; 949c9e68d9Seric 959c9e68d9Seric if (e->e_hopcount > MaxHopCount) 969c9e68d9Seric { 979c9e68d9Seric errno = 0; 984bb8b0fdSeric syserr("554 too many hops %d (%d max): from %s via %s, to %s", 999c9e68d9Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 1004bb8b0fdSeric RealHostName, e->e_sendqueue->q_paddr); 1019c9e68d9Seric return; 1029c9e68d9Seric } 1039c9e68d9Seric 104b8004690Seric /* 105b8004690Seric ** Do sender deletion. 106b8004690Seric ** 107b8004690Seric ** If the sender has the QQUEUEUP flag set, skip this. 108b8004690Seric ** This can happen if the name server is hosed when you 109b8004690Seric ** are trying to send mail. The result is that the sender 110b8004690Seric ** is instantiated in the queue as a recipient. 111b8004690Seric */ 112b8004690Seric 113e76a6a8fSeric if (!bitset(EF_METOO, e->e_flags) && 114e76a6a8fSeric !bitset(QQUEUEUP, e->e_from.q_flags)) 1159c9e68d9Seric { 1169c9e68d9Seric if (tTd(13, 5)) 1179c9e68d9Seric { 1189c9e68d9Seric printf("sendall: QDONTSEND "); 1199c9e68d9Seric printaddr(&e->e_from, FALSE); 1209c9e68d9Seric } 1219c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 1229c9e68d9Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 1239c9e68d9Seric } 1249c9e68d9Seric 125ce5531bdSeric /* 126ce5531bdSeric ** Handle alias owners. 127ce5531bdSeric ** 128ce5531bdSeric ** We scan up the q_alias chain looking for owners. 129ce5531bdSeric ** We discard owners that are the same as the return path. 130ce5531bdSeric */ 131ce5531bdSeric 132ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 133ce5531bdSeric { 134ce5531bdSeric register struct address *a; 135ce5531bdSeric 136ce5531bdSeric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 137ce5531bdSeric continue; 138ce5531bdSeric if (a != NULL) 139ce5531bdSeric q->q_owner = a->q_owner; 140ce5531bdSeric 141ce5531bdSeric if (q->q_owner != NULL && 142ce5531bdSeric !bitset(QDONTSEND, q->q_flags) && 143ce5531bdSeric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 144ce5531bdSeric q->q_owner = NULL; 145ce5531bdSeric } 146ce5531bdSeric 147ce5531bdSeric owner = ""; 148ce5531bdSeric otherowners = 1; 149ce5531bdSeric while (owner != NULL && otherowners > 0) 150ce5531bdSeric { 151ce5531bdSeric owner = NULL; 152ce5531bdSeric otherowners = 0; 153ce5531bdSeric 154ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 155ce5531bdSeric { 156ce5531bdSeric if (bitset(QDONTSEND, q->q_flags)) 157ce5531bdSeric continue; 158ce5531bdSeric 159ce5531bdSeric if (q->q_owner != NULL) 160ce5531bdSeric { 161ce5531bdSeric if (owner == NULL) 162ce5531bdSeric owner = q->q_owner; 163ce5531bdSeric else if (owner != q->q_owner) 164ce5531bdSeric { 165ce5531bdSeric if (strcmp(owner, q->q_owner) == 0) 166ce5531bdSeric { 167ce5531bdSeric /* make future comparisons cheap */ 168ce5531bdSeric q->q_owner = owner; 169ce5531bdSeric } 170ce5531bdSeric else 171ce5531bdSeric { 172ce5531bdSeric otherowners++; 173ce5531bdSeric } 174ce5531bdSeric owner = q->q_owner; 175ce5531bdSeric } 176ce5531bdSeric } 177ce5531bdSeric else 178ce5531bdSeric { 179ce5531bdSeric otherowners++; 180ce5531bdSeric } 181ce5531bdSeric } 182ce5531bdSeric 183ce5531bdSeric if (owner != NULL && otherowners > 0) 184ce5531bdSeric { 185ce5531bdSeric extern HDR *copyheader(); 186ce5531bdSeric extern ADDRESS *copyqueue(); 187ce5531bdSeric 188ce5531bdSeric /* 189ce5531bdSeric ** Split this envelope into two. 190ce5531bdSeric */ 191ce5531bdSeric 192ce5531bdSeric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 193ce5531bdSeric *ee = *e; 194ce5531bdSeric ee->e_id = NULL; 195ce5531bdSeric (void) queuename(ee, '\0'); 196ce5531bdSeric 197ce5531bdSeric if (tTd(13, 1)) 198ce5531bdSeric printf("sendall: split %s into %s\n", 199ce5531bdSeric e->e_id, ee->e_id); 200ce5531bdSeric 201ce5531bdSeric ee->e_header = copyheader(e->e_header); 202ce5531bdSeric ee->e_sendqueue = copyqueue(e->e_sendqueue); 203ce5531bdSeric ee->e_errorqueue = copyqueue(e->e_errorqueue); 204ce5531bdSeric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS); 205ce5531bdSeric setsender(owner, ee, NULL, TRUE); 206ce5531bdSeric if (tTd(13, 5)) 207ce5531bdSeric { 208ce5531bdSeric printf("sendall(split): QDONTSEND "); 209ce5531bdSeric printaddr(&ee->e_from, FALSE); 210ce5531bdSeric } 211ce5531bdSeric ee->e_from.q_flags |= QDONTSEND; 212ce5531bdSeric ee->e_dfp = NULL; 213ce5531bdSeric ee->e_xfp = NULL; 214ce5531bdSeric ee->e_df = NULL; 215ce5531bdSeric ee->e_errormode = EM_MAIL; 216ce5531bdSeric ee->e_sibling = splitenv; 217ce5531bdSeric splitenv = ee; 218ce5531bdSeric 219ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 220ce5531bdSeric if (q->q_owner == owner) 221ce5531bdSeric q->q_flags |= QDONTSEND; 222ce5531bdSeric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 223ce5531bdSeric if (q->q_owner != owner) 224ce5531bdSeric q->q_flags |= QDONTSEND; 225ce5531bdSeric 226ce5531bdSeric if (e->e_df != NULL && mode != SM_VERIFY) 227ce5531bdSeric { 228ce5531bdSeric ee->e_dfp = NULL; 229da662164Seric ee->e_df = queuename(ee, 'd'); 230da662164Seric ee->e_df = newstr(ee->e_df); 231ce5531bdSeric if (link(e->e_df, ee->e_df) < 0) 232ce5531bdSeric { 233ce5531bdSeric syserr("sendall: link(%s, %s)", 234ce5531bdSeric e->e_df, ee->e_df); 235ce5531bdSeric } 236ce5531bdSeric } 237ce5531bdSeric 238ce5531bdSeric if (mode != SM_VERIFY) 239ce5531bdSeric openxscript(ee); 240ce5531bdSeric #ifdef LOG 241ce5531bdSeric if (LogLevel > 4) 242ce5531bdSeric syslog(LOG_INFO, "%s: clone %s", 243ce5531bdSeric ee->e_id, e->e_id); 244ce5531bdSeric #endif 245ce5531bdSeric } 246ce5531bdSeric } 247ce5531bdSeric 248ce5531bdSeric if (owner != NULL) 249ce5531bdSeric { 250ce5531bdSeric setsender(owner, e, NULL, TRUE); 251ce5531bdSeric if (tTd(13, 5)) 252ce5531bdSeric { 253ce5531bdSeric printf("sendall(owner): QDONTSEND "); 254ce5531bdSeric printaddr(&e->e_from, FALSE); 255ce5531bdSeric } 256ce5531bdSeric e->e_from.q_flags |= QDONTSEND; 257ce5531bdSeric e->e_errormode = EM_MAIL; 258ce5531bdSeric } 259ce5531bdSeric 260c1ac89b1Seric # ifdef QUEUE 261c1ac89b1Seric if ((mode == SM_QUEUE || mode == SM_FORK || 262c1ac89b1Seric (mode != SM_VERIFY && SuperSafe)) && 263c1ac89b1Seric !bitset(EF_INQUEUE, e->e_flags)) 264c1ac89b1Seric { 265c1ac89b1Seric /* be sure everything is instantiated in the queue */ 2666103d9b4Seric queueup(e, TRUE, announcequeueup); 267ce5531bdSeric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 2686103d9b4Seric queueup(ee, TRUE, announcequeueup); 269c1ac89b1Seric } 270c1ac89b1Seric #endif /* QUEUE */ 271c1ac89b1Seric 272ce5531bdSeric if (splitenv != NULL) 273ce5531bdSeric { 274ce5531bdSeric if (tTd(13, 1)) 275ce5531bdSeric { 276ce5531bdSeric printf("\nsendall: Split queue; remaining queue:\n"); 277ce5531bdSeric printaddr(e->e_sendqueue, TRUE); 278ce5531bdSeric } 279ce5531bdSeric 280ce5531bdSeric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 281ce5531bdSeric { 282ce5531bdSeric CurEnv = ee; 283ce5531bdSeric sendenvelope(ee, mode); 284ce5531bdSeric } 285ce5531bdSeric 286ce5531bdSeric CurEnv = e; 287ce5531bdSeric } 288ce5531bdSeric sendenvelope(e, mode); 289ce5531bdSeric 290ce5531bdSeric for (; splitenv != NULL; splitenv = splitenv->e_sibling) 291ce5531bdSeric dropenvelope(splitenv); 292ce5531bdSeric } 293ce5531bdSeric 294ce5531bdSeric sendenvelope(e, mode) 295ce5531bdSeric register ENVELOPE *e; 296ce5531bdSeric char mode; 297ce5531bdSeric { 298ce5531bdSeric bool oldverbose; 299ce5531bdSeric int pid; 300ce5531bdSeric register ADDRESS *q; 3016b2765c6Seric char *qf; 3026b2765c6Seric char *id; 303ce5531bdSeric 3047880f3e4Seric /* 3057880f3e4Seric ** If we have had global, fatal errors, don't bother sending 3067880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 3077880f3e4Seric ** (e.g., a single address failing) will still cause the other 3087880f3e4Seric ** addresses to be sent. 3097880f3e4Seric */ 3107880f3e4Seric 3117880f3e4Seric if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 3127880f3e4Seric { 3137880f3e4Seric e->e_flags |= EF_CLRQUEUE; 3147880f3e4Seric return; 3157880f3e4Seric } 3167880f3e4Seric 317ce5531bdSeric oldverbose = Verbose; 318c1ac89b1Seric switch (mode) 319c1ac89b1Seric { 320c1ac89b1Seric case SM_VERIFY: 321c1ac89b1Seric Verbose = TRUE; 322c1ac89b1Seric break; 323c1ac89b1Seric 324c1ac89b1Seric case SM_QUEUE: 325c1ac89b1Seric queueonly: 326c1ac89b1Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 327c1ac89b1Seric return; 328c1ac89b1Seric 329c1ac89b1Seric case SM_FORK: 330c1ac89b1Seric if (e->e_xfp != NULL) 331c1ac89b1Seric (void) fflush(e->e_xfp); 332c1ac89b1Seric 3332b9178d3Seric # ifndef HASFLOCK 334c1ac89b1Seric /* 3356b2765c6Seric ** Since fcntl locking has the interesting semantic that 3366b2765c6Seric ** the lock is owned by a process, not by an open file 3376b2765c6Seric ** descriptor, we have to flush this to the queue, and 3386b2765c6Seric ** then restart from scratch in the child. 339c1ac89b1Seric */ 340c1ac89b1Seric 3416b2765c6Seric /* save id for future use */ 3426b2765c6Seric id = e->e_id; 3436b2765c6Seric 3446b2765c6Seric /* now drop the envelope in the parent */ 3456b2765c6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 3466b2765c6Seric dropenvelope(e); 3476b2765c6Seric 3486b2765c6Seric /* and reacquire in the child */ 3496b2765c6Seric (void) dowork(id, TRUE, FALSE, e); 3506b2765c6Seric 3516b2765c6Seric return; 3526b2765c6Seric 3536b2765c6Seric # else /* HASFLOCK */ 354c1ac89b1Seric 355c1ac89b1Seric pid = fork(); 356c1ac89b1Seric if (pid < 0) 357c1ac89b1Seric { 358c1ac89b1Seric goto queueonly; 359c1ac89b1Seric } 360c1ac89b1Seric else if (pid > 0) 361c1ac89b1Seric { 3620e484fe5Seric /* be sure we leave the temp files to our child */ 3630e484fe5Seric /* can't call unlockqueue to avoid unlink of xfp */ 3640e484fe5Seric if (e->e_lockfp != NULL) 3650e484fe5Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 3660e484fe5Seric e->e_lockfp = NULL; 3670e484fe5Seric 3680e484fe5Seric /* close any random open files in the envelope */ 3690e484fe5Seric closexscript(e); 3700e484fe5Seric if (e->e_dfp != NULL) 3710e484fe5Seric (void) xfclose(e->e_dfp, "sendenvelope", e->e_df); 3720e484fe5Seric e->e_dfp = NULL; 3730e484fe5Seric e->e_id = e->e_df = NULL; 374c1ac89b1Seric return; 375c1ac89b1Seric } 376c1ac89b1Seric 377c1ac89b1Seric /* double fork to avoid zombies */ 378c1ac89b1Seric if (fork() > 0) 379c1ac89b1Seric exit(EX_OK); 380c1ac89b1Seric 381c1ac89b1Seric /* be sure we are immune from the terminal */ 3827880f3e4Seric disconnect(1, e); 383c1ac89b1Seric 384c1ac89b1Seric /* 385c1ac89b1Seric ** Close any cached connections. 386c1ac89b1Seric ** 387c1ac89b1Seric ** We don't send the QUIT protocol because the parent 388c1ac89b1Seric ** still knows about the connection. 389c1ac89b1Seric ** 390c1ac89b1Seric ** This should only happen when delivering an error 391c1ac89b1Seric ** message. 392c1ac89b1Seric */ 393c1ac89b1Seric 394c1ac89b1Seric mci_flush(FALSE, NULL); 395c1ac89b1Seric 3966b2765c6Seric # endif /* HASFLOCK */ 3976b2765c6Seric 398c1ac89b1Seric break; 399c1ac89b1Seric } 400c1ac89b1Seric 401c1ac89b1Seric /* 4029c9e68d9Seric ** Run through the list and send everything. 4035288e21aSeric ** 4045288e21aSeric ** Set EF_GLOBALERRS so that error messages during delivery 4055288e21aSeric ** result in returned mail. 4069c9e68d9Seric */ 4079c9e68d9Seric 4089c9e68d9Seric e->e_nsent = 0; 4095288e21aSeric e->e_flags |= EF_GLOBALERRS; 410faad2b16Seric 411faad2b16Seric /* now run through the queue */ 4129c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 4139c9e68d9Seric { 414fdc75a0fSeric #ifdef XDEBUG 415fdc75a0fSeric char wbuf[MAXNAME + 20]; 416fdc75a0fSeric 417fdc75a0fSeric (void) sprintf(wbuf, "sendall(%s)", q->q_paddr); 418fdc75a0fSeric checkfd012(wbuf); 419fdc75a0fSeric #endif 4209c9e68d9Seric if (mode == SM_VERIFY) 4219c9e68d9Seric { 4229c9e68d9Seric e->e_to = q->q_paddr; 4239c9e68d9Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4248dfca105Seric { 425fafe46e1Seric if (q->q_host != NULL && q->q_host[0] != '\0') 4268dfca105Seric message("deliverable: mailer %s, host %s, user %s", 4278dfca105Seric q->q_mailer->m_name, 4288dfca105Seric q->q_host, 4298dfca105Seric q->q_user); 430fafe46e1Seric else 431fafe46e1Seric message("deliverable: mailer %s, user %s", 432fafe46e1Seric q->q_mailer->m_name, 433fafe46e1Seric q->q_user); 4348dfca105Seric } 4359c9e68d9Seric } 4369c9e68d9Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4379c9e68d9Seric { 4389c9e68d9Seric # ifdef QUEUE 4399c9e68d9Seric /* 4409c9e68d9Seric ** Checkpoint the send list every few addresses 4419c9e68d9Seric */ 4429c9e68d9Seric 4439c9e68d9Seric if (e->e_nsent >= CheckpointInterval) 4449c9e68d9Seric { 4459c9e68d9Seric queueup(e, TRUE, FALSE); 4469c9e68d9Seric e->e_nsent = 0; 4479c9e68d9Seric } 4489c9e68d9Seric # endif /* QUEUE */ 4499c9e68d9Seric (void) deliver(e, q); 4509c9e68d9Seric } 4519c9e68d9Seric } 4529c9e68d9Seric Verbose = oldverbose; 4539c9e68d9Seric 454fdc75a0fSeric #ifdef XDEBUG 455fdc75a0fSeric checkfd012("end of sendenvelope"); 456fdc75a0fSeric #endif 457fdc75a0fSeric 4589c9e68d9Seric if (mode == SM_FORK) 4599c9e68d9Seric finis(); 4609c9e68d9Seric } 4619c9e68d9Seric /* 4629c9e68d9Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4639c9e68d9Seric ** 4649c9e68d9Seric ** This MUST be a macro, since after a vfork we are running 4659c9e68d9Seric ** two processes on the same stack!!! 4669c9e68d9Seric ** 4679c9e68d9Seric ** Parameters: 4689c9e68d9Seric ** none. 4699c9e68d9Seric ** 4709c9e68d9Seric ** Returns: 4719c9e68d9Seric ** From a macro??? You've got to be kidding! 4729c9e68d9Seric ** 4739c9e68d9Seric ** Side Effects: 4749c9e68d9Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4759c9e68d9Seric ** pid of child in parent, zero in child. 4769c9e68d9Seric ** -1 on unrecoverable error. 4779c9e68d9Seric ** 4789c9e68d9Seric ** Notes: 4799c9e68d9Seric ** I'm awfully sorry this looks so awful. That's 4809c9e68d9Seric ** vfork for you..... 4819c9e68d9Seric */ 4829c9e68d9Seric 4839c9e68d9Seric # define NFORKTRIES 5 4849c9e68d9Seric 4859c9e68d9Seric # ifndef FORK 4869c9e68d9Seric # define FORK fork 4879c9e68d9Seric # endif 4889c9e68d9Seric 4899c9e68d9Seric # define DOFORK(fORKfN) \ 4909c9e68d9Seric {\ 4919c9e68d9Seric register int i;\ 4929c9e68d9Seric \ 4939c9e68d9Seric for (i = NFORKTRIES; --i >= 0; )\ 4949c9e68d9Seric {\ 4959c9e68d9Seric pid = fORKfN();\ 4969c9e68d9Seric if (pid >= 0)\ 4979c9e68d9Seric break;\ 4989c9e68d9Seric if (i > 0)\ 4999c9e68d9Seric sleep((unsigned) NFORKTRIES - i);\ 5009c9e68d9Seric }\ 5019c9e68d9Seric } 5029c9e68d9Seric /* 5039c9e68d9Seric ** DOFORK -- simple fork interface to DOFORK. 5049c9e68d9Seric ** 5059c9e68d9Seric ** Parameters: 5069c9e68d9Seric ** none. 5079c9e68d9Seric ** 5089c9e68d9Seric ** Returns: 5099c9e68d9Seric ** pid of child in parent. 5109c9e68d9Seric ** zero in child. 5119c9e68d9Seric ** -1 on error. 5129c9e68d9Seric ** 5139c9e68d9Seric ** Side Effects: 5149c9e68d9Seric ** returns twice, once in parent and once in child. 5159c9e68d9Seric */ 5169c9e68d9Seric 5179c9e68d9Seric dofork() 5189c9e68d9Seric { 5199c9e68d9Seric register int pid; 5209c9e68d9Seric 5219c9e68d9Seric DOFORK(fork); 5229c9e68d9Seric return (pid); 5239c9e68d9Seric } 5249c9e68d9Seric /* 52513bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 52613bbc08cSeric ** 52713bbc08cSeric ** This routine delivers to everyone on the same host as the 52813bbc08cSeric ** user on the head of the list. It is clever about mailers 52913bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 53013bbc08cSeric ** that it will deliver to all these addresses however -- so 53113bbc08cSeric ** deliver should be called once for each address on the 53213bbc08cSeric ** list. 53325a99e2eSeric ** 53425a99e2eSeric ** Parameters: 535588cad61Seric ** e -- the envelope to deliver. 536c77d1c25Seric ** firstto -- head of the address list to deliver to. 53725a99e2eSeric ** 53825a99e2eSeric ** Returns: 53925a99e2eSeric ** zero -- successfully delivered. 54025a99e2eSeric ** else -- some failure, see ExitStat for more info. 54125a99e2eSeric ** 54225a99e2eSeric ** Side Effects: 54325a99e2eSeric ** The standard input is passed off to someone. 54425a99e2eSeric */ 54525a99e2eSeric 546588cad61Seric deliver(e, firstto) 547588cad61Seric register ENVELOPE *e; 548c77d1c25Seric ADDRESS *firstto; 54925a99e2eSeric { 55078442df3Seric char *host; /* host being sent to */ 55178442df3Seric char *user; /* user being sent to */ 55225a99e2eSeric char **pvp; 5535dfc646bSeric register char **mvp; 55425a99e2eSeric register char *p; 555588cad61Seric register MAILER *m; /* mailer for this recipient */ 5566259796dSeric ADDRESS *ctladdr; 557b31e7f2bSeric register MCI *mci; 558c77d1c25Seric register ADDRESS *to = firstto; 559c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 560772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 561911693bfSbostic int rcode; /* response code */ 562e103b48fSeric char *firstsig; /* signature of firstto */ 5639c9e68d9Seric int pid; 5649c9e68d9Seric char *curhost; 5659c9e68d9Seric int mpvect[2]; 5669c9e68d9Seric int rpvect[2]; 567ee6bf8dfSeric char *pv[MAXPV+1]; 568579ef0ddSeric char tobuf[TOBUFSIZE]; /* text line of to people */ 569ee6bf8dfSeric char buf[MAXNAME]; 570c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 571fabb3bd4Seric extern int checkcompat(); 572df106f0bSeric extern char SmtpError[]; 57325a99e2eSeric 57435490626Seric errno = 0; 575ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5765dfc646bSeric return (0); 57725a99e2eSeric 578134746fbSeric #ifdef NAMED_BIND 579912a731aSbostic /* unless interactive, try twice, over a minute */ 580912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 581912a731aSbostic _res.retrans = 30; 582912a731aSbostic _res.retry = 2; 583912a731aSbostic } 584d4bd8f0eSbostic #endif 585912a731aSbostic 58651552439Seric m = to->q_mailer; 58751552439Seric host = to->q_host; 588c9be6216Seric CurEnv = e; /* just in case */ 5894384d521Seric e->e_statmsg = NULL; 590df106f0bSeric SmtpError[0] = '\0'; 59151552439Seric 5926ef48975Seric if (tTd(10, 1)) 5935dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 59451552439Seric m->m_mno, host, to->q_user); 595ab81ee53Seric if (tTd(10, 100)) 596ab81ee53Seric printopenfds(FALSE); 597f3dbc832Seric 598f3dbc832Seric /* 599f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 600f3dbc832Seric ** connections now, just mark these addresses and return. 601f3dbc832Seric ** This is useful if we want to batch connections to 602f3dbc832Seric ** reduce load. This will cause the messages to be 603f3dbc832Seric ** queued up, and a daemon will come along to send the 604f3dbc832Seric ** messages later. 605f3dbc832Seric ** This should be on a per-mailer basis. 606f3dbc832Seric */ 607f3dbc832Seric 60879aa5155Seric if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 609f3dbc832Seric { 610f3dbc832Seric for (; to != NULL; to = to->q_next) 611f4560e80Seric { 612ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 613c6e18ac5Seric to->q_mailer != m) 614f4560e80Seric continue; 615268a25e1Seric to->q_flags |= QQUEUEUP; 616588cad61Seric e->e_to = to->q_paddr; 61708b25121Seric message("queued"); 6182f624c86Seric if (LogLevel > 8) 6194dee0003Seric logdelivery(m, NULL, "queued", NULL, e); 620f4560e80Seric } 621588cad61Seric e->e_to = NULL; 622f3dbc832Seric return (0); 623f3dbc832Seric } 624f3dbc832Seric 62525a99e2eSeric /* 6265dfc646bSeric ** Do initial argv setup. 6275dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 6285dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 6295dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 6305dfc646bSeric ** code does not check for 'pv' overflow; this places a 6315dfc646bSeric ** manifest lower limit of 4 for MAXPV. 6323bea8136Seric ** The from address rewrite is expected to make 6333bea8136Seric ** the address relative to the other end. 6345dfc646bSeric */ 6355dfc646bSeric 63678442df3Seric /* rewrite from address, using rewriting rules */ 637efe54562Seric rcode = EX_OK; 638efe54562Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, 639efe54562Seric RF_SENDERADDR|RF_CANONICAL, 640efe54562Seric &rcode, e)); 641ee4b0922Seric define('g', rpathbuf, e); /* translated return path */ 642588cad61Seric define('h', host, e); /* to host */ 6435dfc646bSeric Errors = 0; 6445dfc646bSeric pvp = pv; 6455dfc646bSeric *pvp++ = m->m_argv[0]; 6465dfc646bSeric 6475dfc646bSeric /* insert -f or -r flag as appropriate */ 64857fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6495dfc646bSeric { 65057fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 6515dfc646bSeric *pvp++ = "-f"; 6525dfc646bSeric else 6535dfc646bSeric *pvp++ = "-r"; 654c23ed322Seric *pvp++ = newstr(rpathbuf); 6555dfc646bSeric } 6565dfc646bSeric 6575dfc646bSeric /* 6585dfc646bSeric ** Append the other fixed parts of the argv. These run 6595dfc646bSeric ** up to the first entry containing "$u". There can only 6605dfc646bSeric ** be one of these, and there are only a few more slots 6615dfc646bSeric ** in the pv after it. 6625dfc646bSeric */ 6635dfc646bSeric 6645dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 6655dfc646bSeric { 6662bc47524Seric /* can't use strchr here because of sign extension problems */ 6672bc47524Seric while (*p != '\0') 6682bc47524Seric { 6692bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 6702bc47524Seric { 6712bc47524Seric if (*p == 'u') 6725dfc646bSeric break; 6732bc47524Seric } 6742bc47524Seric } 6752bc47524Seric 6762bc47524Seric if (*p != '\0') 6775dfc646bSeric break; 6785dfc646bSeric 6795dfc646bSeric /* this entry is safe -- go ahead and process it */ 680588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 6815dfc646bSeric *pvp++ = newstr(buf); 6825dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 6835dfc646bSeric { 68408b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 6855dfc646bSeric return (-1); 6865dfc646bSeric } 6875dfc646bSeric } 688c579ef51Seric 68933db8731Seric /* 69033db8731Seric ** If we have no substitution for the user name in the argument 69133db8731Seric ** list, we know that we must supply the names otherwise -- and 69233db8731Seric ** SMTP is the answer!! 69333db8731Seric */ 69433db8731Seric 6955dfc646bSeric if (*mvp == NULL) 696c579ef51Seric { 697c579ef51Seric /* running SMTP */ 6982c7e1b8dSeric # ifdef SMTP 699c579ef51Seric clever = TRUE; 700c579ef51Seric *pvp = NULL; 7016c2c3107Seric # else /* SMTP */ 70233db8731Seric /* oops! we don't implement SMTP */ 703df106f0bSeric syserr("554 SMTP style mailer not implemented"); 7042c7e1b8dSeric return (EX_SOFTWARE); 7056c2c3107Seric # endif /* SMTP */ 706c579ef51Seric } 7075dfc646bSeric 7085dfc646bSeric /* 7095dfc646bSeric ** At this point *mvp points to the argument with $u. We 7105dfc646bSeric ** run through our address list and append all the addresses 7115dfc646bSeric ** we can. If we run out of space, do not fret! We can 7125dfc646bSeric ** always send another copy later. 7135dfc646bSeric */ 7145dfc646bSeric 7155dfc646bSeric tobuf[0] = '\0'; 716588cad61Seric e->e_to = tobuf; 7176259796dSeric ctladdr = NULL; 718e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7195dfc646bSeric for (; to != NULL; to = to->q_next) 7205dfc646bSeric { 7215dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 72257fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7235dfc646bSeric break; 7245dfc646bSeric 7255dfc646bSeric /* if already sent or not for this host, don't send */ 726ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 727e103b48fSeric to->q_mailer != firstto->q_mailer || 728e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7295dfc646bSeric continue; 7306259796dSeric 7314b22ea87Seric /* avoid overflowing tobuf */ 732aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7334b22ea87Seric break; 7344b22ea87Seric 7356ef48975Seric if (tTd(10, 1)) 736772e6e50Seric { 737772e6e50Seric printf("\nsend to "); 738772e6e50Seric printaddr(to, FALSE); 739772e6e50Seric } 740772e6e50Seric 7416259796dSeric /* compute effective uid/gid when sending */ 7427da1035fSeric if (to->q_mailer == ProgMailer) 7436259796dSeric ctladdr = getctladdr(to); 7446259796dSeric 7455dfc646bSeric user = to->q_user; 746588cad61Seric e->e_to = to->q_paddr; 74775f1ade9Seric if (tTd(10, 5)) 74875f1ade9Seric { 74975f1ade9Seric printf("deliver: QDONTSEND "); 75075f1ade9Seric printaddr(to, FALSE); 75175f1ade9Seric } 752ee4b0922Seric to->q_flags |= QDONTSEND; 7535dfc646bSeric 7545dfc646bSeric /* 7555dfc646bSeric ** Check to see that these people are allowed to 7565dfc646bSeric ** talk to each other. 7572a6e0786Seric */ 7582a6e0786Seric 75969582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 76069582d2fSeric { 76169582d2fSeric NoReturn = TRUE; 76208b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 7634dee0003Seric giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e); 76469582d2fSeric continue; 76569582d2fSeric } 766fabb3bd4Seric rcode = checkcompat(to, e); 7671793c9c5Seric if (rcode != EX_OK) 7685dfc646bSeric { 7691000c37aSeric markfailure(e, to, rcode); 7704dee0003Seric giveresponse(rcode, m, NULL, ctladdr, e); 7715dfc646bSeric continue; 7725dfc646bSeric } 7732a6e0786Seric 7742a6e0786Seric /* 7759ec9501bSeric ** Strip quote bits from names if the mailer is dumb 7769ec9501bSeric ** about them. 77725a99e2eSeric */ 77825a99e2eSeric 77957fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 78025a99e2eSeric { 7811d8f1806Seric stripquotes(user); 7821d8f1806Seric stripquotes(host); 78325a99e2eSeric } 78425a99e2eSeric 785cdb828c5Seric /* hack attack -- delivermail compatibility */ 786cdb828c5Seric if (m == ProgMailer && *user == '|') 787cdb828c5Seric user++; 788cdb828c5Seric 78925a99e2eSeric /* 7903efaed6eSeric ** If an error message has already been given, don't 7913efaed6eSeric ** bother to send to this address. 7923efaed6eSeric ** 7933efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 7943efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 7953efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 7963efaed6eSeric */ 7973efaed6eSeric 7986cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 7993efaed6eSeric continue; 8003efaed6eSeric 801f2fec898Seric /* save statistics.... */ 802588cad61Seric markstats(e, to); 803f2fec898Seric 8043efaed6eSeric /* 80525a99e2eSeric ** See if this user name is "special". 80625a99e2eSeric ** If the user name has a slash in it, assume that this 80751552439Seric ** is a file -- send it off without further ado. Note 80851552439Seric ** that this type of addresses is not processed along 80951552439Seric ** with the others, so we fudge on the To person. 81025a99e2eSeric */ 81125a99e2eSeric 8122c017f8dSeric if (m == FileMailer) 81325a99e2eSeric { 8144dee0003Seric ADDRESS *caddr = getctladdr(to); 8154dee0003Seric 8164dee0003Seric rcode = mailfile(user, caddr, e); 8174dee0003Seric giveresponse(rcode, m, NULL, caddr, e); 818dde5acadSeric if (rcode == EX_OK) 819dde5acadSeric to->q_flags |= QSENT; 8205dfc646bSeric continue; 82125a99e2eSeric } 82225a99e2eSeric 82313bbc08cSeric /* 82413bbc08cSeric ** Address is verified -- add this user to mailer 82513bbc08cSeric ** argv, and add it to the print list of recipients. 82613bbc08cSeric */ 82713bbc08cSeric 828508daeccSeric /* link together the chain of recipients */ 829508daeccSeric to->q_tchain = tochain; 830508daeccSeric tochain = to; 831508daeccSeric 8325dfc646bSeric /* create list of users for error messages */ 833db8841e9Seric (void) strcat(tobuf, ","); 834db8841e9Seric (void) strcat(tobuf, to->q_paddr); 835588cad61Seric define('u', user, e); /* to user */ 836b6f89e6eSeric p = to->q_home; 837b6f89e6eSeric if (p == NULL && ctladdr != NULL) 838b6f89e6eSeric p = ctladdr->q_home; 839b6f89e6eSeric define('z', p, e); /* user's home */ 8405dfc646bSeric 841c579ef51Seric /* 842508daeccSeric ** Expand out this user into argument list. 843c579ef51Seric */ 844c579ef51Seric 845508daeccSeric if (!clever) 846c579ef51Seric { 847588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8485dfc646bSeric *pvp++ = newstr(buf); 8495dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 8505dfc646bSeric { 8515dfc646bSeric /* allow some space for trailing parms */ 8525dfc646bSeric break; 8535dfc646bSeric } 8545dfc646bSeric } 855c579ef51Seric } 8565dfc646bSeric 857145b49b1Seric /* see if any addresses still exist */ 858145b49b1Seric if (tobuf[0] == '\0') 859c579ef51Seric { 860588cad61Seric define('g', (char *) NULL, e); 861145b49b1Seric return (0); 862c579ef51Seric } 863145b49b1Seric 8645dfc646bSeric /* print out messages as full list */ 86563780dbdSeric e->e_to = tobuf + 1; 8665dfc646bSeric 8675dfc646bSeric /* 8685dfc646bSeric ** Fill out any parameters after the $u parameter. 8695dfc646bSeric */ 8705dfc646bSeric 871c579ef51Seric while (!clever && *++mvp != NULL) 8725dfc646bSeric { 873588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8745dfc646bSeric *pvp++ = newstr(buf); 8755dfc646bSeric if (pvp >= &pv[MAXPV]) 87608b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 8775dfc646bSeric } 8785dfc646bSeric *pvp++ = NULL; 8795dfc646bSeric 88025a99e2eSeric /* 88125a99e2eSeric ** Call the mailer. 8826328bdf7Seric ** The argument vector gets built, pipes 88325a99e2eSeric ** are created as necessary, and we fork & exec as 8846328bdf7Seric ** appropriate. 885c579ef51Seric ** If we are running SMTP, we just need to clean up. 88625a99e2eSeric */ 88725a99e2eSeric 8887ee87e5fSeric if (ctladdr == NULL && m != ProgMailer) 88986b26461Seric ctladdr = &e->e_from; 890134746fbSeric #ifdef NAMED_BIND 8912bcc6d2dSeric if (ConfigLevel < 2) 892912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 893134746fbSeric #endif 8949c9e68d9Seric 8959c9e68d9Seric if (tTd(11, 1)) 896134746fbSeric { 8979c9e68d9Seric printf("openmailer:"); 8989c9e68d9Seric printav(pv); 8999c9e68d9Seric } 9009c9e68d9Seric errno = 0; 9019c9e68d9Seric 9029c9e68d9Seric CurHostName = m->m_mailer; 9039c9e68d9Seric 9049c9e68d9Seric /* 9059c9e68d9Seric ** Deal with the special case of mail handled through an IPC 9069c9e68d9Seric ** connection. 9079c9e68d9Seric ** In this case we don't actually fork. We must be 9089c9e68d9Seric ** running SMTP for this to work. We will return a 9099c9e68d9Seric ** zero pid to indicate that we are running IPC. 9109c9e68d9Seric ** We also handle a debug version that just talks to stdin/out. 9119c9e68d9Seric */ 9129c9e68d9Seric 9139c9e68d9Seric curhost = NULL; 914c931b82bSeric SmtpPhase = NULL; 915df106f0bSeric mci = NULL; 9169c9e68d9Seric 9177febfd66Seric #ifdef XDEBUG 9187febfd66Seric { 9197febfd66Seric char wbuf[MAXLINE]; 9207febfd66Seric 9217febfd66Seric /* make absolutely certain 0, 1, and 2 are in use */ 9227febfd66Seric sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 9237febfd66Seric checkfd012(wbuf); 9247febfd66Seric } 9257febfd66Seric #endif 9267febfd66Seric 9279c9e68d9Seric /* check for Local Person Communication -- not for mortals!!! */ 9289c9e68d9Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 9299c9e68d9Seric { 9309c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 9319c9e68d9Seric bzero((char *) mci, sizeof *mci); 9329c9e68d9Seric mci->mci_in = stdin; 9339c9e68d9Seric mci->mci_out = stdout; 9349c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9359c9e68d9Seric mci->mci_mailer = m; 9369c9e68d9Seric } 9379c9e68d9Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 9389c9e68d9Seric strcmp(m->m_mailer, "[TCP]") == 0) 9399c9e68d9Seric { 9409c9e68d9Seric #ifdef DAEMON 9419c9e68d9Seric register int i; 9429c9e68d9Seric register u_short port; 9439c9e68d9Seric 944a5d44642Seric if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0') 945a5d44642Seric { 946a5d44642Seric syserr("null host name for %s mailer", m->m_mailer); 947a5d44642Seric rcode = EX_CONFIG; 948a5d44642Seric goto give_up; 949a5d44642Seric } 950a5d44642Seric 9519c9e68d9Seric CurHostName = pv[1]; 9529c9e68d9Seric curhost = hostsignature(m, pv[1], e); 9539c9e68d9Seric 9549c9e68d9Seric if (curhost == NULL || curhost[0] == '\0') 9559c9e68d9Seric { 956a5d44642Seric syserr("null host signature for %s", pv[1]); 957845e533cSeric rcode = EX_OSERR; 958b31e7f2bSeric goto give_up; 959b31e7f2bSeric } 9609c9e68d9Seric 9619c9e68d9Seric if (!clever) 9629c9e68d9Seric { 9639c9e68d9Seric syserr("554 non-clever IPC"); 964df106f0bSeric rcode = EX_CONFIG; 9659c9e68d9Seric goto give_up; 9669c9e68d9Seric } 9679c9e68d9Seric if (pv[2] != NULL) 9689c9e68d9Seric port = atoi(pv[2]); 9699c9e68d9Seric else 9709c9e68d9Seric port = 0; 9719c9e68d9Seric tryhost: 9729c9e68d9Seric while (*curhost != '\0') 9739c9e68d9Seric { 9749c9e68d9Seric register char *p; 9759c9e68d9Seric static char hostbuf[MAXNAME]; 9769c9e68d9Seric 9779c9e68d9Seric /* pull the next host from the signature */ 9789c9e68d9Seric p = strchr(curhost, ':'); 9799c9e68d9Seric if (p == NULL) 9809c9e68d9Seric p = &curhost[strlen(curhost)]; 981df106f0bSeric if (p == curhost) 982df106f0bSeric { 983df106f0bSeric syserr("deliver: null host name in signature"); 9848087428aSeric curhost++; 985df106f0bSeric continue; 986df106f0bSeric } 9879c9e68d9Seric strncpy(hostbuf, curhost, p - curhost); 9889c9e68d9Seric hostbuf[p - curhost] = '\0'; 9899c9e68d9Seric if (*p != '\0') 9909c9e68d9Seric p++; 9919c9e68d9Seric curhost = p; 9929c9e68d9Seric 9939c9e68d9Seric /* see if we already know that this host is fried */ 9949c9e68d9Seric CurHostName = hostbuf; 9959c9e68d9Seric mci = mci_get(hostbuf, m); 9969c9e68d9Seric if (mci->mci_state != MCIS_CLOSED) 9979c9e68d9Seric { 9989c9e68d9Seric if (tTd(11, 1)) 9999c9e68d9Seric { 10009c9e68d9Seric printf("openmailer: "); 10014052916eSeric mci_dump(mci, FALSE); 10029c9e68d9Seric } 10039c9e68d9Seric CurHostName = mci->mci_host; 10049c9e68d9Seric break; 10059c9e68d9Seric } 10069c9e68d9Seric mci->mci_mailer = m; 10079c9e68d9Seric if (mci->mci_exitstat != EX_OK) 10089c9e68d9Seric continue; 10099c9e68d9Seric 10109c9e68d9Seric /* try the connection */ 10119c9e68d9Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 10129c9e68d9Seric message("Connecting to %s (%s)...", 10139c9e68d9Seric hostbuf, m->m_name); 10149c9e68d9Seric i = makeconnection(hostbuf, port, mci, 10159c9e68d9Seric bitnset(M_SECURE_PORT, m->m_flags)); 10169c9e68d9Seric mci->mci_exitstat = i; 10179c9e68d9Seric mci->mci_errno = errno; 1018f170942cSeric #ifdef NAMED_BIND 1019f170942cSeric mci->mci_herrno = h_errno; 1020f170942cSeric #endif 10219c9e68d9Seric if (i == EX_OK) 10229c9e68d9Seric { 10239c9e68d9Seric mci->mci_state = MCIS_OPENING; 10249c9e68d9Seric mci_cache(mci); 1025f170942cSeric if (TrafficLogFile != NULL) 1026f170942cSeric fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 1027f170942cSeric getpid(), hostbuf); 10289c9e68d9Seric break; 10299c9e68d9Seric } 10309c9e68d9Seric else if (tTd(11, 1)) 10319c9e68d9Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 10329c9e68d9Seric i, errno); 10339c9e68d9Seric 10349c9e68d9Seric /* enter status of this host */ 10359c9e68d9Seric setstat(i); 1036df106f0bSeric 1037df106f0bSeric /* should print some message here for -v mode */ 1038df106f0bSeric } 1039df106f0bSeric if (mci == NULL) 1040df106f0bSeric { 1041df106f0bSeric syserr("deliver: no host name"); 1042df106f0bSeric rcode = EX_OSERR; 1043df106f0bSeric goto give_up; 10449c9e68d9Seric } 10459c9e68d9Seric mci->mci_pid = 0; 10469c9e68d9Seric #else /* no DAEMON */ 10479c9e68d9Seric syserr("554 openmailer: no IPC"); 10489c9e68d9Seric if (tTd(11, 1)) 10499c9e68d9Seric printf("openmailer: NULL\n"); 1050df106f0bSeric rcode = EX_UNAVAILABLE; 1051df106f0bSeric goto give_up; 10529c9e68d9Seric #endif /* DAEMON */ 10539c9e68d9Seric } 10549c9e68d9Seric else 10559c9e68d9Seric { 1056f170942cSeric if (TrafficLogFile != NULL) 10570e3bfef5Seric { 1058f170942cSeric char **av; 1059f170942cSeric 1060f170942cSeric fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1061f170942cSeric for (av = pv; *av != NULL; av++) 1062f170942cSeric fprintf(TrafficLogFile, " %s", *av); 1063f170942cSeric fprintf(TrafficLogFile, "\n"); 10646fe8c3bcSeric } 10656fe8c3bcSeric 10669c9e68d9Seric /* create a pipe to shove the mail through */ 10679c9e68d9Seric if (pipe(mpvect) < 0) 10689c9e68d9Seric { 10690e3bfef5Seric syserr("%s... openmailer(%s): pipe (to mailer)", 10700e3bfef5Seric e->e_to, m->m_name); 10719c9e68d9Seric if (tTd(11, 1)) 10729c9e68d9Seric printf("openmailer: NULL\n"); 10739c9e68d9Seric rcode = EX_OSERR; 10749c9e68d9Seric goto give_up; 10759c9e68d9Seric } 10769c9e68d9Seric 10779c9e68d9Seric /* if this mailer speaks smtp, create a return pipe */ 10789c9e68d9Seric if (clever && pipe(rpvect) < 0) 10799c9e68d9Seric { 10800e3bfef5Seric syserr("%s... openmailer(%s): pipe (from mailer)", 10810e3bfef5Seric e->e_to, m->m_name); 10829c9e68d9Seric (void) close(mpvect[0]); 10839c9e68d9Seric (void) close(mpvect[1]); 10849c9e68d9Seric if (tTd(11, 1)) 10859c9e68d9Seric printf("openmailer: NULL\n"); 10869c9e68d9Seric rcode = EX_OSERR; 10879c9e68d9Seric goto give_up; 10889c9e68d9Seric } 10899c9e68d9Seric 10909c9e68d9Seric /* 10919c9e68d9Seric ** Actually fork the mailer process. 10929c9e68d9Seric ** DOFORK is clever about retrying. 10939c9e68d9Seric ** 10949c9e68d9Seric ** Dispose of SIGCHLD signal catchers that may be laying 10959c9e68d9Seric ** around so that endmail will get it. 10969c9e68d9Seric */ 10979c9e68d9Seric 10989c9e68d9Seric if (e->e_xfp != NULL) 10999c9e68d9Seric (void) fflush(e->e_xfp); /* for debugging */ 11009c9e68d9Seric (void) fflush(stdout); 11019c9e68d9Seric # ifdef SIGCHLD 11022b9178d3Seric (void) setsignal(SIGCHLD, SIG_DFL); 11039c9e68d9Seric # endif /* SIGCHLD */ 11049c9e68d9Seric DOFORK(FORK); 11059c9e68d9Seric /* pid is set by DOFORK */ 11069c9e68d9Seric if (pid < 0) 11079c9e68d9Seric { 11089c9e68d9Seric /* failure */ 11090e3bfef5Seric syserr("%s... openmailer(%s): cannot fork", 11100e3bfef5Seric e->e_to, m->m_name); 11119c9e68d9Seric (void) close(mpvect[0]); 11129c9e68d9Seric (void) close(mpvect[1]); 11139c9e68d9Seric if (clever) 11149c9e68d9Seric { 11159c9e68d9Seric (void) close(rpvect[0]); 11169c9e68d9Seric (void) close(rpvect[1]); 11179c9e68d9Seric } 11189c9e68d9Seric if (tTd(11, 1)) 11199c9e68d9Seric printf("openmailer: NULL\n"); 11209c9e68d9Seric rcode = EX_OSERR; 11219c9e68d9Seric goto give_up; 11229c9e68d9Seric } 11239c9e68d9Seric else if (pid == 0) 11249c9e68d9Seric { 11259c9e68d9Seric int i; 11269c9e68d9Seric int saveerrno; 11279c9e68d9Seric char **ep; 11289c9e68d9Seric char *env[MAXUSERENVIRON]; 11299c9e68d9Seric extern char **environ; 11309c9e68d9Seric extern int DtableSize; 11319c9e68d9Seric 11329c9e68d9Seric /* child -- set up input & exec mailer */ 11332b9178d3Seric (void) setsignal(SIGINT, SIG_IGN); 11342b9178d3Seric (void) setsignal(SIGHUP, SIG_IGN); 11352b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 11369c9e68d9Seric 113744f2317fSeric /* reset user and group */ 113844f2317fSeric if (!bitnset(M_RESTR, m->m_flags)) 113944f2317fSeric { 114044f2317fSeric if (ctladdr == NULL || ctladdr->q_uid == 0) 114144f2317fSeric { 114244f2317fSeric (void) initgroups(DefUser, DefGid); 1143fc354902Seric (void) setgid(DefGid); 114444f2317fSeric (void) setuid(DefUid); 114544f2317fSeric } 114644f2317fSeric else 114744f2317fSeric { 114844f2317fSeric (void) initgroups(ctladdr->q_ruser? 114944f2317fSeric ctladdr->q_ruser: ctladdr->q_user, 115044f2317fSeric ctladdr->q_gid); 1151*51cfe111Seric (void) setgid(ctladdr->q_gid); 115244f2317fSeric (void) setuid(ctladdr->q_uid); 115344f2317fSeric } 115444f2317fSeric } 115544f2317fSeric 115644f2317fSeric if (tTd(11, 2)) 115744f2317fSeric printf("openmailer: running as r/euid=%d/%d\n", 115844f2317fSeric getuid(), geteuid()); 115944f2317fSeric 1160b986f6aaSeric /* move into some "safe" directory */ 1161b986f6aaSeric if (m->m_execdir != NULL) 1162b986f6aaSeric { 1163b986f6aaSeric char *p, *q; 1164b986f6aaSeric char buf[MAXLINE]; 1165b986f6aaSeric 1166b986f6aaSeric for (p = m->m_execdir; p != NULL; p = q) 1167b986f6aaSeric { 1168b986f6aaSeric q = strchr(p, ':'); 1169b986f6aaSeric if (q != NULL) 1170b986f6aaSeric *q = '\0'; 1171b986f6aaSeric expand(p, buf, &buf[sizeof buf] - 1, e); 1172b986f6aaSeric if (q != NULL) 1173b986f6aaSeric *q++ = ':'; 1174b986f6aaSeric if (tTd(11, 20)) 1175b986f6aaSeric printf("openmailer: trydir %s\n", 1176b986f6aaSeric buf); 1177b986f6aaSeric if (buf[0] != '\0' && chdir(buf) >= 0) 1178b986f6aaSeric break; 1179b986f6aaSeric } 1180b986f6aaSeric } 1181b986f6aaSeric 11829c9e68d9Seric /* arrange to filter std & diag output of command */ 11839c9e68d9Seric if (clever) 11849c9e68d9Seric { 11859c9e68d9Seric (void) close(rpvect[0]); 11866fe8c3bcSeric if (dup2(rpvect[1], STDOUT_FILENO) < 0) 11876fe8c3bcSeric { 11880e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 11890e3bfef5Seric e->e_to, m->m_name, rpvect[1]); 11906fe8c3bcSeric _exit(EX_OSERR); 11916fe8c3bcSeric } 11929c9e68d9Seric (void) close(rpvect[1]); 11939c9e68d9Seric } 11949c9e68d9Seric else if (OpMode == MD_SMTP || HoldErrs) 11959c9e68d9Seric { 11969c9e68d9Seric /* put mailer output in transcript */ 11976fe8c3bcSeric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 11986fe8c3bcSeric { 11990e3bfef5Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 12000e3bfef5Seric e->e_to, m->m_name, 12016fe8c3bcSeric fileno(e->e_xfp)); 12026fe8c3bcSeric _exit(EX_OSERR); 12039c9e68d9Seric } 12046fe8c3bcSeric } 12056fe8c3bcSeric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 12066fe8c3bcSeric { 12070e3bfef5Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr", 12080e3bfef5Seric e->e_to, m->m_name); 12096fe8c3bcSeric _exit(EX_OSERR); 12106fe8c3bcSeric } 12119c9e68d9Seric 12129c9e68d9Seric /* arrange to get standard input */ 12139c9e68d9Seric (void) close(mpvect[1]); 12149c9e68d9Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 12159c9e68d9Seric { 12160e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 12170e3bfef5Seric e->e_to, m->m_name, mpvect[0]); 12189c9e68d9Seric _exit(EX_OSERR); 12199c9e68d9Seric } 12209c9e68d9Seric (void) close(mpvect[0]); 12219c9e68d9Seric 12229c9e68d9Seric /* arrange for all the files to be closed */ 12239c9e68d9Seric for (i = 3; i < DtableSize; i++) 12249c9e68d9Seric { 12259c9e68d9Seric register int j; 122644f2317fSeric 12279c9e68d9Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 12289c9e68d9Seric (void) fcntl(i, F_SETFD, j | 1); 12299c9e68d9Seric } 12309c9e68d9Seric 12319c9e68d9Seric /* set up the mailer environment */ 12329c9e68d9Seric i = 0; 12339c9e68d9Seric env[i++] = "AGENT=sendmail"; 12349c9e68d9Seric for (ep = environ; *ep != NULL; ep++) 12359c9e68d9Seric { 12369c9e68d9Seric if (strncmp(*ep, "TZ=", 3) == 0) 12379c9e68d9Seric env[i++] = *ep; 12389c9e68d9Seric } 12399c9e68d9Seric env[i++] = NULL; 12409c9e68d9Seric 12419c9e68d9Seric /* try to execute the mailer */ 12429c9e68d9Seric execve(m->m_mailer, pv, env); 12439c9e68d9Seric saveerrno = errno; 12449c9e68d9Seric syserr("Cannot exec %s", m->m_mailer); 12457c941fd2Seric if (m == LocalMailer || transienterror(saveerrno)) 12467c941fd2Seric _exit(EX_OSERR); 12479c9e68d9Seric _exit(EX_UNAVAILABLE); 12489c9e68d9Seric } 12499c9e68d9Seric 12509c9e68d9Seric /* 12519c9e68d9Seric ** Set up return value. 12529c9e68d9Seric */ 12539c9e68d9Seric 12549c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 12559c9e68d9Seric bzero((char *) mci, sizeof *mci); 12569c9e68d9Seric mci->mci_mailer = m; 12579c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 12589c9e68d9Seric mci->mci_pid = pid; 12599c9e68d9Seric (void) close(mpvect[0]); 12609c9e68d9Seric mci->mci_out = fdopen(mpvect[1], "w"); 1261335eae58Seric if (mci->mci_out == NULL) 1262335eae58Seric { 1263335eae58Seric syserr("deliver: cannot create mailer output channel, fd=%d", 1264335eae58Seric mpvect[1]); 1265335eae58Seric (void) close(mpvect[1]); 1266335eae58Seric if (clever) 1267335eae58Seric { 1268335eae58Seric (void) close(rpvect[0]); 1269335eae58Seric (void) close(rpvect[1]); 1270335eae58Seric } 1271335eae58Seric rcode = EX_OSERR; 1272335eae58Seric goto give_up; 1273335eae58Seric } 12749c9e68d9Seric if (clever) 12759c9e68d9Seric { 12769c9e68d9Seric (void) close(rpvect[1]); 12779c9e68d9Seric mci->mci_in = fdopen(rpvect[0], "r"); 1278335eae58Seric if (mci->mci_in == NULL) 1279335eae58Seric { 1280335eae58Seric syserr("deliver: cannot create mailer input channel, fd=%d", 1281335eae58Seric mpvect[1]); 1282335eae58Seric (void) close(rpvect[0]); 1283335eae58Seric fclose(mci->mci_out); 1284335eae58Seric mci->mci_out = NULL; 1285335eae58Seric rcode = EX_OSERR; 1286335eae58Seric goto give_up; 1287335eae58Seric } 12889c9e68d9Seric } 12899c9e68d9Seric else 12909c9e68d9Seric { 12919c9e68d9Seric mci->mci_flags |= MCIF_TEMP; 12929c9e68d9Seric mci->mci_in = NULL; 12939c9e68d9Seric } 12949c9e68d9Seric } 12959c9e68d9Seric 12969c9e68d9Seric /* 12979c9e68d9Seric ** If we are in SMTP opening state, send initial protocol. 12989c9e68d9Seric */ 12999c9e68d9Seric 13009c9e68d9Seric if (clever && mci->mci_state != MCIS_CLOSED) 13019c9e68d9Seric { 13029c9e68d9Seric smtpinit(m, mci, e); 13039c9e68d9Seric } 13049c9e68d9Seric if (tTd(11, 1)) 13059c9e68d9Seric { 13069c9e68d9Seric printf("openmailer: "); 13074052916eSeric mci_dump(mci, FALSE); 13089c9e68d9Seric } 13099c9e68d9Seric 13109c9e68d9Seric if (mci->mci_state != MCIS_OPEN) 1311b31e7f2bSeric { 1312b31e7f2bSeric /* couldn't open the mailer */ 1313b31e7f2bSeric rcode = mci->mci_exitstat; 13142a6bc25bSeric errno = mci->mci_errno; 1315f170942cSeric #ifdef NAMED_BIND 1316f170942cSeric h_errno = mci->mci_herrno; 1317f170942cSeric #endif 1318b31e7f2bSeric if (rcode == EX_OK) 1319b31e7f2bSeric { 1320b31e7f2bSeric /* shouldn't happen */ 132108b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 13226b0f339dSeric rcode, mci->mci_state, firstsig); 1323b31e7f2bSeric rcode = EX_SOFTWARE; 1324b31e7f2bSeric } 1325cb082c5bSeric else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0') 132690891494Seric { 132790891494Seric /* try next MX site */ 132890891494Seric goto tryhost; 132990891494Seric } 1330b31e7f2bSeric } 1331b31e7f2bSeric else if (!clever) 1332b31e7f2bSeric { 1333b31e7f2bSeric /* 1334b31e7f2bSeric ** Format and send message. 1335b31e7f2bSeric */ 133615d084d5Seric 1337b31e7f2bSeric putfromline(mci->mci_out, m, e); 1338b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 1339b31e7f2bSeric putline("\n", mci->mci_out, m); 134003c02fdeSeric (*e->e_putbody)(mci->mci_out, m, e, NULL); 1341b31e7f2bSeric 1342b31e7f2bSeric /* get the exit status */ 1343c9be6216Seric rcode = endmailer(mci, e, pv); 1344134746fbSeric } 1345134746fbSeric else 1346b31e7f2bSeric #ifdef SMTP 1347134746fbSeric { 1348b31e7f2bSeric /* 1349b31e7f2bSeric ** Send the MAIL FROM: protocol 1350b31e7f2bSeric */ 135115d084d5Seric 1352b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 1353b31e7f2bSeric if (rcode == EX_OK) 135475889e88Seric { 1355ded0d3daSkarels register char *t = tobuf; 1356ded0d3daSkarels register int i; 1357ded0d3daSkarels 1358588cad61Seric /* send the recipient list */ 135963780dbdSeric tobuf[0] = '\0'; 136075889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 136175889e88Seric { 136263780dbdSeric e->e_to = to->q_paddr; 136315d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 136475889e88Seric { 136583b7ddc9Seric markfailure(e, to, i); 13664dee0003Seric giveresponse(i, m, mci, ctladdr, e); 136763780dbdSeric } 136875889e88Seric else 136975889e88Seric { 1370911693bfSbostic *t++ = ','; 1371b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 1372b31e7f2bSeric continue; 1373ee39df61Seric *t = '\0'; 1374588cad61Seric } 1375588cad61Seric } 1376588cad61Seric 137763780dbdSeric /* now send the data */ 137863780dbdSeric if (tobuf[0] == '\0') 1379b31e7f2bSeric { 13809c9e68d9Seric rcode = EX_OK; 138163780dbdSeric e->e_to = NULL; 1382b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 1383b31e7f2bSeric smtprset(m, mci, e); 1384b31e7f2bSeric } 138575889e88Seric else 138675889e88Seric { 138763780dbdSeric e->e_to = tobuf + 1; 138875889e88Seric rcode = smtpdata(m, mci, e); 138963780dbdSeric } 139063780dbdSeric 139163780dbdSeric /* now close the connection */ 1392b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 139315d084d5Seric smtpquit(m, mci, e); 139463780dbdSeric } 1395cb082c5bSeric if (rcode != EX_OK && curhost != NULL && *curhost != '\0') 13969c9e68d9Seric { 13979c9e68d9Seric /* try next MX site */ 13989c9e68d9Seric goto tryhost; 13999c9e68d9Seric } 1400c579ef51Seric } 1401b31e7f2bSeric #else /* not SMTP */ 1402a05b3449Sbostic { 140308b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 1404845e533cSeric rcode = EX_CONFIG; 1405b31e7f2bSeric goto give_up; 1406a05b3449Sbostic } 1407b31e7f2bSeric #endif /* SMTP */ 1408134746fbSeric #ifdef NAMED_BIND 14092bcc6d2dSeric if (ConfigLevel < 2) 1410912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1411134746fbSeric #endif 14125dfc646bSeric 1413b31e7f2bSeric /* arrange a return receipt if requested */ 1414df106f0bSeric if (rcode == EX_OK && e->e_receiptto != NULL && 1415df106f0bSeric bitnset(M_LOCALMAILER, m->m_flags)) 1416b31e7f2bSeric { 1417b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 1418b31e7f2bSeric /* do we want to send back more info? */ 1419b31e7f2bSeric } 1420b31e7f2bSeric 1421c77d1c25Seric /* 142263780dbdSeric ** Do final status disposal. 142363780dbdSeric ** We check for something in tobuf for the SMTP case. 1424c77d1c25Seric ** If we got a temporary failure, arrange to queue the 1425c77d1c25Seric ** addressees. 1426c77d1c25Seric */ 1427c77d1c25Seric 1428b31e7f2bSeric give_up: 142963780dbdSeric if (tobuf[0] != '\0') 14304dee0003Seric giveresponse(rcode, m, mci, ctladdr, e); 1431772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 1432b31e7f2bSeric { 1433dde5acadSeric if (rcode != EX_OK) 143483b7ddc9Seric markfailure(e, to, rcode); 1435dde5acadSeric else 1436655518ecSeric { 1437dde5acadSeric to->q_flags |= QSENT; 1438655518ecSeric e->e_nsent++; 1439df106f0bSeric if (e->e_receiptto != NULL && 1440df106f0bSeric bitnset(M_LOCALMAILER, m->m_flags)) 1441df106f0bSeric { 1442df106f0bSeric fprintf(e->e_xfp, "%s... Successfully delivered\n", 1443df106f0bSeric to->q_paddr); 1444df106f0bSeric } 1445655518ecSeric } 1446b31e7f2bSeric } 1447b31e7f2bSeric 1448b31e7f2bSeric /* 1449b31e7f2bSeric ** Restore state and return. 1450b31e7f2bSeric */ 1451c77d1c25Seric 14527febfd66Seric #ifdef XDEBUG 14537febfd66Seric { 14547febfd66Seric char wbuf[MAXLINE]; 14557febfd66Seric 14567febfd66Seric /* make absolutely certain 0, 1, and 2 are in use */ 145744622ea3Seric sprintf(wbuf, "%s... end of deliver(%s)", 145844622ea3Seric e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 145944622ea3Seric m->m_name); 14607febfd66Seric checkfd012(wbuf); 14617febfd66Seric } 14627febfd66Seric #endif 14637febfd66Seric 146435490626Seric errno = 0; 1465588cad61Seric define('g', (char *) NULL, e); 14665826d9d3Seric return (rcode); 146725a99e2eSeric } 14685dfc646bSeric /* 146983b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 147083b7ddc9Seric ** 147183b7ddc9Seric ** Parameters: 147283b7ddc9Seric ** e -- the envelope we are sending. 147383b7ddc9Seric ** q -- the address to mark. 147483b7ddc9Seric ** rcode -- the code signifying the particular failure. 147583b7ddc9Seric ** 147683b7ddc9Seric ** Returns: 147783b7ddc9Seric ** none. 147883b7ddc9Seric ** 147983b7ddc9Seric ** Side Effects: 148083b7ddc9Seric ** marks the address (and possibly the envelope) with the 148183b7ddc9Seric ** failure so that an error will be returned or 148283b7ddc9Seric ** the message will be queued, as appropriate. 148383b7ddc9Seric */ 148483b7ddc9Seric 148583b7ddc9Seric markfailure(e, q, rcode) 148683b7ddc9Seric register ENVELOPE *e; 148783b7ddc9Seric register ADDRESS *q; 148883b7ddc9Seric int rcode; 148983b7ddc9Seric { 149019c47125Seric char buf[MAXLINE]; 149119c47125Seric 149283b7ddc9Seric if (rcode == EX_OK) 149383b7ddc9Seric return; 1494f170942cSeric else if (rcode == EX_TEMPFAIL) 149583b7ddc9Seric q->q_flags |= QQUEUEUP; 1496f170942cSeric else if (rcode != EX_IOERR && rcode != EX_OSERR) 1497f170942cSeric q->q_flags |= QBADADDR; 149883b7ddc9Seric } 149983b7ddc9Seric /* 1500c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 1501c579ef51Seric ** 1502c579ef51Seric ** We should never get fatal errors (e.g., segmentation 1503c579ef51Seric ** violation), so we report those specially. For other 1504c579ef51Seric ** errors, we choose a status message (into statmsg), 1505c579ef51Seric ** and if it represents an error, we print it. 1506c579ef51Seric ** 1507c579ef51Seric ** Parameters: 1508c579ef51Seric ** pid -- pid of mailer. 1509c9be6216Seric ** e -- the current envelope. 1510c9be6216Seric ** pv -- the parameter vector that invoked the mailer 1511c9be6216Seric ** (for error messages). 1512c579ef51Seric ** 1513c579ef51Seric ** Returns: 1514c579ef51Seric ** exit code of mailer. 1515c579ef51Seric ** 1516c579ef51Seric ** Side Effects: 1517c579ef51Seric ** none. 1518c579ef51Seric */ 1519c579ef51Seric 1520c9be6216Seric endmailer(mci, e, pv) 1521b31e7f2bSeric register MCI *mci; 1522c9be6216Seric register ENVELOPE *e; 1523c9be6216Seric char **pv; 1524c579ef51Seric { 1525588cad61Seric int st; 1526c579ef51Seric 152775889e88Seric /* close any connections */ 152875889e88Seric if (mci->mci_in != NULL) 1529c9be6216Seric (void) xfclose(mci->mci_in, pv[0], "mci_in"); 153075889e88Seric if (mci->mci_out != NULL) 1531c9be6216Seric (void) xfclose(mci->mci_out, pv[0], "mci_out"); 153275889e88Seric mci->mci_in = mci->mci_out = NULL; 153375889e88Seric mci->mci_state = MCIS_CLOSED; 153475889e88Seric 153533db8731Seric /* in the IPC case there is nothing to wait for */ 153675889e88Seric if (mci->mci_pid == 0) 153733db8731Seric return (EX_OK); 153833db8731Seric 153933db8731Seric /* wait for the mailer process to die and collect status */ 154075889e88Seric st = waitfor(mci->mci_pid); 1541588cad61Seric if (st == -1) 154278de67c1Seric { 1543c9be6216Seric syserr("endmailer %s: wait", pv[0]); 1544588cad61Seric return (EX_SOFTWARE); 1545c579ef51Seric } 154633db8731Seric 1547bf9bc890Seric if (WIFEXITED(st)) 1548c579ef51Seric { 1549bf9bc890Seric /* normal death -- return status */ 1550bf9bc890Seric return (WEXITSTATUS(st)); 1551bf9bc890Seric } 1552bf9bc890Seric 1553bf9bc890Seric /* it died a horrid death */ 1554c9be6216Seric syserr("mailer %s died with signal %o", pv[0], st); 1555c9be6216Seric 1556c9be6216Seric /* log the arguments */ 1557c9be6216Seric if (e->e_xfp != NULL) 1558c9be6216Seric { 1559c9be6216Seric register char **av; 1560c9be6216Seric 1561c9be6216Seric fprintf(e->e_xfp, "Arguments:"); 1562c9be6216Seric for (av = pv; *av != NULL; av++) 1563c9be6216Seric fprintf(e->e_xfp, " %s", *av); 1564c9be6216Seric fprintf(e->e_xfp, "\n"); 1565c9be6216Seric } 1566c9be6216Seric 15675f73204aSeric ExitStat = EX_TEMPFAIL; 15685f73204aSeric return (EX_TEMPFAIL); 1569c579ef51Seric } 1570c579ef51Seric /* 157125a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 157225a99e2eSeric ** 157325a99e2eSeric ** Parameters: 157425a99e2eSeric ** stat -- the status code from the mailer (high byte 157525a99e2eSeric ** only; core dumps must have been taken care of 157625a99e2eSeric ** already). 157781161401Seric ** m -- the mailer info for this mailer. 157881161401Seric ** mci -- the mailer connection info -- can be NULL if the 157981161401Seric ** response is given before the connection is made. 15804dee0003Seric ** ctladdr -- the controlling address for the recipient 15814dee0003Seric ** address(es). 158281161401Seric ** e -- the current envelope. 158325a99e2eSeric ** 158425a99e2eSeric ** Returns: 1585db8841e9Seric ** none. 158625a99e2eSeric ** 158725a99e2eSeric ** Side Effects: 1588c1f9df2cSeric ** Errors may be incremented. 158925a99e2eSeric ** ExitStat may be set. 159025a99e2eSeric */ 159125a99e2eSeric 15924dee0003Seric giveresponse(stat, m, mci, ctladdr, e) 159325a99e2eSeric int stat; 1594588cad61Seric register MAILER *m; 159581161401Seric register MCI *mci; 15964dee0003Seric ADDRESS *ctladdr; 1597198d9be0Seric ENVELOPE *e; 159825a99e2eSeric { 15999c16475dSeric register const char *statmsg; 160025a99e2eSeric extern char *SysExMsg[]; 160125a99e2eSeric register int i; 1602d4bd8f0eSbostic extern int N_SysEx; 1603198d9be0Seric char buf[MAXLINE]; 160425a99e2eSeric 160513bbc08cSeric /* 160613bbc08cSeric ** Compute status message from code. 160713bbc08cSeric */ 160813bbc08cSeric 160925a99e2eSeric i = stat - EX__BASE; 1610588cad61Seric if (stat == 0) 16116fe8c3bcSeric { 1612588cad61Seric statmsg = "250 Sent"; 1613ce5531bdSeric if (e->e_statmsg != NULL) 16146fe8c3bcSeric { 1615ce5531bdSeric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 16166fe8c3bcSeric statmsg = buf; 16176fe8c3bcSeric } 16186fe8c3bcSeric } 1619588cad61Seric else if (i < 0 || i > N_SysEx) 1620588cad61Seric { 1621588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1622588cad61Seric stat = EX_UNAVAILABLE; 1623588cad61Seric statmsg = buf; 1624588cad61Seric } 1625198d9be0Seric else if (stat == EX_TEMPFAIL) 1626198d9be0Seric { 16277d55540cSeric (void) strcpy(buf, SysExMsg[i] + 1); 1628d4bd8f0eSbostic #ifdef NAMED_BIND 1629f28da541Smiriam if (h_errno == TRY_AGAIN) 16304d50702aSeric statmsg = errstring(h_errno+E_DNSBASE); 1631f28da541Smiriam else 1632d4bd8f0eSbostic #endif 1633f28da541Smiriam { 16348557d168Seric if (errno != 0) 1635d87e85f3Seric statmsg = errstring(errno); 1636d87e85f3Seric else 1637d87e85f3Seric { 1638d87e85f3Seric #ifdef SMTP 1639d87e85f3Seric extern char SmtpError[]; 1640d87e85f3Seric 1641d87e85f3Seric statmsg = SmtpError; 16426c2c3107Seric #else /* SMTP */ 1643d87e85f3Seric statmsg = NULL; 16446c2c3107Seric #endif /* SMTP */ 1645d87e85f3Seric } 1646f28da541Smiriam } 1647d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1648d87e85f3Seric { 164987c9b3e7Seric (void) strcat(buf, ": "); 1650d87e85f3Seric (void) strcat(buf, statmsg); 16518557d168Seric } 1652198d9be0Seric statmsg = buf; 1653198d9be0Seric } 1654f170942cSeric #ifdef NAMED_BIND 1655f170942cSeric else if (stat == EX_NOHOST && h_errno != 0) 1656f170942cSeric { 16574d50702aSeric statmsg = errstring(h_errno + E_DNSBASE); 1658f170942cSeric (void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg); 1659f170942cSeric statmsg = buf; 1660f170942cSeric } 1661f170942cSeric #endif 166225a99e2eSeric else 1663d87e85f3Seric { 166425a99e2eSeric statmsg = SysExMsg[i]; 16657d55540cSeric if (*statmsg++ == ':') 16667d55540cSeric { 16677d55540cSeric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 16687d55540cSeric statmsg = buf; 16697d55540cSeric } 1670d87e85f3Seric } 1671588cad61Seric 1672588cad61Seric /* 1673588cad61Seric ** Print the message as appropriate 1674588cad61Seric */ 1675588cad61Seric 1676198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 1677df106f0bSeric { 1678df106f0bSeric extern char MsgBuf[]; 1679df106f0bSeric 1680e12d03eeSeric message(&statmsg[4], errstring(errno)); 1681df106f0bSeric if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 1682df106f0bSeric fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 1683df106f0bSeric } 168425a99e2eSeric else 168525a99e2eSeric { 1686c1f9df2cSeric Errors++; 1687e12d03eeSeric usrerr(statmsg, errstring(errno)); 168825a99e2eSeric } 168925a99e2eSeric 169025a99e2eSeric /* 169125a99e2eSeric ** Final cleanup. 169225a99e2eSeric ** Log a record of the transaction. Compute the new 169325a99e2eSeric ** ExitStat -- if we already had an error, stick with 169425a99e2eSeric ** that. 169525a99e2eSeric */ 169625a99e2eSeric 16972f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 16984dee0003Seric logdelivery(m, mci, &statmsg[4], ctladdr, e); 1699eb238f8cSeric 1700eb238f8cSeric if (stat != EX_TEMPFAIL) 1701eb238f8cSeric setstat(stat); 17021097d4c9Seric if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL)) 1703198d9be0Seric { 1704198d9be0Seric if (e->e_message != NULL) 1705198d9be0Seric free(e->e_message); 1706198d9be0Seric e->e_message = newstr(&statmsg[4]); 1707198d9be0Seric } 17088557d168Seric errno = 0; 1709d4bd8f0eSbostic #ifdef NAMED_BIND 1710f28da541Smiriam h_errno = 0; 1711d4bd8f0eSbostic #endif 1712eb238f8cSeric } 1713eb238f8cSeric /* 1714eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1715eb238f8cSeric ** 17162c9b4f99Seric ** Care is taken to avoid logging lines that are too long, because 17172c9b4f99Seric ** some versions of syslog have an unfortunate proclivity for core 17182c9b4f99Seric ** dumping. This is a hack, to be sure, that is at best empirical. 17192c9b4f99Seric ** 1720eb238f8cSeric ** Parameters: 172181161401Seric ** m -- the mailer info. Can be NULL for initial queue. 172281161401Seric ** mci -- the mailer connection info -- can be NULL if the 172381161401Seric ** log is occuring when no connection is active. 172481161401Seric ** stat -- the message to print for the status. 17254dee0003Seric ** ctladdr -- the controlling address for the to list. 172681161401Seric ** e -- the current envelope. 1727eb238f8cSeric ** 1728eb238f8cSeric ** Returns: 1729eb238f8cSeric ** none 1730eb238f8cSeric ** 1731eb238f8cSeric ** Side Effects: 1732eb238f8cSeric ** none 1733eb238f8cSeric */ 1734eb238f8cSeric 17352c9b4f99Seric #ifndef SYSLOG_BUFSIZE 17362c9b4f99Seric # define SYSLOG_BUFSIZE 1024 17372c9b4f99Seric #endif 17382c9b4f99Seric 17394dee0003Seric logdelivery(m, mci, stat, ctladdr, e) 174081161401Seric MAILER *m; 174181161401Seric register MCI *mci; 1742eb238f8cSeric char *stat; 17434dee0003Seric ADDRESS *ctladdr; 1744b31e7f2bSeric register ENVELOPE *e; 17455cf56be3Seric { 1746eb238f8cSeric # ifdef LOG 17474dee0003Seric register char *bp; 17482c9b4f99Seric register char *p; 17492c9b4f99Seric int l; 1750d6acf3eeSeric char buf[512]; 17519507d1f9Seric 17524dee0003Seric bp = buf; 17534dee0003Seric if (ctladdr != NULL) 17544dee0003Seric { 17554dee0003Seric strcpy(bp, ", ctladdr="); 17562c9b4f99Seric l = strlen(ctladdr->q_paddr); 17572c9b4f99Seric if (l > 83) 17582c9b4f99Seric sprintf(bp, "%.40s...%s", 17592c9b4f99Seric ctladdr->q_paddr, ctladdr->q_paddr + l - 40); 17602c9b4f99Seric else 17614dee0003Seric strcat(bp, ctladdr->q_paddr); 17624dee0003Seric bp += strlen(bp); 17634dee0003Seric if (bitset(QGOODUID, ctladdr->q_flags)) 17644dee0003Seric { 17654dee0003Seric (void) sprintf(bp, " (%d/%d)", 17664dee0003Seric ctladdr->q_uid, ctladdr->q_gid); 17674dee0003Seric bp += strlen(bp); 17684dee0003Seric } 17694dee0003Seric } 17704dee0003Seric 17714dee0003Seric (void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 17724dee0003Seric bp += strlen(bp); 177381161401Seric 177448ed5d33Seric if (m != NULL) 177571ff6caaSeric { 17764dee0003Seric (void) strcpy(bp, ", mailer="); 17774dee0003Seric (void) strcat(bp, m->m_name); 17784dee0003Seric bp += strlen(bp); 177971ff6caaSeric } 178048ed5d33Seric 178148ed5d33Seric if (mci != NULL && mci->mci_host != NULL) 178271ff6caaSeric { 178371ff6caaSeric # ifdef DAEMON 1784e2f2f828Seric extern SOCKADDR CurHostAddr; 178548ed5d33Seric # endif 178671ff6caaSeric 17874dee0003Seric (void) strcpy(bp, ", relay="); 17884dee0003Seric (void) strcat(bp, mci->mci_host); 178948ed5d33Seric 179048ed5d33Seric # ifdef DAEMON 17914dee0003Seric (void) strcat(bp, " ("); 17924dee0003Seric (void) strcat(bp, anynet_ntoa(&CurHostAddr)); 17934dee0003Seric (void) strcat(bp, ")"); 179471ff6caaSeric # endif 179571ff6caaSeric } 17969507d1f9Seric else 179748ed5d33Seric { 179848ed5d33Seric char *p = macvalue('h', e); 17999507d1f9Seric 180048ed5d33Seric if (p != NULL && p[0] != '\0') 180148ed5d33Seric { 18024dee0003Seric (void) strcpy(bp, ", relay="); 18034dee0003Seric (void) strcat(bp, p); 180448ed5d33Seric } 180548ed5d33Seric } 1806cb082c5bSeric bp += strlen(bp); 1807d6acf3eeSeric 18082c9b4f99Seric if ((bp - buf) > (sizeof buf - 220)) 18092c9b4f99Seric { 18102c9b4f99Seric /* desperation move -- truncate data */ 18112c9b4f99Seric bp = buf + sizeof buf - 217; 18122c9b4f99Seric strcpy(bp, "..."); 18132c9b4f99Seric bp += 3; 18142c9b4f99Seric } 18152c9b4f99Seric 18162c9b4f99Seric (void) strcpy(bp, ", stat="); 18172c9b4f99Seric bp += strlen(bp); 18182c9b4f99Seric l = strlen(stat); 18192c9b4f99Seric #define STATLEN (((SYSLOG_BUFSIZE) - 100) / 8) 18202c9b4f99Seric #if (STATLEN) < 30 18212c9b4f99Seric # undef STATLEN 18222c9b4f99Seric # define STATLEN 30 18232c9b4f99Seric #endif 18242c9b4f99Seric #if (STATLEN) > 100 18252c9b4f99Seric # undef STATLEN 18262c9b4f99Seric # define STATLEN 100 18272c9b4f99Seric #endif 18282c9b4f99Seric if (l > (STATLEN * 2 + 3)) 18292c9b4f99Seric sprintf(bp, "%.*s...%s", STATLEN, stat, stat + l - STATLEN); 18302c9b4f99Seric else 18312c9b4f99Seric (void) strcpy(bp, stat); 18322c9b4f99Seric 18332c9b4f99Seric l = SYSLOG_BUFSIZE - 100 - strlen(buf); 18342c9b4f99Seric p = e->e_to; 18352c9b4f99Seric while (strlen(p) >= l) 18362c9b4f99Seric { 18372c9b4f99Seric register char *q = strchr(p + l, ','); 18382c9b4f99Seric 18392c9b4f99Seric if (*q == NULL) 18402c9b4f99Seric break; 18412c9b4f99Seric syslog(LOG_INFO, "%s: to=%.*s [more]%s", 18422c9b4f99Seric e->e_id, ++q - p, p, buf); 18432c9b4f99Seric p = q; 18442c9b4f99Seric } 18452c9b4f99Seric syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf); 18466c2c3107Seric # endif /* LOG */ 184725a99e2eSeric } 184825a99e2eSeric /* 184951552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 185025a99e2eSeric ** 185151552439Seric ** This can be made an arbitrary message separator by changing $l 185251552439Seric ** 18539b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 18549b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 18559b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 18569b6c17a6Seric ** this kind of antique garbage???? 185725a99e2eSeric ** 185825a99e2eSeric ** Parameters: 185951552439Seric ** fp -- the file to output to. 186051552439Seric ** m -- the mailer describing this entry. 186125a99e2eSeric ** 186225a99e2eSeric ** Returns: 186351552439Seric ** none 186425a99e2eSeric ** 186525a99e2eSeric ** Side Effects: 186651552439Seric ** outputs some text to fp. 186725a99e2eSeric */ 186825a99e2eSeric 1869b31e7f2bSeric putfromline(fp, m, e) 187051552439Seric register FILE *fp; 187151552439Seric register MAILER *m; 1872b31e7f2bSeric ENVELOPE *e; 187325a99e2eSeric { 18742bc47524Seric char *template = "\201l\n"; 187551552439Seric char buf[MAXLINE]; 187625a99e2eSeric 187757fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 187851552439Seric return; 187913bbc08cSeric 18802c7e1b8dSeric # ifdef UGLYUUCP 188157fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 188274b6e67bSeric { 1883ea09d6edSeric char *bang; 1884ea09d6edSeric char xbuf[MAXLINE]; 188574b6e67bSeric 1886ee4b0922Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 18876c2c3107Seric bang = strchr(buf, '!'); 188874b6e67bSeric if (bang == NULL) 188934fcca25Seric { 189034fcca25Seric errno = 0; 189134fcca25Seric syserr("554 No ! in UUCP From address! (%s given)", buf); 189234fcca25Seric } 189374b6e67bSeric else 1894588cad61Seric { 1895ea09d6edSeric *bang++ = '\0'; 18962bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1897ea09d6edSeric template = xbuf; 189874b6e67bSeric } 1899588cad61Seric } 19006c2c3107Seric # endif /* UGLYUUCP */ 1901b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 190277b52738Seric putline(buf, fp, m); 1903bc6e2962Seric } 1904bc6e2962Seric /* 190551552439Seric ** PUTBODY -- put the body of a message. 190651552439Seric ** 190751552439Seric ** Parameters: 190851552439Seric ** fp -- file to output onto. 190977b52738Seric ** m -- a mailer descriptor to control output format. 19109a6a5f55Seric ** e -- the envelope to put out. 191103c02fdeSeric ** separator -- if non-NULL, a message separator that must 191203c02fdeSeric ** not be permitted in the resulting message. 191351552439Seric ** 191451552439Seric ** Returns: 191551552439Seric ** none. 191651552439Seric ** 191751552439Seric ** Side Effects: 191851552439Seric ** The message is written onto fp. 191951552439Seric */ 192051552439Seric 192103c02fdeSeric putbody(fp, m, e, separator) 192251552439Seric FILE *fp; 1923588cad61Seric MAILER *m; 19249a6a5f55Seric register ENVELOPE *e; 192503c02fdeSeric char *separator; 192651552439Seric { 192777b52738Seric char buf[MAXLINE]; 192851552439Seric 192951552439Seric /* 193051552439Seric ** Output the body of the message 193151552439Seric */ 193251552439Seric 19339a6a5f55Seric if (e->e_dfp == NULL) 193451552439Seric { 19359a6a5f55Seric if (e->e_df != NULL) 19369a6a5f55Seric { 19379a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 19389a6a5f55Seric if (e->e_dfp == NULL) 19398f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 1940e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 19419a6a5f55Seric } 19429a6a5f55Seric else 194377b52738Seric putline("<<< No Message Collected >>>", fp, m); 19449a6a5f55Seric } 19459a6a5f55Seric if (e->e_dfp != NULL) 19469a6a5f55Seric { 19479a6a5f55Seric rewind(e->e_dfp); 194877b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 194924fc8aeeSeric { 195024fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1951d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 19523462ad9eSeric (void) putc('>', fp); 195303c02fdeSeric if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 195403c02fdeSeric { 195503c02fdeSeric /* possible separator */ 195603c02fdeSeric int sl = strlen(separator); 195703c02fdeSeric 195803c02fdeSeric if (strncmp(&buf[2], separator, sl) == 0) 195903c02fdeSeric (void) putc(' ', fp); 196003c02fdeSeric } 196177b52738Seric putline(buf, fp, m); 196224fc8aeeSeric } 196351552439Seric 19649a6a5f55Seric if (ferror(e->e_dfp)) 196551552439Seric { 1966df106f0bSeric syserr("putbody: %s: read error", e->e_df); 196751552439Seric ExitStat = EX_IOERR; 196851552439Seric } 196951552439Seric } 197051552439Seric 19710890ba1fSeric /* some mailers want extra blank line at end of message */ 19720890ba1fSeric if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 19730890ba1fSeric putline("", fp, m); 19740890ba1fSeric 197551552439Seric (void) fflush(fp); 197651552439Seric if (ferror(fp) && errno != EPIPE) 197751552439Seric { 197851552439Seric syserr("putbody: write error"); 197951552439Seric ExitStat = EX_IOERR; 198051552439Seric } 198151552439Seric errno = 0; 198225a99e2eSeric } 198325a99e2eSeric /* 198425a99e2eSeric ** MAILFILE -- Send a message to a file. 198525a99e2eSeric ** 1986f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1987f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1988f129ec7dSeric ** rather than the real user. Obviously, this only works if 1989f129ec7dSeric ** sendmail runs as root. 1990f129ec7dSeric ** 1991588cad61Seric ** This could be done as a subordinate mailer, except that it 1992588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1993588cad61Seric ** view this as being sufficiently important as to include it 1994588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1995588cad61Seric ** to create another process plus some pipes to save the message. 1996588cad61Seric ** 199725a99e2eSeric ** Parameters: 199825a99e2eSeric ** filename -- the name of the file to send to. 19996259796dSeric ** ctladdr -- the controlling address header -- includes 20006259796dSeric ** the userid/groupid to be when sending. 200125a99e2eSeric ** 200225a99e2eSeric ** Returns: 200325a99e2eSeric ** The exit code associated with the operation. 200425a99e2eSeric ** 200525a99e2eSeric ** Side Effects: 200625a99e2eSeric ** none. 200725a99e2eSeric */ 200825a99e2eSeric 2009b31e7f2bSeric mailfile(filename, ctladdr, e) 201025a99e2eSeric char *filename; 20116259796dSeric ADDRESS *ctladdr; 2012b31e7f2bSeric register ENVELOPE *e; 201325a99e2eSeric { 201425a99e2eSeric register FILE *f; 201532d19d43Seric register int pid; 201615d084d5Seric int mode; 201725a99e2eSeric 2018671745f3Seric if (tTd(11, 1)) 2019671745f3Seric { 2020671745f3Seric printf("mailfile %s\n ctladdr=", filename); 2021671745f3Seric printaddr(ctladdr, FALSE); 2022671745f3Seric } 2023671745f3Seric 2024f170942cSeric if (e->e_xfp != NULL) 2025f170942cSeric fflush(e->e_xfp); 2026f170942cSeric 202732d19d43Seric /* 202832d19d43Seric ** Fork so we can change permissions here. 202932d19d43Seric ** Note that we MUST use fork, not vfork, because of 203032d19d43Seric ** the complications of calling subroutines, etc. 203132d19d43Seric */ 203232d19d43Seric 203332d19d43Seric DOFORK(fork); 203432d19d43Seric 203532d19d43Seric if (pid < 0) 203632d19d43Seric return (EX_OSERR); 203732d19d43Seric else if (pid == 0) 203832d19d43Seric { 203932d19d43Seric /* child -- actually write to file */ 2040f129ec7dSeric struct stat stb; 2041f129ec7dSeric 20422b9178d3Seric (void) setsignal(SIGINT, SIG_DFL); 20432b9178d3Seric (void) setsignal(SIGHUP, SIG_DFL); 20442b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 20453462ad9eSeric (void) umask(OldUmask); 204695f16dc0Seric 2047f129ec7dSeric if (stat(filename, &stb) < 0) 20483a98e7eaSeric stb.st_mode = FileMode; 204915d084d5Seric mode = stb.st_mode; 205095f16dc0Seric 205195f16dc0Seric /* limit the errors to those actually caused in the child */ 205295f16dc0Seric errno = 0; 205395f16dc0Seric ExitStat = EX_OK; 205495f16dc0Seric 2055f129ec7dSeric if (bitset(0111, stb.st_mode)) 2056f129ec7dSeric exit(EX_CANTCREAT); 205719428781Seric if (ctladdr != NULL) 205815d084d5Seric { 205915d084d5Seric /* ignore setuid and setgid bits */ 206015d084d5Seric mode &= ~(S_ISGID|S_ISUID); 206115d084d5Seric } 206215d084d5Seric 20638f9146b0Srick /* we have to open the dfile BEFORE setuid */ 20648f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 20658f9146b0Srick { 20668f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 206795f16dc0Seric if (e->e_dfp == NULL) 206895f16dc0Seric { 20698f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 2070e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 20718f9146b0Srick } 20728f9146b0Srick } 20738f9146b0Srick 207415d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 2075e36b99e2Seric { 207619428781Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 207795f16dc0Seric { 2078898a126bSbostic (void) initgroups(DefUser, DefGid); 207995f16dc0Seric } 208095f16dc0Seric else 208195f16dc0Seric { 2082898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 2083898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 2084898a126bSbostic ctladdr->q_gid); 2085898a126bSbostic } 2086e36b99e2Seric } 208715d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 2088e36b99e2Seric { 208919428781Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 2090e36b99e2Seric (void) setuid(DefUid); 2091e36b99e2Seric else 20926259796dSeric (void) setuid(ctladdr->q_uid); 2093e36b99e2Seric } 209495f16dc0Seric FileName = filename; 209595f16dc0Seric LineNumber = 0; 20963a98e7eaSeric f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 209725a99e2eSeric if (f == NULL) 209895f16dc0Seric { 2099b6a0de9dSeric message("554 cannot open: %s", errstring(errno)); 210032d19d43Seric exit(EX_CANTCREAT); 210195f16dc0Seric } 210225a99e2eSeric 21030331ce05Seric putfromline(f, FileMailer, e); 21040331ce05Seric (*e->e_puthdr)(f, FileMailer, e); 21050331ce05Seric putline("\n", f, FileMailer); 210603c02fdeSeric (*e->e_putbody)(f, FileMailer, e, NULL); 21070331ce05Seric putline("\n", f, FileMailer); 210895f16dc0Seric if (ferror(f)) 210995f16dc0Seric { 2110b6a0de9dSeric message("451 I/O error: %s", errstring(errno)); 211195f16dc0Seric setstat(EX_IOERR); 211295f16dc0Seric } 2113ee4b0922Seric (void) xfclose(f, "mailfile", filename); 211432d19d43Seric (void) fflush(stdout); 2115e36b99e2Seric 211627628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 2117c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 211895f16dc0Seric exit(ExitStat); 211913bbc08cSeric /*NOTREACHED*/ 212032d19d43Seric } 212132d19d43Seric else 212232d19d43Seric { 212332d19d43Seric /* parent -- wait for exit status */ 2124588cad61Seric int st; 212532d19d43Seric 2126588cad61Seric st = waitfor(pid); 2127bf9bc890Seric if (WIFEXITED(st)) 2128bf9bc890Seric return (WEXITSTATUS(st)); 2129588cad61Seric else 2130b6a0de9dSeric { 2131b6a0de9dSeric syserr("child died on signal %d", st); 2132bf9bc890Seric return (EX_UNAVAILABLE); 2133b6a0de9dSeric } 21348f9146b0Srick /*NOTREACHED*/ 213532d19d43Seric } 213625a99e2eSeric } 2137ea4dc939Seric /* 2138e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 2139e103b48fSeric ** 2140e103b48fSeric ** The signature describes how we are going to send this -- it 2141e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 2142e103b48fSeric ** an ordered list of MX hosts. 2143e103b48fSeric ** 2144e103b48fSeric ** Parameters: 2145e103b48fSeric ** m -- the mailer describing this host. 2146e103b48fSeric ** host -- the host name. 2147e103b48fSeric ** e -- the current envelope. 2148e103b48fSeric ** 2149e103b48fSeric ** Returns: 2150e103b48fSeric ** The signature for this host. 2151e103b48fSeric ** 2152e103b48fSeric ** Side Effects: 2153e103b48fSeric ** Can tweak the symbol table. 2154e103b48fSeric */ 2155e103b48fSeric 2156e103b48fSeric char * 2157e103b48fSeric hostsignature(m, host, e) 2158e103b48fSeric register MAILER *m; 2159e103b48fSeric char *host; 2160e103b48fSeric ENVELOPE *e; 2161e103b48fSeric { 2162e103b48fSeric register char *p; 2163e103b48fSeric register STAB *s; 2164e103b48fSeric int i; 2165e103b48fSeric int len; 2166e103b48fSeric #ifdef NAMED_BIND 2167e103b48fSeric int nmx; 2168e103b48fSeric auto int rcode; 2169bafdc4e5Seric char *hp; 2170bafdc4e5Seric char *endp; 2171516782b4Seric int oldoptions; 2172e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 2173e103b48fSeric #endif 2174e103b48fSeric 2175e103b48fSeric /* 2176e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 2177e103b48fSeric */ 2178e103b48fSeric 2179e103b48fSeric p = m->m_mailer; 2180e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2181e103b48fSeric { 2182e103b48fSeric /* just an ordinary mailer */ 2183e103b48fSeric return host; 2184e103b48fSeric } 2185e103b48fSeric 2186e103b48fSeric /* 2187e103b48fSeric ** Look it up in the symbol table. 2188e103b48fSeric */ 2189e103b48fSeric 2190e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 2191e103b48fSeric if (s->s_hostsig != NULL) 2192e103b48fSeric return s->s_hostsig; 2193e103b48fSeric 2194e103b48fSeric /* 2195e103b48fSeric ** Not already there -- create a signature. 2196e103b48fSeric */ 2197e103b48fSeric 2198e103b48fSeric #ifdef NAMED_BIND 2199516782b4Seric if (ConfigLevel < 2) 2200516782b4Seric { 2201516782b4Seric oldoptions = _res.options; 2202516782b4Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2203516782b4Seric } 2204516782b4Seric 2205bafdc4e5Seric for (hp = host; hp != NULL; hp = endp) 2206bafdc4e5Seric { 2207bafdc4e5Seric endp = strchr(hp, ':'); 2208bafdc4e5Seric if (endp != NULL) 2209bafdc4e5Seric *endp = '\0'; 2210bafdc4e5Seric 22117bf809e6Seric nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 22127d55540cSeric 2213e103b48fSeric if (nmx <= 0) 2214e103b48fSeric { 2215e103b48fSeric register MCI *mci; 2216e103b48fSeric 2217e103b48fSeric /* update the connection info for this host */ 2218bafdc4e5Seric mci = mci_get(hp, m); 2219e103b48fSeric mci->mci_exitstat = rcode; 2220e103b48fSeric mci->mci_errno = errno; 2221f170942cSeric #ifdef NAMED_BIND 2222f170942cSeric mci->mci_herrno = h_errno; 2223f170942cSeric #endif 2224e103b48fSeric 2225e103b48fSeric /* and return the original host name as the signature */ 2226bafdc4e5Seric nmx = 1; 2227bafdc4e5Seric mxhosts[0] = hp; 2228e103b48fSeric } 2229e103b48fSeric 2230e103b48fSeric len = 0; 2231e103b48fSeric for (i = 0; i < nmx; i++) 2232e103b48fSeric { 2233e103b48fSeric len += strlen(mxhosts[i]) + 1; 2234e103b48fSeric } 2235bafdc4e5Seric if (s->s_hostsig != NULL) 2236bafdc4e5Seric len += strlen(s->s_hostsig) + 1; 2237bafdc4e5Seric p = xalloc(len); 2238bafdc4e5Seric if (s->s_hostsig != NULL) 2239bafdc4e5Seric { 2240bafdc4e5Seric (void) strcpy(p, s->s_hostsig); 2241bafdc4e5Seric free(s->s_hostsig); 2242bafdc4e5Seric s->s_hostsig = p; 2243bafdc4e5Seric p += strlen(p); 2244bafdc4e5Seric *p++ = ':'; 2245bafdc4e5Seric } 2246bafdc4e5Seric else 2247bafdc4e5Seric s->s_hostsig = p; 2248e103b48fSeric for (i = 0; i < nmx; i++) 2249e103b48fSeric { 2250e103b48fSeric if (i != 0) 2251e103b48fSeric *p++ = ':'; 2252e103b48fSeric strcpy(p, mxhosts[i]); 2253e103b48fSeric p += strlen(p); 2254e103b48fSeric } 2255bafdc4e5Seric if (endp != NULL) 2256bafdc4e5Seric *endp++ = ':'; 2257bafdc4e5Seric } 2258e103b48fSeric makelower(s->s_hostsig); 2259516782b4Seric if (ConfigLevel < 2) 2260516782b4Seric _res.options = oldoptions; 2261e103b48fSeric #else 2262e103b48fSeric /* not using BIND -- the signature is just the host name */ 2263e103b48fSeric s->s_hostsig = host; 2264e103b48fSeric #endif 2265e103b48fSeric if (tTd(17, 1)) 2266e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2267e103b48fSeric return s->s_hostsig; 2268e103b48fSeric } 2269