blob: 6c6c5fc2921d12912ed74988f057be5529af8c96 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * chap.c - Challenge Handshake Authentication Protocol.
3 *
4 * Copyright (c) 1993 The Australian National University.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the Australian National University. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * Copyright (c) 1991 Gregory M. Christy.
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms are permitted
23 * provided that the above copyright notice and this paragraph are
24 * duplicated in all such forms and that any documentation,
25 * advertising materials, and other materials related to such
26 * distribution and use acknowledge that the software was developed
27 * by Gregory M. Christy. The name of the author may not be used to
28 * endorse or promote products derived from this software without
29 * specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
33 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
34 */
35
36#define RCSID "$Id: chap.c,v 1.1 2008-08-04 06:11:51 winfred Exp $"
37
38/*
39 * TODO:
40 */
41
42#include <stdio.h>
43#include <string.h>
44#include <sys/types.h>
45#include <sys/time.h>
46
47#include <pppd.h>
48#include "chap.h"
49#include "md5.h"
50#include "fsm.h"
51#include "lcp.h"
52#ifdef CHAPMS
53#include "chap_ms.h"
54#endif
55
56static const char rcsid[] = RCSID;
57
58/*
59 * Command-line options.
60 */
61static option_t chap_option_list[] = {
62 { "chap-restart", o_int, &chap[0].timeouttime,
63 "Set timeout for CHAP", OPT_PRIO },
64 { "chap-max-challenge", o_int, &chap[0].max_transmits,
65 "Set max #xmits for challenge", OPT_PRIO },
66 { "chap-interval", o_int, &chap[0].chal_interval,
67 "Set interval for rechallenge", OPT_PRIO },
68#ifdef MSLANMAN
69 { "ms-lanman", o_bool, &ms_lanman,
70 "Use LanMan passwd when using MS-CHAP", 1 },
71#endif
72 { NULL }
73};
74
75/*
76 * Protocol entry points.
77 */
78static void ChapInit __P((int));
79static void ChapLowerUp __P((int));
80static void ChapLowerDown __P((int));
81static void ChapInput __P((int, u_char *, int));
82static void ChapProtocolReject __P((int));
83static int ChapPrintPkt __P((u_char *, int,
84 void (*) __P((void *, char *, ...)), void *));
85
86struct protent chap_protent = {
87 PPP_CHAP,
88 ChapInit,
89 ChapInput,
90 ChapProtocolReject,
91 ChapLowerUp,
92 ChapLowerDown,
93 NULL,
94 NULL,
95 ChapPrintPkt,
96 NULL,
97 1,
98 "CHAP",
99 NULL,
100 chap_option_list,
101 NULL,
102 NULL,
103 NULL
104};
105
106chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
107
108static void ChapChallengeTimeout __P((void *));
109static void ChapResponseTimeout __P((void *));
110static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
111static void ChapRechallenge __P((void *));
112static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
113static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
114static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
115static void ChapSendStatus __P((chap_state *, int));
116static void ChapSendChallenge __P((chap_state *));
117static void ChapSendResponse __P((chap_state *));
118void ChapGenChallenge __P((chap_state *));
119
120extern double drand48 __P((void));
121extern void srand48 __P((long));
122
123/*
124 * ChapInit - Initialize a CHAP unit.
125 */
126static void
127ChapInit(unit)
128 int unit;
129{
130 chap_state *cstate = &chap[unit];
131
132 BZERO(cstate, sizeof(*cstate));
133 cstate->unit = unit;
134 cstate->clientstate = CHAPCS_INITIAL;
135 cstate->serverstate = CHAPSS_INITIAL;
136 cstate->timeouttime = CHAP_DEFTIMEOUT;
137 cstate->max_transmits = CHAP_DEFTRANSMITS;
138 /* random number generator is initialized in magic_init */
139}
140
141
142/*
143 * ChapAuthWithPeer - Authenticate us with our peer (start client).
144 *
145 */
146void
147ChapAuthWithPeer(unit, our_name, digest)
148 int unit;
149 char *our_name;
150 int digest;
151{
152 chap_state *cstate = &chap[unit];
153
154 cstate->resp_name = our_name;
155 cstate->resp_type = digest;
156
157 if (cstate->clientstate == CHAPCS_INITIAL ||
158 cstate->clientstate == CHAPCS_PENDING) {
159 /* lower layer isn't up - wait until later */
160 cstate->clientstate = CHAPCS_PENDING;
161 return;
162 }
163
164 /*
165 * We get here as a result of LCP coming up.
166 * So even if CHAP was open before, we will
167 * have to re-authenticate ourselves.
168 */
169 cstate->clientstate = CHAPCS_LISTEN;
170}
171
172
173/*
174 * ChapAuthPeer - Authenticate our peer (start server).
175 */
176void
177ChapAuthPeer(unit, our_name, digest)
178 int unit;
179 char *our_name;
180 int digest;
181{
182 chap_state *cstate = &chap[unit];
183
184 cstate->chal_name = our_name;
185 cstate->chal_type = digest;
186
187 if (cstate->serverstate == CHAPSS_INITIAL ||
188 cstate->serverstate == CHAPSS_PENDING) {
189 /* lower layer isn't up - wait until later */
190 cstate->serverstate = CHAPSS_PENDING;
191 return;
192 }
193
194 ChapGenChallenge(cstate);
195 ChapSendChallenge(cstate); /* crank it up dude! */
196 cstate->serverstate = CHAPSS_INITIAL_CHAL;
197}
198
199
200/*
201 * ChapChallengeTimeout - Timeout expired on sending challenge.
202 */
203static void
204ChapChallengeTimeout(arg)
205 void *arg;
206{
207 chap_state *cstate = (chap_state *) arg;
208
209 /* if we aren't sending challenges, don't worry. then again we */
210 /* probably shouldn't be here either */
211 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
212 cstate->serverstate != CHAPSS_RECHALLENGE)
213 return;
214
215 if (cstate->chal_transmits >= cstate->max_transmits) {
216 /* give up on peer */
217 error("Peer failed to respond to CHAP challenge");
218 cstate->serverstate = CHAPSS_BADAUTH;
219 auth_peer_fail(cstate->unit, PPP_CHAP);
220 return;
221 }
222
223 ChapSendChallenge(cstate); /* Re-send challenge */
224}
225
226
227/*
228 * ChapResponseTimeout - Timeout expired on sending response.
229 */
230static void
231ChapResponseTimeout(arg)
232 void *arg;
233{
234 chap_state *cstate = (chap_state *) arg;
235
236 /* if we aren't sending a response, don't worry. */
237 if (cstate->clientstate != CHAPCS_RESPONSE)
238 return;
239
240 ChapSendResponse(cstate); /* re-send response */
241}
242
243
244/*
245 * ChapRechallenge - Time to challenge the peer again.
246 */
247static void
248ChapRechallenge(arg)
249 void *arg;
250{
251 chap_state *cstate = (chap_state *) arg;
252
253 /* if we aren't sending a response, don't worry. */
254 if (cstate->serverstate != CHAPSS_OPEN)
255 return;
256
257 ChapGenChallenge(cstate);
258 ChapSendChallenge(cstate);
259 cstate->serverstate = CHAPSS_RECHALLENGE;
260}
261
262
263/*
264 * ChapLowerUp - The lower layer is up.
265 *
266 * Start up if we have pending requests.
267 */
268static void
269ChapLowerUp(unit)
270 int unit;
271{
272 chap_state *cstate = &chap[unit];
273
274 if (cstate->clientstate == CHAPCS_INITIAL)
275 cstate->clientstate = CHAPCS_CLOSED;
276 else if (cstate->clientstate == CHAPCS_PENDING)
277 cstate->clientstate = CHAPCS_LISTEN;
278
279 if (cstate->serverstate == CHAPSS_INITIAL)
280 cstate->serverstate = CHAPSS_CLOSED;
281 else if (cstate->serverstate == CHAPSS_PENDING) {
282 ChapGenChallenge(cstate);
283 ChapSendChallenge(cstate);
284 cstate->serverstate = CHAPSS_INITIAL_CHAL;
285 }
286}
287
288
289/*
290 * ChapLowerDown - The lower layer is down.
291 *
292 * Cancel all timeouts.
293 */
294static void
295ChapLowerDown(unit)
296 int unit;
297{
298 chap_state *cstate = &chap[unit];
299
300 /* Timeout(s) pending? Cancel if so. */
301 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
302 cstate->serverstate == CHAPSS_RECHALLENGE)
303 UNTIMEOUT(ChapChallengeTimeout, cstate);
304 else if (cstate->serverstate == CHAPSS_OPEN
305 && cstate->chal_interval != 0)
306 UNTIMEOUT(ChapRechallenge, cstate);
307 if (cstate->clientstate == CHAPCS_RESPONSE)
308 UNTIMEOUT(ChapResponseTimeout, cstate);
309
310 cstate->clientstate = CHAPCS_INITIAL;
311 cstate->serverstate = CHAPSS_INITIAL;
312}
313
314
315/*
316 * ChapProtocolReject - Peer doesn't grok CHAP.
317 */
318static void
319ChapProtocolReject(unit)
320 int unit;
321{
322 chap_state *cstate = &chap[unit];
323
324 if (cstate->serverstate != CHAPSS_INITIAL &&
325 cstate->serverstate != CHAPSS_CLOSED)
326 auth_peer_fail(unit, PPP_CHAP);
327 if (cstate->clientstate != CHAPCS_INITIAL &&
328 cstate->clientstate != CHAPCS_CLOSED)
329 auth_withpeer_fail(unit, PPP_CHAP);
330 ChapLowerDown(unit); /* shutdown chap */
331}
332
333
334/*
335 * ChapInput - Input CHAP packet.
336 */
337static void
338ChapInput(unit, inpacket, packet_len)
339 int unit;
340 u_char *inpacket;
341 int packet_len;
342{
343 chap_state *cstate = &chap[unit];
344 u_char *inp;
345 u_char code, id;
346 int len;
347
348 /*
349 * Parse header (code, id and length).
350 * If packet too short, drop it.
351 */
352 inp = inpacket;
353 if (packet_len < CHAP_HEADERLEN) {
354 CHAPDEBUG(("ChapInput: rcvd short header."));
355 return;
356 }
357 GETCHAR(code, inp);
358 GETCHAR(id, inp);
359 GETSHORT(len, inp);
360 if (len < CHAP_HEADERLEN) {
361 CHAPDEBUG(("ChapInput: rcvd illegal length."));
362 return;
363 }
364 if (len > packet_len) {
365 CHAPDEBUG(("ChapInput: rcvd short packet."));
366 return;
367 }
368 len -= CHAP_HEADERLEN;
369
370 /*
371 * Action depends on code (as in fact it usually does :-).
372 */
373 switch (code) {
374 case CHAP_CHALLENGE:
375 ChapReceiveChallenge(cstate, inp, id, len);
376 break;
377
378 case CHAP_RESPONSE:
379 ChapReceiveResponse(cstate, inp, id, len);
380 break;
381
382 case CHAP_FAILURE:
383 ChapReceiveFailure(cstate, inp, id, len);
384 break;
385
386 case CHAP_SUCCESS:
387 ChapReceiveSuccess(cstate, inp, id, len);
388 break;
389
390 default: /* Need code reject? */
391 warn("Unknown CHAP code (%d) received.", code);
392 break;
393 }
394}
395
396
397/*
398 * ChapReceiveChallenge - Receive Challenge and send Response.
399 */
400static void
401ChapReceiveChallenge(cstate, inp, id, len)
402 chap_state *cstate;
403 u_char *inp;
404 int id;
405 int len;
406{
407 int rchallenge_len;
408 u_char *rchallenge;
409 int secret_len;
410 char secret[MAXSECRETLEN];
411 char rhostname[256];
412 MD5_CTX mdContext;
413 u_char hash[MD5_SIGNATURE_SIZE];
414
415 if (cstate->clientstate == CHAPCS_CLOSED ||
416 cstate->clientstate == CHAPCS_PENDING) {
417 CHAPDEBUG(("ChapReceiveChallenge: in state %d", cstate->clientstate));
418 return;
419 }
420
421 if (len < 2) {
422 CHAPDEBUG(("ChapReceiveChallenge: rcvd short packet."));
423 return;
424 }
425
426 GETCHAR(rchallenge_len, inp);
427 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
428 if (len < 0) {
429 CHAPDEBUG(("ChapReceiveChallenge: rcvd short packet."));
430 return;
431 }
432 rchallenge = inp;
433 INCPTR(rchallenge_len, inp);
434
435 if (len >= sizeof(rhostname))
436 len = sizeof(rhostname) - 1;
437 BCOPY(inp, rhostname, len);
438 rhostname[len] = '\000';
439
440 /* Microsoft doesn't send their name back in the PPP packet */
441 if (explicit_remote || (remote_name[0] != 0 && rhostname[0] == 0)) {
442 strlcpy(rhostname, remote_name, sizeof(rhostname));
443 CHAPDEBUG(("ChapReceiveChallenge: using '%q' as remote name",
444 rhostname));
445 }
446
447 /* get secret for authenticating ourselves with the specified host */
448 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
449 secret, &secret_len, 0)) {
450 secret_len = 0; /* assume null secret if can't find one */
451 warn("No CHAP secret found for authenticating us to %q", rhostname);
452 }
453
454 /* cancel response send timeout if necessary */
455 if (cstate->clientstate == CHAPCS_RESPONSE)
456 UNTIMEOUT(ChapResponseTimeout, cstate);
457
458 cstate->resp_id = id;
459 cstate->resp_transmits = 0;
460
461 /* generate MD based on negotiated type */
462 switch (cstate->resp_type) {
463
464 case CHAP_DIGEST_MD5:
465 CHAPDEBUG(("ChapReceiveChallenge: rcvd type CHAP-DIGEST-MD5"));
466 MD5Init(&mdContext);
467 MD5Update(&mdContext, &cstate->resp_id, 1);
468 MD5Update(&mdContext, secret, secret_len);
469 MD5Update(&mdContext, rchallenge, rchallenge_len);
470 MD5Final(hash, &mdContext);
471 BCOPY(hash, cstate->response, MD5_SIGNATURE_SIZE);
472 cstate->resp_length = MD5_SIGNATURE_SIZE;
473 break;
474
475#ifdef CHAPMS
476 case CHAP_MICROSOFT:
477 CHAPDEBUG(("ChapReceiveChallenge: rcvd type MS-CHAP-V1."));
478 if(rchallenge_len != 8)
479 {
480 CHAPDEBUG(("Invalid challenge length for MS-CHAP-V1"));
481 return;
482 }
483 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
484 break;
485
486 case CHAP_MICROSOFT_V2:
487 CHAPDEBUG(("ChapReceiveChallenge: rcvd type MS-CHAP-V2."));
488 if(rchallenge_len != 16)
489 {
490 CHAPDEBUG(("Invalid challenge length for MS-CHAP-V2"));
491 return;
492 }
493 ChapMS_v2(cstate, rchallenge, rchallenge_len, secret, secret_len);
494 break;
495#endif
496
497 default:
498 CHAPDEBUG(("unknown digest type %d", cstate->resp_type));
499 return;
500 }
501
502 BZERO(secret, sizeof(secret));
503 ChapSendResponse(cstate);
504}
505
506
507/*
508 * ChapReceiveResponse - Receive and process response.
509 */
510static void
511ChapReceiveResponse(cstate, inp, id, len)
512 chap_state *cstate;
513 u_char *inp;
514 int id;
515 int len;
516{
517 u_char *remmd, remmd_len;
518 int secret_len, old_state;
519 int code;
520 char rhostname[256];
521 MD5_CTX mdContext;
522 char secret[MAXSECRETLEN];
523 u_char hash[MD5_SIGNATURE_SIZE];
524
525 if (cstate->serverstate == CHAPSS_CLOSED ||
526 cstate->serverstate == CHAPSS_PENDING) {
527 CHAPDEBUG(("ChapReceiveResponse: in state %d", cstate->serverstate));
528 return;
529 }
530
531 if (id != cstate->chal_id)
532 return; /* doesn't match ID of last challenge */
533
534 /*
535 * If we have received a duplicate or bogus Response,
536 * we have to send the same answer (Success/Failure)
537 * as we did for the first Response we saw.
538 */
539 if (cstate->serverstate == CHAPSS_OPEN) {
540 ChapSendStatus(cstate, CHAP_SUCCESS);
541 return;
542 }
543 if (cstate->serverstate == CHAPSS_BADAUTH) {
544 ChapSendStatus(cstate, CHAP_FAILURE);
545 return;
546 }
547
548 if (len < 2) {
549 CHAPDEBUG(("ChapReceiveResponse: rcvd short packet."));
550 return;
551 }
552 GETCHAR(remmd_len, inp); /* get length of MD */
553 remmd = inp; /* get pointer to MD */
554 INCPTR(remmd_len, inp);
555
556 len -= sizeof (u_char) + remmd_len;
557 if (len < 0) {
558 CHAPDEBUG(("ChapReceiveResponse: rcvd short packet."));
559 return;
560 }
561
562 UNTIMEOUT(ChapChallengeTimeout, cstate);
563
564 if (len >= sizeof(rhostname))
565 len = sizeof(rhostname) - 1;
566 BCOPY(inp, rhostname, len);
567 rhostname[len] = '\000';
568
569 /*
570 * Get secret for authenticating them with us,
571 * do the hash ourselves, and compare the result.
572 */
573 code = CHAP_FAILURE;
574 if (!get_secret(cstate->unit, (explicit_remote? remote_name: rhostname),
575 cstate->chal_name, secret, &secret_len, 1)) {
576 warn("No CHAP secret found for authenticating %q", rhostname);
577 } else {
578
579 /* generate MD based on negotiated type */
580 switch (cstate->chal_type) {
581
582 case CHAP_DIGEST_MD5:
583 CHAPDEBUG(("ChapReceiveResponse: rcvd type CHAP-DIGEST-MD5"));
584 if (remmd_len != MD5_SIGNATURE_SIZE)
585 break; /* it's not even the right length */
586 MD5Init(&mdContext);
587 MD5Update(&mdContext, &cstate->chal_id, 1);
588 MD5Update(&mdContext, secret, secret_len);
589 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
590 MD5Final(hash, &mdContext);
591
592 /* compare local and remote MDs and send the appropriate status */
593 if (memcmp (hash, remmd, MD5_SIGNATURE_SIZE) == 0)
594 code = CHAP_SUCCESS; /* they are the same! */
595 break;
596
597#ifdef CHAPMS
598 case CHAP_MICROSOFT:
599 CHAPDEBUG(("ChapReceiveResponse: rcvd type MS-CHAP-V1"));
600 if(remmd_len != MS_CHAP_RESPONSE_LEN)
601 break;
602 if(ChapMS_Resp(cstate, secret, secret_len, remmd) == 0)
603 code = CHAP_SUCCESS;
604 break;
605
606 case CHAP_MICROSOFT_V2:
607 CHAPDEBUG(("ChapReceiveResponse: rcvd type MS-CHAP-V2"));
608 if(remmd_len != MS_CHAP_RESPONSE_LEN)
609 break;
610 if(ChapMS_v2_Resp(cstate,secret,secret_len,remmd,rhostname) == 0)
611 {
612 code = CHAP_SUCCESS_R;
613 ChapMS_v2_Auth(cstate, secret, secret_len, remmd, rhostname);
614 }
615 break;
616#endif
617
618 default:
619 CHAPDEBUG(("unknown digest type %d", cstate->chal_type));
620 }
621 }
622
623 BZERO(secret, sizeof(secret));
624 ChapSendStatus(cstate, code);
625
626 if ((code == CHAP_SUCCESS) || (code == CHAP_SUCCESS_R)) {
627 old_state = cstate->serverstate;
628 cstate->serverstate = CHAPSS_OPEN;
629 if (old_state == CHAPSS_INITIAL_CHAL) {
630 auth_peer_success(cstate->unit, PPP_CHAP, rhostname, len);
631 }
632 if (cstate->chal_interval != 0)
633 TIMEOUT(ChapRechallenge, cstate, cstate->chal_interval);
634 switch (cstate->chal_type) {
635 case CHAP_DIGEST_MD5:
636 notice("CHAP peer authentication succeeded for %q", rhostname);
637 break;
638#ifdef CHAPMS
639 case CHAP_MICROSOFT:
640 notice("MSCHAP peer authentication succeeded for %q", rhostname);
641 break;
642 case CHAP_MICROSOFT_V2:
643 notice("MSCHAP-v2 peer authentication succeeded for %q", rhostname);
644 break;
645#endif
646 default:
647 notice("CHAP (unknown) peer authentication succeeded for %q",
648 rhostname);
649 break;
650 }
651 } else {
652 switch (cstate->chal_type) {
653 case CHAP_DIGEST_MD5:
654 error("CHAP peer authentication failed for remote host %q",
655 rhostname);
656 break;
657#ifdef CHAPMS
658 case CHAP_MICROSOFT:
659 error("MSCHAP peer authentication failed for remote host %q",
660 rhostname);
661 break;
662 case CHAP_MICROSOFT_V2:
663 error("MSCHAP-v2 peer authentication failed for remote host %q",
664 rhostname);
665 break;
666#endif
667 default:
668 error("CHAP (unknown) peer authentication failed for remote host %q", rhostname);
669 break;
670 }
671 cstate->serverstate = CHAPSS_BADAUTH;
672 auth_peer_fail(cstate->unit, PPP_CHAP);
673 }
674}
675
676/*
677 * ChapReceiveSuccess - Receive Success
678 */
679static void
680ChapReceiveSuccess(cstate, inp, id, len)
681 chap_state *cstate;
682 u_char *inp;
683 u_char id;
684 int len;
685{
686
687 if (cstate->clientstate == CHAPCS_OPEN)
688 /* presumably an answer to a duplicate response */
689 return;
690
691 if (cstate->clientstate != CHAPCS_RESPONSE) {
692 /* don't know what this is */
693 CHAPDEBUG(("ChapReceiveSuccess: in state %d\n", cstate->clientstate));
694 return;
695 }
696
697 UNTIMEOUT(ChapResponseTimeout, cstate);
698
699 /*
700 * Print message.
701 */
702 if (len > 0)
703 PRINTMSG(inp, len);
704
705 cstate->clientstate = CHAPCS_OPEN;
706
707 auth_withpeer_success(cstate->unit, PPP_CHAP);
708}
709
710
711/*
712 * ChapReceiveFailure - Receive failure.
713 */
714static void
715ChapReceiveFailure(cstate, inp, id, len)
716 chap_state *cstate;
717 u_char *inp;
718 u_char id;
719 int len;
720{
721 if (cstate->clientstate != CHAPCS_RESPONSE) {
722 /* don't know what this is */
723 CHAPDEBUG(("ChapReceiveFailure: in state %d\n", cstate->clientstate));
724 return;
725 }
726
727 UNTIMEOUT(ChapResponseTimeout, cstate);
728
729 /*
730 * Print message.
731 */
732 if (len > 0)
733 PRINTMSG(inp, len);
734
735 error("CHAP authentication failed");
736
737 //log_to_file("CHAP_AUTH_FAIL");
738 system("ppp_event -t CHAP_AUTH_FAIL &");
739
740 auth_withpeer_fail(cstate->unit, PPP_CHAP);
741}
742
743
744/*
745 * ChapSendChallenge - Send an Authenticate challenge.
746 */
747static void
748ChapSendChallenge(cstate)
749 chap_state *cstate;
750{
751 u_char *outp;
752 int chal_len, name_len;
753 int outlen;
754
755 chal_len = cstate->chal_len;
756 name_len = strlen(cstate->chal_name);
757 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
758 outp = outpacket_buf;
759
760 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
761
762 PUTCHAR(CHAP_CHALLENGE, outp);
763 PUTCHAR(cstate->chal_id, outp);
764 PUTSHORT(outlen, outp);
765
766 PUTCHAR(chal_len, outp); /* put length of challenge */
767 BCOPY(cstate->challenge, outp, chal_len);
768 INCPTR(chal_len, outp);
769
770 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
771
772 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
773
774 TIMEOUT(ChapChallengeTimeout, cstate, cstate->timeouttime);
775 ++cstate->chal_transmits;
776}
777
778
779/*
780 * ChapSendStatus - Send a status response (ack or nak).
781 */
782static void
783ChapSendStatus(cstate, code)
784 chap_state *cstate;
785 int code;
786{
787 u_char *outp;
788 int outlen, msglen;
789 char msg[256];
790
791 if (code == CHAP_SUCCESS)
792 slprintf(msg, sizeof(msg), "Welcome to %s.", hostname);
793 else if(code == CHAP_SUCCESS_R)
794 strcpy(msg, cstate->response);
795 else
796 slprintf(msg, sizeof(msg), "I don't like you. Go 'way.");
797 msglen = strlen(msg);
798
799 outlen = CHAP_HEADERLEN + msglen;
800 outp = outpacket_buf;
801
802 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
803
804 PUTCHAR(code == CHAP_SUCCESS_R ? CHAP_SUCCESS : code, outp);
805 PUTCHAR(cstate->chal_id, outp);
806 PUTSHORT(outlen, outp);
807 BCOPY(msg, outp, msglen);
808 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
809}
810
811/*
812 * ChapGenChallenge is used to generate a pseudo-random challenge string of
813 * a pseudo-random length between min_len and max_len. The challenge
814 * string and its length are stored in *cstate, and various other fields of
815 * *cstate are initialized.
816 */
817
818void
819ChapGenChallenge(cstate)
820 chap_state *cstate;
821{
822 int chal_len;
823 u_char *ptr = cstate->challenge;
824 int i;
825
826#ifdef CHAPMS
827 if(cstate->chal_type == CHAP_MICROSOFT)
828 chal_len = 8;
829 else if(cstate->chal_type == CHAP_MICROSOFT_V2)
830 chal_len = 16;
831 else
832#endif
833
834 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
835 MAX_CHALLENGE_LENGTH */
836 chal_len = (unsigned) ((drand48() *
837 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
838 MIN_CHALLENGE_LENGTH);
839 cstate->chal_len = chal_len;
840 cstate->chal_id = ++cstate->id;
841 cstate->chal_transmits = 0;
842
843 /* generate a random string */
844 for (i = 0; i < chal_len; i++)
845 *ptr++ = (char) (drand48() * 0xff);
846}
847
848/*
849 * ChapSendResponse - send a response packet with values as specified
850 * in *cstate.
851 */
852/* ARGSUSED */
853static void
854ChapSendResponse(cstate)
855 chap_state *cstate;
856{
857 u_char *outp;
858 int outlen, md_len, name_len;
859
860 md_len = cstate->resp_length;
861 name_len = strlen(cstate->resp_name);
862 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
863 outp = outpacket_buf;
864
865 MAKEHEADER(outp, PPP_CHAP);
866
867 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
868 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
869 PUTSHORT(outlen, outp); /* packet length */
870
871 PUTCHAR(md_len, outp); /* length of MD */
872 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
873 INCPTR(md_len, outp);
874
875 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
876
877 /* send the packet */
878 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
879
880 cstate->clientstate = CHAPCS_RESPONSE;
881 TIMEOUT(ChapResponseTimeout, cstate, cstate->timeouttime);
882 ++cstate->resp_transmits;
883}
884
885/*
886 * ChapPrintPkt - print the contents of a CHAP packet.
887 */
888static char *ChapCodenames[] = {
889 "Challenge", "Response", "Success", "Failure"
890};
891
892static int
893ChapPrintPkt(p, plen, printer, arg)
894 u_char *p;
895 int plen;
896 void (*printer) __P((void *, char *, ...));
897 void *arg;
898{
899 int code, id, len;
900 int clen, nlen;
901 u_char x;
902
903 if (plen < CHAP_HEADERLEN)
904 return 0;
905 GETCHAR(code, p);
906 GETCHAR(id, p);
907 GETSHORT(len, p);
908 if (len < CHAP_HEADERLEN || len > plen)
909 return 0;
910
911 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
912 printer(arg, " %s", ChapCodenames[code-1]);
913 else
914 printer(arg, " code=0x%x", code);
915 printer(arg, " id=0x%x", id);
916 len -= CHAP_HEADERLEN;
917 switch (code) {
918 case CHAP_CHALLENGE:
919 case CHAP_RESPONSE:
920 if (len < 1)
921 break;
922 clen = p[0];
923 if (len < clen + 1)
924 break;
925 ++p;
926 nlen = len - clen - 1;
927 printer(arg, " <");
928 for (; clen > 0; --clen) {
929 GETCHAR(x, p);
930 printer(arg, "%.2x", x);
931 }
932 printer(arg, ">, name = ");
933 print_string((char *)p, nlen, printer, arg);
934 break;
935 case CHAP_FAILURE:
936 case CHAP_SUCCESS:
937 printer(arg, " ");
938 print_string((char *)p, len, printer, arg);
939 break;
940 default:
941 for (clen = len; clen > 0; --clen) {
942 GETCHAR(x, p);
943 printer(arg, " %.2x", x);
944 }
945 }
946
947 return len + CHAP_HEADERLEN;
948}
949
950int
951reqchap(argv)
952 char **argv;
953{
954 lcp_wantoptions[0].neg_chap = 1;
955 lcp_wantoptions[0].use_digest = 1;
956 auth_required = 1;
957 return 1;
958}