1N/A/*
1N/A** Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers.
1N/A** All rights reserved.
1N/A**
1N/A** By using this file, you agree to the terms and conditions set
1N/A** forth in the LICENSE file which can be found at the top level of
1N/A** the sendmail distribution.
1N/A*/
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sm/gen.h>
1N/ASM_RCSID("@(#)$Id: smdb.c,v 8.58 2004/08/03 20:58:38 ca Exp $")
1N/A
1N/A#include <fcntl.h>
1N/A#include <stdlib.h>
1N/A#include <unistd.h>
1N/A
1N/A
1N/A#include <sendmail/sendmail.h>
1N/A#include <libsmdb/smdb.h>
1N/A
1N/Astatic bool smdb_lockfile __P((int, int));
1N/A
1N/A/*
1N/A** SMDB_MALLOC_DATABASE -- Allocates a database structure.
1N/A**
1N/A** Parameters:
1N/A** None
1N/A**
1N/A** Returns:
1N/A** An pointer to an allocated SMDB_DATABASE structure or
1N/A** NULL if it couldn't allocate the memory.
1N/A*/
1N/A
1N/ASMDB_DATABASE *
1N/Asmdb_malloc_database()
1N/A{
1N/A SMDB_DATABASE *db;
1N/A
1N/A db = (SMDB_DATABASE *) malloc(sizeof(SMDB_DATABASE));
1N/A
1N/A if (db != NULL)
1N/A (void) memset(db, '\0', sizeof(SMDB_DATABASE));
1N/A
1N/A return db;
1N/A}
1N/A
1N/A
1N/A/*
1N/A** SMDB_FREE_DATABASE -- Unallocates a database structure.
1N/A**
1N/A** Parameters:
1N/A** database -- a SMDB_DATABASE pointer to deallocate.
1N/A**
1N/A** Returns:
1N/A** None
1N/A*/
1N/A
1N/Avoid
1N/Asmdb_free_database(database)
1N/A SMDB_DATABASE *database;
1N/A{
1N/A if (database != NULL)
1N/A free(database);
1N/A}
1N/A/*
1N/A** SMDB_LOCKFILE -- lock a file using flock or (shudder) fcntl locking
1N/A**
1N/A** Parameters:
1N/A** fd -- the file descriptor of the file.
1N/A** type -- type of the lock. Bits can be:
1N/A** LOCK_EX -- exclusive lock.
1N/A** LOCK_NB -- non-blocking.
1N/A**
1N/A** Returns:
1N/A** true if the lock was acquired.
1N/A** false otherwise.
1N/A*/
1N/A
1N/Astatic bool
1N/Asmdb_lockfile(fd, type)
1N/A int fd;
1N/A int type;
1N/A{
1N/A int i;
1N/A int save_errno;
1N/A#if !HASFLOCK
1N/A int action;
1N/A struct flock lfd;
1N/A
1N/A (void) memset(&lfd, '\0', sizeof lfd);
1N/A if (bitset(LOCK_UN, type))
1N/A lfd.l_type = F_UNLCK;
1N/A else if (bitset(LOCK_EX, type))
1N/A lfd.l_type = F_WRLCK;
1N/A else
1N/A lfd.l_type = F_RDLCK;
1N/A
1N/A if (bitset(LOCK_NB, type))
1N/A action = F_SETLK;
1N/A else
1N/A action = F_SETLKW;
1N/A
1N/A while ((i = fcntl(fd, action, &lfd)) < 0 && errno == EINTR)
1N/A continue;
1N/A if (i >= 0)
1N/A return true;
1N/A save_errno = errno;
1N/A
1N/A /*
1N/A ** On SunOS, if you are testing using -oQ/tmp/mqueue or
1N/A ** -oA/tmp/aliases or anything like that, and /tmp is mounted
1N/A ** as type "tmp" (that is, served from swap space), the
1N/A ** previous fcntl will fail with "Invalid argument" errors.
1N/A ** Since this is fairly common during testing, we will assume
1N/A ** that this indicates that the lock is successfully grabbed.
1N/A */
1N/A
1N/A if (save_errno == EINVAL)
1N/A return true;
1N/A
1N/A if (!bitset(LOCK_NB, type) ||
1N/A (save_errno != EACCES && save_errno != EAGAIN))
1N/A {
1N/A# if 0
1N/A int omode = fcntl(fd, F_GETFL, NULL);
1N/A int euid = (int) geteuid();
1N/A
1N/A syslog(LOG_ERR, "cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1N/A filename, ext, fd, type, omode, euid);
1N/A# endif /* 0 */
1N/A errno = save_errno;
1N/A return false;
1N/A }
1N/A#else /* !HASFLOCK */
1N/A
1N/A while ((i = flock(fd, type)) < 0 && errno == EINTR)
1N/A continue;
1N/A if (i >= 0)
1N/A return true;
1N/A save_errno = errno;
1N/A
1N/A if (!bitset(LOCK_NB, type) || save_errno != EWOULDBLOCK)
1N/A {
1N/A# if 0
1N/A int omode = fcntl(fd, F_GETFL, NULL);
1N/A int euid = (int) geteuid();
1N/A
1N/A syslog(LOG_ERR, "cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1N/A filename, ext, fd, type, omode, euid);
1N/A# endif /* 0 */
1N/A errno = save_errno;
1N/A return false;
1N/A }
1N/A#endif /* !HASFLOCK */
1N/A errno = save_errno;
1N/A return false;
1N/A}
1N/A/*
1N/A** SMDB_OPEN_DATABASE -- Opens a database.
1N/A**
1N/A** This opens a database. If type is SMDB_DEFAULT it tries to
1N/A** use a DB1 or DB2 hash. If that isn't available, it will try
1N/A** to use NDBM. If a specific type is given it will try to open
1N/A** a database of that type.
1N/A**
1N/A** Parameters:
1N/A** database -- An pointer to a SMDB_DATABASE pointer where the
1N/A** opened database will be stored. This should
1N/A** be unallocated.
1N/A** db_name -- The name of the database to open. Do not include
1N/A** the file name extension.
1N/A** mode -- The mode to set on the database file or files.
1N/A** mode_mask -- Mode bits that must match on an opened database.
1N/A** sff -- Flags to safefile.
1N/A** type -- The type of database to open. Supported types
1N/A** vary depending on what was compiled in.
1N/A** user_info -- Information on the user to use for file
1N/A** permissions.
1N/A** params -- Params specific to the database being opened.
1N/A** Only supports some DB hash options right now
1N/A** (see smdb_db_open() for details).
1N/A**
1N/A** Returns:
1N/A** SMDBE_OK -- Success.
1N/A** Anything else is an error. Look up more info about the
1N/A** error in the comments for the specific open() used.
1N/A*/
1N/A
1N/Aint
1N/Asmdb_open_database(database, db_name, mode, mode_mask, sff, type, user_info,
1N/A params)
1N/A SMDB_DATABASE **database;
1N/A char *db_name;
1N/A int mode;
1N/A int mode_mask;
1N/A long sff;
1N/A SMDB_DBTYPE type;
1N/A SMDB_USER_INFO *user_info;
1N/A SMDB_DBPARAMS *params;
1N/A{
1N/A bool type_was_default = false;
1N/A
1N/A if (type == SMDB_TYPE_DEFAULT)
1N/A {
1N/A type_was_default = true;
1N/A#ifdef NEWDB
1N/A type = SMDB_TYPE_HASH;
1N/A#else /* NEWDB */
1N/A# ifdef NDBM
1N/A type = SMDB_TYPE_NDBM;
1N/A# endif /* NDBM */
1N/A#endif /* NEWDB */
1N/A }
1N/A
1N/A if (type == SMDB_TYPE_DEFAULT)
1N/A return SMDBE_UNKNOWN_DB_TYPE;
1N/A
1N/A if ((strncmp(type, SMDB_TYPE_HASH, SMDB_TYPE_HASH_LEN) == 0) ||
1N/A (strncmp(type, SMDB_TYPE_BTREE, SMDB_TYPE_BTREE_LEN) == 0))
1N/A {
1N/A#ifdef NEWDB
1N/A int result;
1N/A
1N/A result = smdb_db_open(database, db_name, mode, mode_mask, sff,
1N/A type, user_info, params);
1N/A# ifdef NDBM
1N/A if (result == ENOENT && type_was_default)
1N/A type = SMDB_TYPE_NDBM;
1N/A else
1N/A# endif /* NDBM */
1N/A return result;
1N/A#else /* NEWDB */
1N/A return SMDBE_UNSUPPORTED_DB_TYPE;
1N/A#endif /* NEWDB */
1N/A }
1N/A
1N/A if (strncmp(type, SMDB_TYPE_NDBM, SMDB_TYPE_NDBM_LEN) == 0)
1N/A {
1N/A#ifdef NDBM
1N/A int result;
1N/A
1N/A result = smdb_ndbm_open(database, db_name, mode, mode_mask,
1N/A sff, type, user_info, params);
1N/A return result;
1N/A#else /* NDBM */
1N/A return SMDBE_UNSUPPORTED_DB_TYPE;
1N/A#endif /* NDBM */
1N/A }
1N/A
1N/A return SMDBE_UNKNOWN_DB_TYPE;
1N/A}
1N/A/*
1N/A** SMDB_ADD_EXTENSION -- Adds an extension to a file name.
1N/A**
1N/A** Just adds a . followed by a string to a db_name if there
1N/A** is room and the db_name does not already have that extension.
1N/A**
1N/A** Parameters:
1N/A** full_name -- The final file name.
1N/A** max_full_name_len -- The max length for full_name.
1N/A** db_name -- The name of the db.
1N/A** extension -- The extension to add.
1N/A**
1N/A** Returns:
1N/A** SMDBE_OK -- Success.
1N/A** Anything else is an error. Look up more info about the
1N/A** error in the comments for the specific open() used.
1N/A*/
1N/A
1N/Aint
1N/Asmdb_add_extension(full_name, max_full_name_len, db_name, extension)
1N/A char *full_name;
1N/A int max_full_name_len;
1N/A char *db_name;
1N/A char *extension;
1N/A{
1N/A int extension_len;
1N/A int db_name_len;
1N/A
1N/A if (full_name == NULL || db_name == NULL || extension == NULL)
1N/A return SMDBE_INVALID_PARAMETER;
1N/A
1N/A extension_len = strlen(extension);
1N/A db_name_len = strlen(db_name);
1N/A
1N/A if (extension_len + db_name_len + 2 > max_full_name_len)
1N/A return SMDBE_DB_NAME_TOO_LONG;
1N/A
1N/A if (db_name_len < extension_len + 1 ||
1N/A db_name[db_name_len - extension_len - 1] != '.' ||
1N/A strcmp(&db_name[db_name_len - extension_len], extension) != 0)
1N/A (void) sm_snprintf(full_name, max_full_name_len, "%s.%s",
1N/A db_name, extension);
1N/A else
1N/A (void) sm_strlcpy(full_name, db_name, max_full_name_len);
1N/A
1N/A return SMDBE_OK;
1N/A}
1N/A/*
1N/A** SMDB_LOCK_FILE -- Locks the database file.
1N/A**
1N/A** Locks the actual database file.
1N/A**
1N/A** Parameters:
1N/A** lock_fd -- The resulting descriptor for the locked file.
1N/A** db_name -- The name of the database without extension.
1N/A** mode -- The open mode.
1N/A** sff -- Flags to safefile.
1N/A** extension -- The extension for the file.
1N/A**
1N/A** Returns:
1N/A** SMDBE_OK -- Success, otherwise errno.
1N/A*/
1N/A
1N/Aint
1N/Asmdb_lock_file(lock_fd, db_name, mode, sff, extension)
1N/A int *lock_fd;
1N/A char *db_name;
1N/A int mode;
1N/A long sff;
1N/A char *extension;
1N/A{
1N/A int result;
1N/A char file_name[MAXPATHLEN];
1N/A
1N/A result = smdb_add_extension(file_name, sizeof file_name, db_name,
1N/A extension);
1N/A if (result != SMDBE_OK)
1N/A return result;
1N/A
1N/A *lock_fd = safeopen(file_name, mode & ~O_TRUNC, DBMMODE, sff);
1N/A if (*lock_fd < 0)
1N/A return errno;
1N/A
1N/A return SMDBE_OK;
1N/A}
1N/A/*
1N/A** SMDB_UNLOCK_FILE -- Unlocks a file
1N/A**
1N/A** Unlocks a file.
1N/A**
1N/A** Parameters:
1N/A** lock_fd -- The descriptor for the locked file.
1N/A**
1N/A** Returns:
1N/A** SMDBE_OK -- Success, otherwise errno.
1N/A*/
1N/A
1N/Aint
1N/Asmdb_unlock_file(lock_fd)
1N/A int lock_fd;
1N/A{
1N/A int result;
1N/A
1N/A result = close(lock_fd);
1N/A if (result != 0)
1N/A return errno;
1N/A
1N/A return SMDBE_OK;
1N/A}
1N/A/*
1N/A** SMDB_LOCK_MAP -- Locks a database.
1N/A**
1N/A** Parameters:
1N/A** database -- database description.
1N/A** type -- type of the lock. Bits can be:
1N/A** LOCK_EX -- exclusive lock.
1N/A** LOCK_NB -- non-blocking.
1N/A**
1N/A** Returns:
1N/A** SMDBE_OK -- Success, otherwise errno.
1N/A*/
1N/A
1N/Aint
1N/Asmdb_lock_map(database, type)
1N/A SMDB_DATABASE *database;
1N/A int type;
1N/A{
1N/A int fd;
1N/A
1N/A fd = database->smdb_lockfd(database);
1N/A if (fd < 0)
1N/A return SMDBE_NOT_FOUND;
1N/A if (!smdb_lockfile(fd, type))
1N/A return SMDBE_LOCK_NOT_GRANTED;
1N/A return SMDBE_OK;
1N/A}
1N/A/*
1N/A** SMDB_UNLOCK_MAP -- Unlocks a database
1N/A**
1N/A** Parameters:
1N/A** database -- database description.
1N/A**
1N/A** Returns:
1N/A** SMDBE_OK -- Success, otherwise errno.
1N/A*/
1N/A
1N/Aint
1N/Asmdb_unlock_map(database)
1N/A SMDB_DATABASE *database;
1N/A{
1N/A int fd;
1N/A
1N/A fd = database->smdb_lockfd(database);
1N/A if (fd < 0)
1N/A return SMDBE_NOT_FOUND;
1N/A if (!smdb_lockfile(fd, LOCK_UN))
1N/A return SMDBE_LOCK_NOT_HELD;
1N/A return SMDBE_OK;
1N/A}
1N/A/*
1N/A** SMDB_SETUP_FILE -- Gets db file ready for use.
1N/A**
1N/A** Makes sure permissions on file are safe and creates it if it
1N/A** doesn't exist.
1N/A**
1N/A** Parameters:
1N/A** db_name -- The name of the database without extension.
1N/A** extension -- The extension.
1N/A** sff -- Flags to safefile.
1N/A** mode_mask -- Mode bits that must match.
1N/A** user_info -- Information on the user to use for file
1N/A** permissions.
1N/A** stat_info -- A place to put the stat info for the file.
1N/A** Returns:
1N/A** SMDBE_OK -- Success, otherwise errno.
1N/A*/
1N/A
1N/Aint
1N/Asmdb_setup_file(db_name, extension, mode_mask, sff, user_info, stat_info)
1N/A char *db_name;
1N/A char *extension;
1N/A int mode_mask;
1N/A long sff;
1N/A SMDB_USER_INFO *user_info;
1N/A struct stat *stat_info;
1N/A{
1N/A int st;
1N/A int result;
1N/A char db_file_name[MAXPATHLEN];
1N/A
1N/A result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name,
1N/A extension);
1N/A if (result != SMDBE_OK)
1N/A return result;
1N/A
1N/A st = safefile(db_file_name, user_info->smdbu_id,
1N/A user_info->smdbu_group_id, user_info->smdbu_name,
1N/A sff, mode_mask, stat_info);
1N/A if (st != 0)
1N/A return st;
1N/A
1N/A return SMDBE_OK;
1N/A}
1N/A/*
1N/A** SMDB_FILECHANGED -- Checks to see if a file changed.
1N/A**
1N/A** Compares the passed in stat_info with a current stat on
1N/A** the passed in file descriptor. Check filechanged for
1N/A** return values.
1N/A**
1N/A** Parameters:
1N/A** db_name -- The name of the database without extension.
1N/A** extension -- The extension.
1N/A** db_fd -- A file descriptor for the database file.
1N/A** stat_info -- An old stat_info.
1N/A** Returns:
1N/A** SMDBE_OK -- Success, otherwise errno.
1N/A*/
1N/A
1N/Aint
1N/Asmdb_filechanged(db_name, extension, db_fd, stat_info)
1N/A char *db_name;
1N/A char *extension;
1N/A int db_fd;
1N/A struct stat *stat_info;
1N/A{
1N/A int result;
1N/A char db_file_name[MAXPATHLEN];
1N/A
1N/A result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name,
1N/A extension);
1N/A if (result != SMDBE_OK)
1N/A return result;
1N/A return filechanged(db_file_name, db_fd, stat_info);
1N/A}
1N/A/*
1N/A** SMDB_PRINT_AVAILABLE_TYPES -- Prints the names of the available types.
1N/A**
1N/A** Parameters:
1N/A** None
1N/A**
1N/A** Returns:
1N/A** None
1N/A*/
1N/A
1N/Avoid
1N/Asmdb_print_available_types()
1N/A{
1N/A#ifdef NDBM
1N/A printf("dbm\n");
1N/A#endif /* NDBM */
1N/A#ifdef NEWDB
1N/A printf("hash\n");
1N/A printf("btree\n");
1N/A#endif /* NEWDB */
1N/A}
1N/A/*
1N/A** SMDB_DB_DEFINITION -- Given a database type, return database definition
1N/A**
1N/A** Reads though a structure making an association with the database
1N/A** type and the required cpp define from sendmail/README.
1N/A** List size is dynamic and must be NULL terminated.
1N/A**
1N/A** Parameters:
1N/A** type -- The name of the database type.
1N/A**
1N/A** Returns:
1N/A** definition for type, otherwise NULL.
1N/A*/
1N/A
1N/Atypedef struct
1N/A{
1N/A SMDB_DBTYPE type;
1N/A char *dbdef;
1N/A} dbtype;
1N/A
1N/Astatic dbtype DatabaseDefs[] =
1N/A{
1N/A { SMDB_TYPE_HASH, "NEWDB" },
1N/A { SMDB_TYPE_BTREE, "NEWDB" },
1N/A { SMDB_TYPE_NDBM, "NDBM" },
1N/A { NULL, "OOPS" }
1N/A};
1N/A
1N/Achar *
1N/Asmdb_db_definition(type)
1N/A SMDB_DBTYPE type;
1N/A{
1N/A dbtype *ptr = DatabaseDefs;
1N/A
1N/A while (ptr != NULL && ptr->type != NULL)
1N/A {
1N/A if (strcmp(type, ptr->type) == 0)
1N/A return ptr->dbdef;
1N/A ptr++;
1N/A }
1N/A return NULL;
1N/A}