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 * $Id: smdb.h,v 8.38 2001/11/19 19:30:03 gshapiro Exp $ 10 * 11 */ 12 13 #ifndef _SMDB_H_ 14 # define _SMDB_H_ 15 16 # include <sys/types.h> 17 # include <sys/stat.h> 18 # include <sm/gen.h> 19 # include <sm/errstring.h> 20 21 # ifdef NDBM 22 # include <ndbm.h> 23 # endif /* NDBM */ 24 25 # ifdef NEWDB 26 # include <db.h> 27 # ifndef DB_VERSION_MAJOR 28 # define DB_VERSION_MAJOR 1 29 # endif /* ! DB_VERSION_MAJOR */ 30 # endif /* NEWDB */ 31 32 /* 33 ** Some size constants 34 */ 35 36 #define SMDB_MAX_USER_NAME_LEN 1024 37 #define SMDB_MAX_NAME_LEN 1024 38 39 /* 40 ** This file defines the abstraction for database lookups. It is pretty 41 ** much a copy of the db2 interface with the exception that every function 42 ** returns 0 on success and non-zero on failure. The non-zero return code 43 ** is meaningful. 44 ** 45 ** I'm going to put the function comments in this file since the interface 46 ** MUST be the same for all inheritors of this interface. 47 */ 48 49 typedef struct database_struct SMDB_DATABASE; 50 typedef struct cursor_struct SMDB_CURSOR; 51 typedef struct entry_struct SMDB_DBENT; 52 53 /* 54 ** DB_CLOSE_FUNC -- close the database 55 ** 56 ** Parameters: 57 ** db -- The database to close. 58 ** 59 ** Returns: 60 ** 0 - Success, otherwise errno. 61 ** 62 */ 63 64 typedef int (*db_close_func) __P((SMDB_DATABASE *db)); 65 66 /* 67 ** DB_DEL_FUNC -- removes a key and data pair from the database 68 ** 69 ** Parameters: 70 ** db -- The database to close. 71 ** key -- The key to remove. 72 ** flags -- delete options. There are currently no defined 73 ** flags for delete. 74 ** 75 ** Returns: 76 ** 0 - Success, otherwise errno. 77 ** 78 */ 79 80 typedef int (*db_del_func) __P((SMDB_DATABASE *db, 81 SMDB_DBENT *key, unsigned int flags)); 82 83 /* 84 ** DB_FD_FUNC -- Returns a pointer to a file used for the database. 85 ** 86 ** Parameters: 87 ** db -- The database to close. 88 ** fd -- A pointer to store the returned fd in. 89 ** 90 ** Returns: 91 ** 0 - Success, otherwise errno. 92 ** 93 */ 94 95 typedef int (*db_fd_func) __P((SMDB_DATABASE *db, int* fd)); 96 97 /* 98 ** DB_GET_FUNC -- Gets the data associated with a key. 99 ** 100 ** Parameters: 101 ** db -- The database to close. 102 ** key -- The key to access. 103 ** data -- A place to store the returned data. 104 ** flags -- get options. There are currently no defined 105 ** flags for get. 106 ** 107 ** Returns: 108 ** 0 - Success, otherwise errno. 109 ** 110 */ 111 112 typedef int (*db_get_func) __P((SMDB_DATABASE *db, 113 SMDB_DBENT *key, 114 SMDB_DBENT *data, unsigned int flags)); 115 116 /* 117 ** DB_PUT_FUNC -- Sets some data according to the key. 118 ** 119 ** Parameters: 120 ** db -- The database to close. 121 ** key -- The key to use. 122 ** data -- The data to store. 123 ** flags -- put options: 124 ** SMDBF_NO_OVERWRITE - Return an error if key alread 125 ** exists. 126 ** SMDBF_ALLOW_DUP - Allow duplicates in btree maps. 127 ** 128 ** Returns: 129 ** 0 - Success, otherwise errno. 130 ** 131 */ 132 133 typedef int (*db_put_func) __P((SMDB_DATABASE *db, 134 SMDB_DBENT *key, 135 SMDB_DBENT *data, unsigned int flags)); 136 137 /* 138 ** DB_SYNC_FUNC -- Flush any cached information to disk. 139 ** 140 ** Parameters: 141 ** db -- The database to sync. 142 ** flags -- sync options: 143 ** 144 ** Returns: 145 ** 0 - Success, otherwise errno. 146 ** 147 */ 148 149 typedef int (*db_sync_func) __P((SMDB_DATABASE *db, unsigned int flags)); 150 151 /* 152 ** DB_SET_OWNER_FUNC -- Set the owner and group of the database files. 153 ** 154 ** Parameters: 155 ** db -- The database to set. 156 ** uid -- The UID for the new owner (-1 for no change) 157 ** gid -- The GID for the new owner (-1 for no change) 158 ** 159 ** Returns: 160 ** 0 - Success, otherwise errno. 161 ** 162 */ 163 164 typedef int (*db_set_owner_func) __P((SMDB_DATABASE *db, uid_t uid, gid_t gid)); 165 166 /* 167 ** DB_CURSOR -- Obtain a cursor for sequential access 168 ** 169 ** Parameters: 170 ** db -- The database to use. 171 ** cursor -- The address of a cursor pointer. 172 ** flags -- sync options: 173 ** 174 ** Returns: 175 ** 0 - Success, otherwise errno. 176 ** 177 */ 178 179 typedef int (*db_cursor_func) __P((SMDB_DATABASE *db, 180 SMDB_CURSOR **cursor, unsigned int flags)); 181 182 typedef int (*db_lockfd_func) __P((SMDB_DATABASE *db)); 183 184 struct database_struct 185 { 186 db_close_func smdb_close; 187 db_del_func smdb_del; 188 db_fd_func smdb_fd; 189 db_get_func smdb_get; 190 db_put_func smdb_put; 191 db_sync_func smdb_sync; 192 db_set_owner_func smdb_set_owner; 193 db_cursor_func smdb_cursor; 194 db_lockfd_func smdb_lockfd; 195 void *smdb_impl; 196 }; 197 /* 198 ** DB_CURSOR_CLOSE -- Close a cursor 199 ** 200 ** Parameters: 201 ** cursor -- The cursor to close. 202 ** 203 ** Returns: 204 ** 0 - Success, otherwise errno. 205 ** 206 */ 207 208 typedef int (*db_cursor_close_func) __P((SMDB_CURSOR *cursor)); 209 210 /* 211 ** DB_CURSOR_DEL -- Delete the key/value pair of this cursor 212 ** 213 ** Parameters: 214 ** cursor -- The cursor. 215 ** flags -- flags 216 ** 217 ** Returns: 218 ** 0 - Success, otherwise errno. 219 ** 220 */ 221 222 typedef int (*db_cursor_del_func) __P((SMDB_CURSOR *cursor, 223 unsigned int flags)); 224 225 /* 226 ** DB_CURSOR_GET -- Get the key/value of this cursor. 227 ** 228 ** Parameters: 229 ** cursor -- The cursor. 230 ** key -- The current key. 231 ** value -- The current value 232 ** flags -- flags 233 ** 234 ** Returns: 235 ** 0 - Success, otherwise errno. 236 ** SMDBE_LAST_ENTRY - This is a success condition that 237 ** gets returned when the end of the 238 ** database is hit. 239 ** 240 */ 241 242 typedef int (*db_cursor_get_func) __P((SMDB_CURSOR *cursor, 243 SMDB_DBENT *key, 244 SMDB_DBENT *data, 245 unsigned int flags)); 246 247 /* 248 ** Flags for DB_CURSOR_GET 249 */ 250 251 #define SMDB_CURSOR_GET_FIRST 0 252 #define SMDB_CURSOR_GET_LAST 1 253 #define SMDB_CURSOR_GET_NEXT 2 254 #define SMDB_CURSOR_GET_RANGE 3 255 256 /* 257 ** DB_CURSOR_PUT -- Put the key/value at this cursor. 258 ** 259 ** Parameters: 260 ** cursor -- The cursor. 261 ** key -- The current key. 262 ** value -- The current value 263 ** flags -- flags 264 ** 265 ** Returns: 266 ** 0 - Success, otherwise errno. 267 ** 268 */ 269 270 typedef int (*db_cursor_put_func) __P((SMDB_CURSOR *cursor, 271 SMDB_DBENT *key, 272 SMDB_DBENT *data, 273 unsigned int flags)); 274 275 276 277 struct cursor_struct 278 { 279 db_cursor_close_func smdbc_close; 280 db_cursor_del_func smdbc_del; 281 db_cursor_get_func smdbc_get; 282 db_cursor_put_func smdbc_put; 283 void *smdbc_impl; 284 }; 285 286 287 struct database_params_struct 288 { 289 unsigned int smdbp_num_elements; 290 unsigned int smdbp_cache_size; 291 bool smdbp_allow_dup; 292 }; 293 294 typedef struct database_params_struct SMDB_DBPARAMS; 295 296 struct database_user_struct 297 { 298 uid_t smdbu_id; 299 gid_t smdbu_group_id; 300 char smdbu_name[SMDB_MAX_USER_NAME_LEN]; 301 }; 302 303 typedef struct database_user_struct SMDB_USER_INFO; 304 305 struct entry_struct 306 { 307 void *data; 308 size_t size; 309 }; 310 311 typedef char *SMDB_DBTYPE; 312 typedef unsigned int SMDB_FLAG; 313 314 /* 315 ** These are types of databases. 316 */ 317 318 # define SMDB_TYPE_DEFAULT NULL 319 # define SMDB_TYPE_DEFAULT_LEN 0 320 # define SMDB_TYPE_HASH "hash" 321 # define SMDB_TYPE_HASH_LEN 5 322 # define SMDB_TYPE_BTREE "btree" 323 # define SMDB_TYPE_BTREE_LEN 6 324 # define SMDB_TYPE_NDBM "dbm" 325 # define SMDB_TYPE_NDBM_LEN 4 326 327 /* 328 ** These are flags 329 */ 330 331 /* Flags for put */ 332 # define SMDBF_NO_OVERWRITE 0x00000001 333 # define SMDBF_ALLOW_DUP 0x00000002 334 335 336 extern SMDB_DATABASE *smdb_malloc_database __P((void)); 337 extern void smdb_free_database __P((SMDB_DATABASE *)); 338 extern int smdb_open_database __P((SMDB_DATABASE **, char *, int, 339 int, long, SMDB_DBTYPE, 340 SMDB_USER_INFO *, 341 SMDB_DBPARAMS *)); 342 # ifdef NEWDB 343 extern int smdb_db_open __P((SMDB_DATABASE **, char *, int, int, 344 long, SMDB_DBTYPE, SMDB_USER_INFO *, 345 SMDB_DBPARAMS *)); 346 # endif /* NEWDB */ 347 # ifdef NDBM 348 extern int smdb_ndbm_open __P((SMDB_DATABASE **, char *, int, int, 349 long, SMDB_DBTYPE, 350 SMDB_USER_INFO *, 351 SMDB_DBPARAMS *)); 352 # endif /* NDBM */ 353 extern int smdb_add_extension __P((char *, int, char *, char *)); 354 extern int smdb_setup_file __P((char *, char *, int, long, 355 SMDB_USER_INFO *, struct stat *)); 356 extern int smdb_lock_file __P((int *, char *, int, long, char *)); 357 extern int smdb_unlock_file __P((int)); 358 extern int smdb_filechanged __P((char *, char *, int, 359 struct stat *)); 360 extern void smdb_print_available_types __P((void)); 361 extern char *smdb_db_definition __P((SMDB_DBTYPE)); 362 extern int smdb_lock_map __P((SMDB_DATABASE *, int)); 363 extern int smdb_unlock_map __P((SMDB_DATABASE *)); 364 #endif /* ! _SMDB_H_ */ 365