122710Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 363589Sbostic * Copyright (c) 1988, 1993 463589Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822710Sdist 922710Sdist #ifndef lint 10*68494Seric static char sccsid[] = "@(#)recipient.c 8.70 (Berkeley) 03/06/95"; 1133731Sbostic #endif /* not lint */ 1222710Sdist 1358332Seric # include "sendmail.h" 144174Seric # include <pwd.h> 154174Seric 164174Seric /* 179622Seric ** SENDTOLIST -- Designate a send list. 184174Seric ** 194174Seric ** The parameter is a comma-separated list of people to send to. 204174Seric ** This routine arranges to send to all of them. 214174Seric ** 224174Seric ** Parameters: 234174Seric ** list -- the send list. 244399Seric ** ctladdr -- the address template for the person to 254399Seric ** send to -- effective uid/gid are important. 265006Seric ** This is typically the alias that caused this 275006Seric ** expansion. 285006Seric ** sendq -- a pointer to the head of a queue to put 295006Seric ** these people into. 3068481Seric ** aliaslevel -- the current alias nesting depth -- to 3168481Seric ** diagnose loops. 3258247Seric ** e -- the envelope in which to add these recipients. 334174Seric ** 344174Seric ** Returns: 3558082Seric ** The number of addresses actually on the list. 364174Seric ** 374174Seric ** Side Effects: 384174Seric ** none. 394174Seric */ 404174Seric 4168481Seric #define MAXRCRSN 10 /* maximum levels of alias recursion */ 424174Seric 4368481Seric /* q_flags bits inherited from ctladdr */ 4468481Seric #define QINHERITEDBITS (QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY|QHAS_RET_PARAM|QRET_HDRS) 4568481Seric 4668481Seric int 4768481Seric sendtolist(list, ctladdr, sendq, aliaslevel, e) 484174Seric char *list; 494399Seric ADDRESS *ctladdr; 505198Seric ADDRESS **sendq; 5168481Seric int aliaslevel; 5255012Seric register ENVELOPE *e; 534174Seric { 544174Seric register char *p; 558223Seric register ADDRESS *al; /* list of addresses to send to */ 564423Seric bool firstone; /* set on first address sent */ 5711446Seric char delimiter; /* the address delimiter */ 5858082Seric int naddrs; 5968392Seric int i; 6063847Seric char *oldto = e->e_to; 6168392Seric char *bufp; 6268271Seric char buf[MAXNAME + 1]; 634174Seric 6464131Seric if (list == NULL) 6564131Seric { 6664131Seric syserr("sendtolist: null list"); 6764131Seric return 0; 6864131Seric } 6964131Seric 707676Seric if (tTd(25, 1)) 714444Seric { 724444Seric printf("sendto: %s\n ctladdr=", list); 734444Seric printaddr(ctladdr, FALSE); 744444Seric } 754324Seric 768223Seric /* heuristic to determine old versus new style addresses */ 778230Seric if (ctladdr == NULL && 7856795Seric (strchr(list, ',') != NULL || strchr(list, ';') != NULL || 7956795Seric strchr(list, '<') != NULL || strchr(list, '(') != NULL)) 8055012Seric e->e_flags &= ~EF_OLDSTYLE; 8111446Seric delimiter = ' '; 8255012Seric if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL) 8311446Seric delimiter = ','; 848223Seric 854423Seric firstone = TRUE; 864324Seric al = NULL; 8758082Seric naddrs = 0; 888223Seric 8968392Seric /* make sure we have enough space to copy the string */ 9068392Seric i = strlen(list) + 1; 9168392Seric if (i <= sizeof buf) 9268271Seric bufp = buf; 9368392Seric else 9468392Seric bufp = xalloc(i); 9568478Seric strcpy(bufp, denlstring(list, FALSE, TRUE)); 9668271Seric 9768271Seric for (p = bufp; *p != '\0'; ) 9868271Seric { 9958333Seric auto char *delimptr; 1008081Seric register ADDRESS *a; 1014319Seric 1028081Seric /* parse the address */ 10358050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 1044174Seric p++; 10564284Seric a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter, &delimptr, e); 10658333Seric p = delimptr; 1079297Seric if (a == NULL) 1084174Seric continue; 1094324Seric a->q_next = al; 1104399Seric a->q_alias = ctladdr; 1114444Seric 1124444Seric /* see if this should be marked as a primary address */ 1134423Seric if (ctladdr == NULL || 1148081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 1154423Seric a->q_flags |= QPRIMARY; 1164444Seric 11768481Seric /* arrange to inherit attributes from parent */ 11868481Seric if (ctladdr != NULL) 11968481Seric { 12068481Seric /* self reference test */ 12168481Seric if (sameaddr(ctladdr, a)) 12268481Seric ctladdr->q_flags |= QSELFREF; 12368481Seric 12468481Seric /* full name */ 12568481Seric if (a->q_fullname == NULL) 12668481Seric a->q_fullname = ctladdr->q_fullname; 12768481Seric 12868481Seric /* various flag bits */ 12968481Seric a->q_flags &= ~QINHERITEDBITS; 13068481Seric a->q_flags |= ctladdr->q_flags & QINHERITEDBITS; 13168481Seric 13268481Seric /* original recipient information */ 13368481Seric a->q_orcpt = ctladdr->q_orcpt; 13468481Seric } 13568481Seric 13657731Seric al = a; 1374423Seric firstone = FALSE; 1384324Seric } 1394324Seric 1404324Seric /* arrange to send to everyone on the local send list */ 1414324Seric while (al != NULL) 1424324Seric { 1434324Seric register ADDRESS *a = al; 1444324Seric 1454324Seric al = a->q_next; 14668481Seric a = recipient(a, sendq, aliaslevel, e); 14758082Seric naddrs++; 1484174Seric } 1494324Seric 15063847Seric e->e_to = oldto; 15168392Seric if (bufp != buf) 15268392Seric free(bufp); 15358082Seric return (naddrs); 1544174Seric } 1554174Seric /* 1564174Seric ** RECIPIENT -- Designate a message recipient 1574174Seric ** 1584174Seric ** Saves the named person for future mailing. 1594174Seric ** 1604174Seric ** Parameters: 1614174Seric ** a -- the (preparsed) address header for the recipient. 1625006Seric ** sendq -- a pointer to the head of a queue to put the 1635006Seric ** recipient in. Duplicate supression is done 1645006Seric ** in this queue. 16568481Seric ** aliaslevel -- the current alias nesting depth. 16657731Seric ** e -- the current envelope. 1674174Seric ** 1684174Seric ** Returns: 16912613Seric ** The actual address in the queue. This will be "a" if 17012613Seric ** the address is not a duplicate, else the original address. 1714174Seric ** 1724174Seric ** Side Effects: 1734174Seric ** none. 1744174Seric */ 1754174Seric 17612613Seric ADDRESS * 17768481Seric recipient(a, sendq, aliaslevel, e) 1784174Seric register ADDRESS *a; 1795006Seric register ADDRESS **sendq; 18068481Seric int aliaslevel; 18155012Seric register ENVELOPE *e; 1824174Seric { 1834174Seric register ADDRESS *q; 1844319Seric ADDRESS **pq; 1854174Seric register struct mailer *m; 1869210Seric register char *p; 1879210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 18853735Seric int findusercount = 0; 18968481Seric int i; 19068481Seric char *buf; 19168481Seric char buf0[MAXNAME]; /* unquoted image of the user name */ 19258247Seric extern int safefile(); 1934174Seric 19455012Seric e->e_to = a->q_paddr; 1954600Seric m = a->q_mailer; 1964174Seric errno = 0; 1977676Seric if (tTd(26, 1)) 1984444Seric { 1994444Seric printf("\nrecipient: "); 2004444Seric printaddr(a, FALSE); 2014444Seric } 2024174Seric 20364146Seric /* if this is primary, add it to the original recipient list */ 20464146Seric if (a->q_alias == NULL) 20564146Seric { 20664146Seric if (e->e_origrcpt == NULL) 20764146Seric e->e_origrcpt = a->q_paddr; 20864146Seric else if (e->e_origrcpt != a->q_paddr) 20964146Seric e->e_origrcpt = ""; 21064146Seric } 21164146Seric 2124174Seric /* break aliasing loops */ 21368481Seric if (aliaslevel > MAXRCRSN) 2144174Seric { 21568481Seric usrerr("554 aliasing/forwarding loop broken (%d aliases deep; %d max", 21668481Seric aliaslevel, MAXRCRSN); 21712613Seric return (a); 2184174Seric } 2194174Seric 2204174Seric /* 2214627Seric ** Finish setting up address structure. 2224174Seric */ 2234174Seric 22416160Seric /* get unquoted user for file, program or user.name check */ 22568481Seric i = strlen(a->q_user); 22668481Seric if (i >= sizeof buf) 22768481Seric buf = xalloc(i + 1); 22868481Seric else 22968481Seric buf = buf0; 2309210Seric (void) strcpy(buf, a->q_user); 2319210Seric for (p = buf; *p != '\0' && !quoted; p++) 2329210Seric { 23354993Seric if (*p == '\\') 2349210Seric quoted = TRUE; 2359210Seric } 23654983Seric stripquotes(buf); 2379210Seric 23857402Seric /* check for direct mailing to restricted mailers */ 23965496Seric if (m == ProgMailer) 2404174Seric { 24165496Seric if (a->q_alias == NULL) 24265496Seric { 24365496Seric a->q_flags |= QBADADDR; 24465496Seric usrerr("550 Cannot mail directly to programs"); 24565496Seric } 24665496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 24765496Seric { 24865496Seric a->q_flags |= QBADADDR; 24965496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs", 25065496Seric a->q_alias->q_ruser, MyHostName); 25165496Seric } 25265496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 25365496Seric { 25465496Seric a->q_flags |= QBADADDR; 25565496Seric usrerr("550 Address %s is unsafe for mailing to programs", 25665496Seric a->q_alias->q_paddr); 25765496Seric } 2584174Seric } 2594174Seric 2604174Seric /* 2614419Seric ** Look up this person in the recipient list. 2624419Seric ** If they are there already, return, otherwise continue. 2634419Seric ** If the list is empty, just add it. Notice the cute 2644419Seric ** hack to make from addresses suppress things correctly: 2654419Seric ** the QDONTSEND bit will be set in the send list. 2664419Seric ** [Please note: the emphasis is on "hack."] 2674174Seric */ 2684174Seric 2695006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2704174Seric { 27158294Seric if (sameaddr(q, a)) 2724174Seric { 2737676Seric if (tTd(26, 1)) 2744444Seric { 2754444Seric printf("%s in sendq: ", a->q_paddr); 2764444Seric printaddr(q, FALSE); 2774444Seric } 27865593Seric if (!bitset(QPRIMARY, q->q_flags)) 27958065Seric { 28065593Seric if (!bitset(QDONTSEND, a->q_flags)) 28158151Seric message("duplicate suppressed"); 28265593Seric q->q_flags |= a->q_flags; 28365593Seric } 28465593Seric else if (bitset(QSELFREF, q->q_flags)) 28565579Seric q->q_flags |= a->q_flags & ~QDONTSEND; 28663847Seric a = q; 28768481Seric goto done; 2884174Seric } 2894319Seric } 2904174Seric 2914319Seric /* add address on list */ 29258884Seric *pq = a; 29358884Seric a->q_next = NULL; 2944174Seric 2954174Seric /* 29657402Seric ** Alias the name and handle special mailer types. 2974174Seric */ 2984174Seric 29953735Seric trylocaluser: 30055354Seric if (tTd(29, 7)) 30155354Seric printf("at trylocaluser %s\n", a->q_user); 30255354Seric 30358680Seric if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 30463847Seric goto testselfdestruct; 30557402Seric 30657402Seric if (m == InclMailer) 3074174Seric { 30857402Seric a->q_flags |= QDONTSEND; 30964761Seric if (a->q_alias == NULL) 3104174Seric { 31158680Seric a->q_flags |= QBADADDR; 31258151Seric usrerr("550 Cannot mail directly to :include:s"); 3134174Seric } 3144174Seric else 31550556Seric { 31659563Seric int ret; 31758247Seric 31858151Seric message("including file %s", a->q_user); 31968481Seric ret = include(a->q_user, FALSE, a, sendq, aliaslevel, e); 32059563Seric if (transienterror(ret)) 32159563Seric { 32259563Seric #ifdef LOG 32359563Seric if (LogLevel > 2) 32466239Seric syslog(LOG_ERR, "%s: include %s: transient error: %s", 32566284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 32666284Seric a->q_user, errstring(ret)); 32759563Seric #endif 32863853Seric a->q_flags |= QQUEUEUP; 32965215Seric a->q_flags &= ~QDONTSEND; 33059563Seric usrerr("451 Cannot open %s: %s", 33159563Seric a->q_user, errstring(ret)); 33259563Seric } 33359563Seric else if (ret != 0) 33459563Seric { 33563938Seric a->q_flags |= QBADADDR; 33659563Seric usrerr("550 Cannot open %s: %s", 33759563Seric a->q_user, errstring(ret)); 33859563Seric } 33950556Seric } 3404174Seric } 34157642Seric else if (m == FileMailer) 3424174Seric { 3434329Seric extern bool writable(); 3444174Seric 34551317Seric /* check if writable or creatable */ 34664761Seric if (a->q_alias == NULL) 3474174Seric { 34858680Seric a->q_flags |= QBADADDR; 34958151Seric usrerr("550 Cannot mail directly to files"); 3504174Seric } 35165496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 35265496Seric { 35365496Seric a->q_flags |= QBADADDR; 35465496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to files", 35565496Seric a->q_alias->q_ruser, MyHostName); 35665496Seric } 35765496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 35865496Seric { 35965496Seric a->q_flags |= QBADADDR; 36065496Seric usrerr("550 Address %s is unsafe for mailing to files", 36165496Seric a->q_alias->q_paddr); 36265496Seric } 363*68494Seric else if (!writable(buf, getctladdr(a), SFF_CREAT)) 36451317Seric { 36558680Seric a->q_flags |= QBADADDR; 36668481Seric giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, 36768481Seric (time_t) 0, e); 36851317Seric } 36951317Seric } 37051317Seric 37157402Seric /* try aliasing */ 37268481Seric if (!bitset(QDONTSEND, a->q_flags) && bitnset(M_ALIASABLE, m->m_flags)) 37368481Seric alias(a, sendq, aliaslevel, e); 37457402Seric 37557402Seric # ifdef USERDB 37657402Seric /* if not aliased, look it up in the user database */ 37768481Seric if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags) && 37868481Seric bitnset(M_CHECKUDB, m->m_flags)) 37957402Seric { 38057402Seric extern int udbexpand(); 38157402Seric 38268481Seric if (udbexpand(a, sendq, aliaslevel, e) == EX_TEMPFAIL) 38357402Seric { 38463853Seric a->q_flags |= QQUEUEUP; 38557402Seric if (e->e_message == NULL) 38657402Seric e->e_message = newstr("Deferred: user database error"); 38757402Seric # ifdef LOG 38858020Seric if (LogLevel > 8) 38959623Seric syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 39066284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 39166284Seric errstring(errno)); 39257402Seric # endif 39359615Seric message("queued (user database error): %s", 39459615Seric errstring(errno)); 39557642Seric e->e_nrcpts++; 39663847Seric goto testselfdestruct; 39757402Seric } 39857402Seric } 39957402Seric # endif 40057402Seric 40151317Seric /* 40251317Seric ** If we have a level two config file, then pass the name through 40351317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 40451317Seric ** to send rewrite it to another mailer. This gives us a hook 40551317Seric ** after local aliasing has been done. 40651317Seric */ 40751317Seric 40851317Seric if (tTd(29, 5)) 40951317Seric { 41051317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 41151317Seric ConfigLevel, RewriteRules[5]); 41251317Seric printaddr(a, FALSE); 41351317Seric } 41468481Seric if (!bitset(QNOTREMOTE|QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags) && 41568481Seric ConfigLevel >= 2 && RewriteRules[5] != NULL && 41668481Seric bitnset(M_TRYRULESET5, m->m_flags)) 41751317Seric { 41868481Seric maplocaluser(a, sendq, aliaslevel, e); 41951317Seric } 42051317Seric 42151317Seric /* 42251317Seric ** If it didn't get rewritten to another mailer, go ahead 42351317Seric ** and deliver it. 42451317Seric */ 42551317Seric 42668481Seric if (!bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags) && 42768481Seric bitnset(M_HASPWENT, m->m_flags)) 42851317Seric { 42955354Seric auto bool fuzzy; 43051317Seric register struct passwd *pw; 43151317Seric extern struct passwd *finduser(); 43251317Seric 43351317Seric /* warning -- finduser may trash buf */ 43455354Seric pw = finduser(buf, &fuzzy); 43551317Seric if (pw == NULL) 43651317Seric { 43758680Seric a->q_flags |= QBADADDR; 43868481Seric giveresponse(EX_NOUSER, m, NULL, a->q_alias, 43968481Seric (time_t) 0, e); 44051317Seric } 4414174Seric else 4424174Seric { 44351317Seric char nbuf[MAXNAME]; 4444373Seric 44555354Seric if (fuzzy) 4464174Seric { 44753735Seric /* name was a fuzzy match */ 44851317Seric a->q_user = newstr(pw->pw_name); 44953735Seric if (findusercount++ > 3) 45053735Seric { 45158680Seric a->q_flags |= QBADADDR; 45258151Seric usrerr("554 aliasing/forwarding loop for %s broken", 45353735Seric pw->pw_name); 45468481Seric goto done; 45553735Seric } 45653735Seric 45753735Seric /* see if it aliases */ 45851317Seric (void) strcpy(buf, pw->pw_name); 45953735Seric goto trylocaluser; 4604174Seric } 46165822Seric if (strcmp(pw->pw_dir, "/") == 0) 46265822Seric a->q_home = ""; 46365822Seric else 46465822Seric a->q_home = newstr(pw->pw_dir); 46551317Seric a->q_uid = pw->pw_uid; 46651317Seric a->q_gid = pw->pw_gid; 46759083Seric a->q_ruser = newstr(pw->pw_name); 46851317Seric a->q_flags |= QGOODUID; 46951317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 47051317Seric if (nbuf[0] != '\0') 47151317Seric a->q_fullname = newstr(nbuf); 47265211Seric if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' && 47365211Seric !usershellok(pw->pw_shell)) 47465206Seric { 47565211Seric a->q_flags |= QBOGUSSHELL; 47665206Seric } 47751317Seric if (!quoted) 47868481Seric forward(a, sendq, aliaslevel, e); 4794174Seric } 4804174Seric } 48157642Seric if (!bitset(QDONTSEND, a->q_flags)) 48257642Seric e->e_nrcpts++; 48363847Seric 48463847Seric testselfdestruct: 48563978Seric if (tTd(26, 8)) 48663847Seric { 48763978Seric printf("testselfdestruct: "); 48863978Seric printaddr(a, TRUE); 48963978Seric } 49063978Seric if (a->q_alias == NULL && a != &e->e_from && 49163978Seric bitset(QDONTSEND, a->q_flags)) 49263978Seric { 49363978Seric q = *sendq; 49463965Seric while (q != NULL && bitset(QDONTSEND, q->q_flags)) 49563847Seric q = q->q_next; 49663978Seric if (q == NULL) 49763847Seric { 49863847Seric a->q_flags |= QBADADDR; 49963847Seric usrerr("554 aliasing/forwarding loop broken"); 50063847Seric } 50163847Seric } 50268481Seric 50368481Seric done: 50468481Seric if (buf != buf0) 50568481Seric free(buf); 50612613Seric return (a); 5074174Seric } 5084174Seric /* 5094373Seric ** FINDUSER -- find the password entry for a user. 5104373Seric ** 5114373Seric ** This looks a lot like getpwnam, except that it may want to 5124373Seric ** do some fancier pattern matching in /etc/passwd. 5134373Seric ** 5149379Seric ** This routine contains most of the time of many sendmail runs. 5159379Seric ** It deserves to be optimized. 5169379Seric ** 5174373Seric ** Parameters: 5184373Seric ** name -- the name to match against. 51955354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 52055354Seric ** was found using the fuzzy matching algorithm; 52155354Seric ** set to FALSE otherwise. 5224373Seric ** 5234373Seric ** Returns: 5244373Seric ** A pointer to a pw struct. 5254373Seric ** NULL if name is unknown or ambiguous. 5264373Seric ** 5274373Seric ** Side Effects: 5284407Seric ** may modify name. 5294373Seric */ 5304373Seric 5314373Seric struct passwd * 53255354Seric finduser(name, fuzzyp) 5334373Seric char *name; 53455354Seric bool *fuzzyp; 5354373Seric { 5364376Seric register struct passwd *pw; 5374407Seric register char *p; 53815325Seric extern struct passwd *getpwent(); 53915325Seric extern struct passwd *getpwnam(); 5404373Seric 54155354Seric if (tTd(29, 4)) 54255354Seric printf("finduser(%s): ", name); 54355354Seric 54455354Seric *fuzzyp = FALSE; 5454407Seric 54668481Seric #ifdef HESIOD 54764673Seric /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */ 54864673Seric for (p = name; *p != '\0'; p++) 54964673Seric if (!isascii(*p) || !isdigit(*p)) 55064673Seric break; 55164673Seric if (*p == '\0') 55264673Seric { 55364673Seric if (tTd(29, 4)) 55464673Seric printf("failed (numeric input)\n"); 55564673Seric return NULL; 55664673Seric } 55768481Seric #endif 55864673Seric 55925777Seric /* look up this login name using fast path */ 56012634Seric if ((pw = getpwnam(name)) != NULL) 56155354Seric { 56255354Seric if (tTd(29, 4)) 56355354Seric printf("found (non-fuzzy)\n"); 56412634Seric return (pw); 56555354Seric } 56612634Seric 56753735Seric #ifdef MATCHGECOS 56853735Seric /* see if fuzzy matching allowed */ 56953735Seric if (!MatchGecos) 57055354Seric { 57155354Seric if (tTd(29, 4)) 57255354Seric printf("not found (fuzzy disabled)\n"); 57353735Seric return NULL; 57455354Seric } 57553735Seric 57612634Seric /* search for a matching full name instead */ 57725777Seric for (p = name; *p != '\0'; p++) 57825777Seric { 57925777Seric if (*p == (SpaceSub & 0177) || *p == '_') 58025777Seric *p = ' '; 58125777Seric } 58223107Seric (void) setpwent(); 5834376Seric while ((pw = getpwent()) != NULL) 5844376Seric { 5854998Seric char buf[MAXNAME]; 5864376Seric 5874998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 58856795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5894381Seric { 59055354Seric if (tTd(29, 4)) 59155354Seric printf("fuzzy matches %s\n", pw->pw_name); 59258151Seric message("sending to login name %s", pw->pw_name); 59355354Seric *fuzzyp = TRUE; 5944376Seric return (pw); 5954377Seric } 5964376Seric } 59755354Seric if (tTd(29, 4)) 59855354Seric printf("no fuzzy match found\n"); 59959015Seric #else 60059015Seric if (tTd(29, 4)) 60159015Seric printf("not found (fuzzy disabled)\n"); 60259015Seric #endif 6034376Seric return (NULL); 6044373Seric } 6054373Seric /* 6064329Seric ** WRITABLE -- predicate returning if the file is writable. 6074329Seric ** 6084329Seric ** This routine must duplicate the algorithm in sys/fio.c. 6094329Seric ** Unfortunately, we cannot use the access call since we 6104329Seric ** won't necessarily be the real uid when we try to 6114329Seric ** actually open the file. 6124329Seric ** 6134329Seric ** Notice that ANY file with ANY execute bit is automatically 6144329Seric ** not writable. This is also enforced by mailfile. 6154329Seric ** 6164329Seric ** Parameters: 61765064Seric ** filename -- the file name to check. 61865112Seric ** ctladdr -- the controlling address for this file. 61965064Seric ** flags -- SFF_* flags to control the function. 6204329Seric ** 6214329Seric ** Returns: 6224329Seric ** TRUE -- if we will be able to write this file. 6234329Seric ** FALSE -- if we cannot write this file. 6244329Seric ** 6254329Seric ** Side Effects: 6264329Seric ** none. 6274329Seric */ 6284329Seric 6294329Seric bool 63065112Seric writable(filename, ctladdr, flags) 63164819Seric char *filename; 63265112Seric ADDRESS *ctladdr; 63365064Seric int flags; 6344329Seric { 63555372Seric uid_t euid; 63655372Seric gid_t egid; 6374329Seric int bits; 63864944Seric register char *p; 63964944Seric char *uname; 6404329Seric 64164819Seric if (tTd(29, 5)) 64265064Seric printf("writable(%s, %x)\n", filename, flags); 64364944Seric 64465225Seric #ifdef SUID_ROOT_FILES_OK 64565225Seric /* really ought to be passed down -- and not a good idea */ 64665225Seric flags |= SFF_ROOTOK; 64765225Seric #endif 64865225Seric 64964944Seric /* 65064944Seric ** File does exist -- check that it is writable. 65164944Seric */ 65264944Seric 65365112Seric if (ctladdr != NULL && geteuid() == 0) 65464944Seric { 65565112Seric euid = ctladdr->q_uid; 65665112Seric egid = ctladdr->q_gid; 65765112Seric uname = ctladdr->q_user; 65864944Seric } 65968481Seric #ifdef RUN_AS_REAL_UID 66065112Seric else 66165112Seric { 662*68494Seric extern char RealUserName[]; 663*68494Seric 66465112Seric euid = RealUid; 66565112Seric egid = RealGid; 66665112Seric uname = RealUserName; 66765112Seric } 66868481Seric #else 66968481Seric else if (FileMailer != NULL) 67068481Seric { 67168481Seric euid = FileMailer->m_uid; 67268481Seric egid = FileMailer->m_gid; 67368481Seric } 67468481Seric else 67568481Seric { 67668481Seric euid = egid = 0; 67768481Seric } 67868481Seric #endif 67965138Seric if (euid == 0) 68065138Seric { 68165138Seric euid = DefUid; 68265138Seric uname = DefUser; 68365138Seric } 68465138Seric if (egid == 0) 68565138Seric egid = DefGid; 6864329Seric if (geteuid() == 0) 687*68494Seric flags |= SFF_SETUIDOK; 6884329Seric 689*68494Seric errno = safefile(filename, euid, egid, uname, flags, S_IWRITE, NULL); 69065067Seric return errno == 0; 6914329Seric } 6924329Seric /* 6934174Seric ** INCLUDE -- handle :include: specification. 6944174Seric ** 6954174Seric ** Parameters: 6964174Seric ** fname -- filename to include. 69753037Seric ** forwarding -- if TRUE, we are reading a .forward file. 69853037Seric ** if FALSE, it's a :include: file. 6994399Seric ** ctladdr -- address template to use to fill in these 7004399Seric ** addresses -- effective user/group id are 7014399Seric ** the important things. 7025006Seric ** sendq -- a pointer to the head of the send queue 7035006Seric ** to put these addresses in. 70468481Seric ** aliaslevel -- the alias nesting depth. 70568481Seric ** e -- the current envelope. 7064174Seric ** 7074174Seric ** Returns: 70857136Seric ** open error status 7094174Seric ** 7104174Seric ** Side Effects: 7114174Seric ** reads the :include: file and sends to everyone 7124174Seric ** listed in that file. 71365909Seric ** 71465909Seric ** Security Note: 71565909Seric ** If you have restricted chown (that is, you can't 71665909Seric ** give a file away), it is reasonable to allow programs 71765909Seric ** and files called from this :include: file to be to be 71865909Seric ** run as the owner of the :include: file. This is bogus 71965909Seric ** if there is any chance of someone giving away a file. 72065909Seric ** We assume that pre-POSIX systems can give away files. 72165909Seric ** 72265909Seric ** There is an additional restriction that if you 72365909Seric ** forward to a :include: file, it will not take on 72465909Seric ** the ownership of the :include: file. This may not 72565909Seric ** be necessary, but shouldn't hurt. 7264174Seric */ 7274174Seric 72853037Seric static jmp_buf CtxIncludeTimeout; 72968481Seric static void includetimeout(); 73053037Seric 73157136Seric int 73268481Seric include(fname, forwarding, ctladdr, sendq, aliaslevel, e) 7334174Seric char *fname; 73453037Seric bool forwarding; 7354399Seric ADDRESS *ctladdr; 7365006Seric ADDRESS **sendq; 73768481Seric int aliaslevel; 73855012Seric ENVELOPE *e; 7394174Seric { 74068481Seric FILE *fp = NULL; 74155012Seric char *oldto = e->e_to; 7429379Seric char *oldfilename = FileName; 7439379Seric int oldlinenumber = LineNumber; 74453037Seric register EVENT *ev = NULL; 74558082Seric int nincludes; 74664325Seric register ADDRESS *ca; 74764325Seric uid_t saveduid, uid; 74864325Seric gid_t savedgid, gid; 74964083Seric char *uname; 75064325Seric int rval = 0; 75165064Seric int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE; 75265496Seric struct stat st; 75365948Seric char buf[MAXLINE]; 75465909Seric #ifdef _POSIX_CHOWN_RESTRICTED 75565948Seric # if _POSIX_CHOWN_RESTRICTED == -1 75665948Seric # define safechown FALSE 75765948Seric # else 75865948Seric # define safechown TRUE 75965948Seric # endif 76065948Seric #else 76165948Seric # ifdef _PC_CHOWN_RESTRICTED 76265909Seric bool safechown; 76365948Seric # else 76465948Seric # ifdef BSD 76565948Seric # define safechown TRUE 76665948Seric # else 76765948Seric # define safechown FALSE 76865948Seric # endif 76965948Seric # endif 77065909Seric #endif 77165948Seric extern bool chownsafe(); 7724174Seric 77357186Seric if (tTd(27, 2)) 77457186Seric printf("include(%s)\n", fname); 77563902Seric if (tTd(27, 4)) 77663902Seric printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 77763581Seric if (tTd(27, 14)) 77863581Seric { 77963581Seric printf("ctladdr "); 78063581Seric printaddr(ctladdr, FALSE); 78163581Seric } 78257186Seric 78364325Seric if (tTd(27, 9)) 78464325Seric printf("include: old uid = %d/%d\n", getuid(), geteuid()); 78553037Seric 78663581Seric ca = getctladdr(ctladdr); 78763581Seric if (ca == NULL) 78864083Seric { 78964846Seric uid = DefUid; 79064846Seric gid = DefGid; 79164846Seric uname = DefUser; 79264083Seric } 79363581Seric else 79464083Seric { 79563581Seric uid = ca->q_uid; 79664083Seric gid = ca->q_gid; 79764083Seric uname = ca->q_user; 79868481Seric } 79964325Seric #ifdef HASSETREUID 80068481Seric saveduid = geteuid(); 80168481Seric savedgid = getegid(); 80268481Seric if (saveduid == 0) 80368481Seric { 80468481Seric initgroups(uname, gid); 80568481Seric if (uid != 0) 80664325Seric { 80768481Seric if (setreuid(0, uid) < 0) 80868481Seric syserr("setreuid(0, %d) failure (real=%d, eff=%d)", 80968481Seric uid, getuid(), geteuid()); 81068481Seric else 81168481Seric sfflags |= SFF_NOPATHCHECK; 81264325Seric } 81368481Seric } 81468478Seric #endif 81563581Seric 81664325Seric if (tTd(27, 9)) 81764325Seric printf("include: new uid = %d/%d\n", getuid(), geteuid()); 81864325Seric 81964325Seric /* 82064325Seric ** If home directory is remote mounted but server is down, 82164325Seric ** this can hang or give errors; use a timeout to avoid this 82264325Seric */ 82364325Seric 82453037Seric if (setjmp(CtxIncludeTimeout) != 0) 82553037Seric { 82663853Seric ctladdr->q_flags |= QQUEUEUP; 82753037Seric errno = 0; 82863993Seric 82963993Seric /* return pseudo-error code */ 83064325Seric rval = EOPENTIMEOUT; 83164325Seric goto resetuid; 83253037Seric } 83368481Seric if (TimeOuts.to_fileopen > 0) 83468481Seric ev = setevent(TimeOuts.to_fileopen, includetimeout, 0); 83568481Seric else 83668481Seric ev = NULL; 83753037Seric 83863581Seric /* the input file must be marked safe */ 839*68494Seric rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD, NULL); 84064329Seric if (rval != 0) 84153037Seric { 84264325Seric /* don't use this :include: file */ 84357186Seric if (tTd(27, 4)) 84458247Seric printf("include: not safe (uid=%d): %s\n", 84564329Seric uid, errstring(rval)); 84653037Seric } 84765496Seric else 8484174Seric { 84965496Seric fp = fopen(fname, "r"); 85065496Seric if (fp == NULL) 85158061Seric { 85264329Seric rval = errno; 85365496Seric if (tTd(27, 4)) 85465496Seric printf("include: open: %s\n", errstring(rval)); 85558061Seric } 8564406Seric } 85768481Seric if (ev != NULL) 85868481Seric clrevent(ev); 85953037Seric 86064570Seric resetuid: 86164570Seric 86264570Seric #ifdef HASSETREUID 86364570Seric if (saveduid == 0) 86464570Seric { 86564570Seric if (uid != 0) 86668481Seric { 86768481Seric if (setreuid(-1, 0) < 0) 86868481Seric syserr("setreuid(-1, 0) failure (real=%d, eff=%d)", 86968481Seric getuid(), geteuid()); 87068481Seric if (setreuid(RealUid, 0) < 0) 87164570Seric syserr("setreuid(%d, 0) failure (real=%d, eff=%d)", 87264570Seric RealUid, getuid(), geteuid()); 87368481Seric } 87464570Seric setgid(savedgid); 87564570Seric } 87664570Seric #endif 87764570Seric 87864570Seric if (tTd(27, 9)) 87964570Seric printf("include: reset uid = %d/%d\n", getuid(), geteuid()); 88064570Seric 88165593Seric if (rval == EOPENTIMEOUT) 88265593Seric usrerr("451 open timeout on %s", fname); 88365593Seric 88464570Seric if (fp == NULL) 88564570Seric return rval; 88664570Seric 88765496Seric if (fstat(fileno(fp), &st) < 0) 88865496Seric { 88965496Seric rval = errno; 89065496Seric syserr("Cannot fstat %s!", fname); 89165496Seric return rval; 89265496Seric } 89365496Seric 89465948Seric #ifndef safechown 89565948Seric safechown = chownsafe(fileno(fp)); 89665948Seric #endif 89765909Seric if (ca == NULL && safechown) 89865496Seric { 89965496Seric ctladdr->q_uid = st.st_uid; 90065496Seric ctladdr->q_gid = st.st_gid; 90165496Seric ctladdr->q_flags |= QGOODUID; 90265496Seric } 90365496Seric if (ca != NULL && ca->q_uid == st.st_uid) 90465496Seric { 90565496Seric /* optimization -- avoid getpwuid if we already have info */ 90665496Seric ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL; 90765496Seric ctladdr->q_ruser = ca->q_ruser; 90865496Seric } 90965496Seric else 91065496Seric { 91165496Seric register struct passwd *pw; 91265496Seric 91365496Seric pw = getpwuid(st.st_uid); 91468481Seric if (pw == NULL) 91568481Seric ctladdr->q_flags |= QBOGUSSHELL; 91668481Seric else 91768478Seric { 91868481Seric char *sh; 91968481Seric 92068478Seric ctladdr->q_ruser = newstr(pw->pw_name); 92168478Seric if (safechown) 92268478Seric sh = pw->pw_shell; 92365909Seric else 92468481Seric sh = "/SENDMAIL/ANY/SHELL/"; 92568481Seric if (!usershellok(sh)) 92668481Seric { 92768481Seric if (safechown) 92868481Seric ctladdr->q_flags |= QBOGUSSHELL; 92968481Seric else 93068481Seric ctladdr->q_flags |= QUNSAFEADDR; 93168481Seric } 93265496Seric } 93365496Seric } 93465496Seric 93558092Seric if (bitset(EF_VRFYONLY, e->e_flags)) 93658092Seric { 93758092Seric /* don't do any more now */ 93858868Seric ctladdr->q_flags |= QVERIFIED; 93958884Seric e->e_nrcpts++; 94058680Seric xfclose(fp, "include", fname); 94164570Seric return rval; 94258092Seric } 94358092Seric 94465496Seric /* 94565496Seric ** Check to see if some bad guy can write this file 94665496Seric ** 94765496Seric ** This should really do something clever with group 94865496Seric ** permissions; currently we just view world writable 94965496Seric ** as unsafe. Also, we don't check for writable 95065496Seric ** directories in the path. We've got to leave 95165496Seric ** something for the local sysad to do. 95265496Seric */ 95365496Seric 95465496Seric if (bitset(S_IWOTH, st.st_mode)) 95565496Seric ctladdr->q_flags |= QUNSAFEADDR; 95665496Seric 9574174Seric /* read the file -- each line is a comma-separated list. */ 9589379Seric FileName = fname; 9599379Seric LineNumber = 0; 96058082Seric ctladdr->q_flags &= ~QSELFREF; 96158082Seric nincludes = 0; 9624174Seric while (fgets(buf, sizeof buf, fp) != NULL) 9634174Seric { 96456795Seric register char *p = strchr(buf, '\n'); 9654174Seric 96640963Sbostic LineNumber++; 9674174Seric if (p != NULL) 9684174Seric *p = '\0'; 96957186Seric if (buf[0] == '#' || buf[0] == '\0') 97057139Seric continue; 97158008Seric e->e_to = NULL; 97258151Seric message("%s to %s", 97353037Seric forwarding ? "forwarding" : "sending", buf); 97457977Seric #ifdef LOG 97558020Seric if (forwarding && LogLevel > 9) 97657977Seric syslog(LOG_INFO, "%s: forward %s => %s", 97766284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 97866284Seric oldto, buf); 97957977Seric #endif 98057977Seric 98168481Seric nincludes += sendtolist(buf, ctladdr, sendq, aliaslevel + 1, e); 9824174Seric } 98363902Seric 98463902Seric if (ferror(fp) && tTd(27, 3)) 98563902Seric printf("include: read error: %s\n", errstring(errno)); 98658082Seric if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 98758065Seric { 98858065Seric if (tTd(27, 5)) 98958065Seric { 99058065Seric printf("include: QDONTSEND "); 99158065Seric printaddr(ctladdr, FALSE); 99258065Seric } 99358065Seric ctladdr->q_flags |= QDONTSEND; 99458065Seric } 9954174Seric 99658680Seric (void) xfclose(fp, "include", fname); 9979379Seric FileName = oldfilename; 9989379Seric LineNumber = oldlinenumber; 99963847Seric e->e_to = oldto; 100064325Seric return rval; 10014174Seric } 100253037Seric 100368481Seric static void 100453037Seric includetimeout() 100553037Seric { 100653037Seric longjmp(CtxIncludeTimeout, 1); 100753037Seric } 10084324Seric /* 10094324Seric ** SENDTOARGV -- send to an argument vector. 10104324Seric ** 10114324Seric ** Parameters: 10124324Seric ** argv -- argument vector to send to. 101358247Seric ** e -- the current envelope. 10144324Seric ** 10154324Seric ** Returns: 10164324Seric ** none. 10174324Seric ** 10184324Seric ** Side Effects: 10194324Seric ** puts all addresses on the argument vector onto the 10204324Seric ** send queue. 10214324Seric */ 10224324Seric 102355012Seric sendtoargv(argv, e) 10244324Seric register char **argv; 102555012Seric register ENVELOPE *e; 10264324Seric { 10274324Seric register char *p; 10284324Seric 10294324Seric while ((p = *argv++) != NULL) 10304324Seric { 103168481Seric (void) sendtolist(p, NULLADDR, &e->e_sendqueue, 0, e); 10324324Seric } 10334324Seric } 10344399Seric /* 10354399Seric ** GETCTLADDR -- get controlling address from an address header. 10364399Seric ** 10374399Seric ** If none, get one corresponding to the effective userid. 10384399Seric ** 10394399Seric ** Parameters: 10404399Seric ** a -- the address to find the controller of. 10414399Seric ** 10424399Seric ** Returns: 10434399Seric ** the controlling address. 10444399Seric ** 10454399Seric ** Side Effects: 10464399Seric ** none. 10474399Seric */ 10484399Seric 10494399Seric ADDRESS * 10504399Seric getctladdr(a) 10514399Seric register ADDRESS *a; 10524399Seric { 10534404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 10544399Seric a = a->q_alias; 10554399Seric return (a); 10564399Seric } 1057