]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/chap.c
support micro$oft DNS options;
[ppp.git] / pppd / chap.c
index e0ea4afeee00e569bb28879fc4f32909043ae20d..d72c36d4a9896e964e97ddd13c6e8a72e0b3baad 100644 (file)
@@ -19,7 +19,7 @@
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: chap.c,v 1.1 1993/11/11 03:54:25 paulus Exp $";
+static char rcsid[] = "$Id: chap.c,v 1.8 1995/07/04 12:32:26 paulus Exp $";
 #endif
 
 /*
@@ -27,30 +27,30 @@ static char rcsid[] = "$Id: chap.c,v 1.1 1993/11/11 03:54:25 paulus Exp $";
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <syslog.h>
 
-#include "ppp.h"
 #include "pppd.h"
 #include "chap.h"
 #include "md5.h"
 
-chap_state chap[NPPP];         /* CHAP state; one for each unit */
+chap_state chap[NUM_PPP];              /* CHAP state; one for each unit */
 
-static void ChapChallengeTimeout __ARGS((caddr_t));
-static void ChapResponseTimeout __ARGS((caddr_t));
-static void ChapReceiveChallenge __ARGS((chap_state *, u_char *, int, int));
-static void ChapReceiveResponse __ARGS((chap_state *, u_char *, int, int));
-static void ChapReceiveSuccess __ARGS((chap_state *, u_char *, int, int));
-static void ChapReceiveFailure __ARGS((chap_state *, u_char *, int, int));
-static void ChapSendStatus __ARGS((chap_state *, int));
-static void ChapSendChallenge __ARGS((chap_state *));
-static void ChapSendResponse __ARGS((chap_state *));
-static void ChapGenChallenge __ARGS((chap_state *));
+static void ChapChallengeTimeout __P((caddr_t));
+static void ChapResponseTimeout __P((caddr_t));
+static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
+static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
+static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
+static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
+static void ChapSendStatus __P((chap_state *, int));
+static void ChapSendChallenge __P((chap_state *));
+static void ChapSendResponse __P((chap_state *));
+static void ChapGenChallenge __P((chap_state *));
 
-extern double drand48 __ARGS((void));
-extern void srand48 __ARGS((long));
+extern double drand48 __P((void));
+extern void srand48 __P((long));
 
 /*
  * ChapInit - Initialize a CHAP unit.
@@ -67,7 +67,7 @@ ChapInit(unit)
     cstate->serverstate = CHAPSS_INITIAL;
     cstate->timeouttime = CHAP_DEFTIMEOUT;
     cstate->max_transmits = CHAP_DEFTRANSMITS;
-    srand48((long) time(NULL));        /* joggle random number generator */
+    /* random number generator is initialized in magic_init */
 }
 
 
@@ -148,7 +148,7 @@ ChapChallengeTimeout(arg)
        /* give up on peer */
        syslog(LOG_ERR, "Peer failed to respond to CHAP challenge");
        cstate->serverstate = CHAPSS_BADAUTH;
-       auth_peer_fail(cstate->unit, CHAP);
+       auth_peer_fail(cstate->unit, PPP_CHAP);
        return;
     }
 
@@ -189,9 +189,6 @@ ChapRechallenge(arg)
     ChapGenChallenge(cstate);
     ChapSendChallenge(cstate);
     cstate->serverstate = CHAPSS_RECHALLENGE;
-
-    if (cstate->chal_interval != 0)
-       TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
 }
 
 
@@ -258,10 +255,10 @@ ChapProtocolReject(unit)
 
     if (cstate->serverstate != CHAPSS_INITIAL &&
        cstate->serverstate != CHAPSS_CLOSED)
-       auth_peer_fail(unit, CHAP);
+       auth_peer_fail(unit, PPP_CHAP);
     if (cstate->clientstate != CHAPCS_INITIAL &&
        cstate->clientstate != CHAPCS_CLOSED)
-       auth_withpeer_fail(unit, CHAP);
+       auth_withpeer_fail(unit, PPP_CHAP);
     ChapLowerDown(unit);               /* shutdown chap */
 }
 
@@ -506,7 +503,7 @@ ChapReceiveResponse(cstate, inp, id, len)
            MD5Final(&mdContext); 
 
            /* compare local and remote MDs and send the appropriate status */
-           if (bcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
+           if (memcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
                code = CHAP_SUCCESS;    /* they are the same! */
            break;
 
@@ -521,7 +518,7 @@ ChapReceiveResponse(cstate, inp, id, len)
        old_state = cstate->serverstate;
        cstate->serverstate = CHAPSS_OPEN;
        if (old_state == CHAPSS_INITIAL_CHAL) {
-           auth_peer_success(cstate->unit, CHAP);
+           auth_peer_success(cstate->unit, PPP_CHAP);
        }
        if (cstate->chal_interval != 0)
            TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
@@ -529,7 +526,7 @@ ChapReceiveResponse(cstate, inp, id, len)
     } else {
        syslog(LOG_ERR, "CHAP peer authentication failed");
        cstate->serverstate = CHAPSS_BADAUTH;
-       auth_peer_fail(cstate->unit, CHAP);
+       auth_peer_fail(cstate->unit, PPP_CHAP);
     }
 }
 
