Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Sat Feb 25 03:28:05 2012 +0000
Revision:
1:c7958aa34fa1
Parent:
0:616601bde9fb
Use published libraries where possible.

Who changed what in which revision?

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