14174Seric # include <pwd.h> 24329Seric # include <sys/types.h> 34329Seric # include <sys/stat.h> 44174Seric # include "sendmail.h" 54174Seric 6*4444Seric static char SccsId[] = "@(#)recipient.c 3.24 09/28/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 */ 374423Seric bool firstone; /* set on first address sent */ 38*4444Seric bool selfref; /* set if this list includes ctladdr */ 394174Seric 404324Seric # ifdef DEBUG 414324Seric if (Debug > 1) 42*4444Seric { 43*4444Seric printf("sendto: %s\n ctladdr=", list); 44*4444Seric printaddr(ctladdr, FALSE); 45*4444Seric } 464324Seric # endif DEBUG 474324Seric 484174Seric more = TRUE; 494423Seric firstone = TRUE; 50*4444Seric selfref = FALSE; 514324Seric al = NULL; 524174Seric for (p = list; more; ) 534174Seric { 544319Seric register char *q; 554319Seric register char c; 564319Seric ADDRESS *a; 574319Seric 584174Seric /* find the end of this address */ 594174Seric while (*p == ' ' || *p == '\t') 604174Seric p++; 614174Seric q = p; 624174Seric while ((c = *p++) != '\0' && c != ',' && c != '\n') 634174Seric continue; 644174Seric more = c != '\0'; 654174Seric *--p = '\0'; 664174Seric if (more) 674174Seric p++; 684324Seric if (*q == '\0') 694324Seric continue; 704174Seric 714174Seric /* parse the address */ 724174Seric if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL) 734174Seric continue; 744324Seric a->q_next = al; 754399Seric a->q_alias = ctladdr; 76*4444Seric 77*4444Seric /* see if this should be marked as a primary address */ 784423Seric if (ctladdr == NULL || 794423Seric (firstone && !more && bitset(QPRIMARY, ctladdr->q_flags))) 804423Seric a->q_flags |= QPRIMARY; 81*4444Seric 82*4444Seric /* put on send queue or suppress self-reference */ 83*4444Seric if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE)) 84*4444Seric selfref = TRUE; 85*4444Seric else 86*4444Seric al = a; 874423Seric firstone = FALSE; 884324Seric } 894324Seric 90*4444Seric /* if this alias doesn't include itself, delete ctladdr */ 91*4444Seric if (!selfref && ctladdr != NULL) 92*4444Seric ctladdr->q_flags |= QDONTSEND; 93*4444Seric 944324Seric /* arrange to send to everyone on the local send list */ 954324Seric while (al != NULL) 964324Seric { 974324Seric register ADDRESS *a = al; 984324Seric 994324Seric al = a->q_next; 1004174Seric recipient(a); 1014174Seric } 1024324Seric 1034174Seric To = NULL; 1044174Seric } 1054174Seric /* 1064174Seric ** RECIPIENT -- Designate a message recipient 1074174Seric ** 1084174Seric ** Saves the named person for future mailing. 1094174Seric ** 1104174Seric ** Parameters: 1114174Seric ** a -- the (preparsed) address header for the recipient. 1124174Seric ** 1134174Seric ** Returns: 1144174Seric ** none. 1154174Seric ** 1164174Seric ** Side Effects: 1174174Seric ** none. 1184174Seric */ 1194174Seric 1204174Seric recipient(a) 1214174Seric register ADDRESS *a; 1224174Seric { 1234174Seric register ADDRESS *q; 1244319Seric ADDRESS **pq; 1254174Seric register struct mailer *m; 1264399Seric extern ADDRESS *getctladdr(); 1274174Seric 1284174Seric To = a->q_paddr; 1294174Seric m = Mailer[a->q_mailer]; 1304174Seric errno = 0; 1314174Seric # ifdef DEBUG 1324174Seric if (Debug) 133*4444Seric { 134*4444Seric printf("\nrecipient: "); 135*4444Seric printaddr(a, FALSE); 136*4444Seric } 1374174Seric # endif DEBUG 1384174Seric 1394174Seric /* break aliasing loops */ 1404174Seric if (AliasLevel > MAXRCRSN) 1414174Seric { 1424174Seric usrerr("aliasing/forwarding loop broken"); 1434174Seric return; 1444174Seric } 1454174Seric 1464174Seric /* 1474174Seric ** Do sickly crude mapping for program mailing, etc. 1484174Seric */ 1494174Seric 1504197Seric if (a->q_mailer == MN_LOCAL) 1514174Seric { 1524174Seric if (a->q_user[0] == '|') 1534174Seric { 1544197Seric a->q_mailer = MN_PROG; 1554197Seric m = Mailer[MN_PROG]; 1564174Seric a->q_user++; 1574406Seric if (a->q_alias == NULL && Debug == 0) 1584217Seric { 1594217Seric usrerr("Cannot mail directly to programs"); 1604217Seric a->q_flags |= QDONTSEND; 1614217Seric } 1624174Seric } 1634174Seric } 1644174Seric 1654174Seric /* 1664419Seric ** Look up this person in the recipient list. 1674419Seric ** If they are there already, return, otherwise continue. 1684419Seric ** If the list is empty, just add it. Notice the cute 1694419Seric ** hack to make from addresses suppress things correctly: 1704419Seric ** the QDONTSEND bit will be set in the send list. 1714419Seric ** [Please note: the emphasis is on "hack."] 1724174Seric */ 1734174Seric 1744319Seric for (pq = &m->m_sendq; (q = *pq) != NULL; pq = &q->q_next) 1754174Seric { 1764319Seric if (!ForceMail && sameaddr(q, a, FALSE)) 1774174Seric { 1784174Seric # ifdef DEBUG 1794319Seric if (Debug) 180*4444Seric { 181*4444Seric printf("%s in sendq: ", a->q_paddr); 182*4444Seric printaddr(q, FALSE); 183*4444Seric } 1844174Seric # endif DEBUG 1854319Seric if (Verbose && !bitset(QDONTSEND, a->q_flags)) 1864324Seric message(Arpa_Info, "duplicate suppressed"); 1874423Seric if (!bitset(QPRIMARY, q->q_flags)) 1884423Seric q->q_flags |= a->q_flags; 1894319Seric return; 1904174Seric } 1914319Seric } 1924174Seric 1934319Seric /* add address on list */ 1944319Seric *pq = a; 1954174Seric a->q_next = NULL; 1964247Seric if (DontSend) 1974247Seric a->q_flags |= QDONTSEND; 1984174Seric 1994174Seric /* 2004174Seric ** Alias the name and handle :include: specs. 2014174Seric */ 2024174Seric 2034197Seric if (a->q_mailer == MN_LOCAL) 2044174Seric { 2054174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2064174Seric { 2074174Seric a->q_flags |= QDONTSEND; 2084406Seric if (a->q_alias == NULL && Debug == 0) 2094399Seric usrerr("Cannot mail directly to :include:s"); 2104399Seric else 2114399Seric { 2124399Seric if (Verbose) 2134399Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2144399Seric include(&a->q_user[9], " sending", a); 2154399Seric } 2164174Seric } 2174174Seric else 2184174Seric alias(a); 2194174Seric } 2204174Seric 2214174Seric /* 2224174Seric ** If the user is local and still being sent, verify that 2234174Seric ** the address is good. If it is, try to forward. 2244174Seric ** If the address is already good, we have a forwarding 2254174Seric ** loop. This can be broken by just sending directly to 2264174Seric ** the user (which is probably correct anyway). 2274174Seric */ 2284174Seric 2294197Seric if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == MN_LOCAL) 2304174Seric { 2314174Seric char buf[MAXNAME]; 2324201Seric register char *p; 2334329Seric struct stat stb; 2344329Seric extern bool writable(); 2354399Seric bool quoted = FALSE; 2364174Seric 2374174Seric strcpy(buf, a->q_user); 2384399Seric for (p = buf; *p != '\0' && !quoted; p++) 2394399Seric { 2404399Seric if (!isascii(*p)) 2414399Seric quoted = TRUE; 2424399Seric } 2434174Seric stripquotes(buf, TRUE); 2444174Seric 2454174Seric /* see if this is to a file */ 2464201Seric if ((p = rindex(buf, '/')) != NULL) 2474174Seric { 2484201Seric /* check if writable or creatable */ 2494406Seric if (a->q_alias == NULL && Debug == 0) 2504399Seric { 2514399Seric usrerr("Cannot mail directly to files"); 2524399Seric a->q_flags |= QDONTSEND; 2534399Seric } 2544399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2554201Seric (*p = '\0', access(buf, 3) < 0)) 2564174Seric { 2574174Seric a->q_flags |= QBADADDR; 2584174Seric giveresponse(EX_CANTCREAT, TRUE, m); 2594174Seric } 2604174Seric } 2614174Seric else 2624174Seric { 2634174Seric register struct passwd *pw; 2644373Seric extern struct passwd *finduser(); 2654373Seric 2664407Seric /* warning -- finduser may trash buf */ 2674373Seric pw = finduser(buf); 2684174Seric if (pw == NULL) 2694174Seric { 2704174Seric a->q_flags |= QBADADDR; 2714174Seric giveresponse(EX_NOUSER, TRUE, m); 2724174Seric } 2734174Seric else 2744174Seric { 2754376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 2764376Seric { 2774376Seric a->q_user = newstr(pw->pw_name); 2784376Seric strcpy(buf, pw->pw_name); 2794376Seric } 2804174Seric a->q_home = newstr(pw->pw_dir); 2814213Seric a->q_uid = pw->pw_uid; 2824399Seric a->q_gid = pw->pw_gid; 2834404Seric a->q_flags |= QGOODUID; 2844399Seric if (!quoted) 2854174Seric forward(a); 2864174Seric } 2874174Seric } 2884174Seric } 2894174Seric } 2904174Seric /* 2914373Seric ** FINDUSER -- find the password entry for a user. 2924373Seric ** 2934373Seric ** This looks a lot like getpwnam, except that it may want to 2944373Seric ** do some fancier pattern matching in /etc/passwd. 2954373Seric ** 2964373Seric ** Parameters: 2974373Seric ** name -- the name to match against. 2984373Seric ** 2994373Seric ** Returns: 3004373Seric ** A pointer to a pw struct. 3014373Seric ** NULL if name is unknown or ambiguous. 3024373Seric ** 3034373Seric ** Side Effects: 3044407Seric ** may modify name. 3054373Seric */ 3064373Seric 3074373Seric struct passwd * 3084373Seric finduser(name) 3094373Seric char *name; 3104373Seric { 3114376Seric extern struct passwd *getpwent(); 3124376Seric register struct passwd *pw; 3134407Seric register char *p; 3144373Seric 3154407Seric /* 3164407Seric ** Make name canonical. 3174407Seric */ 3184407Seric 3194407Seric for (p = name; *p != '\0'; p++) 3204407Seric { 3214407Seric if (*p == (SPACESUB & 0177) || *p == '_') 3224407Seric *p = ' '; 3234407Seric } 3244407Seric 3254376Seric setpwent(); 3264376Seric while ((pw = getpwent()) != NULL) 3274376Seric { 3284376Seric char buf[MAXNAME]; 3294376Seric extern bool sameword(); 3304376Seric 3314376Seric if (strcmp(pw->pw_name, name) == 0) 3324376Seric return (pw); 3334376Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 3344407Seric if (index(buf, ' ') != NULL && sameword(buf, name)) 3354381Seric { 3364377Seric if (Verbose) 3374381Seric message(Arpa_Info, "sending to login name %s", 3384381Seric pw->pw_name); 3394376Seric return (pw); 3404377Seric } 3414376Seric } 3424376Seric return (NULL); 3434373Seric } 3444373Seric /* 3454329Seric ** WRITABLE -- predicate returning if the file is writable. 3464329Seric ** 3474329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3484329Seric ** Unfortunately, we cannot use the access call since we 3494329Seric ** won't necessarily be the real uid when we try to 3504329Seric ** actually open the file. 3514329Seric ** 3524329Seric ** Notice that ANY file with ANY execute bit is automatically 3534329Seric ** not writable. This is also enforced by mailfile. 3544329Seric ** 3554329Seric ** Parameters: 3564329Seric ** s -- pointer to a stat struct for the file. 3574329Seric ** 3584329Seric ** Returns: 3594329Seric ** TRUE -- if we will be able to write this file. 3604329Seric ** FALSE -- if we cannot write this file. 3614329Seric ** 3624329Seric ** Side Effects: 3634329Seric ** none. 3644329Seric */ 3654329Seric 3664329Seric bool 3674329Seric writable(s) 3684329Seric register struct stat *s; 3694329Seric { 3704329Seric int euid, egid; 3714329Seric int bits; 3724329Seric 3734329Seric if (bitset(0111, s->st_mode)) 3744329Seric return (FALSE); 3754329Seric euid = getruid(); 3764329Seric egid = getrgid(); 3774329Seric if (geteuid() == 0) 3784329Seric { 3794329Seric if (bitset(S_ISUID, s->st_mode)) 3804329Seric euid = s->st_uid; 3814329Seric if (bitset(S_ISGID, s->st_mode)) 3824329Seric egid = s->st_gid; 3834329Seric } 3844329Seric 3854329Seric if (euid == 0) 3864329Seric return (TRUE); 3874329Seric bits = S_IWRITE; 3884329Seric if (euid != s->st_uid) 3894329Seric { 3904329Seric bits >>= 3; 3914329Seric if (egid != s->st_gid) 3924329Seric bits >>= 3; 3934329Seric } 3944329Seric return ((s->st_mode & bits) != 0); 3954329Seric } 3964329Seric /* 3974174Seric ** INCLUDE -- handle :include: specification. 3984174Seric ** 3994174Seric ** Parameters: 4004174Seric ** fname -- filename to include. 4014176Seric ** msg -- message to print in verbose mode. 4024399Seric ** ctladdr -- address template to use to fill in these 4034399Seric ** addresses -- effective user/group id are 4044399Seric ** the important things. 4054174Seric ** 4064174Seric ** Returns: 4074174Seric ** none. 4084174Seric ** 4094174Seric ** Side Effects: 4104174Seric ** reads the :include: file and sends to everyone 4114174Seric ** listed in that file. 4124174Seric */ 4134174Seric 4144399Seric include(fname, msg, ctladdr) 4154174Seric char *fname; 4164176Seric char *msg; 4174399Seric ADDRESS *ctladdr; 4184174Seric { 4194174Seric char buf[MAXLINE]; 4204174Seric register FILE *fp; 4214178Seric char *oldto = To; 4224174Seric 4234174Seric fp = fopen(fname, "r"); 4244174Seric if (fp == NULL) 4254174Seric { 4264174Seric usrerr("Cannot open %s", fname); 4274174Seric return; 4284174Seric } 4294406Seric if (getctladdr(ctladdr) == NULL) 4304406Seric { 4314406Seric struct stat st; 4324174Seric 4334406Seric if (fstat(fileno(fp), &st) < 0) 4344406Seric syserr("Cannot fstat %s!", fname); 4354406Seric ctladdr->q_uid = st.st_uid; 4364406Seric ctladdr->q_gid = st.st_gid; 4374406Seric ctladdr->q_flags |= QGOODUID; 4384406Seric } 4394406Seric 4404174Seric /* read the file -- each line is a comma-separated list. */ 4414174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4424174Seric { 4434174Seric register char *p = index(buf, '\n'); 4444174Seric 4454174Seric if (p != NULL) 4464174Seric *p = '\0'; 4474174Seric if (buf[0] == '\0') 4484174Seric continue; 4494178Seric To = oldto; 4504174Seric if (Verbose) 4514176Seric message(Arpa_Info, "%s to %s", msg, buf); 4524176Seric AliasLevel++; 4534399Seric sendto(buf, 1, ctladdr); 4544176Seric AliasLevel--; 4554174Seric } 4564174Seric 4574319Seric (void) fclose(fp); 4584174Seric } 4594324Seric /* 4604324Seric ** SENDTOARGV -- send to an argument vector. 4614324Seric ** 4624324Seric ** Parameters: 4634324Seric ** argv -- argument vector to send to. 4644324Seric ** 4654324Seric ** Returns: 4664324Seric ** none. 4674324Seric ** 4684324Seric ** Side Effects: 4694324Seric ** puts all addresses on the argument vector onto the 4704324Seric ** send queue. 4714324Seric */ 4724324Seric 4734324Seric sendtoargv(argv) 4744324Seric register char **argv; 4754324Seric { 4764324Seric register char *p; 4774324Seric extern bool sameword(); 4784324Seric 4794324Seric while ((p = *argv++) != NULL) 4804324Seric { 4814324Seric if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at")) 4824324Seric { 4834324Seric char nbuf[MAXNAME]; 4844324Seric 4854324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 4864324Seric usrerr("address overflow"); 4874324Seric else 4884324Seric { 4894324Seric (void) strcpy(nbuf, p); 4904324Seric (void) strcat(nbuf, "@"); 4914324Seric (void) strcat(nbuf, argv[1]); 4924324Seric p = newstr(nbuf); 4934324Seric argv += 2; 4944324Seric } 4954324Seric } 4964402Seric sendto(p, 0, NULL); 4974324Seric } 4984324Seric } 4994399Seric /* 5004399Seric ** GETCTLADDR -- get controlling address from an address header. 5014399Seric ** 5024399Seric ** If none, get one corresponding to the effective userid. 5034399Seric ** 5044399Seric ** Parameters: 5054399Seric ** a -- the address to find the controller of. 5064399Seric ** 5074399Seric ** Returns: 5084399Seric ** the controlling address. 5094399Seric ** 5104399Seric ** Side Effects: 5114399Seric ** none. 5124399Seric */ 5134399Seric 5144399Seric ADDRESS * 5154399Seric getctladdr(a) 5164399Seric register ADDRESS *a; 5174399Seric { 5184404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5194399Seric a = a->q_alias; 5204399Seric return (a); 5214399Seric } 522