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.
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <rpcsvc/nis.h>
28 #include <bits/libc-lock.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock
);
34 static nis_result
*result
;
35 static nis_name tablename_val
;
36 static u_long tablename_len
;
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)
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)
46 _nss_nisplus_parse_servent (nis_result
*result
, struct servent
*serv
,
47 char *buffer
, size_t buflen
, int *errnop
)
49 char *first_unused
= buffer
;
50 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
, "services_tbl") != 0
58 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 4)
61 if (NISENTRYLEN (0, 0, result
) >= room_left
)
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;
75 if (NISENTRYLEN (0, 2, result
) >= room_left
)
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;
85 serv
->s_port
= htons (atoi (NISENTRYVAL (0, 3, result
)));
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
)
93 if (strcmp (NISENTRYVAL (i
, 1, result
), serv
->s_name
) != 0)
95 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
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;
103 *first_unused
++ = '\0';
105 /* Adjust the pointer so it is aligned for
107 size_t adjust
= ((__alignof__ (char *)
108 - (first_unused
- (char *) 0) % __alignof__ (char *))
109 % __alignof__ (char *));
110 if (room_left
< adjust
+ sizeof (char *))
112 first_unused
+= adjust
;
114 serv
->s_aliases
= (char **) first_unused
;
116 /* For the terminating NULL pointer. */
117 room_left
-= (sizeof (char *));
120 while (*line
!= '\0')
122 /* Skip leading blanks. */
123 while (isspace (*line
))
129 if (room_left
< sizeof (char *))
132 room_left
-= sizeof (char *);
133 serv
->s_aliases
[i
++] = line
;
135 while (*line
!= '\0' && *line
!= ' ')
141 serv
->s_aliases
[i
] = NULL
;
147 static enum nss_status
148 _nss_create_tablename (int *errnop
)
150 if (tablename_val
== NULL
)
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.";
156 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
160 return NSS_STATUS_TRYAGAIN
;
163 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
165 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
167 atomic_write_barrier ();
172 return NSS_STATUS_SUCCESS
;
177 _nss_nisplus_setservent (int stayopen
)
179 enum nss_status status
= NSS_STATUS_SUCCESS
;
182 __libc_lock_lock (lock
);
186 nis_freeresult (result
);
190 if (tablename_val
== NULL
)
191 status
= _nss_create_tablename (&err
);
193 __libc_lock_unlock (lock
);
199 _nss_nisplus_endservent (void)
201 __libc_lock_lock (lock
);
205 nis_freeresult (result
);
209 __libc_lock_unlock (lock
);
211 return NSS_STATUS_SUCCESS
;
214 static enum nss_status
215 internal_nisplus_getservent_r (struct servent
*serv
, char *buffer
,
216 size_t buflen
, int *errnop
)
220 /* Get the next entry until we found a correct one. */
223 nis_result
*saved_res
;
228 if (tablename_val
== NULL
)
230 enum nss_status status
= _nss_create_tablename (errnop
);
232 if (status
!= NSS_STATUS_SUCCESS
)
236 result
= nis_first_entry (tablename_val
);
240 return NSS_STATUS_TRYAGAIN
;
242 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
243 return niserr2nss (result
->status
);
248 result
= nis_next_entry (tablename_val
, &result
->cookie
);
252 return NSS_STATUS_TRYAGAIN
;
254 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
256 nis_freeresult (saved_res
);
257 return niserr2nss (result
->status
);
261 parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
,
263 if (__builtin_expect (parse_res
== -1, 0))
265 nis_freeresult (result
);
268 return NSS_STATUS_TRYAGAIN
;
273 nis_freeresult (saved_res
);
278 return NSS_STATUS_SUCCESS
;
282 _nss_nisplus_getservent_r (struct servent
*result
, char *buffer
,
283 size_t buflen
, int *errnop
)
285 __libc_lock_lock (lock
);
287 int status
= internal_nisplus_getservent_r (result
, buffer
, buflen
, errnop
);
289 __libc_lock_unlock (lock
);
295 _nss_nisplus_getservbyname_r (const char *name
, const char *protocol
,
296 struct servent
*serv
,
297 char *buffer
, size_t buflen
, int *errnop
)
299 if (tablename_val
== NULL
)
301 __libc_lock_lock (lock
);
303 enum nss_status status
= _nss_create_tablename (errnop
);
305 __libc_lock_unlock (lock
);
307 if (status
!= NSS_STATUS_SUCCESS
)
311 if (name
== NULL
|| protocol
== NULL
)
314 return NSS_STATUS_NOTFOUND
;
317 size_t protocol_len
= strlen (protocol
);
318 char buf
[strlen (name
) + protocol_len
+ 17 + tablename_len
];
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
,
325 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
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
,
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
,
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
349 bufptr
= alloca (buflen
);
350 snprintf (bufptr
, buflen
, "[cname=%s,proto=%s],%s",
351 entryval
, protocol
, tablename_val
);
354 nis_freeresult (result
);
355 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
362 return NSS_STATUS_TRYAGAIN
;
365 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
367 enum nss_status status
= niserr2nss (result
->status
);
369 __set_errno (olderr
);
371 nis_freeresult (result
);
375 int parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
,
377 nis_freeresult (result
);
379 if (__builtin_expect (parse_res
< 1, 0))
384 return NSS_STATUS_TRYAGAIN
;
388 __set_errno (olderr
);
389 return NSS_STATUS_NOTFOUND
;
393 return NSS_STATUS_SUCCESS
;
397 _nss_nisplus_getservbyport_r (const int number
, const char *protocol
,
398 struct servent
*serv
,
399 char *buffer
, size_t buflen
, int *errnop
)
401 if (tablename_val
== NULL
)
403 __libc_lock_lock (lock
);
405 enum nss_status status
= _nss_create_tablename (errnop
);
407 __libc_lock_unlock (lock
);
409 if (status
!= NSS_STATUS_SUCCESS
)
413 if (protocol
== NULL
)
416 return NSS_STATUS_NOTFOUND
;
419 char buf
[17 + 3 * sizeof (int) + strlen (protocol
) + tablename_len
];
422 snprintf (buf
, sizeof (buf
), "[port=%d,proto=%s],%s",
423 number
, protocol
, tablename_val
);
425 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
431 return NSS_STATUS_TRYAGAIN
;
434 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
436 enum nss_status status
= niserr2nss (result
->status
);
438 __set_errno (olderr
);
440 nis_freeresult (result
);
444 int parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
,
446 nis_freeresult (result
);
448 if (__builtin_expect (parse_res
< 1, 0))
453 return NSS_STATUS_TRYAGAIN
;
457 __set_errno (olderr
);
458 return NSS_STATUS_NOTFOUND
;
462 return NSS_STATUS_SUCCESS
;