Update.
[glibc.git] / nis / nss_nisplus / nisplus-service.c
blobc95507a468bf917dea856b48e194355457ff5b64
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 servent
37 #define DATABASE "services"
38 #define TRAILING_LIST_MEMBER s_aliases
39 #define TRAILING_LIST_SEPARATOR_P isspace
40 #include "../../nss/nss_files/files-parse.c"
41 #define ISSLASH(c) ((c) == '/')
42 LINE_PARSER
43 ("#",
44 STRING_FIELD (result->s_name, isspace, 1);
45 INT_FIELD (result->s_port, ISSLASH, 10, 0, htons);
46 STRING_FIELD (result->s_proto, isspace, 1);
50 #define NISENTRYVAL(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_val)
53 #define NISENTRYLEN(idx,col,res) \
54 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
56 static int
57 _nss_nisplus_parse_servent (nis_result *result, struct servent *serv,
58 char *buffer, size_t buflen)
60 char *p = buffer;
61 size_t room_left = buflen;
62 unsigned int i;
63 struct parser_data *data = (void *) buffer;
65 if (result == NULL)
66 return 0;
68 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
69 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
70 strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
71 "services_tbl") != 0 ||
72 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 4)
73 return 0;
75 memset (p, '\0', room_left);
77 /* Generate the services entry format and use the normal parser */
78 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
80 __set_errno (ERANGE);
81 return 0;
83 strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
84 room_left -= (NISENTRYLEN (0, 0, result) + 1);
86 if (NISENTRYLEN (0, 3, result) + 1 > room_left)
88 __set_errno (ERANGE);
89 return 0;
91 strcat (p, "\t");
92 strncat (p, NISENTRYVAL (0, 3, result), NISENTRYLEN (0, 3, result));
93 room_left -= (NISENTRYLEN (0, 3, result) + 1);
94 if (NISENTRYLEN (0, 2, result) + 1 > room_left)
96 __set_errno (ERANGE);
97 return 0;
99 strcat (p, "/");
100 strncat (p, NISENTRYVAL (0, 2, result), NISENTRYLEN (0, 2, result));
101 room_left -= (NISENTRYLEN (0, 2, result) + 1);
103 for (i = 1; i < result->objects.objects_len; i++)
105 if (NISENTRYLEN (i, 1, result) + 1 > room_left)
107 __set_errno (ERANGE);
108 return 0;
110 strcat (p, " ");
111 strcat (p, NISENTRYVAL (i, 1, result));
112 room_left -= (NISENTRYLEN (i, 1, result) + 1);
115 return _nss_files_parse_servent (p, serv, data, buflen);
118 enum nss_status
119 _nss_nisplus_setservent (void)
121 __libc_lock_lock (lock);
123 if (result)
124 nis_freeresult (result);
125 result = NULL;
126 if (names)
128 nis_freenames (names);
129 names = NULL;
132 __libc_lock_unlock (lock);
134 return NSS_STATUS_SUCCESS;
137 enum nss_status
138 _nss_nisplus_endservent (void)
140 __libc_lock_lock (lock);
142 if (result)
143 nis_freeresult (result);
144 result = NULL;
145 if (names)
147 nis_freenames (names);
148 names = NULL;
151 __libc_lock_unlock (lock);
153 return NSS_STATUS_SUCCESS;
156 static enum nss_status
157 internal_nisplus_getservent_r (struct servent *serv, char *buffer,
158 size_t buflen)
160 int parse_res;
162 /* Get the next entry until we found a correct one. */
165 if (result == NULL)
167 names = nis_getnames ("services.org_dir");
168 if (names == NULL || names[0] == NULL)
169 return NSS_STATUS_UNAVAIL;
171 result = nis_first_entry (names[0]);
172 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
173 return niserr2nss (result->status);
175 else
177 nis_result *res;
179 res = nis_next_entry (names[0], &result->cookie);
180 nis_freeresult (result);
181 result = res;
182 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
183 return niserr2nss (result->status);
186 parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen);
188 while (!parse_res);
190 return NSS_STATUS_SUCCESS;
193 enum nss_status
194 _nss_nisplus_getservent_r (struct servent *result, char *buffer,
195 size_t buflen)
197 int status;
199 __libc_lock_lock (lock);
201 status = internal_nisplus_getservent_r (result, buffer, buflen);
203 __libc_lock_unlock (lock);
205 return status;
208 enum nss_status
209 _nss_nisplus_getservbyname_r (const char *name, const char *protocol,
210 struct servent *serv,
211 char *buffer, size_t buflen)
213 int parse_res;
215 if (name == NULL || protocol == NULL)
217 __set_errno (EINVAL);
218 return NSS_STATUS_NOTFOUND;
220 else
222 nis_result *result;
223 char buf[strlen (name) + 255];
225 /* Search at first in the alias list, and use the correct name
226 for the next search */
227 sprintf (buf, "[name=%s,proto=%s],services.org_dir", name,
228 protocol);
229 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
231 /* If we do not find it, try it as original name. But if the
232 database is correct, we should find it in the first case, too */
233 if ((result->status != NIS_SUCCESS &&
234 result->status != NIS_S_SUCCESS) ||
235 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
236 strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
237 "services_tbl") != 0 ||
238 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 4)
239 sprintf (buf, "[cname=%s,proto=%s],services.org_dir", name, protocol);
240 else
241 sprintf (buf, "[cname=%s,proto=%s],services.org_dir",
242 NISENTRYVAL (0, 0, result), protocol);
244 nis_freeresult (result);
245 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
247 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
249 enum nss_status status = niserr2nss (result->status);
251 nis_freeresult (result);
252 return status;
255 parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen);
257 nis_freeresult (result);
259 if (parse_res)
260 return NSS_STATUS_SUCCESS;
262 if (!parse_res && errno == ERANGE)
263 return NSS_STATUS_TRYAGAIN;
264 else
265 return NSS_STATUS_NOTFOUND;
269 enum nss_status
270 _nss_nisplus_getservbynumber_r (const int number, const char *protocol,
271 struct servent *serv,
272 char *buffer, size_t buflen)
274 int parse_res;
275 nis_result *result;
276 char buf[60 + strlen (protocol)];
278 if (protocol == NULL)
280 __set_errno (EINVAL);
281 return NSS_STATUS_NOTFOUND;
284 snprintf (buf, sizeof (buf), "[number=%d,proto=%s],services.org_dir",
285 number, protocol);
287 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
289 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
291 enum nss_status status = niserr2nss (result->status);
293 nis_freeresult (result);
294 return status;
297 parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen);
299 nis_freeresult (result);
301 if (parse_res)
302 return NSS_STATUS_SUCCESS;
304 if (!parse_res && errno == ERANGE)
305 return NSS_STATUS_TRYAGAIN;
306 else
307 return NSS_STATUS_NOTFOUND;