xref: /titanic_51/usr/src/cmd/sendmail/src/alias.c (revision 058561cbaa119a6f2659bc27ef343e1b47266bb2)
1 /*
2  * Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * By using this file, you agree to the terms and conditions set
9  * forth in the LICENSE file which can be found at the top level of
10  * the sendmail distribution.
11  *
12  */
13 
14 #pragma ident	"%Z%%M%	%I%	%E% SMI"
15 
16 #include <sendmail.h>
17 
18 SM_RCSID("@(#)$Id: alias.c,v 8.219 2006/10/24 18:04:09 ca Exp $")
19 
20 #define SEPARATOR ':'
21 # define ALIAS_SPEC_SEPARATORS	" ,/:"
22 
23 static MAP	*AliasFileMap = NULL;	/* the actual aliases.files map */
24 static int	NAliasFileMaps;	/* the number of entries in AliasFileMap */
25 
26 static char	*aliaslookup __P((char *, int *, char *));
27 
28 /*
29 **  ALIAS -- Compute aliases.
30 **
31 **	Scans the alias file for an alias for the given address.
32 **	If found, it arranges to deliver to the alias list instead.
33 **	Uses libdbm database if -DDBM.
34 **
35 **	Parameters:
36 **		a -- address to alias.
37 **		sendq -- a pointer to the head of the send queue
38 **			to put the aliases in.
39 **		aliaslevel -- the current alias nesting depth.
40 **		e -- the current envelope.
41 **
42 **	Returns:
43 **		none
44 **
45 **	Side Effects:
46 **		Aliases found are expanded.
47 **
48 **	Deficiencies:
49 **		It should complain about names that are aliased to
50 **			nothing.
51 */
52 
53 void
54 alias(a, sendq, aliaslevel, e)
55 	register ADDRESS *a;
56 	ADDRESS **sendq;
57 	int aliaslevel;
58 	register ENVELOPE *e;
59 {
60 	register char *p;
61 	char *owner;
62 	auto int status = EX_OK;
63 	char obuf[MAXNAME + 7];
64 
65 	if (tTd(27, 1))
66 		sm_dprintf("alias(%s)\n", a->q_user);
67 
68 	/* don't realias already aliased names */
69 	if (!QS_IS_OK(a->q_state))
70 		return;
71 
72 	if (NoAlias)
73 		return;
74 
75 	e->e_to = a->q_paddr;
76 
77 	/*
78 	**  Look up this name.
79 	**
80 	**	If the map was unavailable, we will queue this message
81 	**	until the map becomes available; otherwise, we could
82 	**	bounce messages inappropriately.
83 	*/
84 
85 #if _FFR_REDIRECTEMPTY
86 	/*
87 	**  envelope <> can't be sent to mailing lists, only owner-
88 	**  send spam of this type to owner- of the list
89 	**  ----  to stop spam from going to mailing lists!
90 	*/
91 
92 	if (e->e_sender != NULL && *e->e_sender == '\0')
93 	{
94 		/* Look for owner of alias */
95 		(void) sm_strlcpyn(obuf, sizeof(obuf), 2, "owner-", a->q_user);
96 		if (aliaslookup(obuf, &status, a->q_host) != NULL)
97 		{
98 			if (LogLevel > 8)
99 				sm_syslog(LOG_WARNING, e->e_id,
100 				       "possible spam from <> to list: %s, redirected to %s\n",
101 				       a->q_user, obuf);
102 			a->q_user = sm_rpool_strdup_x(e->e_rpool, obuf);
103 		}
104 	}
105 #endif /* _FFR_REDIRECTEMPTY */
106 
107 	p = aliaslookup(a->q_user, &status, a->q_host);
108 	if (status == EX_TEMPFAIL || status == EX_UNAVAILABLE)
109 	{
110 		a->q_state = QS_QUEUEUP;
111 		if (e->e_message == NULL)
112 			e->e_message = sm_rpool_strdup_x(e->e_rpool,
113 						"alias database unavailable");
114 
115 		/* XXX msg only per recipient? */
116 		if (a->q_message == NULL)
117 			a->q_message = "alias database unavailable";
118 		return;
119 	}
120 	if (p == NULL)
121 		return;
122 
123 	/*
124 	**  Match on Alias.
125 	**	Deliver to the target list.
126 	*/
127 
128 	if (tTd(27, 1))
129 		sm_dprintf("%s (%s, %s) aliased to %s\n",
130 			   a->q_paddr, a->q_host, a->q_user, p);
131 	if (bitset(EF_VRFYONLY, e->e_flags))
132 	{
133 		a->q_state = QS_VERIFIED;
134 		return;
135 	}
136 	message("aliased to %s", shortenstring(p, MAXSHORTSTR));
137 	if (LogLevel > 10)
138 		sm_syslog(LOG_INFO, e->e_id,
139 			  "alias %.100s => %s",
140 			  a->q_paddr, shortenstring(p, MAXSHORTSTR));
141 	a->q_flags &= ~QSELFREF;
142 	if (tTd(27, 5))
143 	{
144 		sm_dprintf("alias: QS_EXPANDED ");
145 		printaddr(sm_debug_file(), a, false);
146 	}
147 	a->q_state = QS_EXPANDED;
148 
149 	/*
150 	**  Always deliver aliased items as the default user.
151 	**  Setting q_gid to 0 forces deliver() to use DefUser
152 	**  instead of the alias name for the call to initgroups().
153 	*/
154 
155 	a->q_uid = DefUid;
156 	a->q_gid = 0;
157 	a->q_fullname = NULL;
158 	a->q_flags |= QGOODUID|QALIAS;
159 
160 	(void) sendtolist(p, a, sendq, aliaslevel + 1, e);
161 
162 	if (bitset(QSELFREF, a->q_flags) && QS_IS_EXPANDED(a->q_state))
163 		a->q_state = QS_OK;
164 
165 	/*
166 	**  Look for owner of alias
167 	*/
168 
169 	if (strncmp(a->q_user, "owner-", 6) == 0 ||
170 	    strlen(a->q_user) > sizeof(obuf) - 7)
171 		(void) sm_strlcpy(obuf, "owner-owner", sizeof(obuf));
172 	else
173 		(void) sm_strlcpyn(obuf, sizeof(obuf), 2, "owner-", a->q_user);
174 	owner = aliaslookup(obuf, &status, a->q_host);
175 	if (owner == NULL)
176 		return;
177 
178 	/* reflect owner into envelope sender */
179 	if (strpbrk(owner, ",:/|\"") != NULL)
180 		owner = obuf;
181 	a->q_owner = sm_rpool_strdup_x(e->e_rpool, owner);
182 
183 	/* announce delivery to this alias; NORECEIPT bit set later */
184 	if (e->e_xfp != NULL)
185 		(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
186 				"Message delivered to mailing list %s\n",
187 				a->q_paddr);
188 	e->e_flags |= EF_SENDRECEIPT;
189 	a->q_flags |= QDELIVERED|QEXPANDED;
190 }
191 /*
192 **  ALIASLOOKUP -- look up a name in the alias file.
193 **
194 **	Parameters:
195 **		name -- the name to look up.
196 **		pstat -- a pointer to a place to put the status.
197 **		av -- argument for %1 expansion.
198 **
199 **	Returns:
200 **		the value of name.
201 **		NULL if unknown.
202 **
203 **	Side Effects:
204 **		none.
205 **
206 **	Warnings:
207 **		The return value will be trashed across calls.
208 */
209 
210 static char *
211 aliaslookup(name, pstat, av)
212 	char *name;
213 	int *pstat;
214 	char *av;
215 {
216 	static MAP *map = NULL;
217 #if _FFR_ALIAS_DETAIL
218 	int i;
219 	char *argv[4];
220 #endif /* _FFR_ALIAS_DETAIL */
221 
222 	if (map == NULL)
223 	{
224 		STAB *s = stab("aliases", ST_MAP, ST_FIND);
225 
226 		if (s == NULL)
227 			return NULL;
228 		map = &s->s_map;
229 	}
230 	DYNOPENMAP(map);
231 
232 	/* special case POstMastER -- always use lower case */
233 	if (sm_strcasecmp(name, "postmaster") == 0)
234 		name = "postmaster";
235 
236 #if _FFR_ALIAS_DETAIL
237 	i = 0;
238 	argv[i++] = name;
239 	argv[i++] = av;
240 
241 	/* XXX '+' is hardwired here as delimiter! */
242 	if (av != NULL && *av == '+')
243 		argv[i++] = av + 1;
244 	argv[i++] = NULL;
245 	return (*map->map_class->map_lookup)(map, name, argv, pstat);
246 #else /* _FFR_ALIAS_DETAIL */
247 	return (*map->map_class->map_lookup)(map, name, NULL, pstat);
248 #endif /* _FFR_ALIAS_DETAIL */
249 }
250 /*
251 **  SETALIAS -- set up an alias map
252 **
253 **	Called when reading configuration file.
254 **
255 **	Parameters:
256 **		spec -- the alias specification
257 **
258 **	Returns:
259 **		none.
260 */
261 
262 void
263 setalias(spec)
264 	char *spec;
265 {
266 	register char *p;
267 	register MAP *map;
268 	char *class;
269 	STAB *s;
270 
271 	if (tTd(27, 8))
272 		sm_dprintf("setalias(%s)\n", spec);
273 
274 	for (p = spec; p != NULL; )
275 	{
276 		char buf[50];
277 
278 		while (isascii(*p) && isspace(*p))
279 			p++;
280 		if (*p == '\0')
281 			break;
282 		spec = p;
283 
284 		if (NAliasFileMaps >= MAXMAPSTACK)
285 		{
286 			syserr("Too many alias databases defined, %d max",
287 				MAXMAPSTACK);
288 			return;
289 		}
290 		if (AliasFileMap == NULL)
291 		{
292 			(void) sm_strlcpy(buf, "aliases.files sequence",
293 					  sizeof(buf));
294 			AliasFileMap = makemapentry(buf);
295 			if (AliasFileMap == NULL)
296 			{
297 				syserr("setalias: cannot create aliases.files map");
298 				return;
299 			}
300 		}
301 		(void) sm_snprintf(buf, sizeof(buf), "Alias%d", NAliasFileMaps);
302 		s = stab(buf, ST_MAP, ST_ENTER);
303 		map = &s->s_map;
304 		memset(map, '\0', sizeof(*map));
305 		map->map_mname = s->s_name;
306 		p = strpbrk(p, ALIAS_SPEC_SEPARATORS);
307 		if (p != NULL && *p == SEPARATOR)
308 		{
309 			/* map name */
310 			*p++ = '\0';
311 			class = spec;
312 			spec = p;
313 		}
314 		else
315 		{
316 			class = "implicit";
317 			map->map_mflags = MF_INCLNULL;
318 		}
319 
320 		/* find end of spec */
321 		if (p != NULL)
322 		{
323 			bool quoted = false;
324 
325 			for (; *p != '\0'; p++)
326 			{
327 				/*
328 				**  Don't break into a quoted string.
329 				**  Needed for ldap maps which use
330 				**  commas in their specifications.
331 				*/
332 
333 				if (*p == '"')
334 					quoted = !quoted;
335 				else if (*p == ',' && !quoted)
336 					break;
337 			}
338 
339 			/* No more alias specifications follow */
340 			if (*p == '\0')
341 				p = NULL;
342 		}
343 		if (p != NULL)
344 			*p++ = '\0';
345 
346 		if (tTd(27, 20))
347 			sm_dprintf("  map %s:%s %s\n", class, s->s_name, spec);
348 
349 		/* look up class */
350 		s = stab(class, ST_MAPCLASS, ST_FIND);
351 		if (s == NULL)
352 		{
353 			syserr("setalias: unknown alias class %s", class);
354 		}
355 		else if (!bitset(MCF_ALIASOK, s->s_mapclass.map_cflags))
356 		{
357 			syserr("setalias: map class %s can't handle aliases",
358 				class);
359 		}
360 		else
361 		{
362 			map->map_class = &s->s_mapclass;
363 			map->map_mflags |= MF_ALIAS;
364 			if (map->map_class->map_parse(map, spec))
365 			{
366 				map->map_mflags |= MF_VALID;
367 				AliasFileMap->map_stack[NAliasFileMaps++] = map;
368 			}
369 		}
370 	}
371 }
372 /*
373 **  ALIASWAIT -- wait for distinguished @:@ token to appear.
374 **
375 **	This can decide to reopen or rebuild the alias file
376 **
377 **	Parameters:
378 **		map -- a pointer to the map descriptor for this alias file.
379 **		ext -- the filename extension (e.g., ".db") for the
380 **			database file.
381 **		isopen -- if set, the database is already open, and we
382 **			should check for validity; otherwise, we are
383 **			just checking to see if it should be created.
384 **
385 **	Returns:
386 **		true -- if the database is open when we return.
387 **		false -- if the database is closed when we return.
388 */
389 
390 bool
391 aliaswait(map, ext, isopen)
392 	MAP *map;
393 	char *ext;
394 	bool isopen;
395 {
396 	bool attimeout = false;
397 	time_t mtime;
398 	struct stat stb;
399 	char buf[MAXPATHLEN];
400 
401 	if (tTd(27, 3))
402 		sm_dprintf("aliaswait(%s:%s)\n",
403 			   map->map_class->map_cname, map->map_file);
404 	if (bitset(MF_ALIASWAIT, map->map_mflags))
405 		return isopen;
406 	map->map_mflags |= MF_ALIASWAIT;
407 
408 	if (SafeAlias > 0)
409 	{
410 		auto int st;
411 		unsigned int sleeptime = 2;
412 		unsigned int loopcount = 0;	/* only used for debugging */
413 		time_t toolong = curtime() + SafeAlias;
414 
415 		while (isopen &&
416 		       map->map_class->map_lookup(map, "@", NULL, &st) == NULL)
417 		{
418 			if (curtime() > toolong)
419 			{
420 				/* we timed out */
421 				attimeout = true;
422 				break;
423 			}
424 
425 			/*
426 			**  Close and re-open the alias database in case
427 			**  the one is mv'ed instead of cp'ed in.
428 			*/
429 
430 			if (tTd(27, 2))
431 			{
432 				loopcount++;
433 				sm_dprintf("aliaswait: sleeping for %u seconds (loopcount = %u)\n",
434 					   sleeptime, loopcount);
435 			}
436 
437 			map->map_mflags |= MF_CLOSING;
438 			map->map_class->map_close(map);
439 			map->map_mflags &= ~(MF_OPEN|MF_WRITABLE|MF_CLOSING);
440 			(void) sleep(sleeptime);
441 			sleeptime *= 2;
442 			if (sleeptime > 60)
443 				sleeptime = 60;
444 			isopen = map->map_class->map_open(map, O_RDONLY);
445 		}
446 	}
447 
448 	/* see if we need to go into auto-rebuild mode */
449 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
450 	{
451 		if (tTd(27, 3))
452 			sm_dprintf("aliaswait: not rebuildable\n");
453 		map->map_mflags &= ~MF_ALIASWAIT;
454 		return isopen;
455 	}
456 	if (stat(map->map_file, &stb) < 0)
457 	{
458 		if (tTd(27, 3))
459 			sm_dprintf("aliaswait: no source file\n");
460 		map->map_mflags &= ~MF_ALIASWAIT;
461 		return isopen;
462 	}
463 	mtime = stb.st_mtime;
464 	if (sm_strlcpyn(buf, sizeof(buf), 2,
465 			map->map_file, ext == NULL ? "" : ext) >= sizeof(buf))
466 	{
467 		if (LogLevel > 3)
468 			sm_syslog(LOG_INFO, NOQID,
469 				  "alias database %s%s name too long",
470 				  map->map_file, ext == NULL ? "" : ext);
471 		message("alias database %s%s name too long",
472 			map->map_file, ext == NULL ? "" : ext);
473 	}
474 
475 	if (stat(buf, &stb) < 0 || stb.st_mtime < mtime || attimeout)
476 	{
477 		if (LogLevel > 3)
478 			sm_syslog(LOG_INFO, NOQID,
479 				  "alias database %s out of date", buf);
480 		message("Warning: alias database %s out of date", buf);
481 	}
482 	map->map_mflags &= ~MF_ALIASWAIT;
483 	return isopen;
484 }
485 /*
486 **  REBUILDALIASES -- rebuild the alias database.
487 **
488 **	Parameters:
489 **		map -- the database to rebuild.
490 **		automatic -- set if this was automatically generated.
491 **
492 **	Returns:
493 **		true if successful; false otherwise.
494 **
495 **	Side Effects:
496 **		Reads the text version of the database, builds the
497 **		DBM or DB version.
498 */
499 
500 bool
501 rebuildaliases(map, automatic)
502 	register MAP *map;
503 	bool automatic;
504 {
505 	SM_FILE_T *af;
506 	bool nolock = false;
507 	bool success = false;
508 	long sff = SFF_OPENASROOT|SFF_REGONLY|SFF_NOLOCK;
509 	sigfunc_t oldsigint, oldsigquit;
510 #ifdef SIGTSTP
511 	sigfunc_t oldsigtstp;
512 #endif /* SIGTSTP */
513 
514 	if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
515 		return false;
516 
517 	if (!bitnset(DBS_LINKEDALIASFILEINWRITABLEDIR, DontBlameSendmail))
518 		sff |= SFF_NOWLINK;
519 	if (!bitnset(DBS_GROUPWRITABLEALIASFILE, DontBlameSendmail))
520 		sff |= SFF_NOGWFILES;
521 	if (!bitnset(DBS_WORLDWRITABLEALIASFILE, DontBlameSendmail))
522 		sff |= SFF_NOWWFILES;
523 
524 	/* try to lock the source file */
525 	if ((af = safefopen(map->map_file, O_RDWR, 0, sff)) == NULL)
526 	{
527 		struct stat stb;
528 
529 		if ((errno != EACCES && errno != EROFS) || automatic ||
530 		    (af = safefopen(map->map_file, O_RDONLY, 0, sff)) == NULL)
531 		{
532 			int saveerr = errno;
533 
534 			if (tTd(27, 1))
535 				sm_dprintf("Can't open %s: %s\n",
536 					map->map_file, sm_errstring(saveerr));
537 			if (!automatic && !bitset(MF_OPTIONAL, map->map_mflags))
538 				message("newaliases: cannot open %s: %s",
539 					map->map_file, sm_errstring(saveerr));
540 			errno = 0;
541 			return false;
542 		}
543 		nolock = true;
544 		if (tTd(27, 1) ||
545 		    fstat(sm_io_getinfo(af, SM_IO_WHAT_FD, NULL), &stb) < 0 ||
546 		    bitset(S_IWUSR|S_IWGRP|S_IWOTH, stb.st_mode))
547 			message("warning: cannot lock %s: %s",
548 				map->map_file, sm_errstring(errno));
549 	}
550 
551 	/* see if someone else is rebuilding the alias file */
552 	if (!nolock &&
553 	    !lockfile(sm_io_getinfo(af, SM_IO_WHAT_FD, NULL), map->map_file,
554 		      NULL, LOCK_EX|LOCK_NB))
555 	{
556 		/* yes, they are -- wait until done */
557 		message("Alias file %s is locked (maybe being rebuilt)",
558 			map->map_file);
559 		if (OpMode != MD_INITALIAS)
560 		{
561 			/* wait for other rebuild to complete */
562 			(void) lockfile(sm_io_getinfo(af, SM_IO_WHAT_FD, NULL),
563 					map->map_file, NULL, LOCK_EX);
564 		}
565 		(void) sm_io_close(af, SM_TIME_DEFAULT);
566 		errno = 0;
567 		return false;
568 	}
569 
570 	oldsigint = sm_signal(SIGINT, SIG_IGN);
571 	oldsigquit = sm_signal(SIGQUIT, SIG_IGN);
572 #ifdef SIGTSTP
573 	oldsigtstp = sm_signal(SIGTSTP, SIG_IGN);
574 #endif /* SIGTSTP */
575 
576 	if (map->map_class->map_open(map, O_RDWR))
577 	{
578 		if (LogLevel > 7)
579 		{
580 			sm_syslog(LOG_NOTICE, NOQID,
581 				"alias database %s %srebuilt by %s",
582 				map->map_file, automatic ? "auto" : "",
583 				username());
584 		}
585 		map->map_mflags |= MF_OPEN|MF_WRITABLE;
586 		map->map_pid = CurrentPid;
587 		readaliases(map, af, !automatic, true);
588 		success = true;
589 	}
590 	else
591 	{
592 		if (tTd(27, 1))
593 			sm_dprintf("Can't create database for %s: %s\n",
594 				map->map_file, sm_errstring(errno));
595 		if (!automatic)
596 			syserr("Cannot create database for alias file %s",
597 				map->map_file);
598 	}
599 
600 	/* close the file, thus releasing locks */
601 	(void) sm_io_close(af, SM_TIME_DEFAULT);
602 
603 	/* add distinguished entries and close the database */
604 	if (bitset(MF_OPEN, map->map_mflags))
605 	{
606 		map->map_mflags |= MF_CLOSING;
607 		map->map_class->map_close(map);
608 		map->map_mflags &= ~(MF_OPEN|MF_WRITABLE|MF_CLOSING);
609 	}
610 
611 	/* restore the old signals */
612 	(void) sm_signal(SIGINT, oldsigint);
613 	(void) sm_signal(SIGQUIT, oldsigquit);
614 #ifdef SIGTSTP
615 	(void) sm_signal(SIGTSTP, oldsigtstp);
616 #endif /* SIGTSTP */
617 	return success;
618 }
619 /*
620 **  READALIASES -- read and process the alias file.
621 **
622 **	This routine implements the part of initaliases that occurs
623 **	when we are not going to use the DBM stuff.
624 **
625 **	Parameters:
626 **		map -- the alias database descriptor.
627 **		af -- file to read the aliases from.
628 **		announcestats -- announce statistics regarding number of
629 **			aliases, longest alias, etc.
630 **		logstats -- lot the same info.
631 **
632 **	Returns:
633 **		none.
634 **
635 **	Side Effects:
636 **		Reads aliasfile into the symbol table.
637 **		Optionally, builds the .dir & .pag files.
638 */
639 
640 void
641 readaliases(map, af, announcestats, logstats)
642 	register MAP *map;
643 	SM_FILE_T *af;
644 	bool announcestats;
645 	bool logstats;
646 {
647 	register char *p;
648 	char *rhs;
649 	bool skipping;
650 	long naliases, bytes, longest;
651 	ADDRESS al, bl;
652 	char line[BUFSIZ];
653 
654 	/*
655 	**  Read and interpret lines
656 	*/
657 
658 	FileName = map->map_file;
659 	LineNumber = 0;
660 	naliases = bytes = longest = 0;
661 	skipping = false;
662 	while (sm_io_fgets(af, SM_TIME_DEFAULT, line, sizeof(line)) != NULL)
663 	{
664 		int lhssize, rhssize;
665 		int c;
666 
667 		LineNumber++;
668 		p = strchr(line, '\n');
669 
670 		/* XXX what if line="a\\" ? */
671 		while (p != NULL && p > line && p[-1] == '\\')
672 		{
673 			p--;
674 			if (sm_io_fgets(af, SM_TIME_DEFAULT, p,
675 					SPACELEFT(line, p)) == NULL)
676 				break;
677 			LineNumber++;
678 			p = strchr(p, '\n');
679 		}
680 		if (p != NULL)
681 			*p = '\0';
682 		else if (!sm_io_eof(af))
683 		{
684 			errno = 0;
685 			syserr("554 5.3.0 alias line too long");
686 
687 			/* flush to end of line */
688 			while ((c = sm_io_getc(af, SM_TIME_DEFAULT)) !=
689 				SM_IO_EOF && c != '\n')
690 				continue;
691 
692 			/* skip any continuation lines */
693 			skipping = true;
694 			continue;
695 		}
696 		switch (line[0])
697 		{
698 		  case '#':
699 		  case '\0':
700 			skipping = false;
701 			continue;
702 
703 		  case ' ':
704 		  case '\t':
705 			if (!skipping)
706 				syserr("554 5.3.5 Non-continuation line starts with space");
707 			skipping = true;
708 			continue;
709 		}
710 		skipping = false;
711 
712 		/*
713 		**  Process the LHS
714 		**	Find the colon separator, and parse the address.
715 		**	It should resolve to a local name -- this will
716 		**	be checked later (we want to optionally do
717 		**	parsing of the RHS first to maximize error
718 		**	detection).
719 		*/
720 
721 		for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++)
722 			continue;
723 		if (*p++ != ':')
724 		{
725 			syserr("554 5.3.5 missing colon");
726 			continue;
727 		}
728 		if (parseaddr(line, &al, RF_COPYALL, ':', NULL, CurEnv, true)
729 		    == NULL)
730 		{
731 			syserr("554 5.3.5 %.40s... illegal alias name", line);
732 			continue;
733 		}
734 
735 		/*
736 		**  Process the RHS.
737 		**	'al' is the internal form of the LHS address.
738 		**	'p' points to the text of the RHS.
739 		*/
740 
741 		while (isascii(*p) && isspace(*p))
742 			p++;
743 		rhs = p;
744 		for (;;)
745 		{
746 			register char *nlp;
747 
748 			nlp = &p[strlen(p)];
749 			if (nlp > p && nlp[-1] == '\n')
750 				*--nlp = '\0';
751 
752 			if (CheckAliases)
753 			{
754 				/* do parsing & compression of addresses */
755 				while (*p != '\0')
756 				{
757 					auto char *delimptr;
758 
759 					while ((isascii(*p) && isspace(*p)) ||
760 								*p == ',')
761 						p++;
762 					if (*p == '\0')
763 						break;
764 					if (parseaddr(p, &bl, RF_COPYNONE, ',',
765 						      &delimptr, CurEnv, true)
766 					    == NULL)
767 						usrerr("553 5.3.5 %s... bad address", p);
768 					p = delimptr;
769 				}
770 			}
771 			else
772 			{
773 				p = nlp;
774 			}
775 
776 			/* see if there should be a continuation line */
777 			c = sm_io_getc(af, SM_TIME_DEFAULT);
778 			if (!sm_io_eof(af))
779 				(void) sm_io_ungetc(af, SM_TIME_DEFAULT, c);
780 			if (c != ' ' && c != '\t')
781 				break;
782 
783 			/* read continuation line */
784 			if (sm_io_fgets(af, SM_TIME_DEFAULT, p,
785 					sizeof(line) - (p-line)) == NULL)
786 				break;
787 			LineNumber++;
788 
789 			/* check for line overflow */
790 			if (strchr(p, '\n') == NULL && !sm_io_eof(af))
791 			{
792 				usrerr("554 5.3.5 alias too long");
793 				while ((c = sm_io_getc(af, SM_TIME_DEFAULT))
794 				       != SM_IO_EOF && c != '\n')
795 					continue;
796 				skipping = true;
797 				break;
798 			}
799 		}
800 
801 		if (skipping)
802 			continue;
803 
804 		if (!bitnset(M_ALIASABLE, al.q_mailer->m_flags))
805 		{
806 			syserr("554 5.3.5 %s... cannot alias non-local names",
807 				al.q_paddr);
808 			continue;
809 		}
810 
811 		/*
812 		**  Insert alias into symbol table or database file.
813 		**
814 		**	Special case pOStmaStER -- always make it lower case.
815 		*/
816 
817 		if (sm_strcasecmp(al.q_user, "postmaster") == 0)
818 			makelower(al.q_user);
819 
820 		lhssize = strlen(al.q_user);
821 		rhssize = strlen(rhs);
822 		if (rhssize > 0)
823 		{
824 			/* is RHS empty (just spaces)? */
825 			p = rhs;
826 			while (isascii(*p) && isspace(*p))
827 				p++;
828 		}
829 		if (rhssize == 0 || *p == '\0')
830 		{
831 			syserr("554 5.3.5 %.40s... missing value for alias",
832 			       line);
833 
834 		}
835 		else
836 		{
837 			map->map_class->map_store(map, al.q_user, rhs);
838 
839 			/* statistics */
840 			naliases++;
841 			bytes += lhssize + rhssize;
842 			if (rhssize > longest)
843 				longest = rhssize;
844 		}
845 
846 #if 0
847 	/*
848 	**  address strings are now stored in the envelope rpool,
849 	**  and therefore cannot be freed.
850 	*/
851 		if (al.q_paddr != NULL)
852 			sm_free(al.q_paddr);  /* disabled */
853 		if (al.q_host != NULL)
854 			sm_free(al.q_host);  /* disabled */
855 		if (al.q_user != NULL)
856 			sm_free(al.q_user);  /* disabled */
857 #endif /* 0 */
858 	}
859 
860 	CurEnv->e_to = NULL;
861 	FileName = NULL;
862 	if (Verbose || announcestats)
863 		message("%s: %ld aliases, longest %ld bytes, %ld bytes total",
864 			map->map_file, naliases, longest, bytes);
865 	if (LogLevel > 7 && logstats)
866 		sm_syslog(LOG_INFO, NOQID,
867 			"%s: %ld aliases, longest %ld bytes, %ld bytes total",
868 			map->map_file, naliases, longest, bytes);
869 }
870 /*
871 **  FORWARD -- Try to forward mail
872 **
873 **	This is similar but not identical to aliasing.
874 **
875 **	Parameters:
876 **		user -- the name of the user who's mail we would like
877 **			to forward to.  It must have been verified --
878 **			i.e., the q_home field must have been filled
879 **			in.
880 **		sendq -- a pointer to the head of the send queue to
881 **			put this user's aliases in.
882 **		aliaslevel -- the current alias nesting depth.
883 **		e -- the current envelope.
884 **
885 **	Returns:
886 **		none.
887 **
888 **	Side Effects:
889 **		New names are added to send queues.
890 */
891 
892 void
893 forward(user, sendq, aliaslevel, e)
894 	ADDRESS *user;
895 	ADDRESS **sendq;
896 	int aliaslevel;
897 	register ENVELOPE *e;
898 {
899 	char *pp;
900 	char *ep;
901 	bool got_transient;
902 
903 	if (tTd(27, 1))
904 		sm_dprintf("forward(%s)\n", user->q_paddr);
905 
906 	if (!bitnset(M_HASPWENT, user->q_mailer->m_flags) ||
907 	    !QS_IS_OK(user->q_state))
908 		return;
909 	if (ForwardPath != NULL && *ForwardPath == '\0')
910 		return;
911 	if (user->q_home == NULL)
912 	{
913 		syserr("554 5.3.0 forward: no home");
914 		user->q_home = "/no/such/directory";
915 	}
916 
917 	/* good address -- look for .forward file in home */
918 	macdefine(&e->e_macro, A_PERM, 'z', user->q_home);
919 	macdefine(&e->e_macro, A_PERM, 'u', user->q_user);
920 	macdefine(&e->e_macro, A_PERM, 'h', user->q_host);
921 	if (ForwardPath == NULL)
922 		ForwardPath = newstr("\201z/.forward");
923 
924 	got_transient = false;
925 	for (pp = ForwardPath; pp != NULL; pp = ep)
926 	{
927 		int err;
928 		char buf[MAXPATHLEN];
929 		struct stat st;
930 
931 		ep = strchr(pp, SEPARATOR);
932 		if (ep != NULL)
933 			*ep = '\0';
934 		expand(pp, buf, sizeof(buf), e);
935 		if (ep != NULL)
936 			*ep++ = SEPARATOR;
937 		if (buf[0] == '\0')
938 			continue;
939 		if (tTd(27, 3))
940 			sm_dprintf("forward: trying %s\n", buf);
941 
942 		err = include(buf, true, user, sendq, aliaslevel, e);
943 		if (err == 0)
944 			break;
945 		else if (transienterror(err))
946 		{
947 			/* we may have to suspend this message */
948 			got_transient = true;
949 			if (tTd(27, 2))
950 				sm_dprintf("forward: transient error on %s\n",
951 					   buf);
952 			if (LogLevel > 2)
953 			{
954 				char *curhost = CurHostName;
955 
956 				CurHostName = NULL;
957 				sm_syslog(LOG_ERR, e->e_id,
958 					  "forward %s: transient error: %s",
959 					  buf, sm_errstring(err));
960 				CurHostName = curhost;
961 			}
962 
963 		}
964 		else
965 		{
966 			switch (err)
967 			{
968 			  case ENOENT:
969 				break;
970 
971 			  case E_SM_WWDIR:
972 			  case E_SM_GWDIR:
973 				/* check if it even exists */
974 				if (stat(buf, &st) < 0 && errno == ENOENT)
975 				{
976 					if (bitnset(DBS_DONTWARNFORWARDFILEINUNSAFEDIRPATH,
977 						    DontBlameSendmail))
978 						break;
979 				}
980 				/* FALLTHROUGH */
981 
982 #if _FFR_FORWARD_SYSERR
983 			  case E_SM_NOSLINK:
984 			  case E_SM_NOHLINK:
985 			  case E_SM_REGONLY:
986 			  case E_SM_ISEXEC:
987 			  case E_SM_WWFILE:
988 			  case E_SM_GWFILE:
989 				syserr("forward: %s: %s", buf, sm_errstring(err));
990 				break;
991 #endif /* _FFR_FORWARD_SYSERR */
992 
993 			  default:
994 				if (LogLevel > (RunAsUid == 0 ? 2 : 10))
995 					sm_syslog(LOG_WARNING, e->e_id,
996 						  "forward %s: %s", buf,
997 						  sm_errstring(err));
998 				if (Verbose)
999 					message("forward: %s: %s",
1000 						buf, sm_errstring(err));
1001 				break;
1002 			}
1003 		}
1004 	}
1005 	if (pp == NULL && got_transient)
1006 	{
1007 		/*
1008 		**  There was no successful .forward open and at least one
1009 		**  transient open.  We have to defer this address for
1010 		**  further delivery.
1011 		*/
1012 
1013 		message("transient .forward open error: message queued");
1014 		user->q_state = QS_QUEUEUP;
1015 		return;
1016 	}
1017 }
1018