Update copyright dates with scripts/update-copyrights.
[glibc.git] / nss / nss_files / files-XXX.c
bloba7a45e5b0e9f68f5ece6cdbb393d8701d1bb9bca
1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <bits/libc-lock.h>
24 #include "nsswitch.h"
26 #include <kernel-features.h>
28 /* These symbols are defined by the including source file:
30 ENTNAME -- database name of the structure and functions (hostent, pwent).
31 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
32 DATABASE -- string of the database file's name ("hosts", "passwd").
34 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
36 Also see files-parse.c.
39 #define ENTNAME_r CONCAT(ENTNAME,_r)
41 #define DATAFILE "/etc/" DATABASE
43 #ifdef NEED_H_ERRNO
44 # include <netdb.h>
45 # define H_ERRNO_PROTO , int *herrnop
46 # define H_ERRNO_ARG , herrnop
47 # define H_ERRNO_SET(val) (*herrnop = (val))
48 #else
49 # define H_ERRNO_PROTO
50 # define H_ERRNO_ARG
51 # define H_ERRNO_SET(val) ((void) 0)
52 #endif
54 #ifndef EXTRA_ARGS
55 # define EXTRA_ARGS
56 # define EXTRA_ARGS_DECL
57 # define EXTRA_ARGS_VALUE
58 #endif
60 /* Locks the static variables in this file. */
61 __libc_lock_define_initialized (static, lock)
63 /* Maintenance of the shared stream open on the database file. */
65 static FILE *stream;
66 static fpos_t position;
67 static enum { nouse, getent, getby } last_use;
68 static int keep_stream;
70 /* Open database file if not already opened. */
71 static enum nss_status
72 internal_setent (int stayopen)
74 enum nss_status status = NSS_STATUS_SUCCESS;
76 if (stream == NULL)
78 stream = fopen (DATAFILE, "rce");
80 if (stream == NULL)
81 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
82 else
84 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
85 # ifdef O_CLOEXEC
86 if (__have_o_cloexec <= 0)
87 # endif
89 /* We have to make sure the file is `closed on exec'. */
90 int result;
91 int flags;
93 result = flags = fcntl (fileno (stream), F_GETFD, 0);
94 if (result >= 0)
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
102 flags |= FD_CLOEXEC;
103 result = fcntl (fileno (stream), F_SETFD, flags);
106 if (result < 0)
108 /* Something went wrong. Close the stream and return a
109 failure. */
110 fclose (stream);
111 stream = NULL;
112 status = NSS_STATUS_UNAVAIL;
115 #endif
118 else
119 rewind (stream);
121 /* Remember STAYOPEN flag. */
122 if (stream != NULL)
123 keep_stream |= stayopen;
125 return status;
129 /* Thread-safe, exported version of that. */
130 enum nss_status
131 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
133 enum nss_status status;
135 __libc_lock_lock (lock);
137 status = internal_setent (stayopen);
139 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
141 fclose (stream);
142 stream = NULL;
143 status = NSS_STATUS_UNAVAIL;
146 last_use = getent;
148 __libc_lock_unlock (lock);
150 return status;
154 /* Close the database file. */
155 static void
156 internal_endent (void)
158 if (stream != NULL)
160 fclose (stream);
161 stream = NULL;
166 /* Thread-safe, exported version of that. */
167 enum nss_status
168 CONCAT(_nss_files_end,ENTNAME) (void)
170 __libc_lock_lock (lock);
172 internal_endent ();
174 /* Reset STAYOPEN flag. */
175 keep_stream = 0;
177 __libc_lock_unlock (lock);
179 return NSS_STATUS_SUCCESS;
183 typedef enum
185 gcr_ok = 0,
186 gcr_error = -1,
187 gcr_overflow = -2
188 } get_contents_ret;
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)
194 size_t remaining_len = len;
195 char *curbuf = linebuf;
199 int curlen = ((remaining_len > (size_t) INT_MAX) ? INT_MAX
200 : remaining_len);
202 /* Terminate the line so that we can test for overflow. */
203 ((unsigned char *) curbuf)[curlen - 1] = 0xff;
205 char *p = fgets_unlocked (curbuf, curlen, stream);
207 /* EOF or read error. */
208 if (p == NULL)
209 return gcr_error;
211 /* Done reading in the line. */
212 if (((unsigned char *) curbuf)[curlen - 1] == 0xff)
213 return gcr_ok;
215 /* Drop the terminating '\0'. */
216 remaining_len -= curlen - 1;
217 curbuf += curlen - 1;
219 /* fgets copies one less than the input length. Our last iteration is of
220 REMAINING_LEN and once that is done, REMAINING_LEN is decremented by
221 REMAINING_LEN - 1, leaving the result as 1. */
222 while (remaining_len > 1);
224 /* This means that the current buffer was not large enough. */
225 return gcr_overflow;
228 /* Parsing the database file into `struct STRUCTURE' data structures. */
229 static enum nss_status
230 internal_getent (struct STRUCTURE *result,
231 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO
232 EXTRA_ARGS_DECL)
234 char *p;
235 struct parser_data *data = (void *) buffer;
236 size_t linebuflen = buffer + buflen - data->linebuffer;
237 int parse_result;
239 if (buflen < sizeof *data + 2)
241 *errnop = ERANGE;
242 H_ERRNO_SET (NETDB_INTERNAL);
243 return NSS_STATUS_TRYAGAIN;
248 get_contents_ret r = get_contents (data->linebuffer, linebuflen, stream);
250 if (r == gcr_error)
252 /* End of file or read error. */
253 H_ERRNO_SET (HOST_NOT_FOUND);
254 return NSS_STATUS_NOTFOUND;
257 if (r == gcr_overflow)
259 /* The line is too long. Give the user the opportunity to
260 enlarge the buffer. */
261 *errnop = ERANGE;
262 H_ERRNO_SET (NETDB_INTERNAL);
263 return NSS_STATUS_TRYAGAIN;
266 /* Everything OK. Now skip leading blanks. */
267 p = data->linebuffer;
268 while (isspace (*p))
269 ++p;
271 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
272 /* Parse the line. If it is invalid, loop to get the next
273 line of the file to parse. */
274 || ! (parse_result = parse_line (p, result, data, buflen, errnop
275 EXTRA_ARGS)));
277 if (__glibc_unlikely (parse_result == -1))
279 H_ERRNO_SET (NETDB_INTERNAL);
280 return NSS_STATUS_TRYAGAIN;
283 /* Filled in RESULT with the next entry from the database file. */
284 return NSS_STATUS_SUCCESS;
288 /* Return the next entry from the database file, doing locking. */
289 enum nss_status
290 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
291 size_t buflen, int *errnop H_ERRNO_PROTO)
293 /* Return next entry in host file. */
294 enum nss_status status = NSS_STATUS_SUCCESS;
296 __libc_lock_lock (lock);
298 /* Be prepared that the set*ent function was not called before. */
299 if (stream == NULL)
301 int save_errno = errno;
303 status = internal_setent (0);
305 __set_errno (save_errno);
307 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
309 fclose (stream);
310 stream = NULL;
311 status = NSS_STATUS_UNAVAIL;
315 if (status == NSS_STATUS_SUCCESS)
317 /* If the last use was not by the getent function we need the
318 position the stream. */
319 if (last_use != getent)
321 if (fsetpos (stream, &position) < 0)
322 status = NSS_STATUS_UNAVAIL;
323 else
324 last_use = getent;
327 if (status == NSS_STATUS_SUCCESS)
329 status = internal_getent (result, buffer, buflen, errnop
330 H_ERRNO_ARG EXTRA_ARGS_VALUE);
332 /* Remember this position if we were successful. If the
333 operation failed we give the user a chance to repeat the
334 operation (perhaps the buffer was too small). */
335 if (status == NSS_STATUS_SUCCESS)
336 fgetpos (stream, &position);
337 else
338 /* We must make sure we reposition the stream the next call. */
339 last_use = nouse;
343 __libc_lock_unlock (lock);
345 return status;
348 /* Macro for defining lookup functions for this file-based database.
350 NAME is the name of the lookup; e.g. `hostbyname'.
352 DB_CHAR, KEYPATTERN, KEYSIZE are ignored here but used by db-XXX.c
353 e.g. `1 + sizeof (id) * 4'.
355 PROTO is the potentially empty list of other parameters.
357 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
358 to the lookup key arguments and does `break;' if they match. */
360 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
361 enum nss_status \
362 _nss_files_get##name##_r (proto, \
363 struct STRUCTURE *result, char *buffer, \
364 size_t buflen, int *errnop H_ERRNO_PROTO) \
366 enum nss_status status; \
368 __libc_lock_lock (lock); \
370 /* Reset file pointer to beginning or open file. */ \
371 status = internal_setent (keep_stream); \
373 if (status == NSS_STATUS_SUCCESS) \
375 /* Tell getent function that we have repositioned the file pointer. */ \
376 last_use = getby; \
378 while ((status = internal_getent (result, buffer, buflen, errnop \
379 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
380 == NSS_STATUS_SUCCESS) \
381 { break_if_match } \
383 if (! keep_stream) \
384 internal_endent (); \
387 __libc_lock_unlock (lock); \
389 return status; \