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 /* Some sanity checks. */
193 /* User bug. setnetgrent() wasn't called before. */
196 /* First skip leading spaces. */
197 while (isspace (*cp))
202 /* We have a list of other netgroups. */
205 while (*cp != '\0' && ! isspace (*cp))
210 /* It is another netgroup name. */
211 int last = *cp == '\0';
213 result->type = group_val;
214 result->val.group = name;
221 return NSS_STATUS_SUCCESS;
224 return first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
227 /* Match host name. */
231 return first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
233 /* Match user name. */
237 return first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
239 /* Match domain name. */
243 return first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
247 /* When we got here we have found an entry. Before we can copy it
248 to the private buffer we have to make sure it is big enough. */
249 if (cp - host > buflen)
251 __set_errno (ERANGE);
252 status = NSS_STATUS_UNAVAIL;
256 memcpy (buffer, host, cp - host);
257 result->type = triple_val;
259 buffer[(user - host) - 1] = '\0';
260 result->val.triple.host = *host == ',' ? NULL : buffer;
262 buffer[(domain - host) - 1] = '\0';
263 result->val.triple.user = *user == ',' ? NULL : buffer + (user - host);
265 buffer[(cp - host) - 1] = '\0';
266 result->val.triple.domain =
267 *domain == ')' ? NULL : buffer + (domain - host);
269 status = NSS_STATUS_SUCCESS;
271 /* Rememember where we stopped reading. */
282 _nss_files_getnetgrent_r (struct __netgrent *result, char *buffer, int buflen)
284 enum nss_status status;
286 __libc_lock_lock (lock);
288 status = _nss_netgroup_parseline (&cursor, result, buffer, buflen);
290 __libc_lock_unlock (lock);