(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nis / nss_nisplus / nisplus-parser.c
blobb61733a6280a389370c9d8542b93bf2966427d5d
1 /* Copyright (C) 1997, 1999, 2004 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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 = strtoul (first_unused, NULL, 10);
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 = strtoul (first_unused, NULL, 10);
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;
144 libnss_nisplus_hidden_def (_nss_nisplus_parse_pwent)
147 _nss_nisplus_parse_grent (nis_result *result, u_long entry, struct group *gr,
148 char *buffer, size_t buflen, int *errnop)
150 char *first_unused = buffer;
151 size_t room_left = buflen;
152 char *line;
153 int count;
154 size_t len;
156 if (result == NULL)
157 return 0;
159 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
160 || __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ
161 || strcmp (result->objects.objects_val[entry].EN_data.en_type,
162 "group_tbl") != 0
163 || result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 4)
164 return 0;
166 if (NISENTRYLEN (entry, 0, result) >= room_left)
168 /* The line is too long for our buffer. */
169 no_more_room:
170 *errnop = ERANGE;
171 return -1;
174 strncpy (first_unused, NISENTRYVAL (entry, 0, result),
175 NISENTRYLEN (entry, 0, result));
176 first_unused[NISENTRYLEN (entry, 0, result)] = '\0';
177 len = strlen (first_unused);
178 if (len == 0) /* group table is corrupt */
179 return 0;
180 gr->gr_name = first_unused;
181 room_left -= (len + 1);
182 first_unused += (len + 1);
184 if (NISENTRYLEN (entry, 1, result) >= room_left)
185 goto no_more_room;
187 strncpy (first_unused, NISENTRYVAL (entry, 1, result),
188 NISENTRYLEN (entry, 1, result));
189 first_unused[NISENTRYLEN (entry, 1, result)] = '\0';
190 gr->gr_passwd = first_unused;
191 len = strlen (first_unused);
192 room_left -= (len + 1);
193 first_unused += (len + 1);
195 if (NISENTRYLEN (entry, 2, result) >= room_left)
196 goto no_more_room;
198 strncpy (first_unused, NISENTRYVAL (entry, 2, result),
199 NISENTRYLEN (entry, 2, result));
200 first_unused[NISENTRYLEN (entry, 2, result)] = '\0';
201 len = strlen (first_unused);
202 if (len == 0) /* We should always have an gid */
203 return 0;
204 gr->gr_gid = strtoul (first_unused, NULL, 10);
205 room_left -= (strlen (first_unused) + 1);
206 first_unused += strlen (first_unused) + 1;
208 if (NISENTRYLEN (entry, 3, result) >= room_left)
209 goto no_more_room;
211 strncpy (first_unused, NISENTRYVAL (entry, 3, result),
212 NISENTRYLEN (entry, 3, result));
213 first_unused[NISENTRYLEN (entry, 3, result)] = '\0';
214 line = first_unused;
215 len = strlen (line);
216 room_left -= (len + 1);
217 first_unused += (len + 1);
218 /* Adjust the pointer so it is aligned for
219 storing pointers. */
220 first_unused += __alignof__ (char *) - 1;
221 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
222 gr->gr_mem = (char **) first_unused;
224 count = 0;
225 while (*line != '\0')
227 /* Skip leading blanks. */
228 while (isspace (*line))
229 ++line;
231 if (*line == '\0')
232 break;
234 if (room_left < sizeof (char *))
235 goto no_more_room;
236 room_left -= sizeof (char *);
237 gr->gr_mem[count++] = line;
239 while (*line != '\0' && *line != ',' && !isspace (*line))
240 ++line;
242 if (*line == ',' || isspace (*line))
244 int is = isspace (*line);
246 *line = '\0';
247 if (is)
248 while (*line != '\0' && (*line == ',' || isspace (*line)))
249 ++line;
250 else
251 ++line;
254 if (room_left < sizeof (char *))
255 goto no_more_room;
256 room_left -= sizeof (char *);
257 gr->gr_mem[count] = NULL;
259 return 1;
261 libnss_nisplus_hidden_def (_nss_nisplus_parse_grent)
264 _nss_nisplus_parse_spent (nis_result *result, struct spwd *sp,
265 char *buffer, size_t buflen, int *errnop)
267 char *first_unused = buffer;
268 size_t room_left = buflen;
269 size_t len;
271 if (result == NULL)
272 return 0;
274 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
275 || result->objects.objects_len != 1
276 || __type_of(result->objects.objects_val) != NIS_ENTRY_OBJ
277 || strcmp (result->objects.objects_val->EN_data.en_type,
278 "passwd_tbl") != 0
279 || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 8)
280 return 0;
282 if (NISENTRYLEN (0, 0, result) >= room_left)
284 /* The line is too long for our buffer. */
285 no_more_room:
286 *errnop = ERANGE;
287 return -1;
290 strncpy (first_unused, NISENTRYVAL (0, 0, result),
291 NISENTRYLEN (0, 0, result));
292 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
293 len = strlen (first_unused);
294 if (len == 0)
295 return 0;
296 sp->sp_namp = first_unused;
297 room_left -= (len + 1);
298 first_unused += (len + 1);
300 if (NISENTRYLEN (0, 1, result) >= room_left)
301 goto no_more_room;
303 strncpy (first_unused, NISENTRYVAL (0, 1, result),
304 NISENTRYLEN (0, 1, result));
305 first_unused[NISENTRYLEN (0, 1, result)] = '\0';
306 sp->sp_pwdp = first_unused;
307 len = strlen (first_unused);
308 room_left -= (len + 1);
309 first_unused += (len + 1);
311 sp->sp_lstchg = sp->sp_min = sp->sp_max = sp->sp_warn = sp->sp_inact =
312 sp->sp_expire = -1;
313 sp->sp_flag = ~0ul;
315 if (NISENTRYLEN (0, 7, result) > 0)
317 char *line, *cp;
319 line = NISENTRYVAL (0, 7, result);
320 cp = strchr (line, ':');
321 if (cp == NULL)
322 return 1;
323 *cp++ = '\0';
324 if (*line)
325 sp->sp_lstchg = atol (line);
327 line = cp;
328 cp = strchr (line, ':');
329 if (cp == NULL)
330 return 1;
331 *cp++ = '\0';
332 if (*line)
333 sp->sp_min = atol (line);
335 line = cp;
336 cp = strchr (line, ':');
337 if (cp == NULL)
338 return 1;
339 *cp++ = '\0';
340 if (*line)
341 sp->sp_max = atol (line);
343 line = cp;
344 cp = strchr (line, ':');
345 if (cp == NULL)
346 return 1;
347 *cp++ = '\0';
348 if (*line)
349 sp->sp_warn = atol (line);
351 line = cp;
352 cp = strchr (line, ':');
353 if (cp == NULL)
354 return 1;
355 *cp++ = '\0';
356 if (*line)
357 sp->sp_inact = atol (line);
359 line = cp;
360 cp = strchr (line, ':');
361 if (cp == NULL)
362 return 1;
363 *cp++ = '\0';
364 if (*line)
365 sp->sp_expire = atol (line);
367 line = cp;
368 if (line == NULL)
369 return 1;
370 if (*line)
371 sp->sp_flag = atol (line);
374 return 1;
376 libnss_nisplus_hidden_def (_nss_nisplus_parse_spent)