Update.
[glibc.git] / nis / nss_nisplus / nisplus-parser.c
blobfa4073e6d87b5f8550d20bfeb59923654d1e4e19
1 /* Copyright (C) 1997 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;
42 if (result == NULL)
43 return 0;
45 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
46 || result->objects.objects_len != 1
47 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
48 || strcmp (result->objects.objects_val->EN_data.en_type,
49 "passwd_tbl") != 0
50 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 7)
51 return 0;
53 if (NISENTRYLEN (0, 0, result) >= room_left)
55 /* The line is too long for our buffer. */
56 no_more_room:
57 *errnop = ERANGE;
58 return -1;
61 strncpy (first_unused, NISENTRYVAL (0, 0, result),
62 NISENTRYLEN (0, 0, result));
63 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
64 pw->pw_name = first_unused;
65 room_left -= (strlen (first_unused) +1);
66 first_unused += strlen (first_unused) +1;
68 if (NISENTRYLEN (0, 1, result) >= room_left)
69 goto no_more_room;
71 strncpy (first_unused, NISENTRYVAL (0, 1, result),
72 NISENTRYLEN (0, 1, result));
73 first_unused[NISENTRYLEN (0, 1, result)] = '\0';
74 pw->pw_passwd = first_unused;
75 room_left -= (strlen (first_unused) +1);
76 first_unused += strlen (first_unused) +1;
78 if (NISENTRYLEN(0, 2, result) >= room_left)
79 goto no_more_room;
81 strncpy (first_unused, NISENTRYVAL (0, 2, result),
82 NISENTRYLEN (0, 2, result));
83 first_unused[NISENTRYLEN (0, 2, result)] = '\0';
84 pw->pw_uid = atoi (first_unused);
85 room_left -= (strlen (first_unused) +1);
86 first_unused += strlen (first_unused) +1;
88 if (NISENTRYLEN (0, 3, result) >= room_left)
89 goto no_more_room;
91 strncpy (first_unused, NISENTRYVAL (0, 3, result),
92 NISENTRYLEN (0, 3, result));
93 first_unused[NISENTRYLEN (0, 3, result)] = '\0';
94 pw->pw_gid = atoi (first_unused);
95 room_left -= (strlen (first_unused) +1);
96 first_unused += strlen (first_unused) +1;
98 if (NISENTRYLEN(0, 4, result) >= room_left)
99 goto no_more_room;
101 strncpy (first_unused, NISENTRYVAL (0, 4, result),
102 NISENTRYLEN (0, 4, result));
103 first_unused[NISENTRYLEN (0, 4, result)] = '\0';
104 pw->pw_gecos = first_unused;
105 room_left -= (strlen (first_unused) +1);
106 first_unused += strlen (first_unused) +1;
108 if (NISENTRYLEN (0, 5, result) >= room_left)
109 goto no_more_room;
111 strncpy (first_unused, NISENTRYVAL (0, 5, result),
112 NISENTRYLEN (0, 5, result));
113 first_unused[NISENTRYLEN (0, 5, result)] = '\0';
114 pw->pw_dir = first_unused;
115 room_left -= (strlen (first_unused) +1);
116 first_unused += strlen (first_unused) +1;
118 if (NISENTRYLEN (0, 6, result) >= room_left)
119 goto no_more_room;
121 strncpy (first_unused, NISENTRYVAL (0, 6, result),
122 NISENTRYLEN (0, 6, result));
123 first_unused[NISENTRYLEN (0, 6, result)] = '\0';
124 pw->pw_shell = first_unused;
125 room_left -= (strlen (first_unused) +1);
126 first_unused += strlen (first_unused) +1;
128 return 1;
132 _nss_nisplus_parse_grent (nis_result *result, u_long entry, struct group *gr,
133 char *buffer, size_t buflen, int *errnop)
135 char *first_unused = buffer;
136 size_t room_left = buflen;
137 char *line;
138 int count;
140 if (result == NULL)
141 return 0;
143 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
144 || __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ
145 || strcmp (result->objects.objects_val[entry].EN_data.en_type,
146 "group_tbl") != 0
147 || result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 4)
148 return 0;
150 if (NISENTRYLEN (entry, 0, result) >= room_left)
152 /* The line is too long for our buffer. */
153 no_more_room:
154 *errnop = ERANGE;
155 return -1;
158 strncpy (first_unused, NISENTRYVAL (entry, 0, result),
159 NISENTRYLEN (entry, 0, result));
160 first_unused[NISENTRYLEN (entry, 0, result)] = '\0';
161 gr->gr_name = first_unused;
162 room_left -= (strlen (first_unused) + 1);
163 first_unused += strlen (first_unused) + 1;
165 if (NISENTRYLEN (entry, 1, result) >= room_left)
166 goto no_more_room;
168 strncpy (first_unused, NISENTRYVAL (entry, 1, result),
169 NISENTRYLEN (entry, 1, result));
170 first_unused[NISENTRYLEN (entry, 1, result)] = '\0';
171 gr->gr_passwd = first_unused;
172 room_left -= (strlen (first_unused) + 1);
173 first_unused += strlen (first_unused) + 1;
175 if (NISENTRYLEN (entry, 2, result) >= room_left)
176 goto no_more_room;
178 strncpy (first_unused, NISENTRYVAL (entry, 2, result),
179 NISENTRYLEN (entry, 2, result));
180 first_unused[NISENTRYLEN (entry, 2, result)] = '\0';
181 gr->gr_gid = atoi (first_unused);
182 room_left -= (strlen (first_unused) + 1);
183 first_unused += strlen (first_unused) + 1;
185 if (NISENTRYLEN (entry, 3, result) >= room_left)
186 goto no_more_room;
188 strncpy (first_unused, NISENTRYVAL (entry, 3, result),
189 NISENTRYLEN (entry, 3, result));
190 first_unused[NISENTRYLEN (entry, 3, result)] = '\0';
191 line = first_unused;
192 room_left -= (strlen (line) + 1);
193 first_unused += strlen (line) + 1;
194 /* Adjust the pointer so it is aligned for
195 storing pointers. */
196 first_unused += __alignof__ (char *) - 1;
197 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
198 gr->gr_mem = (char **) first_unused;
200 count = 0;
201 while (*line != '\0')
203 /* Skip leading blanks. */
204 while (isspace (*line))
205 ++line;
207 if (*line == '\0')
208 break;
210 if (room_left < sizeof (char *))
211 goto no_more_room;
212 room_left -= sizeof (char *);
213 gr->gr_mem[count] = line;
215 while (*line != '\0' && *line != ',' && !isspace (*line))
216 ++line;
218 if (*line == ',' || isspace (*line))
220 int is = isspace (*line);
222 *line = '\0';
223 if (is)
224 while (*line != '\0' && (*line == ',' || isspace (*line)))
225 ++line;
226 else
227 ++line;
228 ++count;
230 else
231 gr->gr_mem[count+1] = NULL;
233 if (room_left < sizeof (char *))
234 goto no_more_room;
235 room_left -= sizeof (char *);
236 gr->gr_mem[count] = NULL;
238 return 1;
242 _nss_nisplus_parse_spent (nis_result *result, struct spwd *sp,
243 char *buffer, size_t buflen, int *errnop)
245 char *first_unused = buffer;
246 size_t room_left = buflen;
248 if (result == NULL)
249 return 0;
251 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
252 || result->objects.objects_len != 1
253 || __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ
254 || strcmp (result->objects.objects_val->EN_data.en_type,
255 "passwd_tbl") != 0
256 || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 8)
257 return 0;
259 if (NISENTRYLEN (0, 0, result) >= room_left)
261 /* The line is too long for our buffer. */
262 no_more_room:
263 *errnop = ERANGE;
264 return -1;
267 strncpy (first_unused, NISENTRYVAL (0, 0, result),
268 NISENTRYLEN (0, 0, result));
269 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
270 sp->sp_namp = first_unused;
271 room_left -= (strlen (first_unused) +1);
272 first_unused += strlen (first_unused) +1;
274 if (NISENTRYLEN (0, 1, result) >= room_left)
275 goto no_more_room;
277 strncpy (first_unused, NISENTRYVAL (0, 1, result),
278 NISENTRYLEN (0, 1, result));
279 first_unused[NISENTRYLEN (0, 1, result)] = '\0';
280 sp->sp_pwdp = first_unused;
281 room_left -= (strlen (first_unused) +1);
282 first_unused += strlen (first_unused) +1;
284 sp->sp_lstchg = sp->sp_min = sp->sp_max = sp->sp_warn = sp->sp_inact =
285 sp->sp_expire = sp->sp_flag = -1;
287 if (NISENTRYLEN (0, 7, result) > 0)
289 char *line, *cp;
291 line = NISENTRYVAL (0, 7, result);
292 cp = strchr (line, ':');
293 if (cp == NULL)
294 return 0;
295 *cp++ = '\0';
296 sp->sp_lstchg = atol (line);
298 line = cp;
299 cp = strchr (line, ':');
300 if (cp == NULL)
301 return 0;
302 *cp++ = '\0';
303 sp->sp_min = atol (line);
305 line = cp;
306 cp = strchr (line, ':');
307 if (cp == NULL)
308 return 0;
309 *cp++ = '\0';
310 sp->sp_max = atol (line);
312 line = cp;
313 cp = strchr (line, ':');
314 if (cp == NULL)
315 return 0;
316 *cp++ = '\0';
317 sp->sp_warn = atol (line);
319 line = cp;
320 cp = strchr (line, ':');
321 if (cp == NULL)
322 return 0;
323 *cp++ = '\0';
324 sp->sp_inact = atol (line);
326 line = cp;
327 cp = strchr (line, ':');
328 if (cp == NULL)
329 return 0;
330 *cp++ = '\0';
331 sp->sp_expire = atol (line);
333 line = cp;
334 if (line == NULL)
335 return 0;
336 sp->sp_flag = atol (line);
339 return 1;