-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendauth.c
348 lines (287 loc) · 8.71 KB
/
sendauth.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* $Id: sendauth.c,v 1.7 2009-03-17 18:40:20 heas Exp $
*
* Copyright (c) 1995-1998 by Cisco systems, Inc.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that this
* copyright and permission notice appear on all copies of the
* software and supporting documentation, the name of Cisco Systems,
* Inc. not be used in advertising or publicity pertaining to
* distribution of the program without specific prior permission, and
* notice be given in supporting documentation that modification,
* copying and distribution is by permission of Cisco Systems, Inc.
*
* Cisco Systems, Inc. makes no representations about the suitability
* of this software for any purpose. THIS SOFTWARE IS PROVIDED ``AS
* IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "tac_plus.h"
#include "expire.h"
#include "md5.h"
static int do_sendauth_fn();
static void outbound_chap();
#ifdef MSCHAP
static void outbound_mschap();
#endif /* MSCHAP */
void outbound_pap();
int
sendauth_fn(struct authen_data *data)
{
int status;
char *name, *p;
status = 0;
name = data->NAS_id->username;
if (STREQ(name, DEFAULT_USERNAME)) {
/* This username is only valid for authorization */
data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
} else {
status = do_sendauth_fn(data);
}
if (debug) {
switch (data->type) {
case TAC_PLUS_AUTHEN_TYPE_CHAP:
p = "chap";
break;
#ifdef MSCHAP
case TAC_PLUS_AUTHEN_TYPE_MSCHAP:
p = "ms-chap";
break;
#endif /* MSCHAP */
case TAC_PLUS_AUTHEN_TYPE_PAP:
p = "pap";
break;
default:
p = "unknown";
break;
}
report(LOG_INFO, "%s-sendauth query for '%s' %s from %s %s",
p,
name && name[0] ? name : "unknown",
session.peer, session.port,
(data->status == TAC_PLUS_AUTHEN_STATUS_PASS) ?
"accepted" : "rejected");
}
return(status);
}
/*
* For PAP we need to supply the outgoing PAP cleartext password.
* from the config file.
*
* For CHAP, we expect an id and a challenge. We will return an MD5 hash
* if we're successful,
*
* Return 0 if data->status is valid, otherwise 1
*/
static int
do_sendauth_fn(struct authen_data *data)
{
char *name, *exp_date;
data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
/* We must have a username */
if (!data->NAS_id->username[0]) {
/* Missing username is a gross error */
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
data->server_msg = tac_strdup("No username supplied");
report(LOG_ERR, "%s: No username for sendauth_fn", session.peer);
return(0);
}
name = data->NAS_id->username;
switch (data->type) {
case TAC_PLUS_AUTHEN_TYPE_CHAP:
outbound_chap(data);
break;
#ifdef MSCHAP
case TAC_PLUS_AUTHEN_TYPE_MSCHAP:
outbound_mschap(data);
break;
#endif /* MSCHAP */
case TAC_PLUS_AUTHEN_TYPE_PAP:
outbound_pap(data);
break;
default:
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
report(LOG_ERR, "%s %s: %s Illegal data type for sendauth_fn",
session.peer, session.port, name);
return(0);
}
exp_date = cfg_get_expires(name, TAC_PLUS_RECURSE);
set_expiration_status(exp_date, data);
return(0);
}
void
outbound_pap(struct authen_data *data)
{
char *secret, *p, *name;
name = data->NAS_id->username;
/* We must have a username */
if (!name) {
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
return;
}
/* Return her secret outbound PAP info */
secret = cfg_get_opap_secret(name, TAC_PLUS_RECURSE);
if (!secret) {
if (debug & DEBUG_AUTHEN_FLAG) {
report(LOG_ERR, "%s %s: No opap secret for %s",
session.peer, session.port, name);
}
data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
return;
}
p = tac_find_substring("cleartext ", secret);
if (!p) {
/* Should never happen */
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
report(LOG_ERR, "%s %s: Illegal opap secret format %s",
session.peer, session.port, secret);
return;
}
data->server_data = tac_strdup(p);
data->server_dlen = strlen(data->server_data);
data->status = TAC_PLUS_AUTHEN_STATUS_PASS;
}
static void
outbound_chap(struct authen_data *data)
{
char *name, *secret, *chal, digest[TAC_MD5_DIGEST_LEN];
char *p;
u_char *mdp;
char id;
int chal_len;
ssize_t inlen;
MD5_CTX mdcontext;
name = data->NAS_id->username;
if (!name) {
report(LOG_ERR, "%s %s: no username for outbound_chap",
session.peer, session.port);
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
return;
}
id = data->client_data[0];
chal_len = data->client_dlen - 1;
if (chal_len < 0) {
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
return;
}
if (debug & DEBUG_AUTHEN_FLAG) {
report(LOG_DEBUG, "%s %s: user %s, id=%d chal_len=%d",
session.peer, session.port, name, (int)id, chal_len);
}
/* Assume failure */
data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
/* Get the secret */
secret = cfg_get_chap_secret(name, TAC_PLUS_RECURSE);
/* If there is no chap password for this user, see if there is
a global password for her that we can use */
if (!secret) {
secret = cfg_get_global_secret(name, TAC_PLUS_RECURSE);
}
if (!secret) {
/* No secret. Fail */
if (debug & DEBUG_AUTHEN_FLAG) {
report(LOG_DEBUG, "%s %s: No chap or global secret for %s",
session.peer, session.port, name);
}
data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
return;
}
p = tac_find_substring("cleartext ", secret);
if (!p) {
/* Should never happen */
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
report(LOG_ERR, "%s %s: Illegal opap secret format %s",
session.peer, session.port, secret);
return;
}
secret = p;
/*
* We now have the secret, the id, and the challenge value.
* Put them all together, and run them through the MD5 digest
* algorithm. */
inlen = sizeof(u_char) + strlen(secret) + chal_len;
mdp = (u_char *)tac_malloc(inlen);
mdp[0] = id;
memcpy(&mdp[1], secret, strlen(secret));
chal = data->client_data + 1;
memcpy(mdp + strlen(secret) + 1, chal, chal_len);
MD5Init(&mdcontext);
MD5Update(&mdcontext, mdp, (int)inlen);
MD5Final((u_char *)digest, &mdcontext);
free(mdp);
/*
* Now return the calculated response value */
data->server_data = tac_malloc(TAC_MD5_DIGEST_LEN);
memcpy(data->server_data, digest, TAC_MD5_DIGEST_LEN);
data->server_dlen = TAC_MD5_DIGEST_LEN;
data->status = TAC_PLUS_AUTHEN_STATUS_PASS;
}
#ifdef MSCHAP
static void
outbound_mschap(struct authen_data *data)
{
char *name, *secret, *chal;
char *p;
char id;
int chal_len;
name = data->NAS_id->username;
if (!name) {
report(LOG_ERR, "%s %s: no username for outbound_mschap",
session.peer, session.port);
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
return;
}
id = data->client_data[0];
chal_len = data->client_dlen - 1;
if (data->client_dlen <= 2) {
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
return;
}
if (debug & DEBUG_AUTHEN_FLAG) {
report(LOG_DEBUG, "%s %s: user %s, id=%d chal_len=%d",
session.peer, session.port, name, (int)id, chal_len);
}
/* Assume failure */
data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
/* Get the secret */
secret = cfg_get_mschap_secret(name, TAC_PLUS_RECURSE);
/* If there is no chap password for this user, see if there is
a global password for her that we can use */
if (!secret) {
secret = cfg_get_global_secret(name, TAC_PLUS_RECURSE);
}
if (!secret) {
/* No secret. Fail */
if (debug & DEBUG_AUTHEN_FLAG) {
report(LOG_DEBUG, "%s %s: No ms-chap or global secret for %s",
session.peer, session.port, name);
}
data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
return;
}
p = tac_find_substring("cleartext ", secret);
if (!p) {
/* Should never happen */
data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
report(LOG_ERR, "%s %s: Illegal ms-chap secret format %s",
session.peer, session.port, secret);
return;
}
secret = p;
/*
* We now have the secret, the id, and the challenge value.
* Put them all together, and run them through the MD4 digest
* algorithm. */
chal = data->client_data + 1;
/*
* Now return the calculated response value */
data->server_data = tac_malloc(MSCHAP_DIGEST_LEN);
mschap_lmchallengeresponse(chal,secret,&data->server_data[0]);
mschap_ntchallengeresponse(chal,secret,&data->server_data[24]);
data->server_data[48] = 1; /* Mark it to use the NT response*/
data->server_dlen = MSCHAP_DIGEST_LEN;
data->status = TAC_PLUS_AUTHEN_STATUS_PASS;
}
#endif /* MSCHAP */