1 /* Copyright (C) 1997-2013 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 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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
25 #include <rpcsvc/nis.h>
26 #include <bits/libc-lock.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock
)
32 static nis_result
*result
;
33 static nis_name tablename_val
;
34 static u_long tablename_len
;
36 #define NISENTRYVAL(idx, col, res) \
37 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
39 #define NISENTRYLEN(idx, col, res) \
40 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
44 _nss_nisplus_parse_protoent (nis_result
*result
, struct protoent
*proto
,
45 char *buffer
, size_t buflen
, int *errnop
)
47 char *first_unused
= buffer
;
48 size_t room_left
= buflen
;
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
,
58 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 3)
61 /* Generate the protocols entry format and use the normal parser */
62 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
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 size_t len
= strlen (first_unused
) + 1;
77 proto
->p_proto
= atoi (NISENTRYVAL (0, 2, result
));
79 /* XXX Rewrite at some point to allocate the array first and then
80 copy the strings. It wasteful to first concatenate the strings
81 to just split them again later. */
82 char *line
= first_unused
;
83 for (i
= 0; i
< NIS_RES_NUMOBJ (result
); ++i
)
85 if (strcmp (NISENTRYVAL (i
, 1, result
), proto
->p_name
) != 0)
87 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
89 *first_unused
++ = ' ';
90 first_unused
= __stpncpy (first_unused
, NISENTRYVAL (i
, 1, result
),
91 NISENTRYLEN (i
, 1, result
));
92 room_left
-= NISENTRYLEN (i
, 1, result
) + 1;
95 *first_unused
++ = '\0';
97 /* Adjust the pointer so it is aligned for
99 size_t adjust
= ((__alignof__ (char *)
100 - (first_unused
- (char *) 0) % __alignof__ (char *))
101 % __alignof__ (char *));
102 if (room_left
< adjust
+ sizeof (char *))
104 first_unused
+= adjust
;
106 proto
->p_aliases
= (char **) first_unused
;
108 /* For the terminating NULL pointer. */
109 room_left
-= sizeof (char *);
112 while (*line
!= '\0')
114 /* Skip leading blanks. */
115 while (isspace (*line
))
120 if (room_left
< sizeof (char *))
123 room_left
-= sizeof (char *);
124 proto
->p_aliases
[i
++] = line
;
126 while (*line
!= '\0' && *line
!= ' ')
132 proto
->p_aliases
[i
] = NULL
;
137 static enum nss_status
138 _nss_create_tablename (int *errnop
)
140 if (tablename_val
== NULL
)
142 const char *local_dir
= nis_local_directory ();
143 size_t local_dir_len
= strlen (local_dir
);
144 static const char prefix
[] = "protocols.org_dir.";
146 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
150 return NSS_STATUS_TRYAGAIN
;
153 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
155 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
157 atomic_write_barrier ();
162 return NSS_STATUS_SUCCESS
;
166 _nss_nisplus_setprotoent (int stayopen
)
168 enum nss_status status
= NSS_STATUS_SUCCESS
;
170 __libc_lock_lock (lock
);
174 nis_freeresult (result
);
178 if (tablename_val
== NULL
)
181 status
= _nss_create_tablename (&err
);
184 __libc_lock_unlock (lock
);
190 _nss_nisplus_endprotoent (void)
192 __libc_lock_lock (lock
);
196 nis_freeresult (result
);
200 __libc_lock_unlock (lock
);
202 return NSS_STATUS_SUCCESS
;
205 static enum nss_status
206 internal_nisplus_getprotoent_r (struct protoent
*proto
, char *buffer
,
207 size_t buflen
, int *errnop
)
211 /* Get the next entry until we found a correct one. */
214 nis_result
*saved_res
;
219 if (tablename_val
== NULL
)
221 enum nss_status status
= _nss_create_tablename (errnop
);
223 if (status
!= NSS_STATUS_SUCCESS
)
227 result
= nis_first_entry (tablename_val
);
231 return NSS_STATUS_TRYAGAIN
;
233 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
234 return niserr2nss (result
->status
);
239 result
= nis_next_entry (tablename_val
, &result
->cookie
);
243 return NSS_STATUS_TRYAGAIN
;
245 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
247 nis_freeresult (saved_res
);
248 return niserr2nss (result
->status
);
252 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
,
256 nis_freeresult (result
);
259 return NSS_STATUS_TRYAGAIN
;
264 nis_freeresult (saved_res
);
269 return NSS_STATUS_SUCCESS
;
273 _nss_nisplus_getprotoent_r (struct protoent
*result
, char *buffer
,
274 size_t buflen
, int *errnop
)
278 __libc_lock_lock (lock
);
280 status
= internal_nisplus_getprotoent_r (result
, buffer
, buflen
, errnop
);
282 __libc_lock_unlock (lock
);
288 _nss_nisplus_getprotobyname_r (const char *name
, struct protoent
*proto
,
289 char *buffer
, size_t buflen
, int *errnop
)
293 if (tablename_val
== NULL
)
295 __libc_lock_lock (lock
);
297 enum nss_status status
= _nss_create_tablename (errnop
);
299 __libc_lock_unlock (lock
);
301 if (status
!= NSS_STATUS_SUCCESS
)
306 return NSS_STATUS_NOTFOUND
;
308 char buf
[strlen (name
) + 10 + tablename_len
];
311 /* Search at first in the alias list, and use the correct name
312 for the next search */
313 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
314 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
320 /* If we did not find it, try it as original name. But if the
321 database is correct, we should find it in the first case, too */
322 if ((result
->status
!= NIS_SUCCESS
323 && result
->status
!= NIS_S_SUCCESS
)
324 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
325 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
326 "protocols_tbl") != 0
327 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 3)
328 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
331 /* We need to allocate a new buffer since there is no
332 guarantee the returned name has a length limit. */
333 const char *entryval
= NISENTRYVAL (0, 0, result
);
334 size_t buflen
= strlen (entryval
) + 10 + tablename_len
;
335 bufptr
= alloca (buflen
);
336 snprintf (bufptr
, buflen
, "[cname=%s],%s",
337 entryval
, tablename_val
);
340 nis_freeresult (result
);
341 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
346 __set_errno (ENOMEM
);
347 return NSS_STATUS_TRYAGAIN
;
350 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
352 enum nss_status status
= niserr2nss (result
->status
);
354 __set_errno (olderr
);
356 nis_freeresult (result
);
360 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
,
363 nis_freeresult (result
);
370 return NSS_STATUS_TRYAGAIN
;
374 __set_errno (olderr
);
375 return NSS_STATUS_NOTFOUND
;
379 return NSS_STATUS_SUCCESS
;
383 _nss_nisplus_getprotobynumber_r (const int number
, struct protoent
*proto
,
384 char *buffer
, size_t buflen
, int *errnop
)
386 if (tablename_val
== NULL
)
388 __libc_lock_lock (lock
);
390 enum nss_status status
= _nss_create_tablename (errnop
);
392 __libc_lock_unlock (lock
);
394 if (status
!= NSS_STATUS_SUCCESS
)
398 char buf
[12 + 3 * sizeof (number
) + tablename_len
];
401 snprintf (buf
, sizeof (buf
), "[number=%d],%s", number
, tablename_val
);
403 nis_result
*result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
407 __set_errno (ENOMEM
);
408 return NSS_STATUS_TRYAGAIN
;
411 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
413 enum nss_status status
= niserr2nss (result
->status
);
415 __set_errno (olderr
);
417 nis_freeresult (result
);
421 int parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
,
424 nis_freeresult (result
);
431 return NSS_STATUS_TRYAGAIN
;
435 __set_errno (olderr
);
436 return NSS_STATUS_NOTFOUND
;
440 return NSS_STATUS_SUCCESS
;