]>
Commit | Line | Data |
---|---|---|
5f0e6fc7 | 1 | /* Common code for file-based databases in nss_files module. |
d4697bc9 | 2 | Copyright (C) 1996-2014 Free Software Foundation, Inc. |
2303f5fd | 3 | This file is part of the GNU C Library. |
5f0e6fc7 | 4 | |
2303f5fd | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
5f0e6fc7 | 9 | |
2303f5fd UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
5f0e6fc7 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
5f0e6fc7 RM |
18 | |
19 | #include <stdio.h> | |
20 | #include <ctype.h> | |
26761c28 | 21 | #include <errno.h> |
3996f34b | 22 | #include <fcntl.h> |
5107cf1d | 23 | #include <bits/libc-lock.h> |
5f0e6fc7 RM |
24 | #include "nsswitch.h" |
25 | ||
aa132749 UD |
26 | #include <kernel-features.h> |
27 | ||
5f0e6fc7 RM |
28 | /* These symbols are defined by the including source file: |
29 | ||
30 | ENTNAME -- database name of the structure and functions (hostent, pwent). | |
31 | STRUCTURE -- struct name, define only if not ENTNAME (passwd, group). | |
96bda0ea | 32 | DATABASE -- string of the database file's name ("hosts", "passwd"). |
5f0e6fc7 RM |
33 | |
34 | NEED_H_ERRNO - defined iff an arg `int *herrnop' is used. | |
adc6ff7f RM |
35 | |
36 | Also see files-parse.c. | |
5f0e6fc7 RM |
37 | */ |
38 | ||
96bda0ea RM |
39 | #define ENTNAME_r CONCAT(ENTNAME,_r) |
40 | ||
41 | #define DATAFILE "/etc/" DATABASE | |
5f0e6fc7 RM |
42 | |
43 | #ifdef NEED_H_ERRNO | |
47707456 | 44 | # include <netdb.h> |
26761c28 UD |
45 | # define H_ERRNO_PROTO , int *herrnop |
46 | # define H_ERRNO_ARG , herrnop | |
47 | # define H_ERRNO_SET(val) (*herrnop = (val)) | |
5f0e6fc7 | 48 | #else |
26761c28 UD |
49 | # define H_ERRNO_PROTO |
50 | # define H_ERRNO_ARG | |
51 | # define H_ERRNO_SET(val) ((void) 0) | |
5f0e6fc7 RM |
52 | #endif |
53 | ||
71d3bda9 UD |
54 | #ifndef EXTRA_ARGS |
55 | # define EXTRA_ARGS | |
56 | # define EXTRA_ARGS_DECL | |
57 | # define EXTRA_ARGS_VALUE | |
58 | #endif | |
59 | ||
5f0e6fc7 | 60 | /* Locks the static variables in this file. */ |
1e16111c | 61 | __libc_lock_define_initialized (static, lock) |
5f0e6fc7 RM |
62 | \f |
63 | /* Maintenance of the shared stream open on the database file. */ | |
64 | ||
65 | static FILE *stream; | |
2303f5fd | 66 | static fpos_t position; |
0f283ffc | 67 | static enum { nouse, getent, getby } last_use; |
5f0e6fc7 RM |
68 | static int keep_stream; |
69 | ||
70 | /* Open database file if not already opened. */ | |
26761c28 | 71 | static enum nss_status |
5f0e6fc7 RM |
72 | internal_setent (int stayopen) |
73 | { | |
26761c28 | 74 | enum nss_status status = NSS_STATUS_SUCCESS; |
5f0e6fc7 RM |
75 | |
76 | if (stream == NULL) | |
77 | { | |
312be3f9 | 78 | stream = fopen (DATAFILE, "rce"); |
5f0e6fc7 RM |
79 | |
80 | if (stream == NULL) | |
0413b54c | 81 | status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; |
3996f34b UD |
82 | else |
83 | { | |
aa132749 UD |
84 | #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC |
85 | # ifdef O_CLOEXEC | |
86 | if (__have_o_cloexec <= 0) | |
87 | # endif | |
3996f34b | 88 | { |
aa132749 UD |
89 | /* We have to make sure the file is `closed on exec'. */ |
90 | int result; | |
91 | int flags; | |
92 | ||
93 | result = flags = fcntl (fileno (stream), F_GETFD, 0); | |
94 | if (result >= 0) | |
95 | { | |
96 | # ifdef O_CLOEXEC | |
97 | if (__have_o_cloexec == 0) | |
98 | __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1; | |
99 | if (__have_o_cloexec < 0) | |
100 | # endif | |
101 | { | |
102 | flags |= FD_CLOEXEC; | |
103 | result = fcntl (fileno (stream), F_SETFD, flags); | |
104 | } | |
105 | } | |
106 | if (result < 0) | |
107 | { | |
108 | /* Something went wrong. Close the stream and return a | |
109 | failure. */ | |
110 | fclose (stream); | |
111 | stream = NULL; | |
112 | status = NSS_STATUS_UNAVAIL; | |
113 | } | |
3996f34b | 114 | } |
aa132749 | 115 | #endif |
3996f34b | 116 | } |
5f0e6fc7 RM |
117 | } |
118 | else | |
119 | rewind (stream); | |
120 | ||
121 | /* Remember STAYOPEN flag. */ | |
122 | if (stream != NULL) | |
123 | keep_stream |= stayopen; | |
124 | ||
125 | return status; | |
126 | } | |
127 | ||
128 | ||
129 | /* Thread-safe, exported version of that. */ | |
26761c28 | 130 | enum nss_status |
5f0e6fc7 RM |
131 | CONCAT(_nss_files_set,ENTNAME) (int stayopen) |
132 | { | |
26761c28 | 133 | enum nss_status status; |
5f0e6fc7 RM |
134 | |
135 | __libc_lock_lock (lock); | |
136 | ||
137 | status = internal_setent (stayopen); | |
138 | ||
2303f5fd UD |
139 | if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0) |
140 | { | |
141 | fclose (stream); | |
142 | stream = NULL; | |
143 | status = NSS_STATUS_UNAVAIL; | |
144 | } | |
145 | ||
146 | last_use = getent; | |
147 | ||
5f0e6fc7 RM |
148 | __libc_lock_unlock (lock); |
149 | ||
150 | return status; | |
151 | } | |
152 | ||
153 | ||
154 | /* Close the database file. */ | |
155 | static void | |
156 | internal_endent (void) | |
157 | { | |
158 | if (stream != NULL) | |
159 | { | |
160 | fclose (stream); | |
161 | stream = NULL; | |
162 | } | |
5f0e6fc7 RM |
163 | } |
164 | ||
165 | ||
166 | /* Thread-safe, exported version of that. */ | |
26761c28 | 167 | enum nss_status |
5f0e6fc7 RM |
168 | CONCAT(_nss_files_end,ENTNAME) (void) |
169 | { | |
170 | __libc_lock_lock (lock); | |
171 | ||
172 | internal_endent (); | |
173 | ||
6dbe2837 RM |
174 | /* Reset STAYOPEN flag. */ |
175 | keep_stream = 0; | |
176 | ||
5f0e6fc7 RM |
177 | __libc_lock_unlock (lock); |
178 | ||
179 | return NSS_STATUS_SUCCESS; | |
180 | } | |
181 | \f | |
5f0e6fc7 | 182 | |
977f4b31 SP |
183 | typedef enum |
184 | { | |
185 | gcr_ok = 0, | |
186 | gcr_error = -1, | |
187 | gcr_overflow = -2 | |
188 | } get_contents_ret; | |
189 | ||
190 | /* Hack around the fact that fgets only accepts int sizes. */ | |
191 | static get_contents_ret | |
192 | get_contents (char *linebuf, size_t len, FILE *stream) | |
193 | { | |
194 | size_t remaining_len = len; | |
195 | char *curbuf = linebuf; | |
196 | ||
197 | do | |
198 | { | |
199 | int curlen = ((remaining_len > (size_t) INT_MAX) ? INT_MAX | |
200 | : remaining_len); | |
201 | char *p = fgets_unlocked (curbuf, curlen, stream); | |
202 | ||
203 | ((unsigned char *) curbuf)[curlen - 1] = 0xff; | |
204 | ||
205 | /* EOF or read error. */ | |
206 | if (p == NULL) | |
207 | return gcr_error; | |
208 | ||
209 | /* Done reading in the line. */ | |
210 | if (((unsigned char *) curbuf)[curlen - 1] == 0xff) | |
211 | return gcr_ok; | |
212 | ||
213 | /* Drop the terminating '\0'. */ | |
214 | remaining_len -= curlen - 1; | |
215 | curbuf += curlen - 1; | |
216 | } | |
217 | /* fgets copies one less than the input length. Our last iteration is of | |
218 | REMAINING_LEN and once that is done, REMAINING_LEN is decremented by | |
219 | REMAINING_LEN - 1, leaving the result as 1. */ | |
220 | while (remaining_len > 1); | |
221 | ||
222 | /* This means that the current buffer was not large enough. */ | |
223 | return gcr_overflow; | |
224 | } | |
225 | ||
226 | /* Parsing the database file into `struct STRUCTURE' data structures. */ | |
5f0e6fc7 RM |
227 | static enum nss_status |
228 | internal_getent (struct STRUCTURE *result, | |
71d3bda9 UD |
229 | char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO |
230 | EXTRA_ARGS_DECL) | |
5f0e6fc7 RM |
231 | { |
232 | char *p; | |
233 | struct parser_data *data = (void *) buffer; | |
977f4b31 | 234 | size_t linebuflen = buffer + buflen - data->linebuffer; |
22d57dd3 | 235 | int parse_result; |
5f0e6fc7 | 236 | |
4caef86c | 237 | if (buflen < sizeof *data + 2) |
5f0e6fc7 | 238 | { |
d71b808a | 239 | *errnop = ERANGE; |
47707456 | 240 | H_ERRNO_SET (NETDB_INTERNAL); |
ffee1316 | 241 | return NSS_STATUS_TRYAGAIN; |
5f0e6fc7 RM |
242 | } |
243 | ||
244 | do | |
245 | { | |
977f4b31 | 246 | get_contents_ret r = get_contents (data->linebuffer, linebuflen, stream); |
26761c28 | 247 | |
977f4b31 | 248 | if (r == gcr_error) |
5f0e6fc7 RM |
249 | { |
250 | /* End of file or read error. */ | |
251 | H_ERRNO_SET (HOST_NOT_FOUND); | |
252 | return NSS_STATUS_NOTFOUND; | |
253 | } | |
977f4b31 SP |
254 | |
255 | if (r == gcr_overflow) | |
26761c28 UD |
256 | { |
257 | /* The line is too long. Give the user the opportunity to | |
258 | enlarge the buffer. */ | |
d71b808a | 259 | *errnop = ERANGE; |
26761c28 UD |
260 | H_ERRNO_SET (NETDB_INTERNAL); |
261 | return NSS_STATUS_TRYAGAIN; | |
262 | } | |
6dbe2837 | 263 | |
977f4b31 SP |
264 | /* Everything OK. Now skip leading blanks. */ |
265 | p = data->linebuffer; | |
5f0e6fc7 RM |
266 | while (isspace (*p)) |
267 | ++p; | |
26761c28 UD |
268 | } |
269 | while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */ | |
270 | /* Parse the line. If it is invalid, loop to get the next | |
271 | line of the file to parse. */ | |
71d3bda9 UD |
272 | || ! (parse_result = parse_line (p, result, data, buflen, errnop |
273 | EXTRA_ARGS))); | |
5f0e6fc7 | 274 | |
405521b5 UD |
275 | if (__builtin_expect (parse_result == -1, 0)) |
276 | { | |
277 | H_ERRNO_SET (NETDB_INTERNAL); | |
278 | return NSS_STATUS_TRYAGAIN; | |
279 | } | |
280 | ||
5f0e6fc7 | 281 | /* Filled in RESULT with the next entry from the database file. */ |
405521b5 | 282 | return NSS_STATUS_SUCCESS; |
5f0e6fc7 RM |
283 | } |
284 | ||
285 | ||
286 | /* Return the next entry from the database file, doing locking. */ | |
26761c28 | 287 | enum nss_status |
d71b808a UD |
288 | CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer, |
289 | size_t buflen, int *errnop H_ERRNO_PROTO) | |
5f0e6fc7 RM |
290 | { |
291 | /* Return next entry in host file. */ | |
26761c28 | 292 | enum nss_status status = NSS_STATUS_SUCCESS; |
5f0e6fc7 RM |
293 | |
294 | __libc_lock_lock (lock); | |
295 | ||
26761c28 UD |
296 | /* Be prepared that the set*ent function was not called before. */ |
297 | if (stream == NULL) | |
d47aac39 | 298 | { |
0facd3df UD |
299 | int save_errno = errno; |
300 | ||
d47aac39 UD |
301 | status = internal_setent (0); |
302 | ||
9aef35a5 | 303 | __set_errno (save_errno); |
0facd3df | 304 | |
d47aac39 UD |
305 | if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0) |
306 | { | |
307 | fclose (stream); | |
308 | stream = NULL; | |
309 | status = NSS_STATUS_UNAVAIL; | |
310 | } | |
311 | } | |
2303f5fd | 312 | |
54d79e99 | 313 | if (status == NSS_STATUS_SUCCESS) |
2303f5fd | 314 | { |
26761c28 UD |
315 | /* If the last use was not by the getent function we need the |
316 | position the stream. */ | |
317 | if (last_use != getent) | |
04795ad9 UD |
318 | { |
319 | if (fsetpos (stream, &position) < 0) | |
320 | status = NSS_STATUS_UNAVAIL; | |
321 | else | |
322 | last_use = getent; | |
323 | } | |
26761c28 UD |
324 | |
325 | if (status == NSS_STATUS_SUCCESS) | |
326 | { | |
d71b808a | 327 | status = internal_getent (result, buffer, buflen, errnop |
71d3bda9 | 328 | H_ERRNO_ARG EXTRA_ARGS_VALUE); |
26761c28 UD |
329 | |
330 | /* Remember this position if we were successful. If the | |
331 | operation failed we give the user a chance to repeat the | |
332 | operation (perhaps the buffer was too small). */ | |
333 | if (status == NSS_STATUS_SUCCESS) | |
334 | fgetpos (stream, &position); | |
335 | else | |
336 | /* We must make sure we reposition the stream the next call. */ | |
0f283ffc | 337 | last_use = nouse; |
26761c28 | 338 | } |
2303f5fd | 339 | } |
5f0e6fc7 RM |
340 | |
341 | __libc_lock_unlock (lock); | |
342 | ||
343 | return status; | |
344 | } | |
345 | \f | |
346 | /* Macro for defining lookup functions for this file-based database. | |
347 | ||
348 | NAME is the name of the lookup; e.g. `hostbyname'. | |
349 | ||
2666d441 UD |
350 | DB_CHAR, KEYPATTERN, KEYSIZE are ignored here but used by db-XXX.c |
351 | e.g. `1 + sizeof (id) * 4'. | |
96bda0ea | 352 | |
2666d441 | 353 | PROTO is the potentially empty list of other parameters. |
5f0e6fc7 RM |
354 | |
355 | BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result' | |
356 | to the lookup key arguments and does `break;' if they match. */ | |
357 | ||
2666d441 | 358 | #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\ |
5f0e6fc7 RM |
359 | enum nss_status \ |
360 | _nss_files_get##name##_r (proto, \ | |
d71b808a UD |
361 | struct STRUCTURE *result, char *buffer, \ |
362 | size_t buflen, int *errnop H_ERRNO_PROTO) \ | |
5f0e6fc7 RM |
363 | { \ |
364 | enum nss_status status; \ | |
365 | \ | |
366 | __libc_lock_lock (lock); \ | |
367 | \ | |
368 | /* Reset file pointer to beginning or open file. */ \ | |
26761c28 | 369 | status = internal_setent (keep_stream); \ |
5f0e6fc7 | 370 | \ |
26761c28 UD |
371 | if (status == NSS_STATUS_SUCCESS) \ |
372 | { \ | |
373 | /* Tell getent function that we have repositioned the file pointer. */ \ | |
374 | last_use = getby; \ | |
2303f5fd | 375 | \ |
d71b808a | 376 | while ((status = internal_getent (result, buffer, buflen, errnop \ |
71d3bda9 | 377 | H_ERRNO_ARG EXTRA_ARGS_VALUE)) \ |
26761c28 UD |
378 | == NSS_STATUS_SUCCESS) \ |
379 | { break_if_match } \ | |
5f0e6fc7 | 380 | \ |
26761c28 UD |
381 | if (! keep_stream) \ |
382 | internal_endent (); \ | |
383 | } \ | |
5f0e6fc7 RM |
384 | \ |
385 | __libc_lock_unlock (lock); \ | |
386 | \ | |
387 | return status; \ | |
388 | } |