1294Seric # include <stdio.h>
2294Seric # include <pwd.h>
3294Seric # include "dlvrmail.h"
4294Seric # include <whoami.h>
5294Seric 
6294Seric /*
7294Seric **  CONF.C -- Delivermail Configuration Tables.
8294Seric **
9294Seric **	Defines the configuration of this installation.
10294Seric **
11294Seric **	The first table describes available mailers.  This is
12294Seric **	just a list of argument vectors, with the following
13294Seric **	codes embedded:
14294Seric **		$u -- insert the user name.
15294Seric **		$h -- insert the host name.
16294Seric **		$f -- insert the from person name.
17294Seric **		$c -- insert the hop count.
18294Seric **	This stuff is interpreted in buildmail.  There are two
19294Seric **	important conventions here: entry zero must be the
20294Seric **	local mailer & entry one must be the shell.
21294Seric **
22294Seric **	The second table gives a list of special characters.  This
23294Seric **	table is scanned linearly by parse() until an entry is
24294Seric **	found using one of the magic characters.  Other fields
25294Seric **	give more information on how to handle it.
26294Seric **
27294Seric **	Defined Constants:
28294Seric **		M_* -- indices into Mailer, used only in this module.
29294Seric **
30294Seric **	Defines:
31294Seric **		Mailer -- the mailer descriptor table.
32294Seric **		ParseTab -- the parse table.
33294Seric **
34294Seric **	Notes:
35294Seric **		Ingres 11/70 version.
36294Seric **
37294Seric **	History:
38294Seric **		3/5/80 -- Generalized to use <whoami.h>.
39294Seric **		12/26/79 -- written for Ingres 11/70.
40294Seric */
41294Seric 
42294Seric 
43294Seric 
44294Seric 
45294Seric 
46294Seric # ifdef ING70
47294Seric static char	*BerkLocal[] = { "i", "ingres", "ing70", NULL };
48294Seric char		*MyLocNam = "Ing70";
49294Seric # define HASARPA
50294Seric # define V6
51294Seric # endif ING70
52294Seric 
53294Seric # ifdef INGVAX
54294Seric /* untested */
55294Seric static char	*BerkLocal[] = { "j", "ingvax", NULL };
56294Seric char		*MyLocNam = "IngVax";
57294Seric # endif INGVAX
58294Seric 
59294Seric # ifdef CSVAX
60294Seric /* untested */
61294Seric static char	*BerkLocal[] = { "v", "csvax", "vax", NULL };
62294Seric char		*MyLocNam = "CSVax";
63294Seric # define HASUUCP
64294Seric # define NETV6MAIL
65294Seric # endif CSVAX
66294Seric 
67294Seric # ifdef CORY
68294Seric /* untested */
69294Seric static char	*BerkLocal[] = { "y", "cory", NULL };
70294Seric char		*MyLocNam = "Cory";
71294Seric # endif CORY
72294Seric 
73294Seric # ifdef IMAGE
74294Seric /* untested */
75294Seric static char	*BerkLocal[] = { "m", "image", NULL };
76294Seric char		*MyLocNam = "Image";
77294Seric # define V6
78294Seric # endif IMAGE
79294Seric 
80294Seric # ifdef ESVAX
81294Seric /* untested */
82294Seric static char	*BerkLocal[] = { "o", "esvax", NULL };
83294Seric char		*MyLocNam = "ESVax";
84294Seric # endif ESVAX
85294Seric 
86294Seric # ifdef EECS40
87294Seric /* untested */
88294Seric static char	*BerkLocal[] = { "z", "eecs40", NULL };
89294Seric char		*MyLocNam = "EECS40";
90294Seric # define V6
91294Seric # endif EECS40
92294Seric 
93294Seric struct mailer Mailer[] =
94294Seric {
95294Seric 	/* local mail -- must be #0 */
96294Seric 	{
97294Seric # ifdef NETV6MAIL
98294Seric 		"/usr/net/bin/v6mail",
99294Seric # else
100294Seric 		"/bin/mail",
101294Seric # endif
102294Seric 		M_ROPT|M_NOHOST|M_STRIPQ,	EX_NOUSER,	NULL,
103294Seric 		{ "...local%mail", "-d", "$u", NULL }
104294Seric 	},
105294Seric 	/* pipes through programs -- must be #1 */
106294Seric 	{
107294Seric 		"/bin/csh",
108294Seric 		M_HDR|M_NOHOST,			EX_UNAVAIL,	NULL,
109294Seric 		{ "...prog%mail", "-fc", "$u", NULL }
110294Seric 	},
111294Seric 	/* local berkeley mail */
112294Seric 	{
113294Seric 		"/usr/net/bin/sendberkmail",
114*310Seric 		M_FOPT|M_HDR|M_STRIPQ,		EX_UNAVAIL,	BerkLocal,
115294Seric 		{ "...berk%mail", "-m", "$h", "-t", "$u", "-h", "$c", NULL }
116294Seric 	},
117294Seric 	/* arpanet mail */
118294Seric 	{
119294Seric 		"/usr/lib/mailers/arpa",
120*310Seric 		M_STRIPQ,			0,		NULL,
121294Seric 		{ "...arpa%mail", "$f", "$h", "$u", NULL }
122294Seric 	},
123294Seric 	/* uucp mail (cheat & use Bell's v7 mail) */
124294Seric 	{
125294Seric # ifdef UCKMAIL
126294Seric 		"/bin/badmail",
127294Seric # else
128294Seric 		"/bin/mail",
129294Seric # endif
130294Seric 		M_ROPT|M_NOHOST|M_STRIPQ,	EX_NOUSER,	NULL,
131294Seric # ifdef DUMBMAIL
132294Seric 		{ "...uucp%mail", "$h!$u", NULL }
133294Seric # else
134294Seric 		{ "...uucp%mail", "-d", "$h!$u", NULL }
135294Seric # endif DUMBMAIL
136294Seric 	},
137294Seric };
138294Seric 
139294Seric # define M_LOCAL	0
140294Seric # define M_BERK		2
141294Seric # define M_ARPA		3
142294Seric # define M_UUCP		4
143294Seric 
144294Seric 
145294Seric 
146294Seric struct parsetab ParseTab[] =
147294Seric {
148294Seric 	':',	M_BERK,		P_ONE,				NULL,
149294Seric # ifdef HASARPA
150294Seric 	'@',	M_ARPA,		P_HLAST|P_USR_UPPER,		NULL,
151294Seric # else
152294Seric 	'@',	M_BERK,		P_HLAST|P_USR_UPPER|P_MOVE,	"ing70",
153294Seric # endif HASARPA
154294Seric 	'^',	-1,		P_MAP,				"!",
155294Seric # ifdef HASUUCP
156294Seric 	'!',	M_UUCP,		0,				NULL,
157294Seric # else
158294Seric 	'!',	M_BERK,		P_MOVE,				"csvax",
159294Seric # endif HASUUCP
160294Seric 	'.',	-1,		P_MAP|P_ONE,			":",
161294Seric 	'\0',	M_LOCAL,	P_MOVE,				"",
162294Seric };
163294Seric /*
164294Seric **  GETNAME -- Get the current users login name.
165294Seric **
166294Seric **	This is in config.c because it is somewhat machine dependent.
167294Seric **	Examine it carefully for your installation.
168294Seric **
169294Seric **	Algorithm:
170294Seric **		See if the person is logged in.  If so, return
171294Seric **			the name s/he is logged in as.
172294Seric **		Look up the user id in /etc/passwd.  If found,
173294Seric **			return that name.
174294Seric **		Return NULL.
175294Seric **
176294Seric **	Parameters:
177294Seric **		none
178294Seric **
179294Seric **	Returns:
180294Seric **		The login name of this user.
181294Seric **		NULL if this person is noone.
182294Seric **
183294Seric **	Side Effects:
184294Seric **		none
185294Seric **
186294Seric **	Requires:
187294Seric **		getlogin (sys)
188294Seric **		getpwuid (sys)
189294Seric **		getuid (sys)
190294Seric **
191294Seric **	Called By:
192294Seric **		main
193294Seric **
194294Seric **	History:
195294Seric **		12/26/79 -- written.
196294Seric */
197294Seric 
198294Seric char *
199294Seric getname()
200294Seric {
201294Seric 	register char *p;
202294Seric 	register struct passwd *w;
203294Seric 	extern char *getlogin();
204294Seric 	extern struct passwd *getpwuid();
205294Seric 	static char namebuf[9];
206294Seric 
207294Seric 	p = getlogin();
208294Seric 	if (p != NULL && p[0] != '\0')
209294Seric 		return (p);
210294Seric # ifdef V6
211294Seric 	w = getpwuid(getuid() & 0377);
212294Seric # else
213294Seric 	w = getpwuid(getuid());
214294Seric # endif V6
215294Seric 	if (w != NULL)
216294Seric 	{
217294Seric 		strcpy(namebuf, w->pw_name);
218294Seric 		return (namebuf);
219294Seric 	}
220294Seric 	return (NULL);
221294Seric }
222294Seric 
223294Seric # ifdef V6
224294Seric /*
225294Seric **  TTYPATH -- Get the path of the user's tty -- Version 6 version.
226294Seric **
227294Seric **	Returns the pathname of the user's tty.  Returns NULL if
228294Seric **	the user is not logged in or if s/he has write permission
229294Seric **	denied.
230294Seric **
231294Seric **	Parameters:
232294Seric **		none
233294Seric **
234294Seric **	Returns:
235294Seric **		pathname of the user's tty.
236294Seric **		NULL if not logged in or write permission denied.
237294Seric **
238294Seric **	Side Effects:
239294Seric **		none.
240294Seric **
241294Seric **	WARNING:
242294Seric **		Return value is in a local buffer.
243294Seric **
244294Seric **	Requires:
245294Seric **		stat (sys)
246294Seric **		ttyn (sys)
247294Seric **		open (sys)
248294Seric **		read (sys)
249294Seric **		close (sys)
250294Seric **		seek (sys)
251294Seric **
252294Seric **	Called By:
253294Seric **		savemail
254294Seric **
255294Seric **	History:
256294Seric **		1/12/80 -- written.
257294Seric */
258294Seric 
259294Seric # include <sys/types.h>
260294Seric # include <sys/stat.h>
261294Seric 
262294Seric char *
263294Seric ttypath()
264294Seric {
265294Seric 	struct stat stbuf;
266294Seric 	register int i;
267294Seric 	static char pathn[] = "/dev/ttyx";
268294Seric 	extern int errno;
269294Seric 
270294Seric 	/* compute the pathname of the controlling tty */
271294Seric 	if ((i = ttyn(2)) == 'x' && (i = ttyn(1)) == 'x' && (i = ttyn(0)) == 'x')
272294Seric 	{
273294Seric 		errno = 0;
274294Seric 		return (NULL);
275294Seric 	}
276294Seric 	pathn[8] = i;
277294Seric 
278294Seric 	/* see if we have write permission */
279294Seric 	if (stat(pathn, &stbuf) < 0 || !flagset(02, stbuf.st_mode))
280294Seric 	{
281294Seric 		errno = 0;
282294Seric 		return (NULL);
283294Seric 	}
284294Seric 
285294Seric 	/* see if the user is logged in */
286294Seric 	if (getlogin() == NULL)
287294Seric 		return (NULL);
288294Seric 
289294Seric 	/* looks good */
290294Seric 	return (pathn);
291294Seric }
292294Seric /*
293294Seric **  FDOPEN -- Open a stdio file given an open file descriptor.
294294Seric **
295294Seric **	This is included here because it is standard in v7, but we
296294Seric **	need it in v6.
297294Seric **
298294Seric **	Algorithm:
299294Seric **		Open /dev/null to create a descriptor.
300294Seric **		Close that descriptor.
301294Seric **		Copy the existing fd into the descriptor.
302294Seric **
303294Seric **	Parameters:
304294Seric **		fd -- the open file descriptor.
305294Seric **		type -- "r", "w", or whatever.
306294Seric **
307294Seric **	Returns:
308294Seric **		The file descriptor it creates.
309294Seric **
310294Seric **	Side Effects:
311294Seric **		none
312294Seric **
313294Seric **	Requires:
314294Seric **		fopen (sys)
315294Seric **
316294Seric **	Called By:
317294Seric **		deliver
318294Seric **
319294Seric **	Notes:
320294Seric **		The mode of fd must match "type".
321294Seric */
322294Seric 
323294Seric FILE *
324294Seric fdopen(fd, type)
325294Seric 	int fd;
326294Seric 	char *type;
327294Seric {
328294Seric 	register FILE *f;
329294Seric 
330294Seric 	f = fopen("/dev/null", type);
331294Seric 	close(fileno(f));
332294Seric 	fileno(f) = fd;
333294Seric 	return (f);
334294Seric }
335294Seric /*
336294Seric **  INDEX -- Return pointer to character in string
337294Seric **
338294Seric **	For V7 compatibility.
339294Seric **
340294Seric **	Parameters:
341294Seric **		s -- a string to scan.
342294Seric **		c -- a character to look for.
343294Seric **
344294Seric **	Returns:
345294Seric **		If c is in s, returns the address of the first
346294Seric **			instance of c in s.
347294Seric **		NULL if c is not in s.
348294Seric **
349294Seric **	Side Effects:
350294Seric **		none.
351294Seric **
352294Seric **	Requires:
353294Seric **		none.
354294Seric **
355294Seric **	History:
356294Seric **		3/14/80 -- written.  Why isn't this in -lS?
357294Seric */
358294Seric 
359294Seric index(s, c)
360294Seric 	register char *s;
361294Seric 	register char c;
362294Seric {
363294Seric 	while (*s != '\0')
364294Seric 	{
365294Seric 		if (*s++ == c)
366294Seric 			return (--s);
367294Seric 	}
368294Seric 	return (NULL);
369294Seric }
370294Seric # endif V6
371294Seric 
372294Seric # ifndef V6
373294Seric /*
374294Seric **  TTYPATH -- Get the path of the user's tty -- Version 7 version.
375294Seric **
376294Seric **	Returns the pathname of the user's tty.  Returns NULL if
377294Seric **	the user is not logged in or if s/he has write permission
378294Seric **	denied.
379294Seric **
380294Seric **	Parameters:
381294Seric **		none
382294Seric **
383294Seric **	Returns:
384294Seric **		pathname of the user's tty.
385294Seric **		NULL if not logged in or write permission denied.
386294Seric **
387294Seric **	Side Effects:
388294Seric **		none.
389294Seric **
390294Seric **	WARNING:
391294Seric **		Return value is in a local buffer.
392294Seric **
393294Seric **	Requires:
394294Seric **		stat (sys)
395294Seric **		ttyn (sys)
396294Seric **		open (sys)
397294Seric **		read (sys)
398294Seric **		close (sys)
399294Seric **		seek (sys)
400294Seric **
401294Seric **	Called By:
402294Seric **		savemail
403294Seric **
404294Seric **	History:
405294Seric **		1/12/80 -- written.
406294Seric */
407294Seric 
408294Seric # include <sys/types.h>
409294Seric # include <sys/stat.h>
410294Seric 
411294Seric char *
412294Seric ttypath()
413294Seric {
414294Seric 	struct stat stbuf;
415294Seric 	register char *pathn;
416294Seric 	extern int errno;
417294Seric 	extern char *ttyname();
418294Seric 
419294Seric 	/* compute the pathname of the controlling tty */
420294Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && (pathn = ttyname(0)) == NULL)
421294Seric 	{
422294Seric 		errno = 0;
423294Seric 		return (NULL);
424294Seric 	}
425294Seric 
426294Seric 	/* see if we have write permission */
427294Seric 	if (stat(pathn, &stbuf) < 0 || !flagset(02, stbuf.st_mode))
428294Seric 	{
429294Seric 		errno = 0;
430294Seric 		return (NULL);
431294Seric 	}
432294Seric 
433294Seric 	/* see if the user is logged in */
434294Seric 	if (getlogin() == NULL)
435294Seric 		return (NULL);
436294Seric 
437294Seric 	/* looks good */
438294Seric 	return (pathn);
439294Seric }
440294Seric # endif V6
441