1998-01-29 00:44:16 +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.
|
|
|
|
*
|
1998-04-28 01:25:46 +00:00
|
|
|
* $Id: chap.c,v 1.28.2.25 1998/04/24 19:15:58 brian Exp $
|
1998-01-29 00:44:16 +00:00
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
*/
|
1998-04-07 00:54:26 +00:00
|
|
|
#include <sys/types.h>
|
1998-01-29 00:44:16 +00:00
|
|
|
#include <netinet/in.h>
|
1998-03-16 22:54:35 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1998-04-28 01:25:46 +00:00
|
|
|
#include <sys/un.h>
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
#include <md5.h>
|
|
|
|
#include <stdlib.h>
|
1998-02-02 19:33:40 +00:00
|
|
|
#include <termios.h>
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "lcpproto.h"
|
|
|
|
#include "lcp.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "auth.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "chap.h"
|
1998-02-02 19:33:40 +00:00
|
|
|
#include "async.h"
|
|
|
|
#include "throughput.h"
|
1998-02-09 19:21:11 +00:00
|
|
|
#include "descriptor.h"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "iplist.h"
|
1998-03-16 22:54:35 +00:00
|
|
|
#include "slcompress.h"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "ipcp.h"
|
1998-03-16 22:52:54 +00:00
|
|
|
#include "filter.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "ccp.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "link.h"
|
|
|
|
#include "physical.h"
|
|
|
|
#include "mp.h"
|
|
|
|
#include "bundle.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "chat.h"
|
|
|
|
#include "datalink.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
static const char *chapcodes[] = {
|
|
|
|
"???", "CHALLENGE", "RESPONSE", "SUCCESS", "FAILURE"
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
1998-01-29 00:49:32 +00:00
|
|
|
ChapOutput(struct physical *physical, u_int code, u_int id,
|
|
|
|
const u_char * ptr, int count)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
int plen;
|
|
|
|
struct fsmheader lh;
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
|
|
|
plen = sizeof(struct fsmheader) + count;
|
|
|
|
lh.code = code;
|
|
|
|
lh.id = id;
|
|
|
|
lh.length = htons(plen);
|
|
|
|
bp = mballoc(plen, MB_FSM);
|
|
|
|
memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
|
|
|
|
if (count)
|
|
|
|
memcpy(MBUF_CTOP(bp) + sizeof(struct fsmheader), ptr, count);
|
|
|
|
LogDumpBp(LogDEBUG, "ChapOutput", bp);
|
|
|
|
LogPrintf(LogLCP, "ChapOutput: %s\n", chapcodes[code]);
|
1998-01-30 19:46:07 +00:00
|
|
|
HdlcOutput(physical2link(physical), PRI_LINK, PROTO_CHAP, bp);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
1998-03-01 01:07:49 +00:00
|
|
|
void
|
|
|
|
SendChapChallenge(struct authinfo *auth, int chapid, struct physical *physical)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-03-01 01:07:49 +00:00
|
|
|
struct chap *chap = auth2chap(auth);
|
1998-01-29 00:44:16 +00:00
|
|
|
int len, i;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
randinit();
|
1998-03-01 01:07:49 +00:00
|
|
|
cp = chap->challenge_data;
|
|
|
|
*cp++ = chap->challenge_len = random() % 32 + 16;
|
|
|
|
for (i = 0; i < chap->challenge_len; i++)
|
1998-01-29 00:44:16 +00:00
|
|
|
*cp++ = random() & 0xff;
|
1998-04-03 19:24:49 +00:00
|
|
|
len = strlen(physical->dl->bundle->cfg.auth.name);
|
|
|
|
memcpy(cp, physical->dl->bundle->cfg.auth.name, len);
|
1998-01-29 00:44:16 +00:00
|
|
|
cp += len;
|
1998-03-01 01:07:49 +00:00
|
|
|
ChapOutput(physical, CHAP_CHALLENGE, chapid, chap->challenge_data,
|
|
|
|
cp - chap->challenge_data);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-02 19:32:16 +00:00
|
|
|
RecvChapTalk(struct bundle *bundle, struct fsmheader *chp, struct mbuf *bp,
|
|
|
|
struct physical *physical)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
int valsize, len;
|
|
|
|
int arglen, keylen, namelen;
|
|
|
|
char *cp, *argp, *ap, *name, *digest;
|
|
|
|
char *keyp;
|
|
|
|
MD5_CTX MD5context; /* context for MD5 */
|
|
|
|
char answer[100];
|
|
|
|
char cdigest[16];
|
|
|
|
#ifdef HAVE_DES
|
|
|
|
int ix;
|
|
|
|
MD4_CTX MD4context; /* context for MD4 */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
len = ntohs(chp->length);
|
|
|
|
LogPrintf(LogDEBUG, "RecvChapTalk: length: %d\n", len);
|
|
|
|
arglen = len - sizeof(struct fsmheader);
|
|
|
|
cp = (char *) MBUF_CTOP(bp);
|
|
|
|
valsize = *cp++ & 255;
|
|
|
|
name = cp + valsize;
|
|
|
|
namelen = arglen - valsize - 1;
|
|
|
|
name[namelen] = 0;
|
|
|
|
LogPrintf(LogLCP, " Valsize = %d, Name = \"%s\"\n", valsize, name);
|
|
|
|
|
|
|
|
switch (chp->code) {
|
|
|
|
case CHAP_CHALLENGE:
|
1998-04-03 19:24:49 +00:00
|
|
|
keyp = bundle->cfg.auth.key;
|
|
|
|
keylen = strlen(bundle->cfg.auth.key);
|
|
|
|
name = bundle->cfg.auth.name;
|
|
|
|
namelen = strlen(bundle->cfg.auth.name);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DES
|
1998-04-03 19:26:29 +00:00
|
|
|
if (physical->dl->chap.using_MSChap)
|
1998-01-29 00:44:16 +00:00
|
|
|
argp = malloc(1 + namelen + MS_CHAP_RESPONSE_LEN);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
argp = malloc(1 + valsize + namelen + 16);
|
|
|
|
|
|
|
|
if (argp == NULL) {
|
1998-01-29 00:49:32 +00:00
|
|
|
ChapOutput(physical, CHAP_FAILURE, chp->id, "Out of memory!", 14);
|
1998-01-29 00:44:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef HAVE_DES
|
1998-04-03 19:26:29 +00:00
|
|
|
if (physical->dl->chap.using_MSChap) {
|
1998-01-29 00:44:16 +00:00
|
|
|
digest = argp; /* this is the response */
|
|
|
|
*digest++ = MS_CHAP_RESPONSE_LEN; /* 49 */
|
|
|
|
memset(digest, '\0', 24);
|
|
|
|
digest += 24;
|
|
|
|
|
|
|
|
ap = answer; /* this is the challenge */
|
|
|
|
memcpy(ap, keyp, keylen);
|
|
|
|
ap += 2 * keylen;
|
|
|
|
memcpy(ap, cp, valsize);
|
|
|
|
LogDumpBuff(LogDEBUG, "recv", ap, valsize);
|
|
|
|
ap += valsize;
|
|
|
|
for (ix = keylen; ix > 0 ; ix--) {
|
|
|
|
answer[2*ix-2] = answer[ix-1];
|
|
|
|
answer[2*ix-1] = 0;
|
|
|
|
}
|
|
|
|
MD4Init(&MD4context);
|
|
|
|
MD4Update(&MD4context, answer, 2 * keylen);
|
|
|
|
MD4Final(digest, &MD4context);
|
|
|
|
memcpy(digest + 25, name, namelen);
|
|
|
|
ap += 2 * keylen;
|
|
|
|
ChapMS(digest, answer + 2 * keylen, valsize);
|
|
|
|
LogDumpBuff(LogDEBUG, "answer", digest, 24);
|
1998-01-29 00:49:32 +00:00
|
|
|
ChapOutput(physical, CHAP_RESPONSE, chp->id, argp,
|
|
|
|
namelen + MS_CHAP_RESPONSE_LEN + 1);
|
1998-01-29 00:44:16 +00:00
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
digest = argp;
|
|
|
|
*digest++ = 16; /* value size */
|
|
|
|
ap = answer;
|
|
|
|
*ap++ = chp->id;
|
|
|
|
memcpy(ap, keyp, keylen);
|
|
|
|
ap += keylen;
|
|
|
|
memcpy(ap, cp, valsize);
|
|
|
|
LogDumpBuff(LogDEBUG, "recv", ap, valsize);
|
|
|
|
ap += valsize;
|
|
|
|
MD5Init(&MD5context);
|
|
|
|
MD5Update(&MD5context, answer, ap - answer);
|
|
|
|
MD5Final(digest, &MD5context);
|
|
|
|
LogDumpBuff(LogDEBUG, "answer", digest, 16);
|
|
|
|
memcpy(digest + 16, name, namelen);
|
|
|
|
ap += namelen;
|
|
|
|
/* Send answer to the peer */
|
1998-01-29 00:49:32 +00:00
|
|
|
ChapOutput(physical, CHAP_RESPONSE, chp->id, argp, namelen + 17);
|
1998-01-29 00:44:16 +00:00
|
|
|
#ifdef HAVE_DES
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
free(argp);
|
|
|
|
break;
|
|
|
|
case CHAP_RESPONSE:
|
|
|
|
/*
|
|
|
|
* Get a secret key corresponds to the peer
|
|
|
|
*/
|
1998-04-24 19:16:15 +00:00
|
|
|
keyp = AuthGetSecret(bundle, name, namelen, physical);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (keyp) {
|
|
|
|
/*
|
|
|
|
* Compute correct digest value
|
|
|
|
*/
|
|
|
|
keylen = strlen(keyp);
|
|
|
|
ap = answer;
|
|
|
|
*ap++ = chp->id;
|
|
|
|
memcpy(ap, keyp, keylen);
|
|
|
|
ap += keylen;
|
|
|
|
MD5Init(&MD5context);
|
|
|
|
MD5Update(&MD5context, answer, ap - answer);
|
1998-04-03 19:26:29 +00:00
|
|
|
MD5Update(&MD5context, physical->dl->chap.challenge_data + 1,
|
|
|
|
physical->dl->chap.challenge_len);
|
1998-01-29 00:44:16 +00:00
|
|
|
MD5Final(cdigest, &MD5context);
|
|
|
|
LogDumpBuff(LogDEBUG, "got", cp, 16);
|
|
|
|
LogDumpBuff(LogDEBUG, "expect", cdigest, 16);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare with the response
|
|
|
|
*/
|
|
|
|
if (memcmp(cp, cdigest, 16) == 0) {
|
1998-04-24 19:16:15 +00:00
|
|
|
datalink_GotAuthname(physical->dl, name, namelen);
|
1998-01-29 00:49:32 +00:00
|
|
|
ChapOutput(physical, CHAP_SUCCESS, chp->id, "Welcome!!", 10);
|
1998-04-16 00:26:21 +00:00
|
|
|
if (Enabled(bundle, OPT_UTMP))
|
|
|
|
Physical_Login(physical, name);
|
1998-02-07 20:50:08 +00:00
|
|
|
|
1998-04-03 19:26:29 +00:00
|
|
|
if (physical->link.lcp.auth_iwait == 0)
|
1998-02-07 20:50:08 +00:00
|
|
|
/*
|
|
|
|
* Either I didn't need to authenticate, or I've already been
|
|
|
|
* told that I got the answer right.
|
|
|
|
*/
|
1998-04-03 19:26:29 +00:00
|
|
|
datalink_AuthOk(physical->dl);
|
1998-02-07 20:50:08 +00:00
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Peer is not registerd, or response digest is wrong.
|
|
|
|
*/
|
1998-01-29 00:49:32 +00:00
|
|
|
ChapOutput(physical, CHAP_FAILURE, chp->id, "Invalid!!", 9);
|
1998-04-03 19:26:29 +00:00
|
|
|
datalink_AuthNotOk(physical->dl);
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-02 19:32:16 +00:00
|
|
|
RecvChapResult(struct bundle *bundle, struct fsmheader *chp, struct mbuf *bp,
|
1998-01-29 00:49:32 +00:00
|
|
|
struct physical *physical)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = ntohs(chp->length);
|
|
|
|
LogPrintf(LogDEBUG, "RecvChapResult: length: %d\n", len);
|
|
|
|
if (chp->code == CHAP_SUCCESS) {
|
1998-04-03 19:26:29 +00:00
|
|
|
if (physical->link.lcp.auth_iwait == PROTO_CHAP) {
|
|
|
|
physical->link.lcp.auth_iwait = 0;
|
|
|
|
if (physical->link.lcp.auth_ineed == 0)
|
1998-02-07 20:50:08 +00:00
|
|
|
/*
|
|
|
|
* We've succeeded in our ``login''
|
|
|
|
* If we're not expecting the peer to authenticate (or he already
|
|
|
|
* has), proceed to network phase.
|
|
|
|
*/
|
1998-04-03 19:26:29 +00:00
|
|
|
datalink_AuthOk(physical->dl);
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-03-01 01:07:49 +00:00
|
|
|
} else {
|
1998-02-07 20:50:08 +00:00
|
|
|
/* CHAP failed - it's not going to get any better */
|
1998-03-01 01:07:49 +00:00
|
|
|
LogPrintf(LogPHASE, "Received CHAP_FAILURE\n");
|
1998-04-03 19:26:29 +00:00
|
|
|
datalink_AuthNotOk(physical->dl);
|
1998-03-01 01:07:49 +00:00
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-02-02 19:32:16 +00:00
|
|
|
ChapInput(struct bundle *bundle, struct mbuf *bp, struct physical *physical)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
int len = plength(bp);
|
|
|
|
struct fsmheader *chp;
|
|
|
|
|
|
|
|
if (len >= sizeof(struct fsmheader)) {
|
|
|
|
chp = (struct fsmheader *) MBUF_CTOP(bp);
|
|
|
|
if (len >= ntohs(chp->length)) {
|
|
|
|
if (chp->code < 1 || chp->code > 4)
|
|
|
|
chp->code = 0;
|
|
|
|
LogPrintf(LogLCP, "ChapInput: %s\n", chapcodes[chp->code]);
|
|
|
|
|
|
|
|
bp->offset += sizeof(struct fsmheader);
|
|
|
|
bp->cnt -= sizeof(struct fsmheader);
|
|
|
|
|
|
|
|
switch (chp->code) {
|
|
|
|
case CHAP_RESPONSE:
|
1998-04-03 19:26:29 +00:00
|
|
|
StopAuthTimer(&physical->dl->chap.auth);
|
1998-01-29 00:44:16 +00:00
|
|
|
/* Fall into.. */
|
|
|
|
case CHAP_CHALLENGE:
|
1998-02-02 19:32:16 +00:00
|
|
|
RecvChapTalk(bundle, chp, bp, physical);
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
case CHAP_SUCCESS:
|
|
|
|
case CHAP_FAILURE:
|
1998-02-02 19:32:16 +00:00
|
|
|
RecvChapResult(bundle, chp, bp, physical);
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pfree(bp);
|
|
|
|
}
|