xref: /csrg-svn/usr.sbin/sendmail/src/map.c (revision 66843)
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*66843Seric static char sccsid[] = "@(#)map.c	8.25 (Berkeley) 04/17/94";
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
6264718Seric 
6364718Seric extern bool	aliaswait __P((MAP *, char *, int));
6460089Seric /*
6560089Seric **  MAP_PARSEARGS -- parse config line arguments for database lookup
6660089Seric **
6760089Seric **	This is a generic version of the map_parse method.
6860089Seric **
6956822Seric **	Parameters:
7060089Seric **		map -- the map being initialized.
7160089Seric **		ap -- a pointer to the args on the config line.
7256822Seric **
7356822Seric **	Returns:
7460089Seric **		TRUE -- if everything parsed OK.
7556822Seric **		FALSE -- otherwise.
7656822Seric **
7756822Seric **	Side Effects:
7860089Seric **		null terminates the filename; stores it in map
7956822Seric */
8056822Seric 
8156822Seric bool
8260089Seric map_parseargs(map, ap)
8356822Seric 	MAP *map;
8460089Seric 	char *ap;
8556822Seric {
8660089Seric 	register char *p = ap;
8756822Seric 
8863753Seric 	map->map_mflags |= MF_TRY0NULL | MF_TRY1NULL;
8960089Seric 	for (;;)
9060089Seric 	{
9160089Seric 		while (isascii(*p) && isspace(*p))
9260089Seric 			p++;
9360089Seric 		if (*p != '-')
9460089Seric 			break;
9560089Seric 		switch (*++p)
9660089Seric 		{
9760089Seric 		  case 'N':
9860207Seric 			map->map_mflags |= MF_INCLNULL;
9963753Seric 			map->map_mflags &= ~MF_TRY0NULL;
10060089Seric 			break;
10160089Seric 
10263753Seric 		  case 'O':
10363753Seric 			map->map_mflags &= ~MF_TRY1NULL;
10463753Seric 			break;
10563753Seric 
10660089Seric 		  case 'o':
10760207Seric 			map->map_mflags |= MF_OPTIONAL;
10860089Seric 			break;
10960089Seric 
11060089Seric 		  case 'f':
11160207Seric 			map->map_mflags |= MF_NOFOLDCASE;
11260089Seric 			break;
11360089Seric 
11460089Seric 		  case 'm':
11560207Seric 			map->map_mflags |= MF_MATCHONLY;
11660089Seric 			break;
11760089Seric 
11860089Seric 		  case 'a':
11960089Seric 			map->map_app = ++p;
12060089Seric 			break;
12160089Seric 		}
12260089Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
12360089Seric 			p++;
12460089Seric 		if (*p != '\0')
12560089Seric 			*p++ = '\0';
12660089Seric 	}
12760089Seric 	if (map->map_app != NULL)
12860089Seric 		map->map_app = newstr(map->map_app);
12960089Seric 
13060089Seric 	if (*p != '\0')
13160089Seric 	{
13260089Seric 		map->map_file = p;
13360089Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
13460089Seric 			p++;
13560089Seric 		if (*p != '\0')
13660089Seric 			*p++ = '\0';
13760089Seric 		map->map_file = newstr(map->map_file);
13860089Seric 	}
13960089Seric 
14060089Seric 	while (*p != '\0' && isascii(*p) && isspace(*p))
14160089Seric 		p++;
14260089Seric 	if (*p != '\0')
14360089Seric 		map->map_rebuild = newstr(p);
14460089Seric 
14556822Seric 	if (map->map_file == NULL)
14657208Seric 	{
14760089Seric 		syserr("No file name for %s map %s",
14860089Seric 			map->map_class->map_cname, map->map_mname);
14956822Seric 		return FALSE;
15057208Seric 	}
15160089Seric 	return TRUE;
15260089Seric }
15360089Seric /*
15460089Seric **  MAP_REWRITE -- rewrite a database key, interpolating %n indications.
15560089Seric **
15660089Seric **	It also adds the map_app string.  It can be used as a utility
15760089Seric **	in the map_lookup method.
15860089Seric **
15960089Seric **	Parameters:
16060089Seric **		map -- the map that causes this.
16160089Seric **		s -- the string to rewrite, NOT necessarily null terminated.
16260089Seric **		slen -- the length of s.
16360089Seric **		av -- arguments to interpolate into buf.
16460089Seric **
16560089Seric **	Returns:
16660089Seric **		Pointer to rewritten result.
16760089Seric **
16860089Seric **	Side Effects:
16960089Seric **		none.
17060089Seric */
17160089Seric 
17260492Seric struct rwbuf
17360492Seric {
17460492Seric 	int	rwb_len;	/* size of buffer */
17560492Seric 	char	*rwb_buf;	/* ptr to buffer */
17660492Seric };
17760492Seric 
17860492Seric struct rwbuf	RwBufs[2];	/* buffers for rewriting output */
17960492Seric 
18060089Seric char *
18160089Seric map_rewrite(map, s, slen, av)
18260089Seric 	register MAP *map;
18360089Seric 	register char *s;
18460089Seric 	int slen;
18560089Seric 	char **av;
18660089Seric {
18760089Seric 	register char *bp;
18860089Seric 	register char c;
18960089Seric 	char **avp;
19060089Seric 	register char *ap;
19160492Seric 	register struct rwbuf *rwb;
19260089Seric 	int i;
19360089Seric 	int len;
19460089Seric 
19560537Seric 	if (tTd(39, 1))
19660089Seric 	{
19760256Seric 		printf("map_rewrite(%.*s), av =", slen, s);
19860256Seric 		if (av == NULL)
19960256Seric 			printf(" (nullv)");
20060256Seric 		else
20160256Seric 		{
20260256Seric 			for (avp = av; *avp != NULL; avp++)
20360256Seric 				printf("\n\t%s", *avp);
20460256Seric 		}
20560256Seric 		printf("\n");
20660089Seric 	}
20760089Seric 
20860492Seric 	rwb = RwBufs;
20960492Seric 	if (av == NULL)
21060492Seric 		rwb++;
21160492Seric 
21260089Seric 	/* count expected size of output (can safely overestimate) */
21360089Seric 	i = len = slen;
21460089Seric 	if (av != NULL)
21560089Seric 	{
21660089Seric 		bp = s;
21760089Seric 		for (i = slen; --i >= 0 && (c = *bp++) != 0; )
21860089Seric 		{
21960089Seric 			if (c != '%')
22060089Seric 				continue;
22160089Seric 			if (--i < 0)
22260089Seric 				break;
22360089Seric 			c = *bp++;
22460089Seric 			if (!(isascii(c) && isdigit(c)))
22560089Seric 				continue;
22663937Seric 			for (avp = av; --c >= '0' && *avp != NULL; avp++)
22760089Seric 				continue;
22860089Seric 			if (*avp == NULL)
22960089Seric 				continue;
23060089Seric 			len += strlen(*avp);
23160089Seric 		}
23260089Seric 	}
23360089Seric 	if (map->map_app != NULL)
23460089Seric 		len += strlen(map->map_app);
23560492Seric 	if (rwb->rwb_len < ++len)
23660089Seric 	{
23760089Seric 		/* need to malloc additional space */
23860492Seric 		rwb->rwb_len = len;
23960492Seric 		if (rwb->rwb_buf != NULL)
24060492Seric 			free(rwb->rwb_buf);
24160492Seric 		rwb->rwb_buf = xalloc(rwb->rwb_len);
24260089Seric 	}
24360089Seric 
24460492Seric 	bp = rwb->rwb_buf;
24560089Seric 	if (av == NULL)
24660089Seric 	{
24760089Seric 		bcopy(s, bp, slen);
24860089Seric 		bp += slen;
24960089Seric 	}
25060089Seric 	else
25160089Seric 	{
25260089Seric 		while (--slen >= 0 && (c = *s++) != '\0')
25360089Seric 		{
25460089Seric 			if (c != '%')
25560089Seric 			{
25660089Seric   pushc:
25760089Seric 				*bp++ = c;
25860089Seric 				continue;
25960089Seric 			}
26060089Seric 			if (--slen < 0 || (c = *s++) == '\0')
26160089Seric 				c = '%';
26260089Seric 			if (c == '%')
26360089Seric 				goto pushc;
26460089Seric 			if (!(isascii(c) && isdigit(c)))
26560089Seric 			{
26660089Seric 				*bp++ = '%';
26760089Seric 				goto pushc;
26860089Seric 			}
26963937Seric 			for (avp = av; --c >= '0' && *avp != NULL; avp++)
27060089Seric 				continue;
27160089Seric 			if (*avp == NULL)
27260089Seric 				continue;
27360089Seric 
27460089Seric 			/* transliterate argument into output string */
27560089Seric 			for (ap = *avp; (c = *ap++) != '\0'; )
27660089Seric 				*bp++ = c;
27760089Seric 		}
27860089Seric 	}
27960089Seric 	if (map->map_app != NULL)
28060089Seric 		strcpy(bp, map->map_app);
28160089Seric 	else
28260089Seric 		*bp = '\0';
28360537Seric 	if (tTd(39, 1))
28460492Seric 		printf("map_rewrite => %s\n", rwb->rwb_buf);
28560492Seric 	return rwb->rwb_buf;
28660089Seric }
28760089Seric /*
28860537Seric **  INITMAPS -- initialize for aliasing
28960537Seric **
29060537Seric **	Parameters:
29160537Seric **		rebuild -- if TRUE, this rebuilds the cached versions.
29260537Seric **		e -- current envelope.
29360537Seric **
29460537Seric **	Returns:
29560537Seric **		none.
29660537Seric **
29760537Seric **	Side Effects:
29860537Seric **		initializes aliases:
29960537Seric **		if NDBM:  opens the database.
30060537Seric **		if ~NDBM: reads the aliases into the symbol table.
30160537Seric */
30260537Seric 
30360537Seric initmaps(rebuild, e)
30460537Seric 	bool rebuild;
30560537Seric 	register ENVELOPE *e;
30660537Seric {
30760537Seric 	extern void map_init();
30860537Seric 
30964671Seric #ifdef XDEBUG
31064671Seric 	checkfd012("entering initmaps");
31164671Seric #endif
31260537Seric 	CurEnv = e;
31365085Seric 	if (rebuild)
31465085Seric 	{
31565085Seric 		stabapply(map_init, 1);
31665085Seric 		stabapply(map_init, 2);
31765085Seric 	}
31865085Seric 	else
31965085Seric 	{
32065085Seric 		stabapply(map_init, 0);
32165085Seric 	}
32264671Seric #ifdef XDEBUG
32364671Seric 	checkfd012("exiting initmaps");
32464671Seric #endif
32560537Seric }
32660537Seric 
32760537Seric void
32860537Seric map_init(s, rebuild)
32960537Seric 	register STAB *s;
33060537Seric 	int rebuild;
33160537Seric {
33260537Seric 	register MAP *map;
33360537Seric 
33460537Seric 	/* has to be a map */
33560537Seric 	if (s->s_type != ST_MAP)
33660537Seric 		return;
33760537Seric 
33860537Seric 	map = &s->s_map;
33960537Seric 	if (!bitset(MF_VALID, map->map_mflags))
34060537Seric 		return;
34160537Seric 
34260537Seric 	if (tTd(38, 2))
34365085Seric 		printf("map_init(%s:%s, %d)\n",
34464690Seric 			map->map_class->map_cname == NULL ? "NULL" :
34564690Seric 				map->map_class->map_cname,
34665085Seric 			map->map_file == NULL ? "NULL" : map->map_file,
34765085Seric 			rebuild);
34860537Seric 
34965085Seric 	if (rebuild == (bitset(MF_ALIAS, map->map_mflags) &&
35065085Seric 		    bitset(MCF_REBUILDABLE, map->map_class->map_cflags) ? 1 : 2))
35165085Seric 	{
35265085Seric 		if (tTd(38, 3))
35365085Seric 			printf("\twrong pass\n");
35465085Seric 		return;
35565085Seric 	}
35665085Seric 
35760537Seric 	/* if already open, close it (for nested open) */
35860537Seric 	if (bitset(MF_OPEN, map->map_mflags))
35960537Seric 	{
36060537Seric 		map->map_class->map_close(map);
36160537Seric 		map->map_mflags &= ~(MF_OPEN|MF_WRITABLE);
36260537Seric 	}
36360537Seric 
36465085Seric 	if (rebuild == 2)
36560537Seric 	{
36665085Seric 		rebuildaliases(map, FALSE);
36760537Seric 	}
36860537Seric 	else
36960537Seric 	{
37060537Seric 		if (map->map_class->map_open(map, O_RDONLY))
37160537Seric 		{
37260537Seric 			if (tTd(38, 4))
37365085Seric 				printf("\t%s:%s: valid\n",
37464690Seric 					map->map_class->map_cname == NULL ? "NULL" :
37564690Seric 						map->map_class->map_cname,
37664690Seric 					map->map_file == NULL ? "NULL" :
37764690Seric 						map->map_file);
37860537Seric 			map->map_mflags |= MF_OPEN;
37960537Seric 		}
38060537Seric 		else if (tTd(38, 4))
38165085Seric 			printf("\t%s:%s: invalid: %s\n",
38264690Seric 				map->map_class->map_cname == NULL ? "NULL" :
38364690Seric 					map->map_class->map_cname,
38464690Seric 				map->map_file == NULL ? "NULL" :
38564690Seric 					map->map_file,
38660537Seric 				errstring(errno));
38760537Seric 	}
38860537Seric }
38960537Seric /*
39060089Seric **  NDBM modules
39160089Seric */
39260089Seric 
39360089Seric #ifdef NDBM
39460089Seric 
39560089Seric /*
39660089Seric **  DBM_MAP_OPEN -- DBM-style map open
39760089Seric */
39860089Seric 
39960089Seric bool
40060089Seric ndbm_map_open(map, mode)
40160089Seric 	MAP *map;
40260089Seric 	int mode;
40360089Seric {
40464284Seric 	register DBM *dbm;
40564284Seric 	struct stat st;
40660089Seric 
40760537Seric 	if (tTd(38, 2))
40860089Seric 		printf("ndbm_map_open(%s, %d)\n", map->map_file, mode);
40960089Seric 
41060207Seric 	if (mode == O_RDWR)
41160207Seric 		mode |= O_CREAT|O_TRUNC;
41260207Seric 
41360089Seric 	/* open the database */
41460089Seric 	dbm = dbm_open(map->map_file, mode, DBMMODE);
41556822Seric 	if (dbm == NULL)
41656822Seric 	{
41764718Seric #ifdef MAYBENEXTRELEASE
41864718Seric 		if (aliaswait(map, ".pag", FALSE))
41964718Seric 			return TRUE;
42064718Seric #endif
42160207Seric 		if (!bitset(MF_OPTIONAL, map->map_mflags))
42256836Seric 			syserr("Cannot open DBM database %s", map->map_file);
42356822Seric 		return FALSE;
42456822Seric 	}
42560089Seric 	map->map_db1 = (void *) dbm;
42664964Seric 	if (mode == O_RDONLY)
42764964Seric 	{
42864964Seric 		if (bitset(MF_ALIAS, map->map_mflags) &&
42964964Seric 		    !aliaswait(map, ".pag", TRUE))
43064718Seric 			return FALSE;
43164964Seric 	}
43264964Seric 	else
43364964Seric 	{
43464964Seric 		int fd;
43564964Seric 
43664964Seric 		/* exclusive lock for duration of rebuild */
43764964Seric 		fd = dbm_dirfno((DBM *) map->map_db1);
43864964Seric 		if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags) &&
43964964Seric 		    lockfile(fd, map->map_file, ".dir", LOCK_EX))
44064964Seric 			map->map_mflags |= MF_LOCKED;
44164964Seric 	}
44264718Seric 	if (fstat(dbm_dirfno((DBM *) map->map_db1), &st) >= 0)
44364284Seric 		map->map_mtime = st.st_mtime;
44456822Seric 	return TRUE;
44556822Seric }
44660089Seric 
44760089Seric 
44860089Seric /*
44956822Seric **  DBM_MAP_LOOKUP -- look up a datum in a DBM-type map
45056822Seric */
45156822Seric 
45256822Seric char *
45360089Seric ndbm_map_lookup(map, name, av, statp)
45456822Seric 	MAP *map;
45560089Seric 	char *name;
45656822Seric 	char **av;
45759084Seric 	int *statp;
45856822Seric {
45956822Seric 	datum key, val;
46064373Seric 	int fd;
46160089Seric 	char keybuf[MAXNAME + 1];
46256822Seric 
46360537Seric 	if (tTd(38, 20))
46460089Seric 		printf("ndbm_map_lookup(%s)\n", name);
46560089Seric 
46660089Seric 	key.dptr = name;
46760089Seric 	key.dsize = strlen(name);
46860207Seric 	if (!bitset(MF_NOFOLDCASE, map->map_mflags))
46957014Seric 	{
47060089Seric 		if (key.dsize > sizeof keybuf - 1)
47160089Seric 			key.dsize = sizeof keybuf - 1;
47260089Seric 		bcopy(key.dptr, keybuf, key.dsize + 1);
47360089Seric 		makelower(keybuf);
47460089Seric 		key.dptr = keybuf;
47557014Seric 	}
47664373Seric 	fd = dbm_dirfno((DBM *) map->map_db1);
47764388Seric 	if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags))
47864373Seric 		(void) lockfile(fd, map->map_file, ".dir", LOCK_SH);
47963753Seric 	val.dptr = NULL;
48063753Seric 	if (bitset(MF_TRY0NULL, map->map_mflags))
48163753Seric 	{
48263753Seric 		val = dbm_fetch((DBM *) map->map_db1, key);
48363753Seric 		if (val.dptr != NULL)
48463753Seric 			map->map_mflags &= ~MF_TRY1NULL;
48563753Seric 	}
48663753Seric 	if (val.dptr == NULL && bitset(MF_TRY1NULL, map->map_mflags))
48763753Seric 	{
48856822Seric 		key.dsize++;
48963753Seric 		val = dbm_fetch((DBM *) map->map_db1, key);
49063753Seric 		if (val.dptr != NULL)
49163753Seric 			map->map_mflags &= ~MF_TRY0NULL;
49263753Seric 	}
49364388Seric 	if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags))
49464373Seric 		(void) lockfile(fd, map->map_file, ".dir", LOCK_UN);
49556822Seric 	if (val.dptr == NULL)
49656822Seric 		return NULL;
49760207Seric 	if (bitset(MF_MATCHONLY, map->map_mflags))
49863753Seric 		return map_rewrite(map, name, strlen(name), NULL);
49963753Seric 	else
50063753Seric 		return map_rewrite(map, val.dptr, val.dsize, av);
50156822Seric }
50256822Seric 
50356822Seric 
50456822Seric /*
50560089Seric **  DBM_MAP_STORE -- store a datum in the database
50656822Seric */
50756822Seric 
50860089Seric void
50960089Seric ndbm_map_store(map, lhs, rhs)
51060089Seric 	register MAP *map;
51160089Seric 	char *lhs;
51260089Seric 	char *rhs;
51360089Seric {
51460089Seric 	datum key;
51560089Seric 	datum data;
51660089Seric 	int stat;
51760089Seric 
51860537Seric 	if (tTd(38, 12))
51960089Seric 		printf("ndbm_map_store(%s, %s)\n", lhs, rhs);
52060089Seric 
52160089Seric 	key.dsize = strlen(lhs);
52260089Seric 	key.dptr = lhs;
52360089Seric 
52460089Seric 	data.dsize = strlen(rhs);
52560089Seric 	data.dptr = rhs;
52660089Seric 
52760207Seric 	if (bitset(MF_INCLNULL, map->map_mflags))
52860089Seric 	{
52960089Seric 		key.dsize++;
53060089Seric 		data.dsize++;
53160089Seric 	}
53260089Seric 
53360089Seric 	stat = dbm_store((DBM *) map->map_db1, key, data, DBM_INSERT);
53460089Seric 	if (stat > 0)
53560089Seric 	{
53660089Seric 		usrerr("050 Warning: duplicate alias name %s", lhs);
53760089Seric 		stat = dbm_store((DBM *) map->map_db1, key, data, DBM_REPLACE);
53860089Seric 	}
53960089Seric 	if (stat != 0)
54060089Seric 		syserr("readaliases: dbm put (%s)", lhs);
54160089Seric }
54260089Seric 
54360089Seric 
54460089Seric /*
54560207Seric **  NDBM_MAP_CLOSE -- close the database
54660089Seric */
54760089Seric 
54860089Seric void
54960089Seric ndbm_map_close(map)
55060089Seric 	register MAP  *map;
55160089Seric {
55266773Seric 	if (tTd(38, 9))
55366773Seric 		printf("ndbm_map_close(%s, %x)\n", map->map_file, map->map_mflags);
55466773Seric 
55560207Seric 	if (bitset(MF_WRITABLE, map->map_mflags))
55660089Seric 	{
55764250Seric #ifdef NIS
55864075Seric 		bool inclnull;
55960089Seric 		char buf[200];
56060089Seric 
56164075Seric 		inclnull = bitset(MF_INCLNULL, map->map_mflags);
56264075Seric 		map->map_mflags &= ~MF_INCLNULL;
56364075Seric 
56460089Seric 		(void) sprintf(buf, "%010ld", curtime());
56560089Seric 		ndbm_map_store(map, "YP_LAST_MODIFIED", buf);
56660089Seric 
56764941Seric 		(void) gethostname(buf, sizeof buf);
56860089Seric 		ndbm_map_store(map, "YP_MASTER_NAME", buf);
56964075Seric 
57064075Seric 		if (inclnull)
57164075Seric 			map->map_mflags |= MF_INCLNULL;
57260089Seric #endif
57360089Seric 
57460089Seric 		/* write out the distinguished alias */
57560089Seric 		ndbm_map_store(map, "@", "@");
57660089Seric 	}
57760089Seric 	dbm_close((DBM *) map->map_db1);
57860089Seric }
57960089Seric 
58060089Seric #endif
58160089Seric /*
58260582Seric **  NEWDB (Hash and BTree) Modules
58360089Seric */
58460089Seric 
58560089Seric #ifdef NEWDB
58660089Seric 
58760089Seric /*
58860582Seric **  BT_MAP_OPEN, HASH_MAP_OPEN -- database open primitives.
58960582Seric **
59060582Seric **	These do rather bizarre locking.  If you can lock on open,
59160582Seric **	do that to avoid the condition of opening a database that
59260582Seric **	is being rebuilt.  If you don't, we'll try to fake it, but
59360582Seric **	there will be a race condition.  If opening for read-only,
59460582Seric **	we immediately release the lock to avoid freezing things up.
59560582Seric **	We really ought to hold the lock, but guarantee that we won't
59660582Seric **	be pokey about it.  That's hard to do.
59760089Seric */
59860089Seric 
59956822Seric bool
60060089Seric bt_map_open(map, mode)
60156822Seric 	MAP *map;
60260089Seric 	int mode;
60356822Seric {
60456822Seric 	DB *db;
60560228Seric 	int i;
60660582Seric 	int omode;
60764373Seric 	int fd;
60864284Seric 	struct stat st;
60960089Seric 	char buf[MAXNAME];
61056822Seric 
61160537Seric 	if (tTd(38, 2))
61260089Seric 		printf("bt_map_open(%s, %d)\n", map->map_file, mode);
61360089Seric 
61460582Seric 	omode = mode;
61560582Seric 	if (omode == O_RDWR)
61660582Seric 	{
61760582Seric 		omode |= O_CREAT|O_TRUNC;
61865830Seric #if defined(O_EXLOCK) && HASFLOCK
61960582Seric 		omode |= O_EXLOCK;
620*66843Seric # if !OLD_NEWDB
62160582Seric 	}
62260582Seric 	else
62360582Seric 	{
62460582Seric 		omode |= O_SHLOCK;
62560582Seric # endif
62660582Seric #endif
62760582Seric 	}
62860207Seric 
62960228Seric 	(void) strcpy(buf, map->map_file);
63060228Seric 	i = strlen(buf);
63160228Seric 	if (i < 3 || strcmp(&buf[i - 3], ".db") != 0)
63260228Seric 		(void) strcat(buf, ".db");
63360582Seric 	db = dbopen(buf, omode, DBMMODE, DB_BTREE, NULL);
63456822Seric 	if (db == NULL)
63556822Seric 	{
63664718Seric #ifdef MAYBENEXTRELEASE
63764718Seric 		if (aliaswait(map, ".db", FALSE))
63864718Seric 			return TRUE;
63964718Seric #endif
64060207Seric 		if (!bitset(MF_OPTIONAL, map->map_mflags))
64156836Seric 			syserr("Cannot open BTREE database %s", map->map_file);
64256822Seric 		return FALSE;
64356822Seric 	}
644*66843Seric #if !OLD_NEWDB && HASFLOCK
64564373Seric 	fd = db->fd(db);
64660582Seric # if !defined(O_EXLOCK)
64764373Seric 	if (mode == O_RDWR && fd >= 0)
64864388Seric 	{
64964388Seric 		if (lockfile(fd, map->map_file, ".db", LOCK_EX))
65064388Seric 			map->map_mflags |= MF_LOCKED;
65164388Seric 	}
65260582Seric # else
65364373Seric 	if (mode == O_RDONLY && fd >= 0)
65464373Seric 		(void) lockfile(fd, map->map_file, ".db", LOCK_UN);
65564388Seric 	else
65664388Seric 		map->map_mflags |= MF_LOCKED;
65760582Seric # endif
65860582Seric #endif
65960585Seric 
66060585Seric 	/* try to make sure that at least the database header is on disk */
66160585Seric 	if (mode == O_RDWR)
662*66843Seric #if OLD_NEWDB
66364373Seric 		(void) db->sync(db);
66464373Seric #else
66560585Seric 		(void) db->sync(db, 0);
66660585Seric 
66764373Seric 	if (fd >= 0 && fstat(fd, &st) >= 0)
66864284Seric 		map->map_mtime = st.st_mtime;
66964284Seric #endif
67064284Seric 
67160089Seric 	map->map_db2 = (void *) db;
67260207Seric 	if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags))
67364718Seric 		if (!aliaswait(map, ".db", TRUE))
67464718Seric 			return FALSE;
67556822Seric 	return TRUE;
67656822Seric }
67756822Seric 
67856822Seric 
67956822Seric /*
68056822Seric **  HASH_MAP_INIT -- HASH-style map initialization
68156822Seric */
68256822Seric 
68356822Seric bool
68460089Seric hash_map_open(map, mode)
68556822Seric 	MAP *map;
68660089Seric 	int mode;
68756822Seric {
68856822Seric 	DB *db;
68960228Seric 	int i;
69060582Seric 	int omode;
69164373Seric 	int fd;
69264284Seric 	struct stat st;
69360089Seric 	char buf[MAXNAME];
69456822Seric 
69560537Seric 	if (tTd(38, 2))
69660089Seric 		printf("hash_map_open(%s, %d)\n", map->map_file, mode);
69760089Seric 
69860582Seric 	omode = mode;
69960582Seric 	if (omode == O_RDWR)
70060582Seric 	{
70160582Seric 		omode |= O_CREAT|O_TRUNC;
70265830Seric #if defined(O_EXLOCK) && HASFLOCK
70360582Seric 		omode |= O_EXLOCK;
704*66843Seric # if !OLD_NEWDB
70560582Seric 	}
70660582Seric 	else
70760582Seric 	{
70860582Seric 		omode |= O_SHLOCK;
70960582Seric # endif
71060582Seric #endif
71160582Seric 	}
71260207Seric 
71360228Seric 	(void) strcpy(buf, map->map_file);
71460228Seric 	i = strlen(buf);
71560228Seric 	if (i < 3 || strcmp(&buf[i - 3], ".db") != 0)
71660228Seric 		(void) strcat(buf, ".db");
71760582Seric 	db = dbopen(buf, omode, DBMMODE, DB_HASH, NULL);
71856822Seric 	if (db == NULL)
71956822Seric 	{
72064718Seric #ifdef MAYBENEXTRELEASE
72164718Seric 		if (aliaswait(map, ".db", FALSE))
72264718Seric 			return TRUE;
72364718Seric #endif
72460207Seric 		if (!bitset(MF_OPTIONAL, map->map_mflags))
72556836Seric 			syserr("Cannot open HASH database %s", map->map_file);
72656822Seric 		return FALSE;
72756822Seric 	}
728*66843Seric #if !OLD_NEWDB && HASFLOCK
72964373Seric 	fd = db->fd(db);
73060582Seric # if !defined(O_EXLOCK)
73164373Seric 	if (mode == O_RDWR && fd >= 0)
73264388Seric 	{
73364388Seric 		if (lockfile(fd, map->map_file, ".db", LOCK_EX))
73464388Seric 			map->map_mflags |= MF_LOCKED;
73564388Seric 	}
73660582Seric # else
73764373Seric 	if (mode == O_RDONLY && fd >= 0)
73864373Seric 		(void) lockfile(fd, map->map_file, ".db", LOCK_UN);
73964388Seric 	else
74064388Seric 		map->map_mflags |= MF_LOCKED;
74160582Seric # endif
74260582Seric #endif
74360585Seric 
74460585Seric 	/* try to make sure that at least the database header is on disk */
74560585Seric 	if (mode == O_RDWR)
746*66843Seric #if OLD_NEWDB
74764373Seric 		(void) db->sync(db);
74864373Seric #else
74960585Seric 		(void) db->sync(db, 0);
75060585Seric 
75164373Seric 	if (fd >= 0 && fstat(fd, &st) >= 0)
75264284Seric 		map->map_mtime = st.st_mtime;
75364284Seric #endif
75464284Seric 
75560089Seric 	map->map_db2 = (void *) db;
75660207Seric 	if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags))
75764718Seric 		if (!aliaswait(map, ".db", TRUE))
75864718Seric 			return FALSE;
75956822Seric 	return TRUE;
76056822Seric }
76156822Seric 
76256822Seric 
76356822Seric /*
76456822Seric **  DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map
76556822Seric */
76656822Seric 
76756822Seric char *
76860089Seric db_map_lookup(map, name, av, statp)
76956822Seric 	MAP *map;
77060089Seric 	char *name;
77156822Seric 	char **av;
77259084Seric 	int *statp;
77356822Seric {
77456822Seric 	DBT key, val;
77560422Seric 	register DB *db = (DB *) map->map_db2;
77660422Seric 	int st;
77760422Seric 	int saveerrno;
77864373Seric 	int fd;
77960089Seric 	char keybuf[MAXNAME + 1];
78056822Seric 
78160537Seric 	if (tTd(38, 20))
78260089Seric 		printf("db_map_lookup(%s)\n", name);
78360089Seric 
78460089Seric 	key.size = strlen(name);
78560089Seric 	if (key.size > sizeof keybuf - 1)
78660089Seric 		key.size = sizeof keybuf - 1;
78760089Seric 	key.data = keybuf;
78860089Seric 	bcopy(name, keybuf, key.size + 1);
78960207Seric 	if (!bitset(MF_NOFOLDCASE, map->map_mflags))
79060089Seric 		makelower(keybuf);
791*66843Seric #if !OLD_NEWDB
79264388Seric 	fd = db->fd(db);
79364388Seric 	if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags))
79464388Seric 		(void) lockfile(db->fd(db), map->map_file, ".db", LOCK_SH);
79560422Seric #endif
79663753Seric 	st = 1;
79763753Seric 	if (bitset(MF_TRY0NULL, map->map_mflags))
79863753Seric 	{
79963753Seric 		st = db->get(db, &key, &val, 0);
80063753Seric 		if (st == 0)
80163753Seric 			map->map_mflags &= ~MF_TRY1NULL;
80263753Seric 	}
80363753Seric 	if (st != 0 && bitset(MF_TRY1NULL, map->map_mflags))
80463753Seric 	{
80563753Seric 		key.size++;
80663753Seric 		st = db->get(db, &key, &val, 0);
80763753Seric 		if (st == 0)
80863753Seric 			map->map_mflags &= ~MF_TRY0NULL;
80963753Seric 	}
81060422Seric 	saveerrno = errno;
811*66843Seric #if !OLD_NEWDB
81264388Seric 	if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags))
81364373Seric 		(void) lockfile(fd, map->map_file, ".db", LOCK_UN);
81460422Seric #endif
81560422Seric 	if (st != 0)
81660422Seric 	{
81760422Seric 		errno = saveerrno;
81860422Seric 		if (st < 0)
81960422Seric 			syserr("db_map_lookup: get (%s)", name);
82056822Seric 		return NULL;
82160422Seric 	}
82260207Seric 	if (bitset(MF_MATCHONLY, map->map_mflags))
82363753Seric 		return map_rewrite(map, name, strlen(name), NULL);
82463753Seric 	else
82563753Seric 		return map_rewrite(map, val.data, val.size, av);
82656822Seric }
82756822Seric 
82860089Seric 
82960089Seric /*
83060089Seric **  DB_MAP_STORE -- store a datum in the NEWDB database
83156822Seric */
83256822Seric 
83360089Seric void
83460089Seric db_map_store(map, lhs, rhs)
83560089Seric 	register MAP *map;
83660089Seric 	char *lhs;
83760089Seric 	char *rhs;
83856822Seric {
83960089Seric 	int stat;
84060089Seric 	DBT key;
84160089Seric 	DBT data;
84260089Seric 	register DB *db = map->map_db2;
84356822Seric 
84460537Seric 	if (tTd(38, 20))
84560089Seric 		printf("db_map_store(%s, %s)\n", lhs, rhs);
84660089Seric 
84760089Seric 	key.size = strlen(lhs);
84860089Seric 	key.data = lhs;
84960089Seric 
85060089Seric 	data.size = strlen(rhs);
85160089Seric 	data.data = rhs;
85260089Seric 
85360207Seric 	if (bitset(MF_INCLNULL, map->map_mflags))
85456822Seric 	{
85560089Seric 		key.size++;
85660089Seric 		data.size++;
85760089Seric 	}
85856836Seric 
85960089Seric 	stat = db->put(db, &key, &data, R_NOOVERWRITE);
86060089Seric 	if (stat > 0)
86160089Seric 	{
86260089Seric 		usrerr("050 Warning: duplicate alias name %s", lhs);
86360089Seric 		stat = db->put(db, &key, &data, 0);
86460089Seric 	}
86560089Seric 	if (stat != 0)
86660089Seric 		syserr("readaliases: db put (%s)", lhs);
86760089Seric }
86856836Seric 
86956847Seric 
87060089Seric /*
87160089Seric **  DB_MAP_CLOSE -- add distinguished entries and close the database
87260089Seric */
87360089Seric 
87460089Seric void
87560089Seric db_map_close(map)
87660089Seric 	MAP *map;
87760089Seric {
87860089Seric 	register DB *db = map->map_db2;
87960089Seric 
88060537Seric 	if (tTd(38, 9))
88160207Seric 		printf("db_map_close(%s, %x)\n", map->map_file, map->map_mflags);
88260089Seric 
88360207Seric 	if (bitset(MF_WRITABLE, map->map_mflags))
88458804Seric 	{
88560089Seric 		/* write out the distinguished alias */
88660089Seric 		db_map_store(map, "@", "@");
88758804Seric 	}
88858963Seric 
88960089Seric 	if (db->close(db) != 0)
89060089Seric 		syserr("readaliases: db close failure");
89156822Seric }
89257208Seric 
89360089Seric #endif
89460089Seric /*
89560089Seric **  NIS Modules
89660089Seric */
89760089Seric 
89860089Seric # ifdef NIS
89960089Seric 
90064369Seric # ifndef YPERR_BUSY
90164369Seric #  define YPERR_BUSY	16
90264369Seric # endif
90364369Seric 
90457208Seric /*
90560089Seric **  NIS_MAP_OPEN -- open DBM map
90657208Seric */
90757208Seric 
90857208Seric bool
90960089Seric nis_map_open(map, mode)
91057208Seric 	MAP *map;
91160089Seric 	int mode;
91257208Seric {
91357216Seric 	int yperr;
91460215Seric 	register char *p;
91560215Seric 	auto char *vp;
91660215Seric 	auto int vsize;
91757216Seric 	char *master;
91857216Seric 
91960537Seric 	if (tTd(38, 2))
92060089Seric 		printf("nis_map_open(%s)\n", map->map_file);
92160089Seric 
92260207Seric 	if (mode != O_RDONLY)
92360207Seric 	{
92464650Seric 		/* issue a pseudo-error message */
92564650Seric #ifdef ENOSYS
92664650Seric 		errno = ENOSYS;
92764650Seric #else
92864650Seric # ifdef EFTYPE
92964650Seric 		errno = EFTYPE;
93064650Seric # else
93164650Seric 		errno = ENXIO;
93264650Seric # endif
93364650Seric #endif
93460207Seric 		return FALSE;
93560207Seric 	}
93660207Seric 
93760089Seric 	p = strchr(map->map_file, '@');
93860089Seric 	if (p != NULL)
93960089Seric 	{
94060089Seric 		*p++ = '\0';
94160089Seric 		if (*p != '\0')
94260089Seric 			map->map_domain = p;
94360089Seric 	}
94460215Seric 
94560089Seric 	if (*map->map_file == '\0')
94660089Seric 		map->map_file = "mail.aliases";
94760089Seric 
94866157Seric 	if (map->map_domain == NULL)
94966157Seric 	{
95066157Seric 		yperr = yp_get_default_domain(&map->map_domain);
95166157Seric 		if (yperr != 0)
95266157Seric 		{
95366744Seric 			if (!bitset(MF_OPTIONAL, map->map_mflags))
95466744Seric 				syserr("NIS map %s specified, but NIS not running\n",
95566744Seric 					map->map_file);
95666157Seric 			return FALSE;
95766157Seric 		}
95866157Seric 	}
95966157Seric 
96060215Seric 	/* check to see if this map actually exists */
96160089Seric 	yperr = yp_match(map->map_domain, map->map_file, "@", 1,
96260089Seric 			&vp, &vsize);
96360537Seric 	if (tTd(38, 10))
96460089Seric 		printf("nis_map_open: yp_match(%s, %s) => %s\n",
96560089Seric 			map->map_domain, map->map_file, yperr_string(yperr));
96660089Seric 	if (yperr == 0 || yperr == YPERR_KEY || yperr == YPERR_BUSY)
96760089Seric 		return TRUE;
96860215Seric 
96960215Seric 	if (!bitset(MF_OPTIONAL, map->map_mflags))
97060215Seric 		syserr("Cannot bind to domain %s: %s", map->map_domain,
97160215Seric 			yperr_string(yperr));
97260215Seric 
97360089Seric 	return FALSE;
97460089Seric }
97560089Seric 
97660089Seric 
97760089Seric /*
97857208Seric **  NIS_MAP_LOOKUP -- look up a datum in a NIS map
97957208Seric */
98057208Seric 
98157208Seric char *
98260089Seric nis_map_lookup(map, name, av, statp)
98357208Seric 	MAP *map;
98460089Seric 	char *name;
98557208Seric 	char **av;
98659084Seric 	int *statp;
98757208Seric {
98857208Seric 	char *vp;
98957642Seric 	auto int vsize;
99059274Seric 	int buflen;
99160215Seric 	int yperr;
99260089Seric 	char keybuf[MAXNAME + 1];
99357208Seric 
99460537Seric 	if (tTd(38, 20))
99560089Seric 		printf("nis_map_lookup(%s)\n", name);
99660089Seric 
99760089Seric 	buflen = strlen(name);
99860089Seric 	if (buflen > sizeof keybuf - 1)
99960089Seric 		buflen = sizeof keybuf - 1;
100060089Seric 	bcopy(name, keybuf, buflen + 1);
100160207Seric 	if (!bitset(MF_NOFOLDCASE, map->map_mflags))
100260089Seric 		makelower(keybuf);
100363753Seric 	yperr = YPERR_KEY;
100463753Seric 	if (bitset(MF_TRY0NULL, map->map_mflags))
100563753Seric 	{
100663753Seric 		yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen,
100763753Seric 			     &vp, &vsize);
100863753Seric 		if (yperr == 0)
100963753Seric 			map->map_mflags &= ~MF_TRY1NULL;
101063753Seric 	}
101163753Seric 	if (yperr == YPERR_KEY && bitset(MF_TRY1NULL, map->map_mflags))
101263753Seric 	{
101359274Seric 		buflen++;
101463753Seric 		yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen,
101563753Seric 			     &vp, &vsize);
101663753Seric 		if (yperr == 0)
101763753Seric 			map->map_mflags &= ~MF_TRY0NULL;
101863753Seric 	}
101960089Seric 	if (yperr != 0)
102060089Seric 	{
102160089Seric 		if (yperr != YPERR_KEY && yperr != YPERR_BUSY)
102260215Seric 			map->map_mflags &= ~(MF_VALID|MF_OPEN);
102357208Seric 		return NULL;
102460089Seric 	}
102560207Seric 	if (bitset(MF_MATCHONLY, map->map_mflags))
102663753Seric 		return map_rewrite(map, name, strlen(name), NULL);
102763753Seric 	else
102863753Seric 		return map_rewrite(map, vp, vsize, av);
102957208Seric }
103057208Seric 
103160089Seric 
103260089Seric /*
103360207Seric **  NIS_MAP_STORE
103460089Seric */
103560089Seric 
103660089Seric void
103760089Seric nis_map_store(map, lhs, rhs)
103860089Seric 	MAP *map;
103960089Seric 	char *lhs;
104060089Seric 	char *rhs;
104160089Seric {
104260089Seric 	/* nothing */
104360089Seric }
104460089Seric 
104560089Seric 
104660089Seric /*
104760207Seric **  NIS_MAP_CLOSE
104860089Seric */
104960089Seric 
105060089Seric void
105160089Seric nis_map_close(map)
105260089Seric 	MAP *map;
105360089Seric {
105460089Seric 	/* nothing */
105560089Seric }
105660089Seric 
105760089Seric #endif /* NIS */
105857208Seric /*
105960089Seric **  STAB (Symbol Table) Modules
106060089Seric */
106160089Seric 
106260089Seric 
106360089Seric /*
106460207Seric **  STAB_MAP_LOOKUP -- look up alias in symbol table
106560089Seric */
106660089Seric 
106760089Seric char *
106861707Seric stab_map_lookup(map, name, av, pstat)
106960089Seric 	register MAP *map;
107060089Seric 	char *name;
107161707Seric 	char **av;
107261707Seric 	int *pstat;
107360089Seric {
107460089Seric 	register STAB *s;
107560089Seric 
107660537Seric 	if (tTd(38, 20))
107760089Seric 		printf("stab_lookup(%s)\n", name);
107860089Seric 
107960089Seric 	s = stab(name, ST_ALIAS, ST_FIND);
108060089Seric 	if (s != NULL)
108160089Seric 		return (s->s_alias);
108260089Seric 	return (NULL);
108360089Seric }
108460089Seric 
108560089Seric 
108660089Seric /*
108760207Seric **  STAB_MAP_STORE -- store in symtab (actually using during init, not rebuild)
108860089Seric */
108960089Seric 
109060089Seric void
109160089Seric stab_map_store(map, lhs, rhs)
109260089Seric 	register MAP *map;
109360089Seric 	char *lhs;
109460089Seric 	char *rhs;
109560089Seric {
109660089Seric 	register STAB *s;
109760089Seric 
109860089Seric 	s = stab(lhs, ST_ALIAS, ST_ENTER);
109960089Seric 	s->s_alias = newstr(rhs);
110060089Seric }
110160089Seric 
110260089Seric 
110360089Seric /*
110460207Seric **  STAB_MAP_OPEN -- initialize (reads data file)
110560207Seric **
110660207Seric **	This is a wierd case -- it is only intended as a fallback for
110760207Seric **	aliases.  For this reason, opens for write (only during a
110860207Seric **	"newaliases") always fails, and opens for read open the
110960207Seric **	actual underlying text file instead of the database.
111060089Seric */
111160089Seric 
111260089Seric bool
111360089Seric stab_map_open(map, mode)
111460089Seric 	register MAP *map;
111560089Seric 	int mode;
111660089Seric {
111763835Seric 	FILE *af;
111864284Seric 	struct stat st;
111963835Seric 
112060537Seric 	if (tTd(38, 2))
112160089Seric 		printf("stab_map_open(%s)\n", map->map_file);
112260089Seric 
112360089Seric 	if (mode != O_RDONLY)
112460207Seric 	{
112560207Seric 		errno = ENODEV;
112660089Seric 		return FALSE;
112760207Seric 	}
112860089Seric 
112963835Seric 	af = fopen(map->map_file, "r");
113063835Seric 	if (af == NULL)
113163835Seric 		return FALSE;
113263835Seric 	readaliases(map, af, TRUE);
113364284Seric 
113464284Seric 	if (fstat(fileno(af), &st) >= 0)
113564284Seric 		map->map_mtime = st.st_mtime;
113663835Seric 	fclose(af);
113763835Seric 
113860089Seric 	return TRUE;
113960089Seric }
114060089Seric 
114160089Seric 
114260089Seric /*
114364642Seric **  STAB_MAP_CLOSE -- close symbol table.
114464642Seric **
114564642Seric **	Since this is in memory, there is nothing to do.
114660089Seric */
114760089Seric 
114860089Seric void
114960089Seric stab_map_close(map)
115060089Seric 	MAP *map;
115160089Seric {
115260089Seric 	/* ignore it */
115360089Seric }
115460089Seric /*
115560089Seric **  Implicit Modules
115656822Seric **
115760089Seric **	Tries several types.  For back compatibility of aliases.
115856822Seric */
115956822Seric 
116060089Seric 
116160089Seric /*
116260207Seric **  IMPL_MAP_LOOKUP -- lookup in best open database
116360089Seric */
116460089Seric 
116560089Seric char *
116660089Seric impl_map_lookup(map, name, av, pstat)
116760089Seric 	MAP *map;
116860089Seric 	char *name;
116956822Seric 	char **av;
117060089Seric 	int *pstat;
117156822Seric {
117260537Seric 	if (tTd(38, 20))
117360089Seric 		printf("impl_map_lookup(%s)\n", name);
117456822Seric 
117560089Seric #ifdef NEWDB
117660207Seric 	if (bitset(MF_IMPL_HASH, map->map_mflags))
117760089Seric 		return db_map_lookup(map, name, av, pstat);
117860089Seric #endif
117960089Seric #ifdef NDBM
118060207Seric 	if (bitset(MF_IMPL_NDBM, map->map_mflags))
118160089Seric 		return ndbm_map_lookup(map, name, av, pstat);
118260089Seric #endif
118360089Seric 	return stab_map_lookup(map, name, av, pstat);
118460089Seric }
118560089Seric 
118660089Seric /*
118760207Seric **  IMPL_MAP_STORE -- store in open databases
118860089Seric */
118960089Seric 
119060089Seric void
119160089Seric impl_map_store(map, lhs, rhs)
119260089Seric 	MAP *map;
119360089Seric 	char *lhs;
119460089Seric 	char *rhs;
119560089Seric {
119660089Seric #ifdef NEWDB
119760207Seric 	if (bitset(MF_IMPL_HASH, map->map_mflags))
119860089Seric 		db_map_store(map, lhs, rhs);
119960089Seric #endif
120060089Seric #ifdef NDBM
120160207Seric 	if (bitset(MF_IMPL_NDBM, map->map_mflags))
120260089Seric 		ndbm_map_store(map, lhs, rhs);
120360089Seric #endif
120460089Seric 	stab_map_store(map, lhs, rhs);
120560089Seric }
120660089Seric 
120760089Seric /*
120860089Seric **  IMPL_MAP_OPEN -- implicit database open
120960089Seric */
121060089Seric 
121160089Seric bool
121260089Seric impl_map_open(map, mode)
121360089Seric 	MAP *map;
121460089Seric 	int mode;
121560089Seric {
121660089Seric 	struct stat stb;
121760089Seric 
121860537Seric 	if (tTd(38, 2))
121964718Seric 		printf("impl_map_open(%s, %d)\n", map->map_file, mode);
122060089Seric 
122160089Seric 	if (stat(map->map_file, &stb) < 0)
122256822Seric 	{
122360089Seric 		/* no alias file at all */
122464718Seric 		if (tTd(38, 3))
122564718Seric 			printf("no map file\n");
122660089Seric 		return FALSE;
122756822Seric 	}
122856822Seric 
122960089Seric #ifdef NEWDB
123060207Seric 	map->map_mflags |= MF_IMPL_HASH;
123160089Seric 	if (hash_map_open(map, mode))
123256822Seric 	{
123364250Seric #if defined(NDBM) && defined(NIS)
123460561Seric 		if (mode == O_RDONLY || access("/var/yp/Makefile", R_OK) != 0)
123560207Seric #endif
123660207Seric 			return TRUE;
123760089Seric 	}
123860207Seric 	else
123960207Seric 		map->map_mflags &= ~MF_IMPL_HASH;
124060089Seric #endif
124160089Seric #ifdef NDBM
124260207Seric 	map->map_mflags |= MF_IMPL_NDBM;
124360089Seric 	if (ndbm_map_open(map, mode))
124460089Seric 	{
124560089Seric 		return TRUE;
124660089Seric 	}
124760207Seric 	else
124860207Seric 		map->map_mflags &= ~MF_IMPL_NDBM;
124960089Seric #endif
125056822Seric 
125164650Seric #if defined(NEWDB) || defined(NDBM)
125260089Seric 	if (Verbose)
125360089Seric 		message("WARNING: cannot open alias database %s", map->map_file);
125464964Seric #else
125564964Seric 	if (mode != O_RDONLY)
125664964Seric 		usrerr("Cannot rebuild aliases: no database format defined");
125760207Seric #endif
125860089Seric 
125960207Seric 	return stab_map_open(map, mode);
126056822Seric }
126160089Seric 
126260207Seric 
126360089Seric /*
126460207Seric **  IMPL_MAP_CLOSE -- close any open database(s)
126560089Seric */
126660089Seric 
126760089Seric void
126860207Seric impl_map_close(map)
126960089Seric 	MAP *map;
127060089Seric {
127160089Seric #ifdef NEWDB
127260207Seric 	if (bitset(MF_IMPL_HASH, map->map_mflags))
127360089Seric 	{
127460207Seric 		db_map_close(map);
127560207Seric 		map->map_mflags &= ~MF_IMPL_HASH;
127660089Seric 	}
127760089Seric #endif
127860089Seric 
127960089Seric #ifdef NDBM
128060207Seric 	if (bitset(MF_IMPL_NDBM, map->map_mflags))
128160089Seric 	{
128260207Seric 		ndbm_map_close(map);
128360207Seric 		map->map_mflags &= ~MF_IMPL_NDBM;
128460089Seric 	}
128560089Seric #endif
128660089Seric }
128760207Seric /*
128860207Seric **  NULL stubs
128960089Seric */
129060089Seric 
129160207Seric bool
129260207Seric null_map_open(map, mode)
129360089Seric 	MAP *map;
129460207Seric 	int mode;
129560089Seric {
129660207Seric 	return TRUE;
129760089Seric }
129860089Seric 
129960207Seric void
130060207Seric null_map_close(map)
130160207Seric 	MAP *map;
130260089Seric {
130360207Seric 	return;
130460207Seric }
130560089Seric 
130660207Seric void
130760207Seric null_map_store(map, key, val)
130860207Seric 	MAP *map;
130960207Seric 	char *key;
131060207Seric 	char *val;
131160089Seric {
131260207Seric 	return;
131360089Seric }
1314