xref: /csrg-svn/usr.sbin/sendmail/src/map.c (revision 63937)
156822Seric /*
256822Seric  * Copyright (c) 1992 Eric P. Allman.
362526Sbostic  * Copyright (c) 1992, 1993
462526Sbostic  *	The Regents of the University of California.  All rights reserved.
556822Seric  *
656822Seric  * %sccs.include.redist.c%
756822Seric  */
856822Seric 
956822Seric #ifndef lint
10*63937Seric static char sccsid[] = "@(#)map.c	8.4 (Berkeley) 07/19/93";
1156822Seric #endif /* not lint */
1256822Seric 
1356822Seric #include "sendmail.h"
1456822Seric 
1560089Seric #ifdef NDBM
1656822Seric #include <ndbm.h>
1756822Seric #endif
1860089Seric #ifdef NEWDB
1956822Seric #include <db.h>
2056822Seric #endif
2160089Seric #ifdef NIS
2257208Seric #include <rpcsvc/ypclnt.h>
2357208Seric #endif
2456822Seric 
2556822Seric /*
2660089Seric **  MAP.C -- implementations for various map classes.
2756822Seric **
2860089Seric **	Each map class implements a series of functions:
2960089Seric **
3060089Seric **	bool map_parse(MAP *map, char *args)
3160089Seric **		Parse the arguments from the config file.  Return TRUE
3260089Seric **		if they were ok, FALSE otherwise.  Fill in map with the
3360089Seric **		values.
3460089Seric **
3560222Seric **	char *map_lookup(MAP *map, char *key, char **args, int *pstat)
3660222Seric **		Look up the key in the given map.  If found, do any
3760222Seric **		rewriting the map wants (including "args" if desired)
3860089Seric **		and return the value.  Set *pstat to the appropriate status
3960222Seric **		on error and return NULL.  Args will be NULL if called
4060222Seric **		from the alias routines, although this should probably
4160222Seric **		not be relied upon.  It is suggested you call map_rewrite
4260222Seric **		to return the results -- it takes care of null termination
4360222Seric **		and uses a dynamically expanded buffer as needed.
4460089Seric **
4560089Seric **	void map_store(MAP *map, char *key, char *value)
4660089Seric **		Store the key:value pair in the map.
4760089Seric **
4860089Seric **	bool map_open(MAP *map, int mode)
4960222Seric **		Open the map for the indicated mode.  Mode should
5060222Seric **		be either O_RDONLY or O_RDWR.  Return TRUE if it
5160222Seric **		was opened successfully, FALSE otherwise.  If the open
5260222Seric **		failed an the MF_OPTIONAL flag is not set, it should
5360222Seric **		also print an error.  If the MF_ALIAS bit is set
5460222Seric **		and this map class understands the @:@ convention, it
5560222Seric **		should call aliaswait() before returning.
5660089Seric **
5760089Seric **	void map_close(MAP *map)
5860089Seric **		Close the map.
5960089Seric */
6060089Seric 
6160089Seric #define DBMMODE		0644
6260089Seric /*
6360089Seric **  MAP_PARSEARGS -- parse config line arguments for database lookup
6460089Seric **
6560089Seric **	This is a generic version of the map_parse method.
6660089Seric **
6756822Seric **	Parameters:
6860089Seric **		map -- the map being initialized.
6960089Seric **		ap -- a pointer to the args on the config line.
7056822Seric **
7156822Seric **	Returns:
7260089Seric **		TRUE -- if everything parsed OK.
7356822Seric **		FALSE -- otherwise.
7456822Seric **
7556822Seric **	Side Effects:
7660089Seric **		null terminates the filename; stores it in map
7756822Seric */
7856822Seric 
7956822Seric bool
8060089Seric map_parseargs(map, ap)
8156822Seric 	MAP *map;
8260089Seric 	char *ap;
8356822Seric {
8460089Seric 	register char *p = ap;
8556822Seric 
8663753Seric 	map->map_mflags |= MF_TRY0NULL | MF_TRY1NULL;
8760089Seric 	for (;;)
8860089Seric 	{
8960089Seric 		while (isascii(*p) && isspace(*p))
9060089Seric 			p++;
9160089Seric 		if (*p != '-')
9260089Seric 			break;
9360089Seric 		switch (*++p)
9460089Seric 		{
9560089Seric 		  case 'N':
9660207Seric 			map->map_mflags |= MF_INCLNULL;
9763753Seric 			map->map_mflags &= ~MF_TRY0NULL;
9860089Seric 			break;
9960089Seric 
10063753Seric 		  case 'O':
10163753Seric 			map->map_mflags &= ~MF_TRY1NULL;
10263753Seric 			break;
10363753Seric 
10460089Seric 		  case 'o':
10560207Seric 			map->map_mflags |= MF_OPTIONAL;
10660089Seric 			break;
10760089Seric 
10860089Seric 		  case 'f':
10960207Seric 			map->map_mflags |= MF_NOFOLDCASE;
11060089Seric 			break;
11160089Seric 
11260089Seric 		  case 'm':
11360207Seric 			map->map_mflags |= MF_MATCHONLY;
11460089Seric 			break;
11560089Seric 
11660089Seric 		  case 'a':
11760089Seric 			map->map_app = ++p;
11860089Seric 			break;
11960089Seric 		}
12060089Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
12160089Seric 			p++;
12260089Seric 		if (*p != '\0')
12360089Seric 			*p++ = '\0';
12460089Seric 	}
12560089Seric 	if (map->map_app != NULL)
12660089Seric 		map->map_app = newstr(map->map_app);
12760089Seric 
12860089Seric 	if (*p != '\0')
12960089Seric 	{
13060089Seric 		map->map_file = p;
13160089Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
13260089Seric 			p++;
13360089Seric 		if (*p != '\0')
13460089Seric 			*p++ = '\0';
13560089Seric 		map->map_file = newstr(map->map_file);
13660089Seric 	}
13760089Seric 
13860089Seric 	while (*p != '\0' && isascii(*p) && isspace(*p))
13960089Seric 		p++;
14060089Seric 	if (*p != '\0')
14160089Seric 		map->map_rebuild = newstr(p);
14260089Seric 
14356822Seric 	if (map->map_file == NULL)
14457208Seric 	{
14560089Seric 		syserr("No file name for %s map %s",
14660089Seric 			map->map_class->map_cname, map->map_mname);
14756822Seric 		return FALSE;
14857208Seric 	}
14960089Seric 	return TRUE;
15060089Seric }
15160089Seric /*
15260089Seric **  MAP_REWRITE -- rewrite a database key, interpolating %n indications.
15360089Seric **
15460089Seric **	It also adds the map_app string.  It can be used as a utility
15560089Seric **	in the map_lookup method.
15660089Seric **
15760089Seric **	Parameters:
15860089Seric **		map -- the map that causes this.
15960089Seric **		s -- the string to rewrite, NOT necessarily null terminated.
16060089Seric **		slen -- the length of s.
16160089Seric **		av -- arguments to interpolate into buf.
16260089Seric **
16360089Seric **	Returns:
16460089Seric **		Pointer to rewritten result.
16560089Seric **
16660089Seric **	Side Effects:
16760089Seric **		none.
16860089Seric */
16960089Seric 
17060492Seric struct rwbuf
17160492Seric {
17260492Seric 	int	rwb_len;	/* size of buffer */
17360492Seric 	char	*rwb_buf;	/* ptr to buffer */
17460492Seric };
17560492Seric 
17660492Seric struct rwbuf	RwBufs[2];	/* buffers for rewriting output */
17760492Seric 
17860089Seric char *
17960089Seric map_rewrite(map, s, slen, av)
18060089Seric 	register MAP *map;
18160089Seric 	register char *s;
18260089Seric 	int slen;
18360089Seric 	char **av;
18460089Seric {
18560089Seric 	register char *bp;
18660089Seric 	register char c;
18760089Seric 	char **avp;
18860089Seric 	register char *ap;
18960492Seric 	register struct rwbuf *rwb;
19060089Seric 	int i;
19160089Seric 	int len;
19260089Seric 
19360537Seric 	if (tTd(39, 1))
19460089Seric 	{
19560256Seric 		printf("map_rewrite(%.*s), av =", slen, s);
19660256Seric 		if (av == NULL)
19760256Seric 			printf(" (nullv)");
19860256Seric 		else
19960256Seric 		{
20060256Seric 			for (avp = av; *avp != NULL; avp++)
20160256Seric 				printf("\n\t%s", *avp);
20260256Seric 		}
20360256Seric 		printf("\n");
20460089Seric 	}
20560089Seric 
20660492Seric 	rwb = RwBufs;
20760492Seric 	if (av == NULL)
20860492Seric 		rwb++;
20960492Seric 
21060089Seric 	/* count expected size of output (can safely overestimate) */
21160089Seric 	i = len = slen;
21260089Seric 	if (av != NULL)
21360089Seric 	{
21460089Seric 		bp = s;
21560089Seric 		for (i = slen; --i >= 0 && (c = *bp++) != 0; )
21660089Seric 		{
21760089Seric 			if (c != '%')
21860089Seric 				continue;
21960089Seric 			if (--i < 0)
22060089Seric 				break;
22160089Seric 			c = *bp++;
22260089Seric 			if (!(isascii(c) && isdigit(c)))
22360089Seric 				continue;
224*63937Seric 			for (avp = av; --c >= '0' && *avp != NULL; avp++)
22560089Seric 				continue;
22660089Seric 			if (*avp == NULL)
22760089Seric 				continue;
22860089Seric 			len += strlen(*avp);
22960089Seric 		}
23060089Seric 	}
23160089Seric 	if (map->map_app != NULL)
23260089Seric 		len += strlen(map->map_app);
23360492Seric 	if (rwb->rwb_len < ++len)
23460089Seric 	{
23560089Seric 		/* need to malloc additional space */
23660492Seric 		rwb->rwb_len = len;
23760492Seric 		if (rwb->rwb_buf != NULL)
23860492Seric 			free(rwb->rwb_buf);
23960492Seric 		rwb->rwb_buf = xalloc(rwb->rwb_len);
24060089Seric 	}
24160089Seric 
24260492Seric 	bp = rwb->rwb_buf;
24360089Seric 	if (av == NULL)
24460089Seric 	{
24560089Seric 		bcopy(s, bp, slen);
24660089Seric 		bp += slen;
24760089Seric 	}
24860089Seric 	else
24960089Seric 	{
25060089Seric 		while (--slen >= 0 && (c = *s++) != '\0')
25160089Seric 		{
25260089Seric 			if (c != '%')
25360089Seric 			{
25460089Seric   pushc:
25560089Seric 				*bp++ = c;
25660089Seric 				continue;
25760089Seric 			}
25860089Seric 			if (--slen < 0 || (c = *s++) == '\0')
25960089Seric 				c = '%';
26060089Seric 			if (c == '%')
26160089Seric 				goto pushc;
26260089Seric 			if (!(isascii(c) && isdigit(c)))
26360089Seric 			{
26460089Seric 				*bp++ = '%';
26560089Seric 				goto pushc;
26660089Seric 			}
267*63937Seric 			for (avp = av; --c >= '0' && *avp != NULL; avp++)
26860089Seric 				continue;
26960089Seric 			if (*avp == NULL)
27060089Seric 				continue;
27160089Seric 
27260089Seric 			/* transliterate argument into output string */
27360089Seric 			for (ap = *avp; (c = *ap++) != '\0'; )
27460089Seric 				*bp++ = c;
27560089Seric 		}
27660089Seric 	}
27760089Seric 	if (map->map_app != NULL)
27860089Seric 		strcpy(bp, map->map_app);
27960089Seric 	else
28060089Seric 		*bp = '\0';
28160537Seric 	if (tTd(39, 1))
28260492Seric 		printf("map_rewrite => %s\n", rwb->rwb_buf);
28360492Seric 	return rwb->rwb_buf;
28460089Seric }
28560089Seric /*
28660537Seric **  INITMAPS -- initialize for aliasing
28760537Seric **
28860537Seric **	Parameters:
28960537Seric **		rebuild -- if TRUE, this rebuilds the cached versions.
29060537Seric **		e -- current envelope.
29160537Seric **
29260537Seric **	Returns:
29360537Seric **		none.
29460537Seric **
29560537Seric **	Side Effects:
29660537Seric **		initializes aliases:
29760537Seric **		if NDBM:  opens the database.
29860537Seric **		if ~NDBM: reads the aliases into the symbol table.
29960537Seric */
30060537Seric 
30160537Seric initmaps(rebuild, e)
30260537Seric 	bool rebuild;
30360537Seric 	register ENVELOPE *e;
30460537Seric {
30560537Seric 	extern void map_init();
30660537Seric 
30760537Seric 	CurEnv = e;
30860537Seric 	stabapply(map_init, rebuild);
30960537Seric }
31060537Seric 
31160537Seric void
31260537Seric map_init(s, rebuild)
31360537Seric 	register STAB *s;
31460537Seric 	int rebuild;
31560537Seric {
31660537Seric 	register MAP *map;
31760537Seric 
31860537Seric 	/* has to be a map */
31960537Seric 	if (s->s_type != ST_MAP)
32060537Seric 		return;
32160537Seric 
32260537Seric 	map = &s->s_map;
32360537Seric 	if (!bitset(MF_VALID, map->map_mflags))
32460537Seric 		return;
32560537Seric 
32660537Seric 	if (tTd(38, 2))
32760537Seric 		printf("map_init(%s:%s)\n",
32860537Seric 			map->map_class->map_cname, map->map_file);
32960537Seric 
33060537Seric 	/* if already open, close it (for nested open) */
33160537Seric 	if (bitset(MF_OPEN, map->map_mflags))
33260537Seric 	{
33360537Seric 		map->map_class->map_close(map);
33460537Seric 		map->map_mflags &= ~(MF_OPEN|MF_WRITABLE);
33560537Seric 	}
33660537Seric 
33760537Seric 	if (rebuild)
33860537Seric 	{
33960926Seric 		if (bitset(MF_ALIAS, map->map_mflags) &&
34060926Seric 		    bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
34160537Seric 			rebuildaliases(map, FALSE);
34260537Seric 	}
34360537Seric 	else
34460537Seric 	{
34560537Seric 		if (map->map_class->map_open(map, O_RDONLY))
34660537Seric 		{
34760537Seric 			if (tTd(38, 4))
34860537Seric 				printf("%s:%s: valid\n",
34960537Seric 					map->map_class->map_cname,
35060537Seric 					map->map_file);
35160537Seric 			map->map_mflags |= MF_OPEN;
35260537Seric 		}
35360537Seric 		else if (tTd(38, 4))
35460537Seric 			printf("%s:%s: invalid: %s\n",
35560537Seric 				map->map_class->map_cname,
35660537Seric 				map->map_file,
35760537Seric 				errstring(errno));
35860537Seric 	}
35960537Seric }
36060537Seric /*
36160089Seric **  NDBM modules
36260089Seric */
36360089Seric 
36460089Seric #ifdef NDBM
36560089Seric 
36660089Seric /*
36760089Seric **  DBM_MAP_OPEN -- DBM-style map open
36860089Seric */
36960089Seric 
37060089Seric bool
37160089Seric ndbm_map_open(map, mode)
37260089Seric 	MAP *map;
37360089Seric 	int mode;
37460089Seric {
37560089Seric 	DBM *dbm;
37660089Seric 
37760537Seric 	if (tTd(38, 2))
37860089Seric 		printf("ndbm_map_open(%s, %d)\n", map->map_file, mode);
37960089Seric 
38060207Seric 	if (mode == O_RDWR)
38160207Seric 		mode |= O_CREAT|O_TRUNC;
38260207Seric 
38360089Seric 	/* open the database */
38460089Seric 	dbm = dbm_open(map->map_file, mode, DBMMODE);
38556822Seric 	if (dbm == NULL)
38656822Seric 	{
38760207Seric 		if (!bitset(MF_OPTIONAL, map->map_mflags))
38856836Seric 			syserr("Cannot open DBM database %s", map->map_file);
38956822Seric 		return FALSE;
39056822Seric 	}
39160089Seric 	map->map_db1 = (void *) dbm;
39260207Seric 	if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags))
39360604Seric 		aliaswait(map, ".pag");
39456822Seric 	return TRUE;
39556822Seric }
39660089Seric 
39760089Seric 
39860089Seric /*
39956822Seric **  DBM_MAP_LOOKUP -- look up a datum in a DBM-type map
40056822Seric */
40156822Seric 
40256822Seric char *
40360089Seric ndbm_map_lookup(map, name, av, statp)
40456822Seric 	MAP *map;
40560089Seric 	char *name;
40656822Seric 	char **av;
40759084Seric 	int *statp;
40856822Seric {
40956822Seric 	datum key, val;
41060089Seric 	char keybuf[MAXNAME + 1];
41156822Seric 
41260537Seric 	if (tTd(38, 20))
41360089Seric 		printf("ndbm_map_lookup(%s)\n", name);
41460089Seric 
41560089Seric 	key.dptr = name;
41660089Seric 	key.dsize = strlen(name);
41760207Seric 	if (!bitset(MF_NOFOLDCASE, map->map_mflags))
41857014Seric 	{
41960089Seric 		if (key.dsize > sizeof keybuf - 1)
42060089Seric 			key.dsize = sizeof keybuf - 1;
42160089Seric 		bcopy(key.dptr, keybuf, key.dsize + 1);
42260089Seric 		makelower(keybuf);
42360089Seric 		key.dptr = keybuf;
42457014Seric 	}
42563753Seric 	(void) lockfile(dbm_dirfno((DBM *) map->map_db1), map->map_file, LOCK_SH);
42663753Seric 	val.dptr = NULL;
42763753Seric 	if (bitset(MF_TRY0NULL, map->map_mflags))
42863753Seric 	{
42963753Seric 		val = dbm_fetch((DBM *) map->map_db1, key);
43063753Seric 		if (val.dptr != NULL)
43163753Seric 			map->map_mflags &= ~MF_TRY1NULL;
43263753Seric 	}
43363753Seric 	if (val.dptr == NULL && bitset(MF_TRY1NULL, map->map_mflags))
43463753Seric 	{
43556822Seric 		key.dsize++;
43663753Seric 		val = dbm_fetch((DBM *) map->map_db1, key);
43763753Seric 		if (val.dptr != NULL)
43863753Seric 			map->map_mflags &= ~MF_TRY0NULL;
43963753Seric 	}
44060089Seric 	(void) lockfile(dbm_dirfno((DBM *) map->map_db1), map->map_file, LOCK_UN);
44156822Seric 	if (val.dptr == NULL)
44256822Seric 		return NULL;
44360207Seric 	if (bitset(MF_MATCHONLY, map->map_mflags))
44463753Seric 		return map_rewrite(map, name, strlen(name), NULL);
44563753Seric 	else
44663753Seric 		return map_rewrite(map, val.dptr, val.dsize, av);
44756822Seric }
44856822Seric 
44956822Seric 
45056822Seric /*
45160089Seric **  DBM_MAP_STORE -- store a datum in the database
45256822Seric */
45356822Seric 
45460089Seric void
45560089Seric ndbm_map_store(map, lhs, rhs)
45660089Seric 	register MAP *map;
45760089Seric 	char *lhs;
45860089Seric 	char *rhs;
45960089Seric {
46060089Seric 	datum key;
46160089Seric 	datum data;
46260089Seric 	int stat;
46360089Seric 
46460537Seric 	if (tTd(38, 12))
46560089Seric 		printf("ndbm_map_store(%s, %s)\n", lhs, rhs);
46660089Seric 
46760089Seric 	key.dsize = strlen(lhs);
46860089Seric 	key.dptr = lhs;
46960089Seric 
47060089Seric 	data.dsize = strlen(rhs);
47160089Seric 	data.dptr = rhs;
47260089Seric 
47360207Seric 	if (bitset(MF_INCLNULL, map->map_mflags))
47460089Seric 	{
47560089Seric 		key.dsize++;
47660089Seric 		data.dsize++;
47760089Seric 	}
47860089Seric 
47960089Seric 	stat = dbm_store((DBM *) map->map_db1, key, data, DBM_INSERT);
48060089Seric 	if (stat > 0)
48160089Seric 	{
48260089Seric 		usrerr("050 Warning: duplicate alias name %s", lhs);
48360089Seric 		stat = dbm_store((DBM *) map->map_db1, key, data, DBM_REPLACE);
48460089Seric 	}
48560089Seric 	if (stat != 0)
48660089Seric 		syserr("readaliases: dbm put (%s)", lhs);
48760089Seric }
48860089Seric 
48960089Seric 
49060089Seric /*
49160207Seric **  NDBM_MAP_CLOSE -- close the database
49260089Seric */
49360089Seric 
49460089Seric void
49560089Seric ndbm_map_close(map)
49660089Seric 	register MAP  *map;
49760089Seric {
49860207Seric 	if (bitset(MF_WRITABLE, map->map_mflags))
49960089Seric 	{
50060089Seric #ifdef YPCOMPAT
50160089Seric 		char buf[200];
50260089Seric 
50360089Seric 		(void) sprintf(buf, "%010ld", curtime());
50460089Seric 		ndbm_map_store(map, "YP_LAST_MODIFIED", buf);
50560089Seric 
50660089Seric 		(void) myhostname(buf, sizeof buf);
50760089Seric 		ndbm_map_store(map, "YP_MASTER_NAME", buf);
50860089Seric #endif
50960089Seric 
51060089Seric 		/* write out the distinguished alias */
51160089Seric 		ndbm_map_store(map, "@", "@");
51260089Seric 	}
51360089Seric 	dbm_close((DBM *) map->map_db1);
51460089Seric }
51560089Seric 
51660089Seric #endif
51760089Seric /*
51860582Seric **  NEWDB (Hash and BTree) Modules
51960089Seric */
52060089Seric 
52160089Seric #ifdef NEWDB
52260089Seric 
52360089Seric /*
52460582Seric **  BT_MAP_OPEN, HASH_MAP_OPEN -- database open primitives.
52560582Seric **
52660582Seric **	These do rather bizarre locking.  If you can lock on open,
52760582Seric **	do that to avoid the condition of opening a database that
52860582Seric **	is being rebuilt.  If you don't, we'll try to fake it, but
52960582Seric **	there will be a race condition.  If opening for read-only,
53060582Seric **	we immediately release the lock to avoid freezing things up.
53160582Seric **	We really ought to hold the lock, but guarantee that we won't
53260582Seric **	be pokey about it.  That's hard to do.
53360089Seric */
53460089Seric 
53556822Seric bool
53660089Seric bt_map_open(map, mode)
53756822Seric 	MAP *map;
53860089Seric 	int mode;
53956822Seric {
54056822Seric 	DB *db;
54160228Seric 	int i;
54260582Seric 	int omode;
54360089Seric 	char buf[MAXNAME];
54456822Seric 
54560537Seric 	if (tTd(38, 2))
54660089Seric 		printf("bt_map_open(%s, %d)\n", map->map_file, mode);
54760089Seric 
54860582Seric 	omode = mode;
54960582Seric 	if (omode == O_RDWR)
55060582Seric 	{
55160582Seric 		omode |= O_CREAT|O_TRUNC;
55260582Seric #if defined(O_EXLOCK) && !defined(LOCKF)
55360582Seric 		omode |= O_EXLOCK;
55460582Seric # if !defined(OLD_NEWDB)
55560582Seric 	}
55660582Seric 	else
55760582Seric 	{
55860582Seric 		omode |= O_SHLOCK;
55960582Seric # endif
56060582Seric #endif
56160582Seric 	}
56260207Seric 
56360228Seric 	(void) strcpy(buf, map->map_file);
56460228Seric 	i = strlen(buf);
56560228Seric 	if (i < 3 || strcmp(&buf[i - 3], ".db") != 0)
56660228Seric 		(void) strcat(buf, ".db");
56760582Seric 	db = dbopen(buf, omode, DBMMODE, DB_BTREE, NULL);
56856822Seric 	if (db == NULL)
56956822Seric 	{
57060207Seric 		if (!bitset(MF_OPTIONAL, map->map_mflags))
57156836Seric 			syserr("Cannot open BTREE database %s", map->map_file);
57256822Seric 		return FALSE;
57356822Seric 	}
57460582Seric #if !defined(OLD_NEWDB) && !defined(LOCKF)
57560582Seric # if !defined(O_EXLOCK)
57660582Seric 	if (mode == O_RDWR)
57760582Seric 		(void) lockfile(db->fd(db), map->map_file, LOCK_EX);
57860582Seric # else
57960582Seric 	if (mode == O_RDONLY)
58060582Seric 		(void) lockfile(db->fd(db), map->map_file, LOCK_UN);
58160582Seric # endif
58260582Seric #endif
58360585Seric 
58460585Seric 	/* try to make sure that at least the database header is on disk */
58560585Seric 	if (mode == O_RDWR)
58660585Seric 		(void) db->sync(db, 0);
58760585Seric 
58860089Seric 	map->map_db2 = (void *) db;
58960207Seric 	if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags))
59060207Seric 		aliaswait(map, ".db");
59156822Seric 	return TRUE;
59256822Seric }
59356822Seric 
59456822Seric 
59556822Seric /*
59656822Seric **  HASH_MAP_INIT -- HASH-style map initialization
59756822Seric */
59856822Seric 
59956822Seric bool
60060089Seric hash_map_open(map, mode)
60156822Seric 	MAP *map;
60260089Seric 	int mode;
60356822Seric {
60456822Seric 	DB *db;
60560228Seric 	int i;
60660582Seric 	int omode;
60760089Seric 	char buf[MAXNAME];
60856822Seric 
60960537Seric 	if (tTd(38, 2))
61060089Seric 		printf("hash_map_open(%s, %d)\n", map->map_file, mode);
61160089Seric 
61260582Seric 	omode = mode;
61360582Seric 	if (omode == O_RDWR)
61460582Seric 	{
61560582Seric 		omode |= O_CREAT|O_TRUNC;
61660582Seric #if defined(O_EXLOCK) && !defined(LOCKF)
61760582Seric 		omode |= O_EXLOCK;
61860582Seric # if !defined(OLD_NEWDB)
61960582Seric 	}
62060582Seric 	else
62160582Seric 	{
62260582Seric 		omode |= O_SHLOCK;
62360582Seric # endif
62460582Seric #endif
62560582Seric 	}
62660207Seric 
62760228Seric 	(void) strcpy(buf, map->map_file);
62860228Seric 	i = strlen(buf);
62960228Seric 	if (i < 3 || strcmp(&buf[i - 3], ".db") != 0)
63060228Seric 		(void) strcat(buf, ".db");
63160582Seric 	db = dbopen(buf, omode, DBMMODE, DB_HASH, NULL);
63256822Seric 	if (db == NULL)
63356822Seric 	{
63460207Seric 		if (!bitset(MF_OPTIONAL, map->map_mflags))
63556836Seric 			syserr("Cannot open HASH database %s", map->map_file);
63656822Seric 		return FALSE;
63756822Seric 	}
63860582Seric #if !defined(OLD_NEWDB) && !defined(LOCKF)
63960582Seric # if !defined(O_EXLOCK)
64060582Seric 	if (mode == O_RDWR)
64160582Seric 		(void) lockfile(db->fd(db), map->map_file, LOCK_EX);
64260582Seric # else
64360582Seric 	if (mode == O_RDONLY)
64460582Seric 		(void) lockfile(db->fd(db), map->map_file, LOCK_UN);
64560582Seric # endif
64660582Seric #endif
64760585Seric 
64860585Seric 	/* try to make sure that at least the database header is on disk */
64960585Seric 	if (mode == O_RDWR)
65060585Seric 		(void) db->sync(db, 0);
65160585Seric 
65260089Seric 	map->map_db2 = (void *) db;
65360207Seric 	if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags))
65460207Seric 		aliaswait(map, ".db");
65556822Seric 	return TRUE;
65656822Seric }
65756822Seric 
65856822Seric 
65956822Seric /*
66056822Seric **  DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map
66156822Seric */
66256822Seric 
66356822Seric char *
66460089Seric db_map_lookup(map, name, av, statp)
66556822Seric 	MAP *map;
66660089Seric 	char *name;
66756822Seric 	char **av;
66859084Seric 	int *statp;
66956822Seric {
67056822Seric 	DBT key, val;
67160422Seric 	register DB *db = (DB *) map->map_db2;
67260422Seric 	int st;
67360422Seric 	int saveerrno;
67460089Seric 	char keybuf[MAXNAME + 1];
67556822Seric 
67660537Seric 	if (tTd(38, 20))
67760089Seric 		printf("db_map_lookup(%s)\n", name);
67860089Seric 
67960089Seric 	key.size = strlen(name);
68060089Seric 	if (key.size > sizeof keybuf - 1)
68160089Seric 		key.size = sizeof keybuf - 1;
68260089Seric 	key.data = keybuf;
68360089Seric 	bcopy(name, keybuf, key.size + 1);
68460207Seric 	if (!bitset(MF_NOFOLDCASE, map->map_mflags))
68560089Seric 		makelower(keybuf);
68660422Seric #ifndef OLD_NEWDB
68760422Seric 	(void) lockfile(db->fd(db), map->map_file, LOCK_SH);
68860422Seric #endif
68963753Seric 	st = 1;
69063753Seric 	if (bitset(MF_TRY0NULL, map->map_mflags))
69163753Seric 	{
69263753Seric 		st = db->get(db, &key, &val, 0);
69363753Seric 		if (st == 0)
69463753Seric 			map->map_mflags &= ~MF_TRY1NULL;
69563753Seric 	}
69663753Seric 	if (st != 0 && bitset(MF_TRY1NULL, map->map_mflags))
69763753Seric 	{
69863753Seric 		key.size++;
69963753Seric 		st = db->get(db, &key, &val, 0);
70063753Seric 		if (st == 0)
70163753Seric 			map->map_mflags &= ~MF_TRY0NULL;
70263753Seric 	}
70360422Seric 	saveerrno = errno;
70460422Seric #ifndef OLD_NEWDB
70560422Seric 	(void) lockfile(db->fd(db), map->map_file, LOCK_UN);
70660422Seric #endif
70760422Seric 	if (st != 0)
70860422Seric 	{
70960422Seric 		errno = saveerrno;
71060422Seric 		if (st < 0)
71160422Seric 			syserr("db_map_lookup: get (%s)", name);
71256822Seric 		return NULL;
71360422Seric 	}
71460207Seric 	if (bitset(MF_MATCHONLY, map->map_mflags))
71563753Seric 		return map_rewrite(map, name, strlen(name), NULL);
71663753Seric 	else
71763753Seric 		return map_rewrite(map, val.data, val.size, av);
71856822Seric }
71956822Seric 
72060089Seric 
72160089Seric /*
72260089Seric **  DB_MAP_STORE -- store a datum in the NEWDB database
72356822Seric */
72456822Seric 
72560089Seric void
72660089Seric db_map_store(map, lhs, rhs)
72760089Seric 	register MAP *map;
72860089Seric 	char *lhs;
72960089Seric 	char *rhs;
73056822Seric {
73160089Seric 	int stat;
73260089Seric 	DBT key;
73360089Seric 	DBT data;
73460089Seric 	register DB *db = map->map_db2;
73556822Seric 
73660537Seric 	if (tTd(38, 20))
73760089Seric 		printf("db_map_store(%s, %s)\n", lhs, rhs);
73860089Seric 
73960089Seric 	key.size = strlen(lhs);
74060089Seric 	key.data = lhs;
74160089Seric 
74260089Seric 	data.size = strlen(rhs);
74360089Seric 	data.data = rhs;
74460089Seric 
74560207Seric 	if (bitset(MF_INCLNULL, map->map_mflags))
74656822Seric 	{
74760089Seric 		key.size++;
74860089Seric 		data.size++;
74960089Seric 	}
75056836Seric 
75160089Seric 	stat = db->put(db, &key, &data, R_NOOVERWRITE);
75260089Seric 	if (stat > 0)
75360089Seric 	{
75460089Seric 		usrerr("050 Warning: duplicate alias name %s", lhs);
75560089Seric 		stat = db->put(db, &key, &data, 0);
75660089Seric 	}
75760089Seric 	if (stat != 0)
75860089Seric 		syserr("readaliases: db put (%s)", lhs);
75960089Seric }
76056836Seric 
76156847Seric 
76260089Seric /*
76360089Seric **  DB_MAP_CLOSE -- add distinguished entries and close the database
76460089Seric */
76560089Seric 
76660089Seric void
76760089Seric db_map_close(map)
76860089Seric 	MAP *map;
76960089Seric {
77060089Seric 	register DB *db = map->map_db2;
77160089Seric 
77260537Seric 	if (tTd(38, 9))
77360207Seric 		printf("db_map_close(%s, %x)\n", map->map_file, map->map_mflags);
77460089Seric 
77560207Seric 	if (bitset(MF_WRITABLE, map->map_mflags))
77658804Seric 	{
77760089Seric 		/* write out the distinguished alias */
77860089Seric 		db_map_store(map, "@", "@");
77958804Seric 	}
78058963Seric 
78160089Seric 	if (db->close(db) != 0)
78260089Seric 		syserr("readaliases: db close failure");
78356822Seric }
78457208Seric 
78560089Seric #endif
78660089Seric /*
78760089Seric **  NIS Modules
78860089Seric */
78960089Seric 
79060089Seric # ifdef NIS
79160089Seric 
79257208Seric /*
79360089Seric **  NIS_MAP_OPEN -- open DBM map
79457208Seric */
79557208Seric 
79657208Seric bool
79760089Seric nis_map_open(map, mode)
79857208Seric 	MAP *map;
79960089Seric 	int mode;
80057208Seric {
80157216Seric 	int yperr;
80260215Seric 	register char *p;
80360215Seric 	auto char *vp;
80460215Seric 	auto int vsize;
80557216Seric 	char *master;
80657216Seric 
80760537Seric 	if (tTd(38, 2))
80860089Seric 		printf("nis_map_open(%s)\n", map->map_file);
80960089Seric 
81060207Seric 	if (mode != O_RDONLY)
81160207Seric 	{
81260207Seric 		errno = ENODEV;
81360207Seric 		return FALSE;
81460207Seric 	}
81560207Seric 
81660089Seric 	p = strchr(map->map_file, '@');
81760089Seric 	if (p != NULL)
81860089Seric 	{
81960089Seric 		*p++ = '\0';
82060089Seric 		if (*p != '\0')
82160089Seric 			map->map_domain = p;
82260089Seric 	}
82360215Seric 
82460089Seric 	if (map->map_domain == NULL)
82560089Seric 		yp_get_default_domain(&map->map_domain);
82660089Seric 
82760089Seric 	if (*map->map_file == '\0')
82860089Seric 		map->map_file = "mail.aliases";
82960089Seric 
83060215Seric 	/* check to see if this map actually exists */
83160089Seric 	yperr = yp_match(map->map_domain, map->map_file, "@", 1,
83260089Seric 			&vp, &vsize);
83360537Seric 	if (tTd(38, 10))
83460089Seric 		printf("nis_map_open: yp_match(%s, %s) => %s\n",
83560089Seric 			map->map_domain, map->map_file, yperr_string(yperr));
83660089Seric 	if (yperr == 0 || yperr == YPERR_KEY || yperr == YPERR_BUSY)
83760089Seric 		return TRUE;
83860215Seric 
83960215Seric 	if (!bitset(MF_OPTIONAL, map->map_mflags))
84060215Seric 		syserr("Cannot bind to domain %s: %s", map->map_domain,
84160215Seric 			yperr_string(yperr));
84260215Seric 
84360089Seric 	return FALSE;
84460089Seric }
84560089Seric 
84660089Seric 
84760089Seric /*
84857208Seric **  NIS_MAP_LOOKUP -- look up a datum in a NIS map
84957208Seric */
85057208Seric 
85157208Seric char *
85260089Seric nis_map_lookup(map, name, av, statp)
85357208Seric 	MAP *map;
85460089Seric 	char *name;
85557208Seric 	char **av;
85659084Seric 	int *statp;
85757208Seric {
85857208Seric 	char *vp;
85957642Seric 	auto int vsize;
86059274Seric 	int buflen;
86160215Seric 	int yperr;
86260089Seric 	char keybuf[MAXNAME + 1];
86357208Seric 
86460537Seric 	if (tTd(38, 20))
86560089Seric 		printf("nis_map_lookup(%s)\n", name);
86660089Seric 
86760089Seric 	buflen = strlen(name);
86860089Seric 	if (buflen > sizeof keybuf - 1)
86960089Seric 		buflen = sizeof keybuf - 1;
87060089Seric 	bcopy(name, keybuf, buflen + 1);
87160207Seric 	if (!bitset(MF_NOFOLDCASE, map->map_mflags))
87260089Seric 		makelower(keybuf);
87363753Seric 	yperr = YPERR_KEY;
87463753Seric 	if (bitset(MF_TRY0NULL, map->map_mflags))
87563753Seric 	{
87663753Seric 		yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen,
87763753Seric 			     &vp, &vsize);
87863753Seric 		if (yperr == 0)
87963753Seric 			map->map_mflags &= ~MF_TRY1NULL;
88063753Seric 	}
88163753Seric 	if (yperr == YPERR_KEY && bitset(MF_TRY1NULL, map->map_mflags))
88263753Seric 	{
88359274Seric 		buflen++;
88463753Seric 		yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen,
88563753Seric 			     &vp, &vsize);
88663753Seric 		if (yperr == 0)
88763753Seric 			map->map_mflags &= ~MF_TRY0NULL;
88863753Seric 	}
88960089Seric 	if (yperr != 0)
89060089Seric 	{
89160089Seric 		if (yperr != YPERR_KEY && yperr != YPERR_BUSY)
89260215Seric 			map->map_mflags &= ~(MF_VALID|MF_OPEN);
89357208Seric 		return NULL;
89460089Seric 	}
89560207Seric 	if (bitset(MF_MATCHONLY, map->map_mflags))
89663753Seric 		return map_rewrite(map, name, strlen(name), NULL);
89763753Seric 	else
89863753Seric 		return map_rewrite(map, vp, vsize, av);
89957208Seric }
90057208Seric 
90160089Seric 
90260089Seric /*
90360207Seric **  NIS_MAP_STORE
90460089Seric */
90560089Seric 
90660089Seric void
90760089Seric nis_map_store(map, lhs, rhs)
90860089Seric 	MAP *map;
90960089Seric 	char *lhs;
91060089Seric 	char *rhs;
91160089Seric {
91260089Seric 	/* nothing */
91360089Seric }
91460089Seric 
91560089Seric 
91660089Seric /*
91760207Seric **  NIS_MAP_CLOSE
91860089Seric */
91960089Seric 
92060089Seric void
92160089Seric nis_map_close(map)
92260089Seric 	MAP *map;
92360089Seric {
92460089Seric 	/* nothing */
92560089Seric }
92660089Seric 
92760089Seric #endif /* NIS */
92857208Seric /*
92960089Seric **  STAB (Symbol Table) Modules
93060089Seric */
93160089Seric 
93260089Seric 
93360089Seric /*
93460207Seric **  STAB_MAP_LOOKUP -- look up alias in symbol table
93560089Seric */
93660089Seric 
93760089Seric char *
93861707Seric stab_map_lookup(map, name, av, pstat)
93960089Seric 	register MAP *map;
94060089Seric 	char *name;
94161707Seric 	char **av;
94261707Seric 	int *pstat;
94360089Seric {
94460089Seric 	register STAB *s;
94560089Seric 
94660537Seric 	if (tTd(38, 20))
94760089Seric 		printf("stab_lookup(%s)\n", name);
94860089Seric 
94960089Seric 	s = stab(name, ST_ALIAS, ST_FIND);
95060089Seric 	if (s != NULL)
95160089Seric 		return (s->s_alias);
95260089Seric 	return (NULL);
95360089Seric }
95460089Seric 
95560089Seric 
95660089Seric /*
95760207Seric **  STAB_MAP_STORE -- store in symtab (actually using during init, not rebuild)
95860089Seric */
95960089Seric 
96060089Seric void
96160089Seric stab_map_store(map, lhs, rhs)
96260089Seric 	register MAP *map;
96360089Seric 	char *lhs;
96460089Seric 	char *rhs;
96560089Seric {
96660089Seric 	register STAB *s;
96760089Seric 
96860089Seric 	s = stab(lhs, ST_ALIAS, ST_ENTER);
96960089Seric 	s->s_alias = newstr(rhs);
97060089Seric }
97160089Seric 
97260089Seric 
97360089Seric /*
97460207Seric **  STAB_MAP_OPEN -- initialize (reads data file)
97560207Seric **
97660207Seric **	This is a wierd case -- it is only intended as a fallback for
97760207Seric **	aliases.  For this reason, opens for write (only during a
97860207Seric **	"newaliases") always fails, and opens for read open the
97960207Seric **	actual underlying text file instead of the database.
98060089Seric */
98160089Seric 
98260089Seric bool
98360089Seric stab_map_open(map, mode)
98460089Seric 	register MAP *map;
98560089Seric 	int mode;
98660089Seric {
98763835Seric 	FILE *af;
98863835Seric 
98960537Seric 	if (tTd(38, 2))
99060089Seric 		printf("stab_map_open(%s)\n", map->map_file);
99160089Seric 
99260089Seric 	if (mode != O_RDONLY)
99360207Seric 	{
99460207Seric 		errno = ENODEV;
99560089Seric 		return FALSE;
99660207Seric 	}
99760089Seric 
99863835Seric 	af = fopen(map->map_file, "r");
99963835Seric 	if (af == NULL)
100063835Seric 		return FALSE;
100163835Seric 	readaliases(map, af, TRUE);
100263835Seric 	fclose(af);
100363835Seric 
100460089Seric 	return TRUE;
100560089Seric }
100660089Seric 
100760089Seric 
100860089Seric /*
100960207Seric **  STAB_MAP_CLOSE -- close symbol table (???)
101060089Seric */
101160089Seric 
101260089Seric void
101360089Seric stab_map_close(map)
101460089Seric 	MAP *map;
101560089Seric {
101660089Seric 	/* ignore it */
101760089Seric }
101860089Seric /*
101960089Seric **  Implicit Modules
102056822Seric **
102160089Seric **	Tries several types.  For back compatibility of aliases.
102256822Seric */
102356822Seric 
102460089Seric 
102560089Seric /*
102660207Seric **  IMPL_MAP_LOOKUP -- lookup in best open database
102760089Seric */
102860089Seric 
102960089Seric char *
103060089Seric impl_map_lookup(map, name, av, pstat)
103160089Seric 	MAP *map;
103260089Seric 	char *name;
103356822Seric 	char **av;
103460089Seric 	int *pstat;
103556822Seric {
103660537Seric 	if (tTd(38, 20))
103760089Seric 		printf("impl_map_lookup(%s)\n", name);
103856822Seric 
103960089Seric #ifdef NEWDB
104060207Seric 	if (bitset(MF_IMPL_HASH, map->map_mflags))
104160089Seric 		return db_map_lookup(map, name, av, pstat);
104260089Seric #endif
104360089Seric #ifdef NDBM
104460207Seric 	if (bitset(MF_IMPL_NDBM, map->map_mflags))
104560089Seric 		return ndbm_map_lookup(map, name, av, pstat);
104660089Seric #endif
104760089Seric 	return stab_map_lookup(map, name, av, pstat);
104860089Seric }
104960089Seric 
105060089Seric /*
105160207Seric **  IMPL_MAP_STORE -- store in open databases
105260089Seric */
105360089Seric 
105460089Seric void
105560089Seric impl_map_store(map, lhs, rhs)
105660089Seric 	MAP *map;
105760089Seric 	char *lhs;
105860089Seric 	char *rhs;
105960089Seric {
106060089Seric #ifdef NEWDB
106160207Seric 	if (bitset(MF_IMPL_HASH, map->map_mflags))
106260089Seric 		db_map_store(map, lhs, rhs);
106360089Seric #endif
106460089Seric #ifdef NDBM
106560207Seric 	if (bitset(MF_IMPL_NDBM, map->map_mflags))
106660089Seric 		ndbm_map_store(map, lhs, rhs);
106760089Seric #endif
106860089Seric 	stab_map_store(map, lhs, rhs);
106960089Seric }
107060089Seric 
107160089Seric /*
107260089Seric **  IMPL_MAP_OPEN -- implicit database open
107360089Seric */
107460089Seric 
107560089Seric bool
107660089Seric impl_map_open(map, mode)
107760089Seric 	MAP *map;
107860089Seric 	int mode;
107960089Seric {
108060089Seric 	struct stat stb;
108160089Seric 
108260537Seric 	if (tTd(38, 2))
108360089Seric 		printf("impl_map_open(%s)\n", map->map_file);
108460089Seric 
108560089Seric 	if (stat(map->map_file, &stb) < 0)
108656822Seric 	{
108760089Seric 		/* no alias file at all */
108860089Seric 		return FALSE;
108956822Seric 	}
109056822Seric 
109160089Seric #ifdef NEWDB
109260207Seric 	map->map_mflags |= MF_IMPL_HASH;
109360089Seric 	if (hash_map_open(map, mode))
109456822Seric 	{
109560207Seric #if defined(NDBM) && defined(YPCOMPAT)
109660561Seric 		if (mode == O_RDONLY || access("/var/yp/Makefile", R_OK) != 0)
109760207Seric #endif
109860207Seric 			return TRUE;
109960089Seric 	}
110060207Seric 	else
110160207Seric 		map->map_mflags &= ~MF_IMPL_HASH;
110260089Seric #endif
110360089Seric #ifdef NDBM
110460207Seric 	map->map_mflags |= MF_IMPL_NDBM;
110560089Seric 	if (ndbm_map_open(map, mode))
110660089Seric 	{
110760089Seric 		return TRUE;
110860089Seric 	}
110960207Seric 	else
111060207Seric 		map->map_mflags &= ~MF_IMPL_NDBM;
111160089Seric #endif
111256822Seric 
111360207Seric #if !defined(NEWDB) && !defined(NDBM)
111460089Seric 	if (Verbose)
111560089Seric 		message("WARNING: cannot open alias database %s", map->map_file);
111660207Seric #endif
111760089Seric 
111860207Seric 	return stab_map_open(map, mode);
111956822Seric }
112060089Seric 
112160207Seric 
112260089Seric /*
112360207Seric **  IMPL_MAP_CLOSE -- close any open database(s)
112460089Seric */
112560089Seric 
112660089Seric void
112760207Seric impl_map_close(map)
112860089Seric 	MAP *map;
112960089Seric {
113060089Seric #ifdef NEWDB
113160207Seric 	if (bitset(MF_IMPL_HASH, map->map_mflags))
113260089Seric 	{
113360207Seric 		db_map_close(map);
113460207Seric 		map->map_mflags &= ~MF_IMPL_HASH;
113560089Seric 	}
113660089Seric #endif
113760089Seric 
113860089Seric #ifdef NDBM
113960207Seric 	if (bitset(MF_IMPL_NDBM, map->map_mflags))
114060089Seric 	{
114160207Seric 		ndbm_map_close(map);
114260207Seric 		map->map_mflags &= ~MF_IMPL_NDBM;
114360089Seric 	}
114460089Seric #endif
114560089Seric }
114660207Seric /*
114760207Seric **  NULL stubs
114860089Seric */
114960089Seric 
115060207Seric bool
115160207Seric null_map_open(map, mode)
115260089Seric 	MAP *map;
115360207Seric 	int mode;
115460089Seric {
115560207Seric 	return TRUE;
115660089Seric }
115760089Seric 
115860207Seric void
115960207Seric null_map_close(map)
116060207Seric 	MAP *map;
116160089Seric {
116260207Seric 	return;
116360207Seric }
116460089Seric 
116560207Seric void
116660207Seric null_map_store(map, key, val)
116760207Seric 	MAP *map;
116860207Seric 	char *key;
116960207Seric 	char *val;
117060089Seric {
117160207Seric 	return;
117260089Seric }
1173