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