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*9c9e68d9Seric static char sccsid[] = "@(#)deliver.c 6.55 (Berkeley) 03/26/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 /* 24*9c9e68d9Seric ** SENDALL -- actually send all the messages. 25*9c9e68d9Seric ** 26*9c9e68d9Seric ** Parameters: 27*9c9e68d9Seric ** e -- the envelope to send. 28*9c9e68d9Seric ** mode -- the delivery mode to use. If SM_DEFAULT, use 29*9c9e68d9Seric ** the current e->e_sendmode. 30*9c9e68d9Seric ** 31*9c9e68d9Seric ** Returns: 32*9c9e68d9Seric ** none. 33*9c9e68d9Seric ** 34*9c9e68d9Seric ** Side Effects: 35*9c9e68d9Seric ** Scans the send lists and sends everything it finds. 36*9c9e68d9Seric ** Delivers any appropriate error messages. 37*9c9e68d9Seric ** If we are running in a non-interactive mode, takes the 38*9c9e68d9Seric ** appropriate action. 39*9c9e68d9Seric */ 40*9c9e68d9Seric 41*9c9e68d9Seric sendall(e, mode) 42*9c9e68d9Seric ENVELOPE *e; 43*9c9e68d9Seric char mode; 44*9c9e68d9Seric { 45*9c9e68d9Seric register ADDRESS *q; 46*9c9e68d9Seric char *owner; 47*9c9e68d9Seric int otherowners; 48*9c9e68d9Seric register ENVELOPE *ee; 49*9c9e68d9Seric ENVELOPE *splitenv = NULL; 50*9c9e68d9Seric 51*9c9e68d9Seric /* determine actual delivery mode */ 52*9c9e68d9Seric if (mode == SM_DEFAULT) 53*9c9e68d9Seric { 54*9c9e68d9Seric extern bool shouldqueue(); 55*9c9e68d9Seric 56*9c9e68d9Seric mode = e->e_sendmode; 57*9c9e68d9Seric if (mode != SM_VERIFY && 58*9c9e68d9Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 59*9c9e68d9Seric mode = SM_QUEUE; 60*9c9e68d9Seric } 61*9c9e68d9Seric 62*9c9e68d9Seric if (tTd(13, 1)) 63*9c9e68d9Seric { 64*9c9e68d9Seric printf("\nSENDALL: mode %c, e_from ", mode); 65*9c9e68d9Seric printaddr(&e->e_from, FALSE); 66*9c9e68d9Seric printf("sendqueue:\n"); 67*9c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 68*9c9e68d9Seric } 69*9c9e68d9Seric 70*9c9e68d9Seric /* 71*9c9e68d9Seric ** Do any preprocessing necessary for the mode we are running. 72*9c9e68d9Seric ** Check to make sure the hop count is reasonable. 73*9c9e68d9Seric ** Delete sends to the sender in mailing lists. 74*9c9e68d9Seric */ 75*9c9e68d9Seric 76*9c9e68d9Seric CurEnv = e; 77*9c9e68d9Seric 78*9c9e68d9Seric if (e->e_hopcount > MaxHopCount) 79*9c9e68d9Seric { 80*9c9e68d9Seric errno = 0; 81*9c9e68d9Seric syserr("554 too many hops %d (%d max): from %s, to %s", 82*9c9e68d9Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 83*9c9e68d9Seric e->e_sendqueue->q_paddr); 84*9c9e68d9Seric return; 85*9c9e68d9Seric } 86*9c9e68d9Seric 87*9c9e68d9Seric if (!MeToo) 88*9c9e68d9Seric { 89*9c9e68d9Seric extern ADDRESS *recipient(); 90*9c9e68d9Seric 91*9c9e68d9Seric if (tTd(13, 5)) 92*9c9e68d9Seric { 93*9c9e68d9Seric printf("sendall: QDONTSEND "); 94*9c9e68d9Seric printaddr(&e->e_from, FALSE); 95*9c9e68d9Seric } 96*9c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 97*9c9e68d9Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 98*9c9e68d9Seric } 99*9c9e68d9Seric 100*9c9e68d9Seric /* 101*9c9e68d9Seric ** Handle alias owners. 102*9c9e68d9Seric ** 103*9c9e68d9Seric ** We scan up the q_alias chain looking for owners. 104*9c9e68d9Seric ** We discard owners that are the same as the return path. 105*9c9e68d9Seric */ 106*9c9e68d9Seric 107*9c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 108*9c9e68d9Seric { 109*9c9e68d9Seric register struct address *a; 110*9c9e68d9Seric 111*9c9e68d9Seric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 112*9c9e68d9Seric continue; 113*9c9e68d9Seric if (a != NULL) 114*9c9e68d9Seric q->q_owner = a->q_owner; 115*9c9e68d9Seric 116*9c9e68d9Seric if (q->q_owner != NULL && 117*9c9e68d9Seric !bitset(QDONTSEND, q->q_flags) && 118*9c9e68d9Seric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 119*9c9e68d9Seric q->q_owner = NULL; 120*9c9e68d9Seric } 121*9c9e68d9Seric 122*9c9e68d9Seric owner = ""; 123*9c9e68d9Seric otherowners = 1; 124*9c9e68d9Seric while (owner != NULL && otherowners > 0) 125*9c9e68d9Seric { 126*9c9e68d9Seric owner = NULL; 127*9c9e68d9Seric otherowners = 0; 128*9c9e68d9Seric 129*9c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 130*9c9e68d9Seric { 131*9c9e68d9Seric if (bitset(QDONTSEND, q->q_flags)) 132*9c9e68d9Seric continue; 133*9c9e68d9Seric 134*9c9e68d9Seric if (q->q_owner != NULL) 135*9c9e68d9Seric { 136*9c9e68d9Seric if (owner == NULL) 137*9c9e68d9Seric owner = q->q_owner; 138*9c9e68d9Seric else if (owner != q->q_owner) 139*9c9e68d9Seric { 140*9c9e68d9Seric if (strcmp(owner, q->q_owner) == 0) 141*9c9e68d9Seric { 142*9c9e68d9Seric /* make future comparisons cheap */ 143*9c9e68d9Seric q->q_owner = owner; 144*9c9e68d9Seric } 145*9c9e68d9Seric else 146*9c9e68d9Seric { 147*9c9e68d9Seric otherowners++; 148*9c9e68d9Seric } 149*9c9e68d9Seric owner = q->q_owner; 150*9c9e68d9Seric } 151*9c9e68d9Seric } 152*9c9e68d9Seric else 153*9c9e68d9Seric { 154*9c9e68d9Seric otherowners++; 155*9c9e68d9Seric } 156*9c9e68d9Seric } 157*9c9e68d9Seric 158*9c9e68d9Seric if (owner != NULL && otherowners > 0) 159*9c9e68d9Seric { 160*9c9e68d9Seric extern HDR *copyheader(); 161*9c9e68d9Seric extern ADDRESS *copyqueue(); 162*9c9e68d9Seric 163*9c9e68d9Seric /* 164*9c9e68d9Seric ** Split this envelope into two. 165*9c9e68d9Seric */ 166*9c9e68d9Seric 167*9c9e68d9Seric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 168*9c9e68d9Seric *ee = *e; 169*9c9e68d9Seric ee->e_id = NULL; 170*9c9e68d9Seric (void) queuename(ee, '\0'); 171*9c9e68d9Seric 172*9c9e68d9Seric if (tTd(13, 1)) 173*9c9e68d9Seric printf("sendall: split %s into %s\n", 174*9c9e68d9Seric e->e_id, ee->e_id); 175*9c9e68d9Seric 176*9c9e68d9Seric ee->e_header = copyheader(e->e_header); 177*9c9e68d9Seric ee->e_sendqueue = copyqueue(e->e_sendqueue); 178*9c9e68d9Seric ee->e_errorqueue = copyqueue(e->e_errorqueue); 179*9c9e68d9Seric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE); 180*9c9e68d9Seric setsender(owner, ee, NULL, TRUE); 181*9c9e68d9Seric if (tTd(13, 5)) 182*9c9e68d9Seric { 183*9c9e68d9Seric printf("sendall(split): QDONTSEND "); 184*9c9e68d9Seric printaddr(&ee->e_from, FALSE); 185*9c9e68d9Seric } 186*9c9e68d9Seric ee->e_from.q_flags |= QDONTSEND; 187*9c9e68d9Seric ee->e_dfp = NULL; 188*9c9e68d9Seric ee->e_xfp = NULL; 189*9c9e68d9Seric ee->e_lockfp = NULL; 190*9c9e68d9Seric ee->e_df = NULL; 191*9c9e68d9Seric ee->e_errormode = EM_MAIL; 192*9c9e68d9Seric ee->e_sibling = splitenv; 193*9c9e68d9Seric splitenv = ee; 194*9c9e68d9Seric 195*9c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 196*9c9e68d9Seric if (q->q_owner == owner) 197*9c9e68d9Seric q->q_flags |= QDONTSEND; 198*9c9e68d9Seric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 199*9c9e68d9Seric if (q->q_owner != owner) 200*9c9e68d9Seric q->q_flags |= QDONTSEND; 201*9c9e68d9Seric 202*9c9e68d9Seric if (e->e_df != NULL && mode != SM_VERIFY) 203*9c9e68d9Seric { 204*9c9e68d9Seric ee->e_dfp = NULL; 205*9c9e68d9Seric ee->e_df = newstr(queuename(ee, 'd')); 206*9c9e68d9Seric if (link(e->e_df, ee->e_df) < 0) 207*9c9e68d9Seric { 208*9c9e68d9Seric syserr("sendall: link(%s, %s)", 209*9c9e68d9Seric e->e_df, ee->e_df); 210*9c9e68d9Seric } 211*9c9e68d9Seric } 212*9c9e68d9Seric 213*9c9e68d9Seric if (mode != SM_VERIFY) 214*9c9e68d9Seric openxscript(ee); 215*9c9e68d9Seric #ifdef LOG 216*9c9e68d9Seric if (LogLevel > 4) 217*9c9e68d9Seric syslog(LOG_INFO, "%s: clone %s", 218*9c9e68d9Seric ee->e_id, e->e_id); 219*9c9e68d9Seric #endif 220*9c9e68d9Seric } 221*9c9e68d9Seric } 222*9c9e68d9Seric 223*9c9e68d9Seric if (owner != NULL) 224*9c9e68d9Seric { 225*9c9e68d9Seric setsender(owner, e, NULL, TRUE); 226*9c9e68d9Seric if (tTd(13, 5)) 227*9c9e68d9Seric { 228*9c9e68d9Seric printf("sendall(owner): QDONTSEND "); 229*9c9e68d9Seric printaddr(&e->e_from, FALSE); 230*9c9e68d9Seric } 231*9c9e68d9Seric e->e_from.q_flags |= QDONTSEND; 232*9c9e68d9Seric e->e_errormode = EM_MAIL; 233*9c9e68d9Seric } 234*9c9e68d9Seric 235*9c9e68d9Seric # ifdef QUEUE 236*9c9e68d9Seric if ((mode == SM_QUEUE || mode == SM_FORK || 237*9c9e68d9Seric (mode != SM_VERIFY && SuperSafe)) && 238*9c9e68d9Seric !bitset(EF_INQUEUE, e->e_flags)) 239*9c9e68d9Seric { 240*9c9e68d9Seric /* be sure everything is instantiated in the queue */ 241*9c9e68d9Seric queueup(e, TRUE, mode == SM_QUEUE); 242*9c9e68d9Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 243*9c9e68d9Seric queueup(ee, TRUE, mode == SM_QUEUE); 244*9c9e68d9Seric } 245*9c9e68d9Seric #endif /* QUEUE */ 246*9c9e68d9Seric 247*9c9e68d9Seric if (splitenv != NULL) 248*9c9e68d9Seric { 249*9c9e68d9Seric if (tTd(13, 1)) 250*9c9e68d9Seric { 251*9c9e68d9Seric printf("\nsendall: Split queue; remaining queue:\n"); 252*9c9e68d9Seric printaddr(e->e_sendqueue, TRUE); 253*9c9e68d9Seric } 254*9c9e68d9Seric 255*9c9e68d9Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 256*9c9e68d9Seric { 257*9c9e68d9Seric CurEnv = ee; 258*9c9e68d9Seric sendenvelope(ee, mode); 259*9c9e68d9Seric } 260*9c9e68d9Seric 261*9c9e68d9Seric CurEnv = e; 262*9c9e68d9Seric } 263*9c9e68d9Seric sendenvelope(e, mode); 264*9c9e68d9Seric 265*9c9e68d9Seric for (; splitenv != NULL; splitenv = splitenv->e_sibling) 266*9c9e68d9Seric dropenvelope(splitenv); 267*9c9e68d9Seric } 268*9c9e68d9Seric 269*9c9e68d9Seric sendenvelope(e, mode) 270*9c9e68d9Seric register ENVELOPE *e; 271*9c9e68d9Seric char mode; 272*9c9e68d9Seric { 273*9c9e68d9Seric bool oldverbose; 274*9c9e68d9Seric int pid; 275*9c9e68d9Seric register ADDRESS *q; 276*9c9e68d9Seric #ifdef LOCKF 277*9c9e68d9Seric struct flock lfd; 278*9c9e68d9Seric #endif 279*9c9e68d9Seric 280*9c9e68d9Seric oldverbose = Verbose; 281*9c9e68d9Seric switch (mode) 282*9c9e68d9Seric { 283*9c9e68d9Seric case SM_VERIFY: 284*9c9e68d9Seric Verbose = TRUE; 285*9c9e68d9Seric break; 286*9c9e68d9Seric 287*9c9e68d9Seric case SM_QUEUE: 288*9c9e68d9Seric queueonly: 289*9c9e68d9Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 290*9c9e68d9Seric return; 291*9c9e68d9Seric 292*9c9e68d9Seric case SM_FORK: 293*9c9e68d9Seric if (e->e_xfp != NULL) 294*9c9e68d9Seric (void) fflush(e->e_xfp); 295*9c9e68d9Seric 296*9c9e68d9Seric # ifdef LOCKF 297*9c9e68d9Seric /* 298*9c9e68d9Seric ** Since lockf has the interesting semantic that the 299*9c9e68d9Seric ** lock is lost when we fork, we have to risk losing 300*9c9e68d9Seric ** the lock here by closing before the fork, and then 301*9c9e68d9Seric ** trying to get it back in the child. 302*9c9e68d9Seric */ 303*9c9e68d9Seric 304*9c9e68d9Seric if (e->e_lockfp != NULL) 305*9c9e68d9Seric { 306*9c9e68d9Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 307*9c9e68d9Seric e->e_lockfp = NULL; 308*9c9e68d9Seric } 309*9c9e68d9Seric # endif /* LOCKF */ 310*9c9e68d9Seric 311*9c9e68d9Seric pid = fork(); 312*9c9e68d9Seric if (pid < 0) 313*9c9e68d9Seric { 314*9c9e68d9Seric goto queueonly; 315*9c9e68d9Seric } 316*9c9e68d9Seric else if (pid > 0) 317*9c9e68d9Seric { 318*9c9e68d9Seric /* be sure we leave the temp files to our child */ 319*9c9e68d9Seric e->e_id = e->e_df = NULL; 320*9c9e68d9Seric # ifndef LOCKF 321*9c9e68d9Seric if (e->e_lockfp != NULL) 322*9c9e68d9Seric { 323*9c9e68d9Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 324*9c9e68d9Seric e->e_lockfp = NULL; 325*9c9e68d9Seric } 326*9c9e68d9Seric # endif 327*9c9e68d9Seric 328*9c9e68d9Seric /* close any random open files in the envelope */ 329*9c9e68d9Seric if (e->e_dfp != NULL) 330*9c9e68d9Seric { 331*9c9e68d9Seric (void) xfclose(e->e_dfp, "sendenvelope", "dfp"); 332*9c9e68d9Seric e->e_dfp = NULL; 333*9c9e68d9Seric } 334*9c9e68d9Seric if (e->e_xfp != NULL) 335*9c9e68d9Seric { 336*9c9e68d9Seric (void) xfclose(e->e_xfp, "sendenvelope", "xfp"); 337*9c9e68d9Seric e->e_xfp = NULL; 338*9c9e68d9Seric } 339*9c9e68d9Seric return; 340*9c9e68d9Seric } 341*9c9e68d9Seric 342*9c9e68d9Seric /* double fork to avoid zombies */ 343*9c9e68d9Seric if (fork() > 0) 344*9c9e68d9Seric exit(EX_OK); 345*9c9e68d9Seric 346*9c9e68d9Seric /* be sure we are immune from the terminal */ 347*9c9e68d9Seric disconnect(FALSE, e); 348*9c9e68d9Seric 349*9c9e68d9Seric # ifdef LOCKF 350*9c9e68d9Seric /* 351*9c9e68d9Seric ** Now try to get our lock back. 352*9c9e68d9Seric */ 353*9c9e68d9Seric 354*9c9e68d9Seric lfd.l_type = F_WRLCK; 355*9c9e68d9Seric lfd.l_whence = lfd.l_start = lfd.l_len = 0; 356*9c9e68d9Seric e->e_lockfp = fopen(queuename(e, 'q'), "r+"); 357*9c9e68d9Seric if (e->e_lockfp == NULL || 358*9c9e68d9Seric fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0) 359*9c9e68d9Seric { 360*9c9e68d9Seric /* oops.... lost it */ 361*9c9e68d9Seric if (tTd(13, 1)) 362*9c9e68d9Seric printf("sendenvelope: %s lost lock: lockfp=%x, %s\n", 363*9c9e68d9Seric e->e_id, e->e_lockfp, errstring(errno)); 364*9c9e68d9Seric 365*9c9e68d9Seric # ifdef LOG 366*9c9e68d9Seric if (LogLevel > 29) 367*9c9e68d9Seric syslog(LOG_NOTICE, "%s: lost lock: %m", 368*9c9e68d9Seric e->e_id); 369*9c9e68d9Seric # endif /* LOG */ 370*9c9e68d9Seric exit(EX_OK); 371*9c9e68d9Seric } 372*9c9e68d9Seric # endif /* LOCKF */ 373*9c9e68d9Seric 374*9c9e68d9Seric /* 375*9c9e68d9Seric ** Close any cached connections. 376*9c9e68d9Seric ** 377*9c9e68d9Seric ** We don't send the QUIT protocol because the parent 378*9c9e68d9Seric ** still knows about the connection. 379*9c9e68d9Seric ** 380*9c9e68d9Seric ** This should only happen when delivering an error 381*9c9e68d9Seric ** message. 382*9c9e68d9Seric */ 383*9c9e68d9Seric 384*9c9e68d9Seric mci_flush(FALSE, NULL); 385*9c9e68d9Seric 386*9c9e68d9Seric break; 387*9c9e68d9Seric } 388*9c9e68d9Seric 389*9c9e68d9Seric /* 390*9c9e68d9Seric ** Run through the list and send everything. 391*9c9e68d9Seric */ 392*9c9e68d9Seric 393*9c9e68d9Seric e->e_nsent = 0; 394*9c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 395*9c9e68d9Seric { 396*9c9e68d9Seric if (mode == SM_VERIFY) 397*9c9e68d9Seric { 398*9c9e68d9Seric e->e_to = q->q_paddr; 399*9c9e68d9Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 400*9c9e68d9Seric message("deliverable"); 401*9c9e68d9Seric } 402*9c9e68d9Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 403*9c9e68d9Seric { 404*9c9e68d9Seric # ifdef QUEUE 405*9c9e68d9Seric /* 406*9c9e68d9Seric ** Checkpoint the send list every few addresses 407*9c9e68d9Seric */ 408*9c9e68d9Seric 409*9c9e68d9Seric if (e->e_nsent >= CheckpointInterval) 410*9c9e68d9Seric { 411*9c9e68d9Seric queueup(e, TRUE, FALSE); 412*9c9e68d9Seric e->e_nsent = 0; 413*9c9e68d9Seric } 414*9c9e68d9Seric # endif /* QUEUE */ 415*9c9e68d9Seric (void) deliver(e, q); 416*9c9e68d9Seric } 417*9c9e68d9Seric } 418*9c9e68d9Seric Verbose = oldverbose; 419*9c9e68d9Seric 420*9c9e68d9Seric /* 421*9c9e68d9Seric ** Now run through and check for errors. 422*9c9e68d9Seric */ 423*9c9e68d9Seric 424*9c9e68d9Seric if (mode == SM_VERIFY) 425*9c9e68d9Seric { 426*9c9e68d9Seric return; 427*9c9e68d9Seric } 428*9c9e68d9Seric 429*9c9e68d9Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 430*9c9e68d9Seric { 431*9c9e68d9Seric if (tTd(13, 3)) 432*9c9e68d9Seric { 433*9c9e68d9Seric printf("Checking "); 434*9c9e68d9Seric printaddr(q, FALSE); 435*9c9e68d9Seric } 436*9c9e68d9Seric 437*9c9e68d9Seric /* only send errors if the message failed */ 438*9c9e68d9Seric if (!bitset(QBADADDR, q->q_flags)) 439*9c9e68d9Seric continue; 440*9c9e68d9Seric 441*9c9e68d9Seric e->e_flags |= EF_FATALERRS; 442*9c9e68d9Seric 443*9c9e68d9Seric if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0) 444*9c9e68d9Seric (void) sendtolist(e->e_from.q_paddr, NULL, 445*9c9e68d9Seric &e->e_errorqueue, e); 446*9c9e68d9Seric } 447*9c9e68d9Seric 448*9c9e68d9Seric if (mode == SM_FORK) 449*9c9e68d9Seric finis(); 450*9c9e68d9Seric } 451*9c9e68d9Seric /* 452*9c9e68d9Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 453*9c9e68d9Seric ** 454*9c9e68d9Seric ** This MUST be a macro, since after a vfork we are running 455*9c9e68d9Seric ** two processes on the same stack!!! 456*9c9e68d9Seric ** 457*9c9e68d9Seric ** Parameters: 458*9c9e68d9Seric ** none. 459*9c9e68d9Seric ** 460*9c9e68d9Seric ** Returns: 461*9c9e68d9Seric ** From a macro??? You've got to be kidding! 462*9c9e68d9Seric ** 463*9c9e68d9Seric ** Side Effects: 464*9c9e68d9Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 465*9c9e68d9Seric ** pid of child in parent, zero in child. 466*9c9e68d9Seric ** -1 on unrecoverable error. 467*9c9e68d9Seric ** 468*9c9e68d9Seric ** Notes: 469*9c9e68d9Seric ** I'm awfully sorry this looks so awful. That's 470*9c9e68d9Seric ** vfork for you..... 471*9c9e68d9Seric */ 472*9c9e68d9Seric 473*9c9e68d9Seric # define NFORKTRIES 5 474*9c9e68d9Seric 475*9c9e68d9Seric # ifndef FORK 476*9c9e68d9Seric # define FORK fork 477*9c9e68d9Seric # endif 478*9c9e68d9Seric 479*9c9e68d9Seric # define DOFORK(fORKfN) \ 480*9c9e68d9Seric {\ 481*9c9e68d9Seric register int i;\ 482*9c9e68d9Seric \ 483*9c9e68d9Seric for (i = NFORKTRIES; --i >= 0; )\ 484*9c9e68d9Seric {\ 485*9c9e68d9Seric pid = fORKfN();\ 486*9c9e68d9Seric if (pid >= 0)\ 487*9c9e68d9Seric break;\ 488*9c9e68d9Seric if (i > 0)\ 489*9c9e68d9Seric sleep((unsigned) NFORKTRIES - i);\ 490*9c9e68d9Seric }\ 491*9c9e68d9Seric } 492*9c9e68d9Seric /* 493*9c9e68d9Seric ** DOFORK -- simple fork interface to DOFORK. 494*9c9e68d9Seric ** 495*9c9e68d9Seric ** Parameters: 496*9c9e68d9Seric ** none. 497*9c9e68d9Seric ** 498*9c9e68d9Seric ** Returns: 499*9c9e68d9Seric ** pid of child in parent. 500*9c9e68d9Seric ** zero in child. 501*9c9e68d9Seric ** -1 on error. 502*9c9e68d9Seric ** 503*9c9e68d9Seric ** Side Effects: 504*9c9e68d9Seric ** returns twice, once in parent and once in child. 505*9c9e68d9Seric */ 506*9c9e68d9Seric 507*9c9e68d9Seric dofork() 508*9c9e68d9Seric { 509*9c9e68d9Seric register int pid; 510*9c9e68d9Seric 511*9c9e68d9Seric DOFORK(fork); 512*9c9e68d9Seric return (pid); 513*9c9e68d9Seric } 514*9c9e68d9Seric /* 51513bbc08cSeric ** DELIVER -- Deliver a message to a list of addresses. 51613bbc08cSeric ** 51713bbc08cSeric ** This routine delivers to everyone on the same host as the 51813bbc08cSeric ** user on the head of the list. It is clever about mailers 51913bbc08cSeric ** that don't handle multiple users. It is NOT guaranteed 52013bbc08cSeric ** that it will deliver to all these addresses however -- so 52113bbc08cSeric ** deliver should be called once for each address on the 52213bbc08cSeric ** list. 52325a99e2eSeric ** 52425a99e2eSeric ** Parameters: 525588cad61Seric ** e -- the envelope to deliver. 526c77d1c25Seric ** firstto -- head of the address list to deliver to. 52725a99e2eSeric ** 52825a99e2eSeric ** Returns: 52925a99e2eSeric ** zero -- successfully delivered. 53025a99e2eSeric ** else -- some failure, see ExitStat for more info. 53125a99e2eSeric ** 53225a99e2eSeric ** Side Effects: 53325a99e2eSeric ** The standard input is passed off to someone. 53425a99e2eSeric */ 53525a99e2eSeric 536588cad61Seric deliver(e, firstto) 537588cad61Seric register ENVELOPE *e; 538c77d1c25Seric ADDRESS *firstto; 53925a99e2eSeric { 54078442df3Seric char *host; /* host being sent to */ 54178442df3Seric char *user; /* user being sent to */ 54225a99e2eSeric char **pvp; 5435dfc646bSeric register char **mvp; 54425a99e2eSeric register char *p; 545588cad61Seric register MAILER *m; /* mailer for this recipient */ 5466259796dSeric ADDRESS *ctladdr; 547b31e7f2bSeric register MCI *mci; 548c77d1c25Seric register ADDRESS *to = firstto; 549c579ef51Seric bool clever = FALSE; /* running user smtp to this mailer */ 550772e6e50Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 551911693bfSbostic int rcode; /* response code */ 552e103b48fSeric char *firstsig; /* signature of firstto */ 553*9c9e68d9Seric int pid; 554*9c9e68d9Seric char *curhost; 555*9c9e68d9Seric int mpvect[2]; 556*9c9e68d9Seric int rpvect[2]; 557ee6bf8dfSeric char *pv[MAXPV+1]; 558579ef0ddSeric char tobuf[TOBUFSIZE]; /* text line of to people */ 559ee6bf8dfSeric char buf[MAXNAME]; 560c23ed322Seric char rpathbuf[MAXNAME]; /* translated return path */ 561fabb3bd4Seric extern int checkcompat(); 562ee6bf8dfSeric extern ADDRESS *getctladdr(); 563ee6bf8dfSeric extern char *remotename(); 564e103b48fSeric extern char *hostsignature(); 565*9c9e68d9Seric extern FILE *fdopen(); 56625a99e2eSeric 56735490626Seric errno = 0; 568ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5695dfc646bSeric return (0); 57025a99e2eSeric 571134746fbSeric #ifdef NAMED_BIND 572912a731aSbostic /* unless interactive, try twice, over a minute */ 573912a731aSbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 574912a731aSbostic _res.retrans = 30; 575912a731aSbostic _res.retry = 2; 576912a731aSbostic } 577d4bd8f0eSbostic #endif 578912a731aSbostic 57951552439Seric m = to->q_mailer; 58051552439Seric host = to->q_host; 581c9be6216Seric CurEnv = e; /* just in case */ 58251552439Seric 5836ef48975Seric if (tTd(10, 1)) 5845dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 58551552439Seric m->m_mno, host, to->q_user); 586f3dbc832Seric 587f3dbc832Seric /* 588f3dbc832Seric ** If this mailer is expensive, and if we don't want to make 589f3dbc832Seric ** connections now, just mark these addresses and return. 590f3dbc832Seric ** This is useful if we want to batch connections to 591f3dbc832Seric ** reduce load. This will cause the messages to be 592f3dbc832Seric ** queued up, and a daemon will come along to send the 593f3dbc832Seric ** messages later. 594f3dbc832Seric ** This should be on a per-mailer basis. 595f3dbc832Seric */ 596f3dbc832Seric 59719c47125Seric if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) && 59819c47125Seric bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 599f3dbc832Seric { 600f3dbc832Seric for (; to != NULL; to = to->q_next) 601f4560e80Seric { 602ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 603c6e18ac5Seric to->q_mailer != m) 604f4560e80Seric continue; 605f3dbc832Seric to->q_flags |= QQUEUEUP|QDONTSEND; 606588cad61Seric e->e_to = to->q_paddr; 60708b25121Seric message("queued"); 6082f624c86Seric if (LogLevel > 8) 60981161401Seric logdelivery(m, NULL, "queued", e); 610f4560e80Seric } 611588cad61Seric e->e_to = NULL; 612f3dbc832Seric return (0); 613f3dbc832Seric } 614f3dbc832Seric 61525a99e2eSeric /* 6165dfc646bSeric ** Do initial argv setup. 6175dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 6185dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 6195dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 6205dfc646bSeric ** code does not check for 'pv' overflow; this places a 6215dfc646bSeric ** manifest lower limit of 4 for MAXPV. 6223bea8136Seric ** The from address rewrite is expected to make 6233bea8136Seric ** the address relative to the other end. 6245dfc646bSeric */ 6255dfc646bSeric 62678442df3Seric /* rewrite from address, using rewriting rules */ 627ee4b0922Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE, 6289c69ca01Seric TRUE, FALSE, e)); 629ee4b0922Seric define('g', rpathbuf, e); /* translated return path */ 630588cad61Seric define('h', host, e); /* to host */ 6315dfc646bSeric Errors = 0; 6325dfc646bSeric pvp = pv; 6335dfc646bSeric *pvp++ = m->m_argv[0]; 6345dfc646bSeric 6355dfc646bSeric /* insert -f or -r flag as appropriate */ 63657fc6f17Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6375dfc646bSeric { 63857fc6f17Seric if (bitnset(M_FOPT, m->m_flags)) 6395dfc646bSeric *pvp++ = "-f"; 6405dfc646bSeric else 6415dfc646bSeric *pvp++ = "-r"; 642c23ed322Seric *pvp++ = newstr(rpathbuf); 6435dfc646bSeric } 6445dfc646bSeric 6455dfc646bSeric /* 6465dfc646bSeric ** Append the other fixed parts of the argv. These run 6475dfc646bSeric ** up to the first entry containing "$u". There can only 6485dfc646bSeric ** be one of these, and there are only a few more slots 6495dfc646bSeric ** in the pv after it. 6505dfc646bSeric */ 6515dfc646bSeric 6525dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 6535dfc646bSeric { 6542bc47524Seric /* can't use strchr here because of sign extension problems */ 6552bc47524Seric while (*p != '\0') 6562bc47524Seric { 6572bc47524Seric if ((*p++ & 0377) == MACROEXPAND) 6582bc47524Seric { 6592bc47524Seric if (*p == 'u') 6605dfc646bSeric break; 6612bc47524Seric } 6622bc47524Seric } 6632bc47524Seric 6642bc47524Seric if (*p != '\0') 6655dfc646bSeric break; 6665dfc646bSeric 6675dfc646bSeric /* this entry is safe -- go ahead and process it */ 668588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 6695dfc646bSeric *pvp++ = newstr(buf); 6705dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 6715dfc646bSeric { 67208b25121Seric syserr("554 Too many parameters to %s before $u", pv[0]); 6735dfc646bSeric return (-1); 6745dfc646bSeric } 6755dfc646bSeric } 676c579ef51Seric 67733db8731Seric /* 67833db8731Seric ** If we have no substitution for the user name in the argument 67933db8731Seric ** list, we know that we must supply the names otherwise -- and 68033db8731Seric ** SMTP is the answer!! 68133db8731Seric */ 68233db8731Seric 6835dfc646bSeric if (*mvp == NULL) 684c579ef51Seric { 685c579ef51Seric /* running SMTP */ 6862c7e1b8dSeric # ifdef SMTP 687c579ef51Seric clever = TRUE; 688c579ef51Seric *pvp = NULL; 6896c2c3107Seric # else /* SMTP */ 69033db8731Seric /* oops! we don't implement SMTP */ 69108b25121Seric syserr("554 SMTP style mailer"); 6922c7e1b8dSeric return (EX_SOFTWARE); 6936c2c3107Seric # endif /* SMTP */ 694c579ef51Seric } 6955dfc646bSeric 6965dfc646bSeric /* 6975dfc646bSeric ** At this point *mvp points to the argument with $u. We 6985dfc646bSeric ** run through our address list and append all the addresses 6995dfc646bSeric ** we can. If we run out of space, do not fret! We can 7005dfc646bSeric ** always send another copy later. 7015dfc646bSeric */ 7025dfc646bSeric 7035dfc646bSeric tobuf[0] = '\0'; 704588cad61Seric e->e_to = tobuf; 7056259796dSeric ctladdr = NULL; 706e103b48fSeric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7075dfc646bSeric for (; to != NULL; to = to->q_next) 7085dfc646bSeric { 7095dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 71057fc6f17Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7115dfc646bSeric break; 7125dfc646bSeric 7135dfc646bSeric /* if already sent or not for this host, don't send */ 714ee4b0922Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 715e103b48fSeric to->q_mailer != firstto->q_mailer || 716e103b48fSeric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7175dfc646bSeric continue; 7186259796dSeric 7194b22ea87Seric /* avoid overflowing tobuf */ 720aa50a568Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7214b22ea87Seric break; 7224b22ea87Seric 7236ef48975Seric if (tTd(10, 1)) 724772e6e50Seric { 725772e6e50Seric printf("\nsend to "); 726772e6e50Seric printaddr(to, FALSE); 727772e6e50Seric } 728772e6e50Seric 7296259796dSeric /* compute effective uid/gid when sending */ 7307da1035fSeric if (to->q_mailer == ProgMailer) 7316259796dSeric ctladdr = getctladdr(to); 7326259796dSeric 7335dfc646bSeric user = to->q_user; 734588cad61Seric e->e_to = to->q_paddr; 73575f1ade9Seric if (tTd(10, 5)) 73675f1ade9Seric { 73775f1ade9Seric printf("deliver: QDONTSEND "); 73875f1ade9Seric printaddr(to, FALSE); 73975f1ade9Seric } 740ee4b0922Seric to->q_flags |= QDONTSEND; 7415dfc646bSeric 7425dfc646bSeric /* 7435dfc646bSeric ** Check to see that these people are allowed to 7445dfc646bSeric ** talk to each other. 7452a6e0786Seric */ 7462a6e0786Seric 74769582d2fSeric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 74869582d2fSeric { 74969582d2fSeric NoReturn = TRUE; 75008b25121Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 75181161401Seric giveresponse(EX_UNAVAILABLE, m, NULL, e); 75269582d2fSeric continue; 75369582d2fSeric } 754fabb3bd4Seric rcode = checkcompat(to, e); 7551793c9c5Seric if (rcode != EX_OK) 7565dfc646bSeric { 75781161401Seric giveresponse(rcode, m, NULL, e); 7585dfc646bSeric continue; 7595dfc646bSeric } 7602a6e0786Seric 7612a6e0786Seric /* 7629ec9501bSeric ** Strip quote bits from names if the mailer is dumb 7639ec9501bSeric ** about them. 76425a99e2eSeric */ 76525a99e2eSeric 76657fc6f17Seric if (bitnset(M_STRIPQ, m->m_flags)) 76725a99e2eSeric { 7681d8f1806Seric stripquotes(user); 7691d8f1806Seric stripquotes(host); 77025a99e2eSeric } 77125a99e2eSeric 772cdb828c5Seric /* hack attack -- delivermail compatibility */ 773cdb828c5Seric if (m == ProgMailer && *user == '|') 774cdb828c5Seric user++; 775cdb828c5Seric 77625a99e2eSeric /* 7773efaed6eSeric ** If an error message has already been given, don't 7783efaed6eSeric ** bother to send to this address. 7793efaed6eSeric ** 7803efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 7813efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 7823efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 7833efaed6eSeric */ 7843efaed6eSeric 7856cae517dSeric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 7863efaed6eSeric continue; 7873efaed6eSeric 788f2fec898Seric /* save statistics.... */ 789588cad61Seric markstats(e, to); 790f2fec898Seric 7913efaed6eSeric /* 79225a99e2eSeric ** See if this user name is "special". 79325a99e2eSeric ** If the user name has a slash in it, assume that this 79451552439Seric ** is a file -- send it off without further ado. Note 79551552439Seric ** that this type of addresses is not processed along 79651552439Seric ** with the others, so we fudge on the To person. 79725a99e2eSeric */ 79825a99e2eSeric 7992c017f8dSeric if (m == FileMailer) 80025a99e2eSeric { 801b31e7f2bSeric rcode = mailfile(user, getctladdr(to), e); 80281161401Seric giveresponse(rcode, m, NULL, e); 803dde5acadSeric if (rcode == EX_OK) 804dde5acadSeric to->q_flags |= QSENT; 8055dfc646bSeric continue; 80625a99e2eSeric } 80725a99e2eSeric 80813bbc08cSeric /* 80913bbc08cSeric ** Address is verified -- add this user to mailer 81013bbc08cSeric ** argv, and add it to the print list of recipients. 81113bbc08cSeric */ 81213bbc08cSeric 813508daeccSeric /* link together the chain of recipients */ 814508daeccSeric to->q_tchain = tochain; 815508daeccSeric tochain = to; 816508daeccSeric 8175dfc646bSeric /* create list of users for error messages */ 818db8841e9Seric (void) strcat(tobuf, ","); 819db8841e9Seric (void) strcat(tobuf, to->q_paddr); 820588cad61Seric define('u', user, e); /* to user */ 821588cad61Seric define('z', to->q_home, e); /* user's home */ 8225dfc646bSeric 823c579ef51Seric /* 824508daeccSeric ** Expand out this user into argument list. 825c579ef51Seric */ 826c579ef51Seric 827508daeccSeric if (!clever) 828c579ef51Seric { 829588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8305dfc646bSeric *pvp++ = newstr(buf); 8315dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 8325dfc646bSeric { 8335dfc646bSeric /* allow some space for trailing parms */ 8345dfc646bSeric break; 8355dfc646bSeric } 8365dfc646bSeric } 837c579ef51Seric } 8385dfc646bSeric 839145b49b1Seric /* see if any addresses still exist */ 840145b49b1Seric if (tobuf[0] == '\0') 841c579ef51Seric { 842588cad61Seric define('g', (char *) NULL, e); 843145b49b1Seric return (0); 844c579ef51Seric } 845145b49b1Seric 8465dfc646bSeric /* print out messages as full list */ 84763780dbdSeric e->e_to = tobuf + 1; 8485dfc646bSeric 8495dfc646bSeric /* 8505dfc646bSeric ** Fill out any parameters after the $u parameter. 8515dfc646bSeric */ 8525dfc646bSeric 853c579ef51Seric while (!clever && *++mvp != NULL) 8545dfc646bSeric { 855588cad61Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8565dfc646bSeric *pvp++ = newstr(buf); 8575dfc646bSeric if (pvp >= &pv[MAXPV]) 85808b25121Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 8595dfc646bSeric } 8605dfc646bSeric *pvp++ = NULL; 8615dfc646bSeric 86225a99e2eSeric /* 86325a99e2eSeric ** Call the mailer. 8646328bdf7Seric ** The argument vector gets built, pipes 86525a99e2eSeric ** are created as necessary, and we fork & exec as 8666328bdf7Seric ** appropriate. 867c579ef51Seric ** If we are running SMTP, we just need to clean up. 86825a99e2eSeric */ 86925a99e2eSeric 8707ee87e5fSeric if (ctladdr == NULL && m != ProgMailer) 87186b26461Seric ctladdr = &e->e_from; 872134746fbSeric #ifdef NAMED_BIND 8732bcc6d2dSeric if (ConfigLevel < 2) 874912a731aSbostic _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 875134746fbSeric #endif 876*9c9e68d9Seric 877*9c9e68d9Seric if (tTd(11, 1)) 878134746fbSeric { 879*9c9e68d9Seric printf("openmailer:"); 880*9c9e68d9Seric printav(pv); 881*9c9e68d9Seric } 882*9c9e68d9Seric errno = 0; 883*9c9e68d9Seric 884*9c9e68d9Seric CurHostName = m->m_mailer; 885*9c9e68d9Seric 886*9c9e68d9Seric /* 887*9c9e68d9Seric ** Deal with the special case of mail handled through an IPC 888*9c9e68d9Seric ** connection. 889*9c9e68d9Seric ** In this case we don't actually fork. We must be 890*9c9e68d9Seric ** running SMTP for this to work. We will return a 891*9c9e68d9Seric ** zero pid to indicate that we are running IPC. 892*9c9e68d9Seric ** We also handle a debug version that just talks to stdin/out. 893*9c9e68d9Seric */ 894*9c9e68d9Seric 895*9c9e68d9Seric curhost = NULL; 896*9c9e68d9Seric 897*9c9e68d9Seric /* check for Local Person Communication -- not for mortals!!! */ 898*9c9e68d9Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 899*9c9e68d9Seric { 900*9c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 901*9c9e68d9Seric bzero((char *) mci, sizeof *mci); 902*9c9e68d9Seric mci->mci_in = stdin; 903*9c9e68d9Seric mci->mci_out = stdout; 904*9c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 905*9c9e68d9Seric mci->mci_mailer = m; 906*9c9e68d9Seric } 907*9c9e68d9Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 908*9c9e68d9Seric strcmp(m->m_mailer, "[TCP]") == 0) 909*9c9e68d9Seric { 910*9c9e68d9Seric #ifdef DAEMON 911*9c9e68d9Seric register int i; 912*9c9e68d9Seric register u_short port; 913*9c9e68d9Seric extern MCI *mci_get(); 914*9c9e68d9Seric extern char *hostsignature(); 915*9c9e68d9Seric 916*9c9e68d9Seric CurHostName = pv[1]; 917*9c9e68d9Seric curhost = hostsignature(m, pv[1], e); 918*9c9e68d9Seric 919*9c9e68d9Seric if (curhost == NULL || curhost[0] == '\0') 920*9c9e68d9Seric { 921*9c9e68d9Seric syserr("null signature"); 922845e533cSeric rcode = EX_OSERR; 923b31e7f2bSeric goto give_up; 924b31e7f2bSeric } 925*9c9e68d9Seric 926*9c9e68d9Seric if (!clever) 927*9c9e68d9Seric { 928*9c9e68d9Seric syserr("554 non-clever IPC"); 929*9c9e68d9Seric rcode = EX_OSERR; 930*9c9e68d9Seric goto give_up; 931*9c9e68d9Seric } 932*9c9e68d9Seric if (pv[2] != NULL) 933*9c9e68d9Seric port = atoi(pv[2]); 934*9c9e68d9Seric else 935*9c9e68d9Seric port = 0; 936*9c9e68d9Seric tryhost: 937*9c9e68d9Seric mci = NULL; 938*9c9e68d9Seric while (*curhost != '\0') 939*9c9e68d9Seric { 940*9c9e68d9Seric register char *p; 941*9c9e68d9Seric static char hostbuf[MAXNAME]; 942*9c9e68d9Seric 943*9c9e68d9Seric mci = NULL; 944*9c9e68d9Seric 945*9c9e68d9Seric /* pull the next host from the signature */ 946*9c9e68d9Seric p = strchr(curhost, ':'); 947*9c9e68d9Seric if (p == NULL) 948*9c9e68d9Seric p = &curhost[strlen(curhost)]; 949*9c9e68d9Seric strncpy(hostbuf, curhost, p - curhost); 950*9c9e68d9Seric hostbuf[p - curhost] = '\0'; 951*9c9e68d9Seric if (*p != '\0') 952*9c9e68d9Seric p++; 953*9c9e68d9Seric curhost = p; 954*9c9e68d9Seric 955*9c9e68d9Seric /* see if we already know that this host is fried */ 956*9c9e68d9Seric CurHostName = hostbuf; 957*9c9e68d9Seric mci = mci_get(hostbuf, m); 958*9c9e68d9Seric if (mci->mci_state != MCIS_CLOSED) 959*9c9e68d9Seric { 960*9c9e68d9Seric if (tTd(11, 1)) 961*9c9e68d9Seric { 962*9c9e68d9Seric printf("openmailer: "); 963*9c9e68d9Seric mci_dump(mci); 964*9c9e68d9Seric } 965*9c9e68d9Seric CurHostName = mci->mci_host; 966*9c9e68d9Seric break; 967*9c9e68d9Seric } 968*9c9e68d9Seric mci->mci_mailer = m; 969*9c9e68d9Seric if (mci->mci_exitstat != EX_OK) 970*9c9e68d9Seric continue; 971*9c9e68d9Seric 972*9c9e68d9Seric /* try the connection */ 973*9c9e68d9Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 974*9c9e68d9Seric message("Connecting to %s (%s)...", 975*9c9e68d9Seric hostbuf, m->m_name); 976*9c9e68d9Seric i = makeconnection(hostbuf, port, mci, 977*9c9e68d9Seric bitnset(M_SECURE_PORT, m->m_flags)); 978*9c9e68d9Seric mci->mci_exitstat = i; 979*9c9e68d9Seric mci->mci_errno = errno; 980*9c9e68d9Seric if (i == EX_OK) 981*9c9e68d9Seric { 982*9c9e68d9Seric mci->mci_state = MCIS_OPENING; 983*9c9e68d9Seric mci_cache(mci); 984*9c9e68d9Seric break; 985*9c9e68d9Seric } 986*9c9e68d9Seric else if (tTd(11, 1)) 987*9c9e68d9Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 988*9c9e68d9Seric i, errno); 989*9c9e68d9Seric 990*9c9e68d9Seric 991*9c9e68d9Seric /* enter status of this host */ 992*9c9e68d9Seric setstat(i); 993*9c9e68d9Seric } 994*9c9e68d9Seric mci->mci_pid = 0; 995*9c9e68d9Seric #else /* no DAEMON */ 996*9c9e68d9Seric syserr("554 openmailer: no IPC"); 997*9c9e68d9Seric if (tTd(11, 1)) 998*9c9e68d9Seric printf("openmailer: NULL\n"); 999*9c9e68d9Seric return NULL; 1000*9c9e68d9Seric #endif /* DAEMON */ 1001*9c9e68d9Seric } 1002*9c9e68d9Seric else 1003*9c9e68d9Seric { 1004*9c9e68d9Seric /* create a pipe to shove the mail through */ 1005*9c9e68d9Seric if (pipe(mpvect) < 0) 1006*9c9e68d9Seric { 1007*9c9e68d9Seric syserr("openmailer: pipe (to mailer)"); 1008*9c9e68d9Seric if (tTd(11, 1)) 1009*9c9e68d9Seric printf("openmailer: NULL\n"); 1010*9c9e68d9Seric rcode = EX_OSERR; 1011*9c9e68d9Seric goto give_up; 1012*9c9e68d9Seric } 1013*9c9e68d9Seric 1014*9c9e68d9Seric /* if this mailer speaks smtp, create a return pipe */ 1015*9c9e68d9Seric if (clever && pipe(rpvect) < 0) 1016*9c9e68d9Seric { 1017*9c9e68d9Seric syserr("openmailer: pipe (from mailer)"); 1018*9c9e68d9Seric (void) close(mpvect[0]); 1019*9c9e68d9Seric (void) close(mpvect[1]); 1020*9c9e68d9Seric if (tTd(11, 1)) 1021*9c9e68d9Seric printf("openmailer: NULL\n"); 1022*9c9e68d9Seric rcode = EX_OSERR; 1023*9c9e68d9Seric goto give_up; 1024*9c9e68d9Seric } 1025*9c9e68d9Seric 1026*9c9e68d9Seric /* 1027*9c9e68d9Seric ** Actually fork the mailer process. 1028*9c9e68d9Seric ** DOFORK is clever about retrying. 1029*9c9e68d9Seric ** 1030*9c9e68d9Seric ** Dispose of SIGCHLD signal catchers that may be laying 1031*9c9e68d9Seric ** around so that endmail will get it. 1032*9c9e68d9Seric */ 1033*9c9e68d9Seric 1034*9c9e68d9Seric if (e->e_xfp != NULL) 1035*9c9e68d9Seric (void) fflush(e->e_xfp); /* for debugging */ 1036*9c9e68d9Seric (void) fflush(stdout); 1037*9c9e68d9Seric # ifdef SIGCHLD 1038*9c9e68d9Seric (void) signal(SIGCHLD, SIG_DFL); 1039*9c9e68d9Seric # endif /* SIGCHLD */ 1040*9c9e68d9Seric DOFORK(FORK); 1041*9c9e68d9Seric /* pid is set by DOFORK */ 1042*9c9e68d9Seric if (pid < 0) 1043*9c9e68d9Seric { 1044*9c9e68d9Seric /* failure */ 1045*9c9e68d9Seric syserr("openmailer: cannot fork"); 1046*9c9e68d9Seric (void) close(mpvect[0]); 1047*9c9e68d9Seric (void) close(mpvect[1]); 1048*9c9e68d9Seric if (clever) 1049*9c9e68d9Seric { 1050*9c9e68d9Seric (void) close(rpvect[0]); 1051*9c9e68d9Seric (void) close(rpvect[1]); 1052*9c9e68d9Seric } 1053*9c9e68d9Seric if (tTd(11, 1)) 1054*9c9e68d9Seric printf("openmailer: NULL\n"); 1055*9c9e68d9Seric rcode = EX_OSERR; 1056*9c9e68d9Seric goto give_up; 1057*9c9e68d9Seric } 1058*9c9e68d9Seric else if (pid == 0) 1059*9c9e68d9Seric { 1060*9c9e68d9Seric int i; 1061*9c9e68d9Seric int saveerrno; 1062*9c9e68d9Seric char **ep; 1063*9c9e68d9Seric char *env[MAXUSERENVIRON]; 1064*9c9e68d9Seric extern char **environ; 1065*9c9e68d9Seric extern int DtableSize; 1066*9c9e68d9Seric 1067*9c9e68d9Seric /* child -- set up input & exec mailer */ 1068*9c9e68d9Seric /* make diagnostic output be standard output */ 1069*9c9e68d9Seric (void) signal(SIGINT, SIG_IGN); 1070*9c9e68d9Seric (void) signal(SIGHUP, SIG_IGN); 1071*9c9e68d9Seric (void) signal(SIGTERM, SIG_DFL); 1072*9c9e68d9Seric 1073*9c9e68d9Seric /* close any other cached connections */ 1074*9c9e68d9Seric mci_flush(FALSE, mci); 1075*9c9e68d9Seric 1076*9c9e68d9Seric /* arrange to filter std & diag output of command */ 1077*9c9e68d9Seric if (clever) 1078*9c9e68d9Seric { 1079*9c9e68d9Seric (void) close(rpvect[0]); 1080*9c9e68d9Seric (void) dup2(rpvect[1], STDOUT_FILENO); 1081*9c9e68d9Seric (void) close(rpvect[1]); 1082*9c9e68d9Seric } 1083*9c9e68d9Seric else if (OpMode == MD_SMTP || HoldErrs) 1084*9c9e68d9Seric { 1085*9c9e68d9Seric /* put mailer output in transcript */ 1086*9c9e68d9Seric (void) dup2(fileno(e->e_xfp), STDOUT_FILENO); 1087*9c9e68d9Seric } 1088*9c9e68d9Seric (void) dup2(STDOUT_FILENO, STDERR_FILENO); 1089*9c9e68d9Seric 1090*9c9e68d9Seric /* arrange to get standard input */ 1091*9c9e68d9Seric (void) close(mpvect[1]); 1092*9c9e68d9Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 1093*9c9e68d9Seric { 1094*9c9e68d9Seric syserr("Cannot dup to zero!"); 1095*9c9e68d9Seric _exit(EX_OSERR); 1096*9c9e68d9Seric } 1097*9c9e68d9Seric (void) close(mpvect[0]); 1098*9c9e68d9Seric if (!bitnset(M_RESTR, m->m_flags)) 1099*9c9e68d9Seric { 1100*9c9e68d9Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 1101*9c9e68d9Seric { 1102*9c9e68d9Seric (void) setgid(DefGid); 1103*9c9e68d9Seric (void) initgroups(DefUser, DefGid); 1104*9c9e68d9Seric (void) setuid(DefUid); 1105*9c9e68d9Seric } 1106*9c9e68d9Seric else 1107*9c9e68d9Seric { 1108*9c9e68d9Seric (void) setgid(ctladdr->q_gid); 1109*9c9e68d9Seric (void) initgroups(ctladdr->q_ruser? 1110*9c9e68d9Seric ctladdr->q_ruser: ctladdr->q_user, 1111*9c9e68d9Seric ctladdr->q_gid); 1112*9c9e68d9Seric (void) setuid(ctladdr->q_uid); 1113*9c9e68d9Seric } 1114*9c9e68d9Seric } 1115*9c9e68d9Seric 1116*9c9e68d9Seric /* arrange for all the files to be closed */ 1117*9c9e68d9Seric for (i = 3; i < DtableSize; i++) 1118*9c9e68d9Seric { 1119*9c9e68d9Seric register int j; 1120*9c9e68d9Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 1121*9c9e68d9Seric (void)fcntl(i, F_SETFD, j|1); 1122*9c9e68d9Seric } 1123*9c9e68d9Seric 1124*9c9e68d9Seric /* set up the mailer environment */ 1125*9c9e68d9Seric i = 0; 1126*9c9e68d9Seric env[i++] = "AGENT=sendmail"; 1127*9c9e68d9Seric for (ep = environ; *ep != NULL; ep++) 1128*9c9e68d9Seric { 1129*9c9e68d9Seric if (strncmp(*ep, "TZ=", 3) == 0) 1130*9c9e68d9Seric env[i++] = *ep; 1131*9c9e68d9Seric } 1132*9c9e68d9Seric env[i++] = NULL; 1133*9c9e68d9Seric 1134*9c9e68d9Seric /* try to execute the mailer */ 1135*9c9e68d9Seric execve(m->m_mailer, pv, env); 1136*9c9e68d9Seric saveerrno = errno; 1137*9c9e68d9Seric syserr("Cannot exec %s", m->m_mailer); 1138*9c9e68d9Seric if (m == LocalMailer) 1139*9c9e68d9Seric _exit(EX_TEMPFAIL); 1140*9c9e68d9Seric if (transienterror(saveerrno)) 1141*9c9e68d9Seric _exit(EX_TEMPFAIL); 1142*9c9e68d9Seric _exit(EX_UNAVAILABLE); 1143*9c9e68d9Seric } 1144*9c9e68d9Seric 1145*9c9e68d9Seric /* 1146*9c9e68d9Seric ** Set up return value. 1147*9c9e68d9Seric */ 1148*9c9e68d9Seric 1149*9c9e68d9Seric mci = (MCI *) xalloc(sizeof *mci); 1150*9c9e68d9Seric bzero((char *) mci, sizeof *mci); 1151*9c9e68d9Seric mci->mci_mailer = m; 1152*9c9e68d9Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 1153*9c9e68d9Seric mci->mci_pid = pid; 1154*9c9e68d9Seric (void) close(mpvect[0]); 1155*9c9e68d9Seric mci->mci_out = fdopen(mpvect[1], "w"); 1156*9c9e68d9Seric if (clever) 1157*9c9e68d9Seric { 1158*9c9e68d9Seric (void) close(rpvect[1]); 1159*9c9e68d9Seric mci->mci_in = fdopen(rpvect[0], "r"); 1160*9c9e68d9Seric } 1161*9c9e68d9Seric else 1162*9c9e68d9Seric { 1163*9c9e68d9Seric mci->mci_flags |= MCIF_TEMP; 1164*9c9e68d9Seric mci->mci_in = NULL; 1165*9c9e68d9Seric } 1166*9c9e68d9Seric } 1167*9c9e68d9Seric 1168*9c9e68d9Seric /* 1169*9c9e68d9Seric ** If we are in SMTP opening state, send initial protocol. 1170*9c9e68d9Seric */ 1171*9c9e68d9Seric 1172*9c9e68d9Seric if (clever && mci->mci_state != MCIS_CLOSED) 1173*9c9e68d9Seric { 1174*9c9e68d9Seric smtpinit(m, mci, e); 1175*9c9e68d9Seric } 1176*9c9e68d9Seric if (tTd(11, 1)) 1177*9c9e68d9Seric { 1178*9c9e68d9Seric printf("openmailer: "); 1179*9c9e68d9Seric mci_dump(mci); 1180*9c9e68d9Seric } 1181*9c9e68d9Seric 1182*9c9e68d9Seric if (mci->mci_state != MCIS_OPEN) 1183b31e7f2bSeric { 1184b31e7f2bSeric /* couldn't open the mailer */ 1185b31e7f2bSeric rcode = mci->mci_exitstat; 11862a6bc25bSeric errno = mci->mci_errno; 1187b31e7f2bSeric if (rcode == EX_OK) 1188b31e7f2bSeric { 1189b31e7f2bSeric /* shouldn't happen */ 119008b25121Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 11916b0f339dSeric rcode, mci->mci_state, firstsig); 1192b31e7f2bSeric rcode = EX_SOFTWARE; 1193b31e7f2bSeric } 1194b31e7f2bSeric } 1195b31e7f2bSeric else if (!clever) 1196b31e7f2bSeric { 1197b31e7f2bSeric /* 1198b31e7f2bSeric ** Format and send message. 1199b31e7f2bSeric */ 120015d084d5Seric 1201b31e7f2bSeric putfromline(mci->mci_out, m, e); 1202b31e7f2bSeric (*e->e_puthdr)(mci->mci_out, m, e); 1203b31e7f2bSeric putline("\n", mci->mci_out, m); 1204b31e7f2bSeric (*e->e_putbody)(mci->mci_out, m, e); 1205b31e7f2bSeric 1206b31e7f2bSeric /* get the exit status */ 1207c9be6216Seric rcode = endmailer(mci, e, pv); 1208134746fbSeric } 1209134746fbSeric else 1210b31e7f2bSeric #ifdef SMTP 1211134746fbSeric { 1212b31e7f2bSeric /* 1213b31e7f2bSeric ** Send the MAIL FROM: protocol 1214b31e7f2bSeric */ 121515d084d5Seric 1216b31e7f2bSeric rcode = smtpmailfrom(m, mci, e); 1217b31e7f2bSeric if (rcode == EX_OK) 121875889e88Seric { 1219ded0d3daSkarels register char *t = tobuf; 1220ded0d3daSkarels register int i; 1221ded0d3daSkarels 1222588cad61Seric /* send the recipient list */ 122363780dbdSeric tobuf[0] = '\0'; 122475889e88Seric for (to = tochain; to != NULL; to = to->q_tchain) 122575889e88Seric { 122663780dbdSeric e->e_to = to->q_paddr; 122715d084d5Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 122875889e88Seric { 122983b7ddc9Seric markfailure(e, to, i); 123081161401Seric giveresponse(i, m, mci, e); 123163780dbdSeric } 123275889e88Seric else 123375889e88Seric { 1234911693bfSbostic *t++ = ','; 1235b31e7f2bSeric for (p = to->q_paddr; *p; *t++ = *p++) 1236b31e7f2bSeric continue; 1237588cad61Seric } 1238588cad61Seric } 1239588cad61Seric 124063780dbdSeric /* now send the data */ 124163780dbdSeric if (tobuf[0] == '\0') 1242b31e7f2bSeric { 1243*9c9e68d9Seric rcode = EX_OK; 124463780dbdSeric e->e_to = NULL; 1245b31e7f2bSeric if (bitset(MCIF_CACHED, mci->mci_flags)) 1246b31e7f2bSeric smtprset(m, mci, e); 1247b31e7f2bSeric } 124875889e88Seric else 124975889e88Seric { 125063780dbdSeric e->e_to = tobuf + 1; 125175889e88Seric rcode = smtpdata(m, mci, e); 125263780dbdSeric } 125363780dbdSeric 125463780dbdSeric /* now close the connection */ 1255b31e7f2bSeric if (!bitset(MCIF_CACHED, mci->mci_flags)) 125615d084d5Seric smtpquit(m, mci, e); 125763780dbdSeric } 1258*9c9e68d9Seric if (rcode != EX_OK && *curhost != '\0') 1259*9c9e68d9Seric { 1260*9c9e68d9Seric /* try next MX site */ 1261*9c9e68d9Seric goto tryhost; 1262*9c9e68d9Seric } 1263c579ef51Seric } 1264b31e7f2bSeric #else /* not SMTP */ 1265a05b3449Sbostic { 126608b25121Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 1267845e533cSeric rcode = EX_CONFIG; 1268b31e7f2bSeric goto give_up; 1269a05b3449Sbostic } 1270b31e7f2bSeric #endif /* SMTP */ 1271134746fbSeric #ifdef NAMED_BIND 12722bcc6d2dSeric if (ConfigLevel < 2) 1273912a731aSbostic _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 1274134746fbSeric #endif 12755dfc646bSeric 1276b31e7f2bSeric /* arrange a return receipt if requested */ 12778c13bf07Seric if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags)) 1278b31e7f2bSeric { 1279b31e7f2bSeric e->e_flags |= EF_SENDRECEIPT; 1280b31e7f2bSeric /* do we want to send back more info? */ 1281b31e7f2bSeric } 1282b31e7f2bSeric 1283c77d1c25Seric /* 128463780dbdSeric ** Do final status disposal. 128563780dbdSeric ** We check for something in tobuf for the SMTP case. 1286c77d1c25Seric ** If we got a temporary failure, arrange to queue the 1287c77d1c25Seric ** addressees. 1288c77d1c25Seric */ 1289c77d1c25Seric 1290b31e7f2bSeric give_up: 129163780dbdSeric if (tobuf[0] != '\0') 129281161401Seric giveresponse(rcode, m, mci, e); 1293772e6e50Seric for (to = tochain; to != NULL; to = to->q_tchain) 1294b31e7f2bSeric { 1295dde5acadSeric if (rcode != EX_OK) 129683b7ddc9Seric markfailure(e, to, rcode); 1297dde5acadSeric else 1298655518ecSeric { 1299dde5acadSeric to->q_flags |= QSENT; 1300655518ecSeric e->e_nsent++; 1301655518ecSeric } 1302b31e7f2bSeric } 1303b31e7f2bSeric 1304b31e7f2bSeric /* 1305b31e7f2bSeric ** Restore state and return. 1306b31e7f2bSeric */ 1307c77d1c25Seric 130835490626Seric errno = 0; 1309588cad61Seric define('g', (char *) NULL, e); 13105826d9d3Seric return (rcode); 131125a99e2eSeric } 13125dfc646bSeric /* 131383b7ddc9Seric ** MARKFAILURE -- mark a failure on a specific address. 131483b7ddc9Seric ** 131583b7ddc9Seric ** Parameters: 131683b7ddc9Seric ** e -- the envelope we are sending. 131783b7ddc9Seric ** q -- the address to mark. 131883b7ddc9Seric ** rcode -- the code signifying the particular failure. 131983b7ddc9Seric ** 132083b7ddc9Seric ** Returns: 132183b7ddc9Seric ** none. 132283b7ddc9Seric ** 132383b7ddc9Seric ** Side Effects: 132483b7ddc9Seric ** marks the address (and possibly the envelope) with the 132583b7ddc9Seric ** failure so that an error will be returned or 132683b7ddc9Seric ** the message will be queued, as appropriate. 132783b7ddc9Seric */ 132883b7ddc9Seric 132983b7ddc9Seric markfailure(e, q, rcode) 133083b7ddc9Seric register ENVELOPE *e; 133183b7ddc9Seric register ADDRESS *q; 133283b7ddc9Seric int rcode; 133383b7ddc9Seric { 133419c47125Seric char buf[MAXLINE]; 133519c47125Seric extern char *pintvl(); 133619c47125Seric 133783b7ddc9Seric if (rcode == EX_OK) 133883b7ddc9Seric return; 1339ef137175Sbostic else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR) 134083b7ddc9Seric q->q_flags |= QBADADDR; 134119c47125Seric else if (curtime() > e->e_ctime + TimeOuts.to_q_return) 134283b7ddc9Seric { 134383b7ddc9Seric if (!bitset(EF_TIMEOUT, e->e_flags)) 1344198d9be0Seric { 1345198d9be0Seric (void) sprintf(buf, "Cannot send message for %s", 134619c47125Seric pintvl(TimeOuts.to_q_return, FALSE)); 1347198d9be0Seric if (e->e_message != NULL) 1348198d9be0Seric free(e->e_message); 1349198d9be0Seric e->e_message = newstr(buf); 135008b25121Seric message(buf); 1351198d9be0Seric } 135283b7ddc9Seric q->q_flags |= QBADADDR; 135383b7ddc9Seric e->e_flags |= EF_TIMEOUT; 1354c13b5dfcSeric fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr); 135583b7ddc9Seric } 135683b7ddc9Seric else 135719c47125Seric { 135883b7ddc9Seric q->q_flags |= QQUEUEUP; 135919c47125Seric if (TimeOuts.to_q_warning > 0 && 136019c47125Seric curtime() > e->e_ctime + TimeOuts.to_q_warning) 136119c47125Seric { 136219c47125Seric if (!bitset(EF_WARNING, e->e_flags) && 136319c47125Seric e->e_class >= 0) 136419c47125Seric { 136519c47125Seric (void) sprintf(buf, 136619c47125Seric "warning: cannot send message for %s", 136719c47125Seric pintvl(TimeOuts.to_q_warning, FALSE)); 136819c47125Seric if (e->e_message != NULL) 136919c47125Seric free(e->e_message); 137019c47125Seric e->e_message = newstr(buf); 137119c47125Seric message(buf); 137219c47125Seric e->e_flags |= EF_WARNING|EF_TIMEOUT; 137319c47125Seric } 137419c47125Seric fprintf(e->e_xfp, 137519c47125Seric "%s... Warning: message still undelivered after %s\n", 137619c47125Seric q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE)); 137719c47125Seric fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 137819c47125Seric pintvl(TimeOuts.to_q_return, FALSE)); 137919c47125Seric } 138019c47125Seric } 138183b7ddc9Seric } 138283b7ddc9Seric /* 1383c579ef51Seric ** ENDMAILER -- Wait for mailer to terminate. 1384c579ef51Seric ** 1385c579ef51Seric ** We should never get fatal errors (e.g., segmentation 1386c579ef51Seric ** violation), so we report those specially. For other 1387c579ef51Seric ** errors, we choose a status message (into statmsg), 1388c579ef51Seric ** and if it represents an error, we print it. 1389c579ef51Seric ** 1390c579ef51Seric ** Parameters: 1391c579ef51Seric ** pid -- pid of mailer. 1392c9be6216Seric ** e -- the current envelope. 1393c9be6216Seric ** pv -- the parameter vector that invoked the mailer 1394c9be6216Seric ** (for error messages). 1395c579ef51Seric ** 1396c579ef51Seric ** Returns: 1397c579ef51Seric ** exit code of mailer. 1398c579ef51Seric ** 1399c579ef51Seric ** Side Effects: 1400c579ef51Seric ** none. 1401c579ef51Seric */ 1402c579ef51Seric 1403c9be6216Seric endmailer(mci, e, pv) 1404b31e7f2bSeric register MCI *mci; 1405c9be6216Seric register ENVELOPE *e; 1406c9be6216Seric char **pv; 1407c579ef51Seric { 1408588cad61Seric int st; 1409c579ef51Seric 141075889e88Seric /* close any connections */ 141175889e88Seric if (mci->mci_in != NULL) 1412c9be6216Seric (void) xfclose(mci->mci_in, pv[0], "mci_in"); 141375889e88Seric if (mci->mci_out != NULL) 1414c9be6216Seric (void) xfclose(mci->mci_out, pv[0], "mci_out"); 141575889e88Seric mci->mci_in = mci->mci_out = NULL; 141675889e88Seric mci->mci_state = MCIS_CLOSED; 141775889e88Seric 141833db8731Seric /* in the IPC case there is nothing to wait for */ 141975889e88Seric if (mci->mci_pid == 0) 142033db8731Seric return (EX_OK); 142133db8731Seric 142233db8731Seric /* wait for the mailer process to die and collect status */ 142375889e88Seric st = waitfor(mci->mci_pid); 1424588cad61Seric if (st == -1) 142578de67c1Seric { 1426c9be6216Seric syserr("endmailer %s: wait", pv[0]); 1427588cad61Seric return (EX_SOFTWARE); 1428c579ef51Seric } 142933db8731Seric 143033db8731Seric /* see if it died a horrid death */ 1431c579ef51Seric if ((st & 0377) != 0) 1432c579ef51Seric { 1433c9be6216Seric syserr("mailer %s died with signal %o", pv[0], st); 1434c9be6216Seric 1435c9be6216Seric /* log the arguments */ 1436c9be6216Seric if (e->e_xfp != NULL) 1437c9be6216Seric { 1438c9be6216Seric register char **av; 1439c9be6216Seric 1440c9be6216Seric fprintf(e->e_xfp, "Arguments:"); 1441c9be6216Seric for (av = pv; *av != NULL; av++) 1442c9be6216Seric fprintf(e->e_xfp, " %s", *av); 1443c9be6216Seric fprintf(e->e_xfp, "\n"); 1444c9be6216Seric } 1445c9be6216Seric 14465f73204aSeric ExitStat = EX_TEMPFAIL; 14475f73204aSeric return (EX_TEMPFAIL); 1448c579ef51Seric } 144933db8731Seric 145033db8731Seric /* normal death -- return status */ 1451588cad61Seric st = (st >> 8) & 0377; 1452588cad61Seric return (st); 1453c579ef51Seric } 1454c579ef51Seric /* 145525a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 145625a99e2eSeric ** 145725a99e2eSeric ** Parameters: 145825a99e2eSeric ** stat -- the status code from the mailer (high byte 145925a99e2eSeric ** only; core dumps must have been taken care of 146025a99e2eSeric ** already). 146181161401Seric ** m -- the mailer info for this mailer. 146281161401Seric ** mci -- the mailer connection info -- can be NULL if the 146381161401Seric ** response is given before the connection is made. 146481161401Seric ** e -- the current envelope. 146525a99e2eSeric ** 146625a99e2eSeric ** Returns: 1467db8841e9Seric ** none. 146825a99e2eSeric ** 146925a99e2eSeric ** Side Effects: 1470c1f9df2cSeric ** Errors may be incremented. 147125a99e2eSeric ** ExitStat may be set. 147225a99e2eSeric */ 147325a99e2eSeric 147481161401Seric giveresponse(stat, m, mci, e) 147525a99e2eSeric int stat; 1476588cad61Seric register MAILER *m; 147781161401Seric register MCI *mci; 1478198d9be0Seric ENVELOPE *e; 147925a99e2eSeric { 148025a99e2eSeric register char *statmsg; 148125a99e2eSeric extern char *SysExMsg[]; 148225a99e2eSeric register int i; 1483d4bd8f0eSbostic extern int N_SysEx; 1484d4bd8f0eSbostic #ifdef NAMED_BIND 1485d4bd8f0eSbostic extern int h_errno; 1486d4bd8f0eSbostic #endif 1487198d9be0Seric char buf[MAXLINE]; 14887d55540cSeric extern char *errstring(); 148925a99e2eSeric 149013bbc08cSeric /* 149113bbc08cSeric ** Compute status message from code. 149213bbc08cSeric */ 149313bbc08cSeric 149425a99e2eSeric i = stat - EX__BASE; 1495588cad61Seric if (stat == 0) 1496588cad61Seric statmsg = "250 Sent"; 1497588cad61Seric else if (i < 0 || i > N_SysEx) 1498588cad61Seric { 1499588cad61Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 1500588cad61Seric stat = EX_UNAVAILABLE; 1501588cad61Seric statmsg = buf; 1502588cad61Seric } 1503198d9be0Seric else if (stat == EX_TEMPFAIL) 1504198d9be0Seric { 15057d55540cSeric (void) strcpy(buf, SysExMsg[i] + 1); 1506d4bd8f0eSbostic #ifdef NAMED_BIND 1507f28da541Smiriam if (h_errno == TRY_AGAIN) 1508f28da541Smiriam statmsg = errstring(h_errno+MAX_ERRNO); 1509f28da541Smiriam else 1510d4bd8f0eSbostic #endif 1511f28da541Smiriam { 15128557d168Seric if (errno != 0) 1513d87e85f3Seric statmsg = errstring(errno); 1514d87e85f3Seric else 1515d87e85f3Seric { 1516d87e85f3Seric #ifdef SMTP 1517d87e85f3Seric extern char SmtpError[]; 1518d87e85f3Seric 1519d87e85f3Seric statmsg = SmtpError; 15206c2c3107Seric #else /* SMTP */ 1521d87e85f3Seric statmsg = NULL; 15226c2c3107Seric #endif /* SMTP */ 1523d87e85f3Seric } 1524f28da541Smiriam } 1525d87e85f3Seric if (statmsg != NULL && statmsg[0] != '\0') 1526d87e85f3Seric { 152787c9b3e7Seric (void) strcat(buf, ": "); 1528d87e85f3Seric (void) strcat(buf, statmsg); 15298557d168Seric } 1530198d9be0Seric statmsg = buf; 1531198d9be0Seric } 153225a99e2eSeric else 1533d87e85f3Seric { 153425a99e2eSeric statmsg = SysExMsg[i]; 15357d55540cSeric if (*statmsg++ == ':') 15367d55540cSeric { 15377d55540cSeric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 15387d55540cSeric statmsg = buf; 15397d55540cSeric } 1540d87e85f3Seric } 1541588cad61Seric 1542588cad61Seric /* 1543588cad61Seric ** Print the message as appropriate 1544588cad61Seric */ 1545588cad61Seric 1546198d9be0Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 1547e12d03eeSeric message(&statmsg[4], errstring(errno)); 154825a99e2eSeric else 154925a99e2eSeric { 1550c1f9df2cSeric Errors++; 1551e12d03eeSeric usrerr(statmsg, errstring(errno)); 155225a99e2eSeric } 155325a99e2eSeric 155425a99e2eSeric /* 155525a99e2eSeric ** Final cleanup. 155625a99e2eSeric ** Log a record of the transaction. Compute the new 155725a99e2eSeric ** ExitStat -- if we already had an error, stick with 155825a99e2eSeric ** that. 155925a99e2eSeric */ 156025a99e2eSeric 15612f624c86Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 156281161401Seric logdelivery(m, mci, &statmsg[4], e); 1563eb238f8cSeric 1564eb238f8cSeric if (stat != EX_TEMPFAIL) 1565eb238f8cSeric setstat(stat); 1566198d9be0Seric if (stat != EX_OK) 1567198d9be0Seric { 1568198d9be0Seric if (e->e_message != NULL) 1569198d9be0Seric free(e->e_message); 1570198d9be0Seric e->e_message = newstr(&statmsg[4]); 1571198d9be0Seric } 15728557d168Seric errno = 0; 1573d4bd8f0eSbostic #ifdef NAMED_BIND 1574f28da541Smiriam h_errno = 0; 1575d4bd8f0eSbostic #endif 1576eb238f8cSeric } 1577eb238f8cSeric /* 1578eb238f8cSeric ** LOGDELIVERY -- log the delivery in the system log 1579eb238f8cSeric ** 1580eb238f8cSeric ** Parameters: 158181161401Seric ** m -- the mailer info. Can be NULL for initial queue. 158281161401Seric ** mci -- the mailer connection info -- can be NULL if the 158381161401Seric ** log is occuring when no connection is active. 158481161401Seric ** stat -- the message to print for the status. 158581161401Seric ** e -- the current envelope. 1586eb238f8cSeric ** 1587eb238f8cSeric ** Returns: 1588eb238f8cSeric ** none 1589eb238f8cSeric ** 1590eb238f8cSeric ** Side Effects: 1591eb238f8cSeric ** none 1592eb238f8cSeric */ 1593eb238f8cSeric 159481161401Seric logdelivery(m, mci, stat, e) 159581161401Seric MAILER *m; 159681161401Seric register MCI *mci; 1597eb238f8cSeric char *stat; 1598b31e7f2bSeric register ENVELOPE *e; 15995cf56be3Seric { 1600eb238f8cSeric # ifdef LOG 16019507d1f9Seric char *curhost; 1602d6acf3eeSeric char buf[512]; 16039507d1f9Seric extern char *pintvl(); 16049507d1f9Seric extern char *macvalue(); 16059507d1f9Seric 160648ed5d33Seric (void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 160781161401Seric 160848ed5d33Seric if (m != NULL) 160971ff6caaSeric { 161048ed5d33Seric (void) strcat(buf, ", mailer="); 161148ed5d33Seric (void) strcat(buf, m->m_name); 161271ff6caaSeric } 161348ed5d33Seric 161448ed5d33Seric if (mci != NULL && mci->mci_host != NULL) 161571ff6caaSeric { 161671ff6caaSeric # ifdef DAEMON 1617e2f2f828Seric extern SOCKADDR CurHostAddr; 1618e2f2f828Seric extern char *anynet_ntoa(); 161948ed5d33Seric # endif 162071ff6caaSeric 162148ed5d33Seric (void) strcat(buf, ", relay="); 162248ed5d33Seric (void) strcat(buf, mci->mci_host); 162348ed5d33Seric 162448ed5d33Seric # ifdef DAEMON 162548ed5d33Seric (void) strcat(buf, " ("); 1626e2f2f828Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 162748ed5d33Seric (void) strcat(buf, ")"); 162871ff6caaSeric # endif 162971ff6caaSeric } 16309507d1f9Seric else 163148ed5d33Seric { 163248ed5d33Seric char *p = macvalue('h', e); 16339507d1f9Seric 163448ed5d33Seric if (p != NULL && p[0] != '\0') 163548ed5d33Seric { 163648ed5d33Seric (void) strcat(buf, ", relay="); 163748ed5d33Seric (void) strcat(buf, p); 163848ed5d33Seric } 163948ed5d33Seric } 1640d6acf3eeSeric 1641d6acf3eeSeric syslog(LOG_INFO, "%s: to=%s, %s, stat=%s", 1642d6acf3eeSeric e->e_id, e->e_to, buf, stat); 16436c2c3107Seric # endif /* LOG */ 164425a99e2eSeric } 164525a99e2eSeric /* 164651552439Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 164725a99e2eSeric ** 164851552439Seric ** This can be made an arbitrary message separator by changing $l 164951552439Seric ** 16509b6c17a6Seric ** One of the ugliest hacks seen by human eyes is contained herein: 16519b6c17a6Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 16529b6c17a6Seric ** does a well-meaning programmer such as myself have to deal with 16539b6c17a6Seric ** this kind of antique garbage???? 165425a99e2eSeric ** 165525a99e2eSeric ** Parameters: 165651552439Seric ** fp -- the file to output to. 165751552439Seric ** m -- the mailer describing this entry. 165825a99e2eSeric ** 165925a99e2eSeric ** Returns: 166051552439Seric ** none 166125a99e2eSeric ** 166225a99e2eSeric ** Side Effects: 166351552439Seric ** outputs some text to fp. 166425a99e2eSeric */ 166525a99e2eSeric 1666b31e7f2bSeric putfromline(fp, m, e) 166751552439Seric register FILE *fp; 166851552439Seric register MAILER *m; 1669b31e7f2bSeric ENVELOPE *e; 167025a99e2eSeric { 16712bc47524Seric char *template = "\201l\n"; 167251552439Seric char buf[MAXLINE]; 167325a99e2eSeric 167457fc6f17Seric if (bitnset(M_NHDR, m->m_flags)) 167551552439Seric return; 167613bbc08cSeric 16772c7e1b8dSeric # ifdef UGLYUUCP 167857fc6f17Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 167974b6e67bSeric { 1680ea09d6edSeric char *bang; 1681ea09d6edSeric char xbuf[MAXLINE]; 168274b6e67bSeric 1683ee4b0922Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 16846c2c3107Seric bang = strchr(buf, '!'); 168574b6e67bSeric if (bang == NULL) 168608b25121Seric syserr("554 No ! in UUCP! (%s)", buf); 168774b6e67bSeric else 1688588cad61Seric { 1689ea09d6edSeric *bang++ = '\0'; 16902bc47524Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 1691ea09d6edSeric template = xbuf; 169274b6e67bSeric } 1693588cad61Seric } 16946c2c3107Seric # endif /* UGLYUUCP */ 1695b31e7f2bSeric expand(template, buf, &buf[sizeof buf - 1], e); 169677b52738Seric putline(buf, fp, m); 1697bc6e2962Seric } 1698bc6e2962Seric /* 169951552439Seric ** PUTBODY -- put the body of a message. 170051552439Seric ** 170151552439Seric ** Parameters: 170251552439Seric ** fp -- file to output onto. 170377b52738Seric ** m -- a mailer descriptor to control output format. 17049a6a5f55Seric ** e -- the envelope to put out. 170551552439Seric ** 170651552439Seric ** Returns: 170751552439Seric ** none. 170851552439Seric ** 170951552439Seric ** Side Effects: 171051552439Seric ** The message is written onto fp. 171151552439Seric */ 171251552439Seric 171377b52738Seric putbody(fp, m, e) 171451552439Seric FILE *fp; 1715588cad61Seric MAILER *m; 17169a6a5f55Seric register ENVELOPE *e; 171751552439Seric { 171877b52738Seric char buf[MAXLINE]; 171951552439Seric 172051552439Seric /* 172151552439Seric ** Output the body of the message 172251552439Seric */ 172351552439Seric 17249a6a5f55Seric if (e->e_dfp == NULL) 172551552439Seric { 17269a6a5f55Seric if (e->e_df != NULL) 17279a6a5f55Seric { 17289a6a5f55Seric e->e_dfp = fopen(e->e_df, "r"); 17299a6a5f55Seric if (e->e_dfp == NULL) 17308f9146b0Srick syserr("putbody: Cannot open %s for %s from %s", 17318f9146b0Srick e->e_df, e->e_to, e->e_from); 17329a6a5f55Seric } 17339a6a5f55Seric else 173477b52738Seric putline("<<< No Message Collected >>>", fp, m); 17359a6a5f55Seric } 17369a6a5f55Seric if (e->e_dfp != NULL) 17379a6a5f55Seric { 17389a6a5f55Seric rewind(e->e_dfp); 173977b52738Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 174024fc8aeeSeric { 174124fc8aeeSeric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 1742d6fa2b58Sbostic strncmp(buf, "From ", 5) == 0) 17433462ad9eSeric (void) putc('>', fp); 174477b52738Seric putline(buf, fp, m); 174524fc8aeeSeric } 174651552439Seric 17479a6a5f55Seric if (ferror(e->e_dfp)) 174851552439Seric { 174951552439Seric syserr("putbody: read error"); 175051552439Seric ExitStat = EX_IOERR; 175151552439Seric } 175251552439Seric } 175351552439Seric 175451552439Seric (void) fflush(fp); 175551552439Seric if (ferror(fp) && errno != EPIPE) 175651552439Seric { 175751552439Seric syserr("putbody: write error"); 175851552439Seric ExitStat = EX_IOERR; 175951552439Seric } 176051552439Seric errno = 0; 176125a99e2eSeric } 176225a99e2eSeric /* 176325a99e2eSeric ** MAILFILE -- Send a message to a file. 176425a99e2eSeric ** 1765f129ec7dSeric ** If the file has the setuid/setgid bits set, but NO execute 1766f129ec7dSeric ** bits, sendmail will try to become the owner of that file 1767f129ec7dSeric ** rather than the real user. Obviously, this only works if 1768f129ec7dSeric ** sendmail runs as root. 1769f129ec7dSeric ** 1770588cad61Seric ** This could be done as a subordinate mailer, except that it 1771588cad61Seric ** is used implicitly to save messages in ~/dead.letter. We 1772588cad61Seric ** view this as being sufficiently important as to include it 1773588cad61Seric ** here. For example, if the system is dying, we shouldn't have 1774588cad61Seric ** to create another process plus some pipes to save the message. 1775588cad61Seric ** 177625a99e2eSeric ** Parameters: 177725a99e2eSeric ** filename -- the name of the file to send to. 17786259796dSeric ** ctladdr -- the controlling address header -- includes 17796259796dSeric ** the userid/groupid to be when sending. 178025a99e2eSeric ** 178125a99e2eSeric ** Returns: 178225a99e2eSeric ** The exit code associated with the operation. 178325a99e2eSeric ** 178425a99e2eSeric ** Side Effects: 178525a99e2eSeric ** none. 178625a99e2eSeric */ 178725a99e2eSeric 1788b31e7f2bSeric mailfile(filename, ctladdr, e) 178925a99e2eSeric char *filename; 17906259796dSeric ADDRESS *ctladdr; 1791b31e7f2bSeric register ENVELOPE *e; 179225a99e2eSeric { 179325a99e2eSeric register FILE *f; 179432d19d43Seric register int pid; 179515d084d5Seric int mode; 179625a99e2eSeric 179732d19d43Seric /* 179832d19d43Seric ** Fork so we can change permissions here. 179932d19d43Seric ** Note that we MUST use fork, not vfork, because of 180032d19d43Seric ** the complications of calling subroutines, etc. 180132d19d43Seric */ 180232d19d43Seric 180332d19d43Seric DOFORK(fork); 180432d19d43Seric 180532d19d43Seric if (pid < 0) 180632d19d43Seric return (EX_OSERR); 180732d19d43Seric else if (pid == 0) 180832d19d43Seric { 180932d19d43Seric /* child -- actually write to file */ 1810f129ec7dSeric struct stat stb; 1811f129ec7dSeric 18120984da9fSeric (void) signal(SIGINT, SIG_DFL); 18130984da9fSeric (void) signal(SIGHUP, SIG_DFL); 18140984da9fSeric (void) signal(SIGTERM, SIG_DFL); 18153462ad9eSeric (void) umask(OldUmask); 181695f16dc0Seric 1817f129ec7dSeric if (stat(filename, &stb) < 0) 1818e6e1265fSeric stb.st_mode = 0666; 181915d084d5Seric mode = stb.st_mode; 182095f16dc0Seric 182195f16dc0Seric /* limit the errors to those actually caused in the child */ 182295f16dc0Seric errno = 0; 182395f16dc0Seric ExitStat = EX_OK; 182495f16dc0Seric 1825f129ec7dSeric if (bitset(0111, stb.st_mode)) 1826f129ec7dSeric exit(EX_CANTCREAT); 182703827b5fSeric if (ctladdr == NULL) 18288f9146b0Srick ctladdr = &e->e_from; 182915d084d5Seric else 183015d084d5Seric { 183115d084d5Seric /* ignore setuid and setgid bits */ 183215d084d5Seric mode &= ~(S_ISGID|S_ISUID); 183315d084d5Seric } 183415d084d5Seric 18358f9146b0Srick /* we have to open the dfile BEFORE setuid */ 18368f9146b0Srick if (e->e_dfp == NULL && e->e_df != NULL) 18378f9146b0Srick { 18388f9146b0Srick e->e_dfp = fopen(e->e_df, "r"); 183995f16dc0Seric if (e->e_dfp == NULL) 184095f16dc0Seric { 18418f9146b0Srick syserr("mailfile: Cannot open %s for %s from %s", 18428f9146b0Srick e->e_df, e->e_to, e->e_from); 18438f9146b0Srick } 18448f9146b0Srick } 18458f9146b0Srick 184615d084d5Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 1847e36b99e2Seric { 184895f16dc0Seric if (ctladdr->q_uid == 0) 184995f16dc0Seric { 1850e36b99e2Seric (void) setgid(DefGid); 1851898a126bSbostic (void) initgroups(DefUser, DefGid); 185295f16dc0Seric } 185395f16dc0Seric else 185495f16dc0Seric { 18556259796dSeric (void) setgid(ctladdr->q_gid); 1856898a126bSbostic (void) initgroups(ctladdr->q_ruser ? 1857898a126bSbostic ctladdr->q_ruser : ctladdr->q_user, 1858898a126bSbostic ctladdr->q_gid); 1859898a126bSbostic } 1860e36b99e2Seric } 186115d084d5Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 1862e36b99e2Seric { 1863e36b99e2Seric if (ctladdr->q_uid == 0) 1864e36b99e2Seric (void) setuid(DefUid); 1865e36b99e2Seric else 18666259796dSeric (void) setuid(ctladdr->q_uid); 1867e36b99e2Seric } 186895f16dc0Seric FileName = filename; 186995f16dc0Seric LineNumber = 0; 187027628d59Seric f = dfopen(filename, "a"); 187125a99e2eSeric if (f == NULL) 187295f16dc0Seric { 187308b25121Seric message("554 cannot open"); 187432d19d43Seric exit(EX_CANTCREAT); 187595f16dc0Seric } 187625a99e2eSeric 1877b31e7f2bSeric putfromline(f, ProgMailer, e); 1878b843d759Seric (*e->e_puthdr)(f, ProgMailer, e); 187977b52738Seric putline("\n", f, ProgMailer); 1880b843d759Seric (*e->e_putbody)(f, ProgMailer, e); 188177b52738Seric putline("\n", f, ProgMailer); 188295f16dc0Seric if (ferror(f)) 188395f16dc0Seric { 188408b25121Seric message("451 I/O error"); 188595f16dc0Seric setstat(EX_IOERR); 188695f16dc0Seric } 1887ee4b0922Seric (void) xfclose(f, "mailfile", filename); 188832d19d43Seric (void) fflush(stdout); 1889e36b99e2Seric 189027628d59Seric /* reset ISUID & ISGID bits for paranoid systems */ 1891c77d1c25Seric (void) chmod(filename, (int) stb.st_mode); 189295f16dc0Seric exit(ExitStat); 189313bbc08cSeric /*NOTREACHED*/ 189432d19d43Seric } 189532d19d43Seric else 189632d19d43Seric { 189732d19d43Seric /* parent -- wait for exit status */ 1898588cad61Seric int st; 189932d19d43Seric 1900588cad61Seric st = waitfor(pid); 1901588cad61Seric if ((st & 0377) != 0) 1902588cad61Seric return (EX_UNAVAILABLE); 1903588cad61Seric else 1904588cad61Seric return ((st >> 8) & 0377); 19058f9146b0Srick /*NOTREACHED*/ 190632d19d43Seric } 190725a99e2eSeric } 1908ea4dc939Seric /* 1909e103b48fSeric ** HOSTSIGNATURE -- return the "signature" for a host. 1910e103b48fSeric ** 1911e103b48fSeric ** The signature describes how we are going to send this -- it 1912e103b48fSeric ** can be just the hostname (for non-Internet hosts) or can be 1913e103b48fSeric ** an ordered list of MX hosts. 1914e103b48fSeric ** 1915e103b48fSeric ** Parameters: 1916e103b48fSeric ** m -- the mailer describing this host. 1917e103b48fSeric ** host -- the host name. 1918e103b48fSeric ** e -- the current envelope. 1919e103b48fSeric ** 1920e103b48fSeric ** Returns: 1921e103b48fSeric ** The signature for this host. 1922e103b48fSeric ** 1923e103b48fSeric ** Side Effects: 1924e103b48fSeric ** Can tweak the symbol table. 1925e103b48fSeric */ 1926e103b48fSeric 1927e103b48fSeric char * 1928e103b48fSeric hostsignature(m, host, e) 1929e103b48fSeric register MAILER *m; 1930e103b48fSeric char *host; 1931e103b48fSeric ENVELOPE *e; 1932e103b48fSeric { 1933e103b48fSeric register char *p; 1934e103b48fSeric register STAB *s; 1935e103b48fSeric int i; 1936e103b48fSeric int len; 1937e103b48fSeric #ifdef NAMED_BIND 1938e103b48fSeric int nmx; 1939e103b48fSeric auto int rcode; 1940e103b48fSeric char *mxhosts[MAXMXHOSTS + 1]; 1941e103b48fSeric static char myhostbuf[MAXNAME]; 1942e103b48fSeric #endif 1943e103b48fSeric 1944e103b48fSeric /* 1945e103b48fSeric ** Check to see if this uses IPC -- if not, it can't have MX records. 1946e103b48fSeric */ 1947e103b48fSeric 1948e103b48fSeric p = m->m_mailer; 1949e103b48fSeric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 1950e103b48fSeric { 1951e103b48fSeric /* just an ordinary mailer */ 1952e103b48fSeric return host; 1953e103b48fSeric } 1954e103b48fSeric 1955e103b48fSeric /* 1956e103b48fSeric ** If it is a numeric address, just return it. 1957e103b48fSeric */ 1958e103b48fSeric 1959e103b48fSeric if (host[0] == '[') 1960e103b48fSeric return host; 1961e103b48fSeric 1962e103b48fSeric /* 1963e103b48fSeric ** Look it up in the symbol table. 1964e103b48fSeric */ 1965e103b48fSeric 1966e103b48fSeric s = stab(host, ST_HOSTSIG, ST_ENTER); 1967e103b48fSeric if (s->s_hostsig != NULL) 1968e103b48fSeric return s->s_hostsig; 1969e103b48fSeric 1970e103b48fSeric /* 1971e103b48fSeric ** Not already there -- create a signature. 1972e103b48fSeric */ 1973e103b48fSeric 1974e103b48fSeric #ifdef NAMED_BIND 1975e103b48fSeric if (myhostbuf[0] == '\0') 19762bc47524Seric expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e); 1977e103b48fSeric 1978e103b48fSeric nmx = getmxrr(host, mxhosts, myhostbuf, &rcode); 19797d55540cSeric 1980e103b48fSeric if (nmx <= 0) 1981e103b48fSeric { 1982e103b48fSeric register MCI *mci; 1983e103b48fSeric extern int errno; 1984e103b48fSeric extern MCI *mci_get(); 1985e103b48fSeric 1986e103b48fSeric /* update the connection info for this host */ 1987e103b48fSeric mci = mci_get(host, m); 1988e103b48fSeric mci->mci_exitstat = rcode; 1989e103b48fSeric mci->mci_errno = errno; 1990e103b48fSeric 1991e103b48fSeric /* and return the original host name as the signature */ 1992e103b48fSeric s->s_hostsig = host; 1993e103b48fSeric return host; 1994e103b48fSeric } 1995e103b48fSeric 1996e103b48fSeric len = 0; 1997e103b48fSeric for (i = 0; i < nmx; i++) 1998e103b48fSeric { 1999e103b48fSeric len += strlen(mxhosts[i]) + 1; 2000e103b48fSeric } 2001e103b48fSeric s->s_hostsig = p = xalloc(len); 2002e103b48fSeric for (i = 0; i < nmx; i++) 2003e103b48fSeric { 2004e103b48fSeric if (i != 0) 2005e103b48fSeric *p++ = ':'; 2006e103b48fSeric strcpy(p, mxhosts[i]); 2007e103b48fSeric p += strlen(p); 2008e103b48fSeric } 2009e103b48fSeric makelower(s->s_hostsig); 2010e103b48fSeric #else 2011e103b48fSeric /* not using BIND -- the signature is just the host name */ 2012e103b48fSeric s->s_hostsig = host; 2013e103b48fSeric #endif 2014e103b48fSeric if (tTd(17, 1)) 2015e103b48fSeric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 2016e103b48fSeric return s->s_hostsig; 2017e103b48fSeric } 2018