14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*10109Seric SCCSID(@(#)recipient.c	3.51		01/03/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 */
404174Seric 
414324Seric # ifdef DEBUG
427676Seric 	if (tTd(25, 1))
434444Seric 	{
444444Seric 		printf("sendto: %s\n   ctladdr=", list);
454444Seric 		printaddr(ctladdr, FALSE);
464444Seric 	}
474324Seric # endif DEBUG
484324Seric 
498223Seric 	/* heuristic to determine old versus new style addresses */
508230Seric 	if (ctladdr == NULL &&
518230Seric 	    (index(list, ',') != NULL || index(list, ';') != NULL ||
528230Seric 	     index(list, '<') != NULL || index(list, '(') != NULL))
539340Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
548223Seric 
554423Seric 	firstone = TRUE;
564444Seric 	selfref = FALSE;
574324Seric 	al = NULL;
588223Seric 
598081Seric 	for (p = list; *p != '\0'; )
604174Seric 	{
618081Seric 		register ADDRESS *a;
628081Seric 		extern char *DelimChar;		/* defined in prescan */
634319Seric 
648081Seric 		/* parse the address */
658081Seric 		while (isspace(*p) || *p == ',')
664174Seric 			p++;
679887Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1);
689297Seric 		p = DelimChar;
699297Seric 		if (a == NULL)
704174Seric 			continue;
714324Seric 		a->q_next = al;
724399Seric 		a->q_alias = ctladdr;
734444Seric 
744444Seric 		/* see if this should be marked as a primary address */
754423Seric 		if (ctladdr == NULL ||
768081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
774423Seric 			a->q_flags |= QPRIMARY;
784444Seric 
794444Seric 		/* put on send queue or suppress self-reference */
809379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
814444Seric 			selfref = TRUE;
824444Seric 		else
834444Seric 			al = a;
844423Seric 		firstone = FALSE;
854324Seric 	}
864324Seric 
874444Seric 	/* if this alias doesn't include itself, delete ctladdr */
884444Seric 	if (!selfref && ctladdr != NULL)
894444Seric 		ctladdr->q_flags |= QDONTSEND;
904444Seric 
914324Seric 	/* arrange to send to everyone on the local send list */
924324Seric 	while (al != NULL)
934324Seric 	{
944324Seric 		register ADDRESS *a = al;
954324Seric 
964324Seric 		al = a->q_next;
975006Seric 		recipient(a, sendq);
984993Seric 
994998Seric 		/* arrange to inherit full name */
1004998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1014998Seric 			a->q_fullname = ctladdr->q_fullname;
1024174Seric 	}
1034324Seric 
1046906Seric 	CurEnv->e_to = NULL;
1054174Seric }
1064174Seric /*
1074174Seric **  RECIPIENT -- Designate a message recipient
1084174Seric **
1094174Seric **	Saves the named person for future mailing.
1104174Seric **
1114174Seric **	Parameters:
1124174Seric **		a -- the (preparsed) address header for the recipient.
1135006Seric **		sendq -- a pointer to the head of a queue to put the
1145006Seric **			recipient in.  Duplicate supression is done
1155006Seric **			in this queue.
1164174Seric **
1174174Seric **	Returns:
1184998Seric **		none.
1194174Seric **
1204174Seric **	Side Effects:
1214174Seric **		none.
1224174Seric */
1234174Seric 
1245006Seric recipient(a, sendq)
1254174Seric 	register ADDRESS *a;
1265006Seric 	register ADDRESS **sendq;
1274174Seric {
1284174Seric 	register ADDRESS *q;
1294319Seric 	ADDRESS **pq;
1304174Seric 	register struct mailer *m;
1319210Seric 	register char *p;
1329210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
1339210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1344399Seric 	extern ADDRESS *getctladdr();
1354627Seric 	extern bool safefile();
1364174Seric 
1376906Seric 	CurEnv->e_to = a->q_paddr;
1384600Seric 	m = a->q_mailer;
1394174Seric 	errno = 0;
1404174Seric # ifdef DEBUG
1417676Seric 	if (tTd(26, 1))
1424444Seric 	{
1434444Seric 		printf("\nrecipient: ");
1444444Seric 		printaddr(a, FALSE);
1454444Seric 	}
1464174Seric # endif DEBUG
1474174Seric 
1484174Seric 	/* break aliasing loops */
1494174Seric 	if (AliasLevel > MAXRCRSN)
1504174Seric 	{
1514174Seric 		usrerr("aliasing/forwarding loop broken");
1524998Seric 		return;
1534174Seric 	}
1544174Seric 
1554174Seric 	/*
1564627Seric 	**  Finish setting up address structure.
1574174Seric 	*/
1584174Seric 
1594627Seric 	a->q_timeout = TimeOut;
1604627Seric 
1619210Seric 	(void) strcpy(buf, a->q_user);
1629210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1639210Seric 	{
1649210Seric 		if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
1659210Seric 			quoted = TRUE;
1669210Seric 	}
1679210Seric 	stripquotes(buf, TRUE);
1689210Seric 
1694627Seric 	/* do sickly crude mapping for program mailing, etc. */
1709210Seric 	if (m == LocalMailer && buf[0] == '|')
1714174Seric 	{
1729210Seric 		a->q_mailer = m = ProgMailer;
1739210Seric 		a->q_user++;
1749210Seric 		if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
1754174Seric 		{
1769210Seric 			usrerr("Cannot mail directly to programs");
1779210Seric 			a->q_flags |= QDONTSEND;
1784174Seric 		}
1794174Seric 	}
1804174Seric 
1814174Seric 	/*
1824419Seric 	**  Look up this person in the recipient list.
1834419Seric 	**	If they are there already, return, otherwise continue.
1844419Seric 	**	If the list is empty, just add it.  Notice the cute
1854419Seric 	**	hack to make from addresses suppress things correctly:
1864419Seric 	**	the QDONTSEND bit will be set in the send list.
1874419Seric 	**	[Please note: the emphasis is on "hack."]
1884174Seric 	*/
1894174Seric 
1905006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1914174Seric 	{
1929379Seric 		if (!ForceMail && sameaddr(q, a))
1934174Seric 		{
1944174Seric # ifdef DEBUG
1957676Seric 			if (tTd(26, 1))
1964444Seric 			{
1974444Seric 				printf("%s in sendq: ", a->q_paddr);
1984444Seric 				printaddr(q, FALSE);
1994444Seric 			}
2004174Seric # endif DEBUG
2017054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2024324Seric 				message(Arpa_Info, "duplicate suppressed");
2034423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2044423Seric 				q->q_flags |= a->q_flags;
2054998Seric 			return;
2064174Seric 		}
2074319Seric 	}
2084174Seric 
2094319Seric 	/* add address on list */
2104319Seric 	*pq = a;
2114174Seric 	a->q_next = NULL;
2124174Seric 
2134174Seric 	/*
2144174Seric 	**  Alias the name and handle :include: specs.
2154174Seric 	*/
2164174Seric 
2179210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2184174Seric 	{
2194174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2204174Seric 		{
2214174Seric 			a->q_flags |= QDONTSEND;
2227676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2234399Seric 				usrerr("Cannot mail directly to :include:s");
2244399Seric 			else
2254399Seric 			{
2267054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
2275006Seric 				include(&a->q_user[9], " sending", a, sendq);
2284399Seric 			}
2294174Seric 		}
2304174Seric 		else
2315006Seric 			alias(a, sendq);
2324174Seric 	}
2334174Seric 
2344174Seric 	/*
2354174Seric 	**  If the user is local and still being sent, verify that
2364174Seric 	**  the address is good.  If it is, try to forward.
2374174Seric 	**  If the address is already good, we have a forwarding
2384174Seric 	**  loop.  This can be broken by just sending directly to
2394174Seric 	**  the user (which is probably correct anyway).
2404174Seric 	*/
2414174Seric 
2429210Seric 	if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer)
2434174Seric 	{
2444329Seric 		struct stat stb;
2454329Seric 		extern bool writable();
2464174Seric 
2474174Seric 		/* see if this is to a file */
2485600Seric 		if (buf[0] == '/')
2494174Seric 		{
2505600Seric 			p = rindex(buf, '/');
2514201Seric 			/* check if writable or creatable */
2527676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2534399Seric 			{
2544399Seric 				usrerr("Cannot mail directly to files");
2554399Seric 				a->q_flags |= QDONTSEND;
2564399Seric 			}
2574399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2584539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2594174Seric 			{
2604174Seric 				a->q_flags |= QBADADDR;
261*10109Seric 				giveresponse(EX_CANTCREAT, m, CurEnv);
2624174Seric 			}
2634174Seric 		}
2644174Seric 		else
2654174Seric 		{
2664174Seric 			register struct passwd *pw;
2674373Seric 			extern struct passwd *finduser();
2684373Seric 
2694407Seric 			/* warning -- finduser may trash buf */
2704373Seric 			pw = finduser(buf);
2714174Seric 			if (pw == NULL)
2724174Seric 			{
2734174Seric 				a->q_flags |= QBADADDR;
274*10109Seric 				giveresponse(EX_NOUSER, m, CurEnv);
2754174Seric 			}
2764174Seric 			else
2774174Seric 			{
2784993Seric 				char nbuf[MAXNAME];
2794993Seric 
2804376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2814376Seric 				{
2824376Seric 					a->q_user = newstr(pw->pw_name);
2837008Seric 					(void) strcpy(buf, pw->pw_name);
2844376Seric 				}
2854174Seric 				a->q_home = newstr(pw->pw_dir);
2864213Seric 				a->q_uid = pw->pw_uid;
2874399Seric 				a->q_gid = pw->pw_gid;
2884404Seric 				a->q_flags |= QGOODUID;
2894998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
2904993Seric 				if (nbuf[0] != '\0')
2914993Seric 					a->q_fullname = newstr(nbuf);
2924399Seric 				if (!quoted)
2935006Seric 					forward(a, sendq);
2944174Seric 			}
2954174Seric 		}
2964174Seric 	}
2974174Seric }
2984174Seric /*
2994373Seric **  FINDUSER -- find the password entry for a user.
3004373Seric **
3014373Seric **	This looks a lot like getpwnam, except that it may want to
3024373Seric **	do some fancier pattern matching in /etc/passwd.
3034373Seric **
3049379Seric **	This routine contains most of the time of many sendmail runs.
3059379Seric **	It deserves to be optimized.
3069379Seric **
3074373Seric **	Parameters:
3084373Seric **		name -- the name to match against.
3094373Seric **
3104373Seric **	Returns:
3114373Seric **		A pointer to a pw struct.
3124373Seric **		NULL if name is unknown or ambiguous.
3134373Seric **
3144373Seric **	Side Effects:
3154407Seric **		may modify name.
3164373Seric */
3174373Seric 
3184373Seric struct passwd *
3194373Seric finduser(name)
3204373Seric 	char *name;
3214373Seric {
3224376Seric 	extern struct passwd *getpwent();
3234376Seric 	register struct passwd *pw;
3244407Seric 	register char *p;
3254373Seric 
3264407Seric 	/*
3274407Seric 	**  Make name canonical.
3284407Seric 	*/
3294407Seric 
3304407Seric 	for (p = name; *p != '\0'; p++)
3314407Seric 	{
3329044Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
3334407Seric 			*p = ' ';
3344407Seric 	}
3354407Seric 
3364376Seric 	setpwent();
3374376Seric 	while ((pw = getpwent()) != NULL)
3384376Seric 	{
3394998Seric 		char buf[MAXNAME];
3404993Seric 		extern bool sameword();
3414376Seric 
3424376Seric 		if (strcmp(pw->pw_name, name) == 0)
3434376Seric 			return (pw);
3444998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3454407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3464381Seric 		{
3477054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
3484376Seric 			return (pw);
3494377Seric 		}
3504376Seric 	}
3514376Seric 	return (NULL);
3524373Seric }
3534373Seric /*
3544329Seric **  WRITABLE -- predicate returning if the file is writable.
3554329Seric **
3564329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3574329Seric **	Unfortunately, we cannot use the access call since we
3584329Seric **	won't necessarily be the real uid when we try to
3594329Seric **	actually open the file.
3604329Seric **
3614329Seric **	Notice that ANY file with ANY execute bit is automatically
3624329Seric **	not writable.  This is also enforced by mailfile.
3634329Seric **
3644329Seric **	Parameters:
3654329Seric **		s -- pointer to a stat struct for the file.
3664329Seric **
3674329Seric **	Returns:
3684329Seric **		TRUE -- if we will be able to write this file.
3694329Seric **		FALSE -- if we cannot write this file.
3704329Seric **
3714329Seric **	Side Effects:
3724329Seric **		none.
3734329Seric */
3744329Seric 
3754329Seric bool
3764329Seric writable(s)
3774329Seric 	register struct stat *s;
3784329Seric {
3794329Seric 	int euid, egid;
3804329Seric 	int bits;
3814329Seric 
3824329Seric 	if (bitset(0111, s->st_mode))
3834329Seric 		return (FALSE);
3844329Seric 	euid = getruid();
3854329Seric 	egid = getrgid();
3864329Seric 	if (geteuid() == 0)
3874329Seric 	{
3884329Seric 		if (bitset(S_ISUID, s->st_mode))
3894329Seric 			euid = s->st_uid;
3904329Seric 		if (bitset(S_ISGID, s->st_mode))
3914329Seric 			egid = s->st_gid;
3924329Seric 	}
3934329Seric 
3944329Seric 	if (euid == 0)
3954329Seric 		return (TRUE);
3964329Seric 	bits = S_IWRITE;
3974329Seric 	if (euid != s->st_uid)
3984329Seric 	{
3994329Seric 		bits >>= 3;
4004329Seric 		if (egid != s->st_gid)
4014329Seric 			bits >>= 3;
4024329Seric 	}
4034329Seric 	return ((s->st_mode & bits) != 0);
4044329Seric }
4054329Seric /*
4064174Seric **  INCLUDE -- handle :include: specification.
4074174Seric **
4084174Seric **	Parameters:
4094174Seric **		fname -- filename to include.
4104176Seric **		msg -- message to print in verbose mode.
4114399Seric **		ctladdr -- address template to use to fill in these
4124399Seric **			addresses -- effective user/group id are
4134399Seric **			the important things.
4145006Seric **		sendq -- a pointer to the head of the send queue
4155006Seric **			to put these addresses in.
4164174Seric **
4174174Seric **	Returns:
4184174Seric **		none.
4194174Seric **
4204174Seric **	Side Effects:
4214174Seric **		reads the :include: file and sends to everyone
4224174Seric **		listed in that file.
4234174Seric */
4244174Seric 
4255006Seric include(fname, msg, ctladdr, sendq)
4264174Seric 	char *fname;
4274176Seric 	char *msg;
4284399Seric 	ADDRESS *ctladdr;
4295006Seric 	ADDRESS **sendq;
4304174Seric {
4314174Seric 	char buf[MAXLINE];
4324174Seric 	register FILE *fp;
4336906Seric 	char *oldto = CurEnv->e_to;
4349379Seric 	char *oldfilename = FileName;
4359379Seric 	int oldlinenumber = LineNumber;
4364174Seric 
4374174Seric 	fp = fopen(fname, "r");
4384174Seric 	if (fp == NULL)
4394174Seric 	{
4404174Seric 		usrerr("Cannot open %s", fname);
4414174Seric 		return;
4424174Seric 	}
4434406Seric 	if (getctladdr(ctladdr) == NULL)
4444406Seric 	{
4454406Seric 		struct stat st;
4464174Seric 
4474406Seric 		if (fstat(fileno(fp), &st) < 0)
4484406Seric 			syserr("Cannot fstat %s!", fname);
4494406Seric 		ctladdr->q_uid = st.st_uid;
4504406Seric 		ctladdr->q_gid = st.st_gid;
4514406Seric 		ctladdr->q_flags |= QGOODUID;
4524406Seric 	}
4534406Seric 
4544174Seric 	/* read the file -- each line is a comma-separated list. */
4559379Seric 	FileName = fname;
4569379Seric 	LineNumber = 0;
4574174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4584174Seric 	{
4594174Seric 		register char *p = index(buf, '\n');
4604174Seric 
4614174Seric 		if (p != NULL)
4624174Seric 			*p = '\0';
4634174Seric 		if (buf[0] == '\0')
4644174Seric 			continue;
4656906Seric 		CurEnv->e_to = oldto;
4667054Seric 		message(Arpa_Info, "%s to %s", msg, buf);
4674176Seric 		AliasLevel++;
4689622Seric 		sendtolist(buf, ctladdr, sendq);
4694176Seric 		AliasLevel--;
4704174Seric 	}
4714174Seric 
4724319Seric 	(void) fclose(fp);
4739379Seric 	FileName = oldfilename;
4749379Seric 	LineNumber = oldlinenumber;
4754174Seric }
4764324Seric /*
4774324Seric **  SENDTOARGV -- send to an argument vector.
4784324Seric **
4794324Seric **	Parameters:
4804324Seric **		argv -- argument vector to send to.
4814324Seric **
4824324Seric **	Returns:
4834324Seric **		none.
4844324Seric **
4854324Seric **	Side Effects:
4864324Seric **		puts all addresses on the argument vector onto the
4874324Seric **			send queue.
4884324Seric */
4894324Seric 
4904324Seric sendtoargv(argv)
4914324Seric 	register char **argv;
4924324Seric {
4934324Seric 	register char *p;
4944324Seric 	extern bool sameword();
4954324Seric 
4964324Seric 	while ((p = *argv++) != NULL)
4974324Seric 	{
4984324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4994324Seric 		{
5004324Seric 			char nbuf[MAXNAME];
5014324Seric 
5024324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
5034324Seric 				usrerr("address overflow");
5044324Seric 			else
5054324Seric 			{
5064324Seric 				(void) strcpy(nbuf, p);
5074324Seric 				(void) strcat(nbuf, "@");
5084324Seric 				(void) strcat(nbuf, argv[1]);
5094324Seric 				p = newstr(nbuf);
5104324Seric 				argv += 2;
5114324Seric 			}
5124324Seric 		}
5139622Seric 		sendtolist(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
5144324Seric 	}
5154324Seric }
5164399Seric /*
5174399Seric **  GETCTLADDR -- get controlling address from an address header.
5184399Seric **
5194399Seric **	If none, get one corresponding to the effective userid.
5204399Seric **
5214399Seric **	Parameters:
5224399Seric **		a -- the address to find the controller of.
5234399Seric **
5244399Seric **	Returns:
5254399Seric **		the controlling address.
5264399Seric **
5274399Seric **	Side Effects:
5284399Seric **		none.
5294399Seric */
5304399Seric 
5314399Seric ADDRESS *
5324399Seric getctladdr(a)
5334399Seric 	register ADDRESS *a;
5344399Seric {
5354404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5364399Seric 		a = a->q_alias;
5374399Seric 	return (a);
5384399Seric }
539