summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libsmdb
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/sendmail/libsmdb')
-rwxr-xr-xcontrib/sendmail/libsmdb/Build13
-rw-r--r--contrib/sendmail/libsmdb/Makefile17
-rw-r--r--contrib/sendmail/libsmdb/Makefile.m413
-rw-r--r--contrib/sendmail/libsmdb/smdb.c412
-rw-r--r--contrib/sendmail/libsmdb/smdb1.c507
-rw-r--r--contrib/sendmail/libsmdb/smdb2.c646
-rw-r--r--contrib/sendmail/libsmdb/smndbm.c579
7 files changed, 2187 insertions, 0 deletions
diff --git a/contrib/sendmail/libsmdb/Build b/contrib/sendmail/libsmdb/Build
new file mode 100755
index 0000000..014c45c
--- /dev/null
+++ b/contrib/sendmail/libsmdb/Build
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# Copyright (c) 1999-2000 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.
+#
+#
+# $Id: Build,v 8.2.2.1 2000/04/10 06:41:07 gshapiro Exp $
+
+exec sh ../devtools/bin/Build $*
diff --git a/contrib/sendmail/libsmdb/Makefile b/contrib/sendmail/libsmdb/Makefile
new file mode 100644
index 0000000..0422ed5
--- /dev/null
+++ b/contrib/sendmail/libsmdb/Makefile
@@ -0,0 +1,17 @@
+# $Id: Makefile,v 8.2 1999/09/23 22:36:29 ca Exp $
+
+SHELL= /bin/sh
+BUILD= ./Build
+OPTIONS= $(CONFIG) $(FLAGS)
+
+all: FRC
+ $(SHELL) $(BUILD) $(OPTIONS) $@
+clean: FRC
+ $(SHELL) $(BUILD) $(OPTIONS) $@
+install: FRC
+ $(SHELL) $(BUILD) $(OPTIONS) $@
+
+fresh: FRC
+ $(SHELL) $(BUILD) $(OPTIONS) -c
+
+FRC:
diff --git a/contrib/sendmail/libsmdb/Makefile.m4 b/contrib/sendmail/libsmdb/Makefile.m4
new file mode 100644
index 0000000..f24c0a0
--- /dev/null
+++ b/contrib/sendmail/libsmdb/Makefile.m4
@@ -0,0 +1,13 @@
+include(confBUILDTOOLSDIR`/M4/switch.m4')
+
+# sendmail dir
+SMSRCDIR= ifdef(`confSMSRCDIR', `confSMSRCDIR', `${SRCDIR}/sendmail')
+PREPENDDEF(`confENVDEF', `confMAPDEF')
+PREPENDDEF(`confINCDIRS', `-I${SMSRCDIR} ')
+
+bldPRODUCT_START(`library', `libsmdb')
+define(`bldSOURCES', `smdb.c smdb1.c smdb2.c smndbm.c ')
+APPENDDEF(`confENVDEF', `-DNOT_SENDMAIL')
+bldPRODUCT_END
+
+bldFINISH
diff --git a/contrib/sendmail/libsmdb/smdb.c b/contrib/sendmail/libsmdb/smdb.c
new file mode 100644
index 0000000..ee563a1
--- /dev/null
+++ b/contrib/sendmail/libsmdb/smdb.c
@@ -0,0 +1,412 @@
+/*
+** Copyright (c) 1999-2000 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.
+*/
+
+#ifndef lint
+static char id[] = "@(#)$Id: smdb.c,v 8.37.4.1 2000/05/25 18:56:09 gshapiro Exp $";
+#endif /* ! lint */
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+
+#include <sendmail/sendmail.h>
+#include <libsmdb/smdb.h>
+
+ /*
+** 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)
+ 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_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;
+{
+ int result;
+ 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
+ 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
+ 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)
+ snprintf(full_name, max_full_name_len, "%s.%s", db_name,
+ extension);
+ else
+ (void) 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[SMDB_MAX_NAME_LEN];
+
+ result = smdb_add_extension(file_name, SMDB_MAX_NAME_LEN, db_name,
+ extension);
+ if (result != SMDBE_OK)
+ return result;
+
+ *lock_fd = safeopen(file_name, mode & ~O_TRUNC, 0644, 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_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[SMDB_MAX_NAME_LEN];
+
+ result = smdb_add_extension(db_file_name, SMDB_MAX_NAME_LEN, 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[SMDB_MAX_NAME_LEN];
+
+ result = smdb_add_extension(db_file_name, SMDB_MAX_NAME_LEN, db_name,
+ extension);
+ if (result != SMDBE_OK)
+ return result;
+
+ result = filechanged(db_file_name, db_fd, stat_info);
+
+ return result;
+}
+ /*
+** 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/contrib/sendmail/libsmdb/smdb1.c b/contrib/sendmail/libsmdb/smdb1.c
new file mode 100644
index 0000000..59b11e5
--- /dev/null
+++ b/contrib/sendmail/libsmdb/smdb1.c
@@ -0,0 +1,507 @@
+/*
+** Copyright (c) 1999-2000 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.
+*/
+
+#ifndef lint
+static char id[] = "@(#)$Id: smdb1.c,v 8.43 2000/03/17 07:32:43 gshapiro Exp $";
+#endif /* ! lint */
+
+#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;
+
+ /*
+** 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.
+**
+*/
+
+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.
+**
+*/
+
+u_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.
+**
+*/
+
+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;
+ }
+}
+
+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;
+}
+
+/*
+** The rest of these function correspond to the interface laid out
+** in smdb.h.
+*/
+
+int
+smdb1_close(database)
+ SMDB_DATABASE *database;
+{
+ SMDB_DB1_DATABASE *db1 = (SMDB_DB1_DATABASE *) database->smdb_impl;
+ DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db;
+
+ if (db1->smdb1_lock_fd != -1)
+ (void) close(db1->smdb1_lock_fd);
+
+ free(db1);
+ database->smdb_impl = NULL;
+
+ return db->close(db);
+}
+
+int
+smdb1_del(database, key, flags)
+ SMDB_DATABASE *database;
+ SMDB_DBENT *key;
+ u_int flags;
+{
+ DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db;
+
+ return db->del(db, &key->db, flags);
+}
+
+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;
+}
+
+int
+smdb1_get(database, key, data, flags)
+ SMDB_DATABASE *database;
+ SMDB_DBENT *key;
+ SMDB_DBENT *data;
+ u_int flags;
+{
+ int result;
+ DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db;
+
+ result = db->get(db, &key->db, &data->db, flags);
+ if (result != 0)
+ {
+ if (result == 1)
+ return SMDBE_NOT_FOUND;
+ return errno;
+ }
+ return SMDBE_OK;
+}
+
+int
+smdb1_put(database, key, data, flags)
+ SMDB_DATABASE *database;
+ SMDB_DBENT *key;
+ SMDB_DBENT *data;
+ u_int flags;
+{
+ DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db;
+
+ return db->put(db, &key->db, &data->db,
+ smdb_put_flags_to_db1_flags(flags));
+}
+
+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;
+}
+
+int
+smdb1_sync(database, flags)
+ SMDB_DATABASE *database;
+ u_int flags;
+{
+ DB *db = ((SMDB_DB1_DATABASE *) database->smdb_impl)->smdb1_db;
+
+ return db->sync(db, flags);
+}
+
+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;
+}
+
+int
+smdb1_cursor_del(cursor, flags)
+ SMDB_CURSOR *cursor;
+ u_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);
+}
+
+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;
+
+ db1_flags = smdb_cursor_get_flags_to_smdb1(flags);
+ result = db->seq(db, &key->db, &value->db, db1_flags);
+ if (result == -1)
+ return errno;
+ if (result == 1)
+ return SMDBE_LAST_ENTRY;
+ return SMDBE_OK;
+}
+
+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;
+
+ return db->put(db, &key->db, &value->db, R_CURSOR);
+}
+
+int
+smdb1_cursor(database, cursor, flags)
+ SMDB_DATABASE *database;
+ SMDB_CURSOR **cursor;
+ u_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;
+{
+ 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[SMDB_MAX_NAME_LEN];
+
+ 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, SMDB_MAX_NAME_LEN,
+ 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;
+
+ lock_fd = -1;
+# if O_EXLOCK
+ mode |= O_EXLOCK;
+# else /* O_EXLOCK */
+ result = smdb_lock_file(&lock_fd, db_name, mode, sff,
+ SMDB1_FILE_EXTENSION);
+ if (result != SMDBE_OK)
+ return result;
+# endif /* O_EXLOCK */
+
+ *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))
+ {
+ 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))
+ {
+ 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, 0644, 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_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/contrib/sendmail/libsmdb/smdb2.c b/contrib/sendmail/libsmdb/smdb2.c
new file mode 100644
index 0000000..aa9395b
--- /dev/null
+++ b/contrib/sendmail/libsmdb/smdb2.c
@@ -0,0 +1,646 @@
+/*
+** Copyright (c) 1999-2000 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.
+*/
+
+#ifndef lint
+static char id[] = "@(#)$Id: smdb2.c,v 8.53.2.1.2.1 2000/05/25 18:56:10 gshapiro Exp $";
+#endif /* ! lint */
+
+#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.
+**
+*/
+
+u_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;
+ }
+}
+
+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;
+}
+
+
+/*
+** Except for smdb_db_open, the rest of these function correspond to the
+** interface laid out in smdb.h.
+*/
+
+int
+smdb2_close(database)
+ SMDB_DATABASE *database;
+{
+ SMDB_DB2_DATABASE *db2 = (SMDB_DB2_DATABASE *) database->smdb_impl;
+ DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db;
+
+ if (db2->smdb2_lock_fd != -1)
+ close(db2->smdb2_lock_fd);
+
+ free(db2);
+ database->smdb_impl = NULL;
+
+ return db2_error_to_smdb(db->close(db, 0));
+}
+
+int
+smdb2_del(database, key, flags)
+ SMDB_DATABASE *database;
+ SMDB_DBENT *key;
+ u_int flags;
+{
+ DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db;
+
+ return db2_error_to_smdb(db->del(db, NULL, &key->db, 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_get(database, key, data, flags)
+ SMDB_DATABASE *database;
+ SMDB_DBENT *key;
+ SMDB_DBENT *data;
+ u_int flags;
+{
+ DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db;
+
+ return db2_error_to_smdb(db->get(db, NULL, &key->db, &data->db, flags));
+}
+
+int
+smdb2_put(database, key, data, flags)
+ SMDB_DATABASE *database;
+ SMDB_DBENT *key;
+ SMDB_DBENT *data;
+ u_int flags;
+{
+ DB *db = ((SMDB_DB2_DATABASE *) database->smdb_impl)->smdb2_db;
+
+ return db2_error_to_smdb(db->put(db, NULL, &key->db, &data->db,
+ 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;
+ u_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;
+{
+ DBC *dbc = (DBC *) cursor->smdbc_impl;
+
+ return db2_error_to_smdb(dbc->c_close(dbc));
+}
+
+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;
+
+ db2_flags = smdb_cursor_get_flags_to_db2(flags);
+ result = dbc->c_get(dbc, &key->db, &value->db, db2_flags);
+ if (result == DB_NOTFOUND)
+ return SMDBE_LAST_ENTRY;
+ 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;
+
+ return db2_error_to_smdb(dbc->c_put(dbc, &key->db, &value->db, 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;
+ 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, 0644, 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, db_name, NULL, db_type, db_flags, 0644);
+ 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;
+ SMDB_DATABASE *smdb_db;
+ SMDB_DB2_DATABASE *db2;
+ DB *db;
+ DBTYPE db_type;
+ struct stat stat_info;
+ char db_file_name[SMDB_MAX_NAME_LEN];
+
+ *database = NULL;
+
+ result = smdb_add_extension(db_file_name, SMDB_MAX_NAME_LEN,
+ 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;
+# if !HASFLOCK && defined(DB_FCNTL_LOCKING)
+ db_flags |= DB_FCNTL_LOCKING;
+# endif /* !HASFLOCK && defined(DB_FCNTL_LOCKING) */
+
+ 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_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/contrib/sendmail/libsmdb/smndbm.c b/contrib/sendmail/libsmdb/smndbm.c
new file mode 100644
index 0000000..f5bf388
--- /dev/null
+++ b/contrib/sendmail/libsmdb/smndbm.c
@@ -0,0 +1,579 @@
+/*
+** Copyright (c) 1999-2000 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.
+*/
+
+#ifndef lint
+static char id[] = "@(#)$Id: smndbm.c,v 8.40 2000/03/19 05:03:30 ca Exp $";
+#endif /* ! lint */
+
+#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;
+}
+
+ /*
+** smdbm_malloc_database -- Create and initialize SMDB_DBM_DATABASE
+**
+** Parameters:
+** None
+**
+** Returns:
+** A pointer to an allocated SMDB_DBM_DATABASE or NULL
+**
+*/
+
+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;
+}
+
+/*
+** Except for smdb_ndbm_open, the rest of these function correspond to the
+** interface laid out in smdb.h.
+*/
+
+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;
+ u_int flags;
+{
+ int result;
+ DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
+
+ errno = 0;
+ result = dbm_delete(dbm, key->dbm);
+ 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_get(database, key, data, flags)
+ SMDB_DATABASE *database;
+ SMDB_DBENT *key;
+ SMDB_DBENT *data;
+ u_int flags;
+{
+ DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
+
+ errno = 0;
+ data->dbm = dbm_fetch(dbm, key->dbm);
+ if (data->dbm.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;
+ }
+
+ return SMDBE_OK;
+}
+
+int
+smdbm_put(database, key, data, flags)
+ SMDB_DATABASE *database;
+ SMDB_DBENT *key;
+ SMDB_DBENT *data;
+ u_int flags;
+{
+ int result;
+ int save_errno;
+ DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
+
+ errno = 0;
+ result = dbm_store(dbm, key->dbm, data->dbm,
+ 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;
+ u_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;
+ u_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;
+
+ 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;
+ value->dbm = dbm_fetch(dbm, dbm_cursor->smndbmc_current_key);
+ if (value->dbm.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;
+ }
+ key->dbm = dbm_cursor->smndbmc_current_key;
+
+ 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;
+
+ errno = 0;
+ result = dbm_store(dbm, dbm_cursor->smndbmc_current_key, value->dbm,
+ 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;
+{
+ 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;
+
+ lock_fd = -1;
+# if O_EXLOCK
+ mode |= O_EXLOCK;
+# else /* O_EXLOCK */
+ result = smdb_lock_file(&lock_fd, db_name, mode, sff,
+ SMNDB_DIR_FILE_EXTENSION);
+ if (result != SMDBE_OK)
+ return result;
+# endif /* O_EXLOCK */
+
+ 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, 0644);
+ 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_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 */
OpenPOWER on IntegriCloud