1 /* Netgroup file parser in nss_files modules.
2 Copyright (C) 1996, 1997, 2000, 2004, 2005 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 <stdio_ext.h>
31 #define DATAFILE "/etc/netgroup"
33 libnss_files_hidden_proto (_nss_files_endnetgrent
)
35 #define EXPAND(needed) \
38 size_t old_cursor = result->cursor - result->data; \
39 void *old_data = result->data; \
41 result->data_size += 512 > 2 * needed ? 512 : 2 * needed; \
42 result->data = realloc (result->data, result->data_size); \
44 if (result->data == NULL) \
47 status = NSS_STATUS_UNAVAIL; \
51 result->cursor = result->data + old_cursor; \
57 _nss_files_setnetgrent (const char *group
, struct __netgrent
*result
)
60 enum nss_status status
;
63 return NSS_STATUS_UNAVAIL
;
65 /* Find the netgroups file and open it. */
66 fp
= fopen (DATAFILE
, "r");
68 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
71 /* Read the file line by line and try to find the description
72 GROUP. We must take care for long lines. */
75 const ssize_t group_len
= strlen (group
);
77 status
= NSS_STATUS_NOTFOUND
;
78 result
->cursor
= result
->data
;
80 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
82 while (!feof_unlocked (fp
))
84 ssize_t curlen
= getline (&line
, &line_len
, fp
);
89 status
= NSS_STATUS_NOTFOUND
;
93 found
= (curlen
> group_len
&& strncmp (line
, group
, group_len
) == 0
94 && isspace (line
[group_len
]));
96 /* Read the whole line (including continuation) and store it
97 if FOUND in nonzero. Otherwise we don't need it. */
100 /* Store the data from the first line. */
101 EXPAND (curlen
- group_len
);
102 memcpy (result
->cursor
, &line
[group_len
+ 1],
104 result
->cursor
+= (curlen
- group_len
) - 1;
107 while (line
[curlen
- 1] == '\n' && line
[curlen
- 2] == '\\')
109 /* Yes, we have a continuation line. */
111 /* Remove these characters from the stored line. */
115 curlen
= getline (&line
, &line_len
, fp
);
121 /* Make sure we have enough room. */
122 EXPAND (1 + curlen
+ 1);
124 /* Add separator in case next line starts immediately. */
125 *result
->cursor
++ = ' ';
128 memcpy (result
->cursor
, line
, curlen
+ 1);
129 result
->cursor
+= curlen
;
135 /* Now we have read the line. */
136 status
= NSS_STATUS_SUCCESS
;
137 result
->cursor
= result
->data
;
144 /* We don't need the file and the line buffer anymore. */
148 if (status
!= NSS_STATUS_SUCCESS
)
149 _nss_files_endnetgrent (result
);
157 _nss_files_endnetgrent (struct __netgrent
*result
)
159 /* Free allocated memory for data if some is present. */
162 result
->data_size
= 0;
163 result
->cursor
= NULL
;
164 return NSS_STATUS_SUCCESS
;
166 libnss_files_hidden_def (_nss_files_endnetgrent
)
169 strip_whitespace (char *str
)
173 /* Skip leading spaces. */
174 while (isspace (*cp
))
178 while (*cp
!= '\0' && ! isspace(*cp
))
181 /* Null-terminate, stripping off any trailing spaces. */
184 return *str
== '\0' ? NULL
: str
;
188 _nss_netgroup_parseline (char **cursor
, struct __netgrent
*result
,
189 char *buffer
, size_t buflen
, int *errnop
)
191 enum nss_status status
;
192 const char *host
, *user
, *domain
;
195 /* Some sanity checks. */
197 return NSS_STATUS_NOTFOUND
;
199 /* First skip leading spaces. */
200 while (isspace (*cp
))
205 /* We have a list of other netgroups. */
208 while (*cp
!= '\0' && ! isspace (*cp
))
213 /* It is another netgroup name. */
214 int last
= *cp
== '\0';
216 result
->type
= group_val
;
217 result
->val
.group
= name
;
224 return NSS_STATUS_SUCCESS
;
227 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
230 /* Match host name. */
234 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
236 /* Match user name. */
240 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
242 /* Match domain name. */
246 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
250 /* When we got here we have found an entry. Before we can copy it
251 to the private buffer we have to make sure it is big enough. */
252 if (cp
- host
> buflen
)
255 status
= NSS_STATUS_UNAVAIL
;
259 memcpy (buffer
, host
, cp
- host
);
260 result
->type
= triple_val
;
262 buffer
[(user
- host
) - 1] = '\0'; /* Replace ',' with '\0'. */
263 result
->val
.triple
.host
= strip_whitespace (buffer
);
265 buffer
[(domain
- host
) - 1] = '\0'; /* Replace ',' with '\0'. */
266 result
->val
.triple
.user
= strip_whitespace (buffer
+ (user
- host
));
268 buffer
[(cp
- host
) - 1] = '\0'; /* Replace ')' with '\0'. */
269 result
->val
.triple
.domain
= strip_whitespace (buffer
+ (domain
- host
));
271 status
= NSS_STATUS_SUCCESS
;
273 /* Remember where we stopped reading. */
281 libnss_files_hidden_def (_nss_netgroup_parseline
)
285 _nss_files_getnetgrent_r (struct __netgrent
*result
, char *buffer
,
286 size_t buflen
, int *errnop
)
288 enum nss_status status
;
290 status
= _nss_netgroup_parseline (&result
->cursor
, result
, buffer
, buflen
,