]> git.ozlabs.org Git - ppp.git/blob - pppd/chap.c
fix some minor compilation warnings
[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.3 1994/04/18 04:01:07 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     UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
561
562     /*
563      * Print message.
564      */
565     if (len > 0)
566         PRINTMSG(inp, len);
567
568     cstate->clientstate = CHAPCS_OPEN;
569
570     auth_withpeer_success(cstate->unit, CHAP);
571 }
572
573
574 /*
575  * ChapReceiveFailure - Receive failure.
576  */
577 static void
578 ChapReceiveFailure(cstate, inp, id, len)
579     chap_state *cstate;
580     u_char *inp;
581     u_char id;
582     int len;
583 {
584     u_char msglen;
585     u_char *msg;
586   
587     CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
588
589     if (cstate->clientstate != CHAPCS_RESPONSE) {
590         /* don't know what this is */
591         CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
592                    cstate->clientstate));
593         return;
594     }
595
596     UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
597
598     /*
599      * Print message.
600      */
601     if (len > 0)
602         PRINTMSG(inp, len);
603
604     syslog(LOG_ERR, "CHAP authentication failed");
605     auth_withpeer_fail(cstate->unit, CHAP);
606 }
607
608
609 /*
610  * ChapSendChallenge - Send an Authenticate challenge.
611  */
612 static void
613 ChapSendChallenge(cstate)
614     chap_state *cstate;
615 {
616     u_char *outp;
617     int chal_len, name_len;
618     int outlen;
619
620     chal_len = cstate->chal_len;
621     name_len = strlen(cstate->chal_name);
622     outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
623     outp = outpacket_buf;
624
625     MAKEHEADER(outp, CHAP);             /* paste in a CHAP header */
626
627     PUTCHAR(CHAP_CHALLENGE, outp);
628     PUTCHAR(cstate->chal_id, outp);
629     PUTSHORT(outlen, outp);
630
631     PUTCHAR(chal_len, outp);            /* put length of challenge */
632     BCOPY(cstate->challenge, outp, chal_len);
633     INCPTR(chal_len, outp);
634
635     BCOPY(cstate->chal_name, outp, name_len);   /* append hostname */
636
637     output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
638   
639     CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
640
641     TIMEOUT(ChapChallengeTimeout, (caddr_t) cstate, cstate->timeouttime);
642     ++cstate->chal_transmits;
643 }
644
645
646 /*
647  * ChapSendStatus - Send a status response (ack or nak).
648  */
649 static void
650 ChapSendStatus(cstate, code)
651     chap_state *cstate;
652     int code;
653 {
654     u_char *outp;
655     int outlen, msglen;
656     char msg[256];
657
658     if (code == CHAP_SUCCESS)
659         sprintf(msg, "Welcome to %s.", hostname);
660     else
661         sprintf(msg, "I don't like you.  Go 'way.");
662     msglen = strlen(msg);
663
664     outlen = CHAP_HEADERLEN + msglen;
665     outp = outpacket_buf;
666
667     MAKEHEADER(outp, CHAP);     /* paste in a header */
668   
669     PUTCHAR(code, outp);
670     PUTCHAR(cstate->chal_id, outp);
671     PUTSHORT(outlen, outp);
672     BCOPY(msg, outp, msglen);
673     output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
674   
675     CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
676                cstate->chal_id));
677 }
678
679 /*
680  * ChapGenChallenge is used to generate a pseudo-random challenge string of
681  * a pseudo-random length between min_len and max_len.  The challenge
682  * string and its length are stored in *cstate, and various other fields of
683  * *cstate are initialized.
684  */
685
686 static void
687 ChapGenChallenge(cstate)
688     chap_state *cstate;
689 {
690     int chal_len;
691     u_char *ptr = cstate->challenge;
692     unsigned int i;
693
694     /* pick a random challenge length between MIN_CHALLENGE_LENGTH and 
695        MAX_CHALLENGE_LENGTH */  
696     chal_len =  (unsigned) ((drand48() *
697                              (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
698                             MIN_CHALLENGE_LENGTH);
699     cstate->chal_len = chal_len;
700     cstate->chal_id = ++cstate->id;
701     cstate->chal_transmits = 0;
702
703     /* generate a random string */
704     for (i = 0; i < chal_len; i++ )
705         *ptr++ = (char) (drand48() * 0xff);
706 }
707
708 /*
709  * ChapSendResponse - send a response packet with values as specified
710  * in *cstate.
711  */
712 /* ARGSUSED */
713 static void
714 ChapSendResponse(cstate)
715     chap_state *cstate;
716 {
717     u_char *outp;
718     int outlen, md_len, name_len;
719
720     md_len = cstate->resp_length;
721     name_len = strlen(cstate->resp_name);
722     outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
723     outp = outpacket_buf;
724
725     MAKEHEADER(outp, CHAP);
726
727     PUTCHAR(CHAP_RESPONSE, outp);       /* we are a response */
728     PUTCHAR(cstate->resp_id, outp);     /* copy id from challenge packet */
729     PUTSHORT(outlen, outp);             /* packet length */
730
731     PUTCHAR(md_len, outp);              /* length of MD */
732     BCOPY(cstate->response, outp, md_len);      /* copy MD to buffer */
733     INCPTR(md_len, outp);
734
735     BCOPY(cstate->resp_name, outp, name_len); /* append our name */
736
737     /* send the packet */
738     output(cstate->unit, outpacket_buf, outlen + DLLHEADERLEN);
739
740     cstate->clientstate = CHAPCS_RESPONSE;
741     TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
742     ++cstate->resp_transmits;
743 }
744
745 /*
746  * ChapPrintPkt - print the contents of a CHAP packet.
747  */
748 char *ChapCodenames[] = {
749     "Challenge", "Response", "Success", "Failure"
750 };
751
752 int
753 ChapPrintPkt(p, plen, printer, arg)
754     u_char *p;
755     int plen;
756     void (*printer) __ARGS((void *, char *, ...));
757     void *arg;
758 {
759     int code, id, len;
760     int clen, nlen;
761     u_char x;
762
763     if (plen < CHAP_HEADERLEN)
764         return 0;
765     GETCHAR(code, p);
766     GETCHAR(id, p);
767     GETSHORT(len, p);
768     if (len < CHAP_HEADERLEN || len > plen)
769         return 0;
770
771     if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
772         printer(arg, " %s", ChapCodenames[code-1]);
773     else
774         printer(arg, " code=0x%x", code);
775     printer(arg, " id=0x%x", id);
776     len -= CHAP_HEADERLEN;
777     switch (code) {
778     case CHAP_CHALLENGE:
779     case CHAP_RESPONSE:
780         if (len < 1)
781             break;
782         clen = p[0];
783         if (len < clen + 1)
784             break;
785         ++p;
786         nlen = len - clen - 1;
787         printer(arg, " <");
788         for (; clen > 0; --clen) {
789             GETCHAR(x, p);
790             printer(arg, "%.2x", x);
791         }
792         printer(arg, ">, name = ");
793         print_string((char *)p, nlen, printer, arg);
794         break;
795     case CHAP_FAILURE:
796     case CHAP_SUCCESS:
797         printer(arg, " ");
798         print_string((char *)p, len, printer, arg);
799         break;
800     default:
801         for (clen = len; clen > 0; --clen) {
802             GETCHAR(x, p);
803             printer(arg, " %.2x", x);
804         }
805     }
806
807     return len + CHAP_HEADERLEN;
808 }
809
810 #ifdef NO_DRAND48
811
812 double drand48()
813 {
814   return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
815 }
816
817 void srand48(seedval)
818 long seedval;
819 {
820   srand((int)seedval);
821 }
822
823 #endif