1 /* Mail alias file parser in nss_files module.
2 Copyright (C) 1996-2015 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, see
18 <http://www.gnu.org/licenses/>. */
24 #include <bits/libc-lock.h>
29 #include <kernel-features.h>
33 /* Locks the static variables in this file. */
34 __libc_lock_define_initialized (static, lock
)
36 /* Maintenance of the shared stream open on the database file. */
39 static fpos_t position
;
40 static enum { nouse
, getent
, getby
} last_use
;
43 static enum nss_status
44 internal_setent (void)
46 enum nss_status status
= NSS_STATUS_SUCCESS
;
50 stream
= fopen ("/etc/aliases", "rce");
53 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
56 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
58 if (__have_o_cloexec
<= 0)
61 /* We have to make sure the file is `closed on exec'. */
65 result
= flags
= fcntl (fileno (stream
), F_GETFD
, 0);
69 if (__have_o_cloexec
== 0)
70 __have_o_cloexec
= (flags
& FD_CLOEXEC
) == 0 ? -1 : 1;
71 if (__have_o_cloexec
< 0)
75 result
= fcntl (fileno (stream
), F_SETFD
, flags
);
80 /* Something went wrong. Close the stream and return a
84 status
= NSS_STATUS_UNAVAIL
;
97 /* Thread-safe, exported version of that. */
99 _nss_files_setaliasent (void)
101 enum nss_status status
;
103 __libc_lock_lock (lock
);
105 status
= internal_setent ();
107 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
111 status
= NSS_STATUS_UNAVAIL
;
116 __libc_lock_unlock (lock
);
122 /* Close the database file. */
124 internal_endent (void)
134 /* Thread-safe, exported version of that. */
136 _nss_files_endaliasent (void)
138 __libc_lock_lock (lock
);
142 __libc_lock_unlock (lock
);
144 return NSS_STATUS_SUCCESS
;
147 /* Parsing the database file into `struct aliasent' data structures. */
148 static enum nss_status
149 get_next_alias (const char *match
, struct aliasent
*result
,
150 char *buffer
, size_t buflen
, int *errnop
)
152 enum nss_status status
= NSS_STATUS_NOTFOUND
;
155 result
->alias_members_len
= 0;
159 /* Now we are ready to process the input. We have to read a
160 line and all its continuations and construct the array of
161 string pointers. This pointers and the names itself have to
162 be placed in BUFFER. */
163 char *first_unused
= buffer
;
164 size_t room_left
= buflen
- (buflen
% __alignof__ (char *));
167 /* Check whether the buffer is large enough for even trying to
172 /* Read the first line. It must contain the alias name and
173 possibly some alias names. */
174 first_unused
[room_left
- 1] = '\xff';
175 line
= fgets_unlocked (first_unused
, room_left
, stream
);
177 /* Nothing to read. */
179 else if (first_unused
[room_left
- 1] != '\xff')
181 /* The line is too long for our buffer. */
184 status
= NSS_STATUS_TRYAGAIN
;
191 /* If we are in IGNORE mode and the first character in the
192 line is a white space we ignore the line and start
194 if (ignore
&& isspace (*first_unused
))
197 /* Terminate the line for any case. */
198 cp
= strpbrk (first_unused
, "#\n");
202 /* Skip leading blanks. */
203 while (isspace (*line
))
206 result
->alias_name
= first_unused
;
207 while (*line
!= '\0' && *line
!= ':')
208 *first_unused
++ = *line
++;
209 if (*line
== '\0' || result
->alias_name
== first_unused
)
210 /* No valid name. Ignore the line. */
213 *first_unused
++ = '\0';
214 if (room_left
< (size_t) (first_unused
- result
->alias_name
))
216 room_left
-= first_unused
- result
->alias_name
;
219 /* When we search for a specific alias we can avoid all the
220 difficult parts and compare now with the name we are
221 looking for. If it does not match we simply ignore all
222 lines until the next line containing the start of a new
224 ignore
= (match
!= NULL
225 && __strcasecmp (result
->alias_name
, match
) != 0);
229 while (isspace (*line
))
233 while (*line
!= '\0' && *line
!= ',')
234 *first_unused
++ = *line
++;
236 if (first_unused
!= cp
)
238 /* OK, we can have a regular entry or an include
242 *first_unused
++ = '\0';
244 if (strncmp (cp
, ":include:", 9) != 0)
246 if (room_left
< (first_unused
- cp
) + sizeof (char *))
248 room_left
-= (first_unused
- cp
) + sizeof (char *);
250 ++result
->alias_members_len
;
254 /* Oh well, we have to read the addressed file. */
256 char *old_line
= NULL
;
260 listfile
= fopen (&cp
[9], "rce");
261 /* If the file does not exist we simply ignore
264 && (old_line
= strdup (line
)) != NULL
)
266 while (! feof_unlocked (listfile
))
268 first_unused
[room_left
- 1] = '\xff';
269 line
= fgets_unlocked (first_unused
, room_left
,
273 if (first_unused
[room_left
- 1] != '\xff')
279 /* Parse the line. */
280 cp
= strpbrk (line
, "#\n");
286 while (isspace (*line
))
290 while (*line
!= '\0' && *line
!= ',')
291 *first_unused
++ = *line
++;
296 if (first_unused
!= cp
)
298 *first_unused
++ = '\0';
299 if (room_left
< ((first_unused
- cp
)
300 + __alignof__ (char *)))
305 room_left
-= ((first_unused
- cp
)
306 + __alignof__ (char *));
307 ++result
->alias_members_len
;
310 while (*line
!= '\0');
314 first_unused
[room_left
- 1] = '\0';
315 strncpy (first_unused
, old_line
, room_left
);
320 if (first_unused
[room_left
- 1] != '\0')
328 /* Get the next line. But we must be careful. We
329 must not read the whole line at once since it
330 might belong to the current alias. Simply read
331 the first character. If it is a white space we
332 have a continuation line. Otherwise it is the
333 beginning of a new alias and we can push back the
334 just read character. */
337 ch
= fgetc_unlocked (stream
);
338 if (ch
== EOF
|| ch
== '\n' || !isspace (ch
))
342 /* Now prepare the return. Provide string
343 pointers for the currently selected aliases. */
347 /* Adjust the pointer so it is aligned for
349 first_unused
+= __alignof__ (char *) - 1;
350 first_unused
-= ((first_unused
- (char *) 0)
351 % __alignof__ (char *));
352 result
->alias_members
= (char **) first_unused
;
354 /* Compute addresses of alias entry strings. */
355 cp
= result
->alias_name
;
356 for (cnt
= 0; cnt
< result
->alias_members_len
; ++cnt
)
358 cp
= strchr (cp
, '\0') + 1;
359 result
->alias_members
[cnt
] = cp
;
362 status
= (result
->alias_members_len
== 0
363 ? NSS_STATUS_RETURN
: NSS_STATUS_SUCCESS
);
367 /* The just read character is a white space and so
369 first_unused
[room_left
- 1] = '\xff';
370 line
= fgets_unlocked (first_unused
, room_left
, stream
);
371 if (first_unused
[room_left
- 1] != '\xff')
373 cp
= strpbrk (line
, "#\n");
380 if (status
!= NSS_STATUS_NOTFOUND
)
381 /* We read something. In any case break here. */
390 _nss_files_getaliasent_r (struct aliasent
*result
, char *buffer
, size_t buflen
,
393 /* Return next entry in host file. */
394 enum nss_status status
= NSS_STATUS_SUCCESS
;
396 __libc_lock_lock (lock
);
398 /* Be prepared that the set*ent function was not called before. */
400 status
= internal_setent ();
402 if (status
== NSS_STATUS_SUCCESS
)
404 /* If the last use was not by the getent function we need the
405 position the stream. */
406 if (last_use
!= getent
)
408 if (fsetpos (stream
, &position
) < 0)
409 status
= NSS_STATUS_UNAVAIL
;
414 if (status
== NSS_STATUS_SUCCESS
)
416 result
->alias_local
= 1;
418 /* Read lines until we get a definite result. */
420 status
= get_next_alias (NULL
, result
, buffer
, buflen
, errnop
);
421 while (status
== NSS_STATUS_RETURN
);
423 /* If we successfully read an entry remember this position. */
424 if (status
== NSS_STATUS_SUCCESS
)
425 fgetpos (stream
, &position
);
431 __libc_lock_unlock (lock
);
438 _nss_files_getaliasbyname_r (const char *name
, struct aliasent
*result
,
439 char *buffer
, size_t buflen
, int *errnop
)
441 /* Return next entry in host file. */
442 enum nss_status status
= NSS_STATUS_SUCCESS
;
446 __set_errno (EINVAL
);
447 return NSS_STATUS_UNAVAIL
;
450 __libc_lock_lock (lock
);
452 /* Open the stream or rest it. */
453 status
= internal_setent ();
456 if (status
== NSS_STATUS_SUCCESS
)
458 result
->alias_local
= 1;
460 /* Read lines until we get a definite result. */
462 status
= get_next_alias (name
, result
, buffer
, buflen
, errnop
);
463 while (status
== NSS_STATUS_RETURN
);
468 __libc_lock_unlock (lock
);