Update.
[glibc.git] / nis / nss_nisplus / nisplus-proto.c
blob1399748846bed9505459227483326e3a00655571
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 <libc-lock.h>
26 #include <rpcsvc/nis.h>
27 #include <rpcsvc/nislib.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock)
33 static nis_result *result = NULL;
34 static nis_name *names = NULL;
36 #define ENTNAME protoent
37 #define DATABASE "protocols"
38 #define TRAILING_LIST_MEMBER p_aliases
39 #define TRAILING_LIST_SEPARATOR_P isspace
40 #include "../../nss/nss_files/files-parse.c"
41 LINE_PARSER
42 ("#",
43 STRING_FIELD (result->p_name, isspace, 1);
44 INT_FIELD (result->p_proto, isspace, 1, 10,);
47 #define NISENTRYVAL(idx,col,res) \
48 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
50 #define NISENTRYLEN(idx,col,res) \
51 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
53 static int
54 _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
55 char *buffer, size_t buflen)
57 char *p = buffer;
58 size_t room_left = buflen;
59 unsigned int i;
60 struct parser_data *data = (void *) buffer;
62 if (result == NULL)
63 return 0;
65 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
66 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
67 strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
68 "protocols_tbl") != 0 ||
69 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3)
70 return 0;
72 memset (p, '\0', room_left);
74 /* Generate the protocols entry format and use the normal parser */
75 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
77 __set_errno (ERANGE);
78 return 0;
80 strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
81 room_left -= (NISENTRYLEN (0, 0, result) + 1);
83 if (NISENTRYLEN (0, 2, result) + 1 > room_left)
85 __set_errno (ERANGE);
86 return 0;
88 strcat (p, "\t");
89 strncat (p, NISENTRYVAL (0, 2, result), NISENTRYLEN (0, 2, result));
90 room_left -= (NISENTRYLEN (0, 2, result) + 1);
91 /* + 1: We overwrite the last \0 */
93 for (i = 1; i < result->objects.objects_len; i++)
95 if (NISENTRYLEN (i, 1, result) + 1 > room_left)
97 __set_errno (ERANGE);
98 return 0;
100 strcat (p, " ");
101 strncat (p, NISENTRYVAL (i, 1, result), NISENTRYLEN (i, 1, result));
102 room_left -= (NISENTRYLEN (i, 1, result) + 1);
105 return _nss_files_parse_protoent (p, proto, data, buflen);
108 enum nss_status
109 _nss_nisplus_setprotoent (void)
111 __libc_lock_lock (lock);
113 if (result)
114 nis_freeresult (result);
115 result = NULL;
116 if (names)
118 nis_freenames (names);
119 names = NULL;
122 __libc_lock_unlock (lock);
124 return NSS_STATUS_SUCCESS;
127 enum nss_status
128 _nss_nisplus_endprotoent (void)
130 __libc_lock_lock (lock);
132 if (result)
133 nis_freeresult (result);
134 result = NULL;
135 if (names)
137 nis_freenames (names);
138 names = NULL;
141 __libc_lock_unlock (lock);
143 return NSS_STATUS_SUCCESS;
146 static enum nss_status
147 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
148 size_t buflen)
150 int parse_res;
152 /* Get the next entry until we found a correct one. */
155 if (result == NULL)
157 names = nis_getnames ("protocols.org_dir");
158 if (names == NULL || names[0] == NULL)
159 return NSS_STATUS_UNAVAIL;
161 result = nis_first_entry (names[0]);
162 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
163 return niserr2nss (result->status);
165 else
167 nis_result *res;
169 res = nis_next_entry (names[0], &result->cookie);
170 nis_freeresult (result);
171 result = res;
173 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
174 return niserr2nss (result->status);
177 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
179 while (!parse_res);
181 return NSS_STATUS_SUCCESS;
184 enum nss_status
185 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
186 size_t buflen)
188 int status;
190 __libc_lock_lock (lock);
192 status = internal_nisplus_getprotoent_r (result, buffer, buflen);
194 __libc_lock_unlock (lock);
196 return status;
199 enum nss_status
200 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
201 char *buffer, size_t buflen)
203 int parse_res;
205 if (name == NULL)
206 return NSS_STATUS_NOTFOUND;
207 else
209 nis_result *result;
210 char buf[strlen (name) + 255];
212 /* Search at first in the alias list, and use the correct name
213 for the next search */
214 sprintf (buf, "[name=%s],protocols.org_dir", name);
215 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
217 /* If we do not find it, try it as original name. But if the
218 database is correct, we should find it in the first case, too */
219 if ((result->status != NIS_SUCCESS &&
220 result->status != NIS_S_SUCCESS) ||
221 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
222 strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
223 "protocols_tbl") != 0 ||
224 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3)
225 sprintf (buf, "[cname=%s],protocols.org_dir", name);
226 else
227 sprintf (buf, "[cname=%s],protocols.org_dir", NISENTRYVAL (0, 0, result));
229 nis_freeresult (result);
230 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
232 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
234 enum nss_status status = niserr2nss (result->status);
236 nis_freeresult (result);
237 return status;
240 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
242 nis_freeresult (result);
244 if (parse_res)
245 return NSS_STATUS_SUCCESS;
247 if (!parse_res && errno == ERANGE)
248 return NSS_STATUS_TRYAGAIN;
249 else
250 return NSS_STATUS_NOTFOUND;
254 enum nss_status
255 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
256 char *buffer, size_t buflen)
258 int parse_res;
259 nis_result *result;
260 char buf[46];
262 snprintf (buf, sizeof (buf), "[number=%d],protocols.org_dir", number);
264 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
266 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
268 enum nss_status status = niserr2nss (result->status);
270 nis_freeresult (result);
271 return status;
274 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen);
276 nis_freeresult (result);
277 if (parse_res)
278 return NSS_STATUS_SUCCESS;
280 if (!parse_res && errno == ERANGE)
281 return NSS_STATUS_TRYAGAIN;
282 else
283 return NSS_STATUS_NOTFOUND;