(sysdep_routines, shared-only-routines): Don't add divdi3 here.
[glibc.git] / nss / nss_files / files-netgrp.c
blob34d2b64473c3a2573a69c0bea44d13efe91b5807
1 /* Netgroup file parser in nss_files modules.
2 Copyright (C) 1996, 1997, 2000 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
19 02111-1307 USA. */
21 #include <ctype.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "nsswitch.h"
28 #include "netgroup.h"
30 #define DATAFILE "/etc/netgroup"
33 #define EXPAND(needed) \
34 do \
35 { \
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) \
42 { \
43 status = NSS_STATUS_UNAVAIL; \
44 goto the_end; \
45 } \
47 result->cursor = result->data + old_cursor; \
48 } \
49 while (0)
52 enum nss_status
53 _nss_files_setnetgrent (const char *group, struct __netgrent *result)
55 FILE *fp;
56 enum nss_status status;
58 if (group[0] == '\0')
59 return NSS_STATUS_UNAVAIL;
61 /* Find the netgroups file and open it. */
62 fp = fopen (DATAFILE, "r");
63 if (fp == NULL)
64 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
65 else
67 /* Read the file line by line and try to find the description
68 GROUP. We must take care for long lines. */
69 char *line = NULL;
70 size_t line_len = 0;
71 const ssize_t group_len = strlen (group);
73 status = NSS_STATUS_NOTFOUND;
74 result->cursor = result->data;
76 while (!feof (fp))
78 ssize_t curlen = getline (&line, &line_len, fp);
79 int found;
81 if (curlen < 0)
83 status = NSS_STATUS_NOTFOUND;
84 break;
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. */
92 if (found)
94 /* Store the data from the first line. */
95 EXPAND (curlen - group_len);
96 memcpy (result->cursor, &line[group_len + 1],
97 curlen - group_len);
98 result->cursor += (curlen - group_len) - 1;
101 while (line[curlen - 1] == '\n' && line[curlen - 2] == '\\')
103 /* Yes, we have a continuation line. */
104 if (found)
105 /* Remove these characters from the stored line. */
106 result->cursor -= 2;
108 /* Get next line. */
109 curlen = getline (&line, &line_len, fp);
110 if (curlen <= 0)
111 break;
113 if (found)
115 /* Make sure we have enough room. */
116 EXPAND (1 + curlen + 1);
118 /* Add separator in case next line starts immediately. */
119 *result->cursor++ = ' ';
121 /* Copy new line. */
122 memcpy (result->cursor, line, curlen + 1);
123 result->cursor += curlen;
127 if (found)
129 /* Now we have read the line. */
130 status = NSS_STATUS_SUCCESS;
131 result->cursor = result->data;
132 result->first = 1;
133 break;
137 the_end:
138 /* We don't need the file and the line buffer anymore. */
139 free (line);
140 fclose (fp);
143 return status;
148 _nss_files_endnetgrent (struct __netgrent *result)
150 /* Free allocated memory for data if some is present. */
151 if (result->data != NULL)
153 free (result->data);
154 result->data = NULL;
155 result->data_size = 0;
156 result->cursor = NULL;
159 return NSS_STATUS_SUCCESS;
162 static char *
163 strip_whitespace (char *str)
165 char *cp = str;
167 /* Skip leading spaces. */
168 while (isspace (*cp))
169 cp++;
171 str = cp;
172 while (*cp != '\0' && ! isspace(*cp))
173 cp++;
175 /* Null-terminate, stripping off any trailing spaces. */
176 *cp = '\0';
178 return *str == '\0' ? NULL : str;
181 enum nss_status
182 _nss_netgroup_parseline (char **cursor, struct __netgrent *result,
183 char *buffer, size_t buflen, int *errnop)
185 enum nss_status status;
186 const char *host, *user, *domain;
187 char *cp = *cursor;
189 /* Some sanity checks. */
190 if (cp == NULL)
191 return NSS_STATUS_NOTFOUND;
193 /* First skip leading spaces. */
194 while (isspace (*cp))
195 ++cp;
197 if (*cp != '(')
199 /* We have a list of other netgroups. */
200 char *name = cp;
202 while (*cp != '\0' && ! isspace (*cp))
203 ++cp;
205 if (name != cp)
207 /* It is another netgroup name. */
208 int last = *cp == '\0';
210 result->type = group_val;
211 result->val.group = name;
212 *cp = '\0';
213 if (! last)
214 ++cp;
215 *cursor = cp;
216 result->first = 0;
218 return NSS_STATUS_SUCCESS;
221 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
224 /* Match host name. */
225 host = ++cp;
226 while (*cp != ',')
227 if (*cp++ == '\0')
228 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
230 /* Match user name. */
231 user = ++cp;
232 while (*cp != ',')
233 if (*cp++ == '\0')
234 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
236 /* Match domain name. */
237 domain = ++cp;
238 while (*cp != ')')
239 if (*cp++ == '\0')
240 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
241 ++cp;
244 /* When we got here we have found an entry. Before we can copy it
245 to the private buffer we have to make sure it is big enough. */
246 if (cp - host > buflen)
248 *errnop = ERANGE;
249 status = NSS_STATUS_UNAVAIL;
251 else
253 memcpy (buffer, host, cp - host);
254 result->type = triple_val;
256 buffer[(user - host) - 1] = '\0'; /* Replace ',' with '\0'. */
257 result->val.triple.host = strip_whitespace (buffer);
259 buffer[(domain - host) - 1] = '\0'; /* Replace ',' with '\0'. */
260 result->val.triple.user = strip_whitespace (buffer + (user - host));
262 buffer[(cp - host) - 1] = '\0'; /* Replace ')' with '\0'. */
263 result->val.triple.domain = strip_whitespace (buffer + (domain - host));
265 status = NSS_STATUS_SUCCESS;
267 /* Remember where we stopped reading. */
268 *cursor = cp;
270 result->first = 0;
273 return status;
277 enum nss_status
278 _nss_files_getnetgrent_r (struct __netgrent *result, char *buffer,
279 size_t buflen, int *errnop)
281 enum nss_status status;
283 status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
284 errnop);
286 return status;