(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nis / nss_nisplus / nisplus-proto.c
blob83456cae64df2f9c52af9f263202c096796b96ce
1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003 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 <nss.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock)
32 static nis_result *result;
33 static nis_name tablename_val;
34 static u_long tablename_len;
36 #define NISENTRYVAL(idx,col,res) \
37 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
39 #define NISENTRYLEN(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
42 static int
43 _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
44 char *buffer, size_t buflen, int *errnop)
46 char *first_unused = buffer;
47 size_t room_left = buflen;
48 unsigned int i;
49 char *p, *line;
51 if (result == NULL)
52 return 0;
54 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
55 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
56 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
57 "protocols_tbl") != 0
58 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
59 return 0;
61 /* Generate the protocols entry format and use the normal parser */
62 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
64 no_more_room:
65 *errnop = ERANGE;
66 return -1;
68 strncpy (first_unused, NISENTRYVAL (0, 0, result),
69 NISENTRYLEN (0, 0, result));
70 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
71 proto->p_name = first_unused;
72 room_left -= (strlen (first_unused) +1);
73 first_unused += strlen (first_unused) +1;
76 if (NISENTRYLEN (0, 2, result) + 1 > room_left)
77 goto no_more_room;
78 proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
79 p = first_unused;
81 line = p;
82 for (i = 0; i < result->objects.objects_len; ++i)
84 if (strcmp (NISENTRYVAL (i, 1, result), proto->p_name) != 0)
86 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
87 goto no_more_room;
88 *p++ = ' ';
89 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
90 NISENTRYLEN (i, 1, result));
91 *p = '\0';
92 room_left -= (NISENTRYLEN (i, 1, result) + 1);
95 *p++ = '\0';
96 first_unused = p;
98 /* Adjust the pointer so it is aligned for
99 storing pointers. */
100 first_unused += __alignof__ (char *) - 1;
101 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
102 proto->p_aliases = (char **) first_unused;
103 if (room_left < sizeof (char *))
104 goto no_more_room;
105 room_left -= sizeof (char *);
106 proto->p_aliases[0] = NULL;
108 i = 0;
109 while (*line != '\0')
111 /* Skip leading blanks. */
112 while (isspace (*line))
113 line++;
114 if (*line == '\0')
115 break;
117 if (room_left < sizeof (char *))
118 goto no_more_room;
120 room_left -= sizeof (char *);
121 proto->p_aliases[i] = line;
123 while (*line != '\0' && *line != ' ')
124 ++line;
126 if (*line == ' ')
128 *line = '\0';
129 ++line;
130 ++i;
132 else
133 proto->p_aliases[i+1] = NULL;
136 return 1;
139 static enum nss_status
140 _nss_create_tablename (int *errnop)
142 if (tablename_val == NULL)
144 char buf [40 + strlen (nis_local_directory ())];
145 char *p;
147 p = __stpcpy (buf, "protocols.org_dir.");
148 p = __stpcpy (p, nis_local_directory ());
149 tablename_val = __strdup (buf);
150 if (tablename_val == NULL)
152 *errnop = errno;
153 return NSS_STATUS_TRYAGAIN;
155 tablename_len = strlen (tablename_val);
157 return NSS_STATUS_SUCCESS;
160 enum nss_status
161 _nss_nisplus_setprotoent (int stayopen)
163 enum nss_status status = NSS_STATUS_SUCCESS;
165 __libc_lock_lock (lock);
167 if (result)
168 nis_freeresult (result);
169 result = NULL;
171 if (tablename_val == NULL)
173 int err;
174 status = _nss_create_tablename (&err);
177 __libc_lock_unlock (lock);
179 return status;
182 enum nss_status
183 _nss_nisplus_endprotoent (void)
185 __libc_lock_lock (lock);
187 if (result)
188 nis_freeresult (result);
189 result = NULL;
191 __libc_lock_unlock (lock);
193 return NSS_STATUS_SUCCESS;
196 static enum nss_status
197 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
198 size_t buflen, int *errnop)
200 int parse_res;
202 /* Get the next entry until we found a correct one. */
205 nis_result *saved_res;
207 if (result == NULL)
209 saved_res = NULL;
210 if (tablename_val == NULL)
212 enum nss_status status = _nss_create_tablename (errnop);
214 if (status != NSS_STATUS_SUCCESS)
215 return status;
218 result = nis_first_entry (tablename_val);
219 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
220 return niserr2nss (result->status);
222 else
224 nis_result *res;
226 saved_res = result;
227 res = nis_next_entry (tablename_val, &result->cookie);
228 result = res;
230 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
232 nis_freeresult (saved_res);
233 return niserr2nss (result->status);
237 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
238 buflen, errnop);
239 if (parse_res == -1)
241 nis_freeresult (result);
242 result = saved_res;
243 *errnop = ERANGE;
244 return NSS_STATUS_TRYAGAIN;
246 else
248 if (saved_res)
249 nis_freeresult (saved_res);
252 while (!parse_res);
254 return NSS_STATUS_SUCCESS;
257 enum nss_status
258 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
259 size_t buflen, int *errnop)
261 int status;
263 __libc_lock_lock (lock);
265 status = internal_nisplus_getprotoent_r (result, buffer, buflen, errnop);
267 __libc_lock_unlock (lock);
269 return status;
272 enum nss_status
273 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
274 char *buffer, size_t buflen, int *errnop)
276 int parse_res;
278 if (tablename_val == NULL)
280 enum nss_status status = _nss_create_tablename (errnop);
282 if (status != NSS_STATUS_SUCCESS)
283 return status;
286 if (name == NULL)
287 return NSS_STATUS_NOTFOUND;
288 else
290 nis_result *result;
291 char buf[strlen (name) + 255 + tablename_len];
292 int olderr = errno;
294 /* Search at first in the alias list, and use the correct name
295 for the next search */
296 sprintf (buf, "[name=%s],%s", name, tablename_val);
297 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
299 if (result != NULL)
301 /* If we do not find it, try it as original name. But if the
302 database is correct, we should find it in the first case, too */
303 if ((result->status != NIS_SUCCESS
304 && result->status != NIS_S_SUCCESS)
305 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
306 || strcmp (result->objects.objects_val->EN_data.en_type,
307 "protocols_tbl") != 0
308 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
309 sprintf (buf, "[cname=%s],%s", name, tablename_val);
310 else
311 sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
312 tablename_val);
314 nis_freeresult (result);
315 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
318 if (result == NULL)
320 __set_errno (ENOMEM);
321 return NSS_STATUS_TRYAGAIN;
323 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
325 enum nss_status status = niserr2nss (result->status);
327 __set_errno (olderr);
329 nis_freeresult (result);
330 return status;
333 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
334 errnop);
336 nis_freeresult (result);
338 if (parse_res < 1)
340 if (parse_res == -1)
342 *errnop = ERANGE;
343 return NSS_STATUS_TRYAGAIN;
345 else
347 __set_errno (olderr);
348 return NSS_STATUS_NOTFOUND;
351 return NSS_STATUS_SUCCESS;
355 enum nss_status
356 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
357 char *buffer, size_t buflen, int *errnop)
359 if (tablename_val == NULL)
361 enum nss_status status = _nss_create_tablename (errnop);
363 if (status != NSS_STATUS_SUCCESS)
364 return status;
368 int parse_res;
369 nis_result *result;
370 char buf[46 + tablename_len];
371 int olderr = errno;
373 sprintf (buf, "[number=%d],%s", number, tablename_val);
375 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
377 if (result == NULL)
379 __set_errno (ENOMEM);
380 return NSS_STATUS_TRYAGAIN;
382 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
384 enum nss_status status = niserr2nss (result->status);
386 __set_errno (olderr);
388 nis_freeresult (result);
389 return status;
392 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
393 errnop);
395 nis_freeresult (result);
397 if (parse_res < 1)
399 if (parse_res == -1)
401 *errnop = ERANGE;
402 return NSS_STATUS_TRYAGAIN;
404 else
406 __set_errno (olderr);
407 return NSS_STATUS_NOTFOUND;
410 return NSS_STATUS_SUCCESS;