2 * chap-new.c - New CHAP implementation.
4 * Copyright (c) 2003 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
17 * 3. Redistributions of any form whatsoever must retain the following
19 * "This product includes software developed by Paul Mackerras
20 * <paulus@samba.org>".
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.
31 #define RCSID "$Id: chap-new.c,v 1.9 2007/06/19 02:08:35 carlsonj Exp $"
42 #define MDTYPE_ALL (MDTYPE_MICROSOFT_V2 | MDTYPE_MICROSOFT | MDTYPE_MD5)
44 #define MDTYPE_ALL (MDTYPE_MD5)
47 int chap_mdtype_all = MDTYPE_ALL;
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;
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;
64 * Command-line options.
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 },
81 static struct chap_client_state {
84 struct chap_digest_type *digest;
85 unsigned char priv[64]; /* private area for digest's use */
89 * These limits apply to challenge and response packets we send.
90 * The +4 is the +1 that we actually need rounded up.
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)
95 static struct chap_server_state {
99 struct chap_digest_type *digest;
101 int challenge_pktlen;
102 unsigned char challenge[CHAL_MAX_PKTLEN];
106 /* Values for flags in chap_client_state and chap_server_state */
108 #define AUTH_STARTED 2
110 #define AUTH_FAILED 8
111 #define TIMEOUT_PENDING 0x10
112 #define CHALLENGE_VALID 0x20
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);
138 /* List of digest types that we know about */
139 static struct chap_digest_type *chap_digests;
142 * chap_init - reset to initial state.
147 memset(&client, 0, sizeof(client));
148 memset(&server, 0, sizeof(server));
157 * Add a new digest type to the list.
160 chap_register_digest(struct chap_digest_type *dp)
162 dp->next = chap_digests;
167 * chap_lowerup - we can start doing stuff now.
170 chap_lowerup(int unit)
172 struct chap_client_state *cs = &client;
173 struct chap_server_state *ss = &server;
175 cs->flags |= LOWERUP;
176 ss->flags |= LOWERUP;
177 if (ss->flags & AUTH_STARTED)
178 chap_server_timeout(ss);
182 chap_lowerdown(int unit)
184 struct chap_client_state *cs = &client;
185 struct chap_server_state *ss = &server;
187 if (cs->flags & TIMEOUT_PENDING)
188 UNTIMEOUT(chap_client_timeout, cs);
190 if (ss->flags & TIMEOUT_PENDING)
191 UNTIMEOUT(chap_server_timeout, ss);
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.
201 chap_auth_peer(int unit, char *our_name, int digest_code)
203 struct chap_server_state *ss = &server;
204 struct chap_digest_type *dp;
206 if (ss->flags & AUTH_STARTED) {
207 error("CHAP: peer authentication already started!");
210 for (dp = chap_digests; dp != NULL; dp = dp->next)
211 if (dp->code == digest_code)
214 fatal("CHAP digest 0x%x requested but not available",
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);
227 * chap_auth_with_peer - Prepare to authenticate ourselves to the peer.
228 * There isn't much to do until we receive a challenge.
231 chap_auth_with_peer(int unit, char *our_name, int digest_code)
233 struct chap_client_state *cs = &client;
234 struct chap_digest_type *dp;
236 if (cs->flags & AUTH_STARTED) {
237 error("CHAP: authentication with peer already started!");
240 for (dp = chap_digests; dp != NULL; dp = dp->next)
241 if (dp->code == digest_code)
244 fatal("CHAP digest 0x%x requested but not available",
249 cs->flags |= AUTH_STARTED | TIMEOUT_PENDING;
250 TIMEOUT(chap_client_timeout, cs, chap_client_timeout_time);
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.
259 chap_server_timeout(void *arg)
261 struct chap_server_state *ss = arg;
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);
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);
281 /* chap_client_timeout - Authentication with peer timed out. */
283 chap_client_timeout(void *arg)
285 struct chap_client_state *cs = arg;
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);
294 * chap_generate_challenge - generate a challenge string and format
295 * the challenge packet in ss->challenge_pkt.
298 chap_generate_challenge(struct chap_server_state *ss)
300 int clen = 1, nlen, len;
304 MAKEHEADER(p, PPP_CHAP);
306 ss->digest->generate_challenge(p);
308 nlen = strlen(ss->name);
309 memcpy(p + 1 + clen, ss->name, nlen);
311 len = CHAP_HDRLEN + 1 + clen + nlen;
312 ss->challenge_pktlen = PPP_HDRLEN + len;
314 p = ss->challenge + PPP_HDRLEN;
315 p[0] = CHAP_CHALLENGE;
322 * chap_handle_response - check the response to our challenge.
325 chap_handle_response(struct chap_server_state *ss, int id,
326 unsigned char *pkt, int len)
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];
335 if ((ss->flags & LOWERUP) == 0)
337 if (id != ss->challenge[PPP_HDRLEN+1] || len < 2)
339 if (ss->flags & CHALLENGE_VALID) {
341 GETCHAR(response_len, pkt);
342 len -= response_len + 1; /* length of name */
343 name = (char *)pkt + response_len;
347 if (ss->flags & TIMEOUT_PENDING) {
348 ss->flags &= ~TIMEOUT_PENDING;
349 UNTIMEOUT(chap_server_timeout, ss);
352 if (explicit_remote) {
355 /* Null terminate and clean remote name. */
356 slprintf(rname, sizeof(rname), "%.*v", len, name);
360 if (chap_verify_hook)
361 verifier = chap_verify_hook;
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);
371 } else if ((ss->flags & AUTH_DONE) == 0)
374 /* send the response */
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;
384 memcpy(p + CHAP_HDRLEN, ss->message, mlen);
385 output(0, outpacket_buf, PPP_HDRLEN + len);
387 if (ss->flags & CHALLENGE_VALID) {
388 ss->flags &= ~CHALLENGE_VALID;
389 if (!(ss->flags & AUTH_DONE) && !(ss->flags & AUTH_FAILED)) {
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
400 session_check(name, NULL, devnam, NULL) == 0) {
401 ss->flags |= AUTH_FAILED;
402 warn("Peer %q failed CHAP Session verification", name);
405 if (ss->flags & AUTH_FAILED) {
406 auth_peer_fail(0, PPP_CHAP);
408 if ((ss->flags & AUTH_DONE) == 0)
409 auth_peer_success(0, PPP_CHAP,
412 if (chap_rechallenge_time) {
413 ss->flags |= TIMEOUT_PENDING;
414 TIMEOUT(chap_server_timeout, ss,
415 chap_rechallenge_time);
418 ss->flags |= AUTH_DONE;
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.
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)
434 unsigned char secret[MAXSECRETLEN];
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);
443 ok = digest->verify_response(id, name, secret, secret_len, challenge,
444 response, message, message_space);
445 memset(secret, 0, sizeof(secret));
451 * chap_respond - Generate and send a response to a challenge.
454 chap_respond(struct chap_client_state *cs, int id,
455 unsigned char *pkt, int len)
460 unsigned char response[RESP_MAX_PKTLEN];
461 char rname[MAXNAMELEN+1];
462 char secret[MAXSECRETLEN+1];
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 */
469 nlen = len - (clen + 1);
471 /* Null terminate and clean remote name. */
472 slprintf(rname, sizeof(rname), "%.*v", nlen, pkt + clen + 1);
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));
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);
485 MAKEHEADER(p, PPP_CHAP);
488 cs->digest->make_response(p, id, cs->name, pkt,
489 secret, secret_len, cs->priv);
490 memset(secret, 0, secret_len);
493 nlen = strlen(cs->name);
494 memcpy(p + clen + 1, cs->name, nlen);
496 p = response + PPP_HDRLEN;
497 len = CHAP_HDRLEN + clen + 1 + nlen;
498 p[0] = CHAP_RESPONSE;
503 output(0, response, PPP_HDRLEN + len);
507 chap_handle_status(struct chap_client_state *cs, int code, int id,
508 unsigned char *pkt, int len)
510 const char *msg = NULL;
512 if ((cs->flags & (AUTH_DONE|AUTH_STARTED|LOWERUP))
513 != (AUTH_STARTED|LOWERUP))
515 cs->flags |= AUTH_DONE;
517 UNTIMEOUT(chap_client_timeout, cs);
518 cs->flags &= ~TIMEOUT_PENDING;
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))
526 msg = "CHAP authentication succeeded";
528 if (cs->digest->handle_failure != NULL)
529 (*cs->digest->handle_failure)(pkt, len);
531 msg = "CHAP authentication failed";
535 info("%s: %.*v", msg, len, pkt);
539 if (code == CHAP_SUCCESS)
540 auth_withpeer_success(0, PPP_CHAP, cs->digest->code);
542 cs->flags |= AUTH_FAILED;
543 error("CHAP authentication failed");
544 auth_withpeer_fail(0, PPP_CHAP);
549 chap_input(int unit, unsigned char *pkt, int pktlen)
551 struct chap_client_state *cs = &client;
552 struct chap_server_state *ss = &server;
553 unsigned char code, id;
556 if (pktlen < CHAP_HDRLEN)
561 if (len < CHAP_HDRLEN || len > pktlen)
567 chap_respond(cs, id, pkt, len);
570 chap_handle_response(ss, id, pkt, len);
574 chap_handle_status(cs, code, id, pkt, len);
580 chap_protrej(int unit)
582 struct chap_client_state *cs = &client;
583 struct chap_server_state *ss = &server;
585 if (ss->flags & TIMEOUT_PENDING) {
586 ss->flags &= ~TIMEOUT_PENDING;
587 UNTIMEOUT(chap_server_timeout, ss);
589 if (ss->flags & AUTH_STARTED) {
591 auth_peer_fail(0, PPP_CHAP);
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);
601 * chap_print_pkt - print the contents of a CHAP packet.
603 static char *chap_code_names[] = {
604 "Challenge", "Response", "Success", "Failure"
608 chap_print_pkt(unsigned char *p, int plen,
609 void (*printer)(void *, char *, ...), void *arg)
615 if (plen < CHAP_HDRLEN)
620 if (len < CHAP_HDRLEN || len > plen)
623 if (code >= 1 && code <= sizeof(chap_code_names) / sizeof(char *))
624 printer(arg, " %s", chap_code_names[code-1]);
626 printer(arg, " code=0x%x", code);
627 printer(arg, " id=0x%x", id);
638 nlen = len - clen - 1;
640 for (; clen > 0; --clen) {
642 printer(arg, "%.2x", x);
644 printer(arg, ">, name = ");
645 print_string((char *)p, nlen, printer, arg);
650 print_string((char *)p, len, printer, arg);
653 for (clen = len; clen > 0; --clen) {
655 printer(arg, " %.2x", x);
659 return len + CHAP_HDRLEN;
662 struct protent chap_protent = {
672 NULL, /* datainput */
673 1, /* enabled_flag */
675 NULL, /* data_name */
677 NULL, /* check_options */