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*bf9bc890Seric static char sccsid[] = "@(#)deliver.c 8.19 (Berkeley) 09/02/93"; 11e70a7521Sbostic #endif /* not lint */ 124227346bSdist 13fcde9cc8Sbostic #include "sendmail.h" 14f28da541Smiriam #include <netdb.h> 15911693bfSbostic #include <errno.h> 16*bf9bc890Seric #include <sys/wait.h> 17134746fbSeric #ifdef NAMED_BIND 18912a731aSbostic #include <arpa/nameser.h> 19912a731aSbostic #include <resolv.h> 20f170942cSeric 21f170942cSeric extern int h_errno; 22134746fbSeric #endif 2325a99e2eSeric 24*bf9bc890Seric #ifndef WEXITSTATUS 25*bf9bc890Seric # define WEXITSTATUS(st) (((st) >> 8) & 0377) 26*bf9bc890Seric #endif 27*bf9bc890Seric 2825a99e2eSeric /* 299c9e68d9Seric ** SENDALL -- actually send all the messages. 309c9e68d9Seric ** 319c9e68d9Seric ** Parameters: 329c9e68d9Seric ** e -- the envelope to send. 339c9e68d9Seric ** mode -- the delivery mode to use. If SM_DEFAULT, use 349c9e68d9Seric ** the current e->e_sendmode. 359c9e68d9Seric ** 369c9e68d9Seric ** Returns: 379c9e68d9Seric ** none. 389c9e68d9Seric ** 399c9e68d9Seric ** Side Effects: 409c9e68d9Seric ** Scans the send lists and sends everything it finds. 419c9e68d9Seric ** Delivers any appropriate error messages. 429c9e68d9Seric ** If we are running in a non-interactive mode, takes the 439c9e68d9Seric ** appropriate action. 449c9e68d9Seric */ 459c9e68d9Seric 469c9e68d9Seric sendall(e, mode) 479c9e68d9Seric ENVELOPE *e; 489c9e68d9Seric char mode; 499c9e68d9Seric { 509c9e68d9Seric register ADDRESS *q; 519c9e68d9Seric char *owner; 529c9e68d9Seric int otherowners; 539c9e68d9Seric register ENVELOPE *ee; 549c9e68d9Seric ENVELOPE *splitenv = NULL; 556103d9b4Seric bool announcequeueup; 569c9e68d9Seric 577880f3e4Seric /* 587880f3e4Seric ** If we have had global, fatal errors, don't bother sending 597880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 607880f3e4Seric ** (e.g., a single address failing) will still cause the other 617880f3e4Seric ** addresses to be sent. 627880f3e4Seric */ 637880f3e4Seric 647880f3e4Seric if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 65896b16f6Seric { 66896b16f6Seric e->e_flags |= EF_CLRQUEUE; 67896b16f6Seric return; 68896b16f6Seric } 69896b16f6Seric 709c9e68d9Seric /* determine actual delivery mode */ 719c9e68d9Seric if (mode == SM_DEFAULT) 729c9e68d9Seric { 739c9e68d9Seric mode = e->e_sendmode; 749c9e68d9Seric if (mode != SM_VERIFY && 759c9e68d9Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 769c9e68d9Seric mode = SM_QUEUE; 776103d9b4Seric announcequeueup = mode == SM_QUEUE; 789c9e68d9Seric } 796103d9b4Seric else 806103d9b4Seric announcequeueup = FALSE; 819c9e68d9Seric 829c9e68d9Seric if (tTd(13, 1)) 839c9e68d9Seric { 840e484fe5Seric printf("\n===== SENDALL: mode %c, id %s, e_from ", 850e484fe5Seric mode, e->e_id); 869c9e68d9Seric printaddr(&e->e_from, FALSE); 879c9e68d9Seric printf("sendqueue:\n"); 889c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 899c9e68d9Seric } 909c9e68d9Seric 919c9e68d9Seric /* 929c9e68d9Seric ** Do any preprocessing necessary for the mode we are running. 939c9e68d9Seric ** Check to make sure the hop count is reasonable. 949c9e68d9Seric ** Delete sends to the sender in mailing lists. 959c9e68d9Seric */ 969c9e68d9Seric 979c9e68d9Seric CurEnv = e; 989c9e68d9Seric 999c9e68d9Seric if (e->e_hopcount > MaxHopCount) 1009c9e68d9Seric { 1019c9e68d9Seric errno = 0; 1029c9e68d9Seric syserr("554 too many hops %d (%d max): from %s, to %s", 1039c9e68d9Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 1049c9e68d9Seric e->e_sendqueue->q_paddr); 1059c9e68d9Seric return; 1069c9e68d9Seric } 1079c9e68d9Seric 108b8004690Seric /* 109b8004690Seric ** Do sender deletion. 110b8004690Seric ** 111b8004690Seric ** If the sender has the QQUEUEUP flag set, skip this. 112b8004690Seric ** This can happen if the name server is hosed when you 113b8004690Seric ** are trying to send mail. The result is that the sender 114b8004690Seric ** is instantiated in the queue as a recipient. 115b8004690Seric */ 116b8004690Seric 117e76a6a8fSeric if (!bitset(EF_METOO, e->e_flags) && 118e76a6a8fSeric !bitset(QQUEUEUP, e->e_from.q_flags)) 1199c9e68d9Seric { 1209c9e68d9Seric if (tTd(13, 5)) 1219c9e68d9Seric { 1229c9e68d9Seric printf("sendall: QDONTSEND "); 1239c9e68d9Seric printaddr(&e->e_from, FALSE); 1249c9e68d9Seric } 1259c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 1269c9e68d9Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 1279c9e68d9Seric } 1289c9e68d9Seric 129ce5531bdSeric /* 130ce5531bdSeric ** Handle alias owners. 131ce5531bdSeric ** 132ce5531bdSeric ** We scan up the q_alias chain looking for owners. 133ce5531bdSeric ** We discard owners that are the same as the return path. 134ce5531bdSeric */ 135ce5531bdSeric 136ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 137ce5531bdSeric { 138ce5531bdSeric register struct address *a; 139ce5531bdSeric 140ce5531bdSeric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 141ce5531bdSeric continue; 142ce5531bdSeric if (a != NULL) 143ce5531bdSeric q->q_owner = a->q_owner; 144ce5531bdSeric 145ce5531bdSeric if (q->q_owner != NULL && 146ce5531bdSeric !bitset(QDONTSEND, q->q_flags) && 147ce5531bdSeric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 148ce5531bdSeric q->q_owner = NULL; 149ce5531bdSeric } 150ce5531bdSeric 151ce5531bdSeric owner = ""; 152ce5531bdSeric otherowners = 1; 153ce5531bdSeric while (owner != NULL && otherowners > 0) 154ce5531bdSeric { 155ce5531bdSeric owner = NULL; 156ce5531bdSeric otherowners = 0; 157ce5531bdSeric 158ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 159ce5531bdSeric { 160ce5531bdSeric if (bitset(QDONTSEND, q->q_flags)) 161ce5531bdSeric continue; 162ce5531bdSeric 163ce5531bdSeric if (q->q_owner != NULL) 164ce5531bdSeric { 165ce5531bdSeric if (owner == NULL) 166ce5531bdSeric owner = q->q_owner; 167ce5531bdSeric else if (owner != q->q_owner) 168ce5531bdSeric { 169ce5531bdSeric if (strcmp(owner, q->q_owner) == 0) 170ce5531bdSeric { 171ce5531bdSeric /* make future comparisons cheap */ 172ce5531bdSeric q->q_owner = owner; 173ce5531bdSeric } 174ce5531bdSeric else 175ce5531bdSeric { 176ce5531bdSeric otherowners++; 177ce5531bdSeric } 178ce5531bdSeric owner = q->q_owner; 179ce5531bdSeric } 180ce5531bdSeric } 181ce5531bdSeric else 182ce5531bdSeric { 183ce5531bdSeric otherowners++; 184ce5531bdSeric } 185ce5531bdSeric } 186ce5531bdSeric 187ce5531bdSeric if (owner != NULL && otherowners > 0) 188ce5531bdSeric { 189ce5531bdSeric extern HDR *copyheader(); 190ce5531bdSeric extern ADDRESS *copyqueue(); 191ce5531bdSeric 192ce5531bdSeric /* 193ce5531bdSeric ** Split this envelope into two. 194ce5531bdSeric */ 195ce5531bdSeric 196ce5531bdSeric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 197ce5531bdSeric *ee = *e; 198ce5531bdSeric ee->e_id = NULL; 199ce5531bdSeric (void) queuename(ee, '\0'); 200ce5531bdSeric 201ce5531bdSeric if (tTd(13, 1)) 202ce5531bdSeric printf("sendall: split %s into %s\n", 203ce5531bdSeric e->e_id, ee->e_id); 204ce5531bdSeric 205ce5531bdSeric ee->e_header = copyheader(e->e_header); 206ce5531bdSeric ee->e_sendqueue = copyqueue(e->e_sendqueue); 207ce5531bdSeric ee->e_errorqueue = copyqueue(e->e_errorqueue); 208ce5531bdSeric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS); 209ce5531bdSeric setsender(owner, ee, NULL, TRUE); 210ce5531bdSeric if (tTd(13, 5)) 211ce5531bdSeric { 212ce5531bdSeric printf("sendall(split): QDONTSEND "); 213ce5531bdSeric printaddr(&ee->e_from, FALSE); 214ce5531bdSeric } 215ce5531bdSeric ee->e_from.q_flags |= QDONTSEND; 216ce5531bdSeric ee->e_dfp = NULL; 217ce5531bdSeric ee->e_xfp = NULL; 218ce5531bdSeric ee->e_lockfp = NULL; 219ce5531bdSeric ee->e_df = NULL; 220ce5531bdSeric ee->e_errormode = EM_MAIL; 221ce5531bdSeric ee->e_sibling = splitenv; 222ce5531bdSeric splitenv = ee; 223ce5531bdSeric 224ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 225ce5531bdSeric if (q->q_owner == owner) 226ce5531bdSeric q->q_flags |= QDONTSEND; 227ce5531bdSeric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 228ce5531bdSeric if (q->q_owner != owner) 229ce5531bdSeric q->q_flags |= QDONTSEND; 230ce5531bdSeric 231ce5531bdSeric if (e->e_df != NULL && mode != SM_VERIFY) 232ce5531bdSeric { 233ce5531bdSeric ee->e_dfp = NULL; 234da662164Seric ee->e_df = queuename(ee, 'd'); 235da662164Seric ee->e_df = newstr(ee->e_df); 236ce5531bdSeric if (link(e->e_df, ee->e_df) < 0) 237ce5531bdSeric { 238ce5531bdSeric syserr("sendall: link(%s, %s)", 239ce5531bdSeric e->e_df, ee->e_df); 240ce5531bdSeric } 241ce5531bdSeric } 242ce5531bdSeric 243ce5531bdSeric if (mode != SM_VERIFY) 244ce5531bdSeric openxscript(ee); 245ce5531bdSeric #ifdef LOG 246ce5531bdSeric if (LogLevel > 4) 247ce5531bdSeric syslog(LOG_INFO, "%s: clone %s", 248ce5531bdSeric ee->e_id, e->e_id); 249ce5531bdSeric #endif 250ce5531bdSeric } 251ce5531bdSeric } 252ce5531bdSeric 253ce5531bdSeric if (owner != NULL) 254ce5531bdSeric { 255ce5531bdSeric setsender(owner, e, NULL, TRUE); 256ce5531bdSeric if (tTd(13, 5)) 257ce5531bdSeric { 258ce5531bdSeric printf("sendall(owner): QDONTSEND "); 259ce5531bdSeric printaddr(&e->e_from, FALSE); 260ce5531bdSeric } 261ce5531bdSeric e->e_from.q_flags |= QDONTSEND; 262ce5531bdSeric e->e_errormode = EM_MAIL; 263ce5531bdSeric } 264ce5531bdSeric 265c1ac89b1Seric # ifdef QUEUE 266c1ac89b1Seric if ((mode == SM_QUEUE || mode == SM_FORK || 267c1ac89b1Seric (mode != SM_VERIFY && SuperSafe)) && 268c1ac89b1Seric !bitset(EF_INQUEUE, e->e_flags)) 269c1ac89b1Seric { 270c1ac89b1Seric /* be sure everything is instantiated in the queue */ 2716103d9b4Seric queueup(e, TRUE, announcequeueup); 272ce5531bdSeric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 2736103d9b4Seric queueup(ee, TRUE, announcequeueup); 274c1ac89b1Seric } 275c1ac89b1Seric #endif /* QUEUE */ 276c1ac89b1Seric 277ce5531bdSeric if (splitenv != NULL) 278ce5531bdSeric { 279ce5531bdSeric if (tTd(13, 1)) 280ce5531bdSeric { 281ce5531bdSeric printf("\nsendall: Split queue; remaining queue:\n"); 282ce5531bdSeric printaddr(e->e_sendqueue, TRUE); 283ce5531bdSeric } 284ce5531bdSeric 285ce5531bdSeric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 286ce5531bdSeric { 287ce5531bdSeric CurEnv = ee; 288ce5531bdSeric sendenvelope(ee, mode); 289ce5531bdSeric } 290ce5531bdSeric 291ce5531bdSeric CurEnv = e; 292ce5531bdSeric } 293ce5531bdSeric sendenvelope(e, mode); 294ce5531bdSeric 295ce5531bdSeric for (; splitenv != NULL; splitenv = splitenv->e_sibling) 296ce5531bdSeric dropenvelope(splitenv); 297ce5531bdSeric } 298ce5531bdSeric 299ce5531bdSeric sendenvelope(e, mode) 300ce5531bdSeric register ENVELOPE *e; 301ce5531bdSeric char mode; 302ce5531bdSeric { 303ce5531bdSeric bool oldverbose; 304ce5531bdSeric int pid; 305ce5531bdSeric register ADDRESS *q; 3066b2765c6Seric char *qf; 3076b2765c6Seric char *id; 308ce5531bdSeric 3097880f3e4Seric /* 3107880f3e4Seric ** If we have had global, fatal errors, don't bother sending 3117880f3e4Seric ** the message at all if we are in SMTP mode. Local errors 3127880f3e4Seric ** (e.g., a single address failing) will still cause the other 3137880f3e4Seric ** addresses to be sent. 3147880f3e4Seric */ 3157880f3e4Seric 3167880f3e4Seric if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 3177880f3e4Seric { 3187880f3e4Seric e->e_flags |= EF_CLRQUEUE; 3197880f3e4Seric return; 3207880f3e4Seric } 3217880f3e4Seric 322ce5531bdSeric oldverbose = Verbose; 323c1ac89b1Seric switch (mode) 324c1ac89b1Seric { 325c1ac89b1Seric case SM_VERIFY: 326c1ac89b1Seric Verbose = TRUE; 327c1ac89b1Seric break; 328c1ac89b1Seric 329c1ac89b1Seric case SM_QUEUE: 330c1ac89b1Seric queueonly: 331c1ac89b1Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 332c1ac89b1Seric return; 333c1ac89b1Seric 334c1ac89b1Seric case SM_FORK: 335c1ac89b1Seric if (e->e_xfp != NULL) 336c1ac89b1Seric (void) fflush(e->e_xfp); 337c1ac89b1Seric 3382b9178d3Seric # ifndef HASFLOCK 339c1ac89b1Seric /* 3406b2765c6Seric ** Since fcntl locking has the interesting semantic that 3416b2765c6Seric ** the lock is owned by a process, not by an open file 3426b2765c6Seric ** descriptor, we have to flush this to the queue, and 3436b2765c6Seric ** then restart from scratch in the child. 344c1ac89b1Seric */ 345c1ac89b1Seric 3466b2765c6Seric /* save id for future use */ 3476b2765c6Seric id = e->e_id; 3486b2765c6Seric 3496b2765c6Seric /* now drop the envelope in the parent */ 3506b2765c6Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 3516b2765c6Seric dropenvelope(e); 3526b2765c6Seric 3536b2765c6Seric /* and reacquire in the child */ 3546b2765c6Seric (void) dowork(id, TRUE, FALSE, e); 3556b2765c6Seric 3566b2765c6Seric return; 3576b2765c6Seric 3586b2765c6Seric # else /* HASFLOCK */ 359c1ac89b1Seric 360c1ac89b1Seric pid = fork(); 361c1ac89b1Seric if (pid < 0) 362c1ac89b1Seric { 363c1ac89b1Seric goto queueonly; 364c1ac89b1Seric } 365c1ac89b1Seric else if (pid > 0) 366c1ac89b1Seric { 3670e484fe5Seric /* be sure we leave the temp files to our child */ 3680e484fe5Seric /* can't call unlockqueue to avoid unlink of xfp */ 3690e484fe5Seric if (e->e_lockfp != NULL) 3700e484fe5Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 3710e484fe5Seric e->e_lockfp = NULL; 3720e484fe5Seric 3730e484fe5Seric /* close any random open files in the envelope */ 3740e484fe5Seric closexscript(e); 3750e484fe5Seric if (e->e_dfp != NULL) 3760e484fe5Seric (void) xfclose(e->e_dfp, "sendenvelope", e->e_df); 3770e484fe5Seric e->e_dfp = NULL; 3780e484fe5Seric e->e_id = e->e_df = NULL; 379c1ac89b1Seric return; 380c1ac89b1Seric } 381c1ac89b1Seric 382c1ac89b1Seric /* double fork to avoid zombies */ 383c1ac89b1Seric if (fork() > 0) 384c1ac89b1Seric exit(EX_OK); 385c1ac89b1Seric 386c1ac89b1Seric /* be sure we are immune from the terminal */ 3877880f3e4Seric disconnect(1, e); 388c1ac89b1Seric 389c1ac89b1Seric /* 390c1ac89b1Seric ** Close any cached connections. 391c1ac89b1Seric ** 392c1ac89b1Seric ** We don't send the QUIT protocol because the parent 393c1ac89b1Seric ** still knows about the connection. 394c1ac89b1Seric ** 395c1ac89b1Seric ** This should only happen when delivering an error 396c1ac89b1Seric ** message. 397c1ac89b1Seric */ 398c1ac89b1Seric 399c1ac89b1Seric mci_flush(FALSE, NULL); 400c1ac89b1Seric 4016b2765c6Seric # endif /* HASFLOCK */ 4026b2765c6Seric 403c1ac89b1Seric break; 404c1ac89b1Seric } 405c1ac89b1Seric 406c1ac89b1Seric /* 4079c9e68d9Seric ** Run through the list and send everything. 4085288e21aSeric ** 4095288e21aSeric ** Set EF_GLOBALERRS so that error messages during delivery 4105288e21aSeric ** result in returned mail. 4119c9e68d9Seric */ 4129c9e68d9Seric 4139c9e68d9Seric e->e_nsent = 0; 4145288e21aSeric e->e_flags |= EF_GLOBALERRS; 4159c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 4169c9e68d9Seric { 4179c9e68d9Seric if (mode == SM_VERIFY) 4189c9e68d9Seric { 4199c9e68d9Seric e->e_to = q->q_paddr; 4209c9e68d9Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4218dfca105Seric { 4228dfca105Seric message("deliverable: mailer %s, host %s, user %s", 4238dfca105Seric q->q_mailer->m_name, 4248dfca105Seric q->q_host, 4258dfca105Seric q->q_user); 4268dfca105Seric } 4279c9e68d9Seric } 4289c9e68d9Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4299c9e68d9Seric { 4309c9e68d9Seric # ifdef QUEUE 4319c9e68d9Seric /* 4329c9e68d9Seric ** Checkpoint the send list every few addresses 4339c9e68d9Seric */ 4349c9e68d9Seric 4359c9e68d9Seric if (e->e_nsent >= CheckpointInterval) 4369c9e68d9Seric { 4379c9e68d9Seric queueup(e, TRUE, FALSE); 4389c9e68d9Seric e->e_nsent = 0; 4399c9e68d9Seric } 4409c9e68d9Seric # endif /* QUEUE */ 4419c9e68d9Seric (void) deliver(e, q); 4429c9e68d9Seric } 4439c9e68d9Seric } 4449c9e68d9Seric Verbose = oldverbose; 4459c9e68d9Seric 4469c9e68d9Seric if (mode == SM_FORK) 4479c9e68d9Seric finis(); 4489c9e68d9Seric } 4499c9e68d9Seric /* 4509c9e68d9Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4519c9e68d9Seric ** 4529c9e68d9Seric ** This MUST be a macro, since after a vfork we are running 4539c9e68d9Seric ** two processes on the same stack!!! 4549c9e68d9Seric ** 4559c9e68d9Seric ** Parameters: 4569c9e68d9Seric ** none. 4579c9e68d9Seric ** 4589c9e68d9Seric ** Returns: 4599c9e68d9Seric ** From a macro??? You've got to be kidding! 4609c9e68d9Seric ** 4619c9e68d9Seric ** Side Effects: 4629c9e68d9Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4639c9e68d9Seric ** pid of child in parent, zero in child. 4649c9e68d9Seric ** -1 on unrecoverable error. 4659c9e68d9Seric ** 4669c9e68d9Seric ** Notes: 4679c9e68d9Seric ** I'm awfully sorry this looks so awful. That's 4689c9e68d9Seric ** vfork for you..... 4699c9e68d9Seric */ 4709c9e68d9Seric 4719c9e68d9Seric # define NFORKTRIES 5 4729c9e68d9Seric 4739c9e68d9Seric # ifndef FORK 4749c9e68d9Seric # define FORK fork 4759c9e68d9Seric # endif 4769c9e68d9Seric 4779c9e68d9Seric # define DOFORK(fORKfN) \ 4789c9e68d9Seric {\ 4799c9e68d9Seric register int i;\ 4809c9e68d9Seric \ 4819c9e68d9Seric for (i = NFORKTRIES; --i >= 0; )\ 4829c9e68d9Seric {\ 4839c9e68d9Seric pid = fORKfN();\ 4849c9e68d9Seric if (pid >= 0)\ 4859c9e68d9Seric break;\ 4869c9e68d9Seric if (i > 0)\ 4879c9e68d9Seric sleep((unsigned) NFORKTRIES - i);\ 4889c9e68d9Seric }\ 4899c9e68d9Seric } 4909c9e68d9Seric /* 4919c9e68d9Seric ** DOFORK -- simple fork interface to DOFORK. 4929c9e68d9Seric ** 4939c9e68d9Seric ** Parameters: 4949c9e68d9Seric ** none. 4959c9e68d9Seric ** 4969c9e68d9Seric ** Returns: 4979c9e68d9Seric ** pid of child in parent. 4989c9e68d9Seric ** zero in child. 4999c9e68d9Seric ** -1 on error. 5009c9e68d9Seric ** 5019c9e68d9Seric ** Side Effects: 5029c9e68d9Seric ** returns twice, once in parent and once in child. 5039c9e68d9Seric */ 5049c9e68d9Seric 5059c9e68d9Seric dofork() 5069c9e68d9Seric { 5079c9e68d9Seric register int pid; 5089c9e68d9Seric 5099c9e68d9Seric DOFORK(fork); 5109c9e68d9Seric return (pid); 5119c9e68d9Seric } 5129c9e68d9Seric /* 51313bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 51413bbc08cSeric ** 51513bbc08cSeric ** This routine delivers to everyone on the same host as the 51613bbc08cSeric ** user on the head of the list. It is clever about mailers 51713bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 51813bbc08cSeric ** that it will deliver to all these addresses however -- so 51913bbc08cSeric ** deliver should be called once for each address on the 52013bbc08cSeric ** list. 52125a99e2eSeric ** 52225a99e2eSeric ** Parameters: 523588cad61Seric ** e -- the envelope to deliver. 524c77d1c25Seric ** firstto -- head of the address list to deliver to. 52525a99e2eSeric ** 52625a99e2eSeric ** Returns: 52725a99e2eSeric ** zero -- successfully delivered. 52825a99e2eSeric ** else -- some failure, see ExitStat for more info. 52925a99e2eSeric ** 53025a99e2eSeric ** Side Effects: 53125a99e2eSeric ** The standard input is passed off to someone. 53225a99e2eSeric */ 53325a99e2eSeric 534588cad61Seric deliver(e, firstto) 535588cad61Seric register ENVELOPE *e; 536c77d1c25Seric ADDRESS *firstto; 53725a99e2eSeric { 53878442df3Seric char *host; /* host being sent to */ 53978442df3Seric char *user; /* user being sent to */ 54025a99e2eSeric char **pvp; 5415dfc646bSeric register char **mvp; 54225a99e2eSeric register char *p; 543588cad61Seric register MAILER *m; /* mailer for this recipient */ 5446259796dSeric ADDRESS *ctladdr; 545b31e7f2bSeric register MCI *mci; 546c77d1c25Seric register ADDRESS *to = firstto; 547c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 548772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 549911693bfSbostic int rcode; /* response code */ 550e103b48fSeric char *firstsig; /* signature of firstto */ 5519c9e68d9Seric int pid; 5529c9e68d9Seric char *curhost; 5539c9e68d9Seric int mpvect[2]; 5549c9e68d9Seric int rpvect[2]; 555ee6bf8dfSeric char *pv[MAXPV+1]; 556579ef0ddSeric char tobuf[TOBUFSIZE]; /* text line of to people */ 557ee6bf8dfSeric char buf[MAXNAME]; 558c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 559fabb3bd4Seric extern int checkcompat(); 5609c9e68d9Seric extern FILE *fdopen(); 56125a99e2eSeric 56235490626Seric errno = 0; 563ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5645dfc646bSeric return (0); 56525a99e2eSeric 566134746fbSeric #ifdef NAMED_BIND 567912a731aSbostic /* unless interactive, try twice, over a minute */ 568912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 569912a731aSbostic _res.retrans = 30; 570912a731aSbostic _res.retry = 2; 571912a731aSbostic } 572d4bd8f0eSbostic #endif 573912a731aSbostic 57451552439Seric m = to->q_mailer; 57551552439Seric host = to->q_host; 576c9be6216Seric CurEnv = e; /* just in case */ 5774384d521Seric e->e_statmsg = NULL; 57851552439Seric 5796ef48975Seric if (tTd(10, 1)) 5805dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 58151552439Seric m->m_mno, host, to->q_user); 582f3dbc832Seric 583f3dbc832Seric /* 584f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 585f3dbc832Seric ** connections now, just mark these addresses and return. 586f3dbc832Seric ** This is useful if we want to batch connections to 587f3dbc832Seric ** reduce load. This will cause the messages to be 588f3dbc832Seric ** queued up, and a daemon will come along to send the 589f3dbc832Seric ** messages later. 590f3dbc832Seric ** This should be on a per-mailer basis. 591f3dbc832Seric */ 592f3dbc832Seric 59319c47125Seric if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) && 59419c47125Seric bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 595f3dbc832Seric { 596f3dbc832Seric for (; to != NULL; to = to->q_next) 597f4560e80Seric { 598ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 599c6e18ac5Seric to->q_mailer != m) 600f4560e80Seric continue; 601268a25e1Seric to->q_flags |= QQUEUEUP; 602588cad61Seric e->e_to = to->q_paddr; 60308b25121Seric message("queued"); 6042f624c86Seric if (LogLevel > 8) 60581161401Seric logdelivery(m, NULL, "queued", e); 606f4560e80Seric } 607588cad61Seric e->e_to = NULL; 608f3dbc832Seric return (0); 609f3dbc832Seric } 610f3dbc832Seric 61125a99e2eSeric /* 6125dfc646bSeric ** Do initial argv setup. 6135dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 6145dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 6155dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 6165dfc646bSeric ** code does not check for 'pv' overflow; this places a 6175dfc646bSeric ** manifest lower limit of 4 for MAXPV. 6183bea8136Seric ** The from address rewrite is expected to make 6193bea8136Seric ** the address relative to the other end. 6205dfc646bSeric */ 6215dfc646bSeric 62278442df3Seric /* rewrite from address, using rewriting rules */ 623efe54562Seric rcode = EX_OK; 624efe54562Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, 625efe54562Seric RF_SENDERADDR|RF_CANONICAL, 626efe54562Seric &rcode, e)); 627ee4b0922Seric define('g', rpathbuf, e); /* translated return path */ 628588cad61Seric define('h', host, e); /* to host */ 6295dfc646bSeric Errors = 0; 6305dfc646bSeric pvp = pv; 6315dfc646bSeric *pvp++ = m->m_argv[0]; 6325dfc646bSeric 6335dfc646bSeric /* insert -f or -r flag as appropriate */ 63457fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6355dfc646bSeric { 63657fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 6375dfc646bSeric *pvp++ = "-f"; 6385dfc646bSeric else 6395dfc646bSeric *pvp++ = "-r"; 640c23ed322Seric *pvp++ = newstr(rpathbuf); 6415dfc646bSeric } 6425dfc646bSeric 6435dfc646bSeric /* 6445dfc646bSeric ** Append the other fixed parts of the argv. These run 6455dfc646bSeric ** up to the first entry containing "$u". There can only 6465dfc646bSeric ** be one of these, and there are only a few more slots 6475dfc646bSeric ** in the pv after it. 6485dfc646bSeric */ 6495dfc646bSeric 6505dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 6515dfc646bSeric { 6522bc47524Seric /* can't use strchr here because of sign extension problems */ 6532bc47524Seric while (*p != '\0') 6542bc47524Seric { 6552bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 6562bc47524Seric { 6572bc47524Seric if (*p == 'u') 6585dfc646bSeric break; 6592bc47524Seric } 6602bc47524Seric } 6612bc47524Seric 6622bc47524Seric if (*p != '\0') 6635dfc646bSeric break; 6645dfc646bSeric 6655dfc646bSeric /* this entry is safe -- go ahead and process it */ 666588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 6675dfc646bSeric *pvp++ = newstr(buf); 6685dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 6695dfc646bSeric { 67008b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 6715dfc646bSeric return (-1); 6725dfc646bSeric } 6735dfc646bSeric } 674c579ef51Seric 67533db8731Seric /* 67633db8731Seric ** If we have no substitution for the user name in the argument 67733db8731Seric ** list, we know that we must supply the names otherwise -- and 67833db8731Seric ** SMTP is the answer!! 67933db8731Seric */ 68033db8731Seric 6815dfc646bSeric if (*mvp == NULL) 682c579ef51Seric { 683c579ef51Seric /* running SMTP */ 6842c7e1b8dSeric # ifdef SMTP 685c579ef51Seric clever = TRUE; 686c579ef51Seric *pvp = NULL; 6876c2c3107Seric # else /* SMTP */ 68833db8731Seric /* oops! we don't implement SMTP */ 68908b25121Seric syserr("554 SMTP style mailer"); 6902c7e1b8dSeric return (EX_SOFTWARE); 6916c2c3107Seric # endif /* SMTP */ 692c579ef51Seric } 6935dfc646bSeric 6945dfc646bSeric /* 6955dfc646bSeric ** At this point *mvp points to the argument with $u. We 6965dfc646bSeric ** run through our address list and append all the addresses 6975dfc646bSeric ** we can. If we run out of space, do not fret! We can 6985dfc646bSeric ** always send another copy later. 6995dfc646bSeric */ 7005dfc646bSeric 7015dfc646bSeric tobuf[0] = '\0'; 702588cad61Seric e->e_to = tobuf; 7036259796dSeric ctladdr = NULL; 704e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7055dfc646bSeric for (; to != NULL; to = to->q_next) 7065dfc646bSeric { 7075dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 70857fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7095dfc646bSeric break; 7105dfc646bSeric 7115dfc646bSeric /* if already sent or not for this host, don't send */ 712ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 713e103b48fSeric to->q_mailer != firstto->q_mailer || 714e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7155dfc646bSeric continue; 7166259796dSeric 7174b22ea87Seric /* avoid overflowing tobuf */ 718aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7194b22ea87Seric break; 7204b22ea87Seric 7216ef48975Seric if (tTd(10, 1)) 722772e6e50Seric { 723772e6e50Seric printf("\nsend to "); 724772e6e50Seric printaddr(to, FALSE); 725772e6e50Seric } 726772e6e50Seric 7276259796dSeric /* compute effective uid/gid when sending */ 7287da1035fSeric if (to->q_mailer == ProgMailer) 7296259796dSeric ctladdr = getctladdr(to); 7306259796dSeric 7315dfc646bSeric user = to->q_user; 732588cad61Seric e->e_to = to->q_paddr; 73375f1ade9Seric if (tTd(10, 5)) 73475f1ade9Seric { 73575f1ade9Seric printf("deliver: QDONTSEND "); 73675f1ade9Seric printaddr(to, FALSE); 73775f1ade9Seric } 738ee4b0922Seric to->q_flags |= QDONTSEND; 7395dfc646bSeric 7405dfc646bSeric /* 7415dfc646bSeric ** Check to see that these people are allowed to 7425dfc646bSeric ** talk to each other. 7432a6e0786Seric */ 7442a6e0786Seric 74569582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 74669582d2fSeric { 74769582d2fSeric NoReturn = TRUE; 74808b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 74981161401Seric giveresponse(EX_UNAVAILABLE, m, NULL, e); 75069582d2fSeric continue; 75169582d2fSeric } 752fabb3bd4Seric rcode = checkcompat(to, e); 7531793c9c5Seric if (rcode != EX_OK) 7545dfc646bSeric { 7551000c37aSeric markfailure(e, to, rcode); 75681161401Seric giveresponse(rcode, m, NULL, e); 7575dfc646bSeric continue; 7585dfc646bSeric } 7592a6e0786Seric 7602a6e0786Seric /* 7619ec9501bSeric ** Strip quote bits from names if the mailer is dumb 7629ec9501bSeric ** about them. 76325a99e2eSeric */ 76425a99e2eSeric 76557fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 76625a99e2eSeric { 7671d8f1806Seric stripquotes(user); 7681d8f1806Seric stripquotes(host); 76925a99e2eSeric } 77025a99e2eSeric 771cdb828c5Seric /* hack attack -- delivermail compatibility */ 772cdb828c5Seric if (m == ProgMailer && *user == '|') 773cdb828c5Seric user++; 774cdb828c5Seric 77525a99e2eSeric /* 7763efaed6eSeric ** If an error message has already been given, don't 7773efaed6eSeric ** bother to send to this address. 7783efaed6eSeric ** 7793efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 7803efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 7813efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 7823efaed6eSeric */ 7833efaed6eSeric 7846cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 7853efaed6eSeric continue; 7863efaed6eSeric 787f2fec898Seric /* save statistics.... */ 788588cad61Seric markstats(e, to); 789f2fec898Seric 7903efaed6eSeric /* 79125a99e2eSeric ** See if this user name is "special". 79225a99e2eSeric ** If the user name has a slash in it, assume that this 79351552439Seric ** is a file -- send it off without further ado. Note 79451552439Seric ** that this type of addresses is not processed along 79551552439Seric ** with the others, so we fudge on the To person. 79625a99e2eSeric */ 79725a99e2eSeric 7982c017f8dSeric if (m == FileMailer) 79925a99e2eSeric { 800b31e7f2bSeric rcode = mailfile(user, getctladdr(to), e); 80181161401Seric giveresponse(rcode, m, NULL, e); 802dde5acadSeric if (rcode == EX_OK) 803dde5acadSeric to->q_flags |= QSENT; 8045dfc646bSeric continue; 80525a99e2eSeric } 80625a99e2eSeric 80713bbc08cSeric /* 80813bbc08cSeric ** Address is verified -- add this user to mailer 80913bbc08cSeric ** argv, and add it to the print list of recipients. 81013bbc08cSeric */ 81113bbc08cSeric 812508daeccSeric /* link together the chain of recipients */ 813508daeccSeric to->q_tchain = tochain; 814508daeccSeric tochain = to; 815508daeccSeric 8165dfc646bSeric /* create list of users for error messages */ 817db8841e9Seric (void) strcat(tobuf, ","); 818db8841e9Seric (void) strcat(tobuf, to->q_paddr); 819588cad61Seric define('u', user, e); /* to user */ 820588cad61Seric define('z', to->q_home, e); /* user's home */ 8215dfc646bSeric 822c579ef51Seric /* 823508daeccSeric ** Expand out this user into argument list. 824c579ef51Seric */ 825c579ef51Seric 826508daeccSeric if (!clever) 827c579ef51Seric { 828588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8295dfc646bSeric *pvp++ = newstr(buf); 8305dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 8315dfc646bSeric { 8325dfc646bSeric /* allow some space for trailing parms */ 8335dfc646bSeric break; 8345dfc646bSeric } 8355dfc646bSeric } 836c579ef51Seric } 8375dfc646bSeric 838145b49b1Seric /* see if any addresses still exist */ 839145b49b1Seric if (tobuf[0] == '\0') 840c579ef51Seric { 841588cad61Seric define('g', (char *) NULL, e); 842145b49b1Seric return (0); 843c579ef51Seric } 844145b49b1Seric 8455dfc646bSeric /* print out messages as full list */ 84663780dbdSeric e->e_to = tobuf + 1; 8475dfc646bSeric 8485dfc646bSeric /* 8495dfc646bSeric ** Fill out any parameters after the $u parameter. 8505dfc646bSeric */ 8515dfc646bSeric 852c579ef51Seric while (!clever && *++mvp != NULL) 8535dfc646bSeric { 854588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8555dfc646bSeric *pvp++ = newstr(buf); 8565dfc646bSeric if (pvp >= &pv[MAXPV]) 85708b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 8585dfc646bSeric } 8595dfc646bSeric *pvp++ = NULL; 8605dfc646bSeric 86125a99e2eSeric /* 86225a99e2eSeric ** Call the mailer. 8636328bdf7Seric ** The argument vector gets built, pipes 86425a99e2eSeric ** are created as necessary, and we fork & exec as 8656328bdf7Seric ** appropriate. 866c579ef51Seric ** If we are running SMTP, we just need to clean up. 86725a99e2eSeric */ 86825a99e2eSeric 8697ee87e5fSeric if (ctladdr == NULL && m != ProgMailer) 87086b26461Seric ctladdr = &e->e_from; 871134746fbSeric #ifdef NAMED_BIND 8722bcc6d2dSeric if (ConfigLevel < 2) 873912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 874134746fbSeric #endif 8759c9e68d9Seric 8769c9e68d9Seric if (tTd(11, 1)) 877134746fbSeric { 8789c9e68d9Seric printf("openmailer:"); 8799c9e68d9Seric printav(pv); 8809c9e68d9Seric } 8819c9e68d9Seric errno = 0; 8829c9e68d9Seric 8839c9e68d9Seric CurHostName = m->m_mailer; 8849c9e68d9Seric 8859c9e68d9Seric /* 8869c9e68d9Seric ** Deal with the special case of mail handled through an IPC 8879c9e68d9Seric ** connection. 8889c9e68d9Seric ** In this case we don't actually fork. We must be 8899c9e68d9Seric ** running SMTP for this to work. We will return a 8909c9e68d9Seric ** zero pid to indicate that we are running IPC. 8919c9e68d9Seric ** We also handle a debug version that just talks to stdin/out. 8929c9e68d9Seric */ 8939c9e68d9Seric 8949c9e68d9Seric curhost = NULL; 895c931b82bSeric SmtpPhase = NULL; 8969c9e68d9Seric 8979c9e68d9Seric /* check for Local Person Communication -- not for mortals!!! */ 8989c9e68d9Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 8999c9e68d9Seric { 9009c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 9019c9e68d9Seric bzero((char *) mci, sizeof *mci); 9029c9e68d9Seric mci->mci_in = stdin; 9039c9e68d9Seric mci->mci_out = stdout; 9049c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9059c9e68d9Seric mci->mci_mailer = m; 9069c9e68d9Seric } 9079c9e68d9Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 9089c9e68d9Seric strcmp(m->m_mailer, "[TCP]") == 0) 9099c9e68d9Seric { 9109c9e68d9Seric #ifdef DAEMON 9119c9e68d9Seric register int i; 9129c9e68d9Seric register u_short port; 9139c9e68d9Seric 9149c9e68d9Seric CurHostName = pv[1]; 9159c9e68d9Seric curhost = hostsignature(m, pv[1], e); 9169c9e68d9Seric 9179c9e68d9Seric if (curhost == NULL || curhost[0] == '\0') 9189c9e68d9Seric { 9199c9e68d9Seric syserr("null signature"); 920845e533cSeric rcode = EX_OSERR; 921b31e7f2bSeric goto give_up; 922b31e7f2bSeric } 9239c9e68d9Seric 9249c9e68d9Seric if (!clever) 9259c9e68d9Seric { 9269c9e68d9Seric syserr("554 non-clever IPC"); 9279c9e68d9Seric rcode = EX_OSERR; 9289c9e68d9Seric goto give_up; 9299c9e68d9Seric } 9309c9e68d9Seric if (pv[2] != NULL) 9319c9e68d9Seric port = atoi(pv[2]); 9329c9e68d9Seric else 9339c9e68d9Seric port = 0; 9349c9e68d9Seric tryhost: 9359c9e68d9Seric mci = NULL; 9369c9e68d9Seric while (*curhost != '\0') 9379c9e68d9Seric { 9389c9e68d9Seric register char *p; 9399c9e68d9Seric static char hostbuf[MAXNAME]; 9409c9e68d9Seric 9419c9e68d9Seric mci = NULL; 9429c9e68d9Seric 9439c9e68d9Seric /* pull the next host from the signature */ 9449c9e68d9Seric p = strchr(curhost, ':'); 9459c9e68d9Seric if (p == NULL) 9469c9e68d9Seric p = &curhost[strlen(curhost)]; 9479c9e68d9Seric strncpy(hostbuf, curhost, p - curhost); 9489c9e68d9Seric hostbuf[p - curhost] = '\0'; 9499c9e68d9Seric if (*p != '\0') 9509c9e68d9Seric p++; 9519c9e68d9Seric curhost = p; 9529c9e68d9Seric 9539c9e68d9Seric /* see if we already know that this host is fried */ 9549c9e68d9Seric CurHostName = hostbuf; 9559c9e68d9Seric mci = mci_get(hostbuf, m); 9569c9e68d9Seric if (mci->mci_state != MCIS_CLOSED) 9579c9e68d9Seric { 9589c9e68d9Seric if (tTd(11, 1)) 9599c9e68d9Seric { 9609c9e68d9Seric printf("openmailer: "); 9619c9e68d9Seric mci_dump(mci); 9629c9e68d9Seric } 9639c9e68d9Seric CurHostName = mci->mci_host; 9649c9e68d9Seric break; 9659c9e68d9Seric } 9669c9e68d9Seric mci->mci_mailer = m; 9679c9e68d9Seric if (mci->mci_exitstat != EX_OK) 9689c9e68d9Seric continue; 9699c9e68d9Seric 9709c9e68d9Seric /* try the connection */ 9719c9e68d9Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 9729c9e68d9Seric message("Connecting to %s (%s)...", 9739c9e68d9Seric hostbuf, m->m_name); 9749c9e68d9Seric i = makeconnection(hostbuf, port, mci, 9759c9e68d9Seric bitnset(M_SECURE_PORT, m->m_flags)); 9769c9e68d9Seric mci->mci_exitstat = i; 9779c9e68d9Seric mci->mci_errno = errno; 978f170942cSeric #ifdef NAMED_BIND 979f170942cSeric mci->mci_herrno = h_errno; 980f170942cSeric #endif 9819c9e68d9Seric if (i == EX_OK) 9829c9e68d9Seric { 9839c9e68d9Seric mci->mci_state = MCIS_OPENING; 9849c9e68d9Seric mci_cache(mci); 985f170942cSeric if (TrafficLogFile != NULL) 986f170942cSeric fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 987f170942cSeric getpid(), hostbuf); 9889c9e68d9Seric break; 9899c9e68d9Seric } 9909c9e68d9Seric else if (tTd(11, 1)) 9919c9e68d9Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 9929c9e68d9Seric i, errno); 9939c9e68d9Seric 9949c9e68d9Seric 9959c9e68d9Seric /* enter status of this host */ 9969c9e68d9Seric setstat(i); 9979c9e68d9Seric } 9989c9e68d9Seric mci->mci_pid = 0; 9999c9e68d9Seric #else /* no DAEMON */ 10009c9e68d9Seric syserr("554 openmailer: no IPC"); 10019c9e68d9Seric if (tTd(11, 1)) 10029c9e68d9Seric printf("openmailer: NULL\n"); 10039c9e68d9Seric return NULL; 10049c9e68d9Seric #endif /* DAEMON */ 10059c9e68d9Seric } 10069c9e68d9Seric else 10079c9e68d9Seric { 1008f170942cSeric #ifdef XDEBUG 1009f170942cSeric char wbuf[MAXLINE]; 10106fe8c3bcSeric 10116fe8c3bcSeric /* make absolutely certain 0, 1, and 2 are in use */ 1012f170942cSeric sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 1013f170942cSeric checkfd012(wbuf); 1014f170942cSeric #endif 10150e3bfef5Seric 1016f170942cSeric if (TrafficLogFile != NULL) 10170e3bfef5Seric { 1018f170942cSeric char **av; 1019f170942cSeric 1020f170942cSeric fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 1021f170942cSeric for (av = pv; *av != NULL; av++) 1022f170942cSeric fprintf(TrafficLogFile, " %s", *av); 1023f170942cSeric fprintf(TrafficLogFile, "\n"); 10246fe8c3bcSeric } 10256fe8c3bcSeric 10269c9e68d9Seric /* create a pipe to shove the mail through */ 10279c9e68d9Seric if (pipe(mpvect) < 0) 10289c9e68d9Seric { 10290e3bfef5Seric syserr("%s... openmailer(%s): pipe (to mailer)", 10300e3bfef5Seric e->e_to, m->m_name); 10319c9e68d9Seric if (tTd(11, 1)) 10329c9e68d9Seric printf("openmailer: NULL\n"); 10339c9e68d9Seric rcode = EX_OSERR; 10349c9e68d9Seric goto give_up; 10359c9e68d9Seric } 10369c9e68d9Seric 10379c9e68d9Seric /* if this mailer speaks smtp, create a return pipe */ 10389c9e68d9Seric if (clever && pipe(rpvect) < 0) 10399c9e68d9Seric { 10400e3bfef5Seric syserr("%s... openmailer(%s): pipe (from mailer)", 10410e3bfef5Seric e->e_to, m->m_name); 10429c9e68d9Seric (void) close(mpvect[0]); 10439c9e68d9Seric (void) close(mpvect[1]); 10449c9e68d9Seric if (tTd(11, 1)) 10459c9e68d9Seric printf("openmailer: NULL\n"); 10469c9e68d9Seric rcode = EX_OSERR; 10479c9e68d9Seric goto give_up; 10489c9e68d9Seric } 10499c9e68d9Seric 10509c9e68d9Seric /* 10519c9e68d9Seric ** Actually fork the mailer process. 10529c9e68d9Seric ** DOFORK is clever about retrying. 10539c9e68d9Seric ** 10549c9e68d9Seric ** Dispose of SIGCHLD signal catchers that may be laying 10559c9e68d9Seric ** around so that endmail will get it. 10569c9e68d9Seric */ 10579c9e68d9Seric 10589c9e68d9Seric if (e->e_xfp != NULL) 10599c9e68d9Seric (void) fflush(e->e_xfp); /* for debugging */ 10609c9e68d9Seric (void) fflush(stdout); 10619c9e68d9Seric # ifdef SIGCHLD 10622b9178d3Seric (void) setsignal(SIGCHLD, SIG_DFL); 10639c9e68d9Seric # endif /* SIGCHLD */ 10649c9e68d9Seric DOFORK(FORK); 10659c9e68d9Seric /* pid is set by DOFORK */ 10669c9e68d9Seric if (pid < 0) 10679c9e68d9Seric { 10689c9e68d9Seric /* failure */ 10690e3bfef5Seric syserr("%s... openmailer(%s): cannot fork", 10700e3bfef5Seric e->e_to, m->m_name); 10719c9e68d9Seric (void) close(mpvect[0]); 10729c9e68d9Seric (void) close(mpvect[1]); 10739c9e68d9Seric if (clever) 10749c9e68d9Seric { 10759c9e68d9Seric (void) close(rpvect[0]); 10769c9e68d9Seric (void) close(rpvect[1]); 10779c9e68d9Seric } 10789c9e68d9Seric if (tTd(11, 1)) 10799c9e68d9Seric printf("openmailer: NULL\n"); 10809c9e68d9Seric rcode = EX_OSERR; 10819c9e68d9Seric goto give_up; 10829c9e68d9Seric } 10839c9e68d9Seric else if (pid == 0) 10849c9e68d9Seric { 10859c9e68d9Seric int i; 10869c9e68d9Seric int saveerrno; 10879c9e68d9Seric char **ep; 10889c9e68d9Seric char *env[MAXUSERENVIRON]; 10899c9e68d9Seric extern char **environ; 10909c9e68d9Seric extern int DtableSize; 10919c9e68d9Seric 10929c9e68d9Seric /* child -- set up input & exec mailer */ 10932b9178d3Seric (void) setsignal(SIGINT, SIG_IGN); 10942b9178d3Seric (void) setsignal(SIGHUP, SIG_IGN); 10952b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 10969c9e68d9Seric 10979c9e68d9Seric /* close any other cached connections */ 10989c9e68d9Seric mci_flush(FALSE, mci); 10999c9e68d9Seric 110044f2317fSeric /* reset user and group */ 110144f2317fSeric if (!bitnset(M_RESTR, m->m_flags)) 110244f2317fSeric { 110344f2317fSeric if (ctladdr == NULL || ctladdr->q_uid == 0) 110444f2317fSeric { 110544f2317fSeric (void) initgroups(DefUser, DefGid); 110644f2317fSeric (void) setuid(DefUid); 110744f2317fSeric } 110844f2317fSeric else 110944f2317fSeric { 111044f2317fSeric (void) initgroups(ctladdr->q_ruser? 111144f2317fSeric ctladdr->q_ruser: ctladdr->q_user, 111244f2317fSeric ctladdr->q_gid); 111344f2317fSeric (void) setuid(ctladdr->q_uid); 111444f2317fSeric } 111544f2317fSeric } 111644f2317fSeric 111744f2317fSeric if (tTd(11, 2)) 111844f2317fSeric printf("openmailer: running as r/euid=%d/%d\n", 111944f2317fSeric getuid(), geteuid()); 112044f2317fSeric 1121b986f6aaSeric /* move into some "safe" directory */ 1122b986f6aaSeric if (m->m_execdir != NULL) 1123b986f6aaSeric { 1124b986f6aaSeric char *p, *q; 1125b986f6aaSeric char buf[MAXLINE]; 1126b986f6aaSeric 1127b986f6aaSeric for (p = m->m_execdir; p != NULL; p = q) 1128b986f6aaSeric { 1129b986f6aaSeric q = strchr(p, ':'); 1130b986f6aaSeric if (q != NULL) 1131b986f6aaSeric *q = '\0'; 1132b986f6aaSeric expand(p, buf, &buf[sizeof buf] - 1, e); 1133b986f6aaSeric if (q != NULL) 1134b986f6aaSeric *q++ = ':'; 1135b986f6aaSeric if (tTd(11, 20)) 1136b986f6aaSeric printf("openmailer: trydir %s\n", 1137b986f6aaSeric buf); 1138b986f6aaSeric if (buf[0] != '\0' && chdir(buf) >= 0) 1139b986f6aaSeric break; 1140b986f6aaSeric } 1141b986f6aaSeric } 1142b986f6aaSeric 11439c9e68d9Seric /* arrange to filter std & diag output of command */ 11449c9e68d9Seric if (clever) 11459c9e68d9Seric { 11469c9e68d9Seric (void) close(rpvect[0]); 11476fe8c3bcSeric if (dup2(rpvect[1], STDOUT_FILENO) < 0) 11486fe8c3bcSeric { 11490e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 11500e3bfef5Seric e->e_to, m->m_name, rpvect[1]); 11516fe8c3bcSeric _exit(EX_OSERR); 11526fe8c3bcSeric } 11539c9e68d9Seric (void) close(rpvect[1]); 11549c9e68d9Seric } 11559c9e68d9Seric else if (OpMode == MD_SMTP || HoldErrs) 11569c9e68d9Seric { 11579c9e68d9Seric /* put mailer output in transcript */ 11586fe8c3bcSeric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 11596fe8c3bcSeric { 11600e3bfef5Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 11610e3bfef5Seric e->e_to, m->m_name, 11626fe8c3bcSeric fileno(e->e_xfp)); 11636fe8c3bcSeric _exit(EX_OSERR); 11649c9e68d9Seric } 11656fe8c3bcSeric } 11666fe8c3bcSeric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 11676fe8c3bcSeric { 11680e3bfef5Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr", 11690e3bfef5Seric e->e_to, m->m_name); 11706fe8c3bcSeric _exit(EX_OSERR); 11716fe8c3bcSeric } 11729c9e68d9Seric 11739c9e68d9Seric /* arrange to get standard input */ 11749c9e68d9Seric (void) close(mpvect[1]); 11759c9e68d9Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 11769c9e68d9Seric { 11770e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 11780e3bfef5Seric e->e_to, m->m_name, mpvect[0]); 11799c9e68d9Seric _exit(EX_OSERR); 11809c9e68d9Seric } 11819c9e68d9Seric (void) close(mpvect[0]); 11829c9e68d9Seric 11839c9e68d9Seric /* arrange for all the files to be closed */ 11849c9e68d9Seric for (i = 3; i < DtableSize; i++) 11859c9e68d9Seric { 11869c9e68d9Seric register int j; 118744f2317fSeric 11889c9e68d9Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 11899c9e68d9Seric (void) fcntl(i, F_SETFD, j | 1); 11909c9e68d9Seric } 11919c9e68d9Seric 11929c9e68d9Seric /* set up the mailer environment */ 11939c9e68d9Seric i = 0; 11949c9e68d9Seric env[i++] = "AGENT=sendmail"; 11959c9e68d9Seric for (ep = environ; *ep != NULL; ep++) 11969c9e68d9Seric { 11979c9e68d9Seric if (strncmp(*ep, "TZ=", 3) == 0) 11989c9e68d9Seric env[i++] = *ep; 11999c9e68d9Seric } 12009c9e68d9Seric env[i++] = NULL; 12019c9e68d9Seric 12029c9e68d9Seric /* try to execute the mailer */ 12039c9e68d9Seric execve(m->m_mailer, pv, env); 12049c9e68d9Seric saveerrno = errno; 12059c9e68d9Seric syserr("Cannot exec %s", m->m_mailer); 12067c941fd2Seric if (m == LocalMailer || transienterror(saveerrno)) 12077c941fd2Seric _exit(EX_OSERR); 12089c9e68d9Seric _exit(EX_UNAVAILABLE); 12099c9e68d9Seric } 12109c9e68d9Seric 12119c9e68d9Seric /* 12129c9e68d9Seric ** Set up return value. 12139c9e68d9Seric */ 12149c9e68d9Seric 12159c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 12169c9e68d9Seric bzero((char *) mci, sizeof *mci); 12179c9e68d9Seric mci->mci_mailer = m; 12189c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 12199c9e68d9Seric mci->mci_pid = pid; 12209c9e68d9Seric (void) close(mpvect[0]); 12219c9e68d9Seric mci->mci_out = fdopen(mpvect[1], "w"); 12229c9e68d9Seric if (clever) 12239c9e68d9Seric { 12249c9e68d9Seric (void) close(rpvect[1]); 12259c9e68d9Seric mci->mci_in = fdopen(rpvect[0], "r"); 12269c9e68d9Seric } 12279c9e68d9Seric else 12289c9e68d9Seric { 12299c9e68d9Seric mci->mci_flags |= MCIF_TEMP; 12309c9e68d9Seric mci->mci_in = NULL; 12319c9e68d9Seric } 12329c9e68d9Seric } 12339c9e68d9Seric 12349c9e68d9Seric /* 12359c9e68d9Seric ** If we are in SMTP opening state, send initial protocol. 12369c9e68d9Seric */ 12379c9e68d9Seric 12389c9e68d9Seric if (clever && mci->mci_state != MCIS_CLOSED) 12399c9e68d9Seric { 12409c9e68d9Seric smtpinit(m, mci, e); 12419c9e68d9Seric } 12429c9e68d9Seric if (tTd(11, 1)) 12439c9e68d9Seric { 12449c9e68d9Seric printf("openmailer: "); 12459c9e68d9Seric mci_dump(mci); 12469c9e68d9Seric } 12479c9e68d9Seric 12489c9e68d9Seric if (mci->mci_state != MCIS_OPEN) 1249b31e7f2bSeric { 1250b31e7f2bSeric /* couldn't open the mailer */ 1251b31e7f2bSeric rcode = mci->mci_exitstat; 12522a6bc25bSeric errno = mci->mci_errno; 1253f170942cSeric #ifdef NAMED_BIND 1254f170942cSeric h_errno = mci->mci_herrno; 1255f170942cSeric #endif 1256b31e7f2bSeric if (rcode == EX_OK) 1257b31e7f2bSeric { 1258b31e7f2bSeric /* shouldn't happen */ 125908b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 12606b0f339dSeric rcode, mci->mci_state, firstsig); 1261b31e7f2bSeric rcode = EX_SOFTWARE; 1262b31e7f2bSeric } 1263e9277e33Seric else if (rcode == EX_TEMPFAIL && *curhost != '\0') 126490891494Seric { 126590891494Seric /* try next MX site */ 126690891494Seric goto tryhost; 126790891494Seric } 1268b31e7f2bSeric } 1269b31e7f2bSeric else if (!clever) 1270b31e7f2bSeric { 1271b31e7f2bSeric /* 1272b31e7f2bSeric ** Format and send message. 1273b31e7f2bSeric */ 127415d084d5Seric 1275b31e7f2bSeric putfromline(mci->mci_out, m, e); 1276b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 1277b31e7f2bSeric putline("\n", mci->mci_out, m); 127803c02fdeSeric (*e->e_putbody)(mci->mci_out, m, e, NULL); 1279b31e7f2bSeric 1280b31e7f2bSeric /* get the exit status */ 1281c9be6216Seric rcode = endmailer(mci, e, pv); 1282134746fbSeric } 1283134746fbSeric else 1284b31e7f2bSeric #ifdef SMTP 1285134746fbSeric { 1286b31e7f2bSeric /* 1287b31e7f2bSeric ** Send the MAIL FROM: protocol 1288b31e7f2bSeric */ 128915d084d5Seric 1290b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 1291b31e7f2bSeric if (rcode == EX_OK) 129275889e88Seric { 1293ded0d3daSkarels register char *t = tobuf; 1294ded0d3daSkarels register int i; 1295ded0d3daSkarels 1296588cad61Seric /* send the recipient list */ 129763780dbdSeric tobuf[0] = '\0'; 129875889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 129975889e88Seric { 130063780dbdSeric e->e_to = to->q_paddr; 130115d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 130275889e88Seric { 130383b7ddc9Seric markfailure(e, to, i); 130481161401Seric giveresponse(i, m, mci, e); 130563780dbdSeric } 130675889e88Seric else 130775889e88Seric { 1308911693bfSbostic *t++ = ','; 1309b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 1310b31e7f2bSeric continue; 1311ee39df61Seric *t = '\0'; 1312588cad61Seric } 1313588cad61Seric } 1314588cad61Seric 131563780dbdSeric /* now send the data */ 131663780dbdSeric if (tobuf[0] == '\0') 1317b31e7f2bSeric { 13189c9e68d9Seric rcode = EX_OK; 131963780dbdSeric e->e_to = NULL; 1320b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 1321b31e7f2bSeric smtprset(m, mci, e); 1322b31e7f2bSeric } 132375889e88Seric else 132475889e88Seric { 132563780dbdSeric e->e_to = tobuf + 1; 132675889e88Seric rcode = smtpdata(m, mci, e); 132763780dbdSeric } 132863780dbdSeric 132963780dbdSeric /* now close the connection */ 1330b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 133115d084d5Seric smtpquit(m, mci, e); 133263780dbdSeric } 13339c9e68d9Seric if (rcode != EX_OK && *curhost != '\0') 13349c9e68d9Seric { 13359c9e68d9Seric /* try next MX site */ 13369c9e68d9Seric goto tryhost; 13379c9e68d9Seric } 1338c579ef51Seric } 1339b31e7f2bSeric #else /* not SMTP */ 1340a05b3449Sbostic { 134108b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 1342845e533cSeric rcode = EX_CONFIG; 1343b31e7f2bSeric goto give_up; 1344a05b3449Sbostic } 1345b31e7f2bSeric #endif /* SMTP */ 1346134746fbSeric #ifdef NAMED_BIND 13472bcc6d2dSeric if (ConfigLevel < 2) 1348912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1349134746fbSeric #endif 13505dfc646bSeric 1351b31e7f2bSeric /* arrange a return receipt if requested */ 13528c13bf07Seric if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags)) 1353b31e7f2bSeric { 1354b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 1355b31e7f2bSeric /* do we want to send back more info? */ 1356b31e7f2bSeric } 1357b31e7f2bSeric 1358c77d1c25Seric /* 135963780dbdSeric ** Do final status disposal. 136063780dbdSeric ** We check for something in tobuf for the SMTP case. 1361c77d1c25Seric ** If we got a temporary failure, arrange to queue the 1362c77d1c25Seric ** addressees. 1363c77d1c25Seric */ 1364c77d1c25Seric 1365b31e7f2bSeric give_up: 136663780dbdSeric if (tobuf[0] != '\0') 136781161401Seric giveresponse(rcode, m, mci, e); 1368772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 1369b31e7f2bSeric { 1370dde5acadSeric if (rcode != EX_OK) 137183b7ddc9Seric markfailure(e, to, rcode); 1372dde5acadSeric else 1373655518ecSeric { 1374dde5acadSeric to->q_flags |= QSENT; 1375655518ecSeric e->e_nsent++; 1376655518ecSeric } 1377b31e7f2bSeric } 1378b31e7f2bSeric 1379b31e7f2bSeric /* 1380b31e7f2bSeric ** Restore state and return. 1381b31e7f2bSeric */ 1382c77d1c25Seric 138335490626Seric errno = 0; 1384588cad61Seric define('g', (char *) NULL, e); 13855826d9d3Seric return (rcode); 138625a99e2eSeric } 13875dfc646bSeric /* 138883b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 138983b7ddc9Seric ** 139083b7ddc9Seric ** Parameters: 139183b7ddc9Seric ** e -- the envelope we are sending. 139283b7ddc9Seric ** q -- the address to mark. 139383b7ddc9Seric ** rcode -- the code signifying the particular failure. 139483b7ddc9Seric ** 139583b7ddc9Seric ** Returns: 139683b7ddc9Seric ** none. 139783b7ddc9Seric ** 139883b7ddc9Seric ** Side Effects: 139983b7ddc9Seric ** marks the address (and possibly the envelope) with the 140083b7ddc9Seric ** failure so that an error will be returned or 140183b7ddc9Seric ** the message will be queued, as appropriate. 140283b7ddc9Seric */ 140383b7ddc9Seric 140483b7ddc9Seric markfailure(e, q, rcode) 140583b7ddc9Seric register ENVELOPE *e; 140683b7ddc9Seric register ADDRESS *q; 140783b7ddc9Seric int rcode; 140883b7ddc9Seric { 140919c47125Seric char buf[MAXLINE]; 141019c47125Seric 141183b7ddc9Seric if (rcode == EX_OK) 141283b7ddc9Seric return; 1413f170942cSeric else if (rcode == EX_TEMPFAIL) 141483b7ddc9Seric q->q_flags |= QQUEUEUP; 1415f170942cSeric else if (rcode != EX_IOERR && rcode != EX_OSERR) 1416f170942cSeric q->q_flags |= QBADADDR; 141783b7ddc9Seric } 141883b7ddc9Seric /* 1419c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 1420c579ef51Seric ** 1421c579ef51Seric ** We should never get fatal errors (e.g., segmentation 1422c579ef51Seric ** violation), so we report those specially. For other 1423c579ef51Seric ** errors, we choose a status message (into statmsg), 1424c579ef51Seric ** and if it represents an error, we print it. 1425c579ef51Seric ** 1426c579ef51Seric ** Parameters: 1427c579ef51Seric ** pid -- pid of mailer. 1428c9be6216Seric ** e -- the current envelope. 1429c9be6216Seric ** pv -- the parameter vector that invoked the mailer 1430c9be6216Seric ** (for error messages). 1431c579ef51Seric ** 1432c579ef51Seric ** Returns: 1433c579ef51Seric ** exit code of mailer. 1434c579ef51Seric ** 1435c579ef51Seric ** Side Effects: 1436c579ef51Seric ** none. 1437c579ef51Seric */ 1438c579ef51Seric 1439c9be6216Seric endmailer(mci, e, pv) 1440b31e7f2bSeric register MCI *mci; 1441c9be6216Seric register ENVELOPE *e; 1442c9be6216Seric char **pv; 1443c579ef51Seric { 1444588cad61Seric int st; 1445c579ef51Seric 144675889e88Seric /* close any connections */ 144775889e88Seric if (mci->mci_in != NULL) 1448c9be6216Seric (void) xfclose(mci->mci_in, pv[0], "mci_in"); 144975889e88Seric if (mci->mci_out != NULL) 1450c9be6216Seric (void) xfclose(mci->mci_out, pv[0], "mci_out"); 145175889e88Seric mci->mci_in = mci->mci_out = NULL; 145275889e88Seric mci->mci_state = MCIS_CLOSED; 145375889e88Seric 145433db8731Seric /* in the IPC case there is nothing to wait for */ 145575889e88Seric if (mci->mci_pid == 0) 145633db8731Seric return (EX_OK); 145733db8731Seric 145833db8731Seric /* wait for the mailer process to die and collect status */ 145975889e88Seric st = waitfor(mci->mci_pid); 1460588cad61Seric if (st == -1) 146178de67c1Seric { 1462c9be6216Seric syserr("endmailer %s: wait", pv[0]); 1463588cad61Seric return (EX_SOFTWARE); 1464c579ef51Seric } 146533db8731Seric 1466*bf9bc890Seric if (WIFEXITED(st)) 1467c579ef51Seric { 1468*bf9bc890Seric /* normal death -- return status */ 1469*bf9bc890Seric return (WEXITSTATUS(st)); 1470*bf9bc890Seric } 1471*bf9bc890Seric 1472*bf9bc890Seric /* it died a horrid death */ 1473c9be6216Seric syserr("mailer %s died with signal %o", pv[0], st); 1474c9be6216Seric 1475c9be6216Seric /* log the arguments */ 1476c9be6216Seric if (e->e_xfp != NULL) 1477c9be6216Seric { 1478c9be6216Seric register char **av; 1479c9be6216Seric 1480c9be6216Seric fprintf(e->e_xfp, "Arguments:"); 1481c9be6216Seric for (av = pv; *av != NULL; av++) 1482c9be6216Seric fprintf(e->e_xfp, " %s", *av); 1483c9be6216Seric fprintf(e->e_xfp, "\n"); 1484c9be6216Seric } 1485c9be6216Seric 14865f73204aSeric ExitStat = EX_TEMPFAIL; 14875f73204aSeric return (EX_TEMPFAIL); 1488c579ef51Seric } 1489c579ef51Seric /* 149025a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 149125a99e2eSeric ** 149225a99e2eSeric ** Parameters: 149325a99e2eSeric ** stat -- the status code from the mailer (high byte 149425a99e2eSeric ** only; core dumps must have been taken care of 149525a99e2eSeric ** already). 149681161401Seric ** m -- the mailer info for this mailer. 149781161401Seric ** mci -- the mailer connection info -- can be NULL if the 149881161401Seric ** response is given before the connection is made. 149981161401Seric ** e -- the current envelope. 150025a99e2eSeric ** 150125a99e2eSeric ** Returns: 1502db8841e9Seric ** none. 150325a99e2eSeric ** 150425a99e2eSeric ** Side Effects: 1505c1f9df2cSeric ** Errors may be incremented. 150625a99e2eSeric ** ExitStat may be set. 150725a99e2eSeric */ 150825a99e2eSeric 150981161401Seric giveresponse(stat, m, mci, e) 151025a99e2eSeric int stat; 1511588cad61Seric register MAILER *m; 151281161401Seric register MCI *mci; 1513198d9be0Seric ENVELOPE *e; 151425a99e2eSeric { 15159c16475dSeric register const char *statmsg; 151625a99e2eSeric extern char *SysExMsg[]; 151725a99e2eSeric register int i; 1518d4bd8f0eSbostic extern int N_SysEx; 1519198d9be0Seric char buf[MAXLINE]; 152025a99e2eSeric 152113bbc08cSeric /* 152213bbc08cSeric ** Compute status message from code. 152313bbc08cSeric */ 152413bbc08cSeric 152525a99e2eSeric i = stat - EX__BASE; 1526588cad61Seric if (stat == 0) 15276fe8c3bcSeric { 1528588cad61Seric statmsg = "250 Sent"; 1529ce5531bdSeric if (e->e_statmsg != NULL) 15306fe8c3bcSeric { 1531ce5531bdSeric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 15326fe8c3bcSeric statmsg = buf; 15336fe8c3bcSeric } 15346fe8c3bcSeric } 1535588cad61Seric else if (i < 0 || i > N_SysEx) 1536588cad61Seric { 1537588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1538588cad61Seric stat = EX_UNAVAILABLE; 1539588cad61Seric statmsg = buf; 1540588cad61Seric } 1541198d9be0Seric else if (stat == EX_TEMPFAIL) 1542198d9be0Seric { 15437d55540cSeric (void) strcpy(buf, SysExMsg[i] + 1); 1544d4bd8f0eSbostic #ifdef NAMED_BIND 1545f28da541Smiriam if (h_errno == TRY_AGAIN) 15464d50702aSeric statmsg = errstring(h_errno+E_DNSBASE); 1547f28da541Smiriam else 1548d4bd8f0eSbostic #endif 1549f28da541Smiriam { 15508557d168Seric if (errno != 0) 1551d87e85f3Seric statmsg = errstring(errno); 1552d87e85f3Seric else 1553d87e85f3Seric { 1554d87e85f3Seric #ifdef SMTP 1555d87e85f3Seric extern char SmtpError[]; 1556d87e85f3Seric 1557d87e85f3Seric statmsg = SmtpError; 15586c2c3107Seric #else /* SMTP */ 1559d87e85f3Seric statmsg = NULL; 15606c2c3107Seric #endif /* SMTP */ 1561d87e85f3Seric } 1562f28da541Smiriam } 1563d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1564d87e85f3Seric { 156587c9b3e7Seric (void) strcat(buf, ": "); 1566d87e85f3Seric (void) strcat(buf, statmsg); 15678557d168Seric } 1568198d9be0Seric statmsg = buf; 1569198d9be0Seric } 1570f170942cSeric #ifdef NAMED_BIND 1571f170942cSeric else if (stat == EX_NOHOST && h_errno != 0) 1572f170942cSeric { 15734d50702aSeric statmsg = errstring(h_errno + E_DNSBASE); 1574f170942cSeric (void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg); 1575f170942cSeric statmsg = buf; 1576f170942cSeric } 1577f170942cSeric #endif 157825a99e2eSeric else 1579d87e85f3Seric { 158025a99e2eSeric statmsg = SysExMsg[i]; 15817d55540cSeric if (*statmsg++ == ':') 15827d55540cSeric { 15837d55540cSeric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 15847d55540cSeric statmsg = buf; 15857d55540cSeric } 1586d87e85f3Seric } 1587588cad61Seric 1588588cad61Seric /* 1589588cad61Seric ** Print the message as appropriate 1590588cad61Seric */ 1591588cad61Seric 1592198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 1593e12d03eeSeric message(&statmsg[4], errstring(errno)); 159425a99e2eSeric else 159525a99e2eSeric { 1596c1f9df2cSeric Errors++; 1597e12d03eeSeric usrerr(statmsg, errstring(errno)); 159825a99e2eSeric } 159925a99e2eSeric 160025a99e2eSeric /* 160125a99e2eSeric ** Final cleanup. 160225a99e2eSeric ** Log a record of the transaction. Compute the new 160325a99e2eSeric ** ExitStat -- if we already had an error, stick with 160425a99e2eSeric ** that. 160525a99e2eSeric */ 160625a99e2eSeric 16072f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 160881161401Seric logdelivery(m, mci, &statmsg[4], e); 1609eb238f8cSeric 1610eb238f8cSeric if (stat != EX_TEMPFAIL) 1611eb238f8cSeric setstat(stat); 1612198d9be0Seric if (stat != EX_OK) 1613198d9be0Seric { 1614198d9be0Seric if (e->e_message != NULL) 1615198d9be0Seric free(e->e_message); 1616198d9be0Seric e->e_message = newstr(&statmsg[4]); 1617198d9be0Seric } 16188557d168Seric errno = 0; 1619d4bd8f0eSbostic #ifdef NAMED_BIND 1620f28da541Smiriam h_errno = 0; 1621d4bd8f0eSbostic #endif 1622eb238f8cSeric } 1623eb238f8cSeric /* 1624eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1625eb238f8cSeric ** 1626eb238f8cSeric ** Parameters: 162781161401Seric ** m -- the mailer info. Can be NULL for initial queue. 162881161401Seric ** mci -- the mailer connection info -- can be NULL if the 162981161401Seric ** log is occuring when no connection is active. 163081161401Seric ** stat -- the message to print for the status. 163181161401Seric ** e -- the current envelope. 1632eb238f8cSeric ** 1633eb238f8cSeric ** Returns: 1634eb238f8cSeric ** none 1635eb238f8cSeric ** 1636eb238f8cSeric ** Side Effects: 1637eb238f8cSeric ** none 1638eb238f8cSeric */ 1639eb238f8cSeric 164081161401Seric logdelivery(m, mci, stat, e) 164181161401Seric MAILER *m; 164281161401Seric register MCI *mci; 1643eb238f8cSeric char *stat; 1644b31e7f2bSeric register ENVELOPE *e; 16455cf56be3Seric { 1646eb238f8cSeric # ifdef LOG 1647d6acf3eeSeric char buf[512]; 16489507d1f9Seric 164948ed5d33Seric (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 165081161401Seric 165148ed5d33Seric if (m != NULL) 165271ff6caaSeric { 165348ed5d33Seric (void) strcat(buf, ", mailer="); 165448ed5d33Seric (void) strcat(buf, m->m_name); 165571ff6caaSeric } 165648ed5d33Seric 165748ed5d33Seric if (mci != NULL && mci->mci_host != NULL) 165871ff6caaSeric { 165971ff6caaSeric # ifdef DAEMON 1660e2f2f828Seric extern SOCKADDR CurHostAddr; 166148ed5d33Seric # endif 166271ff6caaSeric 166348ed5d33Seric (void) strcat(buf, ", relay="); 166448ed5d33Seric (void) strcat(buf, mci->mci_host); 166548ed5d33Seric 166648ed5d33Seric # ifdef DAEMON 166748ed5d33Seric (void) strcat(buf, " ("); 1668e2f2f828Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 166948ed5d33Seric (void) strcat(buf, ")"); 167071ff6caaSeric # endif 167171ff6caaSeric } 16729507d1f9Seric else 167348ed5d33Seric { 167448ed5d33Seric char *p = macvalue('h', e); 16759507d1f9Seric 167648ed5d33Seric if (p != NULL && p[0] != '\0') 167748ed5d33Seric { 167848ed5d33Seric (void) strcat(buf, ", relay="); 167948ed5d33Seric (void) strcat(buf, p); 168048ed5d33Seric } 168148ed5d33Seric } 1682d6acf3eeSeric 1683d6acf3eeSeric syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1684d6acf3eeSeric e->e_id, e->e_to, buf, stat); 16856c2c3107Seric # endif /* LOG */ 168625a99e2eSeric } 168725a99e2eSeric /* 168851552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 168925a99e2eSeric ** 169051552439Seric ** This can be made an arbitrary message separator by changing $l 169151552439Seric ** 16929b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 16939b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 16949b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 16959b6c17a6Seric ** this kind of antique garbage???? 169625a99e2eSeric ** 169725a99e2eSeric ** Parameters: 169851552439Seric ** fp -- the file to output to. 169951552439Seric ** m -- the mailer describing this entry. 170025a99e2eSeric ** 170125a99e2eSeric ** Returns: 170251552439Seric ** none 170325a99e2eSeric ** 170425a99e2eSeric ** Side Effects: 170551552439Seric ** outputs some text to fp. 170625a99e2eSeric */ 170725a99e2eSeric 1708b31e7f2bSeric putfromline(fp, m, e) 170951552439Seric register FILE *fp; 171051552439Seric register MAILER *m; 1711b31e7f2bSeric ENVELOPE *e; 171225a99e2eSeric { 17132bc47524Seric char *template = "\201l\n"; 171451552439Seric char buf[MAXLINE]; 171525a99e2eSeric 171657fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 171751552439Seric return; 171813bbc08cSeric 17192c7e1b8dSeric # ifdef UGLYUUCP 172057fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 172174b6e67bSeric { 1722ea09d6edSeric char *bang; 1723ea09d6edSeric char xbuf[MAXLINE]; 172474b6e67bSeric 1725ee4b0922Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 17266c2c3107Seric bang = strchr(buf, '!'); 172774b6e67bSeric if (bang == NULL) 172834fcca25Seric { 172934fcca25Seric errno = 0; 173034fcca25Seric syserr("554 No ! in UUCP From address! (%s given)", buf); 173134fcca25Seric } 173274b6e67bSeric else 1733588cad61Seric { 1734ea09d6edSeric *bang++ = '\0'; 17352bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1736ea09d6edSeric template = xbuf; 173774b6e67bSeric } 1738588cad61Seric } 17396c2c3107Seric # endif /* UGLYUUCP */ 1740b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 174177b52738Seric putline(buf, fp, m); 1742bc6e2962Seric } 1743bc6e2962Seric /* 174451552439Seric ** PUTBODY -- put the body of a message. 174551552439Seric ** 174651552439Seric ** Parameters: 174751552439Seric ** fp -- file to output onto. 174877b52738Seric ** m -- a mailer descriptor to control output format. 17499a6a5f55Seric ** e -- the envelope to put out. 175003c02fdeSeric ** separator -- if non-NULL, a message separator that must 175103c02fdeSeric ** not be permitted in the resulting message. 175251552439Seric ** 175351552439Seric ** Returns: 175451552439Seric ** none. 175551552439Seric ** 175651552439Seric ** Side Effects: 175751552439Seric ** The message is written onto fp. 175851552439Seric */ 175951552439Seric 176003c02fdeSeric putbody(fp, m, e, separator) 176151552439Seric FILE *fp; 1762588cad61Seric MAILER *m; 17639a6a5f55Seric register ENVELOPE *e; 176403c02fdeSeric char *separator; 176551552439Seric { 176677b52738Seric char buf[MAXLINE]; 176751552439Seric 176851552439Seric /* 176951552439Seric ** Output the body of the message 177051552439Seric */ 177151552439Seric 17729a6a5f55Seric if (e->e_dfp == NULL) 177351552439Seric { 17749a6a5f55Seric if (e->e_df != NULL) 17759a6a5f55Seric { 17769a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 17779a6a5f55Seric if (e->e_dfp == NULL) 17788f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 1779e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 17809a6a5f55Seric } 17819a6a5f55Seric else 178277b52738Seric putline("<<< No Message Collected >>>", fp, m); 17839a6a5f55Seric } 17849a6a5f55Seric if (e->e_dfp != NULL) 17859a6a5f55Seric { 17869a6a5f55Seric rewind(e->e_dfp); 178777b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 178824fc8aeeSeric { 178924fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1790d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 17913462ad9eSeric (void) putc('>', fp); 179203c02fdeSeric if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 179303c02fdeSeric { 179403c02fdeSeric /* possible separator */ 179503c02fdeSeric int sl = strlen(separator); 179603c02fdeSeric 179703c02fdeSeric if (strncmp(&buf[2], separator, sl) == 0) 179803c02fdeSeric (void) putc(' ', fp); 179903c02fdeSeric } 180077b52738Seric putline(buf, fp, m); 180124fc8aeeSeric } 180251552439Seric 18039a6a5f55Seric if (ferror(e->e_dfp)) 180451552439Seric { 180551552439Seric syserr("putbody: read error"); 180651552439Seric ExitStat = EX_IOERR; 180751552439Seric } 180851552439Seric } 180951552439Seric 18100890ba1fSeric /* some mailers want extra blank line at end of message */ 18110890ba1fSeric if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 18120890ba1fSeric putline("", fp, m); 18130890ba1fSeric 181451552439Seric (void) fflush(fp); 181551552439Seric if (ferror(fp) && errno != EPIPE) 181651552439Seric { 181751552439Seric syserr("putbody: write error"); 181851552439Seric ExitStat = EX_IOERR; 181951552439Seric } 182051552439Seric errno = 0; 182125a99e2eSeric } 182225a99e2eSeric /* 182325a99e2eSeric ** MAILFILE -- Send a message to a file. 182425a99e2eSeric ** 1825f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1826f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1827f129ec7dSeric ** rather than the real user. Obviously, this only works if 1828f129ec7dSeric ** sendmail runs as root. 1829f129ec7dSeric ** 1830588cad61Seric ** This could be done as a subordinate mailer, except that it 1831588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1832588cad61Seric ** view this as being sufficiently important as to include it 1833588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1834588cad61Seric ** to create another process plus some pipes to save the message. 1835588cad61Seric ** 183625a99e2eSeric ** Parameters: 183725a99e2eSeric ** filename -- the name of the file to send to. 18386259796dSeric ** ctladdr -- the controlling address header -- includes 18396259796dSeric ** the userid/groupid to be when sending. 184025a99e2eSeric ** 184125a99e2eSeric ** Returns: 184225a99e2eSeric ** The exit code associated with the operation. 184325a99e2eSeric ** 184425a99e2eSeric ** Side Effects: 184525a99e2eSeric ** none. 184625a99e2eSeric */ 184725a99e2eSeric 1848b31e7f2bSeric mailfile(filename, ctladdr, e) 184925a99e2eSeric char *filename; 18506259796dSeric ADDRESS *ctladdr; 1851b31e7f2bSeric register ENVELOPE *e; 185225a99e2eSeric { 185325a99e2eSeric register FILE *f; 185432d19d43Seric register int pid; 185515d084d5Seric int mode; 185625a99e2eSeric 1857671745f3Seric if (tTd(11, 1)) 1858671745f3Seric { 1859671745f3Seric printf("mailfile %s\n ctladdr=", filename); 1860671745f3Seric printaddr(ctladdr, FALSE); 1861671745f3Seric } 1862671745f3Seric 1863f170942cSeric if (e->e_xfp != NULL) 1864f170942cSeric fflush(e->e_xfp); 1865f170942cSeric 186632d19d43Seric /* 186732d19d43Seric ** Fork so we can change permissions here. 186832d19d43Seric ** Note that we MUST use fork, not vfork, because of 186932d19d43Seric ** the complications of calling subroutines, etc. 187032d19d43Seric */ 187132d19d43Seric 187232d19d43Seric DOFORK(fork); 187332d19d43Seric 187432d19d43Seric if (pid < 0) 187532d19d43Seric return (EX_OSERR); 187632d19d43Seric else if (pid == 0) 187732d19d43Seric { 187832d19d43Seric /* child -- actually write to file */ 1879f129ec7dSeric struct stat stb; 1880f129ec7dSeric 18812b9178d3Seric (void) setsignal(SIGINT, SIG_DFL); 18822b9178d3Seric (void) setsignal(SIGHUP, SIG_DFL); 18832b9178d3Seric (void) setsignal(SIGTERM, SIG_DFL); 18843462ad9eSeric (void) umask(OldUmask); 188595f16dc0Seric 1886f129ec7dSeric if (stat(filename, &stb) < 0) 18873a98e7eaSeric stb.st_mode = FileMode; 188815d084d5Seric mode = stb.st_mode; 188995f16dc0Seric 189095f16dc0Seric /* limit the errors to those actually caused in the child */ 189195f16dc0Seric errno = 0; 189295f16dc0Seric ExitStat = EX_OK; 189395f16dc0Seric 1894f129ec7dSeric if (bitset(0111, stb.st_mode)) 1895f129ec7dSeric exit(EX_CANTCREAT); 189603827b5fSeric if (ctladdr == NULL) 18978f9146b0Srick ctladdr = &e->e_from; 189815d084d5Seric else 189915d084d5Seric { 190015d084d5Seric /* ignore setuid and setgid bits */ 190115d084d5Seric mode &= ~(S_ISGID|S_ISUID); 190215d084d5Seric } 190315d084d5Seric 19048f9146b0Srick /* we have to open the dfile BEFORE setuid */ 19058f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 19068f9146b0Srick { 19078f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 190895f16dc0Seric if (e->e_dfp == NULL) 190995f16dc0Seric { 19108f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 1911e76a6a8fSeric e->e_df, e->e_to, e->e_from.q_paddr); 19128f9146b0Srick } 19138f9146b0Srick } 19148f9146b0Srick 191515d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1916e36b99e2Seric { 191795f16dc0Seric if (ctladdr->q_uid == 0) 191895f16dc0Seric { 1919898a126bSbostic (void) initgroups(DefUser, DefGid); 192095f16dc0Seric } 192195f16dc0Seric else 192295f16dc0Seric { 1923898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 1924898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 1925898a126bSbostic ctladdr->q_gid); 1926898a126bSbostic } 1927e36b99e2Seric } 192815d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1929e36b99e2Seric { 1930e36b99e2Seric if (ctladdr->q_uid == 0) 1931e36b99e2Seric (void) setuid(DefUid); 1932e36b99e2Seric else 19336259796dSeric (void) setuid(ctladdr->q_uid); 1934e36b99e2Seric } 193595f16dc0Seric FileName = filename; 193695f16dc0Seric LineNumber = 0; 19373a98e7eaSeric f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 193825a99e2eSeric if (f == NULL) 193995f16dc0Seric { 194008b25121Seric message("554 cannot open"); 194132d19d43Seric exit(EX_CANTCREAT); 194295f16dc0Seric } 194325a99e2eSeric 19440331ce05Seric putfromline(f, FileMailer, e); 19450331ce05Seric (*e->e_puthdr)(f, FileMailer, e); 19460331ce05Seric putline("\n", f, FileMailer); 194703c02fdeSeric (*e->e_putbody)(f, FileMailer, e, NULL); 19480331ce05Seric putline("\n", f, FileMailer); 194995f16dc0Seric if (ferror(f)) 195095f16dc0Seric { 195108b25121Seric message("451 I/O error"); 195295f16dc0Seric setstat(EX_IOERR); 195395f16dc0Seric } 1954ee4b0922Seric (void) xfclose(f, "mailfile", filename); 195532d19d43Seric (void) fflush(stdout); 1956e36b99e2Seric 195727628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1958c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 195995f16dc0Seric exit(ExitStat); 196013bbc08cSeric /*NOTREACHED*/ 196132d19d43Seric } 196232d19d43Seric else 196332d19d43Seric { 196432d19d43Seric /* parent -- wait for exit status */ 1965588cad61Seric int st; 196632d19d43Seric 1967588cad61Seric st = waitfor(pid); 1968*bf9bc890Seric if (WIFEXITED(st)) 1969*bf9bc890Seric return (WEXITSTATUS(st)); 1970588cad61Seric else 1971*bf9bc890Seric return (EX_UNAVAILABLE); 19728f9146b0Srick /*NOTREACHED*/ 197332d19d43Seric } 197425a99e2eSeric } 1975ea4dc939Seric /* 1976e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 1977e103b48fSeric ** 1978e103b48fSeric ** The signature describes how we are going to send this -- it 1979e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 1980e103b48fSeric ** an ordered list of MX hosts. 1981e103b48fSeric ** 1982e103b48fSeric ** Parameters: 1983e103b48fSeric ** m -- the mailer describing this host. 1984e103b48fSeric ** host -- the host name. 1985e103b48fSeric ** e -- the current envelope. 1986e103b48fSeric ** 1987e103b48fSeric ** Returns: 1988e103b48fSeric ** The signature for this host. 1989e103b48fSeric ** 1990e103b48fSeric ** Side Effects: 1991e103b48fSeric ** Can tweak the symbol table. 1992e103b48fSeric */ 1993e103b48fSeric 1994e103b48fSeric char * 1995e103b48fSeric hostsignature(m, host, e) 1996e103b48fSeric register MAILER *m; 1997e103b48fSeric char *host; 1998e103b48fSeric ENVELOPE *e; 1999e103b48fSeric { 2000e103b48fSeric register char *p; 2001e103b48fSeric register STAB *s; 2002e103b48fSeric int i; 2003e103b48fSeric int len; 2004e103b48fSeric #ifdef NAMED_BIND 2005e103b48fSeric int nmx; 2006e103b48fSeric auto int rcode; 2007bafdc4e5Seric char *hp; 2008bafdc4e5Seric char *endp; 2009516782b4Seric int oldoptions; 2010e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 2011e103b48fSeric #endif 2012e103b48fSeric 2013e103b48fSeric /* 2014e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 2015e103b48fSeric */ 2016e103b48fSeric 2017e103b48fSeric p = m->m_mailer; 2018e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2019e103b48fSeric { 2020e103b48fSeric /* just an ordinary mailer */ 2021e103b48fSeric return host; 2022e103b48fSeric } 2023e103b48fSeric 2024e103b48fSeric /* 2025e103b48fSeric ** If it is a numeric address, just return it. 2026e103b48fSeric */ 2027e103b48fSeric 2028e103b48fSeric if (host[0] == '[') 2029e103b48fSeric return host; 2030e103b48fSeric 2031e103b48fSeric /* 2032e103b48fSeric ** Look it up in the symbol table. 2033e103b48fSeric */ 2034e103b48fSeric 2035e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 2036e103b48fSeric if (s->s_hostsig != NULL) 2037e103b48fSeric return s->s_hostsig; 2038e103b48fSeric 2039e103b48fSeric /* 2040e103b48fSeric ** Not already there -- create a signature. 2041e103b48fSeric */ 2042e103b48fSeric 2043e103b48fSeric #ifdef NAMED_BIND 2044516782b4Seric if (ConfigLevel < 2) 2045516782b4Seric { 2046516782b4Seric oldoptions = _res.options; 2047516782b4Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 2048516782b4Seric } 2049516782b4Seric 2050bafdc4e5Seric for (hp = host; hp != NULL; hp = endp) 2051bafdc4e5Seric { 2052bafdc4e5Seric endp = strchr(hp, ':'); 2053bafdc4e5Seric if (endp != NULL) 2054bafdc4e5Seric *endp = '\0'; 2055bafdc4e5Seric 20567bf809e6Seric nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 20577d55540cSeric 2058e103b48fSeric if (nmx <= 0) 2059e103b48fSeric { 2060e103b48fSeric register MCI *mci; 2061e103b48fSeric extern int errno; 2062e103b48fSeric 2063e103b48fSeric /* update the connection info for this host */ 2064bafdc4e5Seric mci = mci_get(hp, m); 2065e103b48fSeric mci->mci_exitstat = rcode; 2066e103b48fSeric mci->mci_errno = errno; 2067f170942cSeric #ifdef NAMED_BIND 2068f170942cSeric mci->mci_herrno = h_errno; 2069f170942cSeric #endif 2070e103b48fSeric 2071e103b48fSeric /* and return the original host name as the signature */ 2072bafdc4e5Seric nmx = 1; 2073bafdc4e5Seric mxhosts[0] = hp; 2074e103b48fSeric } 2075e103b48fSeric 2076e103b48fSeric len = 0; 2077e103b48fSeric for (i = 0; i < nmx; i++) 2078e103b48fSeric { 2079e103b48fSeric len += strlen(mxhosts[i]) + 1; 2080e103b48fSeric } 2081bafdc4e5Seric if (s->s_hostsig != NULL) 2082bafdc4e5Seric len += strlen(s->s_hostsig) + 1; 2083bafdc4e5Seric p = xalloc(len); 2084bafdc4e5Seric if (s->s_hostsig != NULL) 2085bafdc4e5Seric { 2086bafdc4e5Seric (void) strcpy(p, s->s_hostsig); 2087bafdc4e5Seric free(s->s_hostsig); 2088bafdc4e5Seric s->s_hostsig = p; 2089bafdc4e5Seric p += strlen(p); 2090bafdc4e5Seric *p++ = ':'; 2091bafdc4e5Seric } 2092bafdc4e5Seric else 2093bafdc4e5Seric s->s_hostsig = p; 2094e103b48fSeric for (i = 0; i < nmx; i++) 2095e103b48fSeric { 2096e103b48fSeric if (i != 0) 2097e103b48fSeric *p++ = ':'; 2098e103b48fSeric strcpy(p, mxhosts[i]); 2099e103b48fSeric p += strlen(p); 2100e103b48fSeric } 2101bafdc4e5Seric if (endp != NULL) 2102bafdc4e5Seric *endp++ = ':'; 2103bafdc4e5Seric } 2104e103b48fSeric makelower(s->s_hostsig); 2105516782b4Seric if (ConfigLevel < 2) 2106516782b4Seric _res.options = oldoptions; 2107e103b48fSeric #else 2108e103b48fSeric /* not using BIND -- the signature is just the host name */ 2109e103b48fSeric s->s_hostsig = host; 2110e103b48fSeric #endif 2111e103b48fSeric if (tTd(17, 1)) 2112e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2113e103b48fSeric return s->s_hostsig; 2114e103b48fSeric } 2115