122710Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 633731Sbostic * Redistribution and use in source and binary forms are permitted 734921Sbostic * provided that the above copyright notice and this paragraph are 834921Sbostic * duplicated in all such forms and that any documentation, 934921Sbostic * advertising materials, and other materials related to such 1034921Sbostic * distribution and use acknowledge that the software was developed 1134921Sbostic * by the University of California, Berkeley. The name of the 1234921Sbostic * University may not be used to endorse or promote products derived 1334921Sbostic * from this software without specific prior written permission. 1434921Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1534921Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1634921Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1733731Sbostic */ 1822710Sdist 1922710Sdist #ifndef lint 20*40973Sbostic static char sccsid[] = "@(#)recipient.c 5.17 (Berkeley) 04/18/90"; 2133731Sbostic #endif /* not lint */ 2222710Sdist 2336928Sbostic # include <sys/types.h> 2436928Sbostic # include <sys/stat.h> 254174Seric # include <pwd.h> 264627Seric # include "sendmail.h" 274174Seric 284174Seric /* 299622Seric ** SENDTOLIST -- Designate a send list. 304174Seric ** 314174Seric ** The parameter is a comma-separated list of people to send to. 324174Seric ** This routine arranges to send to all of them. 334174Seric ** 344174Seric ** Parameters: 354174Seric ** list -- the send list. 364399Seric ** ctladdr -- the address template for the person to 374399Seric ** send to -- effective uid/gid are important. 385006Seric ** This is typically the alias that caused this 395006Seric ** expansion. 405006Seric ** sendq -- a pointer to the head of a queue to put 415006Seric ** these people into. 424174Seric ** 434174Seric ** Returns: 444998Seric ** none 454174Seric ** 464174Seric ** Side Effects: 474174Seric ** none. 484174Seric */ 494174Seric 504174Seric # define MAXRCRSN 10 514174Seric 529622Seric sendtolist(list, ctladdr, sendq) 534174Seric char *list; 544399Seric ADDRESS *ctladdr; 555198Seric ADDRESS **sendq; 564174Seric { 574174Seric register char *p; 588223Seric register ADDRESS *al; /* list of addresses to send to */ 594423Seric bool firstone; /* set on first address sent */ 604444Seric bool selfref; /* set if this list includes ctladdr */ 6111446Seric char delimiter; /* the address delimiter */ 624174Seric 637676Seric if (tTd(25, 1)) 644444Seric { 654444Seric printf("sendto: %s\n ctladdr=", list); 664444Seric printaddr(ctladdr, FALSE); 674444Seric } 684324Seric 698223Seric /* heuristic to determine old versus new style addresses */ 708230Seric if (ctladdr == NULL && 718230Seric (index(list, ',') != NULL || index(list, ';') != NULL || 728230Seric index(list, '<') != NULL || index(list, '(') != NULL)) 739340Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 7411446Seric delimiter = ' '; 7511446Seric if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL) 7611446Seric delimiter = ','; 778223Seric 784423Seric firstone = TRUE; 794444Seric selfref = FALSE; 804324Seric al = NULL; 818223Seric 828081Seric for (p = list; *p != '\0'; ) 834174Seric { 848081Seric register ADDRESS *a; 858081Seric extern char *DelimChar; /* defined in prescan */ 864319Seric 878081Seric /* parse the address */ 888081Seric while (isspace(*p) || *p == ',') 894174Seric p++; 9011446Seric a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter); 919297Seric p = DelimChar; 929297Seric if (a == NULL) 934174Seric continue; 944324Seric a->q_next = al; 954399Seric a->q_alias = ctladdr; 964444Seric 974444Seric /* see if this should be marked as a primary address */ 984423Seric if (ctladdr == NULL || 998081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 1004423Seric a->q_flags |= QPRIMARY; 1014444Seric 1024444Seric /* put on send queue or suppress self-reference */ 1039379Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 1044444Seric selfref = TRUE; 1054444Seric else 1064444Seric al = a; 1074423Seric firstone = FALSE; 1084324Seric } 1094324Seric 1104444Seric /* if this alias doesn't include itself, delete ctladdr */ 1114444Seric if (!selfref && ctladdr != NULL) 1124444Seric ctladdr->q_flags |= QDONTSEND; 1134444Seric 1144324Seric /* arrange to send to everyone on the local send list */ 1154324Seric while (al != NULL) 1164324Seric { 1174324Seric register ADDRESS *a = al; 11812613Seric extern ADDRESS *recipient(); 1194324Seric 1204324Seric al = a->q_next; 121*40973Sbostic setctladdr(a); 12212613Seric a = recipient(a, sendq); 1234993Seric 1244998Seric /* arrange to inherit full name */ 1254998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1264998Seric a->q_fullname = ctladdr->q_fullname; 1274174Seric } 1284324Seric 1296906Seric CurEnv->e_to = NULL; 1304174Seric } 1314174Seric /* 1324174Seric ** RECIPIENT -- Designate a message recipient 1334174Seric ** 1344174Seric ** Saves the named person for future mailing. 1354174Seric ** 1364174Seric ** Parameters: 1374174Seric ** a -- the (preparsed) address header for the recipient. 1385006Seric ** sendq -- a pointer to the head of a queue to put the 1395006Seric ** recipient in. Duplicate supression is done 1405006Seric ** in this queue. 1414174Seric ** 1424174Seric ** Returns: 14312613Seric ** The actual address in the queue. This will be "a" if 14412613Seric ** the address is not a duplicate, else the original address. 1454174Seric ** 1464174Seric ** Side Effects: 1474174Seric ** none. 1484174Seric */ 1494174Seric 15012613Seric ADDRESS * 1515006Seric recipient(a, sendq) 1524174Seric register ADDRESS *a; 1535006Seric register ADDRESS **sendq; 1544174Seric { 1554174Seric register ADDRESS *q; 1564319Seric ADDRESS **pq; 1574174Seric register struct mailer *m; 1589210Seric register char *p; 1599210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 1609210Seric char buf[MAXNAME]; /* unquoted image of the user name */ 1614399Seric extern ADDRESS *getctladdr(); 1624627Seric extern bool safefile(); 1634174Seric 1646906Seric CurEnv->e_to = a->q_paddr; 1654600Seric m = a->q_mailer; 1664174Seric errno = 0; 1677676Seric if (tTd(26, 1)) 1684444Seric { 1694444Seric printf("\nrecipient: "); 1704444Seric printaddr(a, FALSE); 1714444Seric } 1724174Seric 1734174Seric /* break aliasing loops */ 1744174Seric if (AliasLevel > MAXRCRSN) 1754174Seric { 1764174Seric usrerr("aliasing/forwarding loop broken"); 17712613Seric return (a); 1784174Seric } 1794174Seric 1804174Seric /* 1814627Seric ** Finish setting up address structure. 1824174Seric */ 1834174Seric 18416160Seric /* set the queue timeout */ 1854627Seric a->q_timeout = TimeOut; 1864627Seric 18716160Seric /* map user & host to lower case if requested on non-aliases */ 18816160Seric if (a->q_alias == NULL) 18916160Seric loweraddr(a); 19016160Seric 19116160Seric /* get unquoted user for file, program or user.name check */ 1929210Seric (void) strcpy(buf, a->q_user); 1939210Seric for (p = buf; *p != '\0' && !quoted; p++) 1949210Seric { 1959210Seric if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377)) 1969210Seric quoted = TRUE; 1979210Seric } 1989210Seric stripquotes(buf, TRUE); 1999210Seric 2004627Seric /* do sickly crude mapping for program mailing, etc. */ 2019210Seric if (m == LocalMailer && buf[0] == '|') 2024174Seric { 2039210Seric a->q_mailer = m = ProgMailer; 2049210Seric a->q_user++; 20536231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 2064174Seric { 20729915Seric a->q_flags |= QDONTSEND|QBADADDR; 2089210Seric usrerr("Cannot mail directly to programs"); 2094174Seric } 2104174Seric } 2114174Seric 2124174Seric /* 2134419Seric ** Look up this person in the recipient list. 2144419Seric ** If they are there already, return, otherwise continue. 2154419Seric ** If the list is empty, just add it. Notice the cute 2164419Seric ** hack to make from addresses suppress things correctly: 2174419Seric ** the QDONTSEND bit will be set in the send list. 2184419Seric ** [Please note: the emphasis is on "hack."] 2194174Seric */ 2204174Seric 2215006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2224174Seric { 2239379Seric if (!ForceMail && sameaddr(q, a)) 2244174Seric { 2257676Seric if (tTd(26, 1)) 2264444Seric { 2274444Seric printf("%s in sendq: ", a->q_paddr); 2284444Seric printaddr(q, FALSE); 2294444Seric } 2307054Seric if (!bitset(QDONTSEND, a->q_flags)) 2314324Seric message(Arpa_Info, "duplicate suppressed"); 2324423Seric if (!bitset(QPRIMARY, q->q_flags)) 2334423Seric q->q_flags |= a->q_flags; 23412613Seric return (q); 2354174Seric } 2364319Seric } 2374174Seric 2384319Seric /* add address on list */ 2394319Seric *pq = a; 2404174Seric a->q_next = NULL; 24124951Seric CurEnv->e_nrcpts++; 2424174Seric 2434174Seric /* 2444174Seric ** Alias the name and handle :include: specs. 2454174Seric */ 2464174Seric 2479210Seric if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags)) 2484174Seric { 2494174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2504174Seric { 2514174Seric a->q_flags |= QDONTSEND; 25236231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 25329915Seric { 25429915Seric a->q_flags |= QBADADDR; 2554399Seric usrerr("Cannot mail directly to :include:s"); 25629915Seric } 2574399Seric else 2584399Seric { 2597054Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2605006Seric include(&a->q_user[9], " sending", a, sendq); 2614399Seric } 2624174Seric } 2634174Seric else 2645006Seric alias(a, sendq); 2654174Seric } 2664174Seric 2674174Seric /* 2684174Seric ** If the user is local and still being sent, verify that 2694174Seric ** the address is good. If it is, try to forward. 2704174Seric ** If the address is already good, we have a forwarding 2714174Seric ** loop. This can be broken by just sending directly to 2724174Seric ** the user (which is probably correct anyway). 2734174Seric */ 2744174Seric 2759210Seric if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer) 2764174Seric { 2774329Seric struct stat stb; 2784329Seric extern bool writable(); 2794174Seric 2804174Seric /* see if this is to a file */ 2815600Seric if (buf[0] == '/') 2824174Seric { 2835600Seric p = rindex(buf, '/'); 2844201Seric /* check if writable or creatable */ 28536231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 2864399Seric { 28729915Seric a->q_flags |= QDONTSEND|QBADADDR; 2884399Seric usrerr("Cannot mail directly to files"); 2894399Seric } 2904399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2914539Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 2924174Seric { 2934174Seric a->q_flags |= QBADADDR; 29410109Seric giveresponse(EX_CANTCREAT, m, CurEnv); 2954174Seric } 2964174Seric } 2974174Seric else 2984174Seric { 2994174Seric register struct passwd *pw; 3004373Seric extern struct passwd *finduser(); 3014373Seric 3024407Seric /* warning -- finduser may trash buf */ 3034373Seric pw = finduser(buf); 3044174Seric if (pw == NULL) 3054174Seric { 3064174Seric a->q_flags |= QBADADDR; 30710109Seric giveresponse(EX_NOUSER, m, CurEnv); 3084174Seric } 3094174Seric else 3104174Seric { 3114993Seric char nbuf[MAXNAME]; 3124993Seric 3134376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 3144376Seric { 3154376Seric a->q_user = newstr(pw->pw_name); 3167008Seric (void) strcpy(buf, pw->pw_name); 3174376Seric } 3184174Seric a->q_home = newstr(pw->pw_dir); 3194213Seric a->q_uid = pw->pw_uid; 3204399Seric a->q_gid = pw->pw_gid; 3214404Seric a->q_flags |= QGOODUID; 3224998Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 3234993Seric if (nbuf[0] != '\0') 3244993Seric a->q_fullname = newstr(nbuf); 3254399Seric if (!quoted) 3265006Seric forward(a, sendq); 3274174Seric } 3284174Seric } 3294174Seric } 33012613Seric return (a); 3314174Seric } 3324174Seric /* 3334373Seric ** FINDUSER -- find the password entry for a user. 3344373Seric ** 3354373Seric ** This looks a lot like getpwnam, except that it may want to 3364373Seric ** do some fancier pattern matching in /etc/passwd. 3374373Seric ** 3389379Seric ** This routine contains most of the time of many sendmail runs. 3399379Seric ** It deserves to be optimized. 3409379Seric ** 3414373Seric ** Parameters: 3424373Seric ** name -- the name to match against. 3434373Seric ** 3444373Seric ** Returns: 3454373Seric ** A pointer to a pw struct. 3464373Seric ** NULL if name is unknown or ambiguous. 3474373Seric ** 3484373Seric ** Side Effects: 3494407Seric ** may modify name. 3504373Seric */ 3514373Seric 3524373Seric struct passwd * 3534373Seric finduser(name) 3544373Seric char *name; 3554373Seric { 3564376Seric register struct passwd *pw; 3574407Seric register char *p; 35815325Seric extern struct passwd *getpwent(); 35915325Seric extern struct passwd *getpwnam(); 3604373Seric 36125777Seric /* map upper => lower case */ 3624407Seric for (p = name; *p != '\0'; p++) 3634407Seric { 36425777Seric if (isascii(*p) && isupper(*p)) 36525568Seric *p = tolower(*p); 3664407Seric } 3674407Seric 36825777Seric /* look up this login name using fast path */ 36912634Seric if ((pw = getpwnam(name)) != NULL) 37012634Seric return (pw); 37112634Seric 37212634Seric /* search for a matching full name instead */ 37325777Seric for (p = name; *p != '\0'; p++) 37425777Seric { 37525777Seric if (*p == (SpaceSub & 0177) || *p == '_') 37625777Seric *p = ' '; 37725777Seric } 37823107Seric (void) setpwent(); 3794376Seric while ((pw = getpwent()) != NULL) 3804376Seric { 3814998Seric char buf[MAXNAME]; 3824376Seric 3834998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 38433725Sbostic if (index(buf, ' ') != NULL && !strcasecmp(buf, name)) 3854381Seric { 3867054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3874376Seric return (pw); 3884377Seric } 3894376Seric } 3904376Seric return (NULL); 3914373Seric } 3924373Seric /* 3934329Seric ** WRITABLE -- predicate returning if the file is writable. 3944329Seric ** 3954329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3964329Seric ** Unfortunately, we cannot use the access call since we 3974329Seric ** won't necessarily be the real uid when we try to 3984329Seric ** actually open the file. 3994329Seric ** 4004329Seric ** Notice that ANY file with ANY execute bit is automatically 4014329Seric ** not writable. This is also enforced by mailfile. 4024329Seric ** 4034329Seric ** Parameters: 4044329Seric ** s -- pointer to a stat struct for the file. 4054329Seric ** 4064329Seric ** Returns: 4074329Seric ** TRUE -- if we will be able to write this file. 4084329Seric ** FALSE -- if we cannot write this file. 4094329Seric ** 4104329Seric ** Side Effects: 4114329Seric ** none. 4124329Seric */ 4134329Seric 4144329Seric bool 4154329Seric writable(s) 4164329Seric register struct stat *s; 4174329Seric { 4184329Seric int euid, egid; 4194329Seric int bits; 4204329Seric 4214329Seric if (bitset(0111, s->st_mode)) 4224329Seric return (FALSE); 4234329Seric euid = getruid(); 4244329Seric egid = getrgid(); 4254329Seric if (geteuid() == 0) 4264329Seric { 4274329Seric if (bitset(S_ISUID, s->st_mode)) 4284329Seric euid = s->st_uid; 4294329Seric if (bitset(S_ISGID, s->st_mode)) 4304329Seric egid = s->st_gid; 4314329Seric } 4324329Seric 4334329Seric if (euid == 0) 4344329Seric return (TRUE); 4354329Seric bits = S_IWRITE; 4364329Seric if (euid != s->st_uid) 4374329Seric { 4384329Seric bits >>= 3; 4394329Seric if (egid != s->st_gid) 4404329Seric bits >>= 3; 4414329Seric } 4424329Seric return ((s->st_mode & bits) != 0); 4434329Seric } 4444329Seric /* 4454174Seric ** INCLUDE -- handle :include: specification. 4464174Seric ** 4474174Seric ** Parameters: 4484174Seric ** fname -- filename to include. 4494176Seric ** msg -- message to print in verbose mode. 4504399Seric ** ctladdr -- address template to use to fill in these 4514399Seric ** addresses -- effective user/group id are 4524399Seric ** the important things. 4535006Seric ** sendq -- a pointer to the head of the send queue 4545006Seric ** to put these addresses in. 4554174Seric ** 4564174Seric ** Returns: 4574174Seric ** none. 4584174Seric ** 4594174Seric ** Side Effects: 4604174Seric ** reads the :include: file and sends to everyone 4614174Seric ** listed in that file. 4624174Seric */ 4634174Seric 4645006Seric include(fname, msg, ctladdr, sendq) 4654174Seric char *fname; 4664176Seric char *msg; 4674399Seric ADDRESS *ctladdr; 4685006Seric ADDRESS **sendq; 4694174Seric { 4704174Seric char buf[MAXLINE]; 4714174Seric register FILE *fp; 4726906Seric char *oldto = CurEnv->e_to; 4739379Seric char *oldfilename = FileName; 4749379Seric int oldlinenumber = LineNumber; 4754174Seric 4764174Seric fp = fopen(fname, "r"); 4774174Seric if (fp == NULL) 4784174Seric { 4794174Seric usrerr("Cannot open %s", fname); 4804174Seric return; 4814174Seric } 4824406Seric if (getctladdr(ctladdr) == NULL) 4834406Seric { 4844406Seric struct stat st; 4854174Seric 4864406Seric if (fstat(fileno(fp), &st) < 0) 4874406Seric syserr("Cannot fstat %s!", fname); 4884406Seric ctladdr->q_uid = st.st_uid; 4894406Seric ctladdr->q_gid = st.st_gid; 4904406Seric ctladdr->q_flags |= QGOODUID; 4914406Seric } 4924406Seric 4934174Seric /* read the file -- each line is a comma-separated list. */ 4949379Seric FileName = fname; 4959379Seric LineNumber = 0; 4964174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4974174Seric { 4984174Seric register char *p = index(buf, '\n'); 4994174Seric 50040963Sbostic LineNumber++; 5014174Seric if (p != NULL) 5024174Seric *p = '\0'; 5034174Seric if (buf[0] == '\0') 5044174Seric continue; 5056906Seric CurEnv->e_to = oldto; 5067054Seric message(Arpa_Info, "%s to %s", msg, buf); 5074176Seric AliasLevel++; 5089622Seric sendtolist(buf, ctladdr, sendq); 5094176Seric AliasLevel--; 5104174Seric } 5114174Seric 5124319Seric (void) fclose(fp); 5139379Seric FileName = oldfilename; 5149379Seric LineNumber = oldlinenumber; 5154174Seric } 5164324Seric /* 5174324Seric ** SENDTOARGV -- send to an argument vector. 5184324Seric ** 5194324Seric ** Parameters: 5204324Seric ** argv -- argument vector to send to. 5214324Seric ** 5224324Seric ** Returns: 5234324Seric ** none. 5244324Seric ** 5254324Seric ** Side Effects: 5264324Seric ** puts all addresses on the argument vector onto the 5274324Seric ** send queue. 5284324Seric */ 5294324Seric 5304324Seric sendtoargv(argv) 5314324Seric register char **argv; 5324324Seric { 5334324Seric register char *p; 5344324Seric 5354324Seric while ((p = *argv++) != NULL) 5364324Seric { 53733725Sbostic if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at")) 5384324Seric { 5394324Seric char nbuf[MAXNAME]; 5404324Seric 5414324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5424324Seric usrerr("address overflow"); 5434324Seric else 5444324Seric { 5454324Seric (void) strcpy(nbuf, p); 5464324Seric (void) strcat(nbuf, "@"); 5474324Seric (void) strcat(nbuf, argv[1]); 5484324Seric p = newstr(nbuf); 5494324Seric argv += 2; 5504324Seric } 5514324Seric } 5529622Seric sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5534324Seric } 5544324Seric } 5554399Seric /* 5564399Seric ** GETCTLADDR -- get controlling address from an address header. 5574399Seric ** 5584399Seric ** If none, get one corresponding to the effective userid. 5594399Seric ** 5604399Seric ** Parameters: 5614399Seric ** a -- the address to find the controller of. 5624399Seric ** 5634399Seric ** Returns: 5644399Seric ** the controlling address. 5654399Seric ** 5664399Seric ** Side Effects: 5674399Seric ** none. 5684399Seric */ 5694399Seric 5704399Seric ADDRESS * 5714399Seric getctladdr(a) 5724399Seric register ADDRESS *a; 5734399Seric { 5744404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5754399Seric a = a->q_alias; 5764399Seric return (a); 5774399Seric } 578