1 /* Copyright (C) 1997-2014 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, see
17 <http://www.gnu.org/licenses/>. */
23 #include <bits/libc-lock.h>
24 #include <rpcsvc/nis.h>
26 #include "nss-nisplus.h"
27 #include "nisplus-parser.h"
29 __libc_lock_define_initialized (static, lock
)
31 static nis_result
*result
;
33 /* Defined in nisplus-pwd.c. */
34 extern nis_name pwd_tablename_val attribute_hidden
;
35 extern size_t pwd_tablename_len attribute_hidden
;
36 extern enum nss_status
_nss_pwd_create_tablename (int *errnop
);
40 _nss_nisplus_setspent (int stayopen
)
42 enum nss_status status
= NSS_STATUS_SUCCESS
;
45 __libc_lock_lock (lock
);
49 nis_freeresult (result
);
53 if (pwd_tablename_val
== NULL
)
54 status
= _nss_pwd_create_tablename (&err
);
56 __libc_lock_unlock (lock
);
62 _nss_nisplus_endspent (void)
64 __libc_lock_lock (lock
);
68 nis_freeresult (result
);
72 __libc_lock_unlock (lock
);
74 return NSS_STATUS_SUCCESS
;
77 static enum nss_status
78 internal_nisplus_getspent_r (struct spwd
*sp
, char *buffer
, size_t buflen
,
83 /* Get the next entry until we found a correct one. */
86 nis_result
*saved_res
;
92 if (pwd_tablename_val
== NULL
)
94 enum nss_status status
= _nss_pwd_create_tablename (errnop
);
96 if (status
!= NSS_STATUS_SUCCESS
)
100 result
= nis_first_entry (pwd_tablename_val
);
104 return NSS_STATUS_TRYAGAIN
;
106 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
107 return niserr2nss (result
->status
);
112 result
= nis_next_entry (pwd_tablename_val
, &result
->cookie
);
116 return NSS_STATUS_TRYAGAIN
;
118 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
120 nis_freeresult (saved_res
);
121 return niserr2nss (result
->status
);
125 parse_res
= _nss_nisplus_parse_spent (result
, sp
, buffer
,
127 if (__glibc_unlikely (parse_res
== -1))
129 nis_freeresult (result
);
132 return NSS_STATUS_TRYAGAIN
;
135 if (saved_res
!= NULL
)
136 nis_freeresult (saved_res
);
140 return NSS_STATUS_SUCCESS
;
144 _nss_nisplus_getspent_r (struct spwd
*result
, char *buffer
, size_t buflen
,
149 __libc_lock_lock (lock
);
151 status
= internal_nisplus_getspent_r (result
, buffer
, buflen
, errnop
);
153 __libc_lock_unlock (lock
);
159 _nss_nisplus_getspnam_r (const char *name
, struct spwd
*sp
,
160 char *buffer
, size_t buflen
, int *errnop
)
164 if (pwd_tablename_val
== NULL
)
166 enum nss_status status
= _nss_pwd_create_tablename (errnop
);
168 if (status
!= NSS_STATUS_SUCCESS
)
175 return NSS_STATUS_NOTFOUND
;
179 char buf
[strlen (name
) + 9 + pwd_tablename_len
];
182 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, pwd_tablename_val
);
184 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
, NULL
, NULL
);
189 return NSS_STATUS_TRYAGAIN
;
192 if (__glibc_unlikely (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
))
194 enum nss_status status
= niserr2nss (result
->status
);
196 __set_errno (olderr
);
198 nis_freeresult (result
);
202 parse_res
= _nss_nisplus_parse_spent (result
, sp
, buffer
, buflen
, errnop
);
203 nis_freeresult (result
);
205 if (__glibc_unlikely (parse_res
< 1))
210 return NSS_STATUS_TRYAGAIN
;
214 __set_errno (olderr
);
215 return NSS_STATUS_NOTFOUND
;
219 return NSS_STATUS_SUCCESS
;