125a99e2eSeric # include <stdio.h> 225a99e2eSeric # include <pwd.h> 325a99e2eSeric # include <signal.h> 46328bdf7Seric # include <ctype.h> 554aa2b0fSeric # include <errno.h> 6b20b3270Seric # include "sendmail.h" 725a99e2eSeric # ifdef LOG 8e374fd72Seric # include <syslog.h> 925a99e2eSeric # endif LOG 1025a99e2eSeric 11*3efaed6eSeric static char SccsId[] = "@(#)deliver.c 3.22 08/18/81"; 12259cace7Seric 1325a99e2eSeric /* 1425a99e2eSeric ** DELIVER -- Deliver a message to a particular address. 1525a99e2eSeric ** 1625a99e2eSeric ** Parameters: 1725a99e2eSeric ** to -- the address to deliver the message to. 1825a99e2eSeric ** editfcn -- if non-NULL, we want to call this function 1925a99e2eSeric ** to output the letter (instead of just out- 2025a99e2eSeric ** putting it raw). 2125a99e2eSeric ** 2225a99e2eSeric ** Returns: 2325a99e2eSeric ** zero -- successfully delivered. 2425a99e2eSeric ** else -- some failure, see ExitStat for more info. 2525a99e2eSeric ** 2625a99e2eSeric ** Side Effects: 2725a99e2eSeric ** The standard input is passed off to someone. 2825a99e2eSeric */ 2925a99e2eSeric 3025a99e2eSeric deliver(to, editfcn) 312a6e0786Seric ADDRESS *to; 3225a99e2eSeric int (*editfcn)(); 3325a99e2eSeric { 3425a99e2eSeric char *host; 3525a99e2eSeric char *user; 3625a99e2eSeric char **pvp; 375dfc646bSeric register char **mvp; 3825a99e2eSeric register char *p; 395dfc646bSeric register struct mailer *m; 405dfc646bSeric register int i; 416328bdf7Seric extern putmessage(); 422a6e0786Seric extern bool checkcompat(); 435dfc646bSeric char *pv[MAXPV+1]; 445dfc646bSeric char tobuf[MAXLINE]; 455dfc646bSeric char buf[MAXNAME]; 465dfc646bSeric bool firstone; 4725a99e2eSeric 485dfc646bSeric if (bitset(QDONTSEND, to->q_flags)) 495dfc646bSeric return (0); 5025a99e2eSeric 5125a99e2eSeric # ifdef DEBUG 5225a99e2eSeric if (Debug) 535dfc646bSeric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 545dfc646bSeric to->q_mailer, to->q_host, to->q_user); 5525a99e2eSeric # endif DEBUG 5625a99e2eSeric 5725a99e2eSeric /* 585dfc646bSeric ** Do initial argv setup. 595dfc646bSeric ** Insert the mailer name. Notice that $x expansion is 605dfc646bSeric ** NOT done on the mailer name. Then, if the mailer has 615dfc646bSeric ** a picky -f flag, we insert it as appropriate. This 625dfc646bSeric ** code does not check for 'pv' overflow; this places a 635dfc646bSeric ** manifest lower limit of 4 for MAXPV. 645dfc646bSeric */ 655dfc646bSeric 665dfc646bSeric m = Mailer[to->q_mailer]; 675dfc646bSeric host = to->q_host; 685dfc646bSeric define('g', m->m_from); /* translated from address */ 695dfc646bSeric define('h', host); /* to host */ 705dfc646bSeric Errors = 0; 715dfc646bSeric errno = 0; 725dfc646bSeric pvp = pv; 735dfc646bSeric *pvp++ = m->m_argv[0]; 745dfc646bSeric 755dfc646bSeric /* insert -f or -r flag as appropriate */ 765dfc646bSeric if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag) 775dfc646bSeric { 785dfc646bSeric if (bitset(M_FOPT, m->m_flags)) 795dfc646bSeric *pvp++ = "-f"; 805dfc646bSeric else 815dfc646bSeric *pvp++ = "-r"; 82db8841e9Seric (void) expand("$g", buf, &buf[sizeof buf - 1]); 835dfc646bSeric *pvp++ = newstr(buf); 845dfc646bSeric } 855dfc646bSeric 865dfc646bSeric /* 875dfc646bSeric ** Append the other fixed parts of the argv. These run 885dfc646bSeric ** up to the first entry containing "$u". There can only 895dfc646bSeric ** be one of these, and there are only a few more slots 905dfc646bSeric ** in the pv after it. 915dfc646bSeric */ 925dfc646bSeric 935dfc646bSeric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 945dfc646bSeric { 955dfc646bSeric while ((p = index(p, '$')) != NULL) 965dfc646bSeric if (*++p == 'u') 975dfc646bSeric break; 985dfc646bSeric if (p != NULL) 995dfc646bSeric break; 1005dfc646bSeric 1015dfc646bSeric /* this entry is safe -- go ahead and process it */ 102db8841e9Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 1035dfc646bSeric *pvp++ = newstr(buf); 1045dfc646bSeric if (pvp >= &pv[MAXPV - 3]) 1055dfc646bSeric { 1065dfc646bSeric syserr("Too many parameters to %s before $u", pv[0]); 1075dfc646bSeric return (-1); 1085dfc646bSeric } 1095dfc646bSeric } 1105dfc646bSeric if (*mvp == NULL) 1115dfc646bSeric syserr("No $u in mailer argv for %s", pv[0]); 1125dfc646bSeric 1135dfc646bSeric /* 1145dfc646bSeric ** At this point *mvp points to the argument with $u. We 1155dfc646bSeric ** run through our address list and append all the addresses 1165dfc646bSeric ** we can. If we run out of space, do not fret! We can 1175dfc646bSeric ** always send another copy later. 1185dfc646bSeric */ 1195dfc646bSeric 1205dfc646bSeric tobuf[0] = '\0'; 1215dfc646bSeric firstone = TRUE; 1225dfc646bSeric To = tobuf; 1235dfc646bSeric for (; to != NULL; to = to->q_next) 1245dfc646bSeric { 1255dfc646bSeric /* avoid sending multiple recipients to dumb mailers */ 1265dfc646bSeric if (!firstone && !bitset(M_MUSER, m->m_flags)) 1275dfc646bSeric break; 1285dfc646bSeric 1295dfc646bSeric /* if already sent or not for this host, don't send */ 1305dfc646bSeric if (bitset(QDONTSEND, to->q_flags) || strcmp(to->q_host, host) != 0) 1315dfc646bSeric continue; 1325dfc646bSeric user = to->q_user; 1335dfc646bSeric To = to->q_paddr; 1345dfc646bSeric to->q_flags |= QDONTSEND; 1355dfc646bSeric firstone = FALSE; 1365dfc646bSeric 1375dfc646bSeric # ifdef DEBUG 1385dfc646bSeric if (Debug) 1395dfc646bSeric printf(" send to `%s'\n", user); 1405dfc646bSeric # endif DEBUG 1415dfc646bSeric 1425dfc646bSeric /* 1435dfc646bSeric ** Check to see that these people are allowed to 1445dfc646bSeric ** talk to each other. 1452a6e0786Seric */ 1462a6e0786Seric 1472a6e0786Seric if (!checkcompat(to)) 1485dfc646bSeric { 1495dfc646bSeric giveresponse(EX_UNAVAILABLE, TRUE, m); 1505dfc646bSeric continue; 1515dfc646bSeric } 1522a6e0786Seric 1532a6e0786Seric /* 1549ec9501bSeric ** Strip quote bits from names if the mailer is dumb 1559ec9501bSeric ** about them. 15625a99e2eSeric */ 15725a99e2eSeric 1582a6e0786Seric if (bitset(M_STRIPQ, m->m_flags)) 15925a99e2eSeric { 1609ec9501bSeric stripquotes(user, TRUE); 1619ec9501bSeric stripquotes(host, TRUE); 1629ec9501bSeric } 1639ec9501bSeric else 1649ec9501bSeric { 1659ec9501bSeric stripquotes(user, FALSE); 1669ec9501bSeric stripquotes(host, FALSE); 16725a99e2eSeric } 16825a99e2eSeric 16925a99e2eSeric /* 170*3efaed6eSeric ** If an error message has already been given, don't 171*3efaed6eSeric ** bother to send to this address. 172*3efaed6eSeric ** 173*3efaed6eSeric ** >>>>>>>>>> This clause assumes that the local mailer 174*3efaed6eSeric ** >> NOTE >> cannot do any further aliasing; that 175*3efaed6eSeric ** >>>>>>>>>> function is subsumed by sendmail. 176*3efaed6eSeric */ 177*3efaed6eSeric 178*3efaed6eSeric if (bitset(QBADADDR, to->q_flags)) 179*3efaed6eSeric continue; 180*3efaed6eSeric 181*3efaed6eSeric /* 18225a99e2eSeric ** See if this user name is "special". 18325a99e2eSeric ** If the user name has a slash in it, assume that this 18425a99e2eSeric ** is a file -- send it off without further ado. 18525a99e2eSeric ** Note that this means that editfcn's will not 1865dfc646bSeric ** be applied to the message. Also note that 1875dfc646bSeric ** this type of addresses is not processed along 1885dfc646bSeric ** with the others, so we fudge on the To person. 18925a99e2eSeric */ 19025a99e2eSeric 191c2567733Seric if (m == Mailer[M_LOCAL]) 19225a99e2eSeric { 19325a99e2eSeric if (index(user, '/') != NULL) 19425a99e2eSeric { 19525a99e2eSeric i = mailfile(user); 19625a99e2eSeric giveresponse(i, TRUE, m); 1975dfc646bSeric continue; 19825a99e2eSeric } 19925a99e2eSeric } 20025a99e2eSeric 2015dfc646bSeric /* create list of users for error messages */ 2025dfc646bSeric if (tobuf[0] != '\0') 203db8841e9Seric (void) strcat(tobuf, ","); 204db8841e9Seric (void) strcat(tobuf, to->q_paddr); 2055dfc646bSeric define('u', user); /* to user */ 206c2567733Seric define('z', to->q_home); /* user's home */ 2075dfc646bSeric 2085dfc646bSeric /* expand out this user */ 209db8841e9Seric (void) expand(user, buf, &buf[sizeof buf - 1]); 2105dfc646bSeric *pvp++ = newstr(buf); 2115dfc646bSeric if (pvp >= &pv[MAXPV - 2]) 2125dfc646bSeric { 2135dfc646bSeric /* allow some space for trailing parms */ 2145dfc646bSeric break; 2155dfc646bSeric } 2165dfc646bSeric } 2175dfc646bSeric 218145b49b1Seric /* see if any addresses still exist */ 219145b49b1Seric if (tobuf[0] == '\0') 220145b49b1Seric return (0); 221145b49b1Seric 2225dfc646bSeric /* print out messages as full list */ 2235dfc646bSeric To = tobuf; 2245dfc646bSeric 2255dfc646bSeric /* 2265dfc646bSeric ** Fill out any parameters after the $u parameter. 2275dfc646bSeric */ 2285dfc646bSeric 2295dfc646bSeric while (*++mvp != NULL) 2305dfc646bSeric { 231db8841e9Seric (void) expand(*mvp, buf, &buf[sizeof buf - 1]); 2325dfc646bSeric *pvp++ = newstr(buf); 2335dfc646bSeric if (pvp >= &pv[MAXPV]) 2345dfc646bSeric syserr("deliver: pv overflow after $u for %s", pv[0]); 2355dfc646bSeric } 2365dfc646bSeric *pvp++ = NULL; 2375dfc646bSeric 23825a99e2eSeric /* 23925a99e2eSeric ** Call the mailer. 2406328bdf7Seric ** The argument vector gets built, pipes 24125a99e2eSeric ** are created as necessary, and we fork & exec as 2426328bdf7Seric ** appropriate. 243c2567733Seric ** 244c2567733Seric ** Notice the tacky hack to handle private mailers. 24525a99e2eSeric */ 24625a99e2eSeric 2475dfc646bSeric if (editfcn == NULL) 2485dfc646bSeric editfcn = putmessage; 2495dfc646bSeric i = sendoff(m, pv, editfcn); 2505dfc646bSeric 2515dfc646bSeric return (i); 25225a99e2eSeric } 2535dfc646bSeric /* 2545dfc646bSeric ** SENDOFF -- send off call to mailer & collect response. 2555dfc646bSeric ** 2565dfc646bSeric ** Parameters: 2575dfc646bSeric ** m -- mailer descriptor. 2585dfc646bSeric ** pvp -- parameter vector to send to it. 2595dfc646bSeric ** editfcn -- function to pipe it through. 2605dfc646bSeric ** 2615dfc646bSeric ** Returns: 2625dfc646bSeric ** exit status of mailer. 2635dfc646bSeric ** 2645dfc646bSeric ** Side Effects: 2655dfc646bSeric ** none. 2665dfc646bSeric */ 2675dfc646bSeric 268750c6fe8Seric #define NFORKTRIES 5 269750c6fe8Seric 2705dfc646bSeric sendoff(m, pvp, editfcn) 2715dfc646bSeric struct mailer *m; 2725dfc646bSeric char **pvp; 2735dfc646bSeric int (*editfcn)(); 2745dfc646bSeric { 2755dfc646bSeric auto int st; 2765dfc646bSeric register int i; 2775dfc646bSeric int pid; 2785dfc646bSeric int pvect[2]; 2795dfc646bSeric FILE *mfile; 2805dfc646bSeric extern putmessage(); 2815dfc646bSeric extern FILE *fdopen(); 2825dfc646bSeric 2835dfc646bSeric # ifdef DEBUG 2845dfc646bSeric if (Debug) 2855dfc646bSeric { 2865dfc646bSeric printf("Sendoff:\n"); 2875dfc646bSeric printav(pvp); 2885dfc646bSeric } 2895dfc646bSeric # endif DEBUG 2905dfc646bSeric 2916328bdf7Seric /* create a pipe to shove the mail through */ 2926328bdf7Seric if (pipe(pvect) < 0) 29325a99e2eSeric { 29425a99e2eSeric syserr("pipe"); 29525a99e2eSeric return (-1); 29625a99e2eSeric } 297750c6fe8Seric for (i = NFORKTRIES; i-- > 0; ) 298750c6fe8Seric { 299ea235328Smark # ifdef VFORK 300ea235328Smark pid = vfork(); 301ea235328Smark # else 30225a99e2eSeric pid = fork(); 303ea235328Smark # endif 304750c6fe8Seric if (pid >= 0) 305750c6fe8Seric break; 306db8841e9Seric sleep((unsigned) NFORKTRIES - i); 307750c6fe8Seric } 30825a99e2eSeric if (pid < 0) 30925a99e2eSeric { 31025a99e2eSeric syserr("Cannot fork"); 311db8841e9Seric (void) close(pvect[0]); 312db8841e9Seric (void) close(pvect[1]); 31325a99e2eSeric return (-1); 31425a99e2eSeric } 31525a99e2eSeric else if (pid == 0) 31625a99e2eSeric { 31725a99e2eSeric /* child -- set up input & exec mailer */ 31803ab8e55Seric /* make diagnostic output be standard output */ 319db8841e9Seric (void) close(2); 320db8841e9Seric (void) dup(1); 321db8841e9Seric (void) signal(SIGINT, SIG_IGN); 322db8841e9Seric (void) close(0); 32325a99e2eSeric if (dup(pvect[0]) < 0) 32425a99e2eSeric { 32525a99e2eSeric syserr("Cannot dup to zero!"); 326a590b978Seric _exit(EX_OSERR); 32725a99e2eSeric } 328db8841e9Seric (void) close(pvect[0]); 329db8841e9Seric (void) close(pvect[1]); 3302a6e0786Seric if (!bitset(M_RESTR, m->m_flags)) 331db8841e9Seric (void) setuid(getuid()); 332e374fd72Seric # ifndef VFORK 333e374fd72Seric /* 334e374fd72Seric ** We have to be careful with vfork - we can't mung up the 335e374fd72Seric ** memory but we don't want the mailer to inherit any extra 336e374fd72Seric ** open files. Chances are the mailer won't 337e374fd72Seric ** care about an extra file, but then again you never know. 338e374fd72Seric ** Actually, we would like to close(fileno(pwf)), but it's 339e374fd72Seric ** declared static so we can't. But if we fclose(pwf), which 340e374fd72Seric ** is what endpwent does, it closes it in the parent too and 341e374fd72Seric ** the next getpwnam will be slower. If you have a weird 342e374fd72Seric ** mailer that chokes on the extra file you should do the 343e374fd72Seric ** endpwent(). 344e374fd72Seric ** 345e374fd72Seric ** Similar comments apply to log. However, openlog is 346e374fd72Seric ** clever enough to set the FIOCLEX mode on the file, 347e374fd72Seric ** so it will be closed automatically on the exec. 348e374fd72Seric */ 349e374fd72Seric 350e374fd72Seric endpwent(); 35125a99e2eSeric # ifdef LOG 352f9fe028fSeric closelog(); 35325a99e2eSeric # endif LOG 354e374fd72Seric # endif VFORK 35525a99e2eSeric execv(m->m_mailer, pvp); 35625a99e2eSeric /* syserr fails because log is closed */ 35725a99e2eSeric /* syserr("Cannot exec %s", m->m_mailer); */ 3582ac087dbSeric printf("Cannot exec %s\n", m->m_mailer); 359db8841e9Seric (void) fflush(stdout); 360a590b978Seric _exit(EX_UNAVAILABLE); 36125a99e2eSeric } 36225a99e2eSeric 3636328bdf7Seric /* write out message to mailer */ 364db8841e9Seric (void) close(pvect[0]); 36554aa2b0fSeric (void) signal(SIGPIPE, SIG_IGN); 36625a99e2eSeric mfile = fdopen(pvect[1], "w"); 3676328bdf7Seric if (editfcn == NULL) 3686328bdf7Seric editfcn = putmessage; 3696328bdf7Seric (*editfcn)(mfile, m); 370db8841e9Seric (void) fclose(mfile); 37125a99e2eSeric 37225a99e2eSeric /* 37325a99e2eSeric ** Wait for child to die and report status. 37425a99e2eSeric ** We should never get fatal errors (e.g., segmentation 37525a99e2eSeric ** violation), so we report those specially. For other 37625a99e2eSeric ** errors, we choose a status message (into statmsg), 37725a99e2eSeric ** and if it represents an error, we print it. 37825a99e2eSeric */ 37925a99e2eSeric 38025a99e2eSeric while ((i = wait(&st)) > 0 && i != pid) 38125a99e2eSeric continue; 38225a99e2eSeric if (i < 0) 38325a99e2eSeric { 38425a99e2eSeric syserr("wait"); 38525a99e2eSeric return (-1); 38625a99e2eSeric } 38725a99e2eSeric if ((st & 0377) != 0) 38825a99e2eSeric { 38925a99e2eSeric syserr("%s: stat %o", pvp[0], st); 390df2792f7Seric ExitStat = EX_UNAVAILABLE; 39125a99e2eSeric return (-1); 39225a99e2eSeric } 39325a99e2eSeric i = (st >> 8) & 0377; 3942ac087dbSeric giveresponse(i, TRUE, m); 39525a99e2eSeric return (i); 39625a99e2eSeric } 39725a99e2eSeric /* 39825a99e2eSeric ** GIVERESPONSE -- Interpret an error response from a mailer 39925a99e2eSeric ** 40025a99e2eSeric ** Parameters: 40125a99e2eSeric ** stat -- the status code from the mailer (high byte 40225a99e2eSeric ** only; core dumps must have been taken care of 40325a99e2eSeric ** already). 40425a99e2eSeric ** force -- if set, force an error message output, even 40525a99e2eSeric ** if the mailer seems to like to print its own 40625a99e2eSeric ** messages. 40725a99e2eSeric ** m -- the mailer descriptor for this mailer. 40825a99e2eSeric ** 40925a99e2eSeric ** Returns: 410db8841e9Seric ** none. 41125a99e2eSeric ** 41225a99e2eSeric ** Side Effects: 413c1f9df2cSeric ** Errors may be incremented. 41425a99e2eSeric ** ExitStat may be set. 41525a99e2eSeric ** 41625a99e2eSeric ** Called By: 41725a99e2eSeric ** deliver 41825a99e2eSeric */ 41925a99e2eSeric 42025a99e2eSeric giveresponse(stat, force, m) 42125a99e2eSeric int stat; 42225a99e2eSeric int force; 42325a99e2eSeric register struct mailer *m; 42425a99e2eSeric { 42525a99e2eSeric register char *statmsg; 42625a99e2eSeric extern char *SysExMsg[]; 42725a99e2eSeric register int i; 42825a99e2eSeric extern int N_SysEx; 42929dd97a3Seric extern long MsgSize; 43029dd97a3Seric char buf[30]; 43125a99e2eSeric 43225a99e2eSeric i = stat - EX__BASE; 43325a99e2eSeric if (i < 0 || i > N_SysEx) 43425a99e2eSeric statmsg = NULL; 43525a99e2eSeric else 43625a99e2eSeric statmsg = SysExMsg[i]; 43725a99e2eSeric if (stat == 0) 438c38ba59cSeric { 439*3efaed6eSeric if (bitset(M_FINAL, m->m_flags)) 440*3efaed6eSeric statmsg = "delivered"; 441*3efaed6eSeric else 442*3efaed6eSeric statmsg = "queued"; 443c38ba59cSeric if (Verbose) 444*3efaed6eSeric message("050", statmsg); 445c38ba59cSeric } 44625a99e2eSeric else 44725a99e2eSeric { 448c1f9df2cSeric Errors++; 44925a99e2eSeric if (statmsg == NULL && m->m_badstat != 0) 45025a99e2eSeric { 45125a99e2eSeric stat = m->m_badstat; 45225a99e2eSeric i = stat - EX__BASE; 45325a99e2eSeric # ifdef DEBUG 45425a99e2eSeric if (i < 0 || i >= N_SysEx) 45525a99e2eSeric syserr("Bad m_badstat %d", stat); 45625a99e2eSeric else 45725a99e2eSeric # endif DEBUG 45825a99e2eSeric statmsg = SysExMsg[i]; 45925a99e2eSeric } 46025a99e2eSeric if (statmsg == NULL) 46125a99e2eSeric usrerr("unknown mailer response %d", stat); 462c38ba59cSeric else if (force || !bitset(M_QUIET, m->m_flags) || Verbose) 46325a99e2eSeric usrerr("%s", statmsg); 46425a99e2eSeric } 46525a99e2eSeric 46625a99e2eSeric /* 46725a99e2eSeric ** Final cleanup. 46825a99e2eSeric ** Log a record of the transaction. Compute the new 46925a99e2eSeric ** ExitStat -- if we already had an error, stick with 47025a99e2eSeric ** that. 47125a99e2eSeric */ 47225a99e2eSeric 47325a99e2eSeric if (statmsg == NULL) 47429dd97a3Seric { 475db8841e9Seric (void) sprintf(buf, "error %d", stat); 47629dd97a3Seric statmsg = buf; 47729dd97a3Seric } 47829dd97a3Seric 47929dd97a3Seric # ifdef LOG 480e374fd72Seric syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg); 48125a99e2eSeric # endif LOG 482243921eeSeric setstat(stat); 48325a99e2eSeric } 48425a99e2eSeric /* 4856328bdf7Seric ** PUTMESSAGE -- output a message to the final mailer. 48625a99e2eSeric ** 4876328bdf7Seric ** This routine takes care of recreating the header from the 4886328bdf7Seric ** in-core copy, etc. 48925a99e2eSeric ** 49025a99e2eSeric ** Parameters: 4916328bdf7Seric ** fp -- file to output onto. 4926328bdf7Seric ** m -- a mailer descriptor. 49325a99e2eSeric ** 49425a99e2eSeric ** Returns: 4956328bdf7Seric ** none. 49625a99e2eSeric ** 49725a99e2eSeric ** Side Effects: 4986328bdf7Seric ** The message is written onto fp. 49925a99e2eSeric */ 50025a99e2eSeric 5016328bdf7Seric putmessage(fp, m) 5026328bdf7Seric FILE *fp; 5036328bdf7Seric struct mailer *m; 50425a99e2eSeric { 5056328bdf7Seric char buf[BUFSIZ]; 5066328bdf7Seric register int i; 5076328bdf7Seric HDR *h; 508b07ac16bSeric register char *p; 5096328bdf7Seric extern char *arpadate(); 5106328bdf7Seric bool anyheader = FALSE; 511e9ff65b0Seric extern char *capitalize(); 51225a99e2eSeric 51340e4ab56Seric /* output "From" line unless supressed */ 51440e4ab56Seric if (!bitset(M_NHDR, m->m_flags)) 51540e4ab56Seric fprintf(fp, "%s\n", FromLine); 51640e4ab56Seric 5174ae18d0eSeric /* output all header lines */ 5186328bdf7Seric for (h = Header; h != NULL; h = h->h_link) 5196328bdf7Seric { 5209e9163a0Seric if (bitset(H_DELETE, h->h_flags)) 5219e9163a0Seric continue; 5229e9163a0Seric if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags)) 5236328bdf7Seric continue; 5244ae18d0eSeric if (bitset(H_DEFAULT, h->h_flags)) 5254ae18d0eSeric { 526db8841e9Seric (void) expand(h->h_value, buf, &buf[sizeof buf]); 5274ae18d0eSeric p = buf; 5284ae18d0eSeric } 5294ae18d0eSeric else 5304ae18d0eSeric p = h->h_value; 5319e9163a0Seric if (*p == '\0') 5329e9163a0Seric continue; 5334ae18d0eSeric fprintf(fp, "%s: %s\n", capitalize(h->h_field), p); 5346328bdf7Seric h->h_flags |= H_USED; 5356328bdf7Seric anyheader = TRUE; 5366328bdf7Seric } 5376328bdf7Seric 5386328bdf7Seric if (anyheader) 5396328bdf7Seric fprintf(fp, "\n"); 5406328bdf7Seric 5416328bdf7Seric /* output the body of the message */ 54285751117Seric rewind(stdin); 54385751117Seric while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, stdin)) > 0) 544db8841e9Seric (void) fwrite(buf, 1, i, fp); 5456328bdf7Seric 54654aa2b0fSeric if (ferror(fp) && errno != EPIPE) 54725a99e2eSeric { 5486328bdf7Seric syserr("putmessage: write error"); 54925a99e2eSeric setstat(EX_IOERR); 55025a99e2eSeric } 55154aa2b0fSeric errno = 0; 55225a99e2eSeric } 55325a99e2eSeric /* 55425a99e2eSeric ** SENDTO -- Designate a send list. 55525a99e2eSeric ** 55625a99e2eSeric ** The parameter is a comma-separated list of people to send to. 55725a99e2eSeric ** This routine arranges to send to all of them. 55825a99e2eSeric ** 55925a99e2eSeric ** Parameters: 56025a99e2eSeric ** list -- the send list. 56125a99e2eSeric ** copyf -- the copy flag; passed to parse. 56225a99e2eSeric ** 56325a99e2eSeric ** Returns: 56425a99e2eSeric ** none 56525a99e2eSeric ** 56625a99e2eSeric ** Side Effects: 56725a99e2eSeric ** none. 56825a99e2eSeric */ 56925a99e2eSeric 5709ec9501bSeric # define MAXRCRSN 10 5719ec9501bSeric 57225a99e2eSeric sendto(list, copyf) 57325a99e2eSeric char *list; 57425a99e2eSeric int copyf; 57525a99e2eSeric { 57625a99e2eSeric register char *p; 57725a99e2eSeric register char *q; 57825a99e2eSeric register char c; 5792a6e0786Seric ADDRESS *a; 58025a99e2eSeric bool more; 58125a99e2eSeric 58225a99e2eSeric /* more keeps track of what the previous delimiter was */ 58325a99e2eSeric more = TRUE; 58425a99e2eSeric for (p = list; more; ) 58525a99e2eSeric { 58625a99e2eSeric /* find the end of this address */ 5874ec53a16Seric while (*p == ' ' || *p == '\t') 5884ec53a16Seric p++; 58925a99e2eSeric q = p; 59025a99e2eSeric while ((c = *p++) != '\0' && c != ',' && c != '\n') 59125a99e2eSeric continue; 59225a99e2eSeric more = c != '\0'; 59325a99e2eSeric *--p = '\0'; 59425a99e2eSeric if (more) 59525a99e2eSeric p++; 59625a99e2eSeric 59725a99e2eSeric /* parse the address */ 5982a6e0786Seric if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL) 59925a99e2eSeric continue; 60025a99e2eSeric 60125a99e2eSeric /* arrange to send to this person */ 60240e4ab56Seric recipient(a); 60325a99e2eSeric } 60425a99e2eSeric To = NULL; 60525a99e2eSeric } 60625a99e2eSeric /* 60725a99e2eSeric ** RECIPIENT -- Designate a message recipient 60825a99e2eSeric ** 60925a99e2eSeric ** Saves the named person for future mailing. 61025a99e2eSeric ** 61125a99e2eSeric ** Parameters: 61225a99e2eSeric ** a -- the (preparsed) address header for the recipient. 61325a99e2eSeric ** 61425a99e2eSeric ** Returns: 61525a99e2eSeric ** none. 61625a99e2eSeric ** 61725a99e2eSeric ** Side Effects: 61825a99e2eSeric ** none. 61925a99e2eSeric */ 62025a99e2eSeric 62140e4ab56Seric recipient(a) 6222a6e0786Seric register ADDRESS *a; 62325a99e2eSeric { 6242a6e0786Seric register ADDRESS *q; 62525a99e2eSeric register struct mailer *m; 6269ec9501bSeric char buf[MAXNAME]; 62725a99e2eSeric 62825a99e2eSeric To = a->q_paddr; 62986f2b423Seric m = Mailer[a->q_mailer]; 63025a99e2eSeric errno = 0; 63125a99e2eSeric # ifdef DEBUG 63225a99e2eSeric if (Debug) 63325a99e2eSeric printf("recipient(%s)\n", To); 63425a99e2eSeric # endif DEBUG 63525a99e2eSeric 6369ec9501bSeric /* break aliasing loops */ 6379ec9501bSeric if (AliasLevel > MAXRCRSN) 6389ec9501bSeric { 6399ec9501bSeric usrerr("aliasing/forwarding loop broken"); 6409ec9501bSeric return; 6419ec9501bSeric } 6429ec9501bSeric 64325a99e2eSeric /* 64440e4ab56Seric ** Do sickly crude mapping for program mailing, etc. 64525a99e2eSeric */ 64625a99e2eSeric 647c2567733Seric if (a->q_mailer == M_LOCAL) 64825a99e2eSeric { 649c2567733Seric if (a->q_user[0] == '|') 650c2567733Seric { 651c2567733Seric a->q_mailer = M_PROG; 652c2567733Seric m = Mailer[M_PROG]; 65340e4ab56Seric a->q_user++; 65425a99e2eSeric } 655c2567733Seric } 65640e4ab56Seric 65740e4ab56Seric /* 65840e4ab56Seric ** Look up this person in the recipient list. If they 65940e4ab56Seric ** are there already, return, otherwise continue. 66040e4ab56Seric ** If the list is empty, just add it. 66140e4ab56Seric */ 66240e4ab56Seric 66340e4ab56Seric if (m->m_sendq == NULL) 66440e4ab56Seric { 66540e4ab56Seric m->m_sendq = a; 66640e4ab56Seric } 66740e4ab56Seric else 66840e4ab56Seric { 66940e4ab56Seric ADDRESS *pq; 67040e4ab56Seric 67140e4ab56Seric for (q = m->m_sendq; q != NULL; pq = q, q = q->q_next) 67240e4ab56Seric { 67340e4ab56Seric if (!ForceMail && sameaddr(q, a, FALSE)) 67425a99e2eSeric { 67525a99e2eSeric # ifdef DEBUG 67625a99e2eSeric if (Debug) 67740e4ab56Seric printf("(%s in sendq)\n", a->q_paddr); 67825a99e2eSeric # endif DEBUG 679145b49b1Seric if (Verbose && !bitset(QDONTSEND, a->q_flags)) 680c38ba59cSeric message("050", "duplicate supressed"); 68125a99e2eSeric return; 68225a99e2eSeric } 68325a99e2eSeric } 68425a99e2eSeric 68540e4ab56Seric /* add address on list */ 68640e4ab56Seric q = pq; 6875dfc646bSeric q->q_next = a; 68840e4ab56Seric } 68940e4ab56Seric a->q_next = NULL; 69040e4ab56Seric 69125a99e2eSeric /* 6924ec53a16Seric ** Alias the name and handle :include: specs. 69325a99e2eSeric */ 69425a99e2eSeric 6959ec9501bSeric if (a->q_mailer == M_LOCAL) 6964ec53a16Seric { 6974ec53a16Seric if (strncmp(a->q_user, ":include:", 9) == 0) 6984ec53a16Seric { 6994ec53a16Seric a->q_flags |= QDONTSEND; 7004ec53a16Seric include(&a->q_user[9]); 7014ec53a16Seric } 7024ec53a16Seric else 7039ec9501bSeric alias(a); 7044ec53a16Seric } 7059ec9501bSeric 7069ec9501bSeric /* 7079ec9501bSeric ** If the user is local and still being sent, verify that 7089ec9501bSeric ** the address is good. If it is, try to forward. 7099ec9501bSeric ** If the address is already good, we have a forwarding 7109ec9501bSeric ** loop. This can be broken by just sending directly to 7119ec9501bSeric ** the user (which is probably correct anyway). 7129ec9501bSeric */ 7139ec9501bSeric 714*3efaed6eSeric if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == M_LOCAL) 7159ec9501bSeric { 7169ec9501bSeric char buf[MAXNAME]; 7179ec9501bSeric 7189ec9501bSeric strcpy(buf, a->q_user); 7199ec9501bSeric stripquotes(buf, TRUE); 720*3efaed6eSeric 721*3efaed6eSeric /* see if this is to a file */ 722*3efaed6eSeric if (index(buf, '/') != NULL) 723*3efaed6eSeric { 724*3efaed6eSeric if (access(buf, 2) < 0) 725*3efaed6eSeric { 726*3efaed6eSeric a->q_flags |= QBADADDR; 727*3efaed6eSeric giveresponse(EX_CANTCREAT, TRUE, m); 728*3efaed6eSeric } 729*3efaed6eSeric } 730*3efaed6eSeric else 731*3efaed6eSeric { 732*3efaed6eSeric register struct passwd *pw; 733*3efaed6eSeric extern struct passwd *getpwnam(); 7349ec9501bSeric pw = getpwnam(buf); 7359ec9501bSeric if (pw == NULL) 736*3efaed6eSeric { 7379ec9501bSeric a->q_flags |= QBADADDR; 738*3efaed6eSeric giveresponse(EX_NOUSER, TRUE, m); 739*3efaed6eSeric } 7409ec9501bSeric else 7410b148c78Seric { 7429ec9501bSeric a->q_home = newstr(pw->pw_dir); 7439ec9501bSeric if (strcmp(buf, a->q_user) == 0) 7449ec9501bSeric forward(a); 7459ec9501bSeric } 7460b148c78Seric } 7474ec53a16Seric } 748*3efaed6eSeric } 7494ec53a16Seric /* 7504ec53a16Seric ** INCLUDE -- handle :include: specification. 7514ec53a16Seric ** 7524ec53a16Seric ** Parameters: 7534ec53a16Seric ** fname -- filename to include. 7544ec53a16Seric ** 7554ec53a16Seric ** Returns: 7564ec53a16Seric ** none. 7574ec53a16Seric ** 7584ec53a16Seric ** Side Effects: 7594ec53a16Seric ** reads the :include: file and sends to everyone 7604ec53a16Seric ** listed in that file. 7614ec53a16Seric */ 76225a99e2eSeric 7634ec53a16Seric include(fname) 7644ec53a16Seric char *fname; 7654ec53a16Seric { 7664ec53a16Seric char buf[MAXLINE]; 7674ec53a16Seric register FILE *fp; 7684ec53a16Seric 7694ec53a16Seric if (Verbose) 7704ec53a16Seric message("050", "Including file %s", fname); 7714ec53a16Seric fp = fopen(fname, "r"); 7724ec53a16Seric if (fp == NULL) 7734ec53a16Seric { 7744ec53a16Seric usrerr("Cannot open %s", fname); 77525a99e2eSeric return; 77625a99e2eSeric } 7774ec53a16Seric 7784ec53a16Seric /* read the file -- each line is a comma-separated list. */ 7794ec53a16Seric while (fgets(buf, sizeof buf, fp) != NULL) 7804ec53a16Seric { 7814ec53a16Seric register char *p = index(buf, '\n'); 7824ec53a16Seric 7834ec53a16Seric if (p != NULL) 7844ec53a16Seric *p = '\0'; 7854ec53a16Seric if (buf[0] == '\0') 7864ec53a16Seric continue; 7874ec53a16Seric To = fname; 7884ec53a16Seric if (Verbose) 7894ec53a16Seric message("050", " >> %s", buf); 7904ec53a16Seric sendto(buf, 1); 7914ec53a16Seric } 7924ec53a16Seric 7934ec53a16Seric fclose(fp); 7944ec53a16Seric } 79525a99e2eSeric /* 79625a99e2eSeric ** MAILFILE -- Send a message to a file. 79725a99e2eSeric ** 79825a99e2eSeric ** Parameters: 79925a99e2eSeric ** filename -- the name of the file to send to. 80025a99e2eSeric ** 80125a99e2eSeric ** Returns: 80225a99e2eSeric ** The exit code associated with the operation. 80325a99e2eSeric ** 80425a99e2eSeric ** Side Effects: 80525a99e2eSeric ** none. 80625a99e2eSeric ** 80725a99e2eSeric ** Called By: 80825a99e2eSeric ** deliver 80925a99e2eSeric */ 81025a99e2eSeric 81125a99e2eSeric mailfile(filename) 81225a99e2eSeric char *filename; 81325a99e2eSeric { 81425a99e2eSeric register FILE *f; 81525a99e2eSeric 81625a99e2eSeric f = fopen(filename, "a"); 81725a99e2eSeric if (f == NULL) 81825a99e2eSeric return (EX_CANTCREAT); 81925a99e2eSeric 82040e4ab56Seric putmessage(f, Mailer[1]); 82125a99e2eSeric fputs("\n", f); 822db8841e9Seric (void) fclose(f); 82325a99e2eSeric return (EX_OK); 82425a99e2eSeric } 825