smdb.h revision 266692
167754Smsmith/*
267754Smsmith * Copyright (c) 1999-2002 Proofpoint, Inc. and its suppliers.
377424Smsmith *	All rights reserved.
4126372Snjl *
567754Smsmith * By using this file, you agree to the terms and conditions set
667754Smsmith * forth in the LICENSE file which can be found at the top level of
767754Smsmith * the sendmail distribution.
867754Smsmith *
967754Smsmith *	$Id: smdb.h,v 8.42 2013-11-22 20:51:28 ca Exp $
1067754Smsmith *
1167754Smsmith */
12126372Snjl
1370243Smsmith#ifndef _SMDB_H_
1467754Smsmith# define _SMDB_H_
1567754Smsmith
1667754Smsmith# include <sys/types.h>
1767754Smsmith# include <sys/stat.h>
1867754Smsmith# include <sm/gen.h>
1967754Smsmith# include <sm/errstring.h>
2067754Smsmith
2167754Smsmith# ifdef NDBM
2267754Smsmith#  include <ndbm.h>
2367754Smsmith# endif /* NDBM */
2467754Smsmith
2567754Smsmith# ifdef NEWDB
2667754Smsmith#  include "sm/bdb.h"
2767754Smsmith# endif /* NEWDB */
2867754Smsmith
2967754Smsmith/*
3067754Smsmith**  Some size constants
3167754Smsmith*/
3267754Smsmith
3367754Smsmith#define SMDB_MAX_USER_NAME_LEN	1024
3467754Smsmith
3567754Smsmith/*
3667754Smsmith**  This file defines the abstraction for database lookups. It is pretty
3767754Smsmith**  much a copy of the db2 interface with the exception that every function
3867754Smsmith**  returns 0 on success and non-zero on failure. The non-zero return code
3967754Smsmith**  is meaningful.
4067754Smsmith**
4167754Smsmith**  I'm going to put the function comments in this file since the interface
4267754Smsmith**  MUST be the same for all inheritors of this interface.
4367754Smsmith*/
4467754Smsmith
4567754Smsmithtypedef struct database_struct SMDB_DATABASE;
4667754Smsmithtypedef struct cursor_struct SMDB_CURSOR;
4767754Smsmithtypedef struct entry_struct SMDB_DBENT;
4867754Smsmith
4967754Smsmith/*
5067754Smsmith**  DB_CLOSE_FUNC -- close the database
5167754Smsmith**
5267754Smsmith**	Parameters:
5367754Smsmith**		db -- The database to close.
5467754Smsmith**
5567754Smsmith**	Returns:
5667754Smsmith**		0 - Success, otherwise errno.
5767754Smsmith**
5867754Smsmith*/
5967754Smsmith
6067754Smsmithtypedef int (*db_close_func) __P((SMDB_DATABASE *db));
6167754Smsmith
6267754Smsmith/*
6367754Smsmith**  DB_DEL_FUNC -- removes a key and data pair from the database
6467754Smsmith**
6567754Smsmith**	Parameters:
6667754Smsmith**		db -- The database to close.
6767754Smsmith**		key -- The key to remove.
6867754Smsmith**		flags -- delete options. There are currently no defined
6967754Smsmith**			 flags for delete.
7067754Smsmith**
7167754Smsmith**	Returns:
7267754Smsmith**		0 - Success, otherwise errno.
7367754Smsmith**
7467754Smsmith*/
7567754Smsmith
7667754Smsmithtypedef int (*db_del_func) __P((SMDB_DATABASE *db,
7767754Smsmith			   SMDB_DBENT *key, unsigned int flags));
7867754Smsmith
7967754Smsmith/*
8067754Smsmith**  DB_FD_FUNC -- Returns a pointer to a file used for the database.
8167754Smsmith**
8267754Smsmith**	Parameters:
8367754Smsmith**		db -- The database to close.
8467754Smsmith**		fd -- A pointer to store the returned fd in.
8567754Smsmith**
8667754Smsmith**	Returns:
8767754Smsmith**		0 - Success, otherwise errno.
8867754Smsmith**
8967754Smsmith*/
9067754Smsmith
9167754Smsmithtypedef int (*db_fd_func) __P((SMDB_DATABASE *db, int* fd));
9267754Smsmith
9367754Smsmith/*
9467754Smsmith**  DB_GET_FUNC -- Gets the data associated with a key.
9567754Smsmith**
9667754Smsmith**	Parameters:
9767754Smsmith**		db -- The database to close.
9867754Smsmith**		key -- The key to access.
9967754Smsmith**		data -- A place to store the returned data.
10067754Smsmith**		flags -- get options. There are currently no defined
10167754Smsmith**			 flags for get.
10267754Smsmith**
10367754Smsmith**	Returns:
10467754Smsmith**		0 - Success, otherwise errno.
10567754Smsmith**
10667754Smsmith*/
10767754Smsmith
10867754Smsmithtypedef int (*db_get_func) __P((SMDB_DATABASE *db,
10967754Smsmith			   SMDB_DBENT *key,
11067754Smsmith			   SMDB_DBENT *data, unsigned int flags));
11167754Smsmith
11267754Smsmith/*
11367754Smsmith**  DB_PUT_FUNC -- Sets some data according to the key.
11467754Smsmith**
11567754Smsmith**	Parameters:
11667754Smsmith**		db -- The database to close.
11777424Smsmith**		key -- The key to use.
11877424Smsmith**		data -- The data to store.
11967754Smsmith**		flags -- put options:
12067754Smsmith**			SMDBF_NO_OVERWRITE - Return an error if key alread
12173561Smsmith**					     exists.
12273561Smsmith**			SMDBF_ALLOW_DUP - Allow duplicates in btree maps.
12373561Smsmith**
12473561Smsmith**	Returns:
12573561Smsmith**		0 - Success, otherwise errno.
12673561Smsmith**
12773561Smsmith*/
12873561Smsmith
12973561Smsmithtypedef int (*db_put_func) __P((SMDB_DATABASE *db,
13077424Smsmith			   SMDB_DBENT *key,
13173561Smsmith			   SMDB_DBENT *data, unsigned int flags));
13273561Smsmith
13373561Smsmith/*
13473561Smsmith**  DB_SYNC_FUNC -- Flush any cached information to disk.
13573561Smsmith**
13673561Smsmith**	Parameters:
13773561Smsmith**		db -- The database to sync.
13873561Smsmith**		flags -- sync options:
13973561Smsmith**
14091116Smsmith**	Returns:
14173561Smsmith**		0 - Success, otherwise errno.
14273561Smsmith**
143114237Snjl*/
14473561Smsmith
14573561Smsmithtypedef int (*db_sync_func) __P((SMDB_DATABASE *db, unsigned int flags));
14667754Smsmith
14767754Smsmith/*
14867754Smsmith**  DB_SET_OWNER_FUNC -- Set the owner and group of the database files.
14967754Smsmith**
15077424Smsmith**	Parameters:
15167754Smsmith**		db -- The database to set.
15267754Smsmith**		uid -- The UID for the new owner (-1 for no change)
15367754Smsmith**		gid -- The GID for the new owner (-1 for no change)
15467754Smsmith**
15567754Smsmith**	Returns:
15667754Smsmith**		0 - Success, otherwise errno.
15767754Smsmith**
15867754Smsmith*/
15967754Smsmith
16067754Smsmithtypedef int (*db_set_owner_func) __P((SMDB_DATABASE *db, uid_t uid, gid_t gid));
16177424Smsmith
16267754Smsmith/*
16367754Smsmith**  DB_CURSOR -- Obtain a cursor for sequential access
16467754Smsmith**
16577424Smsmith**	Parameters:
16667754Smsmith**		db -- The database to use.
16767754Smsmith**		cursor -- The address of a cursor pointer.
16867754Smsmith**		flags -- sync options:
16967754Smsmith**
17077424Smsmith**	Returns:
17167754Smsmith**		0 - Success, otherwise errno.
17267754Smsmith**
17367754Smsmith*/
17477424Smsmith
17567754Smsmithtypedef int (*db_cursor_func) __P((SMDB_DATABASE *db,
17667754Smsmith			      SMDB_CURSOR **cursor, unsigned int flags));
17799679Siwasaki
17877424Smsmithtypedef int (*db_lockfd_func) __P((SMDB_DATABASE *db));
17967754Smsmith
18067754Smsmithstruct database_struct
18167754Smsmith{
18277424Smsmith	db_close_func		smdb_close;
18367754Smsmith	db_del_func		smdb_del;
18467754Smsmith	db_fd_func		smdb_fd;
18567754Smsmith	db_get_func		smdb_get;
18677424Smsmith	db_put_func		smdb_put;
18767754Smsmith	db_sync_func		smdb_sync;
18867754Smsmith	db_set_owner_func	smdb_set_owner;
189102550Siwasaki	db_cursor_func		smdb_cursor;
19069746Smsmith	db_lockfd_func		smdb_lockfd;
191114237Snjl	void			*smdb_impl;
19277424Smsmith};
19367754Smsmith/*
19467754Smsmith**  DB_CURSOR_CLOSE -- Close a cursor
195100966Siwasaki**
196100966Siwasaki**	Parameters:
197114237Snjl**		cursor -- The cursor to close.
19877424Smsmith**
19991116Smsmith**	Returns:
20067754Smsmith**		0 - Success, otherwise errno.
201114237Snjl**
202123315Snjl*/
203123315Snjl
204123315Snjltypedef int (*db_cursor_close_func) __P((SMDB_CURSOR *cursor));
205123315Snjl
206123315Snjl/*
207123315Snjl**  DB_CURSOR_DEL -- Delete the key/value pair of this cursor
208123315Snjl**
209123315Snjl**	Parameters:
21099679Siwasaki**		cursor -- The cursor.
21199679Siwasaki**		flags -- flags
21299679Siwasaki**
213114237Snjl**	Returns:
21477424Smsmith**		0 - Success, otherwise errno.
21569746Smsmith**
21669746Smsmith*/
217114237Snjl
21891116Smsmithtypedef int (*db_cursor_del_func) __P((SMDB_CURSOR *cursor,
21991116Smsmith					unsigned int flags));
22069746Smsmith
22199679Siwasaki/*
22282367Smsmith**  DB_CURSOR_GET -- Get the key/value of this cursor.
22382367Smsmith**
22482367Smsmith**	Parameters:
22582367Smsmith**		cursor -- The cursor.
22667754Smsmith**		key -- The current key.
22777424Smsmith**		value -- The current value
22891116Smsmith**		flags -- flags
22967754Smsmith**
23067754Smsmith**	Returns:
23177424Smsmith**		0 - Success, otherwise errno.
23267754Smsmith**		SMDBE_LAST_ENTRY - This is a success condition that
23367754Smsmith**				   gets returned when the end of the
23467754Smsmith**				   database is hit.
23567754Smsmith**
23677424Smsmith*/
23767754Smsmith
23867754Smsmithtypedef int (*db_cursor_get_func) __P((SMDB_CURSOR *cursor,
23971867Smsmith				  SMDB_DBENT *key,
24071867Smsmith				  SMDB_DBENT *data,
241104470Siwasaki				  unsigned int flags));
24277424Smsmith
243114237Snjl/*
24467754Smsmith**  Flags for DB_CURSOR_GET
245114237Snjl*/
24677424Smsmith
247114237Snjl#define SMDB_CURSOR_GET_FIRST	0
248114237Snjl#define SMDB_CURSOR_GET_LAST	1
24967754Smsmith#define SMDB_CURSOR_GET_NEXT	2
250114237Snjl#define SMDB_CURSOR_GET_RANGE	3
25177424Smsmith
252114237Snjl/*
253114237Snjl**  DB_CURSOR_PUT -- Put the key/value at this cursor.
254104470Siwasaki**
25567754Smsmith**	Parameters:
25699679Siwasaki**		cursor -- The cursor.
25777424Smsmith**		key -- The current key.
258114237Snjl**		value -- The current value
259114237Snjl**		flags -- flags
260104470Siwasaki**
26167754Smsmith**	Returns:
262104470Siwasaki**		0 - Success, otherwise errno.
26377424Smsmith**
264114237Snjl*/
265114237Snjl
26667754Smsmithtypedef int (*db_cursor_put_func) __P((SMDB_CURSOR *cursor,
267114237Snjl				  SMDB_DBENT *key,
26877424Smsmith				  SMDB_DBENT *data,
269114237Snjl				  unsigned int flags));
270114237Snjl
27167754Smsmith
272114237Snjl
27377424Smsmithstruct cursor_struct
274114237Snjl{
275114237Snjl	db_cursor_close_func	smdbc_close;
276104470Siwasaki	db_cursor_del_func	smdbc_del;
27767754Smsmith	db_cursor_get_func	smdbc_get;
27867754Smsmith	db_cursor_put_func	smdbc_put;
27977424Smsmith	void			*smdbc_impl;
280114237Snjl};
281114237Snjl
28277424Smsmith
28367754Smsmithstruct database_params_struct
284114237Snjl{
28577424Smsmith	unsigned int	smdbp_num_elements;
286114237Snjl	unsigned int	smdbp_cache_size;
287114237Snjl	bool		smdbp_allow_dup;
28867754Smsmith};
28967754Smsmith
29077424Smsmithtypedef struct database_params_struct SMDB_DBPARAMS;
29167754Smsmith
29267754Smsmithstruct database_user_struct
293104470Siwasaki{
29467754Smsmith	uid_t	smdbu_id;
29567754Smsmith	gid_t	smdbu_group_id;
29677424Smsmith	char	smdbu_name[SMDB_MAX_USER_NAME_LEN];
29767754Smsmith};
298114237Snjl
299104470Siwasakitypedef struct database_user_struct SMDB_USER_INFO;
30067754Smsmith
30199679Siwasakistruct entry_struct
30277424Smsmith{
30399679Siwasaki	void	*data;
30467754Smsmith	size_t	size;
30599679Siwasaki};
30677424Smsmith
30799679Siwasakitypedef char *SMDB_DBTYPE;
30867754Smsmithtypedef unsigned int SMDB_FLAG;
30999679Siwasaki
31099679Siwasaki/*
31199679Siwasaki**  These are types of databases.
31299679Siwasaki*/
31399679Siwasaki
31499679Siwasaki# define SMDB_TYPE_DEFAULT	NULL
31599679Siwasaki# define SMDB_TYPE_DEFAULT_LEN	0
31699679Siwasaki# define SMDB_TYPE_HASH		"hash"
31799679Siwasaki# define SMDB_TYPE_HASH_LEN	5
31899679Siwasaki# define SMDB_TYPE_BTREE	"btree"
31999679Siwasaki# define SMDB_TYPE_BTREE_LEN	6
32099679Siwasaki# define SMDB_TYPE_NDBM		"dbm"
32199679Siwasaki# define SMDB_TYPE_NDBM_LEN	4
32299679Siwasaki
32399679Siwasaki/*
32499679Siwasaki**  These are flags
32599679Siwasaki*/
32699679Siwasaki
327100966Siwasaki/* Flags for put */
328100966Siwasaki# define SMDBF_NO_OVERWRITE	0x00000001
329100966Siwasaki# define SMDBF_ALLOW_DUP	0x00000002
33099679Siwasaki
33171867Smsmith
33267754Smsmithextern SMDB_DATABASE	*smdb_malloc_database __P((void));
33367754Smsmithextern void		smdb_free_database __P((SMDB_DATABASE *));
33477424Smsmithextern int		smdb_open_database __P((SMDB_DATABASE **, char *, int,
33567754Smsmith						int, long, SMDB_DBTYPE,
33667754Smsmith						SMDB_USER_INFO *,
33767754Smsmith						SMDB_DBPARAMS *));
33877424Smsmith# ifdef NEWDB
33967754Smsmithextern int		smdb_db_open __P((SMDB_DATABASE **, char *, int, int,
34067754Smsmith					  long, SMDB_DBTYPE, SMDB_USER_INFO *,
34167754Smsmith					  SMDB_DBPARAMS *));
34267754Smsmith# endif /* NEWDB */
34367754Smsmith# ifdef NDBM
34467754Smsmithextern int		smdb_ndbm_open __P((SMDB_DATABASE **, char *, int, int,
34577424Smsmith					    long, SMDB_DBTYPE,
34667754Smsmith					    SMDB_USER_INFO *,
34767754Smsmith					    SMDB_DBPARAMS *));
34867754Smsmith# endif /* NDBM */
34967754Smsmithextern int		smdb_add_extension __P((char *, int, char *, char *));
35067754Smsmithextern int		smdb_setup_file __P((char *, char *, int, long,
35199679Siwasaki					     SMDB_USER_INFO *, struct stat *));
35299679Siwasakiextern int		smdb_lock_file __P((int *, char *, int, long, char *));
35399679Siwasakiextern int		smdb_unlock_file __P((int));
35499679Siwasakiextern int		smdb_filechanged __P((char *, char *, int,
35599679Siwasaki					      struct stat *));
35699679Siwasakiextern void		smdb_print_available_types __P((void));
35799679Siwasakiextern char		*smdb_db_definition __P((SMDB_DBTYPE));
35899679Siwasakiextern int		smdb_lock_map __P((SMDB_DATABASE *, int));
35999679Siwasakiextern int		smdb_unlock_map __P((SMDB_DATABASE *));
36099679Siwasaki#endif /* ! _SMDB_H_ */
36199679Siwasaki