home *** CD-ROM | disk | FTP | other *** search
- /*
- * pap.c -- Password Authentication Protocol for PPP
- *
- * Copyright 1991-1992 William Allen Simpson
- * Licensed for non-profit non-commercial distribution
- * in Merit's MacPPP LAP for MacTCP
- *
- * Copyright 1992-1993 Merit Network, Inc. and The Regents of the
- * University of Michigan. Usage of this source code is restricted
- * to non-profit, non-commercial purposes. The source is provided
- * "as-is", without warranty.
- *
- * This code has been derived solely from the Jan 1991 public
- * domain release of PPP included in KA9Q.
- */
-
- #include "ppp.h"
-
- void pap_sendreq (struct fsm_s *);
- void pap_shutdown(struct fsm_s *);
- short pap_check(struct fsm_s *,struct config_hdr *,struct bufheader *);
- void pap_timeout(void);
-
- #define USERNAME 3
- #define PASSWORD 5
-
- pascal Boolean
- passfilt(DialogPtr dlog, EventRecord *evt, int *itemhit)
- {
- LapInfo *lap;
- short itemType;
- Handle itemHdl;
- Rect itemRect;
- short c,i;
- b_8 *charptr;
- b_8 s[32];
-
- lap = GetLAPPtr();
- charptr = lap->pap_i.password;
- c = evt->message & charCodeMask;
-
- if ( ((DialogPeek) dlog)->editField + 1 == PASSWORD ) {
- switch(evt->what) {
- case keyDown:
- case autoKey:
- if (c > 31) {
- if (*charptr < 31)
- lap->pap_i.password[++(*charptr)] = c;
- *itemhit = 0;
- } else if (c == RETURN || c == ENTER)
- *itemhit = OK;
- else if (c == TAB)
- *itemhit = USERNAME;
- else if (c == BACKSPACE) {
- if (*charptr > 0)
- (*charptr)--;
- *itemhit = 0;
- }
- GetDItem(dlog, PASSWORD, &itemType, &itemHdl, &itemRect);
- s[0] = *charptr;
- for (i = 1; i <= *charptr; i++)
- s[i] = 'Ñ';
- --i;
- SetIText(itemHdl, s);
- SelIText(dlog, PASSWORD, i, i);
- return(true);
- break;
- }
- } else if ( ((DialogPeek) dlog)->editField + 1 == USERNAME ) {
- switch(evt->what) {
- case keyDown:
- case autoKey:
- if (c == RETURN || c == TAB || c == ENTER) {
- *itemhit = PASSWORD;
- SelIText(dlog, PASSWORD, 0, 32767);
- return(true);
- }
- break;
- }
- }
-
- *itemhit=0;
- return(false);
- }
-
- OSErr
- pap_userio(struct fsm_s *fsm_p)
- {
- DialogPtr papdlog;
- short itemhit, type, lenid, lenpw, i;
- b_8 s[256];
- Handle itemH;
- Rect box;
- b_8 *ptr;
- struct pap_s *pap_p = fsm_p->pdv;
-
- if (pap_p->IOFlag)
- {
- papdlog = GetNewDialog(128, 0L, (WindowPtr) -1L);
- DrawDialog(papdlog);
- GetDItem(papdlog, USERNAME, &type, &itemH, &box);
- ptr = pap_p->username; /* check for defaultid */
- if ( *ptr != 0 ) {
- SetIText(itemH, ptr); /* set id field */
- SelIText(papdlog, PASSWORD, 0, 0);
- }
- do
- ModalDialog( (ProcPtr) passfilt, &itemhit);
- while (itemhit != OK && itemhit != Cancel);
-
- GetIText(itemH, s);
- DisposDialog(papdlog);
-
- if (itemhit == Cancel) {
- pap_shutdown(fsm_p);
- return -1;
- }
-
- lenid = s[0] + 1;
- if (lenid > PAPUSERLEN)
- lenid = PAPUSERLEN;
- BlockMove(s, ptr, (long) lenid);
-
- pap_p->IOFlag = FALSE;
- pap_sendreq(fsm_p);
- return noErr;
- }
- }
-
- /******************************************************************/
- /*
- * Send a PAP config request after ensure user and password are given
- */
-
- void pap_sendreq(struct fsm_s *fsm_p )
- {
- if ( !( (struct pap_s * )fsm_p->pdv)->IOFlag )
- fsm_sendreq(fsm_p);
- }
-
- /****************************************************************************/
- /* abandon PAP attempt; shutdown LCP layer */
- static void
- pap_shutdown(fsm_p)
- struct fsm_s *fsm_p;
- {
- struct pap_s *pap_p = fsm_p->pdv;
-
- bzero(pap_p->password, PAPPASSLEN); /* clear password */
- stop_timer(&(fsm_p->timer));
- fsm_close( &(fsm_p->lap->ppp_fsm[Lcp]) );
- }
-
- /* Check acknowledgement from remote host */
- static short
- pap_check(fsm_p, hdr, buffer)
- struct fsm_s *fsm_p;
- struct config_hdr *hdr;
- struct bufheader *buffer;
- {
- b_8 *mess = ((struct pap_s *)(fsm_p->pdv))->message;
- register short mess_length;
-
- if (hdr->id != fsm_p->lastid)
- goto bad_return;
-
- if ( (mess_length = yankbyte(buffer)) == -1 )
- goto bad_return;
-
- if (mess_length >= PAPMESSLEN) /* limit message to our buffer size */
- mess_length = PAPMESSLEN - 1;
- *mess++ = (unsigned char) mess_length;
- yankbuf(buffer, mess, mess_length);
- release(buffer);
- return 0;
-
- bad_return:
- release(buffer);
- return -1;
- }
-
- /************************************************************************/
- /* E V E N T P R O C E S S I N G */
- /************************************************************************/
-
- /* Process incoming packet */
- void
- pap_proc(fsm_p, buffer)
- struct fsm_s *fsm_p;
- struct bufheader *buffer;
- {
- struct pap_s *pap_p = fsm_p->pdv;
- struct config_hdr hdr;
-
- if ( ntohcnf(&hdr, buffer) == -1 ) {
- PPP_DEBUG_CHECKS("\pshort authentication packet");
- return;
- }
-
- hdr.len -= CONFIG_HDR_LEN; /* Length includes envelope */
-
- switch(hdr.code) {
-
- case CONFIG_ACK:
- if (pap_check(fsm_p, &hdr, buffer) == 0) {
- bzero(pap_p->password, PAPPASSLEN); /* clear password */
- stop_timer(&(fsm_p->timer));
- fsm_p->state = fsmOPENED;
- fsm_p->lap->ppp_flags &= ~PPP_AP_REMOTE;
- ppp_ready(fsm_p->lap);
- }
- break;
-
- case CONFIG_NAK:
- if (pap_check(fsm_p, &hdr, buffer) == 0) {
- stop_timer(&(fsm_p->timer));
- bzero(pap_p->password, PAPPASSLEN); /* clear password */
-
- /* Must have sent a bad username or password */
- pap_p->IOFlag = true;
- }
- break;
-
- default:
- release(buffer);
- break;
- }
- }
-
- /* Timeout while waiting for reply from remote host */
- void
- pap_timeout(void)
- {
- struct TMtimer *timerp;
- struct fsm_s *fsm_p;
- struct pap_s *pap_p;
-
- asm {
- move.l a1,timerp
- }
-
- fsm_p = timerp->fsm_p;
- pap_p = fsm_p->pdv;
-
- if (fsm_p->retry > 0) {
- pap_sendreq ( fsm_p);
- fsm_p->retry--;
- } else { /* retry request exceeded */
- pap_shutdown(fsm_p);
- }
- }
-
- void
- pap_down(fsm_p)
- struct fsm_s *fsm_p;
- {
- if ( fsm_p->pdv == NULL )
- return;
-
- fsm_p->lap->ppp_flags &= ~PPP_AP_REMOTE;
-
- switch ( fsm_p->state ) {
- case fsmREQ_Sent:
- stop_timer(&(fsm_p->timer));
- /* fallthru */
- case fsmOPENED:
- fsm_p->state = fsmCLOSED;
- break;
-
- case fsmCLOSED:
- /* Already closed; nothing to do */
- break;
- };
- }
-
- /* Initialize state machine for remote authentication */
- short
- pap_remote(LapInfo *lap)
- {
- struct fsm_s *fsm_p = &(lap->ppp_fsm[Pap]);
- struct pap_s *pap_p = fsm_p->pdv;
- register b_8 len;
-
- fsm_p->retry = lap->configdata.pap_retries;
- fsm_p->pdc.timeout = lap->configdata.pap_timeout;
- /* Initialize timer */
- set_timer(&(fsm_p->timer), fsm_p, (voidProcPtr) pap_timeout);
- pap_p->IOFlag = true;
- pap_p->username[0] = 0;
- pap_p->password[0] = 0;
- if ( *lap->configdata.defaultid != 0) {
- AppendStr(pap_p->username, lap->configdata.defaultid);
- if ( *lap->configdata.defaultpw != 0 ){
- AppendStr(pap_p->password, lap->configdata.defaultpw);
- pap_p->IOFlag = false;
- }
- }
- fsm_p->state = fsmREQ_Sent;
- lap->ppp_flags |= PPP_AP_REMOTE;
-
- pap_sendreq (fsm_p);
- return 0;
- }
-