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;
62 int chapms_strip_domain = 0;
65 * Command-line options.
67 static option_t chap_option_list[] = {
68 { "chap-restart", o_int, &chap_server_timeout_time,
69 "Set timeout for CHAP (as server)", OPT_PRIO },
70 { "chap-max-challenge", o_int, &chap_max_transmits,
71 "Set max #xmits for challenge", OPT_PRIO },
72 { "chap-interval", o_int, &chap_rechallenge_time,
73 "Set interval for rechallenge", OPT_PRIO },
74 { "chap-timeout", o_int, &chap_client_timeout_time,
75 "Set timeout for CHAP (as client)", OPT_PRIO },
76 { "chapms-strip-domain", o_bool, &chapms_strip_domain,
77 "Strip the domain prefix before the Username", 1 },
84 static struct chap_client_state {
87 struct chap_digest_type *digest;
88 unsigned char priv[64]; /* private area for digest's use */
92 * These limits apply to challenge and response packets we send.
93 * The +4 is the +1 that we actually need rounded up.
95 #define CHAL_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_CHALLENGE_LEN + MAXNAMELEN)
96 #define RESP_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_RESPONSE_LEN + MAXNAMELEN)
98 static struct chap_server_state {
102 struct chap_digest_type *digest;
104 int challenge_pktlen;
105 unsigned char challenge[CHAL_MAX_PKTLEN];
109 /* Values for flags in chap_client_state and chap_server_state */
111 #define AUTH_STARTED 2
113 #define AUTH_FAILED 8
114 #define TIMEOUT_PENDING 0x10
115 #define CHALLENGE_VALID 0x20
120 static void chap_init(int unit);
121 static void chap_lowerup(int unit);
122 static void chap_lowerdown(int unit);
123 static void chap_server_timeout(void *arg);
124 static void chap_client_timeout(void *arg);
125 static void chap_generate_challenge(struct chap_server_state *ss);
126 static void chap_handle_response(struct chap_server_state *ss, int code,
127 unsigned char *pkt, int len);
128 static int chap_verify_response(char *name, char *ourname, int id,
129 struct chap_digest_type *digest,
130 unsigned char *challenge, unsigned char *response,
131 char *message, int message_space);
132 static void chap_respond(struct chap_client_state *cs, int id,
133 unsigned char *pkt, int len);
134 static void chap_handle_status(struct chap_client_state *cs, int code, int id,
135 unsigned char *pkt, int len);
136 static void chap_protrej(int unit);
137 static void chap_input(int unit, unsigned char *pkt, int pktlen);
138 static int chap_print_pkt(unsigned char *p, int plen,
139 void (*printer)(void *, char *, ...), void *arg);
141 /* List of digest types that we know about */
142 static struct chap_digest_type *chap_digests;
145 * chap_init - reset to initial state.
150 memset(&client, 0, sizeof(client));
151 memset(&server, 0, sizeof(server));
160 * Add a new digest type to the list.
163 chap_register_digest(struct chap_digest_type *dp)
165 dp->next = chap_digests;
170 * Lookup a digest type by code
172 struct chap_digest_type *
173 chap_find_digest(int digest_code) {
174 struct chap_digest_type *dp = NULL;
175 for (dp = chap_digests; dp != NULL; dp = dp->next)
176 if (dp->code == digest_code)
182 * chap_lowerup - we can start doing stuff now.
185 chap_lowerup(int unit)
187 struct chap_client_state *cs = &client;
188 struct chap_server_state *ss = &server;
190 cs->flags |= LOWERUP;
191 ss->flags |= LOWERUP;
192 if (ss->flags & AUTH_STARTED)
193 chap_server_timeout(ss);
197 chap_lowerdown(int unit)
199 struct chap_client_state *cs = &client;
200 struct chap_server_state *ss = &server;
202 if (cs->flags & TIMEOUT_PENDING)
203 UNTIMEOUT(chap_client_timeout, cs);
205 if (ss->flags & TIMEOUT_PENDING)
206 UNTIMEOUT(chap_server_timeout, ss);
211 * chap_auth_peer - Start authenticating the peer.
212 * If the lower layer is already up, we start sending challenges,
213 * otherwise we wait for the lower layer to come up.
216 chap_auth_peer(int unit, char *our_name, int digest_code)
218 struct chap_server_state *ss = &server;
219 struct chap_digest_type *dp;
221 if (ss->flags & AUTH_STARTED) {
222 error("CHAP: peer authentication already started!");
226 dp = chap_find_digest(digest_code);
228 fatal("CHAP digest 0x%x requested but not available",
233 /* Start with a random ID value */
234 ss->id = (unsigned char)(drand48() * 256);
235 ss->flags |= AUTH_STARTED;
236 if (ss->flags & LOWERUP)
237 chap_server_timeout(ss);
241 * chap_auth_with_peer - Prepare to authenticate ourselves to the peer.
242 * There isn't much to do until we receive a challenge.
245 chap_auth_with_peer(int unit, char *our_name, int digest_code)
247 struct chap_client_state *cs = &client;
248 struct chap_digest_type *dp;
250 if (cs->flags & AUTH_STARTED) {
251 error("CHAP: authentication with peer already started!");
254 for (dp = chap_digests; dp != NULL; dp = dp->next)
255 if (dp->code == digest_code)
258 fatal("CHAP digest 0x%x requested but not available",
263 cs->flags |= AUTH_STARTED | TIMEOUT_PENDING;
264 TIMEOUT(chap_client_timeout, cs, chap_client_timeout_time);
268 * chap_server_timeout - It's time to send another challenge to the peer.
269 * This could be either a retransmission of a previous challenge,
270 * or a new challenge to start re-authentication.
273 chap_server_timeout(void *arg)
275 struct chap_server_state *ss = arg;
277 ss->flags &= ~TIMEOUT_PENDING;
278 if ((ss->flags & CHALLENGE_VALID) == 0) {
279 ss->challenge_xmits = 0;
280 chap_generate_challenge(ss);
281 ss->flags |= CHALLENGE_VALID;
282 } else if (ss->challenge_xmits >= chap_max_transmits) {
283 ss->flags &= ~CHALLENGE_VALID;
284 ss->flags |= AUTH_DONE | AUTH_FAILED;
285 auth_peer_fail(0, PPP_CHAP);
289 output(0, ss->challenge, ss->challenge_pktlen);
290 ++ss->challenge_xmits;
291 ss->flags |= TIMEOUT_PENDING;
292 TIMEOUT(chap_server_timeout, arg, chap_server_timeout_time);
295 /* chap_client_timeout - Authentication with peer timed out. */
297 chap_client_timeout(void *arg)
299 struct chap_client_state *cs = arg;
301 cs->flags &= ~TIMEOUT_PENDING;
302 cs->flags |= AUTH_DONE | AUTH_FAILED;
303 error("CHAP authentication timed out");
304 auth_withpeer_fail(0, PPP_CHAP);
308 * chap_generate_challenge - generate a challenge string and format
309 * the challenge packet in ss->challenge_pkt.
312 chap_generate_challenge(struct chap_server_state *ss)
314 int clen = 1, nlen, len;
318 MAKEHEADER(p, PPP_CHAP);
320 ss->digest->generate_challenge(p);
322 nlen = strlen(ss->name);
323 memcpy(p + 1 + clen, ss->name, nlen);
325 len = CHAP_HDRLEN + 1 + clen + nlen;
326 ss->challenge_pktlen = PPP_HDRLEN + len;
328 p = ss->challenge + PPP_HDRLEN;
329 p[0] = CHAP_CHALLENGE;
336 * chap_handle_response - check the response to our challenge.
339 chap_handle_response(struct chap_server_state *ss, int id,
340 unsigned char *pkt, int len)
342 int response_len, ok, mlen;
343 unsigned char *response, *p;
344 char *name = NULL; /* initialized to shut gcc up */
345 int (*verifier)(char *, char *, int, struct chap_digest_type *,
346 unsigned char *, unsigned char *, char *, int);
347 char rname[MAXNAMELEN+1];
349 if ((ss->flags & LOWERUP) == 0)
351 if (id != ss->challenge[PPP_HDRLEN+1] || len < 2)
353 if (ss->flags & CHALLENGE_VALID) {
355 GETCHAR(response_len, pkt);
356 len -= response_len + 1; /* length of name */
357 name = (char *)pkt + response_len;
361 if (ss->flags & TIMEOUT_PENDING) {
362 ss->flags &= ~TIMEOUT_PENDING;
363 UNTIMEOUT(chap_server_timeout, ss);
366 if (explicit_remote) {
369 /* Null terminate and clean remote name. */
370 slprintf(rname, sizeof(rname), "%.*v", len, name);
373 /* strip the MS domain name */
374 if (chapms_strip_domain && strrchr(rname, '\\')) {
375 char tmp[MAXNAMELEN+1];
377 strcpy(tmp, strrchr(rname, '\\') + 1);
382 if (chap_verify_hook)
383 verifier = chap_verify_hook;
385 verifier = chap_verify_response;
386 ok = (*verifier)(name, ss->name, id, ss->digest,
387 ss->challenge + PPP_HDRLEN + CHAP_HDRLEN,
388 response, ss->message, sizeof(ss->message));
389 if (!ok || !auth_number()) {
390 ss->flags |= AUTH_FAILED;
391 warn("Peer %q failed CHAP authentication", name);
393 } else if ((ss->flags & AUTH_DONE) == 0)
396 /* send the response */
398 MAKEHEADER(p, PPP_CHAP);
399 mlen = strlen(ss->message);
400 len = CHAP_HDRLEN + mlen;
401 p[0] = (ss->flags & AUTH_FAILED)? CHAP_FAILURE: CHAP_SUCCESS;
406 memcpy(p + CHAP_HDRLEN, ss->message, mlen);
407 output(0, outpacket_buf, PPP_HDRLEN + len);
409 if (ss->flags & CHALLENGE_VALID) {
410 ss->flags &= ~CHALLENGE_VALID;
411 if (!(ss->flags & AUTH_DONE) && !(ss->flags & AUTH_FAILED)) {
413 * Auth is OK, so now we need to check session restrictions
414 * to ensure everything is OK, but only if we used a
415 * plugin, and only if we're configured to check. This
416 * allows us to do PAM checks on PPP servers that
417 * authenticate against ActiveDirectory, and use AD for
418 * account info (like when using Winbind integrated with
422 session_check(name, NULL, devnam, NULL) == 0) {
423 ss->flags |= AUTH_FAILED;
424 warn("Peer %q failed CHAP Session verification", name);
427 if (ss->flags & AUTH_FAILED) {
428 auth_peer_fail(0, PPP_CHAP);
430 if ((ss->flags & AUTH_DONE) == 0)
431 auth_peer_success(0, PPP_CHAP,
434 if (chap_rechallenge_time) {
435 ss->flags |= TIMEOUT_PENDING;
436 TIMEOUT(chap_server_timeout, ss,
437 chap_rechallenge_time);
440 ss->flags |= AUTH_DONE;
445 * chap_verify_response - check whether the peer's response matches
446 * what we think it should be. Returns 1 if it does (authentication
447 * succeeded), or 0 if it doesn't.
450 chap_verify_response(char *name, char *ourname, int id,
451 struct chap_digest_type *digest,
452 unsigned char *challenge, unsigned char *response,
453 char *message, int message_space)
456 unsigned char secret[MAXSECRETLEN];
459 /* Get the secret that the peer is supposed to know */
460 if (!get_secret(0, name, ourname, (char *)secret, &secret_len, 1)) {
461 error("No CHAP secret found for authenticating %q", name);
465 ok = digest->verify_response(id, name, secret, secret_len, challenge,
466 response, message, message_space);
467 memset(secret, 0, sizeof(secret));
473 * chap_respond - Generate and send a response to a challenge.
476 chap_respond(struct chap_client_state *cs, int id,
477 unsigned char *pkt, int len)
482 unsigned char response[RESP_MAX_PKTLEN];
483 char rname[MAXNAMELEN+1];
484 char secret[MAXSECRETLEN+1];
486 if ((cs->flags & (LOWERUP | AUTH_STARTED)) != (LOWERUP | AUTH_STARTED))
487 return; /* not ready */
488 if (len < 2 || len < pkt[0] + 1)
489 return; /* too short */
491 nlen = len - (clen + 1);
493 /* Null terminate and clean remote name. */
494 slprintf(rname, sizeof(rname), "%.*v", nlen, pkt + clen + 1);
496 /* Microsoft doesn't send their name back in the PPP packet */
497 if (explicit_remote || (remote_name[0] != 0 && rname[0] == 0))
498 strlcpy(rname, remote_name, sizeof(rname));
500 /* get secret for authenticating ourselves with the specified host */
501 if (!get_secret(0, cs->name, rname, secret, &secret_len, 0)) {
502 secret_len = 0; /* assume null secret if can't find one */
503 warn("No CHAP secret found for authenticating us to %q", rname);
507 MAKEHEADER(p, PPP_CHAP);
510 cs->digest->make_response(p, id, cs->name, pkt,
511 secret, secret_len, cs->priv);
512 memset(secret, 0, secret_len);
515 nlen = strlen(cs->name);
516 memcpy(p + clen + 1, cs->name, nlen);
518 p = response + PPP_HDRLEN;
519 len = CHAP_HDRLEN + clen + 1 + nlen;
520 p[0] = CHAP_RESPONSE;
525 output(0, response, PPP_HDRLEN + len);
529 chap_handle_status(struct chap_client_state *cs, int code, int id,
530 unsigned char *pkt, int len)
532 const char *msg = NULL;
534 if ((cs->flags & (AUTH_DONE|AUTH_STARTED|LOWERUP))
535 != (AUTH_STARTED|LOWERUP))
537 cs->flags |= AUTH_DONE;
539 UNTIMEOUT(chap_client_timeout, cs);
540 cs->flags &= ~TIMEOUT_PENDING;
542 if (code == CHAP_SUCCESS) {
543 /* used for MS-CHAP v2 mutual auth, yuck */
544 if (cs->digest->check_success != NULL) {
545 if (!(*cs->digest->check_success)(id, pkt, len))
548 msg = "CHAP authentication succeeded";
550 if (cs->digest->handle_failure != NULL)
551 (*cs->digest->handle_failure)(pkt, len);
553 msg = "CHAP authentication failed";
557 info("%s: %.*v", msg, len, pkt);
561 if (code == CHAP_SUCCESS)
562 auth_withpeer_success(0, PPP_CHAP, cs->digest->code);
564 cs->flags |= AUTH_FAILED;
565 error("CHAP authentication failed");
566 auth_withpeer_fail(0, PPP_CHAP);
571 chap_input(int unit, unsigned char *pkt, int pktlen)
573 struct chap_client_state *cs = &client;
574 struct chap_server_state *ss = &server;
575 unsigned char code, id;
578 if (pktlen < CHAP_HDRLEN)
583 if (len < CHAP_HDRLEN || len > pktlen)
589 chap_respond(cs, id, pkt, len);
592 chap_handle_response(ss, id, pkt, len);
596 chap_handle_status(cs, code, id, pkt, len);
602 chap_protrej(int unit)
604 struct chap_client_state *cs = &client;
605 struct chap_server_state *ss = &server;
607 if (ss->flags & TIMEOUT_PENDING) {
608 ss->flags &= ~TIMEOUT_PENDING;
609 UNTIMEOUT(chap_server_timeout, ss);
611 if (ss->flags & AUTH_STARTED) {
613 auth_peer_fail(0, PPP_CHAP);
615 if ((cs->flags & (AUTH_STARTED|AUTH_DONE)) == AUTH_STARTED) {
616 cs->flags &= ~AUTH_STARTED;
617 error("CHAP authentication failed due to protocol-reject");
618 auth_withpeer_fail(0, PPP_CHAP);
623 * chap_print_pkt - print the contents of a CHAP packet.
625 static char *chap_code_names[] = {
626 "Challenge", "Response", "Success", "Failure"
630 chap_print_pkt(unsigned char *p, int plen,
631 void (*printer)(void *, char *, ...), void *arg)
637 if (plen < CHAP_HDRLEN)
642 if (len < CHAP_HDRLEN || len > plen)
645 if (code >= 1 && code <= sizeof(chap_code_names) / sizeof(char *))
646 printer(arg, " %s", chap_code_names[code-1]);
648 printer(arg, " code=0x%x", code);
649 printer(arg, " id=0x%x", id);
660 nlen = len - clen - 1;
662 for (; clen > 0; --clen) {
664 printer(arg, "%.2x", x);
666 printer(arg, ">, name = ");
667 print_string((char *)p, nlen, printer, arg);
672 print_string((char *)p, len, printer, arg);
675 for (clen = len; clen > 0; --clen) {
677 printer(arg, " %.2x", x);
681 return len + CHAP_HDRLEN;
684 struct protent chap_protent = {
694 NULL, /* datainput */
695 1, /* enabled_flag */
697 NULL, /* data_name */
699 NULL, /* check_options */