]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-proto.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-proto.c
1 /* Copyright (C) 1997, 1998, 2001, 2002 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.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
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>
27
28 #include "nss-nisplus.h"
29
30 __libc_lock_define_initialized (static, lock)
31
32 static nis_result *result;
33 static nis_name tablename_val;
34 static u_long tablename_len;
35
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)
38
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)
41
42 static int
43 _nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto,
44 char *buffer, size_t buflen, int *errnop)
45 {
46 char *first_unused = buffer;
47 size_t room_left = buflen;
48 unsigned int i;
49 char *p, *line;
50
51 if (result == NULL)
52 return 0;
53
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,
57 "protocols_tbl") != 0
58 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 3)
59 return 0;
60
61 /* Generate the protocols entry format and use the normal parser */
62 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
63 {
64 no_more_room:
65 *errnop = ERANGE;
66 return -1;
67 }
68 strncpy (first_unused, NISENTRYVAL (0, 0, result),
69 NISENTRYLEN (0, 0, result));
70 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
71 proto->p_name = first_unused;
72 room_left -= (strlen (first_unused) +1);
73 first_unused += strlen (first_unused) +1;
74
75
76 if (NISENTRYLEN (0, 2, result) + 1 > room_left)
77 goto no_more_room;
78 proto->p_proto = atoi (NISENTRYVAL (0, 2, result));
79 p = first_unused;
80
81 line = p;
82 for (i = 0; i < result->objects.objects_len; ++i)
83 {
84 if (strcmp (NISENTRYVAL (i, 1, result), proto->p_name) != 0)
85 {
86 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
87 goto no_more_room;
88 *p++ = ' ';
89 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
90 NISENTRYLEN (i, 1, result));
91 *p = '\0';
92 room_left -= (NISENTRYLEN (i, 1, result) + 1);
93 }
94 }
95 *p++ = '\0';
96 first_unused = p;
97
98 /* Adjust the pointer so it is aligned for
99 storing pointers. */
100 first_unused += __alignof__ (char *) - 1;
101 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
102 proto->p_aliases = (char **) first_unused;
103 if (room_left < sizeof (char *))
104 goto no_more_room;
105 room_left -= sizeof (char *);
106 proto->p_aliases[0] = NULL;
107
108 i = 0;
109 while (*line != '\0')
110 {
111 /* Skip leading blanks. */
112 while (isspace (*line))
113 line++;
114 if (*line == '\0')
115 break;
116
117 if (room_left < sizeof (char *))
118 goto no_more_room;
119
120 room_left -= sizeof (char *);
121 proto->p_aliases[i] = line;
122
123 while (*line != '\0' && *line != ' ')
124 ++line;
125
126 if (*line == ' ')
127 {
128 *line = '\0';
129 ++line;
130 ++i;
131 }
132 else
133 proto->p_aliases[i+1] = NULL;
134 }
135
136 return 1;
137 }
138
139 static enum nss_status
140 _nss_create_tablename (int *errnop)
141 {
142 if (tablename_val == NULL)
143 {
144 char buf [40 + strlen (nis_local_directory ())];
145 char *p;
146
147 p = __stpcpy (buf, "protocols.org_dir.");
148 p = __stpcpy (p, nis_local_directory ());
149 tablename_val = __strdup (buf);
150 if (tablename_val == NULL)
151 {
152 *errnop = errno;
153 return NSS_STATUS_TRYAGAIN;
154 }
155 tablename_len = strlen (tablename_val);
156 }
157 return NSS_STATUS_SUCCESS;
158 }
159
160 enum nss_status
161 _nss_nisplus_setprotoent (int stayopen)
162 {
163 enum nss_status status = NSS_STATUS_SUCCESS;
164
165 __libc_lock_lock (lock);
166
167 if (result)
168 nis_freeresult (result);
169 result = NULL;
170
171 if (tablename_val == NULL)
172 {
173 int err;
174 status = _nss_create_tablename (&err);
175 }
176
177 __libc_lock_unlock (lock);
178
179 return status;
180 }
181
182 enum nss_status
183 _nss_nisplus_endprotoent (void)
184 {
185 __libc_lock_lock (lock);
186
187 if (result)
188 nis_freeresult (result);
189 result = NULL;
190
191 __libc_lock_unlock (lock);
192
193 return NSS_STATUS_SUCCESS;
194 }
195
196 static enum nss_status
197 internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer,
198 size_t buflen, int *errnop)
199 {
200 int parse_res;
201
202 /* Get the next entry until we found a correct one. */
203 do
204 {
205 nis_result *saved_res;
206
207 if (result == NULL)
208 {
209 saved_res = NULL;
210 if (tablename_val == NULL)
211 {
212 enum nss_status status = _nss_create_tablename (errnop);
213
214 if (status != NSS_STATUS_SUCCESS)
215 return status;
216 }
217
218 result = nis_first_entry (tablename_val);
219 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
220 return niserr2nss (result->status);
221 }
222 else
223 {
224 nis_result *res;
225
226 saved_res = result;
227 res = nis_next_entry (tablename_val, &result->cookie);
228 result = res;
229
230 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
231 {
232 nis_freeresult (saved_res);
233 return niserr2nss (result->status);
234 }
235 }
236
237 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer,
238 buflen, errnop);
239 if (parse_res == -1)
240 {
241 nis_freeresult (result);
242 result = saved_res;
243 *errnop = ERANGE;
244 return NSS_STATUS_TRYAGAIN;
245 }
246 else
247 {
248 if (saved_res)
249 nis_freeresult (saved_res);
250 }
251 }
252 while (!parse_res);
253
254 return NSS_STATUS_SUCCESS;
255 }
256
257 enum nss_status
258 _nss_nisplus_getprotoent_r (struct protoent *result, char *buffer,
259 size_t buflen, int *errnop)
260 {
261 int status;
262
263 __libc_lock_lock (lock);
264
265 status = internal_nisplus_getprotoent_r (result, buffer, buflen, errnop);
266
267 __libc_lock_unlock (lock);
268
269 return status;
270 }
271
272 enum nss_status
273 _nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto,
274 char *buffer, size_t buflen, int *errnop)
275 {
276 int parse_res;
277
278 if (tablename_val == NULL)
279 {
280 enum nss_status status = _nss_create_tablename (errnop);
281
282 if (status != NSS_STATUS_SUCCESS)
283 return status;
284 }
285
286 if (name == NULL)
287 return NSS_STATUS_NOTFOUND;
288 else
289 {
290 nis_result *result;
291 char buf[strlen (name) + 255 + tablename_len];
292 int olderr = errno;
293
294 /* Search at first in the alias list, and use the correct name
295 for the next search */
296 sprintf (buf, "[name=%s],%s", name, tablename_val);
297 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
298
299 /* If we do not find it, try it as original name. But if the
300 database is correct, we should find it in the first case, too */
301 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
302 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
303 || strcmp (result->objects.objects_val->EN_data.en_type,
304 "protocols_tbl") != 0
305 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
306 sprintf (buf, "[cname=%s],%s", name, tablename_val);
307 else
308 sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
309 tablename_val);
310
311 nis_freeresult (result);
312 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
313
314 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
315 {
316 enum nss_status status = niserr2nss (result->status);
317
318 __set_errno (olderr);
319
320 nis_freeresult (result);
321 return status;
322 }
323
324 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
325 errnop);
326
327 nis_freeresult (result);
328
329 if (parse_res < 1)
330 {
331 if (parse_res == -1)
332 {
333 *errnop = ERANGE;
334 return NSS_STATUS_TRYAGAIN;
335 }
336 else
337 {
338 __set_errno (olderr);
339 return NSS_STATUS_NOTFOUND;
340 }
341 }
342 return NSS_STATUS_SUCCESS;
343 }
344 }
345
346 enum nss_status
347 _nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto,
348 char *buffer, size_t buflen, int *errnop)
349 {
350 if (tablename_val == NULL)
351 {
352 enum nss_status status = _nss_create_tablename (errnop);
353
354 if (status != NSS_STATUS_SUCCESS)
355 return status;
356 }
357
358 {
359 int parse_res;
360 nis_result *result;
361 char buf[46 + tablename_len];
362 int olderr = errno;
363
364 sprintf (buf, "[number=%d],%s", number, tablename_val);
365
366 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
367
368 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
369 {
370 enum nss_status status = niserr2nss (result->status);
371
372 __set_errno (olderr);
373
374 nis_freeresult (result);
375 return status;
376 }
377
378 parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen,
379 errnop);
380
381 nis_freeresult (result);
382
383 if (parse_res < 1)
384 {
385 if (parse_res == -1)
386 {
387 *errnop = ERANGE;
388 return NSS_STATUS_TRYAGAIN;
389 }
390 else
391 {
392 __set_errno (olderr);
393 return NSS_STATUS_NOTFOUND;
394 }
395 }
396 return NSS_STATUS_SUCCESS;
397 }
398 }