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*ee39df61Seric static char sccsid[] = "@(#)deliver.c 8.18 (Berkeley) 08/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 */ 669c9e68d9Seric if (mode == SM_DEFAULT) 679c9e68d9Seric { 689c9e68d9Seric mode = e->e_sendmode; 699c9e68d9Seric if (mode != SM_VERIFY && 709c9e68d9Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 719c9e68d9Seric mode = SM_QUEUE; 726103d9b4Seric announcequeueup = mode == SM_QUEUE; 739c9e68d9Seric } 746103d9b4Seric else 756103d9b4Seric announcequeueup = FALSE; 769c9e68d9Seric 779c9e68d9Seric if (tTd(13, 1)) 789c9e68d9Seric { 790e484fe5Seric printf("\n===== SENDALL: mode %c, id %s, e_from ", 800e484fe5Seric mode, e->e_id); 819c9e68d9Seric printaddr(&e->e_from, FALSE); 829c9e68d9Seric printf("sendqueue:\n"); 839c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 849c9e68d9Seric } 859c9e68d9Seric 869c9e68d9Seric /* 879c9e68d9Seric ** Do any preprocessing necessary for the mode we are running. 889c9e68d9Seric ** Check to make sure the hop count is reasonable. 899c9e68d9Seric ** Delete sends to the sender in mailing lists. 909c9e68d9Seric */ 919c9e68d9Seric 929c9e68d9Seric CurEnv = e; 939c9e68d9Seric 949c9e68d9Seric if (e->e_hopcount > MaxHopCount) 959c9e68d9Seric { 969c9e68d9Seric errno = 0; 979c9e68d9Seric syserr("554 too many hops %d (%d max): from %s, to %s", 989c9e68d9Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 999c9e68d9Seric e->e_sendqueue->q_paddr); 1009c9e68d9Seric return; 1019c9e68d9Seric } 1029c9e68d9Seric 103b8004690Seric /* 104b8004690Seric ** Do sender deletion. 105b8004690Seric ** 106b8004690Seric ** If the sender has the QQUEUEUP flag set, skip this. 107b8004690Seric ** This can happen if the name server is hosed when you 108b8004690Seric ** are trying to send mail. The result is that the sender 109b8004690Seric ** is instantiated in the queue as a recipient. 110b8004690Seric */ 111b8004690Seric 112e76a6a8fSeric if (!bitset(EF_METOO, e->e_flags) && 113e76a6a8fSeric !bitset(QQUEUEUP, e->e_from.q_flags)) 1149c9e68d9Seric { 1159c9e68d9Seric if (tTd(13, 5)) 1169c9e68d9Seric { 1179c9e68d9Seric printf("sendall: QDONTSEND "); 1189c9e68d9Seric printaddr(&e->e_from, FALSE); 1199c9e68d9Seric } 1209c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 1219c9e68d9Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 1229c9e68d9Seric } 1239c9e68d9Seric 124ce5531bdSeric /* 125ce5531bdSeric ** Handle alias owners. 126ce5531bdSeric ** 127ce5531bdSeric ** We scan up the q_alias chain looking for owners. 128ce5531bdSeric ** We discard owners that are the same as the return path. 129ce5531bdSeric */ 130ce5531bdSeric 131ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 132ce5531bdSeric { 133ce5531bdSeric register struct address *a; 134ce5531bdSeric 135ce5531bdSeric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 136ce5531bdSeric continue; 137ce5531bdSeric if (a != NULL) 138ce5531bdSeric q->q_owner = a->q_owner; 139ce5531bdSeric 140ce5531bdSeric if (q->q_owner != NULL && 141ce5531bdSeric !bitset(QDONTSEND, q->q_flags) && 142ce5531bdSeric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 143ce5531bdSeric q->q_owner = NULL; 144ce5531bdSeric } 145ce5531bdSeric 146ce5531bdSeric owner = ""; 147ce5531bdSeric otherowners = 1; 148ce5531bdSeric while (owner != NULL && otherowners > 0) 149ce5531bdSeric { 150ce5531bdSeric owner = NULL; 151ce5531bdSeric otherowners = 0; 152ce5531bdSeric 153ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 154ce5531bdSeric { 155ce5531bdSeric if (bitset(QDONTSEND, q->q_flags)) 156ce5531bdSeric continue; 157ce5531bdSeric 158ce5531bdSeric if (q->q_owner != NULL) 159ce5531bdSeric { 160ce5531bdSeric if (owner == NULL) 161ce5531bdSeric owner = q->q_owner; 162ce5531bdSeric else if (owner != q->q_owner) 163ce5531bdSeric { 164ce5531bdSeric if (strcmp(owner, q->q_owner) == 0) 165ce5531bdSeric { 166ce5531bdSeric /* make future comparisons cheap */ 167ce5531bdSeric q->q_owner = owner; 168ce5531bdSeric } 169ce5531bdSeric else 170ce5531bdSeric { 171ce5531bdSeric otherowners++; 172ce5531bdSeric } 173ce5531bdSeric owner = q->q_owner; 174ce5531bdSeric } 175ce5531bdSeric } 176ce5531bdSeric else 177ce5531bdSeric { 178ce5531bdSeric otherowners++; 179ce5531bdSeric } 180ce5531bdSeric } 181ce5531bdSeric 182ce5531bdSeric if (owner != NULL && otherowners > 0) 183ce5531bdSeric { 184ce5531bdSeric extern HDR *copyheader(); 185ce5531bdSeric extern ADDRESS *copyqueue(); 186ce5531bdSeric 187ce5531bdSeric /* 188ce5531bdSeric ** Split this envelope into two. 189ce5531bdSeric */ 190ce5531bdSeric 191ce5531bdSeric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 192ce5531bdSeric *ee = *e; 193ce5531bdSeric ee->e_id = NULL; 194ce5531bdSeric (void) queuename(ee, '\0'); 195ce5531bdSeric 196ce5531bdSeric if (tTd(13, 1)) 197ce5531bdSeric printf("sendall: split %s into %s\n", 198ce5531bdSeric e->e_id, ee->e_id); 199ce5531bdSeric 200ce5531bdSeric ee->e_header = copyheader(e->e_header); 201ce5531bdSeric ee->e_sendqueue = copyqueue(e->e_sendqueue); 202ce5531bdSeric ee->e_errorqueue = copyqueue(e->e_errorqueue); 203ce5531bdSeric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS); 204ce5531bdSeric setsender(owner, ee, NULL, TRUE); 205ce5531bdSeric if (tTd(13, 5)) 206ce5531bdSeric { 207ce5531bdSeric printf("sendall(split): QDONTSEND "); 208ce5531bdSeric printaddr(&ee->e_from, FALSE); 209ce5531bdSeric } 210ce5531bdSeric ee->e_from.q_flags |= QDONTSEND; 211ce5531bdSeric ee->e_dfp = NULL; 212ce5531bdSeric ee->e_xfp = NULL; 213ce5531bdSeric ee->e_lockfp = 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; 4109c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 4119c9e68d9Seric { 4129c9e68d9Seric if (mode == SM_VERIFY) 4139c9e68d9Seric { 4149c9e68d9Seric e->e_to = q->q_paddr; 4159c9e68d9Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4168dfca105Seric { 4178dfca105Seric message("deliverable: mailer %s, host %s, user %s", 4188dfca105Seric q->q_mailer->m_name, 4198dfca105Seric q->q_host, 4208dfca105Seric q->q_user); 4218dfca105Seric } 4229c9e68d9Seric } 4239c9e68d9Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4249c9e68d9Seric { 4259c9e68d9Seric # ifdef QUEUE 4269c9e68d9Seric /* 4279c9e68d9Seric ** Checkpoint the send list every few addresses 4289c9e68d9Seric */ 4299c9e68d9Seric 4309c9e68d9Seric if (e->e_nsent >= CheckpointInterval) 4319c9e68d9Seric { 4329c9e68d9Seric queueup(e, TRUE, FALSE); 4339c9e68d9Seric e->e_nsent = 0; 4349c9e68d9Seric } 4359c9e68d9Seric # endif /* QUEUE */ 4369c9e68d9Seric (void) deliver(e, q); 4379c9e68d9Seric } 4389c9e68d9Seric } 4399c9e68d9Seric Verbose = oldverbose; 4409c9e68d9Seric 4419c9e68d9Seric if (mode == SM_FORK) 4429c9e68d9Seric finis(); 4439c9e68d9Seric } 4449c9e68d9Seric /* 4459c9e68d9Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4469c9e68d9Seric ** 4479c9e68d9Seric ** This MUST be a macro, since after a vfork we are running 4489c9e68d9Seric ** two processes on the same stack!!! 4499c9e68d9Seric ** 4509c9e68d9Seric ** Parameters: 4519c9e68d9Seric ** none. 4529c9e68d9Seric ** 4539c9e68d9Seric ** Returns: 4549c9e68d9Seric ** From a macro??? You've got to be kidding! 4559c9e68d9Seric ** 4569c9e68d9Seric ** Side Effects: 4579c9e68d9Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4589c9e68d9Seric ** pid of child in parent, zero in child. 4599c9e68d9Seric ** -1 on unrecoverable error. 4609c9e68d9Seric ** 4619c9e68d9Seric ** Notes: 4629c9e68d9Seric ** I'm awfully sorry this looks so awful. That's 4639c9e68d9Seric ** vfork for you..... 4649c9e68d9Seric */ 4659c9e68d9Seric 4669c9e68d9Seric # define NFORKTRIES 5 4679c9e68d9Seric 4689c9e68d9Seric # ifndef FORK 4699c9e68d9Seric # define FORK fork 4709c9e68d9Seric # endif 4719c9e68d9Seric 4729c9e68d9Seric # define DOFORK(fORKfN) \ 4739c9e68d9Seric {\ 4749c9e68d9Seric register int i;\ 4759c9e68d9Seric \ 4769c9e68d9Seric for (i = NFORKTRIES; --i >= 0; )\ 4779c9e68d9Seric {\ 4789c9e68d9Seric pid = fORKfN();\ 4799c9e68d9Seric if (pid >= 0)\ 4809c9e68d9Seric break;\ 4819c9e68d9Seric if (i > 0)\ 4829c9e68d9Seric sleep((unsigned) NFORKTRIES - i);\ 4839c9e68d9Seric }\ 4849c9e68d9Seric } 4859c9e68d9Seric /* 4869c9e68d9Seric ** DOFORK -- simple fork interface to DOFORK. 4879c9e68d9Seric ** 4889c9e68d9Seric ** Parameters: 4899c9e68d9Seric ** none. 4909c9e68d9Seric ** 4919c9e68d9Seric ** Returns: 4929c9e68d9Seric ** pid of child in parent. 4939c9e68d9Seric ** zero in child. 4949c9e68d9Seric ** -1 on error. 4959c9e68d9Seric ** 4969c9e68d9Seric ** Side Effects: 4979c9e68d9Seric ** returns twice, once in parent and once in child. 4989c9e68d9Seric */ 4999c9e68d9Seric 5009c9e68d9Seric dofork() 5019c9e68d9Seric { 5029c9e68d9Seric register int pid; 5039c9e68d9Seric 5049c9e68d9Seric DOFORK(fork); 5059c9e68d9Seric return (pid); 5069c9e68d9Seric } 5079c9e68d9Seric /* 50813bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 50913bbc08cSeric ** 51013bbc08cSeric ** This routine delivers to everyone on the same host as the 51113bbc08cSeric ** user on the head of the list. It is clever about mailers 51213bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 51313bbc08cSeric ** that it will deliver to all these addresses however -- so 51413bbc08cSeric ** deliver should be called once for each address on the 51513bbc08cSeric ** list. 51625a99e2eSeric ** 51725a99e2eSeric ** Parameters: 518588cad61Seric ** e -- the envelope to deliver. 519c77d1c25Seric ** firstto -- head of the address list to deliver to. 52025a99e2eSeric ** 52125a99e2eSeric ** Returns: 52225a99e2eSeric ** zero -- successfully delivered. 52325a99e2eSeric ** else -- some failure, see ExitStat for more info. 52425a99e2eSeric ** 52525a99e2eSeric ** Side Effects: 52625a99e2eSeric ** The standard input is passed off to someone. 52725a99e2eSeric */ 52825a99e2eSeric 529588cad61Seric deliver(e, firstto) 530588cad61Seric register ENVELOPE *e; 531c77d1c25Seric ADDRESS *firstto; 53225a99e2eSeric { 53378442df3Seric char *host; /* host being sent to */ 53478442df3Seric char *user; /* user being sent to */ 53525a99e2eSeric char **pvp; 5365dfc646bSeric register char **mvp; 53725a99e2eSeric register char *p; 538588cad61Seric register MAILER *m; /* mailer for this recipient */ 5396259796dSeric ADDRESS *ctladdr; 540b31e7f2bSeric register MCI *mci; 541c77d1c25Seric register ADDRESS *to = firstto; 542c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 543772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 544911693bfSbostic int rcode; /* response code */ 545e103b48fSeric char *firstsig; /* signature of firstto */ 5469c9e68d9Seric int pid; 5479c9e68d9Seric char *curhost; 5489c9e68d9Seric int mpvect[2]; 5499c9e68d9Seric int rpvect[2]; 550ee6bf8dfSeric char *pv[MAXPV+1]; 551579ef0ddSeric char tobuf[TOBUFSIZE]; /* text line of to people */ 552ee6bf8dfSeric char buf[MAXNAME]; 553c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 554fabb3bd4Seric extern int checkcompat(); 5559c9e68d9Seric extern FILE *fdopen(); 55625a99e2eSeric 55735490626Seric errno = 0; 558ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5595dfc646bSeric return (0); 56025a99e2eSeric 561134746fbSeric #ifdef NAMED_BIND 562912a731aSbostic /* unless interactive, try twice, over a minute */ 563912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 564912a731aSbostic _res.retrans = 30; 565912a731aSbostic _res.retry = 2; 566912a731aSbostic } 567d4bd8f0eSbostic #endif 568912a731aSbostic 56951552439Seric m = to->q_mailer; 57051552439Seric host = to->q_host; 571c9be6216Seric CurEnv = e; /* just in case */ 5724384d521Seric e->e_statmsg = NULL; 57351552439Seric 5746ef48975Seric if (tTd(10, 1)) 5755dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 57651552439Seric m->m_mno, host, to->q_user); 577f3dbc832Seric 578f3dbc832Seric /* 579f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 580f3dbc832Seric ** connections now, just mark these addresses and return. 581f3dbc832Seric ** This is useful if we want to batch connections to 582f3dbc832Seric ** reduce load. This will cause the messages to be 583f3dbc832Seric ** queued up, and a daemon will come along to send the 584f3dbc832Seric ** messages later. 585f3dbc832Seric ** This should be on a per-mailer basis. 586f3dbc832Seric */ 587f3dbc832Seric 58819c47125Seric if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) && 58919c47125Seric bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 590f3dbc832Seric { 591f3dbc832Seric for (; to != NULL; to = to->q_next) 592f4560e80Seric { 593ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 594c6e18ac5Seric to->q_mailer != m) 595f4560e80Seric continue; 596268a25e1Seric to->q_flags |= QQUEUEUP; 597588cad61Seric e->e_to = to->q_paddr; 59808b25121Seric message("queued"); 5992f624c86Seric if (LogLevel > 8) 60081161401Seric logdelivery(m, NULL, "queued", e); 601f4560e80Seric } 602588cad61Seric e->e_to = NULL; 603f3dbc832Seric return (0); 604f3dbc832Seric } 605f3dbc832Seric 60625a99e2eSeric /* 6075dfc646bSeric ** Do initial argv setup. 6085dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 6095dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 6105dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 6115dfc646bSeric ** code does not check for 'pv' overflow; this places a 6125dfc646bSeric ** manifest lower limit of 4 for MAXPV. 6133bea8136Seric ** The from address rewrite is expected to make 6143bea8136Seric ** the address relative to the other end. 6155dfc646bSeric */ 6165dfc646bSeric 61778442df3Seric /* rewrite from address, using rewriting rules */ 618efe54562Seric rcode = EX_OK; 619efe54562Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, 620efe54562Seric RF_SENDERADDR|RF_CANONICAL, 621efe54562Seric &rcode, e)); 622ee4b0922Seric define('g', rpathbuf, e); /* translated return path */ 623588cad61Seric define('h', host, e); /* to host */ 6245dfc646bSeric Errors = 0; 6255dfc646bSeric pvp = pv; 6265dfc646bSeric *pvp++ = m->m_argv[0]; 6275dfc646bSeric 6285dfc646bSeric /* insert -f or -r flag as appropriate */ 62957fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6305dfc646bSeric { 63157fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 6325dfc646bSeric *pvp++ = "-f"; 6335dfc646bSeric else 6345dfc646bSeric *pvp++ = "-r"; 635c23ed322Seric *pvp++ = newstr(rpathbuf); 6365dfc646bSeric } 6375dfc646bSeric 6385dfc646bSeric /* 6395dfc646bSeric ** Append the other fixed parts of the argv. These run 6405dfc646bSeric ** up to the first entry containing "$u". There can only 6415dfc646bSeric ** be one of these, and there are only a few more slots 6425dfc646bSeric ** in the pv after it. 6435dfc646bSeric */ 6445dfc646bSeric 6455dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 6465dfc646bSeric { 6472bc47524Seric /* can't use strchr here because of sign extension problems */ 6482bc47524Seric while (*p != '\0') 6492bc47524Seric { 6502bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 6512bc47524Seric { 6522bc47524Seric if (*p == 'u') 6535dfc646bSeric break; 6542bc47524Seric } 6552bc47524Seric } 6562bc47524Seric 6572bc47524Seric if (*p != '\0') 6585dfc646bSeric break; 6595dfc646bSeric 6605dfc646bSeric /* this entry is safe -- go ahead and process it */ 661588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 6625dfc646bSeric *pvp++ = newstr(buf); 6635dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 6645dfc646bSeric { 66508b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 6665dfc646bSeric return (-1); 6675dfc646bSeric } 6685dfc646bSeric } 669c579ef51Seric 67033db8731Seric /* 67133db8731Seric ** If we have no substitution for the user name in the argument 67233db8731Seric ** list, we know that we must supply the names otherwise -- and 67333db8731Seric ** SMTP is the answer!! 67433db8731Seric */ 67533db8731Seric 6765dfc646bSeric if (*mvp == NULL) 677c579ef51Seric { 678c579ef51Seric /* running SMTP */ 6792c7e1b8dSeric # ifdef SMTP 680c579ef51Seric clever = TRUE; 681c579ef51Seric *pvp = NULL; 6826c2c3107Seric # else /* SMTP */ 68333db8731Seric /* oops! we don't implement SMTP */ 68408b25121Seric syserr("554 SMTP style mailer"); 6852c7e1b8dSeric return (EX_SOFTWARE); 6866c2c3107Seric # endif /* SMTP */ 687c579ef51Seric } 6885dfc646bSeric 6895dfc646bSeric /* 6905dfc646bSeric ** At this point *mvp points to the argument with $u. We 6915dfc646bSeric ** run through our address list and append all the addresses 6925dfc646bSeric ** we can. If we run out of space, do not fret! We can 6935dfc646bSeric ** always send another copy later. 6945dfc646bSeric */ 6955dfc646bSeric 6965dfc646bSeric tobuf[0] = '\0'; 697588cad61Seric e->e_to = tobuf; 6986259796dSeric ctladdr = NULL; 699e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7005dfc646bSeric for (; to != NULL; to = to->q_next) 7015dfc646bSeric { 7025dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 70357fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7045dfc646bSeric break; 7055dfc646bSeric 7065dfc646bSeric /* if already sent or not for this host, don't send */ 707ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 708e103b48fSeric to->q_mailer != firstto->q_mailer || 709e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7105dfc646bSeric continue; 7116259796dSeric 7124b22ea87Seric /* avoid overflowing tobuf */ 713aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7144b22ea87Seric break; 7154b22ea87Seric 7166ef48975Seric if (tTd(10, 1)) 717772e6e50Seric { 718772e6e50Seric printf("\nsend to "); 719772e6e50Seric printaddr(to, FALSE); 720772e6e50Seric } 721772e6e50Seric 7226259796dSeric /* compute effective uid/gid when sending */ 7237da1035fSeric if (to->q_mailer == ProgMailer) 7246259796dSeric ctladdr = getctladdr(to); 7256259796dSeric 7265dfc646bSeric user = to->q_user; 727588cad61Seric e->e_to = to->q_paddr; 72875f1ade9Seric if (tTd(10, 5)) 72975f1ade9Seric { 73075f1ade9Seric printf("deliver: QDONTSEND "); 73175f1ade9Seric printaddr(to, FALSE); 73275f1ade9Seric } 733ee4b0922Seric to->q_flags |= QDONTSEND; 7345dfc646bSeric 7355dfc646bSeric /* 7365dfc646bSeric ** Check to see that these people are allowed to 7375dfc646bSeric ** talk to each other. 7382a6e0786Seric */ 7392a6e0786Seric 74069582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 74169582d2fSeric { 74269582d2fSeric NoReturn = TRUE; 74308b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 74481161401Seric giveresponse(EX_UNAVAILABLE, m, NULL, e); 74569582d2fSeric continue; 74669582d2fSeric } 747fabb3bd4Seric rcode = checkcompat(to, e); 7481793c9c5Seric if (rcode != EX_OK) 7495dfc646bSeric { 7501000c37aSeric markfailure(e, to, rcode); 75181161401Seric giveresponse(rcode, m, NULL, e); 7525dfc646bSeric continue; 7535dfc646bSeric } 7542a6e0786Seric 7552a6e0786Seric /* 7569ec9501bSeric ** Strip quote bits from names if the mailer is dumb 7579ec9501bSeric ** about them. 75825a99e2eSeric */ 75925a99e2eSeric 76057fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 76125a99e2eSeric { 7621d8f1806Seric stripquotes(user); 7631d8f1806Seric stripquotes(host); 76425a99e2eSeric } 76525a99e2eSeric 766cdb828c5Seric /* hack attack -- delivermail compatibility */ 767cdb828c5Seric if (m == ProgMailer && *user == '|') 768cdb828c5Seric user++; 769cdb828c5Seric 77025a99e2eSeric /* 7713efaed6eSeric ** If an error message has already been given, don't 7723efaed6eSeric ** bother to send to this address. 7733efaed6eSeric ** 7743efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 7753efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 7763efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 7773efaed6eSeric */ 7783efaed6eSeric 7796cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 7803efaed6eSeric continue; 7813efaed6eSeric 782f2fec898Seric /* save statistics.... */ 783588cad61Seric markstats(e, to); 784f2fec898Seric 7853efaed6eSeric /* 78625a99e2eSeric ** See if this user name is "special". 78725a99e2eSeric ** If the user name has a slash in it, assume that this 78851552439Seric ** is a file -- send it off without further ado. Note 78951552439Seric ** that this type of addresses is not processed along 79051552439Seric ** with the others, so we fudge on the To person. 79125a99e2eSeric */ 79225a99e2eSeric 7932c017f8dSeric if (m == FileMailer) 79425a99e2eSeric { 795b31e7f2bSeric rcode = mailfile(user, getctladdr(to), e); 79681161401Seric giveresponse(rcode, m, NULL, e); 797dde5acadSeric if (rcode == EX_OK) 798dde5acadSeric to->q_flags |= QSENT; 7995dfc646bSeric continue; 80025a99e2eSeric } 80125a99e2eSeric 80213bbc08cSeric /* 80313bbc08cSeric ** Address is verified -- add this user to mailer 80413bbc08cSeric ** argv, and add it to the print list of recipients. 80513bbc08cSeric */ 80613bbc08cSeric 807508daeccSeric /* link together the chain of recipients */ 808508daeccSeric to->q_tchain = tochain; 809508daeccSeric tochain = to; 810508daeccSeric 8115dfc646bSeric /* create list of users for error messages */ 812db8841e9Seric (void) strcat(tobuf, ","); 813db8841e9Seric (void) strcat(tobuf, to->q_paddr); 814588cad61Seric define('u', user, e); /* to user */ 815588cad61Seric define('z', to->q_home, e); /* user's home */ 8165dfc646bSeric 817c579ef51Seric /* 818508daeccSeric ** Expand out this user into argument list. 819c579ef51Seric */ 820c579ef51Seric 821508daeccSeric if (!clever) 822c579ef51Seric { 823588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8245dfc646bSeric *pvp++ = newstr(buf); 8255dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 8265dfc646bSeric { 8275dfc646bSeric /* allow some space for trailing parms */ 8285dfc646bSeric break; 8295dfc646bSeric } 8305dfc646bSeric } 831c579ef51Seric } 8325dfc646bSeric 833145b49b1Seric /* see if any addresses still exist */ 834145b49b1Seric if (tobuf[0] == '\0') 835c579ef51Seric { 836588cad61Seric define('g', (char *) NULL, e); 837145b49b1Seric return (0); 838c579ef51Seric } 839145b49b1Seric 8405dfc646bSeric /* print out messages as full list */ 84163780dbdSeric e->e_to = tobuf + 1; 8425dfc646bSeric 8435dfc646bSeric /* 8445dfc646bSeric ** Fill out any parameters after the $u parameter. 8455dfc646bSeric */ 8465dfc646bSeric 847c579ef51Seric while (!clever && *++mvp != NULL) 8485dfc646bSeric { 849588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8505dfc646bSeric *pvp++ = newstr(buf); 8515dfc646bSeric if (pvp >= &pv[MAXPV]) 85208b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 8535dfc646bSeric } 8545dfc646bSeric *pvp++ = NULL; 8555dfc646bSeric 85625a99e2eSeric /* 85725a99e2eSeric ** Call the mailer. 8586328bdf7Seric ** The argument vector gets built, pipes 85925a99e2eSeric ** are created as necessary, and we fork & exec as 8606328bdf7Seric ** appropriate. 861c579ef51Seric ** If we are running SMTP, we just need to clean up. 86225a99e2eSeric */ 86325a99e2eSeric 8647ee87e5fSeric if (ctladdr == NULL && m != ProgMailer) 86586b26461Seric ctladdr = &e->e_from; 866134746fbSeric #ifdef NAMED_BIND 8672bcc6d2dSeric if (ConfigLevel < 2) 868912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 869134746fbSeric #endif 8709c9e68d9Seric 8719c9e68d9Seric if (tTd(11, 1)) 872134746fbSeric { 8739c9e68d9Seric printf("openmailer:"); 8749c9e68d9Seric printav(pv); 8759c9e68d9Seric } 8769c9e68d9Seric errno = 0; 8779c9e68d9Seric 8789c9e68d9Seric CurHostName = m->m_mailer; 8799c9e68d9Seric 8809c9e68d9Seric /* 8819c9e68d9Seric ** Deal with the special case of mail handled through an IPC 8829c9e68d9Seric ** connection. 8839c9e68d9Seric ** In this case we don't actually fork. We must be 8849c9e68d9Seric ** running SMTP for this to work. We will return a 8859c9e68d9Seric ** zero pid to indicate that we are running IPC. 8869c9e68d9Seric ** We also handle a debug version that just talks to stdin/out. 8879c9e68d9Seric */ 8889c9e68d9Seric 8899c9e68d9Seric curhost = NULL; 890c931b82bSeric SmtpPhase = NULL; 8919c9e68d9Seric 8929c9e68d9Seric /* check for Local Person Communication -- not for mortals!!! */ 8939c9e68d9Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 8949c9e68d9Seric { 8959c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 8969c9e68d9Seric bzero((char *) mci, sizeof *mci); 8979c9e68d9Seric mci->mci_in = stdin; 8989c9e68d9Seric mci->mci_out = stdout; 8999c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9009c9e68d9Seric mci->mci_mailer = m; 9019c9e68d9Seric } 9029c9e68d9Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 9039c9e68d9Seric strcmp(m->m_mailer, "[TCP]") == 0) 9049c9e68d9Seric { 9059c9e68d9Seric #ifdef DAEMON 9069c9e68d9Seric register int i; 9079c9e68d9Seric register u_short port; 9089c9e68d9Seric 9099c9e68d9Seric CurHostName = pv[1]; 9109c9e68d9Seric curhost = hostsignature(m, pv[1], e); 9119c9e68d9Seric 9129c9e68d9Seric if (curhost == NULL || curhost[0] == '\0') 9139c9e68d9Seric { 9149c9e68d9Seric syserr("null signature"); 915845e533cSeric rcode = EX_OSERR; 916b31e7f2bSeric goto give_up; 917b31e7f2bSeric } 9189c9e68d9Seric 9199c9e68d9Seric if (!clever) 9209c9e68d9Seric { 9219c9e68d9Seric syserr("554 non-clever IPC"); 9229c9e68d9Seric rcode = EX_OSERR; 9239c9e68d9Seric goto give_up; 9249c9e68d9Seric } 9259c9e68d9Seric if (pv[2] != NULL) 9269c9e68d9Seric port = atoi(pv[2]); 9279c9e68d9Seric else 9289c9e68d9Seric port = 0; 9299c9e68d9Seric tryhost: 9309c9e68d9Seric mci = NULL; 9319c9e68d9Seric while (*curhost != '\0') 9329c9e68d9Seric { 9339c9e68d9Seric register char *p; 9349c9e68d9Seric static char hostbuf[MAXNAME]; 9359c9e68d9Seric 9369c9e68d9Seric mci = NULL; 9379c9e68d9Seric 9389c9e68d9Seric /* pull the next host from the signature */ 9399c9e68d9Seric p = strchr(curhost, ':'); 9409c9e68d9Seric if (p == NULL) 9419c9e68d9Seric p = &curhost[strlen(curhost)]; 9429c9e68d9Seric strncpy(hostbuf, curhost, p - curhost); 9439c9e68d9Seric hostbuf[p - curhost] = '\0'; 9449c9e68d9Seric if (*p != '\0') 9459c9e68d9Seric p++; 9469c9e68d9Seric curhost = p; 9479c9e68d9Seric 9489c9e68d9Seric /* see if we already know that this host is fried */ 9499c9e68d9Seric CurHostName = hostbuf; 9509c9e68d9Seric mci = mci_get(hostbuf, m); 9519c9e68d9Seric if (mci->mci_state != MCIS_CLOSED) 9529c9e68d9Seric { 9539c9e68d9Seric if (tTd(11, 1)) 9549c9e68d9Seric { 9559c9e68d9Seric printf("openmailer: "); 9569c9e68d9Seric mci_dump(mci); 9579c9e68d9Seric } 9589c9e68d9Seric CurHostName = mci->mci_host; 9599c9e68d9Seric break; 9609c9e68d9Seric } 9619c9e68d9Seric mci->mci_mailer = m; 9629c9e68d9Seric if (mci->mci_exitstat != EX_OK) 9639c9e68d9Seric continue; 9649c9e68d9Seric 9659c9e68d9Seric /* try the connection */ 9669c9e68d9Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 9679c9e68d9Seric message("Connecting to %s (%s)...", 9689c9e68d9Seric hostbuf, m->m_name); 9699c9e68d9Seric i = makeconnection(hostbuf, port, mci, 9709c9e68d9Seric bitnset(M_SECURE_PORT, m->m_flags)); 9719c9e68d9Seric mci->mci_exitstat = i; 9729c9e68d9Seric mci->mci_errno = errno; 973f170942cSeric #ifdef NAMED_BIND 974f170942cSeric mci->mci_herrno = h_errno; 975f170942cSeric #endif 9769c9e68d9Seric if (i == EX_OK) 9779c9e68d9Seric { 9789c9e68d9Seric mci->mci_state = MCIS_OPENING; 9799c9e68d9Seric mci_cache(mci); 980f170942cSeric if (TrafficLogFile != NULL) 981f170942cSeric fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 982f170942cSeric getpid(), hostbuf); 9839c9e68d9Seric break; 9849c9e68d9Seric } 9859c9e68d9Seric else if (tTd(11, 1)) 9869c9e68d9Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 9879c9e68d9Seric i, errno); 9889c9e68d9Seric 9899c9e68d9Seric 9909c9e68d9Seric /* enter status of this host */ 9919c9e68d9Seric setstat(i); 9929c9e68d9Seric } 9939c9e68d9Seric mci->mci_pid = 0; 9949c9e68d9Seric #else /* no DAEMON */ 9959c9e68d9Seric syserr("554 openmailer: no IPC"); 9969c9e68d9Seric if (tTd(11, 1)) 9979c9e68d9Seric printf("openmailer: NULL\n"); 9989c9e68d9Seric return NULL; 9999c9e68d9Seric #endif /* DAEMON */ 10009c9e68d9Seric } 10019c9e68d9Seric else 10029c9e68d9Seric { 1003f170942cSeric #ifdef XDEBUG 1004f170942cSeric char wbuf[MAXLINE]; 10056fe8c3bcSeric 10066fe8c3bcSeric /* make absolutely certain 0, 1, and 2 are in use */ 1007f170942cSeric sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 1008f170942cSeric checkfd012(wbuf); 1009f170942cSeric #endif 10100e3bfef5Seric 1011f170942cSeric if (TrafficLogFile != NULL) 10120e3bfef5Seric { 1013f170942cSeric char **av; 1014f170942cSeric 1015f170942cSeric fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1016f170942cSeric for (av = pv; *av != NULL; av++) 1017f170942cSeric fprintf(TrafficLogFile, " %s", *av); 1018f170942cSeric fprintf(TrafficLogFile, "\n"); 10196fe8c3bcSeric } 10206fe8c3bcSeric 10219c9e68d9Seric /* create a pipe to shove the mail through */ 10229c9e68d9Seric if (pipe(mpvect) < 0) 10239c9e68d9Seric { 10240e3bfef5Seric syserr("%s... openmailer(%s): pipe (to mailer)", 10250e3bfef5Seric e->e_to, m->m_name); 10269c9e68d9Seric if (tTd(11, 1)) 10279c9e68d9Seric printf("openmailer: NULL\n"); 10289c9e68d9Seric rcode = EX_OSERR; 10299c9e68d9Seric goto give_up; 10309c9e68d9Seric } 10319c9e68d9Seric 10329c9e68d9Seric /* if this mailer speaks smtp, create a return pipe */ 10339c9e68d9Seric if (clever && pipe(rpvect) < 0) 10349c9e68d9Seric { 10350e3bfef5Seric syserr("%s... openmailer(%s): pipe (from mailer)", 10360e3bfef5Seric e->e_to, m->m_name); 10379c9e68d9Seric (void) close(mpvect[0]); 10389c9e68d9Seric (void) close(mpvect[1]); 10399c9e68d9Seric if (tTd(11, 1)) 10409c9e68d9Seric printf("openmailer: NULL\n"); 10419c9e68d9Seric rcode = EX_OSERR; 10429c9e68d9Seric goto give_up; 10439c9e68d9Seric } 10449c9e68d9Seric 10459c9e68d9Seric /* 10469c9e68d9Seric ** Actually fork the mailer process. 10479c9e68d9Seric ** DOFORK is clever about retrying. 10489c9e68d9Seric ** 10499c9e68d9Seric ** Dispose of SIGCHLD signal catchers that may be laying 10509c9e68d9Seric ** around so that endmail will get it. 10519c9e68d9Seric */ 10529c9e68d9Seric 10539c9e68d9Seric if (e->e_xfp != NULL) 10549c9e68d9Seric (void) fflush(e->e_xfp); /* for debugging */ 10559c9e68d9Seric (void) fflush(stdout); 10569c9e68d9Seric # ifdef SIGCHLD 10572b9178d3Seric (void) setsignal(SIGCHLD, SIG_DFL); 10589c9e68d9Seric # endif /* SIGCHLD */ 10599c9e68d9Seric DOFORK(FORK); 10609c9e68d9Seric /* pid is set by DOFORK */ 10619c9e68d9Seric if (pid < 0) 10629c9e68d9Seric { 10639c9e68d9Seric /* failure */ 10640e3bfef5Seric syserr("%s... openmailer(%s): cannot fork", 10650e3bfef5Seric e->e_to, m->m_name); 10669c9e68d9Seric (void) close(mpvect[0]); 10679c9e68d9Seric (void) close(mpvect[1]); 10689c9e68d9Seric if (clever) 10699c9e68d9Seric { 10709c9e68d9Seric (void) close(rpvect[0]); 10719c9e68d9Seric (void) close(rpvect[1]); 10729c9e68d9Seric } 10739c9e68d9Seric if (tTd(11, 1)) 10749c9e68d9Seric printf("openmailer: NULL\n"); 10759c9e68d9Seric rcode = EX_OSERR; 10769c9e68d9Seric goto give_up; 10779c9e68d9Seric } 10789c9e68d9Seric else if (pid == 0) 10799c9e68d9Seric { 10809c9e68d9Seric int i; 10819c9e68d9Seric int saveerrno; 10829c9e68d9Seric char **ep; 10839c9e68d9Seric char *env[MAXUSERENVIRON]; 10849c9e68d9Seric extern char **environ; 10859c9e68d9Seric extern int DtableSize; 10869c9e68d9Seric 10879c9e68d9Seric /* child -- set up input & exec mailer */ 10882b9178d3Seric (void) setsignal(SIGINT, SIG_IGN); 10892b9178d3Seric (void) setsignal(SIGHUP, SIG_IGN); 10902b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 10919c9e68d9Seric 10929c9e68d9Seric /* close any other cached connections */ 10939c9e68d9Seric mci_flush(FALSE, mci); 10949c9e68d9Seric 109544f2317fSeric /* reset user and group */ 109644f2317fSeric if (!bitnset(M_RESTR, m->m_flags)) 109744f2317fSeric { 109844f2317fSeric if (ctladdr == NULL || ctladdr->q_uid == 0) 109944f2317fSeric { 110044f2317fSeric (void) initgroups(DefUser, DefGid); 110144f2317fSeric (void) setuid(DefUid); 110244f2317fSeric } 110344f2317fSeric else 110444f2317fSeric { 110544f2317fSeric (void) initgroups(ctladdr->q_ruser? 110644f2317fSeric ctladdr->q_ruser: ctladdr->q_user, 110744f2317fSeric ctladdr->q_gid); 110844f2317fSeric (void) setuid(ctladdr->q_uid); 110944f2317fSeric } 111044f2317fSeric } 111144f2317fSeric 111244f2317fSeric if (tTd(11, 2)) 111344f2317fSeric printf("openmailer: running as r/euid=%d/%d\n", 111444f2317fSeric getuid(), geteuid()); 111544f2317fSeric 1116b986f6aaSeric /* move into some "safe" directory */ 1117b986f6aaSeric if (m->m_execdir != NULL) 1118b986f6aaSeric { 1119b986f6aaSeric char *p, *q; 1120b986f6aaSeric char buf[MAXLINE]; 1121b986f6aaSeric 1122b986f6aaSeric for (p = m->m_execdir; p != NULL; p = q) 1123b986f6aaSeric { 1124b986f6aaSeric q = strchr(p, ':'); 1125b986f6aaSeric if (q != NULL) 1126b986f6aaSeric *q = '\0'; 1127b986f6aaSeric expand(p, buf, &buf[sizeof buf] - 1, e); 1128b986f6aaSeric if (q != NULL) 1129b986f6aaSeric *q++ = ':'; 1130b986f6aaSeric if (tTd(11, 20)) 1131b986f6aaSeric printf("openmailer: trydir %s\n", 1132b986f6aaSeric buf); 1133b986f6aaSeric if (buf[0] != '\0' && chdir(buf) >= 0) 1134b986f6aaSeric break; 1135b986f6aaSeric } 1136b986f6aaSeric } 1137b986f6aaSeric 11389c9e68d9Seric /* arrange to filter std & diag output of command */ 11399c9e68d9Seric if (clever) 11409c9e68d9Seric { 11419c9e68d9Seric (void) close(rpvect[0]); 11426fe8c3bcSeric if (dup2(rpvect[1], STDOUT_FILENO) < 0) 11436fe8c3bcSeric { 11440e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 11450e3bfef5Seric e->e_to, m->m_name, rpvect[1]); 11466fe8c3bcSeric _exit(EX_OSERR); 11476fe8c3bcSeric } 11489c9e68d9Seric (void) close(rpvect[1]); 11499c9e68d9Seric } 11509c9e68d9Seric else if (OpMode == MD_SMTP || HoldErrs) 11519c9e68d9Seric { 11529c9e68d9Seric /* put mailer output in transcript */ 11536fe8c3bcSeric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 11546fe8c3bcSeric { 11550e3bfef5Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 11560e3bfef5Seric e->e_to, m->m_name, 11576fe8c3bcSeric fileno(e->e_xfp)); 11586fe8c3bcSeric _exit(EX_OSERR); 11599c9e68d9Seric } 11606fe8c3bcSeric } 11616fe8c3bcSeric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 11626fe8c3bcSeric { 11630e3bfef5Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr", 11640e3bfef5Seric e->e_to, m->m_name); 11656fe8c3bcSeric _exit(EX_OSERR); 11666fe8c3bcSeric } 11679c9e68d9Seric 11689c9e68d9Seric /* arrange to get standard input */ 11699c9e68d9Seric (void) close(mpvect[1]); 11709c9e68d9Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 11719c9e68d9Seric { 11720e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 11730e3bfef5Seric e->e_to, m->m_name, mpvect[0]); 11749c9e68d9Seric _exit(EX_OSERR); 11759c9e68d9Seric } 11769c9e68d9Seric (void) close(mpvect[0]); 11779c9e68d9Seric 11789c9e68d9Seric /* arrange for all the files to be closed */ 11799c9e68d9Seric for (i = 3; i < DtableSize; i++) 11809c9e68d9Seric { 11819c9e68d9Seric register int j; 118244f2317fSeric 11839c9e68d9Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 11849c9e68d9Seric (void) fcntl(i, F_SETFD, j | 1); 11859c9e68d9Seric } 11869c9e68d9Seric 11879c9e68d9Seric /* set up the mailer environment */ 11889c9e68d9Seric i = 0; 11899c9e68d9Seric env[i++] = "AGENT=sendmail"; 11909c9e68d9Seric for (ep = environ; *ep != NULL; ep++) 11919c9e68d9Seric { 11929c9e68d9Seric if (strncmp(*ep, "TZ=", 3) == 0) 11939c9e68d9Seric env[i++] = *ep; 11949c9e68d9Seric } 11959c9e68d9Seric env[i++] = NULL; 11969c9e68d9Seric 11979c9e68d9Seric /* try to execute the mailer */ 11989c9e68d9Seric execve(m->m_mailer, pv, env); 11999c9e68d9Seric saveerrno = errno; 12009c9e68d9Seric syserr("Cannot exec %s", m->m_mailer); 12017c941fd2Seric if (m == LocalMailer || transienterror(saveerrno)) 12027c941fd2Seric _exit(EX_OSERR); 12039c9e68d9Seric _exit(EX_UNAVAILABLE); 12049c9e68d9Seric } 12059c9e68d9Seric 12069c9e68d9Seric /* 12079c9e68d9Seric ** Set up return value. 12089c9e68d9Seric */ 12099c9e68d9Seric 12109c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 12119c9e68d9Seric bzero((char *) mci, sizeof *mci); 12129c9e68d9Seric mci->mci_mailer = m; 12139c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 12149c9e68d9Seric mci->mci_pid = pid; 12159c9e68d9Seric (void) close(mpvect[0]); 12169c9e68d9Seric mci->mci_out = fdopen(mpvect[1], "w"); 12179c9e68d9Seric if (clever) 12189c9e68d9Seric { 12199c9e68d9Seric (void) close(rpvect[1]); 12209c9e68d9Seric mci->mci_in = fdopen(rpvect[0], "r"); 12219c9e68d9Seric } 12229c9e68d9Seric else 12239c9e68d9Seric { 12249c9e68d9Seric mci->mci_flags |= MCIF_TEMP; 12259c9e68d9Seric mci->mci_in = NULL; 12269c9e68d9Seric } 12279c9e68d9Seric } 12289c9e68d9Seric 12299c9e68d9Seric /* 12309c9e68d9Seric ** If we are in SMTP opening state, send initial protocol. 12319c9e68d9Seric */ 12329c9e68d9Seric 12339c9e68d9Seric if (clever && mci->mci_state != MCIS_CLOSED) 12349c9e68d9Seric { 12359c9e68d9Seric smtpinit(m, mci, e); 12369c9e68d9Seric } 12379c9e68d9Seric if (tTd(11, 1)) 12389c9e68d9Seric { 12399c9e68d9Seric printf("openmailer: "); 12409c9e68d9Seric mci_dump(mci); 12419c9e68d9Seric } 12429c9e68d9Seric 12439c9e68d9Seric if (mci->mci_state != MCIS_OPEN) 1244b31e7f2bSeric { 1245b31e7f2bSeric /* couldn't open the mailer */ 1246b31e7f2bSeric rcode = mci->mci_exitstat; 12472a6bc25bSeric errno = mci->mci_errno; 1248f170942cSeric #ifdef NAMED_BIND 1249f170942cSeric h_errno = mci->mci_herrno; 1250f170942cSeric #endif 1251b31e7f2bSeric if (rcode == EX_OK) 1252b31e7f2bSeric { 1253b31e7f2bSeric /* shouldn't happen */ 125408b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 12556b0f339dSeric rcode, mci->mci_state, firstsig); 1256b31e7f2bSeric rcode = EX_SOFTWARE; 1257b31e7f2bSeric } 1258e9277e33Seric else if (rcode == EX_TEMPFAIL && *curhost != '\0') 125990891494Seric { 126090891494Seric /* try next MX site */ 126190891494Seric goto tryhost; 126290891494Seric } 1263b31e7f2bSeric } 1264b31e7f2bSeric else if (!clever) 1265b31e7f2bSeric { 1266b31e7f2bSeric /* 1267b31e7f2bSeric ** Format and send message. 1268b31e7f2bSeric */ 126915d084d5Seric 1270b31e7f2bSeric putfromline(mci->mci_out, m, e); 1271b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 1272b31e7f2bSeric putline("\n", mci->mci_out, m); 127303c02fdeSeric (*e->e_putbody)(mci->mci_out, m, e, NULL); 1274b31e7f2bSeric 1275b31e7f2bSeric /* get the exit status */ 1276c9be6216Seric rcode = endmailer(mci, e, pv); 1277134746fbSeric } 1278134746fbSeric else 1279b31e7f2bSeric #ifdef SMTP 1280134746fbSeric { 1281b31e7f2bSeric /* 1282b31e7f2bSeric ** Send the MAIL FROM: protocol 1283b31e7f2bSeric */ 128415d084d5Seric 1285b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 1286b31e7f2bSeric if (rcode == EX_OK) 128775889e88Seric { 1288ded0d3daSkarels register char *t = tobuf; 1289ded0d3daSkarels register int i; 1290ded0d3daSkarels 1291588cad61Seric /* send the recipient list */ 129263780dbdSeric tobuf[0] = '\0'; 129375889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 129475889e88Seric { 129563780dbdSeric e->e_to = to->q_paddr; 129615d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 129775889e88Seric { 129883b7ddc9Seric markfailure(e, to, i); 129981161401Seric giveresponse(i, m, mci, e); 130063780dbdSeric } 130175889e88Seric else 130275889e88Seric { 1303911693bfSbostic *t++ = ','; 1304b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 1305b31e7f2bSeric continue; 1306*ee39df61Seric *t = '\0'; 1307588cad61Seric } 1308588cad61Seric } 1309588cad61Seric 131063780dbdSeric /* now send the data */ 131163780dbdSeric if (tobuf[0] == '\0') 1312b31e7f2bSeric { 13139c9e68d9Seric rcode = EX_OK; 131463780dbdSeric e->e_to = NULL; 1315b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 1316b31e7f2bSeric smtprset(m, mci, e); 1317b31e7f2bSeric } 131875889e88Seric else 131975889e88Seric { 132063780dbdSeric e->e_to = tobuf + 1; 132175889e88Seric rcode = smtpdata(m, mci, e); 132263780dbdSeric } 132363780dbdSeric 132463780dbdSeric /* now close the connection */ 1325b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 132615d084d5Seric smtpquit(m, mci, e); 132763780dbdSeric } 13289c9e68d9Seric if (rcode != EX_OK && *curhost != '\0') 13299c9e68d9Seric { 13309c9e68d9Seric /* try next MX site */ 13319c9e68d9Seric goto tryhost; 13329c9e68d9Seric } 1333c579ef51Seric } 1334b31e7f2bSeric #else /* not SMTP */ 1335a05b3449Sbostic { 133608b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 1337845e533cSeric rcode = EX_CONFIG; 1338b31e7f2bSeric goto give_up; 1339a05b3449Sbostic } 1340b31e7f2bSeric #endif /* SMTP */ 1341134746fbSeric #ifdef NAMED_BIND 13422bcc6d2dSeric if (ConfigLevel < 2) 1343912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1344134746fbSeric #endif 13455dfc646bSeric 1346b31e7f2bSeric /* arrange a return receipt if requested */ 13478c13bf07Seric if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags)) 1348b31e7f2bSeric { 1349b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 1350b31e7f2bSeric /* do we want to send back more info? */ 1351b31e7f2bSeric } 1352b31e7f2bSeric 1353c77d1c25Seric /* 135463780dbdSeric ** Do final status disposal. 135563780dbdSeric ** We check for something in tobuf for the SMTP case. 1356c77d1c25Seric ** If we got a temporary failure, arrange to queue the 1357c77d1c25Seric ** addressees. 1358c77d1c25Seric */ 1359c77d1c25Seric 1360b31e7f2bSeric give_up: 136163780dbdSeric if (tobuf[0] != '\0') 136281161401Seric giveresponse(rcode, m, mci, e); 1363772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 1364b31e7f2bSeric { 1365dde5acadSeric if (rcode != EX_OK) 136683b7ddc9Seric markfailure(e, to, rcode); 1367dde5acadSeric else 1368655518ecSeric { 1369dde5acadSeric to->q_flags |= QSENT; 1370655518ecSeric e->e_nsent++; 1371655518ecSeric } 1372b31e7f2bSeric } 1373b31e7f2bSeric 1374b31e7f2bSeric /* 1375b31e7f2bSeric ** Restore state and return. 1376b31e7f2bSeric */ 1377c77d1c25Seric 137835490626Seric errno = 0; 1379588cad61Seric define('g', (char *) NULL, e); 13805826d9d3Seric return (rcode); 138125a99e2eSeric } 13825dfc646bSeric /* 138383b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 138483b7ddc9Seric ** 138583b7ddc9Seric ** Parameters: 138683b7ddc9Seric ** e -- the envelope we are sending. 138783b7ddc9Seric ** q -- the address to mark. 138883b7ddc9Seric ** rcode -- the code signifying the particular failure. 138983b7ddc9Seric ** 139083b7ddc9Seric ** Returns: 139183b7ddc9Seric ** none. 139283b7ddc9Seric ** 139383b7ddc9Seric ** Side Effects: 139483b7ddc9Seric ** marks the address (and possibly the envelope) with the 139583b7ddc9Seric ** failure so that an error will be returned or 139683b7ddc9Seric ** the message will be queued, as appropriate. 139783b7ddc9Seric */ 139883b7ddc9Seric 139983b7ddc9Seric markfailure(e, q, rcode) 140083b7ddc9Seric register ENVELOPE *e; 140183b7ddc9Seric register ADDRESS *q; 140283b7ddc9Seric int rcode; 140383b7ddc9Seric { 140419c47125Seric char buf[MAXLINE]; 140519c47125Seric 140683b7ddc9Seric if (rcode == EX_OK) 140783b7ddc9Seric return; 1408f170942cSeric else if (rcode == EX_TEMPFAIL) 140983b7ddc9Seric q->q_flags |= QQUEUEUP; 1410f170942cSeric else if (rcode != EX_IOERR && rcode != EX_OSERR) 1411f170942cSeric q->q_flags |= QBADADDR; 141283b7ddc9Seric } 141383b7ddc9Seric /* 1414c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 1415c579ef51Seric ** 1416c579ef51Seric ** We should never get fatal errors (e.g., segmentation 1417c579ef51Seric ** violation), so we report those specially. For other 1418c579ef51Seric ** errors, we choose a status message (into statmsg), 1419c579ef51Seric ** and if it represents an error, we print it. 1420c579ef51Seric ** 1421c579ef51Seric ** Parameters: 1422c579ef51Seric ** pid -- pid of mailer. 1423c9be6216Seric ** e -- the current envelope. 1424c9be6216Seric ** pv -- the parameter vector that invoked the mailer 1425c9be6216Seric ** (for error messages). 1426c579ef51Seric ** 1427c579ef51Seric ** Returns: 1428c579ef51Seric ** exit code of mailer. 1429c579ef51Seric ** 1430c579ef51Seric ** Side Effects: 1431c579ef51Seric ** none. 1432c579ef51Seric */ 1433c579ef51Seric 1434c9be6216Seric endmailer(mci, e, pv) 1435b31e7f2bSeric register MCI *mci; 1436c9be6216Seric register ENVELOPE *e; 1437c9be6216Seric char **pv; 1438c579ef51Seric { 1439588cad61Seric int st; 1440c579ef51Seric 144175889e88Seric /* close any connections */ 144275889e88Seric if (mci->mci_in != NULL) 1443c9be6216Seric (void) xfclose(mci->mci_in, pv[0], "mci_in"); 144475889e88Seric if (mci->mci_out != NULL) 1445c9be6216Seric (void) xfclose(mci->mci_out, pv[0], "mci_out"); 144675889e88Seric mci->mci_in = mci->mci_out = NULL; 144775889e88Seric mci->mci_state = MCIS_CLOSED; 144875889e88Seric 144933db8731Seric /* in the IPC case there is nothing to wait for */ 145075889e88Seric if (mci->mci_pid == 0) 145133db8731Seric return (EX_OK); 145233db8731Seric 145333db8731Seric /* wait for the mailer process to die and collect status */ 145475889e88Seric st = waitfor(mci->mci_pid); 1455588cad61Seric if (st == -1) 145678de67c1Seric { 1457c9be6216Seric syserr("endmailer %s: wait", pv[0]); 1458588cad61Seric return (EX_SOFTWARE); 1459c579ef51Seric } 146033db8731Seric 146133db8731Seric /* see if it died a horrid death */ 1462c579ef51Seric if ((st & 0377) != 0) 1463c579ef51Seric { 1464c9be6216Seric syserr("mailer %s died with signal %o", pv[0], st); 1465c9be6216Seric 1466c9be6216Seric /* log the arguments */ 1467c9be6216Seric if (e->e_xfp != NULL) 1468c9be6216Seric { 1469c9be6216Seric register char **av; 1470c9be6216Seric 1471c9be6216Seric fprintf(e->e_xfp, "Arguments:"); 1472c9be6216Seric for (av = pv; *av != NULL; av++) 1473c9be6216Seric fprintf(e->e_xfp, " %s", *av); 1474c9be6216Seric fprintf(e->e_xfp, "\n"); 1475c9be6216Seric } 1476c9be6216Seric 14775f73204aSeric ExitStat = EX_TEMPFAIL; 14785f73204aSeric return (EX_TEMPFAIL); 1479c579ef51Seric } 148033db8731Seric 148133db8731Seric /* normal death -- return status */ 1482588cad61Seric st = (st >> 8) & 0377; 1483588cad61Seric return (st); 1484c579ef51Seric } 1485c579ef51Seric /* 148625a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 148725a99e2eSeric ** 148825a99e2eSeric ** Parameters: 148925a99e2eSeric ** stat -- the status code from the mailer (high byte 149025a99e2eSeric ** only; core dumps must have been taken care of 149125a99e2eSeric ** already). 149281161401Seric ** m -- the mailer info for this mailer. 149381161401Seric ** mci -- the mailer connection info -- can be NULL if the 149481161401Seric ** response is given before the connection is made. 149581161401Seric ** e -- the current envelope. 149625a99e2eSeric ** 149725a99e2eSeric ** Returns: 1498db8841e9Seric ** none. 149925a99e2eSeric ** 150025a99e2eSeric ** Side Effects: 1501c1f9df2cSeric ** Errors may be incremented. 150225a99e2eSeric ** ExitStat may be set. 150325a99e2eSeric */ 150425a99e2eSeric 150581161401Seric giveresponse(stat, m, mci, e) 150625a99e2eSeric int stat; 1507588cad61Seric register MAILER *m; 150881161401Seric register MCI *mci; 1509198d9be0Seric ENVELOPE *e; 151025a99e2eSeric { 15119c16475dSeric register const char *statmsg; 151225a99e2eSeric extern char *SysExMsg[]; 151325a99e2eSeric register int i; 1514d4bd8f0eSbostic extern int N_SysEx; 1515198d9be0Seric char buf[MAXLINE]; 151625a99e2eSeric 151713bbc08cSeric /* 151813bbc08cSeric ** Compute status message from code. 151913bbc08cSeric */ 152013bbc08cSeric 152125a99e2eSeric i = stat - EX__BASE; 1522588cad61Seric if (stat == 0) 15236fe8c3bcSeric { 1524588cad61Seric statmsg = "250 Sent"; 1525ce5531bdSeric if (e->e_statmsg != NULL) 15266fe8c3bcSeric { 1527ce5531bdSeric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 15286fe8c3bcSeric statmsg = buf; 15296fe8c3bcSeric } 15306fe8c3bcSeric } 1531588cad61Seric else if (i < 0 || i > N_SysEx) 1532588cad61Seric { 1533588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1534588cad61Seric stat = EX_UNAVAILABLE; 1535588cad61Seric statmsg = buf; 1536588cad61Seric } 1537198d9be0Seric else if (stat == EX_TEMPFAIL) 1538198d9be0Seric { 15397d55540cSeric (void) strcpy(buf, SysExMsg[i] + 1); 1540d4bd8f0eSbostic #ifdef NAMED_BIND 1541f28da541Smiriam if (h_errno == TRY_AGAIN) 15424d50702aSeric statmsg = errstring(h_errno+E_DNSBASE); 1543f28da541Smiriam else 1544d4bd8f0eSbostic #endif 1545f28da541Smiriam { 15468557d168Seric if (errno != 0) 1547d87e85f3Seric statmsg = errstring(errno); 1548d87e85f3Seric else 1549d87e85f3Seric { 1550d87e85f3Seric #ifdef SMTP 1551d87e85f3Seric extern char SmtpError[]; 1552d87e85f3Seric 1553d87e85f3Seric statmsg = SmtpError; 15546c2c3107Seric #else /* SMTP */ 1555d87e85f3Seric statmsg = NULL; 15566c2c3107Seric #endif /* SMTP */ 1557d87e85f3Seric } 1558f28da541Smiriam } 1559d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1560d87e85f3Seric { 156187c9b3e7Seric (void) strcat(buf, ": "); 1562d87e85f3Seric (void) strcat(buf, statmsg); 15638557d168Seric } 1564198d9be0Seric statmsg = buf; 1565198d9be0Seric } 1566f170942cSeric #ifdef NAMED_BIND 1567f170942cSeric else if (stat == EX_NOHOST && h_errno != 0) 1568f170942cSeric { 15694d50702aSeric statmsg = errstring(h_errno + E_DNSBASE); 1570f170942cSeric (void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg); 1571f170942cSeric statmsg = buf; 1572f170942cSeric } 1573f170942cSeric #endif 157425a99e2eSeric else 1575d87e85f3Seric { 157625a99e2eSeric statmsg = SysExMsg[i]; 15777d55540cSeric if (*statmsg++ == ':') 15787d55540cSeric { 15797d55540cSeric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 15807d55540cSeric statmsg = buf; 15817d55540cSeric } 1582d87e85f3Seric } 1583588cad61Seric 1584588cad61Seric /* 1585588cad61Seric ** Print the message as appropriate 1586588cad61Seric */ 1587588cad61Seric 1588198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 1589e12d03eeSeric message(&statmsg[4], errstring(errno)); 159025a99e2eSeric else 159125a99e2eSeric { 1592c1f9df2cSeric Errors++; 1593e12d03eeSeric usrerr(statmsg, errstring(errno)); 159425a99e2eSeric } 159525a99e2eSeric 159625a99e2eSeric /* 159725a99e2eSeric ** Final cleanup. 159825a99e2eSeric ** Log a record of the transaction. Compute the new 159925a99e2eSeric ** ExitStat -- if we already had an error, stick with 160025a99e2eSeric ** that. 160125a99e2eSeric */ 160225a99e2eSeric 16032f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 160481161401Seric logdelivery(m, mci, &statmsg[4], e); 1605eb238f8cSeric 1606eb238f8cSeric if (stat != EX_TEMPFAIL) 1607eb238f8cSeric setstat(stat); 1608198d9be0Seric if (stat != EX_OK) 1609198d9be0Seric { 1610198d9be0Seric if (e->e_message != NULL) 1611198d9be0Seric free(e->e_message); 1612198d9be0Seric e->e_message = newstr(&statmsg[4]); 1613198d9be0Seric } 16148557d168Seric errno = 0; 1615d4bd8f0eSbostic #ifdef NAMED_BIND 1616f28da541Smiriam h_errno = 0; 1617d4bd8f0eSbostic #endif 1618eb238f8cSeric } 1619eb238f8cSeric /* 1620eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1621eb238f8cSeric ** 1622eb238f8cSeric ** Parameters: 162381161401Seric ** m -- the mailer info. Can be NULL for initial queue. 162481161401Seric ** mci -- the mailer connection info -- can be NULL if the 162581161401Seric ** log is occuring when no connection is active. 162681161401Seric ** stat -- the message to print for the status. 162781161401Seric ** e -- the current envelope. 1628eb238f8cSeric ** 1629eb238f8cSeric ** Returns: 1630eb238f8cSeric ** none 1631eb238f8cSeric ** 1632eb238f8cSeric ** Side Effects: 1633eb238f8cSeric ** none 1634eb238f8cSeric */ 1635eb238f8cSeric 163681161401Seric logdelivery(m, mci, stat, e) 163781161401Seric MAILER *m; 163881161401Seric register MCI *mci; 1639eb238f8cSeric char *stat; 1640b31e7f2bSeric register ENVELOPE *e; 16415cf56be3Seric { 1642eb238f8cSeric # ifdef LOG 1643d6acf3eeSeric char buf[512]; 16449507d1f9Seric 164548ed5d33Seric (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 164681161401Seric 164748ed5d33Seric if (m != NULL) 164871ff6caaSeric { 164948ed5d33Seric (void) strcat(buf, ", mailer="); 165048ed5d33Seric (void) strcat(buf, m->m_name); 165171ff6caaSeric } 165248ed5d33Seric 165348ed5d33Seric if (mci != NULL && mci->mci_host != NULL) 165471ff6caaSeric { 165571ff6caaSeric # ifdef DAEMON 1656e2f2f828Seric extern SOCKADDR CurHostAddr; 165748ed5d33Seric # endif 165871ff6caaSeric 165948ed5d33Seric (void) strcat(buf, ", relay="); 166048ed5d33Seric (void) strcat(buf, mci->mci_host); 166148ed5d33Seric 166248ed5d33Seric # ifdef DAEMON 166348ed5d33Seric (void) strcat(buf, " ("); 1664e2f2f828Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 166548ed5d33Seric (void) strcat(buf, ")"); 166671ff6caaSeric # endif 166771ff6caaSeric } 16689507d1f9Seric else 166948ed5d33Seric { 167048ed5d33Seric char *p = macvalue('h', e); 16719507d1f9Seric 167248ed5d33Seric if (p != NULL && p[0] != '\0') 167348ed5d33Seric { 167448ed5d33Seric (void) strcat(buf, ", relay="); 167548ed5d33Seric (void) strcat(buf, p); 167648ed5d33Seric } 167748ed5d33Seric } 1678d6acf3eeSeric 1679d6acf3eeSeric syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1680d6acf3eeSeric e->e_id, e->e_to, buf, stat); 16816c2c3107Seric # endif /* LOG */ 168225a99e2eSeric } 168325a99e2eSeric /* 168451552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 168525a99e2eSeric ** 168651552439Seric ** This can be made an arbitrary message separator by changing $l 168751552439Seric ** 16889b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 16899b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 16909b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 16919b6c17a6Seric ** this kind of antique garbage???? 169225a99e2eSeric ** 169325a99e2eSeric ** Parameters: 169451552439Seric ** fp -- the file to output to. 169551552439Seric ** m -- the mailer describing this entry. 169625a99e2eSeric ** 169725a99e2eSeric ** Returns: 169851552439Seric ** none 169925a99e2eSeric ** 170025a99e2eSeric ** Side Effects: 170151552439Seric ** outputs some text to fp. 170225a99e2eSeric */ 170325a99e2eSeric 1704b31e7f2bSeric putfromline(fp, m, e) 170551552439Seric register FILE *fp; 170651552439Seric register MAILER *m; 1707b31e7f2bSeric ENVELOPE *e; 170825a99e2eSeric { 17092bc47524Seric char *template = "\201l\n"; 171051552439Seric char buf[MAXLINE]; 171125a99e2eSeric 171257fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 171351552439Seric return; 171413bbc08cSeric 17152c7e1b8dSeric # ifdef UGLYUUCP 171657fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 171774b6e67bSeric { 1718ea09d6edSeric char *bang; 1719ea09d6edSeric char xbuf[MAXLINE]; 172074b6e67bSeric 1721ee4b0922Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 17226c2c3107Seric bang = strchr(buf, '!'); 172374b6e67bSeric if (bang == NULL) 172434fcca25Seric { 172534fcca25Seric errno = 0; 172634fcca25Seric syserr("554 No ! in UUCP From address! (%s given)", buf); 172734fcca25Seric } 172874b6e67bSeric else 1729588cad61Seric { 1730ea09d6edSeric *bang++ = '\0'; 17312bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1732ea09d6edSeric template = xbuf; 173374b6e67bSeric } 1734588cad61Seric } 17356c2c3107Seric # endif /* UGLYUUCP */ 1736b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 173777b52738Seric putline(buf, fp, m); 1738bc6e2962Seric } 1739bc6e2962Seric /* 174051552439Seric ** PUTBODY -- put the body of a message. 174151552439Seric ** 174251552439Seric ** Parameters: 174351552439Seric ** fp -- file to output onto. 174477b52738Seric ** m -- a mailer descriptor to control output format. 17459a6a5f55Seric ** e -- the envelope to put out. 174603c02fdeSeric ** separator -- if non-NULL, a message separator that must 174703c02fdeSeric ** not be permitted in the resulting message. 174851552439Seric ** 174951552439Seric ** Returns: 175051552439Seric ** none. 175151552439Seric ** 175251552439Seric ** Side Effects: 175351552439Seric ** The message is written onto fp. 175451552439Seric */ 175551552439Seric 175603c02fdeSeric putbody(fp, m, e, separator) 175751552439Seric FILE *fp; 1758588cad61Seric MAILER *m; 17599a6a5f55Seric register ENVELOPE *e; 176003c02fdeSeric char *separator; 176151552439Seric { 176277b52738Seric char buf[MAXLINE]; 176351552439Seric 176451552439Seric /* 176551552439Seric ** Output the body of the message 176651552439Seric */ 176751552439Seric 17689a6a5f55Seric if (e->e_dfp == NULL) 176951552439Seric { 17709a6a5f55Seric if (e->e_df != NULL) 17719a6a5f55Seric { 17729a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 17739a6a5f55Seric if (e->e_dfp == NULL) 17748f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 1775e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 17769a6a5f55Seric } 17779a6a5f55Seric else 177877b52738Seric putline("<<< No Message Collected >>>", fp, m); 17799a6a5f55Seric } 17809a6a5f55Seric if (e->e_dfp != NULL) 17819a6a5f55Seric { 17829a6a5f55Seric rewind(e->e_dfp); 178377b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 178424fc8aeeSeric { 178524fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1786d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 17873462ad9eSeric (void) putc('>', fp); 178803c02fdeSeric if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 178903c02fdeSeric { 179003c02fdeSeric /* possible separator */ 179103c02fdeSeric int sl = strlen(separator); 179203c02fdeSeric 179303c02fdeSeric if (strncmp(&buf[2], separator, sl) == 0) 179403c02fdeSeric (void) putc(' ', fp); 179503c02fdeSeric } 179677b52738Seric putline(buf, fp, m); 179724fc8aeeSeric } 179851552439Seric 17999a6a5f55Seric if (ferror(e->e_dfp)) 180051552439Seric { 180151552439Seric syserr("putbody: read error"); 180251552439Seric ExitStat = EX_IOERR; 180351552439Seric } 180451552439Seric } 180551552439Seric 18060890ba1fSeric /* some mailers want extra blank line at end of message */ 18070890ba1fSeric if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 18080890ba1fSeric putline("", fp, m); 18090890ba1fSeric 181051552439Seric (void) fflush(fp); 181151552439Seric if (ferror(fp) && errno != EPIPE) 181251552439Seric { 181351552439Seric syserr("putbody: write error"); 181451552439Seric ExitStat = EX_IOERR; 181551552439Seric } 181651552439Seric errno = 0; 181725a99e2eSeric } 181825a99e2eSeric /* 181925a99e2eSeric ** MAILFILE -- Send a message to a file. 182025a99e2eSeric ** 1821f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1822f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1823f129ec7dSeric ** rather than the real user. Obviously, this only works if 1824f129ec7dSeric ** sendmail runs as root. 1825f129ec7dSeric ** 1826588cad61Seric ** This could be done as a subordinate mailer, except that it 1827588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1828588cad61Seric ** view this as being sufficiently important as to include it 1829588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1830588cad61Seric ** to create another process plus some pipes to save the message. 1831588cad61Seric ** 183225a99e2eSeric ** Parameters: 183325a99e2eSeric ** filename -- the name of the file to send to. 18346259796dSeric ** ctladdr -- the controlling address header -- includes 18356259796dSeric ** the userid/groupid to be when sending. 183625a99e2eSeric ** 183725a99e2eSeric ** Returns: 183825a99e2eSeric ** The exit code associated with the operation. 183925a99e2eSeric ** 184025a99e2eSeric ** Side Effects: 184125a99e2eSeric ** none. 184225a99e2eSeric */ 184325a99e2eSeric 1844b31e7f2bSeric mailfile(filename, ctladdr, e) 184525a99e2eSeric char *filename; 18466259796dSeric ADDRESS *ctladdr; 1847b31e7f2bSeric register ENVELOPE *e; 184825a99e2eSeric { 184925a99e2eSeric register FILE *f; 185032d19d43Seric register int pid; 185115d084d5Seric int mode; 185225a99e2eSeric 1853671745f3Seric if (tTd(11, 1)) 1854671745f3Seric { 1855671745f3Seric printf("mailfile %s\n ctladdr=", filename); 1856671745f3Seric printaddr(ctladdr, FALSE); 1857671745f3Seric } 1858671745f3Seric 1859f170942cSeric if (e->e_xfp != NULL) 1860f170942cSeric fflush(e->e_xfp); 1861f170942cSeric 186232d19d43Seric /* 186332d19d43Seric ** Fork so we can change permissions here. 186432d19d43Seric ** Note that we MUST use fork, not vfork, because of 186532d19d43Seric ** the complications of calling subroutines, etc. 186632d19d43Seric */ 186732d19d43Seric 186832d19d43Seric DOFORK(fork); 186932d19d43Seric 187032d19d43Seric if (pid < 0) 187132d19d43Seric return (EX_OSERR); 187232d19d43Seric else if (pid == 0) 187332d19d43Seric { 187432d19d43Seric /* child -- actually write to file */ 1875f129ec7dSeric struct stat stb; 1876f129ec7dSeric 18772b9178d3Seric (void) setsignal(SIGINT, SIG_DFL); 18782b9178d3Seric (void) setsignal(SIGHUP, SIG_DFL); 18792b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 18803462ad9eSeric (void) umask(OldUmask); 188195f16dc0Seric 1882f129ec7dSeric if (stat(filename, &stb) < 0) 18833a98e7eaSeric stb.st_mode = FileMode; 188415d084d5Seric mode = stb.st_mode; 188595f16dc0Seric 188695f16dc0Seric /* limit the errors to those actually caused in the child */ 188795f16dc0Seric errno = 0; 188895f16dc0Seric ExitStat = EX_OK; 188995f16dc0Seric 1890f129ec7dSeric if (bitset(0111, stb.st_mode)) 1891f129ec7dSeric exit(EX_CANTCREAT); 189203827b5fSeric if (ctladdr == NULL) 18938f9146b0Srick ctladdr = &e->e_from; 189415d084d5Seric else 189515d084d5Seric { 189615d084d5Seric /* ignore setuid and setgid bits */ 189715d084d5Seric mode &= ~(S_ISGID|S_ISUID); 189815d084d5Seric } 189915d084d5Seric 19008f9146b0Srick /* we have to open the dfile BEFORE setuid */ 19018f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 19028f9146b0Srick { 19038f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 190495f16dc0Seric if (e->e_dfp == NULL) 190595f16dc0Seric { 19068f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 1907e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 19088f9146b0Srick } 19098f9146b0Srick } 19108f9146b0Srick 191115d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1912e36b99e2Seric { 191395f16dc0Seric if (ctladdr->q_uid == 0) 191495f16dc0Seric { 1915898a126bSbostic (void) initgroups(DefUser, DefGid); 191695f16dc0Seric } 191795f16dc0Seric else 191895f16dc0Seric { 1919898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 1920898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 1921898a126bSbostic ctladdr->q_gid); 1922898a126bSbostic } 1923e36b99e2Seric } 192415d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1925e36b99e2Seric { 1926e36b99e2Seric if (ctladdr->q_uid == 0) 1927e36b99e2Seric (void) setuid(DefUid); 1928e36b99e2Seric else 19296259796dSeric (void) setuid(ctladdr->q_uid); 1930e36b99e2Seric } 193195f16dc0Seric FileName = filename; 193295f16dc0Seric LineNumber = 0; 19333a98e7eaSeric f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 193425a99e2eSeric if (f == NULL) 193595f16dc0Seric { 193608b25121Seric message("554 cannot open"); 193732d19d43Seric exit(EX_CANTCREAT); 193895f16dc0Seric } 193925a99e2eSeric 19400331ce05Seric putfromline(f, FileMailer, e); 19410331ce05Seric (*e->e_puthdr)(f, FileMailer, e); 19420331ce05Seric putline("\n", f, FileMailer); 194303c02fdeSeric (*e->e_putbody)(f, FileMailer, e, NULL); 19440331ce05Seric putline("\n", f, FileMailer); 194595f16dc0Seric if (ferror(f)) 194695f16dc0Seric { 194708b25121Seric message("451 I/O error"); 194895f16dc0Seric setstat(EX_IOERR); 194995f16dc0Seric } 1950ee4b0922Seric (void) xfclose(f, "mailfile", filename); 195132d19d43Seric (void) fflush(stdout); 1952e36b99e2Seric 195327628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1954c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 195595f16dc0Seric exit(ExitStat); 195613bbc08cSeric /*NOTREACHED*/ 195732d19d43Seric } 195832d19d43Seric else 195932d19d43Seric { 196032d19d43Seric /* parent -- wait for exit status */ 1961588cad61Seric int st; 196232d19d43Seric 1963588cad61Seric st = waitfor(pid); 1964588cad61Seric if ((st & 0377) != 0) 1965588cad61Seric return (EX_UNAVAILABLE); 1966588cad61Seric else 1967588cad61Seric return ((st >> 8) & 0377); 19688f9146b0Srick /*NOTREACHED*/ 196932d19d43Seric } 197025a99e2eSeric } 1971ea4dc939Seric /* 1972e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 1973e103b48fSeric ** 1974e103b48fSeric ** The signature describes how we are going to send this -- it 1975e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 1976e103b48fSeric ** an ordered list of MX hosts. 1977e103b48fSeric ** 1978e103b48fSeric ** Parameters: 1979e103b48fSeric ** m -- the mailer describing this host. 1980e103b48fSeric ** host -- the host name. 1981e103b48fSeric ** e -- the current envelope. 1982e103b48fSeric ** 1983e103b48fSeric ** Returns: 1984e103b48fSeric ** The signature for this host. 1985e103b48fSeric ** 1986e103b48fSeric ** Side Effects: 1987e103b48fSeric ** Can tweak the symbol table. 1988e103b48fSeric */ 1989e103b48fSeric 1990e103b48fSeric char * 1991e103b48fSeric hostsignature(m, host, e) 1992e103b48fSeric register MAILER *m; 1993e103b48fSeric char *host; 1994e103b48fSeric ENVELOPE *e; 1995e103b48fSeric { 1996e103b48fSeric register char *p; 1997e103b48fSeric register STAB *s; 1998e103b48fSeric int i; 1999e103b48fSeric int len; 2000e103b48fSeric #ifdef NAMED_BIND 2001e103b48fSeric int nmx; 2002e103b48fSeric auto int rcode; 2003bafdc4e5Seric char *hp; 2004bafdc4e5Seric char *endp; 2005516782b4Seric int oldoptions; 2006e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 2007e103b48fSeric #endif 2008e103b48fSeric 2009e103b48fSeric /* 2010e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 2011e103b48fSeric */ 2012e103b48fSeric 2013e103b48fSeric p = m->m_mailer; 2014e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2015e103b48fSeric { 2016e103b48fSeric /* just an ordinary mailer */ 2017e103b48fSeric return host; 2018e103b48fSeric } 2019e103b48fSeric 2020e103b48fSeric /* 2021e103b48fSeric ** If it is a numeric address, just return it. 2022e103b48fSeric */ 2023e103b48fSeric 2024e103b48fSeric if (host[0] == '[') 2025e103b48fSeric return host; 2026e103b48fSeric 2027e103b48fSeric /* 2028e103b48fSeric ** Look it up in the symbol table. 2029e103b48fSeric */ 2030e103b48fSeric 2031e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 2032e103b48fSeric if (s->s_hostsig != NULL) 2033e103b48fSeric return s->s_hostsig; 2034e103b48fSeric 2035e103b48fSeric /* 2036e103b48fSeric ** Not already there -- create a signature. 2037e103b48fSeric */ 2038e103b48fSeric 2039e103b48fSeric #ifdef NAMED_BIND 2040516782b4Seric if (ConfigLevel < 2) 2041516782b4Seric { 2042516782b4Seric oldoptions = _res.options; 2043516782b4Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2044516782b4Seric } 2045516782b4Seric 2046bafdc4e5Seric for (hp = host; hp != NULL; hp = endp) 2047bafdc4e5Seric { 2048bafdc4e5Seric endp = strchr(hp, ':'); 2049bafdc4e5Seric if (endp != NULL) 2050bafdc4e5Seric *endp = '\0'; 2051bafdc4e5Seric 20527bf809e6Seric nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 20537d55540cSeric 2054e103b48fSeric if (nmx <= 0) 2055e103b48fSeric { 2056e103b48fSeric register MCI *mci; 2057e103b48fSeric extern int errno; 2058e103b48fSeric 2059e103b48fSeric /* update the connection info for this host */ 2060bafdc4e5Seric mci = mci_get(hp, m); 2061e103b48fSeric mci->mci_exitstat = rcode; 2062e103b48fSeric mci->mci_errno = errno; 2063f170942cSeric #ifdef NAMED_BIND 2064f170942cSeric mci->mci_herrno = h_errno; 2065f170942cSeric #endif 2066e103b48fSeric 2067e103b48fSeric /* and return the original host name as the signature */ 2068bafdc4e5Seric nmx = 1; 2069bafdc4e5Seric mxhosts[0] = hp; 2070e103b48fSeric } 2071e103b48fSeric 2072e103b48fSeric len = 0; 2073e103b48fSeric for (i = 0; i < nmx; i++) 2074e103b48fSeric { 2075e103b48fSeric len += strlen(mxhosts[i]) + 1; 2076e103b48fSeric } 2077bafdc4e5Seric if (s->s_hostsig != NULL) 2078bafdc4e5Seric len += strlen(s->s_hostsig) + 1; 2079bafdc4e5Seric p = xalloc(len); 2080bafdc4e5Seric if (s->s_hostsig != NULL) 2081bafdc4e5Seric { 2082bafdc4e5Seric (void) strcpy(p, s->s_hostsig); 2083bafdc4e5Seric free(s->s_hostsig); 2084bafdc4e5Seric s->s_hostsig = p; 2085bafdc4e5Seric p += strlen(p); 2086bafdc4e5Seric *p++ = ':'; 2087bafdc4e5Seric } 2088bafdc4e5Seric else 2089bafdc4e5Seric s->s_hostsig = p; 2090e103b48fSeric for (i = 0; i < nmx; i++) 2091e103b48fSeric { 2092e103b48fSeric if (i != 0) 2093e103b48fSeric *p++ = ':'; 2094e103b48fSeric strcpy(p, mxhosts[i]); 2095e103b48fSeric p += strlen(p); 2096e103b48fSeric } 2097bafdc4e5Seric if (endp != NULL) 2098bafdc4e5Seric *endp++ = ':'; 2099bafdc4e5Seric } 2100e103b48fSeric makelower(s->s_hostsig); 2101516782b4Seric if (ConfigLevel < 2) 2102516782b4Seric _res.options = oldoptions; 2103e103b48fSeric #else 2104e103b48fSeric /* not using BIND -- the signature is just the host name */ 2105e103b48fSeric s->s_hostsig = host; 2106e103b48fSeric #endif 2107e103b48fSeric if (tTd(17, 1)) 2108e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2109e103b48fSeric return s->s_hostsig; 2110e103b48fSeric } 2111