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 Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 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 Library General Public
17 License along with this library; if not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
24 extern struct _ldb_nss_context
*_ldb_nss_ctx
;
26 const char *_ldb_nss_pw_attrs
[] = {
37 NSS_STATUS
_nss_ldb_setpwent(void)
40 ret
= _ldb_nss_init();
41 if (ret
!= NSS_STATUS_SUCCESS
) {
45 _ldb_nss_ctx
->pw_cur
= 0;
46 if (_ldb_nss_ctx
->pw_res
!= NULL
) {
47 talloc_free(_ldb_nss_ctx
->pw_res
);
48 _ldb_nss_ctx
->pw_res
= NULL
;
51 ret
= ldb_search(_ldb_nss_ctx
->ldb
,
54 _LDB_NSS_PWENT_FILTER
,
56 &_ldb_nss_ctx
->pw_res
);
57 if (ret
!= LDB_SUCCESS
) {
58 return NSS_STATUS_UNAVAIL
;
61 return NSS_STATUS_SUCCESS
;
64 NSS_STATUS
_nss_ldb_endpwent(void)
68 ret
= _ldb_nss_init();
69 if (ret
!= NSS_STATUS_SUCCESS
) {
73 _ldb_nss_ctx
->pw_cur
= 0;
74 if (_ldb_nss_ctx
->pw_res
) {
75 talloc_free(_ldb_nss_ctx
->pw_res
);
76 _ldb_nss_ctx
->pw_res
= NULL
;
79 return NSS_STATUS_SUCCESS
;
82 NSS_STATUS
_nss_ldb_getpwent_r(struct passwd
*result_buf
,
89 ret
= _ldb_nss_init();
90 if (ret
!= NSS_STATUS_SUCCESS
) {
96 if (_ldb_nss_ctx
->pw_cur
>= _ldb_nss_ctx
->pw_res
->count
) {
97 /* already returned all entries */
98 return NSS_STATUS_NOTFOUND
;
101 ret
= _ldb_nss_fill_passwd(result_buf
,
105 _ldb_nss_ctx
->pw_res
->msgs
[_ldb_nss_ctx
->pw_cur
]);
106 if (ret
!= NSS_STATUS_SUCCESS
) {
110 _ldb_nss_ctx
->pw_cur
++;
112 return NSS_STATUS_SUCCESS
;
115 NSS_STATUS
_nss_ldb_getpwuid_r(uid_t uid
, struct passwd
*result_buf
, char *buffer
, size_t buflen
, int *errnop
)
119 struct ldb_result
*res
;
121 if (uid
== 0) { /* we don't serve root uid by policy */
122 *errnop
= errno
= ENOENT
;
123 return NSS_STATUS_NOTFOUND
;
126 ret
= _ldb_nss_init();
127 if (ret
!= NSS_STATUS_SUCCESS
) {
131 /* build the filter for this uid */
132 filter
= talloc_asprintf(_ldb_nss_ctx
, _LDB_NSS_PWUID_FILTER
, uid
);
133 if (filter
== NULL
) {
134 /* this is a fatal error */
135 *errnop
= errno
= ENOMEM
;
136 ret
= NSS_STATUS_UNAVAIL
;
140 /* search the entry */
141 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
,
208 if (ret
!= LDB_SUCCESS
) {
209 /* this is a fatal error */
210 *errnop
= errno
= ENOENT
;
211 ret
= NSS_STATUS_UNAVAIL
;
215 /* if none found return */
216 if (res
->count
== 0) {
217 *errnop
= errno
= ENOENT
;
218 ret
= NSS_STATUS_NOTFOUND
;
222 if (res
->count
!= 1) {
223 /* this is a fatal error */
224 *errnop
= errno
= ENOENT
;
225 ret
= NSS_STATUS_UNAVAIL
;
229 /* fill in the passwd struct */
230 ret
= _ldb_nss_fill_passwd(result_buf
,