⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppppap.c

📁 在51单片机上实现TCPIP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  PPPPAP.C	-- Password Authentication Protocol for PPP
 *
 *	Acknowledgements and correction history may be found in PPP.C
 */

#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "proc.h"
#include "iface.h"
#include "session.h"
#include "socket.h"
#include "ppp.h"
#include "pppfsm.h"
#include "ppplcp.h"
#include "ppppap.h"
#include "cmdparse.h"
#include "files.h"
#include "trace.h"
#include "main.h"

static int dopap_user(int argc, char *argv[], void *p);

static void pap_monitor(int mustask, void *v1, void *v2);
static void pap_pwdlookup(struct pap_s *pap_p);

static struct mbuf *pap_makereq(struct fsm_s *fsm_p);

static int pap_verify(char *username, char *password);
static void pap_shutdown(struct fsm_s *fsm_p);
static void pap_opening(struct fsm_s *fsm_p, int flag);

static int pap_request(struct fsm_s *fsm_p,
			struct config_hdr *hdr,
			struct mbuf **data);
static int pap_check(struct fsm_s *fsm_p,
			struct config_hdr *hdr,
			struct mbuf **data);
static void pap_timeout(void *vp);

static void pap_free(struct fsm_s *fsm_p);


static struct fsm_constant_s pap_constants = {
	"Pap",
	PPP_PAP_PROTOCOL,
	0x000E,				/* codes 1-3 recognized */

	Pap,
	PAP_REQ_TRY,
	PAP_FAIL_MAX,
	0,
	PAP_TIMEOUT * 1000L,

	pap_free,

	fsm_no_action,		/* pap_reset, */
	fsm_no_action,		/* pap_starting, */
	fsm_no_action,		/* pap_opening, */
	fsm_no_action,		/* pap_closing, */
	fsm_no_action,		/* pap_stopping, */

	pap_makereq,
	fsm_no_check,		/* pap_request, */
	fsm_no_check,		/* pap_ack, */
	fsm_no_check,		/* pap_nak, */
	fsm_no_check,		/* pap_reject */
};


/****************************************************************************/

/* "ppp <iface> pap" subcommands */
static struct cmds Papcmds[] = {
	"timeout",	doppp_timeout,	0,	0,	NULL,
	"try",		doppp_try,	0,	0,	NULL,
	"user",		dopap_user,	0,	0,	NULL,
	NULL,
};


int
doppp_pap(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	register struct iface *ifp = p;
	register struct ppp_s *ppp_p = ifp->edv;

	return subcmd(Papcmds, argc, argv, &(ppp_p->fsm[Pap]));
}


/* Set user/password */
int
dopap_user(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	register struct fsm_s *fsm_p = p;
	register struct pap_s *pap_p = fsm_p->pdv;

	if (argc < 2) {
		printf("%s\n",
			(pap_p->username == NULL) ? "None" : pap_p->username);
		return 0;
	}
	free(pap_p->username);
	pap_p->username = NULL;
	free(pap_p->password);
	pap_p->password = NULL;

	if (stricmp(argv[1],"none") != 0) {
		pap_p->username = strdup(argv[1]);
		if (argc > 2) {
			pap_p->password = strdup(argv[2]);
		} else {
			pap_pwdlookup( pap_p );
		}
	}
	return 0;
}


/****************************************************************************/
/* Bring up a session on the console for for the username/password.
 * Return a NULL in either username or password if aborted.
 */
static void
pap_monitor(unused, v1, v2)
int unused;
void *v1;
void *v2;
{
	struct iface *iface = v1;
	struct fsm_s *fsm_p = v2;
	struct pap_s *pap_p = fsm_p->pdv;
	char buf[21];
	struct session *sp;
	int wait_code = 0;

	/* Allocate a session control block */
	if((sp = newsession("PPP/PAP",PPPPASS,1)) == NULL){
		printf("Too many sessions\n");
		return;
	}

	while ( !main_exit && wait_code == 0 ) {
		/* get user name */
		if (pap_p->username == NULL) {
			printf ("%s: PPP/PAP  Username: ", iface->name);
			fflush(sp->output);
			if (fgets(buf,20,sp->input) != NULL) {
				rip(buf);
				if (strlen(buf) > 0) {
					pap_p->username = strdup(buf);
				}
			}
		} else {
			printf ("%s: PPP/PAP  Username: %s\n",
				iface->name, pap_p->username);
			fflush(sp->output);
		}

		/* get pass word */
		if (pap_p->username != NULL
		 && pap_p->password == NULL) {
			/* turn off echo */
			sp->ttystate.echo = 0;
			printf("%s: PPP/PAP  Password: ",iface->name);
			fflush(sp->output);
			if (fgets(buf,20,sp->input) != NULL) {
				rip(buf);
				if ( strlen(buf) > 0 ) {
					pap_p->password = strdup(buf);
				}
			}
			printf("\n");
			fflush(sp->output);
			/* Turn echo back on */
			sp->ttystate.echo = 1;
		}

		/* send pap request */
		fsm_sendreq(fsm_p);
		wait_code = kwait ( pap_p );

		/* show ack/nak reply */
		if ( wait_code != EABORT && pap_p->message != NULL ) {
			printf ("%s: PPP/PAP  %s\n",
				iface->name, pap_p->message );
		}
		printf ( "\n" );
		fflush(sp->output);

	}

	/* clean up */
	if ( wait_code != EABORT ) {
		ppause ( 10000L );
	}
	freesession(sp);
	pap_p->pp = NULL;
}


