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