forked from amery/qmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpassword.c
291 lines (266 loc) · 7.58 KB
/
checkpassword.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright (c) 1998-2004 Claudio Jeker,
* Internet Business Solutions AG, CH-8005 Zürich, Switzerland
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Internet Business
* Solutions AG and its contributors.
* 4. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/types.h>
#include <unistd.h>
#include "auth_mod.h"
#include "auto_uids.h"
#include "byte.h"
#include "check.h"
#include "env.h"
#include "error.h"
#include "fmt.h"
#include "passwd.h"
#include "pbsexec.h"
#include "prot.h"
#include "qldap.h"
#include "qldap-debug.h"
#include "qldap-errno.h"
#include "qmail-ldap.h"
#include "scan.h"
#include "str.h"
#include "stralloc.h"
#ifdef QLDAP_CLUSTER
#include "qldap-cluster.h"
#endif
#include "checkpassword.h"
int
check(checkfunc *f, stralloc *login, stralloc *authdata,
struct credentials *c, int fast)
{
int i, r;
for (i = 0; f[i] != 0; i++)
switch (r = f[i](login, authdata, c, fast)) {
case OK:
case FORWARD:
return r;
case NOSUCH:
/* lets try an other backend */
break;
case BADPASS:
/* NOTE: users defined in two dbs are not allowed */
return BADPASS;
default:
return r;
}
return NOSUCH;
}
char num[FMT_ULONG];
int
check_ldap(stralloc *login, stralloc *authdata,
struct credentials *c, int fast)
{
static stralloc ld = {0};
qldap *q;
char *filter;
int r, status, pwok, needforward;
unsigned long count, size, max;
const char *attrs[] = {
LDAP_UID, /* the first 10 attrs are default */
LDAP_QMAILUID,
LDAP_QMAILGID,
LDAP_ISACTIVE,
LDAP_MAILHOST,
LDAP_MAILSTORE,
LDAP_HOMEDIR,
LDAP_QUOTA_SIZE,
LDAP_QUOTA_COUNT,
LDAP_MAXMSIZE,
LDAP_PASSWD, 0}; /* passwd is extra */
/* TODO more debug output is needed */
needforward = 0;
q = qldap_new();
if (q == 0)
return ERRNO;
r = qldap_open(q);
if (r != OK) goto fail;
r = qldap_bind(q, 0, 0);
if (r != OK) goto fail;
if (fast) {
/* just comapre passwords and account status */
attrs[0] = LDAP_ISACTIVE;
if (qldap_need_rebind() == 0) {
attrs[1] = LDAP_PASSWD;
attrs[2] = 0;
} else
attrs[1] = 0;
} else {
if (qldap_need_rebind() != 0)
attrs[10] = 0;
}
filter = filter_uid(login->s);
if (filter == 0) { r = ERRNO; goto fail; }
r = qldap_lookup(q, filter, attrs);
if (r != OK) goto fail;
r = qldap_get_status(q, &status);
if (r != OK) goto fail;
if (status == STATUS_BOUNCE || status == STATUS_NOACCESS ||
status == STATUS_DELETE) {
qldap_free(q);
return ACC_DISABLED;
}
if (!fast) {
#ifdef QLDAP_CLUSTER
r = qldap_get_attr(q, LDAP_MAILHOST, &c->forwarder,
SINGLE_VALUE);
if (r != OK && r != NOSUCH) goto fail;
if (r == OK && cluster(c->forwarder.s) == 1) {
/* hostname is different, so I reconnect */
logit(8, "check_ldap: forwarding session to %s\n",
c->forwarder.s);
needforward = 1;
}
#endif
r = qldap_get_uid(q, &c->uid);
if (r != OK) goto fail;
r = qldap_get_gid(q, &c->gid);
if (r != OK) goto fail;
r = qldap_get_mailstore(q, &c->home, &c->maildir);
if (r != OK) goto fail;
if (!stralloc_0(&c->home) ||
!stralloc_0(&c->maildir))
return ERRNO;
size = count = max = 0;
r = qldap_get_quota(q, &size, &count, &max);
if (r != OK) goto fail;
if (max != 0) {
num[fmt_ulong(num, max)] = 0;
if (!env_put2("DATASIZE", num))
auth_error(ERRNO);
}
if (size != 0 || count != 0) {
if (!stralloc_copys(&ld, "")) auth_error(ERRNO);
if (size != 0) {
if (!stralloc_catb(&ld, num,
fmt_ulong(num, size)))
auth_error(ERRNO);
if (!stralloc_append(&ld, "S"))
auth_error(ERRNO);
}
if (count != 0) {
if (size != 0)
if (!stralloc_append(&ld, ","))
auth_error(ERRNO);
if (!stralloc_catb(&ld, num,
fmt_ulong(num, count)))
auth_error(ERRNO);
if (!stralloc_append(&ld, "C"))
auth_error(ERRNO);
}
if (!stralloc_0(&ld)) auth_error(ERRNO);
if (!env_put2(ENV_QUOTA, ld.s )) auth_error(ERRNO);
}
}
if (qldap_need_rebind() == 0) {
r = qldap_get_attr(q, LDAP_PASSWD, &ld, SINGLE_VALUE);
if (r != OK) goto fail;
pwok = cmp_passwd(authdata->s, ld.s);
} else {
r = qldap_get_dn(q, &ld);
if (r != OK) goto fail;
r = qldap_rebind(q, ld.s, authdata->s);
switch (r) {
case OK:
pwok = OK;
break;
case LDAP_BIND_AUTH:
pwok = BADPASS;
break;
default:
pwok = r;
break;
}
}
logit(32, "check_ldap: password compare was %s\n",
pwok == OK?"successful":"not successful");
qldap_free(q);
if (pwok == OK && needforward == 1)
return FORWARD;
return pwok;
fail:
qldap_free(q);
return r;
}
void
change_uid(unsigned int uid, unsigned int gid)
{
unsigned int id;
id = geteuid();
if (id != 0 && (id == uid || uid == (unsigned int)-1)) {
/* not running as root so return */
logit(32, "change_uid: already running non root\n");
return;
}
if (uid == (unsigned int)-1 && gid == (unsigned int)-1) {
/* run as non-privileged user qmaild group nofiles */
uid = auto_uidd;
gid = auto_gidn;
}
/* first set the group id */
if (prot_gid(gid) == -1)
auth_error(ERRNO);
logit(32, "setgid succeeded (%i)\n", gid);
/* ... then the user id */
if (prot_uid(uid) == -1)
auth_error(ERRNO);
logit(32, "setuid succeeded (%i)\n", uid);
/* ... now check that we are realy not running as root */
if (!getuid())
auth_error(FAILED);
}
void
setup_env(char *user, struct credentials *c)
{
/* set up the environment for the execution of the subprogram */
if (!env_put2("USER", user))
auth_error(ERRNO);
/* only courier-imap needs this but we set it anyway */
if (!env_put2("AUTHENTICATED", user))
auth_error(ERRNO);
if (c->home.s != 0 && c->home.len > 0)
if (!env_put2("HOME", c->home.s))
auth_error(ERRNO);
if (c->maildir.s != 0 && c->maildir.len > 0) {
if (!env_put2("MAILDIR", c->maildir.s))
auth_error(ERRNO);
} else {
if (!env_unset("MAILDIR"))
auth_error(ERRNO);
}
logit(32, "environment successfully set: "
"USER %s, HOME %s, MAILDIR %s\n",
user, c->home.s != 0 && c->home.len > 0?
c->home.s:"unset, forwarding",
c->maildir.s != 0 && c->maildir.len > 0?
c->maildir.s:"unset, using aliasempty");
}