📄 ppppap.c
字号:
/***********************************************************************//* *//* Module: tcp_ip/ppp/ppppap.c *//* Release: 2001.3 *//* Version: 2000.1 *//* Purpose: Password Authentication Protocol (PAP) for PPP *//* *//*---------------------------------------------------------------------*//* *//* Copyright 2000, Blunk Microsystems *//* ALL RIGHTS RESERVED *//* *//* Licensees have the non-exclusive right to use, modify, or extract *//* this computer program for software development at a single site. *//* This program may be resold or disseminated in executable format *//* only. The source code may not be redistributed or resold. *//* *//***********************************************************************/#include "pppp.h"#include <string.h>#if MAX_PPP_INTF/***********************************************************************//* Configuration *//***********************************************************************/#define PAP_REQ_LIMIT 10 /* max REQ sends */#define PAP_NAK_LIMIT 5 /* max NAK sends */#define PAP_TIMEOUT 3 /* seconds to wait for response *//***********************************************************************//* Function Prototypes *//***********************************************************************/static NetBuf *make_req(FSM *fsm);/***********************************************************************//* Constant Data Definitions *//***********************************************************************/static const struct fsm_constants pap_constants ={ "PAP", PPP_PAP_PROTO, 0x000E, /* codes 1-3 recognized */ kPAP, PAP_REQ_LIMIT, PAP_NAK_LIMIT, 0, PAP_TIMEOUT * TICKS_PER_SEC, PppFsmNoAction, /* starting */ PppFsmNoAction, /* up */ PppFsmNoAction, /* down */ PppFsmNoAction, /* finished */ make_req, /* makereq */ PppFsmNoCheck, /* request */ PppFsmNoCheck, /* ack */ PppFsmNoCheck, /* nak */ PppFsmNoCheck, /* reject */};/***********************************************************************//* Local Function Definitions *//***********************************************************************//***********************************************************************//* make_req: Build a request to send to the authenticator *//* *//***********************************************************************/static NetBuf *make_req(FSM *fsm){ NetBuf *rbuf; int ulen, plen; ui8 *cp; /*-------------------------------------------------------------------*/ /* Attempt to allocate buffer. Return NULL if unable. */ /*-------------------------------------------------------------------*/ ulen = strlen(Ppp->public.username); plen = strlen(Ppp->public.password); rbuf = tcpGetBuf(NIMHLEN + 2 + ulen + plen + CRC16_LEN); if (rbuf == NULL) return NULL; /*-------------------------------------------------------------------*/ /* Copy user name and password into the packet as Pascal strings. */ /*-------------------------------------------------------------------*/#if PPP_TRACE pppLog(" making user: %s", Ppp->public.username); pppLog(" making password");#endif cp = rbuf->app_data; *cp++ = (ui8)ulen; if (ulen) memcpy(cp, Ppp->public.username, ulen); cp += ulen; *cp++ = (ui8)plen; if (plen) memcpy(cp, Ppp->public.password, plen); cp += plen; /*-------------------------------------------------------------------*/ /* Update buffer length and return pointer to buffer. */ /*-------------------------------------------------------------------*/ rbuf->length += 2 + ulen + plen; return rbuf;}/***********************************************************************//* pap_shutdown: Abandon PAP attempt, shutdown LCP layer *//* *//***********************************************************************/static void pap_shutdown(FSM *fsm){#if PPP_TRACE pppFsmLog(fsm, "Failed; close connection");#endif fsmLcpClose();}/***********************************************************************//* pap_up: Configuration negotiation complete *//* *//***********************************************************************/static void pap_up(FSM *fsm, uint flag){ /*-------------------------------------------------------------------*/ /* Clear the flags particular to this call. */ /*-------------------------------------------------------------------*/ Ppp->public.flags &= ~flag; /*-------------------------------------------------------------------*/ /* Check if all authentication flags have been cleared. */ /*-------------------------------------------------------------------*/ if ((Ppp->public.flags & PPPF_AUTHENTICATION) == 0) {#if PPP_TRACE pppFsmLog(fsm, "Open");#endif fsm->state = fsmOPENED; pppReady(); }}/***********************************************************************//* check_request: Check peer's request *//* *//***********************************************************************/static int check_request(FSM *fsm, ConfigHdr *hdr){ ui8 *cp; NetBuf *rbuf; int len, result; char *username, *password, *known_passwd, *message; /*-------------------------------------------------------------------*/ /* Extract username length and save pointer to username. */ /*-------------------------------------------------------------------*/ cp = pppRcvBuf->ip_pkt; len = *cp++; username = (char *)cp; /*-------------------------------------------------------------------*/ /* Advance to password length, extract it, then replace it with '0'. */ /*-------------------------------------------------------------------*/ cp += len; len = *cp; *cp = 0;#if PPP_TRACE pppLog(" checking user: %s", username);#endif /*-------------------------------------------------------------------*/ /* Save pointer to password and terminate password with '0'. */ /*-------------------------------------------------------------------*/ password = (char *)++cp; cp += len; *cp = 0;#if PPP_TRACE pppLog(" checking password: %s", password);#endif /*-------------------------------------------------------------------*/ /* Ensure packet is not a runt. */ /*-------------------------------------------------------------------*/ if (pppRcvBuf->length < (cp - pppRcvBuf->ip_pkt)) return -1; /*-------------------------------------------------------------------*/ /* Verify username and password. */ /*-------------------------------------------------------------------*/ known_passwd = SysPassword(username); if (known_passwd && (strcmp(known_passwd, password) == 0)) { result = CONFIG_ACK; message = "Welcome"; /* If desired, here would be the place to lookup the remote address ** using the peer's username. */ } else { result = CONFIG_NAK; message = "Invalid username or password"; } /*-------------------------------------------------------------------*/ /* Attempt to allocate buffer. Return NULL if unable. */ /*-------------------------------------------------------------------*/ len = strlen(message); rbuf = tcpGetBuf(NIMHLEN + len + 1 + CRC16_LEN); if (rbuf == NULL) return -1; /*-------------------------------------------------------------------*/ /* Copy message into the packet as a Pascal string. */ /*-------------------------------------------------------------------*/ rbuf->ip_pkt[0] = (ui8)len; memcpy(rbuf->ip_pkt + 1, message, len); rbuf->length = 1 + len; /*-------------------------------------------------------------------*/ /* Send ACK/NAK to the peer. */ /*-------------------------------------------------------------------*/ pppFsmSend(fsm, result, hdr->id, rbuf); /*-------------------------------------------------------------------*/ /* Count NAK replies and shutdown if retry limit exceeded. */ /*-------------------------------------------------------------------*/ if (result == CONFIG_NAK) { if (fsm->rcn_cnt < fsm->pdc->nak_limit) ++fsm->rcn_cnt; else pap_shutdown(fsm); } /*-------------------------------------------------------------------*/ /* Return CONFIG_ACK or CONFIG_NAK. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -