]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nss_db/db-XXX.c
Update.
[thirdparty/glibc.git] / nss / nss_db / db-XXX.c
1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #include <db_185.h>
21 #include <fcntl.h>
22 #include <bits/libc-lock.h>
23 #include "nsswitch.h"
24
25 /* These symbols are defined by the including source file:
26
27 ENTNAME -- database name of the structure and functions (hostent, pwent).
28 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
29 DATABASE -- database file name, ("hosts", "passwd")
30
31 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
32 */
33
34 #define ENTNAME_r CONCAT(ENTNAME,_r)
35
36 #include <paths.h>
37 #define DBFILE _PATH_VARDB DATABASE ".db"
38
39 #ifdef NEED_H_ERRNO
40 #define H_ERRNO_PROTO , int *herrnop
41 #define H_ERRNO_ARG , herrnop
42 #define H_ERRNO_SET(val) (*herrnop = (val))
43 #else
44 #define H_ERRNO_PROTO
45 #define H_ERRNO_ARG
46 #define H_ERRNO_SET(val) ((void) 0)
47 #endif
48
49 /* Locks the static variables in this file. */
50 __libc_lock_define_initialized (static, lock)
51 \f
52 /* Maintenance of the shared handle open on the database. */
53
54 static DB *db;
55 static int keep_db;
56 static unsigned int entidx; /* Index for `getENTNAME'. */
57
58 /* Open database file if not already opened. */
59 static enum nss_status
60 internal_setent (int stayopen)
61 {
62 enum nss_status status = NSS_STATUS_SUCCESS;
63
64 if (db == NULL)
65 {
66 db = __dbopen (DBFILE, O_RDONLY, 0, DB_BTREE, NULL);
67
68 if (db == NULL)
69 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
70 else
71 {
72 /* We have to make sure the file is `closed on exec'. */
73 int result, flags;
74
75 result = flags = fcntl ((*db->fd) (db), F_GETFD, 0);
76 if (result >= 0)
77 {
78 flags |= FD_CLOEXEC;
79 result = fcntl ((*db->fd) (db), F_SETFD, flags);
80 }
81 if (result < 0)
82 {
83 /* Something went wrong. Close the stream and return a
84 failure. */
85 (*db->close) (db);
86 db = NULL;
87 status = NSS_STATUS_UNAVAIL;
88 }
89 }
90 }
91
92 /* Remember STAYOPEN flag. */
93 if (db != NULL)
94 keep_db |= stayopen;
95
96 return status;
97 }
98
99
100 /* Thread-safe, exported version of that. */
101 enum nss_status
102 CONCAT(_nss_db_set,ENTNAME) (int stayopen)
103 {
104 enum nss_status status;
105
106 __libc_lock_lock (lock);
107
108 status = internal_setent (stayopen);
109
110 /* Reset the sequential index. */
111 entidx = 0;
112
113 __libc_lock_unlock (lock);
114
115 return status;
116 }
117
118
119 /* Close the database file. */
120 static void
121 internal_endent (void)
122 {
123 if (db != NULL)
124 {
125 (*db->close) (db);
126 db = NULL;
127 }
128 }
129
130
131 /* Thread-safe, exported version of that. */
132 enum nss_status
133 CONCAT(_nss_db_end,ENTNAME) (void)
134 {
135 __libc_lock_lock (lock);
136
137 internal_endent ();
138
139 /* Reset STAYOPEN flag. */
140 keep_db = 0;
141
142 __libc_lock_unlock (lock);
143
144 return NSS_STATUS_SUCCESS;
145 }
146 \f
147 /* Do a database lookup for KEY. */
148 static enum nss_status
149 lookup (const DBT *key, struct STRUCTURE *result,
150 void *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)
151 {
152 char *p;
153 enum nss_status status;
154 int err;
155 DBT value;
156
157 /* Open the database. */
158 status = internal_setent (keep_db);
159 if (status != NSS_STATUS_SUCCESS)
160 {
161 *errnop = errno;
162 H_ERRNO_SET (NETDB_INTERNAL);
163 return status;
164 }
165
166 /* Succeed iff it matches a value that parses correctly. */
167 err = (*db->get) (db, key, &value, 0);
168 if (err < 0)
169 {
170 *errnop = errno;
171 H_ERRNO_SET (NETDB_INTERNAL);
172 status = NSS_STATUS_UNAVAIL;
173 }
174 else if (err != 0)
175 {
176 H_ERRNO_SET (HOST_NOT_FOUND);
177 status = NSS_STATUS_NOTFOUND;
178 }
179 else
180 {
181 /* Skip leading blanks. */
182 p = (char *) value.data;
183 while (isspace (*p))
184 ++p;
185
186 err = parse_line (p, result, buffer, buflen, errnop);
187
188 if (err == 0)
189 {
190 /* If the key begins with '0' we are trying to get the next
191 entry. We want to ignore unparsable lines in this case. */
192 if (((char *) key->data)[0] == '0')
193 {
194 /* Super magical return value. We need to tell our caller
195 that it should continue looping. This value cannot
196 happen in other cases. */
197 status = NSS_STATUS_RETURN;
198 }
199 else
200 {
201 H_ERRNO_SET (HOST_NOT_FOUND);
202 status = NSS_STATUS_NOTFOUND;
203 }
204 }
205 else if (err < 0)
206 {
207 H_ERRNO_SET (NETDB_INTERNAL);
208 status = NSS_STATUS_TRYAGAIN;
209 }
210 else
211 status = NSS_STATUS_SUCCESS;
212 }
213
214 if (! keep_db)
215 internal_endent ();
216
217 return status;
218 }
219
220
221 /* Macro for defining lookup functions for this DB-based database.
222
223 NAME is the name of the lookup; e.g. `pwnam'.
224
225 KEYPATTERN gives `printf' args to construct a key string;
226 e.g. `(".%s", name)'.
227
228 KEYSIZE gives the allocation size of a buffer to construct it in;
229 e.g. `1 + strlen (name)'.
230
231 PROTO describes the arguments for the lookup key;
232 e.g. `const char *name'.
233
234 BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
235
236 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
237 enum nss_status \
238 _nss_db_get##name##_r (proto, \
239 struct STRUCTURE *result, \
240 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
241 { \
242 DBT key; \
243 enum nss_status status; \
244 const size_t size = (keysize) + 1; \
245 key.data = __alloca (size); \
246 key.size = KEYPRINTF keypattern; \
247 __libc_lock_lock (lock); \
248 status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG); \
249 __libc_lock_unlock (lock); \
250 return status; \
251 }
252
253 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
254 \f
255
256
257
258 /* Return the next entry from the database file, doing locking. */
259 enum nss_status
260 CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
261 size_t buflen, int *errnop H_ERRNO_PROTO)
262 {
263 /* Return next entry in host file. */
264 enum nss_status status;
265 char buf[20];
266 DBT key;
267
268 __libc_lock_lock (lock);
269
270 /* Loop until we find a valid entry or hit EOF. See above for the
271 special meaning of the status value. */
272 do
273 {
274 key.size = snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
275 status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG);
276 }
277 while (status == NSS_STATUS_RETURN);
278
279 __libc_lock_unlock (lock);
280
281 return status;
282 }