1 /* Netgroup file parser in nss_files modules.
2 Copyright (C) 1996, 1997, 2000, 2004, 2005, 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, see
18 <http://www.gnu.org/licenses/>. */
24 #include <stdio_ext.h>
30 #define DATAFILE "/etc/netgroup"
32 libnss_files_hidden_proto (_nss_files_endnetgrent
)
34 #define EXPAND(needed) \
37 size_t old_cursor = result->cursor - result->data; \
38 void *old_data = result->data; \
40 result->data_size += 512 > 2 * needed ? 512 : 2 * needed; \
41 result->data = realloc (result->data, result->data_size); \
43 if (result->data == NULL) \
46 status = NSS_STATUS_UNAVAIL; \
50 result->cursor = result->data + old_cursor; \
56 _nss_files_setnetgrent (const char *group
, struct __netgrent
*result
)
59 enum nss_status status
;
62 return NSS_STATUS_UNAVAIL
;
64 /* Find the netgroups file and open it. */
65 fp
= fopen (DATAFILE
, "rce");
67 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
70 /* Read the file line by line and try to find the description
71 GROUP. We must take care for long lines. */
74 const ssize_t group_len
= strlen (group
);
76 status
= NSS_STATUS_NOTFOUND
;
77 result
->cursor
= result
->data
;
79 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
81 while (!feof_unlocked (fp
))
83 ssize_t curlen
= getline (&line
, &line_len
, fp
);
88 status
= NSS_STATUS_NOTFOUND
;
92 found
= (curlen
> group_len
&& strncmp (line
, group
, group_len
) == 0
93 && isspace (line
[group_len
]));
95 /* Read the whole line (including continuation) and store it
96 if FOUND in nonzero. Otherwise we don't need it. */
99 /* Store the data from the first line. */
100 EXPAND (curlen
- group_len
);
101 memcpy (result
->cursor
, &line
[group_len
+ 1],
103 result
->cursor
+= (curlen
- group_len
) - 1;
106 while (line
[curlen
- 1] == '\n' && line
[curlen
- 2] == '\\')
108 /* Yes, we have a continuation line. */
110 /* Remove these characters from the stored line. */
114 curlen
= getline (&line
, &line_len
, fp
);
120 /* Make sure we have enough room. */
121 EXPAND (1 + curlen
+ 1);
123 /* Add separator in case next line starts immediately. */
124 *result
->cursor
++ = ' ';
127 memcpy (result
->cursor
, line
, curlen
+ 1);
128 result
->cursor
+= curlen
;
134 /* Now we have read the line. */
135 status
= NSS_STATUS_SUCCESS
;
136 result
->cursor
= result
->data
;
143 /* We don't need the file and the line buffer anymore. */
147 if (status
!= NSS_STATUS_SUCCESS
)
148 _nss_files_endnetgrent (result
);
156 _nss_files_endnetgrent (struct __netgrent
*result
)
158 /* Free allocated memory for data if some is present. */
161 result
->data_size
= 0;
162 result
->cursor
= NULL
;
163 return NSS_STATUS_SUCCESS
;
165 libnss_files_hidden_def (_nss_files_endnetgrent
)
168 strip_whitespace (char *str
)
172 /* Skip leading spaces. */
173 while (isspace (*cp
))
177 while (*cp
!= '\0' && ! isspace(*cp
))
180 /* Null-terminate, stripping off any trailing spaces. */
183 return *str
== '\0' ? NULL
: str
;
187 _nss_netgroup_parseline (char **cursor
, struct __netgrent
*result
,
188 char *buffer
, size_t buflen
, int *errnop
)
190 enum nss_status status
;
191 const char *host
, *user
, *domain
;
194 /* Some sanity checks. */
196 return NSS_STATUS_NOTFOUND
;
198 /* First skip leading spaces. */
199 while (isspace (*cp
))
204 /* We have a list of other netgroups. */
207 while (*cp
!= '\0' && ! isspace (*cp
))
212 /* It is another netgroup name. */
213 int last
= *cp
== '\0';
215 result
->type
= group_val
;
216 result
->val
.group
= name
;
223 return NSS_STATUS_SUCCESS
;
226 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
229 /* Match host name. */
233 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
235 /* Match user name. */
239 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
241 /* Match domain name. */
245 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
249 /* When we got here we have found an entry. Before we can copy it
250 to the private buffer we have to make sure it is big enough. */
251 if (cp
- host
> buflen
)
254 status
= NSS_STATUS_UNAVAIL
;
258 memcpy (buffer
, host
, cp
- host
);
259 result
->type
= triple_val
;
261 buffer
[(user
- host
) - 1] = '\0'; /* Replace ',' with '\0'. */
262 result
->val
.triple
.host
= strip_whitespace (buffer
);
264 buffer
[(domain
- host
) - 1] = '\0'; /* Replace ',' with '\0'. */
265 result
->val
.triple
.user
= strip_whitespace (buffer
+ (user
- host
));
267 buffer
[(cp
- host
) - 1] = '\0'; /* Replace ')' with '\0'. */
268 result
->val
.triple
.domain
= strip_whitespace (buffer
+ (domain
- host
));
270 status
= NSS_STATUS_SUCCESS
;
272 /* Remember where we stopped reading. */
280 libnss_files_hidden_def (_nss_netgroup_parseline
)
284 _nss_files_getnetgrent_r (struct __netgrent
*result
, char *buffer
,
285 size_t buflen
, int *errnop
)
287 enum nss_status status
;
289 status
= _nss_netgroup_parseline (&result
->cursor
, result
, buffer
, buflen
,