]>
Commit | Line | Data |
---|---|---|
5f0e6fc7 | 1 | /* Common code for file-based databases in nss_files module. |
af69217f | 2 | Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. |
2303f5fd | 3 | This file is part of the GNU C Library. |
5f0e6fc7 | 4 | |
2303f5fd UD |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | 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 | |
13 | Library General Public License for more details. | |
5f0e6fc7 | 14 | |
2303f5fd UD |
15 | You should have received a copy of the GNU Library General Public |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
5f0e6fc7 RM |
19 | |
20 | #include <stdio.h> | |
21 | #include <ctype.h> | |
22 | #include <assert.h> | |
26761c28 | 23 | #include <errno.h> |
3996f34b | 24 | #include <fcntl.h> |
5107cf1d | 25 | #include <bits/libc-lock.h> |
5f0e6fc7 RM |
26 | #include "nsswitch.h" |
27 | ||
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 | ||
54 | /* Locks the static variables in this file. */ | |
1e16111c | 55 | __libc_lock_define_initialized (static, lock) |
5f0e6fc7 RM |
56 | \f |
57 | /* Maintenance of the shared stream open on the database file. */ | |
58 | ||
59 | static FILE *stream; | |
2303f5fd UD |
60 | static fpos_t position; |
61 | static enum { none, getent, getby } last_use; | |
5f0e6fc7 RM |
62 | static int keep_stream; |
63 | ||
64 | /* Open database file if not already opened. */ | |
26761c28 | 65 | static enum nss_status |
5f0e6fc7 RM |
66 | internal_setent (int stayopen) |
67 | { | |
26761c28 | 68 | enum nss_status status = NSS_STATUS_SUCCESS; |
5f0e6fc7 RM |
69 | |
70 | if (stream == NULL) | |
71 | { | |
72 | stream = fopen (DATAFILE, "r"); | |
73 | ||
74 | if (stream == NULL) | |
0413b54c | 75 | status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; |
3996f34b UD |
76 | else |
77 | { | |
78 | /* We have to make sure the file is `closed on exec'. */ | |
79 | int result, flags; | |
80 | ||
81 | result = flags = fcntl (fileno (stream), F_GETFD, 0); | |
82 | if (result >= 0) | |
83 | { | |
84 | flags |= FD_CLOEXEC; | |
85 | result = fcntl (fileno (stream), F_SETFD, flags); | |
86 | } | |
87 | if (result < 0) | |
88 | { | |
89 | /* Something went wrong. Close the stream and return a | |
90 | failure. */ | |
91 | fclose (stream); | |
92 | stream = NULL; | |
93 | status = NSS_STATUS_UNAVAIL; | |
94 | } | |
95 | } | |
5f0e6fc7 RM |
96 | } |
97 | else | |
98 | rewind (stream); | |
99 | ||
100 | /* Remember STAYOPEN flag. */ | |
101 | if (stream != NULL) | |
102 | keep_stream |= stayopen; | |
103 | ||
104 | return status; | |
105 | } | |
106 | ||
107 | ||
108 | /* Thread-safe, exported version of that. */ | |
26761c28 | 109 | enum nss_status |
5f0e6fc7 RM |
110 | CONCAT(_nss_files_set,ENTNAME) (int stayopen) |
111 | { | |
26761c28 | 112 | enum nss_status status; |
5f0e6fc7 RM |
113 | |
114 | __libc_lock_lock (lock); | |
115 | ||
116 | status = internal_setent (stayopen); | |
117 | ||
2303f5fd UD |
118 | if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0) |
119 | { | |
120 | fclose (stream); | |
121 | stream = NULL; | |
122 | status = NSS_STATUS_UNAVAIL; | |
123 | } | |
124 | ||
125 | last_use = getent; | |
126 | ||
5f0e6fc7 RM |
127 | __libc_lock_unlock (lock); |
128 | ||
129 | return status; | |
130 | } | |
131 | ||
132 | ||
133 | /* Close the database file. */ | |
134 | static void | |
135 | internal_endent (void) | |
136 | { | |
137 | if (stream != NULL) | |
138 | { | |
139 | fclose (stream); | |
140 | stream = NULL; | |
141 | } | |
5f0e6fc7 RM |
142 | } |
143 | ||
144 | ||
145 | /* Thread-safe, exported version of that. */ | |
26761c28 | 146 | enum nss_status |
5f0e6fc7 RM |
147 | CONCAT(_nss_files_end,ENTNAME) (void) |
148 | { | |
149 | __libc_lock_lock (lock); | |
150 | ||
151 | internal_endent (); | |
152 | ||
6dbe2837 RM |
153 | /* Reset STAYOPEN flag. */ |
154 | keep_stream = 0; | |
155 | ||
5f0e6fc7 RM |
156 | __libc_lock_unlock (lock); |
157 | ||
158 | return NSS_STATUS_SUCCESS; | |
159 | } | |
160 | \f | |
161 | /* Parsing the database file into `struct STRUCTURE' data structures. */ | |
162 | ||
163 | static enum nss_status | |
164 | internal_getent (struct STRUCTURE *result, | |
d71b808a | 165 | char *buffer, int buflen, int *errnop H_ERRNO_PROTO) |
5f0e6fc7 RM |
166 | { |
167 | char *p; | |
168 | struct parser_data *data = (void *) buffer; | |
169 | int linebuflen = buffer + buflen - data->linebuffer; | |
22d57dd3 | 170 | int parse_result; |
5f0e6fc7 | 171 | |
6dbe2837 | 172 | if (buflen < (int) sizeof *data + 1) |
5f0e6fc7 | 173 | { |
d71b808a | 174 | *errnop = ERANGE; |
47707456 | 175 | H_ERRNO_SET (NETDB_INTERNAL); |
ffee1316 | 176 | return NSS_STATUS_TRYAGAIN; |
5f0e6fc7 RM |
177 | } |
178 | ||
179 | do | |
180 | { | |
26761c28 | 181 | /* Terminate the line so that we can test for overflow. */ |
af69217f | 182 | data->linebuffer[linebuflen - 1] = '\xff'; |
26761c28 | 183 | |
5f0e6fc7 RM |
184 | p = fgets (data->linebuffer, linebuflen, stream); |
185 | if (p == NULL) | |
186 | { | |
187 | /* End of file or read error. */ | |
d71b808a | 188 | *errnop = errno; |
5f0e6fc7 RM |
189 | H_ERRNO_SET (HOST_NOT_FOUND); |
190 | return NSS_STATUS_NOTFOUND; | |
191 | } | |
af69217f | 192 | else if (data->linebuffer[linebuflen - 1] != '\xff') |
26761c28 UD |
193 | { |
194 | /* The line is too long. Give the user the opportunity to | |
195 | enlarge the buffer. */ | |
d71b808a | 196 | *errnop = ERANGE; |
26761c28 UD |
197 | H_ERRNO_SET (NETDB_INTERNAL); |
198 | return NSS_STATUS_TRYAGAIN; | |
199 | } | |
6dbe2837 | 200 | |
5f0e6fc7 RM |
201 | /* Skip leading blanks. */ |
202 | while (isspace (*p)) | |
203 | ++p; | |
26761c28 UD |
204 | } |
205 | while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */ | |
206 | /* Parse the line. If it is invalid, loop to get the next | |
207 | line of the file to parse. */ | |
d71b808a | 208 | || ! (parse_result = parse_line (p, result, data, buflen, errnop))); |
5f0e6fc7 RM |
209 | |
210 | /* Filled in RESULT with the next entry from the database file. */ | |
22d57dd3 | 211 | return parse_result == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_SUCCESS; |
5f0e6fc7 RM |
212 | } |
213 | ||
214 | ||
215 | /* Return the next entry from the database file, doing locking. */ | |
26761c28 | 216 | enum nss_status |
d71b808a UD |
217 | CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer, |
218 | size_t buflen, int *errnop H_ERRNO_PROTO) | |
5f0e6fc7 RM |
219 | { |
220 | /* Return next entry in host file. */ | |
26761c28 | 221 | enum nss_status status = NSS_STATUS_SUCCESS; |
5f0e6fc7 RM |
222 | |
223 | __libc_lock_lock (lock); | |
224 | ||
26761c28 UD |
225 | /* Be prepared that the set*ent function was not called before. */ |
226 | if (stream == NULL) | |
227 | status = internal_setent (0); | |
2303f5fd | 228 | |
54d79e99 | 229 | if (status == NSS_STATUS_SUCCESS) |
2303f5fd | 230 | { |
26761c28 UD |
231 | /* If the last use was not by the getent function we need the |
232 | position the stream. */ | |
233 | if (last_use != getent) | |
234 | if (fsetpos (stream, &position) < 0) | |
235 | status = NSS_STATUS_UNAVAIL; | |
236 | else | |
237 | last_use = getent; | |
238 | ||
239 | if (status == NSS_STATUS_SUCCESS) | |
240 | { | |
d71b808a UD |
241 | status = internal_getent (result, buffer, buflen, errnop |
242 | H_ERRNO_ARG); | |
26761c28 UD |
243 | |
244 | /* Remember this position if we were successful. If the | |
245 | operation failed we give the user a chance to repeat the | |
246 | operation (perhaps the buffer was too small). */ | |
247 | if (status == NSS_STATUS_SUCCESS) | |
248 | fgetpos (stream, &position); | |
249 | else | |
250 | /* We must make sure we reposition the stream the next call. */ | |
251 | last_use = none; | |
252 | } | |
2303f5fd | 253 | } |
5f0e6fc7 RM |
254 | |
255 | __libc_lock_unlock (lock); | |
256 | ||
257 | return status; | |
258 | } | |
259 | \f | |
260 | /* Macro for defining lookup functions for this file-based database. | |
261 | ||
262 | NAME is the name of the lookup; e.g. `hostbyname'. | |
263 | ||
96bda0ea RM |
264 | KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c. |
265 | ||
5f0e6fc7 RM |
266 | PROTO describes the arguments for the lookup key; |
267 | e.g. `const char *hostname'. | |
268 | ||
269 | BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result' | |
270 | to the lookup key arguments and does `break;' if they match. */ | |
271 | ||
96bda0ea | 272 | #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \ |
5f0e6fc7 RM |
273 | enum nss_status \ |
274 | _nss_files_get##name##_r (proto, \ | |
d71b808a UD |
275 | struct STRUCTURE *result, char *buffer, \ |
276 | size_t buflen, int *errnop H_ERRNO_PROTO) \ | |
5f0e6fc7 RM |
277 | { \ |
278 | enum nss_status status; \ | |
279 | \ | |
280 | __libc_lock_lock (lock); \ | |
281 | \ | |
282 | /* Reset file pointer to beginning or open file. */ \ | |
26761c28 | 283 | status = internal_setent (keep_stream); \ |
5f0e6fc7 | 284 | \ |
26761c28 UD |
285 | if (status == NSS_STATUS_SUCCESS) \ |
286 | { \ | |
287 | /* Tell getent function that we have repositioned the file pointer. */ \ | |
288 | last_use = getby; \ | |
2303f5fd | 289 | \ |
d71b808a UD |
290 | while ((status = internal_getent (result, buffer, buflen, errnop \ |
291 | H_ERRNO_ARG)) \ | |
26761c28 UD |
292 | == NSS_STATUS_SUCCESS) \ |
293 | { break_if_match } \ | |
5f0e6fc7 | 294 | \ |
26761c28 UD |
295 | if (! keep_stream) \ |
296 | internal_endent (); \ | |
297 | } \ | |
5f0e6fc7 RM |
298 | \ |
299 | __libc_lock_unlock (lock); \ | |
300 | \ | |
301 | return status; \ | |
302 | } |