smdb.h revision 64562
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 2000/04/08 20:40:42 ca 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
23ERROR	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
54typedef struct database_struct SMDB_DATABASE;
55typedef struct cursor_struct SMDB_CURSOR;
56typedef union database_entity_union 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*/
69typedef 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*/
86typedef 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*/
102typedef 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*/
120typedef 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*/
142typedef 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*/
158typedef 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*/
173typedef 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*/
189typedef int (*db_cursor_func) __P((SMDB_DATABASE *db,
190			      SMDB_CURSOR **cursor, u_int flags));
191
192
193struct database_struct
194{
195	db_close_func		smdb_close;
196	db_del_func		smdb_del;
197	db_fd_func		smdb_fd;
198	db_get_func		smdb_get;
199	db_put_func		smdb_put;
200	db_sync_func		smdb_sync;
201	db_set_owner_func	smdb_set_owner;
202	db_cursor_func		smdb_cursor;
203	void			*smdb_impl;
204};
205
206
207
208/*
209** DB_CURSOR_CLOSE -- Close a cursor
210**
211**	Parameters:
212**		cursor -- The cursor to close.
213**
214**	Returns:
215**		0 - Success, otherwise errno.
216**
217*/
218typedef int (*db_cursor_close_func) __P((SMDB_CURSOR *cursor));
219
220
221/*
222** DB_CURSOR_DEL -- Delete the key/value pair of this cursor
223**
224**	Parameters:
225**		cursor -- The cursor.
226**		flags -- flags
227**
228**	Returns:
229**		0 - Success, otherwise errno.
230**
231*/
232typedef int (*db_cursor_del_func) __P((SMDB_CURSOR *cursor, u_int flags));
233
234
235/*
236** DB_CURSOR_GET -- Get the key/value of this cursor.
237**
238**	Parameters:
239**		cursor -- The cursor.
240**		key -- The current key.
241**		value -- The current value
242**		flags -- flags
243**
244**	Returns:
245**		0 - Success, otherwise errno.
246**		SMDBE_LAST_ENTRY - This is a success condition that
247**				   gets returned when the end of the
248**				   database is hit.
249**
250*/
251typedef int (*db_cursor_get_func) __P((SMDB_CURSOR *cursor,
252				  SMDB_DBENT *key,
253				  SMDB_DBENT *data,
254				  u_int flags));
255
256/*
257** Flags for DB_CURSOR_GET
258*/
259#define SMDB_CURSOR_GET_FIRST	0
260#define SMDB_CURSOR_GET_LAST	1
261#define SMDB_CURSOR_GET_NEXT	2
262#define SMDB_CURSOR_GET_RANGE	3
263
264
265/*
266** DB_CURSOR_PUT -- Put the key/value at this cursor.
267**
268**	Parameters:
269**		cursor -- The cursor.
270**		key -- The current key.
271**		value -- The current value
272**		flags -- flags
273**
274**	Returns:
275**		0 - Success, otherwise errno.
276**
277*/
278typedef int (*db_cursor_put_func) __P((SMDB_CURSOR *cursor,
279				  SMDB_DBENT *key,
280				  SMDB_DBENT *data,
281				  u_int flags));
282
283
284
285struct cursor_struct
286{
287	db_cursor_close_func	smdbc_close;
288	db_cursor_del_func	smdbc_del;
289	db_cursor_get_func	smdbc_get;
290	db_cursor_put_func	smdbc_put;
291	void			*smdbc_impl;
292};
293
294
295struct database_params_struct
296{
297	u_int	smdbp_num_elements;
298	u_int	smdbp_cache_size;
299	bool	smdbp_allow_dup;
300};
301
302typedef struct database_params_struct SMDB_DBPARAMS;
303
304struct database_user_struct
305{
306	uid_t	smdbu_id;
307	gid_t	smdbu_group_id;
308	char	smdbu_name[SMDB_MAX_USER_NAME_LEN];
309};
310
311typedef struct database_user_struct SMDB_USER_INFO;
312
313union database_entity_union
314{
315# ifdef NDBM
316	datum	dbm;
317# endif /* NDBM */
318# ifdef NEWDB
319	DBT	db;
320# endif /* NEWDB */
321	struct
322	{
323		char	*data;
324		size_t	size;
325	} data;
326};
327
328
329typedef char *SMDB_DBTYPE;
330typedef u_int SMDB_FLAG;
331
332/*
333** These are types of databases.
334*/
335
336# define SMDB_TYPE_DEFAULT	NULL
337# define SMDB_TYPE_DEFAULT_LEN	0
338# define SMDB_TYPE_HASH		"hash"
339# define SMDB_TYPE_HASH_LEN	5
340# define SMDB_TYPE_BTREE	"btree"
341# define SMDB_TYPE_BTREE_LEN	6
342# define SMDB_TYPE_NDBM		"dbm"
343# define SMDB_TYPE_NDBM_LEN	4
344
345/*
346** These are flags
347*/
348/* Flags for put */
349# define SMDBF_NO_OVERWRITE	0x00000001
350# define SMDBF_ALLOW_DUP	0x00000002
351
352
353extern SMDB_DATABASE	*smdb_malloc_database __P((void));
354extern void		smdb_free_database __P((SMDB_DATABASE *));
355extern int		smdb_open_database __P((SMDB_DATABASE **, char *, int,
356						int, long, SMDB_DBTYPE,
357						SMDB_USER_INFO *,
358						SMDB_DBPARAMS *));
359# ifdef NEWDB
360extern int		smdb_db_open __P((SMDB_DATABASE **, char *, int, int,
361					  long, SMDB_DBTYPE, SMDB_USER_INFO *,
362					  SMDB_DBPARAMS *));
363# endif /* NEWDB */
364# ifdef NDBM
365extern int		smdb_ndbm_open __P((SMDB_DATABASE **, char *, int, int,
366					    long, SMDB_DBTYPE,
367					    SMDB_USER_INFO *,
368					    SMDB_DBPARAMS *));
369# endif /* NDBM */
370extern int		smdb_add_extension __P((char *, int, char *, char *));
371extern int		smdb_setup_file __P((char *, char *, int, long,
372					     SMDB_USER_INFO *, struct stat *));
373extern int		smdb_lock_file __P((int *, char *, int, long, char *));
374extern int		smdb_unlock_file __P((int));
375extern int		smdb_filechanged __P((char *, char *, int,
376					      struct stat *));
377extern void		smdb_print_available_types __P((void));
378extern char		*smdb_db_definition __P((SMDB_DBTYPE));
379#endif /* ! _SMDB_H_ */
380