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*67839Seric static char sccsid[] = "@(#)recipient.c 8.50 (Berkeley) 10/20/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; 15467264Seric int i; 15567264Seric char *buf; 15667264Seric 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 /* get unquoted user for file, program or user.name check */ 18967264Seric i = strlen(a->q_user); 19067264Seric if (i >= sizeof buf) 19167264Seric buf = xalloc(i + 1); 19267264Seric else 19367264Seric buf = buf0; 1949210Seric (void) strcpy(buf, a->q_user); 1959210Seric for (p = buf; *p != '\0' && !quoted; p++) 1969210Seric { 19754993Seric if (*p == '\\') 1989210Seric quoted = TRUE; 1999210Seric } 20054983Seric stripquotes(buf); 2019210Seric 20257402Seric /* check for direct mailing to restricted mailers */ 20365496Seric if (m == ProgMailer) 2044174Seric { 20565496Seric if (a->q_alias == NULL) 20665496Seric { 20765496Seric a->q_flags |= QBADADDR; 20865496Seric usrerr("550 Cannot mail directly to programs"); 20965496Seric } 21065496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 21165496Seric { 21265496Seric a->q_flags |= QBADADDR; 21365496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs", 21465496Seric a->q_alias->q_ruser, MyHostName); 21565496Seric } 21665496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 21765496Seric { 21865496Seric a->q_flags |= QBADADDR; 21965496Seric usrerr("550 Address %s is unsafe for mailing to programs", 22065496Seric a->q_alias->q_paddr); 22165496Seric } 2224174Seric } 2234174Seric 2244174Seric /* 2254419Seric ** Look up this person in the recipient list. 2264419Seric ** If they are there already, return, otherwise continue. 2274419Seric ** If the list is empty, just add it. Notice the cute 2284419Seric ** hack to make from addresses suppress things correctly: 2294419Seric ** the QDONTSEND bit will be set in the send list. 2304419Seric ** [Please note: the emphasis is on "hack."] 2314174Seric */ 2324174Seric 2335006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2344174Seric { 23558294Seric if (sameaddr(q, a)) 2364174Seric { 2377676Seric if (tTd(26, 1)) 2384444Seric { 2394444Seric printf("%s in sendq: ", a->q_paddr); 2404444Seric printaddr(q, FALSE); 2414444Seric } 24265593Seric if (!bitset(QPRIMARY, q->q_flags)) 24358065Seric { 24465593Seric if (!bitset(QDONTSEND, a->q_flags)) 24558151Seric message("duplicate suppressed"); 24665593Seric q->q_flags |= a->q_flags; 24765593Seric } 24865593Seric else if (bitset(QSELFREF, q->q_flags)) 24965579Seric q->q_flags |= a->q_flags & ~QDONTSEND; 25063847Seric a = q; 25163847Seric goto testselfdestruct; 2524174Seric } 2534319Seric } 2544174Seric 2554319Seric /* add address on list */ 25658884Seric *pq = a; 25758884Seric a->q_next = NULL; 2584174Seric 2594174Seric /* 26057402Seric ** Alias the name and handle special mailer types. 2614174Seric */ 2624174Seric 26353735Seric trylocaluser: 26455354Seric if (tTd(29, 7)) 26555354Seric printf("at trylocaluser %s\n", a->q_user); 26655354Seric 26758680Seric if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 26863847Seric goto testselfdestruct; 26957402Seric 27057402Seric if (m == InclMailer) 2714174Seric { 27257402Seric a->q_flags |= QDONTSEND; 27364761Seric if (a->q_alias == NULL) 2744174Seric { 27558680Seric a->q_flags |= QBADADDR; 27658151Seric usrerr("550 Cannot mail directly to :include:s"); 2774174Seric } 2784174Seric else 27950556Seric { 28059563Seric int ret; 28158247Seric 28258151Seric message("including file %s", a->q_user); 28359563Seric ret = include(a->q_user, FALSE, a, sendq, e); 28459563Seric if (transienterror(ret)) 28559563Seric { 28659563Seric #ifdef LOG 28759563Seric if (LogLevel > 2) 28866239Seric syslog(LOG_ERR, "%s: include %s: transient error: %s", 28966284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 29066284Seric a->q_user, errstring(ret)); 29159563Seric #endif 29263853Seric a->q_flags |= QQUEUEUP; 29365215Seric a->q_flags &= ~QDONTSEND; 29459563Seric usrerr("451 Cannot open %s: %s", 29559563Seric a->q_user, errstring(ret)); 29659563Seric } 29759563Seric else if (ret != 0) 29859563Seric { 29963938Seric a->q_flags |= QBADADDR; 30059563Seric usrerr("550 Cannot open %s: %s", 30159563Seric a->q_user, errstring(ret)); 30259563Seric } 30350556Seric } 3044174Seric } 30557642Seric else if (m == FileMailer) 3064174Seric { 3074329Seric extern bool writable(); 3084174Seric 30951317Seric /* check if writable or creatable */ 31064761Seric if (a->q_alias == NULL) 3114174Seric { 31258680Seric a->q_flags |= QBADADDR; 31358151Seric usrerr("550 Cannot mail directly to files"); 3144174Seric } 31565496Seric else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 31665496Seric { 31765496Seric a->q_flags |= QBADADDR; 31865496Seric usrerr("550 User %s@%s doesn't have a valid shell for mailing to files", 31965496Seric a->q_alias->q_ruser, MyHostName); 32065496Seric } 32165496Seric else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 32265496Seric { 32365496Seric a->q_flags |= QBADADDR; 32465496Seric usrerr("550 Address %s is unsafe for mailing to files", 32565496Seric a->q_alias->q_paddr); 32665496Seric } 32765112Seric else if (!writable(buf, getctladdr(a), SFF_ANYFILE)) 32851317Seric { 32958680Seric a->q_flags |= QBADADDR; 33064771Seric giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e); 33151317Seric } 33251317Seric } 33351317Seric 33457402Seric /* try aliasing */ 33567472Seric if (!bitset(QDONTSEND, a->q_flags) && bitnset(M_ALIASABLE, m->m_flags)) 33667472Seric alias(a, sendq, e); 33757402Seric 33857402Seric # ifdef USERDB 33957402Seric /* if not aliased, look it up in the user database */ 34067472Seric if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags) && 34167472Seric bitnset(M_CHECKUDB, m->m_flags)) 34257402Seric { 34357402Seric extern int udbexpand(); 34457402Seric 34557402Seric if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 34657402Seric { 34763853Seric a->q_flags |= QQUEUEUP; 34857402Seric if (e->e_message == NULL) 34957402Seric e->e_message = newstr("Deferred: user database error"); 35057402Seric # ifdef LOG 35158020Seric if (LogLevel > 8) 35259623Seric syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 35366284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 35466284Seric errstring(errno)); 35557402Seric # endif 35659615Seric message("queued (user database error): %s", 35759615Seric errstring(errno)); 35857642Seric e->e_nrcpts++; 35963847Seric goto testselfdestruct; 36057402Seric } 36157402Seric } 36257402Seric # endif 36357402Seric 36451317Seric /* 36551317Seric ** If we have a level two config file, then pass the name through 36651317Seric ** Ruleset 5 before sending it off. Ruleset 5 has the right 36751317Seric ** to send rewrite it to another mailer. This gives us a hook 36851317Seric ** after local aliasing has been done. 36951317Seric */ 37051317Seric 37151317Seric if (tTd(29, 5)) 37251317Seric { 37351317Seric printf("recipient: testing local? cl=%d, rr5=%x\n\t", 37451317Seric ConfigLevel, RewriteRules[5]); 37551317Seric printaddr(a, FALSE); 37651317Seric } 37767472Seric if (!bitset(QNOTREMOTE|QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags) && 37867472Seric ConfigLevel >= 2 && RewriteRules[5] != NULL && 37967472Seric bitnset(M_TRYRULESET5, m->m_flags)) 38051317Seric { 38155012Seric maplocaluser(a, sendq, e); 38251317Seric } 38351317Seric 38451317Seric /* 38551317Seric ** If it didn't get rewritten to another mailer, go ahead 38651317Seric ** and deliver it. 38751317Seric */ 38851317Seric 38967472Seric if (!bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags) && 39067472Seric bitnset(M_HASPWENT, m->m_flags)) 39151317Seric { 39255354Seric auto bool fuzzy; 39351317Seric register struct passwd *pw; 39451317Seric extern struct passwd *finduser(); 39551317Seric 39651317Seric /* warning -- finduser may trash buf */ 39755354Seric pw = finduser(buf, &fuzzy); 39851317Seric if (pw == NULL) 39951317Seric { 40058680Seric a->q_flags |= QBADADDR; 40164771Seric giveresponse(EX_NOUSER, m, NULL, a->q_alias, e); 40251317Seric } 4034174Seric else 4044174Seric { 40551317Seric char nbuf[MAXNAME]; 4064373Seric 40755354Seric if (fuzzy) 4084174Seric { 40953735Seric /* name was a fuzzy match */ 41051317Seric a->q_user = newstr(pw->pw_name); 41153735Seric if (findusercount++ > 3) 41253735Seric { 41358680Seric a->q_flags |= QBADADDR; 41458151Seric usrerr("554 aliasing/forwarding loop for %s broken", 41553735Seric pw->pw_name); 41667264Seric goto done; 41753735Seric } 41853735Seric 41953735Seric /* see if it aliases */ 42051317Seric (void) strcpy(buf, pw->pw_name); 42153735Seric goto trylocaluser; 4224174Seric } 42365822Seric if (strcmp(pw->pw_dir, "/") == 0) 42465822Seric a->q_home = ""; 42565822Seric else 42665822Seric a->q_home = newstr(pw->pw_dir); 42751317Seric a->q_uid = pw->pw_uid; 42851317Seric a->q_gid = pw->pw_gid; 42959083Seric a->q_ruser = newstr(pw->pw_name); 43051317Seric a->q_flags |= QGOODUID; 43151317Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 43251317Seric if (nbuf[0] != '\0') 43351317Seric a->q_fullname = newstr(nbuf); 43465211Seric if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' && 43565211Seric !usershellok(pw->pw_shell)) 43665206Seric { 43765211Seric a->q_flags |= QBOGUSSHELL; 43865206Seric } 43951317Seric if (!quoted) 44055012Seric forward(a, sendq, e); 4414174Seric } 4424174Seric } 44357642Seric if (!bitset(QDONTSEND, a->q_flags)) 44457642Seric e->e_nrcpts++; 44563847Seric 44663847Seric testselfdestruct: 44763978Seric if (tTd(26, 8)) 44863847Seric { 44963978Seric printf("testselfdestruct: "); 45063978Seric printaddr(a, TRUE); 45163978Seric } 45263978Seric if (a->q_alias == NULL && a != &e->e_from && 45363978Seric bitset(QDONTSEND, a->q_flags)) 45463978Seric { 45563978Seric q = *sendq; 45663965Seric while (q != NULL && bitset(QDONTSEND, q->q_flags)) 45763847Seric q = q->q_next; 45863978Seric if (q == NULL) 45963847Seric { 46063847Seric a->q_flags |= QBADADDR; 46163847Seric usrerr("554 aliasing/forwarding loop broken"); 46263847Seric } 46363847Seric } 46467264Seric 46567264Seric done: 46667264Seric if (buf != buf0) 46767264Seric free(buf); 46812613Seric return (a); 4694174Seric } 4704174Seric /* 4714373Seric ** FINDUSER -- find the password entry for a user. 4724373Seric ** 4734373Seric ** This looks a lot like getpwnam, except that it may want to 4744373Seric ** do some fancier pattern matching in /etc/passwd. 4754373Seric ** 4769379Seric ** This routine contains most of the time of many sendmail runs. 4779379Seric ** It deserves to be optimized. 4789379Seric ** 4794373Seric ** Parameters: 4804373Seric ** name -- the name to match against. 48155354Seric ** fuzzyp -- an outarg that is set to TRUE if this entry 48255354Seric ** was found using the fuzzy matching algorithm; 48355354Seric ** set to FALSE otherwise. 4844373Seric ** 4854373Seric ** Returns: 4864373Seric ** A pointer to a pw struct. 4874373Seric ** NULL if name is unknown or ambiguous. 4884373Seric ** 4894373Seric ** Side Effects: 4904407Seric ** may modify name. 4914373Seric */ 4924373Seric 4934373Seric struct passwd * 49455354Seric finduser(name, fuzzyp) 4954373Seric char *name; 49655354Seric bool *fuzzyp; 4974373Seric { 4984376Seric register struct passwd *pw; 4994407Seric register char *p; 50015325Seric extern struct passwd *getpwent(); 50115325Seric extern struct passwd *getpwnam(); 5024373Seric 50355354Seric if (tTd(29, 4)) 50455354Seric printf("finduser(%s): ", name); 50555354Seric 50655354Seric *fuzzyp = FALSE; 5074407Seric 508*67839Seric #ifdef HESIOD 50964673Seric /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */ 51064673Seric for (p = name; *p != '\0'; p++) 51164673Seric if (!isascii(*p) || !isdigit(*p)) 51264673Seric break; 51364673Seric if (*p == '\0') 51464673Seric { 51564673Seric if (tTd(29, 4)) 51664673Seric printf("failed (numeric input)\n"); 51764673Seric return NULL; 51864673Seric } 519*67839Seric #endif 52064673Seric 52125777Seric /* look up this login name using fast path */ 52212634Seric if ((pw = getpwnam(name)) != NULL) 52355354Seric { 52455354Seric if (tTd(29, 4)) 52555354Seric printf("found (non-fuzzy)\n"); 52612634Seric return (pw); 52755354Seric } 52812634Seric 52953735Seric #ifdef MATCHGECOS 53053735Seric /* see if fuzzy matching allowed */ 53153735Seric if (!MatchGecos) 53255354Seric { 53355354Seric if (tTd(29, 4)) 53455354Seric printf("not found (fuzzy disabled)\n"); 53553735Seric return NULL; 53655354Seric } 53753735Seric 53812634Seric /* search for a matching full name instead */ 53925777Seric for (p = name; *p != '\0'; p++) 54025777Seric { 54125777Seric if (*p == (SpaceSub & 0177) || *p == '_') 54225777Seric *p = ' '; 54325777Seric } 54423107Seric (void) setpwent(); 5454376Seric while ((pw = getpwent()) != NULL) 5464376Seric { 5474998Seric char buf[MAXNAME]; 5484376Seric 5494998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 55056795Seric if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 5514381Seric { 55255354Seric if (tTd(29, 4)) 55355354Seric printf("fuzzy matches %s\n", pw->pw_name); 55458151Seric message("sending to login name %s", pw->pw_name); 55555354Seric *fuzzyp = TRUE; 5564376Seric return (pw); 5574377Seric } 5584376Seric } 55955354Seric if (tTd(29, 4)) 56055354Seric printf("no fuzzy match found\n"); 56159015Seric #else 56259015Seric if (tTd(29, 4)) 56359015Seric printf("not found (fuzzy disabled)\n"); 56459015Seric #endif 5654376Seric return (NULL); 5664373Seric } 5674373Seric /* 5684329Seric ** WRITABLE -- predicate returning if the file is writable. 5694329Seric ** 5704329Seric ** This routine must duplicate the algorithm in sys/fio.c. 5714329Seric ** Unfortunately, we cannot use the access call since we 5724329Seric ** won't necessarily be the real uid when we try to 5734329Seric ** actually open the file. 5744329Seric ** 5754329Seric ** Notice that ANY file with ANY execute bit is automatically 5764329Seric ** not writable. This is also enforced by mailfile. 5774329Seric ** 5784329Seric ** Parameters: 57965064Seric ** filename -- the file name to check. 58065112Seric ** ctladdr -- the controlling address for this file. 58165064Seric ** flags -- SFF_* flags to control the function. 5824329Seric ** 5834329Seric ** Returns: 5844329Seric ** TRUE -- if we will be able to write this file. 5854329Seric ** FALSE -- if we cannot write this file. 5864329Seric ** 5874329Seric ** Side Effects: 5884329Seric ** none. 5894329Seric */ 5904329Seric 5914329Seric bool 59265112Seric writable(filename, ctladdr, flags) 59364819Seric char *filename; 59465112Seric ADDRESS *ctladdr; 59565064Seric int flags; 5964329Seric { 59755372Seric uid_t euid; 59855372Seric gid_t egid; 5994329Seric int bits; 60064944Seric register char *p; 60164944Seric char *uname; 60264944Seric struct stat stb; 60364944Seric extern char RealUserName[]; 6044329Seric 60564819Seric if (tTd(29, 5)) 60665064Seric printf("writable(%s, %x)\n", filename, flags); 60764944Seric 60864944Seric #ifdef HASLSTAT 60965064Seric if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb) 61065064Seric : stat(filename, &stb)) < 0) 61164944Seric #else 61264944Seric if (stat(filename, &stb) < 0) 61364944Seric #endif 61464944Seric { 61564944Seric /* file does not exist -- see if directory is safe */ 61664944Seric p = strrchr(filename, '/'); 61764944Seric if (p == NULL) 61864944Seric { 61965067Seric errno = ENOTDIR; 62064944Seric return FALSE; 62164944Seric } 62265067Seric *p = '\0'; 62365067Seric errno = safefile(filename, RealUid, RealGid, RealUserName, 62465067Seric SFF_MUSTOWN, S_IWRITE|S_IEXEC); 62564944Seric *p = '/'; 62665067Seric return errno == 0; 62764944Seric } 62864944Seric 62965225Seric #ifdef SUID_ROOT_FILES_OK 63065225Seric /* really ought to be passed down -- and not a good idea */ 63165225Seric flags |= SFF_ROOTOK; 63265225Seric #endif 63365225Seric 63464944Seric /* 63564944Seric ** File does exist -- check that it is writable. 63664944Seric */ 63764944Seric 63864944Seric if (bitset(0111, stb.st_mode)) 63965022Seric { 64065022Seric if (tTd(29, 5)) 64165022Seric printf("failed (mode %o: x bits)\n", stb.st_mode); 64265067Seric errno = EPERM; 6434329Seric return (FALSE); 64465022Seric } 64564944Seric 64665112Seric if (ctladdr != NULL && geteuid() == 0) 64764944Seric { 64865112Seric euid = ctladdr->q_uid; 64965112Seric egid = ctladdr->q_gid; 65065112Seric uname = ctladdr->q_user; 65164944Seric } 65265112Seric else 65365112Seric { 65465112Seric euid = RealUid; 65565112Seric egid = RealGid; 65665112Seric uname = RealUserName; 65765112Seric } 65865138Seric if (euid == 0) 65965138Seric { 66065138Seric euid = DefUid; 66165138Seric uname = DefUser; 66265138Seric } 66365138Seric if (egid == 0) 66465138Seric egid = DefGid; 6654329Seric if (geteuid() == 0) 6664329Seric { 66765225Seric if (bitset(S_ISUID, stb.st_mode) && 66865225Seric (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags))) 66964944Seric { 67064944Seric euid = stb.st_uid; 67164944Seric uname = NULL; 67264944Seric } 67365225Seric if (bitset(S_ISGID, stb.st_mode) && 67465225Seric (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags))) 67564944Seric egid = stb.st_gid; 6764329Seric } 6774329Seric 67864819Seric if (tTd(29, 5)) 67964819Seric printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n", 68064944Seric euid, egid, stb.st_uid, stb.st_gid); 68164819Seric 68265067Seric errno = safefile(filename, euid, egid, uname, flags, S_IWRITE); 68365067Seric return errno == 0; 6844329Seric } 6854329Seric /* 6864174Seric ** INCLUDE -- handle :include: specification. 6874174Seric ** 6884174Seric ** Parameters: 6894174Seric ** fname -- filename to include. 69053037Seric ** forwarding -- if TRUE, we are reading a .forward file. 69153037Seric ** if FALSE, it's a :include: file. 6924399Seric ** ctladdr -- address template to use to fill in these 6934399Seric ** addresses -- effective user/group id are 6944399Seric ** the important things. 6955006Seric ** sendq -- a pointer to the head of the send queue 6965006Seric ** to put these addresses in. 6974174Seric ** 6984174Seric ** Returns: 69957136Seric ** open error status 7004174Seric ** 7014174Seric ** Side Effects: 7024174Seric ** reads the :include: file and sends to everyone 7034174Seric ** listed in that file. 70465909Seric ** 70565909Seric ** Security Note: 70665909Seric ** If you have restricted chown (that is, you can't 70765909Seric ** give a file away), it is reasonable to allow programs 70865909Seric ** and files called from this :include: file to be to be 70965909Seric ** run as the owner of the :include: file. This is bogus 71065909Seric ** if there is any chance of someone giving away a file. 71165909Seric ** We assume that pre-POSIX systems can give away files. 71265909Seric ** 71365909Seric ** There is an additional restriction that if you 71465909Seric ** forward to a :include: file, it will not take on 71565909Seric ** the ownership of the :include: file. This may not 71665909Seric ** be necessary, but shouldn't hurt. 7174174Seric */ 7184174Seric 71953037Seric static jmp_buf CtxIncludeTimeout; 72063937Seric static int includetimeout(); 72153037Seric 72265496Seric #ifndef S_IWOTH 72365496Seric # define S_IWOTH (S_IWRITE >> 6) 72465496Seric #endif 72565496Seric 72657136Seric int 72755012Seric include(fname, forwarding, ctladdr, sendq, e) 7284174Seric char *fname; 72953037Seric bool forwarding; 7304399Seric ADDRESS *ctladdr; 7315006Seric ADDRESS **sendq; 73255012Seric ENVELOPE *e; 7334174Seric { 73464570Seric register FILE *fp = NULL; 73555012Seric char *oldto = e->e_to; 7369379Seric char *oldfilename = FileName; 7379379Seric int oldlinenumber = LineNumber; 73853037Seric register EVENT *ev = NULL; 73958082Seric int nincludes; 74064325Seric register ADDRESS *ca; 74164325Seric uid_t saveduid, uid; 74264325Seric gid_t savedgid, gid; 74364083Seric char *uname; 74464325Seric int rval = 0; 74565064Seric int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE; 74665496Seric struct stat st; 74765948Seric char buf[MAXLINE]; 74865909Seric #ifdef _POSIX_CHOWN_RESTRICTED 74965948Seric # if _POSIX_CHOWN_RESTRICTED == -1 75065948Seric # define safechown FALSE 75165948Seric # else 75265948Seric # define safechown TRUE 75365948Seric # endif 75465948Seric #else 75565948Seric # ifdef _PC_CHOWN_RESTRICTED 75665909Seric bool safechown; 75765948Seric # else 75865948Seric # ifdef BSD 75965948Seric # define safechown TRUE 76065948Seric # else 76165948Seric # define safechown FALSE 76265948Seric # endif 76365948Seric # endif 76465909Seric #endif 76565948Seric extern bool chownsafe(); 7664174Seric 76757186Seric if (tTd(27, 2)) 76857186Seric printf("include(%s)\n", fname); 76963902Seric if (tTd(27, 4)) 77063902Seric printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 77163581Seric if (tTd(27, 14)) 77263581Seric { 77363581Seric printf("ctladdr "); 77463581Seric printaddr(ctladdr, FALSE); 77563581Seric } 77657186Seric 77764325Seric if (tTd(27, 9)) 77864325Seric printf("include: old uid = %d/%d\n", getuid(), geteuid()); 77953037Seric 78063581Seric ca = getctladdr(ctladdr); 78163581Seric if (ca == NULL) 78264083Seric { 78364846Seric uid = DefUid; 78464846Seric gid = DefGid; 78564846Seric uname = DefUser; 78664325Seric saveduid = -1; 78764083Seric } 78863581Seric else 78964083Seric { 79063581Seric uid = ca->q_uid; 79164083Seric gid = ca->q_gid; 79264083Seric uname = ca->q_user; 79364325Seric #ifdef HASSETREUID 79464325Seric saveduid = geteuid(); 79564325Seric savedgid = getegid(); 79664325Seric if (saveduid == 0) 79764325Seric { 79864325Seric initgroups(uname, gid); 79964325Seric if (uid != 0) 80067827Seric { 80167827Seric if (setreuid(0, uid) < 0) 80267827Seric syserr("setreuid(0, %d) failure (real=%d, eff=%d)", 80367827Seric uid, getuid(), geteuid()); 80467827Seric } 80564325Seric } 80664325Seric #endif 80764083Seric } 80863581Seric 80964325Seric if (tTd(27, 9)) 81064325Seric printf("include: new uid = %d/%d\n", getuid(), geteuid()); 81164325Seric 81264325Seric /* 81364325Seric ** If home directory is remote mounted but server is down, 81464325Seric ** this can hang or give errors; use a timeout to avoid this 81564325Seric */ 81664325Seric 81753037Seric if (setjmp(CtxIncludeTimeout) != 0) 81853037Seric { 81963853Seric ctladdr->q_flags |= QQUEUEUP; 82053037Seric errno = 0; 82163993Seric 82263993Seric /* return pseudo-error code */ 82364325Seric rval = EOPENTIMEOUT; 82464325Seric goto resetuid; 82553037Seric } 82667711Seric if (TimeOuts.to_fileopen > 0) 82767711Seric ev = setevent(TimeOuts.to_fileopen, includetimeout, 0); 82867711Seric else 82967711Seric ev = NULL; 83053037Seric 83163581Seric /* the input file must be marked safe */ 83264944Seric rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD); 83364329Seric if (rval != 0) 83453037Seric { 83564325Seric /* don't use this :include: file */ 83657186Seric if (tTd(27, 4)) 83758247Seric printf("include: not safe (uid=%d): %s\n", 83864329Seric uid, errstring(rval)); 83953037Seric } 84065496Seric else 8414174Seric { 84265496Seric fp = fopen(fname, "r"); 84365496Seric if (fp == NULL) 84458061Seric { 84564329Seric rval = errno; 84665496Seric if (tTd(27, 4)) 84765496Seric printf("include: open: %s\n", errstring(rval)); 84858061Seric } 8494406Seric } 85067711Seric if (ev != NULL) 85167711Seric clrevent(ev); 85253037Seric 85364570Seric resetuid: 85464570Seric 85564570Seric #ifdef HASSETREUID 85664570Seric if (saveduid == 0) 85764570Seric { 85864570Seric if (uid != 0) 85967827Seric { 86067827Seric if (setreuid(-1, 0) < 0) 86167827Seric syserr("setreuid(-1, 0) failure (real=%d, eff=%d)", 86267827Seric getuid(), geteuid()); 86367827Seric if (setreuid(RealUid, 0) < 0) 86464570Seric syserr("setreuid(%d, 0) failure (real=%d, eff=%d)", 86564570Seric RealUid, getuid(), geteuid()); 86667827Seric } 86764570Seric setgid(savedgid); 86864570Seric } 86964570Seric #endif 87064570Seric 87164570Seric if (tTd(27, 9)) 87264570Seric printf("include: reset uid = %d/%d\n", getuid(), geteuid()); 87364570Seric 87465593Seric if (rval == EOPENTIMEOUT) 87565593Seric usrerr("451 open timeout on %s", fname); 87665593Seric 87764570Seric if (fp == NULL) 87864570Seric return rval; 87964570Seric 88065496Seric if (fstat(fileno(fp), &st) < 0) 88165496Seric { 88265496Seric rval = errno; 88365496Seric syserr("Cannot fstat %s!", fname); 88465496Seric return rval; 88565496Seric } 88665496Seric 88765948Seric #ifndef safechown 88865948Seric safechown = chownsafe(fileno(fp)); 88965948Seric #endif 89065909Seric if (ca == NULL && safechown) 89165496Seric { 89265496Seric ctladdr->q_uid = st.st_uid; 89365496Seric ctladdr->q_gid = st.st_gid; 89465496Seric ctladdr->q_flags |= QGOODUID; 89565496Seric } 89665496Seric if (ca != NULL && ca->q_uid == st.st_uid) 89765496Seric { 89865496Seric /* optimization -- avoid getpwuid if we already have info */ 89965496Seric ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL; 90065496Seric ctladdr->q_ruser = ca->q_ruser; 90165496Seric } 90265496Seric else 90365496Seric { 90465909Seric char *sh; 90565496Seric register struct passwd *pw; 90665496Seric 90765909Seric sh = "/SENDMAIL/ANY/SHELL/"; 90865496Seric pw = getpwuid(st.st_uid); 90965909Seric if (pw != NULL) 91065496Seric { 91165496Seric ctladdr->q_ruser = newstr(pw->pw_name); 91265909Seric if (safechown) 91365909Seric sh = pw->pw_shell; 91465909Seric } 91565909Seric if (pw == NULL) 91665496Seric ctladdr->q_flags |= QBOGUSSHELL; 91765909Seric else if(!usershellok(sh)) 91865909Seric { 91965909Seric if (safechown) 92065909Seric ctladdr->q_flags |= QBOGUSSHELL; 92165909Seric else 92265909Seric ctladdr->q_flags |= QUNSAFEADDR; 92365496Seric } 92465496Seric } 92565496Seric 92658092Seric if (bitset(EF_VRFYONLY, e->e_flags)) 92758092Seric { 92858092Seric /* don't do any more now */ 92958868Seric ctladdr->q_flags |= QVERIFIED; 93058884Seric e->e_nrcpts++; 93158680Seric xfclose(fp, "include", fname); 93264570Seric return rval; 93358092Seric } 93458092Seric 93565496Seric /* 93665496Seric ** Check to see if some bad guy can write this file 93765496Seric ** 93865496Seric ** This should really do something clever with group 93965496Seric ** permissions; currently we just view world writable 94065496Seric ** as unsafe. Also, we don't check for writable 94165496Seric ** directories in the path. We've got to leave 94265496Seric ** something for the local sysad to do. 94365496Seric */ 94465496Seric 94565496Seric if (bitset(S_IWOTH, st.st_mode)) 94665496Seric ctladdr->q_flags |= QUNSAFEADDR; 94765496Seric 9484174Seric /* read the file -- each line is a comma-separated list. */ 9499379Seric FileName = fname; 9509379Seric LineNumber = 0; 95158082Seric ctladdr->q_flags &= ~QSELFREF; 95258082Seric nincludes = 0; 9534174Seric while (fgets(buf, sizeof buf, fp) != NULL) 9544174Seric { 95556795Seric register char *p = strchr(buf, '\n'); 9564174Seric 95740963Sbostic LineNumber++; 9584174Seric if (p != NULL) 9594174Seric *p = '\0'; 96057186Seric if (buf[0] == '#' || buf[0] == '\0') 96157139Seric continue; 96258008Seric e->e_to = NULL; 96358151Seric message("%s to %s", 96453037Seric forwarding ? "forwarding" : "sending", buf); 96557977Seric #ifdef LOG 96658020Seric if (forwarding && LogLevel > 9) 96757977Seric syslog(LOG_INFO, "%s: forward %s => %s", 96866284Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 96966284Seric oldto, buf); 97057977Seric #endif 97157977Seric 9724176Seric AliasLevel++; 97358082Seric nincludes += sendtolist(buf, ctladdr, sendq, e); 9744176Seric AliasLevel--; 9754174Seric } 97663902Seric 97763902Seric if (ferror(fp) && tTd(27, 3)) 97863902Seric printf("include: read error: %s\n", errstring(errno)); 97958082Seric if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 98058065Seric { 98158065Seric if (tTd(27, 5)) 98258065Seric { 98358065Seric printf("include: QDONTSEND "); 98458065Seric printaddr(ctladdr, FALSE); 98558065Seric } 98658065Seric ctladdr->q_flags |= QDONTSEND; 98758065Seric } 9884174Seric 98958680Seric (void) xfclose(fp, "include", fname); 9909379Seric FileName = oldfilename; 9919379Seric LineNumber = oldlinenumber; 99263847Seric e->e_to = oldto; 99364325Seric return rval; 9944174Seric } 99553037Seric 99653037Seric static 99753037Seric includetimeout() 99853037Seric { 99953037Seric longjmp(CtxIncludeTimeout, 1); 100053037Seric } 10014324Seric /* 10024324Seric ** SENDTOARGV -- send to an argument vector. 10034324Seric ** 10044324Seric ** Parameters: 10054324Seric ** argv -- argument vector to send to. 100658247Seric ** e -- the current envelope. 10074324Seric ** 10084324Seric ** Returns: 10094324Seric ** none. 10104324Seric ** 10114324Seric ** Side Effects: 10124324Seric ** puts all addresses on the argument vector onto the 10134324Seric ** send queue. 10144324Seric */ 10154324Seric 101655012Seric sendtoargv(argv, e) 10174324Seric register char **argv; 101855012Seric register ENVELOPE *e; 10194324Seric { 10204324Seric register char *p; 10214324Seric 10224324Seric while ((p = *argv++) != NULL) 10234324Seric { 102464284Seric (void) sendtolist(p, NULLADDR, &e->e_sendqueue, e); 10254324Seric } 10264324Seric } 10274399Seric /* 10284399Seric ** GETCTLADDR -- get controlling address from an address header. 10294399Seric ** 10304399Seric ** If none, get one corresponding to the effective userid. 10314399Seric ** 10324399Seric ** Parameters: 10334399Seric ** a -- the address to find the controller of. 10344399Seric ** 10354399Seric ** Returns: 10364399Seric ** the controlling address. 10374399Seric ** 10384399Seric ** Side Effects: 10394399Seric ** none. 10404399Seric */ 10414399Seric 10424399Seric ADDRESS * 10434399Seric getctladdr(a) 10444399Seric register ADDRESS *a; 10454399Seric { 10464404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 10474399Seric a = a->q_alias; 10484399Seric return (a); 10494399Seric } 1050