2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/usr.sbin/ppp/chap.c,v 1.61.2.7 2002/09/01 02:12:23 brian Exp $
31 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <sys/socket.h>
66 #include "throughput.h"
67 #include "descriptor.h"
70 #include "slcompress.h"
94 static const char * const chapcodes
[] = {
95 "???", "CHALLENGE", "RESPONSE", "SUCCESS", "FAILURE"
97 #define MAXCHAPCODE (NELEM(chapcodes) - 1)
100 ChapOutput(struct physical
*physical
, u_int code
, u_int id
,
101 const u_char
*ptr
, int count
, const char *text
)
107 plen
= sizeof(struct fsmheader
) + count
;
110 lh
.length
= htons(plen
);
111 bp
= m_get(plen
, MB_CHAPOUT
);
112 memcpy(MBUF_CTOP(bp
), &lh
, sizeof(struct fsmheader
));
114 memcpy(MBUF_CTOP(bp
) + sizeof(struct fsmheader
), ptr
, count
);
115 log_DumpBp(LogDEBUG
, "ChapOutput", bp
);
117 log_Printf(LogPHASE
, "Chap Output: %s\n", chapcodes
[code
]);
119 log_Printf(LogPHASE
, "Chap Output: %s (%s)\n", chapcodes
[code
], text
);
120 link_PushPacket(&physical
->link
, bp
, physical
->dl
->bundle
,
121 LINK_QUEUES(&physical
->link
) - 1, PROTO_CHAP
);
125 chap_BuildAnswer(char *name
, char *key
, u_char id
, char *challenge
127 , u_char type
, char *peerchallenge
, char *authresponse
,
132 char *result
, *digest
;
140 char expkey
[AUTHLEN
<< 2];
144 if ((result
= malloc(1 + nlen
+ MS_CHAP_RESPONSE_LEN
)) == NULL
)
147 digest
= result
; /* the response */
148 *digest
++ = MS_CHAP_RESPONSE_LEN
; /* 49 */
149 memcpy(digest
+ MS_CHAP_RESPONSE_LEN
, name
, nlen
);
151 memset(digest
+ 24, '\0', 25);
152 mschap_LANMan(digest
, challenge
+ 1, key
); /* LANMan response */
154 memset(digest
, '\0', 25);
157 for (f
= 0; f
< klen
; f
++) {
158 expkey
[2*f
] = key
[f
];
159 expkey
[2*f
+1] = '\0';
163 * expkey = | k\0e\0y\0 |
166 MD4Init(&MD4context
);
167 MD4Update(&MD4context
, expkey
, klen
<< 1);
168 MD4Final(digest
, &MD4context
);
171 * ---- -------- ---------------- ------- ------
172 * result = | 49 | LANMan | 16 byte digest | 9 * ? | name |
173 * ---- -------- ---------------- ------- ------
175 mschap_NT(digest
, challenge
+ 1);
178 * ---- -------- ------------- ----- ------
179 * | | struct MS_ChapResponse24 | |
180 * result = | 49 | LANMan | NT digest | 0/1 | name |
181 * ---- -------- ------------- ----- ------
182 * where only one of LANMan & NT digest are set.
184 } else if (type
== 0x81) {
185 char expkey
[AUTHLEN
<< 2];
186 char pwdhash
[CHAP81_HASH_LEN
];
187 char pwdhashhash
[CHAP81_HASH_LEN
];
191 if ((result
= malloc(1 + nlen
+ CHAP81_RESPONSE_LEN
)) == NULL
)
194 memset(result
, 0, 1 + nlen
+ CHAP81_RESPONSE_LEN
);
197 *digest
++ = CHAP81_RESPONSE_LEN
; /* value size */
199 /* Copy our challenge */
200 memcpy(digest
, peerchallenge
+ 1, CHAP81_CHALLENGE_LEN
);
202 /* Expand password to Unicode XXX */
203 for (f
= 0; f
< klen
; f
++) {
204 expkey
[2*f
] = key
[f
];
205 expkey
[2*f
+1] = '\0';
208 ntresponse
= digest
+ CHAP81_NTRESPONSE_OFF
;
210 /* Get some needed hashes */
211 NtPasswordHash(expkey
, klen
* 2, pwdhash
);
212 HashNtPasswordHash(pwdhash
, pwdhashhash
);
214 /* Generate NTRESPONSE to respond on challenge call */
215 GenerateNTResponse(challenge
+ 1, peerchallenge
+ 1, name
,
216 expkey
, klen
* 2, ntresponse
);
218 /* Generate MPPE MASTERKEY */
219 GetMasterKey(pwdhashhash
, ntresponse
, MPPE_MasterKey
); /* XXX Global ! */
221 /* Generate AUTHRESPONSE to verify on auth success */
222 GenerateAuthenticatorResponse(expkey
, klen
* 2, ntresponse
,
223 peerchallenge
+ 1, challenge
+ 1, name
,
226 authresponse
[CHAP81_AUTHRESPONSE_LEN
] = 0;
228 memcpy(digest
+ CHAP81_RESPONSE_LEN
, name
, nlen
);
231 if ((result
= malloc(nlen
+ 17)) != NULL
) {
232 /* Normal MD5 stuff */
236 *digest
++ = 16; /* value size */
238 MD5Init(&MD5context
);
239 MD5Update(&MD5context
, &id
, 1);
240 MD5Update(&MD5context
, key
, klen
);
241 MD5Update(&MD5context
, challenge
+ 1, *challenge
);
242 MD5Final(digest
, &MD5context
);
244 memcpy(digest
+ 16, name
, nlen
);
246 * ---- -------- ------
247 * result = | 16 | digest | name |
248 * ---- -------- ------
256 chap_StartChild(struct chap
*chap
, char *prog
, const char *name
)
258 char *argv
[MAXARGS
], *nargv
[MAXARGS
];
263 if (chap
->child
.fd
!= -1) {
264 log_Printf(LogWARN
, "Chap: %s: Program already running\n", prog
);
268 if (pipe(in
) == -1) {
269 log_Printf(LogERROR
, "Chap: pipe: %s\n", strerror(errno
));
273 if (pipe(out
) == -1) {
274 log_Printf(LogERROR
, "Chap: pipe: %s\n", strerror(errno
));
281 switch ((chap
->child
.pid
= fork())) {
283 log_Printf(LogERROR
, "Chap: fork: %s\n", strerror(errno
));
294 if ((argc
= command_Interpret(prog
, strlen(prog
), argv
)) <= 0) {
296 log_Printf(LogWARN
, "CHAP: Invalid command syntax\n");
304 if (out
[1] == STDIN_FILENO
)
305 out
[1] = dup(out
[1]);
306 dup2(in
[0], STDIN_FILENO
);
307 dup2(out
[1], STDOUT_FILENO
);
308 close(STDERR_FILENO
);
309 if (open(_PATH_DEVNULL
, O_RDWR
) != STDERR_FILENO
) {
310 log_Printf(LogALERT
, "Chap: Failed to open %s: %s\n",
311 _PATH_DEVNULL
, strerror(errno
));
314 for (fd
= getdtablesize(); fd
> STDERR_FILENO
; fd
--)
315 fcntl(fd
, F_SETFD
, 1);
317 setuid(ID0realuid());
319 command_Expand(nargv
, argc
, (char const *const *)argv
,
320 chap
->auth
.physical
->dl
->bundle
, 0, pid
);
321 execvp(nargv
[0], nargv
);
322 printf("exec() of %s failed: %s\n", nargv
[0], strerror(errno
));
328 chap
->child
.fd
= out
[0];
329 chap
->child
.buf
.len
= 0;
330 write(in
[1], chap
->auth
.in
.name
, strlen(chap
->auth
.in
.name
));
331 write(in
[1], "\n", 1);
332 write(in
[1], chap
->challenge
.peer
+ 1, *chap
->challenge
.peer
);
333 write(in
[1], "\n", 1);
334 write(in
[1], name
, strlen(name
));
335 write(in
[1], "\n", 1);
342 chap_Cleanup(struct chap
*chap
, int sig
)
344 if (chap
->child
.pid
) {
347 close(chap
->child
.fd
);
350 kill(chap
->child
.pid
, SIGTERM
);
352 chap
->child
.buf
.len
= 0;
354 if (wait(&status
) == -1)
355 log_Printf(LogERROR
, "Chap: wait: %s\n", strerror(errno
));
356 else if (WIFSIGNALED(status
))
357 log_Printf(LogWARN
, "Chap: Child received signal %d\n", WTERMSIG(status
));
358 else if (WIFEXITED(status
) && WEXITSTATUS(status
))
359 log_Printf(LogERROR
, "Chap: Child exited %d\n", WEXITSTATUS(status
));
361 *chap
->challenge
.local
= *chap
->challenge
.peer
= '\0';
368 chap_Respond(struct chap
*chap
, char *name
, char *key
370 , u_char type
, int lm
376 ans
= chap_BuildAnswer(name
, key
, chap
->auth
.id
, chap
->challenge
.peer
378 , type
, chap
->challenge
.local
, chap
->authresponse
, lm
383 ChapOutput(chap
->auth
.physical
, CHAP_RESPONSE
, chap
->auth
.id
,
384 ans
, *ans
+ 1 + strlen(name
), name
);
386 chap
->NTRespSent
= !lm
;
387 MPPE_IsServer
= 0; /* XXX Global ! */
391 ChapOutput(chap
->auth
.physical
, CHAP_FAILURE
, chap
->auth
.id
,
392 "Out of memory!", 14, NULL
);
396 chap_UpdateSet(struct fdescriptor
*d
, fd_set
*r
, fd_set
*w __unused
,
397 fd_set
*e __unused
, int *n
)
399 struct chap
*chap
= descriptor2chap(d
);
401 if (r
&& chap
&& chap
->child
.fd
!= -1) {
402 FD_SET(chap
->child
.fd
, r
);
403 if (*n
< chap
->child
.fd
+ 1)
404 *n
= chap
->child
.fd
+ 1;
405 log_Printf(LogTIMER
, "Chap: fdset(r) %d\n", chap
->child
.fd
);
413 chap_IsSet(struct fdescriptor
*d
, const fd_set
*fdset
)
415 struct chap
*chap
= descriptor2chap(d
);
417 return chap
&& chap
->child
.fd
!= -1 && FD_ISSET(chap
->child
.fd
, fdset
);
421 chap_Read(struct fdescriptor
*d
, struct bundle
*bundle __unused
,
422 const fd_set
*fdset __unused
)
424 struct chap
*chap
= descriptor2chap(d
);
427 got
= read(chap
->child
.fd
, chap
->child
.buf
.ptr
+ chap
->child
.buf
.len
,
428 sizeof chap
->child
.buf
.ptr
- chap
->child
.buf
.len
- 1);
430 log_Printf(LogERROR
, "Chap: Read: %s\n", strerror(errno
));
431 chap_Cleanup(chap
, SIGTERM
);
432 } else if (got
== 0) {
433 log_Printf(LogWARN
, "Chap: Read: Child terminated connection\n");
434 chap_Cleanup(chap
, SIGTERM
);
436 char *name
, *key
, *end
;
438 chap
->child
.buf
.len
+= got
;
439 chap
->child
.buf
.ptr
[chap
->child
.buf
.len
] = '\0';
440 name
= chap
->child
.buf
.ptr
;
441 name
+= strspn(name
, " \t");
442 if ((key
= strchr(name
, '\n')) == NULL
)
445 end
= strchr(++key
, '\n');
448 if (chap
->child
.buf
.len
== sizeof chap
->child
.buf
.ptr
- 1) {
449 log_Printf(LogWARN
, "Chap: Read: Input buffer overflow\n");
450 chap_Cleanup(chap
, SIGTERM
);
454 int lanman
= chap
->auth
.physical
->link
.lcp
.his_authtype
== 0x80 &&
455 ((chap
->NTRespSent
&&
456 IsAccepted(chap
->auth
.physical
->link
.lcp
.cfg
.chap80lm
)) ||
457 !IsAccepted(chap
->auth
.physical
->link
.lcp
.cfg
.chap80nt
));
460 while (end
>= name
&& strchr(" \t\r\n", *end
))
463 while (end
>= name
&& strchr(" \t\r\n", *end
))
465 key
+= strspn(key
, " \t");
467 chap_Respond(chap
, name
, key
469 , chap
->auth
.physical
->link
.lcp
.his_authtype
, lanman
472 chap_Cleanup(chap
, 0);
478 chap_Write(struct fdescriptor
*d __unused
, struct bundle
*bundle __unused
,
479 const fd_set
*fdset __unused
)
481 /* We never want to write here ! */
482 log_Printf(LogALERT
, "chap_Write: Internal error: Bad call !\n");
487 chap_ChallengeInit(struct authinfo
*authp
)
489 struct chap
*chap
= auth2chap(authp
);
493 len
= strlen(authp
->physical
->dl
->bundle
->cfg
.auth
.name
);
495 if (!*chap
->challenge
.local
) {
497 cp
= chap
->challenge
.local
;
500 if (*authp
->physical
->dl
->bundle
->radius
.cfg
.file
) {
501 /* For radius, our challenge is 16 readable NUL terminated bytes :*/
503 for (i
= 0; i
< 16; i
++)
504 *cp
++ = (random() % 10) + '0';
509 if (authp
->physical
->link
.lcp
.want_authtype
== 0x80)
510 *cp
++ = 8; /* MS does 8 byte callenges :-/ */
511 else if (authp
->physical
->link
.lcp
.want_authtype
== 0x81)
512 *cp
++ = 16; /* MS-CHAP-V2 does 16 bytes challenges */
515 *cp
++ = random() % (CHAPCHALLENGELEN
-16) + 16;
516 for (i
= 0; i
< *chap
->challenge
.local
; i
++)
517 *cp
++ = random() & 0xff;
519 memcpy(cp
, authp
->physical
->dl
->bundle
->cfg
.auth
.name
, len
);
524 chap_Challenge(struct authinfo
*authp
)
526 struct chap
*chap
= auth2chap(authp
);
529 log_Printf(LogDEBUG
, "CHAP%02X: Challenge\n",
530 authp
->physical
->link
.lcp
.want_authtype
);
532 len
= strlen(authp
->physical
->dl
->bundle
->cfg
.auth
.name
);
534 /* Generate new local challenge value */
535 if (!*chap
->challenge
.local
)
536 chap_ChallengeInit(authp
);
539 if (authp
->physical
->link
.lcp
.want_authtype
== 0x81)
540 ChapOutput(authp
->physical
, CHAP_CHALLENGE
, authp
->id
,
541 chap
->challenge
.local
, 1 + *chap
->challenge
.local
, NULL
);
544 ChapOutput(authp
->physical
, CHAP_CHALLENGE
, authp
->id
,
545 chap
->challenge
.local
, 1 + *chap
->challenge
.local
+ len
, NULL
);
549 chap_Success(struct authinfo
*authp
)
551 struct bundle
*bundle
= authp
->physical
->dl
->bundle
;
554 datalink_GotAuthname(authp
->physical
->dl
, authp
->in
.name
);
556 if (authp
->physical
->link
.lcp
.want_authtype
== 0x81) {
558 if (*bundle
->radius
.cfg
.file
&& bundle
->radius
.msrepstr
)
559 msg
= bundle
->radius
.msrepstr
;
562 msg
= auth2chap(authp
)->authresponse
;
563 MPPE_MasterKeyValid
= 1; /* XXX Global ! */
567 if (*bundle
->radius
.cfg
.file
&& bundle
->radius
.repstr
)
568 msg
= bundle
->radius
.repstr
;
573 ChapOutput(authp
->physical
, CHAP_SUCCESS
, authp
->id
, msg
, strlen(msg
),
576 authp
->physical
->link
.lcp
.auth_ineed
= 0;
577 if (Enabled(bundle
, OPT_UTMP
))
578 physical_Login(authp
->physical
, authp
->in
.name
);
580 if (authp
->physical
->link
.lcp
.auth_iwait
== 0)
582 * Either I didn't need to authenticate, or I've already been
583 * told that I got the answer right.
585 datalink_AuthOk(authp
->physical
->dl
);
589 chap_Failure(struct authinfo
*authp
)
592 char buf
[1024], *ptr
;
597 struct bundle
*bundle
= authp
->physical
->link
.lcp
.fsm
.bundle
;
598 if (*bundle
->radius
.cfg
.file
&& bundle
->radius
.errstr
)
599 msg
= bundle
->radius
.errstr
;
603 if (authp
->physical
->link
.lcp
.want_authtype
== 0x80) {
604 sprintf(buf
, "E=691 R=1 M=Invalid!");
606 } else if (authp
->physical
->link
.lcp
.want_authtype
== 0x81) {
610 ptr
+= sprintf(buf
, "E=691 R=0 C=");
612 ptr
+= sprintf(ptr
, "%02X", *(auth2chap(authp
)->challenge
.local
+1+i
));
614 sprintf(ptr
, " V=3 M=Invalid!");
620 ChapOutput(authp
->physical
, CHAP_FAILURE
, authp
->id
, msg
, strlen(msg
) + 1,
622 datalink_AuthNotOk(authp
->physical
->dl
);
626 chap_Cmp(char *myans
, int mylen
, char *hisans
, int hislen
628 , u_char type
, int lm
635 else if (type
== 0x80) {
636 int off
= lm
? 0 : 24;
638 if (memcmp(myans
+ off
, hisans
+ off
, 24))
642 else if (memcmp(myans
, hisans
, mylen
))
650 chap_HaveAnotherGo(struct chap
*chap
)
652 if (++chap
->peertries
< 3) {
653 /* Give the peer another shot */
654 *chap
->challenge
.local
= '\0';
655 chap_Challenge(&chap
->auth
);
664 chap_Init(struct chap
*chap
, struct physical
*p
)
666 chap
->desc
.type
= CHAP_DESCRIPTOR
;
667 chap
->desc
.UpdateSet
= chap_UpdateSet
;
668 chap
->desc
.IsSet
= chap_IsSet
;
669 chap
->desc
.Read
= chap_Read
;
670 chap
->desc
.Write
= chap_Write
;
673 auth_Init(&chap
->auth
, p
, chap_Challenge
, chap_Success
, chap_Failure
);
674 *chap
->challenge
.local
= *chap
->challenge
.peer
= '\0';
676 chap
->NTRespSent
= 0;
682 chap_ReInit(struct chap
*chap
)
684 chap_Cleanup(chap
, SIGTERM
);
688 chap_Input(struct bundle
*bundle
, struct link
*l
, struct mbuf
*bp
)
690 struct physical
*p
= link2physical(l
);
691 struct chap
*chap
= &p
->dl
->chap
;
692 char *name
, *key
, *ans
;
701 log_Printf(LogERROR
, "chap_Input: Not a physical link - dropped\n");
706 if (bundle_Phase(bundle
) != PHASE_NETWORK
&&
707 bundle_Phase(bundle
) != PHASE_AUTHENTICATE
) {
708 log_Printf(LogPHASE
, "Unexpected chap input - dropped !\n");
713 m_settype(bp
, MB_CHAPIN
);
714 if ((bp
= auth_ReadHeader(&chap
->auth
, bp
)) == NULL
&&
715 ntohs(chap
->auth
.in
.hdr
.length
) == 0)
716 log_Printf(LogWARN
, "Chap Input: Truncated header !\n");
717 else if (chap
->auth
.in
.hdr
.code
== 0 || chap
->auth
.in
.hdr
.code
> MAXCHAPCODE
)
718 log_Printf(LogPHASE
, "Chap Input: %d: Bad CHAP code !\n",
719 chap
->auth
.in
.hdr
.code
);
724 if (chap
->auth
.in
.hdr
.code
!= CHAP_CHALLENGE
&&
725 chap
->auth
.id
!= chap
->auth
.in
.hdr
.id
&&
726 Enabled(bundle
, OPT_IDCHECK
)) {
727 /* Wrong conversation dude ! */
728 log_Printf(LogPHASE
, "Chap Input: %s dropped (got id %d, not %d)\n",
729 chapcodes
[chap
->auth
.in
.hdr
.code
], chap
->auth
.in
.hdr
.id
,
734 chap
->auth
.id
= chap
->auth
.in
.hdr
.id
; /* We respond with this id */
739 switch (chap
->auth
.in
.hdr
.code
) {
741 bp
= mbuf_Read(bp
, &alen
, 1);
744 log_Printf(LogERROR
, "Chap Input: Truncated challenge !\n");
748 *chap
->challenge
.peer
= alen
;
749 bp
= mbuf_Read(bp
, chap
->challenge
.peer
+ 1, alen
);
750 bp
= auth_ReadName(&chap
->auth
, bp
, len
);
752 lanman
= p
->link
.lcp
.his_authtype
== 0x80 &&
753 ((chap
->NTRespSent
&& IsAccepted(p
->link
.lcp
.cfg
.chap80lm
)) ||
754 !IsAccepted(p
->link
.lcp
.cfg
.chap80nt
));
756 /* Generate local challenge value */
757 chap_ChallengeInit(&chap
->auth
);
762 auth_StopTimer(&chap
->auth
);
763 bp
= mbuf_Read(bp
, &alen
, 1);
766 log_Printf(LogERROR
, "Chap Input: Truncated response !\n");
770 if ((ans
= malloc(alen
+ 1)) == NULL
) {
771 log_Printf(LogERROR
, "Chap Input: Out of memory !\n");
775 *ans
= chap
->auth
.id
;
776 bp
= mbuf_Read(bp
, ans
+ 1, alen
);
777 bp
= auth_ReadName(&chap
->auth
, bp
, len
);
779 lanman
= p
->link
.lcp
.want_authtype
== 0x80 &&
780 alen
== 49 && ans
[alen
] == 0;
786 /* chap->auth.in.name is already set up at CHALLENGE time */
787 if ((ans
= malloc(len
+ 1)) == NULL
) {
788 log_Printf(LogERROR
, "Chap Input: Out of memory !\n");
792 bp
= mbuf_Read(bp
, ans
, len
);
797 switch (chap
->auth
.in
.hdr
.code
) {
800 if (*chap
->auth
.in
.name
)
801 log_Printf(LogPHASE
, "Chap Input: %s (%d bytes from %s%s)\n",
802 chapcodes
[chap
->auth
.in
.hdr
.code
], alen
,
805 lanman
&& chap
->auth
.in
.hdr
.code
== CHAP_RESPONSE
?
810 log_Printf(LogPHASE
, "Chap Input: %s (%d bytes%s)\n",
811 chapcodes
[chap
->auth
.in
.hdr
.code
], alen
,
813 lanman
&& chap
->auth
.in
.hdr
.code
== CHAP_RESPONSE
?
822 log_Printf(LogPHASE
, "Chap Input: %s (%s)\n",
823 chapcodes
[chap
->auth
.in
.hdr
.code
], ans
);
825 log_Printf(LogPHASE
, "Chap Input: %s\n",
826 chapcodes
[chap
->auth
.in
.hdr
.code
]);
830 switch (chap
->auth
.in
.hdr
.code
) {
832 if (*bundle
->cfg
.auth
.key
== '!' && bundle
->cfg
.auth
.key
[1] != '!')
833 chap_StartChild(chap
, bundle
->cfg
.auth
.key
+ 1,
834 bundle
->cfg
.auth
.name
);
836 chap_Respond(chap
, bundle
->cfg
.auth
.name
, bundle
->cfg
.auth
.key
+
837 (*bundle
->cfg
.auth
.key
== '!' ? 1 : 0)
840 , p
->link
.lcp
.his_authtype
, lanman
846 name
= chap
->auth
.in
.name
;
849 if (p
->link
.lcp
.want_authtype
== 0x81) {
850 struct MSCHAPv2_resp
*resp
= (struct MSCHAPv2_resp
*)(ans
+ 1);
852 chap
->challenge
.peer
[0] = sizeof resp
->PeerChallenge
;
853 memcpy(chap
->challenge
.peer
+ 1, resp
->PeerChallenge
,
854 sizeof resp
->PeerChallenge
);
859 if (*bundle
->radius
.cfg
.file
) {
860 if (!radius_Authenticate(&bundle
->radius
, &chap
->auth
,
861 chap
->auth
.in
.name
, ans
, alen
+ 1,
862 chap
->challenge
.local
+ 1,
863 *chap
->challenge
.local
))
864 chap_Failure(&chap
->auth
);
868 if (p
->link
.lcp
.want_authtype
== 0x81 && ans
[alen
] != '\0' &&
869 alen
== sizeof(struct MSCHAPv2_resp
)) {
870 struct MSCHAPv2_resp
*resp
= (struct MSCHAPv2_resp
*)(ans
+ 1);
872 log_Printf(LogWARN
, "%s: Compensating for corrupt (Win98/WinME?) "
873 "CHAP81 RESPONSE\n", l
->name
);
874 resp
->Flags
= '\0'; /* rfc2759 says it *MUST* be zero */
876 key
= auth_GetSecret(name
, nlen
);
879 if (p
->link
.lcp
.want_authtype
== 0x80 &&
880 lanman
&& !IsEnabled(p
->link
.lcp
.cfg
.chap80lm
)) {
881 log_Printf(LogPHASE
, "Auth failure: LANMan not enabled\n");
882 if (chap_HaveAnotherGo(chap
))
885 } else if (p
->link
.lcp
.want_authtype
== 0x80 &&
886 !lanman
&& !IsEnabled(p
->link
.lcp
.cfg
.chap80nt
)) {
887 log_Printf(LogPHASE
, "Auth failure: mschap not enabled\n");
888 if (chap_HaveAnotherGo(chap
))
891 } else if (p
->link
.lcp
.want_authtype
== 0x81 &&
892 !IsEnabled(p
->link
.lcp
.cfg
.chap81
)) {
893 log_Printf(LogPHASE
, "Auth failure: CHAP81 not enabled\n");
898 char *myans
= chap_BuildAnswer(name
, key
, chap
->auth
.id
,
899 chap
->challenge
.local
901 , p
->link
.lcp
.want_authtype
,
902 chap
->challenge
.peer
,
903 chap
->authresponse
, lanman
);
904 MPPE_IsServer
= 1; /* XXX Global ! */
911 if (!chap_Cmp(myans
+ 1, *myans
, ans
+ 1, alen
913 , p
->link
.lcp
.want_authtype
, lanman
923 chap_Success(&chap
->auth
);
925 chap_Failure(&chap
->auth
);
931 if (p
->link
.lcp
.auth_iwait
== PROTO_CHAP
) {
932 p
->link
.lcp
.auth_iwait
= 0;
933 if (p
->link
.lcp
.auth_ineed
== 0) {
935 if (p
->link
.lcp
.his_authtype
== 0x81) {
936 if (strncasecmp(ans
, chap
->authresponse
, 42)) {
937 datalink_AuthNotOk(p
->dl
);
938 log_Printf(LogWARN
, "CHAP81: AuthenticatorResponse: (%.42s)"
939 " != ans: (%.42s)\n", chap
->authresponse
, ans
);
942 /* Successful login */
943 MPPE_MasterKeyValid
= 1; /* XXX Global ! */
944 datalink_AuthOk(p
->dl
);
949 * We've succeeded in our ``login''
950 * If we're not expecting the peer to authenticate (or he already
951 * has), proceed to network phase.
953 datalink_AuthOk(p
->dl
);
959 datalink_AuthNotOk(p
->dl
);