17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * chap.c - Challenge Handshake Authentication Protocol.
37c478bd9Sstevel@tonic-gate  *
4f53eecf5SJames Carlson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
5f53eecf5SJames Carlson  * Use is subject to license terms.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1993 The Australian National University.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the Australian National University.  The name of the University
167c478bd9Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
177c478bd9Sstevel@tonic-gate  * software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
197c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
207c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * Copyright (c) 1991 Gregory M. Christy.
237c478bd9Sstevel@tonic-gate  * All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
267c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
277c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
287c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
297c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
307c478bd9Sstevel@tonic-gate  * by Gregory M. Christy.  The name of the author may not be used to
317c478bd9Sstevel@tonic-gate  * endorse or promote products derived from this software without
327c478bd9Sstevel@tonic-gate  * specific prior written permission.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
357c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
367c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/time.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "pppd.h"
467c478bd9Sstevel@tonic-gate #include "chap.h"
477c478bd9Sstevel@tonic-gate #include "md5.h"
487c478bd9Sstevel@tonic-gate #if defined(CHAPMS) || defined(CHAPMSV2)
497c478bd9Sstevel@tonic-gate #include "chap_ms.h"
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Command-line options.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate static option_t chap_option_list[] = {
567c478bd9Sstevel@tonic-gate     { "chap-restart", o_int, &chap[0].timeouttime,
577c478bd9Sstevel@tonic-gate       "Set CHAP Challenge retry interval" },
587c478bd9Sstevel@tonic-gate     { "chap-max-challenge", o_int, &chap[0].max_transmits,
597c478bd9Sstevel@tonic-gate       "Max number of Challenges sent without a valid response" },
607c478bd9Sstevel@tonic-gate     { "chap-interval", o_int, &chap[0].chal_interval,
617c478bd9Sstevel@tonic-gate       "Set interval for rechallenge" },
627c478bd9Sstevel@tonic-gate #if defined(CHAPMS) && defined(MSLANMAN)
637c478bd9Sstevel@tonic-gate     { "ms-lanman", o_bool, &ms_lanman,
647c478bd9Sstevel@tonic-gate       "Use obsolete LAN Manager password when using MS-CHAP", 1 },
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate     { NULL }
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * Protocol entry points.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate static void ChapInit __P((int));
737c478bd9Sstevel@tonic-gate static void ChapLowerUp __P((int));
747c478bd9Sstevel@tonic-gate static void ChapLowerDown __P((int));
757c478bd9Sstevel@tonic-gate static void ChapInput __P((int, u_char *, int));
767c478bd9Sstevel@tonic-gate static void ChapProtocolReject __P((int));
777c478bd9Sstevel@tonic-gate static int  ChapPrintPkt __P((u_char *, int,
787c478bd9Sstevel@tonic-gate     void (*) __P((void *, const char *, ...)), void *));
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate struct protent chap_protent = {
817c478bd9Sstevel@tonic-gate     PPP_CHAP,			/* PPP protocol number */
82*55fea89dSDan Cross     ChapInit,			/* Initialization procedure */
83*55fea89dSDan Cross     ChapInput,			/* Process a received packet */
84*55fea89dSDan Cross     ChapProtocolReject,		/* Process a received protocol-reject */
85*55fea89dSDan Cross     ChapLowerUp,		/* Lower layer has come up */
86*55fea89dSDan Cross     ChapLowerDown,		/* Lower layer has gone down */
87*55fea89dSDan Cross     NULL,			/* Open the protocol */
88*55fea89dSDan Cross     NULL,			/* Close the protocol */
89*55fea89dSDan Cross     ChapPrintPkt,		/* Print a packet in readable form */
90*55fea89dSDan Cross     NULL,			/* Process a received data packet */
91*55fea89dSDan Cross     1,				/* 0 iff protocol is disabled */
92*55fea89dSDan Cross     "CHAP",			/* Text name of protocol */
93*55fea89dSDan Cross     NULL,			/* Text name of corresponding data protocol */
94*55fea89dSDan Cross     chap_option_list,		/* List of command-line options */
95*55fea89dSDan Cross     NULL,			/* Check requested options, assign defaults */
96*55fea89dSDan Cross     NULL,			/* Configure interface for demand-dial */
977c478bd9Sstevel@tonic-gate     NULL			/* Say whether to bring up link for this pkt */
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /* Not static'd for plug-ins */
1017c478bd9Sstevel@tonic-gate chap_state chap[NUM_PPP];		/* CHAP state; one for each unit */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static void ChapChallengeTimeout __P((void *));
1047c478bd9Sstevel@tonic-gate static void ChapResponseTimeout __P((void *));
1057c478bd9Sstevel@tonic-gate static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
1067c478bd9Sstevel@tonic-gate static void ChapRechallenge __P((void *));
1077c478bd9Sstevel@tonic-gate static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
1087c478bd9Sstevel@tonic-gate static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
1097c478bd9Sstevel@tonic-gate static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
1107c478bd9Sstevel@tonic-gate static void ChapSendStatus __P((chap_state *, int));
1117c478bd9Sstevel@tonic-gate static void ChapSendChallenge __P((chap_state *));
1127c478bd9Sstevel@tonic-gate static void ChapSendResponse __P((chap_state *));
1137c478bd9Sstevel@tonic-gate static void ChapGenChallenge __P((chap_state *));
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static const char *
chap_cstate(int clientstate)1167c478bd9Sstevel@tonic-gate chap_cstate(int clientstate)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate     static const char *cstate[] = { CHAPCS__LIST };
1197c478bd9Sstevel@tonic-gate     static char buf[32];
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate     if (clientstate < 0 || clientstate >= Dim(cstate)) {
1227c478bd9Sstevel@tonic-gate 	(void) slprintf(buf, sizeof(buf), "#%d", clientstate);
1237c478bd9Sstevel@tonic-gate 	return (buf);
1247c478bd9Sstevel@tonic-gate     }
1257c478bd9Sstevel@tonic-gate     return cstate[clientstate];
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static const char *
chap_sstate(int serverstate)1297c478bd9Sstevel@tonic-gate chap_sstate(int serverstate)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate     static const char *sstate[] = { CHAPSS__LIST };
1327c478bd9Sstevel@tonic-gate     static char buf[32];
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate     if (serverstate < 0 || serverstate >= Dim(sstate)) {
1357c478bd9Sstevel@tonic-gate 	(void) slprintf(buf, sizeof(buf), "#%d", serverstate);
1367c478bd9Sstevel@tonic-gate 	return (buf);
1377c478bd9Sstevel@tonic-gate     }
1387c478bd9Sstevel@tonic-gate     return sstate[serverstate];
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * ChapInit - Initialize a CHAP unit.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate static void
ChapInit(unit)1457c478bd9Sstevel@tonic-gate ChapInit(unit)
1467c478bd9Sstevel@tonic-gate     int unit;
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate     chap_state *cstate = &chap[unit];
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate     BZERO(cstate, sizeof(*cstate));
1517c478bd9Sstevel@tonic-gate     cstate->unit = unit;
1527c478bd9Sstevel@tonic-gate     cstate->clientstate = CHAPCS_INITIAL;
1537c478bd9Sstevel@tonic-gate     cstate->serverstate = CHAPSS_INITIAL;
1547c478bd9Sstevel@tonic-gate     cstate->timeouttime = CHAP_DEFTIMEOUT;
1557c478bd9Sstevel@tonic-gate     cstate->max_transmits = CHAP_DEFTRANSMITS;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate     /* XXX save get_first_hwaddr() here. */
1587c478bd9Sstevel@tonic-gate     /* random number generator is initialized in magic_init */
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * ChapAuthWithPeer - Authenticate us with our peer (start client).
1647c478bd9Sstevel@tonic-gate  *
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate void
ChapAuthWithPeer(unit,our_name,digest)1677c478bd9Sstevel@tonic-gate ChapAuthWithPeer(unit, our_name, digest)
1687c478bd9Sstevel@tonic-gate     int unit;
1697c478bd9Sstevel@tonic-gate     char *our_name;
1707c478bd9Sstevel@tonic-gate     int digest;
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate     chap_state *cstate = &chap[unit];
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate     cstate->resp_name = our_name;
1757c478bd9Sstevel@tonic-gate     cstate->resp_type = digest;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate     if (cstate->clientstate == CHAPCS_INITIAL ||
1787c478bd9Sstevel@tonic-gate 	cstate->clientstate == CHAPCS_PENDING) {
1797c478bd9Sstevel@tonic-gate 	/* lower layer isn't up - wait until later */
1807c478bd9Sstevel@tonic-gate 	cstate->clientstate = CHAPCS_PENDING;
1817c478bd9Sstevel@tonic-gate 	return;
1827c478bd9Sstevel@tonic-gate     }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate     /*
1857c478bd9Sstevel@tonic-gate      * We get here as a result of LCP coming up.
186*55fea89dSDan Cross      * So even if CHAP was open before, we will
1877c478bd9Sstevel@tonic-gate      * have to re-authenticate ourselves.
1887c478bd9Sstevel@tonic-gate      */
1897c478bd9Sstevel@tonic-gate     cstate->clientstate = CHAPCS_LISTEN;
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * ChapAuthPeer - Authenticate our peer (start server).
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate void
ChapAuthPeer(unit,our_name,digest)1977c478bd9Sstevel@tonic-gate ChapAuthPeer(unit, our_name, digest)
1987c478bd9Sstevel@tonic-gate     int unit;
1997c478bd9Sstevel@tonic-gate     char *our_name;
2007c478bd9Sstevel@tonic-gate     int digest;
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate     chap_state *cstate = &chap[unit];
203*55fea89dSDan Cross 
2047c478bd9Sstevel@tonic-gate     cstate->chal_name = our_name;
2057c478bd9Sstevel@tonic-gate     cstate->chal_type = digest;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate     if (cstate->serverstate == CHAPSS_INITIAL ||
2087c478bd9Sstevel@tonic-gate 	cstate->serverstate == CHAPSS_PENDING) {
2097c478bd9Sstevel@tonic-gate 	/* lower layer isn't up - wait until later */
2107c478bd9Sstevel@tonic-gate 	cstate->serverstate = CHAPSS_PENDING;
2117c478bd9Sstevel@tonic-gate 	return;
2127c478bd9Sstevel@tonic-gate     }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate     ChapGenChallenge(cstate);
2157c478bd9Sstevel@tonic-gate     ChapSendChallenge(cstate);		/* crank it up dude! */
2167c478bd9Sstevel@tonic-gate     cstate->serverstate = CHAPSS_INITIAL_CHAL;
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * ChapChallengeTimeout - Timeout expired on sending challenge.
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate static void
ChapChallengeTimeout(arg)2247c478bd9Sstevel@tonic-gate ChapChallengeTimeout(arg)
2257c478bd9Sstevel@tonic-gate     void *arg;
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate     chap_state *cstate = (chap_state *) arg;
228*55fea89dSDan Cross 
2297c478bd9Sstevel@tonic-gate     /* if we aren't sending challenges, don't worry.  then again we */
2307c478bd9Sstevel@tonic-gate     /* probably shouldn't be here either */
2317c478bd9Sstevel@tonic-gate     if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
2327c478bd9Sstevel@tonic-gate 	cstate->serverstate != CHAPSS_RECHALLENGE)
2337c478bd9Sstevel@tonic-gate 	return;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate     if (cstate->chal_transmits >= cstate->max_transmits) {
2367c478bd9Sstevel@tonic-gate 	/* give up on peer */
2377c478bd9Sstevel@tonic-gate 	error("Peer failed to respond to CHAP challenge");
2387c478bd9Sstevel@tonic-gate 	cstate->serverstate = CHAPSS_BADAUTH;
2397c478bd9Sstevel@tonic-gate 	auth_peer_fail(cstate->unit, PPP_CHAP);
2407c478bd9Sstevel@tonic-gate 	return;
2417c478bd9Sstevel@tonic-gate     }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate     ChapSendChallenge(cstate);		/* Re-send challenge */
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * ChapResponseTimeout - Timeout expired on sending response.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate static void
ChapResponseTimeout(arg)2517c478bd9Sstevel@tonic-gate ChapResponseTimeout(arg)
2527c478bd9Sstevel@tonic-gate     void *arg;
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate     chap_state *cstate = (chap_state *) arg;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate     /* if we aren't sending a response, don't worry. */
2577c478bd9Sstevel@tonic-gate     if (cstate->clientstate != CHAPCS_RESPONSE)
2587c478bd9Sstevel@tonic-gate 	return;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate     ChapSendResponse(cstate);		/* re-send response */
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate  * ChapRechallenge - Time to challenge the peer again.
2667c478bd9Sstevel@tonic-gate  */
2677c478bd9Sstevel@tonic-gate static void
ChapRechallenge(arg)2687c478bd9Sstevel@tonic-gate ChapRechallenge(arg)
2697c478bd9Sstevel@tonic-gate     void *arg;
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate     chap_state *cstate = (chap_state *) arg;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate     /* if we aren't sending a response, don't worry. */
2747c478bd9Sstevel@tonic-gate     if (cstate->serverstate != CHAPSS_OPEN)
2757c478bd9Sstevel@tonic-gate 	return;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate     ChapGenChallenge(cstate);
2787c478bd9Sstevel@tonic-gate     ChapSendChallenge(cstate);
2797c478bd9Sstevel@tonic-gate     cstate->serverstate = CHAPSS_RECHALLENGE;
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * ChapLowerUp - The lower layer is up.
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * Start up if we have pending requests.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate static void
ChapLowerUp(unit)2897c478bd9Sstevel@tonic-gate ChapLowerUp(unit)
2907c478bd9Sstevel@tonic-gate     int unit;
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate     chap_state *cstate = &chap[unit];
293*55fea89dSDan Cross 
2947c478bd9Sstevel@tonic-gate     if (cstate->clientstate == CHAPCS_INITIAL)
2957c478bd9Sstevel@tonic-gate 	cstate->clientstate = CHAPCS_CLOSED;
2967c478bd9Sstevel@tonic-gate     else if (cstate->clientstate == CHAPCS_PENDING)
2977c478bd9Sstevel@tonic-gate 	cstate->clientstate = CHAPCS_LISTEN;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate     if (cstate->serverstate == CHAPSS_INITIAL)
3007c478bd9Sstevel@tonic-gate 	cstate->serverstate = CHAPSS_CLOSED;
3017c478bd9Sstevel@tonic-gate     else if (cstate->serverstate == CHAPSS_PENDING) {
3027c478bd9Sstevel@tonic-gate 	ChapGenChallenge(cstate);
3037c478bd9Sstevel@tonic-gate 	ChapSendChallenge(cstate);
3047c478bd9Sstevel@tonic-gate 	cstate->serverstate = CHAPSS_INITIAL_CHAL;
3057c478bd9Sstevel@tonic-gate     }
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * ChapLowerDown - The lower layer is down.
3117c478bd9Sstevel@tonic-gate  *
3127c478bd9Sstevel@tonic-gate  * Cancel all timeouts.
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate static void
ChapLowerDown(unit)3157c478bd9Sstevel@tonic-gate ChapLowerDown(unit)
3167c478bd9Sstevel@tonic-gate     int unit;
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate     chap_state *cstate = &chap[unit];
319*55fea89dSDan Cross 
3207c478bd9Sstevel@tonic-gate     /* Timeout(s) pending?  Cancel if so. */
3217c478bd9Sstevel@tonic-gate     if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
3227c478bd9Sstevel@tonic-gate 	cstate->serverstate == CHAPSS_RECHALLENGE)
3237c478bd9Sstevel@tonic-gate 	UNTIMEOUT(ChapChallengeTimeout, cstate);
3247c478bd9Sstevel@tonic-gate     else if (cstate->serverstate == CHAPSS_OPEN
3257c478bd9Sstevel@tonic-gate 	     && cstate->chal_interval != 0)
3267c478bd9Sstevel@tonic-gate 	UNTIMEOUT(ChapRechallenge, cstate);
3277c478bd9Sstevel@tonic-gate     if (cstate->clientstate == CHAPCS_RESPONSE)
3287c478bd9Sstevel@tonic-gate 	UNTIMEOUT(ChapResponseTimeout, cstate);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate     cstate->clientstate = CHAPCS_INITIAL;
3317c478bd9Sstevel@tonic-gate     cstate->serverstate = CHAPSS_INITIAL;
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * ChapProtocolReject - Peer doesn't grok CHAP.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate static void
ChapProtocolReject(unit)3397c478bd9Sstevel@tonic-gate ChapProtocolReject(unit)
3407c478bd9Sstevel@tonic-gate     int unit;
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate     chap_state *cstate = &chap[unit];
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate     if (cstate->serverstate != CHAPSS_INITIAL &&
3457c478bd9Sstevel@tonic-gate 	cstate->serverstate != CHAPSS_CLOSED)
3467c478bd9Sstevel@tonic-gate 	auth_peer_fail(unit, PPP_CHAP);
3477c478bd9Sstevel@tonic-gate     if (cstate->clientstate != CHAPCS_INITIAL &&
3487c478bd9Sstevel@tonic-gate 	cstate->clientstate != CHAPCS_CLOSED)
3497c478bd9Sstevel@tonic-gate 	auth_withpeer_fail(unit, PPP_CHAP);
3507c478bd9Sstevel@tonic-gate     ChapLowerDown(unit);		/* shutdown chap */
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  * ChapInput - Input CHAP packet.
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate static void
ChapInput(unit,inpacket,packet_len)3587c478bd9Sstevel@tonic-gate ChapInput(unit, inpacket, packet_len)
3597c478bd9Sstevel@tonic-gate     int unit;
3607c478bd9Sstevel@tonic-gate     u_char *inpacket;
3617c478bd9Sstevel@tonic-gate     int packet_len;
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate     chap_state *cstate = &chap[unit];
3647c478bd9Sstevel@tonic-gate     u_char *inp;
3657c478bd9Sstevel@tonic-gate     u_char code, id;
3667c478bd9Sstevel@tonic-gate     int len;
367*55fea89dSDan Cross 
3687c478bd9Sstevel@tonic-gate     /*
3697c478bd9Sstevel@tonic-gate      * Parse header (code, id and length).
3707c478bd9Sstevel@tonic-gate      * If packet too short, drop it.
3717c478bd9Sstevel@tonic-gate      */
3727c478bd9Sstevel@tonic-gate     inp = inpacket;
3737c478bd9Sstevel@tonic-gate     if (packet_len < CHAP_HEADERLEN) {
3747c478bd9Sstevel@tonic-gate 	error("CHAP: packet is too small (%d < %d)", packet_len,
3757c478bd9Sstevel@tonic-gate 	    CHAP_HEADERLEN);
3767c478bd9Sstevel@tonic-gate 	return;
3777c478bd9Sstevel@tonic-gate     }
3787c478bd9Sstevel@tonic-gate     GETCHAR(code, inp);
3797c478bd9Sstevel@tonic-gate     GETCHAR(id, inp);
3807c478bd9Sstevel@tonic-gate     GETSHORT(len, inp);
3817c478bd9Sstevel@tonic-gate     if (len < CHAP_HEADERLEN || len > packet_len) {
3827c478bd9Sstevel@tonic-gate 	error("CHAP: packet has illegal length %d (%d..%d)",
3837c478bd9Sstevel@tonic-gate 	    len, CHAP_HEADERLEN, packet_len);
3847c478bd9Sstevel@tonic-gate 	return;
3857c478bd9Sstevel@tonic-gate     }
3867c478bd9Sstevel@tonic-gate     len -= CHAP_HEADERLEN;
387*55fea89dSDan Cross 
3887c478bd9Sstevel@tonic-gate     /*
3897c478bd9Sstevel@tonic-gate      * Action depends on code (as in fact it usually does :-).
3907c478bd9Sstevel@tonic-gate      */
3917c478bd9Sstevel@tonic-gate     switch (code) {
3927c478bd9Sstevel@tonic-gate     case CHAP_CHALLENGE:
3937c478bd9Sstevel@tonic-gate 	ChapReceiveChallenge(cstate, inp, id, len);
3947c478bd9Sstevel@tonic-gate 	break;
395*55fea89dSDan Cross 
3967c478bd9Sstevel@tonic-gate     case CHAP_RESPONSE:
3977c478bd9Sstevel@tonic-gate 	ChapReceiveResponse(cstate, inp, id, len);
3987c478bd9Sstevel@tonic-gate 	break;
399*55fea89dSDan Cross 
4007c478bd9Sstevel@tonic-gate     case CHAP_FAILURE:
4017c478bd9Sstevel@tonic-gate 	ChapReceiveFailure(cstate, inp, id, len);
4027c478bd9Sstevel@tonic-gate 	break;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate     case CHAP_SUCCESS:
4057c478bd9Sstevel@tonic-gate 	ChapReceiveSuccess(cstate, inp, id, len);
4067c478bd9Sstevel@tonic-gate 	break;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate     default:
4097c478bd9Sstevel@tonic-gate 	/* CHAP does not define a code reject. */
4107c478bd9Sstevel@tonic-gate 	warn("Unknown CHAP code (%d) received.", code);
4117c478bd9Sstevel@tonic-gate 	break;
4127c478bd9Sstevel@tonic-gate     }
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate  * ChapReceiveChallenge - Receive Challenge and send Response.
4187c478bd9Sstevel@tonic-gate  */
4197c478bd9Sstevel@tonic-gate static void
ChapReceiveChallenge(cstate,inp,id,len)4207c478bd9Sstevel@tonic-gate ChapReceiveChallenge(cstate, inp, id, len)
4217c478bd9Sstevel@tonic-gate     chap_state *cstate;
4227c478bd9Sstevel@tonic-gate     u_char *inp;
4237c478bd9Sstevel@tonic-gate     int id;
4247c478bd9Sstevel@tonic-gate     int len;
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate     int rchallenge_len;
4277c478bd9Sstevel@tonic-gate     u_char *rchallenge;
4287c478bd9Sstevel@tonic-gate     int secret_len;
4297c478bd9Sstevel@tonic-gate     char rhostname[MAXNAMELEN];
4307c478bd9Sstevel@tonic-gate     char secret[MAXSECRETLEN];
4317c478bd9Sstevel@tonic-gate     MD5_CTX mdContext;
4327c478bd9Sstevel@tonic-gate     u_char hash[MD5_SIGNATURE_SIZE];
4337c478bd9Sstevel@tonic-gate     int fake_response = 0;
434*55fea89dSDan Cross 
4357c478bd9Sstevel@tonic-gate     if (cstate->clientstate == CHAPCS_CLOSED ||
4367c478bd9Sstevel@tonic-gate 	cstate->clientstate == CHAPCS_PENDING) {
4377c478bd9Sstevel@tonic-gate 	if (debug)
4387c478bd9Sstevel@tonic-gate 	    dbglog("CHAP Challenge unexpectedly received in state %s",
4397c478bd9Sstevel@tonic-gate 		chap_cstate(cstate->clientstate));
4407c478bd9Sstevel@tonic-gate 	return;
4417c478bd9Sstevel@tonic-gate     }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate     if (len < 2) {
4447c478bd9Sstevel@tonic-gate 	error("CHAP: Challenge message length %d", len);
4457c478bd9Sstevel@tonic-gate 	return;
4467c478bd9Sstevel@tonic-gate     }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate     GETCHAR(rchallenge_len, inp);
4497c478bd9Sstevel@tonic-gate     len -= sizeof (u_char) + rchallenge_len;	/* now name field length */
4507c478bd9Sstevel@tonic-gate     if (len < 0) {
4517c478bd9Sstevel@tonic-gate 	error("CHAP: Challenge truncated");
4527c478bd9Sstevel@tonic-gate 	return;
4537c478bd9Sstevel@tonic-gate     }
4547c478bd9Sstevel@tonic-gate     rchallenge = inp;
4557c478bd9Sstevel@tonic-gate     INCPTR(rchallenge_len, inp);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate     if (len >= sizeof(rhostname))
4587c478bd9Sstevel@tonic-gate 	len = sizeof(rhostname) - 1;
4597c478bd9Sstevel@tonic-gate     if (len > 0) {
4607c478bd9Sstevel@tonic-gate 	BCOPY(inp, rhostname, len);
4617c478bd9Sstevel@tonic-gate     }
4627c478bd9Sstevel@tonic-gate     rhostname[len] = '\0';
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate #ifdef CHECK_CHALLENGE_LENGTH
4657c478bd9Sstevel@tonic-gate     if (rchallenge_len < CHECK_CHALLENGE_LENGTH) {
4667c478bd9Sstevel@tonic-gate 	warn("CHAP: Challenge from %s unreasonably short (%d octets)",
4677c478bd9Sstevel@tonic-gate 	    rhostname, rchallenge_len);
4687c478bd9Sstevel@tonic-gate 	fake_response = 1;
4697c478bd9Sstevel@tonic-gate     }
4707c478bd9Sstevel@tonic-gate #endif
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate     /* XXX compare against saved get_first_hwaddr() here. */
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate     /* Microsoft NT doesn't send a name in the CHAP Challenge message */
4757c478bd9Sstevel@tonic-gate     if (explicit_remote ||
4767c478bd9Sstevel@tonic-gate 	(remote_name[0] != '\0' && rhostname[0] == '\0')) {
4777c478bd9Sstevel@tonic-gate 	(void) strlcpy(rhostname, remote_name, sizeof(rhostname));
4787c478bd9Sstevel@tonic-gate 	if (debug)
4797c478bd9Sstevel@tonic-gate 	    dbglog("CHAP:  Peer gave no name; using '%q' as remote name",
4807c478bd9Sstevel@tonic-gate 		rhostname);
4817c478bd9Sstevel@tonic-gate     }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate     if (cstate->peercname[0] == '\0') {
4847c478bd9Sstevel@tonic-gate 	(void) strlcpy(cstate->peercname, rhostname,
4857c478bd9Sstevel@tonic-gate 	    sizeof (cstate->peercname));
4867c478bd9Sstevel@tonic-gate     } else if (strcmp(rhostname, cstate->peercname) != 0) {
487f53eecf5SJames Carlson 	if (++cstate->rename_count == 1) {
488f53eecf5SJames Carlson 	    info("CHAP: peer challenge name changed from '%q' to '%q'",
489f53eecf5SJames Carlson 		cstate->peercname, rhostname);
490f53eecf5SJames Carlson 	    (void) strlcpy(cstate->peercname, rhostname,
491f53eecf5SJames Carlson 		sizeof (cstate->peercname));
492f53eecf5SJames Carlson 	} else {
493f53eecf5SJames Carlson 	    fake_response = 1;
494f53eecf5SJames Carlson 	    if (cstate->rename_count == 2)
495f53eecf5SJames Carlson 		warn("CHAP: peer challenge name changed again to '%q'",
496f53eecf5SJames Carlson 		    rhostname);
497f53eecf5SJames Carlson 	}
4987c478bd9Sstevel@tonic-gate     }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate     /* get secret for authenticating ourselves with the specified host */
5017c478bd9Sstevel@tonic-gate     if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
5027c478bd9Sstevel@tonic-gate 		    secret, &secret_len, 0)) {
5037c478bd9Sstevel@tonic-gate 	secret_len = 0;		/* assume null secret if can't find one */
5047c478bd9Sstevel@tonic-gate 	warn("No CHAP secret found for authenticating us (%q) to %q",
5057c478bd9Sstevel@tonic-gate 	    cstate->resp_name, rhostname);
5067c478bd9Sstevel@tonic-gate     }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate     /* cancel response send timeout if necessary */
5097c478bd9Sstevel@tonic-gate     if (cstate->clientstate == CHAPCS_RESPONSE)
5107c478bd9Sstevel@tonic-gate 	UNTIMEOUT(ChapResponseTimeout, cstate);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate     cstate->resp_id = id;
5137c478bd9Sstevel@tonic-gate     cstate->resp_transmits = 0;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate     /*  generate MD based on negotiated type */
516*55fea89dSDan Cross     switch (cstate->resp_type) {
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate     case CHAP_DIGEST_MD5:
5197c478bd9Sstevel@tonic-gate 	MD5Init(&mdContext);
5207c478bd9Sstevel@tonic-gate 	MD5Update(&mdContext, &cstate->resp_id, 1);
5217c478bd9Sstevel@tonic-gate 	MD5Update(&mdContext, (u_char *)secret, (unsigned)secret_len);
5227c478bd9Sstevel@tonic-gate 	MD5Update(&mdContext, rchallenge, rchallenge_len);
5237c478bd9Sstevel@tonic-gate 	MD5Final(hash, &mdContext);
5247c478bd9Sstevel@tonic-gate 	if (fake_response) {
5257c478bd9Sstevel@tonic-gate 	    for (len = 0; len < MD5_SIGNATURE_SIZE; len++)
5267c478bd9Sstevel@tonic-gate 		cstate->response[len] = (char) (drand48() * 0xff);
5277c478bd9Sstevel@tonic-gate 	} else {
5287c478bd9Sstevel@tonic-gate 	    BCOPY(hash, cstate->response, MD5_SIGNATURE_SIZE);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	cstate->resp_length = MD5_SIGNATURE_SIZE;
5317c478bd9Sstevel@tonic-gate 	break;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate #ifdef CHAPMS
5347c478bd9Sstevel@tonic-gate     case CHAP_MICROSOFT:
5357c478bd9Sstevel@tonic-gate 	ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
5367c478bd9Sstevel@tonic-gate 	break;
5377c478bd9Sstevel@tonic-gate #endif
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate #ifdef CHAPMSV2
5407c478bd9Sstevel@tonic-gate     case CHAP_MICROSOFT_V2:
5417c478bd9Sstevel@tonic-gate 	ChapMSv2(cstate, rchallenge, rchallenge_len, secret, secret_len);
5427c478bd9Sstevel@tonic-gate 	break;
5437c478bd9Sstevel@tonic-gate #endif
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate     default:
5467c478bd9Sstevel@tonic-gate 	error("CHAP: unknown digest type %d", cstate->resp_type);
5477c478bd9Sstevel@tonic-gate 	return;
5487c478bd9Sstevel@tonic-gate     }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate     BZERO(secret, sizeof(secret));
5517c478bd9Sstevel@tonic-gate     ChapSendResponse(cstate);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * ChapReceiveResponse - Receive and process response.
5577c478bd9Sstevel@tonic-gate  */
5587c478bd9Sstevel@tonic-gate static void
ChapReceiveResponse(cstate,inp,id,len)5597c478bd9Sstevel@tonic-gate ChapReceiveResponse(cstate, inp, id, len)
5607c478bd9Sstevel@tonic-gate     chap_state *cstate;
5617c478bd9Sstevel@tonic-gate     u_char *inp;
5627c478bd9Sstevel@tonic-gate     int id;
5637c478bd9Sstevel@tonic-gate     int len;
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate     u_char *remmd, remmd_len;
5667c478bd9Sstevel@tonic-gate     int secret_len, old_state;
5677c478bd9Sstevel@tonic-gate     int code;
5687c478bd9Sstevel@tonic-gate     char rhostname[MAXNAMELEN], *rhn;
5697c478bd9Sstevel@tonic-gate     MD5_CTX mdContext;
5707c478bd9Sstevel@tonic-gate     char secret[MAXSECRETLEN];
5717c478bd9Sstevel@tonic-gate     u_char hash[MD5_SIGNATURE_SIZE];
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate     if (cstate->serverstate == CHAPSS_CLOSED ||
5747c478bd9Sstevel@tonic-gate 	cstate->serverstate == CHAPSS_PENDING) {
5757c478bd9Sstevel@tonic-gate 	if (debug)
5767c478bd9Sstevel@tonic-gate 	    dbglog("CHAP Response unexpectedly received in state %s",
5777c478bd9Sstevel@tonic-gate 		chap_sstate(cstate->serverstate));
5787c478bd9Sstevel@tonic-gate 	return;
5797c478bd9Sstevel@tonic-gate     }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate     if (id != cstate->chal_id) {
5827c478bd9Sstevel@tonic-gate 	if (debug)
5837c478bd9Sstevel@tonic-gate 	    dbglog("CHAP: discard response %d; expecting %d", id,
5847c478bd9Sstevel@tonic-gate 		cstate->chal_id);
5857c478bd9Sstevel@tonic-gate 	return;			/* doesn't match ID of last challenge */
5867c478bd9Sstevel@tonic-gate     }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate     /*
5897c478bd9Sstevel@tonic-gate      * If we have received a duplicate or bogus Response,
5907c478bd9Sstevel@tonic-gate      * we have to send the same answer (Success/Failure)
5917c478bd9Sstevel@tonic-gate      * as we did for the first Response we saw.
5927c478bd9Sstevel@tonic-gate      */
5937c478bd9Sstevel@tonic-gate     if (cstate->serverstate == CHAPSS_OPEN) {
5947c478bd9Sstevel@tonic-gate 	if (debug)
5957c478bd9Sstevel@tonic-gate 	    dbglog("CHAP ignoring response and resending success message");
5967c478bd9Sstevel@tonic-gate 	ChapSendStatus(cstate, CHAP_SUCCESS);
5977c478bd9Sstevel@tonic-gate 	return;
5987c478bd9Sstevel@tonic-gate     }
5997c478bd9Sstevel@tonic-gate     if (cstate->serverstate == CHAPSS_BADAUTH) {
6007c478bd9Sstevel@tonic-gate 	if (debug)
6017c478bd9Sstevel@tonic-gate 	    dbglog("CHAP ignoring response and resending failure message");
6027c478bd9Sstevel@tonic-gate 	ChapSendStatus(cstate, CHAP_FAILURE);
6037c478bd9Sstevel@tonic-gate 	return;
6047c478bd9Sstevel@tonic-gate     }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate     if (len < 2) {
6077c478bd9Sstevel@tonic-gate 	error("CHAP: Response message length %d", len);
6087c478bd9Sstevel@tonic-gate 	return;
6097c478bd9Sstevel@tonic-gate     }
6107c478bd9Sstevel@tonic-gate     GETCHAR(remmd_len, inp);		/* get length of MD */
6117c478bd9Sstevel@tonic-gate     remmd = inp;			/* get pointer to MD */
6127c478bd9Sstevel@tonic-gate     INCPTR(remmd_len, inp);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate     len -= sizeof (u_char) + remmd_len;
6157c478bd9Sstevel@tonic-gate     if (len < 0) {
6167c478bd9Sstevel@tonic-gate 	error("CHAP: Response truncated");
6177c478bd9Sstevel@tonic-gate 	return;
6187c478bd9Sstevel@tonic-gate     }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate     UNTIMEOUT(ChapChallengeTimeout, cstate);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate     if (len >= sizeof(rhostname))
6237c478bd9Sstevel@tonic-gate 	len = sizeof(rhostname) - 1;
6247c478bd9Sstevel@tonic-gate     BCOPY(inp, rhostname, len);
6257c478bd9Sstevel@tonic-gate     rhostname[len] = '\0';
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate     /*
6287c478bd9Sstevel@tonic-gate      * Get secret for authenticating them with us,
6297c478bd9Sstevel@tonic-gate      * do the hash ourselves, and compare the result.
6307c478bd9Sstevel@tonic-gate      */
6317c478bd9Sstevel@tonic-gate     code = CHAP_FAILURE;
6327c478bd9Sstevel@tonic-gate     rhn = (explicit_remote? remote_name: rhostname);
6337c478bd9Sstevel@tonic-gate     if (cstate->serverstate == CHAPSS_RECHALLENGE &&
6347c478bd9Sstevel@tonic-gate 	strcmp(rhostname, peer_authname) != 0) {
6357c478bd9Sstevel@tonic-gate 	warn("Peer changed his name from '%q' to '%q' on rechallenge",
6367c478bd9Sstevel@tonic-gate 	    peer_authname, rhostname);
6377c478bd9Sstevel@tonic-gate     } else if (!get_secret(cstate->unit, rhn, cstate->chal_name, secret,
6387c478bd9Sstevel@tonic-gate 	&secret_len, 1)) {
6397c478bd9Sstevel@tonic-gate 	warn("No CHAP secret found for authenticating %q to us (%q)",
6407c478bd9Sstevel@tonic-gate 	    rhn, cstate->chal_name);
6417c478bd9Sstevel@tonic-gate     } else {
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/*  generate MD based on negotiated type */
644*55fea89dSDan Cross 	switch (cstate->chal_type) {
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	case CHAP_DIGEST_MD5:		/* only MD5 is defined for now */
6477c478bd9Sstevel@tonic-gate 	    if (remmd_len != MD5_SIGNATURE_SIZE)
6487c478bd9Sstevel@tonic-gate 		break;			/* it's not even the right length */
6497c478bd9Sstevel@tonic-gate 	    MD5Init(&mdContext);
6507c478bd9Sstevel@tonic-gate 	    MD5Update(&mdContext, &cstate->chal_id, 1);
6517c478bd9Sstevel@tonic-gate 	    MD5Update(&mdContext, (u_char *)secret, secret_len);
6527c478bd9Sstevel@tonic-gate 	    MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
653*55fea89dSDan Cross 	    MD5Final(hash, &mdContext);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	    /* compare local and remote MDs and send the appropriate status */
6567c478bd9Sstevel@tonic-gate 	    if (memcmp (hash, remmd, MD5_SIGNATURE_SIZE) == 0)
6577c478bd9Sstevel@tonic-gate 		code = CHAP_SUCCESS;	/* they are the same! */
6587c478bd9Sstevel@tonic-gate 	    break;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate #ifdef CHAPMS
6617c478bd9Sstevel@tonic-gate 	case CHAP_MICROSOFT:
6627c478bd9Sstevel@tonic-gate 		if (ChapMSValidate(cstate, remmd, remmd_len, secret,
6637c478bd9Sstevel@tonic-gate 		    secret_len))
6647c478bd9Sstevel@tonic-gate 			code = CHAP_SUCCESS;
6657c478bd9Sstevel@tonic-gate 		break;
6667c478bd9Sstevel@tonic-gate #endif
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate #ifdef CHAPMSV2
6697c478bd9Sstevel@tonic-gate 	case CHAP_MICROSOFT_V2:
6707c478bd9Sstevel@tonic-gate 		if (ChapMSv2Validate(cstate, rhostname, remmd, remmd_len,
6717c478bd9Sstevel@tonic-gate 		    secret, secret_len))
6727c478bd9Sstevel@tonic-gate 			code = CHAP_SUCCESS;
6737c478bd9Sstevel@tonic-gate 		break;
6747c478bd9Sstevel@tonic-gate #endif
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	default:
6777c478bd9Sstevel@tonic-gate 	    error("CHAP: unknown digest type %d", cstate->chal_type);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate     }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate     BZERO(secret, sizeof(secret));
6827c478bd9Sstevel@tonic-gate     ChapSendStatus(cstate, code);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate     if (code == CHAP_SUCCESS) {
6857c478bd9Sstevel@tonic-gate 	old_state = cstate->serverstate;
6867c478bd9Sstevel@tonic-gate 	cstate->serverstate = CHAPSS_OPEN;
6877c478bd9Sstevel@tonic-gate 	if (old_state == CHAPSS_INITIAL_CHAL) {
6887c478bd9Sstevel@tonic-gate 	    auth_peer_success(cstate->unit, PPP_CHAP, rhostname, len);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 	if (cstate->chal_interval != 0)
6917c478bd9Sstevel@tonic-gate 	    TIMEOUT(ChapRechallenge, cstate, cstate->chal_interval);
6927c478bd9Sstevel@tonic-gate 	if (old_state == CHAPSS_INITIAL_CHAL)
6937c478bd9Sstevel@tonic-gate 	    notice("CHAP peer authentication succeeded for %q", rhostname);
6947c478bd9Sstevel@tonic-gate 	else if (debug)
6957c478bd9Sstevel@tonic-gate 	    dbglog("CHAP peer reauthentication succeeded for %q", rhostname);
6967c478bd9Sstevel@tonic-gate     } else {
6977c478bd9Sstevel@tonic-gate 	error("CHAP peer %sauthentication failed for remote host %q",
6987c478bd9Sstevel@tonic-gate 	    (cstate->serverstate == CHAPSS_INITIAL_CHAL ? "" : "re"),
6997c478bd9Sstevel@tonic-gate 	    rhostname);
7007c478bd9Sstevel@tonic-gate 	cstate->serverstate = CHAPSS_BADAUTH;
7017c478bd9Sstevel@tonic-gate 	auth_peer_fail(cstate->unit, PPP_CHAP);
7027c478bd9Sstevel@tonic-gate     }
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate /*
7067c478bd9Sstevel@tonic-gate  * ChapReceiveSuccess - Receive Success
7077c478bd9Sstevel@tonic-gate  */
7087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7097c478bd9Sstevel@tonic-gate static void
ChapReceiveSuccess(cstate,inp,id,len)7107c478bd9Sstevel@tonic-gate ChapReceiveSuccess(cstate, inp, id, len)
7117c478bd9Sstevel@tonic-gate     chap_state *cstate;
7127c478bd9Sstevel@tonic-gate     u_char *inp;
7137c478bd9Sstevel@tonic-gate     u_char id;
7147c478bd9Sstevel@tonic-gate     int len;
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate     if (cstate->clientstate == CHAPCS_OPEN) {
7187c478bd9Sstevel@tonic-gate 	/* presumably an answer to a duplicate response */
7197c478bd9Sstevel@tonic-gate 	if (debug)
7207c478bd9Sstevel@tonic-gate 	    dbglog("CHAP duplicate Success message ignored");
7217c478bd9Sstevel@tonic-gate 	return;
7227c478bd9Sstevel@tonic-gate     }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate     if (cstate->clientstate != CHAPCS_RESPONSE) {
7257c478bd9Sstevel@tonic-gate 	/* don't know what this is */
7267c478bd9Sstevel@tonic-gate 	if (debug)
7277c478bd9Sstevel@tonic-gate 	    dbglog("CHAP Success unexpectedly received in state %s",
7287c478bd9Sstevel@tonic-gate 		chap_cstate(cstate->clientstate));
7297c478bd9Sstevel@tonic-gate 	return;
7307c478bd9Sstevel@tonic-gate     }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate     UNTIMEOUT(ChapResponseTimeout, cstate);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate     /*
7357c478bd9Sstevel@tonic-gate      * Print message.
7367c478bd9Sstevel@tonic-gate      */
7377c478bd9Sstevel@tonic-gate     if (len > 0)
7387c478bd9Sstevel@tonic-gate 	PRINTMSG(inp, len);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate     cstate->clientstate = CHAPCS_OPEN;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate     auth_withpeer_success(cstate->unit, PPP_CHAP);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate /*
7477c478bd9Sstevel@tonic-gate  * ChapReceiveFailure - Receive failure.
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7507c478bd9Sstevel@tonic-gate static void
ChapReceiveFailure(cstate,inp,id,len)7517c478bd9Sstevel@tonic-gate ChapReceiveFailure(cstate, inp, id, len)
7527c478bd9Sstevel@tonic-gate     chap_state *cstate;
7537c478bd9Sstevel@tonic-gate     u_char *inp;
7547c478bd9Sstevel@tonic-gate     u_char id;
7557c478bd9Sstevel@tonic-gate     int len;
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate     if (cstate->clientstate != CHAPCS_RESPONSE) {
7587c478bd9Sstevel@tonic-gate 	/* don't know what this is */
7597c478bd9Sstevel@tonic-gate 	if (debug)
7607c478bd9Sstevel@tonic-gate 	    dbglog("CHAP Failure unexpectedly received in state %s",
7617c478bd9Sstevel@tonic-gate 		chap_cstate(cstate->clientstate));
7627c478bd9Sstevel@tonic-gate 	return;
7637c478bd9Sstevel@tonic-gate     }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate     UNTIMEOUT(ChapResponseTimeout, cstate);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate     /*
7687c478bd9Sstevel@tonic-gate      * Print message.
7697c478bd9Sstevel@tonic-gate      */
7707c478bd9Sstevel@tonic-gate     if (len > 0)
7717c478bd9Sstevel@tonic-gate 	PRINTMSG(inp, len);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate     error("CHAP authentication failed");
7747c478bd9Sstevel@tonic-gate     auth_withpeer_fail(cstate->unit, PPP_CHAP);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate /*
7797c478bd9Sstevel@tonic-gate  * ChapSendChallenge - Send an Authenticate challenge.
7807c478bd9Sstevel@tonic-gate  */
7817c478bd9Sstevel@tonic-gate static void
ChapSendChallenge(cstate)7827c478bd9Sstevel@tonic-gate ChapSendChallenge(cstate)
7837c478bd9Sstevel@tonic-gate     chap_state *cstate;
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate     u_char *outp;
7867c478bd9Sstevel@tonic-gate     int chal_len, name_len;
7877c478bd9Sstevel@tonic-gate     int outlen;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate     chal_len = cstate->chal_len;
7907c478bd9Sstevel@tonic-gate     name_len = strlen(cstate->chal_name);
7917c478bd9Sstevel@tonic-gate     outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
7927c478bd9Sstevel@tonic-gate     outp = outpacket_buf;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate     MAKEHEADER(outp, PPP_CHAP);		/* paste in a CHAP header */
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate     PUTCHAR(CHAP_CHALLENGE, outp);
7977c478bd9Sstevel@tonic-gate     PUTCHAR(cstate->chal_id, outp);
7987c478bd9Sstevel@tonic-gate     PUTSHORT(outlen, outp);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate     PUTCHAR(chal_len, outp);		/* put length of challenge */
8017c478bd9Sstevel@tonic-gate     BCOPY(cstate->challenge, outp, chal_len);
8027c478bd9Sstevel@tonic-gate     INCPTR(chal_len, outp);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate     BCOPY(cstate->chal_name, outp, name_len);	/* append hostname */
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate     output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
807*55fea89dSDan Cross 
8087c478bd9Sstevel@tonic-gate     TIMEOUT(ChapChallengeTimeout, cstate, cstate->timeouttime);
8097c478bd9Sstevel@tonic-gate     ++cstate->chal_transmits;
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * ChapSendStatus - Send a status response (ack or nak).
8157c478bd9Sstevel@tonic-gate  */
8167c478bd9Sstevel@tonic-gate static void
ChapSendStatus(cstate,code)8177c478bd9Sstevel@tonic-gate ChapSendStatus(cstate, code)
8187c478bd9Sstevel@tonic-gate     chap_state *cstate;
8197c478bd9Sstevel@tonic-gate     int code;
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate     u_char *outp;
8227c478bd9Sstevel@tonic-gate     int outlen, msglen;
8237c478bd9Sstevel@tonic-gate     char msg[256], *msgp;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate     if ((msgp = cstate->stat_message) != NULL) {
8267c478bd9Sstevel@tonic-gate 	msglen = cstate->stat_length;
8277c478bd9Sstevel@tonic-gate     } else {
8287c478bd9Sstevel@tonic-gate 	if (code == CHAP_SUCCESS)
8297c478bd9Sstevel@tonic-gate 	    (void) slprintf(msg, sizeof(msg), "Welcome to %s.", hostname);
8307c478bd9Sstevel@tonic-gate 	else
8317c478bd9Sstevel@tonic-gate 	    (void) slprintf(msg, sizeof(msg), "I don't like you.  Go 'way.");
8327c478bd9Sstevel@tonic-gate 	msglen = strlen(msg);
8337c478bd9Sstevel@tonic-gate 	msgp = msg;
8347c478bd9Sstevel@tonic-gate     }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate     outlen = CHAP_HEADERLEN + msglen;
8377c478bd9Sstevel@tonic-gate     outp = outpacket_buf;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate     MAKEHEADER(outp, PPP_CHAP);	/* paste in a header */
840*55fea89dSDan Cross 
8417c478bd9Sstevel@tonic-gate     PUTCHAR(code, outp);
8427c478bd9Sstevel@tonic-gate     PUTCHAR(cstate->chal_id, outp);
8437c478bd9Sstevel@tonic-gate     PUTSHORT(outlen, outp);
8447c478bd9Sstevel@tonic-gate     BCOPY(msgp, outp, msglen);
8457c478bd9Sstevel@tonic-gate     output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate /*
8497c478bd9Sstevel@tonic-gate  * ChapGenChallenge is used to generate a pseudo-random challenge string of
8507c478bd9Sstevel@tonic-gate  * a pseudo-random length between min_len and max_len.  The challenge
8517c478bd9Sstevel@tonic-gate  * string and its length are stored in *cstate, and various other fields of
8527c478bd9Sstevel@tonic-gate  * *cstate are initialized.
8537c478bd9Sstevel@tonic-gate  */
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate static void
ChapGenChallenge(cstate)8567c478bd9Sstevel@tonic-gate ChapGenChallenge(cstate)
8577c478bd9Sstevel@tonic-gate     chap_state *cstate;
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate     int chal_len;
8607c478bd9Sstevel@tonic-gate     u_char *ptr = cstate->challenge;
8617c478bd9Sstevel@tonic-gate     int i;
8627c478bd9Sstevel@tonic-gate 
863*55fea89dSDan Cross     /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
864*55fea89dSDan Cross        MAX_CHALLENGE_LENGTH */
8657c478bd9Sstevel@tonic-gate     chal_len =  (unsigned) ((drand48() *
8667c478bd9Sstevel@tonic-gate 			     (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
8677c478bd9Sstevel@tonic-gate 			    MIN_CHALLENGE_LENGTH);
8687c478bd9Sstevel@tonic-gate     cstate->chal_len = chal_len;
8697c478bd9Sstevel@tonic-gate     cstate->chal_id = ++cstate->id;
8707c478bd9Sstevel@tonic-gate     cstate->chal_transmits = 0;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate     /* XXX tack on saved get_first_hwaddr() here. */
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate     /* generate a random string */
8757c478bd9Sstevel@tonic-gate     for (i = 0; i < chal_len; i++)
8767c478bd9Sstevel@tonic-gate 	*ptr++ = (char) (drand48() * 0xff);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate /*
8807c478bd9Sstevel@tonic-gate  * ChapSendResponse - send a response packet with values as specified
8817c478bd9Sstevel@tonic-gate  * in *cstate.
8827c478bd9Sstevel@tonic-gate  */
8837c478bd9Sstevel@tonic-gate /* ARGSUSED */
8847c478bd9Sstevel@tonic-gate static void
ChapSendResponse(cstate)8857c478bd9Sstevel@tonic-gate ChapSendResponse(cstate)
8867c478bd9Sstevel@tonic-gate     chap_state *cstate;
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate     u_char *outp;
8897c478bd9Sstevel@tonic-gate     int outlen, md_len, name_len;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate     md_len = cstate->resp_length;
8927c478bd9Sstevel@tonic-gate     name_len = strlen(cstate->resp_name);
8937c478bd9Sstevel@tonic-gate     outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
8947c478bd9Sstevel@tonic-gate     outp = outpacket_buf;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate     MAKEHEADER(outp, PPP_CHAP);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate     PUTCHAR(CHAP_RESPONSE, outp);	/* we are a response */
8997c478bd9Sstevel@tonic-gate     PUTCHAR(cstate->resp_id, outp);	/* copy id from challenge packet */
9007c478bd9Sstevel@tonic-gate     PUTSHORT(outlen, outp);		/* packet length */
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate     PUTCHAR(md_len, outp);		/* length of MD */
9037c478bd9Sstevel@tonic-gate     BCOPY(cstate->response, outp, md_len);	/* copy MD to buffer */
9047c478bd9Sstevel@tonic-gate     INCPTR(md_len, outp);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate     BCOPY(cstate->resp_name, outp, name_len); /* append our name */
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate     /* send the packet */
9097c478bd9Sstevel@tonic-gate     output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate     cstate->clientstate = CHAPCS_RESPONSE;
9127c478bd9Sstevel@tonic-gate     TIMEOUT(ChapResponseTimeout, cstate, cstate->timeouttime);
9137c478bd9Sstevel@tonic-gate     ++cstate->resp_transmits;
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate /*
9177c478bd9Sstevel@tonic-gate  * ChapPrintPkt - print the contents of a CHAP packet.
9187c478bd9Sstevel@tonic-gate  */
9197c478bd9Sstevel@tonic-gate static char *ChapCodenames[] = {
9207c478bd9Sstevel@tonic-gate     "Challenge", "Response", "Success", "Failure"
9217c478bd9Sstevel@tonic-gate };
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate static int
ChapPrintPkt(p,plen,printer,arg)9247c478bd9Sstevel@tonic-gate ChapPrintPkt(p, plen, printer, arg)
9257c478bd9Sstevel@tonic-gate     u_char *p;
9267c478bd9Sstevel@tonic-gate     int plen;
9277c478bd9Sstevel@tonic-gate     void (*printer) __P((void *, const char *, ...));
9287c478bd9Sstevel@tonic-gate     void *arg;
9297c478bd9Sstevel@tonic-gate {
9307c478bd9Sstevel@tonic-gate     int code, id, len;
9317c478bd9Sstevel@tonic-gate     int clen, nlen;
9327c478bd9Sstevel@tonic-gate     u_char x;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate     if (plen < CHAP_HEADERLEN)
9357c478bd9Sstevel@tonic-gate 	return 0;
9367c478bd9Sstevel@tonic-gate     GETCHAR(code, p);
9377c478bd9Sstevel@tonic-gate     GETCHAR(id, p);
9387c478bd9Sstevel@tonic-gate     GETSHORT(len, p);
9397c478bd9Sstevel@tonic-gate     if (len < CHAP_HEADERLEN || len > plen)
9407c478bd9Sstevel@tonic-gate 	return 0;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate     if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
9437c478bd9Sstevel@tonic-gate 	printer(arg, " %s", ChapCodenames[code-1]);
9447c478bd9Sstevel@tonic-gate     else
9457c478bd9Sstevel@tonic-gate 	printer(arg, " code=0x%x", code);
9467c478bd9Sstevel@tonic-gate     printer(arg, " id=0x%x", id);
9477c478bd9Sstevel@tonic-gate     len -= CHAP_HEADERLEN;
9487c478bd9Sstevel@tonic-gate     switch (code) {
9497c478bd9Sstevel@tonic-gate     case CHAP_CHALLENGE:
9507c478bd9Sstevel@tonic-gate     case CHAP_RESPONSE:
9517c478bd9Sstevel@tonic-gate 	if (len < 1)
9527c478bd9Sstevel@tonic-gate 	    break;
9537c478bd9Sstevel@tonic-gate 	clen = p[0];
9547c478bd9Sstevel@tonic-gate 	if (len < clen + 1)
9557c478bd9Sstevel@tonic-gate 	    break;
9567c478bd9Sstevel@tonic-gate 	++p;
9577c478bd9Sstevel@tonic-gate 	nlen = len - clen - 1;
9587c478bd9Sstevel@tonic-gate 	printer(arg, " <");
9597c478bd9Sstevel@tonic-gate 	for (; clen > 0; --clen) {
9607c478bd9Sstevel@tonic-gate 	    GETCHAR(x, p);
9617c478bd9Sstevel@tonic-gate 	    printer(arg, "%.2x", x);
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 	printer(arg, ">, name = ");
9647c478bd9Sstevel@tonic-gate 	print_string((char *)p, nlen, printer, arg);
9657c478bd9Sstevel@tonic-gate 	break;
9667c478bd9Sstevel@tonic-gate     case CHAP_FAILURE:
9677c478bd9Sstevel@tonic-gate     case CHAP_SUCCESS:
9687c478bd9Sstevel@tonic-gate 	printer(arg, " ");
9697c478bd9Sstevel@tonic-gate 	print_string((char *)p, len, printer, arg);
9707c478bd9Sstevel@tonic-gate 	break;
9717c478bd9Sstevel@tonic-gate     default:
9727c478bd9Sstevel@tonic-gate 	for (clen = len; clen > 0; --clen) {
9737c478bd9Sstevel@tonic-gate 	    GETCHAR(x, p);
9747c478bd9Sstevel@tonic-gate 	    printer(arg, " %.2x", x);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate     }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate     return len + CHAP_HEADERLEN;
9797c478bd9Sstevel@tonic-gate }
980