Update.
[glibc.git] / nis / nss_nisplus / nisplus-proto.c
blob2cbd9fe2fa6fc35b0ef67a780483ed3016895f79
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 <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 = NULL;
33 static nis_name tablename_val = NULL;
34 static u_long tablename_len = 0;
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)
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, "protocols_tbl") != 0
57 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
58 return 0;
60 /* Generate the protocols entry format and use the normal parser */
61 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
63 no_more_room:
64 __set_errno (ERANGE);
65 return -1;
67 strncpy (first_unused, NISENTRYVAL (0, 0, result),
68 NISENTRYLEN (0, 0, result));
69 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
70 proto->p_name = first_unused;
71 room_left -= (strlen (first_unused) +1);
72 first_unused += strlen (first_unused) +1;
75 if (NISENTRYLEN (0, 2, result) + 1 > room_left)
76 goto no_more_room;
77 proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
78 p = first_unused;
80 line = p;
81 for (i = 0; i < result->objects.objects_len; i++)
83 if (strcmp (NISENTRYVAL (i, 1, result), proto->p_name) != 0)
85 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
86 goto no_more_room;
87 *p++ = ' ';
88 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
89 NISENTRYLEN (i, 1, result));
90 *p = '\0';
91 room_left -= (NISENTRYLEN (i, 1, result) + 1);
94 ++p;
95 first_unused = p;
97 /* Adjust the pointer so it is aligned for
98 storing pointers. */
99 first_unused += __alignof__ (char *) - 1;
100 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
101 proto->p_aliases = (char **) first_unused;
102 if (room_left < sizeof (char *))
103 goto no_more_room;
104 room_left -= (sizeof (char *));
105 proto->p_aliases[0] = NULL;
107 i = 0;
108 while (*line != '\0')
110 /* Skip leading blanks. */
111 while (isspace (*line))
112 line++;
113 if (*line == '\0')
114 break;
116 if (room_left < sizeof (char *))
117 goto no_more_room;
119 room_left -= sizeof (char *);
120 proto->p_aliases[i] = line;
122 while (*line != '\0' && *line != ' ')
123 ++line;
125 if (*line == ' ')
127 *line = '\0';
128 ++line;
129 ++i;
131 else
132 proto->p_aliases[i+1] = NULL;
135 return 1;
138 static enum nss_status
139 _nss_create_tablename (void)
141 if (tablename_val == NULL)
143 char buf [40 + strlen (nis_local_directory ())];
144 char *p;
146 p = __stpcpy (buf, "protocols.org_dir.");
147 p = __stpcpy (p, nis_local_directory ());
148 tablename_val = __strdup (buf);
149 if (tablename_val == NULL)
150 return NSS_STATUS_TRYAGAIN;
151 tablename_len = strlen (tablename_val);
153 return NSS_STATUS_SUCCESS;
156 enum nss_status
157 _nss_nisplus_setprotoent (void)
159 enum nss_status status = NSS_STATUS_SUCCESS;
161 __libc_lock_lock (lock);
163 if (result)
164 nis_freeresult (result);
165 result = NULL;
167 if (tablename_val == NULL)
168 status = _nss_create_tablename ();
170 __libc_lock_unlock (lock);
172 return status;
175 enum nss_status
176 _nss_nisplus_endprotoent (void)
178 __libc_lock_lock (lock);
180 if (result)
181 nis_freeresult (result);
182 result = NULL;
184 __libc_lock_unlock (lock);
186 return NSS_STATUS_SUCCESS;
189 static enum nss_status
190 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
191 size_t buflen)
193 int parse_res;
195 /* Get the next entry until we found a correct one. */
198 nis_result *saved_res;
200 if (result == NULL)
202 saved_res = NULL;
203 if (tablename_val == NULL)
204 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
205 return NSS_STATUS_UNAVAIL;
207 result = nis_first_entry (tablename_val);
208 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
209 return niserr2nss (result->status);
211 else
213 nis_result *res;
215 saved_res = result;
216 res = nis_next_entry (tablename_val, &result->cookie);
217 result = res;
219 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
221 nis_freeresult (saved_res);
222 return niserr2nss (result->status);
226 if ((parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
227 buflen)) == -1)
229 nis_freeresult (result);
230 result = saved_res;
231 return NSS_STATUS_TRYAGAIN;
233 else
235 if (saved_res)
236 nis_freeresult (saved_res);
239 while (!parse_res);
241 return NSS_STATUS_SUCCESS;
244 enum nss_status
245 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
246 size_t buflen)
248 int status;
250 __libc_lock_lock (lock);
252 status = internal_nisplus_getprotoent_r (result, buffer, buflen);
254 __libc_lock_unlock (lock);
256 return status;
259 enum nss_status
260 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
261 char *buffer, size_t buflen)
263 int parse_res;
265 if (tablename_val == NULL)
266 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
267 return NSS_STATUS_UNAVAIL;
269 if (name == NULL)
270 return NSS_STATUS_NOTFOUND;
271 else
273 nis_result *result;
274 char buf[strlen (name) + 255 + tablename_len];
276 /* Search at first in the alias list, and use the correct name
277 for the next search */
278 sprintf (buf, "[name=%s],%s", name, tablename_val);
279 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
281 /* If we do not find it, try it as original name. But if the
282 database is correct, we should find it in the first case, too */
283 if ((result->status != NIS_SUCCESS &&
284 result->status != NIS_S_SUCCESS) ||
285 __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
286 strcmp (result->objects.objects_val->EN_data.en_type,
287 "protocols_tbl") != 0 ||
288 result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
289 sprintf (buf, "[cname=%s],%s", name, tablename_val);
290 else
291 sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
292 tablename_val);
294 nis_freeresult (result);
295 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
297 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
299 enum nss_status status = niserr2nss (result->status);
301 nis_freeresult (result);
302 return status;
305 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
307 nis_freeresult (result);
309 if (parse_res == -1)
310 return NSS_STATUS_TRYAGAIN;
311 if (parse_res)
312 return NSS_STATUS_SUCCESS;
314 return NSS_STATUS_NOTFOUND;
318 enum nss_status
319 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
320 char *buffer, size_t buflen)
322 if (tablename_val == NULL)
323 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
324 return NSS_STATUS_UNAVAIL;
326 int parse_res;
327 nis_result *result;
328 char buf[46 + tablename_len];
330 sprintf (buf, "[number=%d],%s", number, tablename_val);
332 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
334 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
336 enum nss_status status = niserr2nss (result->status);
338 nis_freeresult (result);
339 return status;
342 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
344 nis_freeresult (result);
346 if (parse_res == -1)
347 return NSS_STATUS_TRYAGAIN;
349 if (parse_res)
350 return NSS_STATUS_SUCCESS;
352 return NSS_STATUS_NOTFOUND;