14174Seric # include <pwd.h> 24329Seric # include <sys/types.h> 34329Seric # include <sys/stat.h> 44174Seric # include "sendmail.h" 54174Seric 6*4404Seric static char SccsId[] = "@(#)recipient.c 3.19 09/16/81"; 74174Seric 84174Seric /* 94174Seric ** SENDTO -- Designate a send list. 104174Seric ** 114174Seric ** The parameter is a comma-separated list of people to send to. 124174Seric ** This routine arranges to send to all of them. 134174Seric ** 144174Seric ** Parameters: 154174Seric ** list -- the send list. 164174Seric ** copyf -- the copy flag; passed to parse. 174399Seric ** ctladdr -- the address template for the person to 184399Seric ** send to -- effective uid/gid are important. 194174Seric ** 204174Seric ** Returns: 214174Seric ** none 224174Seric ** 234174Seric ** Side Effects: 244174Seric ** none. 254174Seric */ 264174Seric 274174Seric # define MAXRCRSN 10 284174Seric 294399Seric sendto(list, copyf, ctladdr) 304174Seric char *list; 314174Seric int copyf; 324399Seric ADDRESS *ctladdr; 334174Seric { 344174Seric register char *p; 354319Seric bool more; /* set if more addresses to send to */ 364324Seric ADDRESS *al; /* list of addresses to send to */ 374174Seric 384324Seric # ifdef DEBUG 394324Seric if (Debug > 1) 404324Seric printf("sendto: %s\n", list); 414324Seric # endif DEBUG 424324Seric 434174Seric more = TRUE; 444324Seric al = NULL; 454174Seric for (p = list; more; ) 464174Seric { 474319Seric register char *q; 484319Seric register char c; 494319Seric ADDRESS *a; 504319Seric 514174Seric /* find the end of this address */ 524174Seric while (*p == ' ' || *p == '\t') 534174Seric p++; 544174Seric q = p; 554174Seric while ((c = *p++) != '\0' && c != ',' && c != '\n') 564174Seric continue; 574174Seric more = c != '\0'; 584174Seric *--p = '\0'; 594174Seric if (more) 604174Seric p++; 614324Seric if (*q == '\0') 624324Seric continue; 634174Seric 644174Seric /* parse the address */ 654174Seric if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL) 664174Seric continue; 674174Seric 684324Seric /* put it on the local send list */ 694324Seric a->q_next = al; 704399Seric a->q_alias = ctladdr; 714324Seric al = a; 724324Seric } 734324Seric 744324Seric /* arrange to send to everyone on the local send list */ 754324Seric while (al != NULL) 764324Seric { 774324Seric register ADDRESS *a = al; 784324Seric 794324Seric al = a->q_next; 804174Seric recipient(a); 814174Seric } 824324Seric 834174Seric To = NULL; 844174Seric } 854174Seric /* 864174Seric ** RECIPIENT -- Designate a message recipient 874174Seric ** 884174Seric ** Saves the named person for future mailing. 894174Seric ** 904174Seric ** Parameters: 914174Seric ** a -- the (preparsed) address header for the recipient. 924174Seric ** 934174Seric ** Returns: 944174Seric ** none. 954174Seric ** 964174Seric ** Side Effects: 974174Seric ** none. 984174Seric */ 994174Seric 1004174Seric recipient(a) 1014174Seric register ADDRESS *a; 1024174Seric { 1034174Seric register ADDRESS *q; 1044319Seric ADDRESS **pq; 1054174Seric register struct mailer *m; 1064399Seric extern ADDRESS *getctladdr(); 1074174Seric 1084174Seric To = a->q_paddr; 1094174Seric m = Mailer[a->q_mailer]; 1104174Seric errno = 0; 1114174Seric # ifdef DEBUG 1124174Seric if (Debug) 1134174Seric printf("recipient(%s)\n", To); 1144174Seric # endif DEBUG 1154174Seric 1164174Seric /* break aliasing loops */ 1174174Seric if (AliasLevel > MAXRCRSN) 1184174Seric { 1194174Seric usrerr("aliasing/forwarding loop broken"); 1204174Seric return; 1214174Seric } 1224174Seric 1234174Seric /* 1244174Seric ** Do sickly crude mapping for program mailing, etc. 1254174Seric */ 1264174Seric 1274197Seric if (a->q_mailer == MN_LOCAL) 1284174Seric { 1294174Seric if (a->q_user[0] == '|') 1304174Seric { 1314197Seric a->q_mailer = MN_PROG; 1324197Seric m = Mailer[MN_PROG]; 1334174Seric a->q_user++; 1344402Seric if (getctladdr(a) == NULL && Debug == 0) 1354217Seric { 1364217Seric usrerr("Cannot mail directly to programs"); 1374217Seric a->q_flags |= QDONTSEND; 1384217Seric } 1394174Seric } 1404174Seric } 1414174Seric 1424174Seric /* 1434174Seric ** Look up this person in the recipient list. If they 1444174Seric ** are there already, return, otherwise continue. 1454174Seric ** If the list is empty, just add it. 1464174Seric */ 1474174Seric 1484319Seric for (pq = &m->m_sendq; (q = *pq) != NULL; pq = &q->q_next) 1494174Seric { 1504319Seric if (!ForceMail && sameaddr(q, a, FALSE)) 1514174Seric { 1524174Seric # ifdef DEBUG 1534319Seric if (Debug) 1544319Seric printf("(%s in sendq)\n", a->q_paddr); 1554174Seric # endif DEBUG 1564319Seric if (Verbose && !bitset(QDONTSEND, a->q_flags)) 1574324Seric message(Arpa_Info, "duplicate suppressed"); 1584319Seric return; 1594174Seric } 1604319Seric } 1614174Seric 1624319Seric /* add address on list */ 1634319Seric *pq = a; 1644174Seric a->q_next = NULL; 1654247Seric if (DontSend) 1664247Seric a->q_flags |= QDONTSEND; 1674174Seric 1684174Seric /* 1694174Seric ** Alias the name and handle :include: specs. 1704174Seric */ 1714174Seric 1724197Seric if (a->q_mailer == MN_LOCAL) 1734174Seric { 1744174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 1754174Seric { 1764174Seric a->q_flags |= QDONTSEND; 1774402Seric if (getctladdr(a) == NULL && Debug == 0) 1784399Seric usrerr("Cannot mail directly to :include:s"); 1794399Seric else 1804399Seric { 1814399Seric if (Verbose) 1824399Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 1834399Seric include(&a->q_user[9], " sending", a); 1844399Seric } 1854174Seric } 1864174Seric else 1874174Seric alias(a); 1884174Seric } 1894174Seric 1904174Seric /* 1914174Seric ** If the user is local and still being sent, verify that 1924174Seric ** the address is good. If it is, try to forward. 1934174Seric ** If the address is already good, we have a forwarding 1944174Seric ** loop. This can be broken by just sending directly to 1954174Seric ** the user (which is probably correct anyway). 1964174Seric */ 1974174Seric 1984197Seric if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == MN_LOCAL) 1994174Seric { 2004174Seric char buf[MAXNAME]; 2014201Seric register char *p; 2024329Seric struct stat stb; 2034329Seric extern bool writable(); 2044399Seric bool quoted = FALSE; 2054174Seric 2064174Seric strcpy(buf, a->q_user); 2074399Seric for (p = buf; *p != '\0' && !quoted; p++) 2084399Seric { 2094399Seric if (!isascii(*p)) 2104399Seric quoted = TRUE; 2114399Seric } 2124174Seric stripquotes(buf, TRUE); 2134174Seric 2144174Seric /* see if this is to a file */ 2154201Seric if ((p = rindex(buf, '/')) != NULL) 2164174Seric { 2174201Seric /* check if writable or creatable */ 2184402Seric if (getctladdr(a) == NULL && Debug == 0) 2194399Seric { 2204399Seric usrerr("Cannot mail directly to files"); 2214399Seric a->q_flags |= QDONTSEND; 2224399Seric } 2234399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2244201Seric (*p = '\0', access(buf, 3) < 0)) 2254174Seric { 2264174Seric a->q_flags |= QBADADDR; 2274174Seric giveresponse(EX_CANTCREAT, TRUE, m); 2284174Seric } 2294174Seric } 2304174Seric else 2314174Seric { 2324174Seric register struct passwd *pw; 2334373Seric extern struct passwd *finduser(); 2344373Seric 2354373Seric pw = finduser(buf); 2364174Seric if (pw == NULL) 2374174Seric { 2384174Seric a->q_flags |= QBADADDR; 2394174Seric giveresponse(EX_NOUSER, TRUE, m); 2404174Seric } 2414174Seric else 2424174Seric { 2434376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 2444376Seric { 2454376Seric a->q_user = newstr(pw->pw_name); 2464376Seric strcpy(buf, pw->pw_name); 2474376Seric } 2484174Seric a->q_home = newstr(pw->pw_dir); 2494213Seric a->q_uid = pw->pw_uid; 2504399Seric a->q_gid = pw->pw_gid; 251*4404Seric a->q_flags |= QGOODUID; 2524399Seric if (!quoted) 2534174Seric forward(a); 2544174Seric } 2554174Seric } 2564174Seric } 2574174Seric } 2584174Seric /* 2594373Seric ** FINDUSER -- find the password entry for a user. 2604373Seric ** 2614373Seric ** This looks a lot like getpwnam, except that it may want to 2624373Seric ** do some fancier pattern matching in /etc/passwd. 2634373Seric ** 2644373Seric ** Parameters: 2654373Seric ** name -- the name to match against. 2664373Seric ** 2674373Seric ** Returns: 2684373Seric ** A pointer to a pw struct. 2694373Seric ** NULL if name is unknown or ambiguous. 2704373Seric ** 2714373Seric ** Side Effects: 2724373Seric ** none. 2734373Seric */ 2744373Seric 2754373Seric struct passwd * 2764373Seric finduser(name) 2774373Seric char *name; 2784373Seric { 2794376Seric extern struct passwd *getpwent(); 2804376Seric register struct passwd *pw; 2814373Seric 2824376Seric setpwent(); 2834376Seric while ((pw = getpwent()) != NULL) 2844376Seric { 2854376Seric char buf[MAXNAME]; 2864376Seric register char *p; 2874376Seric extern bool sameword(); 2884381Seric bool gotaspace; 2894376Seric 2904376Seric if (strcmp(pw->pw_name, name) == 0) 2914376Seric return (pw); 2924376Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 2934381Seric gotaspace = FALSE; 2944376Seric for (p = buf; (p = index(p, ' ')) != NULL; ) 2954381Seric { 2964376Seric *p++ = SPACESUB & 0177; 2974381Seric gotaspace = TRUE; 2984381Seric } 2994381Seric if (gotaspace && sameword(buf, name)) 3004377Seric { 3014377Seric if (Verbose) 3024381Seric message(Arpa_Info, "sending to login name %s", 3034381Seric pw->pw_name); 3044376Seric return (pw); 3054377Seric } 3064376Seric } 3074376Seric return (NULL); 3084373Seric } 3094373Seric /* 3104329Seric ** WRITABLE -- predicate returning if the file is writable. 3114329Seric ** 3124329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3134329Seric ** Unfortunately, we cannot use the access call since we 3144329Seric ** won't necessarily be the real uid when we try to 3154329Seric ** actually open the file. 3164329Seric ** 3174329Seric ** Notice that ANY file with ANY execute bit is automatically 3184329Seric ** not writable. This is also enforced by mailfile. 3194329Seric ** 3204329Seric ** Parameters: 3214329Seric ** s -- pointer to a stat struct for the file. 3224329Seric ** 3234329Seric ** Returns: 3244329Seric ** TRUE -- if we will be able to write this file. 3254329Seric ** FALSE -- if we cannot write this file. 3264329Seric ** 3274329Seric ** Side Effects: 3284329Seric ** none. 3294329Seric */ 3304329Seric 3314329Seric bool 3324329Seric writable(s) 3334329Seric register struct stat *s; 3344329Seric { 3354329Seric int euid, egid; 3364329Seric int bits; 3374329Seric 3384329Seric if (bitset(0111, s->st_mode)) 3394329Seric return (FALSE); 3404329Seric euid = getruid(); 3414329Seric egid = getrgid(); 3424329Seric if (geteuid() == 0) 3434329Seric { 3444329Seric if (bitset(S_ISUID, s->st_mode)) 3454329Seric euid = s->st_uid; 3464329Seric if (bitset(S_ISGID, s->st_mode)) 3474329Seric egid = s->st_gid; 3484329Seric } 3494329Seric 3504329Seric if (euid == 0) 3514329Seric return (TRUE); 3524329Seric bits = S_IWRITE; 3534329Seric if (euid != s->st_uid) 3544329Seric { 3554329Seric bits >>= 3; 3564329Seric if (egid != s->st_gid) 3574329Seric bits >>= 3; 3584329Seric } 3594329Seric return ((s->st_mode & bits) != 0); 3604329Seric } 3614329Seric /* 3624174Seric ** INCLUDE -- handle :include: specification. 3634174Seric ** 3644174Seric ** Parameters: 3654174Seric ** fname -- filename to include. 3664176Seric ** msg -- message to print in verbose mode. 3674399Seric ** ctladdr -- address template to use to fill in these 3684399Seric ** addresses -- effective user/group id are 3694399Seric ** the important things. 3704174Seric ** 3714174Seric ** Returns: 3724174Seric ** none. 3734174Seric ** 3744174Seric ** Side Effects: 3754174Seric ** reads the :include: file and sends to everyone 3764174Seric ** listed in that file. 3774174Seric */ 3784174Seric 3794399Seric include(fname, msg, ctladdr) 3804174Seric char *fname; 3814176Seric char *msg; 3824399Seric ADDRESS *ctladdr; 3834174Seric { 3844174Seric char buf[MAXLINE]; 3854174Seric register FILE *fp; 3864178Seric char *oldto = To; 387*4404Seric struct stat st; 3884174Seric 3894174Seric fp = fopen(fname, "r"); 3904174Seric if (fp == NULL) 3914174Seric { 3924174Seric usrerr("Cannot open %s", fname); 3934174Seric return; 3944174Seric } 395*4404Seric if (fstat(fileno(fp), &st) < 0) 396*4404Seric syserr("Cannot fstat %s!", fname); 397*4404Seric ctladdr->q_uid = st.st_uid; 398*4404Seric ctladdr->q_gid = st.st_gid; 399*4404Seric ctladdr->q_flags |= QGOODUID; 4004174Seric 4014174Seric /* read the file -- each line is a comma-separated list. */ 4024174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4034174Seric { 4044174Seric register char *p = index(buf, '\n'); 4054174Seric 4064174Seric if (p != NULL) 4074174Seric *p = '\0'; 4084174Seric if (buf[0] == '\0') 4094174Seric continue; 4104178Seric To = oldto; 4114174Seric if (Verbose) 4124176Seric message(Arpa_Info, "%s to %s", msg, buf); 4134176Seric AliasLevel++; 4144399Seric sendto(buf, 1, ctladdr); 4154176Seric AliasLevel--; 4164174Seric } 4174174Seric 4184319Seric (void) fclose(fp); 4194174Seric } 4204324Seric /* 4214324Seric ** SENDTOARGV -- send to an argument vector. 4224324Seric ** 4234324Seric ** Parameters: 4244324Seric ** argv -- argument vector to send to. 4254324Seric ** 4264324Seric ** Returns: 4274324Seric ** none. 4284324Seric ** 4294324Seric ** Side Effects: 4304324Seric ** puts all addresses on the argument vector onto the 4314324Seric ** send queue. 4324324Seric */ 4334324Seric 4344324Seric sendtoargv(argv) 4354324Seric register char **argv; 4364324Seric { 4374324Seric register char *p; 4384324Seric extern bool sameword(); 4394324Seric 4404324Seric while ((p = *argv++) != NULL) 4414324Seric { 4424324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 4434324Seric { 4444324Seric char nbuf[MAXNAME]; 4454324Seric 4464324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 4474324Seric usrerr("address overflow"); 4484324Seric else 4494324Seric { 4504324Seric (void) strcpy(nbuf, p); 4514324Seric (void) strcat(nbuf, "@"); 4524324Seric (void) strcat(nbuf, argv[1]); 4534324Seric p = newstr(nbuf); 4544324Seric argv += 2; 4554324Seric } 4564324Seric } 4574402Seric sendto(p, 0, NULL); 4584324Seric } 4594324Seric } 4604399Seric /* 4614399Seric ** GETCTLADDR -- get controlling address from an address header. 4624399Seric ** 4634399Seric ** If none, get one corresponding to the effective userid. 4644399Seric ** 4654399Seric ** Parameters: 4664399Seric ** a -- the address to find the controller of. 4674399Seric ** 4684399Seric ** Returns: 4694399Seric ** the controlling address. 4704399Seric ** 4714399Seric ** Side Effects: 4724399Seric ** none. 4734399Seric */ 4744399Seric 4754399Seric ADDRESS * 4764399Seric getctladdr(a) 4774399Seric register ADDRESS *a; 4784399Seric { 479*4404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 4804399Seric a = a->q_alias; 4814399Seric return (a); 4824399Seric } 483