14174Seric # include <pwd.h> 24627Seric # include "sendmail.h" 34329Seric # include <sys/stat.h> 44174Seric 5*12613Seric SCCSID(@(#)recipient.c 3.53 05/20/83); 64174Seric 74174Seric /* 89622Seric ** SENDTOLIST -- Designate a send list. 94174Seric ** 104174Seric ** The parameter is a comma-separated list of people to send to. 114174Seric ** This routine arranges to send to all of them. 124174Seric ** 134174Seric ** Parameters: 144174Seric ** list -- the send list. 154399Seric ** ctladdr -- the address template for the person to 164399Seric ** send to -- effective uid/gid are important. 175006Seric ** This is typically the alias that caused this 185006Seric ** expansion. 195006Seric ** sendq -- a pointer to the head of a queue to put 205006Seric ** these people into. 214174Seric ** 224174Seric ** Returns: 234998Seric ** none 244174Seric ** 254174Seric ** Side Effects: 264174Seric ** none. 274174Seric */ 284174Seric 294174Seric # define MAXRCRSN 10 304174Seric 319622Seric sendtolist(list, ctladdr, sendq) 324174Seric char *list; 334399Seric ADDRESS *ctladdr; 345198Seric ADDRESS **sendq; 354174Seric { 364174Seric register char *p; 378223Seric register ADDRESS *al; /* list of addresses to send to */ 384423Seric bool firstone; /* set on first address sent */ 394444Seric bool selfref; /* set if this list includes ctladdr */ 4011446Seric char delimiter; /* the address delimiter */ 414174Seric 424324Seric # ifdef DEBUG 437676Seric if (tTd(25, 1)) 444444Seric { 454444Seric printf("sendto: %s\n ctladdr=", list); 464444Seric printaddr(ctladdr, FALSE); 474444Seric } 484324Seric # endif DEBUG 494324Seric 508223Seric /* heuristic to determine old versus new style addresses */ 518230Seric if (ctladdr == NULL && 528230Seric (index(list, ',') != NULL || index(list, ';') != NULL || 538230Seric index(list, '<') != NULL || index(list, '(') != NULL)) 549340Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 5511446Seric delimiter = ' '; 5611446Seric if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL) 5711446Seric delimiter = ','; 588223Seric 594423Seric firstone = TRUE; 604444Seric selfref = FALSE; 614324Seric al = NULL; 628223Seric 638081Seric for (p = list; *p != '\0'; ) 644174Seric { 658081Seric register ADDRESS *a; 668081Seric extern char *DelimChar; /* defined in prescan */ 674319Seric 688081Seric /* parse the address */ 698081Seric while (isspace(*p) || *p == ',') 704174Seric p++; 7111446Seric a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter); 729297Seric p = DelimChar; 739297Seric if (a == NULL) 744174Seric continue; 754324Seric a->q_next = al; 764399Seric a->q_alias = ctladdr; 774444Seric 784444Seric /* see if this should be marked as a primary address */ 794423Seric if (ctladdr == NULL || 808081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 814423Seric a->q_flags |= QPRIMARY; 824444Seric 834444Seric /* put on send queue or suppress self-reference */ 849379Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 854444Seric selfref = TRUE; 864444Seric else 874444Seric al = a; 884423Seric firstone = FALSE; 894324Seric } 904324Seric 914444Seric /* if this alias doesn't include itself, delete ctladdr */ 924444Seric if (!selfref && ctladdr != NULL) 934444Seric ctladdr->q_flags |= QDONTSEND; 944444Seric 954324Seric /* arrange to send to everyone on the local send list */ 964324Seric while (al != NULL) 974324Seric { 984324Seric register ADDRESS *a = al; 99*12613Seric extern ADDRESS *recipient(); 1004324Seric 1014324Seric al = a->q_next; 102*12613Seric a = recipient(a, sendq); 1034993Seric 1044998Seric /* arrange to inherit full name */ 1054998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1064998Seric a->q_fullname = ctladdr->q_fullname; 1074174Seric } 1084324Seric 1096906Seric CurEnv->e_to = NULL; 1104174Seric } 1114174Seric /* 1124174Seric ** RECIPIENT -- Designate a message recipient 1134174Seric ** 1144174Seric ** Saves the named person for future mailing. 1154174Seric ** 1164174Seric ** Parameters: 1174174Seric ** a -- the (preparsed) address header for the recipient. 1185006Seric ** sendq -- a pointer to the head of a queue to put the 1195006Seric ** recipient in. Duplicate supression is done 1205006Seric ** in this queue. 1214174Seric ** 1224174Seric ** Returns: 123*12613Seric ** The actual address in the queue. This will be "a" if 124*12613Seric ** the address is not a duplicate, else the original address. 1254174Seric ** 1264174Seric ** Side Effects: 1274174Seric ** none. 1284174Seric */ 1294174Seric 130*12613Seric ADDRESS * 1315006Seric recipient(a, sendq) 1324174Seric register ADDRESS *a; 1335006Seric register ADDRESS **sendq; 1344174Seric { 1354174Seric register ADDRESS *q; 1364319Seric ADDRESS **pq; 1374174Seric register struct mailer *m; 1389210Seric register char *p; 1399210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 1409210Seric char buf[MAXNAME]; /* unquoted image of the user name */ 1414399Seric extern ADDRESS *getctladdr(); 1424627Seric extern bool safefile(); 1434174Seric 1446906Seric CurEnv->e_to = a->q_paddr; 1454600Seric m = a->q_mailer; 1464174Seric errno = 0; 1474174Seric # ifdef DEBUG 1487676Seric if (tTd(26, 1)) 1494444Seric { 1504444Seric printf("\nrecipient: "); 1514444Seric printaddr(a, FALSE); 1524444Seric } 1534174Seric # endif DEBUG 1544174Seric 1554174Seric /* break aliasing loops */ 1564174Seric if (AliasLevel > MAXRCRSN) 1574174Seric { 1584174Seric usrerr("aliasing/forwarding loop broken"); 159*12613Seric return (a); 1604174Seric } 1614174Seric 1624174Seric /* 1634627Seric ** Finish setting up address structure. 1644174Seric */ 1654174Seric 1664627Seric a->q_timeout = TimeOut; 1674627Seric 1689210Seric (void) strcpy(buf, a->q_user); 1699210Seric for (p = buf; *p != '\0' && !quoted; p++) 1709210Seric { 1719210Seric if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377)) 1729210Seric quoted = TRUE; 1739210Seric } 1749210Seric stripquotes(buf, TRUE); 1759210Seric 1764627Seric /* do sickly crude mapping for program mailing, etc. */ 1779210Seric if (m == LocalMailer && buf[0] == '|') 1784174Seric { 1799210Seric a->q_mailer = m = ProgMailer; 1809210Seric a->q_user++; 1819210Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 1824174Seric { 1839210Seric usrerr("Cannot mail directly to programs"); 1849210Seric a->q_flags |= QDONTSEND; 1854174Seric } 1864174Seric } 1874174Seric 1884174Seric /* 1894419Seric ** Look up this person in the recipient list. 1904419Seric ** If they are there already, return, otherwise continue. 1914419Seric ** If the list is empty, just add it. Notice the cute 1924419Seric ** hack to make from addresses suppress things correctly: 1934419Seric ** the QDONTSEND bit will be set in the send list. 1944419Seric ** [Please note: the emphasis is on "hack."] 1954174Seric */ 1964174Seric 1975006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 1984174Seric { 1999379Seric if (!ForceMail && sameaddr(q, a)) 2004174Seric { 2014174Seric # ifdef DEBUG 2027676Seric if (tTd(26, 1)) 2034444Seric { 2044444Seric printf("%s in sendq: ", a->q_paddr); 2054444Seric printaddr(q, FALSE); 2064444Seric } 2074174Seric # endif DEBUG 2087054Seric if (!bitset(QDONTSEND, a->q_flags)) 2094324Seric message(Arpa_Info, "duplicate suppressed"); 2104423Seric if (!bitset(QPRIMARY, q->q_flags)) 2114423Seric q->q_flags |= a->q_flags; 212*12613Seric return (q); 2134174Seric } 2144319Seric } 2154174Seric 2164319Seric /* add address on list */ 2174319Seric *pq = a; 2184174Seric a->q_next = NULL; 2194174Seric 2204174Seric /* 2214174Seric ** Alias the name and handle :include: specs. 2224174Seric */ 2234174Seric 2249210Seric if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags)) 2254174Seric { 2264174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2274174Seric { 2284174Seric a->q_flags |= QDONTSEND; 2297676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 2304399Seric usrerr("Cannot mail directly to :include:s"); 2314399Seric else 2324399Seric { 2337054Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2345006Seric include(&a->q_user[9], " sending", a, sendq); 2354399Seric } 2364174Seric } 2374174Seric else 2385006Seric alias(a, sendq); 2394174Seric } 2404174Seric 2414174Seric /* 2424174Seric ** If the user is local and still being sent, verify that 2434174Seric ** the address is good. If it is, try to forward. 2444174Seric ** If the address is already good, we have a forwarding 2454174Seric ** loop. This can be broken by just sending directly to 2464174Seric ** the user (which is probably correct anyway). 2474174Seric */ 2484174Seric 2499210Seric if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer) 2504174Seric { 2514329Seric struct stat stb; 2524329Seric extern bool writable(); 2534174Seric 2544174Seric /* see if this is to a file */ 2555600Seric if (buf[0] == '/') 2564174Seric { 2575600Seric p = rindex(buf, '/'); 2584201Seric /* check if writable or creatable */ 2597676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 2604399Seric { 2614399Seric usrerr("Cannot mail directly to files"); 2624399Seric a->q_flags |= QDONTSEND; 2634399Seric } 2644399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2654539Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 2664174Seric { 2674174Seric a->q_flags |= QBADADDR; 26810109Seric giveresponse(EX_CANTCREAT, m, CurEnv); 2694174Seric } 2704174Seric } 2714174Seric else 2724174Seric { 2734174Seric register struct passwd *pw; 2744373Seric extern struct passwd *finduser(); 2754373Seric 2764407Seric /* warning -- finduser may trash buf */ 2774373Seric pw = finduser(buf); 2784174Seric if (pw == NULL) 2794174Seric { 2804174Seric a->q_flags |= QBADADDR; 28110109Seric giveresponse(EX_NOUSER, m, CurEnv); 2824174Seric } 2834174Seric else 2844174Seric { 2854993Seric char nbuf[MAXNAME]; 2864993Seric 2874376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 2884376Seric { 2894376Seric a->q_user = newstr(pw->pw_name); 2907008Seric (void) strcpy(buf, pw->pw_name); 2914376Seric } 2924174Seric a->q_home = newstr(pw->pw_dir); 2934213Seric a->q_uid = pw->pw_uid; 2944399Seric a->q_gid = pw->pw_gid; 2954404Seric a->q_flags |= QGOODUID; 2964998Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 2974993Seric if (nbuf[0] != '\0') 2984993Seric a->q_fullname = newstr(nbuf); 2994399Seric if (!quoted) 3005006Seric forward(a, sendq); 3014174Seric } 3024174Seric } 3034174Seric } 304*12613Seric return (a); 3054174Seric } 3064174Seric /* 3074373Seric ** FINDUSER -- find the password entry for a user. 3084373Seric ** 3094373Seric ** This looks a lot like getpwnam, except that it may want to 3104373Seric ** do some fancier pattern matching in /etc/passwd. 3114373Seric ** 3129379Seric ** This routine contains most of the time of many sendmail runs. 3139379Seric ** It deserves to be optimized. 3149379Seric ** 3154373Seric ** Parameters: 3164373Seric ** name -- the name to match against. 3174373Seric ** 3184373Seric ** Returns: 3194373Seric ** A pointer to a pw struct. 3204373Seric ** NULL if name is unknown or ambiguous. 3214373Seric ** 3224373Seric ** Side Effects: 3234407Seric ** may modify name. 3244373Seric */ 3254373Seric 3264373Seric struct passwd * 3274373Seric finduser(name) 3284373Seric char *name; 3294373Seric { 3304376Seric extern struct passwd *getpwent(); 3314376Seric register struct passwd *pw; 3324407Seric register char *p; 3334373Seric 3344407Seric /* 3354407Seric ** Make name canonical. 3364407Seric */ 3374407Seric 3384407Seric for (p = name; *p != '\0'; p++) 3394407Seric { 3409044Seric if (*p == (SpaceSub & 0177) || *p == '_') 3414407Seric *p = ' '; 3424407Seric } 3434407Seric 3444376Seric setpwent(); 3454376Seric while ((pw = getpwent()) != NULL) 3464376Seric { 3474998Seric char buf[MAXNAME]; 3484993Seric extern bool sameword(); 3494376Seric 3504376Seric if (strcmp(pw->pw_name, name) == 0) 3514376Seric return (pw); 3524998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 3534407Seric if (index(buf, ' ') != NULL && sameword(buf, name)) 3544381Seric { 3557054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3564376Seric return (pw); 3574377Seric } 3584376Seric } 3594376Seric return (NULL); 3604373Seric } 3614373Seric /* 3624329Seric ** WRITABLE -- predicate returning if the file is writable. 3634329Seric ** 3644329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3654329Seric ** Unfortunately, we cannot use the access call since we 3664329Seric ** won't necessarily be the real uid when we try to 3674329Seric ** actually open the file. 3684329Seric ** 3694329Seric ** Notice that ANY file with ANY execute bit is automatically 3704329Seric ** not writable. This is also enforced by mailfile. 3714329Seric ** 3724329Seric ** Parameters: 3734329Seric ** s -- pointer to a stat struct for the file. 3744329Seric ** 3754329Seric ** Returns: 3764329Seric ** TRUE -- if we will be able to write this file. 3774329Seric ** FALSE -- if we cannot write this file. 3784329Seric ** 3794329Seric ** Side Effects: 3804329Seric ** none. 3814329Seric */ 3824329Seric 3834329Seric bool 3844329Seric writable(s) 3854329Seric register struct stat *s; 3864329Seric { 3874329Seric int euid, egid; 3884329Seric int bits; 3894329Seric 3904329Seric if (bitset(0111, s->st_mode)) 3914329Seric return (FALSE); 3924329Seric euid = getruid(); 3934329Seric egid = getrgid(); 3944329Seric if (geteuid() == 0) 3954329Seric { 3964329Seric if (bitset(S_ISUID, s->st_mode)) 3974329Seric euid = s->st_uid; 3984329Seric if (bitset(S_ISGID, s->st_mode)) 3994329Seric egid = s->st_gid; 4004329Seric } 4014329Seric 4024329Seric if (euid == 0) 4034329Seric return (TRUE); 4044329Seric bits = S_IWRITE; 4054329Seric if (euid != s->st_uid) 4064329Seric { 4074329Seric bits >>= 3; 4084329Seric if (egid != s->st_gid) 4094329Seric bits >>= 3; 4104329Seric } 4114329Seric return ((s->st_mode & bits) != 0); 4124329Seric } 4134329Seric /* 4144174Seric ** INCLUDE -- handle :include: specification. 4154174Seric ** 4164174Seric ** Parameters: 4174174Seric ** fname -- filename to include. 4184176Seric ** msg -- message to print in verbose mode. 4194399Seric ** ctladdr -- address template to use to fill in these 4204399Seric ** addresses -- effective user/group id are 4214399Seric ** the important things. 4225006Seric ** sendq -- a pointer to the head of the send queue 4235006Seric ** to put these addresses in. 4244174Seric ** 4254174Seric ** Returns: 4264174Seric ** none. 4274174Seric ** 4284174Seric ** Side Effects: 4294174Seric ** reads the :include: file and sends to everyone 4304174Seric ** listed in that file. 4314174Seric */ 4324174Seric 4335006Seric include(fname, msg, ctladdr, sendq) 4344174Seric char *fname; 4354176Seric char *msg; 4364399Seric ADDRESS *ctladdr; 4375006Seric ADDRESS **sendq; 4384174Seric { 4394174Seric char buf[MAXLINE]; 4404174Seric register FILE *fp; 4416906Seric char *oldto = CurEnv->e_to; 4429379Seric char *oldfilename = FileName; 4439379Seric int oldlinenumber = LineNumber; 4444174Seric 4454174Seric fp = fopen(fname, "r"); 4464174Seric if (fp == NULL) 4474174Seric { 4484174Seric usrerr("Cannot open %s", fname); 4494174Seric return; 4504174Seric } 4514406Seric if (getctladdr(ctladdr) == NULL) 4524406Seric { 4534406Seric struct stat st; 4544174Seric 4554406Seric if (fstat(fileno(fp), &st) < 0) 4564406Seric syserr("Cannot fstat %s!", fname); 4574406Seric ctladdr->q_uid = st.st_uid; 4584406Seric ctladdr->q_gid = st.st_gid; 4594406Seric ctladdr->q_flags |= QGOODUID; 4604406Seric } 4614406Seric 4624174Seric /* read the file -- each line is a comma-separated list. */ 4639379Seric FileName = fname; 4649379Seric LineNumber = 0; 4654174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4664174Seric { 4674174Seric register char *p = index(buf, '\n'); 4684174Seric 4694174Seric if (p != NULL) 4704174Seric *p = '\0'; 4714174Seric if (buf[0] == '\0') 4724174Seric continue; 4736906Seric CurEnv->e_to = oldto; 4747054Seric message(Arpa_Info, "%s to %s", msg, buf); 4754176Seric AliasLevel++; 4769622Seric sendtolist(buf, ctladdr, sendq); 4774176Seric AliasLevel--; 4784174Seric } 4794174Seric 4804319Seric (void) fclose(fp); 4819379Seric FileName = oldfilename; 4829379Seric LineNumber = oldlinenumber; 4834174Seric } 4844324Seric /* 4854324Seric ** SENDTOARGV -- send to an argument vector. 4864324Seric ** 4874324Seric ** Parameters: 4884324Seric ** argv -- argument vector to send to. 4894324Seric ** 4904324Seric ** Returns: 4914324Seric ** none. 4924324Seric ** 4934324Seric ** Side Effects: 4944324Seric ** puts all addresses on the argument vector onto the 4954324Seric ** send queue. 4964324Seric */ 4974324Seric 4984324Seric sendtoargv(argv) 4994324Seric register char **argv; 5004324Seric { 5014324Seric register char *p; 5024324Seric extern bool sameword(); 5034324Seric 5044324Seric while ((p = *argv++) != NULL) 5054324Seric { 5064324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 5074324Seric { 5084324Seric char nbuf[MAXNAME]; 5094324Seric 5104324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5114324Seric usrerr("address overflow"); 5124324Seric else 5134324Seric { 5144324Seric (void) strcpy(nbuf, p); 5154324Seric (void) strcat(nbuf, "@"); 5164324Seric (void) strcat(nbuf, argv[1]); 5174324Seric p = newstr(nbuf); 5184324Seric argv += 2; 5194324Seric } 5204324Seric } 5219622Seric sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5224324Seric } 5234324Seric } 5244399Seric /* 5254399Seric ** GETCTLADDR -- get controlling address from an address header. 5264399Seric ** 5274399Seric ** If none, get one corresponding to the effective userid. 5284399Seric ** 5294399Seric ** Parameters: 5304399Seric ** a -- the address to find the controller of. 5314399Seric ** 5324399Seric ** Returns: 5334399Seric ** the controlling address. 5344399Seric ** 5354399Seric ** Side Effects: 5364399Seric ** none. 5374399Seric */ 5384399Seric 5394399Seric ADDRESS * 5404399Seric getctladdr(a) 5414399Seric register ADDRESS *a; 5424399Seric { 5434404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5444399Seric a = a->q_alias; 5454399Seric return (a); 5464399Seric } 547