8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / cmd / sendmail / libsmdb / smndbm.c
blob3327346bd2a69a199c02eb8ddfa9eaae2a632819
1 /*
2 ** Copyright (c) 1999-2002 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 */
10 #pragma ident "%Z%%M% %I% %E% SMI"
12 #include <sm/gen.h>
13 SM_RCSID("@(#)$Id: smndbm.c,v 8.52 2002/05/21 22:30:30 gshapiro Exp $")
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <unistd.h>
19 #include <sendmail/sendmail.h>
20 #include <libsmdb/smdb.h>
22 #ifdef NDBM
24 # define SMNDB_DIR_FILE_EXTENSION "dir"
25 # define SMNDB_PAG_FILE_EXTENSION "pag"
27 struct smdb_dbm_database_struct
29 DBM *smndbm_dbm;
30 int smndbm_lock_fd;
31 bool smndbm_cursor_in_use;
33 typedef struct smdb_dbm_database_struct SMDB_DBM_DATABASE;
35 struct smdb_dbm_cursor_struct
37 SMDB_DBM_DATABASE *smndbmc_db;
38 datum smndbmc_current_key;
40 typedef struct smdb_dbm_cursor_struct SMDB_DBM_CURSOR;
43 ** SMDB_PUT_FLAGS_TO_NDBM_FLAGS -- Translates smdb put flags to ndbm put flags.
45 ** Parameters:
46 ** flags -- The flags to translate.
48 ** Returns:
49 ** The ndbm flags that are equivalent to the smdb flags.
51 ** Notes:
52 ** Any invalid flags are ignored.
56 int
57 smdb_put_flags_to_ndbm_flags(flags)
58 SMDB_FLAG flags;
60 int return_flags;
62 return_flags = 0;
63 if (bitset(SMDBF_NO_OVERWRITE, flags))
64 return_flags = DBM_INSERT;
65 else
66 return_flags = DBM_REPLACE;
68 return return_flags;
72 ** Except for smdb_ndbm_open, the rest of these function correspond to the
73 ** interface laid out in smdb.h.
76 SMDB_DBM_DATABASE *
77 smdbm_malloc_database()
79 SMDB_DBM_DATABASE *db;
81 db = (SMDB_DBM_DATABASE *) malloc(sizeof(SMDB_DBM_DATABASE));
82 if (db != NULL)
84 db->smndbm_dbm = NULL;
85 db->smndbm_lock_fd = -1;
86 db->smndbm_cursor_in_use = false;
89 return db;
92 int
93 smdbm_close(database)
94 SMDB_DATABASE *database;
96 SMDB_DBM_DATABASE *db = (SMDB_DBM_DATABASE *) database->smdb_impl;
97 DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
99 dbm_close(dbm);
100 if (db->smndbm_lock_fd != -1)
101 close(db->smndbm_lock_fd);
103 free(db);
104 database->smdb_impl = NULL;
106 return SMDBE_OK;
110 smdbm_del(database, key, flags)
111 SMDB_DATABASE *database;
112 SMDB_DBENT *key;
113 unsigned int flags;
115 int result;
116 DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
117 datum dbkey;
119 (void) memset(&dbkey, '\0', sizeof dbkey);
120 dbkey.dptr = key->data;
121 dbkey.dsize = key->size;
123 errno = 0;
124 result = dbm_delete(dbm, dbkey);
125 if (result != 0)
127 int save_errno = errno;
129 if (dbm_error(dbm))
130 return SMDBE_IO_ERROR;
132 if (save_errno != 0)
133 return save_errno;
135 return SMDBE_NOT_FOUND;
137 return SMDBE_OK;
141 smdbm_fd(database, fd)
142 SMDB_DATABASE *database;
143 int *fd;
145 DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
147 *fd = dbm_dirfno(dbm);
148 if (*fd <= 0)
149 return EINVAL;
151 return SMDBE_OK;
155 smdbm_lockfd(database)
156 SMDB_DATABASE *database;
158 SMDB_DBM_DATABASE *db = (SMDB_DBM_DATABASE *) database->smdb_impl;
160 return db->smndbm_lock_fd;
164 smdbm_get(database, key, data, flags)
165 SMDB_DATABASE *database;
166 SMDB_DBENT *key;
167 SMDB_DBENT *data;
168 unsigned int flags;
170 DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
171 datum dbkey, dbdata;
173 (void) memset(&dbkey, '\0', sizeof dbkey);
174 (void) memset(&dbdata, '\0', sizeof dbdata);
175 dbkey.dptr = key->data;
176 dbkey.dsize = key->size;
178 errno = 0;
179 dbdata = dbm_fetch(dbm, dbkey);
180 if (dbdata.dptr == NULL)
182 int save_errno = errno;
184 if (dbm_error(dbm))
185 return SMDBE_IO_ERROR;
187 if (save_errno != 0)
188 return save_errno;
190 return SMDBE_NOT_FOUND;
192 data->data = dbdata.dptr;
193 data->size = dbdata.dsize;
194 return SMDBE_OK;
198 smdbm_put(database, key, data, flags)
199 SMDB_DATABASE *database;
200 SMDB_DBENT *key;
201 SMDB_DBENT *data;
202 unsigned int flags;
204 int result;
205 int save_errno;
206 DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
207 datum dbkey, dbdata;
209 (void) memset(&dbkey, '\0', sizeof dbkey);
210 (void) memset(&dbdata, '\0', sizeof dbdata);
211 dbkey.dptr = key->data;
212 dbkey.dsize = key->size;
213 dbdata.dptr = data->data;
214 dbdata.dsize = data->size;
216 errno = 0;
217 result = dbm_store(dbm, dbkey, dbdata,
218 smdb_put_flags_to_ndbm_flags(flags));
219 switch (result)
221 case 1:
222 return SMDBE_DUPLICATE;
224 case 0:
225 return SMDBE_OK;
227 default:
228 save_errno = errno;
230 if (dbm_error(dbm))
231 return SMDBE_IO_ERROR;
233 if (save_errno != 0)
234 return save_errno;
236 return SMDBE_IO_ERROR;
238 /* NOTREACHED */
242 smndbm_set_owner(database, uid, gid)
243 SMDB_DATABASE *database;
244 uid_t uid;
245 gid_t gid;
247 # if HASFCHOWN
248 int fd;
249 int result;
250 DBM *dbm = ((SMDB_DBM_DATABASE *) database->smdb_impl)->smndbm_dbm;
252 fd = dbm_dirfno(dbm);
253 if (fd <= 0)
254 return EINVAL;
256 result = fchown(fd, uid, gid);
257 if (result < 0)
258 return errno;
260 fd = dbm_pagfno(dbm);
261 if (fd <= 0)
262 return EINVAL;
264 result = fchown(fd, uid, gid);
265 if (result < 0)
266 return errno;
267 # endif /* HASFCHOWN */
269 return SMDBE_OK;
273 smdbm_sync(database, flags)
274 SMDB_DATABASE *database;
275 unsigned int flags;
277 return SMDBE_UNSUPPORTED;
281 smdbm_cursor_close(cursor)
282 SMDB_CURSOR *cursor;
284 SMDB_DBM_CURSOR *dbm_cursor = (SMDB_DBM_CURSOR *) cursor->smdbc_impl;
285 SMDB_DBM_DATABASE *db = dbm_cursor->smndbmc_db;
287 if (!db->smndbm_cursor_in_use)
288 return SMDBE_NOT_A_VALID_CURSOR;
290 db->smndbm_cursor_in_use = false;
291 free(dbm_cursor);
292 free(cursor);
294 return SMDBE_OK;
298 smdbm_cursor_del(cursor, flags)
299 SMDB_CURSOR *cursor;
300 unsigned int flags;
302 int result;
303 SMDB_DBM_CURSOR *dbm_cursor = (SMDB_DBM_CURSOR *) cursor->smdbc_impl;
304 SMDB_DBM_DATABASE *db = dbm_cursor->smndbmc_db;
305 DBM *dbm = db->smndbm_dbm;
307 errno = 0;
308 result = dbm_delete(dbm, dbm_cursor->smndbmc_current_key);
309 if (result != 0)
311 int save_errno = errno;
313 if (dbm_error(dbm))
314 return SMDBE_IO_ERROR;
316 if (save_errno != 0)
317 return save_errno;
319 return SMDBE_NOT_FOUND;
321 return SMDBE_OK;
325 smdbm_cursor_get(cursor, key, value, flags)
326 SMDB_CURSOR *cursor;
327 SMDB_DBENT *key;
328 SMDB_DBENT *value;
329 SMDB_FLAG flags;
331 SMDB_DBM_CURSOR *dbm_cursor = (SMDB_DBM_CURSOR *) cursor->smdbc_impl;
332 SMDB_DBM_DATABASE *db = dbm_cursor->smndbmc_db;
333 DBM *dbm = db->smndbm_dbm;
334 datum dbkey, dbdata;
336 (void) memset(&dbkey, '\0', sizeof dbkey);
337 (void) memset(&dbdata, '\0', sizeof dbdata);
339 if (flags == SMDB_CURSOR_GET_RANGE)
340 return SMDBE_UNSUPPORTED;
342 if (dbm_cursor->smndbmc_current_key.dptr == NULL)
344 dbm_cursor->smndbmc_current_key = dbm_firstkey(dbm);
345 if (dbm_cursor->smndbmc_current_key.dptr == NULL)
347 if (dbm_error(dbm))
348 return SMDBE_IO_ERROR;
349 return SMDBE_LAST_ENTRY;
352 else
354 dbm_cursor->smndbmc_current_key = dbm_nextkey(dbm);
355 if (dbm_cursor->smndbmc_current_key.dptr == NULL)
357 if (dbm_error(dbm))
358 return SMDBE_IO_ERROR;
359 return SMDBE_LAST_ENTRY;
363 errno = 0;
364 dbdata = dbm_fetch(dbm, dbm_cursor->smndbmc_current_key);
365 if (dbdata.dptr == NULL)
367 int save_errno = errno;
369 if (dbm_error(dbm))
370 return SMDBE_IO_ERROR;
372 if (save_errno != 0)
373 return save_errno;
375 return SMDBE_NOT_FOUND;
377 value->data = dbdata.dptr;
378 value->size = dbdata.dsize;
379 key->data = dbm_cursor->smndbmc_current_key.dptr;
380 key->size = dbm_cursor->smndbmc_current_key.dsize;
382 return SMDBE_OK;
386 smdbm_cursor_put(cursor, key, value, flags)
387 SMDB_CURSOR *cursor;
388 SMDB_DBENT *key;
389 SMDB_DBENT *value;
390 SMDB_FLAG flags;
392 int result;
393 int save_errno;
394 SMDB_DBM_CURSOR *dbm_cursor = (SMDB_DBM_CURSOR *) cursor->smdbc_impl;
395 SMDB_DBM_DATABASE *db = dbm_cursor->smndbmc_db;
396 DBM *dbm = db->smndbm_dbm;
397 datum dbdata;
399 (void) memset(&dbdata, '\0', sizeof dbdata);
400 dbdata.dptr = value->data;
401 dbdata.dsize = value->size;
403 errno = 0;
404 result = dbm_store(dbm, dbm_cursor->smndbmc_current_key, dbdata,
405 smdb_put_flags_to_ndbm_flags(flags));
406 switch (result)
408 case 1:
409 return SMDBE_DUPLICATE;
411 case 0:
412 return SMDBE_OK;
414 default:
415 save_errno = errno;
417 if (dbm_error(dbm))
418 return SMDBE_IO_ERROR;
420 if (save_errno != 0)
421 return save_errno;
423 return SMDBE_IO_ERROR;
425 /* NOTREACHED */
429 smdbm_cursor(database, cursor, flags)
430 SMDB_DATABASE *database;
431 SMDB_CURSOR **cursor;
432 SMDB_FLAG flags;
434 SMDB_DBM_DATABASE *db = (SMDB_DBM_DATABASE *) database->smdb_impl;
435 SMDB_CURSOR *cur;
436 SMDB_DBM_CURSOR *dbm_cursor;
438 if (db->smndbm_cursor_in_use)
439 return SMDBE_ONLY_SUPPORTS_ONE_CURSOR;
441 db->smndbm_cursor_in_use = true;
442 dbm_cursor = (SMDB_DBM_CURSOR *) malloc(sizeof(SMDB_DBM_CURSOR));
443 dbm_cursor->smndbmc_db = db;
444 dbm_cursor->smndbmc_current_key.dptr = NULL;
445 dbm_cursor->smndbmc_current_key.dsize = 0;
447 cur = (SMDB_CURSOR*) malloc(sizeof(SMDB_CURSOR));
448 if (cur == NULL)
449 return SMDBE_MALLOC;
451 cur->smdbc_impl = dbm_cursor;
452 cur->smdbc_close = smdbm_cursor_close;
453 cur->smdbc_del = smdbm_cursor_del;
454 cur->smdbc_get = smdbm_cursor_get;
455 cur->smdbc_put = smdbm_cursor_put;
456 *cursor = cur;
458 return SMDBE_OK;
461 ** SMDB_NDBM_OPEN -- Opens a ndbm database.
463 ** Parameters:
464 ** database -- An unallocated database pointer to a pointer.
465 ** db_name -- The name of the database without extension.
466 ** mode -- File permisions on a created database.
467 ** mode_mask -- Mode bits that much match on an opened database.
468 ** sff -- Flags to safefile.
469 ** type -- The type of database to open.
470 ** Only SMDB_NDBM is supported.
471 ** user_info -- Information on the user to use for file
472 ** permissions.
473 ** db_params -- No params are supported.
475 ** Returns:
476 ** SMDBE_OK -- Success, otherwise errno:
477 ** SMDBE_MALLOC -- Cannot allocate memory.
478 ** SMDBE_UNSUPPORTED -- The type is not supported.
479 ** SMDBE_GDBM_IS_BAD -- We have detected GDBM and we don't
480 ** like it.
481 ** SMDBE_BAD_OPEN -- dbm_open failed and errno was not set.
482 ** Anything else: errno
486 smdb_ndbm_open(database, db_name, mode, mode_mask, sff, type, user_info,
487 db_params)
488 SMDB_DATABASE **database;
489 char *db_name;
490 int mode;
491 int mode_mask;
492 long sff;
493 SMDB_DBTYPE type;
494 SMDB_USER_INFO *user_info;
495 SMDB_DBPARAMS *db_params;
497 bool lockcreated = false;
498 int result;
499 int lock_fd;
500 SMDB_DATABASE *smdb_db;
501 SMDB_DBM_DATABASE *db;
502 DBM *dbm = NULL;
503 struct stat dir_stat_info;
504 struct stat pag_stat_info;
506 result = SMDBE_OK;
507 *database = NULL;
509 if (type == NULL)
510 return SMDBE_UNKNOWN_DB_TYPE;
512 result = smdb_setup_file(db_name, SMNDB_DIR_FILE_EXTENSION, mode_mask,
513 sff, user_info, &dir_stat_info);
514 if (result != SMDBE_OK)
515 return result;
517 result = smdb_setup_file(db_name, SMNDB_PAG_FILE_EXTENSION, mode_mask,
518 sff, user_info, &pag_stat_info);
519 if (result != SMDBE_OK)
520 return result;
522 if ((dir_stat_info.st_mode == ST_MODE_NOFILE ||
523 pag_stat_info.st_mode == ST_MODE_NOFILE) &&
524 bitset(mode, O_CREAT))
525 lockcreated = true;
527 lock_fd = -1;
528 result = smdb_lock_file(&lock_fd, db_name, mode, sff,
529 SMNDB_DIR_FILE_EXTENSION);
530 if (result != SMDBE_OK)
531 return result;
533 if (lockcreated)
535 int pag_fd;
537 /* Need to pre-open the .pag file as well with O_EXCL */
538 result = smdb_lock_file(&pag_fd, db_name, mode, sff,
539 SMNDB_PAG_FILE_EXTENSION);
540 if (result != SMDBE_OK)
542 (void) close(lock_fd);
543 return result;
545 (void) close(pag_fd);
547 mode |= O_TRUNC;
548 mode &= ~(O_CREAT|O_EXCL);
551 smdb_db = smdb_malloc_database();
552 if (smdb_db == NULL)
553 result = SMDBE_MALLOC;
555 db = smdbm_malloc_database();
556 if (db == NULL)
557 result = SMDBE_MALLOC;
559 /* Try to open database */
560 if (result == SMDBE_OK)
562 db->smndbm_lock_fd = lock_fd;
564 errno = 0;
565 dbm = dbm_open(db_name, mode, DBMMODE);
566 if (dbm == NULL)
568 if (errno == 0)
569 result = SMDBE_BAD_OPEN;
570 else
571 result = errno;
573 db->smndbm_dbm = dbm;
576 /* Check for GDBM */
577 if (result == SMDBE_OK)
579 if (dbm_dirfno(dbm) == dbm_pagfno(dbm))
580 result = SMDBE_GDBM_IS_BAD;
583 /* Check for filechanged */
584 if (result == SMDBE_OK)
586 result = smdb_filechanged(db_name, SMNDB_DIR_FILE_EXTENSION,
587 dbm_dirfno(dbm), &dir_stat_info);
588 if (result == SMDBE_OK)
590 result = smdb_filechanged(db_name,
591 SMNDB_PAG_FILE_EXTENSION,
592 dbm_pagfno(dbm),
593 &pag_stat_info);
597 /* XXX Got to get fchown stuff in here */
599 /* Setup driver if everything is ok */
600 if (result == SMDBE_OK)
602 *database = smdb_db;
604 smdb_db->smdb_close = smdbm_close;
605 smdb_db->smdb_del = smdbm_del;
606 smdb_db->smdb_fd = smdbm_fd;
607 smdb_db->smdb_lockfd = smdbm_lockfd;
608 smdb_db->smdb_get = smdbm_get;
609 smdb_db->smdb_put = smdbm_put;
610 smdb_db->smdb_set_owner = smndbm_set_owner;
611 smdb_db->smdb_sync = smdbm_sync;
612 smdb_db->smdb_cursor = smdbm_cursor;
614 smdb_db->smdb_impl = db;
616 return SMDBE_OK;
619 /* If we're here, something bad happened, clean up */
620 if (dbm != NULL)
621 dbm_close(dbm);
623 smdb_unlock_file(db->smndbm_lock_fd);
624 free(db);
625 smdb_free_database(smdb_db);
627 return result;
629 #endif /* NDBM */