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 * chap_lowerup - we can start doing stuff now.
173 chap_lowerup(int unit)
175 struct chap_client_state *cs = &client;
176 struct chap_server_state *ss = &server;
178 cs->flags |= LOWERUP;
179 ss->flags |= LOWERUP;
180 if (ss->flags & AUTH_STARTED)
181 chap_server_timeout(ss);
185 chap_lowerdown(int unit)
187 struct chap_client_state *cs = &client;
188 struct chap_server_state *ss = &server;
190 if (cs->flags & TIMEOUT_PENDING)
191 UNTIMEOUT(chap_client_timeout, cs);
193 if (ss->flags & TIMEOUT_PENDING)
194 UNTIMEOUT(chap_server_timeout, ss);
199 * chap_auth_peer - Start authenticating the peer.
200 * If the lower layer is already up, we start sending challenges,
201 * otherwise we wait for the lower layer to come up.
204 chap_auth_peer(int unit, char *our_name, int digest_code)
206 struct chap_server_state *ss = &server;
207 struct chap_digest_type *dp;
209 if (ss->flags & AUTH_STARTED) {
210 error("CHAP: peer authentication already started!");
213 for (dp = chap_digests; dp != NULL; dp = dp->next)
214 if (dp->code == digest_code)
217 fatal("CHAP digest 0x%x requested but not available",
222 /* Start with a random ID value */
223 ss->id = (unsigned char)(drand48() * 256);
224 ss->flags |= AUTH_STARTED;
225 if (ss->flags & LOWERUP)
226 chap_server_timeout(ss);
230 * chap_auth_with_peer - Prepare to authenticate ourselves to the peer.
231 * There isn't much to do until we receive a challenge.
234 chap_auth_with_peer(int unit, char *our_name, int digest_code)
236 struct chap_client_state *cs = &client;
237 struct chap_digest_type *dp;
239 if (cs->flags & AUTH_STARTED) {
240 error("CHAP: authentication with peer already started!");
243 for (dp = chap_digests; dp != NULL; dp = dp->next)
244 if (dp->code == digest_code)
247 fatal("CHAP digest 0x%x requested but not available",
252 cs->flags |= AUTH_STARTED | TIMEOUT_PENDING;
253 TIMEOUT(chap_client_timeout, cs, chap_client_timeout_time);
257 * chap_server_timeout - It's time to send another challenge to the peer.
258 * This could be either a retransmission of a previous challenge,
259 * or a new challenge to start re-authentication.
262 chap_server_timeout(void *arg)
264 struct chap_server_state *ss = arg;
266 ss->flags &= ~TIMEOUT_PENDING;
267 if ((ss->flags & CHALLENGE_VALID) == 0) {
268 ss->challenge_xmits = 0;
269 chap_generate_challenge(ss);
270 ss->flags |= CHALLENGE_VALID;
271 } else if (ss->challenge_xmits >= chap_max_transmits) {
272 ss->flags &= ~CHALLENGE_VALID;
273 ss->flags |= AUTH_DONE | AUTH_FAILED;
274 auth_peer_fail(0, PPP_CHAP);
278 output(0, ss->challenge, ss->challenge_pktlen);
279 ++ss->challenge_xmits;
280 ss->flags |= TIMEOUT_PENDING;
281 TIMEOUT(chap_server_timeout, arg, chap_server_timeout_time);
284 /* chap_client_timeout - Authentication with peer timed out. */
286 chap_client_timeout(void *arg)
288 struct chap_client_state *cs = arg;
290 cs->flags &= ~TIMEOUT_PENDING;
291 cs->flags |= AUTH_DONE | AUTH_FAILED;
292 error("CHAP authentication timed out");
293 auth_withpeer_fail(0, PPP_CHAP);
297 * chap_generate_challenge - generate a challenge string and format
298 * the challenge packet in ss->challenge_pkt.
301 chap_generate_challenge(struct chap_server_state *ss)
303 int clen = 1, nlen, len;
307 MAKEHEADER(p, PPP_CHAP);
309 ss->digest->generate_challenge(p);
311 nlen = strlen(ss->name);
312 memcpy(p + 1 + clen, ss->name, nlen);
314 len = CHAP_HDRLEN + 1 + clen + nlen;
315 ss->challenge_pktlen = PPP_HDRLEN + len;
317 p = ss->challenge + PPP_HDRLEN;
318 p[0] = CHAP_CHALLENGE;
325 * chap_handle_response - check the response to our challenge.
328 chap_handle_response(struct chap_server_state *ss, int id,
329 unsigned char *pkt, int len)
331 int response_len, ok, mlen;
332 unsigned char *response, *p;
333 char *name = NULL; /* initialized to shut gcc up */
334 int (*verifier)(char *, char *, int, struct chap_digest_type *,
335 unsigned char *, unsigned char *, char *, int);
336 char rname[MAXNAMELEN+1];
338 if ((ss->flags & LOWERUP) == 0)
340 if (id != ss->challenge[PPP_HDRLEN+1] || len < 2)
342 if (ss->flags & CHALLENGE_VALID) {
344 GETCHAR(response_len, pkt);
345 len -= response_len + 1; /* length of name */
346 name = (char *)pkt + response_len;
350 if (ss->flags & TIMEOUT_PENDING) {
351 ss->flags &= ~TIMEOUT_PENDING;
352 UNTIMEOUT(chap_server_timeout, ss);
355 if (explicit_remote) {
358 /* Null terminate and clean remote name. */
359 slprintf(rname, sizeof(rname), "%.*v", len, name);
362 /* strip the MS domain name */
363 if (chapms_strip_domain && strrchr(rname, '\\')) {
364 char tmp[MAXNAMELEN+1];
366 strcpy(tmp, strrchr(rname, '\\') + 1);
371 if (chap_verify_hook)
372 verifier = chap_verify_hook;
374 verifier = chap_verify_response;
375 ok = (*verifier)(name, ss->name, id, ss->digest,
376 ss->challenge + PPP_HDRLEN + CHAP_HDRLEN,
377 response, ss->message, sizeof(ss->message));
378 if (!ok || !auth_number()) {
379 ss->flags |= AUTH_FAILED;
380 warn("Peer %q failed CHAP authentication", name);
382 } else if ((ss->flags & AUTH_DONE) == 0)
385 /* send the response */
387 MAKEHEADER(p, PPP_CHAP);
388 mlen = strlen(ss->message);
389 len = CHAP_HDRLEN + mlen;
390 p[0] = (ss->flags & AUTH_FAILED)? CHAP_FAILURE: CHAP_SUCCESS;
395 memcpy(p + CHAP_HDRLEN, ss->message, mlen);
396 output(0, outpacket_buf, PPP_HDRLEN + len);
398 if (ss->flags & CHALLENGE_VALID) {
399 ss->flags &= ~CHALLENGE_VALID;
400 if (!(ss->flags & AUTH_DONE) && !(ss->flags & AUTH_FAILED)) {
402 * Auth is OK, so now we need to check session restrictions
403 * to ensure everything is OK, but only if we used a
404 * plugin, and only if we're configured to check. This
405 * allows us to do PAM checks on PPP servers that
406 * authenticate against ActiveDirectory, and use AD for
407 * account info (like when using Winbind integrated with
411 session_check(name, NULL, devnam, NULL) == 0) {
412 ss->flags |= AUTH_FAILED;
413 warn("Peer %q failed CHAP Session verification", name);
416 if (ss->flags & AUTH_FAILED) {
417 auth_peer_fail(0, PPP_CHAP);
419 if ((ss->flags & AUTH_DONE) == 0)
420 auth_peer_success(0, PPP_CHAP,
423 if (chap_rechallenge_time) {
424 ss->flags |= TIMEOUT_PENDING;
425 TIMEOUT(chap_server_timeout, ss,
426 chap_rechallenge_time);
429 ss->flags |= AUTH_DONE;
434 * chap_verify_response - check whether the peer's response matches
435 * what we think it should be. Returns 1 if it does (authentication
436 * succeeded), or 0 if it doesn't.
439 chap_verify_response(char *name, char *ourname, int id,
440 struct chap_digest_type *digest,
441 unsigned char *challenge, unsigned char *response,
442 char *message, int message_space)
445 unsigned char secret[MAXSECRETLEN];
448 /* Get the secret that the peer is supposed to know */
449 if (!get_secret(0, name, ourname, (char *)secret, &secret_len, 1)) {
450 error("No CHAP secret found for authenticating %q", name);
454 ok = digest->verify_response(id, name, secret, secret_len, challenge,
455 response, message, message_space);
456 memset(secret, 0, sizeof(secret));
462 * chap_respond - Generate and send a response to a challenge.
465 chap_respond(struct chap_client_state *cs, int id,
466 unsigned char *pkt, int len)
471 unsigned char response[RESP_MAX_PKTLEN];
472 char rname[MAXNAMELEN+1];
473 char secret[MAXSECRETLEN+1];
475 if ((cs->flags & (LOWERUP | AUTH_STARTED)) != (LOWERUP | AUTH_STARTED))
476 return; /* not ready */
477 if (len < 2 || len < pkt[0] + 1)
478 return; /* too short */
480 nlen = len - (clen + 1);
482 /* Null terminate and clean remote name. */
483 slprintf(rname, sizeof(rname), "%.*v", nlen, pkt + clen + 1);
485 /* Microsoft doesn't send their name back in the PPP packet */
486 if (explicit_remote || (remote_name[0] != 0 && rname[0] == 0))
487 strlcpy(rname, remote_name, sizeof(rname));
489 /* get secret for authenticating ourselves with the specified host */
490 if (!get_secret(0, cs->name, rname, secret, &secret_len, 0)) {
491 secret_len = 0; /* assume null secret if can't find one */
492 warn("No CHAP secret found for authenticating us to %q", rname);
496 MAKEHEADER(p, PPP_CHAP);
499 cs->digest->make_response(p, id, cs->name, pkt,
500 secret, secret_len, cs->priv);
501 memset(secret, 0, secret_len);
504 nlen = strlen(cs->name);
505 memcpy(p + clen + 1, cs->name, nlen);
507 p = response + PPP_HDRLEN;
508 len = CHAP_HDRLEN + clen + 1 + nlen;
509 p[0] = CHAP_RESPONSE;
514 output(0, response, PPP_HDRLEN + len);
518 chap_handle_status(struct chap_client_state *cs, int code, int id,
519 unsigned char *pkt, int len)
521 const char *msg = NULL;
523 if ((cs->flags & (AUTH_DONE|AUTH_STARTED|LOWERUP))
524 != (AUTH_STARTED|LOWERUP))
526 cs->flags |= AUTH_DONE;
528 UNTIMEOUT(chap_client_timeout, cs);
529 cs->flags &= ~TIMEOUT_PENDING;
531 if (code == CHAP_SUCCESS) {
532 /* used for MS-CHAP v2 mutual auth, yuck */
533 if (cs->digest->check_success != NULL) {
534 if (!(*cs->digest->check_success)(id, pkt, len))
537 msg = "CHAP authentication succeeded";
539 if (cs->digest->handle_failure != NULL)
540 (*cs->digest->handle_failure)(pkt, len);
542 msg = "CHAP authentication failed";
546 info("%s: %.*v", msg, len, pkt);
550 if (code == CHAP_SUCCESS)
551 auth_withpeer_success(0, PPP_CHAP, cs->digest->code);
553 cs->flags |= AUTH_FAILED;
554 error("CHAP authentication failed");
555 auth_withpeer_fail(0, PPP_CHAP);
560 chap_input(int unit, unsigned char *pkt, int pktlen)
562 struct chap_client_state *cs = &client;
563 struct chap_server_state *ss = &server;
564 unsigned char code, id;
567 if (pktlen < CHAP_HDRLEN)
572 if (len < CHAP_HDRLEN || len > pktlen)
578 chap_respond(cs, id, pkt, len);
581 chap_handle_response(ss, id, pkt, len);
585 chap_handle_status(cs, code, id, pkt, len);
591 chap_protrej(int unit)
593 struct chap_client_state *cs = &client;
594 struct chap_server_state *ss = &server;
596 if (ss->flags & TIMEOUT_PENDING) {
597 ss->flags &= ~TIMEOUT_PENDING;
598 UNTIMEOUT(chap_server_timeout, ss);
600 if (ss->flags & AUTH_STARTED) {
602 auth_peer_fail(0, PPP_CHAP);
604 if ((cs->flags & (AUTH_STARTED|AUTH_DONE)) == AUTH_STARTED) {
605 cs->flags &= ~AUTH_STARTED;
606 error("CHAP authentication failed due to protocol-reject");
607 auth_withpeer_fail(0, PPP_CHAP);
612 * chap_print_pkt - print the contents of a CHAP packet.
614 static char *chap_code_names[] = {
615 "Challenge", "Response", "Success", "Failure"
619 chap_print_pkt(unsigned char *p, int plen,
620 void (*printer)(void *, char *, ...), void *arg)
626 if (plen < CHAP_HDRLEN)
631 if (len < CHAP_HDRLEN || len > plen)
634 if (code >= 1 && code <= sizeof(chap_code_names) / sizeof(char *))
635 printer(arg, " %s", chap_code_names[code-1]);
637 printer(arg, " code=0x%x", code);
638 printer(arg, " id=0x%x", id);
649 nlen = len - clen - 1;
651 for (; clen > 0; --clen) {
653 printer(arg, "%.2x", x);
655 printer(arg, ">, name = ");
656 print_string((char *)p, nlen, printer, arg);
661 print_string((char *)p, len, printer, arg);
664 for (clen = len; clen > 0; --clen) {
666 printer(arg, " %.2x", x);
670 return len + CHAP_HDRLEN;
673 struct protent chap_protent = {
683 NULL, /* datainput */
684 1, /* enabled_flag */
686 NULL, /* data_name */
688 NULL, /* check_options */