X-Git-Url: http://git.sameswireless.fr/l2tpns.git/blobdiff_plain/b5d3f182cd565ae0a4e523957664a11055cad3d4..c07a2e8553cada19c523ef9aa07581ae8dd6a71f:/ppp.c diff --git a/ppp.c b/ppp.c index 0ad3044..fb63d88 100644 --- a/ppp.c +++ b/ppp.c @@ -1,6 +1,6 @@ // L2TPNS PPP Stuff -char const *cvs_id_ppp = "$Id: ppp.c,v 1.43 2005-01-25 04:38:49 bodea Exp $"; +char const *cvs_id_ppp = "$Id: ppp.c,v 1.50 2005-05-07 08:53:23 bodea Exp $"; #include #include @@ -28,8 +28,8 @@ static void initccp(tunnelidt t, sessionidt s); // Process PAP messages void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) { - char user[129]; - char pass[129]; + char user[MAXUSER]; + char pass[MAXPASS]; uint16_t hl; CSTAT(processpap); @@ -39,6 +39,7 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) { LOG(1, s, t, "Short PAP %u bytes\n", l); STAT(tunnel_rx_errors); + sessionshutdown(s, "Short PAP packet.", 3, 0); return ; } @@ -46,6 +47,7 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) { LOG(1, s, t, "Length mismatch PAP %u/%u\n", hl, l); STAT(tunnel_rx_errors); + sessionshutdown(s, "PAP length mismatch.", 3, 0); return ; } l = hl; @@ -54,22 +56,28 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) { LOG(1, s, t, "Unexpected PAP code %d\n", *p); STAT(tunnel_rx_errors); + sessionshutdown(s, "Unexpected PAP code.", 3, 0); return ; } { uint8_t *b = p; b += 4; + user[0] = pass[0] = 0; if (*b && *b < sizeof(user)) + { memcpy(user, b + 1, *b); - user[*b] = 0; - b += 1 + *b; - if (*b && *b < sizeof(pass)) - memcpy(pass, b + 1, *b); - pass[*b] = 0; + user[*b] = 0; + b += 1 + *b; + if (*b && *b < sizeof(pass)) + { + memcpy(pass, b + 1, *b); + pass[*b] = 0; + } + } LOG(3, s, t, "PAP login %s/%s\n", user, pass); } - if (session[s].ip || !session[s].radius) + if (session[s].ip || !sess_local[s].radius) { // respond now, either no RADIUS available or already authenticated uint8_t b[MAXCONTROL]; @@ -97,11 +105,12 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) } LOG(3, s, t, "Fallback response to PAP (%s)\n", (session[s].ip) ? "ACK" : "NAK"); tunnelsend(b, 5 + (p - b), t); // send it + sessionshutdown(s, "PAP authentication failed.", 3, 0); } else { // set up RADIUS request - uint16_t r = session[s].radius; + uint16_t r = sess_local[s].radius; // Run PRE_AUTH plugins struct param_pre_auth packet = { &tunnel[t], &session[s], strdup(user), strdup(pass), PPPPAP, 1 }; @@ -135,7 +144,7 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) CSTAT(processchap); LOG_HEX(5, "CHAP", p, l); - r = session[s].radius; + r = sess_local[s].radius; if (!r) { LOG(1, s, t, "Unexpected CHAP message\n"); @@ -270,8 +279,14 @@ static void dumplcp(uint8_t *p, int l) { int proto = ntohs(*(uint16_t *)(o + 2)); LOG(4, 0, 0, " %s 0x%x (%s)\n", lcp_type(type), proto, - proto == PPPCHAP ? "CHAP" : - proto == PPPPAP ? "PAP" : "UNKNOWN"); + proto == PPPPAP ? "PAP" : "UNSUPPORTED"); + } + else if (length == 5) + { + int proto = ntohs(*(uint16_t *)(o + 2)); + int algo = *(uint8_t *)(o + 4); + LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", lcp_type(type), proto, algo, + (proto == PPPCHAP && algo == 5) ? "CHAP MD5" : "UNSUPPORTED"); } else LOG(4, 0, 0, " %s odd length %d\n", lcp_type(type), length); @@ -389,17 +404,28 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) { int proto = ntohs(*(uint16_t *)(o + 2)); char proto_name[] = "0x0000"; + if (proto == PPPPAP) - break; + { + if (config->radius_authtypes & AUTHPAP) + break; - if (response && *response != ConfigNak) // rej already queued - break; + strcpy(proto_name, "PAP"); + } + else if (proto == PPPCHAP) + { + if (config->radius_authtypes & AUTHCHAP + && *(o + 4) == 5) // MD5 + break; - if (proto == PPPCHAP) strcpy(proto_name, "CHAP"); + } else sprintf(proto_name, "%#4.4x", proto); + if (response && *response != ConfigNak) // rej already queued + break; + LOG(2, s, t, " Remote requesting %s authentication. Rejecting.\n", proto_name); if (!response) @@ -410,16 +436,27 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) q += 4; } - if ((q - b + length) > sizeof(b)) + if ((q - b + 5) > sizeof(b)) // 5 is the larger (CHAP+MD5) of the two NAKs { LOG(2, s, t, "LCP overflow for %s ConfigNak.\n", proto_name); break; } - memcpy(q, o, length); - *(uint16_t *)(q += 2) = htons(PPPPAP); // NAK -> Use PAP instead - q += length; - *((uint16_t *) (response + 2)) = htons(q - response); + *q++ = type; + if (config->radius_authprefer == AUTHCHAP) + { + *q++ = 5; + *(uint16_t *) q = htons(PPPCHAP); q += 2; + *q++ = 5; // MD5 + } + else + { + *q++ = 4; + *(uint16_t *) q = htons(PPPPAP); q += 2; + } + + *((uint16_t *) (response + 2)) = htons(q - response); // LCP header length + break; } break; @@ -474,7 +511,7 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) { LOG(1, s, t, "Remote end sent a ConfigNak. Ignoring\n"); if (config->debug > 3) dumplcp(p, l); - return ; + return; } else if (*p == TerminateReq) { @@ -483,11 +520,11 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) q = makeppp(b, sizeof(b), p, l, t, s, PPPLCP); if (!q) return; tunnelsend(b, l + (q - b), t); // send it - sessionshutdown(s, "Remote end closed connection."); + sessionshutdown(s, "Remote end closed connection.", 3, 0); } else if (*p == TerminateAck) { - sessionshutdown(s, "Connection closed."); + sessionshutdown(s, "Connection closed.", 3, 0); } else if (*p == ProtocolRej) { @@ -583,7 +620,7 @@ void processipcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) if (*p == ConfigAck) { // happy with our IPCP - uint16_t r = session[s].radius; + uint16_t r = sess_local[s].radius; if ((!r || radius[r].state == RADIUSIPCP) && !session[s].walled_garden) { if (!r) @@ -1071,11 +1108,11 @@ void processccp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l) tunnelsend(b, l + (q - b), t); // send it } -// send a CHAP PP packet +// send a CHAP challenge void sendchap(tunnelidt t, sessionidt s) { uint8_t b[MAXCONTROL]; - uint16_t r = session[s].radius; + uint16_t r = sess_local[s].radius; uint8_t *q; CSTAT(sendchap); @@ -1089,8 +1126,6 @@ void sendchap(tunnelidt t, sessionidt s) LOG(1, s, t, "Send CHAP challenge\n"); - // new challenge - random_data(radius[r].auth, sizeof(radius[r].auth)); radius[r].chap = 1; // CHAP not PAP radius[r].id++; if (radius[r].state != RADIUSCHAP) @@ -1100,7 +1135,7 @@ void sendchap(tunnelidt t, sessionidt s) radius[r].retry = backoff(radius[r].try++); if (radius[r].try > 5) { - sessionshutdown(s, "Timeout CHAP"); + sessionshutdown(s, "CHAP timeout.", 3, 0); STAT(tunnel_tx_errors); return ; } @@ -1109,7 +1144,7 @@ void sendchap(tunnelidt t, sessionidt s) *q = 1; // challenge q[1] = radius[r].id; // ID - q[4] = 16; // length + q[4] = 16; // value size (size of challenge) memcpy(q + 5, radius[r].auth, 16); // challenge strcpy(q + 21, hostname); // our name *(uint16_t *) (q + 2) = htons(strlen(hostname) + 21); // length @@ -1164,6 +1199,7 @@ uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, tunnelidt t, sessionid void initlcp(tunnelidt t, sessionidt s) { char b[500], *q; + int size; if (!(q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPLCP))) return; @@ -1176,11 +1212,22 @@ void initlcp(tunnelidt t, sessionidt s) *(uint8_t *)(q + 5) = 6; *(uint32_t *)(q + 6) = htonl(session[s].magic); *(uint8_t *)(q + 10) = 3; - *(uint8_t *)(q + 11) = 4; - *(uint16_t *)(q + 12) = htons(PPPPAP); // PAP + if (config->radius_authprefer == AUTHCHAP) + { + *(uint8_t *)(q + 11) = 5; + *(uint16_t *)(q + 12) = htons(PPPCHAP); + *(uint8_t *)(q + 14) = 5; // MD5 + size = 15; + } + else + { + *(uint8_t *)(q + 11) = 4; + *(uint16_t *)(q + 12) = htons(PPPPAP); + size = 14; + } - LOG_HEX(5, "PPPLCP", q, 14); - tunnelsend(b, (q - b) + 14, t); + LOG_HEX(5, "PPPLCP", q, size); + tunnelsend(b, (q - b) + size, t); } // Send CCP request for no compression