14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*15325Seric SCCSID(@(#)recipient.c	4.2		10/29/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 */
4011446Seric 	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;
5511446Seric 	delimiter = ' ';
5611446Seric 	if (!bitset(EF_OLDSTYLE, CurEnv->e_flags) || ctladdr != NULL)
5711446Seric 		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++;
7111446Seric 		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;
9912613Seric 		extern ADDRESS *recipient();
1004324Seric 
1014324Seric 		al = a->q_next;
10212613Seric 		a = recipient(a, sendq);
1034993Seric 
1044998Seric 		/* arrange to inherit full name */
1054998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1064998Seric 			a->q_fullname = ctladdr->q_fullname;
1074174Seric 	}
1084324Seric 
1096906Seric 	CurEnv->e_to = NULL;
1104174Seric }
1114174Seric /*
1124174Seric **  RECIPIENT -- Designate a message recipient
1134174Seric **
1144174Seric **	Saves the named person for future mailing.
1154174Seric **
1164174Seric **	Parameters:
1174174Seric **		a -- the (preparsed) address header for the recipient.
1185006Seric **		sendq -- a pointer to the head of a queue to put the
1195006Seric **			recipient in.  Duplicate supression is done
1205006Seric **			in this queue.
1214174Seric **
1224174Seric **	Returns:
12312613Seric **		The actual address in the queue.  This will be "a" if
12412613Seric **		the address is not a duplicate, else the original address.
1254174Seric **
1264174Seric **	Side Effects:
1274174Seric **		none.
1284174Seric */
1294174Seric 
13012613Seric ADDRESS *
1315006Seric recipient(a, sendq)
1324174Seric 	register ADDRESS *a;
1335006Seric 	register ADDRESS **sendq;
1344174Seric {
1354174Seric 	register ADDRESS *q;
1364319Seric 	ADDRESS **pq;
1374174Seric 	register struct mailer *m;
1389210Seric 	register char *p;
1399210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
1409210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1414399Seric 	extern ADDRESS *getctladdr();
1424627Seric 	extern bool safefile();
1434174Seric 
1446906Seric 	CurEnv->e_to = a->q_paddr;
1454600Seric 	m = a->q_mailer;
1464174Seric 	errno = 0;
1474174Seric # ifdef DEBUG
1487676Seric 	if (tTd(26, 1))
1494444Seric 	{
1504444Seric 		printf("\nrecipient: ");
1514444Seric 		printaddr(a, FALSE);
1524444Seric 	}
1534174Seric # endif DEBUG
1544174Seric 
1554174Seric 	/* break aliasing loops */
1564174Seric 	if (AliasLevel > MAXRCRSN)
1574174Seric 	{
1584174Seric 		usrerr("aliasing/forwarding loop broken");
15912613Seric 		return (a);
1604174Seric 	}
1614174Seric 
1624174Seric 	/*
1634627Seric 	**  Finish setting up address structure.
1644174Seric 	*/
1654174Seric 
1664627Seric 	a->q_timeout = TimeOut;
1674627Seric 
1689210Seric 	(void) strcpy(buf, a->q_user);
1699210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1709210Seric 	{
1719210Seric 		if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
1729210Seric 			quoted = TRUE;
1739210Seric 	}
1749210Seric 	stripquotes(buf, TRUE);
1759210Seric 
1764627Seric 	/* do sickly crude mapping for program mailing, etc. */
1779210Seric 	if (m == LocalMailer && buf[0] == '|')
1784174Seric 	{
1799210Seric 		a->q_mailer = m = ProgMailer;
1809210Seric 		a->q_user++;
1819210Seric 		if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
1824174Seric 		{
1839210Seric 			usrerr("Cannot mail directly to programs");
1849210Seric 			a->q_flags |= QDONTSEND;
1854174Seric 		}
1864174Seric 	}
1874174Seric 
1884174Seric 	/*
1894419Seric 	**  Look up this person in the recipient list.
1904419Seric 	**	If they are there already, return, otherwise continue.
1914419Seric 	**	If the list is empty, just add it.  Notice the cute
1924419Seric 	**	hack to make from addresses suppress things correctly:
1934419Seric 	**	the QDONTSEND bit will be set in the send list.
1944419Seric 	**	[Please note: the emphasis is on "hack."]
1954174Seric 	*/
1964174Seric 
1975006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1984174Seric 	{
1999379Seric 		if (!ForceMail && sameaddr(q, a))
2004174Seric 		{
2014174Seric # ifdef DEBUG
2027676Seric 			if (tTd(26, 1))
2034444Seric 			{
2044444Seric 				printf("%s in sendq: ", a->q_paddr);
2054444Seric 				printaddr(q, FALSE);
2064444Seric 			}
2074174Seric # endif DEBUG
2087054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2094324Seric 				message(Arpa_Info, "duplicate suppressed");
2104423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2114423Seric 				q->q_flags |= a->q_flags;
21212613Seric 			return (q);
2134174Seric 		}
2144319Seric 	}
2154174Seric 
2164319Seric 	/* add address on list */
2174319Seric 	*pq = a;
2184174Seric 	a->q_next = NULL;
2194174Seric 
2204174Seric 	/*
2214174Seric 	**  Alias the name and handle :include: specs.
2224174Seric 	*/
2234174Seric 
2249210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2254174Seric 	{
2264174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2274174Seric 		{
2284174Seric 			a->q_flags |= QDONTSEND;
2297676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2304399Seric 				usrerr("Cannot mail directly to :include:s");
2314399Seric 			else
2324399Seric 			{
2337054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
2345006Seric 				include(&a->q_user[9], " sending", a, sendq);
2354399Seric 			}
2364174Seric 		}
2374174Seric 		else
2385006Seric 			alias(a, sendq);
2394174Seric 	}
2404174Seric 
2414174Seric 	/*
2424174Seric 	**  If the user is local and still being sent, verify that
2434174Seric 	**  the address is good.  If it is, try to forward.
2444174Seric 	**  If the address is already good, we have a forwarding
2454174Seric 	**  loop.  This can be broken by just sending directly to
2464174Seric 	**  the user (which is probably correct anyway).
2474174Seric 	*/
2484174Seric 
2499210Seric 	if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer)
2504174Seric 	{
2514329Seric 		struct stat stb;
2524329Seric 		extern bool writable();
2534174Seric 
2544174Seric 		/* see if this is to a file */
2555600Seric 		if (buf[0] == '/')
2564174Seric 		{
2575600Seric 			p = rindex(buf, '/');
2584201Seric 			/* check if writable or creatable */
2597676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2604399Seric 			{
2614399Seric 				usrerr("Cannot mail directly to files");
2624399Seric 				a->q_flags |= QDONTSEND;
2634399Seric 			}
2644399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2654539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2664174Seric 			{
2674174Seric 				a->q_flags |= QBADADDR;
26810109Seric 				giveresponse(EX_CANTCREAT, m, CurEnv);
2694174Seric 			}
2704174Seric 		}
2714174Seric 		else
2724174Seric 		{
2734174Seric 			register struct passwd *pw;
2744373Seric 			extern struct passwd *finduser();
2754373Seric 
2764407Seric 			/* warning -- finduser may trash buf */
2774373Seric 			pw = finduser(buf);
2784174Seric 			if (pw == NULL)
2794174Seric 			{
2804174Seric 				a->q_flags |= QBADADDR;
28110109Seric 				giveresponse(EX_NOUSER, m, CurEnv);
2824174Seric 			}
2834174Seric 			else
2844174Seric 			{
2854993Seric 				char nbuf[MAXNAME];
2864993Seric 
2874376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2884376Seric 				{
2894376Seric 					a->q_user = newstr(pw->pw_name);
2907008Seric 					(void) strcpy(buf, pw->pw_name);
2914376Seric 				}
2924174Seric 				a->q_home = newstr(pw->pw_dir);
2934213Seric 				a->q_uid = pw->pw_uid;
2944399Seric 				a->q_gid = pw->pw_gid;
2954404Seric 				a->q_flags |= QGOODUID;
2964998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
2974993Seric 				if (nbuf[0] != '\0')
2984993Seric 					a->q_fullname = newstr(nbuf);
2994399Seric 				if (!quoted)
3005006Seric 					forward(a, sendq);
3014174Seric 			}
3024174Seric 		}
3034174Seric 	}
30412613Seric 	return (a);
3054174Seric }
3064174Seric /*
3074373Seric **  FINDUSER -- find the password entry for a user.
3084373Seric **
3094373Seric **	This looks a lot like getpwnam, except that it may want to
3104373Seric **	do some fancier pattern matching in /etc/passwd.
3114373Seric **
3129379Seric **	This routine contains most of the time of many sendmail runs.
3139379Seric **	It deserves to be optimized.
3149379Seric **
3154373Seric **	Parameters:
3164373Seric **		name -- the name to match against.
3174373Seric **
3184373Seric **	Returns:
3194373Seric **		A pointer to a pw struct.
3204373Seric **		NULL if name is unknown or ambiguous.
3214373Seric **
3224373Seric **	Side Effects:
3234407Seric **		may modify name.
3244373Seric */
3254373Seric 
3264373Seric struct passwd *
3274373Seric finduser(name)
3284373Seric 	char *name;
3294373Seric {
3304376Seric 	register struct passwd *pw;
3314407Seric 	register char *p;
332*15325Seric 	extern struct passwd *getpwent();
333*15325Seric 	extern struct passwd *getpwnam();
3344373Seric 
3354407Seric 	/*
3364407Seric 	**  Make name canonical.
3374407Seric 	*/
3384407Seric 
3394407Seric 	for (p = name; *p != '\0'; p++)
3404407Seric 	{
3419044Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
3424407Seric 			*p = ' ';
3434407Seric 	}
3444407Seric 
34512634Seric 	/* look up this login name */
34612634Seric 	if ((pw = getpwnam(name)) != NULL)
34712634Seric 		return (pw);
34812634Seric 
34912634Seric 	/* search for a matching full name instead */
3504376Seric 	setpwent();
3514376Seric 	while ((pw = getpwent()) != NULL)
3524376Seric 	{
3534998Seric 		char buf[MAXNAME];
3544993Seric 		extern bool sameword();
3554376Seric 
3564376Seric 		if (strcmp(pw->pw_name, name) == 0)
3574376Seric 			return (pw);
3584998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3594407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3604381Seric 		{
3617054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
3624376Seric 			return (pw);
3634377Seric 		}
3644376Seric 	}
3654376Seric 	return (NULL);
3664373Seric }
3674373Seric /*
3684329Seric **  WRITABLE -- predicate returning if the file is writable.
3694329Seric **
3704329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3714329Seric **	Unfortunately, we cannot use the access call since we
3724329Seric **	won't necessarily be the real uid when we try to
3734329Seric **	actually open the file.
3744329Seric **
3754329Seric **	Notice that ANY file with ANY execute bit is automatically
3764329Seric **	not writable.  This is also enforced by mailfile.
3774329Seric **
3784329Seric **	Parameters:
3794329Seric **		s -- pointer to a stat struct for the file.
3804329Seric **
3814329Seric **	Returns:
3824329Seric **		TRUE -- if we will be able to write this file.
3834329Seric **		FALSE -- if we cannot write this file.
3844329Seric **
3854329Seric **	Side Effects:
3864329Seric **		none.
3874329Seric */
3884329Seric 
3894329Seric bool
3904329Seric writable(s)
3914329Seric 	register struct stat *s;
3924329Seric {
3934329Seric 	int euid, egid;
3944329Seric 	int bits;
3954329Seric 
3964329Seric 	if (bitset(0111, s->st_mode))
3974329Seric 		return (FALSE);
3984329Seric 	euid = getruid();
3994329Seric 	egid = getrgid();
4004329Seric 	if (geteuid() == 0)
4014329Seric 	{
4024329Seric 		if (bitset(S_ISUID, s->st_mode))
4034329Seric 			euid = s->st_uid;
4044329Seric 		if (bitset(S_ISGID, s->st_mode))
4054329Seric 			egid = s->st_gid;
4064329Seric 	}
4074329Seric 
4084329Seric 	if (euid == 0)
4094329Seric 		return (TRUE);
4104329Seric 	bits = S_IWRITE;
4114329Seric 	if (euid != s->st_uid)
4124329Seric 	{
4134329Seric 		bits >>= 3;
4144329Seric 		if (egid != s->st_gid)
4154329Seric 			bits >>= 3;
4164329Seric 	}
4174329Seric 	return ((s->st_mode & bits) != 0);
4184329Seric }
4194329Seric /*
4204174Seric **  INCLUDE -- handle :include: specification.
4214174Seric **
4224174Seric **	Parameters:
4234174Seric **		fname -- filename to include.
4244176Seric **		msg -- message to print in verbose mode.
4254399Seric **		ctladdr -- address template to use to fill in these
4264399Seric **			addresses -- effective user/group id are
4274399Seric **			the important things.
4285006Seric **		sendq -- a pointer to the head of the send queue
4295006Seric **			to put these addresses in.
4304174Seric **
4314174Seric **	Returns:
4324174Seric **		none.
4334174Seric **
4344174Seric **	Side Effects:
4354174Seric **		reads the :include: file and sends to everyone
4364174Seric **		listed in that file.
4374174Seric */
4384174Seric 
4395006Seric include(fname, msg, ctladdr, sendq)
4404174Seric 	char *fname;
4414176Seric 	char *msg;
4424399Seric 	ADDRESS *ctladdr;
4435006Seric 	ADDRESS **sendq;
4444174Seric {
4454174Seric 	char buf[MAXLINE];
4464174Seric 	register FILE *fp;
4476906Seric 	char *oldto = CurEnv->e_to;
4489379Seric 	char *oldfilename = FileName;
4499379Seric 	int oldlinenumber = LineNumber;
4504174Seric 
4514174Seric 	fp = fopen(fname, "r");
4524174Seric 	if (fp == NULL)
4534174Seric 	{
4544174Seric 		usrerr("Cannot open %s", fname);
4554174Seric 		return;
4564174Seric 	}
4574406Seric 	if (getctladdr(ctladdr) == NULL)
4584406Seric 	{
4594406Seric 		struct stat st;
4604174Seric 
4614406Seric 		if (fstat(fileno(fp), &st) < 0)
4624406Seric 			syserr("Cannot fstat %s!", fname);
4634406Seric 		ctladdr->q_uid = st.st_uid;
4644406Seric 		ctladdr->q_gid = st.st_gid;
4654406Seric 		ctladdr->q_flags |= QGOODUID;
4664406Seric 	}
4674406Seric 
4684174Seric 	/* read the file -- each line is a comma-separated list. */
4699379Seric 	FileName = fname;
4709379Seric 	LineNumber = 0;
4714174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4724174Seric 	{
4734174Seric 		register char *p = index(buf, '\n');
4744174Seric 
4754174Seric 		if (p != NULL)
4764174Seric 			*p = '\0';
4774174Seric 		if (buf[0] == '\0')
4784174Seric 			continue;
4796906Seric 		CurEnv->e_to = oldto;
4807054Seric 		message(Arpa_Info, "%s to %s", msg, buf);
4814176Seric 		AliasLevel++;
4829622Seric 		sendtolist(buf, ctladdr, sendq);
4834176Seric 		AliasLevel--;
4844174Seric 	}
4854174Seric 
4864319Seric 	(void) fclose(fp);
4879379Seric 	FileName = oldfilename;
4889379Seric 	LineNumber = oldlinenumber;
4894174Seric }
4904324Seric /*
4914324Seric **  SENDTOARGV -- send to an argument vector.
4924324Seric **
4934324Seric **	Parameters:
4944324Seric **		argv -- argument vector to send to.
4954324Seric **
4964324Seric **	Returns:
4974324Seric **		none.
4984324Seric **
4994324Seric **	Side Effects:
5004324Seric **		puts all addresses on the argument vector onto the
5014324Seric **			send queue.
5024324Seric */
5034324Seric 
5044324Seric sendtoargv(argv)
5054324Seric 	register char **argv;
5064324Seric {
5074324Seric 	register char *p;
5084324Seric 	extern bool sameword();
5094324Seric 
5104324Seric 	while ((p = *argv++) != NULL)
5114324Seric 	{
5124324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
5134324Seric 		{
5144324Seric 			char nbuf[MAXNAME];
5154324Seric 
5164324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
5174324Seric 				usrerr("address overflow");
5184324Seric 			else
5194324Seric 			{
5204324Seric 				(void) strcpy(nbuf, p);
5214324Seric 				(void) strcat(nbuf, "@");
5224324Seric 				(void) strcat(nbuf, argv[1]);
5234324Seric 				p = newstr(nbuf);
5244324Seric 				argv += 2;
5254324Seric 			}
5264324Seric 		}
5279622Seric 		sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
5284324Seric 	}
5294324Seric }
5304399Seric /*
5314399Seric **  GETCTLADDR -- get controlling address from an address header.
5324399Seric **
5334399Seric **	If none, get one corresponding to the effective userid.
5344399Seric **
5354399Seric **	Parameters:
5364399Seric **		a -- the address to find the controller of.
5374399Seric **
5384399Seric **	Returns:
5394399Seric **		the controlling address.
5404399Seric **
5414399Seric **	Side Effects:
5424399Seric **		none.
5434399Seric */
5444399Seric 
5454399Seric ADDRESS *
5464399Seric getctladdr(a)
5474399Seric 	register ADDRESS *a;
5484399Seric {
5494404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5504399Seric 		a = a->q_alias;
5514399Seric 	return (a);
5524399Seric }
553