1 /* Copyright (C) 1997, 2001, 2002, 2003, 2005, 2007
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
29 #include "nisplus-parser.h"
31 __libc_lock_define_initialized (static, lock
)
33 static nis_result
*result
;
35 /* Defined in nisplus-pwd.c. */
36 extern nis_name pwd_tablename_val attribute_hidden
;
37 extern size_t pwd_tablename_len attribute_hidden
;
38 extern enum nss_status
_nss_pwd_create_tablename (int *errnop
);
42 _nss_nisplus_setspent (int stayopen
)
44 enum nss_status status
= NSS_STATUS_SUCCESS
;
47 __libc_lock_lock (lock
);
51 nis_freeresult (result
);
55 if (pwd_tablename_val
== NULL
)
56 status
= _nss_pwd_create_tablename (&err
);
58 __libc_lock_unlock (lock
);
60 return NSS_STATUS_SUCCESS
;
64 _nss_nisplus_endspent (void)
66 __libc_lock_lock (lock
);
70 nis_freeresult (result
);
74 __libc_lock_unlock (lock
);
76 return NSS_STATUS_SUCCESS
;
79 static enum nss_status
80 internal_nisplus_getspent_r (struct spwd
*sp
, char *buffer
, size_t buflen
,
85 /* Get the next entry until we found a correct one. */
88 nis_result
*saved_res
;
94 if (pwd_tablename_val
== NULL
)
96 enum nss_status status
= _nss_pwd_create_tablename (errnop
);
98 if (status
!= NSS_STATUS_SUCCESS
)
102 result
= nis_first_entry (pwd_tablename_val
);
106 return NSS_STATUS_TRYAGAIN
;
108 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
109 return niserr2nss (result
->status
);
114 result
= nis_next_entry (pwd_tablename_val
, &result
->cookie
);
118 return NSS_STATUS_TRYAGAIN
;
120 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
122 nis_freeresult (saved_res
);
123 return niserr2nss (result
->status
);
127 parse_res
= _nss_nisplus_parse_spent (result
, sp
, buffer
,
129 if (__builtin_expect (parse_res
== -1, 0))
131 nis_freeresult (result
);
134 return NSS_STATUS_TRYAGAIN
;
137 if (saved_res
!= NULL
)
138 nis_freeresult (saved_res
);
142 return NSS_STATUS_SUCCESS
;
146 _nss_nisplus_getspent_r (struct spwd
*result
, char *buffer
, size_t buflen
,
151 __libc_lock_lock (lock
);
153 status
= internal_nisplus_getspent_r (result
, buffer
, buflen
, errnop
);
155 __libc_lock_unlock (lock
);
161 _nss_nisplus_getspnam_r (const char *name
, struct spwd
*sp
,
162 char *buffer
, size_t buflen
, int *errnop
)
166 if (pwd_tablename_val
== NULL
)
168 enum nss_status status
= _nss_pwd_create_tablename (errnop
);
170 if (status
!= NSS_STATUS_SUCCESS
)
177 return NSS_STATUS_NOTFOUND
;
181 char buf
[strlen (name
) + 9 + pwd_tablename_len
];
184 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, pwd_tablename_val
);
186 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
, NULL
, NULL
);
191 return NSS_STATUS_TRYAGAIN
;
194 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
196 enum nss_status status
= niserr2nss (result
->status
);
198 __set_errno (olderr
);
200 nis_freeresult (result
);
204 parse_res
= _nss_nisplus_parse_spent (result
, sp
, buffer
, buflen
, errnop
);
205 nis_freeresult (result
);
207 if (__builtin_expect (parse_res
< 1, 0))
212 return NSS_STATUS_TRYAGAIN
;
216 __set_errno (olderr
);
217 return NSS_STATUS_NOTFOUND
;
221 return NSS_STATUS_SUCCESS
;