added stuff to print packets in readable form
[ppp.git] / pppd / chap.c
1 /*
2  * chap.c - Crytographic Handshake Authentication Protocol.
3  *
4  * Copyright (c) 1991 Gregory M. Christy.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by Gregory M. Christy.  The name of the author may not be used to
13  * endorse or promote products derived from this software without
14  * specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20
21 #ifndef lint
22 static char rcsid[] = "$Id: chap.c,v 1.2 1994/04/11 07:13:44 paulus Exp $";
23 #endif
24
25 /*
26  * TODO:
27  */
28
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <syslog.h>
33
34 #include "ppp.h"
35 #include "pppd.h"
36 #include "chap.h"
37 #include "md5.h"
38
39 chap_state chap[NPPP];          /* CHAP state; one for each unit */
40
41 static void ChapChallengeTimeout __ARGS((caddr_t));
42 static void ChapResponseTimeout __ARGS((caddr_t));
43 static void ChapReceiveChallenge __ARGS((chap_state *, u_char *, int, int));
44 static void ChapReceiveResponse __ARGS((chap_state *, u_char *, int, int));
45 static void ChapReceiveSuccess __ARGS((chap_state *, u_char *, int, int));
46 static void ChapReceiveFailure __ARGS((chap_state *, u_char *, int, int));
47 static void ChapSendStatus __ARGS((chap_state *, int));
48 static void ChapSendChallenge __ARGS((chap_state *));
49 static void ChapSendResponse __ARGS((chap_state *));
50 static void ChapGenChallenge __ARGS((chap_state *));
51
52 extern double drand48 __ARGS((void));
53 extern void srand48 __ARGS((long));
54
55 /*
56  * ChapInit - Initialize a CHAP unit.
57  */
58 void
59 ChapInit(unit)
60     int unit;
61 {
62     chap_state *cstate = &chap[unit];
63
64     BZERO(cstate, sizeof(*cstate));
65     cstate->unit = unit;
66     cstate->clientstate = CHAPCS_INITIAL;
67     cstate->serverstate = CHAPSS_INITIAL;
68     cstate->timeouttime = CHAP_DEFTIMEOUT;
69     cstate->max_transmits = CHAP_DEFTRANSMITS;
70     srand48((long) time(NULL)); /* joggle random number generator */
71 }
72
73
74 /*
75  * ChapAuthWithPeer - Authenticate us with our peer (start client).
76  *
77  */
78 void
79 ChapAuthWithPeer(unit, our_name, digest)
80     int unit;
81     char *our_name;
82     int digest;
83 {
84     chap_state *cstate = &chap[unit];
85
86     cstate->resp_name = our_name;
87     cstate->resp_type = digest;
88
89     if (cstate->clientstate == CHAPCS_INITIAL ||
90         cstate->clientstate == CHAPCS_PENDING) {
91         /* lower layer isn't up - wait until later */
92         cstate->clientstate = CHAPCS_PENDING;
93         return;
94     }
95
96     /*
97      * We get here as a result of LCP coming up.
98      * So even if CHAP was open before, we will 
99      * have to re-authenticate ourselves.
100      */
101     cstate->clientstate = CHAPCS_LISTEN;
102 }
103
104
105 /*
106  * ChapAuthPeer - Authenticate our peer (start server).
107  */
108 void
109 ChapAuthPeer(unit, our_name, digest)
110     int unit;
111     char *our_name;
112     int digest;
113 {
114     chap_state *cstate = &chap[unit];
115   
116     cstate->chal_name = our_name;
117     cstate->chal_type = digest;
118
119     if (cstate->serverstate == CHAPSS_INITIAL ||
120         cstate->serverstate == CHAPSS_PENDING) {
121         /* lower layer isn't up - wait until later */
122         cstate->serverstate = CHAPSS_PENDING;
123         return;
124     }
125
126     ChapGenChallenge(cstate);
127     ChapSendChallenge(cstate);          /* crank it up dude! */
128     cstate->serverstate = CHAPSS_INITIAL_CHAL;
129 }
130
131
132 /*
133  * ChapChallengeTimeout - Timeout expired on sending challenge.
134  */
135 static void
136 ChapChallengeTimeout(arg)
137     caddr_t arg;
138 {
139     chap_state *cstate = (chap_state *) arg;
140   
141     /* if we aren't sending challenges, don't worry.  then again we */
142     /* probably shouldn't be here either */
143     if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
144         cstate->serverstate != CHAPSS_RECHALLENGE)
145         return;
146
147     if (cstate->chal_transmits >= cstate->max_transmits) {
148         /* give up on peer */
149         syslog(LOG_ERR, "Peer failed to respond to CHAP challenge");
150         cstate->serverstate = CHAPSS_BADAUTH;
151         auth_peer_fail(cstate->unit, CHAP);
152         return;
153     }
154
155     ChapSendChallenge(cstate);          /* Re-send challenge */
156 }
157
158
159 /*
160  * ChapResponseTimeout - Timeout expired on sending response.
161  */
162 static void
163 ChapResponseTimeout(arg)
164     caddr_t arg;
165 {
166     chap_state *cstate = (chap_state *) arg;
167
168     /* if we aren't sending a response, don't worry. */
169     if (cstate->clientstate != CHAPCS_RESPONSE)
170         return;
171
172     ChapSendResponse(cstate);           /* re-send response */
173 }
174
175
176 /*
177  * ChapRechallenge - Time to challenge the peer again.
178  */
179 static void
180 ChapRechallenge(arg)
181     caddr_t arg;
182 {
183     chap_state *cstate = (chap_state *) arg;
184
185     /* if we aren't sending a response, don't worry. */
186     if (cstate->serverstate != CHAPSS_OPEN)
187         return;
188
189     ChapGenChallenge(cstate);
190     ChapSendChallenge(cstate);
191     cstate->serverstate = CHAPSS_RECHALLENGE;
192
193     if (cstate->chal_interval != 0)
194         TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
195 }
196
197
198 /*
199  * ChapLowerUp - The lower layer is up.
200  *
201  * Start up if we have pending requests.
202  */
203 void
204 ChapLowerUp(unit)
205     int unit;
206 {
207     chap_state *cstate = &chap[unit];
208   
209     if (cstate->clientstate == CHAPCS_INITIAL)
210         cstate->clientstate = CHAPCS_CLOSED;
211     else if (cstate->clientstate == CHAPCS_PENDING)
212         cstate->clientstate = CHAPCS_LISTEN;
213
214     if (cstate->serverstate == CHAPSS_INITIAL)
215         cstate->serverstate = CHAPSS_CLOSED;
216     else if (cstate->serverstate == CHAPSS_PENDING) {
217         ChapGenChallenge(cstate);
218         ChapSendChallenge(cstate);
219         cstate->serverstate = CHAPSS_INITIAL_CHAL;
220     }
221 }
222
223
224 /*
225  * ChapLowerDown - The lower layer is down.
226  *
227  * Cancel all timeouts.
228  */
229 void
230 ChapLowerDown(unit)
231     int unit;
232 {
233     chap_state *cstate = &chap[unit];
234   
235     /* Timeout(s) pending?  Cancel if so. */
236     if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
237         cstate->serverstate == CHAPSS_RECHALLENGE)
238         UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
239     else if (cstate->serverstate == CHAPSS_OPEN
240              && cstate->chal_interval != 0)
241         UNTIMEOUT(ChapRechallenge, (caddr_t) cstate);
242     if (cstate->clientstate == CHAPCS_RESPONSE)
243         UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
244
245     cstate->clientstate = CHAPCS_INITIAL;
246     cstate->serverstate = CHAPSS_INITIAL;
247 }
248
249
250 /*
251  * ChapProtocolReject - Peer doesn't grok CHAP.
252  */
253 void
254 ChapProtocolReject(unit)
255     int unit;
256 {
257     chap_state *cstate = &chap[unit];
258
259     if (cstate->serverstate != CHAPSS_INITIAL &&
260         cstate->serverstate != CHAPSS_CLOSED)
261         auth_peer_fail(unit, CHAP);
262     if (cstate->clientstate != CHAPCS_INITIAL &&
263         cstate->clientstate != CHAPCS_CLOSED)
264         auth_withpeer_fail(unit, CHAP);
265     ChapLowerDown(unit);                /* shutdown chap */
266 }
267
268
269 /*
270  * ChapInput - Input CHAP packet.
271  */
272 void
273 ChapInput(unit, inpacket, packet_len)
274     int unit;
275     u_char *inpacket;
276     int packet_len;
277 {
278     chap_state *cstate = &chap[unit];
279     u_char *inp;
280     u_char code, id;
281     int len;
282   
283     /*
284      * Parse header (code, id and length).
285      * If packet too short, drop it.
286      */
287     inp = inpacket;
288     if (packet_len < CHAP_HEADERLEN) {
289         CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short header."));
290         return;
291     }
292     GETCHAR(code, inp);
293     GETCHAR(id, inp);
294     GETSHORT(len, inp);
295     if (len < CHAP_HEADERLEN) {
296         CHAPDEBUG((LOG_INFO, "ChapInput: rcvd illegal length."));
297         return;
298     }
299     if (len > packet_len) {
300         CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short packet."));
301         return;
302     }
303     len -= CHAP_HEADERLEN;
304   
305     /*
306      * Action depends on code (as in fact it usually does :-).
307      */
308     switch (code) {
309     case CHAP_CHALLENGE:
310         ChapReceiveChallenge(cstate, inp, id, len);
311         break;
312     
313     case CHAP_RESPONSE:
314         ChapReceiveResponse(cstate, inp, id, len);
315         break;
316     
317     case CHAP_FAILURE:
318         ChapReceiveFailure(cstate, inp, id, len);
319         break;
320
321     case CHAP_SUCCESS:
322         ChapReceiveSuccess(cstate, inp, id, len);
323         break;
324
325     default:                            /* Need code reject? */
326         syslog(LOG_WARNING, "Unknown CHAP code (%d) received.", code);
327         break;
328     }
329 }
330
331
332 /*
333  * ChapReceiveChallenge - Receive Challenge and send Response.
334  */
335 static void
336 ChapReceiveChallenge(cstate, inp, id, len)
337     chap_state *cstate;
338     u_char *inp;
339     int id;
340     int len;
341 {
342     int rchallenge_len;
343     u_char *rchallenge;
344     int secret_len;
345     char secret[MAXSECRETLEN];
346     char rhostname[256];
347     MD5_CTX mdContext;
348  
349     CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: Rcvd id %d.", id));
350     if (cstate->clientstate == CHAPCS_CLOSED ||
351         cstate->clientstate == CHAPCS_PENDING) {
352         CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: in state %d",
353                    cstate->clientstate));
354         return;
355     }
356
357     if (len < 2) {
358         CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
359         return;
360     }
361
362     GETCHAR(rchallenge_len, inp);
363     len -= sizeof (u_char) + rchallenge_len;    /* now name field length */
364     if (len < 0) {
365         CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
366         return;
367     }
368     rchallenge = inp;
369     INCPTR(rchallenge_len, inp);
370
371     if (len >= sizeof(rhostname))
372         len = sizeof(rhostname) - 1;
373     BCOPY(inp, rhostname, len);
374     rhostname[len] = '\000';
375
376     CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: received name field: %s",
377                rhostname));
378
379     /* get secret for authenticating ourselves with the specified host */
380     if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
381                     secret, &secret_len, 0)) {
382         secret_len = 0;         /* assume null secret if can't find one */
383         syslog(LOG_WARNING, "No CHAP secret found for authenticating us to %s",
384                rhostname);
385     }
386
387     /* cancel response send timeout if necessary */
388     if (cstate->clientstate == CHAPCS_RESPONSE)
389         UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
390
391     cstate->resp_id = id;
392     cstate->resp_transmits = 0;
393
394     /*  generate MD based on negotiated type */
395     switch (cstate->resp_type) { 
396
397     case CHAP_DIGEST_MD5:               /* only MD5 is defined for now */
398         MD5Init(&mdContext);
399         MD5Update(&mdContext, &cstate->resp_id, 1);
400         MD5Update(&mdContext, secret, secret_len);
401         MD5Update(&mdContext, rchallenge, rchallenge_len);
402         MD5Final(&mdContext);
403         BCOPY(mdContext.digest, cstate->response, MD5_SIGNATURE_SIZE);
404         cstate->resp_length = MD5_SIGNATURE_SIZE;
405         break;
406
407     default:
408         CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->resp_type));
409         return;
410     }
411
412     ChapSendResponse(cstate);
413 }
414
415
416 /*
417  * ChapReceiveResponse - Receive and process response.
418  */
419 static void
420 ChapReceiveResponse(cstate, inp, id, len)
421     chap_state *cstate;
422     u_char *inp;
423     int id;
424     int len;
425 {
426     u_char *remmd, remmd_len;
427     int secret_len, old_state;
428     int code;
429     char rhostname[256];
430     u_char buf[256];
431     MD5_CTX mdContext;
432     u_char msg[256];
433     char secret[MAXSECRETLEN];
434
435     CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: Rcvd id %d.", id));
436
437     if (cstate->serverstate == CHAPSS_CLOSED ||
438         cstate->serverstate == CHAPSS_PENDING) {
439         CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: in state %d",
440                    cstate->serverstate));
441         return;
442     }
443
444     if (id != cstate->chal_id)
445         return;                 /* doesn't match ID of last challenge */
446
447     /*
448      * If we have received a duplicate or bogus Response,
449      * we have to send the same answer (Success/Failure)
450      * as we did for the first Response we saw.
451      */
452     if (cstate->serverstate == CHAPSS_OPEN) {
453         ChapSendStatus(cstate, CHAP_SUCCESS);
454         return;
455     }
456     if (cstate->serverstate == CHAPSS_BADAUTH) {
457         ChapSendStatus(cstate, CHAP_FAILURE);
458         return;
459     }
460
461     if (len < 2) {
462         CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
463         return;
464     }
465     GETCHAR(remmd_len, inp);            /* get length of MD */
466     remmd = inp;                        /* get pointer to MD */
467     INCPTR(remmd_len, inp);
468
469     len -= sizeof (u_char) + remmd_len;
470     if (len < 0) {
471         CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
472         return;
473     }
474
475     UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
476
477     if (len >= sizeof(rhostname))
478         len = sizeof(rhostname) - 1;
479     BCOPY(inp, rhostname, len);
480     rhostname[len] = '\000';
481
482     CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: received name field: %s",
483                rhostname));
484
485     /*
486      * Get secret for authenticating them with us,
487      * do the hash ourselves, and compare the result.
488      */
489     code = CHAP_FAILURE;
490     if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
491                    secret, &secret_len, 1)) {
492         syslog(LOG_WARNING, "No CHAP secret found for authenticating %s",
493                rhostname);
494     } else {
495
496         /*  generate MD based on negotiated type */
497         switch (cstate->chal_type) { 
498
499         case CHAP_DIGEST_MD5:           /* only MD5 is defined for now */
500             if (remmd_len != MD5_SIGNATURE_SIZE)
501                 break;                  /* it's not even the right length */
502             MD5Init(&mdContext);
503             MD5Update(&mdContext, &cstate->chal_id, 1);
504             MD5Update(&mdContext, secret, secret_len);
505             MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
506             MD5Final(&mdContext); 
507
508             /* compare local and remote MDs and send the appropriate status */
509             if (bcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
510                 code = CHAP_SUCCESS;    /* they are the same! */
511             break;
512
513         default:
514             CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->chal_type));
515         }
516     }
517
518     ChapSendStatus(cstate, code);
519
520     if (code == CHAP_SUCCESS) {
521         old_state = cstate->serverstate;
522         cstate->serverstate = CHAPSS_OPEN;
523         if (old_state == CHAPSS_INITIAL_CHAL) {
524             auth_peer_success(cstate->unit, CHAP);
525         }
526         if (cstate->chal_interval != 0)
527             TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
528
529     } else {
530         syslog(LOG_ERR, "CHAP peer authentication failed");
531         cstate->serverstate = CHAPSS_BADAUTH;
532         auth_peer_fail(cstate->unit, CHAP);
533     }
534 }
535
536 /*
537  * ChapReceiveSuccess - Receive Success
538  */
539 static void
540 ChapReceiveSuccess(cstate, inp, id, len)
541     chap_state *cstate;
542     u_char *inp;
543     u_char id;
544     int len;
545 {
546
547     CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: Rcvd id %d.", id));
548
549     if (cstate->clientstate == CHAPCS_OPEN)
550         /* presumably an answer to a duplicate response */
551         return;
552
553     if (cstate->clientstate != CHAPCS_RESPONSE) {
554         /* don't know what this is */
555         CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: in state %d\n",
556                    cstate->clientstate));
557         return;
558     }
559
560     /*
561      * Print message.
562      */
563     if (len > 0)
564         PRINTMSG(inp, len);
565
566     cstate->clientstate = CHAPCS_OPEN;
567
568     auth_withpeer_success(cstate->unit, CHAP);
569 }
570
571
572 /*
573  * ChapReceiveFailure - Receive failure.
574  */
575 static void
576 ChapReceiveFailure(cstate, inp, id, len)
577     chap_state *cstate;
578     u_char *inp;
579     u_char id;
580     int len;
581 {
582     u_char msglen;
583     u_char *msg;
584   
585     CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
586
587     if (cstate->clientstate != CHAPCS_RESPONSE) {
588         /* don't know what this is */
589         CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
590                    cstate->clientstate));
591         return;
592     }
593
594     /*
595      * Print message.
596      */
597     if (len > 0)
598         PRINTMSG(inp, len);
599
600     syslog(LOG_ERR, "CHAP authentication failed");
601     auth_withpeer_fail(cstate->unit, CHAP);
602 }
603
604
605 /*
606  * ChapSendChallenge - Send an Authenticate challenge.
607  */
608 static void
609 ChapSendChallenge(cstate)
610     chap_state *cstate;
611 {
612     u_char *outp;
613     int chal_len, name_len;
614     int outlen;
615
616     chal_len = cstate->chal_len;
617     name_len = strlen(cstate->chal_name);
618     outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
619     outp = outpacket_buf;
620
621     MAKEHEADER(outp, CHAP);             /* paste in a CHAP header */
622
623     PUTCHAR(CHAP_CHALLENGE, outp);
624     PUTCHAR(cstate->chal_id, outp);
625     PUTSHORT(outlen, outp);
626
627     PUTCHAR(chal_len, outp);            /* put length of challenge */
628     BCOPY(cstate->challenge, outp, chal_len);
629     INCPTR(chal_len, outp);
630
631     BCOPY(cstate->chal_name, outp, name_len);   /* append hostname */
632
633     output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
634   
635     CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
636
637     TIMEOUT(ChapChallengeTimeout, (caddr_t) cstate, cstate->timeouttime);
638     ++cstate->chal_transmits;
639 }
640
641
642 /*
643  * ChapSendStatus - Send a status response (ack or nak).
644  */
645 static void
646 ChapSendStatus(cstate, code)
647     chap_state *cstate;
648     int code;
649 {
650     u_char *outp;
651     int outlen, msglen;
652     char msg[256];
653
654     if (code == CHAP_SUCCESS)
655         sprintf(msg, "Welcome to %s.", hostname);
656     else
657         sprintf(msg, "I don't like you.  Go 'way.");
658     msglen = strlen(msg);
659
660     outlen = CHAP_HEADERLEN + msglen;
661     outp = outpacket_buf;
662
663     MAKEHEADER(outp, CHAP);     /* paste in a header */
664   
665     PUTCHAR(code, outp);
666     PUTCHAR(cstate->chal_id, outp);
667     PUTSHORT(outlen, outp);
668     BCOPY(msg, outp, msglen);
669     output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
670   
671     CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
672                cstate->chal_id));
673 }
674
675 /*
676  * ChapGenChallenge is used to generate a pseudo-random challenge string of
677  * a pseudo-random length between min_len and max_len.  The challenge
678  * string and its length are stored in *cstate, and various other fields of
679  * *cstate are initialized.
680  */
681
682 static void
683 ChapGenChallenge(cstate)
684     chap_state *cstate;
685 {
686     int chal_len;
687     u_char *ptr = cstate->challenge;
688     unsigned int i;
689
690     /* pick a random challenge length between MIN_CHALLENGE_LENGTH and 
691        MAX_CHALLENGE_LENGTH */  
692     chal_len =  (unsigned) ((drand48() *
693                              (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
694                             MIN_CHALLENGE_LENGTH);
695     cstate->chal_len = chal_len;
696     cstate->chal_id = ++cstate->id;
697     cstate->chal_transmits = 0;
698
699     /* generate a random string */
700     for (i = 0; i < chal_len; i++ )
701         *ptr++ = (char) (drand48() * 0xff);
702 }
703
704 /*
705  * ChapSendResponse - send a response packet with values as specified
706  * in *cstate.
707  */
708 /* ARGSUSED */
709 static void
710 ChapSendResponse(cstate)
711     chap_state *cstate;
712 {
713     u_char *outp;
714     int outlen, md_len, name_len;
715
716     md_len = cstate->resp_length;
717     name_len = strlen(cstate->resp_name);
718     outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
719     outp = outpacket_buf;
720
721     MAKEHEADER(outp, CHAP);
722
723     PUTCHAR(CHAP_RESPONSE, outp);       /* we are a response */
724     PUTCHAR(cstate->resp_id, outp);     /* copy id from challenge packet */
725     PUTSHORT(outlen, outp);             /* packet length */
726
727     PUTCHAR(md_len, outp);              /* length of MD */
728     BCOPY(cstate->response, outp, md_len);      /* copy MD to buffer */
729     INCPTR(md_len, outp);
730
731     BCOPY(cstate->resp_name, outp, name_len); /* append our name */
732
733     /* send the packet */
734     output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
735
736     cstate->clientstate = CHAPCS_RESPONSE;
737     TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
738     ++cstate->resp_transmits;
739 }
740
741 /*
742  * ChapPrintPkt - print the contents of a CHAP packet.
743  */
744 char *ChapCodenames[] = {
745     "Challenge", "Response", "Success", "Failure"
746 };
747
748 int
749 ChapPrintPkt(p, plen, printer, arg)
750     u_char *p;
751     int plen;
752     void (*printer) __ARGS((void *, char *, ...));
753     void *arg;
754 {
755     int code, id, len;
756     int clen, nlen;
757     u_char x;
758
759     if (plen < CHAP_HEADERLEN)
760         return 0;
761     GETCHAR(code, p);
762     GETCHAR(id, p);
763     GETSHORT(len, p);
764     if (len < CHAP_HEADERLEN || len > plen)
765         return 0;
766
767     if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
768         printer(arg, " %s", ChapCodenames[code-1]);
769     else
770         printer(arg, " code=0x%x", code);
771     printer(arg, " id=0x%x", id);
772     len -= CHAP_HEADERLEN;
773     switch (code) {
774     case CHAP_CHALLENGE:
775     case CHAP_RESPONSE:
776         if (len < 1)
777             break;
778         clen = p[0];
779         if (len < clen + 1)
780             break;
781         ++p;
782         nlen = len - clen - 1;
783         printer(arg, " <");
784         for (; clen > 0; --clen) {
785             GETCHAR(x, p);
786             printer(arg, "%.2x", x);
787         }
788         printer(arg, ">, name = ");
789         print_string((char *)p, nlen, printer, arg);
790         break;
791     case CHAP_FAILURE:
792     case CHAP_SUCCESS:
793         printer(arg, " ");
794         print_string((char *)p, len, printer, arg);
795         break;
796     default:
797         for (clen = len; clen > 0; --clen) {
798             GETCHAR(x, p);
799             printer(arg, " %.2x", x);
800         }
801     }
802
803     return len + CHAP_HEADERLEN;
804 }
805
806 #ifdef NO_DRAND48
807
808 double drand48()
809 {
810   return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
811 }
812
813 void srand48(seedval)
814 long seedval;
815 {
816   srand((int)seedval);
817 }
818
819 #endif