14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*11446Seric SCCSID(@(#)recipient.c	3.52		03/08/83);
64174Seric 
74174Seric /*
89622Seric **  SENDTOLIST -- 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.
154399Seric **		ctladdr -- the address template for the person to
164399Seric **			send to -- effective uid/gid are important.
175006Seric **			This is typically the alias that caused this
185006Seric **			expansion.
195006Seric **		sendq -- a pointer to the head of a queue to put
205006Seric **			these people into.
214174Seric **
224174Seric **	Returns:
234998Seric **		none
244174Seric **
254174Seric **	Side Effects:
264174Seric **		none.
274174Seric */
284174Seric 
294174Seric # define MAXRCRSN	10
304174Seric 
319622Seric sendtolist(list, ctladdr, sendq)
324174Seric 	char *list;
334399Seric 	ADDRESS *ctladdr;
345198Seric 	ADDRESS **sendq;
354174Seric {
364174Seric 	register char *p;
378223Seric 	register ADDRESS *al;	/* list of addresses to send to */
384423Seric 	bool firstone;		/* set on first address sent */
394444Seric 	bool selfref;		/* set if this list includes ctladdr */
40*11446Seric 	char delimiter;		/* the address delimiter */
414174Seric 
424324Seric # ifdef DEBUG
437676Seric 	if (tTd(25, 1))
444444Seric 	{
454444Seric 		printf("sendto: %s\n   ctladdr=", list);
464444Seric 		printaddr(ctladdr, FALSE);
474444Seric 	}
484324Seric # endif DEBUG
494324Seric 
508223Seric 	/* heuristic to determine old versus new style addresses */
518230Seric 	if (ctladdr == NULL &&
528230Seric 	    (index(list, ',') != NULL || index(list, ';') != NULL ||
538230Seric 	     index(list, '<') != NULL || index(list, '(') != NULL))
549340Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
55*11446Seric 	delimiter = ' ';
56*11446Seric 	if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL)
57*11446Seric 		delimiter = ',';
588223Seric 
594423Seric 	firstone = TRUE;
604444Seric 	selfref = FALSE;
614324Seric 	al = NULL;
628223Seric 
638081Seric 	for (p = list; *p != '\0'; )
644174Seric 	{
658081Seric 		register ADDRESS *a;
668081Seric 		extern char *DelimChar;		/* defined in prescan */
674319Seric 
688081Seric 		/* parse the address */
698081Seric 		while (isspace(*p) || *p == ',')
704174Seric 			p++;
71*11446Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter);
729297Seric 		p = DelimChar;
739297Seric 		if (a == NULL)
744174Seric 			continue;
754324Seric 		a->q_next = al;
764399Seric 		a->q_alias = ctladdr;
774444Seric 
784444Seric 		/* see if this should be marked as a primary address */
794423Seric 		if (ctladdr == NULL ||
808081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
814423Seric 			a->q_flags |= QPRIMARY;
824444Seric 
834444Seric 		/* put on send queue or suppress self-reference */
849379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
854444Seric 			selfref = TRUE;
864444Seric 		else
874444Seric 			al = a;
884423Seric 		firstone = FALSE;
894324Seric 	}
904324Seric 
914444Seric 	/* if this alias doesn't include itself, delete ctladdr */
924444Seric 	if (!selfref && ctladdr != NULL)
934444Seric 		ctladdr->q_flags |= QDONTSEND;
944444Seric 
954324Seric 	/* arrange to send to everyone on the local send list */
964324Seric 	while (al != NULL)
974324Seric 	{
984324Seric 		register ADDRESS *a = al;
994324Seric 
1004324Seric 		al = a->q_next;
1015006Seric 		recipient(a, sendq);
1024993Seric 
1034998Seric 		/* arrange to inherit full name */
1044998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1054998Seric 			a->q_fullname = ctladdr->q_fullname;
1064174Seric 	}
1074324Seric 
1086906Seric 	CurEnv->e_to = NULL;
1094174Seric }
1104174Seric /*
1114174Seric **  RECIPIENT -- Designate a message recipient
1124174Seric **
1134174Seric **	Saves the named person for future mailing.
1144174Seric **
1154174Seric **	Parameters:
1164174Seric **		a -- the (preparsed) address header for the recipient.
1175006Seric **		sendq -- a pointer to the head of a queue to put the
1185006Seric **			recipient in.  Duplicate supression is done
1195006Seric **			in this queue.
1204174Seric **
1214174Seric **	Returns:
1224998Seric **		none.
1234174Seric **
1244174Seric **	Side Effects:
1254174Seric **		none.
1264174Seric */
1274174Seric 
1285006Seric recipient(a, sendq)
1294174Seric 	register ADDRESS *a;
1305006Seric 	register ADDRESS **sendq;
1314174Seric {
1324174Seric 	register ADDRESS *q;
1334319Seric 	ADDRESS **pq;
1344174Seric 	register struct mailer *m;
1359210Seric 	register char *p;
1369210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
1379210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1384399Seric 	extern ADDRESS *getctladdr();
1394627Seric 	extern bool safefile();
1404174Seric 
1416906Seric 	CurEnv->e_to = a->q_paddr;
1424600Seric 	m = a->q_mailer;
1434174Seric 	errno = 0;
1444174Seric # ifdef DEBUG
1457676Seric 	if (tTd(26, 1))
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 
1659210Seric 	(void) strcpy(buf, a->q_user);
1669210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1679210Seric 	{
1689210Seric 		if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
1699210Seric 			quoted = TRUE;
1709210Seric 	}
1719210Seric 	stripquotes(buf, TRUE);
1729210Seric 
1734627Seric 	/* do sickly crude mapping for program mailing, etc. */
1749210Seric 	if (m == LocalMailer && buf[0] == '|')
1754174Seric 	{
1769210Seric 		a->q_mailer = m = ProgMailer;
1779210Seric 		a->q_user++;
1789210Seric 		if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
1794174Seric 		{
1809210Seric 			usrerr("Cannot mail directly to programs");
1819210Seric 			a->q_flags |= QDONTSEND;
1824174Seric 		}
1834174Seric 	}
1844174Seric 
1854174Seric 	/*
1864419Seric 	**  Look up this person in the recipient list.
1874419Seric 	**	If they are there already, return, otherwise continue.
1884419Seric 	**	If the list is empty, just add it.  Notice the cute
1894419Seric 	**	hack to make from addresses suppress things correctly:
1904419Seric 	**	the QDONTSEND bit will be set in the send list.
1914419Seric 	**	[Please note: the emphasis is on "hack."]
1924174Seric 	*/
1934174Seric 
1945006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1954174Seric 	{
1969379Seric 		if (!ForceMail && sameaddr(q, a))
1974174Seric 		{
1984174Seric # ifdef DEBUG
1997676Seric 			if (tTd(26, 1))
2004444Seric 			{
2014444Seric 				printf("%s in sendq: ", a->q_paddr);
2024444Seric 				printaddr(q, FALSE);
2034444Seric 			}
2044174Seric # endif DEBUG
2057054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2064324Seric 				message(Arpa_Info, "duplicate suppressed");
2074423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2084423Seric 				q->q_flags |= a->q_flags;
2094998Seric 			return;
2104174Seric 		}
2114319Seric 	}
2124174Seric 
2134319Seric 	/* add address on list */
2144319Seric 	*pq = a;
2154174Seric 	a->q_next = NULL;
2164174Seric 
2174174Seric 	/*
2184174Seric 	**  Alias the name and handle :include: specs.
2194174Seric 	*/
2204174Seric 
2219210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2224174Seric 	{
2234174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2244174Seric 		{
2254174Seric 			a->q_flags |= QDONTSEND;
2267676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2274399Seric 				usrerr("Cannot mail directly to :include:s");
2284399Seric 			else
2294399Seric 			{
2307054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
2315006Seric 				include(&a->q_user[9], " sending", a, sendq);
2324399Seric 			}
2334174Seric 		}
2344174Seric 		else
2355006Seric 			alias(a, sendq);
2364174Seric 	}
2374174Seric 
2384174Seric 	/*
2394174Seric 	**  If the user is local and still being sent, verify that
2404174Seric 	**  the address is good.  If it is, try to forward.
2414174Seric 	**  If the address is already good, we have a forwarding
2424174Seric 	**  loop.  This can be broken by just sending directly to
2434174Seric 	**  the user (which is probably correct anyway).
2444174Seric 	*/
2454174Seric 
2469210Seric 	if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer)
2474174Seric 	{
2484329Seric 		struct stat stb;
2494329Seric 		extern bool writable();
2504174Seric 
2514174Seric 		/* see if this is to a file */
2525600Seric 		if (buf[0] == '/')
2534174Seric 		{
2545600Seric 			p = rindex(buf, '/');
2554201Seric 			/* check if writable or creatable */
2567676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2574399Seric 			{
2584399Seric 				usrerr("Cannot mail directly to files");
2594399Seric 				a->q_flags |= QDONTSEND;
2604399Seric 			}
2614399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2624539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2634174Seric 			{
2644174Seric 				a->q_flags |= QBADADDR;
26510109Seric 				giveresponse(EX_CANTCREAT, m, CurEnv);
2664174Seric 			}
2674174Seric 		}
2684174Seric 		else
2694174Seric 		{
2704174Seric 			register struct passwd *pw;
2714373Seric 			extern struct passwd *finduser();
2724373Seric 
2734407Seric 			/* warning -- finduser may trash buf */
2744373Seric 			pw = finduser(buf);
2754174Seric 			if (pw == NULL)
2764174Seric 			{
2774174Seric 				a->q_flags |= QBADADDR;
27810109Seric 				giveresponse(EX_NOUSER, m, CurEnv);
2794174Seric 			}
2804174Seric 			else
2814174Seric 			{
2824993Seric 				char nbuf[MAXNAME];
2834993Seric 
2844376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2854376Seric 				{
2864376Seric 					a->q_user = newstr(pw->pw_name);
2877008Seric 					(void) strcpy(buf, pw->pw_name);
2884376Seric 				}
2894174Seric 				a->q_home = newstr(pw->pw_dir);
2904213Seric 				a->q_uid = pw->pw_uid;
2914399Seric 				a->q_gid = pw->pw_gid;
2924404Seric 				a->q_flags |= QGOODUID;
2934998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
2944993Seric 				if (nbuf[0] != '\0')
2954993Seric 					a->q_fullname = newstr(nbuf);
2964399Seric 				if (!quoted)
2975006Seric 					forward(a, sendq);
2984174Seric 			}
2994174Seric 		}
3004174Seric 	}
3014174Seric }
3024174Seric /*
3034373Seric **  FINDUSER -- find the password entry for a user.
3044373Seric **
3054373Seric **	This looks a lot like getpwnam, except that it may want to
3064373Seric **	do some fancier pattern matching in /etc/passwd.
3074373Seric **
3089379Seric **	This routine contains most of the time of many sendmail runs.
3099379Seric **	It deserves to be optimized.
3109379Seric **
3114373Seric **	Parameters:
3124373Seric **		name -- the name to match against.
3134373Seric **
3144373Seric **	Returns:
3154373Seric **		A pointer to a pw struct.
3164373Seric **		NULL if name is unknown or ambiguous.
3174373Seric **
3184373Seric **	Side Effects:
3194407Seric **		may modify name.
3204373Seric */
3214373Seric 
3224373Seric struct passwd *
3234373Seric finduser(name)
3244373Seric 	char *name;
3254373Seric {
3264376Seric 	extern struct passwd *getpwent();
3274376Seric 	register struct passwd *pw;
3284407Seric 	register char *p;
3294373Seric 
3304407Seric 	/*
3314407Seric 	**  Make name canonical.
3324407Seric 	*/
3334407Seric 
3344407Seric 	for (p = name; *p != '\0'; p++)
3354407Seric 	{
3369044Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
3374407Seric 			*p = ' ';
3384407Seric 	}
3394407Seric 
3404376Seric 	setpwent();
3414376Seric 	while ((pw = getpwent()) != NULL)
3424376Seric 	{
3434998Seric 		char buf[MAXNAME];
3444993Seric 		extern bool sameword();
3454376Seric 
3464376Seric 		if (strcmp(pw->pw_name, name) == 0)
3474376Seric 			return (pw);
3484998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3494407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3504381Seric 		{
3517054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
3524376Seric 			return (pw);
3534377Seric 		}
3544376Seric 	}
3554376Seric 	return (NULL);
3564373Seric }
3574373Seric /*
3584329Seric **  WRITABLE -- predicate returning if the file is writable.
3594329Seric **
3604329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3614329Seric **	Unfortunately, we cannot use the access call since we
3624329Seric **	won't necessarily be the real uid when we try to
3634329Seric **	actually open the file.
3644329Seric **
3654329Seric **	Notice that ANY file with ANY execute bit is automatically
3664329Seric **	not writable.  This is also enforced by mailfile.
3674329Seric **
3684329Seric **	Parameters:
3694329Seric **		s -- pointer to a stat struct for the file.
3704329Seric **
3714329Seric **	Returns:
3724329Seric **		TRUE -- if we will be able to write this file.
3734329Seric **		FALSE -- if we cannot write this file.
3744329Seric **
3754329Seric **	Side Effects:
3764329Seric **		none.
3774329Seric */
3784329Seric 
3794329Seric bool
3804329Seric writable(s)
3814329Seric 	register struct stat *s;
3824329Seric {
3834329Seric 	int euid, egid;
3844329Seric 	int bits;
3854329Seric 
3864329Seric 	if (bitset(0111, s->st_mode))
3874329Seric 		return (FALSE);
3884329Seric 	euid = getruid();
3894329Seric 	egid = getrgid();
3904329Seric 	if (geteuid() == 0)
3914329Seric 	{
3924329Seric 		if (bitset(S_ISUID, s->st_mode))
3934329Seric 			euid = s->st_uid;
3944329Seric 		if (bitset(S_ISGID, s->st_mode))
3954329Seric 			egid = s->st_gid;
3964329Seric 	}
3974329Seric 
3984329Seric 	if (euid == 0)
3994329Seric 		return (TRUE);
4004329Seric 	bits = S_IWRITE;
4014329Seric 	if (euid != s->st_uid)
4024329Seric 	{
4034329Seric 		bits >>= 3;
4044329Seric 		if (egid != s->st_gid)
4054329Seric 			bits >>= 3;
4064329Seric 	}
4074329Seric 	return ((s->st_mode & bits) != 0);
4084329Seric }
4094329Seric /*
4104174Seric **  INCLUDE -- handle :include: specification.
4114174Seric **
4124174Seric **	Parameters:
4134174Seric **		fname -- filename to include.
4144176Seric **		msg -- message to print in verbose mode.
4154399Seric **		ctladdr -- address template to use to fill in these
4164399Seric **			addresses -- effective user/group id are
4174399Seric **			the important things.
4185006Seric **		sendq -- a pointer to the head of the send queue
4195006Seric **			to put these addresses in.
4204174Seric **
4214174Seric **	Returns:
4224174Seric **		none.
4234174Seric **
4244174Seric **	Side Effects:
4254174Seric **		reads the :include: file and sends to everyone
4264174Seric **		listed in that file.
4274174Seric */
4284174Seric 
4295006Seric include(fname, msg, ctladdr, sendq)
4304174Seric 	char *fname;
4314176Seric 	char *msg;
4324399Seric 	ADDRESS *ctladdr;
4335006Seric 	ADDRESS **sendq;
4344174Seric {
4354174Seric 	char buf[MAXLINE];
4364174Seric 	register FILE *fp;
4376906Seric 	char *oldto = CurEnv->e_to;
4389379Seric 	char *oldfilename = FileName;
4399379Seric 	int oldlinenumber = LineNumber;
4404174Seric 
4414174Seric 	fp = fopen(fname, "r");
4424174Seric 	if (fp == NULL)
4434174Seric 	{
4444174Seric 		usrerr("Cannot open %s", fname);
4454174Seric 		return;
4464174Seric 	}
4474406Seric 	if (getctladdr(ctladdr) == NULL)
4484406Seric 	{
4494406Seric 		struct stat st;
4504174Seric 
4514406Seric 		if (fstat(fileno(fp), &st) < 0)
4524406Seric 			syserr("Cannot fstat %s!", fname);
4534406Seric 		ctladdr->q_uid = st.st_uid;
4544406Seric 		ctladdr->q_gid = st.st_gid;
4554406Seric 		ctladdr->q_flags |= QGOODUID;
4564406Seric 	}
4574406Seric 
4584174Seric 	/* read the file -- each line is a comma-separated list. */
4599379Seric 	FileName = fname;
4609379Seric 	LineNumber = 0;
4614174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4624174Seric 	{
4634174Seric 		register char *p = index(buf, '\n');
4644174Seric 
4654174Seric 		if (p != NULL)
4664174Seric 			*p = '\0';
4674174Seric 		if (buf[0] == '\0')
4684174Seric 			continue;
4696906Seric 		CurEnv->e_to = oldto;
4707054Seric 		message(Arpa_Info, "%s to %s", msg, buf);
4714176Seric 		AliasLevel++;
4729622Seric 		sendtolist(buf, ctladdr, sendq);
4734176Seric 		AliasLevel--;
4744174Seric 	}
4754174Seric 
4764319Seric 	(void) fclose(fp);
4779379Seric 	FileName = oldfilename;
4789379Seric 	LineNumber = oldlinenumber;
4794174Seric }
4804324Seric /*
4814324Seric **  SENDTOARGV -- send to an argument vector.
4824324Seric **
4834324Seric **	Parameters:
4844324Seric **		argv -- argument vector to send to.
4854324Seric **
4864324Seric **	Returns:
4874324Seric **		none.
4884324Seric **
4894324Seric **	Side Effects:
4904324Seric **		puts all addresses on the argument vector onto the
4914324Seric **			send queue.
4924324Seric */
4934324Seric 
4944324Seric sendtoargv(argv)
4954324Seric 	register char **argv;
4964324Seric {
4974324Seric 	register char *p;
4984324Seric 	extern bool sameword();
4994324Seric 
5004324Seric 	while ((p = *argv++) != NULL)
5014324Seric 	{
5024324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
5034324Seric 		{
5044324Seric 			char nbuf[MAXNAME];
5054324Seric 
5064324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
5074324Seric 				usrerr("address overflow");
5084324Seric 			else
5094324Seric 			{
5104324Seric 				(void) strcpy(nbuf, p);
5114324Seric 				(void) strcat(nbuf, "@");
5124324Seric 				(void) strcat(nbuf, argv[1]);
5134324Seric 				p = newstr(nbuf);
5144324Seric 				argv += 2;
5154324Seric 			}
5164324Seric 		}
5179622Seric 		sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
5184324Seric 	}
5194324Seric }
5204399Seric /*
5214399Seric **  GETCTLADDR -- get controlling address from an address header.
5224399Seric **
5234399Seric **	If none, get one corresponding to the effective userid.
5244399Seric **
5254399Seric **	Parameters:
5264399Seric **		a -- the address to find the controller of.
5274399Seric **
5284399Seric **	Returns:
5294399Seric **		the controlling address.
5304399Seric **
5314399Seric **	Side Effects:
5324399Seric **		none.
5334399Seric */
5344399Seric 
5354399Seric ADDRESS *
5364399Seric getctladdr(a)
5374399Seric 	register ADDRESS *a;
5384399Seric {
5394404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5404399Seric 		a = a->q_alias;
5414399Seric 	return (a);
5424399Seric }
543