1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-1999,2001,2002,2004,2007,2008,2011
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
24 #include <bits/libc-lock.h>
27 #include <kernel-features.h>
29 /* These symbols are defined by the including source file:
31 ENTNAME -- database name of the structure and functions (hostent, pwent).
32 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
33 DATABASE -- string of the database file's name ("hosts", "passwd").
35 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
37 Also see files-parse.c.
40 #define ENTNAME_r CONCAT(ENTNAME,_r)
42 #define DATAFILE "/etc/" DATABASE
46 # define H_ERRNO_PROTO , int *herrnop
47 # define H_ERRNO_ARG , herrnop
48 # define H_ERRNO_SET(val) (*herrnop = (val))
50 # define H_ERRNO_PROTO
52 # define H_ERRNO_SET(val) ((void) 0)
57 # define EXTRA_ARGS_DECL
58 # define EXTRA_ARGS_VALUE
61 /* Locks the static variables in this file. */
62 __libc_lock_define_initialized (static, lock
)
64 /* Maintenance of the shared stream open on the database file. */
67 static fpos_t position
;
68 static enum { nouse
, getent
, getby
} last_use
;
69 static int keep_stream
;
71 /* Open database file if not already opened. */
72 static enum nss_status
73 internal_setent (int stayopen
)
75 enum nss_status status
= NSS_STATUS_SUCCESS
;
79 stream
= fopen (DATAFILE
, "rce");
82 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
85 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
87 if (__have_o_cloexec
<= 0)
90 /* We have to make sure the file is `closed on exec'. */
94 result
= flags
= fcntl (fileno (stream
), F_GETFD
, 0);
98 if (__have_o_cloexec
== 0)
99 __have_o_cloexec
= (flags
& FD_CLOEXEC
) == 0 ? -1 : 1;
100 if (__have_o_cloexec
< 0)
104 result
= fcntl (fileno (stream
), F_SETFD
, flags
);
109 /* Something went wrong. Close the stream and return a
113 status
= NSS_STATUS_UNAVAIL
;
122 /* Remember STAYOPEN flag. */
124 keep_stream
|= stayopen
;
130 /* Thread-safe, exported version of that. */
132 CONCAT(_nss_files_set
,ENTNAME
) (int stayopen
)
134 enum nss_status status
;
136 __libc_lock_lock (lock
);
138 status
= internal_setent (stayopen
);
140 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
144 status
= NSS_STATUS_UNAVAIL
;
149 __libc_lock_unlock (lock
);
155 /* Close the database file. */
157 internal_endent (void)
167 /* Thread-safe, exported version of that. */
169 CONCAT(_nss_files_end
,ENTNAME
) (void)
171 __libc_lock_lock (lock
);
175 /* Reset STAYOPEN flag. */
178 __libc_lock_unlock (lock
);
180 return NSS_STATUS_SUCCESS
;
183 /* Parsing the database file into `struct STRUCTURE' data structures. */
185 static enum nss_status
186 internal_getent (struct STRUCTURE
*result
,
187 char *buffer
, size_t buflen
, int *errnop H_ERRNO_PROTO
191 struct parser_data
*data
= (void *) buffer
;
192 int linebuflen
= buffer
+ buflen
- data
->linebuffer
;
195 if (buflen
< sizeof *data
+ 2)
198 H_ERRNO_SET (NETDB_INTERNAL
);
199 return NSS_STATUS_TRYAGAIN
;
204 /* Terminate the line so that we can test for overflow. */
205 ((unsigned char *) data
->linebuffer
)[linebuflen
- 1] = '\xff';
207 p
= fgets_unlocked (data
->linebuffer
, linebuflen
, stream
);
210 /* End of file or read error. */
211 H_ERRNO_SET (HOST_NOT_FOUND
);
212 return NSS_STATUS_NOTFOUND
;
214 else if (((unsigned char *) data
->linebuffer
)[linebuflen
- 1] != 0xff)
216 /* The line is too long. Give the user the opportunity to
217 enlarge the buffer. */
219 H_ERRNO_SET (NETDB_INTERNAL
);
220 return NSS_STATUS_TRYAGAIN
;
223 /* Skip leading blanks. */
227 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
228 /* Parse the line. If it is invalid, loop to get the next
229 line of the file to parse. */
230 || ! (parse_result
= parse_line (p
, result
, data
, buflen
, errnop
233 if (__builtin_expect (parse_result
== -1, 0))
235 H_ERRNO_SET (NETDB_INTERNAL
);
236 return NSS_STATUS_TRYAGAIN
;
239 /* Filled in RESULT with the next entry from the database file. */
240 return NSS_STATUS_SUCCESS
;
244 /* Return the next entry from the database file, doing locking. */
246 CONCAT(_nss_files_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
247 size_t buflen
, int *errnop H_ERRNO_PROTO
)
249 /* Return next entry in host file. */
250 enum nss_status status
= NSS_STATUS_SUCCESS
;
252 __libc_lock_lock (lock
);
254 /* Be prepared that the set*ent function was not called before. */
257 int save_errno
= errno
;
259 status
= internal_setent (0);
261 __set_errno (save_errno
);
263 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
267 status
= NSS_STATUS_UNAVAIL
;
271 if (status
== NSS_STATUS_SUCCESS
)
273 /* If the last use was not by the getent function we need the
274 position the stream. */
275 if (last_use
!= getent
)
277 if (fsetpos (stream
, &position
) < 0)
278 status
= NSS_STATUS_UNAVAIL
;
283 if (status
== NSS_STATUS_SUCCESS
)
285 status
= internal_getent (result
, buffer
, buflen
, errnop
286 H_ERRNO_ARG EXTRA_ARGS_VALUE
);
288 /* Remember this position if we were successful. If the
289 operation failed we give the user a chance to repeat the
290 operation (perhaps the buffer was too small). */
291 if (status
== NSS_STATUS_SUCCESS
)
292 fgetpos (stream
, &position
);
294 /* We must make sure we reposition the stream the next call. */
299 __libc_lock_unlock (lock
);
304 /* Macro for defining lookup functions for this file-based database.
306 NAME is the name of the lookup; e.g. `hostbyname'.
308 DB_CHAR, KEYPATTERN, KEYSIZE are ignored here but used by db-XXX.c
309 e.g. `1 + sizeof (id) * 4'.
311 PROTO is the potentially empty list of other parameters.
313 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
314 to the lookup key arguments and does `break;' if they match. */
316 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
318 _nss_files_get##name##_r (proto, \
319 struct STRUCTURE *result, char *buffer, \
320 size_t buflen, int *errnop H_ERRNO_PROTO) \
322 enum nss_status status; \
324 __libc_lock_lock (lock); \
326 /* Reset file pointer to beginning or open file. */ \
327 status = internal_setent (keep_stream); \
329 if (status == NSS_STATUS_SUCCESS) \
331 /* Tell getent function that we have repositioned the file pointer. */ \
334 while ((status = internal_getent (result, buffer, buflen, errnop \
335 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
336 == NSS_STATUS_SUCCESS) \
340 internal_endent (); \
343 __libc_lock_unlock (lock); \