1 /* Copyright (C) 1997-1999,2001,2002,2003,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx,col,res) \
41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
44 _nss_nisplus_parse_servent (nis_result
*result
, struct servent
*serv
,
45 char *buffer
, size_t buflen
, int *errnop
)
47 char *first_unused
= buffer
;
48 size_t room_left
= buflen
;
53 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
54 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
55 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
57 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 4)
60 if (NISENTRYLEN (0, 0, result
) >= room_left
)
66 strncpy (first_unused
, NISENTRYVAL (0, 0, result
),
67 NISENTRYLEN (0, 0, result
));
68 first_unused
[NISENTRYLEN (0, 0, result
)] = '\0';
69 serv
->s_name
= first_unused
;
70 room_left
-= (strlen (first_unused
) +1);
71 first_unused
+= strlen (first_unused
) +1;
73 if (NISENTRYLEN (0, 2, result
) >= room_left
)
75 strncpy (first_unused
, NISENTRYVAL (0, 2, result
),
76 NISENTRYLEN (0, 2, result
));
77 first_unused
[NISENTRYLEN (0, 2, result
)] = '\0';
78 serv
->s_proto
= first_unused
;
79 room_left
-= strlen (first_unused
) + 1;
80 first_unused
+= strlen (first_unused
) + 1;
82 serv
->s_port
= htons (atoi (NISENTRYVAL (0, 3, result
)));
83 char *p
= first_unused
;
86 for (unsigned int i
= 0; i
< result
->objects
.objects_len
; ++i
)
88 if (strcmp (NISENTRYVAL (i
, 1, result
), serv
->s_name
) != 0)
90 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
93 p
= __stpncpy (p
, NISENTRYVAL (i
, 1, result
),
94 NISENTRYLEN (i
, 1, result
));
96 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
102 /* Adjust the pointer so it is aligned for
104 first_unused
+= __alignof__ (char *) - 1;
105 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
106 serv
->s_aliases
= (char **) first_unused
;
107 if (room_left
< sizeof (char *))
109 room_left
-= (sizeof (char *));
110 serv
->s_aliases
[0] = NULL
;
113 while (*line
!= '\0')
115 /* Skip leading blanks. */
116 while (isspace (*line
))
122 if (room_left
< sizeof (char *))
125 room_left
-= sizeof (char *);
126 serv
->s_aliases
[i
] = line
;
128 while (*line
!= '\0' && *line
!= ' ')
138 serv
->s_aliases
[i
+1] = NULL
;
144 static enum nss_status
145 _nss_create_tablename (int *errnop
)
147 if (tablename_val
== NULL
)
149 const char *local_dir
= nis_local_directory ();
150 size_t local_dir_len
= strlen (local_dir
);
151 static const char prefix
[] = "services.org_dir.";
153 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
154 if (tablename_val
== NULL
)
157 return NSS_STATUS_TRYAGAIN
;
160 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
162 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
164 atomic_write_barrier ();
169 return NSS_STATUS_SUCCESS
;
174 _nss_nisplus_setservent (int stayopen
)
176 enum nss_status status
= NSS_STATUS_SUCCESS
;
179 __libc_lock_lock (lock
);
183 nis_freeresult (result
);
187 if (tablename_val
== NULL
)
188 status
= _nss_create_tablename (&err
);
190 __libc_lock_unlock (lock
);
196 _nss_nisplus_endservent (void)
198 __libc_lock_lock (lock
);
202 nis_freeresult (result
);
206 __libc_lock_unlock (lock
);
208 return NSS_STATUS_SUCCESS
;
211 static enum nss_status
212 internal_nisplus_getservent_r (struct servent
*serv
, char *buffer
,
213 size_t buflen
, int *errnop
)
217 /* Get the next entry until we found a correct one. */
220 nis_result
*saved_res
;
225 if (tablename_val
== NULL
)
227 enum nss_status status
= _nss_create_tablename (errnop
);
229 if (status
!= NSS_STATUS_SUCCESS
)
233 result
= nis_first_entry (tablename_val
);
234 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
235 return niserr2nss (result
->status
);
240 result
= nis_next_entry (tablename_val
, &result
->cookie
);
241 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
243 nis_freeresult (saved_res
);
244 return niserr2nss (result
->status
);
248 parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
,
250 if (__builtin_expect (parse_res
== -1, 0))
252 nis_freeresult (result
);
255 return NSS_STATUS_TRYAGAIN
;
260 nis_freeresult (saved_res
);
265 return NSS_STATUS_SUCCESS
;
269 _nss_nisplus_getservent_r (struct servent
*result
, char *buffer
,
270 size_t buflen
, int *errnop
)
272 __libc_lock_lock (lock
);
274 int status
= internal_nisplus_getservent_r (result
, buffer
, buflen
, errnop
);
276 __libc_lock_unlock (lock
);
282 _nss_nisplus_getservbyname_r (const char *name
, const char *protocol
,
283 struct servent
*serv
,
284 char *buffer
, size_t buflen
, int *errnop
)
286 if (tablename_val
== NULL
)
288 __libc_lock_lock (lock
);
290 enum nss_status status
= _nss_create_tablename (errnop
);
292 __libc_lock_unlock (lock
);
294 if (status
!= NSS_STATUS_SUCCESS
)
298 if (name
== NULL
|| protocol
== NULL
)
301 return NSS_STATUS_NOTFOUND
;
304 size_t protocol_len
= strlen (protocol
);
305 char buf
[strlen (name
) + protocol_len
+ 17 + tablename_len
];
308 /* Search at first in the alias list, and use the correct name
309 for the next search */
310 snprintf (buf
, sizeof (buf
), "[name=%s,proto=%s],%s", name
, protocol
,
312 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
318 /* If we did not find it, try it as original name. But if the
319 database is correct, we should find it in the first case, too */
320 if ((result
->status
!= NIS_SUCCESS
321 && result
->status
!= NIS_S_SUCCESS
)
322 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
323 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
325 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 4)
326 snprintf (buf
, sizeof (buf
), "[cname=%s,proto=%s],%s", name
, protocol
,
330 /* We need to allocate a new buffer since there is no
331 guarantee the returned name has a length limit. */
332 const char *entryval
= NISENTRYVAL(0, 0, result
);
333 size_t buflen
= (strlen (entryval
) + protocol_len
+ 17
335 bufptr
= alloca (buflen
);
336 snprintf (bufptr
, buflen
, "[cname=%s,proto=%s],%s",
337 entryval
, protocol
, tablename_val
);
340 nis_freeresult (result
);
341 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
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 int parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
,
362 nis_freeresult (result
);
364 if (__builtin_expect (parse_res
< 1, 0))
369 return NSS_STATUS_TRYAGAIN
;
373 __set_errno (olderr
);
374 return NSS_STATUS_NOTFOUND
;
378 return NSS_STATUS_SUCCESS
;
382 _nss_nisplus_getservbyport_r (const int number
, const char *protocol
,
383 struct servent
*serv
,
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 if (protocol
== NULL
)
401 return NSS_STATUS_NOTFOUND
;
404 char buf
[17 + 3 * sizeof (int) + strlen (protocol
) + tablename_len
];
407 snprintf (buf
, sizeof (buf
), "[port=%d,proto=%s],%s",
408 number
, protocol
, tablename_val
);
410 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
415 return NSS_STATUS_TRYAGAIN
;
418 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
420 enum nss_status status
= niserr2nss (result
->status
);
422 __set_errno (olderr
);
424 nis_freeresult (result
);
428 int parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
,
430 nis_freeresult (result
);
432 if (__builtin_expect (parse_res
< 1, 0))
437 return NSS_STATUS_TRYAGAIN
;
441 __set_errno (olderr
);
442 return NSS_STATUS_NOTFOUND
;
446 return NSS_STATUS_SUCCESS
;