NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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