14174Seric # include <pwd.h> 24627Seric # include "sendmail.h" 34329Seric # include <sys/stat.h> 44174Seric 5*12634Seric SCCSID(@(#)recipient.c 3.54 05/21/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; 9912613Seric extern ADDRESS *recipient(); 1004324Seric 1014324Seric al = a->q_next; 10212613Seric 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: 12312613Seric ** The actual address in the queue. This will be "a" if 12412613Seric ** the address is not a duplicate, else the original address. 1254174Seric ** 1264174Seric ** Side Effects: 1274174Seric ** none. 1284174Seric */ 1294174Seric 13012613Seric 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"); 15912613Seric 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; 21212613Seric 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 } 30412613Seric 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 344*12634Seric /* look up this login name */ 345*12634Seric if ((pw = getpwnam(name)) != NULL) 346*12634Seric return (pw); 347*12634Seric 348*12634Seric /* search for a matching full name instead */ 3494376Seric setpwent(); 3504376Seric while ((pw = getpwent()) != NULL) 3514376Seric { 3524998Seric char buf[MAXNAME]; 3534993Seric extern bool sameword(); 3544376Seric 3554376Seric if (strcmp(pw->pw_name, name) == 0) 3564376Seric return (pw); 3574998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 3584407Seric if (index(buf, ' ') != NULL && sameword(buf, name)) 3594381Seric { 3607054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3614376Seric return (pw); 3624377Seric } 3634376Seric } 3644376Seric return (NULL); 3654373Seric } 3664373Seric /* 3674329Seric ** WRITABLE -- predicate returning if the file is writable. 3684329Seric ** 3694329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3704329Seric ** Unfortunately, we cannot use the access call since we 3714329Seric ** won't necessarily be the real uid when we try to 3724329Seric ** actually open the file. 3734329Seric ** 3744329Seric ** Notice that ANY file with ANY execute bit is automatically 3754329Seric ** not writable. This is also enforced by mailfile. 3764329Seric ** 3774329Seric ** Parameters: 3784329Seric ** s -- pointer to a stat struct for the file. 3794329Seric ** 3804329Seric ** Returns: 3814329Seric ** TRUE -- if we will be able to write this file. 3824329Seric ** FALSE -- if we cannot write this file. 3834329Seric ** 3844329Seric ** Side Effects: 3854329Seric ** none. 3864329Seric */ 3874329Seric 3884329Seric bool 3894329Seric writable(s) 3904329Seric register struct stat *s; 3914329Seric { 3924329Seric int euid, egid; 3934329Seric int bits; 3944329Seric 3954329Seric if (bitset(0111, s->st_mode)) 3964329Seric return (FALSE); 3974329Seric euid = getruid(); 3984329Seric egid = getrgid(); 3994329Seric if (geteuid() == 0) 4004329Seric { 4014329Seric if (bitset(S_ISUID, s->st_mode)) 4024329Seric euid = s->st_uid; 4034329Seric if (bitset(S_ISGID, s->st_mode)) 4044329Seric egid = s->st_gid; 4054329Seric } 4064329Seric 4074329Seric if (euid == 0) 4084329Seric return (TRUE); 4094329Seric bits = S_IWRITE; 4104329Seric if (euid != s->st_uid) 4114329Seric { 4124329Seric bits >>= 3; 4134329Seric if (egid != s->st_gid) 4144329Seric bits >>= 3; 4154329Seric } 4164329Seric return ((s->st_mode & bits) != 0); 4174329Seric } 4184329Seric /* 4194174Seric ** INCLUDE -- handle :include: specification. 4204174Seric ** 4214174Seric ** Parameters: 4224174Seric ** fname -- filename to include. 4234176Seric ** msg -- message to print in verbose mode. 4244399Seric ** ctladdr -- address template to use to fill in these 4254399Seric ** addresses -- effective user/group id are 4264399Seric ** the important things. 4275006Seric ** sendq -- a pointer to the head of the send queue 4285006Seric ** to put these addresses in. 4294174Seric ** 4304174Seric ** Returns: 4314174Seric ** none. 4324174Seric ** 4334174Seric ** Side Effects: 4344174Seric ** reads the :include: file and sends to everyone 4354174Seric ** listed in that file. 4364174Seric */ 4374174Seric 4385006Seric include(fname, msg, ctladdr, sendq) 4394174Seric char *fname; 4404176Seric char *msg; 4414399Seric ADDRESS *ctladdr; 4425006Seric ADDRESS **sendq; 4434174Seric { 4444174Seric char buf[MAXLINE]; 4454174Seric register FILE *fp; 4466906Seric char *oldto = CurEnv->e_to; 4479379Seric char *oldfilename = FileName; 4489379Seric int oldlinenumber = LineNumber; 4494174Seric 4504174Seric fp = fopen(fname, "r"); 4514174Seric if (fp == NULL) 4524174Seric { 4534174Seric usrerr("Cannot open %s", fname); 4544174Seric return; 4554174Seric } 4564406Seric if (getctladdr(ctladdr) == NULL) 4574406Seric { 4584406Seric struct stat st; 4594174Seric 4604406Seric if (fstat(fileno(fp), &st) < 0) 4614406Seric syserr("Cannot fstat %s!", fname); 4624406Seric ctladdr->q_uid = st.st_uid; 4634406Seric ctladdr->q_gid = st.st_gid; 4644406Seric ctladdr->q_flags |= QGOODUID; 4654406Seric } 4664406Seric 4674174Seric /* read the file -- each line is a comma-separated list. */ 4689379Seric FileName = fname; 4699379Seric LineNumber = 0; 4704174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4714174Seric { 4724174Seric register char *p = index(buf, '\n'); 4734174Seric 4744174Seric if (p != NULL) 4754174Seric *p = '\0'; 4764174Seric if (buf[0] == '\0') 4774174Seric continue; 4786906Seric CurEnv->e_to = oldto; 4797054Seric message(Arpa_Info, "%s to %s", msg, buf); 4804176Seric AliasLevel++; 4819622Seric sendtolist(buf, ctladdr, sendq); 4824176Seric AliasLevel--; 4834174Seric } 4844174Seric 4854319Seric (void) fclose(fp); 4869379Seric FileName = oldfilename; 4879379Seric LineNumber = oldlinenumber; 4884174Seric } 4894324Seric /* 4904324Seric ** SENDTOARGV -- send to an argument vector. 4914324Seric ** 4924324Seric ** Parameters: 4934324Seric ** argv -- argument vector to send to. 4944324Seric ** 4954324Seric ** Returns: 4964324Seric ** none. 4974324Seric ** 4984324Seric ** Side Effects: 4994324Seric ** puts all addresses on the argument vector onto the 5004324Seric ** send queue. 5014324Seric */ 5024324Seric 5034324Seric sendtoargv(argv) 5044324Seric register char **argv; 5054324Seric { 5064324Seric register char *p; 5074324Seric extern bool sameword(); 5084324Seric 5094324Seric while ((p = *argv++) != NULL) 5104324Seric { 5114324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 5124324Seric { 5134324Seric char nbuf[MAXNAME]; 5144324Seric 5154324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5164324Seric usrerr("address overflow"); 5174324Seric else 5184324Seric { 5194324Seric (void) strcpy(nbuf, p); 5204324Seric (void) strcat(nbuf, "@"); 5214324Seric (void) strcat(nbuf, argv[1]); 5224324Seric p = newstr(nbuf); 5234324Seric argv += 2; 5244324Seric } 5254324Seric } 5269622Seric sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5274324Seric } 5284324Seric } 5294399Seric /* 5304399Seric ** GETCTLADDR -- get controlling address from an address header. 5314399Seric ** 5324399Seric ** If none, get one corresponding to the effective userid. 5334399Seric ** 5344399Seric ** Parameters: 5354399Seric ** a -- the address to find the controller of. 5364399Seric ** 5374399Seric ** Returns: 5384399Seric ** the controlling address. 5394399Seric ** 5404399Seric ** Side Effects: 5414399Seric ** none. 5424399Seric */ 5434399Seric 5444399Seric ADDRESS * 5454399Seric getctladdr(a) 5464399Seric register ADDRESS *a; 5474399Seric { 5484404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5494399Seric a = a->q_alias; 5504399Seric return (a); 5514399Seric } 552