use bounds-checking lookup functions for string constants

use random_data() to set radius[].auth
This commit is contained in:
Brendan O'Dea 2005-01-05 13:50:30 +00:00
parent 692ad810ce
commit efd5b85a33

69
ppp.c
View file

@ -1,6 +1,6 @@
// L2TPNS PPP Stuff // L2TPNS PPP Stuff
char const *cvs_id_ppp = "$Id: ppp.c,v 1.39 2004-12-16 08:49:53 bodea Exp $"; char const *cvs_id_ppp = "$Id: ppp.c,v 1.40 2005-01-05 13:50:30 bodea Exp $";
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -32,7 +32,7 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
char pass[129]; char pass[129];
uint16_t hl; uint16_t hl;
CSTAT(call_processpap); CSTAT(processpap);
LOG_HEX(5, "PAP", p, l); LOG_HEX(5, "PAP", p, l);
if (l < 4) if (l < 4)
@ -132,7 +132,7 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
uint16_t r; uint16_t r;
uint16_t hl; uint16_t hl;
CSTAT(call_processchap); CSTAT(processchap);
LOG_HEX(5, "CHAP", p, l); LOG_HEX(5, "CHAP", p, l);
r = session[s].radius; r = session[s].radius;
@ -224,29 +224,13 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
radiussend(r, RADIUSAUTH); radiussend(r, RADIUSAUTH);
} }
static char *ppp_lcp_types[] = {
NULL,
"ConfigReq",
"ConfigAck",
"ConfigNak",
"ConfigRej",
"TerminateReq",
"TerminateAck",
"CodeRej",
"ProtocolRej",
"EchoReq",
"EchoReply",
"DiscardRequest",
"IdentRequest",
};
static void dumplcp(uint8_t *p, int l) static void dumplcp(uint8_t *p, int l)
{ {
int x = l - 4; int x = l - 4;
uint8_t *o = (p + 4); uint8_t *o = (p + 4);
LOG_HEX(5, "PPP LCP Packet", p, l); LOG_HEX(5, "PPP LCP Packet", p, l);
LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_lcp_types[(int)*p], ntohs( ((uint16_t *) p)[1]) ); LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_lcp_type((int)*p), ntohs( ((uint16_t *) p)[1]) );
LOG(4, 0, 0, "Length: %d\n", l); LOG(4, 0, 0, "Length: %d\n", l);
if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck) if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
return; return;
@ -271,48 +255,48 @@ static void dumplcp(uint8_t *p, int l)
{ {
case 1: // Maximum-Receive-Unit case 1: // Maximum-Receive-Unit
if (length == 4) if (length == 4)
LOG(4, 0, 0, " %s %d\n", lcp_types[type], ntohs(*(uint16_t *)(o + 2))); LOG(4, 0, 0, " %s %d\n", lcp_type(type), ntohs(*(uint16_t *)(o + 2)));
else else
LOG(4, 0, 0, " %s odd length %d\n", lcp_types[type], length); LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length);
break; break;
case 2: // Async-Control-Character-Map case 2: // Async-Control-Character-Map
if (length == 6) if (length == 6)
{ {
uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2)); uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2));
LOG(4, 0, 0, " %s %x\n", lcp_types[type], asyncmap); LOG(4, 0, 0, " %s %x\n", lcp_type(type), asyncmap);
} }
else else
LOG(4, 0, 0, " %s odd length %d\n", lcp_types[type], length); LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length);
break; break;
case 3: // Authentication-Protocol case 3: // Authentication-Protocol
if (length == 4) if (length == 4)
{ {
int proto = ntohs(*(uint16_t *)(o + 2)); int proto = ntohs(*(uint16_t *)(o + 2));
LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_types[type], proto, LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_type(type), proto,
proto == PPPCHAP ? "CHAP" : proto == PPPCHAP ? "CHAP" :
proto == PPPPAP ? "PAP" : "UNKNOWN"); proto == PPPPAP ? "PAP" : "UNKNOWN");
} }
else else
LOG(4, 0, 0, " %s odd length %d\n", lcp_types[type], length); LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length);
break; break;
case 4: // Quality-Protocol case 4: // Quality-Protocol
{ {
uint32_t qp = ntohl(*(uint32_t *)(o + 2)); uint32_t qp = ntohl(*(uint32_t *)(o + 2));
LOG(4, 0, 0, " %s %x\n", lcp_types[type], qp); LOG(4, 0, 0, " %s %x\n", lcp_type(type), qp);
} }
break; break;
case 5: // Magic-Number case 5: // Magic-Number
if (length == 6) if (length == 6)
{ {
uint32_t magicno = ntohl(*(uint32_t *)(o + 2)); uint32_t magicno = ntohl(*(uint32_t *)(o + 2));
LOG(4, 0, 0, " %s %x\n", lcp_types[type], magicno); LOG(4, 0, 0, " %s %x\n", lcp_type(type), magicno);
} }
else else
LOG(4, 0, 0, " %s odd length %d\n", lcp_types[type], length); LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length);
break; break;
case 7: // Protocol-Field-Compression case 7: // Protocol-Field-Compression
case 8: // Address-And-Control-Field-Compression case 8: // Address-And-Control-Field-Compression
LOG(4, 0, 0, " %s\n", lcp_types[type]); LOG(4, 0, 0, " %s\n", lcp_type(type));
break; break;
default: default:
LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type); LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type);
@ -331,7 +315,7 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
uint32_t magicno = 0; uint32_t magicno = 0;
uint16_t hl; uint16_t hl;
CSTAT(call_processlcp); CSTAT(processlcp);
LOG_HEX(5, "LCP", p, l); LOG_HEX(5, "LCP", p, l);
if (l < 4) if (l < 4)
@ -483,7 +467,7 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
*q = ConfigAck; *q = ConfigAck;
} }
LOG(3, s, t, "Sending %s\n", ppp_lcp_types[*response]); LOG(3, s, t, "Sending %s\n", ppp_lcp_type(*response));
tunnelsend(b, l + (q - b), t); tunnelsend(b, l + (q - b), t);
if (!(session[s].flags & SF_LCP_ACKED)) if (!(session[s].flags & SF_LCP_ACKED))
@ -567,7 +551,7 @@ void processipcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
{ {
uint16_t hl; uint16_t hl;
CSTAT(call_processipcp); CSTAT(processipcp);
LOG_HEX(5, "IPCP", p, l); LOG_HEX(5, "IPCP", p, l);
if (l < 5) if (l < 5)
@ -709,7 +693,7 @@ void processipin(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
{ {
in_addr_t ip; in_addr_t ip;
CSTAT(call_processipin); CSTAT(processipin);
LOG_HEX(5, "IP", p, l); LOG_HEX(5, "IP", p, l);
@ -817,7 +801,7 @@ void processccp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
uint8_t b[MAXCONTROL]; uint8_t b[MAXCONTROL];
uint8_t *q; uint8_t *q;
CSTAT(call_processccp); CSTAT(processccp);
LOG_HEX(5, "CCP", p, l); LOG_HEX(5, "CCP", p, l);
switch (l > 1 ? *p : 0) switch (l > 1 ? *p : 0)
@ -869,25 +853,24 @@ void sendchap(tunnelidt t, sessionidt s)
uint16_t r = session[s].radius; uint16_t r = session[s].radius;
uint8_t *q; uint8_t *q;
CSTAT(call_sendchap); CSTAT(sendchap);
if (!r) if (!r)
{ {
LOG(1, s, t, "No RADIUS to send challenge\n"); LOG(1, s, t, "No RADIUS to send challenge\n");
STAT(tunnel_tx_errors); STAT(tunnel_tx_errors);
return ; return;
} }
LOG(1, s, t, "Send CHAP challenge\n"); LOG(1, s, t, "Send CHAP challenge\n");
{
// new challenge // new challenge
int n; random_data(radius[r].auth, sizeof(radius[r].auth));
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++; radius[r].id++;
if (radius[r].state != RADIUSCHAP) if (radius[r].state != RADIUSCHAP)
radius[r].try = 0; radius[r].try = 0;
radius[r].state = RADIUSCHAP; radius[r].state = RADIUSCHAP;
radius[r].retry = backoff(radius[r].try++); radius[r].retry = backoff(radius[r].try++);
if (radius[r].try > 5) if (radius[r].try > 5)