@@ -557,6 +554,8 @@ ChapReceiveSuccess(cstate, inp, id, len)
        return;
     }
 
+    UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
+
     /*
      * Print message.
      */
@@ -565,7 +564,7 @@ ChapReceiveSuccess(cstate, inp, id, len)
 
     cstate->clientstate = CHAPCS_OPEN;
 
-    auth_withpeer_success(cstate->unit, CHAP);
+    auth_withpeer_success(cstate->unit, PPP_CHAP);
 }
 
 
@@ -591,6 +590,8 @@ ChapReceiveFailure(cstate, inp, id, len)
        return;
     }
 
+    UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
+
     /*
      * Print message.
      */
@@ -598,7 +599,7 @@ ChapReceiveFailure(cstate, inp, id, len)
        PRINTMSG(inp, len);
 
     syslog(LOG_ERR, "CHAP authentication failed");
-    auth_withpeer_fail(cstate->unit, CHAP);
+    auth_withpeer_fail(cstate->unit, PPP_CHAP);
 }
 
 
@@ -618,7 +619,7 @@ ChapSendChallenge(cstate)
     outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
     outp = outpacket_buf;
 
-    MAKEHEADER(outp, CHAP);            /* paste in a CHAP header */
+    MAKEHEADER(outp, PPP_CHAP);                /* paste in a CHAP header */
 
     PUTCHAR(CHAP_CHALLENGE, outp);
     PUTCHAR(cstate->chal_id, outp);
@@ -630,7 +631,7 @@ ChapSendChallenge(cstate)
 
     BCOPY(cstate->chal_name, outp, name_len);  /* append hostname */
 
-    output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
+    output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
   
     CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
 
@@ -660,13 +661,13 @@ ChapSendStatus(cstate, code)
     outlen = CHAP_HEADERLEN + msglen;
     outp = outpacket_buf;
 
-    MAKEHEADER(outp, CHAP);    /* paste in a header */
+    MAKEHEADER(outp, PPP_CHAP);        /* paste in a header */
   
     PUTCHAR(code, outp);
     PUTCHAR(cstate->chal_id, outp);
     PUTSHORT(outlen, outp);
     BCOPY(msg, outp, msglen);
-    output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
+    output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
   
     CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
               cstate->chal_id));
@@ -718,7 +719,7 @@ ChapSendResponse(cstate)
     outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
     outp = outpacket_buf;
 
-    MAKEHEADER(outp, CHAP);
+    MAKEHEADER(outp, PPP_CHAP);
 
     PUTCHAR(CHAP_RESPONSE, outp);      /* we are a response */
     PUTCHAR(cstate->resp_id, outp);    /* copy id from challenge packet */
@@ -731,24 +732,74 @@ ChapSendResponse(cstate)
     BCOPY(cstate->resp_name, outp, name_len); /* append our name */
 
     /* send the packet */
-    output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
+    output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
 
     cstate->clientstate = CHAPCS_RESPONSE;
     TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
     ++cstate->resp_transmits;
 }
 
-#ifdef NO_DRAND48
-
-double drand48()
+/*
+ * ChapPrintPkt - print the contents of a CHAP packet.
+ */
+char *ChapCodenames[] = {
+    "Challenge", "Response", "Success", "Failure"
+};
+
+int
+ChapPrintPkt(p, plen, printer, arg)
+    u_char *p;
+    int plen;
+    void (*printer) __P((void *, char *, ...));
+    void *arg;
 {
-  return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
-}
+    int code, id, len;
+    int clen, nlen;
+    u_char x;
+
+    if (plen < CHAP_HEADERLEN)
+       return 0;
+    GETCHAR(code, p);
+    GETCHAR(id, p);
+    GETSHORT(len, p);
+    if (len < CHAP_HEADERLEN || len > plen)
+       return 0;
+
+    if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
+       printer(arg, " %s", ChapCodenames[code-1]);
+    else
+       printer(arg, " code=0x%x", code);
+    printer(arg, " id=0x%x", id);
+    len -= CHAP_HEADERLEN;
+    switch (code) {
+    case CHAP_CHALLENGE:
+    case CHAP_RESPONSE:
+       if (len < 1)
+           break;
+       clen = p[0];
+       if (len < clen + 1)
+           break;
+       ++p;
+       nlen = len - clen - 1;
+       printer(arg, " <");
+       for (; clen > 0; --clen) {
+           GETCHAR(x, p);
+           printer(arg, "%.2x", x);
+       }
+       printer(arg, ">, name = ");
+       print_string((char *)p, nlen, printer, arg);
+       break;
+    case CHAP_FAILURE:
+    case CHAP_SUCCESS:
+       printer(arg, " ");
+       print_string((char *)p, len, printer, arg);
+       break;
+    default:
+       for (clen = len; clen > 0; --clen) {
+           GETCHAR(x, p);
+           printer(arg, " %.2x", x);
+       }
+    }
 
-void srand48(seedval)
-long seedval;
-{
-  srand((int)seedval);
+    return len + CHAP_HEADERLEN;
 }
-
-#endif