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. */
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)
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;
54 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
55 __type_of (NIS_RES_OBJECT (result)) != 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)
60 /* Generate the protocols entry format and use the normal parser */
61 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
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)
77 proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
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)
88 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
89 NISENTRYLEN (i, 1, result));
91 room_left -= (NISENTRYLEN (i, 1, result) + 1);
97 /* Adjust the pointer so it is aligned for
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 *))
104 room_left -= (sizeof (char *));
105 proto->p_aliases[0] = NULL;
108 while (*line != '\0')
110 /* Skip leading blanks. */
111 while (isspace (*line))
116 if (room_left < sizeof (char *))
119 room_left -= sizeof (char *);
120 proto->p_aliases[i] = line;
122 while (*line != '\0' && *line != ' ')
132 proto->p_aliases[i+1] = NULL;
138 static enum nss_status
139 _nss_create_tablename (void)
141 if (tablename_val == NULL)
143 char buf [40 + strlen (nis_local_directory ())];
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;
157 _nss_nisplus_setprotoent (void)
159 enum nss_status status = NSS_STATUS_SUCCESS;
161 __libc_lock_lock (lock);
164 nis_freeresult (result);
167 if (tablename_val == NULL)
168 status = _nss_create_tablename ();
170 __libc_lock_unlock (lock);
176 _nss_nisplus_endprotoent (void)
178 __libc_lock_lock (lock);
181 nis_freeresult (result);
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,
195 /* Get the next entry until we found a correct one. */
198 nis_result *saved_res;
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);
216 res = nis_next_entry (tablename_val, &result->cookie);
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,
229 nis_freeresult (result);
231 return NSS_STATUS_TRYAGAIN;
236 nis_freeresult (saved_res);
241 return NSS_STATUS_SUCCESS;
245 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
250 __libc_lock_lock (lock);
252 status = internal_nisplus_getprotoent_r (result, buffer, buflen);
254 __libc_lock_unlock (lock);
260 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
261 char *buffer, size_t buflen)
265 if (tablename_val == NULL)
266 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
267 return NSS_STATUS_UNAVAIL;
270 return NSS_STATUS_NOTFOUND;
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) != 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);
291 sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
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);
305 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
307 nis_freeresult (result);
310 return NSS_STATUS_TRYAGAIN;
312 return NSS_STATUS_SUCCESS;
314 return NSS_STATUS_NOTFOUND;
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;
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);
342 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
344 nis_freeresult (result);
347 return NSS_STATUS_TRYAGAIN;
350 return NSS_STATUS_SUCCESS;
352 return NSS_STATUS_NOTFOUND;