14227346bSdist /* 20942ea6aSbostic * Copyright (c) 1983 Eric P. Allman 3e70a7521Sbostic * Copyright (c) 1988 Regents of the University of California. 4e70a7521Sbostic * All rights reserved. 5e70a7521Sbostic * 63bc94712Sbostic * %sccs.include.redist.c% 74227346bSdist */ 84227346bSdist 94227346bSdist #ifndef lint 10*0e3bfef5Seric static char sccsid[] = "@(#)deliver.c 6.58 (Berkeley) 04/01/93"; 11e70a7521Sbostic #endif /* not lint */ 124227346bSdist 13fcde9cc8Sbostic #include "sendmail.h" 14a8c080f0Seric #include <signal.h> 15c77d1c25Seric #include <sys/stat.h> 16f28da541Smiriam #include <netdb.h> 17911693bfSbostic #include <errno.h> 18134746fbSeric #ifdef NAMED_BIND 19912a731aSbostic #include <arpa/nameser.h> 20912a731aSbostic #include <resolv.h> 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; 509c9e68d9Seric 519c9e68d9Seric /* determine actual delivery mode */ 529c9e68d9Seric if (mode == SM_DEFAULT) 539c9e68d9Seric { 549c9e68d9Seric extern bool shouldqueue(); 559c9e68d9Seric 569c9e68d9Seric mode = e->e_sendmode; 579c9e68d9Seric if (mode != SM_VERIFY && 589c9e68d9Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 599c9e68d9Seric mode = SM_QUEUE; 609c9e68d9Seric } 619c9e68d9Seric 629c9e68d9Seric if (tTd(13, 1)) 639c9e68d9Seric { 649c9e68d9Seric printf("\nSENDALL: mode %c, e_from ", mode); 659c9e68d9Seric printaddr(&e->e_from, FALSE); 669c9e68d9Seric printf("sendqueue:\n"); 679c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 689c9e68d9Seric } 699c9e68d9Seric 709c9e68d9Seric /* 719c9e68d9Seric ** Do any preprocessing necessary for the mode we are running. 729c9e68d9Seric ** Check to make sure the hop count is reasonable. 739c9e68d9Seric ** Delete sends to the sender in mailing lists. 749c9e68d9Seric */ 759c9e68d9Seric 769c9e68d9Seric CurEnv = e; 779c9e68d9Seric 789c9e68d9Seric if (e->e_hopcount > MaxHopCount) 799c9e68d9Seric { 809c9e68d9Seric errno = 0; 819c9e68d9Seric syserr("554 too many hops %d (%d max): from %s, to %s", 829c9e68d9Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 839c9e68d9Seric e->e_sendqueue->q_paddr); 849c9e68d9Seric return; 859c9e68d9Seric } 869c9e68d9Seric 879c9e68d9Seric if (!MeToo) 889c9e68d9Seric { 899c9e68d9Seric extern ADDRESS *recipient(); 909c9e68d9Seric 919c9e68d9Seric if (tTd(13, 5)) 929c9e68d9Seric { 939c9e68d9Seric printf("sendall: QDONTSEND "); 949c9e68d9Seric printaddr(&e->e_from, FALSE); 959c9e68d9Seric } 969c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 979c9e68d9Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 989c9e68d9Seric } 999c9e68d9Seric 100ce5531bdSeric /* 101ce5531bdSeric ** Handle alias owners. 102ce5531bdSeric ** 103ce5531bdSeric ** We scan up the q_alias chain looking for owners. 104ce5531bdSeric ** We discard owners that are the same as the return path. 105ce5531bdSeric */ 106ce5531bdSeric 107ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 108ce5531bdSeric { 109ce5531bdSeric register struct address *a; 110ce5531bdSeric 111ce5531bdSeric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 112ce5531bdSeric continue; 113ce5531bdSeric if (a != NULL) 114ce5531bdSeric q->q_owner = a->q_owner; 115ce5531bdSeric 116ce5531bdSeric if (q->q_owner != NULL && 117ce5531bdSeric !bitset(QDONTSEND, q->q_flags) && 118ce5531bdSeric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 119ce5531bdSeric q->q_owner = NULL; 120ce5531bdSeric } 121ce5531bdSeric 122ce5531bdSeric owner = ""; 123ce5531bdSeric otherowners = 1; 124ce5531bdSeric while (owner != NULL && otherowners > 0) 125ce5531bdSeric { 126ce5531bdSeric owner = NULL; 127ce5531bdSeric otherowners = 0; 128ce5531bdSeric 129ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 130ce5531bdSeric { 131ce5531bdSeric if (bitset(QDONTSEND, q->q_flags)) 132ce5531bdSeric continue; 133ce5531bdSeric 134ce5531bdSeric if (q->q_owner != NULL) 135ce5531bdSeric { 136ce5531bdSeric if (owner == NULL) 137ce5531bdSeric owner = q->q_owner; 138ce5531bdSeric else if (owner != q->q_owner) 139ce5531bdSeric { 140ce5531bdSeric if (strcmp(owner, q->q_owner) == 0) 141ce5531bdSeric { 142ce5531bdSeric /* make future comparisons cheap */ 143ce5531bdSeric q->q_owner = owner; 144ce5531bdSeric } 145ce5531bdSeric else 146ce5531bdSeric { 147ce5531bdSeric otherowners++; 148ce5531bdSeric } 149ce5531bdSeric owner = q->q_owner; 150ce5531bdSeric } 151ce5531bdSeric } 152ce5531bdSeric else 153ce5531bdSeric { 154ce5531bdSeric otherowners++; 155ce5531bdSeric } 156ce5531bdSeric } 157ce5531bdSeric 158ce5531bdSeric if (owner != NULL && otherowners > 0) 159ce5531bdSeric { 160ce5531bdSeric extern HDR *copyheader(); 161ce5531bdSeric extern ADDRESS *copyqueue(); 162ce5531bdSeric 163ce5531bdSeric /* 164ce5531bdSeric ** Split this envelope into two. 165ce5531bdSeric */ 166ce5531bdSeric 167ce5531bdSeric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 168ce5531bdSeric *ee = *e; 169ce5531bdSeric ee->e_id = NULL; 170ce5531bdSeric (void) queuename(ee, '\0'); 171ce5531bdSeric 172ce5531bdSeric if (tTd(13, 1)) 173ce5531bdSeric printf("sendall: split %s into %s\n", 174ce5531bdSeric e->e_id, ee->e_id); 175ce5531bdSeric 176ce5531bdSeric ee->e_header = copyheader(e->e_header); 177ce5531bdSeric ee->e_sendqueue = copyqueue(e->e_sendqueue); 178ce5531bdSeric ee->e_errorqueue = copyqueue(e->e_errorqueue); 179ce5531bdSeric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS); 180ce5531bdSeric setsender(owner, ee, NULL, TRUE); 181ce5531bdSeric if (tTd(13, 5)) 182ce5531bdSeric { 183ce5531bdSeric printf("sendall(split): QDONTSEND "); 184ce5531bdSeric printaddr(&ee->e_from, FALSE); 185ce5531bdSeric } 186ce5531bdSeric ee->e_from.q_flags |= QDONTSEND; 187ce5531bdSeric ee->e_dfp = NULL; 188ce5531bdSeric ee->e_xfp = NULL; 189ce5531bdSeric ee->e_lockfp = NULL; 190ce5531bdSeric ee->e_df = NULL; 191ce5531bdSeric ee->e_errormode = EM_MAIL; 192ce5531bdSeric ee->e_sibling = splitenv; 193ce5531bdSeric splitenv = ee; 194ce5531bdSeric 195ce5531bdSeric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 196ce5531bdSeric if (q->q_owner == owner) 197ce5531bdSeric q->q_flags |= QDONTSEND; 198ce5531bdSeric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 199ce5531bdSeric if (q->q_owner != owner) 200ce5531bdSeric q->q_flags |= QDONTSEND; 201ce5531bdSeric 202ce5531bdSeric if (e->e_df != NULL && mode != SM_VERIFY) 203ce5531bdSeric { 204ce5531bdSeric ee->e_dfp = NULL; 205ce5531bdSeric ee->e_df = newstr(queuename(ee, 'd')); 206ce5531bdSeric if (link(e->e_df, ee->e_df) < 0) 207ce5531bdSeric { 208ce5531bdSeric syserr("sendall: link(%s, %s)", 209ce5531bdSeric e->e_df, ee->e_df); 210ce5531bdSeric } 211ce5531bdSeric } 212ce5531bdSeric 213ce5531bdSeric if (mode != SM_VERIFY) 214ce5531bdSeric openxscript(ee); 215ce5531bdSeric #ifdef LOG 216ce5531bdSeric if (LogLevel > 4) 217ce5531bdSeric syslog(LOG_INFO, "%s: clone %s", 218ce5531bdSeric ee->e_id, e->e_id); 219ce5531bdSeric #endif 220ce5531bdSeric } 221ce5531bdSeric } 222ce5531bdSeric 223ce5531bdSeric if (owner != NULL) 224ce5531bdSeric { 225ce5531bdSeric setsender(owner, e, NULL, TRUE); 226ce5531bdSeric if (tTd(13, 5)) 227ce5531bdSeric { 228ce5531bdSeric printf("sendall(owner): QDONTSEND "); 229ce5531bdSeric printaddr(&e->e_from, FALSE); 230ce5531bdSeric } 231ce5531bdSeric e->e_from.q_flags |= QDONTSEND; 232ce5531bdSeric e->e_errormode = EM_MAIL; 233ce5531bdSeric } 234ce5531bdSeric 235c1ac89b1Seric # ifdef QUEUE 236c1ac89b1Seric if ((mode == SM_QUEUE || mode == SM_FORK || 237c1ac89b1Seric (mode != SM_VERIFY && SuperSafe)) && 238c1ac89b1Seric !bitset(EF_INQUEUE, e->e_flags)) 239c1ac89b1Seric { 240c1ac89b1Seric /* be sure everything is instantiated in the queue */ 241c1ac89b1Seric queueup(e, TRUE, mode == SM_QUEUE); 242ce5531bdSeric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 243ce5531bdSeric queueup(ee, TRUE, mode == SM_QUEUE); 244c1ac89b1Seric } 245c1ac89b1Seric #endif /* QUEUE */ 246c1ac89b1Seric 247ce5531bdSeric if (splitenv != NULL) 248ce5531bdSeric { 249ce5531bdSeric if (tTd(13, 1)) 250ce5531bdSeric { 251ce5531bdSeric printf("\nsendall: Split queue; remaining queue:\n"); 252ce5531bdSeric printaddr(e->e_sendqueue, TRUE); 253ce5531bdSeric } 254ce5531bdSeric 255ce5531bdSeric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 256ce5531bdSeric { 257ce5531bdSeric CurEnv = ee; 258ce5531bdSeric sendenvelope(ee, mode); 259ce5531bdSeric } 260ce5531bdSeric 261ce5531bdSeric CurEnv = e; 262ce5531bdSeric } 263ce5531bdSeric sendenvelope(e, mode); 264ce5531bdSeric 265ce5531bdSeric for (; splitenv != NULL; splitenv = splitenv->e_sibling) 266ce5531bdSeric dropenvelope(splitenv); 267ce5531bdSeric } 268ce5531bdSeric 269ce5531bdSeric sendenvelope(e, mode) 270ce5531bdSeric register ENVELOPE *e; 271ce5531bdSeric char mode; 272ce5531bdSeric { 273ce5531bdSeric bool oldverbose; 274ce5531bdSeric int pid; 275ce5531bdSeric register ADDRESS *q; 276ce5531bdSeric #ifdef LOCKF 277ce5531bdSeric struct flock lfd; 278ce5531bdSeric #endif 279ce5531bdSeric 280ce5531bdSeric oldverbose = Verbose; 281ce5531bdSeric e->e_statmsg = NULL; 282c1ac89b1Seric switch (mode) 283c1ac89b1Seric { 284c1ac89b1Seric case SM_VERIFY: 285c1ac89b1Seric Verbose = TRUE; 286c1ac89b1Seric break; 287c1ac89b1Seric 288c1ac89b1Seric case SM_QUEUE: 289c1ac89b1Seric queueonly: 290c1ac89b1Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 291c1ac89b1Seric return; 292c1ac89b1Seric 293c1ac89b1Seric case SM_FORK: 294c1ac89b1Seric if (e->e_xfp != NULL) 295c1ac89b1Seric (void) fflush(e->e_xfp); 296c1ac89b1Seric 297c1ac89b1Seric # ifdef LOCKF 298c1ac89b1Seric /* 299c1ac89b1Seric ** Since lockf has the interesting semantic that the 300c1ac89b1Seric ** lock is lost when we fork, we have to risk losing 301c1ac89b1Seric ** the lock here by closing before the fork, and then 302c1ac89b1Seric ** trying to get it back in the child. 303c1ac89b1Seric */ 304c1ac89b1Seric 305c1ac89b1Seric if (e->e_lockfp != NULL) 306c1ac89b1Seric { 307c1ac89b1Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 308c1ac89b1Seric e->e_lockfp = NULL; 309c1ac89b1Seric } 310c1ac89b1Seric # endif /* LOCKF */ 311c1ac89b1Seric 312c1ac89b1Seric pid = fork(); 313c1ac89b1Seric if (pid < 0) 314c1ac89b1Seric { 315c1ac89b1Seric goto queueonly; 316c1ac89b1Seric } 317c1ac89b1Seric else if (pid > 0) 318c1ac89b1Seric { 319c1ac89b1Seric /* be sure we leave the temp files to our child */ 320c1ac89b1Seric e->e_id = e->e_df = NULL; 321c1ac89b1Seric # ifndef LOCKF 322c1ac89b1Seric if (e->e_lockfp != NULL) 323c1ac89b1Seric { 324c1ac89b1Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 325c1ac89b1Seric e->e_lockfp = NULL; 326c1ac89b1Seric } 327c1ac89b1Seric # endif 328c1ac89b1Seric 329c1ac89b1Seric /* close any random open files in the envelope */ 330c1ac89b1Seric if (e->e_dfp != NULL) 331c1ac89b1Seric { 332c1ac89b1Seric (void) xfclose(e->e_dfp, "sendenvelope", "dfp"); 333c1ac89b1Seric e->e_dfp = NULL; 334c1ac89b1Seric } 335c1ac89b1Seric if (e->e_xfp != NULL) 336c1ac89b1Seric { 337c1ac89b1Seric (void) xfclose(e->e_xfp, "sendenvelope", "xfp"); 338c1ac89b1Seric e->e_xfp = NULL; 339c1ac89b1Seric } 340c1ac89b1Seric return; 341c1ac89b1Seric } 342c1ac89b1Seric 343c1ac89b1Seric /* double fork to avoid zombies */ 344c1ac89b1Seric if (fork() > 0) 345c1ac89b1Seric exit(EX_OK); 346c1ac89b1Seric 347c1ac89b1Seric /* be sure we are immune from the terminal */ 348c1ac89b1Seric disconnect(FALSE, e); 349c1ac89b1Seric 350c1ac89b1Seric # ifdef LOCKF 351c1ac89b1Seric /* 352c1ac89b1Seric ** Now try to get our lock back. 353c1ac89b1Seric */ 354c1ac89b1Seric 355c1ac89b1Seric lfd.l_type = F_WRLCK; 356c1ac89b1Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 357c1ac89b1Seric e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 358c1ac89b1Seric if (e->e_lockfp == NULL || 359c1ac89b1Seric fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 360c1ac89b1Seric { 361c1ac89b1Seric /* oops.... lost it */ 362c1ac89b1Seric if (tTd(13, 1)) 363c1ac89b1Seric printf("sendenvelope: %s lost lock: lockfp=%x, %s\n", 364c1ac89b1Seric e->e_id, e->e_lockfp, errstring(errno)); 365c1ac89b1Seric 366c1ac89b1Seric # ifdef LOG 367c1ac89b1Seric if (LogLevel > 29) 368c1ac89b1Seric syslog(LOG_NOTICE, "%s: lost lock: %m", 369c1ac89b1Seric e->e_id); 370c1ac89b1Seric # endif /* LOG */ 371c1ac89b1Seric exit(EX_OK); 372c1ac89b1Seric } 373c1ac89b1Seric # endif /* LOCKF */ 374c1ac89b1Seric 375c1ac89b1Seric /* 376c1ac89b1Seric ** Close any cached connections. 377c1ac89b1Seric ** 378c1ac89b1Seric ** We don't send the QUIT protocol because the parent 379c1ac89b1Seric ** still knows about the connection. 380c1ac89b1Seric ** 381c1ac89b1Seric ** This should only happen when delivering an error 382c1ac89b1Seric ** message. 383c1ac89b1Seric */ 384c1ac89b1Seric 385c1ac89b1Seric mci_flush(FALSE, NULL); 386c1ac89b1Seric 387c1ac89b1Seric break; 388c1ac89b1Seric } 389c1ac89b1Seric 390c1ac89b1Seric /* 3919c9e68d9Seric ** Run through the list and send everything. 3929c9e68d9Seric */ 3939c9e68d9Seric 3949c9e68d9Seric e->e_nsent = 0; 3959c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 3969c9e68d9Seric { 3979c9e68d9Seric if (mode == SM_VERIFY) 3989c9e68d9Seric { 3999c9e68d9Seric e->e_to = q->q_paddr; 4009c9e68d9Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4019c9e68d9Seric message("deliverable"); 4029c9e68d9Seric } 4039c9e68d9Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 4049c9e68d9Seric { 4059c9e68d9Seric # ifdef QUEUE 4069c9e68d9Seric /* 4079c9e68d9Seric ** Checkpoint the send list every few addresses 4089c9e68d9Seric */ 4099c9e68d9Seric 4109c9e68d9Seric if (e->e_nsent >= CheckpointInterval) 4119c9e68d9Seric { 4129c9e68d9Seric queueup(e, TRUE, FALSE); 4139c9e68d9Seric e->e_nsent = 0; 4149c9e68d9Seric } 4159c9e68d9Seric # endif /* QUEUE */ 4169c9e68d9Seric (void) deliver(e, q); 4179c9e68d9Seric } 4189c9e68d9Seric } 4199c9e68d9Seric Verbose = oldverbose; 4209c9e68d9Seric 4219c9e68d9Seric /* 4229c9e68d9Seric ** Now run through and check for errors. 4239c9e68d9Seric */ 4249c9e68d9Seric 4259c9e68d9Seric if (mode == SM_VERIFY) 4269c9e68d9Seric { 4279c9e68d9Seric return; 4289c9e68d9Seric } 4299c9e68d9Seric 4309c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 4319c9e68d9Seric { 4329c9e68d9Seric if (tTd(13, 3)) 4339c9e68d9Seric { 4349c9e68d9Seric printf("Checking "); 4359c9e68d9Seric printaddr(q, FALSE); 4369c9e68d9Seric } 4379c9e68d9Seric 4389c9e68d9Seric /* only send errors if the message failed */ 439ce5531bdSeric if (!bitset(QBADADDR, q->q_flags) || 440ce5531bdSeric bitset(QDONTSEND, q->q_flags)) 4419c9e68d9Seric continue; 4429c9e68d9Seric 4439c9e68d9Seric e->e_flags |= EF_FATALERRS; 4449c9e68d9Seric 4459c9e68d9Seric if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0) 4469c9e68d9Seric (void) sendtolist(e->e_from.q_paddr, NULL, 4479c9e68d9Seric &e->e_errorqueue, e); 4489c9e68d9Seric } 4499c9e68d9Seric 4509c9e68d9Seric if (mode == SM_FORK) 4519c9e68d9Seric finis(); 4529c9e68d9Seric } 4539c9e68d9Seric /* 4549c9e68d9Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 4559c9e68d9Seric ** 4569c9e68d9Seric ** This MUST be a macro, since after a vfork we are running 4579c9e68d9Seric ** two processes on the same stack!!! 4589c9e68d9Seric ** 4599c9e68d9Seric ** Parameters: 4609c9e68d9Seric ** none. 4619c9e68d9Seric ** 4629c9e68d9Seric ** Returns: 4639c9e68d9Seric ** From a macro??? You've got to be kidding! 4649c9e68d9Seric ** 4659c9e68d9Seric ** Side Effects: 4669c9e68d9Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 4679c9e68d9Seric ** pid of child in parent, zero in child. 4689c9e68d9Seric ** -1 on unrecoverable error. 4699c9e68d9Seric ** 4709c9e68d9Seric ** Notes: 4719c9e68d9Seric ** I'm awfully sorry this looks so awful. That's 4729c9e68d9Seric ** vfork for you..... 4739c9e68d9Seric */ 4749c9e68d9Seric 4759c9e68d9Seric # define NFORKTRIES 5 4769c9e68d9Seric 4779c9e68d9Seric # ifndef FORK 4789c9e68d9Seric # define FORK fork 4799c9e68d9Seric # endif 4809c9e68d9Seric 4819c9e68d9Seric # define DOFORK(fORKfN) \ 4829c9e68d9Seric {\ 4839c9e68d9Seric register int i;\ 4849c9e68d9Seric \ 4859c9e68d9Seric for (i = NFORKTRIES; --i >= 0; )\ 4869c9e68d9Seric {\ 4879c9e68d9Seric pid = fORKfN();\ 4889c9e68d9Seric if (pid >= 0)\ 4899c9e68d9Seric break;\ 4909c9e68d9Seric if (i > 0)\ 4919c9e68d9Seric sleep((unsigned) NFORKTRIES - i);\ 4929c9e68d9Seric }\ 4939c9e68d9Seric } 4949c9e68d9Seric /* 4959c9e68d9Seric ** DOFORK -- simple fork interface to DOFORK. 4969c9e68d9Seric ** 4979c9e68d9Seric ** Parameters: 4989c9e68d9Seric ** none. 4999c9e68d9Seric ** 5009c9e68d9Seric ** Returns: 5019c9e68d9Seric ** pid of child in parent. 5029c9e68d9Seric ** zero in child. 5039c9e68d9Seric ** -1 on error. 5049c9e68d9Seric ** 5059c9e68d9Seric ** Side Effects: 5069c9e68d9Seric ** returns twice, once in parent and once in child. 5079c9e68d9Seric */ 5089c9e68d9Seric 5099c9e68d9Seric dofork() 5109c9e68d9Seric { 5119c9e68d9Seric register int pid; 5129c9e68d9Seric 5139c9e68d9Seric DOFORK(fork); 5149c9e68d9Seric return (pid); 5159c9e68d9Seric } 5169c9e68d9Seric /* 51713bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 51813bbc08cSeric ** 51913bbc08cSeric ** This routine delivers to everyone on the same host as the 52013bbc08cSeric ** user on the head of the list. It is clever about mailers 52113bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 52213bbc08cSeric ** that it will deliver to all these addresses however -- so 52313bbc08cSeric ** deliver should be called once for each address on the 52413bbc08cSeric ** list. 52525a99e2eSeric ** 52625a99e2eSeric ** Parameters: 527588cad61Seric ** e -- the envelope to deliver. 528c77d1c25Seric ** firstto -- head of the address list to deliver to. 52925a99e2eSeric ** 53025a99e2eSeric ** Returns: 53125a99e2eSeric ** zero -- successfully delivered. 53225a99e2eSeric ** else -- some failure, see ExitStat for more info. 53325a99e2eSeric ** 53425a99e2eSeric ** Side Effects: 53525a99e2eSeric ** The standard input is passed off to someone. 53625a99e2eSeric */ 53725a99e2eSeric 538588cad61Seric deliver(e, firstto) 539588cad61Seric register ENVELOPE *e; 540c77d1c25Seric ADDRESS *firstto; 54125a99e2eSeric { 54278442df3Seric char *host; /* host being sent to */ 54378442df3Seric char *user; /* user being sent to */ 54425a99e2eSeric char **pvp; 5455dfc646bSeric register char **mvp; 54625a99e2eSeric register char *p; 547588cad61Seric register MAILER *m; /* mailer for this recipient */ 5486259796dSeric ADDRESS *ctladdr; 549b31e7f2bSeric register MCI *mci; 550c77d1c25Seric register ADDRESS *to = firstto; 551c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 552772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 553911693bfSbostic int rcode; /* response code */ 554e103b48fSeric char *firstsig; /* signature of firstto */ 5559c9e68d9Seric int pid; 5569c9e68d9Seric char *curhost; 5579c9e68d9Seric int mpvect[2]; 5589c9e68d9Seric int rpvect[2]; 559ee6bf8dfSeric char *pv[MAXPV+1]; 560579ef0ddSeric char tobuf[TOBUFSIZE]; /* text line of to people */ 561ee6bf8dfSeric char buf[MAXNAME]; 562c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 563fabb3bd4Seric extern int checkcompat(); 564ee6bf8dfSeric extern ADDRESS *getctladdr(); 565ee6bf8dfSeric extern char *remotename(); 566e103b48fSeric extern char *hostsignature(); 5679c9e68d9Seric extern FILE *fdopen(); 56825a99e2eSeric 56935490626Seric errno = 0; 570ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5715dfc646bSeric return (0); 57225a99e2eSeric 573134746fbSeric #ifdef NAMED_BIND 574912a731aSbostic /* unless interactive, try twice, over a minute */ 575912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 576912a731aSbostic _res.retrans = 30; 577912a731aSbostic _res.retry = 2; 578912a731aSbostic } 579d4bd8f0eSbostic #endif 580912a731aSbostic 58151552439Seric m = to->q_mailer; 58251552439Seric host = to->q_host; 583c9be6216Seric CurEnv = e; /* just in case */ 58451552439Seric 5856ef48975Seric if (tTd(10, 1)) 5865dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 58751552439Seric m->m_mno, host, to->q_user); 588f3dbc832Seric 589f3dbc832Seric /* 590f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 591f3dbc832Seric ** connections now, just mark these addresses and return. 592f3dbc832Seric ** This is useful if we want to batch connections to 593f3dbc832Seric ** reduce load. This will cause the messages to be 594f3dbc832Seric ** queued up, and a daemon will come along to send the 595f3dbc832Seric ** messages later. 596f3dbc832Seric ** This should be on a per-mailer basis. 597f3dbc832Seric */ 598f3dbc832Seric 59919c47125Seric if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) && 60019c47125Seric bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 601f3dbc832Seric { 602f3dbc832Seric for (; to != NULL; to = to->q_next) 603f4560e80Seric { 604ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 605c6e18ac5Seric to->q_mailer != m) 606f4560e80Seric continue; 607f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 608588cad61Seric e->e_to = to->q_paddr; 60908b25121Seric message("queued"); 6102f624c86Seric if (LogLevel > 8) 61181161401Seric logdelivery(m, NULL, "queued", e); 612f4560e80Seric } 613588cad61Seric e->e_to = NULL; 614f3dbc832Seric return (0); 615f3dbc832Seric } 616f3dbc832Seric 61725a99e2eSeric /* 6185dfc646bSeric ** Do initial argv setup. 6195dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 6205dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 6215dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 6225dfc646bSeric ** code does not check for 'pv' overflow; this places a 6235dfc646bSeric ** manifest lower limit of 4 for MAXPV. 6243bea8136Seric ** The from address rewrite is expected to make 6253bea8136Seric ** the address relative to the other end. 6265dfc646bSeric */ 6275dfc646bSeric 62878442df3Seric /* rewrite from address, using rewriting rules */ 629ee4b0922Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE, 6309c69ca01Seric TRUE, FALSE, e)); 631ee4b0922Seric define('g', rpathbuf, e); /* translated return path */ 632588cad61Seric define('h', host, e); /* to host */ 6335dfc646bSeric Errors = 0; 6345dfc646bSeric pvp = pv; 6355dfc646bSeric *pvp++ = m->m_argv[0]; 6365dfc646bSeric 6375dfc646bSeric /* insert -f or -r flag as appropriate */ 63857fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6395dfc646bSeric { 64057fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 6415dfc646bSeric *pvp++ = "-f"; 6425dfc646bSeric else 6435dfc646bSeric *pvp++ = "-r"; 644c23ed322Seric *pvp++ = newstr(rpathbuf); 6455dfc646bSeric } 6465dfc646bSeric 6475dfc646bSeric /* 6485dfc646bSeric ** Append the other fixed parts of the argv. These run 6495dfc646bSeric ** up to the first entry containing "$u". There can only 6505dfc646bSeric ** be one of these, and there are only a few more slots 6515dfc646bSeric ** in the pv after it. 6525dfc646bSeric */ 6535dfc646bSeric 6545dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 6555dfc646bSeric { 6562bc47524Seric /* can't use strchr here because of sign extension problems */ 6572bc47524Seric while (*p != '\0') 6582bc47524Seric { 6592bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 6602bc47524Seric { 6612bc47524Seric if (*p == 'u') 6625dfc646bSeric break; 6632bc47524Seric } 6642bc47524Seric } 6652bc47524Seric 6662bc47524Seric if (*p != '\0') 6675dfc646bSeric break; 6685dfc646bSeric 6695dfc646bSeric /* this entry is safe -- go ahead and process it */ 670588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 6715dfc646bSeric *pvp++ = newstr(buf); 6725dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 6735dfc646bSeric { 67408b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 6755dfc646bSeric return (-1); 6765dfc646bSeric } 6775dfc646bSeric } 678c579ef51Seric 67933db8731Seric /* 68033db8731Seric ** If we have no substitution for the user name in the argument 68133db8731Seric ** list, we know that we must supply the names otherwise -- and 68233db8731Seric ** SMTP is the answer!! 68333db8731Seric */ 68433db8731Seric 6855dfc646bSeric if (*mvp == NULL) 686c579ef51Seric { 687c579ef51Seric /* running SMTP */ 6882c7e1b8dSeric # ifdef SMTP 689c579ef51Seric clever = TRUE; 690c579ef51Seric *pvp = NULL; 6916c2c3107Seric # else /* SMTP */ 69233db8731Seric /* oops! we don't implement SMTP */ 69308b25121Seric syserr("554 SMTP style mailer"); 6942c7e1b8dSeric return (EX_SOFTWARE); 6956c2c3107Seric # endif /* SMTP */ 696c579ef51Seric } 6975dfc646bSeric 6985dfc646bSeric /* 6995dfc646bSeric ** At this point *mvp points to the argument with $u. We 7005dfc646bSeric ** run through our address list and append all the addresses 7015dfc646bSeric ** we can. If we run out of space, do not fret! We can 7025dfc646bSeric ** always send another copy later. 7035dfc646bSeric */ 7045dfc646bSeric 7055dfc646bSeric tobuf[0] = '\0'; 706588cad61Seric e->e_to = tobuf; 7076259796dSeric ctladdr = NULL; 708e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7095dfc646bSeric for (; to != NULL; to = to->q_next) 7105dfc646bSeric { 7115dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 71257fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7135dfc646bSeric break; 7145dfc646bSeric 7155dfc646bSeric /* if already sent or not for this host, don't send */ 716ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 717e103b48fSeric to->q_mailer != firstto->q_mailer || 718e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7195dfc646bSeric continue; 7206259796dSeric 7214b22ea87Seric /* avoid overflowing tobuf */ 722aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7234b22ea87Seric break; 7244b22ea87Seric 7256ef48975Seric if (tTd(10, 1)) 726772e6e50Seric { 727772e6e50Seric printf("\nsend to "); 728772e6e50Seric printaddr(to, FALSE); 729772e6e50Seric } 730772e6e50Seric 7316259796dSeric /* compute effective uid/gid when sending */ 7327da1035fSeric if (to->q_mailer == ProgMailer) 7336259796dSeric ctladdr = getctladdr(to); 7346259796dSeric 7355dfc646bSeric user = to->q_user; 736588cad61Seric e->e_to = to->q_paddr; 73775f1ade9Seric if (tTd(10, 5)) 73875f1ade9Seric { 73975f1ade9Seric printf("deliver: QDONTSEND "); 74075f1ade9Seric printaddr(to, FALSE); 74175f1ade9Seric } 742ee4b0922Seric to->q_flags |= QDONTSEND; 7435dfc646bSeric 7445dfc646bSeric /* 7455dfc646bSeric ** Check to see that these people are allowed to 7465dfc646bSeric ** talk to each other. 7472a6e0786Seric */ 7482a6e0786Seric 74969582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 75069582d2fSeric { 75169582d2fSeric NoReturn = TRUE; 75208b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 75381161401Seric giveresponse(EX_UNAVAILABLE, m, NULL, e); 75469582d2fSeric continue; 75569582d2fSeric } 756fabb3bd4Seric rcode = checkcompat(to, e); 7571793c9c5Seric if (rcode != EX_OK) 7585dfc646bSeric { 75981161401Seric giveresponse(rcode, m, NULL, e); 7605dfc646bSeric continue; 7615dfc646bSeric } 7622a6e0786Seric 7632a6e0786Seric /* 7649ec9501bSeric ** Strip quote bits from names if the mailer is dumb 7659ec9501bSeric ** about them. 76625a99e2eSeric */ 76725a99e2eSeric 76857fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 76925a99e2eSeric { 7701d8f1806Seric stripquotes(user); 7711d8f1806Seric stripquotes(host); 77225a99e2eSeric } 77325a99e2eSeric 774cdb828c5Seric /* hack attack -- delivermail compatibility */ 775cdb828c5Seric if (m == ProgMailer && *user == '|') 776cdb828c5Seric user++; 777cdb828c5Seric 77825a99e2eSeric /* 7793efaed6eSeric ** If an error message has already been given, don't 7803efaed6eSeric ** bother to send to this address. 7813efaed6eSeric ** 7823efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 7833efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 7843efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 7853efaed6eSeric */ 7863efaed6eSeric 7876cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 7883efaed6eSeric continue; 7893efaed6eSeric 790f2fec898Seric /* save statistics.... */ 791588cad61Seric markstats(e, to); 792f2fec898Seric 7933efaed6eSeric /* 79425a99e2eSeric ** See if this user name is "special". 79525a99e2eSeric ** If the user name has a slash in it, assume that this 79651552439Seric ** is a file -- send it off without further ado. Note 79751552439Seric ** that this type of addresses is not processed along 79851552439Seric ** with the others, so we fudge on the To person. 79925a99e2eSeric */ 80025a99e2eSeric 8012c017f8dSeric if (m == FileMailer) 80225a99e2eSeric { 803b31e7f2bSeric rcode = mailfile(user, getctladdr(to), e); 80481161401Seric giveresponse(rcode, m, NULL, e); 805dde5acadSeric if (rcode == EX_OK) 806dde5acadSeric to->q_flags |= QSENT; 8075dfc646bSeric continue; 80825a99e2eSeric } 80925a99e2eSeric 81013bbc08cSeric /* 81113bbc08cSeric ** Address is verified -- add this user to mailer 81213bbc08cSeric ** argv, and add it to the print list of recipients. 81313bbc08cSeric */ 81413bbc08cSeric 815508daeccSeric /* link together the chain of recipients */ 816508daeccSeric to->q_tchain = tochain; 817508daeccSeric tochain = to; 818508daeccSeric 8195dfc646bSeric /* create list of users for error messages */ 820db8841e9Seric (void) strcat(tobuf, ","); 821db8841e9Seric (void) strcat(tobuf, to->q_paddr); 822588cad61Seric define('u', user, e); /* to user */ 823588cad61Seric define('z', to->q_home, e); /* user's home */ 8245dfc646bSeric 825c579ef51Seric /* 826508daeccSeric ** Expand out this user into argument list. 827c579ef51Seric */ 828c579ef51Seric 829508daeccSeric if (!clever) 830c579ef51Seric { 831588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8325dfc646bSeric *pvp++ = newstr(buf); 8335dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 8345dfc646bSeric { 8355dfc646bSeric /* allow some space for trailing parms */ 8365dfc646bSeric break; 8375dfc646bSeric } 8385dfc646bSeric } 839c579ef51Seric } 8405dfc646bSeric 841145b49b1Seric /* see if any addresses still exist */ 842145b49b1Seric if (tobuf[0] == '\0') 843c579ef51Seric { 844588cad61Seric define('g', (char *) NULL, e); 845145b49b1Seric return (0); 846c579ef51Seric } 847145b49b1Seric 8485dfc646bSeric /* print out messages as full list */ 84963780dbdSeric e->e_to = tobuf + 1; 8505dfc646bSeric 8515dfc646bSeric /* 8525dfc646bSeric ** Fill out any parameters after the $u parameter. 8535dfc646bSeric */ 8545dfc646bSeric 855c579ef51Seric while (!clever && *++mvp != NULL) 8565dfc646bSeric { 857588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8585dfc646bSeric *pvp++ = newstr(buf); 8595dfc646bSeric if (pvp >= &pv[MAXPV]) 86008b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 8615dfc646bSeric } 8625dfc646bSeric *pvp++ = NULL; 8635dfc646bSeric 86425a99e2eSeric /* 86525a99e2eSeric ** Call the mailer. 8666328bdf7Seric ** The argument vector gets built, pipes 86725a99e2eSeric ** are created as necessary, and we fork & exec as 8686328bdf7Seric ** appropriate. 869c579ef51Seric ** If we are running SMTP, we just need to clean up. 87025a99e2eSeric */ 87125a99e2eSeric 8727ee87e5fSeric if (ctladdr == NULL && m != ProgMailer) 87386b26461Seric ctladdr = &e->e_from; 874134746fbSeric #ifdef NAMED_BIND 8752bcc6d2dSeric if (ConfigLevel < 2) 876912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 877134746fbSeric #endif 8789c9e68d9Seric 8799c9e68d9Seric if (tTd(11, 1)) 880134746fbSeric { 8819c9e68d9Seric printf("openmailer:"); 8829c9e68d9Seric printav(pv); 8839c9e68d9Seric } 8849c9e68d9Seric errno = 0; 8859c9e68d9Seric 8869c9e68d9Seric CurHostName = m->m_mailer; 8879c9e68d9Seric 8889c9e68d9Seric /* 8899c9e68d9Seric ** Deal with the special case of mail handled through an IPC 8909c9e68d9Seric ** connection. 8919c9e68d9Seric ** In this case we don't actually fork. We must be 8929c9e68d9Seric ** running SMTP for this to work. We will return a 8939c9e68d9Seric ** zero pid to indicate that we are running IPC. 8949c9e68d9Seric ** We also handle a debug version that just talks to stdin/out. 8959c9e68d9Seric */ 8969c9e68d9Seric 8979c9e68d9Seric curhost = NULL; 8989c9e68d9Seric 8999c9e68d9Seric /* check for Local Person Communication -- not for mortals!!! */ 9009c9e68d9Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 9019c9e68d9Seric { 9029c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 9039c9e68d9Seric bzero((char *) mci, sizeof *mci); 9049c9e68d9Seric mci->mci_in = stdin; 9059c9e68d9Seric mci->mci_out = stdout; 9069c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 9079c9e68d9Seric mci->mci_mailer = m; 9089c9e68d9Seric } 9099c9e68d9Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 9109c9e68d9Seric strcmp(m->m_mailer, "[TCP]") == 0) 9119c9e68d9Seric { 9129c9e68d9Seric #ifdef DAEMON 9139c9e68d9Seric register int i; 9149c9e68d9Seric register u_short port; 9159c9e68d9Seric extern MCI *mci_get(); 9169c9e68d9Seric extern char *hostsignature(); 9179c9e68d9Seric 9189c9e68d9Seric CurHostName = pv[1]; 9199c9e68d9Seric curhost = hostsignature(m, pv[1], e); 9209c9e68d9Seric 9219c9e68d9Seric if (curhost == NULL || curhost[0] == '\0') 9229c9e68d9Seric { 9239c9e68d9Seric syserr("null signature"); 924845e533cSeric rcode = EX_OSERR; 925b31e7f2bSeric goto give_up; 926b31e7f2bSeric } 9279c9e68d9Seric 9289c9e68d9Seric if (!clever) 9299c9e68d9Seric { 9309c9e68d9Seric syserr("554 non-clever IPC"); 9319c9e68d9Seric rcode = EX_OSERR; 9329c9e68d9Seric goto give_up; 9339c9e68d9Seric } 9349c9e68d9Seric if (pv[2] != NULL) 9359c9e68d9Seric port = atoi(pv[2]); 9369c9e68d9Seric else 9379c9e68d9Seric port = 0; 9389c9e68d9Seric tryhost: 9399c9e68d9Seric mci = NULL; 9409c9e68d9Seric while (*curhost != '\0') 9419c9e68d9Seric { 9429c9e68d9Seric register char *p; 9439c9e68d9Seric static char hostbuf[MAXNAME]; 9449c9e68d9Seric 9459c9e68d9Seric mci = NULL; 9469c9e68d9Seric 9479c9e68d9Seric /* pull the next host from the signature */ 9489c9e68d9Seric p = strchr(curhost, ':'); 9499c9e68d9Seric if (p == NULL) 9509c9e68d9Seric p = &curhost[strlen(curhost)]; 9519c9e68d9Seric strncpy(hostbuf, curhost, p - curhost); 9529c9e68d9Seric hostbuf[p - curhost] = '\0'; 9539c9e68d9Seric if (*p != '\0') 9549c9e68d9Seric p++; 9559c9e68d9Seric curhost = p; 9569c9e68d9Seric 9579c9e68d9Seric /* see if we already know that this host is fried */ 9589c9e68d9Seric CurHostName = hostbuf; 9599c9e68d9Seric mci = mci_get(hostbuf, m); 9609c9e68d9Seric if (mci->mci_state != MCIS_CLOSED) 9619c9e68d9Seric { 9629c9e68d9Seric if (tTd(11, 1)) 9639c9e68d9Seric { 9649c9e68d9Seric printf("openmailer: "); 9659c9e68d9Seric mci_dump(mci); 9669c9e68d9Seric } 9679c9e68d9Seric CurHostName = mci->mci_host; 9689c9e68d9Seric break; 9699c9e68d9Seric } 9709c9e68d9Seric mci->mci_mailer = m; 9719c9e68d9Seric if (mci->mci_exitstat != EX_OK) 9729c9e68d9Seric continue; 9739c9e68d9Seric 9749c9e68d9Seric /* try the connection */ 9759c9e68d9Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 9769c9e68d9Seric message("Connecting to %s (%s)...", 9779c9e68d9Seric hostbuf, m->m_name); 9789c9e68d9Seric i = makeconnection(hostbuf, port, mci, 9799c9e68d9Seric bitnset(M_SECURE_PORT, m->m_flags)); 9809c9e68d9Seric mci->mci_exitstat = i; 9819c9e68d9Seric mci->mci_errno = errno; 9829c9e68d9Seric if (i == EX_OK) 9839c9e68d9Seric { 9849c9e68d9Seric mci->mci_state = MCIS_OPENING; 9859c9e68d9Seric mci_cache(mci); 9869c9e68d9Seric break; 9879c9e68d9Seric } 9889c9e68d9Seric else if (tTd(11, 1)) 9899c9e68d9Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 9909c9e68d9Seric i, errno); 9919c9e68d9Seric 9929c9e68d9Seric 9939c9e68d9Seric /* enter status of this host */ 9949c9e68d9Seric setstat(i); 9959c9e68d9Seric } 9969c9e68d9Seric mci->mci_pid = 0; 9979c9e68d9Seric #else /* no DAEMON */ 9989c9e68d9Seric syserr("554 openmailer: no IPC"); 9999c9e68d9Seric if (tTd(11, 1)) 10009c9e68d9Seric printf("openmailer: NULL\n"); 10019c9e68d9Seric return NULL; 10029c9e68d9Seric #endif /* DAEMON */ 10039c9e68d9Seric } 10049c9e68d9Seric else 10059c9e68d9Seric { 10066fe8c3bcSeric int i; 10076fe8c3bcSeric struct stat stbuf; 10086fe8c3bcSeric 10096fe8c3bcSeric /* make absolutely certain 0, 1, and 2 are in use */ 10106fe8c3bcSeric for (i = 0; i < 3; i++) 10116fe8c3bcSeric { 10126fe8c3bcSeric if (fstat(i, &stbuf) < 0) 10136fe8c3bcSeric { 10146fe8c3bcSeric /* oops.... */ 1015*0e3bfef5Seric int fd; 1016*0e3bfef5Seric 1017*0e3bfef5Seric syserr("%s... openmailer(%s): fd %d not open", 1018*0e3bfef5Seric e->e_to, m->m_name, i); 1019*0e3bfef5Seric fd = open("/dev/null", O_RDONLY, 0666); 1020*0e3bfef5Seric if (fd != i) 1021*0e3bfef5Seric { 1022*0e3bfef5Seric (void) dup2(fd, i); 1023*0e3bfef5Seric (void) close(fd); 1024*0e3bfef5Seric } 10256fe8c3bcSeric } 10266fe8c3bcSeric } 10276fe8c3bcSeric 10289c9e68d9Seric /* create a pipe to shove the mail through */ 10299c9e68d9Seric if (pipe(mpvect) < 0) 10309c9e68d9Seric { 1031*0e3bfef5Seric syserr("%s... openmailer(%s): pipe (to mailer)", 1032*0e3bfef5Seric e->e_to, m->m_name); 10339c9e68d9Seric if (tTd(11, 1)) 10349c9e68d9Seric printf("openmailer: NULL\n"); 10359c9e68d9Seric rcode = EX_OSERR; 10369c9e68d9Seric goto give_up; 10379c9e68d9Seric } 10389c9e68d9Seric 10399c9e68d9Seric /* if this mailer speaks smtp, create a return pipe */ 10409c9e68d9Seric if (clever && pipe(rpvect) < 0) 10419c9e68d9Seric { 1042*0e3bfef5Seric syserr("%s... openmailer(%s): pipe (from mailer)", 1043*0e3bfef5Seric e->e_to, m->m_name); 10449c9e68d9Seric (void) close(mpvect[0]); 10459c9e68d9Seric (void) close(mpvect[1]); 10469c9e68d9Seric if (tTd(11, 1)) 10479c9e68d9Seric printf("openmailer: NULL\n"); 10489c9e68d9Seric rcode = EX_OSERR; 10499c9e68d9Seric goto give_up; 10509c9e68d9Seric } 10519c9e68d9Seric 10529c9e68d9Seric /* 10539c9e68d9Seric ** Actually fork the mailer process. 10549c9e68d9Seric ** DOFORK is clever about retrying. 10559c9e68d9Seric ** 10569c9e68d9Seric ** Dispose of SIGCHLD signal catchers that may be laying 10579c9e68d9Seric ** around so that endmail will get it. 10589c9e68d9Seric */ 10599c9e68d9Seric 10609c9e68d9Seric if (e->e_xfp != NULL) 10619c9e68d9Seric (void) fflush(e->e_xfp); /* for debugging */ 10629c9e68d9Seric (void) fflush(stdout); 10639c9e68d9Seric # ifdef SIGCHLD 10649c9e68d9Seric (void) signal(SIGCHLD, SIG_DFL); 10659c9e68d9Seric # endif /* SIGCHLD */ 10669c9e68d9Seric DOFORK(FORK); 10679c9e68d9Seric /* pid is set by DOFORK */ 10689c9e68d9Seric if (pid < 0) 10699c9e68d9Seric { 10709c9e68d9Seric /* failure */ 1071*0e3bfef5Seric syserr("%s... openmailer(%s): cannot fork", 1072*0e3bfef5Seric e->e_to, m->m_name); 10739c9e68d9Seric (void) close(mpvect[0]); 10749c9e68d9Seric (void) close(mpvect[1]); 10759c9e68d9Seric if (clever) 10769c9e68d9Seric { 10779c9e68d9Seric (void) close(rpvect[0]); 10789c9e68d9Seric (void) close(rpvect[1]); 10799c9e68d9Seric } 10809c9e68d9Seric if (tTd(11, 1)) 10819c9e68d9Seric printf("openmailer: NULL\n"); 10829c9e68d9Seric rcode = EX_OSERR; 10839c9e68d9Seric goto give_up; 10849c9e68d9Seric } 10859c9e68d9Seric else if (pid == 0) 10869c9e68d9Seric { 10879c9e68d9Seric int i; 10889c9e68d9Seric int saveerrno; 10899c9e68d9Seric char **ep; 10909c9e68d9Seric char *env[MAXUSERENVIRON]; 10919c9e68d9Seric extern char **environ; 10929c9e68d9Seric extern int DtableSize; 10939c9e68d9Seric 10949c9e68d9Seric /* child -- set up input & exec mailer */ 10959c9e68d9Seric /* make diagnostic output be standard output */ 10969c9e68d9Seric (void) signal(SIGINT, SIG_IGN); 10979c9e68d9Seric (void) signal(SIGHUP, SIG_IGN); 10989c9e68d9Seric (void) signal(SIGTERM, SIG_DFL); 10999c9e68d9Seric 11009c9e68d9Seric /* close any other cached connections */ 11019c9e68d9Seric mci_flush(FALSE, mci); 11029c9e68d9Seric 11039c9e68d9Seric /* arrange to filter std & diag output of command */ 11049c9e68d9Seric if (clever) 11059c9e68d9Seric { 11069c9e68d9Seric (void) close(rpvect[0]); 11076fe8c3bcSeric if (dup2(rpvect[1], STDOUT_FILENO) < 0) 11086fe8c3bcSeric { 1109*0e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 1110*0e3bfef5Seric e->e_to, m->m_name, rpvect[1]); 11116fe8c3bcSeric _exit(EX_OSERR); 11126fe8c3bcSeric } 11139c9e68d9Seric (void) close(rpvect[1]); 11149c9e68d9Seric } 11159c9e68d9Seric else if (OpMode == MD_SMTP || HoldErrs) 11169c9e68d9Seric { 11179c9e68d9Seric /* put mailer output in transcript */ 11186fe8c3bcSeric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 11196fe8c3bcSeric { 1120*0e3bfef5Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 1121*0e3bfef5Seric e->e_to, m->m_name, 11226fe8c3bcSeric fileno(e->e_xfp)); 11236fe8c3bcSeric _exit(EX_OSERR); 11249c9e68d9Seric } 11256fe8c3bcSeric } 11266fe8c3bcSeric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 11276fe8c3bcSeric { 1128*0e3bfef5Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr", 1129*0e3bfef5Seric e->e_to, m->m_name); 11306fe8c3bcSeric _exit(EX_OSERR); 11316fe8c3bcSeric } 11329c9e68d9Seric 11339c9e68d9Seric /* arrange to get standard input */ 11349c9e68d9Seric (void) close(mpvect[1]); 11359c9e68d9Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 11369c9e68d9Seric { 1137*0e3bfef5Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 1138*0e3bfef5Seric e->e_to, m->m_name, mpvect[0]); 11399c9e68d9Seric _exit(EX_OSERR); 11409c9e68d9Seric } 11419c9e68d9Seric (void) close(mpvect[0]); 11429c9e68d9Seric if (!bitnset(M_RESTR, m->m_flags)) 11439c9e68d9Seric { 11449c9e68d9Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 11459c9e68d9Seric { 11469c9e68d9Seric (void) setgid(DefGid); 11479c9e68d9Seric (void) initgroups(DefUser, DefGid); 11489c9e68d9Seric (void) setuid(DefUid); 11499c9e68d9Seric } 11509c9e68d9Seric else 11519c9e68d9Seric { 11529c9e68d9Seric (void) setgid(ctladdr->q_gid); 11539c9e68d9Seric (void) initgroups(ctladdr->q_ruser? 11549c9e68d9Seric ctladdr->q_ruser: ctladdr->q_user, 11559c9e68d9Seric ctladdr->q_gid); 11569c9e68d9Seric (void) setuid(ctladdr->q_uid); 11579c9e68d9Seric } 11589c9e68d9Seric } 11599c9e68d9Seric 11609c9e68d9Seric /* arrange for all the files to be closed */ 11619c9e68d9Seric for (i = 3; i < DtableSize; i++) 11629c9e68d9Seric { 11639c9e68d9Seric register int j; 11649c9e68d9Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 11659c9e68d9Seric (void)fcntl(i, F_SETFD, j|1); 11669c9e68d9Seric } 11679c9e68d9Seric 11689c9e68d9Seric /* set up the mailer environment */ 11699c9e68d9Seric i = 0; 11709c9e68d9Seric env[i++] = "AGENT=sendmail"; 11719c9e68d9Seric for (ep = environ; *ep != NULL; ep++) 11729c9e68d9Seric { 11739c9e68d9Seric if (strncmp(*ep, "TZ=", 3) == 0) 11749c9e68d9Seric env[i++] = *ep; 11759c9e68d9Seric } 11769c9e68d9Seric env[i++] = NULL; 11779c9e68d9Seric 11789c9e68d9Seric /* try to execute the mailer */ 11799c9e68d9Seric execve(m->m_mailer, pv, env); 11809c9e68d9Seric saveerrno = errno; 11819c9e68d9Seric syserr("Cannot exec %s", m->m_mailer); 11829c9e68d9Seric if (m == LocalMailer) 11839c9e68d9Seric _exit(EX_TEMPFAIL); 11849c9e68d9Seric if (transienterror(saveerrno)) 11859c9e68d9Seric _exit(EX_TEMPFAIL); 11869c9e68d9Seric _exit(EX_UNAVAILABLE); 11879c9e68d9Seric } 11889c9e68d9Seric 11899c9e68d9Seric /* 11909c9e68d9Seric ** Set up return value. 11919c9e68d9Seric */ 11929c9e68d9Seric 11939c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 11949c9e68d9Seric bzero((char *) mci, sizeof *mci); 11959c9e68d9Seric mci->mci_mailer = m; 11969c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 11979c9e68d9Seric mci->mci_pid = pid; 11989c9e68d9Seric (void) close(mpvect[0]); 11999c9e68d9Seric mci->mci_out = fdopen(mpvect[1], "w"); 12009c9e68d9Seric if (clever) 12019c9e68d9Seric { 12029c9e68d9Seric (void) close(rpvect[1]); 12039c9e68d9Seric mci->mci_in = fdopen(rpvect[0], "r"); 12049c9e68d9Seric } 12059c9e68d9Seric else 12069c9e68d9Seric { 12079c9e68d9Seric mci->mci_flags |= MCIF_TEMP; 12089c9e68d9Seric mci->mci_in = NULL; 12099c9e68d9Seric } 12109c9e68d9Seric } 12119c9e68d9Seric 12129c9e68d9Seric /* 12139c9e68d9Seric ** If we are in SMTP opening state, send initial protocol. 12149c9e68d9Seric */ 12159c9e68d9Seric 12169c9e68d9Seric if (clever && mci->mci_state != MCIS_CLOSED) 12179c9e68d9Seric { 12189c9e68d9Seric smtpinit(m, mci, e); 12199c9e68d9Seric } 12209c9e68d9Seric if (tTd(11, 1)) 12219c9e68d9Seric { 12229c9e68d9Seric printf("openmailer: "); 12239c9e68d9Seric mci_dump(mci); 12249c9e68d9Seric } 12259c9e68d9Seric 12269c9e68d9Seric if (mci->mci_state != MCIS_OPEN) 1227b31e7f2bSeric { 1228b31e7f2bSeric /* couldn't open the mailer */ 1229b31e7f2bSeric rcode = mci->mci_exitstat; 12302a6bc25bSeric errno = mci->mci_errno; 1231b31e7f2bSeric if (rcode == EX_OK) 1232b31e7f2bSeric { 1233b31e7f2bSeric /* shouldn't happen */ 123408b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 12356b0f339dSeric rcode, mci->mci_state, firstsig); 1236b31e7f2bSeric rcode = EX_SOFTWARE; 1237b31e7f2bSeric } 1238b31e7f2bSeric } 1239b31e7f2bSeric else if (!clever) 1240b31e7f2bSeric { 1241b31e7f2bSeric /* 1242b31e7f2bSeric ** Format and send message. 1243b31e7f2bSeric */ 124415d084d5Seric 1245b31e7f2bSeric putfromline(mci->mci_out, m, e); 1246b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 1247b31e7f2bSeric putline("\n", mci->mci_out, m); 1248b31e7f2bSeric (*e->e_putbody)(mci->mci_out, m, e); 1249b31e7f2bSeric 1250b31e7f2bSeric /* get the exit status */ 1251c9be6216Seric rcode = endmailer(mci, e, pv); 1252134746fbSeric } 1253134746fbSeric else 1254b31e7f2bSeric #ifdef SMTP 1255134746fbSeric { 1256b31e7f2bSeric /* 1257b31e7f2bSeric ** Send the MAIL FROM: protocol 1258b31e7f2bSeric */ 125915d084d5Seric 1260b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 1261b31e7f2bSeric if (rcode == EX_OK) 126275889e88Seric { 1263ded0d3daSkarels register char *t = tobuf; 1264ded0d3daSkarels register int i; 1265ded0d3daSkarels 1266588cad61Seric /* send the recipient list */ 126763780dbdSeric tobuf[0] = '\0'; 126875889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 126975889e88Seric { 127063780dbdSeric e->e_to = to->q_paddr; 127115d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 127275889e88Seric { 127383b7ddc9Seric markfailure(e, to, i); 127481161401Seric giveresponse(i, m, mci, e); 127563780dbdSeric } 127675889e88Seric else 127775889e88Seric { 1278911693bfSbostic *t++ = ','; 1279b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 1280b31e7f2bSeric continue; 1281588cad61Seric } 1282588cad61Seric } 1283588cad61Seric 128463780dbdSeric /* now send the data */ 128563780dbdSeric if (tobuf[0] == '\0') 1286b31e7f2bSeric { 12879c9e68d9Seric rcode = EX_OK; 128863780dbdSeric e->e_to = NULL; 1289b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 1290b31e7f2bSeric smtprset(m, mci, e); 1291b31e7f2bSeric } 129275889e88Seric else 129375889e88Seric { 129463780dbdSeric e->e_to = tobuf + 1; 129575889e88Seric rcode = smtpdata(m, mci, e); 129663780dbdSeric } 129763780dbdSeric 129863780dbdSeric /* now close the connection */ 1299b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 130015d084d5Seric smtpquit(m, mci, e); 130163780dbdSeric } 13029c9e68d9Seric if (rcode != EX_OK && *curhost != '\0') 13039c9e68d9Seric { 13049c9e68d9Seric /* try next MX site */ 13059c9e68d9Seric goto tryhost; 13069c9e68d9Seric } 1307c579ef51Seric } 1308b31e7f2bSeric #else /* not SMTP */ 1309a05b3449Sbostic { 131008b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 1311845e533cSeric rcode = EX_CONFIG; 1312b31e7f2bSeric goto give_up; 1313a05b3449Sbostic } 1314b31e7f2bSeric #endif /* SMTP */ 1315134746fbSeric #ifdef NAMED_BIND 13162bcc6d2dSeric if (ConfigLevel < 2) 1317912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1318134746fbSeric #endif 13195dfc646bSeric 1320b31e7f2bSeric /* arrange a return receipt if requested */ 13218c13bf07Seric if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags)) 1322b31e7f2bSeric { 1323b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 1324b31e7f2bSeric /* do we want to send back more info? */ 1325b31e7f2bSeric } 1326b31e7f2bSeric 1327c77d1c25Seric /* 132863780dbdSeric ** Do final status disposal. 132963780dbdSeric ** We check for something in tobuf for the SMTP case. 1330c77d1c25Seric ** If we got a temporary failure, arrange to queue the 1331c77d1c25Seric ** addressees. 1332c77d1c25Seric */ 1333c77d1c25Seric 1334b31e7f2bSeric give_up: 133563780dbdSeric if (tobuf[0] != '\0') 133681161401Seric giveresponse(rcode, m, mci, e); 1337772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 1338b31e7f2bSeric { 1339dde5acadSeric if (rcode != EX_OK) 134083b7ddc9Seric markfailure(e, to, rcode); 1341dde5acadSeric else 1342655518ecSeric { 1343dde5acadSeric to->q_flags |= QSENT; 1344655518ecSeric e->e_nsent++; 1345655518ecSeric } 1346b31e7f2bSeric } 1347b31e7f2bSeric 1348b31e7f2bSeric /* 1349b31e7f2bSeric ** Restore state and return. 1350b31e7f2bSeric */ 1351c77d1c25Seric 135235490626Seric errno = 0; 1353588cad61Seric define('g', (char *) NULL, e); 13545826d9d3Seric return (rcode); 135525a99e2eSeric } 13565dfc646bSeric /* 135783b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 135883b7ddc9Seric ** 135983b7ddc9Seric ** Parameters: 136083b7ddc9Seric ** e -- the envelope we are sending. 136183b7ddc9Seric ** q -- the address to mark. 136283b7ddc9Seric ** rcode -- the code signifying the particular failure. 136383b7ddc9Seric ** 136483b7ddc9Seric ** Returns: 136583b7ddc9Seric ** none. 136683b7ddc9Seric ** 136783b7ddc9Seric ** Side Effects: 136883b7ddc9Seric ** marks the address (and possibly the envelope) with the 136983b7ddc9Seric ** failure so that an error will be returned or 137083b7ddc9Seric ** the message will be queued, as appropriate. 137183b7ddc9Seric */ 137283b7ddc9Seric 137383b7ddc9Seric markfailure(e, q, rcode) 137483b7ddc9Seric register ENVELOPE *e; 137583b7ddc9Seric register ADDRESS *q; 137683b7ddc9Seric int rcode; 137783b7ddc9Seric { 137819c47125Seric char buf[MAXLINE]; 137919c47125Seric extern char *pintvl(); 138019c47125Seric 138183b7ddc9Seric if (rcode == EX_OK) 138283b7ddc9Seric return; 1383ef137175Sbostic else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 138483b7ddc9Seric q->q_flags |= QBADADDR; 138519c47125Seric else if (curtime() > e->e_ctime + TimeOuts.to_q_return) 138683b7ddc9Seric { 138783b7ddc9Seric if (!bitset(EF_TIMEOUT, e->e_flags)) 1388198d9be0Seric { 1389198d9be0Seric (void) sprintf(buf, "Cannot send message for %s", 139019c47125Seric pintvl(TimeOuts.to_q_return, FALSE)); 1391198d9be0Seric if (e->e_message != NULL) 1392198d9be0Seric free(e->e_message); 1393198d9be0Seric e->e_message = newstr(buf); 139408b25121Seric message(buf); 1395198d9be0Seric } 139683b7ddc9Seric q->q_flags |= QBADADDR; 139783b7ddc9Seric e->e_flags |= EF_TIMEOUT; 1398c13b5dfcSeric fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr); 139983b7ddc9Seric } 140083b7ddc9Seric else 140119c47125Seric { 140283b7ddc9Seric q->q_flags |= QQUEUEUP; 140319c47125Seric if (TimeOuts.to_q_warning > 0 && 140419c47125Seric curtime() > e->e_ctime + TimeOuts.to_q_warning) 140519c47125Seric { 140619c47125Seric if (!bitset(EF_WARNING, e->e_flags) && 140719c47125Seric e->e_class >= 0) 140819c47125Seric { 140919c47125Seric (void) sprintf(buf, 141019c47125Seric "warning: cannot send message for %s", 141119c47125Seric pintvl(TimeOuts.to_q_warning, FALSE)); 141219c47125Seric if (e->e_message != NULL) 141319c47125Seric free(e->e_message); 141419c47125Seric e->e_message = newstr(buf); 141519c47125Seric message(buf); 141619c47125Seric e->e_flags |= EF_WARNING|EF_TIMEOUT; 141719c47125Seric } 141819c47125Seric fprintf(e->e_xfp, 141919c47125Seric "%s... Warning: message still undelivered after %s\n", 142019c47125Seric q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE)); 142119c47125Seric fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 142219c47125Seric pintvl(TimeOuts.to_q_return, FALSE)); 142319c47125Seric } 142419c47125Seric } 142583b7ddc9Seric } 142683b7ddc9Seric /* 1427c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 1428c579ef51Seric ** 1429c579ef51Seric ** We should never get fatal errors (e.g., segmentation 1430c579ef51Seric ** violation), so we report those specially. For other 1431c579ef51Seric ** errors, we choose a status message (into statmsg), 1432c579ef51Seric ** and if it represents an error, we print it. 1433c579ef51Seric ** 1434c579ef51Seric ** Parameters: 1435c579ef51Seric ** pid -- pid of mailer. 1436c9be6216Seric ** e -- the current envelope. 1437c9be6216Seric ** pv -- the parameter vector that invoked the mailer 1438c9be6216Seric ** (for error messages). 1439c579ef51Seric ** 1440c579ef51Seric ** Returns: 1441c579ef51Seric ** exit code of mailer. 1442c579ef51Seric ** 1443c579ef51Seric ** Side Effects: 1444c579ef51Seric ** none. 1445c579ef51Seric */ 1446c579ef51Seric 1447c9be6216Seric endmailer(mci, e, pv) 1448b31e7f2bSeric register MCI *mci; 1449c9be6216Seric register ENVELOPE *e; 1450c9be6216Seric char **pv; 1451c579ef51Seric { 1452588cad61Seric int st; 1453c579ef51Seric 145475889e88Seric /* close any connections */ 145575889e88Seric if (mci->mci_in != NULL) 1456c9be6216Seric (void) xfclose(mci->mci_in, pv[0], "mci_in"); 145775889e88Seric if (mci->mci_out != NULL) 1458c9be6216Seric (void) xfclose(mci->mci_out, pv[0], "mci_out"); 145975889e88Seric mci->mci_in = mci->mci_out = NULL; 146075889e88Seric mci->mci_state = MCIS_CLOSED; 146175889e88Seric 146233db8731Seric /* in the IPC case there is nothing to wait for */ 146375889e88Seric if (mci->mci_pid == 0) 146433db8731Seric return (EX_OK); 146533db8731Seric 146633db8731Seric /* wait for the mailer process to die and collect status */ 146775889e88Seric st = waitfor(mci->mci_pid); 1468588cad61Seric if (st == -1) 146978de67c1Seric { 1470c9be6216Seric syserr("endmailer %s: wait", pv[0]); 1471588cad61Seric return (EX_SOFTWARE); 1472c579ef51Seric } 147333db8731Seric 147433db8731Seric /* see if it died a horrid death */ 1475c579ef51Seric if ((st & 0377) != 0) 1476c579ef51Seric { 1477c9be6216Seric syserr("mailer %s died with signal %o", pv[0], st); 1478c9be6216Seric 1479c9be6216Seric /* log the arguments */ 1480c9be6216Seric if (e->e_xfp != NULL) 1481c9be6216Seric { 1482c9be6216Seric register char **av; 1483c9be6216Seric 1484c9be6216Seric fprintf(e->e_xfp, "Arguments:"); 1485c9be6216Seric for (av = pv; *av != NULL; av++) 1486c9be6216Seric fprintf(e->e_xfp, " %s", *av); 1487c9be6216Seric fprintf(e->e_xfp, "\n"); 1488c9be6216Seric } 1489c9be6216Seric 14905f73204aSeric ExitStat = EX_TEMPFAIL; 14915f73204aSeric return (EX_TEMPFAIL); 1492c579ef51Seric } 149333db8731Seric 149433db8731Seric /* normal death -- return status */ 1495588cad61Seric st = (st >> 8) & 0377; 1496588cad61Seric return (st); 1497c579ef51Seric } 1498c579ef51Seric /* 149925a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 150025a99e2eSeric ** 150125a99e2eSeric ** Parameters: 150225a99e2eSeric ** stat -- the status code from the mailer (high byte 150325a99e2eSeric ** only; core dumps must have been taken care of 150425a99e2eSeric ** already). 150581161401Seric ** m -- the mailer info for this mailer. 150681161401Seric ** mci -- the mailer connection info -- can be NULL if the 150781161401Seric ** response is given before the connection is made. 150881161401Seric ** e -- the current envelope. 150925a99e2eSeric ** 151025a99e2eSeric ** Returns: 1511db8841e9Seric ** none. 151225a99e2eSeric ** 151325a99e2eSeric ** Side Effects: 1514c1f9df2cSeric ** Errors may be incremented. 151525a99e2eSeric ** ExitStat may be set. 151625a99e2eSeric */ 151725a99e2eSeric 151881161401Seric giveresponse(stat, m, mci, e) 151925a99e2eSeric int stat; 1520588cad61Seric register MAILER *m; 152181161401Seric register MCI *mci; 1522198d9be0Seric ENVELOPE *e; 152325a99e2eSeric { 152425a99e2eSeric register char *statmsg; 152525a99e2eSeric extern char *SysExMsg[]; 152625a99e2eSeric register int i; 1527d4bd8f0eSbostic extern int N_SysEx; 1528d4bd8f0eSbostic #ifdef NAMED_BIND 1529d4bd8f0eSbostic extern int h_errno; 1530d4bd8f0eSbostic #endif 1531198d9be0Seric char buf[MAXLINE]; 15327d55540cSeric extern char *errstring(); 153325a99e2eSeric 153413bbc08cSeric /* 153513bbc08cSeric ** Compute status message from code. 153613bbc08cSeric */ 153713bbc08cSeric 153825a99e2eSeric i = stat - EX__BASE; 1539588cad61Seric if (stat == 0) 15406fe8c3bcSeric { 1541588cad61Seric statmsg = "250 Sent"; 1542ce5531bdSeric if (e->e_statmsg != NULL) 15436fe8c3bcSeric { 1544ce5531bdSeric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 15456fe8c3bcSeric statmsg = buf; 15466fe8c3bcSeric } 15476fe8c3bcSeric } 1548588cad61Seric else if (i < 0 || i > N_SysEx) 1549588cad61Seric { 1550588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1551588cad61Seric stat = EX_UNAVAILABLE; 1552588cad61Seric statmsg = buf; 1553588cad61Seric } 1554198d9be0Seric else if (stat == EX_TEMPFAIL) 1555198d9be0Seric { 15567d55540cSeric (void) strcpy(buf, SysExMsg[i] + 1); 1557d4bd8f0eSbostic #ifdef NAMED_BIND 1558f28da541Smiriam if (h_errno == TRY_AGAIN) 1559f28da541Smiriam statmsg = errstring(h_errno+MAX_ERRNO); 1560f28da541Smiriam else 1561d4bd8f0eSbostic #endif 1562f28da541Smiriam { 15638557d168Seric if (errno != 0) 1564d87e85f3Seric statmsg = errstring(errno); 1565d87e85f3Seric else 1566d87e85f3Seric { 1567d87e85f3Seric #ifdef SMTP 1568d87e85f3Seric extern char SmtpError[]; 1569d87e85f3Seric 1570d87e85f3Seric statmsg = SmtpError; 15716c2c3107Seric #else /* SMTP */ 1572d87e85f3Seric statmsg = NULL; 15736c2c3107Seric #endif /* SMTP */ 1574d87e85f3Seric } 1575f28da541Smiriam } 1576d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1577d87e85f3Seric { 157887c9b3e7Seric (void) strcat(buf, ": "); 1579d87e85f3Seric (void) strcat(buf, statmsg); 15808557d168Seric } 1581198d9be0Seric statmsg = buf; 1582198d9be0Seric } 158325a99e2eSeric else 1584d87e85f3Seric { 158525a99e2eSeric statmsg = SysExMsg[i]; 15867d55540cSeric if (*statmsg++ == ':') 15877d55540cSeric { 15887d55540cSeric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 15897d55540cSeric statmsg = buf; 15907d55540cSeric } 1591d87e85f3Seric } 1592588cad61Seric 1593588cad61Seric /* 1594588cad61Seric ** Print the message as appropriate 1595588cad61Seric */ 1596588cad61Seric 1597198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 1598e12d03eeSeric message(&statmsg[4], errstring(errno)); 159925a99e2eSeric else 160025a99e2eSeric { 1601c1f9df2cSeric Errors++; 1602e12d03eeSeric usrerr(statmsg, errstring(errno)); 160325a99e2eSeric } 160425a99e2eSeric 160525a99e2eSeric /* 160625a99e2eSeric ** Final cleanup. 160725a99e2eSeric ** Log a record of the transaction. Compute the new 160825a99e2eSeric ** ExitStat -- if we already had an error, stick with 160925a99e2eSeric ** that. 161025a99e2eSeric */ 161125a99e2eSeric 16122f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 161381161401Seric logdelivery(m, mci, &statmsg[4], e); 1614eb238f8cSeric 1615eb238f8cSeric if (stat != EX_TEMPFAIL) 1616eb238f8cSeric setstat(stat); 1617198d9be0Seric if (stat != EX_OK) 1618198d9be0Seric { 1619198d9be0Seric if (e->e_message != NULL) 1620198d9be0Seric free(e->e_message); 1621198d9be0Seric e->e_message = newstr(&statmsg[4]); 1622198d9be0Seric } 16238557d168Seric errno = 0; 1624d4bd8f0eSbostic #ifdef NAMED_BIND 1625f28da541Smiriam h_errno = 0; 1626d4bd8f0eSbostic #endif 1627eb238f8cSeric } 1628eb238f8cSeric /* 1629eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1630eb238f8cSeric ** 1631eb238f8cSeric ** Parameters: 163281161401Seric ** m -- the mailer info. Can be NULL for initial queue. 163381161401Seric ** mci -- the mailer connection info -- can be NULL if the 163481161401Seric ** log is occuring when no connection is active. 163581161401Seric ** stat -- the message to print for the status. 163681161401Seric ** e -- the current envelope. 1637eb238f8cSeric ** 1638eb238f8cSeric ** Returns: 1639eb238f8cSeric ** none 1640eb238f8cSeric ** 1641eb238f8cSeric ** Side Effects: 1642eb238f8cSeric ** none 1643eb238f8cSeric */ 1644eb238f8cSeric 164581161401Seric logdelivery(m, mci, stat, e) 164681161401Seric MAILER *m; 164781161401Seric register MCI *mci; 1648eb238f8cSeric char *stat; 1649b31e7f2bSeric register ENVELOPE *e; 16505cf56be3Seric { 1651eb238f8cSeric # ifdef LOG 16529507d1f9Seric char *curhost; 1653d6acf3eeSeric char buf[512]; 16549507d1f9Seric extern char *pintvl(); 16559507d1f9Seric extern char *macvalue(); 16569507d1f9Seric 165748ed5d33Seric (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 165881161401Seric 165948ed5d33Seric if (m != NULL) 166071ff6caaSeric { 166148ed5d33Seric (void) strcat(buf, ", mailer="); 166248ed5d33Seric (void) strcat(buf, m->m_name); 166371ff6caaSeric } 166448ed5d33Seric 166548ed5d33Seric if (mci != NULL && mci->mci_host != NULL) 166671ff6caaSeric { 166771ff6caaSeric # ifdef DAEMON 1668e2f2f828Seric extern SOCKADDR CurHostAddr; 1669e2f2f828Seric extern char *anynet_ntoa(); 167048ed5d33Seric # endif 167171ff6caaSeric 167248ed5d33Seric (void) strcat(buf, ", relay="); 167348ed5d33Seric (void) strcat(buf, mci->mci_host); 167448ed5d33Seric 167548ed5d33Seric # ifdef DAEMON 167648ed5d33Seric (void) strcat(buf, " ("); 1677e2f2f828Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 167848ed5d33Seric (void) strcat(buf, ")"); 167971ff6caaSeric # endif 168071ff6caaSeric } 16819507d1f9Seric else 168248ed5d33Seric { 168348ed5d33Seric char *p = macvalue('h', e); 16849507d1f9Seric 168548ed5d33Seric if (p != NULL && p[0] != '\0') 168648ed5d33Seric { 168748ed5d33Seric (void) strcat(buf, ", relay="); 168848ed5d33Seric (void) strcat(buf, p); 168948ed5d33Seric } 169048ed5d33Seric } 1691d6acf3eeSeric 1692d6acf3eeSeric syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1693d6acf3eeSeric e->e_id, e->e_to, buf, stat); 16946c2c3107Seric # endif /* LOG */ 169525a99e2eSeric } 169625a99e2eSeric /* 169751552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 169825a99e2eSeric ** 169951552439Seric ** This can be made an arbitrary message separator by changing $l 170051552439Seric ** 17019b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 17029b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 17039b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 17049b6c17a6Seric ** this kind of antique garbage???? 170525a99e2eSeric ** 170625a99e2eSeric ** Parameters: 170751552439Seric ** fp -- the file to output to. 170851552439Seric ** m -- the mailer describing this entry. 170925a99e2eSeric ** 171025a99e2eSeric ** Returns: 171151552439Seric ** none 171225a99e2eSeric ** 171325a99e2eSeric ** Side Effects: 171451552439Seric ** outputs some text to fp. 171525a99e2eSeric */ 171625a99e2eSeric 1717b31e7f2bSeric putfromline(fp, m, e) 171851552439Seric register FILE *fp; 171951552439Seric register MAILER *m; 1720b31e7f2bSeric ENVELOPE *e; 172125a99e2eSeric { 17222bc47524Seric char *template = "\201l\n"; 172351552439Seric char buf[MAXLINE]; 172425a99e2eSeric 172557fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 172651552439Seric return; 172713bbc08cSeric 17282c7e1b8dSeric # ifdef UGLYUUCP 172957fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 173074b6e67bSeric { 1731ea09d6edSeric char *bang; 1732ea09d6edSeric char xbuf[MAXLINE]; 173374b6e67bSeric 1734ee4b0922Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 17356c2c3107Seric bang = strchr(buf, '!'); 173674b6e67bSeric if (bang == NULL) 173708b25121Seric syserr("554 No ! in UUCP! (%s)", buf); 173874b6e67bSeric else 1739588cad61Seric { 1740ea09d6edSeric *bang++ = '\0'; 17412bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1742ea09d6edSeric template = xbuf; 174374b6e67bSeric } 1744588cad61Seric } 17456c2c3107Seric # endif /* UGLYUUCP */ 1746b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 174777b52738Seric putline(buf, fp, m); 1748bc6e2962Seric } 1749bc6e2962Seric /* 175051552439Seric ** PUTBODY -- put the body of a message. 175151552439Seric ** 175251552439Seric ** Parameters: 175351552439Seric ** fp -- file to output onto. 175477b52738Seric ** m -- a mailer descriptor to control output format. 17559a6a5f55Seric ** e -- the envelope to put out. 175651552439Seric ** 175751552439Seric ** Returns: 175851552439Seric ** none. 175951552439Seric ** 176051552439Seric ** Side Effects: 176151552439Seric ** The message is written onto fp. 176251552439Seric */ 176351552439Seric 176477b52738Seric putbody(fp, m, e) 176551552439Seric FILE *fp; 1766588cad61Seric MAILER *m; 17679a6a5f55Seric register ENVELOPE *e; 176851552439Seric { 176977b52738Seric char buf[MAXLINE]; 177051552439Seric 177151552439Seric /* 177251552439Seric ** Output the body of the message 177351552439Seric */ 177451552439Seric 17759a6a5f55Seric if (e->e_dfp == NULL) 177651552439Seric { 17779a6a5f55Seric if (e->e_df != NULL) 17789a6a5f55Seric { 17799a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 17809a6a5f55Seric if (e->e_dfp == NULL) 17818f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 17828f9146b0Srick e->e_df, e->e_to, e->e_from); 17839a6a5f55Seric } 17849a6a5f55Seric else 178577b52738Seric putline("<<< No Message Collected >>>", fp, m); 17869a6a5f55Seric } 17879a6a5f55Seric if (e->e_dfp != NULL) 17889a6a5f55Seric { 17899a6a5f55Seric rewind(e->e_dfp); 179077b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 179124fc8aeeSeric { 179224fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1793d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 17943462ad9eSeric (void) putc('>', fp); 179577b52738Seric putline(buf, fp, m); 179624fc8aeeSeric } 179751552439Seric 17989a6a5f55Seric if (ferror(e->e_dfp)) 179951552439Seric { 180051552439Seric syserr("putbody: read error"); 180151552439Seric ExitStat = EX_IOERR; 180251552439Seric } 180351552439Seric } 180451552439Seric 180551552439Seric (void) fflush(fp); 180651552439Seric if (ferror(fp) && errno != EPIPE) 180751552439Seric { 180851552439Seric syserr("putbody: write error"); 180951552439Seric ExitStat = EX_IOERR; 181051552439Seric } 181151552439Seric errno = 0; 181225a99e2eSeric } 181325a99e2eSeric /* 181425a99e2eSeric ** MAILFILE -- Send a message to a file. 181525a99e2eSeric ** 1816f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1817f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1818f129ec7dSeric ** rather than the real user. Obviously, this only works if 1819f129ec7dSeric ** sendmail runs as root. 1820f129ec7dSeric ** 1821588cad61Seric ** This could be done as a subordinate mailer, except that it 1822588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1823588cad61Seric ** view this as being sufficiently important as to include it 1824588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1825588cad61Seric ** to create another process plus some pipes to save the message. 1826588cad61Seric ** 182725a99e2eSeric ** Parameters: 182825a99e2eSeric ** filename -- the name of the file to send to. 18296259796dSeric ** ctladdr -- the controlling address header -- includes 18306259796dSeric ** the userid/groupid to be when sending. 183125a99e2eSeric ** 183225a99e2eSeric ** Returns: 183325a99e2eSeric ** The exit code associated with the operation. 183425a99e2eSeric ** 183525a99e2eSeric ** Side Effects: 183625a99e2eSeric ** none. 183725a99e2eSeric */ 183825a99e2eSeric 1839b31e7f2bSeric mailfile(filename, ctladdr, e) 184025a99e2eSeric char *filename; 18416259796dSeric ADDRESS *ctladdr; 1842b31e7f2bSeric register ENVELOPE *e; 184325a99e2eSeric { 184425a99e2eSeric register FILE *f; 184532d19d43Seric register int pid; 184615d084d5Seric int mode; 184725a99e2eSeric 184832d19d43Seric /* 184932d19d43Seric ** Fork so we can change permissions here. 185032d19d43Seric ** Note that we MUST use fork, not vfork, because of 185132d19d43Seric ** the complications of calling subroutines, etc. 185232d19d43Seric */ 185332d19d43Seric 185432d19d43Seric DOFORK(fork); 185532d19d43Seric 185632d19d43Seric if (pid < 0) 185732d19d43Seric return (EX_OSERR); 185832d19d43Seric else if (pid == 0) 185932d19d43Seric { 186032d19d43Seric /* child -- actually write to file */ 1861f129ec7dSeric struct stat stb; 1862f129ec7dSeric 18630984da9fSeric (void) signal(SIGINT, SIG_DFL); 18640984da9fSeric (void) signal(SIGHUP, SIG_DFL); 18650984da9fSeric (void) signal(SIGTERM, SIG_DFL); 18663462ad9eSeric (void) umask(OldUmask); 186795f16dc0Seric 1868f129ec7dSeric if (stat(filename, &stb) < 0) 1869e6e1265fSeric stb.st_mode = 0666; 187015d084d5Seric mode = stb.st_mode; 187195f16dc0Seric 187295f16dc0Seric /* limit the errors to those actually caused in the child */ 187395f16dc0Seric errno = 0; 187495f16dc0Seric ExitStat = EX_OK; 187595f16dc0Seric 1876f129ec7dSeric if (bitset(0111, stb.st_mode)) 1877f129ec7dSeric exit(EX_CANTCREAT); 187803827b5fSeric if (ctladdr == NULL) 18798f9146b0Srick ctladdr = &e->e_from; 188015d084d5Seric else 188115d084d5Seric { 188215d084d5Seric /* ignore setuid and setgid bits */ 188315d084d5Seric mode &= ~(S_ISGID|S_ISUID); 188415d084d5Seric } 188515d084d5Seric 18868f9146b0Srick /* we have to open the dfile BEFORE setuid */ 18878f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 18888f9146b0Srick { 18898f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 189095f16dc0Seric if (e->e_dfp == NULL) 189195f16dc0Seric { 18928f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 18938f9146b0Srick e->e_df, e->e_to, e->e_from); 18948f9146b0Srick } 18958f9146b0Srick } 18968f9146b0Srick 189715d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1898e36b99e2Seric { 189995f16dc0Seric if (ctladdr->q_uid == 0) 190095f16dc0Seric { 1901e36b99e2Seric (void) setgid(DefGid); 1902898a126bSbostic (void) initgroups(DefUser, DefGid); 190395f16dc0Seric } 190495f16dc0Seric else 190595f16dc0Seric { 19066259796dSeric (void) setgid(ctladdr->q_gid); 1907898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 1908898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 1909898a126bSbostic ctladdr->q_gid); 1910898a126bSbostic } 1911e36b99e2Seric } 191215d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1913e36b99e2Seric { 1914e36b99e2Seric if (ctladdr->q_uid == 0) 1915e36b99e2Seric (void) setuid(DefUid); 1916e36b99e2Seric else 19176259796dSeric (void) setuid(ctladdr->q_uid); 1918e36b99e2Seric } 191995f16dc0Seric FileName = filename; 192095f16dc0Seric LineNumber = 0; 192127628d59Seric f = dfopen(filename, "a"); 192225a99e2eSeric if (f == NULL) 192395f16dc0Seric { 192408b25121Seric message("554 cannot open"); 192532d19d43Seric exit(EX_CANTCREAT); 192695f16dc0Seric } 192725a99e2eSeric 1928b31e7f2bSeric putfromline(f, ProgMailer, e); 1929b843d759Seric (*e->e_puthdr)(f, ProgMailer, e); 193077b52738Seric putline("\n", f, ProgMailer); 1931b843d759Seric (*e->e_putbody)(f, ProgMailer, e); 193277b52738Seric putline("\n", f, ProgMailer); 193395f16dc0Seric if (ferror(f)) 193495f16dc0Seric { 193508b25121Seric message("451 I/O error"); 193695f16dc0Seric setstat(EX_IOERR); 193795f16dc0Seric } 1938ee4b0922Seric (void) xfclose(f, "mailfile", filename); 193932d19d43Seric (void) fflush(stdout); 1940e36b99e2Seric 194127628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1942c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 194395f16dc0Seric exit(ExitStat); 194413bbc08cSeric /*NOTREACHED*/ 194532d19d43Seric } 194632d19d43Seric else 194732d19d43Seric { 194832d19d43Seric /* parent -- wait for exit status */ 1949588cad61Seric int st; 195032d19d43Seric 1951588cad61Seric st = waitfor(pid); 1952588cad61Seric if ((st & 0377) != 0) 1953588cad61Seric return (EX_UNAVAILABLE); 1954588cad61Seric else 1955588cad61Seric return ((st >> 8) & 0377); 19568f9146b0Srick /*NOTREACHED*/ 195732d19d43Seric } 195825a99e2eSeric } 1959ea4dc939Seric /* 1960e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 1961e103b48fSeric ** 1962e103b48fSeric ** The signature describes how we are going to send this -- it 1963e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 1964e103b48fSeric ** an ordered list of MX hosts. 1965e103b48fSeric ** 1966e103b48fSeric ** Parameters: 1967e103b48fSeric ** m -- the mailer describing this host. 1968e103b48fSeric ** host -- the host name. 1969e103b48fSeric ** e -- the current envelope. 1970e103b48fSeric ** 1971e103b48fSeric ** Returns: 1972e103b48fSeric ** The signature for this host. 1973e103b48fSeric ** 1974e103b48fSeric ** Side Effects: 1975e103b48fSeric ** Can tweak the symbol table. 1976e103b48fSeric */ 1977e103b48fSeric 1978e103b48fSeric char * 1979e103b48fSeric hostsignature(m, host, e) 1980e103b48fSeric register MAILER *m; 1981e103b48fSeric char *host; 1982e103b48fSeric ENVELOPE *e; 1983e103b48fSeric { 1984e103b48fSeric register char *p; 1985e103b48fSeric register STAB *s; 1986e103b48fSeric int i; 1987e103b48fSeric int len; 1988e103b48fSeric #ifdef NAMED_BIND 1989e103b48fSeric int nmx; 1990e103b48fSeric auto int rcode; 1991e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 1992e103b48fSeric static char myhostbuf[MAXNAME]; 1993e103b48fSeric #endif 1994e103b48fSeric 1995e103b48fSeric /* 1996e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 1997e103b48fSeric */ 1998e103b48fSeric 1999e103b48fSeric p = m->m_mailer; 2000e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 2001e103b48fSeric { 2002e103b48fSeric /* just an ordinary mailer */ 2003e103b48fSeric return host; 2004e103b48fSeric } 2005e103b48fSeric 2006e103b48fSeric /* 2007e103b48fSeric ** If it is a numeric address, just return it. 2008e103b48fSeric */ 2009e103b48fSeric 2010e103b48fSeric if (host[0] == '[') 2011e103b48fSeric return host; 2012e103b48fSeric 2013e103b48fSeric /* 2014e103b48fSeric ** Look it up in the symbol table. 2015e103b48fSeric */ 2016e103b48fSeric 2017e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 2018e103b48fSeric if (s->s_hostsig != NULL) 2019e103b48fSeric return s->s_hostsig; 2020e103b48fSeric 2021e103b48fSeric /* 2022e103b48fSeric ** Not already there -- create a signature. 2023e103b48fSeric */ 2024e103b48fSeric 2025e103b48fSeric #ifdef NAMED_BIND 2026e103b48fSeric if (myhostbuf[0] == '\0') 20272bc47524Seric expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e); 2028e103b48fSeric 2029e103b48fSeric nmx = getmxrr(host, mxhosts, myhostbuf, &rcode); 20307d55540cSeric 2031e103b48fSeric if (nmx <= 0) 2032e103b48fSeric { 2033e103b48fSeric register MCI *mci; 2034e103b48fSeric extern int errno; 2035e103b48fSeric extern MCI *mci_get(); 2036e103b48fSeric 2037e103b48fSeric /* update the connection info for this host */ 2038e103b48fSeric mci = mci_get(host, m); 2039e103b48fSeric mci->mci_exitstat = rcode; 2040e103b48fSeric mci->mci_errno = errno; 2041e103b48fSeric 2042e103b48fSeric /* and return the original host name as the signature */ 2043e103b48fSeric s->s_hostsig = host; 2044e103b48fSeric return host; 2045e103b48fSeric } 2046e103b48fSeric 2047e103b48fSeric len = 0; 2048e103b48fSeric for (i = 0; i < nmx; i++) 2049e103b48fSeric { 2050e103b48fSeric len += strlen(mxhosts[i]) + 1; 2051e103b48fSeric } 2052e103b48fSeric s->s_hostsig = p = xalloc(len); 2053e103b48fSeric for (i = 0; i < nmx; i++) 2054e103b48fSeric { 2055e103b48fSeric if (i != 0) 2056e103b48fSeric *p++ = ':'; 2057e103b48fSeric strcpy(p, mxhosts[i]); 2058e103b48fSeric p += strlen(p); 2059e103b48fSeric } 2060e103b48fSeric makelower(s->s_hostsig); 2061e103b48fSeric #else 2062e103b48fSeric /* not using BIND -- the signature is just the host name */ 2063e103b48fSeric s->s_hostsig = host; 2064e103b48fSeric #endif 2065e103b48fSeric if (tTd(17, 1)) 2066e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2067e103b48fSeric return s->s_hostsig; 2068e103b48fSeric } 2069