Add renegotiation patch from Yuri (ppp-aug10-patch)

Indentation and style cleanups
Make md5.c use standard memcpy and memset
This commit is contained in:
David Parrish 2004-08-13 00:02:50 +00:00
parent 5779efc7ae
commit d490579025
7 changed files with 2628 additions and 2666 deletions

View file

@ -10,7 +10,8 @@
- Add startup script and monitor script from Yuri
- Some logging correctness fixes from Iain Wade
- Add support for LCP Ident and CallBack (rejection only) from Yuri
- Initiate LCP if not attempted by the client
- Initiate LCP if not attempted by the client, or in renegotiation - Yuri
- Indentation and style cleanups
* Tue Jul 13 2004 Brendan O'Dea <bod@optusnet.com.au> 2.0.1
- Update INSTALL, Docs/manual.html documentation.

1845
bgp.c

File diff suppressed because it is too large Load diff

2480
cli.c

File diff suppressed because it is too large Load diff

399
l2tpns.c
View file

@ -4,7 +4,7 @@
// Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced
// vim: sw=8 ts=8
char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.21 2004-08-02 06:06:28 fred_nerk Exp $";
char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.22 2004-08-13 00:02:50 fred_nerk Exp $";
#include <arpa/inet.h>
#include <assert.h>
@ -538,7 +538,7 @@ int cmd_show_ipcache(struct cli_def *cli, char *command, char **argv, int argc)
if (CLI_HELP_REQUESTED)
return CLI_HELP_NO_ARGS;
cli_print(cli, "%7s %s", "Sess#", "IP Address");
cli_print(cli, "%7s %s", "Sess#", "IP Address");
for (i = 0; i < 256; ++i) {
if (!d[i]) continue;
@ -960,8 +960,8 @@ int throttle_session(sessionidt s, int throttle)
if (!session[s].tunnel)
return 0; // No-one home.
if (!*session[s].user)
return 0; // User not logged in
if (!*session[s].user)
return 0; // User not logged in
if (throttle) {
if (session[s].tbf_in || session[s].tbf_out) {
@ -1780,6 +1780,7 @@ void processudp(u8 * buf, int len, struct sockaddr_in *addr)
// TBA
break;
case 12: // ICCN
if ( amagic == 0) amagic = time_now;
session[s].magic = amagic; // set magic number
session[s].l2tp_flags = aflags; // set flags received
log(3, ntohl(addr->sin_addr.s_addr), s, t, "Magic %X Flags %X\n", amagic, aflags);
@ -2785,55 +2786,57 @@ void snoop_send_packet(char *packet, u16 size, ipt destination, u16 port)
}
void dump_acct_info()
{
char filename[1024];
char timestr[64];
time_t t = time(NULL);
int i;
FILE *f = NULL;
char filename[1024];
char timestr[64];
time_t t = time(NULL);
int i;
FILE *f = NULL;
CSTAT(call_dump_acct_info);
CSTAT(call_dump_acct_info);
strftime(timestr, 64, "%Y%m%d%H%M%S", localtime(&t));
snprintf(filename, 1024, "%s/%s", config->accounting_dir, timestr);
strftime(timestr, 64, "%Y%m%d%H%M%S", localtime(&t));
snprintf(filename, 1024, "%s/%s", config->accounting_dir, timestr);
for (i = 0; i < MAXSESSION; i++)
{
if (!session[i].opened || !session[i].ip || !session[i].cin || !session[i].cout || !*session[i].user || session[i].walled_garden)
continue;
if (!f)
for (i = 0; i < MAXSESSION; i++)
{
time_t now = time(NULL);
if (!(f = fopen(filename, "w")))
{
log(0, 0, 0, 0, "Can't write accounting info to %s: %s\n", filename, strerror(errno));
return;
}
log(3, 0, 0, 0, "Dumping accounting information to %s\n", filename);
fprintf(f, "# dslwatch.pl dump file V1.01\n"
"# host: %s\n"
"# time: %ld\n"
"# uptime: %ld\n"
"# format: username ip qos uptxoctets downrxoctets\n",
hostname,
now,
now - basetime);
if (!session[i].opened || !session[i].ip || !session[i].cin || !session[i].cout || !*session[i].user || session[i].walled_garden)
continue;
if (!f)
{
time_t now = time(NULL);
if (!(f = fopen(filename, "w")))
{
log(0, 0, 0, 0, "Can't write accounting info to %s: %s\n", filename, strerror(errno));
return ;
}
log(3, 0, 0, 0, "Dumping accounting information to %s\n", filename);
fprintf(f, "# dslwatch.pl dump file V1.01\n"
"# host: %s\n"
"# time: %ld\n"
"# uptime: %ld\n"
"# format: username ip qos uptxoctets downrxoctets\n",
hostname,
now,
now - basetime);
}
log(4, 0, 0, 0, "Dumping accounting information for %s\n", session[i].user);
fprintf(f, "%s %s %d %u %u\n",
session[i].user, // username
inet_toa(htonl(session[i].ip)), // ip
(session[i].throttle) ? 2 : 1, // qos
(u32)session[i].cin, // uptxoctets
(u32)session[i].cout); // downrxoctets
session[i].pin = session[i].cin = 0;
session[i].pout = session[i].cout = 0;
}
log(4, 0, 0, 0, "Dumping accounting information for %s\n", session[i].user);
fprintf(f, "%s %s %d %u %u\n",
session[i].user, // username
inet_toa(htonl(session[i].ip)), // ip
(session[i].throttle) ? 2 : 1, // qos
(u32)session[i].cin, // uptxoctets
(u32)session[i].cout); // downrxoctets
session[i].pin = session[i].cin = 0;
session[i].pout = session[i].cout = 0;
}
if (f) fclose(f);
if (f)
fclose(f);
}
// Main program
@ -2939,11 +2942,11 @@ int main(int argc, char *argv[])
signal(SIGPIPE, SIG_IGN);
bgp_setup(config->as_number);
bgp_add_route(config->bind_address, 0xffffffff);
if (*config->bgp_peer[0])
if (*config->bgp_peer[0])
bgp_start(&bgp_peers[0], config->bgp_peer[0],
config->bgp_peer_as[0], 0); /* 0 = routing disabled */
if (*config->bgp_peer[1])
if (*config->bgp_peer[1])
bgp_start(&bgp_peers[1], config->bgp_peer[1],
config->bgp_peer_as[1], 0);
#endif /* BGP */
@ -3084,168 +3087,178 @@ void sigchild_handler(int signal)
void read_state()
{
struct stat sb;
int i;
ippoolt itmp;
FILE *f;
char magic[sizeof(DUMP_MAGIC)-1];
u32 buf[2];
struct stat sb;
int i;
ippoolt itmp;
FILE *f;
char magic[sizeof(DUMP_MAGIC) - 1];
u32 buf[2];
if (!config->save_state)
{
unlink(STATEFILE);
return;
}
if (stat(STATEFILE, &sb) < 0)
{
unlink(STATEFILE);
return;
}
if (sb.st_mtime < (time(NULL) - 60))
{
log(0, 0, 0, 0, "State file is too old to read, ignoring\n");
unlink(STATEFILE);
return;
}
f = fopen(STATEFILE, "r");
unlink(STATEFILE);
if (!f)
{
log(0, 0, 0, 0, "Can't read state file: %s\n", strerror(errno));
exit(1);
}
if (fread(magic, sizeof(magic), 1, f) != 1 || strncmp(magic, DUMP_MAGIC, sizeof(magic)))
{
log(0, 0, 0, 0, "Bad state file magic\n");
exit(1);
}
log(1, 0, 0, 0, "Reading state information\n");
if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] > MAXIPPOOL || buf[1] != sizeof(ippoolt))
{
log(0, 0, 0, 0, "Error/mismatch reading ip pool header from state file\n");
exit(1);
}
if (buf[0] > ip_pool_size)
{
log(0, 0, 0, 0, "ip pool has shrunk! state = %d, current = %d\n", buf[0], ip_pool_size);
exit(1);
}
log(2, 0, 0, 0, "Loading %u ip addresses\n", buf[0]);
for (i = 0; i < buf[0]; i++)
{
if (fread(&itmp, sizeof(itmp), 1, f) != 1)
if (!config->save_state)
{
log(0, 0, 0, 0, "Error reading ip %d from state file: %s\n", i, strerror(errno));
exit(1);
unlink(STATEFILE);
return ;
}
if (itmp.address != ip_address_pool[i].address)
if (stat(STATEFILE, &sb) < 0)
{
log(0, 0, 0, 0, "Mismatched ip %d from state file: pool may only be extended\n", i);
exit(1);
unlink(STATEFILE);
return ;
}
memcpy(&ip_address_pool[i], &itmp, sizeof(itmp));
}
if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] != MAXTUNNEL || buf[1] != sizeof(tunnelt))
{
log(0, 0, 0, 0, "Error/mismatch reading tunnel header from state file\n");
exit(1);
}
log(2, 0, 0, 0, "Loading %u tunnels\n", MAXTUNNEL);
if (fread(tunnel, sizeof(tunnelt), MAXTUNNEL, f) != MAXTUNNEL)
{
log(0, 0, 0, 0, "Error reading tunnel data from state file\n");
exit(1);
}
for (i = 0; i < MAXTUNNEL; i++)
{
tunnel[i].controlc = 0;
tunnel[i].controls = NULL;
tunnel[i].controle = NULL;
if (*tunnel[i].hostname)
log(3, 0, 0, 0, "Created tunnel for %s\n", tunnel[i].hostname);
}
if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] != MAXSESSION || buf[1] != sizeof(sessiont))
{
log(0, 0, 0, 0, "Error/mismatch reading session header from state file\n");
exit(1);
}
log(2, 0, 0, 0, "Loading %u sessions\n", MAXSESSION);
if (fread(session, sizeof(sessiont), MAXSESSION, f) != MAXSESSION)
{
log(0, 0, 0, 0, "Error reading session data from state file\n");
exit(1);
}
for (i = 0; i < MAXSESSION; i++)
{
session[i].tbf_in = 0;
session[i].tbf_out = 0;
if (session[i].opened)
if (sb.st_mtime < (time(NULL) - 60))
{
log(2, 0, i, 0, "Loaded active session for user %s\n", session[i].user);
if (session[i].ip)
sessionsetup(session[i].tunnel, i);
log(0, 0, 0, 0, "State file is too old to read, ignoring\n");
unlink(STATEFILE);
return ;
}
}
fclose(f);
log(0, 0, 0, 0, "Loaded saved state information\n");
f = fopen(STATEFILE, "r");
unlink(STATEFILE);
if (!f)
{
log(0, 0, 0, 0, "Can't read state file: %s\n", strerror(errno));
exit(1);
}
if (fread(magic, sizeof(magic), 1, f) != 1 || strncmp(magic, DUMP_MAGIC, sizeof(magic)))
{
log(0, 0, 0, 0, "Bad state file magic\n");
exit(1);
}
log(1, 0, 0, 0, "Reading state information\n");
if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] > MAXIPPOOL || buf[1] != sizeof(ippoolt))
{
log(0, 0, 0, 0, "Error/mismatch reading ip pool header from state file\n");
exit(1);
}
if (buf[0] > ip_pool_size)
{
log(0, 0, 0, 0, "ip pool has shrunk! state = %d, current = %d\n", buf[0], ip_pool_size);
exit(1);
}
log(2, 0, 0, 0, "Loading %u ip addresses\n", buf[0]);
for (i = 0; i < buf[0]; i++)
{
if (fread(&itmp, sizeof(itmp), 1, f) != 1)
{
log(0, 0, 0, 0, "Error reading ip %d from state file: %s\n", i, strerror(errno));
exit(1);
}
if (itmp.address != ip_address_pool[i].address)
{
log(0, 0, 0, 0, "Mismatched ip %d from state file: pool may only be extended\n", i);
exit(1);
}
memcpy(&ip_address_pool[i], &itmp, sizeof(itmp));
}
if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] != MAXTUNNEL || buf[1] != sizeof(tunnelt))
{
log(0, 0, 0, 0, "Error/mismatch reading tunnel header from state file\n");
exit(1);
}
log(2, 0, 0, 0, "Loading %u tunnels\n", MAXTUNNEL);
if (fread(tunnel, sizeof(tunnelt), MAXTUNNEL, f) != MAXTUNNEL)
{
log(0, 0, 0, 0, "Error reading tunnel data from state file\n");
exit(1);
}
for (i = 0; i < MAXTUNNEL; i++)
{
tunnel[i].controlc = 0;
tunnel[i].controls = NULL;
tunnel[i].controle = NULL;
if (*tunnel[i].hostname)
log(3, 0, 0, 0, "Created tunnel for %s\n", tunnel[i].hostname);
}
if (fread(buf, sizeof(buf), 1, f) != 1 || buf[0] != MAXSESSION || buf[1] != sizeof(sessiont))
{
log(0, 0, 0, 0, "Error/mismatch reading session header from state file\n");
exit(1);
}
log(2, 0, 0, 0, "Loading %u sessions\n", MAXSESSION);
if (fread(session, sizeof(sessiont), MAXSESSION, f) != MAXSESSION)
{
log(0, 0, 0, 0, "Error reading session data from state file\n");
exit(1);
}
for (i = 0; i < MAXSESSION; i++)
{
session[i].tbf_in = 0;
session[i].tbf_out = 0;
if (session[i].opened)
{
log(2, 0, i, 0, "Loaded active session for user %s\n", session[i].user);
if (session[i].ip)
sessionsetup(session[i].tunnel, i);
}
}
fclose(f);
log(0, 0, 0, 0, "Loaded saved state information\n");
}
void dump_state()
{
FILE *f;
u32 buf[2];
FILE *f;
u32 buf[2];
if (!config->save_state)
return;
if (!config->save_state)
return;
do {
if (!(f = fopen(STATEFILE, "w")))
break;
do
{
if (!(f = fopen(STATEFILE, "w")))
break;
log(1, 0, 0, 0, "Dumping state information\n");
log(1, 0, 0, 0, "Dumping state information\n");
if (fwrite(DUMP_MAGIC, sizeof(DUMP_MAGIC)-1, 1, f) != 1) break;
if (fwrite(DUMP_MAGIC, sizeof(DUMP_MAGIC) - 1, 1, f) != 1)
break;
log(2, 0, 0, 0, "Dumping %u ip addresses\n", ip_pool_size);
buf[0] = ip_pool_size;
buf[1] = sizeof(ippoolt);
if (fwrite(buf, sizeof(buf), 1, f) != 1) break;
if (fwrite(ip_address_pool, sizeof(ippoolt), ip_pool_size, f) != ip_pool_size) break;
log(2, 0, 0, 0, "Dumping %u ip addresses\n", ip_pool_size);
buf[0] = ip_pool_size;
buf[1] = sizeof(ippoolt);
if (fwrite(buf, sizeof(buf), 1, f) != 1)
break;
if (fwrite(ip_address_pool, sizeof(ippoolt), ip_pool_size, f) != ip_pool_size)
break;
log(2, 0, 0, 0, "Dumping %u tunnels\n", MAXTUNNEL);
buf[0] = MAXTUNNEL;
buf[1] = sizeof(tunnelt);
if (fwrite(buf, sizeof(buf), 1, f) != 1) break;
if (fwrite(tunnel, sizeof(tunnelt), MAXTUNNEL, f) != MAXTUNNEL) break;
log(2, 0, 0, 0, "Dumping %u tunnels\n", MAXTUNNEL);
buf[0] = MAXTUNNEL;
buf[1] = sizeof(tunnelt);
if (fwrite(buf, sizeof(buf), 1, f) != 1)
break;
if (fwrite(tunnel, sizeof(tunnelt), MAXTUNNEL, f) != MAXTUNNEL)
break;
log(2, 0, 0, 0, "Dumping %u sessions\n", MAXSESSION);
buf[0] = MAXSESSION;
buf[1] = sizeof(sessiont);
if (fwrite(buf, sizeof(buf), 1, f) != 1) break;
if (fwrite(session, sizeof(sessiont), MAXSESSION, f) != MAXSESSION) break;
log(2, 0, 0, 0, "Dumping %u sessions\n", MAXSESSION);
buf[0] = MAXSESSION;
buf[1] = sizeof(sessiont);
if (fwrite(buf, sizeof(buf), 1, f) != 1)
break;
if (fwrite(session, sizeof(sessiont), MAXSESSION, f) != MAXSESSION)
break;
if (fclose(f) == 0) return; // OK
} while (0);
if (fclose(f) == 0)
return ; // OK
}
while (0);
log(0, 0, 0, 0, "Can't write state information: %s\n", strerror(errno));
unlink(STATEFILE);
log(0, 0, 0, 0, "Can't write state information: %s\n", strerror(errno));
unlink(STATEFILE);
}
void build_chap_response(char *challenge, u8 id, u16 challenge_length, char **challenge_response)
@ -3274,13 +3287,13 @@ void build_chap_response(char *challenge, u8 id, u16 challenge_length, char **ch
static int facility_value(char *name)
{
int i;
for (i = 0; facilitynames[i].c_name; i++)
{
if (strcmp(facilitynames[i].c_name, name) == 0)
return facilitynames[i].c_val;
}
return 0;
int i;
for (i = 0; facilitynames[i].c_name; i++)
{
if (strcmp(facilitynames[i].c_name, name) == 0)
return facilitynames[i].c_val;
}
return 0;
}
void update_config()

146
ll.c
View file

@ -1,6 +1,6 @@
// L2TPNS Linked List Stuff
char const *cvs_id_ll = "$Id: ll.c,v 1.4 2004-06-28 02:43:13 fred_nerk Exp $";
char const *cvs_id_ll = "$Id: ll.c,v 1.5 2004-08-13 00:02:50 fred_nerk Exp $";
#include <stdio.h>
#include <sys/file.h>
@ -16,127 +16,127 @@ char const *cvs_id_ll = "$Id: ll.c,v 1.4 2004-06-28 02:43:13 fred_nerk Exp $";
linked_list *ll_init()
{
return (linked_list *)calloc(sizeof(linked_list), 1);
return (linked_list *)calloc(sizeof(linked_list), 1);
}
void ll_done(linked_list *l)
{
li *i = l->head, *n;
li *i = l->head, *n;
while (i)
{
n = i->next;
free(i);
i = n;
}
while (i)
{
n = i->next;
free(i);
i = n;
}
free(l);
free(l);
}
li *ll_push(linked_list *l, void *data)
{
li *i;
li *i;
if (!l) return NULL;
if (!(i = (li *)calloc(sizeof(li), 1))) return NULL;
if (!l) return NULL;
if (!(i = (li *)calloc(sizeof(li), 1))) return NULL;
i->data = data;
i->next = NULL;
if (l->end)
l->end->next = i;
else
l->head = i;
l->end = i;
i->data = data;
i->next = NULL;
if (l->end)
l->end->next = i;
else
l->head = i;
l->end = i;
return i;
return i;
}
void *ll_pop(linked_list *l)
{
li *i;
void *data;
li *i;
void *data;
if (!l) return NULL;
if (!l->head)
return NULL;
if (!l) return NULL;
if (!l->head)
return NULL;
data = l->head->data;
i = l->head->next;
free(l->head);
l->head = i;
return data;
data = l->head->data;
i = l->head->next;
free(l->head);
l->head = i;
return data;
}
void ll_iterate(linked_list *l, int(*func)(void *))
{
li *i;
if (!l || !func) return;
li *i;
if (!l || !func) return;
for (i = l->head; i; i = i->next)
{
if (i->data)
if (!func(i))
break;
}
for (i = l->head; i; i = i->next)
{
if (i->data)
if (!func(i))
break;
}
}
void ll_reset(linked_list *l)
{
if (!l) return;
l->current = NULL;
if (!l) return;
l->current = NULL;
}
void *ll_next(linked_list *l)
{
if (!l) return NULL;
if (!l->current)
l->current = l->head;
else
l->current = l->current->next;
if (!l->current)
return NULL;
return l->current->data;
if (!l) return NULL;
if (!l->current)
l->current = l->head;
else
l->current = l->current->next;
if (!l->current)
return NULL;
return l->current->data;
}
void ll_delete(linked_list *l, void *data)
{
li *i = l->head, *p = NULL;
li *i = l->head, *p = NULL;
while (i)
{
if (i->data == data)
while (i)
{
if (l->head == i) l->head = i->next;
if (l->end == i) l->end = i->next;
if (p) p->next = i->next;
free(i);
l->current = NULL;
return;
if (i->data == data)
{
if (l->head == i) l->head = i->next;
if (l->end == i) l->end = i->next;
if (p) p->next = i->next;
free(i);
l->current = NULL;
return;
}
p = i;
i = i->next;
}
p = i;
i = i->next;
}
}
int ll_size(linked_list *l)
{
int count = 0;
li *i;
int count = 0;
li *i;
if (!l) return 0;
if (!l) return 0;
for (i = l->head; i; i = i->next)
if (i->data) count++;
for (i = l->head; i; i = i->next)
if (i->data) count++;
return count;
return count;
}
int ll_contains(linked_list *l, void *search)
{
li *i;
for (i = l->head; i; i = i->next)
if (i->data == search)
return 1;
return 0;
li *i;
for (i = l->head; i; i = i->next)
if (i->data == search)
return 1;
return 0;
}

353
md5.c
View file

@ -1,7 +1,7 @@
/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
*/
char const *cvs_id_md5 = "$Id: md5.c,v 1.2 2004-06-28 02:43:13 fred_nerk Exp $";
char const *cvs_id_md5 = "$Id: md5.c,v 1.3 2004-08-13 00:02:50 fred_nerk Exp $";
/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved.
@ -25,6 +25,7 @@ These notices must be retained in any copies of any part of this
documentation and/or software.
*/
#include <string.h>
#include "md5.h"
/* Constants for MD5Transform routine.
@ -50,13 +51,11 @@ documentation and/or software.
static void MD5Transform PROTO_LIST((UINT4[4], unsigned char[64]));
static void Encode PROTO_LIST((unsigned char *, UINT4 *, unsigned int));
static void Decode PROTO_LIST((UINT4 *, unsigned char *, unsigned int));
static void MD5_memcpy PROTO_LIST((POINTER, POINTER, unsigned int));
static void MD5_memset PROTO_LIST((POINTER, int, unsigned int));
static unsigned char PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/* F, G, H and I are basic MD5 functions.
@ -96,256 +95,202 @@ Rotation is separate from addition to prevent recomputation.
/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
void
MD5Init(context)
MD5_CTX *context; /* context */
void MD5Init(MD5_CTX *context)
{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
context->count[0] = context->count[1] = 0;
// Load magic initialization constants.
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}
/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
void
MD5Update(context, input, inputLen)
MD5_CTX *context; /* context */
unsigned char *input; /* input block */
unsigned int inputLen; /* length of input block */
void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen)
{
unsigned int i,
index,
partLen;
unsigned int i,
index,
partLen;
/* Compute number of bytes mod 64 */
index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
/* Compute number of bytes mod 64 */
index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((context->count[0] += ((UINT4) inputLen << 3)) < ((UINT4) inputLen << 3))
context->count[1]++;
context->count[1] += ((UINT4) inputLen >> 29);
/* Update number of bits */
if ((context->count[0] += ((UINT4) inputLen << 3)) < ((UINT4) inputLen << 3))
context->count[1]++;
context->count[1] += ((UINT4) inputLen >> 29);
partLen = 64 - index;
partLen = 64 - index;
/* Transform as many times as possible.
*/
if (inputLen >= partLen)
{
MD5_memcpy((POINTER) & context->buffer[index], (POINTER) input, partLen);
MD5Transform(context->state, context->buffer);
/* Transform as many times as possible.
*/
if (inputLen >= partLen)
{
memcpy(&context->buffer[index], input, partLen);
MD5Transform(context->state, context->buffer);
for (i = partLen; i + 63 < inputLen; i += 64)
MD5Transform(context->state, &input[i]);
for (i = partLen; i + 63 < inputLen; i += 64)
MD5Transform(context->state, &input[i]);
index = 0;
}
else
i = 0;
index = 0;
}
else
i = 0;
/* Buffer remaining input */
MD5_memcpy((POINTER) & context->buffer[index], (POINTER) & input[i], inputLen - i);
/* Buffer remaining input */
memcpy(&context->buffer[index], &input[i], inputLen - i);
}
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context.
*/
void
MD5Final(digest, context)
unsigned char digest[16]; /* message digest */
MD5_CTX *context; /* context */
void MD5Final(unsigned char digest[16], MD5_CTX *context)
{
unsigned char bits[8];
unsigned int index,
padLen;
unsigned char bits[8];
unsigned int index, padLen;
/* Save number of bits */
Encode(bits, context->count, 8);
/* Save number of bits */
Encode(bits, context->count, 8);
/* Pad out to 56 mod 64.
*/
index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update(context, PADDING, padLen);
/* Pad out to 56 mod 64.
*/
index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update(context, PADDING, padLen);
/* Append length (before padding) */
MD5Update(context, bits, 8);
/* Append length (before padding) */
MD5Update(context, bits, 8);
/* Store state in digest */
Encode(digest, context->state, 16);
/* Store state in digest */
Encode(digest, context->state, 16);
/* Zeroize sensitive information.
*/
MD5_memset((POINTER) context, 0, sizeof(*context));
/* Zeroize sensitive information.
*/
memset(context, 0, sizeof(*context));
}
/* MD5 basic transformation. Transforms state based on block.
*/
static void
MD5Transform(state, block)
UINT4 state[4];
unsigned char block[64];
static void MD5Transform(UINT4 state[4], unsigned char block[64])
{
UINT4 a = state[0],
b = state[1],
c = state[2],
d = state[3],
x[16];
UINT4 a = state[0],
b = state[1],
c = state[2],
d = state[3],
x[16];
Decode(x, block, 64);
Decode(x, block, 64);
/* Round 1 */
FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 1 */
FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
/* Round 2 */
GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
/* Round 3 */
HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
/* Round 4 */
II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information.
*/
MD5_memset((POINTER) x, 0, sizeof(x));
// Zeroize sensitive information.
memset(x, 0, sizeof(x));
}
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4.
*/
static void
Encode(output, input, len)
unsigned char *output;
UINT4 *input;
unsigned int len;
static void Encode(unsigned char *output, UINT4 *input, unsigned int len)
{
unsigned int i,
j;
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
{
output[j] = (unsigned char) (input[i] & 0xff);
output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
}
for (i = 0, j = 0; j < len; i++, j += 4)
{
output[j] = (unsigned char) (input[i] & 0xff);
output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
}
}
/* Decodes input (unsigned char) into output (UINT4). Assumes len is
a multiple of 4.
*/
static void
Decode(output, input, len)
UINT4 *output;
unsigned char *input;
unsigned int len;
static void Decode(UINT4 *output, unsigned char *input, unsigned int len)
{
unsigned int i,
j;
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT4) input[j]) | (((UINT4) input[j + 1]) << 8) | (((UINT4) input[j + 2]) << 16) | (((UINT4) input[j + 3]) << 24);
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT4) input[j]) | (((UINT4) input[j + 1]) << 8) | (((UINT4) input[j + 2]) << 16) | (((UINT4) input[j + 3]) << 24);
}
/* Note: Replace "for loop" with standard memcpy if possible.
*/
static void
MD5_memcpy(output, input, len)
POINTER output;
POINTER input;
unsigned int len;
{
unsigned int i;
for (i = 0; i < len; i++)
output[i] = input[i];
}
/* Note: Replace "for loop" with standard memset if possible.
*/
static void
MD5_memset(output, value, len)
POINTER output;
int value;
unsigned int len;
{
unsigned int i;
for (i = 0; i < len; i++)
((char *) output)[i] = (char) value;
}

