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
|
|
|
*
|
1999-02-18 19:11:46 +00:00
|
|
|
* $Id: chap.c,v 1.44 1999/02/18 00:52:12 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
*/
|
1999-01-28 01:56:34 +00:00
|
|
|
#include <sys/param.h>
|
1997-10-26 01:04:02 +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>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1999-02-11 10:14:08 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1998-06-27 14:18:15 +00:00
|
|
|
#ifdef HAVE_DES
|
1998-05-21 22:55:08 +00:00
|
|
|
#include <md4.h>
|
1998-06-27 14:18:15 +00:00
|
|
|
#endif
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <md5.h>
|
1999-02-11 10:14:08 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <signal.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdlib.h>
|
1999-02-18 00:52:15 +00:00
|
|
|
#include <string.h>
|
1999-02-11 10:14:08 +00:00
|
|
|
#include <sys/wait.h>
|
1998-02-02 19:33:40 +00:00
|
|
|
#include <termios.h>
|
1999-02-11 10:14:08 +00:00
|
|
|
#include <unistd.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-03-13 00:44:26 +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-02-02 19:33:40 +00:00
|
|
|
#include "async.h"
|
|
|
|
#include "throughput.h"
|
1998-02-09 19:21:11 +00:00
|
|
|
#include "descriptor.h"
|
1999-02-11 10:14:08 +00:00
|
|
|
#include "chap.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"
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "bundle.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "chat.h"
|
1998-08-07 18:42:51 +00:00
|
|
|
#include "cbcp.h"
|
1999-02-11 10:14:08 +00:00
|
|
|
#include "command.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "datalink.h"
|
1998-06-27 14:18:15 +00:00
|
|
|
#ifdef HAVE_DES
|
1998-05-21 22:55:08 +00:00
|
|
|
#include "chap_ms.h"
|
1998-06-27 14:18:15 +00:00
|
|
|
#endif
|
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
|
|
|
};
|
1999-02-06 02:54:47 +00:00
|
|
|
#define MAXCHAPCODE (sizeof chapcodes / sizeof chapcodes[0] - 1)
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1998-01-29 00:49:32 +00:00
|
|
|
ChapOutput(struct physical *physical, u_int code, u_int id,
|
1999-02-06 02:54:47 +00:00
|
|
|
const u_char *ptr, int count, const char *text)
|
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-01 19:26:12 +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-01 19:26:12 +00:00
|
|
|
log_DumpBp(LogDEBUG, "ChapOutput", bp);
|
1998-07-28 21:54:30 +00:00
|
|
|
if (text == NULL)
|
|
|
|
log_Printf(LogPHASE, "Chap Output: %s\n", chapcodes[code]);
|
|
|
|
else
|
|
|
|
log_Printf(LogPHASE, "Chap Output: %s (%s)\n", chapcodes[code], text);
|
1998-05-01 19:26:12 +00:00
|
|
|
hdlc_Output(&physical->link, PRI_LINK, PROTO_CHAP, bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1999-02-06 02:54:47 +00:00
|
|
|
static char *
|
1999-02-18 00:52:15 +00:00
|
|
|
chap_BuildAnswer(char *name, char *key, u_char id, char *challenge,
|
|
|
|
u_char type, int lanman)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-02-06 02:54:47 +00:00
|
|
|
char *result, *digest;
|
|
|
|
size_t nlen, klen;
|
|
|
|
|
|
|
|
nlen = strlen(name);
|
|
|
|
klen = strlen(key);
|
|
|
|
|
|
|
|
#ifdef HAVE_DES
|
1999-02-18 00:52:15 +00:00
|
|
|
if (type == 0x80) {
|
1999-02-06 02:54:47 +00:00
|
|
|
char expkey[AUTHLEN << 2];
|
|
|
|
MD4_CTX MD4context;
|
|
|
|
int f;
|
|
|
|
|
|
|
|
if ((result = malloc(1 + nlen + MS_CHAP_RESPONSE_LEN)) == NULL)
|
|
|
|
return result;
|
|
|
|
|
1999-02-18 00:52:15 +00:00
|
|
|
digest = result; /* the response */
|
|
|
|
*digest++ = MS_CHAP_RESPONSE_LEN; /* 49 */
|
|
|
|
memcpy(digest + MS_CHAP_RESPONSE_LEN, name, nlen);
|
|
|
|
if (lanman) {
|
|
|
|
memset(digest + 24, '\0', 25);
|
|
|
|
mschap_LANMan(digest, challenge + 1, key); /* LANMan response */
|
|
|
|
} else {
|
|
|
|
memset(digest, '\0', 25);
|
|
|
|
digest += 24;
|
1999-02-06 02:54:47 +00:00
|
|
|
|
1999-02-18 00:52:15 +00:00
|
|
|
for (f = 0; f < klen; f++) {
|
|
|
|
expkey[2*f] = key[f];
|
|
|
|
expkey[2*f+1] = '\0';
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* -----------
|
|
|
|
* expkey = | k\0e\0y\0 |
|
|
|
|
* -----------
|
|
|
|
*/
|
|
|
|
MD4Init(&MD4context);
|
|
|
|
MD4Update(&MD4context, expkey, klen << 1);
|
|
|
|
MD4Final(digest, &MD4context);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ---- -------- ---------------- ------- ------
|
|
|
|
* result = | 49 | LANMan | 16 byte digest | 9 * ? | name |
|
|
|
|
* ---- -------- ---------------- ------- ------
|
|
|
|
*/
|
|
|
|
mschap_NT(digest, challenge + 1);
|
1999-02-06 02:54:47 +00:00
|
|
|
}
|
|
|
|
/*
|
1999-02-18 00:52:15 +00:00
|
|
|
* ---- -------- ------------- ----- ------
|
|
|
|
* | | struct MS_ChapResponse24 | |
|
|
|
|
* result = | 49 | LANMan | NT digest | 0/1 | name |
|
|
|
|
* ---- -------- ------------- ----- ------
|
|
|
|
* where only one of LANMan & NT digest are set.
|
1999-02-06 02:54:47 +00:00
|
|
|
*/
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if ((result = malloc(nlen + 17)) != NULL) {
|
|
|
|
/* Normal MD5 stuff */
|
|
|
|
MD5_CTX MD5context;
|
|
|
|
|
|
|
|
digest = result;
|
|
|
|
*digest++ = 16; /* value size */
|
|
|
|
|
|
|
|
MD5Init(&MD5context);
|
|
|
|
MD5Update(&MD5context, &id, 1);
|
|
|
|
MD5Update(&MD5context, key, klen);
|
|
|
|
MD5Update(&MD5context, challenge + 1, *challenge);
|
|
|
|
MD5Final(digest, &MD5context);
|
|
|
|
|
|
|
|
memcpy(digest + 16, name, nlen);
|
|
|
|
/*
|
|
|
|
* ---- -------- ------
|
|
|
|
* result = | 16 | digest | name |
|
|
|
|
* ---- -------- ------
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-02-11 10:14:08 +00:00
|
|
|
static void
|
|
|
|
chap_StartChild(struct chap *chap, char *prog, const char *name)
|
|
|
|
{
|
|
|
|
char *argv[MAXARGS], *nargv[MAXARGS];
|
|
|
|
int argc, fd;
|
|
|
|
int in[2], out[2];
|
|
|
|
|
|
|
|
if (chap->child.fd != -1) {
|
|
|
|
log_Printf(LogWARN, "Chap: %s: Program already running\n", prog);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipe(in) == -1) {
|
|
|
|
log_Printf(LogERROR, "Chap: pipe: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipe(out) == -1) {
|
|
|
|
log_Printf(LogERROR, "Chap: pipe: %s\n", strerror(errno));
|
|
|
|
close(in[0]);
|
|
|
|
close(in[1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ((chap->child.pid = fork())) {
|
|
|
|
case -1:
|
|
|
|
log_Printf(LogERROR, "Chap: fork: %s\n", strerror(errno));
|
|
|
|
close(in[0]);
|
|
|
|
close(in[1]);
|
|
|
|
close(out[0]);
|
|
|
|
close(out[1]);
|
|
|
|
chap->child.pid = 0;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
timer_TermService();
|
|
|
|
close(in[1]);
|
|
|
|
close(out[0]);
|
|
|
|
if (out[1] == STDIN_FILENO) {
|
|
|
|
fd = dup(out[1]);
|
|
|
|
close(out[1]);
|
|
|
|
out[1] = fd;
|
|
|
|
}
|
|
|
|
dup2(in[0], STDIN_FILENO);
|
|
|
|
dup2(out[1], STDOUT_FILENO);
|
|
|
|
if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
|
|
|
|
log_Printf(LogALERT, "Chap: Failed to open %s: %s\n",
|
|
|
|
_PATH_DEVNULL, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
fcntl(3, F_SETFD, 1); /* Set close-on-exec flag */
|
|
|
|
|
|
|
|
setuid(geteuid());
|
|
|
|
argc = command_Interpret(prog, strlen(prog), argv);
|
|
|
|
command_Expand(nargv, argc, (char const *const *)argv,
|
|
|
|
chap->auth.physical->dl->bundle, 0);
|
|
|
|
execvp(nargv[0], nargv);
|
|
|
|
|
|
|
|
log_Printf(LogWARN, "exec() of %s failed: %s\n",
|
|
|
|
nargv[0], strerror(errno));
|
|
|
|
exit(255);
|
|
|
|
|
|
|
|
default:
|
|
|
|
close(in[0]);
|
|
|
|
close(out[1]);
|
|
|
|
chap->child.fd = out[0];
|
|
|
|
chap->child.buf.len = 0;
|
|
|
|
write(in[1], chap->auth.in.name, strlen(chap->auth.in.name));
|
|
|
|
write(in[1], "\n", 1);
|
|
|
|
write(in[1], chap->challenge + 1, *chap->challenge);
|
|
|
|
write(in[1], "\n", 1);
|
|
|
|
write(in[1], name, strlen(name));
|
|
|
|
write(in[1], "\n", 1);
|
|
|
|
close(in[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chap_Cleanup(struct chap *chap, int sig)
|
|
|
|
{
|
|
|
|
if (chap->child.pid) {
|
|
|
|
int status;
|
|
|
|
|
|
|
|
close(chap->child.fd);
|
|
|
|
chap->child.fd = -1;
|
|
|
|
if (sig)
|
|
|
|
kill(chap->child.pid, SIGTERM);
|
|
|
|
chap->child.pid = 0;
|
|
|
|
chap->child.buf.len = 0;
|
|
|
|
|
|
|
|
if (wait(&status) == -1)
|
|
|
|
log_Printf(LogERROR, "Chap: wait: %s\n", strerror(errno));
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
log_Printf(LogWARN, "Chap: Child received signal %d\n", WTERMSIG(status));
|
|
|
|
else if (WIFEXITED(status) && WEXITSTATUS(status))
|
|
|
|
log_Printf(LogERROR, "Chap: Child exited %d\n", WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
*chap->challenge = 0;
|
1999-02-18 00:52:15 +00:00
|
|
|
chap->peertries = 0;
|
1999-02-11 10:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-02-18 00:52:15 +00:00
|
|
|
chap_Respond(struct chap *chap, char *name, char *key, u_char type, int lm)
|
1999-02-11 10:14:08 +00:00
|
|
|
{
|
1999-02-18 00:52:15 +00:00
|
|
|
u_char *ans;
|
1999-02-11 10:14:08 +00:00
|
|
|
|
1999-02-18 00:52:15 +00:00
|
|
|
ans = chap_BuildAnswer(name, key, chap->auth.id, chap->challenge, type, lm);
|
1999-02-11 10:14:08 +00:00
|
|
|
|
|
|
|
if (ans) {
|
|
|
|
ChapOutput(chap->auth.physical, CHAP_RESPONSE, chap->auth.id,
|
|
|
|
ans, *ans + 1 + strlen(name), name);
|
1999-02-18 00:52:15 +00:00
|
|
|
chap->NTRespSent = !lm;
|
1999-02-11 10:14:08 +00:00
|
|
|
free(ans);
|
|
|
|
} else
|
|
|
|
ChapOutput(chap->auth.physical, CHAP_FAILURE, chap->auth.id,
|
|
|
|
"Out of memory!", 14, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
chap_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
|
|
|
|
{
|
|
|
|
struct chap *chap = descriptor2chap(d);
|
|
|
|
|
|
|
|
if (r && chap && chap->child.fd != -1) {
|
|
|
|
FD_SET(chap->child.fd, r);
|
|
|
|
if (*n < chap->child.fd + 1)
|
|
|
|
*n = chap->child.fd + 1;
|
|
|
|
log_Printf(LogTIMER, "Chap: fdset(r) %d\n", chap->child.fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
chap_IsSet(struct descriptor *d, const fd_set *fdset)
|
|
|
|
{
|
|
|
|
struct chap *chap = descriptor2chap(d);
|
|
|
|
|
|
|
|
return chap && chap->child.fd != -1 && FD_ISSET(chap->child.fd, fdset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chap_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
|
|
|
{
|
|
|
|
struct chap *chap = descriptor2chap(d);
|
|
|
|
int got;
|
|
|
|
|
|
|
|
got = read(chap->child.fd, chap->child.buf.ptr + chap->child.buf.len,
|
|
|
|
sizeof chap->child.buf.ptr - chap->child.buf.len - 1);
|
|
|
|
if (got == -1) {
|
|
|
|
log_Printf(LogERROR, "Chap: Read: %s\n", strerror(errno));
|
|
|
|
chap_Cleanup(chap, SIGTERM);
|
|
|
|
} else if (got == 0) {
|
|
|
|
log_Printf(LogWARN, "Chap: Read: Child terminated connection\n");
|
|
|
|
chap_Cleanup(chap, SIGTERM);
|
|
|
|
} else {
|
|
|
|
char *name, *key, *end;
|
|
|
|
|
|
|
|
chap->child.buf.len += got;
|
|
|
|
chap->child.buf.ptr[chap->child.buf.len] = '\0';
|
|
|
|
name = chap->child.buf.ptr;
|
|
|
|
name += strspn(name, " \t");
|
|
|
|
if ((key = strchr(name, '\n')) == NULL)
|
|
|
|
end = NULL;
|
|
|
|
else
|
|
|
|
end = strchr(++key, '\n');
|
|
|
|
|
|
|
|
if (end == NULL) {
|
|
|
|
if (chap->child.buf.len == sizeof chap->child.buf.ptr - 1) {
|
|
|
|
log_Printf(LogWARN, "Chap: Read: Input buffer overflow\n");
|
|
|
|
chap_Cleanup(chap, SIGTERM);
|
|
|
|
}
|
|
|
|
} else {
|
1999-02-18 00:52:15 +00:00
|
|
|
int lanman = chap->auth.physical->link.lcp.his_authtype == 0x80 &&
|
|
|
|
((chap->NTRespSent &&
|
|
|
|
IsAccepted(chap->auth.physical->link.lcp.cfg.chap80lm)) ||
|
|
|
|
!IsAccepted(chap->auth.physical->link.lcp.cfg.chap80nt));
|
|
|
|
|
1999-02-11 10:14:08 +00:00
|
|
|
while (end >= name && strchr(" \t\r\n", *end))
|
|
|
|
*end-- = '\0';
|
|
|
|
end = key - 1;
|
|
|
|
while (end >= name && strchr(" \t\r\n", *end))
|
|
|
|
*end-- = '\0';
|
|
|
|
key += strspn(key, " \t");
|
|
|
|
|
1999-02-18 00:52:15 +00:00
|
|
|
chap_Respond(chap, name, key,
|
|
|
|
chap->auth.physical->link.lcp.his_authtype, lanman);
|
1999-02-11 10:14:08 +00:00
|
|
|
chap_Cleanup(chap, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
chap_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
|
|
|
{
|
|
|
|
/* We never want to write here ! */
|
|
|
|
log_Printf(LogALERT, "chap_Write: Internal error: Bad call !\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-02-06 02:54:47 +00:00
|
|
|
static void
|
|
|
|
chap_Challenge(struct authinfo *authp)
|
|
|
|
{
|
|
|
|
struct chap *chap = auth2chap(authp);
|
1996-01-10 21:28:04 +00:00
|
|
|
int len, i;
|
1995-01-31 06:29:58 +00:00
|
|
|
char *cp;
|
|
|
|
|
1999-02-11 10:14:08 +00:00
|
|
|
len = strlen(authp->physical->dl->bundle->cfg.auth.name);
|
|
|
|
|
|
|
|
if (!*chap->challenge) {
|
|
|
|
randinit();
|
|
|
|
cp = chap->challenge;
|
1999-01-29 22:46:31 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
1999-02-11 10:14:08 +00:00
|
|
|
if (*authp->physical->dl->bundle->radius.cfg.file) {
|
|
|
|
/* For radius, our challenge is 16 readable NUL terminated bytes :*/
|
|
|
|
*cp++ = 16;
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
*cp++ = (random() % 10) + '0';
|
|
|
|
} else
|
1999-01-28 01:56:34 +00:00
|
|
|
#endif
|
1999-02-11 10:14:08 +00:00
|
|
|
{
|
1999-02-18 00:52:15 +00:00
|
|
|
#ifdef HAVE_DES
|
|
|
|
if (authp->physical->link.lcp.want_authtype == 0x80)
|
|
|
|
*cp++ = 8; /* MS does 8 byte callenges :-/ */
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
*cp++ = random() % (CHAPCHALLENGELEN-16) + 16;
|
1999-02-11 10:14:08 +00:00
|
|
|
for (i = 0; i < *chap->challenge; i++)
|
|
|
|
*cp++ = random() & 0xff;
|
|
|
|
}
|
|
|
|
memcpy(cp, authp->physical->dl->bundle->cfg.auth.name, len);
|
1999-01-28 01:56:34 +00:00
|
|
|
}
|
1999-02-06 02:54:47 +00:00
|
|
|
ChapOutput(authp->physical, CHAP_CHALLENGE, authp->id, chap->challenge,
|
1999-02-11 10:14:08 +00:00
|
|
|
1 + *chap->challenge + len, NULL);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1999-02-06 02:54:47 +00:00
|
|
|
chap_Success(struct authinfo *authp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-02-06 02:54:47 +00:00
|
|
|
datalink_GotAuthname(authp->physical->dl, authp->in.name);
|
|
|
|
ChapOutput(authp->physical, CHAP_SUCCESS, authp->id, "Welcome!!", 10, NULL);
|
|
|
|
authp->physical->link.lcp.auth_ineed = 0;
|
|
|
|
if (Enabled(authp->physical->dl->bundle, OPT_UTMP))
|
|
|
|
physical_Login(authp->physical, authp->in.name);
|
1997-09-25 00:52:37 +00:00
|
|
|
|
1999-02-06 02:54:47 +00:00
|
|
|
if (authp->physical->link.lcp.auth_iwait == 0)
|
1997-11-09 03:55:27 +00:00
|
|
|
/*
|
1999-02-06 02:54:47 +00:00
|
|
|
* Either I didn't need to authenticate, or I've already been
|
|
|
|
* told that I got the answer right.
|
1997-11-09 03:55:27 +00:00
|
|
|
*/
|
1999-02-06 02:54:47 +00:00
|
|
|
datalink_AuthOk(authp->physical->dl);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1999-02-06 02:54:47 +00:00
|
|
|
chap_Failure(struct authinfo *authp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-02-06 02:54:47 +00:00
|
|
|
ChapOutput(authp->physical, CHAP_FAILURE, authp->id, "Invalid!!", 9, NULL);
|
|
|
|
datalink_AuthNotOk(authp->physical->dl);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1999-02-18 00:52:15 +00:00
|
|
|
static int
|
|
|
|
chap_Cmp(u_char type, int lm, char *myans, int mylen, char *hisans, int hislen)
|
|
|
|
{
|
|
|
|
if (mylen != hislen)
|
|
|
|
return 0;
|
|
|
|
else if (type == 0x80) {
|
|
|
|
int off = lm ? 0 : 24;
|
|
|
|
|
|
|
|
if (memcmp(myans + off, hisans + off, 24))
|
|
|
|
return 0;
|
|
|
|
} else if (memcmp(myans, hisans, mylen))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
chap_HaveAnotherGo(struct chap *chap)
|
|
|
|
{
|
|
|
|
if (++chap->peertries < 3) {
|
|
|
|
/* Give the peer another shot */
|
|
|
|
*chap->challenge = '\0';
|
|
|
|
chap_Challenge(&chap->auth);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1999-02-06 02:54:47 +00:00
|
|
|
chap_Init(struct chap *chap, struct physical *p)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-02-11 10:14:08 +00:00
|
|
|
chap->desc.type = CHAP_DESCRIPTOR;
|
|
|
|
chap->desc.UpdateSet = chap_UpdateSet;
|
|
|
|
chap->desc.IsSet = chap_IsSet;
|
|
|
|
chap->desc.Read = chap_Read;
|
|
|
|
chap->desc.Write = chap_Write;
|
|
|
|
chap->child.pid = 0;
|
|
|
|
chap->child.fd = -1;
|
1999-02-06 02:54:47 +00:00
|
|
|
auth_Init(&chap->auth, p, chap_Challenge, chap_Success, chap_Failure);
|
|
|
|
*chap->challenge = 0;
|
1999-02-18 00:52:15 +00:00
|
|
|
chap->NTRespSent = 0;
|
|
|
|
chap->peertries = 0;
|
1999-02-06 02:54:47 +00:00
|
|
|
}
|
|
|
|
|
1999-02-11 10:14:08 +00:00
|
|
|
void
|
|
|
|
chap_ReInit(struct chap *chap)
|
|
|
|
{
|
|
|
|
chap_Cleanup(chap, SIGTERM);
|
|
|
|
}
|
|
|
|
|
1999-02-06 02:54:47 +00:00
|
|
|
void
|
|
|
|
chap_Input(struct physical *p, struct mbuf *bp)
|
|
|
|
{
|
|
|
|
struct chap *chap = &p->dl->chap;
|
1999-02-18 00:52:15 +00:00
|
|
|
char *name, *key, *ans;
|
|
|
|
int len, nlen, lanman;
|
1999-02-06 02:54:47 +00:00
|
|
|
u_char alen;
|
|
|
|
|
|
|
|
if ((bp = auth_ReadHeader(&chap->auth, bp)) == NULL)
|
|
|
|
log_Printf(LogERROR, "Chap Input: Truncated header !\n");
|
|
|
|
else if (chap->auth.in.hdr.code == 0 || chap->auth.in.hdr.code > MAXCHAPCODE)
|
|
|
|
log_Printf(LogPHASE, "Chap Input: %d: Bad CHAP code !\n",
|
|
|
|
chap->auth.in.hdr.code);
|
|
|
|
else {
|
|
|
|
len = mbuf_Length(bp);
|
|
|
|
ans = NULL;
|
|
|
|
|
|
|
|
if (chap->auth.in.hdr.code != CHAP_CHALLENGE &&
|
|
|
|
chap->auth.id != chap->auth.in.hdr.id &&
|
|
|
|
Enabled(p->dl->bundle, OPT_IDCHECK)) {
|
|
|
|
/* Wrong conversation dude ! */
|
|
|
|
log_Printf(LogPHASE, "Chap Input: %s dropped (got id %d, not %d)\n",
|
|
|
|
chapcodes[chap->auth.in.hdr.code], chap->auth.in.hdr.id,
|
|
|
|
chap->auth.id);
|
|
|
|
mbuf_Free(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chap->auth.id = chap->auth.in.hdr.id; /* We respond with this id */
|
|
|
|
|
1999-02-18 00:52:15 +00:00
|
|
|
lanman = 0;
|
1999-02-06 02:54:47 +00:00
|
|
|
switch (chap->auth.in.hdr.code) {
|
|
|
|
case CHAP_CHALLENGE:
|
1999-02-18 00:52:15 +00:00
|
|
|
bp = mbuf_Read(bp, &alen, 1);
|
|
|
|
len -= alen + 1;
|
1999-02-06 02:54:47 +00:00
|
|
|
if (len < 0) {
|
|
|
|
log_Printf(LogERROR, "Chap Input: Truncated challenge !\n");
|
|
|
|
mbuf_Free(bp);
|
|
|
|
return;
|
|
|
|
}
|
1999-02-18 00:52:15 +00:00
|
|
|
*chap->challenge = alen;
|
|
|
|
bp = mbuf_Read(bp, chap->challenge + 1, alen);
|
1999-02-06 02:54:47 +00:00
|
|
|
bp = auth_ReadName(&chap->auth, bp, len);
|
1999-02-18 00:52:15 +00:00
|
|
|
lanman = p->link.lcp.his_authtype == 0x80 &&
|
|
|
|
((chap->NTRespSent && IsAccepted(p->link.lcp.cfg.chap80lm)) ||
|
|
|
|
!IsAccepted(p->link.lcp.cfg.chap80nt));
|
1999-02-06 02:54:47 +00:00
|
|
|
break;
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
case CHAP_RESPONSE:
|
1999-02-06 02:54:47 +00:00
|
|
|
auth_StopTimer(&chap->auth);
|
|
|
|
bp = mbuf_Read(bp, &alen, 1);
|
|
|
|
len -= alen + 1;
|
|
|
|
if (len < 0) {
|
|
|
|
log_Printf(LogERROR, "Chap Input: Truncated response !\n");
|
|
|
|
mbuf_Free(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((ans = malloc(alen + 2)) == NULL) {
|
|
|
|
log_Printf(LogERROR, "Chap Input: Out of memory !\n");
|
|
|
|
mbuf_Free(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*ans = chap->auth.id;
|
|
|
|
bp = mbuf_Read(bp, ans + 1, alen);
|
|
|
|
ans[alen+1] = '\0';
|
|
|
|
bp = auth_ReadName(&chap->auth, bp, len);
|
1999-02-18 00:52:15 +00:00
|
|
|
lanman = alen == 49 && ans[alen] == 0;
|
1999-02-06 02:54:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAP_SUCCESS:
|
|
|
|
case CHAP_FAILURE:
|
|
|
|
/* chap->auth.in.name is already set up at CHALLENGE time */
|
|
|
|
if ((ans = malloc(len + 1)) == NULL) {
|
|
|
|
log_Printf(LogERROR, "Chap Input: Out of memory !\n");
|
|
|
|
mbuf_Free(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bp = mbuf_Read(bp, ans, len);
|
|
|
|
ans[len] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (chap->auth.in.hdr.code) {
|
1995-02-26 12:18:08 +00:00
|
|
|
case CHAP_CHALLENGE:
|
1999-02-06 02:54:47 +00:00
|
|
|
case CHAP_RESPONSE:
|
|
|
|
if (*chap->auth.in.name)
|
1999-02-18 00:52:15 +00:00
|
|
|
log_Printf(LogPHASE, "Chap Input: %s (%d bytes from %s%s)\n",
|
|
|
|
chapcodes[chap->auth.in.hdr.code], alen,
|
|
|
|
chap->auth.in.name,
|
|
|
|
lanman && chap->auth.in.hdr.code == CHAP_RESPONSE ?
|
|
|
|
" - lanman" : "");
|
1999-02-06 02:54:47 +00:00
|
|
|
else
|
1999-02-18 00:52:15 +00:00
|
|
|
log_Printf(LogPHASE, "Chap Input: %s (%d bytes%s)\n",
|
|
|
|
chapcodes[chap->auth.in.hdr.code], alen,
|
|
|
|
lanman && chap->auth.in.hdr.code == CHAP_RESPONSE ?
|
|
|
|
" - lanman" : "");
|
1999-02-06 02:54:47 +00:00
|
|
|
break;
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
case CHAP_SUCCESS:
|
|
|
|
case CHAP_FAILURE:
|
1999-02-06 02:54:47 +00:00
|
|
|
if (*ans)
|
|
|
|
log_Printf(LogPHASE, "Chap Input: %s (%s)\n",
|
|
|
|
chapcodes[chap->auth.in.hdr.code], ans);
|
|
|
|
else
|
|
|
|
log_Printf(LogPHASE, "Chap Input: %s\n",
|
|
|
|
chapcodes[chap->auth.in.hdr.code]);
|
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-02-06 02:54:47 +00:00
|
|
|
|
|
|
|
switch (chap->auth.in.hdr.code) {
|
|
|
|
case CHAP_CHALLENGE:
|
1999-02-11 10:14:08 +00:00
|
|
|
if (*p->dl->bundle->cfg.auth.key == '!')
|
|
|
|
chap_StartChild(chap, p->dl->bundle->cfg.auth.key + 1,
|
|
|
|
p->dl->bundle->cfg.auth.name);
|
|
|
|
else
|
1999-02-18 00:52:15 +00:00
|
|
|
chap_Respond(chap, p->dl->bundle->cfg.auth.name,
|
|
|
|
p->dl->bundle->cfg.auth.key,
|
|
|
|
p->link.lcp.his_authtype, lanman);
|
1999-02-06 02:54:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAP_RESPONSE:
|
|
|
|
name = chap->auth.in.name;
|
|
|
|
nlen = strlen(name);
|
|
|
|
#ifndef NORADIUS
|
|
|
|
if (*p->dl->bundle->radius.cfg.file) {
|
|
|
|
chap->challenge[*chap->challenge+1] = '\0';
|
|
|
|
radius_Authenticate(&p->dl->bundle->radius, &chap->auth,
|
|
|
|
chap->auth.in.name, ans, chap->challenge + 1);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
key = auth_GetSecret(p->dl->bundle, name, nlen, p);
|
|
|
|
if (key) {
|
1999-02-18 00:52:15 +00:00
|
|
|
char *myans;
|
|
|
|
if (lanman && !IsEnabled(p->link.lcp.cfg.chap80lm)) {
|
|
|
|
log_Printf(LogPHASE, "Auth failure: LANMan not enabled\n");
|
|
|
|
if (chap_HaveAnotherGo(chap))
|
|
|
|
break;
|
|
|
|
key = NULL;
|
1999-02-18 19:11:46 +00:00
|
|
|
} else if (!lanman && !IsEnabled(p->link.lcp.cfg.chap80nt) &&
|
|
|
|
p->link.lcp.want_authtype == 0x80) {
|
1999-02-18 00:52:15 +00:00
|
|
|
log_Printf(LogPHASE, "Auth failure: mschap not enabled\n");
|
|
|
|
if (chap_HaveAnotherGo(chap))
|
|
|
|
break;
|
1999-02-06 02:54:47 +00:00
|
|
|
key = NULL;
|
1999-02-18 00:52:15 +00:00
|
|
|
} else {
|
|
|
|
myans = chap_BuildAnswer(name, key, chap->auth.id,
|
|
|
|
chap->challenge,
|
|
|
|
p->link.lcp.want_authtype, lanman);
|
|
|
|
if (myans == NULL)
|
1999-02-06 02:54:47 +00:00
|
|
|
key = NULL;
|
1999-02-18 00:52:15 +00:00
|
|
|
else {
|
|
|
|
if (!chap_Cmp(p->link.lcp.want_authtype, lanman,
|
|
|
|
myans + 1, *myans, ans + 1, alen))
|
|
|
|
key = NULL;
|
|
|
|
free(myans);
|
|
|
|
}
|
1999-02-06 02:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key)
|
|
|
|
chap_Success(&chap->auth);
|
|
|
|
else
|
|
|
|
chap_Failure(&chap->auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAP_SUCCESS:
|
|
|
|
if (p->link.lcp.auth_iwait == PROTO_CHAP) {
|
|
|
|
p->link.lcp.auth_iwait = 0;
|
|
|
|
if (p->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(p->dl);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAP_FAILURE:
|
|
|
|
datalink_AuthNotOk(p->dl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(ans);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-02-06 02:54:47 +00:00
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Free(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|