122710Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 6*42829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822710Sdist 922710Sdist #ifndef lint 10*42829Sbostic static char sccsid[] = "@(#)recipient.c 5.18 (Berkeley) 06/01/90"; 1133731Sbostic #endif /* not lint */ 1222710Sdist 1336928Sbostic # include <sys/types.h> 1436928Sbostic # include <sys/stat.h> 154174Seric # include <pwd.h> 164627Seric # include "sendmail.h" 174174Seric 184174Seric /* 199622Seric ** SENDTOLIST -- Designate a send list. 204174Seric ** 214174Seric ** The parameter is a comma-separated list of people to send to. 224174Seric ** This routine arranges to send to all of them. 234174Seric ** 244174Seric ** Parameters: 254174Seric ** list -- the send list. 264399Seric ** ctladdr -- the address template for the person to 274399Seric ** send to -- effective uid/gid are important. 285006Seric ** This is typically the alias that caused this 295006Seric ** expansion. 305006Seric ** sendq -- a pointer to the head of a queue to put 315006Seric ** these people into. 324174Seric ** 334174Seric ** Returns: 344998Seric ** none 354174Seric ** 364174Seric ** Side Effects: 374174Seric ** none. 384174Seric */ 394174Seric 404174Seric # define MAXRCRSN 10 414174Seric 429622Seric sendtolist(list, ctladdr, sendq) 434174Seric char *list; 444399Seric ADDRESS *ctladdr; 455198Seric ADDRESS **sendq; 464174Seric { 474174Seric register char *p; 488223Seric register ADDRESS *al; /* list of addresses to send to */ 494423Seric bool firstone; /* set on first address sent */ 504444Seric bool selfref; /* set if this list includes ctladdr */ 5111446Seric char delimiter; /* the address delimiter */ 524174Seric 537676Seric if (tTd(25, 1)) 544444Seric { 554444Seric printf("sendto: %s\n ctladdr=", list); 564444Seric printaddr(ctladdr, FALSE); 574444Seric } 584324Seric 598223Seric /* heuristic to determine old versus new style addresses */ 608230Seric if (ctladdr == NULL && 618230Seric (index(list, ',') != NULL || index(list, ';') != NULL || 628230Seric index(list, '<') != NULL || index(list, '(') != NULL)) 639340Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 6411446Seric delimiter = ' '; 6511446Seric if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL) 6611446Seric delimiter = ','; 678223Seric 684423Seric firstone = TRUE; 694444Seric selfref = FALSE; 704324Seric al = NULL; 718223Seric 728081Seric for (p = list; *p != '\0'; ) 734174Seric { 748081Seric register ADDRESS *a; 758081Seric extern char *DelimChar; /* defined in prescan */ 764319Seric 778081Seric /* parse the address */ 788081Seric while (isspace(*p) || *p == ',') 794174Seric p++; 8011446Seric a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter); 819297Seric p = DelimChar; 829297Seric if (a == NULL) 834174Seric continue; 844324Seric a->q_next = al; 854399Seric a->q_alias = ctladdr; 864444Seric 874444Seric /* see if this should be marked as a primary address */ 884423Seric if (ctladdr == NULL || 898081Seric (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 904423Seric a->q_flags |= QPRIMARY; 914444Seric 924444Seric /* put on send queue or suppress self-reference */ 939379Seric if (ctladdr != NULL && sameaddr(ctladdr, a)) 944444Seric selfref = TRUE; 954444Seric else 964444Seric al = a; 974423Seric firstone = FALSE; 984324Seric } 994324Seric 1004444Seric /* if this alias doesn't include itself, delete ctladdr */ 1014444Seric if (!selfref && ctladdr != NULL) 1024444Seric ctladdr->q_flags |= QDONTSEND; 1034444Seric 1044324Seric /* arrange to send to everyone on the local send list */ 1054324Seric while (al != NULL) 1064324Seric { 1074324Seric register ADDRESS *a = al; 10812613Seric extern ADDRESS *recipient(); 1094324Seric 1104324Seric al = a->q_next; 11140973Sbostic setctladdr(a); 11212613Seric a = recipient(a, sendq); 1134993Seric 1144998Seric /* arrange to inherit full name */ 1154998Seric if (a->q_fullname == NULL && ctladdr != NULL) 1164998Seric a->q_fullname = ctladdr->q_fullname; 1174174Seric } 1184324Seric 1196906Seric CurEnv->e_to = NULL; 1204174Seric } 1214174Seric /* 1224174Seric ** RECIPIENT -- Designate a message recipient 1234174Seric ** 1244174Seric ** Saves the named person for future mailing. 1254174Seric ** 1264174Seric ** Parameters: 1274174Seric ** a -- the (preparsed) address header for the recipient. 1285006Seric ** sendq -- a pointer to the head of a queue to put the 1295006Seric ** recipient in. Duplicate supression is done 1305006Seric ** in this queue. 1314174Seric ** 1324174Seric ** Returns: 13312613Seric ** The actual address in the queue. This will be "a" if 13412613Seric ** the address is not a duplicate, else the original address. 1354174Seric ** 1364174Seric ** Side Effects: 1374174Seric ** none. 1384174Seric */ 1394174Seric 14012613Seric ADDRESS * 1415006Seric recipient(a, sendq) 1424174Seric register ADDRESS *a; 1435006Seric register ADDRESS **sendq; 1444174Seric { 1454174Seric register ADDRESS *q; 1464319Seric ADDRESS **pq; 1474174Seric register struct mailer *m; 1489210Seric register char *p; 1499210Seric bool quoted = FALSE; /* set if the addr has a quote bit */ 1509210Seric char buf[MAXNAME]; /* unquoted image of the user name */ 1514399Seric extern ADDRESS *getctladdr(); 1524627Seric extern bool safefile(); 1534174Seric 1546906Seric CurEnv->e_to = a->q_paddr; 1554600Seric m = a->q_mailer; 1564174Seric errno = 0; 1577676Seric if (tTd(26, 1)) 1584444Seric { 1594444Seric printf("\nrecipient: "); 1604444Seric printaddr(a, FALSE); 1614444Seric } 1624174Seric 1634174Seric /* break aliasing loops */ 1644174Seric if (AliasLevel > MAXRCRSN) 1654174Seric { 1664174Seric usrerr("aliasing/forwarding loop broken"); 16712613Seric return (a); 1684174Seric } 1694174Seric 1704174Seric /* 1714627Seric ** Finish setting up address structure. 1724174Seric */ 1734174Seric 17416160Seric /* set the queue timeout */ 1754627Seric a->q_timeout = TimeOut; 1764627Seric 17716160Seric /* map user & host to lower case if requested on non-aliases */ 17816160Seric if (a->q_alias == NULL) 17916160Seric loweraddr(a); 18016160Seric 18116160Seric /* get unquoted user for file, program or user.name check */ 1829210Seric (void) strcpy(buf, a->q_user); 1839210Seric for (p = buf; *p != '\0' && !quoted; p++) 1849210Seric { 1859210Seric if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377)) 1869210Seric quoted = TRUE; 1879210Seric } 1889210Seric stripquotes(buf, TRUE); 1899210Seric 1904627Seric /* do sickly crude mapping for program mailing, etc. */ 1919210Seric if (m == LocalMailer && buf[0] == '|') 1924174Seric { 1939210Seric a->q_mailer = m = ProgMailer; 1949210Seric a->q_user++; 19536231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 1964174Seric { 19729915Seric a->q_flags |= QDONTSEND|QBADADDR; 1989210Seric usrerr("Cannot mail directly to programs"); 1994174Seric } 2004174Seric } 2014174Seric 2024174Seric /* 2034419Seric ** Look up this person in the recipient list. 2044419Seric ** If they are there already, return, otherwise continue. 2054419Seric ** If the list is empty, just add it. Notice the cute 2064419Seric ** hack to make from addresses suppress things correctly: 2074419Seric ** the QDONTSEND bit will be set in the send list. 2084419Seric ** [Please note: the emphasis is on "hack."] 2094174Seric */ 2104174Seric 2115006Seric for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 2124174Seric { 2139379Seric if (!ForceMail && sameaddr(q, a)) 2144174Seric { 2157676Seric if (tTd(26, 1)) 2164444Seric { 2174444Seric printf("%s in sendq: ", a->q_paddr); 2184444Seric printaddr(q, FALSE); 2194444Seric } 2207054Seric if (!bitset(QDONTSEND, a->q_flags)) 2214324Seric message(Arpa_Info, "duplicate suppressed"); 2224423Seric if (!bitset(QPRIMARY, q->q_flags)) 2234423Seric q->q_flags |= a->q_flags; 22412613Seric return (q); 2254174Seric } 2264319Seric } 2274174Seric 2284319Seric /* add address on list */ 2294319Seric *pq = a; 2304174Seric a->q_next = NULL; 23124951Seric CurEnv->e_nrcpts++; 2324174Seric 2334174Seric /* 2344174Seric ** Alias the name and handle :include: specs. 2354174Seric */ 2364174Seric 2379210Seric if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags)) 2384174Seric { 2394174Seric if (strncmp(a->q_user, ":include:", 9) == 0) 2404174Seric { 2414174Seric a->q_flags |= QDONTSEND; 24236231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 24329915Seric { 24429915Seric a->q_flags |= QBADADDR; 2454399Seric usrerr("Cannot mail directly to :include:s"); 24629915Seric } 2474399Seric else 2484399Seric { 2497054Seric message(Arpa_Info, "including file %s", &a->q_user[9]); 2505006Seric include(&a->q_user[9], " sending", a, sendq); 2514399Seric } 2524174Seric } 2534174Seric else 2545006Seric alias(a, sendq); 2554174Seric } 2564174Seric 2574174Seric /* 2584174Seric ** If the user is local and still being sent, verify that 2594174Seric ** the address is good. If it is, try to forward. 2604174Seric ** If the address is already good, we have a forwarding 2614174Seric ** loop. This can be broken by just sending directly to 2624174Seric ** the user (which is probably correct anyway). 2634174Seric */ 2644174Seric 2659210Seric if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer) 2664174Seric { 2674329Seric struct stat stb; 2684329Seric extern bool writable(); 2694174Seric 2704174Seric /* see if this is to a file */ 2715600Seric if (buf[0] == '/') 2724174Seric { 2735600Seric p = rindex(buf, '/'); 2744201Seric /* check if writable or creatable */ 27536231Sbostic if (a->q_alias == NULL && !QueueRun && !ForceMail) 2764399Seric { 27729915Seric a->q_flags |= QDONTSEND|QBADADDR; 2784399Seric usrerr("Cannot mail directly to files"); 2794399Seric } 2804399Seric else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) : 2814539Seric (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC))) 2824174Seric { 2834174Seric a->q_flags |= QBADADDR; 28410109Seric giveresponse(EX_CANTCREAT, m, CurEnv); 2854174Seric } 2864174Seric } 2874174Seric else 2884174Seric { 2894174Seric register struct passwd *pw; 2904373Seric extern struct passwd *finduser(); 2914373Seric 2924407Seric /* warning -- finduser may trash buf */ 2934373Seric pw = finduser(buf); 2944174Seric if (pw == NULL) 2954174Seric { 2964174Seric a->q_flags |= QBADADDR; 29710109Seric giveresponse(EX_NOUSER, m, CurEnv); 2984174Seric } 2994174Seric else 3004174Seric { 3014993Seric char nbuf[MAXNAME]; 3024993Seric 3034376Seric if (strcmp(a->q_user, pw->pw_name) != 0) 3044376Seric { 3054376Seric a->q_user = newstr(pw->pw_name); 3067008Seric (void) strcpy(buf, pw->pw_name); 3074376Seric } 3084174Seric a->q_home = newstr(pw->pw_dir); 3094213Seric a->q_uid = pw->pw_uid; 3104399Seric a->q_gid = pw->pw_gid; 3114404Seric a->q_flags |= QGOODUID; 3124998Seric buildfname(pw->pw_gecos, pw->pw_name, nbuf); 3134993Seric if (nbuf[0] != '\0') 3144993Seric a->q_fullname = newstr(nbuf); 3154399Seric if (!quoted) 3165006Seric forward(a, sendq); 3174174Seric } 3184174Seric } 3194174Seric } 32012613Seric return (a); 3214174Seric } 3224174Seric /* 3234373Seric ** FINDUSER -- find the password entry for a user. 3244373Seric ** 3254373Seric ** This looks a lot like getpwnam, except that it may want to 3264373Seric ** do some fancier pattern matching in /etc/passwd. 3274373Seric ** 3289379Seric ** This routine contains most of the time of many sendmail runs. 3299379Seric ** It deserves to be optimized. 3309379Seric ** 3314373Seric ** Parameters: 3324373Seric ** name -- the name to match against. 3334373Seric ** 3344373Seric ** Returns: 3354373Seric ** A pointer to a pw struct. 3364373Seric ** NULL if name is unknown or ambiguous. 3374373Seric ** 3384373Seric ** Side Effects: 3394407Seric ** may modify name. 3404373Seric */ 3414373Seric 3424373Seric struct passwd * 3434373Seric finduser(name) 3444373Seric char *name; 3454373Seric { 3464376Seric register struct passwd *pw; 3474407Seric register char *p; 34815325Seric extern struct passwd *getpwent(); 34915325Seric extern struct passwd *getpwnam(); 3504373Seric 35125777Seric /* map upper => lower case */ 3524407Seric for (p = name; *p != '\0'; p++) 3534407Seric { 35425777Seric if (isascii(*p) && isupper(*p)) 35525568Seric *p = tolower(*p); 3564407Seric } 3574407Seric 35825777Seric /* look up this login name using fast path */ 35912634Seric if ((pw = getpwnam(name)) != NULL) 36012634Seric return (pw); 36112634Seric 36212634Seric /* search for a matching full name instead */ 36325777Seric for (p = name; *p != '\0'; p++) 36425777Seric { 36525777Seric if (*p == (SpaceSub & 0177) || *p == '_') 36625777Seric *p = ' '; 36725777Seric } 36823107Seric (void) setpwent(); 3694376Seric while ((pw = getpwent()) != NULL) 3704376Seric { 3714998Seric char buf[MAXNAME]; 3724376Seric 3734998Seric buildfname(pw->pw_gecos, pw->pw_name, buf); 37433725Sbostic if (index(buf, ' ') != NULL && !strcasecmp(buf, name)) 3754381Seric { 3767054Seric message(Arpa_Info, "sending to login name %s", pw->pw_name); 3774376Seric return (pw); 3784377Seric } 3794376Seric } 3804376Seric return (NULL); 3814373Seric } 3824373Seric /* 3834329Seric ** WRITABLE -- predicate returning if the file is writable. 3844329Seric ** 3854329Seric ** This routine must duplicate the algorithm in sys/fio.c. 3864329Seric ** Unfortunately, we cannot use the access call since we 3874329Seric ** won't necessarily be the real uid when we try to 3884329Seric ** actually open the file. 3894329Seric ** 3904329Seric ** Notice that ANY file with ANY execute bit is automatically 3914329Seric ** not writable. This is also enforced by mailfile. 3924329Seric ** 3934329Seric ** Parameters: 3944329Seric ** s -- pointer to a stat struct for the file. 3954329Seric ** 3964329Seric ** Returns: 3974329Seric ** TRUE -- if we will be able to write this file. 3984329Seric ** FALSE -- if we cannot write this file. 3994329Seric ** 4004329Seric ** Side Effects: 4014329Seric ** none. 4024329Seric */ 4034329Seric 4044329Seric bool 4054329Seric writable(s) 4064329Seric register struct stat *s; 4074329Seric { 4084329Seric int euid, egid; 4094329Seric int bits; 4104329Seric 4114329Seric if (bitset(0111, s->st_mode)) 4124329Seric return (FALSE); 4134329Seric euid = getruid(); 4144329Seric egid = getrgid(); 4154329Seric if (geteuid() == 0) 4164329Seric { 4174329Seric if (bitset(S_ISUID, s->st_mode)) 4184329Seric euid = s->st_uid; 4194329Seric if (bitset(S_ISGID, s->st_mode)) 4204329Seric egid = s->st_gid; 4214329Seric } 4224329Seric 4234329Seric if (euid == 0) 4244329Seric return (TRUE); 4254329Seric bits = S_IWRITE; 4264329Seric if (euid != s->st_uid) 4274329Seric { 4284329Seric bits >>= 3; 4294329Seric if (egid != s->st_gid) 4304329Seric bits >>= 3; 4314329Seric } 4324329Seric return ((s->st_mode & bits) != 0); 4334329Seric } 4344329Seric /* 4354174Seric ** INCLUDE -- handle :include: specification. 4364174Seric ** 4374174Seric ** Parameters: 4384174Seric ** fname -- filename to include. 4394176Seric ** msg -- message to print in verbose mode. 4404399Seric ** ctladdr -- address template to use to fill in these 4414399Seric ** addresses -- effective user/group id are 4424399Seric ** the important things. 4435006Seric ** sendq -- a pointer to the head of the send queue 4445006Seric ** to put these addresses in. 4454174Seric ** 4464174Seric ** Returns: 4474174Seric ** none. 4484174Seric ** 4494174Seric ** Side Effects: 4504174Seric ** reads the :include: file and sends to everyone 4514174Seric ** listed in that file. 4524174Seric */ 4534174Seric 4545006Seric include(fname, msg, ctladdr, sendq) 4554174Seric char *fname; 4564176Seric char *msg; 4574399Seric ADDRESS *ctladdr; 4585006Seric ADDRESS **sendq; 4594174Seric { 4604174Seric char buf[MAXLINE]; 4614174Seric register FILE *fp; 4626906Seric char *oldto = CurEnv->e_to; 4639379Seric char *oldfilename = FileName; 4649379Seric int oldlinenumber = LineNumber; 4654174Seric 4664174Seric fp = fopen(fname, "r"); 4674174Seric if (fp == NULL) 4684174Seric { 4694174Seric usrerr("Cannot open %s", fname); 4704174Seric return; 4714174Seric } 4724406Seric if (getctladdr(ctladdr) == NULL) 4734406Seric { 4744406Seric struct stat st; 4754174Seric 4764406Seric if (fstat(fileno(fp), &st) < 0) 4774406Seric syserr("Cannot fstat %s!", fname); 4784406Seric ctladdr->q_uid = st.st_uid; 4794406Seric ctladdr->q_gid = st.st_gid; 4804406Seric ctladdr->q_flags |= QGOODUID; 4814406Seric } 4824406Seric 4834174Seric /* read the file -- each line is a comma-separated list. */ 4849379Seric FileName = fname; 4859379Seric LineNumber = 0; 4864174Seric while (fgets(buf, sizeof buf, fp) != NULL) 4874174Seric { 4884174Seric register char *p = index(buf, '\n'); 4894174Seric 49040963Sbostic LineNumber++; 4914174Seric if (p != NULL) 4924174Seric *p = '\0'; 4934174Seric if (buf[0] == '\0') 4944174Seric continue; 4956906Seric CurEnv->e_to = oldto; 4967054Seric message(Arpa_Info, "%s to %s", msg, buf); 4974176Seric AliasLevel++; 4989622Seric sendtolist(buf, ctladdr, sendq); 4994176Seric AliasLevel--; 5004174Seric } 5014174Seric 5024319Seric (void) fclose(fp); 5039379Seric FileName = oldfilename; 5049379Seric LineNumber = oldlinenumber; 5054174Seric } 5064324Seric /* 5074324Seric ** SENDTOARGV -- send to an argument vector. 5084324Seric ** 5094324Seric ** Parameters: 5104324Seric ** argv -- argument vector to send to. 5114324Seric ** 5124324Seric ** Returns: 5134324Seric ** none. 5144324Seric ** 5154324Seric ** Side Effects: 5164324Seric ** puts all addresses on the argument vector onto the 5174324Seric ** send queue. 5184324Seric */ 5194324Seric 5204324Seric sendtoargv(argv) 5214324Seric register char **argv; 5224324Seric { 5234324Seric register char *p; 5244324Seric 5254324Seric while ((p = *argv++) != NULL) 5264324Seric { 52733725Sbostic if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at")) 5284324Seric { 5294324Seric char nbuf[MAXNAME]; 5304324Seric 5314324Seric if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf) 5324324Seric usrerr("address overflow"); 5334324Seric else 5344324Seric { 5354324Seric (void) strcpy(nbuf, p); 5364324Seric (void) strcat(nbuf, "@"); 5374324Seric (void) strcat(nbuf, argv[1]); 5384324Seric p = newstr(nbuf); 5394324Seric argv += 2; 5404324Seric } 5414324Seric } 5429622Seric sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 5434324Seric } 5444324Seric } 5454399Seric /* 5464399Seric ** GETCTLADDR -- get controlling address from an address header. 5474399Seric ** 5484399Seric ** If none, get one corresponding to the effective userid. 5494399Seric ** 5504399Seric ** Parameters: 5514399Seric ** a -- the address to find the controller of. 5524399Seric ** 5534399Seric ** Returns: 5544399Seric ** the controlling address. 5554399Seric ** 5564399Seric ** Side Effects: 5574399Seric ** none. 5584399Seric */ 5594399Seric 5604399Seric ADDRESS * 5614399Seric getctladdr(a) 5624399Seric register ADDRESS *a; 5634399Seric { 5644404Seric while (a != NULL && !bitset(QGOODUID, a->q_flags)) 5654399Seric a = a->q_alias; 5664399Seric return (a); 5674399Seric } 568