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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <openssl/md5.h>
#include "log.h"
#include "ppp.h"
#include "ppp_auth.h"
#include "ppp_lcp.h"
#include "pwdb.h"
#include "memdebug.h"
#define CHAP_CHALLENGE 1
#define CHAP_RESPONSE 2
#define CHAP_SUCCESS 3
#define CHAP_FAILURE 4
#define CHAP_MD5 5
#define VALUE_SIZE 16
#define MSG_FAILURE "Authentication failed"
#define MSG_SUCCESS "Authentication successed"
#define HDR_LEN (sizeof(struct chap_hdr_t)-2)
static int conf_timeout = 5;
static int conf_interval = 0;
static int conf_max_failure = 3;
static int urandom_fd;
struct chap_hdr_t
{
uint16_t proto;
uint8_t code;
uint8_t id;
uint16_t len;
} __attribute__((packed));
struct chap_challenge_t
{
struct chap_hdr_t hdr;
uint8_t val_size;
uint8_t val[VALUE_SIZE];
char name[0];
} __attribute__((packed));
struct chap_failure_t
{
struct chap_hdr_t hdr;
char message[sizeof(MSG_FAILURE)];
} __attribute__((packed));
struct chap_success_t
{
struct chap_hdr_t hdr;
char message[sizeof(MSG_SUCCESS)];
} __attribute__((packed));
struct chap_auth_data_t
{
struct auth_data_t auth;
struct ppp_handler_t h;
struct ppp_t *ppp;
int id;
uint8_t val[VALUE_SIZE];
struct triton_timer_t timeout;
struct triton_timer_t interval;
int failure;
int started:1;
};
static void chap_send_challenge(struct chap_auth_data_t *ad);
static void chap_recv(struct ppp_handler_t *h);
static void chap_timeout(struct triton_timer_t *t);
static void chap_restart(struct triton_timer_t *t);
static void print_buf(const uint8_t *buf, int size)
{
int i;
for (i=0; i < size; i++)
log_ppp_info("%x", buf[i]);
}
static void print_str(const char *buf, int size)
{
int i;
for (i = 0; i < size; i++)
log_ppp_info("%c", buf[i]);
}
static struct auth_data_t* auth_data_init(struct ppp_t *ppp)
{
struct chap_auth_data_t *d = _malloc(sizeof(*d));
memset(d, 0, sizeof(*d));
d->auth.proto = PPP_CHAP;
d->ppp = ppp;
return &d->auth;
}
static void auth_data_free(struct ppp_t *ppp, struct auth_data_t *auth)
{
struct chap_auth_data_t *d = container_of(auth, typeof(*d), auth);
_free(d);
}
static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth)
{
struct chap_auth_data_t *d = container_of(auth, typeof(*d), auth);
d->h.proto = PPP_CHAP;
d->h.recv = chap_recv;
d->timeout.expire = chap_timeout;
d->timeout.period = conf_timeout * 1000;
d->interval.expire = chap_restart;
d->interval.period = conf_interval * 1000;
ppp_register_chan_handler(ppp, &d->h);
chap_send_challenge(d);
return 0;
}
static int chap_finish(struct ppp_t *ppp, struct auth_data_t *auth)
{
struct chap_auth_data_t *d = container_of(auth, typeof(*d), auth);
if (d->timeout.tpd)
triton_timer_del(&d->timeout);
if (d->interval.tpd)
triton_timer_del(&d->interval);
ppp_unregister_handler(ppp, &d->h);
return 0;
}
static void chap_timeout(struct triton_timer_t *t)
{
struct chap_auth_data_t *d = container_of(t, typeof(*d), timeout);
if (conf_ppp_verbose)
log_ppp_warn("chap-md5: timeout\n");
if (++d->failure == conf_max_failure) {
if (d->started)
ppp_terminate(d->ppp, TERM_USER_ERROR, 0);
else
auth_failed(d->ppp);
} else {
--d->id;
chap_send_challenge(d);
}
}
static void chap_restart(struct triton_timer_t *t)
{
struct chap_auth_data_t *d = container_of(t, typeof(*d), interval);
chap_send_challenge(d);
}
static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr)
{
*ptr = CHAP_MD5;
return 1;
}
static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr)
{
if (*ptr == CHAP_MD5)
return LCP_OPT_ACK;
return LCP_OPT_NAK;
}
static void chap_send_failure(struct chap_auth_data_t *ad)
{
struct chap_failure_t msg = {
.hdr.proto = htons(PPP_CHAP),
.hdr.code = CHAP_FAILURE,
.hdr.id = ad->id,
.hdr.len = htons(sizeof(msg) - 1 - 2),
.message = MSG_FAILURE,
};
if (conf_ppp_verbose)
log_ppp_info("send [CHAP Failure id=%x \"%s\"]\n", msg.hdr.id, MSG_FAILURE);
ppp_chan_send(ad->ppp, &msg, ntohs(msg.hdr.len) + 2);
}
static void chap_send_success(struct chap_auth_data_t *ad)
{
struct chap_success_t msg = {
.hdr.proto = htons(PPP_CHAP),
.hdr.code = CHAP_SUCCESS,
.hdr.id = ad->id,
.hdr.len = htons(sizeof(msg)-1-2),
.message = MSG_SUCCESS,
};
if (conf_ppp_verbose)
log_ppp_info("send [CHAP Success id=%x \"%s\"]\n", msg.hdr.id, MSG_SUCCESS);
ppp_chan_send(ad->ppp, &msg, ntohs(msg.hdr.len) + 2);
}
static void chap_send_challenge(struct chap_auth_data_t *ad)
{
struct chap_challenge_t msg = {
.hdr.proto = htons(PPP_CHAP),
.hdr.code = CHAP_CHALLENGE,
.hdr.id = ++ad->id,
.hdr.len = htons(sizeof(msg) - 2),
.val_size = VALUE_SIZE,
};
read(urandom_fd, ad->val, VALUE_SIZE);
memcpy(msg.val, ad->val, VALUE_SIZE);
if (conf_ppp_verbose) {
log_ppp_info("send [CHAP Challenge id=%x <", msg.hdr.id);
print_buf(msg.val, VALUE_SIZE);
log_ppp_info(">]\n");
}
ppp_chan_send(ad->ppp, &msg, ntohs(msg.hdr.len) + 2);
if (conf_timeout && !ad->timeout.tpd)
triton_timer_add(ad->ppp->ctrl->ctx, &ad->timeout, 0);
}
static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr)
{
MD5_CTX md5_ctx;
uint8_t md5[MD5_DIGEST_LENGTH];
char *passwd;
char *name;
int r;
struct chap_challenge_t *msg = (struct chap_challenge_t*)hdr;
if (ad->timeout.tpd)
triton_timer_del(&ad->timeout);
if (conf_ppp_verbose) {
log_ppp_info("recv [CHAP Response id=%x <", msg->hdr.id);
print_buf(msg->val, msg->val_size);
log_ppp_info(">, name=\"");
print_str(msg->name, ntohs(msg->hdr.len) - sizeof(*msg) + 2);
log_ppp_info("\"]\n");
}
if (msg->hdr.id != ad->id) {
if (conf_ppp_verbose)
log_ppp_error("chap-md5: id mismatch\n");
chap_send_failure(ad);
ppp_terminate(ad->ppp, TERM_USER_ERROR, 0);
}
if (msg->val_size != VALUE_SIZE) {
log_ppp_error("chap-md5: incorrect value-size (%i)\n", msg->val_size);
chap_send_failure(ad);
ppp_terminate(ad->ppp, TERM_USER_ERROR, 0);
}
name = _strndup(msg->name,ntohs(msg->hdr.len) - sizeof(*msg) + 2);
r = pwdb_check(ad->ppp, name, PPP_CHAP, CHAP_MD5, ad->id, ad->val, VALUE_SIZE, msg->val);
if (r == PWDB_NO_IMPL) {
passwd = pwdb_get_passwd(ad->ppp,name);
if (!passwd)
{
_free(name);
if (conf_ppp_verbose)
log_ppp_warn("chap-md5: user not found\n");
chap_send_failure(ad);
return;
}
MD5_Init(&md5_ctx);
MD5_Update(&md5_ctx,&msg->hdr.id,1);
MD5_Update(&md5_ctx,passwd,strlen(passwd));
MD5_Update(&md5_ctx,ad->val,VALUE_SIZE);
MD5_Final(md5,&md5_ctx);
if (memcmp(md5,msg->val,sizeof(md5)))
{
if (conf_ppp_verbose)
log_ppp_warn("chap-md5: challenge response mismatch\n");
chap_send_failure(ad);
if (ad->started)
ppp_terminate(ad->ppp, TERM_USER_ERROR, 0);
else
auth_failed(ad->ppp);
}else
{
chap_send_success(ad);
if (!ad->started) {
ad->started = 1;
if (conf_interval)
triton_timer_add(ad->ppp->ctrl->ctx, &ad->interval, 0);
auth_successed(ad->ppp, name);
}
}
_free(passwd);
} else if (r == PWDB_DENIED) {
chap_send_failure(ad);
_free(name);
if (ad->started)
ppp_terminate(ad->ppp, TERM_USER_ERROR, 0);
else
auth_failed(ad->ppp);
} else {
chap_send_success(ad);
if (!ad->started) {
ad->started = 1;
if (conf_interval)
triton_timer_add(ad->ppp->ctrl->ctx, &ad->interval, 0);
auth_successed(ad->ppp, name);
}
}
}
static int chap_check(uint8_t *ptr)
{
return *ptr == CHAP_MD5;
}
static struct ppp_auth_handler_t chap=
{
.name = "CHAP-md5",
.init = auth_data_init,
.free = auth_data_free,
.send_conf_req = lcp_send_conf_req,
.recv_conf_req = lcp_recv_conf_req,
.start = chap_start,
.finish = chap_finish,
.check = chap_check,
};
static void chap_recv(struct ppp_handler_t *h)
{
struct chap_auth_data_t *d = container_of(h, typeof(*d), h);
struct chap_hdr_t *hdr = (struct chap_hdr_t *)d->ppp->chan_buf;
if (d->ppp->chan_buf_size < sizeof(*hdr) || ntohs(hdr->len) < HDR_LEN || ntohs(hdr->len) < d->ppp->chan_buf_size - 2) {
log_ppp_warn("chap-md5: short packet received\n");
return;
}
if (hdr->code == CHAP_RESPONSE)
chap_recv_response(d, hdr);
else
log_ppp_warn("chap-md5: unknown code received %x\n", hdr->code);
}
static void __init auth_chap_md5_init()
{
char *opt;
opt = conf_get_opt("auth", "timeout");
if (opt && atoi(opt) > 0)
conf_timeout = atoi(opt);
opt = conf_get_opt("auth", "interval");
if (opt && atoi(opt) > 0)
conf_interval = atoi(opt);
opt = conf_get_opt("auth", "max-failure");
if (opt && atoi(opt) > 0)
conf_max_failure = atoi(opt);
urandom_fd=open("/dev/urandom", O_RDONLY);
if (urandom_fd < 0) {
log_emerg("chap-md5: failed to open /dev/urandom: %s\n", strerror(errno));
return;
}
if (ppp_auth_register_handler(&chap))
log_emerg("chap-md5: failed to register handler\n");
}
|