14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*9044Seric SCCSID(@(#)recipient.c	3.43		11/03/82);
64174Seric 
74174Seric /*
84174Seric **  SENDTO -- 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 
318081Seric sendto(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))
538223Seric 		CurEnv->e_oldstyle = FALSE;
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++;
678081Seric 		if ((a = parse(p, (ADDRESS *) NULL, 1)) == NULL)
684174Seric 			continue;
698081Seric 		p = DelimChar;
704324Seric 		a->q_next = al;
714399Seric 		a->q_alias = ctladdr;
724444Seric 
734444Seric 		/* see if this should be marked as a primary address */
744423Seric 		if (ctladdr == NULL ||
758081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
764423Seric 			a->q_flags |= QPRIMARY;
774444Seric 
784444Seric 		/* put on send queue or suppress self-reference */
794444Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE))
804444Seric 			selfref = TRUE;
814444Seric 		else
824444Seric 			al = a;
834423Seric 		firstone = FALSE;
844324Seric 	}
854324Seric 
864444Seric 	/* if this alias doesn't include itself, delete ctladdr */
874444Seric 	if (!selfref && ctladdr != NULL)
884444Seric 		ctladdr->q_flags |= QDONTSEND;
894444Seric 
904324Seric 	/* arrange to send to everyone on the local send list */
914324Seric 	while (al != NULL)
924324Seric 	{
934324Seric 		register ADDRESS *a = al;
944324Seric 
954324Seric 		al = a->q_next;
965006Seric 		recipient(a, sendq);
974993Seric 
984998Seric 		/* arrange to inherit full name */
994998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1004998Seric 			a->q_fullname = ctladdr->q_fullname;
1014174Seric 	}
1024324Seric 
1036906Seric 	CurEnv->e_to = NULL;
1044174Seric }
1054174Seric /*
1064174Seric **  RECIPIENT -- Designate a message recipient
1074174Seric **
1084174Seric **	Saves the named person for future mailing.
1094174Seric **
1104174Seric **	Parameters:
1114174Seric **		a -- the (preparsed) address header for the recipient.
1125006Seric **		sendq -- a pointer to the head of a queue to put the
1135006Seric **			recipient in.  Duplicate supression is done
1145006Seric **			in this queue.
1154174Seric **
1164174Seric **	Returns:
1174998Seric **		none.
1184174Seric **
1194174Seric **	Side Effects:
1204174Seric **		none.
1214174Seric */
1224174Seric 
1235006Seric recipient(a, sendq)
1244174Seric 	register ADDRESS *a;
1255006Seric 	register ADDRESS **sendq;
1264174Seric {
1274174Seric 	register ADDRESS *q;
1284319Seric 	ADDRESS **pq;
1294174Seric 	register struct mailer *m;
1304399Seric 	extern ADDRESS *getctladdr();
1314627Seric 	extern bool safefile();
1324174Seric 
1336906Seric 	CurEnv->e_to = a->q_paddr;
1344600Seric 	m = a->q_mailer;
1354174Seric 	errno = 0;
1364174Seric # ifdef DEBUG
1377676Seric 	if (tTd(26, 1))
1384444Seric 	{
1394444Seric 		printf("\nrecipient: ");
1404444Seric 		printaddr(a, FALSE);
1414444Seric 	}
1424174Seric # endif DEBUG
1434174Seric 
1444174Seric 	/* break aliasing loops */
1454174Seric 	if (AliasLevel > MAXRCRSN)
1464174Seric 	{
1474174Seric 		usrerr("aliasing/forwarding loop broken");
1484998Seric 		return;
1494174Seric 	}
1504174Seric 
1514174Seric 	/*
1524627Seric 	**  Finish setting up address structure.
1534174Seric 	*/
1544174Seric 
1554627Seric 	a->q_timeout = TimeOut;
1564627Seric 
1574627Seric 	/* do sickly crude mapping for program mailing, etc. */
1584600Seric 	if (a->q_mailer == LocalMailer)
1594174Seric 	{
1604174Seric 		if (a->q_user[0] == '|')
1614174Seric 		{
1624600Seric 			a->q_mailer = m = ProgMailer;
1634174Seric 			a->q_user++;
1647676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
1654217Seric 			{
1664217Seric 				usrerr("Cannot mail directly to programs");
1674217Seric 				a->q_flags |= QDONTSEND;
1684217Seric 			}
1694174Seric 		}
1704174Seric 	}
1714174Seric 
1724174Seric 	/*
1734419Seric 	**  Look up this person in the recipient list.
1744419Seric 	**	If they are there already, return, otherwise continue.
1754419Seric 	**	If the list is empty, just add it.  Notice the cute
1764419Seric 	**	hack to make from addresses suppress things correctly:
1774419Seric 	**	the QDONTSEND bit will be set in the send list.
1784419Seric 	**	[Please note: the emphasis is on "hack."]
1794174Seric 	*/
1804174Seric 
1815006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
1824174Seric 	{
1834319Seric 		if (!ForceMail && sameaddr(q, a, FALSE))
1844174Seric 		{
1854174Seric # ifdef DEBUG
1867676Seric 			if (tTd(26, 1))
1874444Seric 			{
1884444Seric 				printf("%s in sendq: ", a->q_paddr);
1894444Seric 				printaddr(q, FALSE);
1904444Seric 			}
1914174Seric # endif DEBUG
1927054Seric 			if (!bitset(QDONTSEND, a->q_flags))
1934324Seric 				message(Arpa_Info, "duplicate suppressed");
1944423Seric 			if (!bitset(QPRIMARY, q->q_flags))
1954423Seric 				q->q_flags |= a->q_flags;
1964998Seric 			return;
1974174Seric 		}
1984319Seric 	}
1994174Seric 
2004319Seric 	/* add address on list */
2014319Seric 	*pq = a;
2024174Seric 	a->q_next = NULL;
2034247Seric 	if (DontSend)
2044247Seric 		a->q_flags |= QDONTSEND;
2054174Seric 
2064174Seric 	/*
2074174Seric 	**  Alias the name and handle :include: specs.
2084174Seric 	*/
2094174Seric 
2104600Seric 	if (a->q_mailer == LocalMailer)
2114174Seric 	{
2124174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2134174Seric 		{
2144174Seric 			a->q_flags |= QDONTSEND;
2157676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2164399Seric 				usrerr("Cannot mail directly to :include:s");
2174399Seric 			else
2184399Seric 			{
2197054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
2205006Seric 				include(&a->q_user[9], " sending", a, sendq);
2214399Seric 			}
2224174Seric 		}
2234174Seric 		else
2245006Seric 			alias(a, sendq);
2254174Seric 	}
2264174Seric 
2274174Seric 	/*
2284174Seric 	**  If the user is local and still being sent, verify that
2294174Seric 	**  the address is good.  If it is, try to forward.
2304174Seric 	**  If the address is already good, we have a forwarding
2314174Seric 	**  loop.  This can be broken by just sending directly to
2324174Seric 	**  the user (which is probably correct anyway).
2334174Seric 	*/
2344174Seric 
2354600Seric 	if (!bitset(QDONTSEND, a->q_flags) && a->q_mailer == LocalMailer)
2364174Seric 	{
2374174Seric 		char buf[MAXNAME];
2384201Seric 		register char *p;
2394329Seric 		struct stat stb;
2404329Seric 		extern bool writable();
2414399Seric 		bool quoted = FALSE;
2424174Seric 
2437008Seric 		(void) strcpy(buf, a->q_user);
2444399Seric 		for (p = buf; *p != '\0' && !quoted; p++)
2454399Seric 		{
246*9044Seric 			if (!isascii(*p) && (*p & 0377) != (SpaceSub & 0377))
2474399Seric 				quoted = TRUE;
2484399Seric 		}
2494174Seric 		stripquotes(buf, TRUE);
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;
2654174Seric 				giveresponse(EX_CANTCREAT, TRUE, m);
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;
2784174Seric 				giveresponse(EX_NOUSER, TRUE, m);
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 **
3084373Seric **	Parameters:
3094373Seric **		name -- the name to match against.
3104373Seric **
3114373Seric **	Returns:
3124373Seric **		A pointer to a pw struct.
3134373Seric **		NULL if name is unknown or ambiguous.
3144373Seric **
3154373Seric **	Side Effects:
3164407Seric **		may modify name.
3174373Seric */
3184373Seric 
3194373Seric struct passwd *
3204373Seric finduser(name)
3214373Seric 	char *name;
3224373Seric {
3234376Seric 	extern struct passwd *getpwent();
3244376Seric 	register struct passwd *pw;
3254407Seric 	register char *p;
3264373Seric 
3274407Seric 	/*
3284407Seric 	**  Make name canonical.
3294407Seric 	*/
3304407Seric 
3314407Seric 	for (p = name; *p != '\0'; p++)
3324407Seric 	{
333*9044Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
3344407Seric 			*p = ' ';
3354407Seric 	}
3364407Seric 
3374376Seric 	setpwent();
3384376Seric 	while ((pw = getpwent()) != NULL)
3394376Seric 	{
3404998Seric 		char buf[MAXNAME];
3414993Seric 		extern bool sameword();
3424376Seric 
3434376Seric 		if (strcmp(pw->pw_name, name) == 0)
3444376Seric 			return (pw);
3454998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3464407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3474381Seric 		{
3487054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
3494376Seric 			return (pw);
3504377Seric 		}
3514376Seric 	}
3524376Seric 	return (NULL);
3534373Seric }
3544373Seric /*
3554329Seric **  WRITABLE -- predicate returning if the file is writable.
3564329Seric **
3574329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3584329Seric **	Unfortunately, we cannot use the access call since we
3594329Seric **	won't necessarily be the real uid when we try to
3604329Seric **	actually open the file.
3614329Seric **
3624329Seric **	Notice that ANY file with ANY execute bit is automatically
3634329Seric **	not writable.  This is also enforced by mailfile.
3644329Seric **
3654329Seric **	Parameters:
3664329Seric **		s -- pointer to a stat struct for the file.
3674329Seric **
3684329Seric **	Returns:
3694329Seric **		TRUE -- if we will be able to write this file.
3704329Seric **		FALSE -- if we cannot write this file.
3714329Seric **
3724329Seric **	Side Effects:
3734329Seric **		none.
3744329Seric */
3754329Seric 
3764329Seric bool
3774329Seric writable(s)
3784329Seric 	register struct stat *s;
3794329Seric {
3804329Seric 	int euid, egid;
3814329Seric 	int bits;
3824329Seric 
3834329Seric 	if (bitset(0111, s->st_mode))
3844329Seric 		return (FALSE);
3854329Seric 	euid = getruid();
3864329Seric 	egid = getrgid();
3874329Seric 	if (geteuid() == 0)
3884329Seric 	{
3894329Seric 		if (bitset(S_ISUID, s->st_mode))
3904329Seric 			euid = s->st_uid;
3914329Seric 		if (bitset(S_ISGID, s->st_mode))
3924329Seric 			egid = s->st_gid;
3934329Seric 	}
3944329Seric 
3954329Seric 	if (euid == 0)
3964329Seric 		return (TRUE);
3974329Seric 	bits = S_IWRITE;
3984329Seric 	if (euid != s->st_uid)
3994329Seric 	{
4004329Seric 		bits >>= 3;
4014329Seric 		if (egid != s->st_gid)
4024329Seric 			bits >>= 3;
4034329Seric 	}
4044329Seric 	return ((s->st_mode & bits) != 0);
4054329Seric }
4064329Seric /*
4074174Seric **  INCLUDE -- handle :include: specification.
4084174Seric **
4094174Seric **	Parameters:
4104174Seric **		fname -- filename to include.
4114176Seric **		msg -- message to print in verbose mode.
4124399Seric **		ctladdr -- address template to use to fill in these
4134399Seric **			addresses -- effective user/group id are
4144399Seric **			the important things.
4155006Seric **		sendq -- a pointer to the head of the send queue
4165006Seric **			to put these addresses in.
4174174Seric **
4184174Seric **	Returns:
4194174Seric **		none.
4204174Seric **
4214174Seric **	Side Effects:
4224174Seric **		reads the :include: file and sends to everyone
4234174Seric **		listed in that file.
4244174Seric */
4254174Seric 
4265006Seric include(fname, msg, ctladdr, sendq)
4274174Seric 	char *fname;
4284176Seric 	char *msg;
4294399Seric 	ADDRESS *ctladdr;
4305006Seric 	ADDRESS **sendq;
4314174Seric {
4324174Seric 	char buf[MAXLINE];
4334174Seric 	register FILE *fp;
4346906Seric 	char *oldto = CurEnv->e_to;
4354174Seric 
4364174Seric 	fp = fopen(fname, "r");
4374174Seric 	if (fp == NULL)
4384174Seric 	{
4394174Seric 		usrerr("Cannot open %s", fname);
4404174Seric 		return;
4414174Seric 	}
4424406Seric 	if (getctladdr(ctladdr) == NULL)
4434406Seric 	{
4444406Seric 		struct stat st;
4454174Seric 
4464406Seric 		if (fstat(fileno(fp), &st) < 0)
4474406Seric 			syserr("Cannot fstat %s!", fname);
4484406Seric 		ctladdr->q_uid = st.st_uid;
4494406Seric 		ctladdr->q_gid = st.st_gid;
4504406Seric 		ctladdr->q_flags |= QGOODUID;
4514406Seric 	}
4524406Seric 
4534174Seric 	/* read the file -- each line is a comma-separated list. */
4544174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4554174Seric 	{
4564174Seric 		register char *p = index(buf, '\n');
4574174Seric 
4584174Seric 		if (p != NULL)
4594174Seric 			*p = '\0';
4604174Seric 		if (buf[0] == '\0')
4614174Seric 			continue;
4626906Seric 		CurEnv->e_to = oldto;
4637054Seric 		message(Arpa_Info, "%s to %s", msg, buf);
4644176Seric 		AliasLevel++;
4658081Seric 		sendto(buf, ctladdr, sendq);
4664176Seric 		AliasLevel--;
4674174Seric 	}
4684174Seric 
4694319Seric 	(void) fclose(fp);
4704174Seric }
4714324Seric /*
4724324Seric **  SENDTOARGV -- send to an argument vector.
4734324Seric **
4744324Seric **	Parameters:
4754324Seric **		argv -- argument vector to send to.
4764324Seric **
4774324Seric **	Returns:
4784324Seric **		none.
4794324Seric **
4804324Seric **	Side Effects:
4814324Seric **		puts all addresses on the argument vector onto the
4824324Seric **			send queue.
4834324Seric */
4844324Seric 
4854324Seric sendtoargv(argv)
4864324Seric 	register char **argv;
4874324Seric {
4884324Seric 	register char *p;
4894324Seric 	extern bool sameword();
4904324Seric 
4914324Seric 	while ((p = *argv++) != NULL)
4924324Seric 	{
4934324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4944324Seric 		{
4954324Seric 			char nbuf[MAXNAME];
4964324Seric 
4974324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
4984324Seric 				usrerr("address overflow");
4994324Seric 			else
5004324Seric 			{
5014324Seric 				(void) strcpy(nbuf, p);
5024324Seric 				(void) strcat(nbuf, "@");
5034324Seric 				(void) strcat(nbuf, argv[1]);
5044324Seric 				p = newstr(nbuf);
5054324Seric 				argv += 2;
5064324Seric 			}
5074324Seric 		}
5088081Seric 		sendto(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
5094324Seric 	}
5104324Seric }
5114399Seric /*
5124399Seric **  GETCTLADDR -- get controlling address from an address header.
5134399Seric **
5144399Seric **	If none, get one corresponding to the effective userid.
5154399Seric **
5164399Seric **	Parameters:
5174399Seric **		a -- the address to find the controller of.
5184399Seric **
5194399Seric **	Returns:
5204399Seric **		the controlling address.
5214399Seric **
5224399Seric **	Side Effects:
5234399Seric **		none.
5244399Seric */
5254399Seric 
5264399Seric ADDRESS *
5274399Seric getctladdr(a)
5284399Seric 	register ADDRESS *a;
5294399Seric {
5304404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5314399Seric 		a = a->q_alias;
5324399Seric 	return (a);
5334399Seric }
534