1 /* Netgroup file parser in nss_files modules.
2 Copyright (C) 1996, 1997 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 not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
30 #define DATAFILE "/etc/netgroup"
33 #define EXPAND(needed) \
36 size_t old_cursor = result->cursor - result->data; \
38 result->data_size += 512 > 2 * needed ? 512 : 2 * needed; \
39 result->data = realloc (result->data, result->data_size); \
41 if (result->data == NULL) \
43 status = NSS_STATUS_UNAVAIL; \
47 result->cursor = result->data + old_cursor; \
53 _nss_files_setnetgrent (const char *group
, struct __netgrent
*result
)
56 enum nss_status status
;
59 return NSS_STATUS_UNAVAIL
;
61 /* Find the netgroups file and open it. */
62 fp
= fopen (DATAFILE
, "r");
64 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
67 /* Read the file line by line and try to find the description
68 GROUP. We must take care for long lines. */
71 const ssize_t group_len
= strlen (group
);
73 status
= NSS_STATUS_NOTFOUND
;
74 result
->cursor
= result
->data
;
78 ssize_t curlen
= getline (&line
, &line_len
, fp
);
83 status
= NSS_STATUS_NOTFOUND
;
87 found
= (curlen
> group_len
&& strncmp (line
, group
, group_len
) == 0
88 && isspace (line
[group_len
]));
90 /* Read the whole line (including continuation) and store it
91 if FOUND in nonzero. Otherwise we don't need it. */
94 /* Store the data from the first line. */
95 EXPAND (curlen
- group_len
);
96 memcpy (result
->cursor
, &line
[group_len
+ 1],
98 result
->cursor
+= (curlen
- group_len
) - 1;
101 while (line
[curlen
- 1] == '\n' && line
[curlen
- 2] == '\\')
103 /* Yes, we have a continuation line. */
105 /* Remove these characters from the stored line. */
109 curlen
= getline (&line
, &line_len
, fp
);
115 /* Make sure we have enough room. */
116 EXPAND (1 + curlen
+ 1);
118 /* Add separator in case next line starts immediately. */
119 *result
->cursor
++ = ' ';
122 memcpy (result
->cursor
, line
, curlen
+ 1);
123 result
->cursor
+= curlen
;
129 /* Now we have read the line. */
130 status
= NSS_STATUS_SUCCESS
;
131 result
->cursor
= result
->data
;
138 /* We don't need the file and the line buffer anymore. */
148 _nss_files_endnetgrent (struct __netgrent
*result
)
150 /* Free allocated memory for data if some is present. */
151 if (result
->data
!= NULL
)
155 result
->data_size
= 0;
156 result
->cursor
= NULL
;
159 return NSS_STATUS_SUCCESS
;
164 _nss_netgroup_parseline (char **cursor
, struct __netgrent
*result
,
165 char *buffer
, int buflen
)
167 enum nss_status status
;
168 const char *host
, *user
, *domain
;
171 /* Some sanity checks. */
173 return NSS_STATUS_NOTFOUND
;
175 /* First skip leading spaces. */
176 while (isspace (*cp
))
181 /* We have a list of other netgroups. */
184 while (*cp
!= '\0' && ! isspace (*cp
))
189 /* It is another netgroup name. */
190 int last
= *cp
== '\0';
192 result
->type
= group_val
;
193 result
->val
.group
= name
;
200 return NSS_STATUS_SUCCESS
;
203 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
206 /* Match host name. */
210 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
212 /* Match user name. */
216 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
218 /* Match domain name. */
222 return result
->first
? NSS_STATUS_NOTFOUND
: NSS_STATUS_RETURN
;
226 /* When we got here we have found an entry. Before we can copy it
227 to the private buffer we have to make sure it is big enough. */
228 if (cp
- host
> buflen
)
230 __set_errno (ERANGE
);
231 status
= NSS_STATUS_UNAVAIL
;
235 memcpy (buffer
, host
, cp
- host
);
236 result
->type
= triple_val
;
238 buffer
[(user
- host
) - 1] = '\0';
239 result
->val
.triple
.host
= *host
== ',' ? NULL
: buffer
;
241 buffer
[(domain
- host
) - 1] = '\0';
242 result
->val
.triple
.user
= *user
== ',' ? NULL
: buffer
+ (user
- host
);
244 buffer
[(cp
- host
) - 1] = '\0';
245 result
->val
.triple
.domain
=
246 *domain
== ')' ? NULL
: buffer
+ (domain
- host
);
248 status
= NSS_STATUS_SUCCESS
;
250 /* Remember where we stopped reading. */
261 _nss_files_getnetgrent_r (struct __netgrent
*result
, char *buffer
, int buflen
)
263 enum nss_status status
;
265 status
= _nss_netgroup_parseline (&result
->cursor
, result
, buffer
, buflen
);