2.9.90-4
[glibc/history.git] / nss / nss_files / files-alias.c
blob57cc982f77ae7330f86700c6e96f295ab4ac0552
1 /* Mail alias file parser in nss_files module.
2 Copyright (C) 1996,97,98,99,2002,2006,2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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
19 02111-1307 USA. */
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>
30 #include <kernel-features.h>
32 #include "nsswitch.h"
34 /* Locks the static variables in this file. */
35 __libc_lock_define_initialized (static, lock)
37 /* Maintenance of the shared stream open on the database file. */
39 static FILE *stream;
40 static fpos_t position;
41 static enum { nouse, getent, getby } last_use;
44 static enum nss_status
45 internal_setent (void)
47 enum nss_status status = NSS_STATUS_SUCCESS;
49 if (stream == NULL)
51 stream = fopen ("/etc/aliases", "re");
53 if (stream == NULL)
54 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
55 else
57 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
58 # ifdef O_CLOEXEC
59 if (__have_o_cloexec <= 0)
60 # endif
62 /* We have to make sure the file is `closed on exec'. */
63 int result;
64 int flags;
66 result = flags = fcntl (fileno (stream), F_GETFD, 0);
67 if (result >= 0)
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
75 flags |= FD_CLOEXEC;
76 result = fcntl (fileno (stream), F_SETFD, flags);
79 if (result < 0)
81 /* Something went wrong. Close the stream and return a
82 failure. */
83 fclose (stream);
84 stream = NULL;
85 status = NSS_STATUS_UNAVAIL;
88 #endif
91 else
92 rewind (stream);
94 return status;
98 /* Thread-safe, exported version of that. */
99 enum nss_status
100 _nss_files_setaliasent (void)
102 enum nss_status status;
104 __libc_lock_lock (lock);
106 status = internal_setent ();
108 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
110 fclose (stream);
111 stream = NULL;
112 status = NSS_STATUS_UNAVAIL;
115 last_use = getent;
117 __libc_lock_unlock (lock);
119 return status;
123 /* Close the database file. */
124 static void
125 internal_endent (void)
127 if (stream != NULL)
129 fclose (stream);
130 stream = NULL;
135 /* Thread-safe, exported version of that. */
136 enum nss_status
137 _nss_files_endaliasent (void)
139 __libc_lock_lock (lock);
141 internal_endent ();
143 __libc_lock_unlock (lock);
145 return NSS_STATUS_SUCCESS;
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)
153 enum nss_status status = NSS_STATUS_NOTFOUND;
154 int ignore = 0;
156 result->alias_members_len = 0;
158 while (1)
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;
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;
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')
182 /* The line is too long for our buffer. */
183 no_more_room:
184 *errnop = ERANGE;
185 status = NSS_STATUS_TRYAGAIN;
186 break;
188 else
190 char *cp;
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;
198 /* Terminate the line for any case. */
199 cp = strpbrk (first_unused, "#\n");
200 if (cp != NULL)
201 *cp = '\0';
203 /* Skip leading blanks. */
204 while (isspace (*line))
205 ++line;
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;
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;
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);
228 while (! ignore)
230 while (isspace (*line))
231 ++line;
233 cp = first_unused;
234 while (*line != '\0' && *line != ',')
235 *first_unused++ = *line++;
237 if (first_unused != cp)
239 /* OK, we can have a regular entry or an include
240 request. */
241 if (*line != '\0')
242 ++line;
243 *first_unused++ = '\0';
245 if (strncmp (cp, ":include:", 9) != 0)
247 if (room_left < (first_unused - cp) + sizeof (char *))
248 goto no_more_room;
249 room_left -= (first_unused - cp) + sizeof (char *);
251 ++result->alias_members_len;
253 else
255 /* Oh well, we have to read the addressed file. */
256 FILE *listfile;
257 char *old_line = NULL;
259 first_unused = cp;
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)
267 while (! feof (listfile))
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')
276 free (old_line);
277 goto no_more_room;
280 /* Parse the line. */
281 cp = strpbrk (line, "#\n");
282 if (cp != NULL)
283 *cp = '\0';
287 while (isspace (*line))
288 ++line;
290 cp = first_unused;
291 while (*line != '\0' && *line != ',')
292 *first_unused++ = *line++;
294 if (*line != '\0')
295 ++line;
297 if (first_unused != cp)
299 *first_unused++ = '\0';
300 if (room_left < ((first_unused - cp)
301 + __alignof__ (char *)))
303 free (old_line);
304 goto no_more_room;
306 room_left -= ((first_unused - cp)
307 + __alignof__ (char *));
308 ++result->alias_members_len;
311 while (*line != '\0');
313 fclose (listfile);
315 first_unused[room_left - 1] = '\0';
316 strncpy (first_unused, old_line, room_left);
318 free (old_line);
319 line = first_unused;
321 if (first_unused[room_left - 1] != '\0')
322 goto no_more_room;
327 if (*line == '\0')
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;
338 ch = fgetc (stream);
339 if (ch == EOF || ch == '\n' || !isspace (ch))
341 size_t cnt;
343 /* Now prepare the return. Provide string
344 pointers for the currently selected aliases. */
345 if (ch != EOF)
346 ungetc (ch, stream);
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;
355 /* Compute addresses of alias entry strings. */
356 cp = result->alias_name;
357 for (cnt = 0; cnt < result->alias_members_len; ++cnt)
359 cp = strchr (cp, '\0') + 1;
360 result->alias_members[cnt] = cp;
363 status = (result->alias_members_len == 0
364 ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS);
365 break;
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';
381 if (status != NSS_STATUS_NOTFOUND)
382 /* We read something. In any case break here. */
383 break;
386 return status;
390 enum nss_status
391 _nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen,
392 int *errnop)
394 /* Return next entry in host file. */
395 enum nss_status status = NSS_STATUS_SUCCESS;
397 __libc_lock_lock (lock);
399 /* Be prepared that the set*ent function was not called before. */
400 if (stream == NULL)
401 status = internal_setent ();
403 if (status == NSS_STATUS_SUCCESS)
405 /* If the last use was not by the getent function we need the
406 position the stream. */
407 if (last_use != getent)
409 if (fsetpos (stream, &position) < 0)
410 status = NSS_STATUS_UNAVAIL;
411 else
412 last_use = getent;
415 if (status == NSS_STATUS_SUCCESS)
417 result->alias_local = 1;
419 /* Read lines until we get a definite result. */
421 status = get_next_alias (NULL, result, buffer, buflen, errnop);
422 while (status == NSS_STATUS_RETURN);
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;
432 __libc_lock_unlock (lock);
434 return status;
438 enum nss_status
439 _nss_files_getaliasbyname_r (const char *name, struct aliasent *result,
440 char *buffer, size_t buflen, int *errnop)
442 /* Return next entry in host file. */
443 enum nss_status status = NSS_STATUS_SUCCESS;
445 if (name == NULL)
447 __set_errno (EINVAL);
448 return NSS_STATUS_UNAVAIL;
451 __libc_lock_lock (lock);
453 /* Open the stream or rest it. */
454 status = internal_setent ();
455 last_use = getby;
457 if (status == NSS_STATUS_SUCCESS)
459 result->alias_local = 1;
461 /* Read lines until we get a definite result. */
463 status = get_next_alias (name, result, buffer, buflen, errnop);
464 while (status == NSS_STATUS_RETURN);
467 internal_endent ();
469 __libc_lock_unlock (lock);
471 return status;