diff options
Diffstat (limited to 'usr/src/cmd/sendmail/libsmdb')
| -rw-r--r-- | usr/src/cmd/sendmail/libsmdb/Makefile | 64 | ||||
| -rw-r--r-- | usr/src/cmd/sendmail/libsmdb/smdb.c | 551 | ||||
| -rw-r--r-- | usr/src/cmd/sendmail/libsmdb/smdb1.c | 577 | ||||
| -rw-r--r-- | usr/src/cmd/sendmail/libsmdb/smdb2.c | 700 | ||||
| -rw-r--r-- | usr/src/cmd/sendmail/libsmdb/smndbm.c | 629 |
5 files changed, 2521 insertions, 0 deletions
diff --git a/usr/src/cmd/sendmail/libsmdb/Makefile b/usr/src/cmd/sendmail/libsmdb/Makefile new file mode 100644 index 0000000000..687255b9df --- /dev/null +++ b/usr/src/cmd/sendmail/libsmdb/Makefile @@ -0,0 +1,64 @@ +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License, Version 1.0 only +# (the "License"). You may not use this file except in compliance +# with the License. +# +# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +# or http://www.opensolaris.org/os/licensing. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at usr/src/OPENSOLARIS.LICENSE. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# +# +# Copyright 2004 Sun Microsystems, Inc. All rights reserved. +# Use is subject to license terms. +# +# ident "%Z%%M% %I% %E% SMI" +# +# cmd/sendmail/libsmdb/Makefile +# + +include ../../Makefile.cmd +include ../Makefile.cmd + +INCPATH= -I. -I../src -I../include -I../db +ENVDEF= -DNEWDB -DNDBM +CPPFLAGS= $(INCPATH) $(RLS_DEF) $(ENVDEF) $(CPPFLAGS.master) + +ARFLAGS= cq + +OBJS= smdb.o smdb1.o smdb2.o smndbm.o + +SRCS= $(OBJS:%.o=%.c) + +libsmdb= libsmdb.a + +.KEEP_STATE: +all: $(libsmdb) + +.PARALLEL: $(OBJS) + +$(libsmdb): $(OBJS) + $(RM) $@ + $(AR) $(ARFLAGS) $@ $(OBJS) + +clean: + $(RM) $(OBJS) $(libsmdb) + +depend obj: + +install: all + +lint: lint_SRCS + +include ../../Makefile.targ diff --git a/usr/src/cmd/sendmail/libsmdb/smdb.c b/usr/src/cmd/sendmail/libsmdb/smdb.c new file mode 100644 index 0000000000..d65b083c37 --- /dev/null +++ b/usr/src/cmd/sendmail/libsmdb/smdb.c @@ -0,0 +1,551 @@ +/* +** Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers. +** All rights reserved. +** +** By using this file, you agree to the terms and conditions set +** forth in the LICENSE file which can be found at the top level of +** the sendmail distribution. +*/ + +#pragma ident "%Z%%M% %I% %E% SMI" + +#include <sm/gen.h> +SM_RCSID("@(#)$Id: smdb.c,v 8.58 2004/08/03 20:58:38 ca Exp $") + +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + + +#include <sendmail/sendmail.h> +#include <libsmdb/smdb.h> + +static bool smdb_lockfile __P((int, int)); + +/* +** SMDB_MALLOC_DATABASE -- Allocates a database structure. +** +** Parameters: +** None +** +** Returns: +** An pointer to an allocated SMDB_DATABASE structure or +** NULL if it couldn't allocate the memory. +*/ + +SMDB_DATABASE * +smdb_malloc_database() +{ + SMDB_DATABASE *db; + + db = (SMDB_DATABASE *) malloc(sizeof(SMDB_DATABASE)); + + if (db != NULL) + (void) memset(db, '\0', sizeof(SMDB_DATABASE)); + + return db; +} + + +/* +** SMDB_FREE_DATABASE -- Unallocates a database structure. +** +** Parameters: +** database -- a SMDB_DATABASE pointer to deallocate. +** +** Returns: +** None +*/ + +void +smdb_free_database(database) + SMDB_DATABASE *database; +{ + if (database != NULL) + free(database); +} +/* +** SMDB_LOCKFILE -- lock a file using flock or (shudder) fcntl locking +** +** Parameters: +** fd -- the file descriptor of the file. +** type -- type of the lock. Bits can be: +** LOCK_EX -- exclusive lock. +** LOCK_NB -- non-blocking. +** +** Returns: +** true if the lock was acquired. +** false otherwise. +*/ + +static bool +smdb_lockfile(fd, type) + int fd; + int type; +{ + int i; + int save_errno; +#if !HASFLOCK + int action; + struct flock lfd; + + (void) memset(&lfd, '\0', sizeof lfd); + if (bitset(LOCK_UN, type)) + lfd.l_type = F_UNLCK; + else if (bitset(LOCK_EX, type)) + lfd.l_type = F_WRLCK; + else + lfd.l_type = F_RDLCK; + + if (bitset(LOCK_NB, type)) + action = F_SETLK; + else + action = F_SETLKW; + + while ((i = fcntl(fd, action, &lfd)) < 0 && errno == EINTR) + continue; + if (i >= 0) + return true; + save_errno = errno; + + /* + ** On SunOS, if you are testing using -oQ/tmp/mqueue or + ** -oA/tmp/aliases or anything like that, and /tmp is mounted + ** as type "tmp" (that is, served from swap space), the + ** previous fcntl will fail with "Invalid argument" errors. + ** Since this is fairly common during testing, we will assume + ** that this indicates that the lock is successfully grabbed. + */ + + if (save_errno == EINVAL) + return true; + + if (!bitset(LOCK_NB, type) || + (save_errno != EACCES && save_errno != EAGAIN)) + { +# if 0 + int omode = fcntl(fd, F_GETFL, NULL); + int euid = (int) geteuid(); + + syslog(LOG_ERR, "cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", + filename, ext, fd, type, omode, euid); +# endif /* 0 */ + errno = save_errno; + return false; + } +#else /* !HASFLOCK */ + + while ((i = flock(fd, type)) < 0 && errno == EINTR) + continue; + if (i >= 0) + return true; + save_errno = errno; + + if (!bitset(LOCK_NB, type) || save_errno != EWOULDBLOCK) + { +# if 0 + int omode = fcntl(fd, F_GETFL, NULL); + int euid = (int) geteuid(); + + syslog(LOG_ERR, "cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", + filename, ext, fd, type, omode, euid); +# endif /* 0 */ + errno = save_errno; + return false; + } +#endif /* !HASFLOCK */ + errno = save_errno; + return false; +} +/* +** SMDB_OPEN_DATABASE -- Opens a database. +** +** This opens a database. If type is SMDB_DEFAULT it tries to +** use a DB1 or DB2 hash. If that isn't available, it will try +** to use NDBM. If a specific type is given it will try to open +** a database of that type. +** +** Parameters: +** database -- An pointer to a SMDB_DATABASE pointer where the +** opened database will be stored. This should +** be unallocated. +** db_name -- The name of the database to open. Do not include +** the file name extension. +** mode -- The mode to set on the database file or files. +** mode_mask -- Mode bits that must match on an opened database. +** sff -- Flags to safefile. +** type -- The type of database to open. Supported types +** vary depending on what was compiled in. +** user_info -- Information on the user to use for file +** permissions. +** params -- Params specific to the database being opened. +** Only supports some DB hash options right now +** (see smdb_db_open() for details). +** +** Returns: +** SMDBE_OK -- Success. +** Anything else is an error. Look up more info about the +** error in the comments for the specific open() used. +*/ + +int +smdb_open_database(database, db_name, mode, mode_mask, sff, type, user_info, + params) + SMDB_DATABASE **database; + char *db_name; + int mode; + int mode_mask; + long sff; + SMDB_DBTYPE type; + SMDB_USER_INFO *user_info; + SMDB_DBPARAMS *params; +{ + bool type_was_default = false; + + if (type == SMDB_TYPE_DEFAULT) + { + type_was_default = true; +#ifdef NEWDB + type = SMDB_TYPE_HASH; +#else /* NEWDB */ +# ifdef NDBM + type = SMDB_TYPE_NDBM; +# endif /* NDBM */ +#endif /* NEWDB */ + } + + if (type == SMDB_TYPE_DEFAULT) + return SMDBE_UNKNOWN_DB_TYPE; + + if ((strncmp(type, SMDB_TYPE_HASH, SMDB_TYPE_HASH_LEN) == 0) || + (strncmp(type, SMDB_TYPE_BTREE, SMDB_TYPE_BTREE_LEN) == 0)) + { +#ifdef NEWDB + int result; + + result = smdb_db_open(database, db_name, mode, mode_mask, sff, + type, user_info, params); +# ifdef NDBM + if (result == ENOENT && type_was_default) + type = SMDB_TYPE_NDBM; + else +# endif /* NDBM */ + return result; +#else /* NEWDB */ + return SMDBE_UNSUPPORTED_DB_TYPE; +#endif /* NEWDB */ + } + + if (strncmp(type, SMDB_TYPE_NDBM, SMDB_TYPE_NDBM_LEN) == 0) + { +#ifdef NDBM + int result; + + result = smdb_ndbm_open(database, db_name, mode, mode_mask, + sff, type, user_info, params); + return result; +#else /* NDBM */ + return SMDBE_UNSUPPORTED_DB_TYPE; +#endif /* NDBM */ + } + + return SMDBE_UNKNOWN_DB_TYPE; +} +/* +** SMDB_ADD_EXTENSION -- Adds an extension to a file name. +** +** Just adds a . followed by a string to a db_name if there +** is room and the db_name does not already have that extension. +** +** Parameters: +** full_name -- The final file name. +** max_full_name_len -- The max length for full_name. +** db_name -- The name of the db. +** extension -- The extension to add. +** +** Returns: +** SMDBE_OK -- Success. +** Anything else is an error. Look up more info about the +** error in the comments for the specific open() used. +*/ + +int +smdb_add_extension(full_name, max_full_name_len, db_name, extension) + char *full_name; + int max_full_name_len; + char *db_name; + char *extension; +{ + int extension_len; + int db_name_len; + + if (full_name == NULL || db_name == NULL || extension == NULL) + return SMDBE_INVALID_PARAMETER; + + extension_len = strlen(extension); + db_name_len = strlen(db_name); + + if (extension_len + db_name_len + 2 > max_full_name_len) + return SMDBE_DB_NAME_TOO_LONG; + + if (db_name_len < extension_len + 1 || + db_name[db_name_len - extension_len - 1] != '.' || + strcmp(&db_name[db_name_len - extension_len], extension) != 0) + (void) sm_snprintf(full_name, max_full_name_len, "%s.%s", + db_name, extension); + else + (void) sm_strlcpy(full_name, db_name, max_full_name_len); + + return SMDBE_OK; +} +/* +** SMDB_LOCK_FILE -- Locks the database file. +** +** Locks the actual database file. +** +** Parameters: +** lock_fd -- The resulting descriptor for the locked file. +** db_name -- The name of the database without extension. +** mode -- The open mode. +** sff -- Flags to safefile. +** extension -- The extension for the file. +** +** Returns: +** SMDBE_OK -- Success, otherwise errno. +*/ + +int +smdb_lock_file(lock_fd, db_name, mode, sff, extension) + int *lock_fd; + char *db_name; + int mode; + long sff; + char *extension; +{ + int result; + char file_name[MAXPATHLEN]; + + result = smdb_add_extension(file_name, sizeof file_name, db_name, + extension); + if (result != SMDBE_OK) + return result; + + *lock_fd = safeopen(file_name, mode & ~O_TRUNC, DBMMODE, sff); + if (*lock_fd < 0) + return errno; + + return SMDBE_OK; +} +/* +** SMDB_UNLOCK_FILE -- Unlocks a file +** +** Unlocks a file. +** +** Parameters: +** lock_fd -- The descriptor for the locked file. +** +** Returns: +** SMDBE_OK -- Success, otherwise errno. +*/ + +int +smdb_unlock_file(lock_fd) + int lock_fd; +{ + int result; + + result = close(lock_fd); + if (result != 0) + return errno; + + return SMDBE_OK; +} +/* +** SMDB_LOCK_MAP -- Locks a database. +** +** Parameters: +** database -- database description. +** type -- type of the lock. Bits can be: +** LOCK_EX -- exclusive lock. +** LOCK_NB -- non-blocking. +** +** Returns: +** SMDBE_OK -- Success, otherwise errno. +*/ + +int +smdb_lock_map(database, type) + SMDB_DATABASE *database; + int type; +{ + int fd; + + fd = database->smdb_lockfd(database); + if (fd < 0) + return SMDBE_NOT_FOUND; + if (!smdb_lockfile(fd, type)) + return SMDBE_LOCK_NOT_GRANTED; + return SMDBE_OK; +} +/* +** SMDB_UNLOCK_MAP -- Unlocks a database +** +** Parameters: +** database -- database description. +** +** Returns: +** SMDBE_OK -- Success, otherwise errno. +*/ + +int +smdb_unlock_map(database) + SMDB_DATABASE *database; +{ + int fd; + + fd = database->smdb_lockfd(database); + if (fd < 0) + return SMDBE_NOT_FOUND; + if (!smdb_lockfile(fd, LOCK_UN)) + return SMDBE_LOCK_NOT_HELD; + return SMDBE_OK; +} +/* +** SMDB_SETUP_FILE -- Gets db file ready for use. +** +** Makes sure permissions on file are safe and creates it if it +** doesn't exist. +** +** Parameters: +** db_name -- The name of the database without extension. +** extension -- The extension. +** sff -- Flags to safefile. +** mode_mask -- Mode bits that must match. +** user_info -- Information on the user to use for file +** permissions. +** stat_info -- A place to put the stat info for the file. +** Returns: +** SMDBE_OK -- Success, otherwise errno. +*/ + +int +smdb_setup_file(db_name, extension, mode_mask, sff, user_info, stat_info) + char *db_name; + char *extension; + int mode_mask; + long sff; + SMDB_USER_INFO *user_info; + struct stat *stat_info; +{ + int st; + int result; + char db_file_name[MAXPATHLEN]; + + result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name, + extension); + if (result != SMDBE_OK) + return result; + + st = safefile(db_file_name, user_info->smdbu_id, + user_info->smdbu_group_id, user_info->smdbu_name, + sff, mode_mask, stat_info); + if (st != 0) + return st; + + return SMDBE_OK; +} +/* +** SMDB_FILECHANGED -- Checks to see if a file changed. +** +** Compares the passed in stat_info with a current stat on +** the passed in file descriptor. Check filechanged for +** return values. +** +** Parameters: +** db_name -- The name of the database without extension. +** extension -- The extension. +** db_fd -- A file descriptor for the database file. +** stat_info -- An old stat_info. +** Returns: +** SMDBE_OK -- Success, otherwise errno. +*/ + +int +smdb_filechanged(db_name, extension, db_fd, stat_info) + char *db_name; + char *extension; + int db_fd; + struct stat *stat_info; +{ + int result; + char db_file_name[MAXPATHLEN]; + + result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name, + extension); + if (result != SMDBE_OK) + return result; + return filechanged(db_file_name, db_fd, stat_info); +} +/* +** SMDB_PRINT_AVAILABLE_TYPES -- Prints the names of the available types. +** +** Parameters: +** None +** +** Returns: +** None +*/ + +void +smdb_print_available_types() +{ +#ifdef NDBM + printf("dbm\n"); +#endif /* NDBM */ +#ifdef NEWDB + printf("hash\n"); + printf("btree\n"); +#endif /* NEWDB */ +} +/* +** SMDB_DB_DEFINITION -- Given a database type, return database definition +** +** Reads though a structure making an association with the database +** type and the required cpp define from sendmail/README. +** List size is dynamic and must be NULL terminated. +** +** Parameters: +** type -- The name of the database type. +** +** Returns: +** definition for type, otherwise NULL. +*/ + +typedef struct +{ + SMDB_DBTYPE type; + char *dbdef; +} dbtype; + +static dbtype DatabaseDefs[] = +{ + { SMDB_TYPE_HASH, "NEWDB" }, + { SMDB_TYPE_BTREE, "NEWDB" }, + { SMDB_TYPE_NDBM, "NDBM" }, + { NULL, "OOPS" } +}; + +char * +smdb_db_definition(type) + SMDB_DBTYPE type; +{ + dbtype *ptr = DatabaseDefs; + + while (ptr != NULL && ptr->type != NULL) + { + if (strcmp(type, ptr->type) == 0) + return ptr->dbdef; + ptr++; + } + return NULL; +} diff --git a/usr/src/cmd/sendmail/libsmdb/smdb1.c b/usr/src/cmd/sendmail/libsmdb/smdb1.c new file mode 100644 index 0000000000..3b8508f479 --- /dev/null +++ b/usr/src/cmd/sendmail/libsmdb/smdb1.c @@ -0,0 +1,577 @@ +/* +** Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers. +** All rights reserved. +** +** By using this file, you agree to the terms and conditions set +** forth in the LICENSE file which can be found at the top level of +** the sendmail distribution. +*/ + +#pragma ident "%Z%%M% %I% %E% SMI" + +#include <sm/gen.h> +SM_RCSID("@(#)$Id: smdb1.c,v 8.59 2004/08/03 20:58:39 ca Exp $") + +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> + +#include <sendmail/sendmail.h> +#include <libsmdb/smdb.h> + +#if (DB_VERSION_MAJOR == 1) + +# define SMDB1_FILE_EXTENSION "db" + +struct smdb_db1_struct +{ + DB *smdb1_db; + int smdb1_lock_fd; + bool smdb1_cursor_in_use; +}; +typedef struct smdb_db1_struct SMDB_DB1_DATABASE; + +struct smdb_db1_cursor +{ + SMDB_DB1_DATABASE *db; +}; +typedef struct smdb_db1_cursor SMDB_DB1_CURSOR; + +static DBTYPE smdb_type_to_db1_type __P((SMDB_DBTYPE)); +static unsigned int smdb_put_flags_to_db1_flags __P((SMDB_FLAG)); +static int smdb_cursor_get_flags_to_smdb1 __P((SMDB_FLAG)); +static SMDB_DB1_DATABASE *smdb1_malloc_database __P((void)); +static int smdb1_close __P((SMDB_DATABASE *)); +static int smdb1_del __P((SMDB_DATABASE *, SMDB_DBENT *, unsigned int)); +static int smdb1_fd __P((SMDB_DATABASE *, int *)); +static int smdb1_lockfd __P((SMDB_DATABASE *)); +static int smdb1_get __P((SMDB_DATABASE *, SMDB_DBENT *, SMDB_DBENT *, unsigned int)); +static int smdb1_put __P((SMDB_DATABASE *, SMDB_DBENT *, SMDB_DBENT *, unsigned int)); +static int smdb1_set_owner __P((SMDB_DATABASE *, uid_t, gid_t)); +static int smdb1_sync __P((SMDB_DATABASE *, unsigned int)); +static int smdb1_cursor_close __P((SMDB_CURSOR *)); +static int smdb1_cursor_del __P((SMDB_CURSOR *, unsigned int)); +static int smdb1_cursor_get __P((SMDB_CURSOR *, SMDB_DBENT *, SMDB_DBENT *, SMDB_FLAG)); +static int smdb1_cursor_put __P((SMDB_CURSOR *, SMDB_DBENT *, SMDB_DBENT *, SMDB_FLAG)); +static int smdb1_cursor __P((SMDB_DATABASE *, SMDB_CURSOR **, unsigned int)); + +/* +** SMDB_TYPE_TO_DB1_TYPE -- Translates smdb database type to db1 type. +** +** Parameters: +** type -- The type to translate. +** +** Returns: +** The DB1 type that corresponsds to the passed in SMDB type. +** Returns -1 if there is no equivalent type. +** +*/ + +static DBTYPE +smdb_type_to_db1_type(type) + SMDB_DBTYPE type; +{ + if (type == SMDB_TYPE_DEFAULT) + return DB_HASH; + + if (strncmp(type, SMDB_TYPE_HASH, SMDB_TYPE_HASH_LEN) == 0) + return DB_HASH; + + if (strncmp(type, SMDB_TYPE_BTREE, SMDB_TYPE_BTREE_LEN) == 0) + return DB_BTREE; + + /* Should never get here thanks to test in smdb_db_open() */ + return DB_HASH; +} +/* +** SMDB_PUT_FLAGS_TO_DB1_FLAGS -- Translates smdb put flags to db1 put flags. +** +** Parameters: +** flags -- The flags to translate. +** +** Returns: +** The db1 flags that are equivalent to the smdb flags. +** +** Notes: +** Any invalid flags are ignored. +** +*/ + +static unsigned int +smdb_put_flags_to_db1_flags(flags) + SMDB_FLAG flags; +{ + int return_flags; + + return_flags = 0; + + if (bitset(SMDBF_NO_OVERWRITE, flags)) + return_flags |= R_NOOVERWRITE; + + return return_flags; +} +/* +** SMDB_CURSOR_GET_FLAGS_TO_SMDB1 +** +** Parameters: +** flags -- The flags to translate. +** +** Returns: +** The db1 flags that are equivalent to the smdb flags. +** +** Notes: +** Returns -1 if we don't support the flag. +** +*/ + +static int +smdb_cursor_get_flags_to_smdb1(flags) + SMDB_FLAG flags; +{ + switch(flags) + { + case SMDB_CURSOR_GET_FIRST: + return R_FIRST; + + case SMDB_CURSOR_GET_LAST: + return R_LAST; + + case SMDB_CURSOR_GET_NEXT: + return R_NEXT; + + case SMDB_CURSOR_GET_RANGE: + return R_CURSOR; + + default: + return -1; + } +} + +/* +** The rest of these functions correspond to the interface laid out in smdb.h. +*/ + +static SMDB_DB1_DATABASE * +smdb1_malloc_database() +{ + SMDB_DB1_DATABASE *db1; + + db1 = (SMDB_DB1_DATABASE *) malloc(sizeof(SMDB_DB1_DATABASE)); + + if (db1 != NULL) + { + db1->smdb1_lock_fd = -1; + db1->smdb1_cursor_in_use = false; + } + + return db1; +} + +static int +smdb1_close(database) + SMDB_DATABASE *database; +{ + int result; + SMDB_DB1_DATABASE *db1 = (SMDB_DB1_DATABASE *) database->smdb_impl; + DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db; + + result = db->close(db); + if (db1->smdb1_lock_fd != -1) + (void) close(db1->smdb1_lock_fd); + + free(db1); + database->smdb_impl = NULL; + + return result; +} + +static int +smdb1_del(database, key, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + unsigned int flags; +{ + DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db; + DBT dbkey; + + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.data = key->data; + dbkey.size = key->size; + return db->del(db, &dbkey, flags); +} + +static int +smdb1_fd(database, fd) + SMDB_DATABASE *database; + int *fd; +{ + DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db; + + *fd = db->fd(db); + if (*fd == -1) + return errno; + + return SMDBE_OK; +} + +static int +smdb1_lockfd(database) + SMDB_DATABASE *database; +{ + SMDB_DB1_DATABASE *db1 = (SMDB_DB1_DATABASE *) database->smdb_impl; + + return db1->smdb1_lock_fd; +} + + +static int +smdb1_get(database, key, data, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + SMDB_DBENT *data; + unsigned int flags; +{ + int result; + DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db; + DBT dbkey, dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.data = key->data; + dbkey.size = key->size; + + result = db->get(db, &dbkey, &dbdata, flags); + if (result != 0) + { + if (result == 1) + return SMDBE_NOT_FOUND; + return errno; + } + data->data = dbdata.data; + data->size = dbdata.size; + return SMDBE_OK; +} + +static int +smdb1_put(database, key, data, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + SMDB_DBENT *data; + unsigned int flags; +{ + DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db; + DBT dbkey, dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.data = key->data; + dbkey.size = key->size; + dbdata.data = data->data; + dbdata.size = data->size; + + return db->put(db, &dbkey, &dbdata, + smdb_put_flags_to_db1_flags(flags)); +} + +static int +smdb1_set_owner(database, uid, gid) + SMDB_DATABASE *database; + uid_t uid; + gid_t gid; +{ +# if HASFCHOWN + int fd; + int result; + DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db; + + fd = db->fd(db); + if (fd == -1) + return errno; + + result = fchown(fd, uid, gid); + if (result < 0) + return errno; +# endif /* HASFCHOWN */ + + return SMDBE_OK; +} + +static int +smdb1_sync(database, flags) + SMDB_DATABASE *database; + unsigned int flags; +{ + DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db; + + return db->sync(db, flags); +} + +static int +smdb1_cursor_close(cursor) + SMDB_CURSOR *cursor; +{ + SMDB_DB1_CURSOR *db1_cursor = (SMDB_DB1_CURSOR *) cursor->smdbc_impl; + SMDB_DB1_DATABASE *db1 = db1_cursor->db; + + if (!db1->smdb1_cursor_in_use) + return SMDBE_NOT_A_VALID_CURSOR; + + db1->smdb1_cursor_in_use = false; + free(cursor); + + return SMDBE_OK; +} + +static int +smdb1_cursor_del(cursor, flags) + SMDB_CURSOR *cursor; + unsigned int flags; +{ + SMDB_DB1_CURSOR *db1_cursor = (SMDB_DB1_CURSOR *) cursor->smdbc_impl; + SMDB_DB1_DATABASE *db1 = db1_cursor->db; + DB *db = db1->smdb1_db; + + return db->del(db, NULL, R_CURSOR); +} + +static int +smdb1_cursor_get(cursor, key, value, flags) + SMDB_CURSOR *cursor; + SMDB_DBENT *key; + SMDB_DBENT *value; + SMDB_FLAG flags; +{ + int db1_flags; + int result; + SMDB_DB1_CURSOR *db1_cursor = (SMDB_DB1_CURSOR *) cursor->smdbc_impl; + SMDB_DB1_DATABASE *db1 = db1_cursor->db; + DB *db = db1->smdb1_db; + DBT dbkey, dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + (void) memset(&dbkey, '\0', sizeof dbkey); + + db1_flags = smdb_cursor_get_flags_to_smdb1(flags); + result = db->seq(db, &dbkey, &dbdata, db1_flags); + if (result == -1) + return errno; + if (result == 1) + return SMDBE_LAST_ENTRY; + value->data = dbdata.data; + value->size = dbdata.size; + key->data = dbkey.data; + key->size = dbkey.size; + return SMDBE_OK; +} + +static int +smdb1_cursor_put(cursor, key, value, flags) + SMDB_CURSOR *cursor; + SMDB_DBENT *key; + SMDB_DBENT *value; + SMDB_FLAG flags; +{ + SMDB_DB1_CURSOR *db1_cursor = (SMDB_DB1_CURSOR *) cursor->smdbc_impl; + SMDB_DB1_DATABASE *db1 = db1_cursor->db; + DB *db = db1->smdb1_db; + DBT dbkey, dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.data = key->data; + dbkey.size = key->size; + dbdata.data = value->data; + dbdata.size = value->size; + + return db->put(db, &dbkey, &dbdata, R_CURSOR); +} + +static int +smdb1_cursor(database, cursor, flags) + SMDB_DATABASE *database; + SMDB_CURSOR **cursor; + unsigned int flags; +{ + SMDB_DB1_DATABASE *db1 = (SMDB_DB1_DATABASE *) database->smdb_impl; + SMDB_CURSOR *cur; + SMDB_DB1_CURSOR *db1_cursor; + + if (db1->smdb1_cursor_in_use) + return SMDBE_ONLY_SUPPORTS_ONE_CURSOR; + + db1->smdb1_cursor_in_use = true; + db1_cursor = (SMDB_DB1_CURSOR *) malloc(sizeof(SMDB_DB1_CURSOR)); + db1_cursor->db = db1; + + cur = (SMDB_CURSOR *) malloc(sizeof(SMDB_CURSOR)); + + if (cur == NULL) + return SMDBE_MALLOC; + + cur->smdbc_impl = db1_cursor; + cur->smdbc_close = smdb1_cursor_close; + cur->smdbc_del = smdb1_cursor_del; + cur->smdbc_get = smdb1_cursor_get; + cur->smdbc_put = smdb1_cursor_put; + *cursor = cur; + + return SMDBE_OK; +} +/* +** SMDB_DB_OPEN -- Opens a db1 database. +** +** Parameters: +** database -- An unallocated database pointer to a pointer. +** db_name -- The name of the database without extension. +** mode -- File permisions on the database if created. +** mode_mask -- Mode bits that must match on an existing database. +** sff -- Flags for safefile. +** type -- The type of database to open +** See smdb_type_to_db1_type for valid types. +** user_info -- Information on the user to use for file +** permissions. +** db_params -- +** An SMDB_DBPARAMS struct including params. These +** are processed according to the type of the +** database. Currently supported params (only for +** HASH type) are: +** num_elements +** cache_size +** +** Returns: +** SMDBE_OK -- Success, otherwise errno. +*/ + +int +smdb_db_open(database, db_name, mode, mode_mask, sff, type, user_info, + db_params) + SMDB_DATABASE **database; + char *db_name; + int mode; + int mode_mask; + long sff; + SMDB_DBTYPE type; + SMDB_USER_INFO *user_info; + SMDB_DBPARAMS *db_params; +{ + bool lockcreated = false; + int db_fd; + int lock_fd; + int result; + void *params; + SMDB_DATABASE *smdb_db; + SMDB_DB1_DATABASE *db1; + DB *db; + HASHINFO hash_info; + BTREEINFO btree_info; + DBTYPE db_type; + struct stat stat_info; + char db_file_name[MAXPATHLEN]; + + if (type == NULL || + (strncmp(SMDB_TYPE_HASH, type, SMDB_TYPE_HASH_LEN) != 0 && + strncmp(SMDB_TYPE_BTREE, type, SMDB_TYPE_BTREE_LEN) != 0)) + return SMDBE_UNKNOWN_DB_TYPE; + + result = smdb_add_extension(db_file_name, sizeof db_file_name, + db_name, SMDB1_FILE_EXTENSION); + if (result != SMDBE_OK) + return result; + + result = smdb_setup_file(db_name, SMDB1_FILE_EXTENSION, mode_mask, + sff, user_info, &stat_info); + if (result != SMDBE_OK) + return result; + + if (stat_info.st_mode == ST_MODE_NOFILE && + bitset(mode, O_CREAT)) + lockcreated = true; + + lock_fd = -1; + result = smdb_lock_file(&lock_fd, db_name, mode, sff, + SMDB1_FILE_EXTENSION); + if (result != SMDBE_OK) + return result; + + if (lockcreated) + { + mode |= O_TRUNC; + mode &= ~(O_CREAT|O_EXCL); + } + + *database = NULL; + + smdb_db = smdb_malloc_database(); + db1 = smdb1_malloc_database(); + if (smdb_db == NULL || db1 == NULL) + return SMDBE_MALLOC; + db1->smdb1_lock_fd = lock_fd; + + params = NULL; + if (db_params != NULL && + (strncmp(SMDB_TYPE_HASH, type, SMDB_TYPE_HASH_LEN) == 0)) + { + (void) memset(&hash_info, '\0', sizeof hash_info); + hash_info.nelem = db_params->smdbp_num_elements; + hash_info.cachesize = db_params->smdbp_cache_size; + params = &hash_info; + } + + if (db_params != NULL && + (strncmp(SMDB_TYPE_BTREE, type, SMDB_TYPE_BTREE_LEN) == 0)) + { + (void) memset(&btree_info, '\0', sizeof btree_info); + btree_info.cachesize = db_params->smdbp_cache_size; + if (db_params->smdbp_allow_dup) + btree_info.flags |= R_DUP; + params = &btree_info; + } + + db_type = smdb_type_to_db1_type(type); + db = dbopen(db_file_name, mode, DBMMODE, db_type, params); + if (db != NULL) + { + db_fd = db->fd(db); + result = smdb_filechanged(db_name, SMDB1_FILE_EXTENSION, db_fd, + &stat_info); + } + else + { + if (errno == 0) + result = SMDBE_BAD_OPEN; + else + result = errno; + } + + if (result == SMDBE_OK) + { + /* Everything is ok. Setup driver */ + db1->smdb1_db = db; + + smdb_db->smdb_close = smdb1_close; + smdb_db->smdb_del = smdb1_del; + smdb_db->smdb_fd = smdb1_fd; + smdb_db->smdb_lockfd = smdb1_lockfd; + smdb_db->smdb_get = smdb1_get; + smdb_db->smdb_put = smdb1_put; + smdb_db->smdb_set_owner = smdb1_set_owner; + smdb_db->smdb_sync = smdb1_sync; + smdb_db->smdb_cursor = smdb1_cursor; + smdb_db->smdb_impl = db1; + + *database = smdb_db; + return SMDBE_OK; + } + + if (db != NULL) + (void) db->close(db); + + /* Error opening database */ + (void) smdb_unlock_file(db1->smdb1_lock_fd); + free(db1); + smdb_free_database(smdb_db); + + return result; +} + +#endif /* (DB_VERSION_MAJOR == 1) */ diff --git a/usr/src/cmd/sendmail/libsmdb/smdb2.c b/usr/src/cmd/sendmail/libsmdb/smdb2.c new file mode 100644 index 0000000000..e7cf7e4274 --- /dev/null +++ b/usr/src/cmd/sendmail/libsmdb/smdb2.c @@ -0,0 +1,700 @@ +/* +** Copyright (c) 1999-2003 Sendmail, Inc. and its suppliers. +** All rights reserved. +** +** By using this file, you agree to the terms and conditions set +** forth in the LICENSE file which can be found at the top level of +** the sendmail distribution. +*/ + +#pragma ident "%Z%%M% %I% %E% SMI" + +#include <sm/gen.h> +SM_RCSID("@(#)$Id: smdb2.c,v 8.72.2.7 2003/06/24 17:16:10 ca Exp $") + +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + + +#include <sendmail/sendmail.h> +#include <libsmdb/smdb.h> + +#if (DB_VERSION_MAJOR >= 2) + +# define SMDB2_FILE_EXTENSION "db" + +struct smdb_db2_database +{ + DB *smdb2_db; + int smdb2_lock_fd; +}; +typedef struct smdb_db2_database SMDB_DB2_DATABASE; + +/* +** SMDB_TYPE_TO_DB2_TYPE -- Translates smdb database type to db2 type. +** +** Parameters: +** type -- The type to translate. +** +** Returns: +** The DB2 type that corresponsds to the passed in SMDB type. +** Returns -1 if there is no equivalent type. +** +*/ + +DBTYPE +smdb_type_to_db2_type(type) + SMDB_DBTYPE type; +{ + if (type == SMDB_TYPE_DEFAULT) + return DB_HASH; + + if (strncmp(type, SMDB_TYPE_HASH, SMDB_TYPE_HASH_LEN) == 0) + return DB_HASH; + + if (strncmp(type, SMDB_TYPE_BTREE, SMDB_TYPE_BTREE_LEN) == 0) + return DB_BTREE; + + return DB_UNKNOWN; +} +/* +** DB2_ERROR_TO_SMDB -- Translates db2 errors to smdbe errors +** +** Parameters: +** error -- The error to translate. +** +** Returns: +** The SMDBE error corresponding to the db2 error. +** If we don't have a corresponding error, it returs errno. +** +*/ + +int +db2_error_to_smdb(error) + int error; +{ + int result; + + switch (error) + { +# ifdef DB_INCOMPLETE + case DB_INCOMPLETE: + result = SMDBE_INCOMPLETE; + break; +# endif /* DB_INCOMPLETE */ + +# ifdef DB_NOTFOUND + case DB_NOTFOUND: + result = SMDBE_NOT_FOUND; + break; +# endif /* DB_NOTFOUND */ + +# ifdef DB_KEYEMPTY + case DB_KEYEMPTY: + result = SMDBE_KEY_EMPTY; + break; +# endif /* DB_KEYEMPTY */ + +# ifdef DB_KEYEXIST + case DB_KEYEXIST: + result = SMDBE_KEY_EXIST; + break; +# endif /* DB_KEYEXIST */ + +# ifdef DB_LOCK_DEADLOCK + case DB_LOCK_DEADLOCK: + result = SMDBE_LOCK_DEADLOCK; + break; +# endif /* DB_LOCK_DEADLOCK */ + +# ifdef DB_LOCK_NOTGRANTED + case DB_LOCK_NOTGRANTED: + result = SMDBE_LOCK_NOT_GRANTED; + break; +# endif /* DB_LOCK_NOTGRANTED */ + +# ifdef DB_LOCK_NOTHELD + case DB_LOCK_NOTHELD: + result = SMDBE_LOCK_NOT_HELD; + break; +# endif /* DB_LOCK_NOTHELD */ + +# ifdef DB_RUNRECOVERY + case DB_RUNRECOVERY: + result = SMDBE_RUN_RECOVERY; + break; +# endif /* DB_RUNRECOVERY */ + +# ifdef DB_OLD_VERSION + case DB_OLD_VERSION: + result = SMDBE_OLD_VERSION; + break; +# endif /* DB_OLD_VERSION */ + + case 0: + result = SMDBE_OK; + break; + + default: + result = error; + } + return result; +} +/* +** SMDB_PUT_FLAGS_TO_DB2_FLAGS -- Translates smdb put flags to db2 put flags. +** +** Parameters: +** flags -- The flags to translate. +** +** Returns: +** The db2 flags that are equivalent to the smdb flags. +** +** Notes: +** Any invalid flags are ignored. +** +*/ + +unsigned int +smdb_put_flags_to_db2_flags(flags) + SMDB_FLAG flags; +{ + int return_flags; + + return_flags = 0; + + if (bitset(SMDBF_NO_OVERWRITE, flags)) + return_flags |= DB_NOOVERWRITE; + + return return_flags; +} +/* +** SMDB_CURSOR_GET_FLAGS_TO_DB2 -- Translates smdb cursor get flags to db2 +** getflags. +** +** Parameters: +** flags -- The flags to translate. +** +** Returns: +** The db2 flags that are equivalent to the smdb flags. +** +** Notes: +** -1 is returned if flag is unknown. +** +*/ + +int +smdb_cursor_get_flags_to_db2(flags) + SMDB_FLAG flags; +{ + switch (flags) + { + case SMDB_CURSOR_GET_FIRST: + return DB_FIRST; + + case SMDB_CURSOR_GET_LAST: + return DB_LAST; + + case SMDB_CURSOR_GET_NEXT: + return DB_NEXT; + + case SMDB_CURSOR_GET_RANGE: + return DB_SET_RANGE; + + default: + return -1; + } +} + +/* +** Except for smdb_db_open, the rest of these functions correspond to the +** interface laid out in smdb.h. +*/ + +SMDB_DB2_DATABASE * +smdb2_malloc_database() +{ + SMDB_DB2_DATABASE *db2; + + db2 = (SMDB_DB2_DATABASE *) malloc(sizeof(SMDB_DB2_DATABASE)); + if (db2 != NULL) + db2->smdb2_lock_fd = -1; + + return db2; +} + +int +smdb2_close(database) + SMDB_DATABASE *database; +{ + int result; + SMDB_DB2_DATABASE *db2 = (SMDB_DB2_DATABASE *) database->smdb_impl; + DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db; + + result = db2_error_to_smdb(db->close(db, 0)); + if (db2->smdb2_lock_fd != -1) + close(db2->smdb2_lock_fd); + + free(db2); + database->smdb_impl = NULL; + + return result; +} + +int +smdb2_del(database, key, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + unsigned int flags; +{ + DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db; + DBT dbkey; + + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.data = key->data; + dbkey.size = key->size; + return db2_error_to_smdb(db->del(db, NULL, &dbkey, flags)); +} + +int +smdb2_fd(database, fd) + SMDB_DATABASE *database; + int *fd; +{ + DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db; + + return db2_error_to_smdb(db->fd(db, fd)); +} + +int +smdb2_lockfd(database) + SMDB_DATABASE *database; +{ + SMDB_DB2_DATABASE *db2 = (SMDB_DB2_DATABASE *) database->smdb_impl; + + return db2->smdb2_lock_fd; +} + +int +smdb2_get(database, key, data, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + SMDB_DBENT *data; + unsigned int flags; +{ + int result; + DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db; + DBT dbkey, dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.data = key->data; + dbkey.size = key->size; + + result = db->get(db, NULL, &dbkey, &dbdata, flags); + data->data = dbdata.data; + data->size = dbdata.size; + return db2_error_to_smdb(result); +} + +int +smdb2_put(database, key, data, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + SMDB_DBENT *data; + unsigned int flags; +{ + DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db; + DBT dbkey, dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.data = key->data; + dbkey.size = key->size; + dbdata.data = data->data; + dbdata.size = data->size; + + return db2_error_to_smdb(db->put(db, NULL, &dbkey, &dbdata, + smdb_put_flags_to_db2_flags(flags))); +} + + +int +smdb2_set_owner(database, uid, gid) + SMDB_DATABASE *database; + uid_t uid; + gid_t gid; +{ +# if HASFCHOWN + int fd; + int result; + DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db; + + result = db->fd(db, &fd); + if (result != 0) + return result; + + result = fchown(fd, uid, gid); + if (result < 0) + return errno; +# endif /* HASFCHOWN */ + + return SMDBE_OK; +} + +int +smdb2_sync(database, flags) + SMDB_DATABASE *database; + unsigned int flags; +{ + DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db; + + return db2_error_to_smdb(db->sync(db, flags)); +} + +int +smdb2_cursor_close(cursor) + SMDB_CURSOR *cursor; +{ + int ret; + DBC *dbc = (DBC *) cursor->smdbc_impl; + + ret = db2_error_to_smdb(dbc->c_close(dbc)); + free(cursor); + return ret; +} + +int +smdb2_cursor_del(cursor, flags) + SMDB_CURSOR *cursor; + SMDB_FLAG flags; +{ + DBC *dbc = (DBC *) cursor->smdbc_impl; + + return db2_error_to_smdb(dbc->c_del(dbc, 0)); +} + +int +smdb2_cursor_get(cursor, key, value, flags) + SMDB_CURSOR *cursor; + SMDB_DBENT *key; + SMDB_DBENT *value; + SMDB_FLAG flags; +{ + int db2_flags; + int result; + DBC *dbc = (DBC *) cursor->smdbc_impl; + DBT dbkey, dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + (void) memset(&dbkey, '\0', sizeof dbkey); + + db2_flags = smdb_cursor_get_flags_to_db2(flags); + result = dbc->c_get(dbc, &dbkey, &dbdata, db2_flags); + if (result == DB_NOTFOUND) + return SMDBE_LAST_ENTRY; + key->data = dbkey.data; + key->size = dbkey.size; + value->data = dbdata.data; + value->size = dbdata.size; + return db2_error_to_smdb(result); +} + +int +smdb2_cursor_put(cursor, key, value, flags) + SMDB_CURSOR *cursor; + SMDB_DBENT *key; + SMDB_DBENT *value; + SMDB_FLAG flags; +{ + DBC *dbc = (DBC *) cursor->smdbc_impl; + DBT dbkey, dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.data = key->data; + dbkey.size = key->size; + dbdata.data = value->data; + dbdata.size = value->size; + + return db2_error_to_smdb(dbc->c_put(dbc, &dbkey, &dbdata, 0)); +} + +int +smdb2_cursor(database, cursor, flags) + SMDB_DATABASE *database; + SMDB_CURSOR **cursor; + SMDB_FLAG flags; +{ + int result; + DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db; + DBC *db2_cursor; + +# if DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6 + result = db->cursor(db, NULL, &db2_cursor, 0); +# else /* DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6 */ + result = db->cursor(db, NULL, &db2_cursor); +# endif /* DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >= 6 */ + if (result != 0) + return db2_error_to_smdb(result); + + *cursor = (SMDB_CURSOR *) malloc(sizeof(SMDB_CURSOR)); + if (*cursor == NULL) + return SMDBE_MALLOC; + + (*cursor)->smdbc_close = smdb2_cursor_close; + (*cursor)->smdbc_del = smdb2_cursor_del; + (*cursor)->smdbc_get = smdb2_cursor_get; + (*cursor)->smdbc_put = smdb2_cursor_put; + (*cursor)->smdbc_impl = db2_cursor; + + return SMDBE_OK; +} + +# if DB_VERSION_MAJOR == 2 +static int +smdb_db_open_internal(db_name, db_type, db_flags, db_params, db) + char *db_name; + DBTYPE db_type; + int db_flags; + SMDB_DBPARAMS *db_params; + DB **db; +{ + void *params; + DB_INFO db_info; + + params = NULL; + (void) memset(&db_info, '\0', sizeof db_info); + if (db_params != NULL) + { + db_info.db_cachesize = db_params->smdbp_cache_size; + if (db_type == DB_HASH) + db_info.h_nelem = db_params->smdbp_num_elements; + if (db_params->smdbp_allow_dup) + db_info.flags |= DB_DUP; + params = &db_info; + } + return db_open(db_name, db_type, db_flags, DBMMODE, NULL, params, db); +} +# endif /* DB_VERSION_MAJOR == 2 */ + +# if DB_VERSION_MAJOR > 2 +static int +smdb_db_open_internal(db_name, db_type, db_flags, db_params, db) + char *db_name; + DBTYPE db_type; + int db_flags; + SMDB_DBPARAMS *db_params; + DB **db; +{ + int result; + + result = db_create(db, NULL, 0); + if (result != 0 || *db == NULL) + return result; + + if (db_params != NULL) + { + result = (*db)->set_cachesize(*db, 0, + db_params->smdbp_cache_size, 0); + if (result != 0) + { + (void) (*db)->close((*db), 0); + *db = NULL; + return db2_error_to_smdb(result); + } + if (db_type == DB_HASH) + { + result = (*db)->set_h_nelem(*db, db_params->smdbp_num_elements); + if (result != 0) + { + (void) (*db)->close(*db, 0); + *db = NULL; + return db2_error_to_smdb(result); + } + } + if (db_params->smdbp_allow_dup) + { + result = (*db)->set_flags(*db, DB_DUP); + if (result != 0) + { + (void) (*db)->close(*db, 0); + *db = NULL; + return db2_error_to_smdb(result); + } + } + } + + result = (*db)->open(*db, + DBTXN /* transaction for DB 4.1 */ + db_name, NULL, db_type, db_flags, DBMMODE); + if (result != 0) + { + (void) (*db)->close(*db, 0); + *db = NULL; + } + return db2_error_to_smdb(result); +} +# endif /* DB_VERSION_MAJOR > 2 */ +/* +** SMDB_DB_OPEN -- Opens a db database. +** +** Parameters: +** database -- An unallocated database pointer to a pointer. +** db_name -- The name of the database without extension. +** mode -- File permisions for a created database. +** mode_mask -- Mode bits that must match on an opened database. +** sff -- Flags for safefile. +** type -- The type of database to open +** See smdb_type_to_db2_type for valid types. +** user_info -- User information for file permissions. +** db_params -- +** An SMDB_DBPARAMS struct including params. These +** are processed according to the type of the +** database. Currently supported params (only for +** HASH type) are: +** num_elements +** cache_size +** +** Returns: +** SMDBE_OK -- Success, other errno: +** SMDBE_MALLOC -- Cannot allocate memory. +** SMDBE_BAD_OPEN -- db_open didn't return an error, but +** somehow the DB pointer is NULL. +** Anything else: translated error from db2 +*/ + +int +smdb_db_open(database, db_name, mode, mode_mask, sff, type, user_info, db_params) + SMDB_DATABASE **database; + char *db_name; + int mode; + int mode_mask; + long sff; + SMDB_DBTYPE type; + SMDB_USER_INFO *user_info; + SMDB_DBPARAMS *db_params; +{ + bool lockcreated = false; + int result; + int db_flags; + int lock_fd; + int db_fd; + int major_v, minor_v, patch_v; + SMDB_DATABASE *smdb_db; + SMDB_DB2_DATABASE *db2; + DB *db; + DBTYPE db_type; + struct stat stat_info; + char db_file_name[MAXPATHLEN]; + + (void) db_version(&major_v, &minor_v, &patch_v); + if (major_v != DB_VERSION_MAJOR || minor_v != DB_VERSION_MINOR) + return SMDBE_VERSION_MISMATCH; + + *database = NULL; + + result = smdb_add_extension(db_file_name, sizeof db_file_name, + db_name, SMDB2_FILE_EXTENSION); + if (result != SMDBE_OK) + return result; + + result = smdb_setup_file(db_name, SMDB2_FILE_EXTENSION, + mode_mask, sff, user_info, &stat_info); + if (result != SMDBE_OK) + return result; + + lock_fd = -1; + + if (stat_info.st_mode == ST_MODE_NOFILE && + bitset(mode, O_CREAT)) + lockcreated = true; + + result = smdb_lock_file(&lock_fd, db_name, mode, sff, + SMDB2_FILE_EXTENSION); + if (result != SMDBE_OK) + return result; + + if (lockcreated) + { + mode |= O_TRUNC; + mode &= ~(O_CREAT|O_EXCL); + } + + smdb_db = smdb_malloc_database(); + if (smdb_db == NULL) + return SMDBE_MALLOC; + + db2 = smdb2_malloc_database(); + if (db2 == NULL) + return SMDBE_MALLOC; + + db2->smdb2_lock_fd = lock_fd; + + db_type = smdb_type_to_db2_type(type); + + db = NULL; + + db_flags = 0; + if (bitset(O_CREAT, mode)) + db_flags |= DB_CREATE; + if (bitset(O_TRUNC, mode)) + db_flags |= DB_TRUNCATE; + if (mode == O_RDONLY) + db_flags |= DB_RDONLY; + SM_DB_FLAG_ADD(db_flags); + + result = smdb_db_open_internal(db_file_name, db_type, + db_flags, db_params, &db); + + if (result == 0 && db != NULL) + { + result = db->fd(db, &db_fd); + if (result == 0) + result = SMDBE_OK; + } + else + { + /* Try and narrow down on the problem */ + if (result != 0) + result = db2_error_to_smdb(result); + else + result = SMDBE_BAD_OPEN; + } + + if (result == SMDBE_OK) + result = smdb_filechanged(db_name, SMDB2_FILE_EXTENSION, db_fd, + &stat_info); + + if (result == SMDBE_OK) + { + /* Everything is ok. Setup driver */ + db2->smdb2_db = db; + + smdb_db->smdb_close = smdb2_close; + smdb_db->smdb_del = smdb2_del; + smdb_db->smdb_fd = smdb2_fd; + smdb_db->smdb_lockfd = smdb2_lockfd; + smdb_db->smdb_get = smdb2_get; + smdb_db->smdb_put = smdb2_put; + smdb_db->smdb_set_owner = smdb2_set_owner; + smdb_db->smdb_sync = smdb2_sync; + smdb_db->smdb_cursor = smdb2_cursor; + smdb_db->smdb_impl = db2; + + *database = smdb_db; + + return SMDBE_OK; + } + + if (db != NULL) + db->close(db, 0); + + smdb_unlock_file(db2->smdb2_lock_fd); + free(db2); + smdb_free_database(smdb_db); + + return result; +} + +#endif /* (DB_VERSION_MAJOR >= 2) */ diff --git a/usr/src/cmd/sendmail/libsmdb/smndbm.c b/usr/src/cmd/sendmail/libsmdb/smndbm.c new file mode 100644 index 0000000000..3327346bd2 --- /dev/null +++ b/usr/src/cmd/sendmail/libsmdb/smndbm.c @@ -0,0 +1,629 @@ +/* +** Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers. +** All rights reserved. +** +** By using this file, you agree to the terms and conditions set +** forth in the LICENSE file which can be found at the top level of +** the sendmail distribution. +*/ + +#pragma ident "%Z%%M% %I% %E% SMI" + +#include <sm/gen.h> +SM_RCSID("@(#)$Id: smndbm.c,v 8.52 2002/05/21 22:30:30 gshapiro Exp $") + +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + +#include <sendmail/sendmail.h> +#include <libsmdb/smdb.h> + +#ifdef NDBM + +# define SMNDB_DIR_FILE_EXTENSION "dir" +# define SMNDB_PAG_FILE_EXTENSION "pag" + +struct smdb_dbm_database_struct +{ + DBM *smndbm_dbm; + int smndbm_lock_fd; + bool smndbm_cursor_in_use; +}; +typedef struct smdb_dbm_database_struct SMDB_DBM_DATABASE; + +struct smdb_dbm_cursor_struct +{ + SMDB_DBM_DATABASE *smndbmc_db; + datum smndbmc_current_key; +}; +typedef struct smdb_dbm_cursor_struct SMDB_DBM_CURSOR; + +/* +** SMDB_PUT_FLAGS_TO_NDBM_FLAGS -- Translates smdb put flags to ndbm put flags. +** +** Parameters: +** flags -- The flags to translate. +** +** Returns: +** The ndbm flags that are equivalent to the smdb flags. +** +** Notes: +** Any invalid flags are ignored. +** +*/ + +int +smdb_put_flags_to_ndbm_flags(flags) + SMDB_FLAG flags; +{ + int return_flags; + + return_flags = 0; + if (bitset(SMDBF_NO_OVERWRITE, flags)) + return_flags = DBM_INSERT; + else + return_flags = DBM_REPLACE; + + return return_flags; +} + +/* +** Except for smdb_ndbm_open, the rest of these function correspond to the +** interface laid out in smdb.h. +*/ + +SMDB_DBM_DATABASE * +smdbm_malloc_database() +{ + SMDB_DBM_DATABASE *db; + + db = (SMDB_DBM_DATABASE *) malloc(sizeof(SMDB_DBM_DATABASE)); + if (db != NULL) + { + db->smndbm_dbm = NULL; + db->smndbm_lock_fd = -1; + db->smndbm_cursor_in_use = false; + } + + return db; +} + +int +smdbm_close(database) + SMDB_DATABASE *database; +{ + SMDB_DBM_DATABASE *db = (SMDB_DBM_DATABASE *) database->smdb_impl; + DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm; + + dbm_close(dbm); + if (db->smndbm_lock_fd != -1) + close(db->smndbm_lock_fd); + + free(db); + database->smdb_impl = NULL; + + return SMDBE_OK; +} + +int +smdbm_del(database, key, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + unsigned int flags; +{ + int result; + DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm; + datum dbkey; + + (void) memset(&dbkey, '\0', sizeof dbkey); + dbkey.dptr = key->data; + dbkey.dsize = key->size; + + errno = 0; + result = dbm_delete(dbm, dbkey); + if (result != 0) + { + int save_errno = errno; + + if (dbm_error(dbm)) + return SMDBE_IO_ERROR; + + if (save_errno != 0) + return save_errno; + + return SMDBE_NOT_FOUND; + } + return SMDBE_OK; +} + +int +smdbm_fd(database, fd) + SMDB_DATABASE *database; + int *fd; +{ + DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm; + + *fd = dbm_dirfno(dbm); + if (*fd <= 0) + return EINVAL; + + return SMDBE_OK; +} + +int +smdbm_lockfd(database) + SMDB_DATABASE *database; +{ + SMDB_DBM_DATABASE *db = (SMDB_DBM_DATABASE *) database->smdb_impl; + + return db->smndbm_lock_fd; +} + +int +smdbm_get(database, key, data, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + SMDB_DBENT *data; + unsigned int flags; +{ + DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm; + datum dbkey, dbdata; + + (void) memset(&dbkey, '\0', sizeof dbkey); + (void) memset(&dbdata, '\0', sizeof dbdata); + dbkey.dptr = key->data; + dbkey.dsize = key->size; + + errno = 0; + dbdata = dbm_fetch(dbm, dbkey); + if (dbdata.dptr == NULL) + { + int save_errno = errno; + + if (dbm_error(dbm)) + return SMDBE_IO_ERROR; + + if (save_errno != 0) + return save_errno; + + return SMDBE_NOT_FOUND; + } + data->data = dbdata.dptr; + data->size = dbdata.dsize; + return SMDBE_OK; +} + +int +smdbm_put(database, key, data, flags) + SMDB_DATABASE *database; + SMDB_DBENT *key; + SMDB_DBENT *data; + unsigned int flags; +{ + int result; + int save_errno; + DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm; + datum dbkey, dbdata; + + (void) memset(&dbkey, '\0', sizeof dbkey); + (void) memset(&dbdata, '\0', sizeof dbdata); + dbkey.dptr = key->data; + dbkey.dsize = key->size; + dbdata.dptr = data->data; + dbdata.dsize = data->size; + + errno = 0; + result = dbm_store(dbm, dbkey, dbdata, + smdb_put_flags_to_ndbm_flags(flags)); + switch (result) + { + case 1: + return SMDBE_DUPLICATE; + + case 0: + return SMDBE_OK; + + default: + save_errno = errno; + + if (dbm_error(dbm)) + return SMDBE_IO_ERROR; + + if (save_errno != 0) + return save_errno; + + return SMDBE_IO_ERROR; + } + /* NOTREACHED */ +} + +int +smndbm_set_owner(database, uid, gid) + SMDB_DATABASE *database; + uid_t uid; + gid_t gid; +{ +# if HASFCHOWN + int fd; + int result; + DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm; + + fd = dbm_dirfno(dbm); + if (fd <= 0) + return EINVAL; + + result = fchown(fd, uid, gid); + if (result < 0) + return errno; + + fd = dbm_pagfno(dbm); + if (fd <= 0) + return EINVAL; + + result = fchown(fd, uid, gid); + if (result < 0) + return errno; +# endif /* HASFCHOWN */ + + return SMDBE_OK; +} + +int +smdbm_sync(database, flags) + SMDB_DATABASE *database; + unsigned int flags; +{ + return SMDBE_UNSUPPORTED; +} + +int +smdbm_cursor_close(cursor) + SMDB_CURSOR *cursor; +{ + SMDB_DBM_CURSOR *dbm_cursor = (SMDB_DBM_CURSOR *) cursor->smdbc_impl; + SMDB_DBM_DATABASE *db = dbm_cursor->smndbmc_db; + + if (!db->smndbm_cursor_in_use) + return SMDBE_NOT_A_VALID_CURSOR; + + db->smndbm_cursor_in_use = false; + free(dbm_cursor); + free(cursor); + + return SMDBE_OK; +} + +int +smdbm_cursor_del(cursor, flags) + SMDB_CURSOR *cursor; + unsigned int flags; +{ + int result; + SMDB_DBM_CURSOR *dbm_cursor = (SMDB_DBM_CURSOR *) cursor->smdbc_impl; + SMDB_DBM_DATABASE *db = dbm_cursor->smndbmc_db; + DBM *dbm = db->smndbm_dbm; + + errno = 0; + result = dbm_delete(dbm, dbm_cursor->smndbmc_current_key); + if (result != 0) + { + int save_errno = errno; + + if (dbm_error(dbm)) + return SMDBE_IO_ERROR; + + if (save_errno != 0) + return save_errno; + + return SMDBE_NOT_FOUND; + } + return SMDBE_OK; +} + +int +smdbm_cursor_get(cursor, key, value, flags) + SMDB_CURSOR *cursor; + SMDB_DBENT *key; + SMDB_DBENT *value; + SMDB_FLAG flags; +{ + SMDB_DBM_CURSOR *dbm_cursor = (SMDB_DBM_CURSOR *) cursor->smdbc_impl; + SMDB_DBM_DATABASE *db = dbm_cursor->smndbmc_db; + DBM *dbm = db->smndbm_dbm; + datum dbkey, dbdata; + + (void) memset(&dbkey, '\0', sizeof dbkey); + (void) memset(&dbdata, '\0', sizeof dbdata); + + if (flags == SMDB_CURSOR_GET_RANGE) + return SMDBE_UNSUPPORTED; + + if (dbm_cursor->smndbmc_current_key.dptr == NULL) + { + dbm_cursor->smndbmc_current_key = dbm_firstkey(dbm); + if (dbm_cursor->smndbmc_current_key.dptr == NULL) + { + if (dbm_error(dbm)) + return SMDBE_IO_ERROR; + return SMDBE_LAST_ENTRY; + } + } + else + { + dbm_cursor->smndbmc_current_key = dbm_nextkey(dbm); + if (dbm_cursor->smndbmc_current_key.dptr == NULL) + { + if (dbm_error(dbm)) + return SMDBE_IO_ERROR; + return SMDBE_LAST_ENTRY; + } + } + + errno = 0; + dbdata = dbm_fetch(dbm, dbm_cursor->smndbmc_current_key); + if (dbdata.dptr == NULL) + { + int save_errno = errno; + + if (dbm_error(dbm)) + return SMDBE_IO_ERROR; + + if (save_errno != 0) + return save_errno; + + return SMDBE_NOT_FOUND; + } + value->data = dbdata.dptr; + value->size = dbdata.dsize; + key->data = dbm_cursor->smndbmc_current_key.dptr; + key->size = dbm_cursor->smndbmc_current_key.dsize; + + return SMDBE_OK; +} + +int +smdbm_cursor_put(cursor, key, value, flags) + SMDB_CURSOR *cursor; + SMDB_DBENT *key; + SMDB_DBENT *value; + SMDB_FLAG flags; +{ + int result; + int save_errno; + SMDB_DBM_CURSOR *dbm_cursor = (SMDB_DBM_CURSOR *) cursor->smdbc_impl; + SMDB_DBM_DATABASE *db = dbm_cursor->smndbmc_db; + DBM *dbm = db->smndbm_dbm; + datum dbdata; + + (void) memset(&dbdata, '\0', sizeof dbdata); + dbdata.dptr = value->data; + dbdata.dsize = value->size; + + errno = 0; + result = dbm_store(dbm, dbm_cursor->smndbmc_current_key, dbdata, + smdb_put_flags_to_ndbm_flags(flags)); + switch (result) + { + case 1: + return SMDBE_DUPLICATE; + + case 0: + return SMDBE_OK; + + default: + save_errno = errno; + + if (dbm_error(dbm)) + return SMDBE_IO_ERROR; + + if (save_errno != 0) + return save_errno; + + return SMDBE_IO_ERROR; + } + /* NOTREACHED */ +} + +int +smdbm_cursor(database, cursor, flags) + SMDB_DATABASE *database; + SMDB_CURSOR **cursor; + SMDB_FLAG flags; +{ + SMDB_DBM_DATABASE *db = (SMDB_DBM_DATABASE *) database->smdb_impl; + SMDB_CURSOR *cur; + SMDB_DBM_CURSOR *dbm_cursor; + + if (db->smndbm_cursor_in_use) + return SMDBE_ONLY_SUPPORTS_ONE_CURSOR; + + db->smndbm_cursor_in_use = true; + dbm_cursor = (SMDB_DBM_CURSOR *) malloc(sizeof(SMDB_DBM_CURSOR)); + dbm_cursor->smndbmc_db = db; + dbm_cursor->smndbmc_current_key.dptr = NULL; + dbm_cursor->smndbmc_current_key.dsize = 0; + + cur = (SMDB_CURSOR*) malloc(sizeof(SMDB_CURSOR)); + if (cur == NULL) + return SMDBE_MALLOC; + + cur->smdbc_impl = dbm_cursor; + cur->smdbc_close = smdbm_cursor_close; + cur->smdbc_del = smdbm_cursor_del; + cur->smdbc_get = smdbm_cursor_get; + cur->smdbc_put = smdbm_cursor_put; + *cursor = cur; + + return SMDBE_OK; +} +/* +** SMDB_NDBM_OPEN -- Opens a ndbm database. +** +** Parameters: +** database -- An unallocated database pointer to a pointer. +** db_name -- The name of the database without extension. +** mode -- File permisions on a created database. +** mode_mask -- Mode bits that much match on an opened database. +** sff -- Flags to safefile. +** type -- The type of database to open. +** Only SMDB_NDBM is supported. +** user_info -- Information on the user to use for file +** permissions. +** db_params -- No params are supported. +** +** Returns: +** SMDBE_OK -- Success, otherwise errno: +** SMDBE_MALLOC -- Cannot allocate memory. +** SMDBE_UNSUPPORTED -- The type is not supported. +** SMDBE_GDBM_IS_BAD -- We have detected GDBM and we don't +** like it. +** SMDBE_BAD_OPEN -- dbm_open failed and errno was not set. +** Anything else: errno +*/ + +int +smdb_ndbm_open(database, db_name, mode, mode_mask, sff, type, user_info, + db_params) + SMDB_DATABASE **database; + char *db_name; + int mode; + int mode_mask; + long sff; + SMDB_DBTYPE type; + SMDB_USER_INFO *user_info; + SMDB_DBPARAMS *db_params; +{ + bool lockcreated = false; + int result; + int lock_fd; + SMDB_DATABASE *smdb_db; + SMDB_DBM_DATABASE *db; + DBM *dbm = NULL; + struct stat dir_stat_info; + struct stat pag_stat_info; + + result = SMDBE_OK; + *database = NULL; + + if (type == NULL) + return SMDBE_UNKNOWN_DB_TYPE; + + result = smdb_setup_file(db_name, SMNDB_DIR_FILE_EXTENSION, mode_mask, + sff, user_info, &dir_stat_info); + if (result != SMDBE_OK) + return result; + + result = smdb_setup_file(db_name, SMNDB_PAG_FILE_EXTENSION, mode_mask, + sff, user_info, &pag_stat_info); + if (result != SMDBE_OK) + return result; + + if ((dir_stat_info.st_mode == ST_MODE_NOFILE || + pag_stat_info.st_mode == ST_MODE_NOFILE) && + bitset(mode, O_CREAT)) + lockcreated = true; + + lock_fd = -1; + result = smdb_lock_file(&lock_fd, db_name, mode, sff, + SMNDB_DIR_FILE_EXTENSION); + if (result != SMDBE_OK) + return result; + + if (lockcreated) + { + int pag_fd; + + /* Need to pre-open the .pag file as well with O_EXCL */ + result = smdb_lock_file(&pag_fd, db_name, mode, sff, + SMNDB_PAG_FILE_EXTENSION); + if (result != SMDBE_OK) + { + (void) close(lock_fd); + return result; + } + (void) close(pag_fd); + + mode |= O_TRUNC; + mode &= ~(O_CREAT|O_EXCL); + } + + smdb_db = smdb_malloc_database(); + if (smdb_db == NULL) + result = SMDBE_MALLOC; + + db = smdbm_malloc_database(); + if (db == NULL) + result = SMDBE_MALLOC; + + /* Try to open database */ + if (result == SMDBE_OK) + { + db->smndbm_lock_fd = lock_fd; + + errno = 0; + dbm = dbm_open(db_name, mode, DBMMODE); + if (dbm == NULL) + { + if (errno == 0) + result = SMDBE_BAD_OPEN; + else + result = errno; + } + db->smndbm_dbm = dbm; + } + + /* Check for GDBM */ + if (result == SMDBE_OK) + { + if (dbm_dirfno(dbm) == dbm_pagfno(dbm)) + result = SMDBE_GDBM_IS_BAD; + } + + /* Check for filechanged */ + if (result == SMDBE_OK) + { + result = smdb_filechanged(db_name, SMNDB_DIR_FILE_EXTENSION, + dbm_dirfno(dbm), &dir_stat_info); + if (result == SMDBE_OK) + { + result = smdb_filechanged(db_name, + SMNDB_PAG_FILE_EXTENSION, + dbm_pagfno(dbm), + &pag_stat_info); + } + } + + /* XXX Got to get fchown stuff in here */ + + /* Setup driver if everything is ok */ + if (result == SMDBE_OK) + { + *database = smdb_db; + + smdb_db->smdb_close = smdbm_close; + smdb_db->smdb_del = smdbm_del; + smdb_db->smdb_fd = smdbm_fd; + smdb_db->smdb_lockfd = smdbm_lockfd; + smdb_db->smdb_get = smdbm_get; + smdb_db->smdb_put = smdbm_put; + smdb_db->smdb_set_owner = smndbm_set_owner; + smdb_db->smdb_sync = smdbm_sync; + smdb_db->smdb_cursor = smdbm_cursor; + + smdb_db->smdb_impl = db; + + return SMDBE_OK; + } + + /* If we're here, something bad happened, clean up */ + if (dbm != NULL) + dbm_close(dbm); + + smdb_unlock_file(db->smndbm_lock_fd); + free(db); + smdb_free_database(smdb_db); + + return result; +} +#endif /* NDBM */ |
