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