122710Sdist /*
222710Sdist **  Sendmail
322710Sdist **  Copyright (c) 1983  Eric P. Allman
422710Sdist **  Berkeley, California
522710Sdist **
622710Sdist **  Copyright (c) 1983 Regents of the University of California.
722710Sdist **  All rights reserved.  The Berkeley software License Agreement
822710Sdist **  specifies the terms and conditions for redistribution.
922710Sdist */
1022710Sdist 
1122710Sdist #ifndef lint
12*25777Seric static char	SccsId[] = "@(#)recipient.c	5.7 (Berkeley) 01/09/86";
1322710Sdist #endif not lint
1422710Sdist 
154174Seric # include <pwd.h>
164627Seric # include "sendmail.h"
174329Seric # include <sys/stat.h>
184174Seric 
194174Seric /*
209622Seric **  SENDTOLIST -- Designate a send list.
214174Seric **
224174Seric **	The parameter is a comma-separated list of people to send to.
234174Seric **	This routine arranges to send to all of them.
244174Seric **
254174Seric **	Parameters:
264174Seric **		list -- the send list.
274399Seric **		ctladdr -- the address template for the person to
284399Seric **			send to -- effective uid/gid are important.
295006Seric **			This is typically the alias that caused this
305006Seric **			expansion.
315006Seric **		sendq -- a pointer to the head of a queue to put
325006Seric **			these people into.
334174Seric **
344174Seric **	Returns:
354998Seric **		none
364174Seric **
374174Seric **	Side Effects:
384174Seric **		none.
394174Seric */
404174Seric 
414174Seric # define MAXRCRSN	10
424174Seric 
439622Seric sendtolist(list, ctladdr, sendq)
444174Seric 	char *list;
454399Seric 	ADDRESS *ctladdr;
465198Seric 	ADDRESS **sendq;
474174Seric {
484174Seric 	register char *p;
498223Seric 	register ADDRESS *al;	/* list of addresses to send to */
504423Seric 	bool firstone;		/* set on first address sent */
514444Seric 	bool selfref;		/* set if this list includes ctladdr */
5211446Seric 	char delimiter;		/* the address delimiter */
534174Seric 
544324Seric # ifdef DEBUG
557676Seric 	if (tTd(25, 1))
564444Seric 	{
574444Seric 		printf("sendto: %s\n   ctladdr=", list);
584444Seric 		printaddr(ctladdr, FALSE);
594444Seric 	}
604324Seric # endif DEBUG
614324Seric 
628223Seric 	/* heuristic to determine old versus new style addresses */
638230Seric 	if (ctladdr == NULL &&
648230Seric 	    (index(list, ',') != NULL || index(list, ';') != NULL ||
658230Seric 	     index(list, '<') != NULL || index(list, '(') != NULL))
669340Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
6711446Seric 	delimiter = ' ';
6811446Seric 	if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL)
6911446Seric 		delimiter = ',';
708223Seric 
714423Seric 	firstone = TRUE;
724444Seric 	selfref = FALSE;
734324Seric 	al = NULL;
748223Seric 
758081Seric 	for (p = list; *p != '\0'; )
764174Seric 	{
778081Seric 		register ADDRESS *a;
788081Seric 		extern char *DelimChar;		/* defined in prescan */
794319Seric 
808081Seric 		/* parse the address */
818081Seric 		while (isspace(*p) || *p == ',')
824174Seric 			p++;
8311446Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter);
849297Seric 		p = DelimChar;
859297Seric 		if (a == NULL)
864174Seric 			continue;
874324Seric 		a->q_next = al;
884399Seric 		a->q_alias = ctladdr;
894444Seric 
904444Seric 		/* see if this should be marked as a primary address */
914423Seric 		if (ctladdr == NULL ||
928081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
934423Seric 			a->q_flags |= QPRIMARY;
944444Seric 
954444Seric 		/* put on send queue or suppress self-reference */
969379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
974444Seric 			selfref = TRUE;
984444Seric 		else
994444Seric 			al = a;
1004423Seric 		firstone = FALSE;
1014324Seric 	}
1024324Seric 
1034444Seric 	/* if this alias doesn't include itself, delete ctladdr */
1044444Seric 	if (!selfref && ctladdr != NULL)
1054444Seric 		ctladdr->q_flags |= QDONTSEND;
1064444Seric 
1074324Seric 	/* arrange to send to everyone on the local send list */
1084324Seric 	while (al != NULL)
1094324Seric 	{
1104324Seric 		register ADDRESS *a = al;
11112613Seric 		extern ADDRESS *recipient();
1124324Seric 
1134324Seric 		al = a->q_next;
11412613Seric 		a = recipient(a, sendq);
1154993Seric 
1164998Seric 		/* arrange to inherit full name */
1174998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1184998Seric 			a->q_fullname = ctladdr->q_fullname;
1194174Seric 	}
1204324Seric 
1216906Seric 	CurEnv->e_to = NULL;
1224174Seric }
1234174Seric /*
1244174Seric **  RECIPIENT -- Designate a message recipient
1254174Seric **
1264174Seric **	Saves the named person for future mailing.
1274174Seric **
1284174Seric **	Parameters:
1294174Seric **		a -- the (preparsed) address header for the recipient.
1305006Seric **		sendq -- a pointer to the head of a queue to put the
1315006Seric **			recipient in.  Duplicate supression is done
1325006Seric **			in this queue.
1334174Seric **
1344174Seric **	Returns:
13512613Seric **		The actual address in the queue.  This will be "a" if
13612613Seric **		the address is not a duplicate, else the original address.
1374174Seric **
1384174Seric **	Side Effects:
1394174Seric **		none.
1404174Seric */
1414174Seric 
14212613Seric ADDRESS *
1435006Seric recipient(a, sendq)
1444174Seric 	register ADDRESS *a;
1455006Seric 	register ADDRESS **sendq;
1464174Seric {
1474174Seric 	register ADDRESS *q;
1484319Seric 	ADDRESS **pq;
1494174Seric 	register struct mailer *m;
1509210Seric 	register char *p;
1519210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
1529210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1534399Seric 	extern ADDRESS *getctladdr();
1544627Seric 	extern bool safefile();
1554174Seric 
1566906Seric 	CurEnv->e_to = a->q_paddr;
1574600Seric 	m = a->q_mailer;
1584174Seric 	errno = 0;
1594174Seric # ifdef DEBUG
1607676Seric 	if (tTd(26, 1))
1614444Seric 	{
1624444Seric 		printf("\nrecipient: ");
1634444Seric 		printaddr(a, FALSE);
1644444Seric 	}
1654174Seric # endif DEBUG
1664174Seric 
1674174Seric 	/* break aliasing loops */
1684174Seric 	if (AliasLevel > MAXRCRSN)
1694174Seric 	{
1704174Seric 		usrerr("aliasing/forwarding loop broken");
17112613Seric 		return (a);
1724174Seric 	}
1734174Seric 
1744174Seric 	/*
1754627Seric 	**  Finish setting up address structure.
1764174Seric 	*/
1774174Seric 
17816160Seric 	/* set the queue timeout */
1794627Seric 	a->q_timeout = TimeOut;
1804627Seric 
18116160Seric 	/* map user & host to lower case if requested on non-aliases */
18216160Seric 	if (a->q_alias == NULL)
18316160Seric 		loweraddr(a);
18416160Seric 
18516160Seric 	/* get unquoted user for file, program or user.name check */
1869210Seric 	(void) strcpy(buf, a->q_user);
1879210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1889210Seric 	{
1899210Seric 		if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
1909210Seric 			quoted = TRUE;
1919210Seric 	}
1929210Seric 	stripquotes(buf, TRUE);
1939210Seric 
1944627Seric 	/* do sickly crude mapping for program mailing, etc. */
1959210Seric 	if (m == LocalMailer && buf[0] == '|')
1964174Seric 	{
1979210Seric 		a->q_mailer = m = ProgMailer;
1989210Seric 		a->q_user++;
1999210Seric 		if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2004174Seric 		{
2019210Seric 			usrerr("Cannot mail directly to programs");
2029210Seric 			a->q_flags |= QDONTSEND;
2034174Seric 		}
2044174Seric 	}
2054174Seric 
2064174Seric 	/*
2074419Seric 	**  Look up this person in the recipient list.
2084419Seric 	**	If they are there already, return, otherwise continue.
2094419Seric 	**	If the list is empty, just add it.  Notice the cute
2104419Seric 	**	hack to make from addresses suppress things correctly:
2114419Seric 	**	the QDONTSEND bit will be set in the send list.
2124419Seric 	**	[Please note: the emphasis is on "hack."]
2134174Seric 	*/
2144174Seric 
2155006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2164174Seric 	{
2179379Seric 		if (!ForceMail && sameaddr(q, a))
2184174Seric 		{
2194174Seric # ifdef DEBUG
2207676Seric 			if (tTd(26, 1))
2214444Seric 			{
2224444Seric 				printf("%s in sendq: ", a->q_paddr);
2234444Seric 				printaddr(q, FALSE);
2244444Seric 			}
2254174Seric # endif DEBUG
2267054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2274324Seric 				message(Arpa_Info, "duplicate suppressed");
2284423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2294423Seric 				q->q_flags |= a->q_flags;
23012613Seric 			return (q);
2314174Seric 		}
2324319Seric 	}
2334174Seric 
2344319Seric 	/* add address on list */
2354319Seric 	*pq = a;
2364174Seric 	a->q_next = NULL;
23724951Seric 	CurEnv->e_nrcpts++;
2384174Seric 
2394174Seric 	/*
2404174Seric 	**  Alias the name and handle :include: specs.
2414174Seric 	*/
2424174Seric 
2439210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2444174Seric 	{
2454174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2464174Seric 		{
2474174Seric 			a->q_flags |= QDONTSEND;
2487676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2494399Seric 				usrerr("Cannot mail directly to :include:s");
2504399Seric 			else
2514399Seric 			{
2527054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
2535006Seric 				include(&a->q_user[9], " sending", a, sendq);
2544399Seric 			}
2554174Seric 		}
2564174Seric 		else
2575006Seric 			alias(a, sendq);
2584174Seric 	}
2594174Seric 
2604174Seric 	/*
2614174Seric 	**  If the user is local and still being sent, verify that
2624174Seric 	**  the address is good.  If it is, try to forward.
2634174Seric 	**  If the address is already good, we have a forwarding
2644174Seric 	**  loop.  This can be broken by just sending directly to
2654174Seric 	**  the user (which is probably correct anyway).
2664174Seric 	*/
2674174Seric 
2689210Seric 	if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer)
2694174Seric 	{
2704329Seric 		struct stat stb;
2714329Seric 		extern bool writable();
2724174Seric 
2734174Seric 		/* see if this is to a file */
2745600Seric 		if (buf[0] == '/')
2754174Seric 		{
2765600Seric 			p = rindex(buf, '/');
2774201Seric 			/* check if writable or creatable */
2787676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2794399Seric 			{
2804399Seric 				usrerr("Cannot mail directly to files");
2814399Seric 				a->q_flags |= QDONTSEND;
2824399Seric 			}
2834399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2844539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2854174Seric 			{
2864174Seric 				a->q_flags |= QBADADDR;
28710109Seric 				giveresponse(EX_CANTCREAT, m, CurEnv);
2884174Seric 			}
2894174Seric 		}
2904174Seric 		else
2914174Seric 		{
2924174Seric 			register struct passwd *pw;
2934373Seric 			extern struct passwd *finduser();
2944373Seric 
2954407Seric 			/* warning -- finduser may trash buf */
2964373Seric 			pw = finduser(buf);
2974174Seric 			if (pw == NULL)
2984174Seric 			{
2994174Seric 				a->q_flags |= QBADADDR;
30010109Seric 				giveresponse(EX_NOUSER, m, CurEnv);
3014174Seric 			}
3024174Seric 			else
3034174Seric 			{
3044993Seric 				char nbuf[MAXNAME];
3054993Seric 
3064376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
3074376Seric 				{
3084376Seric 					a->q_user = newstr(pw->pw_name);
3097008Seric 					(void) strcpy(buf, pw->pw_name);
3104376Seric 				}
3114174Seric 				a->q_home = newstr(pw->pw_dir);
3124213Seric 				a->q_uid = pw->pw_uid;
3134399Seric 				a->q_gid = pw->pw_gid;
3144404Seric 				a->q_flags |= QGOODUID;
3154998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
3164993Seric 				if (nbuf[0] != '\0')
3174993Seric 					a->q_fullname = newstr(nbuf);
3184399Seric 				if (!quoted)
3195006Seric 					forward(a, sendq);
3204174Seric 			}
3214174Seric 		}
3224174Seric 	}
32312613Seric 	return (a);
3244174Seric }
3254174Seric /*
3264373Seric **  FINDUSER -- find the password entry for a user.
3274373Seric **
3284373Seric **	This looks a lot like getpwnam, except that it may want to
3294373Seric **	do some fancier pattern matching in /etc/passwd.
3304373Seric **
3319379Seric **	This routine contains most of the time of many sendmail runs.
3329379Seric **	It deserves to be optimized.
3339379Seric **
3344373Seric **	Parameters:
3354373Seric **		name -- the name to match against.
3364373Seric **
3374373Seric **	Returns:
3384373Seric **		A pointer to a pw struct.
3394373Seric **		NULL if name is unknown or ambiguous.
3404373Seric **
3414373Seric **	Side Effects:
3424407Seric **		may modify name.
3434373Seric */
3444373Seric 
3454373Seric struct passwd *
3464373Seric finduser(name)
3474373Seric 	char *name;
3484373Seric {
3494376Seric 	register struct passwd *pw;
3504407Seric 	register char *p;
35115325Seric 	extern struct passwd *getpwent();
35215325Seric 	extern struct passwd *getpwnam();
3534373Seric 
354*25777Seric 	/* map upper => lower case */
3554407Seric 	for (p = name; *p != '\0'; p++)
3564407Seric 	{
357*25777Seric 		if (isascii(*p) && isupper(*p))
35825568Seric 			*p = tolower(*p);
3594407Seric 	}
3604407Seric 
361*25777Seric 	/* look up this login name using fast path */
36212634Seric 	if ((pw = getpwnam(name)) != NULL)
36312634Seric 		return (pw);
36412634Seric 
36512634Seric 	/* search for a matching full name instead */
366*25777Seric 	for (p = name; *p != '\0'; p++)
367*25777Seric 	{
368*25777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
369*25777Seric 			*p = ' ';
370*25777Seric 	}
37123107Seric 	(void) setpwent();
3724376Seric 	while ((pw = getpwent()) != NULL)
3734376Seric 	{
3744998Seric 		char buf[MAXNAME];
3754993Seric 		extern bool sameword();
3764376Seric 
3774998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3784407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3794381Seric 		{
3807054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
3814376Seric 			return (pw);
3824377Seric 		}
3834376Seric 	}
3844376Seric 	return (NULL);
3854373Seric }
3864373Seric /*
3874329Seric **  WRITABLE -- predicate returning if the file is writable.
3884329Seric **
3894329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3904329Seric **	Unfortunately, we cannot use the access call since we
3914329Seric **	won't necessarily be the real uid when we try to
3924329Seric **	actually open the file.
3934329Seric **
3944329Seric **	Notice that ANY file with ANY execute bit is automatically
3954329Seric **	not writable.  This is also enforced by mailfile.
3964329Seric **
3974329Seric **	Parameters:
3984329Seric **		s -- pointer to a stat struct for the file.
3994329Seric **
4004329Seric **	Returns:
4014329Seric **		TRUE -- if we will be able to write this file.
4024329Seric **		FALSE -- if we cannot write this file.
4034329Seric **
4044329Seric **	Side Effects:
4054329Seric **		none.
4064329Seric */
4074329Seric 
4084329Seric bool
4094329Seric writable(s)
4104329Seric 	register struct stat *s;
4114329Seric {
4124329Seric 	int euid, egid;
4134329Seric 	int bits;
4144329Seric 
4154329Seric 	if (bitset(0111, s->st_mode))
4164329Seric 		return (FALSE);
4174329Seric 	euid = getruid();
4184329Seric 	egid = getrgid();
4194329Seric 	if (geteuid() == 0)
4204329Seric 	{
4214329Seric 		if (bitset(S_ISUID, s->st_mode))
4224329Seric 			euid = s->st_uid;
4234329Seric 		if (bitset(S_ISGID, s->st_mode))
4244329Seric 			egid = s->st_gid;
4254329Seric 	}
4264329Seric 
4274329Seric 	if (euid == 0)
4284329Seric 		return (TRUE);
4294329Seric 	bits = S_IWRITE;
4304329Seric 	if (euid != s->st_uid)
4314329Seric 	{
4324329Seric 		bits >>= 3;
4334329Seric 		if (egid != s->st_gid)
4344329Seric 			bits >>= 3;
4354329Seric 	}
4364329Seric 	return ((s->st_mode & bits) != 0);
4374329Seric }
4384329Seric /*
4394174Seric **  INCLUDE -- handle :include: specification.
4404174Seric **
4414174Seric **	Parameters:
4424174Seric **		fname -- filename to include.
4434176Seric **		msg -- message to print in verbose mode.
4444399Seric **		ctladdr -- address template to use to fill in these
4454399Seric **			addresses -- effective user/group id are
4464399Seric **			the important things.
4475006Seric **		sendq -- a pointer to the head of the send queue
4485006Seric **			to put these addresses in.
4494174Seric **
4504174Seric **	Returns:
4514174Seric **		none.
4524174Seric **
4534174Seric **	Side Effects:
4544174Seric **		reads the :include: file and sends to everyone
4554174Seric **		listed in that file.
4564174Seric */
4574174Seric 
4585006Seric include(fname, msg, ctladdr, sendq)
4594174Seric 	char *fname;
4604176Seric 	char *msg;
4614399Seric 	ADDRESS *ctladdr;
4625006Seric 	ADDRESS **sendq;
4634174Seric {
4644174Seric 	char buf[MAXLINE];
4654174Seric 	register FILE *fp;
4666906Seric 	char *oldto = CurEnv->e_to;
4679379Seric 	char *oldfilename = FileName;
4689379Seric 	int oldlinenumber = LineNumber;
4694174Seric 
4704174Seric 	fp = fopen(fname, "r");
4714174Seric 	if (fp == NULL)
4724174Seric 	{
4734174Seric 		usrerr("Cannot open %s", fname);
4744174Seric 		return;
4754174Seric 	}
4764406Seric 	if (getctladdr(ctladdr) == NULL)
4774406Seric 	{
4784406Seric 		struct stat st;
4794174Seric 
4804406Seric 		if (fstat(fileno(fp), &st) < 0)
4814406Seric 			syserr("Cannot fstat %s!", fname);
4824406Seric 		ctladdr->q_uid = st.st_uid;
4834406Seric 		ctladdr->q_gid = st.st_gid;
4844406Seric 		ctladdr->q_flags |= QGOODUID;
4854406Seric 	}
4864406Seric 
4874174Seric 	/* read the file -- each line is a comma-separated list. */
4889379Seric 	FileName = fname;
4899379Seric 	LineNumber = 0;
4904174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4914174Seric 	{
4924174Seric 		register char *p = index(buf, '\n');
4934174Seric 
4944174Seric 		if (p != NULL)
4954174Seric 			*p = '\0';
4964174Seric 		if (buf[0] == '\0')
4974174Seric 			continue;
4986906Seric 		CurEnv->e_to = oldto;
4997054Seric 		message(Arpa_Info, "%s to %s", msg, buf);
5004176Seric 		AliasLevel++;
5019622Seric 		sendtolist(buf, ctladdr, sendq);
5024176Seric 		AliasLevel--;
5034174Seric 	}
5044174Seric 
5054319Seric 	(void) fclose(fp);
5069379Seric 	FileName = oldfilename;
5079379Seric 	LineNumber = oldlinenumber;
5084174Seric }
5094324Seric /*
5104324Seric **  SENDTOARGV -- send to an argument vector.
5114324Seric **
5124324Seric **	Parameters:
5134324Seric **		argv -- argument vector to send to.
5144324Seric **
5154324Seric **	Returns:
5164324Seric **		none.
5174324Seric **
5184324Seric **	Side Effects:
5194324Seric **		puts all addresses on the argument vector onto the
5204324Seric **			send queue.
5214324Seric */
5224324Seric 
5234324Seric sendtoargv(argv)
5244324Seric 	register char **argv;
5254324Seric {
5264324Seric 	register char *p;
5274324Seric 	extern bool sameword();
5284324Seric 
5294324Seric 	while ((p = *argv++) != NULL)
5304324Seric 	{
5314324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
5324324Seric 		{
5334324Seric 			char nbuf[MAXNAME];
5344324Seric 
5354324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
5364324Seric 				usrerr("address overflow");
5374324Seric 			else
5384324Seric 			{
5394324Seric 				(void) strcpy(nbuf, p);
5404324Seric 				(void) strcat(nbuf, "@");
5414324Seric 				(void) strcat(nbuf, argv[1]);
5424324Seric 				p = newstr(nbuf);
5434324Seric 				argv += 2;
5444324Seric 			}
5454324Seric 		}
5469622Seric 		sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
5474324Seric 	}
5484324Seric }
5494399Seric /*
5504399Seric **  GETCTLADDR -- get controlling address from an address header.
5514399Seric **
5524399Seric **	If none, get one corresponding to the effective userid.
5534399Seric **
5544399Seric **	Parameters:
5554399Seric **		a -- the address to find the controller of.
5564399Seric **
5574399Seric **	Returns:
5584399Seric **		the controlling address.
5594399Seric **
5604399Seric **	Side Effects:
5614399Seric **		none.
5624399Seric */
5634399Seric 
5644399Seric ADDRESS *
5654399Seric getctladdr(a)
5664399Seric 	register ADDRESS *a;
5674399Seric {
5684404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5694399Seric 		a = a->q_alias;
5704399Seric 	return (a);
5714399Seric }
572