14174Seric # include <pwd.h> 24627Seric # include "sendmail.h" 34329Seric # include <sys/stat.h> 44174Seric 5*7676Seric SCCSID(@(#)recipient.c 3.39 08/08/82); 64174Seric 74174Seric /* 84174Seric ** SENDTO -- 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. 154174Seric ** copyf -- the copy flag; passed to parse. 164399Seric ** ctladdr -- the address template for the person to 174399Seric ** send to -- effective uid/gid are important. 185006Seric ** This is typically the alias that caused this 195006Seric ** expansion. 205006Seric ** sendq -- a pointer to the head of a queue to put 215006Seric ** these people into. 224174Seric ** 234174Seric ** Returns: 244998Seric ** none 254174Seric ** 264174Seric ** Side Effects: 274174Seric ** none. 284174Seric */ 294174Seric 304174Seric # define MAXRCRSN 10 314174Seric 325006Seric sendto(list, copyf, ctladdr, sendq) 334174Seric char *list; 344174Seric int copyf; 354399Seric ADDRESS *ctladdr; 365198Seric ADDRESS **sendq; 374174Seric { 384174Seric register char *p; 394319Seric bool more; /* set if more addresses to send to */ 404324Seric ADDRESS *al; /* list of addresses to send to */ 414423Seric bool firstone; /* set on first address sent */ 424444Seric bool selfref; /* set if this list includes ctladdr */ 434174Seric 444324Seric # ifdef DEBUG 45*7676Seric if (tTd(25, 1)) 464444Seric { 474444Seric printf("sendto: %s\n ctladdr=", list); 484444Seric printaddr(ctladdr, FALSE); 494444Seric } 504324Seric # endif DEBUG 514324Seric 524174Seric more = TRUE; 534423Seric firstone = TRUE; 544444Seric selfref = FALSE; 554324Seric al = NULL; 564174Seric for (p = list; more; ) 574174Seric { 584319Seric register char *q; 594319Seric register char c; 604319Seric ADDRESS *a; 614319Seric 624174Seric /* find the end of this address */ 634174Seric while (*p == ' ' || *p == '\t') 644174Seric p++; 654174Seric q = p; 664174Seric while ((c = *p++) != '\0' && c != ',' && c != '\n') 674174Seric continue; 684174Seric more = c != '\0'; 694174Seric *--p = '\0'; 704174Seric if (more) 714174Seric p++; 724324Seric if (*q == '\0') 734324Seric continue; 744174Seric 754174Seric /* parse the address */ 764174Seric if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL) 774174Seric continue; 784324Seric a->q_next = al; 794399Seric a->q_alias = ctladdr; 804444Seric 814444Seric /* see if this should be marked as a primary address */ 824423Seric if (ctladdr == NULL || 834423Seric (firstone && !more && bitset(QPRIMARY, ctladdr->q_flags))) 844423Seric a->q_flags |= QPRIMARY; 854444Seric 864444Seric /* put on send queue or suppress self-reference */ 874444Seric if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE)) 884444Seric selfref = TRUE; 894444Seric else 904444Seric al = a; 914423Seric firstone = FALSE; 924324Seric } 934324Seric 944444Seric /* if this alias doesn't include itself, delete ctladdr */ 954444Seric if (!selfref && ctladdr != NULL) 964444Seric ctladdr->q_flags |= QDONTSEND; 974444Seric 984324Seric /* arrange to send to everyone on the local send list */ 994324Seric while (al != NULL) 1004324Seric { 1014324Seric register ADDRESS *a = al; 1024324Seric 1034324Seric al = a->q_next; 1045006Seric recipient(a, sendq); 1054993Seric 1064998Seric /* arrange to inherit full name */ 1074998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1084998Seric a->q_fullname = ctladdr->q_fullname; 1094174Seric } 1104324Seric 1116906Seric CurEnv->e_to = NULL; 1124174Seric } 1134174Seric /* 1144174Seric ** RECIPIENT -- Designate a message recipient 1154174Seric ** 1164174Seric ** Saves the named person for future mailing. 1174174Seric ** 1184174Seric ** Parameters: 1194174Seric ** a -- the (preparsed) address header for the recipient. 1205006Seric ** sendq -- a pointer to the head of a queue to put the 1215006Seric ** recipient in. Duplicate supression is done 1225006Seric ** in this queue. 1234174Seric ** 1244174Seric ** Returns: 1254998Seric ** none. 1264174Seric ** 1274174Seric ** Side Effects: 1284174Seric ** none. 1294174Seric */ 1304174Seric 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; 1384399Seric extern ADDRESS *getctladdr(); 1394627Seric extern bool safefile(); 1404174Seric 1416906Seric CurEnv->e_to = a->q_paddr; 1424600Seric m = a->q_mailer; 1434174Seric errno = 0; 1444174Seric # ifdef DEBUG 145*7676Seric if (tTd(26, 1)) 1464444Seric { 1474444Seric printf("\nrecipient: "); 1484444Seric printaddr(a, FALSE); 1494444Seric } 1504174Seric # endif DEBUG 1514174Seric 1524174Seric /* break aliasing loops */ 1534174Seric if (AliasLevel > MAXRCRSN) 1544174Seric { 1554174Seric usrerr("aliasing/forwarding loop broken"); 1564998Seric return; 1574174Seric } 1584174Seric 1594174Seric /* 1604627Seric ** Finish setting up address structure. 1614174Seric */ 1624174Seric 1634627Seric a->q_timeout = TimeOut; 1644627Seric 1654627Seric /* do sickly crude mapping for program mailing, etc. */ 1664600Seric if (a->q_mailer == LocalMailer) 1674174Seric { 1684174Seric if (a->q_user[0] == '|') 1694174Seric { 1704600Seric a->q_mailer = m = ProgMailer; 1714174Seric a->q_user++; 172*7676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 1734217Seric { 1744217Seric usrerr("Cannot mail directly to programs"); 1754217Seric a->q_flags |= QDONTSEND; 1764217Seric } 1774174Seric } 1784174Seric } 1794174Seric 1804174Seric /* 1814419Seric ** Look up this person in the recipient list. 1824419Seric ** If they are there already, return, otherwise continue. 1834419Seric ** If the list is empty, just add it. Notice the cute 1844419Seric ** hack to make from addresses suppress things correctly: 1854419Seric ** the QDONTSEND bit will be set in the send list. 1864419Seric ** [Please note: the emphasis is on "hack."] 1874174Seric */ 1884174Seric 1895006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 1904174Seric { 1914319Seric if (!ForceMail && sameaddr(q, a, FALSE)) 1924174Seric { 1934174Seric # ifdef DEBUG 194*7676Seric if (tTd(26, 1)) 1954444Seric { 1964444Seric printf("%s in sendq: ", a->q_paddr); 1974444Seric printaddr(q, FALSE); 1984444Seric } 1994174Seric # endif DEBUG 2007054Seric if (!bitset(QDONTSEND, a->q_flags)) 2014324Seric message(Arpa_Info, "duplicate suppressed"); 2024423Seric if (!bitset(QPRIMARY, q->q_flags)) 2034423Seric q->q_flags |= a->q_flags; 2044998Seric return; 2054174Seric } 2064319Seric } 2074174Seric 2084319Seric /* add address on list */ 2094319Seric *pq = a; 2104174Seric a->q_next = NULL; 2114247Seric if (DontSend) 2124247Seric a->q_flags |= QDONTSEND; 2134174Seric 2144174Seric /* 2154174Seric ** Alias the name and handle :include: specs. 2164174Seric */ 2174174Seric 2184600Seric if (a->q_mailer == LocalMailer) 2194174Seric { 2204174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2214174Seric { 2224174Seric a->q_flags |= QDONTSEND; 223*7676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 2244399Seric usrerr("Cannot mail directly to :include:s"); 2254399Seric else 2264399Seric { 2277054Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2285006Seric include(&a->q_user[9], " sending", a, sendq); 2294399Seric } 2304174Seric } 2314174Seric else 2325006Seric alias(a, sendq); 2334174Seric } 2344174Seric 2354174Seric /* 2364174Seric ** If the user is local and still being sent, verify that 2374174Seric ** the address is good. If it is, try to forward. 2384174Seric ** If the address is already good, we have a forwarding 2394174Seric ** loop. This can be broken by just sending directly to 2404174Seric ** the user (which is probably correct anyway). 2414174Seric */ 2424174Seric 2434600Seric if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == LocalMailer) 2444174Seric { 2454174Seric char buf[MAXNAME]; 2464201Seric register char *p; 2474329Seric struct stat stb; 2484329Seric extern bool writable(); 2494399Seric bool quoted = FALSE; 2504174Seric 2517008Seric (void) strcpy(buf, a->q_user); 2524399Seric for (p = buf; *p != '\0' && !quoted; p++) 2534399Seric { 2544998Seric if (!isascii(*p) && (*p & 0377) != (SPACESUB & 0377)) 2554399Seric quoted = TRUE; 2564399Seric } 2574174Seric stripquotes(buf, TRUE); 2584174Seric 2594174Seric /* see if this is to a file */ 2605600Seric if (buf[0] == '/') 2614174Seric { 2625600Seric p = rindex(buf, '/'); 2634201Seric /* check if writable or creatable */ 264*7676Seric if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail) 2654399Seric { 2664399Seric usrerr("Cannot mail directly to files"); 2674399Seric a->q_flags |= QDONTSEND; 2684399Seric } 2694399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2704539Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 2714174Seric { 2724174Seric a->q_flags |= QBADADDR; 2734174Seric giveresponse(EX_CANTCREAT, TRUE, m); 2744174Seric } 2754174Seric } 2764174Seric else 2774174Seric { 2784174Seric register struct passwd *pw; 2794373Seric extern struct passwd *finduser(); 2804373Seric 2814407Seric /* warning -- finduser may trash buf */ 2824373Seric pw = finduser(buf); 2834174Seric if (pw == NULL) 2844174Seric { 2854174Seric a->q_flags |= QBADADDR; 2864174Seric giveresponse(EX_NOUSER, TRUE, m); 2874174Seric } 2884174Seric else 2894174Seric { 2904993Seric char nbuf[MAXNAME]; 2914993Seric 2924376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 2934376Seric { 2944376Seric a->q_user = newstr(pw->pw_name); 2957008Seric (void) strcpy(buf, pw->pw_name); 2964376Seric } 2974174Seric a->q_home = newstr(pw->pw_dir); 2984213Seric a->q_uid = pw->pw_uid; 2994399Seric a->q_gid = pw->pw_gid; 3004404Seric a->q_flags |= QGOODUID; 3014998Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 3024993Seric if (nbuf[0] != '\0') 3034993Seric a->q_fullname = newstr(nbuf); 3044399Seric if (!quoted) 3055006Seric forward(a, sendq); 3064174Seric } 3074174Seric } 3084174Seric } 3094174Seric } 3104174Seric /* 3114373Seric ** FINDUSER -- find the password entry for a user. 3124373Seric ** 3134373Seric ** This looks a lot like getpwnam, except that it may want to 3144373Seric ** do some fancier pattern matching in /etc/passwd. 3154373Seric ** 3164373Seric ** Parameters: 3174373Seric ** name -- the name to match against. 3184373Seric ** 3194373Seric ** Returns: 3204373Seric ** A pointer to a pw struct. 3214373Seric ** NULL if name is unknown or ambiguous. 3224373Seric ** 3234373Seric ** Side Effects: 3244407Seric ** may modify name. 3254373Seric */ 3264373Seric 3274373Seric struct passwd * 3284373Seric finduser(name) 3294373Seric char *name; 3304373Seric { 3314376Seric extern struct passwd *getpwent(); 3324376Seric register struct passwd *pw; 3334407Seric register char *p; 3344373Seric 3354407Seric /* 3364407Seric ** Make name canonical. 3374407Seric */ 3384407Seric 3394407Seric for (p = name; *p != '\0'; p++) 3404407Seric { 3414407Seric if (*p == (SPACESUB & 0177) || *p == '_') 3424407Seric *p = ' '; 3434407Seric } 3444407Seric 3454376Seric setpwent(); 3464376Seric while ((pw = getpwent()) != NULL) 3474376Seric { 3484998Seric char buf[MAXNAME]; 3494993Seric extern bool sameword(); 3504376Seric 3514376Seric if (strcmp(pw->pw_name, name) == 0) 3524376Seric return (pw); 3534998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 3544407Seric if (index(buf, ' ') != NULL && sameword(buf, name)) 3554381Seric { 3567054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3574376Seric return (pw); 3584377Seric } 3594376Seric } 3604376Seric return (NULL); 3614373Seric } 3624373Seric /* 3634329Seric ** WRITABLE -- predicate returning if the file is writable. 3644329Seric ** 3654329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3664329Seric ** Unfortunately, we cannot use the access call since we 3674329Seric ** won't necessarily be the real uid when we try to 3684329Seric ** actually open the file. 3694329Seric ** 3704329Seric ** Notice that ANY file with ANY execute bit is automatically 3714329Seric ** not writable. This is also enforced by mailfile. 3724329Seric ** 3734329Seric ** Parameters: 3744329Seric ** s -- pointer to a stat struct for the file. 3754329Seric ** 3764329Seric ** Returns: 3774329Seric ** TRUE -- if we will be able to write this file. 3784329Seric ** FALSE -- if we cannot write this file. 3794329Seric ** 3804329Seric ** Side Effects: 3814329Seric ** none. 3824329Seric */ 3834329Seric 3844329Seric bool 3854329Seric writable(s) 3864329Seric register struct stat *s; 3874329Seric { 3884329Seric int euid, egid; 3894329Seric int bits; 3904329Seric 3914329Seric if (bitset(0111, s->st_mode)) 3924329Seric return (FALSE); 3934329Seric euid = getruid(); 3944329Seric egid = getrgid(); 3954329Seric if (geteuid() == 0) 3964329Seric { 3974329Seric if (bitset(S_ISUID, s->st_mode)) 3984329Seric euid = s->st_uid; 3994329Seric if (bitset(S_ISGID, s->st_mode)) 4004329Seric egid = s->st_gid; 4014329Seric } 4024329Seric 4034329Seric if (euid == 0) 4044329Seric return (TRUE); 4054329Seric bits = S_IWRITE; 4064329Seric if (euid != s->st_uid) 4074329Seric { 4084329Seric bits >>= 3; 4094329Seric if (egid != s->st_gid) 4104329Seric bits >>= 3; 4114329Seric } 4124329Seric return ((s->st_mode & bits) != 0); 4134329Seric } 4144329Seric /* 4154174Seric ** INCLUDE -- handle :include: specification. 4164174Seric ** 4174174Seric ** Parameters: 4184174Seric ** fname -- filename to include. 4194176Seric ** msg -- message to print in verbose mode. 4204399Seric ** ctladdr -- address template to use to fill in these 4214399Seric ** addresses -- effective user/group id are 4224399Seric ** the important things. 4235006Seric ** sendq -- a pointer to the head of the send queue 4245006Seric ** to put these addresses in. 4254174Seric ** 4264174Seric ** Returns: 4274174Seric ** none. 4284174Seric ** 4294174Seric ** Side Effects: 4304174Seric ** reads the :include: file and sends to everyone 4314174Seric ** listed in that file. 4324174Seric */ 4334174Seric 4345006Seric include(fname, msg, ctladdr, sendq) 4354174Seric char *fname; 4364176Seric char *msg; 4374399Seric ADDRESS *ctladdr; 4385006Seric ADDRESS **sendq; 4394174Seric { 4404174Seric char buf[MAXLINE]; 4414174Seric register FILE *fp; 4426906Seric char *oldto = CurEnv->e_to; 4434174Seric 4444174Seric fp = fopen(fname, "r"); 4454174Seric if (fp == NULL) 4464174Seric { 4474174Seric usrerr("Cannot open %s", fname); 4484174Seric return; 4494174Seric } 4504406Seric if (getctladdr(ctladdr) == NULL) 4514406Seric { 4524406Seric struct stat st; 4534174Seric 4544406Seric if (fstat(fileno(fp), &st) < 0) 4554406Seric syserr("Cannot fstat %s!", fname); 4564406Seric ctladdr->q_uid = st.st_uid; 4574406Seric ctladdr->q_gid = st.st_gid; 4584406Seric ctladdr->q_flags |= QGOODUID; 4594406Seric } 4604406Seric 4614174Seric /* read the file -- each line is a comma-separated list. */ 4624174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4634174Seric { 4644174Seric register char *p = index(buf, '\n'); 4654174Seric 4664174Seric if (p != NULL) 4674174Seric *p = '\0'; 4684174Seric if (buf[0] == '\0') 4694174Seric continue; 4706906Seric CurEnv->e_to = oldto; 4717054Seric message(Arpa_Info, "%s to %s", msg, buf); 4724176Seric AliasLevel++; 4735006Seric sendto(buf, 1, ctladdr, sendq); 4744176Seric AliasLevel--; 4754174Seric } 4764174Seric 4774319Seric (void) fclose(fp); 4784174Seric } 4794324Seric /* 4804324Seric ** SENDTOARGV -- send to an argument vector. 4814324Seric ** 4824324Seric ** Parameters: 4834324Seric ** argv -- argument vector to send to. 4844324Seric ** 4854324Seric ** Returns: 4864324Seric ** none. 4874324Seric ** 4884324Seric ** Side Effects: 4894324Seric ** puts all addresses on the argument vector onto the 4904324Seric ** send queue. 4914324Seric */ 4924324Seric 4934324Seric sendtoargv(argv) 4944324Seric register char **argv; 4954324Seric { 4964324Seric register char *p; 4974324Seric extern bool sameword(); 4984324Seric 4994324Seric while ((p = *argv++) != NULL) 5004324Seric { 5014324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 5024324Seric { 5034324Seric char nbuf[MAXNAME]; 5044324Seric 5054324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5064324Seric usrerr("address overflow"); 5074324Seric else 5084324Seric { 5094324Seric (void) strcpy(nbuf, p); 5104324Seric (void) strcat(nbuf, "@"); 5114324Seric (void) strcat(nbuf, argv[1]); 5124324Seric p = newstr(nbuf); 5134324Seric argv += 2; 5144324Seric } 5154324Seric } 5166906Seric sendto(p, 0, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5174324Seric } 5184324Seric } 5194399Seric /* 5204399Seric ** GETCTLADDR -- get controlling address from an address header. 5214399Seric ** 5224399Seric ** If none, get one corresponding to the effective userid. 5234399Seric ** 5244399Seric ** Parameters: 5254399Seric ** a -- the address to find the controller of. 5264399Seric ** 5274399Seric ** Returns: 5284399Seric ** the controlling address. 5294399Seric ** 5304399Seric ** Side Effects: 5314399Seric ** none. 5324399Seric */ 5334399Seric 5344399Seric ADDRESS * 5354399Seric getctladdr(a) 5364399Seric register ADDRESS *a; 5374399Seric { 5384404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5394399Seric a = a->q_alias; 5404399Seric return (a); 5414399Seric } 542