]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nss_files/files-netgrp.c
1 /* Netgroup file parser in nss_files modules.
2 Copyright (C) 1996 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include <libc-lock.h>
31 #define DATAFILE "/etc/netgroup"
34 /* Locks the static variables in this file. */
35 __libc_lock_define_initialized (static, lock
)
37 /* We share a single place where we store the data for the current
38 netgroup. This buffer is allocated by `setnetgrent' and freed on
39 the next call of this function or when calling `endnetgrent'. */
41 static size_t data_size
;
46 #define EXPAND(needed) \
49 size_t old_cursor = cursor - data; \
51 data_size += 512 > 2 * needed ? 512 : 2 * needed; \
52 data = realloc (data, data_size); \
56 status = NSS_STATUS_UNAVAIL; \
60 cursor = data + old_cursor; \
66 _nss_files_setnetgrent (const char *group
)
69 enum nss_status status
;
72 return NSS_STATUS_UNAVAIL
;
74 __libc_lock_lock (lock
);
76 /* Find the netgroups file and open it. */
77 fp
= fopen (DATAFILE
, "r");
79 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
82 /* Read the file line by line and try to find the description
83 GROUP. We must take care for long lines. */
86 const ssize_t group_len
= strlen (group
);
88 status
= NSS_STATUS_NOTFOUND
;
93 ssize_t curlen
= getline (&line
, &line_len
, fp
);
98 status
= NSS_STATUS_NOTFOUND
;
102 found
= (curlen
> group_len
&& strncmp (line
, group
, group_len
) == 0
103 && isspace (line
[group_len
]));
105 /* Read the whole line (including continuation) and store it
106 if FOUND in nonzero. Otherwise we don't need it. */
109 /* Store the data from the first line. */
110 EXPAND (curlen
- group_len
);
111 memcpy (cursor
, &line
[group_len
+ 1], curlen
- group_len
);
112 cursor
+= (curlen
- group_len
) - 1;
115 while (line
[curlen
- 1] == '\n' && line
[curlen
- 2] == '\\')
117 /* Yes, we have a continuation line. */
119 /* Remove these characters from the stored line. */
122 /* Get netxt line. */
123 curlen
= getline (&line
, &line_len
, fp
);
129 /* Make sure we have enough room. */
130 EXPAND (1 + curlen
+ 1);
132 /* Add separator in case next line starts immediately. */
136 memcpy (cursor
, line
, curlen
+ 1);
143 /* Now we have read the line. */
144 status
= NSS_STATUS_SUCCESS
;
152 /* We don't need the file and the line buffer anymore. */
157 __libc_lock_unlock (lock
);
164 _nss_files_endnetgrent (void)
166 __libc_lock_lock (lock
);
168 /* Free allocated memory for data if some is present. */
177 __libc_lock_unlock (lock
);
179 return NSS_STATUS_SUCCESS
;
184 _nss_netgroup_parseline (char **cursor
, struct __netgrent
*result
,
185 char *buffer
, int buflen
)
187 enum nss_status status
;
188 const char *host
, *user
, *domain
;
191 /* First skip leading spaces. */
192 while (isspace (*cp
))
197 /* We have a list of other netgroups. */
200 while (*cp
!= '\0' && ! isspace (*cp
))
205 /* It is another netgroup name. */
206 int last
= *cp
== '\0';
208 result
->type
= group_val
;
209 result
->val
.group
= name
;
216 return NSS_STATUS_SUCCESS
;
219 return first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
222 /* Match host name. */
226 return first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
228 /* Match user name. */
232 return first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
234 /* Match domain name. */
238 return first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
242 /* When we got here we have found an entry. Before we can copy it
243 to the private buffer we have to make sure it is big enough. */
244 if (cp
- host
> buflen
)
246 __set_errno (ERANGE
);
247 status
= NSS_STATUS_UNAVAIL
;
251 memcpy (buffer
, host
, cp
- host
);
252 result
->type
= triple_val
;
254 buffer
[(user
- host
) - 1] = '\0';
255 result
->val
.triple
.host
= *host
== ',' ? NULL
: buffer
;
257 buffer
[(domain
- host
) - 1] = '\0';
258 result
->val
.triple
.user
= *user
== ',' ? NULL
: buffer
+ (user
- host
);
260 buffer
[(cp
- host
) - 1] = '\0';
261 result
->val
.triple
.domain
=
262 *domain
== ')' ? NULL
: buffer
+ (domain
- host
);
264 status
= NSS_STATUS_SUCCESS
;
266 /* Rememember where we stopped reading. */
277 _nss_files_getnetgrent_r (struct __netgrent
*result
, char *buffer
, int buflen
)
279 enum nss_status status
;
281 __libc_lock_lock (lock
);
283 status
= _nss_netgroup_parseline (&cursor
, result
, buffer
, buflen
);
285 __libc_lock_unlock (lock
);