4 Copyright (C) Simo Sorce 2006
6 This 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 3 of the License, or (at your option) any later version.
11 This 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 Library General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 extern struct _ldb_nss_context
*_ldb_nss_ctx
;
24 const char *_ldb_nss_pw_attrs
[] = {
35 NSS_STATUS
_nss_ldb_setpwent(void)
38 ret
= _ldb_nss_init();
39 if (ret
!= NSS_STATUS_SUCCESS
) {
43 _ldb_nss_ctx
->pw_cur
= 0;
44 if (_ldb_nss_ctx
->pw_res
!= NULL
) {
45 talloc_free(_ldb_nss_ctx
->pw_res
);
46 _ldb_nss_ctx
->pw_res
= NULL
;
49 ret
= ldb_search(_ldb_nss_ctx
->ldb
,
51 &_ldb_nss_ctx
->pw_res
,
55 _LDB_NSS_PWENT_FILTER
);
56 if (ret
!= LDB_SUCCESS
) {
57 return NSS_STATUS_UNAVAIL
;
60 return NSS_STATUS_SUCCESS
;
63 NSS_STATUS
_nss_ldb_endpwent(void)
67 ret
= _ldb_nss_init();
68 if (ret
!= NSS_STATUS_SUCCESS
) {
72 _ldb_nss_ctx
->pw_cur
= 0;
73 if (_ldb_nss_ctx
->pw_res
) {
74 talloc_free(_ldb_nss_ctx
->pw_res
);
75 _ldb_nss_ctx
->pw_res
= NULL
;
78 return NSS_STATUS_SUCCESS
;
81 NSS_STATUS
_nss_ldb_getpwent_r(struct passwd
*result_buf
,
88 ret
= _ldb_nss_init();
89 if (ret
!= NSS_STATUS_SUCCESS
) {
95 if (_ldb_nss_ctx
->pw_cur
>= _ldb_nss_ctx
->pw_res
->count
) {
96 /* already returned all entries */
97 return NSS_STATUS_NOTFOUND
;
100 ret
= _ldb_nss_fill_passwd(result_buf
,
104 _ldb_nss_ctx
->pw_res
->msgs
[_ldb_nss_ctx
->pw_cur
]);
105 if (ret
!= NSS_STATUS_SUCCESS
) {
109 _ldb_nss_ctx
->pw_cur
++;
111 return NSS_STATUS_SUCCESS
;
114 NSS_STATUS
_nss_ldb_getpwuid_r(uid_t uid
, struct passwd
*result_buf
, char *buffer
, size_t buflen
, int *errnop
)
118 struct ldb_result
*res
;
120 if (uid
== 0) { /* we don't serve root uid by policy */
121 *errnop
= errno
= ENOENT
;
122 return NSS_STATUS_NOTFOUND
;
125 ret
= _ldb_nss_init();
126 if (ret
!= NSS_STATUS_SUCCESS
) {
130 /* build the filter for this uid */
131 filter
= talloc_asprintf(_ldb_nss_ctx
, _LDB_NSS_PWUID_FILTER
, uid
);
132 if (filter
== NULL
) {
133 /* this is a fatal error */
134 *errnop
= errno
= ENOMEM
;
135 ret
= NSS_STATUS_UNAVAIL
;
139 /* search the entry */
140 ret
= ldb_search(_ldb_nss_ctx
->ldb
,
147 if (ret
!= LDB_SUCCESS
) {
148 /* this is a fatal error */
149 *errnop
= errno
= ENOENT
;
150 ret
= NSS_STATUS_UNAVAIL
;
154 /* if none found return */
155 if (res
->count
== 0) {
156 *errnop
= errno
= ENOENT
;
157 ret
= NSS_STATUS_NOTFOUND
;
161 if (res
->count
!= 1) {
162 /* this is a fatal error */
163 *errnop
= errno
= ENOENT
;
164 ret
= NSS_STATUS_UNAVAIL
;
168 /* fill in the passwd struct */
169 ret
= _ldb_nss_fill_passwd(result_buf
,
181 NSS_STATUS
_nss_ldb_getpwnam_r(const char *name
, struct passwd
*result_buf
, char *buffer
, size_t buflen
, int *errnop
)
185 struct ldb_result
*res
;
187 ret
= _ldb_nss_init();
188 if (ret
!= NSS_STATUS_SUCCESS
) {
192 /* build the filter for this name */
193 filter
= talloc_asprintf(_ldb_nss_ctx
, _LDB_NSS_PWNAM_FILTER
, name
);
194 if (filter
== NULL
) {
195 /* this is a fatal error */
196 *errnop
= errno
= ENOENT
;
197 ret
= NSS_STATUS_UNAVAIL
;
201 /* search the entry */
202 ret
= ldb_search(_ldb_nss_ctx
->ldb
,
209 if (ret
!= LDB_SUCCESS
) {
210 /* this is a fatal error */
211 *errnop
= errno
= ENOENT
;
212 ret
= NSS_STATUS_UNAVAIL
;
216 /* if none found return */
217 if (res
->count
== 0) {
218 *errnop
= errno
= ENOENT
;
219 ret
= NSS_STATUS_NOTFOUND
;
223 if (res
->count
!= 1) {
224 /* this is a fatal error */
225 *errnop
= errno
= ENOENT
;
226 ret
= NSS_STATUS_UNAVAIL
;
230 /* fill in the passwd struct */
231 ret
= _ldb_nss_fill_passwd(result_buf
,