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.
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>
30 #include <kernel-features.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. */
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
;
51 stream
= fopen ("/etc/aliases", "rce");
54 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
57 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
59 if (__have_o_cloexec
<= 0)
62 /* We have to make sure the file is `closed on exec'. */
66 result
= flags
= fcntl (fileno (stream
), F_GETFD
, 0);
70 if (__have_o_cloexec
== 0)
71 __have_o_cloexec
= (flags
& FD_CLOEXEC
) == 0 ? -1 : 1;
72 if (__have_o_cloexec
< 0)
76 result
= fcntl (fileno (stream
), F_SETFD
, flags
);
81 /* Something went wrong. Close the stream and return a
85 status
= NSS_STATUS_UNAVAIL
;
98 /* Thread-safe, exported version of that. */
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)
112 status
= NSS_STATUS_UNAVAIL
;
117 __libc_lock_unlock (lock
);
123 /* Close the database file. */
125 internal_endent (void)
135 /* Thread-safe, exported version of that. */
137 _nss_files_endaliasent (void)
139 __libc_lock_lock (lock
);
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
;
156 result
->alias_members_len
= 0;
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 *));
168 /* Check whether the buffer is large enough for even trying to
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
);
178 /* Nothing to read. */
180 else if (first_unused
[room_left
- 1] != '\xff')
182 /* The line is too long for our buffer. */
185 status
= NSS_STATUS_TRYAGAIN
;
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
195 if (ignore
&& isspace (*first_unused
))
198 /* Terminate the line for any case. */
199 cp
= strpbrk (first_unused
, "#\n");
203 /* Skip leading blanks. */
204 while (isspace (*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. */
214 *first_unused
++ = '\0';
215 if (room_left
< (size_t) (first_unused
- result
->alias_name
))
217 room_left
-= first_unused
- result
->alias_name
;
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
225 ignore
= (match
!= NULL
226 && __strcasecmp (result
->alias_name
, match
) != 0);
230 while (isspace (*line
))
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
243 *first_unused
++ = '\0';
245 if (strncmp (cp
, ":include:", 9) != 0)
247 if (room_left
< (first_unused
- cp
) + sizeof (char *))
249 room_left
-= (first_unused
- cp
) + sizeof (char *);
251 ++result
->alias_members_len
;
255 /* Oh well, we have to read the addressed file. */
257 char *old_line
= NULL
;
261 listfile
= fopen (&cp
[9], "rce");
262 /* If the file does not exist we simply ignore
265 && (old_line
= strdup (line
)) != NULL
)
267 while (! feof_unlocked (listfile
))
269 first_unused
[room_left
- 1] = '\xff';
270 line
= fgets_unlocked (first_unused
, room_left
,
274 if (first_unused
[room_left
- 1] != '\xff')
280 /* Parse the line. */
281 cp
= strpbrk (line
, "#\n");
287 while (isspace (*line
))
291 while (*line
!= '\0' && *line
!= ',')
292 *first_unused
++ = *line
++;
297 if (first_unused
!= cp
)
299 *first_unused
++ = '\0';
300 if (room_left
< ((first_unused
- cp
)
301 + __alignof__ (char *)))
306 room_left
-= ((first_unused
- cp
)
307 + __alignof__ (char *));
308 ++result
->alias_members_len
;
311 while (*line
!= '\0');
315 first_unused
[room_left
- 1] = '\0';
316 strncpy (first_unused
, old_line
, room_left
);
321 if (first_unused
[room_left
- 1] != '\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. */
338 ch
= fgetc_unlocked (stream
);
339 if (ch
== EOF
|| ch
== '\n' || !isspace (ch
))
343 /* Now prepare the return. Provide string
344 pointers for the currently selected aliases. */
348 /* Adjust the pointer so it is aligned for
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
);
368 /* The just read character is a white space and so
370 first_unused
[room_left
- 1] = '\xff';
371 line
= fgets_unlocked (first_unused
, room_left
, stream
);
372 if (first_unused
[room_left
- 1] != '\xff')
374 cp
= strpbrk (line
, "#\n");
381 if (status
!= NSS_STATUS_NOTFOUND
)
382 /* We read something. In any case break here. */
391 _nss_files_getaliasent_r (struct aliasent
*result
, char *buffer
, size_t buflen
,
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. */
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
;
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
);
432 __libc_lock_unlock (lock
);
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
;
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 ();
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
);
469 __libc_lock_unlock (lock
);