14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*9297Seric SCCSID(@(#)recipient.c	3.46		11/18/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++;
67*9297Seric 		a = parse(p, (ADDRESS *) NULL, 1);
68*9297Seric 		p = DelimChar;
69*9297Seric 		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 */
804444Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE))
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 	{
1924319Seric 		if (!ForceMail && sameaddr(q, a, FALSE))
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;
2124247Seric 	if (DontSend)
2134247Seric 		a->q_flags |= QDONTSEND;
2144174Seric 
2154174Seric 	/*
2164174Seric 	**  Alias the name and handle :include: specs.
2174174Seric 	*/
2184174Seric 
2199210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2204174Seric 	{
2214174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2224174Seric 		{
2234174Seric 			a->q_flags |= QDONTSEND;
2247676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2254399Seric 				usrerr("Cannot mail directly to :include:s");
2264399Seric 			else
2274399Seric 			{
2287054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
2295006Seric 				include(&a->q_user[9], " sending", a, sendq);
2304399Seric 			}
2314174Seric 		}
2324174Seric 		else
2335006Seric 			alias(a, sendq);
2344174Seric 	}
2354174Seric 
2364174Seric 	/*
2374174Seric 	**  If the user is local and still being sent, verify that
2384174Seric 	**  the address is good.  If it is, try to forward.
2394174Seric 	**  If the address is already good, we have a forwarding
2404174Seric 	**  loop.  This can be broken by just sending directly to
2414174Seric 	**  the user (which is probably correct anyway).
2424174Seric 	*/
2434174Seric 
2449210Seric 	if (!bitset(QDONTSEND, a->q_flags) && m == LocalMailer)
2454174Seric 	{
2464329Seric 		struct stat stb;
2474329Seric 		extern bool writable();
2484174Seric 
2494174Seric 		/* see if this is to a file */
2505600Seric 		if (buf[0] == '/')
2514174Seric 		{
2525600Seric 			p = rindex(buf, '/');
2534201Seric 			/* check if writable or creatable */
2547676Seric 			if (a->q_alias == NULL && !tTd(0, 1) && !QueueRun && !ForceMail)
2554399Seric 			{
2564399Seric 				usrerr("Cannot mail directly to files");
2574399Seric 				a->q_flags |= QDONTSEND;
2584399Seric 			}
2594399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2604539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2614174Seric 			{
2624174Seric 				a->q_flags |= QBADADDR;
2634174Seric 				giveresponse(EX_CANTCREAT, TRUE, m);
2644174Seric 			}
2654174Seric 		}
2664174Seric 		else
2674174Seric 		{
2684174Seric 			register struct passwd *pw;
2694373Seric 			extern struct passwd *finduser();
2704373Seric 
2714407Seric 			/* warning -- finduser may trash buf */
2724373Seric 			pw = finduser(buf);
2734174Seric 			if (pw == NULL)
2744174Seric 			{
2754174Seric 				a->q_flags |= QBADADDR;
2764174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2774174Seric 			}
2784174Seric 			else
2794174Seric 			{
2804993Seric 				char nbuf[MAXNAME];
2814993Seric 
2824376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2834376Seric 				{
2844376Seric 					a->q_user = newstr(pw->pw_name);
2857008Seric 					(void) strcpy(buf, pw->pw_name);
2864376Seric 				}
2874174Seric 				a->q_home = newstr(pw->pw_dir);
2884213Seric 				a->q_uid = pw->pw_uid;
2894399Seric 				a->q_gid = pw->pw_gid;
2904404Seric 				a->q_flags |= QGOODUID;
2914998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
2924993Seric 				if (nbuf[0] != '\0')
2934993Seric 					a->q_fullname = newstr(nbuf);
2944399Seric 				if (!quoted)
2955006Seric 					forward(a, sendq);
2964174Seric 			}
2974174Seric 		}
2984174Seric 	}
2994174Seric }
3004174Seric /*
3014373Seric **  FINDUSER -- find the password entry for a user.
3024373Seric **
3034373Seric **	This looks a lot like getpwnam, except that it may want to
3044373Seric **	do some fancier pattern matching in /etc/passwd.
3054373Seric **
3064373Seric **	Parameters:
3074373Seric **		name -- the name to match against.
3084373Seric **
3094373Seric **	Returns:
3104373Seric **		A pointer to a pw struct.
3114373Seric **		NULL if name is unknown or ambiguous.
3124373Seric **
3134373Seric **	Side Effects:
3144407Seric **		may modify name.
3154373Seric */
3164373Seric 
3174373Seric struct passwd *
3184373Seric finduser(name)
3194373Seric 	char *name;
3204373Seric {
3214376Seric 	extern struct passwd *getpwent();
3224376Seric 	register struct passwd *pw;
3234407Seric 	register char *p;
3244373Seric 
3254407Seric 	/*
3264407Seric 	**  Make name canonical.
3274407Seric 	*/
3284407Seric 
3294407Seric 	for (p = name; *p != '\0'; p++)
3304407Seric 	{
3319044Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
3324407Seric 			*p = ' ';
3334407Seric 	}
3344407Seric 
3354376Seric 	setpwent();
3364376Seric 	while ((pw = getpwent()) != NULL)
3374376Seric 	{
3384998Seric 		char buf[MAXNAME];
3394993Seric 		extern bool sameword();
3404376Seric 
3414376Seric 		if (strcmp(pw->pw_name, name) == 0)
3424376Seric 			return (pw);
3434998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3444407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3454381Seric 		{
3467054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
3474376Seric 			return (pw);
3484377Seric 		}
3494376Seric 	}
3504376Seric 	return (NULL);
3514373Seric }
3524373Seric /*
3534329Seric **  WRITABLE -- predicate returning if the file is writable.
3544329Seric **
3554329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3564329Seric **	Unfortunately, we cannot use the access call since we
3574329Seric **	won't necessarily be the real uid when we try to
3584329Seric **	actually open the file.
3594329Seric **
3604329Seric **	Notice that ANY file with ANY execute bit is automatically
3614329Seric **	not writable.  This is also enforced by mailfile.
3624329Seric **
3634329Seric **	Parameters:
3644329Seric **		s -- pointer to a stat struct for the file.
3654329Seric **
3664329Seric **	Returns:
3674329Seric **		TRUE -- if we will be able to write this file.
3684329Seric **		FALSE -- if we cannot write this file.
3694329Seric **
3704329Seric **	Side Effects:
3714329Seric **		none.
3724329Seric */
3734329Seric 
3744329Seric bool
3754329Seric writable(s)
3764329Seric 	register struct stat *s;
3774329Seric {
3784329Seric 	int euid, egid;
3794329Seric 	int bits;
3804329Seric 
3814329Seric 	if (bitset(0111, s->st_mode))
3824329Seric 		return (FALSE);
3834329Seric 	euid = getruid();
3844329Seric 	egid = getrgid();
3854329Seric 	if (geteuid() == 0)
3864329Seric 	{
3874329Seric 		if (bitset(S_ISUID, s->st_mode))
3884329Seric 			euid = s->st_uid;
3894329Seric 		if (bitset(S_ISGID, s->st_mode))
3904329Seric 			egid = s->st_gid;
3914329Seric 	}
3924329Seric 
3934329Seric 	if (euid == 0)
3944329Seric 		return (TRUE);
3954329Seric 	bits = S_IWRITE;
3964329Seric 	if (euid != s->st_uid)
3974329Seric 	{
3984329Seric 		bits >>= 3;
3994329Seric 		if (egid != s->st_gid)
4004329Seric 			bits >>= 3;
4014329Seric 	}
4024329Seric 	return ((s->st_mode & bits) != 0);
4034329Seric }
4044329Seric /*
4054174Seric **  INCLUDE -- handle :include: specification.
4064174Seric **
4074174Seric **	Parameters:
4084174Seric **		fname -- filename to include.
4094176Seric **		msg -- message to print in verbose mode.
4104399Seric **		ctladdr -- address template to use to fill in these
4114399Seric **			addresses -- effective user/group id are
4124399Seric **			the important things.
4135006Seric **		sendq -- a pointer to the head of the send queue
4145006Seric **			to put these addresses in.
4154174Seric **
4164174Seric **	Returns:
4174174Seric **		none.
4184174Seric **
4194174Seric **	Side Effects:
4204174Seric **		reads the :include: file and sends to everyone
4214174Seric **		listed in that file.
4224174Seric */
4234174Seric 
4245006Seric include(fname, msg, ctladdr, sendq)
4254174Seric 	char *fname;
4264176Seric 	char *msg;
4274399Seric 	ADDRESS *ctladdr;
4285006Seric 	ADDRESS **sendq;
4294174Seric {
4304174Seric 	char buf[MAXLINE];
4314174Seric 	register FILE *fp;
4326906Seric 	char *oldto = CurEnv->e_to;
4334174Seric 
4344174Seric 	fp = fopen(fname, "r");
4354174Seric 	if (fp == NULL)
4364174Seric 	{
4374174Seric 		usrerr("Cannot open %s", fname);
4384174Seric 		return;
4394174Seric 	}
4404406Seric 	if (getctladdr(ctladdr) == NULL)
4414406Seric 	{
4424406Seric 		struct stat st;
4434174Seric 
4444406Seric 		if (fstat(fileno(fp), &st) < 0)
4454406Seric 			syserr("Cannot fstat %s!", fname);
4464406Seric 		ctladdr->q_uid = st.st_uid;
4474406Seric 		ctladdr->q_gid = st.st_gid;
4484406Seric 		ctladdr->q_flags |= QGOODUID;
4494406Seric 	}
4504406Seric 
4514174Seric 	/* read the file -- each line is a comma-separated list. */
4524174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
4534174Seric 	{
4544174Seric 		register char *p = index(buf, '\n');
4554174Seric 
4564174Seric 		if (p != NULL)
4574174Seric 			*p = '\0';
4584174Seric 		if (buf[0] == '\0')
4594174Seric 			continue;
4606906Seric 		CurEnv->e_to = oldto;
4617054Seric 		message(Arpa_Info, "%s to %s", msg, buf);
4624176Seric 		AliasLevel++;
4638081Seric 		sendto(buf, ctladdr, sendq);
4644176Seric 		AliasLevel--;
4654174Seric 	}
4664174Seric 
4674319Seric 	(void) fclose(fp);
4684174Seric }
4694324Seric /*
4704324Seric **  SENDTOARGV -- send to an argument vector.
4714324Seric **
4724324Seric **	Parameters:
4734324Seric **		argv -- argument vector to send to.
4744324Seric **
4754324Seric **	Returns:
4764324Seric **		none.
4774324Seric **
4784324Seric **	Side Effects:
4794324Seric **		puts all addresses on the argument vector onto the
4804324Seric **			send queue.
4814324Seric */
4824324Seric 
4834324Seric sendtoargv(argv)
4844324Seric 	register char **argv;
4854324Seric {
4864324Seric 	register char *p;
4874324Seric 	extern bool sameword();
4884324Seric 
4894324Seric 	while ((p = *argv++) != NULL)
4904324Seric 	{
4914324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4924324Seric 		{
4934324Seric 			char nbuf[MAXNAME];
4944324Seric 
4954324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
4964324Seric 				usrerr("address overflow");
4974324Seric 			else
4984324Seric 			{
4994324Seric 				(void) strcpy(nbuf, p);
5004324Seric 				(void) strcat(nbuf, "@");
5014324Seric 				(void) strcat(nbuf, argv[1]);
5024324Seric 				p = newstr(nbuf);
5034324Seric 				argv += 2;
5044324Seric 			}
5054324Seric 		}
5068081Seric 		sendto(p, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
5074324Seric 	}
5084324Seric }
5094399Seric /*
5104399Seric **  GETCTLADDR -- get controlling address from an address header.
5114399Seric **
5124399Seric **	If none, get one corresponding to the effective userid.
5134399Seric **
5144399Seric **	Parameters:
5154399Seric **		a -- the address to find the controller of.
5164399Seric **
5174399Seric **	Returns:
5184399Seric **		the controlling address.
5194399Seric **
5204399Seric **	Side Effects:
5214399Seric **		none.
5224399Seric */
5234399Seric 
5244399Seric ADDRESS *
5254399Seric getctladdr(a)
5264399Seric 	register ADDRESS *a;
5274399Seric {
5284404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5294399Seric 		a = a->q_alias;
5304399Seric 	return (a);
5314399Seric }
532