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!");
225 for (dp = chap_digests; dp != NULL; dp = dp->next)
226 if (dp->code == digest_code)
229 fatal("CHAP digest 0x%x requested but not available",
234 /* Start with a random ID value */
235 ss->id = (unsigned char)(drand48() * 256);
236 ss->flags |= AUTH_STARTED;
237 if (ss->flags & LOWERUP)
238 chap_server_timeout(ss);
242 * chap_auth_with_peer - Prepare to authenticate ourselves to the peer.
243 * There isn't much to do until we receive a challenge.
246 chap_auth_with_peer(int unit, char *our_name, int digest_code)
248 struct chap_client_state *cs = &client;
249 struct chap_digest_type *dp;
251 if (cs->flags & AUTH_STARTED) {
252 error("CHAP: authentication with peer already started!");
255 for (dp = chap_digests; dp != NULL; dp = dp->next)
256 if (dp->code == digest_code)
259 fatal("CHAP digest 0x%x requested but not available",
264 cs->flags |= AUTH_STARTED | TIMEOUT_PENDING;
265 TIMEOUT(chap_client_timeout, cs, chap_client_timeout_time);
269 * chap_server_timeout - It's time to send another challenge to the peer.
270 * This could be either a retransmission of a previous challenge,
271 * or a new challenge to start re-authentication.
274 chap_server_timeout(void *arg)
276 struct chap_server_state *ss = arg;
278 ss->flags &= ~TIMEOUT_PENDING;
279 if ((ss->flags & CHALLENGE_VALID) == 0) {
280 ss->challenge_xmits = 0;
281 chap_generate_challenge(ss);
282 ss->flags |= CHALLENGE_VALID;
283 } else if (ss->challenge_xmits >= chap_max_transmits) {
284 ss->flags &= ~CHALLENGE_VALID;
285 ss->flags |= AUTH_DONE | AUTH_FAILED;
286 auth_peer_fail(0, PPP_CHAP);
290 output(0, ss->challenge, ss->challenge_pktlen);
291 ++ss->challenge_xmits;
292 ss->flags |= TIMEOUT_PENDING;
293 TIMEOUT(chap_server_timeout, arg, chap_server_timeout_time);
296 /* chap_client_timeout - Authentication with peer timed out. */
298 chap_client_timeout(void *arg)
300 struct chap_client_state *cs = arg;
302 cs->flags &= ~TIMEOUT_PENDING;
303 cs->flags |= AUTH_DONE | AUTH_FAILED;
304 error("CHAP authentication timed out");
305 auth_withpeer_fail(0, PPP_CHAP);
309 * chap_generate_challenge - generate a challenge string and format
310 * the challenge packet in ss->challenge_pkt.
313 chap_generate_challenge(struct chap_server_state *ss)
315 int clen = 1, nlen, len;
319 MAKEHEADER(p, PPP_CHAP);
321 ss->digest->generate_challenge(p);
323 nlen = strlen(ss->name);
324 memcpy(p + 1 + clen, ss->name, nlen);
326 len = CHAP_HDRLEN + 1 + clen + nlen;
327 ss->challenge_pktlen = PPP_HDRLEN + len;
329 p = ss->challenge + PPP_HDRLEN;
330 p[0] = CHAP_CHALLENGE;
337 * chap_handle_response - check the response to our challenge.
340 chap_handle_response(struct chap_server_state *ss, int id,
341 unsigned char *pkt, int len)
343 int response_len, ok, mlen;
344 unsigned char *response, *p;
345 char *name = NULL; /* initialized to shut gcc up */
346 int (*verifier)(char *, char *, int, struct chap_digest_type *,
347 unsigned char *, unsigned char *, char *, int);
348 char rname[MAXNAMELEN+1];
350 if ((ss->flags & LOWERUP) == 0)
352 if (id != ss->challenge[PPP_HDRLEN+1] || len < 2)
354 if (ss->flags & CHALLENGE_VALID) {
356 GETCHAR(response_len, pkt);
357 len -= response_len + 1; /* length of name */
358 name = (char *)pkt + response_len;
362 if (ss->flags & TIMEOUT_PENDING) {
363 ss->flags &= ~TIMEOUT_PENDING;
364 UNTIMEOUT(chap_server_timeout, ss);
367 if (explicit_remote) {
370 /* Null terminate and clean remote name. */
371 slprintf(rname, sizeof(rname), "%.*v", len, name);
374 /* strip the MS domain name */
375 if (chapms_strip_domain && strrchr(rname, '\\')) {
376 char tmp[MAXNAMELEN+1];
378 strcpy(tmp, strrchr(rname, '\\') + 1);
383 if (chap_verify_hook)
384 verifier = chap_verify_hook;
386 verifier = chap_verify_response;
387 ok = (*verifier)(name, ss->name, id, ss->digest,
388 ss->challenge + PPP_HDRLEN + CHAP_HDRLEN,
389 response, ss->message, sizeof(ss->message));
390 if (!ok || !auth_number()) {
391 ss->flags |= AUTH_FAILED;
392 warn("Peer %q failed CHAP authentication", name);
394 } else if ((ss->flags & AUTH_DONE) == 0)
397 /* send the response */
399 MAKEHEADER(p, PPP_CHAP);
400 mlen = strlen(ss->message);
401 len = CHAP_HDRLEN + mlen;
402 p[0] = (ss->flags & AUTH_FAILED)? CHAP_FAILURE: CHAP_SUCCESS;
407 memcpy(p + CHAP_HDRLEN, ss->message, mlen);
408 output(0, outpacket_buf, PPP_HDRLEN + len);
410 if (ss->flags & CHALLENGE_VALID) {
411 ss->flags &= ~CHALLENGE_VALID;
412 if (!(ss->flags & AUTH_DONE) && !(ss->flags & AUTH_FAILED)) {
414 * Auth is OK, so now we need to check session restrictions
415 * to ensure everything is OK, but only if we used a
416 * plugin, and only if we're configured to check. This
417 * allows us to do PAM checks on PPP servers that
418 * authenticate against ActiveDirectory, and use AD for
419 * account info (like when using Winbind integrated with
423 session_check(name, NULL, devnam, NULL) == 0) {
424 ss->flags |= AUTH_FAILED;
425 warn("Peer %q failed CHAP Session verification", name);
428 if (ss->flags & AUTH_FAILED) {
429 auth_peer_fail(0, PPP_CHAP);
431 if ((ss->flags & AUTH_DONE) == 0)
432 auth_peer_success(0, PPP_CHAP,
435 if (chap_rechallenge_time) {
436 ss->flags |= TIMEOUT_PENDING;
437 TIMEOUT(chap_server_timeout, ss,
438 chap_rechallenge_time);
441 ss->flags |= AUTH_DONE;
446 * chap_verify_response - check whether the peer's response matches
447 * what we think it should be. Returns 1 if it does (authentication
448 * succeeded), or 0 if it doesn't.
451 chap_verify_response(char *name, char *ourname, int id,
452 struct chap_digest_type *digest,
453 unsigned char *challenge, unsigned char *response,
454 char *message, int message_space)
457 unsigned char secret[MAXSECRETLEN];
460 /* Get the secret that the peer is supposed to know */
461 if (!get_secret(0, name, ourname, (char *)secret, &secret_len, 1)) {
462 error("No CHAP secret found for authenticating %q", name);
466 ok = digest->verify_response(id, name, secret, secret_len, challenge,
467 response, message, message_space);
468 memset(secret, 0, sizeof(secret));
474 * chap_respond - Generate and send a response to a challenge.
477 chap_respond(struct chap_client_state *cs, int id,
478 unsigned char *pkt, int len)
483 unsigned char response[RESP_MAX_PKTLEN];
484 char rname[MAXNAMELEN+1];
485 char secret[MAXSECRETLEN+1];
487 if ((cs->flags & (LOWERUP | AUTH_STARTED)) != (LOWERUP | AUTH_STARTED))
488 return; /* not ready */
489 if (len < 2 || len < pkt[0] + 1)
490 return; /* too short */
492 nlen = len - (clen + 1);
494 /* Null terminate and clean remote name. */
495 slprintf(rname, sizeof(rname), "%.*v", nlen, pkt + clen + 1);
497 /* Microsoft doesn't send their name back in the PPP packet */
498 if (explicit_remote || (remote_name[0] != 0 && rname[0] == 0))
499 strlcpy(rname, remote_name, sizeof(rname));
501 /* get secret for authenticating ourselves with the specified host */
502 if (!get_secret(0, cs->name, rname, secret, &secret_len, 0)) {
503 secret_len = 0; /* assume null secret if can't find one */
504 warn("No CHAP secret found for authenticating us to %q", rname);
508 MAKEHEADER(p, PPP_CHAP);
511 cs->digest->make_response(p, id, cs->name, pkt,
512 secret, secret_len, cs->priv);
513 memset(secret, 0, secret_len);
516 nlen = strlen(cs->name);
517 memcpy(p + clen + 1, cs->name, nlen);
519 p = response + PPP_HDRLEN;
520 len = CHAP_HDRLEN + clen + 1 + nlen;
521 p[0] = CHAP_RESPONSE;
526 output(0, response, PPP_HDRLEN + len);
530 chap_handle_status(struct chap_client_state *cs, int code, int id,
531 unsigned char *pkt, int len)
533 const char *msg = NULL;
535 if ((cs->flags & (AUTH_DONE|AUTH_STARTED|LOWERUP))
536 != (AUTH_STARTED|LOWERUP))
538 cs->flags |= AUTH_DONE;
540 UNTIMEOUT(chap_client_timeout, cs);
541 cs->flags &= ~TIMEOUT_PENDING;
543 if (code == CHAP_SUCCESS) {
544 /* used for MS-CHAP v2 mutual auth, yuck */
545 if (cs->digest->check_success != NULL) {
546 if (!(*cs->digest->check_success)(id, pkt, len))
549 msg = "CHAP authentication succeeded";
551 if (cs->digest->handle_failure != NULL)
552 (*cs->digest->handle_failure)(pkt, len);
554 msg = "CHAP authentication failed";
558 info("%s: %.*v", msg, len, pkt);
562 if (code == CHAP_SUCCESS)
563 auth_withpeer_success(0, PPP_CHAP, cs->digest->code);
565 cs->flags |= AUTH_FAILED;
566 error("CHAP authentication failed");
567 auth_withpeer_fail(0, PPP_CHAP);
572 chap_input(int unit, unsigned char *pkt, int pktlen)
574 struct chap_client_state *cs = &client;
575 struct chap_server_state *ss = &server;
576 unsigned char code, id;
579 if (pktlen < CHAP_HDRLEN)
584 if (len < CHAP_HDRLEN || len > pktlen)
590 chap_respond(cs, id, pkt, len);
593 chap_handle_response(ss, id, pkt, len);
597 chap_handle_status(cs, code, id, pkt, len);
603 chap_protrej(int unit)
605 struct chap_client_state *cs = &client;
606 struct chap_server_state *ss = &server;
608 if (ss->flags & TIMEOUT_PENDING) {
609 ss->flags &= ~TIMEOUT_PENDING;
610 UNTIMEOUT(chap_server_timeout, ss);
612 if (ss->flags & AUTH_STARTED) {
614 auth_peer_fail(0, PPP_CHAP);
616 if ((cs->flags & (AUTH_STARTED|AUTH_DONE)) == AUTH_STARTED) {
617 cs->flags &= ~AUTH_STARTED;
618 error("CHAP authentication failed due to protocol-reject");
619 auth_withpeer_fail(0, PPP_CHAP);
624 * chap_print_pkt - print the contents of a CHAP packet.
626 static char *chap_code_names[] = {
627 "Challenge", "Response", "Success", "Failure"
631 chap_print_pkt(unsigned char *p, int plen,
632 void (*printer)(void *, char *, ...), void *arg)
638 if (plen < CHAP_HDRLEN)
643 if (len < CHAP_HDRLEN || len > plen)
646 if (code >= 1 && code <= sizeof(chap_code_names) / sizeof(char *))
647 printer(arg, " %s", chap_code_names[code-1]);
649 printer(arg, " code=0x%x", code);
650 printer(arg, " id=0x%x", id);
661 nlen = len - clen - 1;
663 for (; clen > 0; --clen) {
665 printer(arg, "%.2x", x);
667 printer(arg, ">, name = ");
668 print_string((char *)p, nlen, printer, arg);
673 print_string((char *)p, len, printer, arg);
676 for (clen = len; clen > 0; --clen) {
678 printer(arg, " %.2x", x);
682 return len + CHAP_HDRLEN;
685 struct protent chap_protent = {
695 NULL, /* datainput */
696 1, /* enabled_flag */
698 NULL, /* data_name */
700 NULL, /* check_options */