14174Seric # include <pwd.h> 24627Seric # include "sendmail.h" 34329Seric # include <sys/stat.h> 44174Seric 5*5316Seric SCCSID(@(#)recipient.c 3.32 01/01/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 454324Seric if (Debug > 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 1114174Seric 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 1414174Seric To = a->q_paddr; 1424600Seric m = a->q_mailer; 1434174Seric errno = 0; 1444174Seric # ifdef DEBUG 1454174Seric if (Debug) 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*5316Seric if (a->q_alias == NULL && Debug == 0 && !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 1944319Seric if (Debug) 1954444Seric { 1964444Seric printf("%s in sendq: ", a->q_paddr); 1974444Seric printaddr(q, FALSE); 1984444Seric } 1994174Seric # endif DEBUG 2004998Seric if (Verbose && !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*5316Seric if (a->q_alias == NULL && Debug == 0 && !QueueRun && !ForceMail) 2244399Seric usrerr("Cannot mail directly to :include:s"); 2254399Seric else 2264399Seric { 2274399Seric if (Verbose) 2284399Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2295006Seric include(&a->q_user[9], " sending", a, sendq); 2304399Seric } 2314174Seric } 2324174Seric else 2335006Seric alias(a, sendq); 2344174Seric } 2354174Seric 2364174Seric /* 2374174Seric ** If the user is local and still being sent, verify that 2384174Seric ** the address is good. If it is, try to forward. 2394174Seric ** If the address is already good, we have a forwarding 2404174Seric ** loop. This can be broken by just sending directly to 2414174Seric ** the user (which is probably correct anyway). 2424174Seric */ 2434174Seric 2444600Seric if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == LocalMailer) 2454174Seric { 2464174Seric char buf[MAXNAME]; 2474201Seric register char *p; 2484329Seric struct stat stb; 2494329Seric extern bool writable(); 2504399Seric bool quoted = FALSE; 2514174Seric 2524174Seric strcpy(buf, a->q_user); 2534399Seric for (p = buf; *p != '\0' && !quoted; p++) 2544399Seric { 2554998Seric if (!isascii(*p) && (*p & 0377) != (SPACESUB & 0377)) 2564399Seric quoted = TRUE; 2574399Seric } 2584174Seric stripquotes(buf, TRUE); 2594174Seric 2604174Seric /* see if this is to a file */ 2614201Seric if ((p = rindex(buf, '/')) != NULL) 2624174Seric { 2634201Seric /* check if writable or creatable */ 264*5316Seric if (a->q_alias == NULL && Debug == 0 && !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); 2954376Seric 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 { 3564377Seric if (Verbose) 3574998Seric message(Arpa_Info, "sending to login name %s", 3584998Seric pw->pw_name); 3594376Seric return (pw); 3604377Seric } 3614376Seric } 3624376Seric return (NULL); 3634373Seric } 3644373Seric /* 3654329Seric ** WRITABLE -- predicate returning if the file is writable. 3664329Seric ** 3674329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3684329Seric ** Unfortunately, we cannot use the access call since we 3694329Seric ** won't necessarily be the real uid when we try to 3704329Seric ** actually open the file. 3714329Seric ** 3724329Seric ** Notice that ANY file with ANY execute bit is automatically 3734329Seric ** not writable. This is also enforced by mailfile. 3744329Seric ** 3754329Seric ** Parameters: 3764329Seric ** s -- pointer to a stat struct for the file. 3774329Seric ** 3784329Seric ** Returns: 3794329Seric ** TRUE -- if we will be able to write this file. 3804329Seric ** FALSE -- if we cannot write this file. 3814329Seric ** 3824329Seric ** Side Effects: 3834329Seric ** none. 3844329Seric */ 3854329Seric 3864329Seric bool 3874329Seric writable(s) 3884329Seric register struct stat *s; 3894329Seric { 3904329Seric int euid, egid; 3914329Seric int bits; 3924329Seric 3934329Seric if (bitset(0111, s->st_mode)) 3944329Seric return (FALSE); 3954329Seric euid = getruid(); 3964329Seric egid = getrgid(); 3974329Seric if (geteuid() == 0) 3984329Seric { 3994329Seric if (bitset(S_ISUID, s->st_mode)) 4004329Seric euid = s->st_uid; 4014329Seric if (bitset(S_ISGID, s->st_mode)) 4024329Seric egid = s->st_gid; 4034329Seric } 4044329Seric 4054329Seric if (euid == 0) 4064329Seric return (TRUE); 4074329Seric bits = S_IWRITE; 4084329Seric if (euid != s->st_uid) 4094329Seric { 4104329Seric bits >>= 3; 4114329Seric if (egid != s->st_gid) 4124329Seric bits >>= 3; 4134329Seric } 4144329Seric return ((s->st_mode & bits) != 0); 4154329Seric } 4164329Seric /* 4174174Seric ** INCLUDE -- handle :include: specification. 4184174Seric ** 4194174Seric ** Parameters: 4204174Seric ** fname -- filename to include. 4214176Seric ** msg -- message to print in verbose mode. 4224399Seric ** ctladdr -- address template to use to fill in these 4234399Seric ** addresses -- effective user/group id are 4244399Seric ** the important things. 4255006Seric ** sendq -- a pointer to the head of the send queue 4265006Seric ** to put these addresses in. 4274174Seric ** 4284174Seric ** Returns: 4294174Seric ** none. 4304174Seric ** 4314174Seric ** Side Effects: 4324174Seric ** reads the :include: file and sends to everyone 4334174Seric ** listed in that file. 4344174Seric */ 4354174Seric 4365006Seric include(fname, msg, ctladdr, sendq) 4374174Seric char *fname; 4384176Seric char *msg; 4394399Seric ADDRESS *ctladdr; 4405006Seric ADDRESS **sendq; 4414174Seric { 4424174Seric char buf[MAXLINE]; 4434174Seric register FILE *fp; 4444178Seric char *oldto = To; 4454174Seric 4464174Seric fp = fopen(fname, "r"); 4474174Seric if (fp == NULL) 4484174Seric { 4494174Seric usrerr("Cannot open %s", fname); 4504174Seric return; 4514174Seric } 4524406Seric if (getctladdr(ctladdr) == NULL) 4534406Seric { 4544406Seric struct stat st; 4554174Seric 4564406Seric if (fstat(fileno(fp), &st) < 0) 4574406Seric syserr("Cannot fstat %s!", fname); 4584406Seric ctladdr->q_uid = st.st_uid; 4594406Seric ctladdr->q_gid = st.st_gid; 4604406Seric ctladdr->q_flags |= QGOODUID; 4614406Seric } 4624406Seric 4634174Seric /* read the file -- each line is a comma-separated list. */ 4644174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4654174Seric { 4664174Seric register char *p = index(buf, '\n'); 4674174Seric 4684174Seric if (p != NULL) 4694174Seric *p = '\0'; 4704174Seric if (buf[0] == '\0') 4714174Seric continue; 4724178Seric To = oldto; 4734174Seric if (Verbose) 4744176Seric message(Arpa_Info, "%s to %s", msg, buf); 4754176Seric AliasLevel++; 4765006Seric sendto(buf, 1, ctladdr, sendq); 4774176Seric AliasLevel--; 4784174Seric } 4794174Seric 4804319Seric (void) fclose(fp); 4814174Seric } 4824324Seric /* 4834324Seric ** SENDTOARGV -- send to an argument vector. 4844324Seric ** 4854324Seric ** Parameters: 4864324Seric ** argv -- argument vector to send to. 4874324Seric ** 4884324Seric ** Returns: 4894324Seric ** none. 4904324Seric ** 4914324Seric ** Side Effects: 4924324Seric ** puts all addresses on the argument vector onto the 4934324Seric ** send queue. 4944324Seric */ 4954324Seric 4964324Seric sendtoargv(argv) 4974324Seric register char **argv; 4984324Seric { 4994324Seric register char *p; 5004324Seric extern bool sameword(); 5014324Seric 5024324Seric while ((p = *argv++) != NULL) 5034324Seric { 5044324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 5054324Seric { 5064324Seric char nbuf[MAXNAME]; 5074324Seric 5084324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5094324Seric usrerr("address overflow"); 5104324Seric else 5114324Seric { 5124324Seric (void) strcpy(nbuf, p); 5134324Seric (void) strcat(nbuf, "@"); 5144324Seric (void) strcat(nbuf, argv[1]); 5154324Seric p = newstr(nbuf); 5164324Seric argv += 2; 5174324Seric } 5184324Seric } 5195006Seric sendto(p, 0, (ADDRESS *) NULL, &SendQueue); 5204324Seric } 5214324Seric } 5224399Seric /* 5234399Seric ** GETCTLADDR -- get controlling address from an address header. 5244399Seric ** 5254399Seric ** If none, get one corresponding to the effective userid. 5264399Seric ** 5274399Seric ** Parameters: 5284399Seric ** a -- the address to find the controller of. 5294399Seric ** 5304399Seric ** Returns: 5314399Seric ** the controlling address. 5324399Seric ** 5334399Seric ** Side Effects: 5344399Seric ** none. 5354399Seric */ 5364399Seric 5374399Seric ADDRESS * 5384399Seric getctladdr(a) 5394399Seric register ADDRESS *a; 5404399Seric { 5414404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5424399Seric a = a->q_alias; 5434399Seric return (a); 5444399Seric } 545