Update.
[glibc.git] / nis / nss_nisplus / nisplus-rpc.c
blob435da5792ce8dad4133b880e4fd21562fb7c7cd6
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 <string.h>
24 #include <libc-lock.h>
25 #include <rpc/netdb.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 rpcent
37 #define DATABASE "rpc"
38 #define TRAILING_LIST_MEMBER r_aliases
39 #define TRAILING_LIST_SEPARATOR_P isspace
40 #include "../../nss/nss_files/files-parse.c"
41 LINE_PARSER
42 ("#",
43 STRING_FIELD (result->r_name, isspace, 1);
44 INT_FIELD (result->r_number, 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_rpcent (nis_result *result, struct rpcent *rpc,
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 "rpc_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 rpc 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 = 0; i < result->objects.objects_len; i++)
94 /* XXX should we start with i = 0 or with i = 1 ? */
96 if (NISENTRYLEN (i, 1, result) +1 > room_left)
98 __set_errno (ERANGE);
99 return 0;
101 strcat (p, " ");
102 strncat (p, NISENTRYVAL (i, 1, result), NISENTRYLEN (i, 1, result));
103 room_left -= (NISENTRYLEN (i, 1, result) + 1);
106 return _nss_files_parse_rpcent (p, rpc, data, buflen);
109 enum nss_status
110 _nss_nisplus_setrpcent (void)
112 __libc_lock_lock (lock);
114 if (result)
115 nis_freeresult (result);
116 result = NULL;
117 if (names)
119 nis_freenames (names);
120 names = NULL;
123 __libc_lock_unlock (lock);
125 return NSS_STATUS_SUCCESS;
128 enum nss_status
129 _nss_nisplus_endrpcent (void)
131 __libc_lock_lock (lock);
133 if (result)
134 nis_freeresult (result);
135 result = NULL;
136 if (names)
138 nis_freenames (names);
139 names = NULL;
142 __libc_lock_unlock (lock);
144 return NSS_STATUS_SUCCESS;
147 static enum nss_status
148 internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer,
149 size_t buflen)
151 int parse_res;
153 /* Get the next entry until we found a correct one. */
156 if (result == NULL)
158 names = nis_getnames ("rpc.org_dir");
159 if (names == NULL || names[0] == NULL)
160 return NSS_STATUS_UNAVAIL;
162 result = nis_first_entry(names[0]);
163 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
164 return niserr2nss (result->status);
166 else
168 nis_result *res;
170 res = nis_next_entry (names[0], &result->cookie);
171 nis_freeresult (result);
172 result = res;
173 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
174 return niserr2nss (result->status);
177 parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen);
178 } while (!parse_res);
180 return NSS_STATUS_SUCCESS;
183 enum nss_status
184 _nss_nisplus_getrpcent_r (struct rpcent *result, char *buffer,
185 size_t buflen)
187 int status;
189 __libc_lock_lock (lock);
191 status = internal_nisplus_getrpcent_r (result, buffer, buflen);
193 __libc_lock_unlock (lock);
195 return status;
198 enum nss_status
199 _nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc,
200 char *buffer, size_t buflen)
202 int parse_res;
204 if (name == NULL)
205 return NSS_STATUS_NOTFOUND;
206 else
208 nis_result *result;
209 char buf[strlen (name) + 255];
211 /* Search at first in the alias list, and use the correct name
212 for the next search */
213 sprintf (buf, "[name=%s],rpc.org_dir", name);
214 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
216 /* If we do not find it, try it as original name. But if the
217 database is correct, we should find it in the first case, too */
218 if ((result->status != NIS_SUCCESS &&
219 result->status != NIS_S_SUCCESS) ||
220 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
221 strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
222 "rpc_tbl") != 0 ||
223 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3)
224 sprintf (buf, "[cname=%s],rpc.org_dir", name);
225 else
226 sprintf (buf, "[cname=%s],rpc.org_dir", NISENTRYVAL(0, 0, result));
228 nis_freeresult (result);
229 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
231 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
233 enum nss_status status = niserr2nss (result->status);
235 nis_freeresult (result);
236 return status;
239 parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen);
241 nis_freeresult (result);
243 if (parse_res)
244 return NSS_STATUS_SUCCESS;
246 if (!parse_res && errno == ERANGE)
247 return NSS_STATUS_TRYAGAIN;
248 else
249 return NSS_STATUS_NOTFOUND;
253 enum nss_status
254 _nss_nisplus_getrpcbynumber_r (const int number, struct rpcent *rpc,
255 char *buffer, size_t buflen)
257 int parse_res;
258 nis_result *result;
259 char buf[100];
261 snprintf (buf, sizeof (buf), "[number=%d],rpc.org_dir", number);
263 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
265 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
267 enum nss_status status = niserr2nss (result->status);
269 nis_freeresult (result);
270 return status;
273 parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen);
275 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;