Michael Fischler / Mbed 2 deprecated Contest_Winner

Dependencies:   mbed

Committer:
mafischl
Date:
Thu Oct 13 18:01:31 2011 +0000
Revision:
1:5a7cce9994a3
Parent:
0:d9266031f832

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mafischl 0:d9266031f832 1 /*****************************************************************************
mafischl 0:d9266031f832 2 * pap.c - Network Password Authentication Protocol program file.
mafischl 0:d9266031f832 3 *
mafischl 0:d9266031f832 4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
mafischl 0:d9266031f832 5 * portions Copyright (c) 1997 by Global Election Systems Inc.
mafischl 0:d9266031f832 6 *
mafischl 0:d9266031f832 7 * The authors hereby grant permission to use, copy, modify, distribute,
mafischl 0:d9266031f832 8 * and license this software and its documentation for any purpose, provided
mafischl 0:d9266031f832 9 * that existing copyright notices are retained in all copies and that this
mafischl 0:d9266031f832 10 * notice and the following disclaimer are included verbatim in any
mafischl 0:d9266031f832 11 * distributions. No written agreement, license, or royalty fee is required
mafischl 0:d9266031f832 12 * for any of the authorized uses.
mafischl 0:d9266031f832 13 *
mafischl 0:d9266031f832 14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
mafischl 0:d9266031f832 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
mafischl 0:d9266031f832 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
mafischl 0:d9266031f832 17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
mafischl 0:d9266031f832 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
mafischl 0:d9266031f832 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
mafischl 0:d9266031f832 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
mafischl 0:d9266031f832 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
mafischl 0:d9266031f832 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
mafischl 0:d9266031f832 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mafischl 0:d9266031f832 24 *
mafischl 0:d9266031f832 25 ******************************************************************************
mafischl 0:d9266031f832 26 * REVISION HISTORY
mafischl 0:d9266031f832 27 *
mafischl 0:d9266031f832 28 * 03-01-01 Marc Boucher <marc@mbsi.ca>
mafischl 0:d9266031f832 29 * Ported to lwIP.
mafischl 0:d9266031f832 30 * 97-12-12 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
mafischl 0:d9266031f832 31 * Original.
mafischl 0:d9266031f832 32 *****************************************************************************/
mafischl 0:d9266031f832 33 /*
mafischl 0:d9266031f832 34 * upap.c - User/Password Authentication Protocol.
mafischl 0:d9266031f832 35 *
mafischl 0:d9266031f832 36 * Copyright (c) 1989 Carnegie Mellon University.
mafischl 0:d9266031f832 37 * All rights reserved.
mafischl 0:d9266031f832 38 *
mafischl 0:d9266031f832 39 * Redistribution and use in source and binary forms are permitted
mafischl 0:d9266031f832 40 * provided that the above copyright notice and this paragraph are
mafischl 0:d9266031f832 41 * duplicated in all such forms and that any documentation,
mafischl 0:d9266031f832 42 * advertising materials, and other materials related to such
mafischl 0:d9266031f832 43 * distribution and use acknowledge that the software was developed
mafischl 0:d9266031f832 44 * by Carnegie Mellon University. The name of the
mafischl 0:d9266031f832 45 * University may not be used to endorse or promote products derived
mafischl 0:d9266031f832 46 * from this software without specific prior written permission.
mafischl 0:d9266031f832 47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
mafischl 0:d9266031f832 48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
mafischl 0:d9266031f832 49 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
mafischl 0:d9266031f832 50 */
mafischl 0:d9266031f832 51
mafischl 0:d9266031f832 52 #include "lwip/opt.h"
mafischl 0:d9266031f832 53
mafischl 0:d9266031f832 54 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
mafischl 0:d9266031f832 55
mafischl 0:d9266031f832 56 #if PAP_SUPPORT /* don't build if not configured for use in lwipopts.h */
mafischl 0:d9266031f832 57
mafischl 0:d9266031f832 58 #include "ppp.h"
mafischl 0:d9266031f832 59 #include "pppdebug.h"
mafischl 0:d9266031f832 60
mafischl 0:d9266031f832 61 #include "auth.h"
mafischl 0:d9266031f832 62 #include "pap.h"
mafischl 0:d9266031f832 63
mafischl 0:d9266031f832 64 #include <string.h>
mafischl 0:d9266031f832 65
mafischl 0:d9266031f832 66 #if 0 /* UNUSED */
mafischl 0:d9266031f832 67 static bool hide_password = 1;
mafischl 0:d9266031f832 68
mafischl 0:d9266031f832 69 /*
mafischl 0:d9266031f832 70 * Command-line options.
mafischl 0:d9266031f832 71 */
mafischl 0:d9266031f832 72 static option_t pap_option_list[] = {
mafischl 0:d9266031f832 73 { "hide-password", o_bool, &hide_password,
mafischl 0:d9266031f832 74 "Don't output passwords to log", 1 },
mafischl 0:d9266031f832 75 { "show-password", o_bool, &hide_password,
mafischl 0:d9266031f832 76 "Show password string in debug log messages", 0 },
mafischl 0:d9266031f832 77 { "pap-restart", o_int, &upap[0].us_timeouttime,
mafischl 0:d9266031f832 78 "Set retransmit timeout for PAP" },
mafischl 0:d9266031f832 79 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
mafischl 0:d9266031f832 80 "Set max number of transmissions for auth-reqs" },
mafischl 0:d9266031f832 81 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
mafischl 0:d9266031f832 82 "Set time limit for peer PAP authentication" },
mafischl 0:d9266031f832 83 { NULL }
mafischl 0:d9266031f832 84 };
mafischl 0:d9266031f832 85 #endif
mafischl 0:d9266031f832 86
mafischl 0:d9266031f832 87 /*
mafischl 0:d9266031f832 88 * Protocol entry points.
mafischl 0:d9266031f832 89 */
mafischl 0:d9266031f832 90 static void upap_init (int);
mafischl 0:d9266031f832 91 static void upap_lowerup (int);
mafischl 0:d9266031f832 92 static void upap_lowerdown (int);
mafischl 0:d9266031f832 93 static void upap_input (int, u_char *, int);
mafischl 0:d9266031f832 94 static void upap_protrej (int);
mafischl 0:d9266031f832 95 #if PPP_ADDITIONAL_CALLBACKS
mafischl 0:d9266031f832 96 static int upap_printpkt (u_char *, int, void (*)(void *, char *, ...), void *);
mafischl 0:d9266031f832 97 #endif /* PPP_ADDITIONAL_CALLBACKS */
mafischl 0:d9266031f832 98
mafischl 0:d9266031f832 99 struct protent pap_protent = {
mafischl 0:d9266031f832 100 PPP_PAP,
mafischl 0:d9266031f832 101 upap_init,
mafischl 0:d9266031f832 102 upap_input,
mafischl 0:d9266031f832 103 upap_protrej,
mafischl 0:d9266031f832 104 upap_lowerup,
mafischl 0:d9266031f832 105 upap_lowerdown,
mafischl 0:d9266031f832 106 NULL,
mafischl 0:d9266031f832 107 NULL,
mafischl 0:d9266031f832 108 #if PPP_ADDITIONAL_CALLBACKS
mafischl 0:d9266031f832 109 upap_printpkt,
mafischl 0:d9266031f832 110 NULL,
mafischl 0:d9266031f832 111 #endif /* PPP_ADDITIONAL_CALLBACKS */
mafischl 0:d9266031f832 112 1,
mafischl 0:d9266031f832 113 "PAP",
mafischl 0:d9266031f832 114 #if PPP_ADDITIONAL_CALLBACKS
mafischl 0:d9266031f832 115 NULL,
mafischl 0:d9266031f832 116 NULL,
mafischl 0:d9266031f832 117 NULL
mafischl 0:d9266031f832 118 #endif /* PPP_ADDITIONAL_CALLBACKS */
mafischl 0:d9266031f832 119 };
mafischl 0:d9266031f832 120
mafischl 0:d9266031f832 121 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
mafischl 0:d9266031f832 122
mafischl 0:d9266031f832 123 static void upap_timeout (void *);
mafischl 0:d9266031f832 124 static void upap_reqtimeout(void *);
mafischl 0:d9266031f832 125 static void upap_rauthreq (upap_state *, u_char *, u_char, int);
mafischl 0:d9266031f832 126 static void upap_rauthack (upap_state *, u_char *, int, int);
mafischl 0:d9266031f832 127 static void upap_rauthnak (upap_state *, u_char *, int, int);
mafischl 0:d9266031f832 128 static void upap_sauthreq (upap_state *);
mafischl 0:d9266031f832 129 static void upap_sresp (upap_state *, u_char, u_char, char *, int);
mafischl 0:d9266031f832 130
mafischl 0:d9266031f832 131
mafischl 0:d9266031f832 132 /*
mafischl 0:d9266031f832 133 * upap_init - Initialize a UPAP unit.
mafischl 0:d9266031f832 134 */
mafischl 0:d9266031f832 135 static void
mafischl 0:d9266031f832 136 upap_init(int unit)
mafischl 0:d9266031f832 137 {
mafischl 0:d9266031f832 138 upap_state *u = &upap[unit];
mafischl 0:d9266031f832 139
mafischl 0:d9266031f832 140 UPAPDEBUG(LOG_INFO, ("upap_init: %d\n", unit));
mafischl 0:d9266031f832 141 u->us_unit = unit;
mafischl 0:d9266031f832 142 u->us_user = NULL;
mafischl 0:d9266031f832 143 u->us_userlen = 0;
mafischl 0:d9266031f832 144 u->us_passwd = NULL;
mafischl 0:d9266031f832 145 u->us_passwdlen = 0;
mafischl 0:d9266031f832 146 u->us_clientstate = UPAPCS_INITIAL;
mafischl 0:d9266031f832 147 u->us_serverstate = UPAPSS_INITIAL;
mafischl 0:d9266031f832 148 u->us_id = 0;
mafischl 0:d9266031f832 149 u->us_timeouttime = UPAP_DEFTIMEOUT;
mafischl 0:d9266031f832 150 u->us_maxtransmits = 10;
mafischl 0:d9266031f832 151 u->us_reqtimeout = UPAP_DEFREQTIME;
mafischl 0:d9266031f832 152 }
mafischl 0:d9266031f832 153
mafischl 0:d9266031f832 154 /*
mafischl 0:d9266031f832 155 * upap_authwithpeer - Authenticate us with our peer (start client).
mafischl 0:d9266031f832 156 *
mafischl 0:d9266031f832 157 * Set new state and send authenticate's.
mafischl 0:d9266031f832 158 */
mafischl 0:d9266031f832 159 void
mafischl 0:d9266031f832 160 upap_authwithpeer(int unit, char *user, char *password)
mafischl 0:d9266031f832 161 {
mafischl 0:d9266031f832 162 upap_state *u = &upap[unit];
mafischl 0:d9266031f832 163
mafischl 0:d9266031f832 164 UPAPDEBUG(LOG_INFO, ("upap_authwithpeer: %d user=%s password=%s s=%d\n",
mafischl 0:d9266031f832 165 unit, user, password, u->us_clientstate));
mafischl 0:d9266031f832 166
mafischl 0:d9266031f832 167 /* Save the username and password we're given */
mafischl 0:d9266031f832 168 u->us_user = user;
mafischl 0:d9266031f832 169 u->us_userlen = (int)strlen(user);
mafischl 0:d9266031f832 170 u->us_passwd = password;
mafischl 0:d9266031f832 171 u->us_passwdlen = (int)strlen(password);
mafischl 0:d9266031f832 172
mafischl 0:d9266031f832 173 u->us_transmits = 0;
mafischl 0:d9266031f832 174
mafischl 0:d9266031f832 175 /* Lower layer up yet? */
mafischl 0:d9266031f832 176 if (u->us_clientstate == UPAPCS_INITIAL ||
mafischl 0:d9266031f832 177 u->us_clientstate == UPAPCS_PENDING) {
mafischl 0:d9266031f832 178 u->us_clientstate = UPAPCS_PENDING;
mafischl 0:d9266031f832 179 return;
mafischl 0:d9266031f832 180 }
mafischl 0:d9266031f832 181
mafischl 0:d9266031f832 182 upap_sauthreq(u); /* Start protocol */
mafischl 0:d9266031f832 183 }
mafischl 0:d9266031f832 184
mafischl 0:d9266031f832 185
mafischl 0:d9266031f832 186 /*
mafischl 0:d9266031f832 187 * upap_authpeer - Authenticate our peer (start server).
mafischl 0:d9266031f832 188 *
mafischl 0:d9266031f832 189 * Set new state.
mafischl 0:d9266031f832 190 */
mafischl 0:d9266031f832 191 void
mafischl 0:d9266031f832 192 upap_authpeer(int unit)
mafischl 0:d9266031f832 193 {
mafischl 0:d9266031f832 194 upap_state *u = &upap[unit];
mafischl 0:d9266031f832 195
mafischl 0:d9266031f832 196 /* Lower layer up yet? */
mafischl 0:d9266031f832 197 if (u->us_serverstate == UPAPSS_INITIAL ||
mafischl 0:d9266031f832 198 u->us_serverstate == UPAPSS_PENDING) {
mafischl 0:d9266031f832 199 u->us_serverstate = UPAPSS_PENDING;
mafischl 0:d9266031f832 200 return;
mafischl 0:d9266031f832 201 }
mafischl 0:d9266031f832 202
mafischl 0:d9266031f832 203 u->us_serverstate = UPAPSS_LISTEN;
mafischl 0:d9266031f832 204 if (u->us_reqtimeout > 0) {
mafischl 0:d9266031f832 205 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
mafischl 0:d9266031f832 206 }
mafischl 0:d9266031f832 207 }
mafischl 0:d9266031f832 208
mafischl 0:d9266031f832 209 /*
mafischl 0:d9266031f832 210 * upap_timeout - Retransmission timer for sending auth-reqs expired.
mafischl 0:d9266031f832 211 */
mafischl 0:d9266031f832 212 static void
mafischl 0:d9266031f832 213 upap_timeout(void *arg)
mafischl 0:d9266031f832 214 {
mafischl 0:d9266031f832 215 upap_state *u = (upap_state *) arg;
mafischl 0:d9266031f832 216
mafischl 0:d9266031f832 217 UPAPDEBUG(LOG_INFO, ("upap_timeout: %d timeout %d expired s=%d\n",
mafischl 0:d9266031f832 218 u->us_unit, u->us_timeouttime, u->us_clientstate));
mafischl 0:d9266031f832 219
mafischl 0:d9266031f832 220 if (u->us_clientstate != UPAPCS_AUTHREQ) {
mafischl 0:d9266031f832 221 UPAPDEBUG(LOG_INFO, ("upap_timeout: not in AUTHREQ state!\n"));
mafischl 0:d9266031f832 222 return;
mafischl 0:d9266031f832 223 }
mafischl 0:d9266031f832 224
mafischl 0:d9266031f832 225 if (u->us_transmits >= u->us_maxtransmits) {
mafischl 0:d9266031f832 226 /* give up in disgust */
mafischl 0:d9266031f832 227 UPAPDEBUG(LOG_ERR, ("No response to PAP authenticate-requests\n"));
mafischl 0:d9266031f832 228 u->us_clientstate = UPAPCS_BADAUTH;
mafischl 0:d9266031f832 229 auth_withpeer_fail(u->us_unit, PPP_PAP);
mafischl 0:d9266031f832 230 return;
mafischl 0:d9266031f832 231 }
mafischl 0:d9266031f832 232
mafischl 0:d9266031f832 233 upap_sauthreq(u); /* Send Authenticate-Request and set upap timeout*/
mafischl 0:d9266031f832 234 }
mafischl 0:d9266031f832 235
mafischl 0:d9266031f832 236
mafischl 0:d9266031f832 237 /*
mafischl 0:d9266031f832 238 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
mafischl 0:d9266031f832 239 */
mafischl 0:d9266031f832 240 static void
mafischl 0:d9266031f832 241 upap_reqtimeout(void *arg)
mafischl 0:d9266031f832 242 {
mafischl 0:d9266031f832 243 upap_state *u = (upap_state *) arg;
mafischl 0:d9266031f832 244
mafischl 0:d9266031f832 245 if (u->us_serverstate != UPAPSS_LISTEN) {
mafischl 0:d9266031f832 246 return; /* huh?? */
mafischl 0:d9266031f832 247 }
mafischl 0:d9266031f832 248
mafischl 0:d9266031f832 249 auth_peer_fail(u->us_unit, PPP_PAP);
mafischl 0:d9266031f832 250 u->us_serverstate = UPAPSS_BADAUTH;
mafischl 0:d9266031f832 251 }
mafischl 0:d9266031f832 252
mafischl 0:d9266031f832 253
mafischl 0:d9266031f832 254 /*
mafischl 0:d9266031f832 255 * upap_lowerup - The lower layer is up.
mafischl 0:d9266031f832 256 *
mafischl 0:d9266031f832 257 * Start authenticating if pending.
mafischl 0:d9266031f832 258 */
mafischl 0:d9266031f832 259 static void
mafischl 0:d9266031f832 260 upap_lowerup(int unit)
mafischl 0:d9266031f832 261 {
mafischl 0:d9266031f832 262 upap_state *u = &upap[unit];
mafischl 0:d9266031f832 263
mafischl 0:d9266031f832 264 UPAPDEBUG(LOG_INFO, ("upap_lowerup: init %d clientstate s=%d\n", unit, u->us_clientstate));
mafischl 0:d9266031f832 265
mafischl 0:d9266031f832 266 if (u->us_clientstate == UPAPCS_INITIAL) {
mafischl 0:d9266031f832 267 u->us_clientstate = UPAPCS_CLOSED;
mafischl 0:d9266031f832 268 } else if (u->us_clientstate == UPAPCS_PENDING) {
mafischl 0:d9266031f832 269 upap_sauthreq(u); /* send an auth-request */
mafischl 0:d9266031f832 270 /* now client state is UPAPCS__AUTHREQ */
mafischl 0:d9266031f832 271 }
mafischl 0:d9266031f832 272
mafischl 0:d9266031f832 273 if (u->us_serverstate == UPAPSS_INITIAL) {
mafischl 0:d9266031f832 274 u->us_serverstate = UPAPSS_CLOSED;
mafischl 0:d9266031f832 275 } else if (u->us_serverstate == UPAPSS_PENDING) {
mafischl 0:d9266031f832 276 u->us_serverstate = UPAPSS_LISTEN;
mafischl 0:d9266031f832 277 if (u->us_reqtimeout > 0) {
mafischl 0:d9266031f832 278 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
mafischl 0:d9266031f832 279 }
mafischl 0:d9266031f832 280 }
mafischl 0:d9266031f832 281 }
mafischl 0:d9266031f832 282
mafischl 0:d9266031f832 283
mafischl 0:d9266031f832 284 /*
mafischl 0:d9266031f832 285 * upap_lowerdown - The lower layer is down.
mafischl 0:d9266031f832 286 *
mafischl 0:d9266031f832 287 * Cancel all timeouts.
mafischl 0:d9266031f832 288 */
mafischl 0:d9266031f832 289 static void
mafischl 0:d9266031f832 290 upap_lowerdown(int unit)
mafischl 0:d9266031f832 291 {
mafischl 0:d9266031f832 292 upap_state *u = &upap[unit];
mafischl 0:d9266031f832 293
mafischl 0:d9266031f832 294 UPAPDEBUG(LOG_INFO, ("upap_lowerdown: %d s=%d\n", unit, u->us_clientstate));
mafischl 0:d9266031f832 295
mafischl 0:d9266031f832 296 if (u->us_clientstate == UPAPCS_AUTHREQ) { /* Timeout pending? */
mafischl 0:d9266031f832 297 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
mafischl 0:d9266031f832 298 }
mafischl 0:d9266031f832 299 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0) {
mafischl 0:d9266031f832 300 UNTIMEOUT(upap_reqtimeout, u);
mafischl 0:d9266031f832 301 }
mafischl 0:d9266031f832 302
mafischl 0:d9266031f832 303 u->us_clientstate = UPAPCS_INITIAL;
mafischl 0:d9266031f832 304 u->us_serverstate = UPAPSS_INITIAL;
mafischl 0:d9266031f832 305 }
mafischl 0:d9266031f832 306
mafischl 0:d9266031f832 307
mafischl 0:d9266031f832 308 /*
mafischl 0:d9266031f832 309 * upap_protrej - Peer doesn't speak this protocol.
mafischl 0:d9266031f832 310 *
mafischl 0:d9266031f832 311 * This shouldn't happen. In any case, pretend lower layer went down.
mafischl 0:d9266031f832 312 */
mafischl 0:d9266031f832 313 static void
mafischl 0:d9266031f832 314 upap_protrej(int unit)
mafischl 0:d9266031f832 315 {
mafischl 0:d9266031f832 316 upap_state *u = &upap[unit];
mafischl 0:d9266031f832 317
mafischl 0:d9266031f832 318 if (u->us_clientstate == UPAPCS_AUTHREQ) {
mafischl 0:d9266031f832 319 UPAPDEBUG(LOG_ERR, ("PAP authentication failed due to protocol-reject\n"));
mafischl 0:d9266031f832 320 auth_withpeer_fail(unit, PPP_PAP);
mafischl 0:d9266031f832 321 }
mafischl 0:d9266031f832 322 if (u->us_serverstate == UPAPSS_LISTEN) {
mafischl 0:d9266031f832 323 UPAPDEBUG(LOG_ERR, ("PAP authentication of peer failed (protocol-reject)\n"));
mafischl 0:d9266031f832 324 auth_peer_fail(unit, PPP_PAP);
mafischl 0:d9266031f832 325 }
mafischl 0:d9266031f832 326 upap_lowerdown(unit);
mafischl 0:d9266031f832 327 }
mafischl 0:d9266031f832 328
mafischl 0:d9266031f832 329
mafischl 0:d9266031f832 330 /*
mafischl 0:d9266031f832 331 * upap_input - Input UPAP packet.
mafischl 0:d9266031f832 332 */
mafischl 0:d9266031f832 333 static void
mafischl 0:d9266031f832 334 upap_input(int unit, u_char *inpacket, int l)
mafischl 0:d9266031f832 335 {
mafischl 0:d9266031f832 336 upap_state *u = &upap[unit];
mafischl 0:d9266031f832 337 u_char *inp;
mafischl 0:d9266031f832 338 u_char code, id;
mafischl 0:d9266031f832 339 int len;
mafischl 0:d9266031f832 340
mafischl 0:d9266031f832 341 /*
mafischl 0:d9266031f832 342 * Parse header (code, id and length).
mafischl 0:d9266031f832 343 * If packet too short, drop it.
mafischl 0:d9266031f832 344 */
mafischl 0:d9266031f832 345 inp = inpacket;
mafischl 0:d9266031f832 346 if (l < (int)UPAP_HEADERLEN) {
mafischl 0:d9266031f832 347 UPAPDEBUG(LOG_INFO, ("pap_input: rcvd short header.\n"));
mafischl 0:d9266031f832 348 return;
mafischl 0:d9266031f832 349 }
mafischl 0:d9266031f832 350 GETCHAR(code, inp);
mafischl 0:d9266031f832 351 GETCHAR(id, inp);
mafischl 0:d9266031f832 352 GETSHORT(len, inp);
mafischl 0:d9266031f832 353 if (len < (int)UPAP_HEADERLEN) {
mafischl 0:d9266031f832 354 UPAPDEBUG(LOG_INFO, ("pap_input: rcvd illegal length.\n"));
mafischl 0:d9266031f832 355 return;
mafischl 0:d9266031f832 356 }
mafischl 0:d9266031f832 357 if (len > l) {
mafischl 0:d9266031f832 358 UPAPDEBUG(LOG_INFO, ("pap_input: rcvd short packet.\n"));
mafischl 0:d9266031f832 359 return;
mafischl 0:d9266031f832 360 }
mafischl 0:d9266031f832 361 len -= UPAP_HEADERLEN;
mafischl 0:d9266031f832 362
mafischl 0:d9266031f832 363 /*
mafischl 0:d9266031f832 364 * Action depends on code.
mafischl 0:d9266031f832 365 */
mafischl 0:d9266031f832 366 switch (code) {
mafischl 0:d9266031f832 367 case UPAP_AUTHREQ:
mafischl 0:d9266031f832 368 upap_rauthreq(u, inp, id, len);
mafischl 0:d9266031f832 369 break;
mafischl 0:d9266031f832 370
mafischl 0:d9266031f832 371 case UPAP_AUTHACK:
mafischl 0:d9266031f832 372 upap_rauthack(u, inp, id, len);
mafischl 0:d9266031f832 373 break;
mafischl 0:d9266031f832 374
mafischl 0:d9266031f832 375 case UPAP_AUTHNAK:
mafischl 0:d9266031f832 376 upap_rauthnak(u, inp, id, len);
mafischl 0:d9266031f832 377 break;
mafischl 0:d9266031f832 378
mafischl 0:d9266031f832 379 default: /* XXX Need code reject */
mafischl 0:d9266031f832 380 UPAPDEBUG(LOG_INFO, ("pap_input: UNHANDLED default: code: %d, id: %d, len: %d.\n", code, id, len));
mafischl 0:d9266031f832 381 break;
mafischl 0:d9266031f832 382 }
mafischl 0:d9266031f832 383 }
mafischl 0:d9266031f832 384
mafischl 0:d9266031f832 385
mafischl 0:d9266031f832 386 /*
mafischl 0:d9266031f832 387 * upap_rauth - Receive Authenticate.
mafischl 0:d9266031f832 388 */
mafischl 0:d9266031f832 389 static void
mafischl 0:d9266031f832 390 upap_rauthreq(upap_state *u, u_char *inp, u_char id, int len)
mafischl 0:d9266031f832 391 {
mafischl 0:d9266031f832 392 u_char ruserlen, rpasswdlen;
mafischl 0:d9266031f832 393 char *ruser, *rpasswd;
mafischl 0:d9266031f832 394 u_char retcode;
mafischl 0:d9266031f832 395 char *msg;
mafischl 0:d9266031f832 396 int msglen;
mafischl 0:d9266031f832 397
mafischl 0:d9266031f832 398 UPAPDEBUG(LOG_INFO, ("pap_rauth: Rcvd id %d.\n", id));
mafischl 0:d9266031f832 399
mafischl 0:d9266031f832 400 if (u->us_serverstate < UPAPSS_LISTEN) {
mafischl 0:d9266031f832 401 return;
mafischl 0:d9266031f832 402 }
mafischl 0:d9266031f832 403
mafischl 0:d9266031f832 404 /*
mafischl 0:d9266031f832 405 * If we receive a duplicate authenticate-request, we are
mafischl 0:d9266031f832 406 * supposed to return the same status as for the first request.
mafischl 0:d9266031f832 407 */
mafischl 0:d9266031f832 408 if (u->us_serverstate == UPAPSS_OPEN) {
mafischl 0:d9266031f832 409 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
mafischl 0:d9266031f832 410 return;
mafischl 0:d9266031f832 411 }
mafischl 0:d9266031f832 412 if (u->us_serverstate == UPAPSS_BADAUTH) {
mafischl 0:d9266031f832 413 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
mafischl 0:d9266031f832 414 return;
mafischl 0:d9266031f832 415 }
mafischl 0:d9266031f832 416
mafischl 0:d9266031f832 417 /*
mafischl 0:d9266031f832 418 * Parse user/passwd.
mafischl 0:d9266031f832 419 */
mafischl 0:d9266031f832 420 if (len < (int)sizeof (u_char)) {
mafischl 0:d9266031f832 421 UPAPDEBUG(LOG_INFO, ("pap_rauth: rcvd short packet.\n"));
mafischl 0:d9266031f832 422 return;
mafischl 0:d9266031f832 423 }
mafischl 0:d9266031f832 424 GETCHAR(ruserlen, inp);
mafischl 0:d9266031f832 425 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
mafischl 0:d9266031f832 426 if (len < 0) {
mafischl 0:d9266031f832 427 UPAPDEBUG(LOG_INFO, ("pap_rauth: rcvd short packet.\n"));
mafischl 0:d9266031f832 428 return;
mafischl 0:d9266031f832 429 }
mafischl 0:d9266031f832 430 ruser = (char *) inp;
mafischl 0:d9266031f832 431 INCPTR(ruserlen, inp);
mafischl 0:d9266031f832 432 GETCHAR(rpasswdlen, inp);
mafischl 0:d9266031f832 433 if (len < rpasswdlen) {
mafischl 0:d9266031f832 434 UPAPDEBUG(LOG_INFO, ("pap_rauth: rcvd short packet.\n"));
mafischl 0:d9266031f832 435 return;
mafischl 0:d9266031f832 436 }
mafischl 0:d9266031f832 437 rpasswd = (char *) inp;
mafischl 0:d9266031f832 438
mafischl 0:d9266031f832 439 /*
mafischl 0:d9266031f832 440 * Check the username and password given.
mafischl 0:d9266031f832 441 */
mafischl 0:d9266031f832 442 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd, rpasswdlen, &msg, &msglen);
mafischl 0:d9266031f832 443 /* lwip: currently retcode is always UPAP_AUTHACK */
mafischl 0:d9266031f832 444 BZERO(rpasswd, rpasswdlen);
mafischl 0:d9266031f832 445
mafischl 0:d9266031f832 446 upap_sresp(u, retcode, id, msg, msglen);
mafischl 0:d9266031f832 447
mafischl 0:d9266031f832 448 if (retcode == UPAP_AUTHACK) {
mafischl 0:d9266031f832 449 u->us_serverstate = UPAPSS_OPEN;
mafischl 0:d9266031f832 450 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
mafischl 0:d9266031f832 451 } else {
mafischl 0:d9266031f832 452 u->us_serverstate = UPAPSS_BADAUTH;
mafischl 0:d9266031f832 453 auth_peer_fail(u->us_unit, PPP_PAP);
mafischl 0:d9266031f832 454 }
mafischl 0:d9266031f832 455
mafischl 0:d9266031f832 456 if (u->us_reqtimeout > 0) {
mafischl 0:d9266031f832 457 UNTIMEOUT(upap_reqtimeout, u);
mafischl 0:d9266031f832 458 }
mafischl 0:d9266031f832 459 }
mafischl 0:d9266031f832 460
mafischl 0:d9266031f832 461
mafischl 0:d9266031f832 462 /*
mafischl 0:d9266031f832 463 * upap_rauthack - Receive Authenticate-Ack.
mafischl 0:d9266031f832 464 */
mafischl 0:d9266031f832 465 static void
mafischl 0:d9266031f832 466 upap_rauthack(upap_state *u, u_char *inp, int id, int len)
mafischl 0:d9266031f832 467 {
mafischl 0:d9266031f832 468 u_char msglen;
mafischl 0:d9266031f832 469 char *msg;
mafischl 0:d9266031f832 470
mafischl 0:d9266031f832 471 LWIP_UNUSED_ARG(id);
mafischl 0:d9266031f832 472
mafischl 0:d9266031f832 473 UPAPDEBUG(LOG_INFO, ("pap_rauthack: Rcvd id %d s=%d\n", id, u->us_clientstate));
mafischl 0:d9266031f832 474
mafischl 0:d9266031f832 475 if (u->us_clientstate != UPAPCS_AUTHREQ) { /* XXX */
mafischl 0:d9266031f832 476 UPAPDEBUG(LOG_INFO, ("pap_rauthack: us_clientstate != UPAPCS_AUTHREQ\n"));
mafischl 0:d9266031f832 477 return;
mafischl 0:d9266031f832 478 }
mafischl 0:d9266031f832 479
mafischl 0:d9266031f832 480 /*
mafischl 0:d9266031f832 481 * Parse message.
mafischl 0:d9266031f832 482 */
mafischl 0:d9266031f832 483 if (len < (int)sizeof (u_char)) {
mafischl 0:d9266031f832 484 UPAPDEBUG(LOG_INFO, ("pap_rauthack: ignoring missing msg-length.\n"));
mafischl 0:d9266031f832 485 } else {
mafischl 0:d9266031f832 486 GETCHAR(msglen, inp);
mafischl 0:d9266031f832 487 if (msglen > 0) {
mafischl 0:d9266031f832 488 len -= sizeof (u_char);
mafischl 0:d9266031f832 489 if (len < msglen) {
mafischl 0:d9266031f832 490 UPAPDEBUG(LOG_INFO, ("pap_rauthack: rcvd short packet.\n"));
mafischl 0:d9266031f832 491 return;
mafischl 0:d9266031f832 492 }
mafischl 0:d9266031f832 493 msg = (char *) inp;
mafischl 0:d9266031f832 494 PRINTMSG(msg, msglen);
mafischl 0:d9266031f832 495 }
mafischl 0:d9266031f832 496 }
mafischl 0:d9266031f832 497 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
mafischl 0:d9266031f832 498 u->us_clientstate = UPAPCS_OPEN;
mafischl 0:d9266031f832 499
mafischl 0:d9266031f832 500 auth_withpeer_success(u->us_unit, PPP_PAP);
mafischl 0:d9266031f832 501 }
mafischl 0:d9266031f832 502
mafischl 0:d9266031f832 503
mafischl 0:d9266031f832 504 /*
mafischl 0:d9266031f832 505 * upap_rauthnak - Receive Authenticate-Nak.
mafischl 0:d9266031f832 506 */
mafischl 0:d9266031f832 507 static void
mafischl 0:d9266031f832 508 upap_rauthnak(upap_state *u, u_char *inp, int id, int len)
mafischl 0:d9266031f832 509 {
mafischl 0:d9266031f832 510 u_char msglen;
mafischl 0:d9266031f832 511 char *msg;
mafischl 0:d9266031f832 512
mafischl 0:d9266031f832 513 LWIP_UNUSED_ARG(id);
mafischl 0:d9266031f832 514
mafischl 0:d9266031f832 515 UPAPDEBUG(LOG_INFO, ("pap_rauthnak: Rcvd id %d s=%d\n", id, u->us_clientstate));
mafischl 0:d9266031f832 516
mafischl 0:d9266031f832 517 if (u->us_clientstate != UPAPCS_AUTHREQ) { /* XXX */
mafischl 0:d9266031f832 518 return;
mafischl 0:d9266031f832 519 }
mafischl 0:d9266031f832 520
mafischl 0:d9266031f832 521 /*
mafischl 0:d9266031f832 522 * Parse message.
mafischl 0:d9266031f832 523 */
mafischl 0:d9266031f832 524 if (len < sizeof (u_char)) {
mafischl 0:d9266031f832 525 UPAPDEBUG(LOG_INFO, ("pap_rauthnak: ignoring missing msg-length.\n"));
mafischl 0:d9266031f832 526 } else {
mafischl 0:d9266031f832 527 GETCHAR(msglen, inp);
mafischl 0:d9266031f832 528 if(msglen > 0) {
mafischl 0:d9266031f832 529 len -= sizeof (u_char);
mafischl 0:d9266031f832 530 if (len < msglen) {
mafischl 0:d9266031f832 531 UPAPDEBUG(LOG_INFO, ("pap_rauthnak: rcvd short packet.\n"));
mafischl 0:d9266031f832 532 return;
mafischl 0:d9266031f832 533 }
mafischl 0:d9266031f832 534 msg = (char *) inp;
mafischl 0:d9266031f832 535 PRINTMSG(msg, msglen);
mafischl 0:d9266031f832 536 }
mafischl 0:d9266031f832 537 }
mafischl 0:d9266031f832 538
mafischl 0:d9266031f832 539 u->us_clientstate = UPAPCS_BADAUTH;
mafischl 0:d9266031f832 540
mafischl 0:d9266031f832 541 UPAPDEBUG(LOG_ERR, ("PAP authentication failed\n"));
mafischl 0:d9266031f832 542 auth_withpeer_fail(u->us_unit, PPP_PAP);
mafischl 0:d9266031f832 543 }
mafischl 0:d9266031f832 544
mafischl 0:d9266031f832 545
mafischl 0:d9266031f832 546 /*
mafischl 0:d9266031f832 547 * upap_sauthreq - Send an Authenticate-Request.
mafischl 0:d9266031f832 548 */
mafischl 0:d9266031f832 549 static void
mafischl 0:d9266031f832 550 upap_sauthreq(upap_state *u)
mafischl 0:d9266031f832 551 {
mafischl 0:d9266031f832 552 u_char *outp;
mafischl 0:d9266031f832 553 int outlen;
mafischl 0:d9266031f832 554
mafischl 0:d9266031f832 555 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char)
mafischl 0:d9266031f832 556 + u->us_userlen + u->us_passwdlen;
mafischl 0:d9266031f832 557 outp = outpacket_buf[u->us_unit];
mafischl 0:d9266031f832 558
mafischl 0:d9266031f832 559 MAKEHEADER(outp, PPP_PAP);
mafischl 0:d9266031f832 560
mafischl 0:d9266031f832 561 PUTCHAR(UPAP_AUTHREQ, outp);
mafischl 0:d9266031f832 562 PUTCHAR(++u->us_id, outp);
mafischl 0:d9266031f832 563 PUTSHORT(outlen, outp);
mafischl 0:d9266031f832 564 PUTCHAR(u->us_userlen, outp);
mafischl 0:d9266031f832 565 BCOPY(u->us_user, outp, u->us_userlen);
mafischl 0:d9266031f832 566 INCPTR(u->us_userlen, outp);
mafischl 0:d9266031f832 567 PUTCHAR(u->us_passwdlen, outp);
mafischl 0:d9266031f832 568 BCOPY(u->us_passwd, outp, u->us_passwdlen);
mafischl 0:d9266031f832 569
mafischl 0:d9266031f832 570 pppWrite(u->us_unit, outpacket_buf[u->us_unit], outlen + PPP_HDRLEN);
mafischl 0:d9266031f832 571
mafischl 0:d9266031f832 572 UPAPDEBUG(LOG_INFO, ("pap_sauth: Sent id %d\n", u->us_id));
mafischl 0:d9266031f832 573
mafischl 0:d9266031f832 574 TIMEOUT(upap_timeout, u, u->us_timeouttime);
mafischl 0:d9266031f832 575 ++u->us_transmits;
mafischl 0:d9266031f832 576 u->us_clientstate = UPAPCS_AUTHREQ;
mafischl 0:d9266031f832 577 }
mafischl 0:d9266031f832 578
mafischl 0:d9266031f832 579
mafischl 0:d9266031f832 580 /*
mafischl 0:d9266031f832 581 * upap_sresp - Send a response (ack or nak).
mafischl 0:d9266031f832 582 */
mafischl 0:d9266031f832 583 static void
mafischl 0:d9266031f832 584 upap_sresp(upap_state *u, u_char code, u_char id, char *msg, int msglen)
mafischl 0:d9266031f832 585 {
mafischl 0:d9266031f832 586 u_char *outp;
mafischl 0:d9266031f832 587 int outlen;
mafischl 0:d9266031f832 588
mafischl 0:d9266031f832 589 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
mafischl 0:d9266031f832 590 outp = outpacket_buf[u->us_unit];
mafischl 0:d9266031f832 591 MAKEHEADER(outp, PPP_PAP);
mafischl 0:d9266031f832 592
mafischl 0:d9266031f832 593 PUTCHAR(code, outp);
mafischl 0:d9266031f832 594 PUTCHAR(id, outp);
mafischl 0:d9266031f832 595 PUTSHORT(outlen, outp);
mafischl 0:d9266031f832 596 PUTCHAR(msglen, outp);
mafischl 0:d9266031f832 597 BCOPY(msg, outp, msglen);
mafischl 0:d9266031f832 598 pppWrite(u->us_unit, outpacket_buf[u->us_unit], outlen + PPP_HDRLEN);
mafischl 0:d9266031f832 599
mafischl 0:d9266031f832 600 UPAPDEBUG(LOG_INFO, ("pap_sresp: Sent code %d, id %d s=%d\n", code, id, u->us_clientstate));
mafischl 0:d9266031f832 601 }
mafischl 0:d9266031f832 602
mafischl 0:d9266031f832 603 #if PPP_ADDITIONAL_CALLBACKS
mafischl 0:d9266031f832 604 static char *upap_codenames[] = {
mafischl 0:d9266031f832 605 "AuthReq", "AuthAck", "AuthNak"
mafischl 0:d9266031f832 606 };
mafischl 0:d9266031f832 607
mafischl 0:d9266031f832 608 /*
mafischl 0:d9266031f832 609 * upap_printpkt - print the contents of a PAP packet.
mafischl 0:d9266031f832 610 */
mafischl 0:d9266031f832 611 static int upap_printpkt(
mafischl 0:d9266031f832 612 u_char *p,
mafischl 0:d9266031f832 613 int plen,
mafischl 0:d9266031f832 614 void (*printer) (void *, char *, ...),
mafischl 0:d9266031f832 615 void *arg
mafischl 0:d9266031f832 616 )
mafischl 0:d9266031f832 617 {
mafischl 0:d9266031f832 618 LWIP_UNUSED_ARG(p);
mafischl 0:d9266031f832 619 LWIP_UNUSED_ARG(plen);
mafischl 0:d9266031f832 620 LWIP_UNUSED_ARG(printer);
mafischl 0:d9266031f832 621 LWIP_UNUSED_ARG(arg);
mafischl 0:d9266031f832 622 return 0;
mafischl 0:d9266031f832 623 }
mafischl 0:d9266031f832 624 #endif /* PPP_ADDITIONAL_CALLBACKS */
mafischl 0:d9266031f832 625
mafischl 0:d9266031f832 626 #endif /* PAP_SUPPORT */
mafischl 0:d9266031f832 627
mafischl 0:d9266031f832 628 #endif /* PPP_SUPPORT */