164562Sgshapiro/*
2261363Sgshapiro** Copyright (c) 1999-2002 Proofpoint, Inc. and its suppliers.
364562Sgshapiro**	All rights reserved.
464562Sgshapiro**
564562Sgshapiro** By using this file, you agree to the terms and conditions set
664562Sgshapiro** forth in the LICENSE file which can be found at the top level of
764562Sgshapiro** the sendmail distribution.
864562Sgshapiro*/
964562Sgshapiro
1090792Sgshapiro#include <sm/gen.h>
11266692SgshapiroSM_RCSID("@(#)$Id: smdb.c,v 8.59 2013-11-22 20:51:49 ca Exp $")
1264562Sgshapiro
1364562Sgshapiro#include <fcntl.h>
1464562Sgshapiro#include <stdlib.h>
1564562Sgshapiro#include <unistd.h>
1664562Sgshapiro
1764562Sgshapiro
1864562Sgshapiro#include <sendmail/sendmail.h>
1964562Sgshapiro#include <libsmdb/smdb.h>
2064562Sgshapiro
21141858Sgshapirostatic bool	smdb_lockfile __P((int, int));
22141858Sgshapiro
2390792Sgshapiro/*
2464562Sgshapiro** SMDB_MALLOC_DATABASE -- Allocates a database structure.
2564562Sgshapiro**
2664562Sgshapiro**	Parameters:
2764562Sgshapiro**		None
2864562Sgshapiro**
2964562Sgshapiro**	Returns:
3064562Sgshapiro**		An pointer to an allocated SMDB_DATABASE structure or
3164562Sgshapiro**		NULL if it couldn't allocate the memory.
3264562Sgshapiro*/
3364562Sgshapiro
3464562SgshapiroSMDB_DATABASE *
3564562Sgshapirosmdb_malloc_database()
3664562Sgshapiro{
3764562Sgshapiro	SMDB_DATABASE *db;
3864562Sgshapiro
3964562Sgshapiro	db = (SMDB_DATABASE *) malloc(sizeof(SMDB_DATABASE));
4064562Sgshapiro
4164562Sgshapiro	if (db != NULL)
4290792Sgshapiro		(void) memset(db, '\0', sizeof(SMDB_DATABASE));
4364562Sgshapiro
4464562Sgshapiro	return db;
4564562Sgshapiro}
4664562Sgshapiro
4764562Sgshapiro
4890792Sgshapiro/*
4964562Sgshapiro** SMDB_FREE_DATABASE -- Unallocates a database structure.
5064562Sgshapiro**
5164562Sgshapiro**	Parameters:
5264562Sgshapiro**		database -- a SMDB_DATABASE pointer to deallocate.
5364562Sgshapiro**
5464562Sgshapiro**	Returns:
5564562Sgshapiro**		None
5664562Sgshapiro*/
5764562Sgshapiro
5864562Sgshapirovoid
5964562Sgshapirosmdb_free_database(database)
6064562Sgshapiro	SMDB_DATABASE *database;
6164562Sgshapiro{
6264562Sgshapiro	if (database != NULL)
6364562Sgshapiro		free(database);
6464562Sgshapiro}
6590792Sgshapiro/*
6666494Sgshapiro**  SMDB_LOCKFILE -- lock a file using flock or (shudder) fcntl locking
6766494Sgshapiro**
6866494Sgshapiro**	Parameters:
6966494Sgshapiro**		fd -- the file descriptor of the file.
7066494Sgshapiro**		type -- type of the lock.  Bits can be:
7166494Sgshapiro**			LOCK_EX -- exclusive lock.
7266494Sgshapiro**			LOCK_NB -- non-blocking.
7366494Sgshapiro**
7466494Sgshapiro**	Returns:
7590792Sgshapiro**		true if the lock was acquired.
7690792Sgshapiro**		false otherwise.
7766494Sgshapiro*/
7864562Sgshapiro
7966494Sgshapirostatic bool
8066494Sgshapirosmdb_lockfile(fd, type)
8166494Sgshapiro	int fd;
8266494Sgshapiro	int type;
8366494Sgshapiro{
8466494Sgshapiro	int i;
8566494Sgshapiro	int save_errno;
8666494Sgshapiro#if !HASFLOCK
8766494Sgshapiro	int action;
8866494Sgshapiro	struct flock lfd;
8966494Sgshapiro
9090792Sgshapiro	(void) memset(&lfd, '\0', sizeof lfd);
9166494Sgshapiro	if (bitset(LOCK_UN, type))
9266494Sgshapiro		lfd.l_type = F_UNLCK;
9366494Sgshapiro	else if (bitset(LOCK_EX, type))
9466494Sgshapiro		lfd.l_type = F_WRLCK;
9566494Sgshapiro	else
9666494Sgshapiro		lfd.l_type = F_RDLCK;
9766494Sgshapiro
9866494Sgshapiro	if (bitset(LOCK_NB, type))
9966494Sgshapiro		action = F_SETLK;
10066494Sgshapiro	else
10166494Sgshapiro		action = F_SETLKW;
10266494Sgshapiro
10366494Sgshapiro	while ((i = fcntl(fd, action, &lfd)) < 0 && errno == EINTR)
10466494Sgshapiro		continue;
10566494Sgshapiro	if (i >= 0)
10690792Sgshapiro		return true;
10766494Sgshapiro	save_errno = errno;
10866494Sgshapiro
10966494Sgshapiro	/*
11066494Sgshapiro	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
11166494Sgshapiro	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
11266494Sgshapiro	**  as type "tmp" (that is, served from swap space), the
11366494Sgshapiro	**  previous fcntl will fail with "Invalid argument" errors.
11466494Sgshapiro	**  Since this is fairly common during testing, we will assume
11566494Sgshapiro	**  that this indicates that the lock is successfully grabbed.
11666494Sgshapiro	*/
11766494Sgshapiro
11866494Sgshapiro	if (save_errno == EINVAL)
11990792Sgshapiro		return true;
12066494Sgshapiro
12166494Sgshapiro	if (!bitset(LOCK_NB, type) ||
12266494Sgshapiro	    (save_errno != EACCES && save_errno != EAGAIN))
12366494Sgshapiro	{
12466494Sgshapiro# if 0
12594334Sgshapiro		int omode = fcntl(fd, F_GETFL, NULL);
12694334Sgshapiro		int euid = (int) geteuid();
12794334Sgshapiro
12866494Sgshapiro		syslog(LOG_ERR, "cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
12994334Sgshapiro		       filename, ext, fd, type, omode, euid);
13066494Sgshapiro# endif /* 0 */
13194334Sgshapiro		errno = save_errno;
13290792Sgshapiro		return false;
13366494Sgshapiro	}
13466494Sgshapiro#else /* !HASFLOCK */
13566494Sgshapiro
13666494Sgshapiro	while ((i = flock(fd, type)) < 0 && errno == EINTR)
13766494Sgshapiro		continue;
13866494Sgshapiro	if (i >= 0)
13990792Sgshapiro		return true;
14066494Sgshapiro	save_errno = errno;
14166494Sgshapiro
14266494Sgshapiro	if (!bitset(LOCK_NB, type) || save_errno != EWOULDBLOCK)
14366494Sgshapiro	{
14466494Sgshapiro# if 0
14594334Sgshapiro		int omode = fcntl(fd, F_GETFL, NULL);
14694334Sgshapiro		int euid = (int) geteuid();
14794334Sgshapiro
14866494Sgshapiro		syslog(LOG_ERR, "cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
14994334Sgshapiro		       filename, ext, fd, type, omode, euid);
15066494Sgshapiro# endif /* 0 */
15194334Sgshapiro		errno = save_errno;
15290792Sgshapiro		return false;
15366494Sgshapiro	}
15466494Sgshapiro#endif /* !HASFLOCK */
15566494Sgshapiro	errno = save_errno;
15690792Sgshapiro	return false;
15766494Sgshapiro}
15890792Sgshapiro/*
15964562Sgshapiro** SMDB_OPEN_DATABASE -- Opens a database.
16064562Sgshapiro**
16164562Sgshapiro**	This opens a database. If type is SMDB_DEFAULT it tries to
16264562Sgshapiro**	use a DB1 or DB2 hash. If that isn't available, it will try
16364562Sgshapiro**	to use NDBM. If a specific type is given it will try to open
16464562Sgshapiro**	a database of that type.
16564562Sgshapiro**
16664562Sgshapiro**	Parameters:
16764562Sgshapiro**		database -- An pointer to a SMDB_DATABASE pointer where the
16864562Sgshapiro**			   opened database will be stored. This should
16964562Sgshapiro**			   be unallocated.
17064562Sgshapiro**		db_name -- The name of the database to open. Do not include
17164562Sgshapiro**			  the file name extension.
17264562Sgshapiro**		mode -- The mode to set on the database file or files.
17364562Sgshapiro**		mode_mask -- Mode bits that must match on an opened database.
17464562Sgshapiro**		sff -- Flags to safefile.
17564562Sgshapiro**		type -- The type of database to open. Supported types
17664562Sgshapiro**		       vary depending on what was compiled in.
17764562Sgshapiro**		user_info -- Information on the user to use for file
17864562Sgshapiro**			    permissions.
17964562Sgshapiro**		params -- Params specific to the database being opened.
18064562Sgshapiro**			 Only supports some DB hash options right now
18164562Sgshapiro**			 (see smdb_db_open() for details).
18264562Sgshapiro**
18364562Sgshapiro**	Returns:
18464562Sgshapiro**		SMDBE_OK -- Success.
18564562Sgshapiro**		Anything else is an error. Look up more info about the
18664562Sgshapiro**		error in the comments for the specific open() used.
18764562Sgshapiro*/
18864562Sgshapiro
18964562Sgshapiroint
19064562Sgshapirosmdb_open_database(database, db_name, mode, mode_mask, sff, type, user_info,
19164562Sgshapiro		   params)
19264562Sgshapiro	SMDB_DATABASE **database;
19364562Sgshapiro	char *db_name;
19464562Sgshapiro	int mode;
19564562Sgshapiro	int mode_mask;
19664562Sgshapiro	long sff;
19764562Sgshapiro	SMDB_DBTYPE type;
19864562Sgshapiro	SMDB_USER_INFO *user_info;
19964562Sgshapiro	SMDB_DBPARAMS *params;
20064562Sgshapiro{
201285303Sgshapiro#if defined(NEWDB) && defined(NDBM)
20290792Sgshapiro	bool type_was_default = false;
203285303Sgshapiro#endif
20464562Sgshapiro
20564562Sgshapiro	if (type == SMDB_TYPE_DEFAULT)
20664562Sgshapiro	{
207285303Sgshapiro#ifdef NEWDB
208285303Sgshapiro# ifdef NDBM
20990792Sgshapiro		type_was_default = true;
210285303Sgshapiro# endif
21164562Sgshapiro		type = SMDB_TYPE_HASH;
21264562Sgshapiro#else /* NEWDB */
21364562Sgshapiro# ifdef NDBM
21464562Sgshapiro		type = SMDB_TYPE_NDBM;
21564562Sgshapiro# endif /* NDBM */
21664562Sgshapiro#endif /* NEWDB */
21764562Sgshapiro	}
21864562Sgshapiro
21964562Sgshapiro	if (type == SMDB_TYPE_DEFAULT)
22064562Sgshapiro		return SMDBE_UNKNOWN_DB_TYPE;
22164562Sgshapiro
22264562Sgshapiro	if ((strncmp(type, SMDB_TYPE_HASH, SMDB_TYPE_HASH_LEN) == 0) ||
22364562Sgshapiro	    (strncmp(type, SMDB_TYPE_BTREE, SMDB_TYPE_BTREE_LEN) == 0))
22464562Sgshapiro	{
22564562Sgshapiro#ifdef NEWDB
22690792Sgshapiro		int result;
22790792Sgshapiro
22864562Sgshapiro		result = smdb_db_open(database, db_name, mode, mode_mask, sff,
22964562Sgshapiro				      type, user_info, params);
23064562Sgshapiro# ifdef NDBM
23164562Sgshapiro		if (result == ENOENT && type_was_default)
23264562Sgshapiro			type = SMDB_TYPE_NDBM;
23364562Sgshapiro		else
23464562Sgshapiro# endif /* NDBM */
23564562Sgshapiro			return result;
23664562Sgshapiro#else /* NEWDB */
23764562Sgshapiro		return SMDBE_UNSUPPORTED_DB_TYPE;
23864562Sgshapiro#endif /* NEWDB */
23964562Sgshapiro	}
24064562Sgshapiro
24164562Sgshapiro	if (strncmp(type, SMDB_TYPE_NDBM, SMDB_TYPE_NDBM_LEN) == 0)
24264562Sgshapiro	{
24364562Sgshapiro#ifdef NDBM
24490792Sgshapiro		int result;
24590792Sgshapiro
24664562Sgshapiro		result = smdb_ndbm_open(database, db_name, mode, mode_mask,
24764562Sgshapiro					sff, type, user_info, params);
24864562Sgshapiro		return result;
24964562Sgshapiro#else /* NDBM */
25064562Sgshapiro		return SMDBE_UNSUPPORTED_DB_TYPE;
25164562Sgshapiro#endif /* NDBM */
25264562Sgshapiro	}
25364562Sgshapiro
25464562Sgshapiro	return SMDBE_UNKNOWN_DB_TYPE;
25564562Sgshapiro}
25690792Sgshapiro/*
25764562Sgshapiro** SMDB_ADD_EXTENSION -- Adds an extension to a file name.
25864562Sgshapiro**
25964562Sgshapiro**	Just adds a . followed by a string to a db_name if there
26064562Sgshapiro**	is room and the db_name does not already have that extension.
26164562Sgshapiro**
26264562Sgshapiro**	Parameters:
26364562Sgshapiro**		full_name -- The final file name.
26464562Sgshapiro**		max_full_name_len -- The max length for full_name.
26564562Sgshapiro**		db_name -- The name of the db.
26664562Sgshapiro**		extension -- The extension to add.
26764562Sgshapiro**
26864562Sgshapiro**	Returns:
26964562Sgshapiro**		SMDBE_OK -- Success.
27064562Sgshapiro**		Anything else is an error. Look up more info about the
27164562Sgshapiro**		error in the comments for the specific open() used.
27264562Sgshapiro*/
27364562Sgshapiro
27464562Sgshapiroint
27564562Sgshapirosmdb_add_extension(full_name, max_full_name_len, db_name, extension)
27664562Sgshapiro	char *full_name;
27764562Sgshapiro	int max_full_name_len;
27864562Sgshapiro	char *db_name;
27964562Sgshapiro	char *extension;
28064562Sgshapiro{
28164562Sgshapiro	int extension_len;
28264562Sgshapiro	int db_name_len;
28364562Sgshapiro
28464562Sgshapiro	if (full_name == NULL || db_name == NULL || extension == NULL)
28564562Sgshapiro		return SMDBE_INVALID_PARAMETER;
28664562Sgshapiro
28764562Sgshapiro	extension_len = strlen(extension);
28864562Sgshapiro	db_name_len = strlen(db_name);
28964562Sgshapiro
29064562Sgshapiro	if (extension_len + db_name_len + 2 > max_full_name_len)
29164562Sgshapiro		return SMDBE_DB_NAME_TOO_LONG;
29264562Sgshapiro
29364562Sgshapiro	if (db_name_len < extension_len + 1 ||
29464562Sgshapiro	    db_name[db_name_len - extension_len - 1] != '.' ||
29564562Sgshapiro	    strcmp(&db_name[db_name_len - extension_len], extension) != 0)
29690792Sgshapiro		(void) sm_snprintf(full_name, max_full_name_len, "%s.%s",
29790792Sgshapiro				   db_name, extension);
29864562Sgshapiro	else
29990792Sgshapiro		(void) sm_strlcpy(full_name, db_name, max_full_name_len);
30064562Sgshapiro
30164562Sgshapiro	return SMDBE_OK;
30264562Sgshapiro}
30390792Sgshapiro/*
30464562Sgshapiro**  SMDB_LOCK_FILE -- Locks the database file.
30564562Sgshapiro**
30664562Sgshapiro**	Locks the actual database file.
30764562Sgshapiro**
30864562Sgshapiro**	Parameters:
30964562Sgshapiro**		lock_fd -- The resulting descriptor for the locked file.
31064562Sgshapiro**		db_name -- The name of the database without extension.
31164562Sgshapiro**		mode -- The open mode.
31264562Sgshapiro**		sff -- Flags to safefile.
31364562Sgshapiro**		extension -- The extension for the file.
31464562Sgshapiro**
31564562Sgshapiro**	Returns:
31664562Sgshapiro**		SMDBE_OK -- Success, otherwise errno.
31764562Sgshapiro*/
31864562Sgshapiro
31964562Sgshapiroint
32064562Sgshapirosmdb_lock_file(lock_fd, db_name, mode, sff, extension)
32164562Sgshapiro	int *lock_fd;
32264562Sgshapiro	char *db_name;
32364562Sgshapiro	int mode;
32464562Sgshapiro	long sff;
32564562Sgshapiro	char *extension;
32664562Sgshapiro{
32764562Sgshapiro	int result;
32898121Sgshapiro	char file_name[MAXPATHLEN];
32964562Sgshapiro
33098121Sgshapiro	result = smdb_add_extension(file_name, sizeof file_name, db_name,
33164562Sgshapiro				    extension);
33264562Sgshapiro	if (result != SMDBE_OK)
33364562Sgshapiro		return result;
33464562Sgshapiro
33598121Sgshapiro	*lock_fd = safeopen(file_name, mode & ~O_TRUNC, DBMMODE, sff);
33664562Sgshapiro	if (*lock_fd < 0)
33764562Sgshapiro		return errno;
33864562Sgshapiro
33964562Sgshapiro	return SMDBE_OK;
34064562Sgshapiro}
34190792Sgshapiro/*
34264562Sgshapiro**  SMDB_UNLOCK_FILE -- Unlocks a file
34364562Sgshapiro**
34464562Sgshapiro**	Unlocks a file.
34564562Sgshapiro**
34664562Sgshapiro**	Parameters:
34764562Sgshapiro**		lock_fd -- The descriptor for the locked file.
34864562Sgshapiro**
34964562Sgshapiro**	Returns:
35064562Sgshapiro**		SMDBE_OK -- Success, otherwise errno.
35164562Sgshapiro*/
35264562Sgshapiro
35364562Sgshapiroint
35464562Sgshapirosmdb_unlock_file(lock_fd)
35564562Sgshapiro	int lock_fd;
35664562Sgshapiro{
35764562Sgshapiro	int result;
35864562Sgshapiro
35964562Sgshapiro	result = close(lock_fd);
36064562Sgshapiro	if (result != 0)
36164562Sgshapiro		return errno;
36264562Sgshapiro
36364562Sgshapiro	return SMDBE_OK;
36464562Sgshapiro}
36590792Sgshapiro/*
36666494Sgshapiro**  SMDB_LOCK_MAP -- Locks a database.
36766494Sgshapiro**
36866494Sgshapiro**	Parameters:
36966494Sgshapiro**		database -- database description.
37066494Sgshapiro**		type -- type of the lock.  Bits can be:
37166494Sgshapiro**			LOCK_EX -- exclusive lock.
37266494Sgshapiro**			LOCK_NB -- non-blocking.
37366494Sgshapiro**
37466494Sgshapiro**	Returns:
37566494Sgshapiro**		SMDBE_OK -- Success, otherwise errno.
37666494Sgshapiro*/
37766494Sgshapiro
37866494Sgshapiroint
37966494Sgshapirosmdb_lock_map(database, type)
38066494Sgshapiro	SMDB_DATABASE *database;
38166494Sgshapiro	int type;
38266494Sgshapiro{
38366494Sgshapiro	int fd;
38466494Sgshapiro
38566494Sgshapiro	fd = database->smdb_lockfd(database);
38666494Sgshapiro	if (fd < 0)
38766494Sgshapiro		return SMDBE_NOT_FOUND;
38866494Sgshapiro	if (!smdb_lockfile(fd, type))
38966494Sgshapiro		return SMDBE_LOCK_NOT_GRANTED;
39066494Sgshapiro	return SMDBE_OK;
39166494Sgshapiro}
39290792Sgshapiro/*
39366494Sgshapiro**  SMDB_UNLOCK_MAP -- Unlocks a database
39466494Sgshapiro**
39566494Sgshapiro**	Parameters:
39666494Sgshapiro**		database -- database description.
39766494Sgshapiro**
39866494Sgshapiro**	Returns:
39966494Sgshapiro**		SMDBE_OK -- Success, otherwise errno.
40066494Sgshapiro*/
40166494Sgshapiro
40266494Sgshapiroint
40366494Sgshapirosmdb_unlock_map(database)
40466494Sgshapiro	SMDB_DATABASE *database;
40566494Sgshapiro{
40666494Sgshapiro	int fd;
40766494Sgshapiro
40866494Sgshapiro	fd = database->smdb_lockfd(database);
40966494Sgshapiro	if (fd < 0)
41066494Sgshapiro		return SMDBE_NOT_FOUND;
41166494Sgshapiro	if (!smdb_lockfile(fd, LOCK_UN))
41266494Sgshapiro		return SMDBE_LOCK_NOT_HELD;
41366494Sgshapiro	return SMDBE_OK;
41466494Sgshapiro}
41590792Sgshapiro/*
41664562Sgshapiro**  SMDB_SETUP_FILE -- Gets db file ready for use.
41764562Sgshapiro**
41864562Sgshapiro**	Makes sure permissions on file are safe and creates it if it
41964562Sgshapiro**	doesn't exist.
42064562Sgshapiro**
42164562Sgshapiro**	Parameters:
42264562Sgshapiro**		db_name -- The name of the database without extension.
42364562Sgshapiro**		extension -- The extension.
42464562Sgshapiro**		sff -- Flags to safefile.
42564562Sgshapiro**		mode_mask -- Mode bits that must match.
42664562Sgshapiro**		user_info -- Information on the user to use for file
42764562Sgshapiro**			    permissions.
42864562Sgshapiro**		stat_info -- A place to put the stat info for the file.
42964562Sgshapiro**	Returns:
43064562Sgshapiro**		SMDBE_OK -- Success, otherwise errno.
43164562Sgshapiro*/
43264562Sgshapiro
43364562Sgshapiroint
43464562Sgshapirosmdb_setup_file(db_name, extension, mode_mask, sff, user_info, stat_info)
43564562Sgshapiro	char *db_name;
43664562Sgshapiro	char *extension;
43764562Sgshapiro	int mode_mask;
43864562Sgshapiro	long sff;
43964562Sgshapiro	SMDB_USER_INFO *user_info;
44064562Sgshapiro	struct stat *stat_info;
44164562Sgshapiro{
44264562Sgshapiro	int st;
44364562Sgshapiro	int result;
44498121Sgshapiro	char db_file_name[MAXPATHLEN];
44564562Sgshapiro
44698121Sgshapiro	result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name,
44764562Sgshapiro				    extension);
44864562Sgshapiro	if (result != SMDBE_OK)
44964562Sgshapiro		return result;
45064562Sgshapiro
45164562Sgshapiro	st = safefile(db_file_name, user_info->smdbu_id,
45264562Sgshapiro		      user_info->smdbu_group_id, user_info->smdbu_name,
45364562Sgshapiro		      sff, mode_mask, stat_info);
45464562Sgshapiro	if (st != 0)
45564562Sgshapiro		return st;
45664562Sgshapiro
45764562Sgshapiro	return SMDBE_OK;
45864562Sgshapiro}
45990792Sgshapiro/*
46064562Sgshapiro**  SMDB_FILECHANGED -- Checks to see if a file changed.
46164562Sgshapiro**
46264562Sgshapiro**	Compares the passed in stat_info with a current stat on
46364562Sgshapiro**	the passed in file descriptor. Check filechanged for
46464562Sgshapiro**	return values.
46564562Sgshapiro**
46664562Sgshapiro**	Parameters:
46764562Sgshapiro**		db_name -- The name of the database without extension.
46864562Sgshapiro**		extension -- The extension.
46964562Sgshapiro**		db_fd -- A file descriptor for the database file.
47064562Sgshapiro**		stat_info -- An old stat_info.
47164562Sgshapiro**	Returns:
47264562Sgshapiro**		SMDBE_OK -- Success, otherwise errno.
47364562Sgshapiro*/
47464562Sgshapiro
47564562Sgshapiroint
47664562Sgshapirosmdb_filechanged(db_name, extension, db_fd, stat_info)
47764562Sgshapiro	char *db_name;
47864562Sgshapiro	char *extension;
47964562Sgshapiro	int db_fd;
48064562Sgshapiro	struct stat *stat_info;
48164562Sgshapiro{
48264562Sgshapiro	int result;
48398121Sgshapiro	char db_file_name[MAXPATHLEN];
48464562Sgshapiro
48598121Sgshapiro	result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name,
48664562Sgshapiro				    extension);
48764562Sgshapiro	if (result != SMDBE_OK)
48864562Sgshapiro		return result;
48990792Sgshapiro	return filechanged(db_file_name, db_fd, stat_info);
49064562Sgshapiro}
49190792Sgshapiro/*
49264562Sgshapiro** SMDB_PRINT_AVAILABLE_TYPES -- Prints the names of the available types.
49364562Sgshapiro**
49464562Sgshapiro**	Parameters:
49564562Sgshapiro**		None
49664562Sgshapiro**
49764562Sgshapiro**	Returns:
49864562Sgshapiro**		None
49964562Sgshapiro*/
50064562Sgshapiro
50164562Sgshapirovoid
50264562Sgshapirosmdb_print_available_types()
50364562Sgshapiro{
50464562Sgshapiro#ifdef NDBM
50564562Sgshapiro	printf("dbm\n");
50664562Sgshapiro#endif /* NDBM */
50764562Sgshapiro#ifdef NEWDB
50864562Sgshapiro	printf("hash\n");
50964562Sgshapiro	printf("btree\n");
51064562Sgshapiro#endif /* NEWDB */
51164562Sgshapiro}
51290792Sgshapiro/*
51364562Sgshapiro** SMDB_DB_DEFINITION -- Given a database type, return database definition
51464562Sgshapiro**
51564562Sgshapiro**	Reads though a structure making an association with the database
51664562Sgshapiro**	type and the required cpp define from sendmail/README.
51764562Sgshapiro**	List size is dynamic and must be NULL terminated.
51864562Sgshapiro**
51964562Sgshapiro**	Parameters:
52064562Sgshapiro**		type -- The name of the database type.
52164562Sgshapiro**
52264562Sgshapiro**	Returns:
52364562Sgshapiro**		definition for type, otherwise NULL.
52464562Sgshapiro*/
52564562Sgshapiro
52664562Sgshapirotypedef struct
52764562Sgshapiro{
52864562Sgshapiro	SMDB_DBTYPE type;
52964562Sgshapiro	char *dbdef;
53064562Sgshapiro} dbtype;
53164562Sgshapiro
53264562Sgshapirostatic dbtype DatabaseDefs[] =
53364562Sgshapiro{
53464562Sgshapiro	{ SMDB_TYPE_HASH,	"NEWDB" },
53564562Sgshapiro	{ SMDB_TYPE_BTREE,	"NEWDB" },
53664562Sgshapiro	{ SMDB_TYPE_NDBM,	"NDBM"	},
53764562Sgshapiro	{ NULL,			"OOPS"	}
53864562Sgshapiro};
53964562Sgshapiro
54064562Sgshapirochar *
54164562Sgshapirosmdb_db_definition(type)
54264562Sgshapiro	SMDB_DBTYPE type;
54364562Sgshapiro{
54464562Sgshapiro	dbtype *ptr = DatabaseDefs;
54564562Sgshapiro
54664562Sgshapiro	while (ptr != NULL && ptr->type != NULL)
54764562Sgshapiro	{
54864562Sgshapiro		if (strcmp(type, ptr->type) == 0)
54964562Sgshapiro			return ptr->dbdef;
55064562Sgshapiro		ptr++;
55164562Sgshapiro	}
55264562Sgshapiro	return NULL;
55364562Sgshapiro}
554