1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP CHAP Module
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1998-05-21 21:49:08 +00:00
|
|
|
* $Id: chap.c,v 1.28.2.27 1998/05/01 19:24:03 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
*/
|
1998-05-21 21:49:08 +00:00
|
|
|
#include <sys/types.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <netinet/in.h>
|
1998-05-21 21:49:08 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <sys/un.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
|
|
|
#include <md5.h>
|
|
|
|
#include <stdlib.h>
|
1998-05-21 21:49:08 +00:00
|
|
|
#include <termios.h>
|
1997-09-25 00:52:37 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "fsm.h"
|
|
|
|
#include "lcpproto.h"
|
|
|
|
#include "lcp.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "lqr.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "hdlc.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
#include "auth.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "chap.h"
|
|
|
|
#include "async.h"
|
|
|
|
#include "throughput.h"
|
|
|
|
#include "descriptor.h"
|
|
|
|
#include "iplist.h"
|
|
|
|
#include "slcompress.h"
|
|
|
|
#include "ipcp.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "ccp.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "physical.h"
|
|
|
|
#include "mp.h"
|
|
|
|
#include "bundle.h"
|
|
|
|
#include "chat.h"
|
|
|
|
#include "datalink.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
static const char *chapcodes[] = {
|
1996-11-19 11:08:27 +00:00
|
|
|
"???", "CHALLENGE", "RESPONSE", "SUCCESS", "FAILURE"
|
1995-01-31 06:29:58 +00:00
|
|
|
};
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1998-05-21 21:49:08 +00:00
|
|
|
ChapOutput(struct physical *physical, u_int code, u_int id,
|
|
|
|
const u_char * ptr, int count)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int plen;
|
|
|
|
struct fsmheader lh;
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
plen = sizeof(struct fsmheader) + count;
|
1995-01-31 06:29:58 +00:00
|
|
|
lh.code = code;
|
|
|
|
lh.id = id;
|
|
|
|
lh.length = htons(plen);
|
1998-05-21 21:49:08 +00:00
|
|
|
bp = mbuf_Alloc(plen, MB_FSM);
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
|
1995-01-31 06:29:58 +00:00
|
|
|
if (count)
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(MBUF_CTOP(bp) + sizeof(struct fsmheader), ptr, count);
|
1998-05-21 21:49:08 +00:00
|
|
|
log_DumpBp(LogDEBUG, "ChapOutput", bp);
|
|
|
|
log_Printf(LogLCP, "ChapOutput: %s\n", chapcodes[code]);
|
|
|
|
hdlc_Output(&physical->link, PRI_LINK, PROTO_CHAP, bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
void
|
|
|
|
chap_SendChallenge(struct authinfo *auth, int chapid, struct physical *physical)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
struct chap *chap = auth2chap(auth);
|
1996-01-10 21:28:04 +00:00
|
|
|
int len, i;
|
1995-01-31 06:29:58 +00:00
|
|
|
char *cp;
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
randinit();
|
1998-05-21 21:49:08 +00:00
|
|
|
cp = chap->challenge_data;
|
|
|
|
*cp++ = chap->challenge_len = random() % 32 + 16;
|
|
|
|
for (i = 0; i < chap->challenge_len; i++)
|
1995-01-31 06:29:58 +00:00
|
|
|
*cp++ = random() & 0xff;
|
1998-05-21 21:49:08 +00:00
|
|
|
len = strlen(physical->dl->bundle->cfg.auth.name);
|
|
|
|
memcpy(cp, physical->dl->bundle->cfg.auth.name, len);
|
1995-01-31 06:29:58 +00:00
|
|
|
cp += len;
|
1998-05-21 21:49:08 +00:00
|
|
|
ChapOutput(physical, CHAP_CHALLENGE, chapid, chap->challenge_data,
|
|
|
|
cp - chap->challenge_data);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1998-05-21 21:49:08 +00:00
|
|
|
RecvChapTalk(struct bundle *bundle, struct fsmheader *chp, struct mbuf *bp,
|
|
|
|
struct physical *physical)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int valsize, len;
|
|
|
|
int arglen, keylen, namelen;
|
|
|
|
char *cp, *argp, *ap, *name, *digest;
|
|
|
|
char *keyp;
|
1997-09-17 23:17:57 +00:00
|
|
|
MD5_CTX MD5context; /* context for MD5 */
|
1995-01-31 06:29:58 +00:00
|
|
|
char answer[100];
|
|
|
|
char cdigest[16];
|
1997-09-25 00:52:37 +00:00
|
|
|
#ifdef HAVE_DES
|
|
|
|
int ix;
|
|
|
|
MD4_CTX MD4context; /* context for MD4 */
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
len = ntohs(chp->length);
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogDEBUG, "RecvChapTalk: length: %d\n", len);
|
1995-01-31 06:29:58 +00:00
|
|
|
arglen = len - sizeof(struct fsmheader);
|
1997-08-25 00:29:32 +00:00
|
|
|
cp = (char *) MBUF_CTOP(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
valsize = *cp++ & 255;
|
|
|
|
name = cp + valsize;
|
|
|
|
namelen = arglen - valsize - 1;
|
|
|
|
name[namelen] = 0;
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogLCP, " Valsize = %d, Name = \"%s\"\n", valsize, name);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
switch (chp->code) {
|
|
|
|
case CHAP_CHALLENGE:
|
1998-05-21 21:49:08 +00:00
|
|
|
keyp = bundle->cfg.auth.key;
|
|
|
|
keylen = strlen(bundle->cfg.auth.key);
|
|
|
|
name = bundle->cfg.auth.name;
|
|
|
|
namelen = strlen(bundle->cfg.auth.name);
|
1997-09-25 00:52:37 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DES
|
1998-05-21 21:49:08 +00:00
|
|
|
if (physical->dl->chap.using_MSChap)
|
1997-09-25 00:52:37 +00:00
|
|
|
argp = malloc(1 + namelen + MS_CHAP_RESPONSE_LEN);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
argp = malloc(1 + valsize + namelen + 16);
|
|
|
|
|
1997-05-10 01:22:19 +00:00
|
|
|
if (argp == NULL) {
|
1998-05-21 21:49:08 +00:00
|
|
|
ChapOutput(physical, CHAP_FAILURE, chp->id, "Out of memory!", 14);
|
1997-05-10 01:22:19 +00:00
|
|
|
return;
|
|
|
|
}
|
1997-09-25 00:52:37 +00:00
|
|
|
#ifdef HAVE_DES
|
1998-05-21 21:49:08 +00:00
|
|
|
if (physical->dl->chap.using_MSChap) {
|
1997-09-25 00:52:37 +00:00
|
|
|
digest = argp; /* this is the response */
|
|
|
|
*digest++ = MS_CHAP_RESPONSE_LEN; /* 49 */
|
1997-10-26 01:04:02 +00:00
|
|
|
memset(digest, '\0', 24);
|
|
|
|
digest += 24;
|
1997-09-25 00:52:37 +00:00
|
|
|
|
|
|
|
ap = answer; /* this is the challenge */
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(ap, keyp, keylen);
|
1997-09-25 00:52:37 +00:00
|
|
|
ap += 2 * keylen;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(ap, cp, valsize);
|
1998-05-21 21:49:08 +00:00
|
|
|
log_DumpBuff(LogDEBUG, "recv", ap, valsize);
|
1997-09-25 00:52:37 +00:00
|
|
|
ap += valsize;
|
|
|
|
for (ix = keylen; ix > 0 ; ix--) {
|
|
|
|
answer[2*ix-2] = answer[ix-1];
|
|
|
|
answer[2*ix-1] = 0;
|
|
|
|
}
|
1997-09-17 23:17:57 +00:00
|
|
|
MD4Init(&MD4context);
|
1997-09-25 00:52:37 +00:00
|
|
|
MD4Update(&MD4context, answer, 2 * keylen);
|
1997-09-17 23:17:57 +00:00
|
|
|
MD4Final(digest, &MD4context);
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(digest + 25, name, namelen);
|
1997-09-25 00:52:37 +00:00
|
|
|
ap += 2 * keylen;
|
1998-05-21 21:49:08 +00:00
|
|
|
chap_MS(digest, answer + 2 * keylen, valsize);
|
|
|
|
log_DumpBuff(LogDEBUG, "answer", digest, 24);
|
|
|
|
ChapOutput(physical, CHAP_RESPONSE, chp->id, argp,
|
|
|
|
namelen + MS_CHAP_RESPONSE_LEN + 1);
|
1997-09-17 23:17:57 +00:00
|
|
|
} else {
|
1997-09-25 00:52:37 +00:00
|
|
|
#endif
|
|
|
|
digest = argp;
|
|
|
|
*digest++ = 16; /* value size */
|
|
|
|
ap = answer;
|
|
|
|
*ap++ = chp->id;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(ap, keyp, keylen);
|
1997-09-25 00:52:37 +00:00
|
|
|
ap += keylen;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(ap, cp, valsize);
|
1998-05-21 21:49:08 +00:00
|
|
|
log_DumpBuff(LogDEBUG, "recv", ap, valsize);
|
1997-09-25 00:52:37 +00:00
|
|
|
ap += valsize;
|
1997-09-17 23:17:57 +00:00
|
|
|
MD5Init(&MD5context);
|
|
|
|
MD5Update(&MD5context, answer, ap - answer);
|
|
|
|
MD5Final(digest, &MD5context);
|
1998-05-21 21:49:08 +00:00
|
|
|
log_DumpBuff(LogDEBUG, "answer", digest, 16);
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(digest + 16, name, namelen);
|
1997-09-25 00:52:37 +00:00
|
|
|
ap += namelen;
|
|
|
|
/* Send answer to the peer */
|
1998-05-21 21:49:08 +00:00
|
|
|
ChapOutput(physical, CHAP_RESPONSE, chp->id, argp, namelen + 17);
|
1997-09-25 00:52:37 +00:00
|
|
|
#ifdef HAVE_DES
|
1997-09-17 23:17:57 +00:00
|
|
|
}
|
1997-09-25 00:52:37 +00:00
|
|
|
#endif
|
1996-10-12 16:20:34 +00:00
|
|
|
free(argp);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case CHAP_RESPONSE:
|
1997-11-09 03:55:27 +00:00
|
|
|
/*
|
|
|
|
* Get a secret key corresponds to the peer
|
|
|
|
*/
|
1998-05-21 21:49:08 +00:00
|
|
|
keyp = auth_GetSecret(bundle, name, namelen, physical);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (keyp) {
|
|
|
|
/*
|
|
|
|
* Compute correct digest value
|
|
|
|
*/
|
|
|
|
keylen = strlen(keyp);
|
|
|
|
ap = answer;
|
|
|
|
*ap++ = chp->id;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(ap, keyp, keylen);
|
1995-01-31 06:29:58 +00:00
|
|
|
ap += keylen;
|
1997-09-25 00:52:37 +00:00
|
|
|
MD5Init(&MD5context);
|
|
|
|
MD5Update(&MD5context, answer, ap - answer);
|
1998-05-21 21:49:08 +00:00
|
|
|
MD5Update(&MD5context, physical->dl->chap.challenge_data + 1,
|
|
|
|
physical->dl->chap.challenge_len);
|
1997-09-25 00:52:37 +00:00
|
|
|
MD5Final(cdigest, &MD5context);
|
1998-05-21 21:49:08 +00:00
|
|
|
log_DumpBuff(LogDEBUG, "got", cp, 16);
|
|
|
|
log_DumpBuff(LogDEBUG, "expect", cdigest, 16);
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* Compare with the response
|
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
if (memcmp(cp, cdigest, 16) == 0) {
|
1998-05-21 21:49:08 +00:00
|
|
|
datalink_GotAuthname(physical->dl, name, namelen);
|
|
|
|
ChapOutput(physical, CHAP_SUCCESS, chp->id, "Welcome!!", 10);
|
|
|
|
if (Enabled(bundle, OPT_UTMP))
|
|
|
|
physical_Login(physical, name);
|
|
|
|
|
|
|
|
if (physical->link.lcp.auth_iwait == 0)
|
|
|
|
/*
|
|
|
|
* Either I didn't need to authenticate, or I've already been
|
|
|
|
* told that I got the answer right.
|
|
|
|
*/
|
|
|
|
datalink_AuthOk(physical->dl);
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* Peer is not registerd, or response digest is wrong.
|
|
|
|
*/
|
1998-05-21 21:49:08 +00:00
|
|
|
ChapOutput(physical, CHAP_FAILURE, chp->id, "Invalid!!", 9);
|
|
|
|
datalink_AuthNotOk(physical->dl);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1998-05-21 21:49:08 +00:00
|
|
|
RecvChapResult(struct bundle *bundle, struct fsmheader *chp, struct mbuf *bp,
|
|
|
|
struct physical *physical)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = ntohs(chp->length);
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogDEBUG, "RecvChapResult: length: %d\n", len);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (chp->code == CHAP_SUCCESS) {
|
1998-05-21 21:49:08 +00:00
|
|
|
if (physical->link.lcp.auth_iwait == PROTO_CHAP) {
|
|
|
|
physical->link.lcp.auth_iwait = 0;
|
|
|
|
if (physical->link.lcp.auth_ineed == 0)
|
|
|
|
/*
|
|
|
|
* We've succeeded in our ``login''
|
|
|
|
* If we're not expecting the peer to authenticate (or he already
|
|
|
|
* has), proceed to network phase.
|
|
|
|
*/
|
|
|
|
datalink_AuthOk(physical->dl);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
1998-05-21 21:49:08 +00:00
|
|
|
/* CHAP failed - it's not going to get any better */
|
|
|
|
log_Printf(LogPHASE, "Received CHAP_FAILURE\n");
|
|
|
|
datalink_AuthNotOk(physical->dl);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-05-21 21:49:08 +00:00
|
|
|
chap_Input(struct bundle *bundle, struct mbuf *bp, struct physical *physical)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
int len = mbuf_Length(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
struct fsmheader *chp;
|
|
|
|
|
|
|
|
if (len >= sizeof(struct fsmheader)) {
|
1997-08-25 00:29:32 +00:00
|
|
|
chp = (struct fsmheader *) MBUF_CTOP(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (len >= ntohs(chp->length)) {
|
|
|
|
if (chp->code < 1 || chp->code > 4)
|
|
|
|
chp->code = 0;
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogLCP, "chap_Input: %s\n", chapcodes[chp->code]);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
bp->offset += sizeof(struct fsmheader);
|
|
|
|
bp->cnt -= sizeof(struct fsmheader);
|
|
|
|
|
|
|
|
switch (chp->code) {
|
|
|
|
case CHAP_RESPONSE:
|
1998-05-21 21:49:08 +00:00
|
|
|
auth_StopTimer(&physical->dl->chap.auth);
|
1995-02-26 12:18:08 +00:00
|
|
|
/* Fall into.. */
|
|
|
|
case CHAP_CHALLENGE:
|
1998-05-21 21:49:08 +00:00
|
|
|
RecvChapTalk(bundle, chp, bp, physical);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case CHAP_SUCCESS:
|
|
|
|
case CHAP_FAILURE:
|
1998-05-21 21:49:08 +00:00
|
|
|
RecvChapResult(bundle, chp, bp, physical);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
mbuf_Free(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|