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*67264Seric static char sccsid[] = "@(#)recipient.c 8.45 (Berkeley) 05/29/94"; 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. 3058247Seric ** e -- the envelope in which to add these recipients. 314174Seric ** 324174Seric ** Returns: 3358082Seric ** The number of addresses actually on the list. 344174Seric ** 354174Seric ** Side Effects: 364174Seric ** none. 374174Seric */ 384174Seric 394174Seric # define MAXRCRSN 10 404174Seric 4155012Seric sendtolist(list, ctladdr, sendq, e) 424174Seric char *list; 434399Seric ADDRESS *ctladdr; 445198Seric ADDRESS **sendq; 4555012Seric register ENVELOPE *e; 464174Seric { 474174Seric register char *p; 488223Seric register ADDRESS *al; /* list of addresses to send to */ 494423Seric bool firstone; /* set on first address sent */ 5011446Seric char delimiter; /* the address delimiter */ 5158082Seric int naddrs; 5263847Seric char *oldto = e->e_to; 534174Seric 5464131Seric if (list == NULL) 5564131Seric { 5664131Seric syserr("sendtolist: null list"); 5764131Seric return 0; 5864131Seric } 5964131Seric 607676Seric if (tTd(25, 1)) 614444Seric { 624444Seric printf("sendto: %s\n ctladdr=", list); 634444Seric printaddr(ctladdr, FALSE); 644444Seric } 654324Seric 668223Seric /* heuristic to determine old versus new style addresses */ 678230Seric if (ctladdr == NULL && 6856795Seric (strchr(list, ',') != NULL || strchr(list, ';') != NULL || 6956795Seric strchr(list, '<') != NULL || strchr(list, '(') != NULL)) 7055012Seric e->e_flags &= ~EF_OLDSTYLE; 7111446Seric delimiter = ' '; 7255012Seric if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL) 7311446Seric delimiter = ','; 748223Seric 754423Seric firstone = TRUE; 764324Seric al = NULL; 7758082Seric naddrs = 0; 788223Seric 798081Seric for (p = list; *p != '\0'; ) 804174Seric { 8158333Seric auto char *delimptr; 828081Seric register ADDRESS *a; 834319Seric 848081Seric /* parse the address */ 8558050Seric while ((isascii(*p) && isspace(*p)) || *p == ',') 864174Seric p++; 8764284Seric a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter, &delimptr, e); 8858333Seric p = delimptr; 899297Seric if (a == NULL) 904174Seric continue; 914324Seric a->q_next = al; 924399Seric a->q_alias = ctladdr; 934444Seric 944444Seric /* see if this should be marked as a primary address */ 954423Seric if (ctladdr == NULL || 968081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 974423Seric a->q_flags |= QPRIMARY; 984444Seric 999379Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 10058061Seric ctladdr->q_flags |= QSELFREF; 10157731Seric al = a; 1024423Seric firstone = FALSE; 1034324Seric } 1044324Seric 1054324Seric /* arrange to send to everyone on the local send list */ 1064324Seric while (al != NULL) 1074324Seric { 1084324Seric register ADDRESS *a = al; 1094324Seric 1104324Seric al = a->q_next; 11155012Seric a = recipient(a, sendq, e); 1124993Seric 1134998Seric /* arrange to inherit full name */ 1144998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1154998Seric a->q_fullname = ctladdr->q_fullname; 11658082Seric naddrs++; 1174174Seric } 1184324Seric 11963847Seric e->e_to = oldto; 12058082Seric return (naddrs); 1214174Seric } 1224174Seric /* 1234174Seric ** RECIPIENT -- Designate a message recipient 1244174Seric ** 1254174Seric ** Saves the named person for future mailing. 1264174Seric ** 1274174Seric ** Parameters: 1284174Seric ** a -- the (preparsed) address header for the recipient. 1295006Seric ** sendq -- a pointer to the head of a queue to put the 1305006Seric ** recipient in. Duplicate supression is done 1315006Seric ** in this queue. 13257731Seric ** e -- the current envelope. 1334174Seric ** 1344174Seric ** Returns: 13512613Seric ** The actual address in the queue. This will be "a" if 13612613Seric ** the address is not a duplicate, else the original address. 1374174Seric ** 1384174Seric ** Side Effects: 1394174Seric ** none. 1404174Seric */ 1414174Seric 14212613Seric ADDRESS * 14355012Seric recipient(a, sendq, e) 1444174Seric register ADDRESS *a; 1455006Seric register ADDRESS **sendq; 14655012Seric register ENVELOPE *e; 1474174Seric { 1484174Seric register ADDRESS *q; 1494319Seric ADDRESS **pq; 1504174Seric register struct mailer *m; 1519210Seric register char *p; 1529210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 15353735Seric int findusercount = 0; 154*67264Seric int i; 155*67264Seric char *buf; 156*67264Seric char buf0[MAXNAME]; /* unquoted image of the user name */ 15758247Seric extern int safefile(); 1584174Seric 15955012Seric e->e_to = a->q_paddr; 1604600Seric m = a->q_mailer; 1614174Seric errno = 0; 1627676Seric if (tTd(26, 1)) 1634444Seric { 1644444Seric printf("\nrecipient: "); 1654444Seric printaddr(a, FALSE); 1664444Seric } 1674174Seric 16864146Seric /* if this is primary, add it to the original recipient list */ 16964146Seric if (a->q_alias == NULL) 17064146Seric { 17164146Seric if (e->e_origrcpt == NULL) 17264146Seric e->e_origrcpt = a->q_paddr; 17364146Seric else if (e->e_origrcpt != a->q_paddr) 17464146Seric e->e_origrcpt = ""; 17564146Seric } 17664146Seric 1774174Seric /* break aliasing loops */ 1784174Seric if (AliasLevel > MAXRCRSN) 1794174Seric { 18058151Seric usrerr("554 aliasing/forwarding loop broken"); 18112613Seric return (a); 1824174Seric } 1834174Seric 1844174Seric /* 1854627Seric ** Finish setting up address structure. 1864174Seric */ 1874174Seric 18816160Seric /* set the queue timeout */ 18958737Seric a->q_timeout = TimeOuts.to_q_return; 1904627Seric 19116160Seric /* get unquoted user for file, program or user.name check */ 192*67264Seric i = strlen(a->q_user); 193*67264Seric if (i >= sizeof buf) 194*67264Seric buf = xalloc(i + 1); 195*67264Seric else 196*67264Seric buf = buf0; 1979210Seric (void) strcpy(buf, a->q_user); 1989210Seric for (p = buf; *p != '\0' && !quoted; p++) 1999210Seric { 20054993Seric if (*p == '\\') 2019210Seric quoted = TRUE; 2029210Seric } 20354983Seric stripquotes(buf); 2049210Seric 20557402Seric /* check for direct mailing to restricted mailers */ 20665496Seric if (m == ProgMailer) 2074174Seric { 20865496Seric if (a->q_alias == NULL) 20965496Seric { 21065496Seric a->q_flags |= QBADADDR; 21165496Seric usrerr("550 Cannot mail directly to programs"); 21265496Seric } 21365496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 21465496Seric { 21565496Seric a->q_flags |= QBADADDR; 21665496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs", 21765496Seric a->q_alias->q_ruser, MyHostName); 21865496Seric } 21965496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 22065496Seric { 22165496Seric a->q_flags |= QBADADDR; 22265496Seric usrerr("550 Address %s is unsafe for mailing to programs", 22365496Seric a->q_alias->q_paddr); 22465496Seric } 2254174Seric } 2264174Seric 2274174Seric /* 2284419Seric ** Look up this person in the recipient list. 2294419Seric ** If they are there already, return, otherwise continue. 2304419Seric ** If the list is empty, just add it. Notice the cute 2314419Seric ** hack to make from addresses suppress things correctly: 2324419Seric ** the QDONTSEND bit will be set in the send list. 2334419Seric ** [Please note: the emphasis is on "hack."] 2344174Seric */ 2354174Seric 2365006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2374174Seric { 23858294Seric if (sameaddr(q, a)) 2394174Seric { 2407676Seric if (tTd(26, 1)) 2414444Seric { 2424444Seric printf("%s in sendq: ", a->q_paddr); 2434444Seric printaddr(q, FALSE); 2444444Seric } 24565593Seric if (!bitset(QPRIMARY, q->q_flags)) 24658065Seric { 24765593Seric if (!bitset(QDONTSEND, a->q_flags)) 24858151Seric message("duplicate suppressed"); 24965593Seric q->q_flags |= a->q_flags; 25065593Seric } 25165593Seric else if (bitset(QSELFREF, q->q_flags)) 25265579Seric q->q_flags |= a->q_flags & ~QDONTSEND; 25363847Seric a = q; 25463847Seric goto testselfdestruct; 2554174Seric } 2564319Seric } 2574174Seric 2584319Seric /* add address on list */ 25958884Seric *pq = a; 26058884Seric a->q_next = NULL; 2614174Seric 2624174Seric /* 26357402Seric ** Alias the name and handle special mailer types. 2644174Seric */ 2654174Seric 26653735Seric trylocaluser: 26755354Seric if (tTd(29, 7)) 26855354Seric printf("at trylocaluser %s\n", a->q_user); 26955354Seric 27058680Seric if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 27163847Seric goto testselfdestruct; 27257402Seric 27357402Seric if (m == InclMailer) 2744174Seric { 27557402Seric a->q_flags |= QDONTSEND; 27664761Seric if (a->q_alias == NULL) 2774174Seric { 27858680Seric a->q_flags |= QBADADDR; 27958151Seric usrerr("550 Cannot mail directly to :include:s"); 2804174Seric } 2814174Seric else 28250556Seric { 28359563Seric int ret; 28458247Seric 28558151Seric message("including file %s", a->q_user); 28659563Seric ret = include(a->q_user, FALSE, a, sendq, e); 28759563Seric if (transienterror(ret)) 28859563Seric { 28959563Seric #ifdef LOG 29059563Seric if (LogLevel > 2) 29166239Seric syslog(LOG_ERR, "%s: include %s: transient error: %s", 29266284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 29366284Seric a->q_user, errstring(ret)); 29459563Seric #endif 29563853Seric a->q_flags |= QQUEUEUP; 29665215Seric a->q_flags &= ~QDONTSEND; 29759563Seric usrerr("451 Cannot open %s: %s", 29859563Seric a->q_user, errstring(ret)); 29959563Seric } 30059563Seric else if (ret != 0) 30159563Seric { 30263938Seric a->q_flags |= QBADADDR; 30359563Seric usrerr("550 Cannot open %s: %s", 30459563Seric a->q_user, errstring(ret)); 30559563Seric } 30650556Seric } 3074174Seric } 30857642Seric else if (m == FileMailer) 3094174Seric { 3104329Seric extern bool writable(); 3114174Seric 31251317Seric /* check if writable or creatable */ 31364761Seric if (a->q_alias == NULL) 3144174Seric { 31558680Seric a->q_flags |= QBADADDR; 31658151Seric usrerr("550 Cannot mail directly to files"); 3174174Seric } 31865496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 31965496Seric { 32065496Seric a->q_flags |= QBADADDR; 32165496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to files", 32265496Seric a->q_alias->q_ruser, MyHostName); 32365496Seric } 32465496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 32565496Seric { 32665496Seric a->q_flags |= QBADADDR; 32765496Seric usrerr("550 Address %s is unsafe for mailing to files", 32865496Seric a->q_alias->q_paddr); 32965496Seric } 33065112Seric else if (!writable(buf, getctladdr(a), SFF_ANYFILE)) 33151317Seric { 33258680Seric a->q_flags |= QBADADDR; 33364771Seric giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e); 33451317Seric } 33551317Seric } 33651317Seric 33757402Seric if (m != LocalMailer) 33857642Seric { 33957642Seric if (!bitset(QDONTSEND, a->q_flags)) 34057642Seric e->e_nrcpts++; 34163847Seric goto testselfdestruct; 34257642Seric } 34357402Seric 34457402Seric /* try aliasing */ 34557402Seric alias(a, sendq, e); 34657402Seric 34757402Seric # ifdef USERDB 34857402Seric /* if not aliased, look it up in the user database */ 34958918Seric if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags)) 35057402Seric { 35157402Seric extern int udbexpand(); 35257402Seric 35357402Seric if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 35457402Seric { 35563853Seric a->q_flags |= QQUEUEUP; 35657402Seric if (e->e_message == NULL) 35757402Seric e->e_message = newstr("Deferred: user database error"); 35857402Seric # ifdef LOG 35958020Seric if (LogLevel > 8) 36059623Seric syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 36166284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 36266284Seric errstring(errno)); 36357402Seric # endif 36459615Seric message("queued (user database error): %s", 36559615Seric errstring(errno)); 36657642Seric e->e_nrcpts++; 36763847Seric goto testselfdestruct; 36857402Seric } 36957402Seric } 37057402Seric # endif 37157402Seric 37257402Seric /* if it was an alias or a UDB expansion, just return now */ 37358247Seric if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags)) 37463847Seric goto testselfdestruct; 37557402Seric 37651317Seric /* 37751317Seric ** If we have a level two config file, then pass the name through 37851317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 37951317Seric ** to send rewrite it to another mailer. This gives us a hook 38051317Seric ** after local aliasing has been done. 38151317Seric */ 38251317Seric 38351317Seric if (tTd(29, 5)) 38451317Seric { 38551317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 38651317Seric ConfigLevel, RewriteRules[5]); 38751317Seric printaddr(a, FALSE); 38851317Seric } 38951317Seric if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 39051317Seric RewriteRules[5] != NULL) 39151317Seric { 39255012Seric maplocaluser(a, sendq, e); 39351317Seric } 39451317Seric 39551317Seric /* 39651317Seric ** If it didn't get rewritten to another mailer, go ahead 39751317Seric ** and deliver it. 39851317Seric */ 39951317Seric 40058247Seric if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags)) 40151317Seric { 40255354Seric auto bool fuzzy; 40351317Seric register struct passwd *pw; 40451317Seric extern struct passwd *finduser(); 40551317Seric 40651317Seric /* warning -- finduser may trash buf */ 40755354Seric pw = finduser(buf, &fuzzy); 40851317Seric if (pw == NULL) 40951317Seric { 41058680Seric a->q_flags |= QBADADDR; 41164771Seric giveresponse(EX_NOUSER, m, NULL, a->q_alias, e); 41251317Seric } 4134174Seric else 4144174Seric { 41551317Seric char nbuf[MAXNAME]; 4164373Seric 41755354Seric if (fuzzy) 4184174Seric { 41953735Seric /* name was a fuzzy match */ 42051317Seric a->q_user = newstr(pw->pw_name); 42153735Seric if (findusercount++ > 3) 42253735Seric { 42358680Seric a->q_flags |= QBADADDR; 42458151Seric usrerr("554 aliasing/forwarding loop for %s broken", 42553735Seric pw->pw_name); 426*67264Seric goto done; 42753735Seric } 42853735Seric 42953735Seric /* see if it aliases */ 43051317Seric (void) strcpy(buf, pw->pw_name); 43153735Seric goto trylocaluser; 4324174Seric } 43365822Seric if (strcmp(pw->pw_dir, "/") == 0) 43465822Seric a->q_home = ""; 43565822Seric else 43665822Seric a->q_home = newstr(pw->pw_dir); 43751317Seric a->q_uid = pw->pw_uid; 43851317Seric a->q_gid = pw->pw_gid; 43959083Seric a->q_ruser = newstr(pw->pw_name); 44051317Seric a->q_flags |= QGOODUID; 44151317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 44251317Seric if (nbuf[0] != '\0') 44351317Seric a->q_fullname = newstr(nbuf); 44465211Seric if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' && 44565211Seric !usershellok(pw->pw_shell)) 44665206Seric { 44765211Seric a->q_flags |= QBOGUSSHELL; 44865206Seric } 44951317Seric if (!quoted) 45055012Seric forward(a, sendq, e); 4514174Seric } 4524174Seric } 45357642Seric if (!bitset(QDONTSEND, a->q_flags)) 45457642Seric e->e_nrcpts++; 45563847Seric 45663847Seric testselfdestruct: 45763978Seric if (tTd(26, 8)) 45863847Seric { 45963978Seric printf("testselfdestruct: "); 46063978Seric printaddr(a, TRUE); 46163978Seric } 46263978Seric if (a->q_alias == NULL && a != &e->e_from && 46363978Seric bitset(QDONTSEND, a->q_flags)) 46463978Seric { 46563978Seric q = *sendq; 46663965Seric while (q != NULL && bitset(QDONTSEND, q->q_flags)) 46763847Seric q = q->q_next; 46863978Seric if (q == NULL) 46963847Seric { 47063847Seric a->q_flags |= QBADADDR; 47163847Seric usrerr("554 aliasing/forwarding loop broken"); 47263847Seric } 47363847Seric } 474*67264Seric 475*67264Seric done: 476*67264Seric if (buf != buf0) 477*67264Seric free(buf); 47812613Seric return (a); 4794174Seric } 4804174Seric /* 4814373Seric ** FINDUSER -- find the password entry for a user. 4824373Seric ** 4834373Seric ** This looks a lot like getpwnam, except that it may want to 4844373Seric ** do some fancier pattern matching in /etc/passwd. 4854373Seric ** 4869379Seric ** This routine contains most of the time of many sendmail runs. 4879379Seric ** It deserves to be optimized. 4889379Seric ** 4894373Seric ** Parameters: 4904373Seric ** name -- the name to match against. 49155354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 49255354Seric ** was found using the fuzzy matching algorithm; 49355354Seric ** set to FALSE otherwise. 4944373Seric ** 4954373Seric ** Returns: 4964373Seric ** A pointer to a pw struct. 4974373Seric ** NULL if name is unknown or ambiguous. 4984373Seric ** 4994373Seric ** Side Effects: 5004407Seric ** may modify name. 5014373Seric */ 5024373Seric 5034373Seric struct passwd * 50455354Seric finduser(name, fuzzyp) 5054373Seric char *name; 50655354Seric bool *fuzzyp; 5074373Seric { 5084376Seric register struct passwd *pw; 5094407Seric register char *p; 51015325Seric extern struct passwd *getpwent(); 51115325Seric extern struct passwd *getpwnam(); 5124373Seric 51355354Seric if (tTd(29, 4)) 51455354Seric printf("finduser(%s): ", name); 51555354Seric 51655354Seric *fuzzyp = FALSE; 5174407Seric 51864673Seric /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */ 51964673Seric for (p = name; *p != '\0'; p++) 52064673Seric if (!isascii(*p) || !isdigit(*p)) 52164673Seric break; 52264673Seric if (*p == '\0') 52364673Seric { 52464673Seric if (tTd(29, 4)) 52564673Seric printf("failed (numeric input)\n"); 52664673Seric return NULL; 52764673Seric } 52864673Seric 52925777Seric /* look up this login name using fast path */ 53012634Seric if ((pw = getpwnam(name)) != NULL) 53155354Seric { 53255354Seric if (tTd(29, 4)) 53355354Seric printf("found (non-fuzzy)\n"); 53412634Seric return (pw); 53555354Seric } 53612634Seric 53753735Seric #ifdef MATCHGECOS 53853735Seric /* see if fuzzy matching allowed */ 53953735Seric if (!MatchGecos) 54055354Seric { 54155354Seric if (tTd(29, 4)) 54255354Seric printf("not found (fuzzy disabled)\n"); 54353735Seric return NULL; 54455354Seric } 54553735Seric 54612634Seric /* search for a matching full name instead */ 54725777Seric for (p = name; *p != '\0'; p++) 54825777Seric { 54925777Seric if (*p == (SpaceSub & 0177) || *p == '_') 55025777Seric *p = ' '; 55125777Seric } 55223107Seric (void) setpwent(); 5534376Seric while ((pw = getpwent()) != NULL) 5544376Seric { 5554998Seric char buf[MAXNAME]; 5564376Seric 5574998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 55856795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5594381Seric { 56055354Seric if (tTd(29, 4)) 56155354Seric printf("fuzzy matches %s\n", pw->pw_name); 56258151Seric message("sending to login name %s", pw->pw_name); 56355354Seric *fuzzyp = TRUE; 5644376Seric return (pw); 5654377Seric } 5664376Seric } 56755354Seric if (tTd(29, 4)) 56855354Seric printf("no fuzzy match found\n"); 56959015Seric #else 57059015Seric if (tTd(29, 4)) 57159015Seric printf("not found (fuzzy disabled)\n"); 57259015Seric #endif 5734376Seric return (NULL); 5744373Seric } 5754373Seric /* 5764329Seric ** WRITABLE -- predicate returning if the file is writable. 5774329Seric ** 5784329Seric ** This routine must duplicate the algorithm in sys/fio.c. 5794329Seric ** Unfortunately, we cannot use the access call since we 5804329Seric ** won't necessarily be the real uid when we try to 5814329Seric ** actually open the file. 5824329Seric ** 5834329Seric ** Notice that ANY file with ANY execute bit is automatically 5844329Seric ** not writable. This is also enforced by mailfile. 5854329Seric ** 5864329Seric ** Parameters: 58765064Seric ** filename -- the file name to check. 58865112Seric ** ctladdr -- the controlling address for this file. 58965064Seric ** flags -- SFF_* flags to control the function. 5904329Seric ** 5914329Seric ** Returns: 5924329Seric ** TRUE -- if we will be able to write this file. 5934329Seric ** FALSE -- if we cannot write this file. 5944329Seric ** 5954329Seric ** Side Effects: 5964329Seric ** none. 5974329Seric */ 5984329Seric 5994329Seric bool 60065112Seric writable(filename, ctladdr, flags) 60164819Seric char *filename; 60265112Seric ADDRESS *ctladdr; 60365064Seric int flags; 6044329Seric { 60555372Seric uid_t euid; 60655372Seric gid_t egid; 6074329Seric int bits; 60864944Seric register char *p; 60964944Seric char *uname; 61064944Seric struct stat stb; 61164944Seric extern char RealUserName[]; 6124329Seric 61364819Seric if (tTd(29, 5)) 61465064Seric printf("writable(%s, %x)\n", filename, flags); 61564944Seric 61664944Seric #ifdef HASLSTAT 61765064Seric if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb) 61865064Seric : stat(filename, &stb)) < 0) 61964944Seric #else 62064944Seric if (stat(filename, &stb) < 0) 62164944Seric #endif 62264944Seric { 62364944Seric /* file does not exist -- see if directory is safe */ 62464944Seric p = strrchr(filename, '/'); 62564944Seric if (p == NULL) 62664944Seric { 62765067Seric errno = ENOTDIR; 62864944Seric return FALSE; 62964944Seric } 63065067Seric *p = '\0'; 63165067Seric errno = safefile(filename, RealUid, RealGid, RealUserName, 63265067Seric SFF_MUSTOWN, S_IWRITE|S_IEXEC); 63364944Seric *p = '/'; 63465067Seric return errno == 0; 63564944Seric } 63664944Seric 63765225Seric #ifdef SUID_ROOT_FILES_OK 63865225Seric /* really ought to be passed down -- and not a good idea */ 63965225Seric flags |= SFF_ROOTOK; 64065225Seric #endif 64165225Seric 64264944Seric /* 64364944Seric ** File does exist -- check that it is writable. 64464944Seric */ 64564944Seric 64664944Seric if (bitset(0111, stb.st_mode)) 64765022Seric { 64865022Seric if (tTd(29, 5)) 64965022Seric printf("failed (mode %o: x bits)\n", stb.st_mode); 65065067Seric errno = EPERM; 6514329Seric return (FALSE); 65265022Seric } 65364944Seric 65465112Seric if (ctladdr != NULL && geteuid() == 0) 65564944Seric { 65665112Seric euid = ctladdr->q_uid; 65765112Seric egid = ctladdr->q_gid; 65865112Seric uname = ctladdr->q_user; 65964944Seric } 66065112Seric else 66165112Seric { 66265112Seric euid = RealUid; 66365112Seric egid = RealGid; 66465112Seric uname = RealUserName; 66565112Seric } 66665138Seric if (euid == 0) 66765138Seric { 66865138Seric euid = DefUid; 66965138Seric uname = DefUser; 67065138Seric } 67165138Seric if (egid == 0) 67265138Seric egid = DefGid; 6734329Seric if (geteuid() == 0) 6744329Seric { 67565225Seric if (bitset(S_ISUID, stb.st_mode) && 67665225Seric (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags))) 67764944Seric { 67864944Seric euid = stb.st_uid; 67964944Seric uname = NULL; 68064944Seric } 68165225Seric if (bitset(S_ISGID, stb.st_mode) && 68265225Seric (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags))) 68364944Seric egid = stb.st_gid; 6844329Seric } 6854329Seric 68664819Seric if (tTd(29, 5)) 68764819Seric printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n", 68864944Seric euid, egid, stb.st_uid, stb.st_gid); 68964819Seric 69065067Seric errno = safefile(filename, euid, egid, uname, flags, S_IWRITE); 69165067Seric return errno == 0; 6924329Seric } 6934329Seric /* 6944174Seric ** INCLUDE -- handle :include: specification. 6954174Seric ** 6964174Seric ** Parameters: 6974174Seric ** fname -- filename to include. 69853037Seric ** forwarding -- if TRUE, we are reading a .forward file. 69953037Seric ** if FALSE, it's a :include: file. 7004399Seric ** ctladdr -- address template to use to fill in these 7014399Seric ** addresses -- effective user/group id are 7024399Seric ** the important things. 7035006Seric ** sendq -- a pointer to the head of the send queue 7045006Seric ** to put these addresses in. 7054174Seric ** 7064174Seric ** Returns: 70757136Seric ** open error status 7084174Seric ** 7094174Seric ** Side Effects: 7104174Seric ** reads the :include: file and sends to everyone 7114174Seric ** listed in that file. 71265909Seric ** 71365909Seric ** Security Note: 71465909Seric ** If you have restricted chown (that is, you can't 71565909Seric ** give a file away), it is reasonable to allow programs 71665909Seric ** and files called from this :include: file to be to be 71765909Seric ** run as the owner of the :include: file. This is bogus 71865909Seric ** if there is any chance of someone giving away a file. 71965909Seric ** We assume that pre-POSIX systems can give away files. 72065909Seric ** 72165909Seric ** There is an additional restriction that if you 72265909Seric ** forward to a :include: file, it will not take on 72365909Seric ** the ownership of the :include: file. This may not 72465909Seric ** be necessary, but shouldn't hurt. 7254174Seric */ 7264174Seric 72753037Seric static jmp_buf CtxIncludeTimeout; 72863937Seric static int includetimeout(); 72953037Seric 73065496Seric #ifndef S_IWOTH 73165496Seric # define S_IWOTH (S_IWRITE >> 6) 73265496Seric #endif 73365496Seric 73457136Seric int 73555012Seric include(fname, forwarding, ctladdr, sendq, e) 7364174Seric char *fname; 73753037Seric bool forwarding; 7384399Seric ADDRESS *ctladdr; 7395006Seric ADDRESS **sendq; 74055012Seric ENVELOPE *e; 7414174Seric { 74264570Seric register FILE *fp = NULL; 74355012Seric char *oldto = e->e_to; 7449379Seric char *oldfilename = FileName; 7459379Seric int oldlinenumber = LineNumber; 74653037Seric register EVENT *ev = NULL; 74758082Seric int nincludes; 74864325Seric register ADDRESS *ca; 74964325Seric uid_t saveduid, uid; 75064325Seric gid_t savedgid, gid; 75164083Seric char *uname; 75264325Seric int rval = 0; 75365064Seric int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE; 75465496Seric struct stat st; 75565948Seric char buf[MAXLINE]; 75665909Seric #ifdef _POSIX_CHOWN_RESTRICTED 75765948Seric # if _POSIX_CHOWN_RESTRICTED == -1 75865948Seric # define safechown FALSE 75965948Seric # else 76065948Seric # define safechown TRUE 76165948Seric # endif 76265948Seric #else 76365948Seric # ifdef _PC_CHOWN_RESTRICTED 76465909Seric bool safechown; 76565948Seric # else 76665948Seric # ifdef BSD 76765948Seric # define safechown TRUE 76865948Seric # else 76965948Seric # define safechown FALSE 77065948Seric # endif 77165948Seric # endif 77265909Seric #endif 77365948Seric extern bool chownsafe(); 7744174Seric 77557186Seric if (tTd(27, 2)) 77657186Seric printf("include(%s)\n", fname); 77763902Seric if (tTd(27, 4)) 77863902Seric printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 77963581Seric if (tTd(27, 14)) 78063581Seric { 78163581Seric printf("ctladdr "); 78263581Seric printaddr(ctladdr, FALSE); 78363581Seric } 78457186Seric 78564325Seric if (tTd(27, 9)) 78664325Seric printf("include: old uid = %d/%d\n", getuid(), geteuid()); 78753037Seric 78863581Seric ca = getctladdr(ctladdr); 78963581Seric if (ca == NULL) 79064083Seric { 79164846Seric uid = DefUid; 79264846Seric gid = DefGid; 79364846Seric uname = DefUser; 79464325Seric saveduid = -1; 79564083Seric } 79663581Seric else 79764083Seric { 79863581Seric uid = ca->q_uid; 79964083Seric gid = ca->q_gid; 80064083Seric uname = ca->q_user; 80164325Seric #ifdef HASSETREUID 80264325Seric saveduid = geteuid(); 80364325Seric savedgid = getegid(); 80464325Seric if (saveduid == 0) 80564325Seric { 80664325Seric initgroups(uname, gid); 80764325Seric if (uid != 0) 80864325Seric (void) setreuid(0, uid); 80964325Seric } 81064325Seric #endif 81164083Seric } 81263581Seric 81364325Seric if (tTd(27, 9)) 81464325Seric printf("include: new uid = %d/%d\n", getuid(), geteuid()); 81564325Seric 81664325Seric /* 81764325Seric ** If home directory is remote mounted but server is down, 81864325Seric ** this can hang or give errors; use a timeout to avoid this 81964325Seric */ 82064325Seric 82153037Seric if (setjmp(CtxIncludeTimeout) != 0) 82253037Seric { 82363853Seric ctladdr->q_flags |= QQUEUEUP; 82453037Seric errno = 0; 82563993Seric 82663993Seric /* return pseudo-error code */ 82764325Seric rval = EOPENTIMEOUT; 82864325Seric goto resetuid; 82953037Seric } 83053037Seric ev = setevent((time_t) 60, includetimeout, 0); 83153037Seric 83263581Seric /* the input file must be marked safe */ 83364944Seric rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD); 83464329Seric if (rval != 0) 83553037Seric { 83664325Seric /* don't use this :include: file */ 83757186Seric if (tTd(27, 4)) 83858247Seric printf("include: not safe (uid=%d): %s\n", 83964329Seric uid, errstring(rval)); 84053037Seric } 84165496Seric else 8424174Seric { 84365496Seric fp = fopen(fname, "r"); 84465496Seric if (fp == NULL) 84558061Seric { 84664329Seric rval = errno; 84765496Seric if (tTd(27, 4)) 84865496Seric printf("include: open: %s\n", errstring(rval)); 84958061Seric } 8504406Seric } 85153037Seric clrevent(ev); 85253037Seric 85364570Seric resetuid: 85464570Seric 85564570Seric #ifdef HASSETREUID 85664570Seric if (saveduid == 0) 85764570Seric { 85864570Seric if (uid != 0) 85964570Seric if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0) 86064570Seric syserr("setreuid(%d, 0) failure (real=%d, eff=%d)", 86164570Seric RealUid, getuid(), geteuid()); 86264570Seric setgid(savedgid); 86364570Seric } 86464570Seric #endif 86564570Seric 86664570Seric if (tTd(27, 9)) 86764570Seric printf("include: reset uid = %d/%d\n", getuid(), geteuid()); 86864570Seric 86965593Seric if (rval == EOPENTIMEOUT) 87065593Seric usrerr("451 open timeout on %s", fname); 87165593Seric 87264570Seric if (fp == NULL) 87364570Seric return rval; 87464570Seric 87565496Seric if (fstat(fileno(fp), &st) < 0) 87665496Seric { 87765496Seric rval = errno; 87865496Seric syserr("Cannot fstat %s!", fname); 87965496Seric return rval; 88065496Seric } 88165496Seric 88265948Seric #ifndef safechown 88365948Seric safechown = chownsafe(fileno(fp)); 88465948Seric #endif 88565909Seric if (ca == NULL && safechown) 88665496Seric { 88765496Seric ctladdr->q_uid = st.st_uid; 88865496Seric ctladdr->q_gid = st.st_gid; 88965496Seric ctladdr->q_flags |= QGOODUID; 89065496Seric } 89165496Seric if (ca != NULL && ca->q_uid == st.st_uid) 89265496Seric { 89365496Seric /* optimization -- avoid getpwuid if we already have info */ 89465496Seric ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL; 89565496Seric ctladdr->q_ruser = ca->q_ruser; 89665496Seric } 89765496Seric else 89865496Seric { 89965909Seric char *sh; 90065496Seric register struct passwd *pw; 90165496Seric 90265909Seric sh = "/SENDMAIL/ANY/SHELL/"; 90365496Seric pw = getpwuid(st.st_uid); 90465909Seric if (pw != NULL) 90565496Seric { 90665496Seric ctladdr->q_ruser = newstr(pw->pw_name); 90765909Seric if (safechown) 90865909Seric sh = pw->pw_shell; 90965909Seric } 91065909Seric if (pw == NULL) 91165496Seric ctladdr->q_flags |= QBOGUSSHELL; 91265909Seric else if(!usershellok(sh)) 91365909Seric { 91465909Seric if (safechown) 91565909Seric ctladdr->q_flags |= QBOGUSSHELL; 91665909Seric else 91765909Seric ctladdr->q_flags |= QUNSAFEADDR; 91865496Seric } 91965496Seric } 92065496Seric 92158092Seric if (bitset(EF_VRFYONLY, e->e_flags)) 92258092Seric { 92358092Seric /* don't do any more now */ 92458868Seric ctladdr->q_flags |= QVERIFIED; 92558884Seric e->e_nrcpts++; 92658680Seric xfclose(fp, "include", fname); 92764570Seric return rval; 92858092Seric } 92958092Seric 93065496Seric /* 93165496Seric ** Check to see if some bad guy can write this file 93265496Seric ** 93365496Seric ** This should really do something clever with group 93465496Seric ** permissions; currently we just view world writable 93565496Seric ** as unsafe. Also, we don't check for writable 93665496Seric ** directories in the path. We've got to leave 93765496Seric ** something for the local sysad to do. 93865496Seric */ 93965496Seric 94065496Seric if (bitset(S_IWOTH, st.st_mode)) 94165496Seric ctladdr->q_flags |= QUNSAFEADDR; 94265496Seric 9434174Seric /* read the file -- each line is a comma-separated list. */ 9449379Seric FileName = fname; 9459379Seric LineNumber = 0; 94658082Seric ctladdr->q_flags &= ~QSELFREF; 94758082Seric nincludes = 0; 9484174Seric while (fgets(buf, sizeof buf, fp) != NULL) 9494174Seric { 95056795Seric register char *p = strchr(buf, '\n'); 9514174Seric 95240963Sbostic LineNumber++; 9534174Seric if (p != NULL) 9544174Seric *p = '\0'; 95557186Seric if (buf[0] == '#' || buf[0] == '\0') 95657139Seric continue; 95758008Seric e->e_to = NULL; 95858151Seric message("%s to %s", 95953037Seric forwarding ? "forwarding" : "sending", buf); 96057977Seric #ifdef LOG 96158020Seric if (forwarding && LogLevel > 9) 96257977Seric syslog(LOG_INFO, "%s: forward %s => %s", 96366284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 96466284Seric oldto, buf); 96557977Seric #endif 96657977Seric 9674176Seric AliasLevel++; 96858082Seric nincludes += sendtolist(buf, ctladdr, sendq, e); 9694176Seric AliasLevel--; 9704174Seric } 97163902Seric 97263902Seric if (ferror(fp) && tTd(27, 3)) 97363902Seric printf("include: read error: %s\n", errstring(errno)); 97458082Seric if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 97558065Seric { 97658065Seric if (tTd(27, 5)) 97758065Seric { 97858065Seric printf("include: QDONTSEND "); 97958065Seric printaddr(ctladdr, FALSE); 98058065Seric } 98158065Seric ctladdr->q_flags |= QDONTSEND; 98258065Seric } 9834174Seric 98458680Seric (void) xfclose(fp, "include", fname); 9859379Seric FileName = oldfilename; 9869379Seric LineNumber = oldlinenumber; 98763847Seric e->e_to = oldto; 98864325Seric return rval; 9894174Seric } 99053037Seric 99153037Seric static 99253037Seric includetimeout() 99353037Seric { 99453037Seric longjmp(CtxIncludeTimeout, 1); 99553037Seric } 9964324Seric /* 9974324Seric ** SENDTOARGV -- send to an argument vector. 9984324Seric ** 9994324Seric ** Parameters: 10004324Seric ** argv -- argument vector to send to. 100158247Seric ** e -- the current envelope. 10024324Seric ** 10034324Seric ** Returns: 10044324Seric ** none. 10054324Seric ** 10064324Seric ** Side Effects: 10074324Seric ** puts all addresses on the argument vector onto the 10084324Seric ** send queue. 10094324Seric */ 10104324Seric 101155012Seric sendtoargv(argv, e) 10124324Seric register char **argv; 101355012Seric register ENVELOPE *e; 10144324Seric { 10154324Seric register char *p; 10164324Seric 10174324Seric while ((p = *argv++) != NULL) 10184324Seric { 101964284Seric (void) sendtolist(p, NULLADDR, &e->e_sendqueue, e); 10204324Seric } 10214324Seric } 10224399Seric /* 10234399Seric ** GETCTLADDR -- get controlling address from an address header. 10244399Seric ** 10254399Seric ** If none, get one corresponding to the effective userid. 10264399Seric ** 10274399Seric ** Parameters: 10284399Seric ** a -- the address to find the controller of. 10294399Seric ** 10304399Seric ** Returns: 10314399Seric ** the controlling address. 10324399Seric ** 10334399Seric ** Side Effects: 10344399Seric ** none. 10354399Seric */ 10364399Seric 10374399Seric ADDRESS * 10384399Seric getctladdr(a) 10394399Seric register ADDRESS *a; 10404399Seric { 10414404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 10424399Seric a = a->q_alias; 10434399Seric return (a); 10444399Seric } 1045