1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003, 2005, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
26 #include <rpcsvc/nis.h>
27 #include <bits/libc-lock.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock
)
33 static nis_result
*result
;
34 static nis_name tablename_val
;
35 static u_long tablename_len
;
37 #define NISENTRYVAL(idx, col, res) \
38 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx, col, res) \
41 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
45 _nss_nisplus_parse_protoent (nis_result
*result
, struct protoent
*proto
,
46 char *buffer
, size_t buflen
, int *errnop
)
48 char *first_unused
= buffer
;
49 size_t room_left
= buflen
;
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
,
59 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 3)
62 /* Generate the protocols entry format and use the normal parser */
63 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
69 strncpy (first_unused
, NISENTRYVAL (0, 0, result
),
70 NISENTRYLEN (0, 0, result
));
71 first_unused
[NISENTRYLEN (0, 0, result
)] = '\0';
72 proto
->p_name
= first_unused
;
73 size_t len
= strlen (first_unused
) + 1;
78 proto
->p_proto
= atoi (NISENTRYVAL (0, 2, result
));
80 /* XXX Rewrite at some point to allocate the array first and then
81 copy the strings. It wasteful to first concatenate the strings
82 to just split them again later. */
83 char *line
= first_unused
;
84 for (i
= 0; i
< NIS_RES_NUMOBJ (result
); ++i
)
86 if (strcmp (NISENTRYVAL (i
, 1, result
), proto
->p_name
) != 0)
88 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
90 *first_unused
++ = ' ';
91 first_unused
= __stpncpy (first_unused
, NISENTRYVAL (i
, 1, result
),
92 NISENTRYLEN (i
, 1, result
));
93 room_left
-= NISENTRYLEN (i
, 1, result
) + 1;
96 *first_unused
++ = '\0';
98 /* Adjust the pointer so it is aligned for
100 size_t adjust
= ((__alignof__ (char *)
101 - (first_unused
- (char *) 0) % __alignof__ (char *))
102 % __alignof__ (char *));
103 if (room_left
< adjust
+ sizeof (char *))
105 first_unused
+= adjust
;
107 proto
->p_aliases
= (char **) first_unused
;
109 /* For the terminating NULL pointer. */
110 room_left
-= sizeof (char *);
113 while (*line
!= '\0')
115 /* Skip leading blanks. */
116 while (isspace (*line
))
121 if (room_left
< sizeof (char *))
124 room_left
-= sizeof (char *);
125 proto
->p_aliases
[i
++] = line
;
127 while (*line
!= '\0' && *line
!= ' ')
133 proto
->p_aliases
[i
] = NULL
;
138 static enum nss_status
139 _nss_create_tablename (int *errnop
)
141 if (tablename_val
== NULL
)
143 const char *local_dir
= nis_local_directory ();
144 size_t local_dir_len
= strlen (local_dir
);
145 static const char prefix
[] = "protocols.org_dir.";
147 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
151 return NSS_STATUS_TRYAGAIN
;
154 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
156 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
158 atomic_write_barrier ();
163 return NSS_STATUS_SUCCESS
;
167 _nss_nisplus_setprotoent (int stayopen
)
169 enum nss_status status
= NSS_STATUS_SUCCESS
;
171 __libc_lock_lock (lock
);
175 nis_freeresult (result
);
179 if (tablename_val
== NULL
)
182 status
= _nss_create_tablename (&err
);
185 __libc_lock_unlock (lock
);
191 _nss_nisplus_endprotoent (void)
193 __libc_lock_lock (lock
);
197 nis_freeresult (result
);
201 __libc_lock_unlock (lock
);
203 return NSS_STATUS_SUCCESS
;
206 static enum nss_status
207 internal_nisplus_getprotoent_r (struct protoent
*proto
, char *buffer
,
208 size_t buflen
, int *errnop
)
212 /* Get the next entry until we found a correct one. */
215 nis_result
*saved_res
;
220 if (tablename_val
== NULL
)
222 enum nss_status status
= _nss_create_tablename (errnop
);
224 if (status
!= NSS_STATUS_SUCCESS
)
228 result
= nis_first_entry (tablename_val
);
232 return NSS_STATUS_TRYAGAIN
;
234 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
235 return niserr2nss (result
->status
);
240 result
= nis_next_entry (tablename_val
, &result
->cookie
);
244 return NSS_STATUS_TRYAGAIN
;
246 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
248 nis_freeresult (saved_res
);
249 return niserr2nss (result
->status
);
253 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
,
257 nis_freeresult (result
);
260 return NSS_STATUS_TRYAGAIN
;
265 nis_freeresult (saved_res
);
270 return NSS_STATUS_SUCCESS
;
274 _nss_nisplus_getprotoent_r (struct protoent
*result
, char *buffer
,
275 size_t buflen
, int *errnop
)
279 __libc_lock_lock (lock
);
281 status
= internal_nisplus_getprotoent_r (result
, buffer
, buflen
, errnop
);
283 __libc_lock_unlock (lock
);
289 _nss_nisplus_getprotobyname_r (const char *name
, struct protoent
*proto
,
290 char *buffer
, size_t buflen
, int *errnop
)
294 if (tablename_val
== NULL
)
296 __libc_lock_lock (lock
);
298 enum nss_status status
= _nss_create_tablename (errnop
);
300 __libc_lock_unlock (lock
);
302 if (status
!= NSS_STATUS_SUCCESS
)
307 return NSS_STATUS_NOTFOUND
;
309 char buf
[strlen (name
) + 10 + tablename_len
];
312 /* Search at first in the alias list, and use the correct name
313 for the next search */
314 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
315 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
321 /* If we did not find it, try it as original name. But if the
322 database is correct, we should find it in the first case, too */
323 if ((result
->status
!= NIS_SUCCESS
324 && result
->status
!= NIS_S_SUCCESS
)
325 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
326 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
327 "protocols_tbl") != 0
328 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 3)
329 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
332 /* We need to allocate a new buffer since there is no
333 guarantee the returned name has a length limit. */
334 const char *entryval
= NISENTRYVAL (0, 0, result
);
335 size_t buflen
= strlen (entryval
) + 10 + tablename_len
;
336 bufptr
= alloca (buflen
);
337 snprintf (bufptr
, buflen
, "[cname=%s],%s",
338 entryval
, tablename_val
);
341 nis_freeresult (result
);
342 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
347 __set_errno (ENOMEM
);
348 return NSS_STATUS_TRYAGAIN
;
351 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
353 enum nss_status status
= niserr2nss (result
->status
);
355 __set_errno (olderr
);
357 nis_freeresult (result
);
361 parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
,
364 nis_freeresult (result
);
371 return NSS_STATUS_TRYAGAIN
;
375 __set_errno (olderr
);
376 return NSS_STATUS_NOTFOUND
;
380 return NSS_STATUS_SUCCESS
;
384 _nss_nisplus_getprotobynumber_r (const int number
, struct protoent
*proto
,
385 char *buffer
, size_t buflen
, int *errnop
)
387 if (tablename_val
== NULL
)
389 __libc_lock_lock (lock
);
391 enum nss_status status
= _nss_create_tablename (errnop
);
393 __libc_lock_unlock (lock
);
395 if (status
!= NSS_STATUS_SUCCESS
)
399 char buf
[12 + 3 * sizeof (number
) + tablename_len
];
402 snprintf (buf
, sizeof (buf
), "[number=%d],%s", number
, tablename_val
);
404 nis_result
*result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
408 __set_errno (ENOMEM
);
409 return NSS_STATUS_TRYAGAIN
;
412 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
414 enum nss_status status
= niserr2nss (result
->status
);
416 __set_errno (olderr
);
418 nis_freeresult (result
);
422 int parse_res
= _nss_nisplus_parse_protoent (result
, proto
, buffer
, buflen
,
425 nis_freeresult (result
);
432 return NSS_STATUS_TRYAGAIN
;
436 __set_errno (olderr
);
437 return NSS_STATUS_NOTFOUND
;
441 return NSS_STATUS_SUCCESS
;