home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / ip / ppp / mac / macppp2.0.1-src.hqx / MacPPP 2.0.1 src / pap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-20  |  6.6 KB  |  304 lines

  1. /*
  2.  *  pap.c    -- Password Authentication Protocol for PPP
  3.  *
  4.  * Copyright 1991-1992 William Allen Simpson
  5.  *  Licensed for non-profit non-commercial distribution
  6.  *  in Merit's MacPPP LAP for MacTCP
  7.  *
  8.  * Copyright 1992-1993 Merit Network, Inc. and The Regents of the
  9.  *  University of Michigan.  Usage of this source code is restricted
  10.  *  to non-profit, non-commercial purposes.  The source is provided
  11.  *  "as-is", without warranty.
  12.  *
  13.  * This code has been derived solely from the Jan 1991 public
  14.  *  domain release of PPP included in KA9Q.
  15.  */
  16.  
  17. #include "ppp.h"
  18.  
  19. void pap_sendreq (struct fsm_s *);
  20. void pap_shutdown(struct fsm_s *);
  21. short pap_check(struct fsm_s *,struct config_hdr *,struct bufheader *);
  22. void pap_timeout(void);
  23.  
  24. #define USERNAME 3
  25. #define PASSWORD 5
  26.  
  27. pascal Boolean
  28. passfilt(DialogPtr dlog, EventRecord *evt, int *itemhit)
  29. {
  30.     LapInfo        *lap;
  31.     short        itemType;
  32.     Handle        itemHdl;
  33.     Rect        itemRect;
  34.     short        c,i;
  35.     b_8            *charptr;
  36.     b_8            s[32];
  37.     
  38.     lap = GetLAPPtr();
  39.     charptr = lap->pap_i.password;
  40.     c = evt->message & charCodeMask;
  41.     
  42.     if ( ((DialogPeek) dlog)->editField + 1 == PASSWORD ) {
  43.       switch(evt->what) {
  44.         case keyDown:
  45.         case autoKey:
  46.             if (c > 31) {
  47.                 if (*charptr < 31)
  48.                     lap->pap_i.password[++(*charptr)] = c;
  49.                 *itemhit = 0;
  50.             } else if (c == RETURN || c == ENTER)
  51.                 *itemhit = OK;
  52.             else if (c == TAB)
  53.                 *itemhit = USERNAME;
  54.             else if (c == BACKSPACE) {
  55.                 if (*charptr > 0)
  56.                     (*charptr)--;
  57.                 *itemhit = 0;
  58.             }
  59.             GetDItem(dlog, PASSWORD, &itemType, &itemHdl, &itemRect);
  60.             s[0] = *charptr;
  61.             for (i = 1; i <= *charptr; i++)
  62.                 s[i] = 'Ñ';
  63.             --i;
  64.             SetIText(itemHdl, s);
  65.             SelIText(dlog, PASSWORD, i, i);
  66.             return(true);
  67.             break;
  68.       }
  69.     } else if ( ((DialogPeek) dlog)->editField + 1 == USERNAME ) {
  70.         switch(evt->what) {
  71.             case keyDown:
  72.             case autoKey:
  73.                 if (c == RETURN || c == TAB || c == ENTER) {
  74.                     *itemhit = PASSWORD;
  75.                     SelIText(dlog, PASSWORD, 0, 32767);
  76.                     return(true);
  77.                 }
  78.                 break;
  79.         }
  80.     }
  81.     
  82.     *itemhit=0;
  83.     return(false);                
  84. }
  85.  
  86. OSErr
  87. pap_userio(struct fsm_s *fsm_p)
  88. {
  89. DialogPtr    papdlog;
  90. short        itemhit, type, lenid, lenpw, i;
  91. b_8        s[256];
  92. Handle    itemH;
  93. Rect    box;
  94. b_8        *ptr;
  95. struct    pap_s *pap_p = fsm_p->pdv;
  96.  
  97.     if (pap_p->IOFlag)
  98.     {        
  99.         papdlog = GetNewDialog(128, 0L, (WindowPtr) -1L);
  100.         DrawDialog(papdlog);
  101.         GetDItem(papdlog, USERNAME, &type, &itemH, &box);
  102.         ptr = pap_p->username;    /* check for defaultid */
  103.         if ( *ptr != 0 ) {
  104.             SetIText(itemH, ptr);        /* set id field */
  105.             SelIText(papdlog, PASSWORD, 0, 0);
  106.         }
  107.         do
  108.             ModalDialog( (ProcPtr) passfilt, &itemhit);
  109.         while (itemhit != OK && itemhit != Cancel);
  110.         
  111.         GetIText(itemH, s);
  112.         DisposDialog(papdlog);
  113.  
  114.         if (itemhit == Cancel) {
  115.             pap_shutdown(fsm_p);
  116.             return -1;
  117.         }
  118.         
  119.         lenid = s[0] + 1;
  120.         if (lenid > PAPUSERLEN)
  121.             lenid = PAPUSERLEN;
  122.         BlockMove(s, ptr, (long) lenid);
  123.  
  124.         pap_p->IOFlag = FALSE;
  125.         pap_sendreq(fsm_p);
  126.         return noErr;
  127.     }        
  128. }
  129.  
  130. /******************************************************************/
  131. /*
  132.  * Send a PAP config request after ensure user and password are given
  133.  */
  134.  
  135. void pap_sendreq(struct fsm_s *fsm_p )
  136. {    
  137.         if ( !( (struct pap_s * )fsm_p->pdv)->IOFlag )
  138.             fsm_sendreq(fsm_p);
  139. }
  140.  
  141. /****************************************************************************/
  142. /* abandon PAP attempt; shutdown LCP layer */
  143. static void
  144. pap_shutdown(fsm_p)
  145. struct fsm_s *fsm_p;
  146. {
  147.     struct    pap_s *pap_p = fsm_p->pdv;
  148.  
  149.     bzero(pap_p->password, PAPPASSLEN);        /* clear password */
  150.     stop_timer(&(fsm_p->timer));
  151.     fsm_close( &(fsm_p->lap->ppp_fsm[Lcp]) );
  152. }
  153.  
  154. /* Check acknowledgement from remote host */
  155. static short
  156. pap_check(fsm_p, hdr, buffer)
  157. struct fsm_s *fsm_p;
  158. struct config_hdr *hdr;
  159. struct bufheader *buffer;
  160. {
  161.     b_8 *mess = ((struct pap_s *)(fsm_p->pdv))->message;
  162.     register short mess_length;
  163.  
  164.     if (hdr->id != fsm_p->lastid)
  165.         goto bad_return;
  166.  
  167.     if ( (mess_length = yankbyte(buffer)) == -1 )
  168.         goto bad_return;
  169.  
  170.     if (mess_length >= PAPMESSLEN)    /* limit message to our buffer size */
  171.         mess_length = PAPMESSLEN - 1;
  172.     *mess++ = (unsigned char) mess_length;
  173.     yankbuf(buffer, mess, mess_length);
  174.     release(buffer);
  175.     return 0;
  176.     
  177. bad_return:
  178.     release(buffer);
  179.     return -1;
  180. }
  181.  
  182. /************************************************************************/
  183. /*            E V E N T   P R O C E S S I N G            */
  184. /************************************************************************/
  185.  
  186. /* Process incoming packet */
  187. void
  188. pap_proc(fsm_p, buffer)
  189. struct fsm_s *fsm_p;
  190. struct bufheader *buffer;
  191. {
  192.     struct pap_s *pap_p = fsm_p->pdv;
  193.     struct config_hdr hdr;
  194.  
  195.     if ( ntohcnf(&hdr, buffer) == -1 ) {
  196.         PPP_DEBUG_CHECKS("\pshort authentication packet");
  197.         return;
  198.     }
  199.  
  200.     hdr.len -= CONFIG_HDR_LEN;        /* Length includes envelope */
  201.  
  202.     switch(hdr.code) {
  203.  
  204.     case CONFIG_ACK:
  205.         if (pap_check(fsm_p, &hdr, buffer) == 0) {
  206.             bzero(pap_p->password, PAPPASSLEN);        /* clear password */
  207.             stop_timer(&(fsm_p->timer));
  208.             fsm_p->state = fsmOPENED;
  209.             fsm_p->lap->ppp_flags &= ~PPP_AP_REMOTE;
  210.             ppp_ready(fsm_p->lap);
  211.         }
  212.         break;
  213.  
  214.     case CONFIG_NAK:
  215.         if (pap_check(fsm_p, &hdr, buffer) == 0) {
  216.             stop_timer(&(fsm_p->timer));
  217.             bzero(pap_p->password, PAPPASSLEN);        /* clear password */
  218.  
  219.             /* Must have sent a bad username or password */
  220.             pap_p->IOFlag = true;
  221.         }
  222.         break;
  223.  
  224.     default:
  225.         release(buffer);
  226.         break;
  227.     }
  228. }
  229.  
  230. /* Timeout while waiting for reply from remote host */
  231. void
  232. pap_timeout(void)
  233. {
  234.     struct TMtimer    *timerp;
  235.     struct fsm_s    *fsm_p;
  236.     struct pap_s    *pap_p;
  237.         
  238.     asm {
  239.             move.l    a1,timerp
  240.     }
  241.     
  242.     fsm_p = timerp->fsm_p;
  243.     pap_p = fsm_p->pdv;
  244.  
  245.     if (fsm_p->retry > 0) {
  246.         pap_sendreq ( fsm_p);
  247.         fsm_p->retry--;
  248.     } else { /* retry request exceeded */
  249.         pap_shutdown(fsm_p);
  250.     }
  251. }
  252.  
  253. void
  254. pap_down(fsm_p)
  255. struct fsm_s *fsm_p;
  256. {
  257.     if ( fsm_p->pdv == NULL )
  258.         return;
  259.  
  260.     fsm_p->lap->ppp_flags &= ~PPP_AP_REMOTE;
  261.  
  262.     switch ( fsm_p->state ) {
  263.     case fsmREQ_Sent:
  264.         stop_timer(&(fsm_p->timer));
  265.         /* fallthru */
  266.     case fsmOPENED:
  267.         fsm_p->state = fsmCLOSED;
  268.         break;
  269.  
  270.     case fsmCLOSED:
  271.         /* Already closed; nothing to do */
  272.         break;
  273.     };
  274. }
  275.  
  276. /* Initialize state machine for remote authentication */
  277. short
  278. pap_remote(LapInfo *lap)
  279. {
  280.     struct fsm_s *fsm_p = &(lap->ppp_fsm[Pap]);
  281.     struct pap_s *pap_p = fsm_p->pdv;
  282.     register    b_8    len;
  283.  
  284.     fsm_p->retry = lap->configdata.pap_retries;
  285.     fsm_p->pdc.timeout = lap->configdata.pap_timeout;
  286.     /* Initialize timer */
  287.     set_timer(&(fsm_p->timer), fsm_p, (voidProcPtr) pap_timeout);
  288.     pap_p->IOFlag = true;
  289.     pap_p->username[0] = 0;
  290.     pap_p->password[0] = 0;
  291.     if ( *lap->configdata.defaultid != 0) {
  292.         AppendStr(pap_p->username, lap->configdata.defaultid);
  293.         if ( *lap->configdata.defaultpw != 0 ){
  294.             AppendStr(pap_p->password, lap->configdata.defaultpw);
  295.             pap_p->IOFlag = false;
  296.         }
  297.     }
  298.     fsm_p->state = fsmREQ_Sent;
  299.     lap->ppp_flags |= PPP_AP_REMOTE;
  300.  
  301.     pap_sendreq (fsm_p);
  302.     return 0;
  303. }
  304.