/* Check the FTP userfile for this user; get password if available */
static void
pap_pwdlookup(pap_p)
struct pap_s *pap_p;
{
	char *buf;
	char *password;
	int permission;

	if ( pap_p->username == NULL )
		return;

	if ( (buf = userlookup( pap_p->username, &password, NULL,
			&permission, NULL )) == NULL )
		return;

	/* Check permissions for this user */
	if ( (permission & PPP_PWD_LOOKUP) == 0 ) {
		/* Not in ftpuser file for password lookup */
		free(buf);
		return;
	}

	/* Save the password from this userfile record */
	if ( strlen(password) != 0 )
		pap_p->password = strdup(password);
	free(buf);
}


/*******************************************/
/* Verify user and password sent by remote host */
static int
pap_verify(username,password)
char *username;
char *password;
{
	int privs;
	char *path;
	int anony = 0;

	/* Use same login as FTP server */
	path = mallocw(128);
	privs = userlogin(username,password,&path,128,&anony);
	free(path);

	/* Check privs for this user */
	if (privs == -1) {
		trace_log(PPPiface,"PAP: username/password incorrect or not found: %s",
				username);
		return -1;
	}

	if ((privs & PPP_ACCESS_PRIV) == 0) {
		trace_log(PPPiface,"PAP: no permission for PPP access: %s",
				username);
		return -1;
	}
	return 0;
}


/****************************************************************************/
/* Build a request to send to remote host */
static struct mbuf *
pap_makereq(fsm_p)
struct fsm_s *fsm_p;
{
	struct pap_s *pap_p = fsm_p->pdv;
	struct mbuf *req_bp = NULL;
	register uint8 *cp;
	int len;

	PPP_DEBUG_ROUTINES("pap_makereq()");

	if ( pap_p->username == NULL
	 ||  pap_p->password == NULL ) {
		fsm_log( fsm_p, "NULL username or password" );
		return NULL;
	}

#ifdef PPP_DEBUG_OPTIONS
	if (PPPtrace & PPP_DEBUG_OPTIONS)
		trace_log(PPPiface, "    making user id %s", pap_p->username);
#endif

	/* Get buffer for authenticate request packet */
	len = 2 + strlen(pap_p->username) + strlen(pap_p->password);
	if ((req_bp = alloc_mbuf(len)) == NULL)
		return NULL;

	/* Load user id and password for authenticate packet */
	cp = req_bp->data;
	*cp++ = (uint8)strlen(pap_p->username);
	if ( strlen(pap_p->username) > 0 )
		cp = (uint8 *)stpcpy((char *)cp, pap_p->username);

	*cp++ = (char)strlen(pap_p->password);
	if ( strlen(pap_p->password) > 0 )
		cp = (uint8 *)stpcpy((char *)cp, pap_p->password);

	req_bp->cnt += len;
	return(req_bp);
}


/****************************************************************************/

/* abandon PAP attempt; shutdown LCP layer */
static void
pap_shutdown(fsm_p)
struct fsm_s *fsm_p;
{
	struct ppp_s *ppp_p = fsm_p->ppp_p;

	PPP_DEBUG_ROUTINES("pap_shutdown()");

	if (PPPtrace > 1)
		fsm_log( fsm_p, "Failed; close connection" );

	fsm_close( &(ppp_p->fsm[Lcp]) );
}


/* Configuration negotiation complete */
static void
pap_opening(fsm_p, flag)
struct fsm_s *fsm_p;
int flag;
{
	register struct ppp_s *ppp_p = fsm_p->ppp_p;

	fsm_log(fsm_p, "Open");

	stop_timer(&(fsm_p->timer));

	if ( !((fsm_p->flags &= ~flag) & (PPP_AP_LOCAL | PPP_AP_REMOTE)) ) {
		fsm_p->state = fsmOPENED;
	}
	ppp_p->flags &= ~flag;
	ppp_ready(ppp_p);
}


/****************************************************************************/
/* Check request from remote host */
static int
pap_request(
struct fsm_s *fsm_p,
struct config_hdr *hdr,
struct mbuf **data
){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -