update from main archive 961001
[glibc.git] / nss / nss_files / files-netgrp.c
blob8af55f209fd239daccf36c87f837a2569097429a
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. */
21 #include <ctype.h>
22 #include <errno.h>
23 #include <libc-lock.h>
24 #include <netdb.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "nsswitch.h"
29 #include "netgroup.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'. */
40 static char *data;
41 static size_t data_size;
42 static char *cursor;
43 static int first;
46 #define EXPAND(needed) \
47 do \
48 { \
49 size_t old_cursor = cursor - data; \
51 data_size += 512 > 2 * needed ? 512 : 2 * needed; \
52 data = realloc (data, data_size); \
54 if (data == NULL) \
55 { \
56 status = NSS_STATUS_UNAVAIL; \
57 goto the_end; \
58 } \
60 cursor = data + old_cursor; \
61 } \
62 while (0)
65 enum nss_status
66 _nss_files_setnetgrent (const char *group)
68 FILE *fp;
69 enum nss_status status;
71 if (group[0] == '\0')
72 return NSS_STATUS_UNAVAIL;
74 __libc_lock_lock (lock);
76 /* Find the netgroups file and open it. */
77 fp = fopen (DATAFILE, "r");
78 if (fp == NULL)
79 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
80 else
82 /* Read the file line by line and try to find the description
83 GROUP. We must take care for long lines. */
84 char *line = NULL;
85 size_t line_len = 0;
86 const ssize_t group_len = strlen (group);
88 status = NSS_STATUS_NOTFOUND;
89 cursor = data;
91 while (!feof (fp))
93 ssize_t curlen = getline (&line, &line_len, fp);
94 int found;
96 if (curlen < 0)
98 status = NSS_STATUS_NOTFOUND;
99 break;
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. */
107 if (found)
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. */
118 if (found)
119 /* Remove these characters from the stored line. */
120 cursor -= 2;
122 /* Get netxt line. */
123 curlen = getline (&line, &line_len, fp);
124 if (curlen <= 0)
125 break;
127 if (found)
129 /* Make sure we have enough room. */
130 EXPAND (1 + curlen + 1);
132 /* Add separator in case next line starts immediately. */
133 *cursor++ = ' ';
135 /* Copy new line. */
136 memcpy (cursor, line, curlen + 1);
137 cursor += curlen;
141 if (found)
143 /* Now we have read the line. */
144 status = NSS_STATUS_SUCCESS;
145 cursor = data;
146 first = 1;
147 break;
151 the_end:
152 /* We don't need the file and the line buffer anymore. */
153 free (line);
154 fclose (fp);
157 __libc_lock_unlock (lock);
159 return status;
164 _nss_files_endnetgrent (void)
166 __libc_lock_lock (lock);
168 /* Free allocated memory for data if some is present. */
169 if (data != NULL)
171 free (data);
172 data = NULL;
173 data_size = 0;
174 cursor = NULL;
177 __libc_lock_unlock (lock);
179 return NSS_STATUS_SUCCESS;
183 enum nss_status
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;
189 char *cp = *cursor;
191 /* First skip leading spaces. */
192 while (isspace (*cp))
193 ++cp;
195 if (*cp != '(')
196 return first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
198 /* Match host name. */
199 host = ++cp;
200 while (*cp != ',')
201 if (*cp++ == '\0')
202 return first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
204 /* Match user name. */
205 user = ++cp;
206 while (*cp != ',')
207 if (*cp++ == '\0')
208 return first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
210 /* Match domain name. */
211 domain = ++cp;
212 while (*cp != ')')
213 if (*cp++ == '\0')
214 return first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
215 ++cp;
218 /* When we got here we have found an entry. Before we can copy it
219 to the private buffer we have to make sure it is big enough. */
220 if (cp - host > buflen)
222 __set_errno (ERANGE);
223 status = NSS_STATUS_UNAVAIL;
225 else
227 memcpy (buffer, host, cp - host);
229 buffer[(user - host) - 1] = '\0';
230 result->host = *host == ',' ? NULL : buffer;
232 buffer[(domain - host) - 1] = '\0';
233 result->user = *user == ',' ? NULL : buffer + (user - host);
235 buffer[(cp - host) - 1] = '\0';
236 result->domain = *domain == ')' ? NULL : buffer + (domain - host);
238 status = NSS_STATUS_SUCCESS;
240 /* Rememember where we stopped reading. */
241 *cursor = cp;
243 first = 0;
246 return status;
250 enum nss_status
251 _nss_files_getnetgrent_r (struct __netgrent *result, char *buffer, int buflen)
253 enum nss_status status;
255 __libc_lock_lock (lock);
257 status = _nss_netgroup_parseline (&cursor, result, buffer, buflen);
259 __libc_lock_unlock (lock);
261 return status;