* sysdeps/m68k/fpu/bits/mathinline.h: Don't define log2 as inline.
[glibc.git] / nis / nss_nisplus / nisplus-parser.c
blob7a9199276403551c3d6f91852daf4ce2916e6f0d
1 /* Copyright (C) 1997, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <pwd.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <rpcsvc/nis.h>
26 #include "nisplus-parser.h"
28 #define NISENTRYVAL(idx,col,res) \
29 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
31 #define NISENTRYLEN(idx,col,res) \
32 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
35 int
36 _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw,
37 char *buffer, size_t buflen, int *errnop)
39 char *first_unused = buffer;
40 size_t room_left = buflen;
41 size_t len;
43 if (result == NULL)
44 return 0;
46 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
47 || result->objects.objects_len != 1
48 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
49 || strcmp (result->objects.objects_val->EN_data.en_type,
50 "passwd_tbl") != 0
51 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 7)
52 return 0;
54 if (NISENTRYLEN (0, 0, result) >= room_left)
56 /* The line is too long for our buffer. */
57 no_more_room:
58 *errnop = ERANGE;
59 return -1;
62 strncpy (first_unused, NISENTRYVAL (0, 0, result),
63 NISENTRYLEN (0, 0, result));
64 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
65 len = strlen (first_unused);
66 if (len == 0) /* No name ? Should never happen, database is corrupt */
67 return 0;
68 pw->pw_name = first_unused;
69 room_left -= (len + 1);
70 first_unused += (len + 1);
72 if (NISENTRYLEN (0, 1, result) >= room_left)
73 goto no_more_room;
75 strncpy (first_unused, NISENTRYVAL (0, 1, result),
76 NISENTRYLEN (0, 1, result));
77 first_unused[NISENTRYLEN (0, 1, result)] = '\0';
78 pw->pw_passwd = first_unused;
79 len = strlen (first_unused);
80 room_left -= (len + 1);
81 first_unused += (len + 1);
83 if (NISENTRYLEN(0, 2, result) >= room_left)
84 goto no_more_room;
86 strncpy (first_unused, NISENTRYVAL (0, 2, result),
87 NISENTRYLEN (0, 2, result));
88 first_unused[NISENTRYLEN (0, 2, result)] = '\0';
89 len = strlen (first_unused);
90 if (len == 0) /* If we don't have a uid, it's an invalid shadow entry */
91 return 0;
92 pw->pw_uid = atoi (first_unused);
93 room_left -= (len + 1);
94 first_unused += (len + 1);
96 if (NISENTRYLEN (0, 3, result) >= room_left)
97 goto no_more_room;
99 strncpy (first_unused, NISENTRYVAL (0, 3, result),
100 NISENTRYLEN (0, 3, result));
101 first_unused[NISENTRYLEN (0, 3, result)] = '\0';
102 len = strlen (first_unused);
103 if (len == 0) /* If we don't have a gid, it's an invalid shadow entry */
104 return 0;
105 pw->pw_gid = atoi (first_unused);
106 room_left -= (len + 1);
107 first_unused += (len + 1);
109 if (NISENTRYLEN(0, 4, result) >= room_left)
110 goto no_more_room;
112 strncpy (first_unused, NISENTRYVAL (0, 4, result),
113 NISENTRYLEN (0, 4, result));
114 first_unused[NISENTRYLEN (0, 4, result)] = '\0';
115 pw->pw_gecos = first_unused;
116 len = strlen (first_unused);
117 room_left -= (len + 1);
118 first_unused += (len + 1);
120 if (NISENTRYLEN (0, 5, result) >= room_left)
121 goto no_more_room;
123 strncpy (first_unused, NISENTRYVAL (0, 5, result),
124 NISENTRYLEN (0, 5, result));
125 first_unused[NISENTRYLEN (0, 5, result)] = '\0';
126 pw->pw_dir = first_unused;
127 len = strlen (first_unused);
128 room_left -= (len + 1);
129 first_unused += (len + 1);
131 if (NISENTRYLEN (0, 6, result) >= room_left)
132 goto no_more_room;
134 strncpy (first_unused, NISENTRYVAL (0, 6, result),
135 NISENTRYLEN (0, 6, result));
136 first_unused[NISENTRYLEN (0, 6, result)] = '\0';
137 pw->pw_shell = first_unused;
138 len = strlen (first_unused);
139 room_left -= (len + 1);
140 first_unused += (len + 1);
142 return 1;
146 _nss_nisplus_parse_grent (nis_result *result, u_long entry, struct group *gr,
147 char *buffer, size_t buflen, int *errnop)
149 char *first_unused = buffer;
150 size_t room_left = buflen;
151 char *line;
152 int count;
153 size_t len;
155 if (result == NULL)
156 return 0;
158 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
159 || __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ
160 || strcmp (result->objects.objects_val[entry].EN_data.en_type,
161 "group_tbl") != 0
162 || result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 4)
163 return 0;
165 if (NISENTRYLEN (entry, 0, result) >= room_left)
167 /* The line is too long for our buffer. */
168 no_more_room:
169 *errnop = ERANGE;
170 return -1;
173 strncpy (first_unused, NISENTRYVAL (entry, 0, result),
174 NISENTRYLEN (entry, 0, result));
175 first_unused[NISENTRYLEN (entry, 0, result)] = '\0';
176 len = strlen (first_unused);
177 if (len == 0) /* group table is corrupt */
178 return 0;
179 gr->gr_name = first_unused;
180 room_left -= (len + 1);
181 first_unused += (len + 1);
183 if (NISENTRYLEN (entry, 1, result) >= room_left)
184 goto no_more_room;
186 strncpy (first_unused, NISENTRYVAL (entry, 1, result),
187 NISENTRYLEN (entry, 1, result));
188 first_unused[NISENTRYLEN (entry, 1, result)] = '\0';
189 gr->gr_passwd = first_unused;
190 len = strlen (first_unused);
191 room_left -= (len + 1);
192 first_unused += (len + 1);
194 if (NISENTRYLEN (entry, 2, result) >= room_left)
195 goto no_more_room;
197 strncpy (first_unused, NISENTRYVAL (entry, 2, result),
198 NISENTRYLEN (entry, 2, result));
199 first_unused[NISENTRYLEN (entry, 2, result)] = '\0';
200 len = strlen (first_unused);
201 if (len == 0) /* We should always have an gid */
202 return 0;
203 gr->gr_gid = atoi (first_unused);
204 room_left -= (strlen (first_unused) + 1);
205 first_unused += strlen (first_unused) + 1;
207 if (NISENTRYLEN (entry, 3, result) >= room_left)
208 goto no_more_room;
210 strncpy (first_unused, NISENTRYVAL (entry, 3, result),
211 NISENTRYLEN (entry, 3, result));
212 first_unused[NISENTRYLEN (entry, 3, result)] = '\0';
213 line = first_unused;
214 len = strlen (line);
215 room_left -= (len + 1);
216 first_unused += (len + 1);
217 /* Adjust the pointer so it is aligned for
218 storing pointers. */
219 first_unused += __alignof__ (char *) - 1;
220 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
221 gr->gr_mem = (char **) first_unused;
223 count = 0;
224 while (*line != '\0')
226 /* Skip leading blanks. */
227 while (isspace (*line))
228 ++line;
230 if (*line == '\0')
231 break;
233 if (room_left < sizeof (char *))
234 goto no_more_room;
235 room_left -= sizeof (char *);
236 gr->gr_mem[count++] = line;
238 while (*line != '\0' && *line != ',' && !isspace (*line))
239 ++line;
241 if (*line == ',' || isspace (*line))
243 int is = isspace (*line);
245 *line = '\0';
246 if (is)
247 while (*line != '\0' && (*line == ',' || isspace (*line)))
248 ++line;
249 else
250 ++line;
253 if (room_left < sizeof (char *))
254 goto no_more_room;
255 room_left -= sizeof (char *);
256 gr->gr_mem[count] = NULL;
258 return 1;
262 _nss_nisplus_parse_spent (nis_result *result, struct spwd *sp,
263 char *buffer, size_t buflen, int *errnop)
265 char *first_unused = buffer;
266 size_t room_left = buflen;
267 size_t len;
269 if (result == NULL)
270 return 0;
272 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
273 || result->objects.objects_len != 1
274 || __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ
275 || strcmp (result->objects.objects_val->EN_data.en_type,
276 "passwd_tbl") != 0
277 || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 8)
278 return 0;
280 if (NISENTRYLEN (0, 0, result) >= room_left)
282 /* The line is too long for our buffer. */
283 no_more_room:
284 *errnop = ERANGE;
285 return -1;
288 strncpy (first_unused, NISENTRYVAL (0, 0, result),
289 NISENTRYLEN (0, 0, result));
290 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
291 len = strlen (first_unused);
292 if (len == 0)
293 return 0;
294 sp->sp_namp = first_unused;
295 room_left -= (len + 1);
296 first_unused += (len + 1);
298 if (NISENTRYLEN (0, 1, result) >= room_left)
299 goto no_more_room;
301 strncpy (first_unused, NISENTRYVAL (0, 1, result),
302 NISENTRYLEN (0, 1, result));
303 first_unused[NISENTRYLEN (0, 1, result)] = '\0';
304 sp->sp_pwdp = first_unused;
305 len = strlen (first_unused);
306 room_left -= (len + 1);
307 first_unused += (len + 1);
309 sp->sp_lstchg = sp->sp_min = sp->sp_max = sp->sp_warn = sp->sp_inact =
310 sp->sp_expire = -1;
311 sp->sp_flag = ~0ul;
313 if (NISENTRYLEN (0, 7, result) > 0)
315 char *line, *cp;
317 line = NISENTRYVAL (0, 7, result);
318 cp = strchr (line, ':');
319 if (cp == NULL)
320 return 1;
321 *cp++ = '\0';
322 if (*line)
323 sp->sp_lstchg = atol (line);
325 line = cp;
326 cp = strchr (line, ':');
327 if (cp == NULL)
328 return 1;
329 *cp++ = '\0';
330 if (*line)
331 sp->sp_min = atol (line);
333 line = cp;
334 cp = strchr (line, ':');
335 if (cp == NULL)
336 return 1;
337 *cp++ = '\0';
338 if (*line)
339 sp->sp_max = atol (line);
341 line = cp;
342 cp = strchr (line, ':');
343 if (cp == NULL)
344 return 1;
345 *cp++ = '\0';
346 if (*line)
347 sp->sp_warn = atol (line);
349 line = cp;
350 cp = strchr (line, ':');
351 if (cp == NULL)
352 return 1;
353 *cp++ = '\0';
354 if (*line)
355 sp->sp_inact = atol (line);
357 line = cp;
358 cp = strchr (line, ':');
359 if (cp == NULL)
360 return 1;
361 *cp++ = '\0';
362 if (*line)
363 sp->sp_expire = atol (line);
365 line = cp;
366 if (line == NULL)
367 return 1;
368 if (*line)
369 sp->sp_flag = atol (line);
372 return 1;