1 /* Copyright (C) 1997, 2001, 2002, 2003, 2005 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/nis.h>
27 #include "nss-nisplus.h"
28 #include "nisplus-parser.h"
30 __libc_lock_define_initialized (static, lock
)
32 static nis_result
*result
;
34 /* Defined in nisplus-pwd.c. */
35 extern nis_name pwd_tablename_val attribute_hidden
;
36 extern size_t pwd_tablename_len attribute_hidden
;
37 extern enum nss_status
_nss_pwd_create_tablename (int *errnop
);
41 _nss_nisplus_setspent (int stayopen
)
43 enum nss_status status
= NSS_STATUS_SUCCESS
;
46 __libc_lock_lock (lock
);
50 nis_freeresult (result
);
54 if (pwd_tablename_val
== NULL
)
55 status
= _nss_pwd_create_tablename (&err
);
57 __libc_lock_unlock (lock
);
59 return NSS_STATUS_SUCCESS
;
63 _nss_nisplus_endspent (void)
65 __libc_lock_lock (lock
);
69 nis_freeresult (result
);
73 __libc_lock_unlock (lock
);
75 return NSS_STATUS_SUCCESS
;
78 static enum nss_status
79 internal_nisplus_getspent_r (struct spwd
*sp
, char *buffer
, size_t buflen
,
84 /* Get the next entry until we found a correct one. */
87 nis_result
*saved_res
;
93 if (pwd_tablename_val
== NULL
)
95 enum nss_status status
= _nss_pwd_create_tablename (errnop
);
97 if (status
!= NSS_STATUS_SUCCESS
)
101 result
= nis_first_entry (pwd_tablename_val
);
105 return NSS_STATUS_TRYAGAIN
;
107 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
108 return niserr2nss (result
->status
);
113 result
= nis_next_entry (pwd_tablename_val
, &result
->cookie
);
117 return NSS_STATUS_TRYAGAIN
;
119 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
121 nis_freeresult (saved_res
);
122 return niserr2nss (result
->status
);
126 parse_res
= _nss_nisplus_parse_spent (result
, sp
, buffer
,
128 if (__builtin_expect (parse_res
== -1, 0))
130 nis_freeresult (result
);
133 return NSS_STATUS_TRYAGAIN
;
136 if (saved_res
!= NULL
)
137 nis_freeresult (saved_res
);
141 return NSS_STATUS_SUCCESS
;
145 _nss_nisplus_getspent_r (struct spwd
*result
, char *buffer
, size_t buflen
,
150 __libc_lock_lock (lock
);
152 status
= internal_nisplus_getspent_r (result
, buffer
, buflen
, errnop
);
154 __libc_lock_unlock (lock
);
160 _nss_nisplus_getspnam_r (const char *name
, struct spwd
*sp
,
161 char *buffer
, size_t buflen
, int *errnop
)
165 if (pwd_tablename_val
== NULL
)
167 enum nss_status status
= _nss_pwd_create_tablename (errnop
);
169 if (status
!= NSS_STATUS_SUCCESS
)
176 return NSS_STATUS_NOTFOUND
;
180 char buf
[strlen (name
) + 9 + pwd_tablename_len
];
183 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, pwd_tablename_val
);
185 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
190 return NSS_STATUS_TRYAGAIN
;
193 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
195 enum nss_status status
= niserr2nss (result
->status
);
197 __set_errno (olderr
);
199 nis_freeresult (result
);
203 parse_res
= _nss_nisplus_parse_spent (result
, sp
, buffer
, buflen
, errnop
);
204 nis_freeresult (result
);
206 if (__builtin_expect (parse_res
< 1, 0))
211 return NSS_STATUS_TRYAGAIN
;
215 __set_errno (olderr
);
216 return NSS_STATUS_NOTFOUND
;
220 return NSS_STATUS_SUCCESS
;