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, _ldb_nss_ctx->ldb,
50
&_ldb_nss_ctx->pw_res,
54
_LDB_NSS_PWENT_FILTER);
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)
116
struct ldb_result *res;
118
if (uid == 0) { /* we don't serve root uid by policy */
119
*errnop = errno = ENOENT;
120
return NSS_STATUS_NOTFOUND;
123
ret = _ldb_nss_init();
124
if (ret != NSS_STATUS_SUCCESS) {
128
/* search the entry */
129
ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &res,
133
_LDB_NSS_PWUID_FILTER, uid);
134
if (ret != LDB_SUCCESS) {
135
/* this is a fatal error */
136
*errnop = errno = ENOENT;
137
ret = NSS_STATUS_UNAVAIL;
141
/* if none found return */
142
if (res->count == 0) {
143
*errnop = errno = ENOENT;
144
ret = NSS_STATUS_NOTFOUND;
148
if (res->count != 1) {
149
/* this is a fatal error */
150
*errnop = errno = ENOENT;
151
ret = NSS_STATUS_UNAVAIL;
155
/* fill in the passwd struct */
156
ret = _ldb_nss_fill_passwd(result_buf,
167
NSS_STATUS _nss_ldb_getpwnam_r(const char *name, struct passwd *result_buf, char *buffer, size_t buflen, int *errnop)
170
struct ldb_result *res;
172
ret = _ldb_nss_init();
173
if (ret != NSS_STATUS_SUCCESS) {
177
/* search the entry */
178
ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &res,
182
_LDB_NSS_PWNAM_FILTER, name);
183
if (ret != LDB_SUCCESS) {
184
/* this is a fatal error */
185
*errnop = errno = ENOENT;
186
ret = NSS_STATUS_UNAVAIL;
190
/* if none found return */
191
if (res->count == 0) {
192
*errnop = errno = ENOENT;
193
ret = NSS_STATUS_NOTFOUND;
197
if (res->count != 1) {
198
/* this is a fatal error */
199
*errnop = errno = ENOENT;
200
ret = NSS_STATUS_UNAVAIL;
204
/* fill in the passwd struct */
205
ret = _ldb_nss_fill_passwd(result_buf,