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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <bits/libc-lock.h>
28 #include <kernel-features.h>
30 /* These symbols are defined by the including source file:
32 ENTNAME -- database name of the structure and functions (hostent, pwent).
33 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
34 DATABASE -- string of the database file's name ("hosts", "passwd").
36 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
38 Also see files-parse.c.
41 #define ENTNAME_r CONCAT(ENTNAME,_r)
43 #define DATAFILE "/etc/" DATABASE
47 # define H_ERRNO_PROTO , int *herrnop
48 # define H_ERRNO_ARG , herrnop
49 # define H_ERRNO_SET(val) (*herrnop = (val))
51 # define H_ERRNO_PROTO
53 # define H_ERRNO_SET(val) ((void) 0)
58 # define EXTRA_ARGS_DECL
59 # define EXTRA_ARGS_VALUE
62 /* Locks the static variables in this file. */
63 __libc_lock_define_initialized (static, lock
)
65 /* Maintenance of the shared stream open on the database file. */
68 static fpos_t position
;
69 static enum { nouse
, getent
, getby
} last_use
;
70 static int keep_stream
;
72 /* Open database file if not already opened. */
73 static enum nss_status
74 internal_setent (int stayopen
)
76 enum nss_status status
= NSS_STATUS_SUCCESS
;
80 stream
= fopen (DATAFILE
, "rce");
83 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
86 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
88 if (__have_o_cloexec
<= 0)
91 /* We have to make sure the file is `closed on exec'. */
95 result
= flags
= fcntl (fileno (stream
), F_GETFD
, 0);
99 if (__have_o_cloexec
== 0)
100 __have_o_cloexec
= (flags
& FD_CLOEXEC
) == 0 ? -1 : 1;
101 if (__have_o_cloexec
< 0)
105 result
= fcntl (fileno (stream
), F_SETFD
, flags
);
110 /* Something went wrong. Close the stream and return a
114 status
= NSS_STATUS_UNAVAIL
;
123 /* Remember STAYOPEN flag. */
125 keep_stream
|= stayopen
;
131 /* Thread-safe, exported version of that. */
133 CONCAT(_nss_files_set
,ENTNAME
) (int stayopen
)
135 enum nss_status status
;
137 __libc_lock_lock (lock
);
139 status
= internal_setent (stayopen
);
141 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
145 status
= NSS_STATUS_UNAVAIL
;
150 __libc_lock_unlock (lock
);
156 /* Close the database file. */
158 internal_endent (void)
168 /* Thread-safe, exported version of that. */
170 CONCAT(_nss_files_end
,ENTNAME
) (void)
172 __libc_lock_lock (lock
);
176 /* Reset STAYOPEN flag. */
179 __libc_lock_unlock (lock
);
181 return NSS_STATUS_SUCCESS
;
184 /* Parsing the database file into `struct STRUCTURE' data structures. */
186 static enum nss_status
187 internal_getent (struct STRUCTURE
*result
,
188 char *buffer
, size_t buflen
, int *errnop H_ERRNO_PROTO
192 struct parser_data
*data
= (void *) buffer
;
193 int linebuflen
= buffer
+ buflen
- data
->linebuffer
;
196 if (buflen
< sizeof *data
+ 2)
199 H_ERRNO_SET (NETDB_INTERNAL
);
200 return NSS_STATUS_TRYAGAIN
;
205 /* Terminate the line so that we can test for overflow. */
206 ((unsigned char *) data
->linebuffer
)[linebuflen
- 1] = '\xff';
208 p
= fgets_unlocked (data
->linebuffer
, linebuflen
, stream
);
211 /* End of file or read error. */
212 H_ERRNO_SET (HOST_NOT_FOUND
);
213 return NSS_STATUS_NOTFOUND
;
215 else if (((unsigned char *) data
->linebuffer
)[linebuflen
- 1] != 0xff)
217 /* The line is too long. Give the user the opportunity to
218 enlarge the buffer. */
220 H_ERRNO_SET (NETDB_INTERNAL
);
221 return NSS_STATUS_TRYAGAIN
;
224 /* Skip leading blanks. */
228 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
229 /* Parse the line. If it is invalid, loop to get the next
230 line of the file to parse. */
231 || ! (parse_result
= parse_line (p
, result
, data
, buflen
, errnop
234 if (__builtin_expect (parse_result
== -1, 0))
236 H_ERRNO_SET (NETDB_INTERNAL
);
237 return NSS_STATUS_TRYAGAIN
;
240 /* Filled in RESULT with the next entry from the database file. */
241 return NSS_STATUS_SUCCESS
;
245 /* Return the next entry from the database file, doing locking. */
247 CONCAT(_nss_files_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
248 size_t buflen
, int *errnop H_ERRNO_PROTO
)
250 /* Return next entry in host file. */
251 enum nss_status status
= NSS_STATUS_SUCCESS
;
253 __libc_lock_lock (lock
);
255 /* Be prepared that the set*ent function was not called before. */
258 int save_errno
= errno
;
260 status
= internal_setent (0);
262 __set_errno (save_errno
);
264 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
268 status
= NSS_STATUS_UNAVAIL
;
272 if (status
== NSS_STATUS_SUCCESS
)
274 /* If the last use was not by the getent function we need the
275 position the stream. */
276 if (last_use
!= getent
)
278 if (fsetpos (stream
, &position
) < 0)
279 status
= NSS_STATUS_UNAVAIL
;
284 if (status
== NSS_STATUS_SUCCESS
)
286 status
= internal_getent (result
, buffer
, buflen
, errnop
287 H_ERRNO_ARG EXTRA_ARGS_VALUE
);
289 /* Remember this position if we were successful. If the
290 operation failed we give the user a chance to repeat the
291 operation (perhaps the buffer was too small). */
292 if (status
== NSS_STATUS_SUCCESS
)
293 fgetpos (stream
, &position
);
295 /* We must make sure we reposition the stream the next call. */
300 __libc_lock_unlock (lock
);
305 /* Macro for defining lookup functions for this file-based database.
307 NAME is the name of the lookup; e.g. `hostbyname'.
309 DB_CHAR, KEYPATTERN, KEYSIZE are ignored here but used by db-XXX.c
310 e.g. `1 + sizeof (id) * 4'.
312 PROTO is the potentially empty list of other parameters.
314 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
315 to the lookup key arguments and does `break;' if they match. */
317 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
319 _nss_files_get##name##_r (proto, \
320 struct STRUCTURE *result, char *buffer, \
321 size_t buflen, int *errnop H_ERRNO_PROTO) \
323 enum nss_status status; \
325 __libc_lock_lock (lock); \
327 /* Reset file pointer to beginning or open file. */ \
328 status = internal_setent (keep_stream); \
330 if (status == NSS_STATUS_SUCCESS) \
332 /* Tell getent function that we have repositioned the file pointer. */ \
335 while ((status = internal_getent (result, buffer, buflen, errnop \
336 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
337 == NSS_STATUS_SUCCESS) \
341 internal_endent (); \
344 __libc_lock_unlock (lock); \