]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nss_files/files-alias.c
More optimizations of nss_files
[thirdparty/glibc.git] / nss / nss_files / files-alias.c
1 /* Mail alias file parser in nss_files module.
2 Copyright (C) 1996-1999,2002,2006,2007,2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
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.
10
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.
15
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
19 02111-1307 USA. */
20
21 #include <aliases.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <bits/libc-lock.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <kernel-features.h>
31
32 #include "nsswitch.h"
33
34 /* Locks the static variables in this file. */
35 __libc_lock_define_initialized (static, lock)
36 \f
37 /* Maintenance of the shared stream open on the database file. */
38
39 static FILE *stream;
40 static fpos_t position;
41 static enum { nouse, getent, getby } last_use;
42
43
44 static enum nss_status
45 internal_setent (void)
46 {
47 enum nss_status status = NSS_STATUS_SUCCESS;
48
49 if (stream == NULL)
50 {
51 stream = fopen ("/etc/aliases", "re");
52
53 if (stream == NULL)
54 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
55 else
56 {
57 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
58 # ifdef O_CLOEXEC
59 if (__have_o_cloexec <= 0)
60 # endif
61 {
62 /* We have to make sure the file is `closed on exec'. */
63 int result;
64 int flags;
65
66 result = flags = fcntl (fileno (stream), F_GETFD, 0);
67 if (result >= 0)
68 {
69 # ifdef O_CLOEXEC
70 if (__have_o_cloexec == 0)
71 __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1;
72 if (__have_o_cloexec < 0)
73 # endif
74 {
75 flags |= FD_CLOEXEC;
76 result = fcntl (fileno (stream), F_SETFD, flags);
77 }
78 }
79 if (result < 0)
80 {
81 /* Something went wrong. Close the stream and return a
82 failure. */
83 fclose (stream);
84 stream = NULL;
85 status = NSS_STATUS_UNAVAIL;
86 }
87 }
88 #endif
89 }
90 }
91 else
92 rewind (stream);
93
94 return status;
95 }
96
97
98 /* Thread-safe, exported version of that. */
99 enum nss_status
100 _nss_files_setaliasent (void)
101 {
102 enum nss_status status;
103
104 __libc_lock_lock (lock);
105
106 status = internal_setent ();
107
108 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
109 {
110 fclose (stream);
111 stream = NULL;
112 status = NSS_STATUS_UNAVAIL;
113 }
114
115 last_use = getent;
116
117 __libc_lock_unlock (lock);
118
119 return status;
120 }
121
122
123 /* Close the database file. */
124 static void
125 internal_endent (void)
126 {
127 if (stream != NULL)
128 {
129 fclose (stream);
130 stream = NULL;
131 }
132 }
133
134
135 /* Thread-safe, exported version of that. */
136 enum nss_status
137 _nss_files_endaliasent (void)
138 {
139 __libc_lock_lock (lock);
140
141 internal_endent ();
142
143 __libc_lock_unlock (lock);
144
145 return NSS_STATUS_SUCCESS;
146 }
147 \f
148 /* Parsing the database file into `struct aliasent' data structures. */
149 static enum nss_status
150 get_next_alias (const char *match, struct aliasent *result,
151 char *buffer, size_t buflen, int *errnop)
152 {
153 enum nss_status status = NSS_STATUS_NOTFOUND;
154 int ignore = 0;
155
156 result->alias_members_len = 0;
157
158 while (1)
159 {
160 /* Now we are ready to process the input. We have to read a
161 line and all its continuations and construct the array of
162 string pointers. This pointers and the names itself have to
163 be placed in BUFFER. */
164 char *first_unused = buffer;
165 size_t room_left = buflen - (buflen % __alignof__ (char *));
166 char *line;
167
168 /* Check whether the buffer is large enough for even trying to
169 read something. */
170 if (room_left < 2)
171 goto no_more_room;
172
173 /* Read the first line. It must contain the alias name and
174 possibly some alias names. */
175 first_unused[room_left - 1] = '\xff';
176 line = fgets_unlocked (first_unused, room_left, stream);
177 if (line == NULL)
178 /* Nothing to read. */
179 break;
180 else if (first_unused[room_left - 1] != '\xff')
181 {
182 /* The line is too long for our buffer. */
183 no_more_room:
184 *errnop = ERANGE;
185 status = NSS_STATUS_TRYAGAIN;
186 break;
187 }
188 else
189 {
190 char *cp;
191
192 /* If we are in IGNORE mode and the first character in the
193 line is a white space we ignore the line and start
194 reading the next. */
195 if (ignore && isspace (*first_unused))
196 continue;
197
198 /* Terminate the line for any case. */
199 cp = strpbrk (first_unused, "#\n");
200 if (cp != NULL)
201 *cp = '\0';
202
203 /* Skip leading blanks. */
204 while (isspace (*line))
205 ++line;
206
207 result->alias_name = first_unused;
208 while (*line != '\0' && *line != ':')
209 *first_unused++ = *line++;
210 if (*line == '\0' || result->alias_name == first_unused)
211 /* No valid name. Ignore the line. */
212 continue;
213
214 *first_unused++ = '\0';
215 if (room_left < (size_t) (first_unused - result->alias_name))
216 goto no_more_room;
217 room_left -= first_unused - result->alias_name;
218 ++line;
219
220 /* When we search for a specific alias we can avoid all the
221 difficult parts and compare now with the name we are
222 looking for. If it does not match we simply ignore all
223 lines until the next line containing the start of a new
224 alias is found. */
225 ignore = (match != NULL
226 && __strcasecmp (result->alias_name, match) != 0);
227
228 while (! ignore)
229 {
230 while (isspace (*line))
231 ++line;
232
233 cp = first_unused;
234 while (*line != '\0' && *line != ',')
235 *first_unused++ = *line++;
236
237 if (first_unused != cp)
238 {
239 /* OK, we can have a regular entry or an include
240 request. */
241 if (*line != '\0')
242 ++line;
243 *first_unused++ = '\0';
244
245 if (strncmp (cp, ":include:", 9) != 0)
246 {
247 if (room_left < (first_unused - cp) + sizeof (char *))
248 goto no_more_room;
249 room_left -= (first_unused - cp) + sizeof (char *);
250
251 ++result->alias_members_len;
252 }
253 else
254 {
255 /* Oh well, we have to read the addressed file. */
256 FILE *listfile;
257 char *old_line = NULL;
258
259 first_unused = cp;
260
261 listfile = fopen (&cp[9], "r");
262 /* If the file does not exist we simply ignore
263 the statement. */
264 if (listfile != NULL
265 && (old_line = strdup (line)) != NULL)
266 {
267 while (! feof_unlocked (listfile))
268 {
269 first_unused[room_left - 1] = '\xff';
270 line = fgets_unlocked (first_unused, room_left,
271 listfile);
272 if (line == NULL)
273 break;
274 if (first_unused[room_left - 1] != '\xff')
275 {
276 free (old_line);
277 goto no_more_room;
278 }
279
280 /* Parse the line. */
281 cp = strpbrk (line, "#\n");
282 if (cp != NULL)
283 *cp = '\0';
284
285 do
286 {
287 while (isspace (*line))
288 ++line;
289
290 cp = first_unused;
291 while (*line != '\0' && *line != ',')
292 *first_unused++ = *line++;
293
294 if (*line != '\0')
295 ++line;
296
297 if (first_unused != cp)
298 {
299 *first_unused++ = '\0';
300 if (room_left < ((first_unused - cp)
301 + __alignof__ (char *)))
302 {
303 free (old_line);
304 goto no_more_room;
305 }
306 room_left -= ((first_unused - cp)
307 + __alignof__ (char *));
308 ++result->alias_members_len;
309 }
310 }
311 while (*line != '\0');
312 }
313 fclose (listfile);
314
315 first_unused[room_left - 1] = '\0';
316 strncpy (first_unused, old_line, room_left);
317
318 free (old_line);
319 line = first_unused;
320
321 if (first_unused[room_left - 1] != '\0')
322 goto no_more_room;
323 }
324 }
325 }
326
327 if (*line == '\0')
328 {
329 /* Get the next line. But we must be careful. We
330 must not read the whole line at once since it
331 might belong to the current alias. Simply read
332 the first character. If it is a white space we
333 have a continuation line. Otherwise it is the
334 beginning of a new alias and we can push back the
335 just read character. */
336 int ch;
337
338 ch = fgetc_unlocked (stream);
339 if (ch == EOF || ch == '\n' || !isspace (ch))
340 {
341 size_t cnt;
342
343 /* Now prepare the return. Provide string
344 pointers for the currently selected aliases. */
345 if (ch != EOF)
346 ungetc (ch, stream);
347
348 /* Adjust the pointer so it is aligned for
349 storing pointers. */
350 first_unused += __alignof__ (char *) - 1;
351 first_unused -= ((first_unused - (char *) 0)
352 % __alignof__ (char *));
353 result->alias_members = (char **) first_unused;
354
355 /* Compute addresses of alias entry strings. */
356 cp = result->alias_name;
357 for (cnt = 0; cnt < result->alias_members_len; ++cnt)
358 {
359 cp = strchr (cp, '\0') + 1;
360 result->alias_members[cnt] = cp;
361 }
362
363 status = (result->alias_members_len == 0
364 ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS);
365 break;
366 }
367
368 /* The just read character is a white space and so
369 can be ignored. */
370 first_unused[room_left - 1] = '\xff';
371 line = fgets_unlocked (first_unused, room_left, stream);
372 if (first_unused[room_left - 1] != '\xff')
373 goto no_more_room;
374 cp = strpbrk (line, "#\n");
375 if (cp != NULL)
376 *cp = '\0';
377 }
378 }
379 }
380
381 if (status != NSS_STATUS_NOTFOUND)
382 /* We read something. In any case break here. */
383 break;
384 }
385
386 return status;
387 }
388
389
390 enum nss_status
391 _nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen,
392 int *errnop)
393 {
394 /* Return next entry in host file. */
395 enum nss_status status = NSS_STATUS_SUCCESS;
396
397 __libc_lock_lock (lock);
398
399 /* Be prepared that the set*ent function was not called before. */
400 if (stream == NULL)
401 status = internal_setent ();
402
403 if (status == NSS_STATUS_SUCCESS)
404 {
405 /* If the last use was not by the getent function we need the
406 position the stream. */
407 if (last_use != getent)
408 {
409 if (fsetpos (stream, &position) < 0)
410 status = NSS_STATUS_UNAVAIL;
411 else
412 last_use = getent;
413 }
414
415 if (status == NSS_STATUS_SUCCESS)
416 {
417 result->alias_local = 1;
418
419 /* Read lines until we get a definite result. */
420 do
421 status = get_next_alias (NULL, result, buffer, buflen, errnop);
422 while (status == NSS_STATUS_RETURN);
423
424 /* If we successfully read an entry remember this position. */
425 if (status == NSS_STATUS_SUCCESS)
426 fgetpos (stream, &position);
427 else
428 last_use = nouse;
429 }
430 }
431
432 __libc_lock_unlock (lock);
433
434 return status;
435 }
436
437
438 enum nss_status
439 _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
440 char *buffer, size_t buflen, int *errnop)
441 {
442 /* Return next entry in host file. */
443 enum nss_status status = NSS_STATUS_SUCCESS;
444
445 if (name == NULL)
446 {
447 __set_errno (EINVAL);
448 return NSS_STATUS_UNAVAIL;
449 }
450
451 __libc_lock_lock (lock);
452
453 /* Open the stream or rest it. */
454 status = internal_setent ();
455 last_use = getby;
456
457 if (status == NSS_STATUS_SUCCESS)
458 {
459 result->alias_local = 1;
460
461 /* Read lines until we get a definite result. */
462 do
463 status = get_next_alias (name, result, buffer, buflen, errnop);
464 while (status == NSS_STATUS_RETURN);
465 }
466
467 internal_endent ();
468
469 __libc_lock_unlock (lock);
470
471 return status;
472 }