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 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 Library 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,
52 _LDB_NSS_PWENT_FILTER,
54 &_ldb_nss_ctx->pw_res);
55 if (ret != LDB_SUCCESS) {
56 return NSS_STATUS_UNAVAIL;
59 return NSS_STATUS_SUCCESS;
62 NSS_STATUS _nss_ldb_endpwent(void)
66 ret = _ldb_nss_init();
67 if (ret != NSS_STATUS_SUCCESS) {
71 _ldb_nss_ctx->pw_cur = 0;
72 if (_ldb_nss_ctx->pw_res) {
73 talloc_free(_ldb_nss_ctx->pw_res);
74 _ldb_nss_ctx->pw_res = NULL;
77 return NSS_STATUS_SUCCESS;
80 NSS_STATUS _nss_ldb_getpwent_r(struct passwd *result_buf,
87 ret = _ldb_nss_init();
88 if (ret != NSS_STATUS_SUCCESS) {
94 if (_ldb_nss_ctx->pw_cur >= _ldb_nss_ctx->pw_res->count) {
95 /* already returned all entries */
96 return NSS_STATUS_NOTFOUND;
99 ret = _ldb_nss_fill_passwd(result_buf,
103 _ldb_nss_ctx->pw_res->msgs[_ldb_nss_ctx->pw_cur]);
104 if (ret != NSS_STATUS_SUCCESS) {
108 _ldb_nss_ctx->pw_cur++;
110 return NSS_STATUS_SUCCESS;
113 NSS_STATUS _nss_ldb_getpwuid_r(uid_t uid, struct passwd *result_buf, char *buffer, size_t buflen, int *errnop)
117 struct ldb_result *res;
119 if (uid == 0) { /* we don't serve root uid by policy */
120 *errnop = errno = ENOENT;
121 return NSS_STATUS_NOTFOUND;
124 ret = _ldb_nss_init();
125 if (ret != NSS_STATUS_SUCCESS) {
129 /* build the filter for this uid */
130 filter = talloc_asprintf(_ldb_nss_ctx, _LDB_NSS_PWUID_FILTER, uid);
131 if (filter == NULL) {
132 /* this is a fatal error */
133 *errnop = errno = ENOMEM;
134 ret = NSS_STATUS_UNAVAIL;
138 /* search the entry */
139 ret = ldb_search(_ldb_nss_ctx->ldb,
145 if (ret != LDB_SUCCESS) {
146 /* this is a fatal error */
147 *errnop = errno = ENOENT;
148 ret = NSS_STATUS_UNAVAIL;
152 /* if none found return */
153 if (res->count == 0) {
154 *errnop = errno = ENOENT;
155 ret = NSS_STATUS_NOTFOUND;
159 if (res->count != 1) {
160 /* this is a fatal error */
161 *errnop = errno = ENOENT;
162 ret = NSS_STATUS_UNAVAIL;
166 /* fill in the passwd struct */
167 ret = _ldb_nss_fill_passwd(result_buf,
179 NSS_STATUS _nss_ldb_getpwnam_r(const char *name, struct passwd *result_buf, char *buffer, size_t buflen, int *errnop)
183 struct ldb_result *res;
185 ret = _ldb_nss_init();
186 if (ret != NSS_STATUS_SUCCESS) {
190 /* build the filter for this name */
191 filter = talloc_asprintf(_ldb_nss_ctx, _LDB_NSS_PWNAM_FILTER, name);
192 if (filter == NULL) {
193 /* this is a fatal error */
194 *errnop = errno = ENOENT;
195 ret = NSS_STATUS_UNAVAIL;
199 /* search the entry */
200 ret = ldb_search(_ldb_nss_ctx->ldb,
206 if (ret != LDB_SUCCESS) {
207 /* this is a fatal error */
208 *errnop = errno = ENOENT;
209 ret = NSS_STATUS_UNAVAIL;
213 /* if none found return */
214 if (res->count == 0) {
215 *errnop = errno = ENOENT;
216 ret = NSS_STATUS_NOTFOUND;
220 if (res->count != 1) {
221 /* this is a fatal error */
222 *errnop = errno = ENOENT;
223 ret = NSS_STATUS_UNAVAIL;
227 /* fill in the passwd struct */
228 ret = _ldb_nss_fill_passwd(result_buf,