Handle SERVFAIL, NOTIMP, REFUSED replies from DNS server better.
[glibc/pb-stable.git] / nss / nss_db / db-open.c
blob94dfe5b7da77a4f87360eeb2afc8dd71d3c0ddc9
1 /* Common database routines for nss_db.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <dlfcn.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
27 #include "dummy-db.h"
28 #include "nss_db.h"
30 /* This file contains the functions used to open and close the databases
31 read by the rest of libnss_db. Not all of them are thread safe;
32 make sure the caller does the appropriate locking.
34 We dynamically load the database library, so that it does not have
35 to be present when glibc is compiled. Once loaded, the database
36 library is never never unloaded again until the libnss_db module is
37 unloaded (from the free_mem routine in nsswitch.c) -- we catch the
38 unload by providing a shlib destructor. (XXX Does that actually
39 work?) */
41 /* Handle for the shared Berkeley DB library. If non-null, the
42 database library is completely loaded and ready to be used by
43 multithreaded code. */
44 static void *libdb_handle;
46 /* The version of the Berkeley DB library we are using. */
47 enum {
48 nodb,
49 db24,
50 db27,
51 db30
52 } libdb_version;
54 /* Pointer to the db_open function. For use with DB 2.x. */
55 static int (*libdb_db_open) (const char *, int,
56 uint32_t, int, void *, void *, void **);
58 /* Pointer to the db_create function. For use with DB 3.x. */
59 static int (*libdb_db_create) (void *, void *, uint32_t);
61 /* Constants which vary from version to version are actually variables
62 here. */
63 int db_first;
64 int db_next;
65 int db_nooverwrite;
66 int db_truncate;
67 int db_rdonly;
68 /* Variables which keep track of the error values. */
69 int db_keyexist;
70 int db_notfound;
72 /* Locks the static variables in this file. */
73 __libc_lock_define_initialized (static, lock)
75 /* Dynamically load the database library. Return zero if successful,
76 non-zero if no suitable version of the library could be loaded.
77 Must be called with the above lock held if it might run in a
78 multithreaded context.
80 We try currently:
81 - libdb.so.3: the name used by glibc 2.1
82 - libdb-3.0.so: the name used by db-3.0.x
83 and maybe others in the future. */
85 enum nss_status
86 load_db (void)
88 static const char *libnames[] = { "libdb.so.3", "libdb-3.0.so" };
89 int x;
91 for (x = 0; x < sizeof (libnames) / sizeof (libnames[0]); ++x)
93 libdb_handle = dlopen (libnames[x], RTLD_LAZY);
94 if (libdb_handle == NULL)
95 continue;
97 /* DB 3.0 has db_create instead of db_open. */
98 libdb_db_create = dlsym (libdb_handle, "db_create");
100 if (libdb_db_create == NULL)
101 /* DB 2.x uses db_open. */
102 libdb_db_open = dlsym (libdb_handle, "db_open");
104 if (libdb_db_open != NULL || libdb_db_create != NULL)
106 /* Alright, we got a library. Now find out which version it is. */
107 const char *(*db_version) (int *, int *, int *);
109 db_version = dlsym (libdb_handle, "db_version");
110 if (db_version != NULL)
112 /* Call the function and get the information. */
113 int major, minor, subminor;
115 DL_CALL_FCT (db_version, (&major, &minor, &subminor));
116 switch (major)
118 case 2:
119 /* Sanity check: Do we have db_open? */
120 if (libdb_db_open != NULL)
122 if (minor < 6 || (minor == 6 && subminor < 4))
124 libdb_version = db24;
125 db_first = DB24_FIRST;
126 db_next = DB24_NEXT;
127 db_nooverwrite = DB24_NOOVERWRITE;
128 db_truncate = DB24_TRUNCATE;
130 else
132 libdb_version = db27;
133 db_first = DB27_FIRST;
134 db_next = DB27_NEXT;
135 db_nooverwrite = DB27_NOOVERWRITE;
136 db_truncate = DB27_TRUNCATE;
138 db_keyexist = DB2x_KEYEXIST;
139 db_notfound = DB2x_NOTFOUND;
140 db_rdonly = DB2x_RDONLY;
142 break;
144 case 3:
145 /* Sanity check: Do we have db_create? */
146 if (libdb_db_create != NULL)
148 libdb_version = db30;
149 db_first = DB30_FIRST;
150 db_next = DB30_NEXT;
151 db_keyexist = DB30_KEYEXIST;
152 db_notfound = DB30_NOTFOUND;
153 db_rdonly = DB30_RDONLY;
155 break;
157 default:
158 break;
162 if (libdb_version != nodb)
163 return NSS_STATUS_SUCCESS;
165 /* Clear variables. */
166 libdb_db_open = NULL;
167 libdb_db_create = NULL;
170 dlclose (libdb_handle);
173 (void) dlerror ();
174 return NSS_STATUS_UNAVAIL;
177 /* Set the `FD_CLOEXEC' flag of FD. Return 0 on success, or -1 on
178 error with `errno' set. */
179 static int
180 set_cloexec_flag (int fd)
182 int oldflags = fcntl (fd, F_GETFD, 0);
184 if (oldflags < 0)
185 return oldflags;
187 oldflags |= FD_CLOEXEC;
189 return fcntl (fd, F_SETFD, oldflags);
192 /* Make sure we don't use the library anymore once we are shutting down. */
193 static void __attribute__ ((destructor))
194 unload_db (void)
196 if (libdb_handle != NULL)
198 libdb_db_open = NULL;
199 libdb_db_create = NULL;
200 libdb_version = nodb;
201 dlclose (libdb_handle);
205 /* Open the database stored in FILE. If succesful, store the database
206 handle in *DBP and return NSS_STATUS_SUCCESS. On failure, return
207 the appropriate lookup status. */
208 enum nss_status
209 internal_setent (const char *file, NSS_DB **dbp)
211 enum nss_status status = NSS_STATUS_SUCCESS;
213 if (*dbp == NULL)
215 if (libdb_db_open == NULL && libdb_db_create == NULL)
217 __libc_lock_lock (lock);
219 if (libdb_db_open == NULL && libdb_db_create == NULL)
220 status = load_db ();
222 __libc_lock_unlock (lock);
225 if (status == NSS_STATUS_SUCCESS)
226 status = dbopen (file, db_rdonly, 0, dbp);
229 return status;
233 /* Close the database *DBP. */
234 void
235 internal_endent (NSS_DB **dbp)
237 NSS_DB *db = *dbp;
239 if (db != NULL)
241 DL_CALL_FCT (db->close, (db->db, 0));
242 *dbp = NULL;
246 /* Allocate a cursor for database DB and transaction TXN. On success,
247 store the cursor in *DBCP and return zero. Otherwise return an
248 error value. */
250 db_cursor (void *db, void *txn, NSS_DBC **dbcp)
252 NSS_DBC *dbc;
253 int ret;
255 dbc = (NSS_DBC *) malloc (sizeof (NSS_DBC));
256 if (dbc == NULL)
257 return ENOMEM;
259 switch (libdb_version)
261 case db24:
262 ret = ((struct db24 *) db)->cursor (db, txn, &dbc->cursor);
264 if (ret == 0)
265 dbc->c_get = ((struct dbc24 *) dbc->cursor)->c_get;
266 break;
268 case db27:
269 ret = ((struct db27 *) db)->cursor (db, txn, &dbc->cursor, 0);
271 if (ret == 0)
272 dbc->c_get = ((struct dbc27 *) dbc->cursor)->c_get;
273 break;
275 case db30:
276 ret = ((struct db30 *) db)->cursor (db, txn, &dbc->cursor, 0);
278 if (ret == 0)
279 dbc->c_get = ((struct dbc30 *) dbc->cursor)->c_get;
280 break;
282 default:
283 abort ();
286 if (ret != 0)
288 free (dbc);
289 return ret;
292 *dbcp = dbc;
294 return 0;
298 /* Open the database in FNAME, for access specified by FLAGS. If
299 opening the database causes the file FNAME to be created, it is
300 created with MODE. If succesful, store the database handle in *DBP
301 and return NSS_STATUS_SUCCESS. On failure, return the appropriate
302 lookup status. */
304 dbopen (const char *fname, int oper, int mode, NSS_DB **dbp)
306 int err;
307 int fd;
308 NSS_DB *db;
310 /* Construct the object we pass up. */
311 db = (NSS_DB *) calloc (1, sizeof (NSS_DB));
312 if (db == NULL)
313 return NSS_STATUS_UNAVAIL;
315 /* Initialize the object. */
316 db->cursor = db_cursor;
318 /* Actually open the database. */
319 switch (libdb_version)
321 case db24:
322 case db27:
323 err = DL_CALL_FCT (libdb_db_open,
324 (fname, DB_BTREE, oper, mode, NULL, NULL, &db->db));
325 if (err != 0)
326 goto fail;
328 if (libdb_version)
330 db->close = ((struct db24 *) db->db)->close;
331 db->fd = ((struct db24 *) db->db)->fd;
332 db->get = ((struct db24 *) db->db)->get;
333 db->put = ((struct db24 *) db->db)->put;
335 else
337 db->close = ((struct db27 *) db->db)->close;
338 db->fd = ((struct db27 *) db->db)->fd;
339 db->get = ((struct db27 *) db->db)->get;
340 db->put = ((struct db27 *) db->db)->put;
342 break;
344 case db30:
345 err = DL_CALL_FCT (libdb_db_create, (db->db, NULL, 0));
346 if (err != 0)
347 goto fail;
349 db->close = ((struct db30 *) db->db)->close;
350 db->fd = ((struct db30 *) db->db)->fd;
351 db->get = ((struct db30 *) db->db)->get;
352 db->put = ((struct db30 *) db->db)->put;
354 err = ((struct db30 *) db->db)->open (db->db, fname, NULL, DB_BTREE,
355 oper, mode);
356 if (err != 0)
357 goto fail;
358 break;
360 default:
361 abort ();
364 /* We have to make sure the file is `closed on exec'. */
365 err = DL_CALL_FCT (db->fd, (db->db, &fd));
366 if (err != 0)
367 goto fail;
368 if (set_cloexec_flag (fd) < 0)
369 goto fail;
371 *dbp = db;
373 return NSS_STATUS_UNAVAIL;
375 fail:
376 /* Something went wrong. Close the database if necessary. */
377 if (db)
379 if (db->db && db->close)
380 DL_CALL_FCT (db->close, (db->db, 0));
381 free (db);
384 /* Make sure `errno' is set. */
385 if (err)
386 __set_errno (err);
388 return err == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;