xref: /csrg-svn/usr.sbin/sendmail/src/alias.c (revision 64718)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 # include "sendmail.h"
10 # include <pwd.h>
11 
12 #ifndef lint
13 static char sccsid[] = "@(#)alias.c	8.17 (Berkeley) 10/15/93";
14 #endif /* not lint */
15 
16 
17 MAP	*AliasDB[MAXALIASDB + 1];	/* actual database list */
18 int	NAliasDBs;			/* number of alias databases */
19 /*
20 **  ALIAS -- Compute aliases.
21 **
22 **	Scans the alias file for an alias for the given address.
23 **	If found, it arranges to deliver to the alias list instead.
24 **	Uses libdbm database if -DDBM.
25 **
26 **	Parameters:
27 **		a -- address to alias.
28 **		sendq -- a pointer to the head of the send queue
29 **			to put the aliases in.
30 **		e -- the current envelope.
31 **
32 **	Returns:
33 **		none
34 **
35 **	Side Effects:
36 **		Aliases found are expanded.
37 **
38 **	Deficiencies:
39 **		It should complain about names that are aliased to
40 **			nothing.
41 */
42 
43 alias(a, sendq, e)
44 	register ADDRESS *a;
45 	ADDRESS **sendq;
46 	register ENVELOPE *e;
47 {
48 	register char *p;
49 	int naliases;
50 	char *owner;
51 	char obuf[MAXNAME + 6];
52 	extern char *aliaslookup();
53 
54 	if (tTd(27, 1))
55 		printf("alias(%s)\n", a->q_paddr);
56 
57 	/* don't realias already aliased names */
58 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
59 		return;
60 
61 	if (NoAlias)
62 		return;
63 
64 	e->e_to = a->q_paddr;
65 
66 	/*
67 	**  Look up this name
68 	*/
69 
70 	p = aliaslookup(a->q_user, e);
71 	if (p == NULL)
72 		return;
73 
74 	/*
75 	**  Match on Alias.
76 	**	Deliver to the target list.
77 	*/
78 
79 	if (tTd(27, 1))
80 		printf("%s (%s, %s) aliased to %s\n",
81 		    a->q_paddr, a->q_host, a->q_user, p);
82 	if (bitset(EF_VRFYONLY, e->e_flags))
83 	{
84 		a->q_flags |= QVERIFIED;
85 		e->e_nrcpts++;
86 		return;
87 	}
88 	message("aliased to %s", p);
89 #ifdef LOG
90 	if (LogLevel > 9)
91 		syslog(LOG_INFO, "%s: alias %s => %s", e->e_id, a->q_paddr, p);
92 #endif
93 	a->q_flags &= ~QSELFREF;
94 	AliasLevel++;
95 	naliases = sendtolist(p, a, sendq, e);
96 	AliasLevel--;
97 	if (!bitset(QSELFREF, a->q_flags))
98 	{
99 		if (tTd(27, 5))
100 		{
101 			printf("alias: QDONTSEND ");
102 			printaddr(a, FALSE);
103 		}
104 		a->q_flags |= QDONTSEND;
105 	}
106 
107 	/*
108 	**  Look for owner of alias
109 	*/
110 
111 	(void) strcpy(obuf, "owner-");
112 	if (strncmp(a->q_user, "owner-", 6) == 0)
113 		(void) strcat(obuf, "owner");
114 	else
115 		(void) strcat(obuf, a->q_user);
116 	if (!bitnset(M_USR_UPPER, a->q_mailer->m_flags))
117 		makelower(obuf);
118 	owner = aliaslookup(obuf, e);
119 	if (owner != NULL)
120 	{
121 		if (strchr(owner, ',') != NULL)
122 			owner = obuf;
123 		a->q_owner = newstr(owner);
124 	}
125 }
126 /*
127 **  ALIASLOOKUP -- look up a name in the alias file.
128 **
129 **	Parameters:
130 **		name -- the name to look up.
131 **
132 **	Returns:
133 **		the value of name.
134 **		NULL if unknown.
135 **
136 **	Side Effects:
137 **		none.
138 **
139 **	Warnings:
140 **		The return value will be trashed across calls.
141 */
142 
143 char *
144 aliaslookup(name, e)
145 	char *name;
146 	ENVELOPE *e;
147 {
148 	register int dbno;
149 	register MAP *map;
150 	register char *p;
151 
152 	for (dbno = 0; dbno < NAliasDBs; dbno++)
153 	{
154 		auto int stat;
155 
156 		map = AliasDB[dbno];
157 		if (!bitset(MF_OPEN, map->map_mflags))
158 			continue;
159 		p = (*map->map_class->map_lookup)(map, name, NULL, &stat);
160 		if (p != NULL)
161 			return p;
162 	}
163 	return NULL;
164 }
165 /*
166 **  SETALIAS -- set up an alias map
167 **
168 **	Called when reading configuration file.
169 **
170 **	Parameters:
171 **		spec -- the alias specification
172 **
173 **	Returns:
174 **		none.
175 */
176 
177 setalias(spec)
178 	char *spec;
179 {
180 	register char *p;
181 	register MAP *map;
182 	char *class;
183 	STAB *s;
184 
185 	if (tTd(27, 8))
186 		printf("setalias(%s)\n", spec);
187 
188 	for (p = spec; p != NULL; )
189 	{
190 		char aname[50];
191 
192 		while (isspace(*p))
193 			p++;
194 		if (*p == '\0')
195 			break;
196 		spec = p;
197 
198 		if (NAliasDBs >= MAXALIASDB)
199 		{
200 			syserr("Too many alias databases defined, %d max", MAXALIASDB);
201 			return;
202 		}
203 		(void) sprintf(aname, "Alias%d", NAliasDBs);
204 		s = stab(aname, ST_MAP, ST_ENTER);
205 		map = &s->s_map;
206 		AliasDB[NAliasDBs] = map;
207 		bzero(map, sizeof *map);
208 
209 		p = strpbrk(p, " ,/:");
210 		if (p != NULL && *p == ':')
211 		{
212 			/* map name */
213 			*p++ = '\0';
214 			class = spec;
215 			spec = p;
216 		}
217 		else
218 		{
219 			class = "implicit";
220 			map->map_mflags = MF_OPTIONAL|MF_INCLNULL;
221 		}
222 
223 		/* find end of spec */
224 		if (p != NULL)
225 			p = strchr(p, ',');
226 		if (p != NULL)
227 			*p++ = '\0';
228 
229 		/* look up class */
230 		s = stab(class, ST_MAPCLASS, ST_FIND);
231 		if (s == NULL)
232 		{
233 			if (tTd(27, 1))
234 				printf("Unknown alias class %s\n", class);
235 		}
236 		else if (!bitset(MCF_ALIASOK, s->s_mapclass.map_cflags))
237 		{
238 			syserr("setalias: map class %s can't handle aliases",
239 				class);
240 		}
241 		else
242 		{
243 			map->map_class = &s->s_mapclass;
244 			if (map->map_class->map_parse(map, spec))
245 			{
246 				map->map_mflags |= MF_VALID|MF_ALIAS;
247 				NAliasDBs++;
248 			}
249 		}
250 	}
251 }
252 /*
253 **  ALIASWAIT -- wait for distinguished @:@ token to appear.
254 **
255 **	This can decide to reopen or rebuild the alias file
256 **
257 **	Parameters:
258 **		map -- a pointer to the map descriptor for this alias file.
259 **		ext -- the filename extension (e.g., ".db") for the
260 **			database file.
261 **		isopen -- if set, the database is already open, and we
262 **			should check for validity; otherwise, we are
263 **			just checking to see if it should be created.
264 **
265 **	Returns:
266 **		TRUE -- if the database is open when we return.
267 **		FALSE -- if the database is closed when we return.
268 */
269 
270 bool
271 aliaswait(map, ext, isopen)
272 	MAP *map;
273 	char *ext;
274 	int isopen;
275 {
276 	int atcnt;
277 	time_t mtime;
278 	struct stat stb;
279 	char buf[MAXNAME];
280 
281 	if (tTd(27, 3))
282 		printf("aliaswait(%s:%s)\n",
283 			map->map_class->map_cname, map->map_file);
284 	if (bitset(MF_ALIASWAIT, map->map_mflags))
285 		return;
286 	map->map_mflags |= MF_ALIASWAIT;
287 
288 	atcnt = SafeAlias * 2;
289 	if (atcnt > 0)
290 	{
291 		auto int st;
292 
293 		while (isopen && atcnt-- >= 0 &&
294 		       map->map_class->map_lookup(map, "@", NULL, &st) == NULL)
295 		{
296 			/*
297 			**  Close and re-open the alias database in case
298 			**  the one is mv'ed instead of cp'ed in.
299 			*/
300 
301 			if (tTd(27, 2))
302 				printf("aliaswait: sleeping\n");
303 
304 			map->map_class->map_close(map);
305 			sleep(30);
306 			isopen = map->map_class->map_open(map, O_RDONLY);
307 		}
308 	}
309 
310 	/* see if we need to go into auto-rebuild mode */
311 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
312 	{
313 		if (tTd(27, 3))
314 			printf("aliaswait: not rebuildable\n");
315 		map->map_mflags &= ~MF_ALIASWAIT;
316 		return isopen;
317 	}
318 	if (stat(map->map_file, &stb) < 0)
319 	{
320 		if (tTd(27, 3))
321 			printf("aliaswait: no source file\n");
322 		map->map_mflags &= ~MF_ALIASWAIT;
323 		return isopen;
324 	}
325 	mtime = stb.st_mtime;
326 	(void) strcpy(buf, map->map_file);
327 	if (ext != NULL)
328 		(void) strcat(buf, ext);
329 	if (stat(buf, &stb) < 0 || stb.st_mtime < mtime || atcnt < 0)
330 	{
331 		/* database is out of date */
332 		if (AutoRebuild && stb.st_ino != 0 && stb.st_uid == geteuid())
333 		{
334 			message("auto-rebuilding alias database %s", buf);
335 			if (isopen)
336 				map->map_class->map_close(map);
337 			rebuildaliases(map, TRUE);
338 			isopen = map->map_class->map_open(map, O_RDONLY);
339 		}
340 		else
341 		{
342 #ifdef LOG
343 			if (LogLevel > 3)
344 				syslog(LOG_INFO, "alias database %s out of date",
345 					buf);
346 #endif /* LOG */
347 			message("Warning: alias database %s out of date", buf);
348 		}
349 	}
350 	map->map_mflags &= ~MF_ALIASWAIT;
351 	return isopen;
352 }
353 /*
354 **  REBUILDALIASES -- rebuild the alias database.
355 **
356 **	Parameters:
357 **		map -- the database to rebuild.
358 **		automatic -- set if this was automatically generated.
359 **
360 **	Returns:
361 **		none.
362 **
363 **	Side Effects:
364 **		Reads the text version of the database, builds the
365 **		DBM or DB version.
366 */
367 
368 rebuildaliases(map, automatic)
369 	register MAP *map;
370 	bool automatic;
371 {
372 	FILE *af;
373 	bool nolock = FALSE;
374 	sigfunc_t oldsigint;
375 
376 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
377 		return;
378 
379 	/* try to lock the source file */
380 	if ((af = fopen(map->map_file, "r+")) == NULL)
381 	{
382 		if (errno != EACCES || automatic ||
383 		    (af = fopen(map->map_file, "r")) == NULL)
384 		{
385 			int saveerr = errno;
386 
387 			if (tTd(27, 1))
388 				printf("Can't open %s: %s\n",
389 					map->map_file, errstring(saveerr));
390 			if (!automatic)
391 				message("newaliases: cannot open %s: %s",
392 					map->map_file, errstring(saveerr));
393 			errno = 0;
394 			return;
395 		}
396 		nolock = TRUE;
397 		message("warning: cannot lock %s: %s",
398 			map->map_file, errstring(errno));
399 	}
400 
401 	/* see if someone else is rebuilding the alias file */
402 	if (!nolock &&
403 	    !lockfile(fileno(af), map->map_file, NULL, LOCK_EX|LOCK_NB))
404 	{
405 		/* yes, they are -- wait until done */
406 		message("Alias file %s is already being rebuilt",
407 			map->map_file);
408 		if (OpMode != MD_INITALIAS)
409 		{
410 			/* wait for other rebuild to complete */
411 			(void) lockfile(fileno(af), map->map_file, NULL,
412 					LOCK_EX);
413 		}
414 		(void) fclose(af);
415 		errno = 0;
416 		return;
417 	}
418 
419 	oldsigint = setsignal(SIGINT, SIG_IGN);
420 
421 	if (map->map_class->map_open(map, O_RDWR))
422 	{
423 #ifdef LOG
424 		if (LogLevel > 7)
425 		{
426 			syslog(LOG_NOTICE, "alias database %s %srebuilt by %s",
427 				map->map_file, automatic ? "auto" : "",
428 				username());
429 		}
430 #endif /* LOG */
431 		map->map_mflags |= MF_OPEN|MF_WRITABLE;
432 		readaliases(map, af, automatic);
433 	}
434 	else
435 	{
436 		if (tTd(27, 1))
437 			printf("Can't create database for %s: %s\n",
438 				map->map_file, errstring(errno));
439 		if (!automatic)
440 			syserr("Cannot create database for alias file %s",
441 				map->map_file);
442 	}
443 
444 	/* close the file, thus releasing locks */
445 	fclose(af);
446 
447 	/* add distinguished entries and close the database */
448 	if (bitset(MF_OPEN, map->map_mflags))
449 		map->map_class->map_close(map);
450 
451 	/* restore the old signal */
452 	(void) setsignal(SIGINT, oldsigint);
453 }
454 /*
455 **  READALIASES -- read and process the alias file.
456 **
457 **	This routine implements the part of initaliases that occurs
458 **	when we are not going to use the DBM stuff.
459 **
460 **	Parameters:
461 **		map -- the alias database descriptor.
462 **		af -- file to read the aliases from.
463 **		automatic -- set if this was an automatic rebuild.
464 **
465 **	Returns:
466 **		none.
467 **
468 **	Side Effects:
469 **		Reads aliasfile into the symbol table.
470 **		Optionally, builds the .dir & .pag files.
471 */
472 
473 readaliases(map, af, automatic)
474 	register MAP *map;
475 	FILE *af;
476 	int automatic;
477 {
478 	register char *p;
479 	char *rhs;
480 	bool skipping;
481 	long naliases, bytes, longest;
482 	ADDRESS al, bl;
483 	char line[BUFSIZ];
484 
485 	/*
486 	**  Read and interpret lines
487 	*/
488 
489 	FileName = map->map_file;
490 	LineNumber = 0;
491 	naliases = bytes = longest = 0;
492 	skipping = FALSE;
493 	while (fgets(line, sizeof (line), af) != NULL)
494 	{
495 		int lhssize, rhssize;
496 
497 		LineNumber++;
498 		p = strchr(line, '\n');
499 		if (p != NULL)
500 			*p = '\0';
501 		switch (line[0])
502 		{
503 		  case '#':
504 		  case '\0':
505 			skipping = FALSE;
506 			continue;
507 
508 		  case ' ':
509 		  case '\t':
510 			if (!skipping)
511 				syserr("554 Non-continuation line starts with space");
512 			skipping = TRUE;
513 			continue;
514 		}
515 		skipping = FALSE;
516 
517 		/*
518 		**  Process the LHS
519 		**	Find the colon separator, and parse the address.
520 		**	It should resolve to a local name -- this will
521 		**	be checked later (we want to optionally do
522 		**	parsing of the RHS first to maximize error
523 		**	detection).
524 		*/
525 
526 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
527 			continue;
528 		if (*p++ != ':')
529 		{
530 			syserr("554 missing colon");
531 			continue;
532 		}
533 		if (parseaddr(line, &al, RF_COPYALL, ':', NULL, CurEnv) == NULL)
534 		{
535 			syserr("554 %s... illegal alias name", al.q_paddr);
536 			continue;
537 		}
538 
539 		/*
540 		**  Process the RHS.
541 		**	'al' is the internal form of the LHS address.
542 		**	'p' points to the text of the RHS.
543 		*/
544 
545 		while (isascii(*p) && isspace(*p))
546 			p++;
547 		rhs = p;
548 		for (;;)
549 		{
550 			register char c;
551 			register char *nlp;
552 
553 			nlp = &p[strlen(p)];
554 			if (nlp[-1] == '\n')
555 				*--nlp = '\0';
556 
557 			if (CheckAliases)
558 			{
559 				/* do parsing & compression of addresses */
560 				while (*p != '\0')
561 				{
562 					auto char *delimptr;
563 
564 					while ((isascii(*p) && isspace(*p)) ||
565 								*p == ',')
566 						p++;
567 					if (*p == '\0')
568 						break;
569 					if (parseaddr(p, &bl, RF_COPYNONE, ',',
570 						      &delimptr, CurEnv) == NULL)
571 						usrerr("553 %s... bad address", p);
572 					p = delimptr;
573 				}
574 			}
575 			else
576 			{
577 				p = nlp;
578 			}
579 
580 			/* see if there should be a continuation line */
581 			c = fgetc(af);
582 			if (!feof(af))
583 				(void) ungetc(c, af);
584 			if (c != ' ' && c != '\t')
585 				break;
586 
587 			/* read continuation line */
588 			if (fgets(p, sizeof line - (p - line), af) == NULL)
589 				break;
590 			LineNumber++;
591 
592 			/* check for line overflow */
593 			if (strchr(p, '\n') == NULL)
594 			{
595 				usrerr("554 alias too long");
596 				break;
597 			}
598 		}
599 		if (al.q_mailer != LocalMailer)
600 		{
601 			syserr("554 %s... cannot alias non-local names",
602 				al.q_paddr);
603 			continue;
604 		}
605 
606 		/*
607 		**  Insert alias into symbol table or DBM file
608 		*/
609 
610 		if (!bitnset(M_USR_UPPER, al.q_mailer->m_flags))
611 			makelower(al.q_user);
612 
613 		lhssize = strlen(al.q_user);
614 		rhssize = strlen(rhs);
615 		map->map_class->map_store(map, al.q_user, rhs);
616 
617 		if (al.q_paddr != NULL)
618 			free(al.q_paddr);
619 		if (al.q_host != NULL)
620 			free(al.q_host);
621 		if (al.q_user != NULL)
622 			free(al.q_user);
623 
624 		/* statistics */
625 		naliases++;
626 		bytes += lhssize + rhssize;
627 		if (rhssize > longest)
628 			longest = rhssize;
629 	}
630 
631 	CurEnv->e_to = NULL;
632 	FileName = NULL;
633 	if (Verbose || !automatic)
634 		message("%s: %d aliases, longest %d bytes, %d bytes total",
635 			map->map_file, naliases, longest, bytes);
636 # ifdef LOG
637 	if (LogLevel > 7)
638 		syslog(LOG_INFO, "%s: %d aliases, longest %d bytes, %d bytes total",
639 			map->map_file, naliases, longest, bytes);
640 # endif /* LOG */
641 }
642 /*
643 **  FORWARD -- Try to forward mail
644 **
645 **	This is similar but not identical to aliasing.
646 **
647 **	Parameters:
648 **		user -- the name of the user who's mail we would like
649 **			to forward to.  It must have been verified --
650 **			i.e., the q_home field must have been filled
651 **			in.
652 **		sendq -- a pointer to the head of the send queue to
653 **			put this user's aliases in.
654 **
655 **	Returns:
656 **		none.
657 **
658 **	Side Effects:
659 **		New names are added to send queues.
660 */
661 
662 forward(user, sendq, e)
663 	ADDRESS *user;
664 	ADDRESS **sendq;
665 	register ENVELOPE *e;
666 {
667 	char *pp;
668 	char *ep;
669 
670 	if (tTd(27, 1))
671 		printf("forward(%s)\n", user->q_paddr);
672 
673 	if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags))
674 		return;
675 	if (user->q_home == NULL)
676 	{
677 		syserr("554 forward: no home");
678 		user->q_home = "/nosuchdirectory";
679 	}
680 
681 	/* good address -- look for .forward file in home */
682 	define('z', user->q_home, e);
683 	define('u', user->q_user, e);
684 	define('h', user->q_host, e);
685 	if (ForwardPath == NULL)
686 		ForwardPath = newstr("\201z/.forward");
687 
688 	for (pp = ForwardPath; pp != NULL; pp = ep)
689 	{
690 		int err;
691 		char buf[MAXPATHLEN+1];
692 
693 		ep = strchr(pp, ':');
694 		if (ep != NULL)
695 			*ep = '\0';
696 		expand(pp, buf, &buf[sizeof buf - 1], e);
697 		if (ep != NULL)
698 			*ep++ = ':';
699 		if (tTd(27, 3))
700 			printf("forward: trying %s\n", buf);
701 
702 		err = include(buf, TRUE, user, sendq, e);
703 		if (err == 0)
704 			break;
705 		else if (transienterror(err))
706 		{
707 			/* we have to suspend this message */
708 			if (tTd(27, 2))
709 				printf("forward: transient error on %s\n", buf);
710 #ifdef LOG
711 			if (LogLevel > 2)
712 				syslog(LOG_ERR, "%s: forward %s: transient error: %s",
713 					e->e_id, buf, errstring(err));
714 #endif
715 			message("%s: %s: message queued", buf, errstring(err));
716 			user->q_flags |= QQUEUEUP;
717 			return;
718 		}
719 	}
720 }
721