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