]> git.ozlabs.org Git - ppp.git/blob - pppd/chap-new.c
0b1bb86e77c0d35a03b6e58629ace81748ec36e5
[ppp.git] / pppd / chap-new.c
1 /*
2  * chap-new.c - New CHAP implementation.
3  *
4  * Copyright (c) 2003 Paul Mackerras. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. The name(s) of the authors of this software must not be used to
14  *    endorse or promote products derived from this software without
15  *    prior written permission.
16  *
17  * 3. Redistributions of any form whatsoever must retain the following
18  *    acknowledgment:
19  *    "This product includes software developed by Paul Mackerras
20  *     <paulus@samba.org>".
21  *
22  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24  * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29  */
30
31 #define RCSID   "$Id: chap-new.c,v 1.9 2007/06/19 02:08:35 carlsonj Exp $"
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include "pppd.h"
36 #include "session.h"
37 #include "chap-new.h"
38 #include "chap-md5.h"
39
40 #ifdef CHAPMS
41 #include "chap_ms.h"
42 #define MDTYPE_ALL (MDTYPE_MICROSOFT_V2 | MDTYPE_MICROSOFT | MDTYPE_MD5)
43 #else
44 #define MDTYPE_ALL (MDTYPE_MD5)
45 #endif
46
47 int chap_mdtype_all = MDTYPE_ALL;
48
49 /* Hook for a plugin to validate CHAP challenge */
50 int (*chap_verify_hook)(char *name, char *ourname, int id,
51                         struct chap_digest_type *digest,
52                         unsigned char *challenge, unsigned char *response,
53                         char *message, int message_space) = NULL;
54
55 /*
56  * Option variables.
57  */
58 int chap_server_timeout_time = 3;
59 int chap_max_transmits = 10;
60 int chap_rechallenge_time = 0;
61 int chap_client_timeout_time = 60;
62
63 /*
64  * Command-line options.
65  */
66 static option_t chap_option_list[] = {
67         { "chap-restart", o_int, &chap_server_timeout_time,
68           "Set timeout for CHAP (as server)", OPT_PRIO },
69         { "chap-max-challenge", o_int, &chap_max_transmits,
70           "Set max #xmits for challenge", OPT_PRIO },
71         { "chap-interval", o_int, &chap_rechallenge_time,
72           "Set interval for rechallenge", OPT_PRIO },
73         { "chap-timeout", o_int, &chap_client_timeout_time,
74           "Set timeout for CHAP (as client)", OPT_PRIO },
75         { NULL }
76 };
77
78 /*
79  * Internal state.
80  */
81 static struct chap_client_state {
82         int flags;
83         char *name;
84         struct chap_digest_type *digest;
85         unsigned char priv[64];         /* private area for digest's use */
86 } client;
87
88 /*
89  * These limits apply to challenge and response packets we send.
90  * The +4 is the +1 that we actually need rounded up.
91  */
92 #define CHAL_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_CHALLENGE_LEN + MAXNAMELEN)
93 #define RESP_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_RESPONSE_LEN + MAXNAMELEN)
94
95 static struct chap_server_state {
96         int flags;
97         int id;
98         char *name;
99         struct chap_digest_type *digest;
100         int challenge_xmits;
101         int challenge_pktlen;
102         unsigned char challenge[CHAL_MAX_PKTLEN];
103         char message[256];
104 } server;
105
106 /* Values for flags in chap_client_state and chap_server_state */
107 #define LOWERUP                 1
108 #define AUTH_STARTED            2
109 #define AUTH_DONE               4
110 #define AUTH_FAILED             8
111 #define TIMEOUT_PENDING         0x10
112 #define CHALLENGE_VALID         0x20
113
114 /*
115  * Prototypes.
116  */
117 static void chap_init(int unit);
118 static void chap_lowerup(int unit);
119 static void chap_lowerdown(int unit);
120 static void chap_server_timeout(void *arg);
121 static void chap_client_timeout(void *arg);
122 static void chap_generate_challenge(struct chap_server_state *ss);
123 static void chap_handle_response(struct chap_server_state *ss, int code,
124                 unsigned char *pkt, int len);
125 static int chap_verify_response(char *name, char *ourname, int id,
126                 struct chap_digest_type *digest,
127                 unsigned char *challenge, unsigned char *response,
128                 char *message, int message_space);
129 static void chap_respond(struct chap_client_state *cs, int id,
130                 unsigned char *pkt, int len);
131 static void chap_handle_status(struct chap_client_state *cs, int code, int id,
132                 unsigned char *pkt, int len);
133 static void chap_protrej(int unit);
134 static void chap_input(int unit, unsigned char *pkt, int pktlen);
135 static int chap_print_pkt(unsigned char *p, int plen,
136                 void (*printer)(void *, char *, ...), void *arg);
137
138 /* List of digest types that we know about */
139 static struct chap_digest_type *chap_digests;
140
141 /*
142  * chap_init - reset to initial state.
143  */
144 static void
145 chap_init(int unit)
146 {
147         memset(&client, 0, sizeof(client));
148         memset(&server, 0, sizeof(server));
149
150         chap_md5_init();
151 #ifdef CHAPMS
152         chapms_init();
153 #endif
154 }
155
156 /*
157  * Add a new digest type to the list.
158  */
159 void
160 chap_register_digest(struct chap_digest_type *dp)
161 {
162         dp->next = chap_digests;
163         chap_digests = dp;
164 }
165
166 /*
167  * chap_lowerup - we can start doing stuff now.
168  */
169 static void
170 chap_lowerup(int unit)
171 {
172         struct chap_client_state *cs = &client;
173         struct chap_server_state *ss = &server;
174
175         cs->flags |= LOWERUP;
176         ss->flags |= LOWERUP;
177         if (ss->flags & AUTH_STARTED)
178                 chap_server_timeout(ss);
179 }
180
181 static void
182 chap_lowerdown(int unit)
183 {
184         struct chap_client_state *cs = &client;
185         struct chap_server_state *ss = &server;
186
187         if (cs->flags & TIMEOUT_PENDING)
188                 UNTIMEOUT(chap_client_timeout, cs);
189         cs->flags = 0;
190         if (ss->flags & TIMEOUT_PENDING)
191                 UNTIMEOUT(chap_server_timeout, ss);
192         ss->flags = 0;
193 }
194
195 /*
196  * chap_auth_peer - Start authenticating the peer.
197  * If the lower layer is already up, we start sending challenges,
198  * otherwise we wait for the lower layer to come up.
199  */
200 void
201 chap_auth_peer(int unit, char *our_name, int digest_code)
202 {
203         struct chap_server_state *ss = &server;
204         struct chap_digest_type *dp;
205
206         if (ss->flags & AUTH_STARTED) {
207                 error("CHAP: peer authentication already started!");
208                 return;
209         }
210         for (dp = chap_digests; dp != NULL; dp = dp->next)
211                 if (dp->code == digest_code)
212                         break;
213         if (dp == NULL)
214                 fatal("CHAP digest 0x%x requested but not available",
215                       digest_code);
216
217         ss->digest = dp;
218         ss->name = our_name;
219         /* Start with a random ID value */
220         ss->id = (unsigned char)(drand48() * 256);
221         ss->flags |= AUTH_STARTED;
222         if (ss->flags & LOWERUP)
223                 chap_server_timeout(ss);
224 }
225
226 /*
227  * chap_auth_with_peer - Prepare to authenticate ourselves to the peer.
228  * There isn't much to do until we receive a challenge.
229  */
230 void
231 chap_auth_with_peer(int unit, char *our_name, int digest_code)
232 {
233         struct chap_client_state *cs = &client;
234         struct chap_digest_type *dp;
235
236         if (cs->flags & AUTH_STARTED) {
237                 error("CHAP: authentication with peer already started!");
238                 return;
239         }
240         for (dp = chap_digests; dp != NULL; dp = dp->next)
241                 if (dp->code == digest_code)
242                         break;
243         if (dp == NULL)
244                 fatal("CHAP digest 0x%x requested but not available",
245                       digest_code);
246
247         cs->digest = dp;
248         cs->name = our_name;
249         cs->flags |= AUTH_STARTED | TIMEOUT_PENDING;
250         TIMEOUT(chap_client_timeout, cs, chap_client_timeout_time);
251 }
252
253 /*
254  * chap_server_timeout - It's time to send another challenge to the peer.
255  * This could be either a retransmission of a previous challenge,
256  * or a new challenge to start re-authentication.
257  */
258 static void
259 chap_server_timeout(void *arg)
260 {
261         struct chap_server_state *ss = arg;
262
263         ss->flags &= ~TIMEOUT_PENDING;
264         if ((ss->flags & CHALLENGE_VALID) == 0) {
265                 ss->challenge_xmits = 0;
266                 chap_generate_challenge(ss);
267                 ss->flags |= CHALLENGE_VALID;
268         } else if (ss->challenge_xmits >= chap_max_transmits) {
269                 ss->flags &= ~CHALLENGE_VALID;
270                 ss->flags |= AUTH_DONE | AUTH_FAILED;
271                 auth_peer_fail(0, PPP_CHAP);
272                 return;
273         }
274
275         output(0, ss->challenge, ss->challenge_pktlen);
276         ++ss->challenge_xmits;
277         ss->flags |= TIMEOUT_PENDING;
278         TIMEOUT(chap_server_timeout, arg, chap_server_timeout_time);
279 }
280
281 /* chap_client_timeout - Authentication with peer timed out. */
282 static void
283 chap_client_timeout(void *arg)
284 {
285         struct chap_client_state *cs = arg;
286
287         cs->flags &= ~TIMEOUT_PENDING;
288         cs->flags |= AUTH_DONE | AUTH_FAILED;
289         error("CHAP authentication timed out");
290         auth_withpeer_fail(0, PPP_CHAP);
291 }
292
293 /*
294  * chap_generate_challenge - generate a challenge string and format
295  * the challenge packet in ss->challenge_pkt.
296  */
297 static void
298 chap_generate_challenge(struct chap_server_state *ss)
299 {
300         int clen = 1, nlen, len;
301         unsigned char *p;
302
303         p = ss->challenge;
304         MAKEHEADER(p, PPP_CHAP);
305         p += CHAP_HDRLEN;
306         ss->digest->generate_challenge(p);
307         clen = *p;
308         nlen = strlen(ss->name);
309         memcpy(p + 1 + clen, ss->name, nlen);
310
311         len = CHAP_HDRLEN + 1 + clen + nlen;
312         ss->challenge_pktlen = PPP_HDRLEN + len;
313
314         p = ss->challenge + PPP_HDRLEN;
315         p[0] = CHAP_CHALLENGE;
316         p[1] = ++ss->id;
317         p[2] = len >> 8;
318         p[3] = len;
319 }
320
321 /*
322  * chap_handle_response - check the response to our challenge.
323  */
324 static void
325 chap_handle_response(struct chap_server_state *ss, int id,
326                      unsigned char *pkt, int len)
327 {
328         int response_len, ok, mlen;
329         unsigned char *response, *p;
330         char *name = NULL;      /* initialized to shut gcc up */
331         int (*verifier)(char *, char *, int, struct chap_digest_type *,
332                 unsigned char *, unsigned char *, char *, int);
333         char rname[MAXNAMELEN+1];
334
335         if ((ss->flags & LOWERUP) == 0)
336                 return;
337         if (id != ss->challenge[PPP_HDRLEN+1] || len < 2)
338                 return;
339         if (ss->flags & CHALLENGE_VALID) {
340                 response = pkt;
341                 GETCHAR(response_len, pkt);
342                 len -= response_len + 1;        /* length of name */
343                 name = (char *)pkt + response_len;
344                 if (len < 0)
345                         return;
346
347                 if (ss->flags & TIMEOUT_PENDING) {
348                         ss->flags &= ~TIMEOUT_PENDING;
349                         UNTIMEOUT(chap_server_timeout, ss);
350                 }
351
352                 if (explicit_remote) {
353                         name = remote_name;
354                 } else {
355                         /* Null terminate and clean remote name. */
356                         slprintf(rname, sizeof(rname), "%.*v", len, name);
357                         name = rname;
358                 }
359
360                 if (chap_verify_hook)
361                         verifier = chap_verify_hook;
362                 else
363                         verifier = chap_verify_response;
364                 ok = (*verifier)(name, ss->name, id, ss->digest,
365                                  ss->challenge + PPP_HDRLEN + CHAP_HDRLEN,
366                                  response, ss->message, sizeof(ss->message));
367                 if (!ok || !auth_number()) {
368                         ss->flags |= AUTH_FAILED;
369                         warn("Peer %q failed CHAP authentication", name);
370                 }
371         } else if ((ss->flags & AUTH_DONE) == 0)
372                 return;
373
374         /* send the response */
375         p = outpacket_buf;
376         MAKEHEADER(p, PPP_CHAP);
377         mlen = strlen(ss->message);
378         len = CHAP_HDRLEN + mlen;
379         p[0] = (ss->flags & AUTH_FAILED)? CHAP_FAILURE: CHAP_SUCCESS;
380         p[1] = id;
381         p[2] = len >> 8;
382         p[3] = len;
383         if (mlen > 0)
384                 memcpy(p + CHAP_HDRLEN, ss->message, mlen);
385         output(0, outpacket_buf, PPP_HDRLEN + len);
386
387         if (ss->flags & CHALLENGE_VALID) {
388                 ss->flags &= ~CHALLENGE_VALID;
389                 if (!(ss->flags & AUTH_DONE) && !(ss->flags & AUTH_FAILED)) {
390                     /*
391                      * Auth is OK, so now we need to check session restrictions
392                      * to ensure everything is OK, but only if we used a
393                      * plugin, and only if we're configured to check.  This
394                      * allows us to do PAM checks on PPP servers that
395                      * authenticate against ActiveDirectory, and use AD for
396                      * account info (like when using Winbind integrated with
397                      * PAM).
398                      */
399                     if (session_mgmt &&
400                         session_check(name, NULL, devnam, NULL) == 0) {
401                         ss->flags |= AUTH_FAILED;
402                         warn("Peer %q failed CHAP Session verification", name);
403                     }
404                 }
405                 if (ss->flags & AUTH_FAILED) {
406                         auth_peer_fail(0, PPP_CHAP);
407                 } else {
408                         if ((ss->flags & AUTH_DONE) == 0)
409                                 auth_peer_success(0, PPP_CHAP,
410                                                   ss->digest->code,
411                                                   name, strlen(name));
412                         if (chap_rechallenge_time) {
413                                 ss->flags |= TIMEOUT_PENDING;
414                                 TIMEOUT(chap_server_timeout, ss,
415                                         chap_rechallenge_time);
416                         }
417                 }
418                 ss->flags |= AUTH_DONE;
419         }
420 }
421
422 /*
423  * chap_verify_response - check whether the peer's response matches
424  * what we think it should be.  Returns 1 if it does (authentication
425  * succeeded), or 0 if it doesn't.
426  */
427 static int
428 chap_verify_response(char *name, char *ourname, int id,
429                      struct chap_digest_type *digest,
430                      unsigned char *challenge, unsigned char *response,
431                      char *message, int message_space)
432 {
433         int ok;
434         unsigned char secret[MAXSECRETLEN];
435         int secret_len;
436
437         /* Get the secret that the peer is supposed to know */
438         if (!get_secret(0, name, ourname, (char *)secret, &secret_len, 1)) {
439                 error("No CHAP secret found for authenticating %q", name);
440                 return 0;
441         }
442
443         ok = digest->verify_response(id, name, secret, secret_len, challenge,
444                                      response, message, message_space);
445         memset(secret, 0, sizeof(secret));
446
447         return ok;
448 }
449
450 /*
451  * chap_respond - Generate and send a response to a challenge.
452  */
453 static void
454 chap_respond(struct chap_client_state *cs, int id,
455              unsigned char *pkt, int len)
456 {
457         int clen, nlen;
458         int secret_len;
459         unsigned char *p;
460         unsigned char response[RESP_MAX_PKTLEN];
461         char rname[MAXNAMELEN+1];
462         char secret[MAXSECRETLEN+1];
463
464         if ((cs->flags & (LOWERUP | AUTH_STARTED)) != (LOWERUP | AUTH_STARTED))
465                 return;         /* not ready */
466         if (len < 2 || len < pkt[0] + 1)
467                 return;         /* too short */
468         clen = pkt[0];
469         nlen = len - (clen + 1);
470
471         /* Null terminate and clean remote name. */
472         slprintf(rname, sizeof(rname), "%.*v", nlen, pkt + clen + 1);
473
474         /* Microsoft doesn't send their name back in the PPP packet */
475         if (explicit_remote || (remote_name[0] != 0 && rname[0] == 0))
476                 strlcpy(rname, remote_name, sizeof(rname));
477
478         /* get secret for authenticating ourselves with the specified host */
479         if (!get_secret(0, cs->name, rname, secret, &secret_len, 0)) {
480                 secret_len = 0; /* assume null secret if can't find one */
481                 warn("No CHAP secret found for authenticating us to %q", rname);
482         }
483
484         p = response;
485         MAKEHEADER(p, PPP_CHAP);
486         p += CHAP_HDRLEN;
487
488         cs->digest->make_response(p, id, cs->name, pkt,
489                                   secret, secret_len, cs->priv);
490         memset(secret, 0, secret_len);
491
492         clen = *p;
493         nlen = strlen(cs->name);
494         memcpy(p + clen + 1, cs->name, nlen);
495
496         p = response + PPP_HDRLEN;
497         len = CHAP_HDRLEN + clen + 1 + nlen;
498         p[0] = CHAP_RESPONSE;
499         p[1] = id;
500         p[2] = len >> 8;
501         p[3] = len;
502
503         output(0, response, PPP_HDRLEN + len);
504 }
505
506 static void
507 chap_handle_status(struct chap_client_state *cs, int code, int id,
508                    unsigned char *pkt, int len)
509 {
510         const char *msg = NULL;
511
512         if ((cs->flags & (AUTH_DONE|AUTH_STARTED|LOWERUP))
513             != (AUTH_STARTED|LOWERUP))
514                 return;
515         cs->flags |= AUTH_DONE;
516
517         UNTIMEOUT(chap_client_timeout, cs);
518         cs->flags &= ~TIMEOUT_PENDING;
519
520         if (code == CHAP_SUCCESS) {
521                 /* used for MS-CHAP v2 mutual auth, yuck */
522                 if (cs->digest->check_success != NULL) {
523                         if (!(*cs->digest->check_success)(id, pkt, len))
524                                 code = CHAP_FAILURE;
525                 } else
526                         msg = "CHAP authentication succeeded";
527         } else {
528                 if (cs->digest->handle_failure != NULL)
529                         (*cs->digest->handle_failure)(pkt, len);
530                 else
531                         msg = "CHAP authentication failed";
532         }
533         if (msg) {
534                 if (len > 0)
535                         info("%s: %.*v", msg, len, pkt);
536                 else
537                         info("%s", msg);
538         }
539         if (code == CHAP_SUCCESS)
540                 auth_withpeer_success(0, PPP_CHAP, cs->digest->code);
541         else {
542                 cs->flags |= AUTH_FAILED;
543                 error("CHAP authentication failed");
544                 auth_withpeer_fail(0, PPP_CHAP);
545         }
546 }
547
548 static void
549 chap_input(int unit, unsigned char *pkt, int pktlen)
550 {
551         struct chap_client_state *cs = &client;
552         struct chap_server_state *ss = &server;
553         unsigned char code, id;
554         int len;
555
556         if (pktlen < CHAP_HDRLEN)
557                 return;
558         GETCHAR(code, pkt);
559         GETCHAR(id, pkt);
560         GETSHORT(len, pkt);
561         if (len < CHAP_HDRLEN || len > pktlen)
562                 return;
563         len -= CHAP_HDRLEN;
564
565         switch (code) {
566         case CHAP_CHALLENGE:
567                 chap_respond(cs, id, pkt, len);
568                 break;
569         case CHAP_RESPONSE:
570                 chap_handle_response(ss, id, pkt, len);
571                 break;
572         case CHAP_FAILURE:
573         case CHAP_SUCCESS:
574                 chap_handle_status(cs, code, id, pkt, len);
575                 break;
576         }
577 }
578
579 static void
580 chap_protrej(int unit)
581 {
582         struct chap_client_state *cs = &client;
583         struct chap_server_state *ss = &server;
584
585         if (ss->flags & TIMEOUT_PENDING) {
586                 ss->flags &= ~TIMEOUT_PENDING;
587                 UNTIMEOUT(chap_server_timeout, ss);
588         }
589         if (ss->flags & AUTH_STARTED) {
590                 ss->flags = 0;
591                 auth_peer_fail(0, PPP_CHAP);
592         }
593         if ((cs->flags & (AUTH_STARTED|AUTH_DONE)) == AUTH_STARTED) {
594                 cs->flags &= ~AUTH_STARTED;
595                 error("CHAP authentication failed due to protocol-reject");
596                 auth_withpeer_fail(0, PPP_CHAP);
597         }
598 }
599
600 /*
601  * chap_print_pkt - print the contents of a CHAP packet.
602  */
603 static char *chap_code_names[] = {
604         "Challenge", "Response", "Success", "Failure"
605 };
606
607 static int
608 chap_print_pkt(unsigned char *p, int plen,
609                void (*printer)(void *, char *, ...), void *arg)
610 {
611         int code, id, len;
612         int clen, nlen;
613         unsigned char x;
614
615         if (plen < CHAP_HDRLEN)
616                 return 0;
617         GETCHAR(code, p);
618         GETCHAR(id, p);
619         GETSHORT(len, p);
620         if (len < CHAP_HDRLEN || len > plen)
621                 return 0;
622
623         if (code >= 1 && code <= sizeof(chap_code_names) / sizeof(char *))
624                 printer(arg, " %s", chap_code_names[code-1]);
625         else
626                 printer(arg, " code=0x%x", code);
627         printer(arg, " id=0x%x", id);
628         len -= CHAP_HDRLEN;
629         switch (code) {
630         case CHAP_CHALLENGE:
631         case CHAP_RESPONSE:
632                 if (len < 1)
633                         break;
634                 clen = p[0];
635                 if (len < clen + 1)
636                         break;
637                 ++p;
638                 nlen = len - clen - 1;
639                 printer(arg, " <");
640                 for (; clen > 0; --clen) {
641                         GETCHAR(x, p);
642                         printer(arg, "%.2x", x);
643                 }
644                 printer(arg, ">, name = ");
645                 print_string((char *)p, nlen, printer, arg);
646                 break;
647         case CHAP_FAILURE:
648         case CHAP_SUCCESS:
649                 printer(arg, " ");
650                 print_string((char *)p, len, printer, arg);
651                 break;
652         default:
653                 for (clen = len; clen > 0; --clen) {
654                         GETCHAR(x, p);
655                         printer(arg, " %.2x", x);
656                 }
657         }
658
659         return len + CHAP_HDRLEN;
660 }
661
662 struct protent chap_protent = {
663         PPP_CHAP,
664         chap_init,
665         chap_input,
666         chap_protrej,
667         chap_lowerup,
668         chap_lowerdown,
669         NULL,           /* open */
670         NULL,           /* close */
671         chap_print_pkt,
672         NULL,           /* datainput */
673         1,              /* enabled_flag */
674         "CHAP",         /* name */
675         NULL,           /* data_name */
676         chap_option_list,
677         NULL,           /* check_options */
678 };