66
ppp.c
View file

@ -1,6 +1,6 @@
// L2TPNS PPP Stuff
char const *cvs_id_ppp = "$Id: ppp.c,v 1.11 2004-08-02 06:06:28 fred_nerk Exp $";
char const *cvs_id_ppp = "$Id: ppp.c,v 1.12 2004-08-13 00:02:50 fred_nerk Exp $";
#include <stdio.h>
#include <string.h>
@ -76,26 +76,27 @@ void processpap(tunnelidt t, sessionidt s, u8 * p, u16 l)
return;
}
if (session[s].ip)
*p = 2; // ACK
*p = 2; // ACK
else
*p = 3; // cant authorise
*p = 3; // cant authorise
p[1] = id;
*(u16 *) (p + 2) = htons(5); // length
p[4] = 0; // no message
*(u16 *) (p + 2) = htons(5); // length
p[4] = 0; // no message
if (session[s].ip)
{
log(3, session[s].ip, s, t, "%d Already an IP allocated: %s (%d)\n", getpid(), inet_toa(htonl(session[s].ip)), session[s].ip_pool_index);
log(3, session[s].ip, s, t, "%d Already an IP allocated: %s (%d)\n", getpid(), inet_toa(htonl(session[s].ip)), session[s].ip_pool_index);
session[s].flags &= ~SF_IPCP_ACKED;
}
else
{
log(1, 0, s, t, "No radius session available to authenticate session...\n");
log(1, 0, s, t, "No radius session available to authenticate session...\n");
}
log(3, 0, s, t, "Fallback response to PAP (%s)\n", (session[s].ip) ? "ACK" : "NAK");
tunnelsend(b, 5 + (p - b), t); // send it
}
else
{ // set up RADIUS request
{
// set up RADIUS request
u16 r = session[s].radius;
// Run PRE_AUTH plugins
@ -410,15 +411,16 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
{
// Send back a ConfigAck
log(3, session[s].ip, s, t, "ConfigReq accepted, sending as Ack\n");
// for win2k L2TP clientis and LCP renegotiation of alive session
if (magicno || l == 4) initlcp(t, s);
q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
if (!q) {
if (!q)
{
log(3, session[s].ip, s, t, " failed to create packet.\n");
return;
}
*q = ConfigAck;
tunnelsend(b, l + (q - b), t);
// For win2k L2TP clients, LCP should be initiated by the LNS
if (magicno) initlcp(t, s);
}
else
{
@ -438,7 +440,7 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
}
else if (*p == TerminateReq)
{
*p = TerminateAck; // close
*p = TerminateAck; // close
q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
if (!q) {
log(3, session[s].ip, s, t, "Failed to create PPP packet in processlcp.\n");
@ -454,7 +456,7 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
}
else if (*p == EchoReq)
{
*p = EchoReply; // reply
*p = EchoReply; // reply
*(u32 *) (p + 4) = htonl(session[s].magic); // our magic number
q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP);
if (!q) {
@ -537,7 +539,7 @@ void processipcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
if (!session[s].ip)
{
log(3, 0, s, t, "Waiting on radius reply\n");
return ; // have to wait on RADIUS reply
return; // have to wait on RADIUS reply
}
// form a config reply quoting the IP in the session
{
@ -602,7 +604,7 @@ void processipcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
log(5, session[s].ip, s, t, " DNS2 = %s\n", inet_toa(session[s].dns1));
}
}
i = findppp(p, 3); // IP address
i = findppp(p, 3); // IP address
if (!i || i[1] != 6)
{
log(1, 0, s, t, "No IP in IPCP request\n");
@ -744,12 +746,12 @@ void processccp(tunnelidt t, sessionidt s, u8 * p, u16 l)
}
else
{
*p = ConfigRej; // reject
*p = ConfigRej; // reject
sendccp(t, s);
}
}
else
*p = TerminateAck; // close
*p = TerminateAck; // close
if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPCCP)))
{
log(1,0,0,0, "Failed to send CCP packet.\n");
@ -775,12 +777,13 @@ void sendchap(tunnelidt t, sessionidt s)
return ;
}
log(1, 0, s, t, "Send CHAP challenge\n");
{ // new challenge
{
// new challenge
int n;
for (n = 0; n < 15; n++)
radius[r].auth[n] = rand();
}
radius[r].chap = 1; // CHAP not PAP
radius[r].chap = 1; // CHAP not PAP
radius[r].id++;
if (radius[r].state != RADIUSCHAP)
radius[r].try = 0;
@ -797,11 +800,11 @@ void sendchap(tunnelidt t, sessionidt s)
log(1, 0, s, t, "failed to send CHAP challenge.\n");
return;
}
*q = 1; // challenhe
q[1] = radius[r].id; // ID
q[4] = 16; // length
memcpy(q + 5, radius[r].auth, 16); // challenge
strcpy(q + 21, hostname); // our name
*q = 1; // challenge
q[1] = radius[r].id; // ID
q[4] = 16; // length
memcpy(q + 5, radius[r].auth, 16); // challenge
strcpy(q + 21, hostname); // our name
*(u16 *) (q + 2) = htons(strlen(hostname) + 21); // length
tunnelsend(b, strlen(hostname) + 21 + (q - b), t); // send it
}
@ -852,7 +855,7 @@ u8 *findppp(u8 * b, u8 mtype)
while (l)
{
if (l < b[1] || !b[1])
return 0; // faulty
return 0; // faulty
if (*b == mtype)
return b;
l -= b[1];
@ -874,11 +877,14 @@ void initlcp(tunnelidt t, sessionidt s)
log(4, 0, s, t, "Sending LCP ConfigReq for PAP\n");
*q = ConfigReq;
*(u8 *)(q + 1) = (time_now % 255) + 1; // ID
*(u16 *)(q + 2) = htons(8); // Length
*(u8 *)(q + 4) = 3;
*(u8 *)(q + 5) = 4;
*(u16 *)(q + 6) = htons(0xC023); // PAP
tunnelsend(b, 12 + 8, t);
*(u16 *)(q + 2) = htons(14); // Length
*(u8 *)(q + 4) = 5;
*(u8 *)(q + 5) = 6;
*(u32 *)(q + 6) = htonl(session[s].magic);
*(u8 *)(q + 10) = 3;
*(u8 *)(q + 11) = 4;
*(u16 *)(q + 12) = htons(0xC023); // PAP
tunnelsend(b, 12 + 14, t);
}
// Send CCP reply