1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-2020 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 <https://www.gnu.org/licenses/>. */
23 #include <libc-lock.h>
25 #include <nss_files.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 stream open on the database file. For getXXent
65 operations the stream needs to be held open across calls, the other
66 getXXbyYY operations all use their own stream. */
70 /* Open database file if not already opened. */
71 static enum nss_status
72 internal_setent (FILE **stream
)
74 enum nss_status status
= NSS_STATUS_SUCCESS
;
78 *stream
= __nss_files_fopen (DATAFILE
);
81 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
90 /* Thread-safe, exported version of that. */
92 CONCAT(_nss_files_set
,ENTNAME
) (int stayopen
)
94 enum nss_status status
;
96 __libc_lock_lock (lock
);
98 status
= internal_setent (&stream
);
100 __libc_lock_unlock (lock
);
106 /* Close the database file. */
108 internal_endent (FILE **stream
)
118 /* Thread-safe, exported version of that. */
120 CONCAT(_nss_files_end
,ENTNAME
) (void)
122 __libc_lock_lock (lock
);
124 internal_endent (&stream
);
126 __libc_lock_unlock (lock
);
128 return NSS_STATUS_SUCCESS
;
132 /* Parsing the database file into `struct STRUCTURE' data structures. */
133 static enum nss_status
134 internal_getent (FILE *stream
, struct STRUCTURE
*result
,
135 char *buffer
, size_t buflen
, int *errnop H_ERRNO_PROTO
139 struct parser_data
*data
= (void *) buffer
;
140 size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
143 if (buflen
< sizeof *data
+ 2)
146 H_ERRNO_SET (NETDB_INTERNAL
);
147 return NSS_STATUS_TRYAGAIN
;
152 ssize_t r
= __libc_readline_unlocked
153 (stream
, data
->linebuffer
, linebuflen
);
157 H_ERRNO_SET (NETDB_INTERNAL
);
158 if (*errnop
== ERANGE
)
159 /* Request larger buffer. */
160 return NSS_STATUS_TRYAGAIN
;
162 /* Other read failure. */
163 return NSS_STATUS_UNAVAIL
;
168 H_ERRNO_SET (HOST_NOT_FOUND
);
169 return NSS_STATUS_NOTFOUND
;
172 /* Everything OK. Now skip leading blanks. */
173 p
= data
->linebuffer
;
177 /* Ignore empty and comment lines. */
178 if (*p
== '\0' || *p
== '#')
181 /* Parse the line. */
183 parse_result
= parse_line (p
, result
, data
, buflen
, errnop EXTRA_ARGS
);
185 if (parse_result
== -1)
187 if (*errnop
== ERANGE
)
189 /* Return to the original file position at the beginning
190 of the line, so that the next call can read it again
192 if (__fseeko64 (stream
, -r
, SEEK_CUR
) != 0)
198 H_ERRNO_SET (NETDB_INTERNAL
);
199 return NSS_STATUS_UNAVAIL
;
202 H_ERRNO_SET (NETDB_INTERNAL
);
203 return NSS_STATUS_TRYAGAIN
;
206 /* Return the data if parsed successfully. */
207 if (parse_result
!= 0)
208 return NSS_STATUS_SUCCESS
;
210 /* If it is invalid, loop to get the next line of the file to
216 /* Return the next entry from the database file, doing locking. */
218 CONCAT(_nss_files_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
219 size_t buflen
, int *errnop H_ERRNO_PROTO
)
221 /* Return next entry in host file. */
222 enum nss_status status
= NSS_STATUS_SUCCESS
;
224 __libc_lock_lock (lock
);
226 /* Be prepared that the set*ent function was not called before. */
229 int save_errno
= errno
;
231 status
= internal_setent (&stream
);
233 __set_errno (save_errno
);
236 if (status
== NSS_STATUS_SUCCESS
)
237 status
= internal_getent (stream
, result
, buffer
, buflen
, errnop
238 H_ERRNO_ARG EXTRA_ARGS_VALUE
);
240 __libc_lock_unlock (lock
);
245 /* Macro for defining lookup functions for this file-based database.
247 NAME is the name of the lookup; e.g. `hostbyname'.
249 DB_CHAR, KEYPATTERN, KEYSIZE are ignored here but used by db-XXX.c
250 e.g. `1 + sizeof (id) * 4'.
252 PROTO is the potentially empty list of other parameters.
254 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
255 to the lookup key arguments and does `break;' if they match. */
257 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
259 _nss_files_get##name##_r (proto, \
260 struct STRUCTURE *result, char *buffer, \
261 size_t buflen, int *errnop H_ERRNO_PROTO) \
263 enum nss_status status; \
264 FILE *stream = NULL; \
267 status = internal_setent (&stream); \
269 if (status == NSS_STATUS_SUCCESS) \
271 while ((status = internal_getent (stream, result, buffer, buflen, errnop \
272 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
273 == NSS_STATUS_SUCCESS) \
276 internal_endent (&stream); \