]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-service.c
.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-service.c
1 /* Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003, 2005, 2006, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21 #include <atomic.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <nss.h>
26 #include <string.h>
27 #include <rpcsvc/nis.h>
28 #include <bits/libc-lock.h>
29
30 #include "nss-nisplus.h"
31
32 __libc_lock_define_initialized (static, lock);
33
34 static nis_result *result;
35 static nis_name tablename_val;
36 static u_long tablename_len;
37
38 #define NISENTRYVAL(idx, col, res) \
39 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
40
41 #define NISENTRYLEN(idx, col, res) \
42 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
43
44
45 static int
46 _nss_nisplus_parse_servent (nis_result *result, struct servent *serv,
47 char *buffer, size_t buflen, int *errnop)
48 {
49 char *first_unused = buffer;
50 size_t room_left = buflen;
51
52 if (result == NULL)
53 return 0;
54
55 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
56 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
57 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type, "services_tbl") != 0
58 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 4)
59 return 0;
60
61 if (NISENTRYLEN (0, 0, result) >= room_left)
62 {
63 no_more_room:
64 *errnop = ERANGE;
65 return -1;
66 }
67 strncpy (first_unused, NISENTRYVAL (0, 0, result),
68 NISENTRYLEN (0, 0, result));
69 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
70 serv->s_name = first_unused;
71 size_t len = strlen (first_unused) + 1;
72 room_left -= len;
73 first_unused += len;
74
75 if (NISENTRYLEN (0, 2, result) >= room_left)
76 goto no_more_room;
77 strncpy (first_unused, NISENTRYVAL (0, 2, result),
78 NISENTRYLEN (0, 2, result));
79 first_unused[NISENTRYLEN (0, 2, result)] = '\0';
80 serv->s_proto = first_unused;
81 len = strlen (first_unused) + 1;
82 room_left -= len;
83 first_unused += len;
84
85 serv->s_port = htons (atoi (NISENTRYVAL (0, 3, result)));
86
87 /* XXX Rewrite at some point to allocate the array first and then
88 copy the strings. It wasteful to first concatenate the strings
89 to just split them again later. */
90 char *line = first_unused;
91 for (unsigned int i = 0; i < NIS_RES_NUMOBJ (result); ++i)
92 {
93 if (strcmp (NISENTRYVAL (i, 1, result), serv->s_name) != 0)
94 {
95 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
96 goto no_more_room;
97 *first_unused++ = ' ';
98 first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
99 NISENTRYLEN (i, 1, result));
100 room_left -= NISENTRYLEN (i, 1, result) + 1;
101 }
102 }
103 *first_unused++ = '\0';
104
105 /* Adjust the pointer so it is aligned for
106 storing pointers. */
107 size_t adjust = ((__alignof__ (char *)
108 - (first_unused - (char *) 0) % __alignof__ (char *))
109 % __alignof__ (char *));
110 if (room_left < adjust + sizeof (char *))
111 goto no_more_room;
112 first_unused += adjust;
113 room_left -= adjust;
114 serv->s_aliases = (char **) first_unused;
115
116 /* For the terminating NULL pointer. */
117 room_left -= (sizeof (char *));
118
119 unsigned int i = 0;
120 while (*line != '\0')
121 {
122 /* Skip leading blanks. */
123 while (isspace (*line))
124 ++line;
125
126 if (*line == '\0')
127 break;
128
129 if (room_left < sizeof (char *))
130 goto no_more_room;
131
132 room_left -= sizeof (char *);
133 serv->s_aliases[i++] = line;
134
135 while (*line != '\0' && *line != ' ')
136 ++line;
137
138 if (*line == ' ')
139 *line++ = '\0';
140 }
141 serv->s_aliases[i] = NULL;
142
143 return 1;
144 }
145
146
147 static enum nss_status
148 _nss_create_tablename (int *errnop)
149 {
150 if (tablename_val == NULL)
151 {
152 const char *local_dir = nis_local_directory ();
153 size_t local_dir_len = strlen (local_dir);
154 static const char prefix[] = "services.org_dir.";
155
156 char *p = malloc (sizeof (prefix) + local_dir_len);
157 if (p == NULL)
158 {
159 *errnop = errno;
160 return NSS_STATUS_TRYAGAIN;
161 }
162
163 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
164
165 tablename_len = sizeof (prefix) - 1 + local_dir_len;
166
167 atomic_write_barrier ();
168
169 tablename_val = p;
170 }
171
172 return NSS_STATUS_SUCCESS;
173 }
174
175
176 enum nss_status
177 _nss_nisplus_setservent (int stayopen)
178 {
179 enum nss_status status = NSS_STATUS_SUCCESS;
180 int err;
181
182 __libc_lock_lock (lock);
183
184 if (result != NULL)
185 {
186 nis_freeresult (result);
187 result = NULL;
188 }
189
190 if (tablename_val == NULL)
191 status = _nss_create_tablename (&err);
192
193 __libc_lock_unlock (lock);
194
195 return status;
196 }
197
198 enum nss_status
199 _nss_nisplus_endservent (void)
200 {
201 __libc_lock_lock (lock);
202
203 if (result != NULL)
204 {
205 nis_freeresult (result);
206 result = NULL;
207 }
208
209 __libc_lock_unlock (lock);
210
211 return NSS_STATUS_SUCCESS;
212 }
213
214 static enum nss_status
215 internal_nisplus_getservent_r (struct servent *serv, char *buffer,
216 size_t buflen, int *errnop)
217 {
218 int parse_res;
219
220 /* Get the next entry until we found a correct one. */
221 do
222 {
223 nis_result *saved_res;
224
225 if (result == NULL)
226 {
227 saved_res = NULL;
228 if (tablename_val == NULL)
229 {
230 enum nss_status status = _nss_create_tablename (errnop);
231
232 if (status != NSS_STATUS_SUCCESS)
233 return status;
234 }
235
236 result = nis_first_entry (tablename_val);
237 if (result == NULL)
238 {
239 *errnop = errno;
240 return NSS_STATUS_TRYAGAIN;
241 }
242 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
243 return niserr2nss (result->status);
244 }
245 else
246 {
247 saved_res = result;
248 result = nis_next_entry (tablename_val, &result->cookie);
249 if (result == NULL)
250 {
251 *errnop = errno;
252 return NSS_STATUS_TRYAGAIN;
253 }
254 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
255 {
256 nis_freeresult (saved_res);
257 return niserr2nss (result->status);
258 }
259 }
260
261 parse_res = _nss_nisplus_parse_servent (result, serv, buffer,
262 buflen, errnop);
263 if (__builtin_expect (parse_res == -1, 0))
264 {
265 nis_freeresult (result);
266 result = saved_res;
267 *errnop = ERANGE;
268 return NSS_STATUS_TRYAGAIN;
269 }
270 else
271 {
272 if (saved_res)
273 nis_freeresult (saved_res);
274 }
275 }
276 while (!parse_res);
277
278 return NSS_STATUS_SUCCESS;
279 }
280
281 enum nss_status
282 _nss_nisplus_getservent_r (struct servent *result, char *buffer,
283 size_t buflen, int *errnop)
284 {
285 __libc_lock_lock (lock);
286
287 int status = internal_nisplus_getservent_r (result, buffer, buflen, errnop);
288
289 __libc_lock_unlock (lock);
290
291 return status;
292 }
293
294 enum nss_status
295 _nss_nisplus_getservbyname_r (const char *name, const char *protocol,
296 struct servent *serv,
297 char *buffer, size_t buflen, int *errnop)
298 {
299 if (tablename_val == NULL)
300 {
301 __libc_lock_lock (lock);
302
303 enum nss_status status = _nss_create_tablename (errnop);
304
305 __libc_lock_unlock (lock);
306
307 if (status != NSS_STATUS_SUCCESS)
308 return status;
309 }
310
311 if (name == NULL || protocol == NULL)
312 {
313 *errnop = EINVAL;
314 return NSS_STATUS_NOTFOUND;
315 }
316
317 size_t protocol_len = strlen (protocol);
318 char buf[strlen (name) + protocol_len + 17 + tablename_len];
319 int olderr = errno;
320
321 /* Search at first in the alias list, and use the correct name
322 for the next search */
323 snprintf (buf, sizeof (buf), "[name=%s,proto=%s],%s", name, protocol,
324 tablename_val);
325 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
326 NULL, NULL);
327
328 if (result != NULL)
329 {
330 char *bufptr = buf;
331
332 /* If we did not find it, try it as original name. But if the
333 database is correct, we should find it in the first case, too */
334 if ((result->status != NIS_SUCCESS
335 && result->status != NIS_S_SUCCESS)
336 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
337 || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
338 "services_tbl") != 0
339 || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 4)
340 snprintf (buf, sizeof (buf), "[cname=%s,proto=%s],%s", name, protocol,
341 tablename_val);
342 else
343 {
344 /* We need to allocate a new buffer since there is no
345 guarantee the returned name has a length limit. */
346 const char *entryval = NISENTRYVAL(0, 0, result);
347 size_t buflen = (strlen (entryval) + protocol_len + 17
348 + tablename_len);
349 bufptr = alloca (buflen);
350 snprintf (bufptr, buflen, "[cname=%s,proto=%s],%s",
351 entryval, protocol, tablename_val);
352 }
353
354 nis_freeresult (result);
355 result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
356 NULL, NULL);
357 }
358
359 if (result == NULL)
360 {
361 *errnop = ENOMEM;
362 return NSS_STATUS_TRYAGAIN;
363 }
364
365 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
366 {
367 enum nss_status status = niserr2nss (result->status);
368
369 __set_errno (olderr);
370
371 nis_freeresult (result);
372 return status;
373 }
374
375 int parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
376 errnop);
377 nis_freeresult (result);
378
379 if (__builtin_expect (parse_res < 1, 0))
380 {
381 if (parse_res == -1)
382 {
383 *errnop = ERANGE;
384 return NSS_STATUS_TRYAGAIN;
385 }
386 else
387 {
388 __set_errno (olderr);
389 return NSS_STATUS_NOTFOUND;
390 }
391 }
392
393 return NSS_STATUS_SUCCESS;
394 }
395
396 enum nss_status
397 _nss_nisplus_getservbyport_r (const int number, const char *protocol,
398 struct servent *serv,
399 char *buffer, size_t buflen, int *errnop)
400 {
401 if (tablename_val == NULL)
402 {
403 __libc_lock_lock (lock);
404
405 enum nss_status status = _nss_create_tablename (errnop);
406
407 __libc_lock_unlock (lock);
408
409 if (status != NSS_STATUS_SUCCESS)
410 return status;
411 }
412
413 if (protocol == NULL)
414 {
415 *errnop = EINVAL;
416 return NSS_STATUS_NOTFOUND;
417 }
418
419 char buf[17 + 3 * sizeof (int) + strlen (protocol) + tablename_len];
420 int olderr = errno;
421
422 snprintf (buf, sizeof (buf), "[port=%d,proto=%s],%s",
423 number, protocol, tablename_val);
424
425 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
426 NULL, NULL);
427
428 if (result == NULL)
429 {
430 *errnop = ENOMEM;
431 return NSS_STATUS_TRYAGAIN;
432 }
433
434 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
435 {
436 enum nss_status status = niserr2nss (result->status);
437
438 __set_errno (olderr);
439
440 nis_freeresult (result);
441 return status;
442 }
443
444 int parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
445 errnop);
446 nis_freeresult (result);
447
448 if (__builtin_expect (parse_res < 1, 0))
449 {
450 if (parse_res == -1)
451 {
452 *errnop = ERANGE;
453 return NSS_STATUS_TRYAGAIN;
454 }
455 else
456 {
457 __set_errno (olderr);
458 return NSS_STATUS_NOTFOUND;
459 }
460 }
461
462 return NSS_STATUS_SUCCESS;
463 }