14174Seric # include <pwd.h>
24627Seric # include "sendmail.h"
34329Seric # include <sys/stat.h>
44174Seric 
5*4998Seric static char SccsId[] = "@(#)recipient.c	3.29	11/21/81";
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.
154174Seric **		copyf -- the copy flag; passed to parse.
164399Seric **		ctladdr -- the address template for the person to
174399Seric **			send to -- effective uid/gid are important.
184174Seric **
194174Seric **	Returns:
20*4998Seric **		none
214174Seric **
224174Seric **	Side Effects:
234174Seric **		none.
244174Seric */
254174Seric 
264174Seric # define MAXRCRSN	10
274174Seric 
28*4998Seric sendto(list, copyf, ctladdr)
294174Seric 	char *list;
304174Seric 	int copyf;
314399Seric 	ADDRESS *ctladdr;
324174Seric {
334174Seric 	register char *p;
344319Seric 	bool more;		/* set if more addresses to send to */
354324Seric 	ADDRESS *al;		/* list of addresses to send to */
364423Seric 	bool firstone;		/* set on first address sent */
374444Seric 	bool selfref;		/* set if this list includes ctladdr */
384174Seric 
394324Seric # ifdef DEBUG
404324Seric 	if (Debug > 1)
414444Seric 	{
424444Seric 		printf("sendto: %s\n   ctladdr=", list);
434444Seric 		printaddr(ctladdr, FALSE);
444444Seric 	}
454324Seric # endif DEBUG
464324Seric 
474174Seric 	more = TRUE;
484423Seric 	firstone = TRUE;
494444Seric 	selfref = FALSE;
504324Seric 	al = NULL;
514174Seric 	for (p = list; more; )
524174Seric 	{
534319Seric 		register char *q;
544319Seric 		register char c;
554319Seric 		ADDRESS *a;
564319Seric 
574174Seric 		/* find the end of this address */
584174Seric 		while (*p == ' ' || *p == '\t')
594174Seric 			p++;
604174Seric 		q = p;
614174Seric 		while ((c = *p++) != '\0' && c != ',' && c != '\n')
624174Seric 			continue;
634174Seric 		more = c != '\0';
644174Seric 		*--p = '\0';
654174Seric 		if (more)
664174Seric 			p++;
674324Seric 		if (*q == '\0')
684324Seric 			continue;
694174Seric 
704174Seric 		/* parse the address */
714174Seric 		if ((a = parse(q, (ADDRESS *) NULL, copyf)) == NULL)
724174Seric 			continue;
734324Seric 		a->q_next = al;
744399Seric 		a->q_alias = ctladdr;
754444Seric 
764444Seric 		/* see if this should be marked as a primary address */
774423Seric 		if (ctladdr == NULL ||
784423Seric 		    (firstone && !more && bitset(QPRIMARY, ctladdr->q_flags)))
794423Seric 			a->q_flags |= QPRIMARY;
804444Seric 
814444Seric 		/* put on send queue or suppress self-reference */
824444Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a, FALSE))
834444Seric 			selfref = TRUE;
844444Seric 		else
854444Seric 			al = a;
864423Seric 		firstone = FALSE;
874324Seric 	}
884324Seric 
894444Seric 	/* if this alias doesn't include itself, delete ctladdr */
904444Seric 	if (!selfref && ctladdr != NULL)
914444Seric 		ctladdr->q_flags |= QDONTSEND;
924444Seric 
934324Seric 	/* arrange to send to everyone on the local send list */
944324Seric 	while (al != NULL)
954324Seric 	{
964324Seric 		register ADDRESS *a = al;
974324Seric 
984324Seric 		al = a->q_next;
99*4998Seric 		recipient(a);
1004993Seric 
101*4998Seric 		/* arrange to inherit full name */
102*4998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
103*4998Seric 			a->q_fullname = ctladdr->q_fullname;
1044174Seric 	}
1054324Seric 
1064174Seric 	To = NULL;
1074174Seric }
1084174Seric /*
1094174Seric **  RECIPIENT -- Designate a message recipient
1104174Seric **
1114174Seric **	Saves the named person for future mailing.
1124174Seric **
1134174Seric **	Parameters:
1144174Seric **		a -- the (preparsed) address header for the recipient.
1154174Seric **
1164174Seric **	Returns:
117*4998Seric **		none.
1184174Seric **
1194174Seric **	Side Effects:
1204174Seric **		none.
1214174Seric */
1224174Seric 
1234174Seric recipient(a)
1244174Seric 	register ADDRESS *a;
1254174Seric {
1264174Seric 	register ADDRESS *q;
1274319Seric 	ADDRESS **pq;
1284174Seric 	register struct mailer *m;
1294399Seric 	extern ADDRESS *getctladdr();
1304627Seric 	extern bool safefile();
1314174Seric 
1324174Seric 	To = a->q_paddr;
1334600Seric 	m = a->q_mailer;
1344174Seric 	errno = 0;
1354174Seric # ifdef DEBUG
1364174Seric 	if (Debug)
1374444Seric 	{
1384444Seric 		printf("\nrecipient: ");
1394444Seric 		printaddr(a, FALSE);
1404444Seric 	}
1414174Seric # endif DEBUG
1424174Seric 
1434174Seric 	/* break aliasing loops */
1444174Seric 	if (AliasLevel > MAXRCRSN)
1454174Seric 	{
1464174Seric 		usrerr("aliasing/forwarding loop broken");
147*4998Seric 		return;
1484174Seric 	}
1494174Seric 
1504174Seric 	/*
1514627Seric 	**  Finish setting up address structure.
1524174Seric 	*/
1534174Seric 
1544627Seric 	a->q_timeout = TimeOut;
1554627Seric 
1564627Seric 	/* do sickly crude mapping for program mailing, etc. */
1574600Seric 	if (a->q_mailer == LocalMailer)
1584174Seric 	{
1594174Seric 		if (a->q_user[0] == '|')
1604174Seric 		{
1614600Seric 			a->q_mailer = m = ProgMailer;
1624174Seric 			a->q_user++;
1634627Seric 			if (a->q_alias == NULL && Debug == 0 && !QueueRun)
1644217Seric 			{
1654217Seric 				usrerr("Cannot mail directly to programs");
1664217Seric 				a->q_flags |= QDONTSEND;
1674217Seric 			}
1684174Seric 		}
1694174Seric 	}
1704174Seric 
1714174Seric 	/*
1724419Seric 	**  Look up this person in the recipient list.
1734419Seric 	**	If they are there already, return, otherwise continue.
1744419Seric 	**	If the list is empty, just add it.  Notice the cute
1754419Seric 	**	hack to make from addresses suppress things correctly:
1764419Seric 	**	the QDONTSEND bit will be set in the send list.
1774419Seric 	**	[Please note: the emphasis is on "hack."]
1784174Seric 	*/
1794174Seric 
1804319Seric 	for (pq = &m->m_sendq; (q = *pq) != NULL; pq = &q->q_next)
1814174Seric 	{
1824319Seric 		if (!ForceMail && sameaddr(q, a, FALSE))
1834174Seric 		{
1844174Seric # ifdef DEBUG
1854319Seric 			if (Debug)
1864444Seric 			{
1874444Seric 				printf("%s in sendq: ", a->q_paddr);
1884444Seric 				printaddr(q, FALSE);
1894444Seric 			}
1904174Seric # endif DEBUG
191*4998Seric 			if (Verbose && !bitset(QDONTSEND, a->q_flags))
1924324Seric 				message(Arpa_Info, "duplicate suppressed");
1934423Seric 			if (!bitset(QPRIMARY, q->q_flags))
1944423Seric 				q->q_flags |= a->q_flags;
195*4998Seric 			return;
1964174Seric 		}
1974319Seric 	}
1984174Seric 
1994319Seric 	/* add address on list */
2004319Seric 	*pq = a;
2014174Seric 	a->q_next = NULL;
2024247Seric 	if (DontSend)
2034247Seric 		a->q_flags |= QDONTSEND;
2044174Seric 
2054174Seric 	/*
2064174Seric 	**  Alias the name and handle :include: specs.
2074174Seric 	*/
2084174Seric 
2094600Seric 	if (a->q_mailer == LocalMailer)
2104174Seric 	{
2114174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2124174Seric 		{
2134174Seric 			a->q_flags |= QDONTSEND;
2144627Seric 			if (a->q_alias == NULL && Debug == 0 && !QueueRun)
2154399Seric 				usrerr("Cannot mail directly to :include:s");
2164399Seric 			else
2174399Seric 			{
2184399Seric 				if (Verbose)
2194399Seric 					message(Arpa_Info, "including file %s", &a->q_user[9]);
2204399Seric 				include(&a->q_user[9], " sending", a);
2214399Seric 			}
2224174Seric 		}
2234174Seric 		else
2244174Seric 			alias(a);
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 
2434174Seric 		strcpy(buf, a->q_user);
2444399Seric 		for (p = buf; *p != '\0' && !quoted; p++)
2454399Seric 		{
246*4998Seric 			if (!isascii(*p) && (*p & 0377) != (SPACESUB & 0377))
2474399Seric 				quoted = TRUE;
2484399Seric 		}
2494174Seric 		stripquotes(buf, TRUE);
2504174Seric 
2514174Seric 		/* see if this is to a file */
2524201Seric 		if ((p = rindex(buf, '/')) != NULL)
2534174Seric 		{
2544201Seric 			/* check if writable or creatable */
2554627Seric 			if (a->q_alias == NULL && Debug == 0 && !QueueRun)
2564399Seric 			{
2574399Seric 				usrerr("Cannot mail directly to files");
2584399Seric 				a->q_flags |= QDONTSEND;
2594399Seric 			}
2604399Seric 			else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
2614539Seric 			    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
2624174Seric 			{
2634174Seric 				a->q_flags |= QBADADDR;
2644174Seric 				giveresponse(EX_CANTCREAT, TRUE, m);
2654174Seric 			}
2664174Seric 		}
2674174Seric 		else
2684174Seric 		{
2694174Seric 			register struct passwd *pw;
2704373Seric 			extern struct passwd *finduser();
2714373Seric 
2724407Seric 			/* warning -- finduser may trash buf */
2734373Seric 			pw = finduser(buf);
2744174Seric 			if (pw == NULL)
2754174Seric 			{
2764174Seric 				a->q_flags |= QBADADDR;
2774174Seric 				giveresponse(EX_NOUSER, TRUE, m);
2784174Seric 			}
2794174Seric 			else
2804174Seric 			{
2814993Seric 				char nbuf[MAXNAME];
2824993Seric 
2834376Seric 				if (strcmp(a->q_user, pw->pw_name) != 0)
2844376Seric 				{
2854376Seric 					a->q_user = newstr(pw->pw_name);
2864376Seric 					strcpy(buf, pw->pw_name);
2874376Seric 				}
2884174Seric 				a->q_home = newstr(pw->pw_dir);
2894213Seric 				a->q_uid = pw->pw_uid;
2904399Seric 				a->q_gid = pw->pw_gid;
2914404Seric 				a->q_flags |= QGOODUID;
292*4998Seric 				buildfname(pw->pw_gecos, pw->pw_name, nbuf);
2934993Seric 				if (nbuf[0] != '\0')
2944993Seric 					a->q_fullname = newstr(nbuf);
2954399Seric 				if (!quoted)
2964174Seric 					forward(a);
2974174Seric 			}
2984174Seric 		}
2994174Seric 	}
3004174Seric }
3014174Seric /*
3024373Seric **  FINDUSER -- find the password entry for a user.
3034373Seric **
3044373Seric **	This looks a lot like getpwnam, except that it may want to
3054373Seric **	do some fancier pattern matching in /etc/passwd.
3064373Seric **
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 	{
3324407Seric 		if (*p == (SPACESUB & 0177) || *p == '_')
3334407Seric 			*p = ' ';
3344407Seric 	}
3354407Seric 
3364376Seric 	setpwent();
3374376Seric 	while ((pw = getpwent()) != NULL)
3384376Seric 	{
339*4998Seric 		char buf[MAXNAME];
3404993Seric 		extern bool sameword();
3414376Seric 
3424376Seric 		if (strcmp(pw->pw_name, name) == 0)
3434376Seric 			return (pw);
344*4998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
3454407Seric 		if (index(buf, ' ') != NULL && sameword(buf, name))
3464381Seric 		{
3474377Seric 			if (Verbose)
348*4998Seric 				message(Arpa_Info, "sending to login name %s",
349*4998Seric 				    pw->pw_name);
3504376Seric 			return (pw);
3514377Seric 		}
3524376Seric 	}
3534376Seric 	return (NULL);
3544373Seric }
3554373Seric /*
3564329Seric **  WRITABLE -- predicate returning if the file is writable.
3574329Seric **
3584329Seric **	This routine must duplicate the algorithm in sys/fio.c.
3594329Seric **	Unfortunately, we cannot use the access call since we
3604329Seric **	won't necessarily be the real uid when we try to
3614329Seric **	actually open the file.
3624329Seric **
3634329Seric **	Notice that ANY file with ANY execute bit is automatically
3644329Seric **	not writable.  This is also enforced by mailfile.
3654329Seric **
3664329Seric **	Parameters:
3674329Seric **		s -- pointer to a stat struct for the file.
3684329Seric **
3694329Seric **	Returns:
3704329Seric **		TRUE -- if we will be able to write this file.
3714329Seric **		FALSE -- if we cannot write this file.
3724329Seric **
3734329Seric **	Side Effects:
3744329Seric **		none.
3754329Seric */
3764329Seric 
3774329Seric bool
3784329Seric writable(s)
3794329Seric 	register struct stat *s;
3804329Seric {
3814329Seric 	int euid, egid;
3824329Seric 	int bits;
3834329Seric 
3844329Seric 	if (bitset(0111, s->st_mode))
3854329Seric 		return (FALSE);
3864329Seric 	euid = getruid();
3874329Seric 	egid = getrgid();
3884329Seric 	if (geteuid() == 0)
3894329Seric 	{
3904329Seric 		if (bitset(S_ISUID, s->st_mode))
3914329Seric 			euid = s->st_uid;
3924329Seric 		if (bitset(S_ISGID, s->st_mode))
3934329Seric 			egid = s->st_gid;
3944329Seric 	}
3954329Seric 
3964329Seric 	if (euid == 0)
3974329Seric 		return (TRUE);
3984329Seric 	bits = S_IWRITE;
3994329Seric 	if (euid != s->st_uid)
4004329Seric 	{
4014329Seric 		bits >>= 3;
4024329Seric 		if (egid != s->st_gid)
4034329Seric 			bits >>= 3;
4044329Seric 	}
4054329Seric 	return ((s->st_mode & bits) != 0);
4064329Seric }
4074329Seric /*
4084174Seric **  INCLUDE -- handle :include: specification.
4094174Seric **
4104174Seric **	Parameters:
4114174Seric **		fname -- filename to include.
4124176Seric **		msg -- message to print in verbose mode.
4134399Seric **		ctladdr -- address template to use to fill in these
4144399Seric **			addresses -- effective user/group id are
4154399Seric **			the important things.
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 
4254399Seric include(fname, msg, ctladdr)
4264174Seric 	char *fname;
4274176Seric 	char *msg;
4284399Seric 	ADDRESS *ctladdr;
4294174Seric {
4304174Seric 	char buf[MAXLINE];
4314174Seric 	register FILE *fp;
4324178Seric 	char *oldto = 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;
4604178Seric 		To = oldto;
4614174Seric 		if (Verbose)
4624176Seric 			message(Arpa_Info, "%s to %s", msg, buf);
4634176Seric 		AliasLevel++;
464*4998Seric 		sendto(buf, 1, ctladdr);
4654176Seric 		AliasLevel--;
4664174Seric 	}
4674174Seric 
4684319Seric 	(void) fclose(fp);
4694174Seric }
4704324Seric /*
4714324Seric **  SENDTOARGV -- send to an argument vector.
4724324Seric **
4734324Seric **	Parameters:
4744324Seric **		argv -- argument vector to send to.
4754324Seric **
4764324Seric **	Returns:
4774324Seric **		none.
4784324Seric **
4794324Seric **	Side Effects:
4804324Seric **		puts all addresses on the argument vector onto the
4814324Seric **			send queue.
4824324Seric */
4834324Seric 
4844324Seric sendtoargv(argv)
4854324Seric 	register char **argv;
4864324Seric {
4874324Seric 	register char *p;
4884324Seric 	extern bool sameword();
4894324Seric 
4904324Seric 	while ((p = *argv++) != NULL)
4914324Seric 	{
4924324Seric 		if (argv[0] != NULL && argv[1] != NULL && sameword(argv[0], "at"))
4934324Seric 		{
4944324Seric 			char nbuf[MAXNAME];
4954324Seric 
4964324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
4974324Seric 				usrerr("address overflow");
4984324Seric 			else
4994324Seric 			{
5004324Seric 				(void) strcpy(nbuf, p);
5014324Seric 				(void) strcat(nbuf, "@");
5024324Seric 				(void) strcat(nbuf, argv[1]);
5034324Seric 				p = newstr(nbuf);
5044324Seric 				argv += 2;
5054324Seric 			}
5064324Seric 		}
507*4998Seric 		sendto(p, 0, (ADDRESS *) NULL);
5084324Seric 	}
5094324Seric }
5104399Seric /*
5114399Seric **  GETCTLADDR -- get controlling address from an address header.
5124399Seric **
5134399Seric **	If none, get one corresponding to the effective userid.
5144399Seric **
5154399Seric **	Parameters:
5164399Seric **		a -- the address to find the controller of.
5174399Seric **
5184399Seric **	Returns:
5194399Seric **		the controlling address.
5204399Seric **
5214399Seric **	Side Effects:
5224399Seric **		none.
5234399Seric */
5244399Seric 
5254399Seric ADDRESS *
5264399Seric getctladdr(a)
5274399Seric 	register ADDRESS *a;
5284399Seric {
5294404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
5304399Seric 		a = a->q_alias;
5314399Seric 	return (a);
5324399Seric }
533