~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source3/lib/ldb/nssldb/ldb-pwd.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   LDB nsswitch module
 
3
 
 
4
   Copyright (C) Simo Sorce 2006
 
5
   
 
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.
 
10
   
 
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.
 
15
   
 
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/>.
 
18
*/
 
19
 
 
20
#include "ldb-nss.h"
 
21
 
 
22
extern struct _ldb_nss_context *_ldb_nss_ctx;
 
23
 
 
24
const char *_ldb_nss_pw_attrs[] = {
 
25
        "uid",
 
26
        "userPassword",
 
27
        "uidNumber",
 
28
        "gidNumber",
 
29
        "gecos",
 
30
        "homeDirectory",
 
31
        "loginShell",
 
32
        NULL
 
33
};
 
34
 
 
35
NSS_STATUS _nss_ldb_setpwent(void)
 
36
{
 
37
        int ret;
 
38
        ret = _ldb_nss_init();
 
39
        if (ret != NSS_STATUS_SUCCESS) {
 
40
                return ret;
 
41
        }
 
42
 
 
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;
 
47
        }
 
48
 
 
49
        ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, 
 
50
                                         &_ldb_nss_ctx->pw_res,
 
51
                         _ldb_nss_ctx->base,
 
52
                         LDB_SCOPE_SUBTREE,
 
53
                         _ldb_nss_pw_attrs,
 
54
                         _LDB_NSS_PWENT_FILTER);
 
55
        if (ret != LDB_SUCCESS) {
 
56
                return NSS_STATUS_UNAVAIL;
 
57
        }
 
58
 
 
59
        return NSS_STATUS_SUCCESS;
 
60
}
 
61
 
 
62
NSS_STATUS _nss_ldb_endpwent(void)
 
63
{
 
64
        int ret;
 
65
 
 
66
        ret = _ldb_nss_init();
 
67
        if (ret != NSS_STATUS_SUCCESS) {
 
68
                return ret;
 
69
        }
 
70
 
 
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;
 
75
        }
 
76
 
 
77
        return NSS_STATUS_SUCCESS;
 
78
}
 
79
 
 
80
NSS_STATUS _nss_ldb_getpwent_r(struct passwd *result_buf,
 
81
                                char *buffer,
 
82
                                int buflen,
 
83
                                int *errnop)
 
84
{
 
85
        int ret;
 
86
 
 
87
        ret = _ldb_nss_init();
 
88
        if (ret != NSS_STATUS_SUCCESS) {
 
89
                return ret;
 
90
        }
 
91
 
 
92
        *errnop = 0;
 
93
 
 
94
        if (_ldb_nss_ctx->pw_cur >= _ldb_nss_ctx->pw_res->count) {
 
95
                /* already returned all entries */
 
96
                return NSS_STATUS_NOTFOUND;
 
97
        }
 
98
 
 
99
        ret = _ldb_nss_fill_passwd(result_buf,
 
100
                                   buffer,
 
101
                                   buflen,
 
102
                                   errnop,
 
103
                                   _ldb_nss_ctx->pw_res->msgs[_ldb_nss_ctx->pw_cur]);
 
104
        if (ret != NSS_STATUS_SUCCESS) {
 
105
                return ret;
 
106
        }
 
107
 
 
108
        _ldb_nss_ctx->pw_cur++;
 
109
 
 
110
        return NSS_STATUS_SUCCESS;
 
111
}
 
112
 
 
113
NSS_STATUS _nss_ldb_getpwuid_r(uid_t uid, struct passwd *result_buf, char *buffer, size_t buflen, int *errnop)
 
114
{
 
115
        int ret;
 
116
        struct ldb_result *res;
 
117
 
 
118
        if (uid == 0) { /* we don't serve root uid by policy */
 
119
                *errnop = errno = ENOENT;
 
120
                return NSS_STATUS_NOTFOUND;
 
121
        }
 
122
 
 
123
        ret = _ldb_nss_init();
 
124
        if (ret != NSS_STATUS_SUCCESS) {
 
125
                return ret;
 
126
        }
 
127
 
 
128
        /* search the entry */
 
129
        ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &res, 
 
130
                         _ldb_nss_ctx->base,
 
131
                         LDB_SCOPE_SUBTREE,
 
132
                         _ldb_nss_pw_attrs,
 
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;
 
138
                goto done;
 
139
        }
 
140
 
 
141
        /* if none found return */
 
142
        if (res->count == 0) {
 
143
                *errnop = errno = ENOENT;
 
144
                ret = NSS_STATUS_NOTFOUND;
 
145
                goto done;
 
146
        }
 
147
 
 
148
        if (res->count != 1) {
 
149
                /* this is a fatal error */
 
150
                *errnop = errno = ENOENT;
 
151
                ret = NSS_STATUS_UNAVAIL;
 
152
                goto done;
 
153
        }
 
154
 
 
155
        /* fill in the passwd struct */
 
156
        ret = _ldb_nss_fill_passwd(result_buf,
 
157
                                   buffer,
 
158
                                   buflen,
 
159
                                   errnop,
 
160
                                   res->msgs[0]);
 
161
 
 
162
done:
 
163
        talloc_free(res);
 
164
        return ret;
 
165
}
 
166
 
 
167
NSS_STATUS _nss_ldb_getpwnam_r(const char *name, struct passwd *result_buf, char *buffer, size_t buflen, int *errnop)
 
168
{
 
169
        int ret;
 
170
        struct ldb_result *res;
 
171
 
 
172
        ret = _ldb_nss_init();
 
173
        if (ret != NSS_STATUS_SUCCESS) {
 
174
                return ret;
 
175
        }
 
176
 
 
177
        /* search the entry */
 
178
        ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &res,
 
179
                         _ldb_nss_ctx->base,
 
180
                         LDB_SCOPE_SUBTREE,
 
181
                         _ldb_nss_pw_attrs,
 
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;
 
187
                goto done;
 
188
        }
 
189
 
 
190
        /* if none found return */
 
191
        if (res->count == 0) {
 
192
                *errnop = errno = ENOENT;
 
193
                ret = NSS_STATUS_NOTFOUND;
 
194
                goto done;
 
195
        }
 
196
 
 
197
        if (res->count != 1) {
 
198
                /* this is a fatal error */
 
199
                *errnop = errno = ENOENT;
 
200
                ret = NSS_STATUS_UNAVAIL;
 
201
                goto done;
 
202
        }
 
203
 
 
204
        /* fill in the passwd struct */
 
205
        ret = _ldb_nss_fill_passwd(result_buf,
 
206
                                   buffer,
 
207
                                   buflen,
 
208
                                   errnop,
 
209
                                   res->msgs[0]);
 
210
 
 
211
done:
 
212
        talloc_free(res);
 
213
        return ret;
 
214
}
 
215