1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate ** Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers. 3*7c478bd9Sstevel@tonic-gate ** All rights reserved. 4*7c478bd9Sstevel@tonic-gate ** 5*7c478bd9Sstevel@tonic-gate ** By using this file, you agree to the terms and conditions set 6*7c478bd9Sstevel@tonic-gate ** forth in the LICENSE file which can be found at the top level of 7*7c478bd9Sstevel@tonic-gate ** the sendmail distribution. 8*7c478bd9Sstevel@tonic-gate */ 9*7c478bd9Sstevel@tonic-gate 10*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 11*7c478bd9Sstevel@tonic-gate 12*7c478bd9Sstevel@tonic-gate #include <sm/gen.h> 13*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: smdb.c,v 8.58 2004/08/03 20:58:38 ca Exp $") 14*7c478bd9Sstevel@tonic-gate 15*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 16*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 17*7c478bd9Sstevel@tonic-gate #include <unistd.h> 18*7c478bd9Sstevel@tonic-gate 19*7c478bd9Sstevel@tonic-gate 20*7c478bd9Sstevel@tonic-gate #include <sendmail/sendmail.h> 21*7c478bd9Sstevel@tonic-gate #include <libsmdb/smdb.h> 22*7c478bd9Sstevel@tonic-gate 23*7c478bd9Sstevel@tonic-gate static bool smdb_lockfile __P((int, int)); 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate /* 26*7c478bd9Sstevel@tonic-gate ** SMDB_MALLOC_DATABASE -- Allocates a database structure. 27*7c478bd9Sstevel@tonic-gate ** 28*7c478bd9Sstevel@tonic-gate ** Parameters: 29*7c478bd9Sstevel@tonic-gate ** None 30*7c478bd9Sstevel@tonic-gate ** 31*7c478bd9Sstevel@tonic-gate ** Returns: 32*7c478bd9Sstevel@tonic-gate ** An pointer to an allocated SMDB_DATABASE structure or 33*7c478bd9Sstevel@tonic-gate ** NULL if it couldn't allocate the memory. 34*7c478bd9Sstevel@tonic-gate */ 35*7c478bd9Sstevel@tonic-gate 36*7c478bd9Sstevel@tonic-gate SMDB_DATABASE * 37*7c478bd9Sstevel@tonic-gate smdb_malloc_database() 38*7c478bd9Sstevel@tonic-gate { 39*7c478bd9Sstevel@tonic-gate SMDB_DATABASE *db; 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate db = (SMDB_DATABASE *) malloc(sizeof(SMDB_DATABASE)); 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate if (db != NULL) 44*7c478bd9Sstevel@tonic-gate (void) memset(db, '\0', sizeof(SMDB_DATABASE)); 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate return db; 47*7c478bd9Sstevel@tonic-gate } 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate /* 51*7c478bd9Sstevel@tonic-gate ** SMDB_FREE_DATABASE -- Unallocates a database structure. 52*7c478bd9Sstevel@tonic-gate ** 53*7c478bd9Sstevel@tonic-gate ** Parameters: 54*7c478bd9Sstevel@tonic-gate ** database -- a SMDB_DATABASE pointer to deallocate. 55*7c478bd9Sstevel@tonic-gate ** 56*7c478bd9Sstevel@tonic-gate ** Returns: 57*7c478bd9Sstevel@tonic-gate ** None 58*7c478bd9Sstevel@tonic-gate */ 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate void 61*7c478bd9Sstevel@tonic-gate smdb_free_database(database) 62*7c478bd9Sstevel@tonic-gate SMDB_DATABASE *database; 63*7c478bd9Sstevel@tonic-gate { 64*7c478bd9Sstevel@tonic-gate if (database != NULL) 65*7c478bd9Sstevel@tonic-gate free(database); 66*7c478bd9Sstevel@tonic-gate } 67*7c478bd9Sstevel@tonic-gate /* 68*7c478bd9Sstevel@tonic-gate ** SMDB_LOCKFILE -- lock a file using flock or (shudder) fcntl locking 69*7c478bd9Sstevel@tonic-gate ** 70*7c478bd9Sstevel@tonic-gate ** Parameters: 71*7c478bd9Sstevel@tonic-gate ** fd -- the file descriptor of the file. 72*7c478bd9Sstevel@tonic-gate ** type -- type of the lock. Bits can be: 73*7c478bd9Sstevel@tonic-gate ** LOCK_EX -- exclusive lock. 74*7c478bd9Sstevel@tonic-gate ** LOCK_NB -- non-blocking. 75*7c478bd9Sstevel@tonic-gate ** 76*7c478bd9Sstevel@tonic-gate ** Returns: 77*7c478bd9Sstevel@tonic-gate ** true if the lock was acquired. 78*7c478bd9Sstevel@tonic-gate ** false otherwise. 79*7c478bd9Sstevel@tonic-gate */ 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate static bool 82*7c478bd9Sstevel@tonic-gate smdb_lockfile(fd, type) 83*7c478bd9Sstevel@tonic-gate int fd; 84*7c478bd9Sstevel@tonic-gate int type; 85*7c478bd9Sstevel@tonic-gate { 86*7c478bd9Sstevel@tonic-gate int i; 87*7c478bd9Sstevel@tonic-gate int save_errno; 88*7c478bd9Sstevel@tonic-gate #if !HASFLOCK 89*7c478bd9Sstevel@tonic-gate int action; 90*7c478bd9Sstevel@tonic-gate struct flock lfd; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate (void) memset(&lfd, '\0', sizeof lfd); 93*7c478bd9Sstevel@tonic-gate if (bitset(LOCK_UN, type)) 94*7c478bd9Sstevel@tonic-gate lfd.l_type = F_UNLCK; 95*7c478bd9Sstevel@tonic-gate else if (bitset(LOCK_EX, type)) 96*7c478bd9Sstevel@tonic-gate lfd.l_type = F_WRLCK; 97*7c478bd9Sstevel@tonic-gate else 98*7c478bd9Sstevel@tonic-gate lfd.l_type = F_RDLCK; 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate if (bitset(LOCK_NB, type)) 101*7c478bd9Sstevel@tonic-gate action = F_SETLK; 102*7c478bd9Sstevel@tonic-gate else 103*7c478bd9Sstevel@tonic-gate action = F_SETLKW; 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate while ((i = fcntl(fd, action, &lfd)) < 0 && errno == EINTR) 106*7c478bd9Sstevel@tonic-gate continue; 107*7c478bd9Sstevel@tonic-gate if (i >= 0) 108*7c478bd9Sstevel@tonic-gate return true; 109*7c478bd9Sstevel@tonic-gate save_errno = errno; 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate /* 112*7c478bd9Sstevel@tonic-gate ** On SunOS, if you are testing using -oQ/tmp/mqueue or 113*7c478bd9Sstevel@tonic-gate ** -oA/tmp/aliases or anything like that, and /tmp is mounted 114*7c478bd9Sstevel@tonic-gate ** as type "tmp" (that is, served from swap space), the 115*7c478bd9Sstevel@tonic-gate ** previous fcntl will fail with "Invalid argument" errors. 116*7c478bd9Sstevel@tonic-gate ** Since this is fairly common during testing, we will assume 117*7c478bd9Sstevel@tonic-gate ** that this indicates that the lock is successfully grabbed. 118*7c478bd9Sstevel@tonic-gate */ 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate if (save_errno == EINVAL) 121*7c478bd9Sstevel@tonic-gate return true; 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate if (!bitset(LOCK_NB, type) || 124*7c478bd9Sstevel@tonic-gate (save_errno != EACCES && save_errno != EAGAIN)) 125*7c478bd9Sstevel@tonic-gate { 126*7c478bd9Sstevel@tonic-gate # if 0 127*7c478bd9Sstevel@tonic-gate int omode = fcntl(fd, F_GETFL, NULL); 128*7c478bd9Sstevel@tonic-gate int euid = (int) geteuid(); 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 131*7c478bd9Sstevel@tonic-gate filename, ext, fd, type, omode, euid); 132*7c478bd9Sstevel@tonic-gate # endif /* 0 */ 133*7c478bd9Sstevel@tonic-gate errno = save_errno; 134*7c478bd9Sstevel@tonic-gate return false; 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate #else /* !HASFLOCK */ 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate while ((i = flock(fd, type)) < 0 && errno == EINTR) 139*7c478bd9Sstevel@tonic-gate continue; 140*7c478bd9Sstevel@tonic-gate if (i >= 0) 141*7c478bd9Sstevel@tonic-gate return true; 142*7c478bd9Sstevel@tonic-gate save_errno = errno; 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate if (!bitset(LOCK_NB, type) || save_errno != EWOULDBLOCK) 145*7c478bd9Sstevel@tonic-gate { 146*7c478bd9Sstevel@tonic-gate # if 0 147*7c478bd9Sstevel@tonic-gate int omode = fcntl(fd, F_GETFL, NULL); 148*7c478bd9Sstevel@tonic-gate int euid = (int) geteuid(); 149*7c478bd9Sstevel@tonic-gate 150*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 151*7c478bd9Sstevel@tonic-gate filename, ext, fd, type, omode, euid); 152*7c478bd9Sstevel@tonic-gate # endif /* 0 */ 153*7c478bd9Sstevel@tonic-gate errno = save_errno; 154*7c478bd9Sstevel@tonic-gate return false; 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate #endif /* !HASFLOCK */ 157*7c478bd9Sstevel@tonic-gate errno = save_errno; 158*7c478bd9Sstevel@tonic-gate return false; 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate /* 161*7c478bd9Sstevel@tonic-gate ** SMDB_OPEN_DATABASE -- Opens a database. 162*7c478bd9Sstevel@tonic-gate ** 163*7c478bd9Sstevel@tonic-gate ** This opens a database. If type is SMDB_DEFAULT it tries to 164*7c478bd9Sstevel@tonic-gate ** use a DB1 or DB2 hash. If that isn't available, it will try 165*7c478bd9Sstevel@tonic-gate ** to use NDBM. If a specific type is given it will try to open 166*7c478bd9Sstevel@tonic-gate ** a database of that type. 167*7c478bd9Sstevel@tonic-gate ** 168*7c478bd9Sstevel@tonic-gate ** Parameters: 169*7c478bd9Sstevel@tonic-gate ** database -- An pointer to a SMDB_DATABASE pointer where the 170*7c478bd9Sstevel@tonic-gate ** opened database will be stored. This should 171*7c478bd9Sstevel@tonic-gate ** be unallocated. 172*7c478bd9Sstevel@tonic-gate ** db_name -- The name of the database to open. Do not include 173*7c478bd9Sstevel@tonic-gate ** the file name extension. 174*7c478bd9Sstevel@tonic-gate ** mode -- The mode to set on the database file or files. 175*7c478bd9Sstevel@tonic-gate ** mode_mask -- Mode bits that must match on an opened database. 176*7c478bd9Sstevel@tonic-gate ** sff -- Flags to safefile. 177*7c478bd9Sstevel@tonic-gate ** type -- The type of database to open. Supported types 178*7c478bd9Sstevel@tonic-gate ** vary depending on what was compiled in. 179*7c478bd9Sstevel@tonic-gate ** user_info -- Information on the user to use for file 180*7c478bd9Sstevel@tonic-gate ** permissions. 181*7c478bd9Sstevel@tonic-gate ** params -- Params specific to the database being opened. 182*7c478bd9Sstevel@tonic-gate ** Only supports some DB hash options right now 183*7c478bd9Sstevel@tonic-gate ** (see smdb_db_open() for details). 184*7c478bd9Sstevel@tonic-gate ** 185*7c478bd9Sstevel@tonic-gate ** Returns: 186*7c478bd9Sstevel@tonic-gate ** SMDBE_OK -- Success. 187*7c478bd9Sstevel@tonic-gate ** Anything else is an error. Look up more info about the 188*7c478bd9Sstevel@tonic-gate ** error in the comments for the specific open() used. 189*7c478bd9Sstevel@tonic-gate */ 190*7c478bd9Sstevel@tonic-gate 191*7c478bd9Sstevel@tonic-gate int 192*7c478bd9Sstevel@tonic-gate smdb_open_database(database, db_name, mode, mode_mask, sff, type, user_info, 193*7c478bd9Sstevel@tonic-gate params) 194*7c478bd9Sstevel@tonic-gate SMDB_DATABASE **database; 195*7c478bd9Sstevel@tonic-gate char *db_name; 196*7c478bd9Sstevel@tonic-gate int mode; 197*7c478bd9Sstevel@tonic-gate int mode_mask; 198*7c478bd9Sstevel@tonic-gate long sff; 199*7c478bd9Sstevel@tonic-gate SMDB_DBTYPE type; 200*7c478bd9Sstevel@tonic-gate SMDB_USER_INFO *user_info; 201*7c478bd9Sstevel@tonic-gate SMDB_DBPARAMS *params; 202*7c478bd9Sstevel@tonic-gate { 203*7c478bd9Sstevel@tonic-gate bool type_was_default = false; 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate if (type == SMDB_TYPE_DEFAULT) 206*7c478bd9Sstevel@tonic-gate { 207*7c478bd9Sstevel@tonic-gate type_was_default = true; 208*7c478bd9Sstevel@tonic-gate #ifdef NEWDB 209*7c478bd9Sstevel@tonic-gate type = SMDB_TYPE_HASH; 210*7c478bd9Sstevel@tonic-gate #else /* NEWDB */ 211*7c478bd9Sstevel@tonic-gate # ifdef NDBM 212*7c478bd9Sstevel@tonic-gate type = SMDB_TYPE_NDBM; 213*7c478bd9Sstevel@tonic-gate # endif /* NDBM */ 214*7c478bd9Sstevel@tonic-gate #endif /* NEWDB */ 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate if (type == SMDB_TYPE_DEFAULT) 218*7c478bd9Sstevel@tonic-gate return SMDBE_UNKNOWN_DB_TYPE; 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate if ((strncmp(type, SMDB_TYPE_HASH, SMDB_TYPE_HASH_LEN) == 0) || 221*7c478bd9Sstevel@tonic-gate (strncmp(type, SMDB_TYPE_BTREE, SMDB_TYPE_BTREE_LEN) == 0)) 222*7c478bd9Sstevel@tonic-gate { 223*7c478bd9Sstevel@tonic-gate #ifdef NEWDB 224*7c478bd9Sstevel@tonic-gate int result; 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate result = smdb_db_open(database, db_name, mode, mode_mask, sff, 227*7c478bd9Sstevel@tonic-gate type, user_info, params); 228*7c478bd9Sstevel@tonic-gate # ifdef NDBM 229*7c478bd9Sstevel@tonic-gate if (result == ENOENT && type_was_default) 230*7c478bd9Sstevel@tonic-gate type = SMDB_TYPE_NDBM; 231*7c478bd9Sstevel@tonic-gate else 232*7c478bd9Sstevel@tonic-gate # endif /* NDBM */ 233*7c478bd9Sstevel@tonic-gate return result; 234*7c478bd9Sstevel@tonic-gate #else /* NEWDB */ 235*7c478bd9Sstevel@tonic-gate return SMDBE_UNSUPPORTED_DB_TYPE; 236*7c478bd9Sstevel@tonic-gate #endif /* NEWDB */ 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate if (strncmp(type, SMDB_TYPE_NDBM, SMDB_TYPE_NDBM_LEN) == 0) 240*7c478bd9Sstevel@tonic-gate { 241*7c478bd9Sstevel@tonic-gate #ifdef NDBM 242*7c478bd9Sstevel@tonic-gate int result; 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate result = smdb_ndbm_open(database, db_name, mode, mode_mask, 245*7c478bd9Sstevel@tonic-gate sff, type, user_info, params); 246*7c478bd9Sstevel@tonic-gate return result; 247*7c478bd9Sstevel@tonic-gate #else /* NDBM */ 248*7c478bd9Sstevel@tonic-gate return SMDBE_UNSUPPORTED_DB_TYPE; 249*7c478bd9Sstevel@tonic-gate #endif /* NDBM */ 250*7c478bd9Sstevel@tonic-gate } 251*7c478bd9Sstevel@tonic-gate 252*7c478bd9Sstevel@tonic-gate return SMDBE_UNKNOWN_DB_TYPE; 253*7c478bd9Sstevel@tonic-gate } 254*7c478bd9Sstevel@tonic-gate /* 255*7c478bd9Sstevel@tonic-gate ** SMDB_ADD_EXTENSION -- Adds an extension to a file name. 256*7c478bd9Sstevel@tonic-gate ** 257*7c478bd9Sstevel@tonic-gate ** Just adds a . followed by a string to a db_name if there 258*7c478bd9Sstevel@tonic-gate ** is room and the db_name does not already have that extension. 259*7c478bd9Sstevel@tonic-gate ** 260*7c478bd9Sstevel@tonic-gate ** Parameters: 261*7c478bd9Sstevel@tonic-gate ** full_name -- The final file name. 262*7c478bd9Sstevel@tonic-gate ** max_full_name_len -- The max length for full_name. 263*7c478bd9Sstevel@tonic-gate ** db_name -- The name of the db. 264*7c478bd9Sstevel@tonic-gate ** extension -- The extension to add. 265*7c478bd9Sstevel@tonic-gate ** 266*7c478bd9Sstevel@tonic-gate ** Returns: 267*7c478bd9Sstevel@tonic-gate ** SMDBE_OK -- Success. 268*7c478bd9Sstevel@tonic-gate ** Anything else is an error. Look up more info about the 269*7c478bd9Sstevel@tonic-gate ** error in the comments for the specific open() used. 270*7c478bd9Sstevel@tonic-gate */ 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate int 273*7c478bd9Sstevel@tonic-gate smdb_add_extension(full_name, max_full_name_len, db_name, extension) 274*7c478bd9Sstevel@tonic-gate char *full_name; 275*7c478bd9Sstevel@tonic-gate int max_full_name_len; 276*7c478bd9Sstevel@tonic-gate char *db_name; 277*7c478bd9Sstevel@tonic-gate char *extension; 278*7c478bd9Sstevel@tonic-gate { 279*7c478bd9Sstevel@tonic-gate int extension_len; 280*7c478bd9Sstevel@tonic-gate int db_name_len; 281*7c478bd9Sstevel@tonic-gate 282*7c478bd9Sstevel@tonic-gate if (full_name == NULL || db_name == NULL || extension == NULL) 283*7c478bd9Sstevel@tonic-gate return SMDBE_INVALID_PARAMETER; 284*7c478bd9Sstevel@tonic-gate 285*7c478bd9Sstevel@tonic-gate extension_len = strlen(extension); 286*7c478bd9Sstevel@tonic-gate db_name_len = strlen(db_name); 287*7c478bd9Sstevel@tonic-gate 288*7c478bd9Sstevel@tonic-gate if (extension_len + db_name_len + 2 > max_full_name_len) 289*7c478bd9Sstevel@tonic-gate return SMDBE_DB_NAME_TOO_LONG; 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate if (db_name_len < extension_len + 1 || 292*7c478bd9Sstevel@tonic-gate db_name[db_name_len - extension_len - 1] != '.' || 293*7c478bd9Sstevel@tonic-gate strcmp(&db_name[db_name_len - extension_len], extension) != 0) 294*7c478bd9Sstevel@tonic-gate (void) sm_snprintf(full_name, max_full_name_len, "%s.%s", 295*7c478bd9Sstevel@tonic-gate db_name, extension); 296*7c478bd9Sstevel@tonic-gate else 297*7c478bd9Sstevel@tonic-gate (void) sm_strlcpy(full_name, db_name, max_full_name_len); 298*7c478bd9Sstevel@tonic-gate 299*7c478bd9Sstevel@tonic-gate return SMDBE_OK; 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate /* 302*7c478bd9Sstevel@tonic-gate ** SMDB_LOCK_FILE -- Locks the database file. 303*7c478bd9Sstevel@tonic-gate ** 304*7c478bd9Sstevel@tonic-gate ** Locks the actual database file. 305*7c478bd9Sstevel@tonic-gate ** 306*7c478bd9Sstevel@tonic-gate ** Parameters: 307*7c478bd9Sstevel@tonic-gate ** lock_fd -- The resulting descriptor for the locked file. 308*7c478bd9Sstevel@tonic-gate ** db_name -- The name of the database without extension. 309*7c478bd9Sstevel@tonic-gate ** mode -- The open mode. 310*7c478bd9Sstevel@tonic-gate ** sff -- Flags to safefile. 311*7c478bd9Sstevel@tonic-gate ** extension -- The extension for the file. 312*7c478bd9Sstevel@tonic-gate ** 313*7c478bd9Sstevel@tonic-gate ** Returns: 314*7c478bd9Sstevel@tonic-gate ** SMDBE_OK -- Success, otherwise errno. 315*7c478bd9Sstevel@tonic-gate */ 316*7c478bd9Sstevel@tonic-gate 317*7c478bd9Sstevel@tonic-gate int 318*7c478bd9Sstevel@tonic-gate smdb_lock_file(lock_fd, db_name, mode, sff, extension) 319*7c478bd9Sstevel@tonic-gate int *lock_fd; 320*7c478bd9Sstevel@tonic-gate char *db_name; 321*7c478bd9Sstevel@tonic-gate int mode; 322*7c478bd9Sstevel@tonic-gate long sff; 323*7c478bd9Sstevel@tonic-gate char *extension; 324*7c478bd9Sstevel@tonic-gate { 325*7c478bd9Sstevel@tonic-gate int result; 326*7c478bd9Sstevel@tonic-gate char file_name[MAXPATHLEN]; 327*7c478bd9Sstevel@tonic-gate 328*7c478bd9Sstevel@tonic-gate result = smdb_add_extension(file_name, sizeof file_name, db_name, 329*7c478bd9Sstevel@tonic-gate extension); 330*7c478bd9Sstevel@tonic-gate if (result != SMDBE_OK) 331*7c478bd9Sstevel@tonic-gate return result; 332*7c478bd9Sstevel@tonic-gate 333*7c478bd9Sstevel@tonic-gate *lock_fd = safeopen(file_name, mode & ~O_TRUNC, DBMMODE, sff); 334*7c478bd9Sstevel@tonic-gate if (*lock_fd < 0) 335*7c478bd9Sstevel@tonic-gate return errno; 336*7c478bd9Sstevel@tonic-gate 337*7c478bd9Sstevel@tonic-gate return SMDBE_OK; 338*7c478bd9Sstevel@tonic-gate } 339*7c478bd9Sstevel@tonic-gate /* 340*7c478bd9Sstevel@tonic-gate ** SMDB_UNLOCK_FILE -- Unlocks a file 341*7c478bd9Sstevel@tonic-gate ** 342*7c478bd9Sstevel@tonic-gate ** Unlocks a file. 343*7c478bd9Sstevel@tonic-gate ** 344*7c478bd9Sstevel@tonic-gate ** Parameters: 345*7c478bd9Sstevel@tonic-gate ** lock_fd -- The descriptor for the locked file. 346*7c478bd9Sstevel@tonic-gate ** 347*7c478bd9Sstevel@tonic-gate ** Returns: 348*7c478bd9Sstevel@tonic-gate ** SMDBE_OK -- Success, otherwise errno. 349*7c478bd9Sstevel@tonic-gate */ 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate int 352*7c478bd9Sstevel@tonic-gate smdb_unlock_file(lock_fd) 353*7c478bd9Sstevel@tonic-gate int lock_fd; 354*7c478bd9Sstevel@tonic-gate { 355*7c478bd9Sstevel@tonic-gate int result; 356*7c478bd9Sstevel@tonic-gate 357*7c478bd9Sstevel@tonic-gate result = close(lock_fd); 358*7c478bd9Sstevel@tonic-gate if (result != 0) 359*7c478bd9Sstevel@tonic-gate return errno; 360*7c478bd9Sstevel@tonic-gate 361*7c478bd9Sstevel@tonic-gate return SMDBE_OK; 362*7c478bd9Sstevel@tonic-gate } 363*7c478bd9Sstevel@tonic-gate /* 364*7c478bd9Sstevel@tonic-gate ** SMDB_LOCK_MAP -- Locks a database. 365*7c478bd9Sstevel@tonic-gate ** 366*7c478bd9Sstevel@tonic-gate ** Parameters: 367*7c478bd9Sstevel@tonic-gate ** database -- database description. 368*7c478bd9Sstevel@tonic-gate ** type -- type of the lock. Bits can be: 369*7c478bd9Sstevel@tonic-gate ** LOCK_EX -- exclusive lock. 370*7c478bd9Sstevel@tonic-gate ** LOCK_NB -- non-blocking. 371*7c478bd9Sstevel@tonic-gate ** 372*7c478bd9Sstevel@tonic-gate ** Returns: 373*7c478bd9Sstevel@tonic-gate ** SMDBE_OK -- Success, otherwise errno. 374*7c478bd9Sstevel@tonic-gate */ 375*7c478bd9Sstevel@tonic-gate 376*7c478bd9Sstevel@tonic-gate int 377*7c478bd9Sstevel@tonic-gate smdb_lock_map(database, type) 378*7c478bd9Sstevel@tonic-gate SMDB_DATABASE *database; 379*7c478bd9Sstevel@tonic-gate int type; 380*7c478bd9Sstevel@tonic-gate { 381*7c478bd9Sstevel@tonic-gate int fd; 382*7c478bd9Sstevel@tonic-gate 383*7c478bd9Sstevel@tonic-gate fd = database->smdb_lockfd(database); 384*7c478bd9Sstevel@tonic-gate if (fd < 0) 385*7c478bd9Sstevel@tonic-gate return SMDBE_NOT_FOUND; 386*7c478bd9Sstevel@tonic-gate if (!smdb_lockfile(fd, type)) 387*7c478bd9Sstevel@tonic-gate return SMDBE_LOCK_NOT_GRANTED; 388*7c478bd9Sstevel@tonic-gate return SMDBE_OK; 389*7c478bd9Sstevel@tonic-gate } 390*7c478bd9Sstevel@tonic-gate /* 391*7c478bd9Sstevel@tonic-gate ** SMDB_UNLOCK_MAP -- Unlocks a database 392*7c478bd9Sstevel@tonic-gate ** 393*7c478bd9Sstevel@tonic-gate ** Parameters: 394*7c478bd9Sstevel@tonic-gate ** database -- database description. 395*7c478bd9Sstevel@tonic-gate ** 396*7c478bd9Sstevel@tonic-gate ** Returns: 397*7c478bd9Sstevel@tonic-gate ** SMDBE_OK -- Success, otherwise errno. 398*7c478bd9Sstevel@tonic-gate */ 399*7c478bd9Sstevel@tonic-gate 400*7c478bd9Sstevel@tonic-gate int 401*7c478bd9Sstevel@tonic-gate smdb_unlock_map(database) 402*7c478bd9Sstevel@tonic-gate SMDB_DATABASE *database; 403*7c478bd9Sstevel@tonic-gate { 404*7c478bd9Sstevel@tonic-gate int fd; 405*7c478bd9Sstevel@tonic-gate 406*7c478bd9Sstevel@tonic-gate fd = database->smdb_lockfd(database); 407*7c478bd9Sstevel@tonic-gate if (fd < 0) 408*7c478bd9Sstevel@tonic-gate return SMDBE_NOT_FOUND; 409*7c478bd9Sstevel@tonic-gate if (!smdb_lockfile(fd, LOCK_UN)) 410*7c478bd9Sstevel@tonic-gate return SMDBE_LOCK_NOT_HELD; 411*7c478bd9Sstevel@tonic-gate return SMDBE_OK; 412*7c478bd9Sstevel@tonic-gate } 413*7c478bd9Sstevel@tonic-gate /* 414*7c478bd9Sstevel@tonic-gate ** SMDB_SETUP_FILE -- Gets db file ready for use. 415*7c478bd9Sstevel@tonic-gate ** 416*7c478bd9Sstevel@tonic-gate ** Makes sure permissions on file are safe and creates it if it 417*7c478bd9Sstevel@tonic-gate ** doesn't exist. 418*7c478bd9Sstevel@tonic-gate ** 419*7c478bd9Sstevel@tonic-gate ** Parameters: 420*7c478bd9Sstevel@tonic-gate ** db_name -- The name of the database without extension. 421*7c478bd9Sstevel@tonic-gate ** extension -- The extension. 422*7c478bd9Sstevel@tonic-gate ** sff -- Flags to safefile. 423*7c478bd9Sstevel@tonic-gate ** mode_mask -- Mode bits that must match. 424*7c478bd9Sstevel@tonic-gate ** user_info -- Information on the user to use for file 425*7c478bd9Sstevel@tonic-gate ** permissions. 426*7c478bd9Sstevel@tonic-gate ** stat_info -- A place to put the stat info for the file. 427*7c478bd9Sstevel@tonic-gate ** Returns: 428*7c478bd9Sstevel@tonic-gate ** SMDBE_OK -- Success, otherwise errno. 429*7c478bd9Sstevel@tonic-gate */ 430*7c478bd9Sstevel@tonic-gate 431*7c478bd9Sstevel@tonic-gate int 432*7c478bd9Sstevel@tonic-gate smdb_setup_file(db_name, extension, mode_mask, sff, user_info, stat_info) 433*7c478bd9Sstevel@tonic-gate char *db_name; 434*7c478bd9Sstevel@tonic-gate char *extension; 435*7c478bd9Sstevel@tonic-gate int mode_mask; 436*7c478bd9Sstevel@tonic-gate long sff; 437*7c478bd9Sstevel@tonic-gate SMDB_USER_INFO *user_info; 438*7c478bd9Sstevel@tonic-gate struct stat *stat_info; 439*7c478bd9Sstevel@tonic-gate { 440*7c478bd9Sstevel@tonic-gate int st; 441*7c478bd9Sstevel@tonic-gate int result; 442*7c478bd9Sstevel@tonic-gate char db_file_name[MAXPATHLEN]; 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name, 445*7c478bd9Sstevel@tonic-gate extension); 446*7c478bd9Sstevel@tonic-gate if (result != SMDBE_OK) 447*7c478bd9Sstevel@tonic-gate return result; 448*7c478bd9Sstevel@tonic-gate 449*7c478bd9Sstevel@tonic-gate st = safefile(db_file_name, user_info->smdbu_id, 450*7c478bd9Sstevel@tonic-gate user_info->smdbu_group_id, user_info->smdbu_name, 451*7c478bd9Sstevel@tonic-gate sff, mode_mask, stat_info); 452*7c478bd9Sstevel@tonic-gate if (st != 0) 453*7c478bd9Sstevel@tonic-gate return st; 454*7c478bd9Sstevel@tonic-gate 455*7c478bd9Sstevel@tonic-gate return SMDBE_OK; 456*7c478bd9Sstevel@tonic-gate } 457*7c478bd9Sstevel@tonic-gate /* 458*7c478bd9Sstevel@tonic-gate ** SMDB_FILECHANGED -- Checks to see if a file changed. 459*7c478bd9Sstevel@tonic-gate ** 460*7c478bd9Sstevel@tonic-gate ** Compares the passed in stat_info with a current stat on 461*7c478bd9Sstevel@tonic-gate ** the passed in file descriptor. Check filechanged for 462*7c478bd9Sstevel@tonic-gate ** return values. 463*7c478bd9Sstevel@tonic-gate ** 464*7c478bd9Sstevel@tonic-gate ** Parameters: 465*7c478bd9Sstevel@tonic-gate ** db_name -- The name of the database without extension. 466*7c478bd9Sstevel@tonic-gate ** extension -- The extension. 467*7c478bd9Sstevel@tonic-gate ** db_fd -- A file descriptor for the database file. 468*7c478bd9Sstevel@tonic-gate ** stat_info -- An old stat_info. 469*7c478bd9Sstevel@tonic-gate ** Returns: 470*7c478bd9Sstevel@tonic-gate ** SMDBE_OK -- Success, otherwise errno. 471*7c478bd9Sstevel@tonic-gate */ 472*7c478bd9Sstevel@tonic-gate 473*7c478bd9Sstevel@tonic-gate int 474*7c478bd9Sstevel@tonic-gate smdb_filechanged(db_name, extension, db_fd, stat_info) 475*7c478bd9Sstevel@tonic-gate char *db_name; 476*7c478bd9Sstevel@tonic-gate char *extension; 477*7c478bd9Sstevel@tonic-gate int db_fd; 478*7c478bd9Sstevel@tonic-gate struct stat *stat_info; 479*7c478bd9Sstevel@tonic-gate { 480*7c478bd9Sstevel@tonic-gate int result; 481*7c478bd9Sstevel@tonic-gate char db_file_name[MAXPATHLEN]; 482*7c478bd9Sstevel@tonic-gate 483*7c478bd9Sstevel@tonic-gate result = smdb_add_extension(db_file_name, sizeof db_file_name, db_name, 484*7c478bd9Sstevel@tonic-gate extension); 485*7c478bd9Sstevel@tonic-gate if (result != SMDBE_OK) 486*7c478bd9Sstevel@tonic-gate return result; 487*7c478bd9Sstevel@tonic-gate return filechanged(db_file_name, db_fd, stat_info); 488*7c478bd9Sstevel@tonic-gate } 489*7c478bd9Sstevel@tonic-gate /* 490*7c478bd9Sstevel@tonic-gate ** SMDB_PRINT_AVAILABLE_TYPES -- Prints the names of the available types. 491*7c478bd9Sstevel@tonic-gate ** 492*7c478bd9Sstevel@tonic-gate ** Parameters: 493*7c478bd9Sstevel@tonic-gate ** None 494*7c478bd9Sstevel@tonic-gate ** 495*7c478bd9Sstevel@tonic-gate ** Returns: 496*7c478bd9Sstevel@tonic-gate ** None 497*7c478bd9Sstevel@tonic-gate */ 498*7c478bd9Sstevel@tonic-gate 499*7c478bd9Sstevel@tonic-gate void 500*7c478bd9Sstevel@tonic-gate smdb_print_available_types() 501*7c478bd9Sstevel@tonic-gate { 502*7c478bd9Sstevel@tonic-gate #ifdef NDBM 503*7c478bd9Sstevel@tonic-gate printf("dbm\n"); 504*7c478bd9Sstevel@tonic-gate #endif /* NDBM */ 505*7c478bd9Sstevel@tonic-gate #ifdef NEWDB 506*7c478bd9Sstevel@tonic-gate printf("hash\n"); 507*7c478bd9Sstevel@tonic-gate printf("btree\n"); 508*7c478bd9Sstevel@tonic-gate #endif /* NEWDB */ 509*7c478bd9Sstevel@tonic-gate } 510*7c478bd9Sstevel@tonic-gate /* 511*7c478bd9Sstevel@tonic-gate ** SMDB_DB_DEFINITION -- Given a database type, return database definition 512*7c478bd9Sstevel@tonic-gate ** 513*7c478bd9Sstevel@tonic-gate ** Reads though a structure making an association with the database 514*7c478bd9Sstevel@tonic-gate ** type and the required cpp define from sendmail/README. 515*7c478bd9Sstevel@tonic-gate ** List size is dynamic and must be NULL terminated. 516*7c478bd9Sstevel@tonic-gate ** 517*7c478bd9Sstevel@tonic-gate ** Parameters: 518*7c478bd9Sstevel@tonic-gate ** type -- The name of the database type. 519*7c478bd9Sstevel@tonic-gate ** 520*7c478bd9Sstevel@tonic-gate ** Returns: 521*7c478bd9Sstevel@tonic-gate ** definition for type, otherwise NULL. 522*7c478bd9Sstevel@tonic-gate */ 523*7c478bd9Sstevel@tonic-gate 524*7c478bd9Sstevel@tonic-gate typedef struct 525*7c478bd9Sstevel@tonic-gate { 526*7c478bd9Sstevel@tonic-gate SMDB_DBTYPE type; 527*7c478bd9Sstevel@tonic-gate char *dbdef; 528*7c478bd9Sstevel@tonic-gate } dbtype; 529*7c478bd9Sstevel@tonic-gate 530*7c478bd9Sstevel@tonic-gate static dbtype DatabaseDefs[] = 531*7c478bd9Sstevel@tonic-gate { 532*7c478bd9Sstevel@tonic-gate { SMDB_TYPE_HASH, "NEWDB" }, 533*7c478bd9Sstevel@tonic-gate { SMDB_TYPE_BTREE, "NEWDB" }, 534*7c478bd9Sstevel@tonic-gate { SMDB_TYPE_NDBM, "NDBM" }, 535*7c478bd9Sstevel@tonic-gate { NULL, "OOPS" } 536*7c478bd9Sstevel@tonic-gate }; 537*7c478bd9Sstevel@tonic-gate 538*7c478bd9Sstevel@tonic-gate char * 539*7c478bd9Sstevel@tonic-gate smdb_db_definition(type) 540*7c478bd9Sstevel@tonic-gate SMDB_DBTYPE type; 541*7c478bd9Sstevel@tonic-gate { 542*7c478bd9Sstevel@tonic-gate dbtype *ptr = DatabaseDefs; 543*7c478bd9Sstevel@tonic-gate 544*7c478bd9Sstevel@tonic-gate while (ptr != NULL && ptr->type != NULL) 545*7c478bd9Sstevel@tonic-gate { 546*7c478bd9Sstevel@tonic-gate if (strcmp(type, ptr->type) == 0) 547*7c478bd9Sstevel@tonic-gate return ptr->dbdef; 548*7c478bd9Sstevel@tonic-gate ptr++; 549*7c478bd9Sstevel@tonic-gate } 550*7c478bd9Sstevel@tonic-gate return NULL; 551*7c478bd9Sstevel@tonic-gate } 552