]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/nss_files/files-netgrp.c
Avoid undefined behaviour in netgroupcache
[thirdparty/glibc.git] / nss / nss_files / files-netgrp.c
CommitLineData
a68b0d31 1/* Netgroup file parser in nss_files modules.
d4697bc9 2 Copyright (C) 1996-2014 Free Software Foundation, Inc.
2303f5fd
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
a68b0d31 5
2303f5fd 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
a68b0d31 10
2303f5fd
UD
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
41bdb6e2 14 Lesser General Public License for more details.
a68b0d31 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
a68b0d31
UD
19
20#include <ctype.h>
21#include <errno.h>
a68b0d31
UD
22#include <netdb.h>
23#include <stdio.h>
ff3cacc5 24#include <stdio_ext.h>
a68b0d31
UD
25#include <stdlib.h>
26#include <string.h>
27#include "nsswitch.h"
28#include "netgroup.h"
29
30#define DATAFILE "/etc/netgroup"
31
ff3cacc5 32libnss_files_hidden_proto (_nss_files_endnetgrent)
a68b0d31 33
a68b0d31
UD
34#define EXPAND(needed) \
35 do \
36 { \
2303f5fd 37 size_t old_cursor = result->cursor - result->data; \
9be31a51 38 void *old_data = result->data; \
a68b0d31 39 \
2303f5fd
UD
40 result->data_size += 512 > 2 * needed ? 512 : 2 * needed; \
41 result->data = realloc (result->data, result->data_size); \
a68b0d31 42 \
2303f5fd 43 if (result->data == NULL) \
a68b0d31 44 { \
9be31a51 45 free (old_data); \
a68b0d31
UD
46 status = NSS_STATUS_UNAVAIL; \
47 goto the_end; \
48 } \
350635a5 49 \
2303f5fd 50 result->cursor = result->data + old_cursor; \
a68b0d31
UD
51 } \
52 while (0)
53
54
55enum nss_status
2303f5fd 56_nss_files_setnetgrent (const char *group, struct __netgrent *result)
a68b0d31
UD
57{
58 FILE *fp;
59 enum nss_status status;
60
61 if (group[0] == '\0')
62 return NSS_STATUS_UNAVAIL;
63
a68b0d31 64 /* Find the netgroups file and open it. */
312be3f9 65 fp = fopen (DATAFILE, "rce");
a68b0d31
UD
66 if (fp == NULL)
67 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
68 else
69 {
70 /* Read the file line by line and try to find the description
71 GROUP. We must take care for long lines. */
72 char *line = NULL;
73 size_t line_len = 0;
74 const ssize_t group_len = strlen (group);
75
76 status = NSS_STATUS_NOTFOUND;
2303f5fd 77 result->cursor = result->data;
a68b0d31 78
ff3cacc5
UD
79 __fsetlocking (fp, FSETLOCKING_BYCALLER);
80
81 while (!feof_unlocked (fp))
a68b0d31
UD
82 {
83 ssize_t curlen = getline (&line, &line_len, fp);
84 int found;
85
86 if (curlen < 0)
87 {
88 status = NSS_STATUS_NOTFOUND;
89 break;
90 }
91
92 found = (curlen > group_len && strncmp (line, group, group_len) == 0
93 && isspace (line[group_len]));
94
95 /* Read the whole line (including continuation) and store it
96 if FOUND in nonzero. Otherwise we don't need it. */
97 if (found)
98 {
99 /* Store the data from the first line. */
100 EXPAND (curlen - group_len);
2303f5fd
UD
101 memcpy (result->cursor, &line[group_len + 1],
102 curlen - group_len);
103 result->cursor += (curlen - group_len) - 1;
a68b0d31
UD
104 }
105
106 while (line[curlen - 1] == '\n' && line[curlen - 2] == '\\')
107 {
108 /* Yes, we have a continuation line. */
109 if (found)
110 /* Remove these characters from the stored line. */
2303f5fd 111 result->cursor -= 2;
a68b0d31 112
01c901a5 113 /* Get next line. */
a68b0d31
UD
114 curlen = getline (&line, &line_len, fp);
115 if (curlen <= 0)
116 break;
117
118 if (found)
119 {
120 /* Make sure we have enough room. */
121 EXPAND (1 + curlen + 1);
122
123 /* Add separator in case next line starts immediately. */
2303f5fd 124 *result->cursor++ = ' ';
a68b0d31
UD
125
126 /* Copy new line. */
2303f5fd
UD
127 memcpy (result->cursor, line, curlen + 1);
128 result->cursor += curlen;
a68b0d31
UD
129 }
130 }
131
132 if (found)
133 {
134 /* Now we have read the line. */
135 status = NSS_STATUS_SUCCESS;
2303f5fd
UD
136 result->cursor = result->data;
137 result->first = 1;
a68b0d31
UD
138 break;
139 }
140 }
141
142 the_end:
143 /* We don't need the file and the line buffer anymore. */
144 free (line);
145 fclose (fp);
ff3cacc5
UD
146
147 if (status != NSS_STATUS_SUCCESS)
148 _nss_files_endnetgrent (result);
a68b0d31
UD
149 }
150
a68b0d31
UD
151 return status;
152}
153
154
34da0098 155enum nss_status
2303f5fd 156_nss_files_endnetgrent (struct __netgrent *result)
a68b0d31 157{
a68b0d31 158 /* Free allocated memory for data if some is present. */
ff3cacc5
UD
159 free (result->data);
160 result->data = NULL;
161 result->data_size = 0;
162 result->cursor = NULL;
a68b0d31
UD
163 return NSS_STATUS_SUCCESS;
164}
ff3cacc5 165libnss_files_hidden_def (_nss_files_endnetgrent)
a68b0d31 166
3452aba8
UD
167static char *
168strip_whitespace (char *str)
169{
170 char *cp = str;
171
172 /* Skip leading spaces. */
173 while (isspace (*cp))
174 cp++;
175
176 str = cp;
177 while (*cp != '\0' && ! isspace(*cp))
178 cp++;
179
180 /* Null-terminate, stripping off any trailing spaces. */
181 *cp = '\0';
182
183 return *str == '\0' ? NULL : str;
184}
a68b0d31 185
d4a089cf 186enum nss_status
a68b0d31 187_nss_netgroup_parseline (char **cursor, struct __netgrent *result,
d71b808a 188 char *buffer, size_t buflen, int *errnop)
a68b0d31
UD
189{
190 enum nss_status status;
191 const char *host, *user, *domain;
192 char *cp = *cursor;
193
1f64ac13
UD
194 /* Some sanity checks. */
195 if (cp == NULL)
9c2322bc 196 return NSS_STATUS_NOTFOUND;
1f64ac13 197
a68b0d31
UD
198 /* First skip leading spaces. */
199 while (isspace (*cp))
200 ++cp;
201
202 if (*cp != '(')
11336c16
UD
203 {
204 /* We have a list of other netgroups. */
205 char *name = cp;
206
207 while (*cp != '\0' && ! isspace (*cp))
208 ++cp;
209
210 if (name != cp)
211 {
212 /* It is another netgroup name. */
213 int last = *cp == '\0';
214
215 result->type = group_val;
216 result->val.group = name;
217 *cp = '\0';
218 if (! last)
219 ++cp;
220 *cursor = cp;
2303f5fd 221 result->first = 0;
11336c16
UD
222
223 return NSS_STATUS_SUCCESS;
224 }
225
2303f5fd 226 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
11336c16 227 }
a68b0d31
UD
228
229 /* Match host name. */
230 host = ++cp;
231 while (*cp != ',')
232 if (*cp++ == '\0')
2303f5fd 233 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
a68b0d31
UD
234
235 /* Match user name. */
236 user = ++cp;
237 while (*cp != ',')
238 if (*cp++ == '\0')
2303f5fd 239 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
a68b0d31
UD
240
241 /* Match domain name. */
242 domain = ++cp;
243 while (*cp != ')')
244 if (*cp++ == '\0')
2303f5fd 245 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
a68b0d31
UD
246 ++cp;
247
248
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)
252 {
d71b808a 253 *errnop = ERANGE;
a68b0d31
UD
254 status = NSS_STATUS_UNAVAIL;
255 }
256 else
257 {
258 memcpy (buffer, host, cp - host);
11336c16 259 result->type = triple_val;
a68b0d31 260
3452aba8
UD
261 buffer[(user - host) - 1] = '\0'; /* Replace ',' with '\0'. */
262 result->val.triple.host = strip_whitespace (buffer);
a68b0d31 263
3452aba8
UD
264 buffer[(domain - host) - 1] = '\0'; /* Replace ',' with '\0'. */
265 result->val.triple.user = strip_whitespace (buffer + (user - host));
a68b0d31 266
3452aba8
UD
267 buffer[(cp - host) - 1] = '\0'; /* Replace ')' with '\0'. */
268 result->val.triple.domain = strip_whitespace (buffer + (domain - host));
a68b0d31
UD
269
270 status = NSS_STATUS_SUCCESS;
271
6d52618b 272 /* Remember where we stopped reading. */
a68b0d31
UD
273 *cursor = cp;
274
2303f5fd 275 result->first = 0;
a68b0d31
UD
276 }
277
278 return status;
279}
8cf26632 280libnss_files_hidden_def (_nss_netgroup_parseline)
a68b0d31
UD
281
282
283enum nss_status
d71b808a
UD
284_nss_files_getnetgrent_r (struct __netgrent *result, char *buffer,
285 size_t buflen, int *errnop)
a68b0d31
UD
286{
287 enum nss_status status;
288
d71b808a
UD
289 status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
290 errnop);
a68b0d31
UD
291
292 return status;
293}