2 * chap.c - New CHAP implementation.
4 * Copyright (c) 2003-2024 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. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
19 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
21 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
23 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
24 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 #include "pppd-private.h"
39 #ifdef PPP_WITH_CHAPMS
41 #define MDTYPE_ALL (MDTYPE_MICROSOFT_V2 | MDTYPE_MICROSOFT | MDTYPE_MD5)
43 #define MDTYPE_ALL (MDTYPE_MD5)
46 int chap_mdtype_all = MDTYPE_ALL;
48 /* Hook for a plugin to validate CHAP challenge */
49 chap_verify_hook_fn *chap_verify_hook = NULL;
54 int chap_server_timeout_time = 3;
55 int chap_max_transmits = 10;
56 int chap_rechallenge_time = 0;
57 int chap_client_timeout_time = 60;
58 int chapms_strip_domain = 0;
61 * Command-line options.
63 static struct option chap_option_list[] = {
64 { "chap-restart", o_int, &chap_server_timeout_time,
65 "Set timeout for CHAP (as server)", OPT_PRIO },
66 { "chap-max-challenge", o_int, &chap_max_transmits,
67 "Set max #xmits for challenge", OPT_PRIO },
68 { "chap-interval", o_int, &chap_rechallenge_time,
69 "Set interval for rechallenge", OPT_PRIO },
70 { "chap-timeout", o_int, &chap_client_timeout_time,
71 "Set timeout for CHAP (as client)", OPT_PRIO },
72 { "chapms-strip-domain", o_bool, &chapms_strip_domain,
73 "Strip the domain prefix before the Username", 1 },
80 static struct chap_client_state {
83 struct chap_digest_type *digest;
84 unsigned char priv[64]; /* private area for digest's use */
88 * These limits apply to challenge and response packets we send.
89 * The +4 is the +1 that we actually need rounded up.
91 #define CHAL_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_CHALLENGE_LEN + MAXNAMELEN)
92 #define RESP_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_RESPONSE_LEN + MAXNAMELEN)
94 static struct chap_server_state {
98 struct chap_digest_type *digest;
100 int challenge_pktlen;
101 unsigned char challenge[CHAL_MAX_PKTLEN];
105 /* Values for flags in chap_client_state and chap_server_state */
107 #define AUTH_STARTED 2
109 #define AUTH_FAILED 8
110 #define TIMEOUT_PENDING 0x10
111 #define CHALLENGE_VALID 0x20
116 static void chap_init(int unit);
117 static void chap_lowerup(int unit);
118 static void chap_lowerdown(int unit);
119 static void chap_server_timeout(void *arg);
120 static void chap_client_timeout(void *arg);
121 static void chap_generate_challenge(struct chap_server_state *ss);
122 static void chap_handle_response(struct chap_server_state *ss, int code,
123 unsigned char *pkt, int len);
124 static chap_verify_hook_fn chap_verify_response;
125 static void chap_respond(struct chap_client_state *cs, int id,
126 unsigned char *pkt, int len);
127 static void chap_handle_status(struct chap_client_state *cs, int code, int id,
128 unsigned char *pkt, int len);
129 static void chap_protrej(int unit);
130 static void chap_input(int unit, unsigned char *pkt, int pktlen);
131 static int chap_print_pkt(unsigned char *p, int plen,
132 void (*printer)(void *, char *, ...), void *arg);
134 /* List of digest types that we know about */
135 static struct chap_digest_type *chap_digests;
138 * chap_init - reset to initial state.
143 memset(&client, 0, sizeof(client));
144 memset(&server, 0, sizeof(server));
147 #ifdef PPP_WITH_CHAPMS
153 * Add a new digest type to the list.
156 chap_register_digest(struct chap_digest_type *dp)
158 dp->next = chap_digests;
163 * Lookup a digest type by code
165 struct chap_digest_type *
166 chap_find_digest(int digest_code) {
167 struct chap_digest_type *dp = NULL;
168 for (dp = chap_digests; dp != NULL; dp = dp->next)
169 if (dp->code == digest_code)
175 * chap_lowerup - we can start doing stuff now.
178 chap_lowerup(int unit)
180 struct chap_client_state *cs = &client;
181 struct chap_server_state *ss = &server;
183 cs->flags |= LOWERUP;
184 ss->flags |= LOWERUP;
185 if (ss->flags & AUTH_STARTED)
186 chap_server_timeout(ss);
190 chap_lowerdown(int unit)
192 struct chap_client_state *cs = &client;
193 struct chap_server_state *ss = &server;
195 if (cs->flags & TIMEOUT_PENDING)
196 UNTIMEOUT(chap_client_timeout, cs);
198 if (ss->flags & TIMEOUT_PENDING)
199 UNTIMEOUT(chap_server_timeout, ss);
204 * chap_auth_peer - Start authenticating the peer.
205 * If the lower layer is already up, we start sending challenges,
206 * otherwise we wait for the lower layer to come up.
209 chap_auth_peer(int unit, char *our_name, int digest_code)
211 struct chap_server_state *ss = &server;
212 struct chap_digest_type *dp;
214 if (ss->flags & AUTH_STARTED) {
215 error("CHAP: peer authentication already started!");
219 dp = chap_find_digest(digest_code);
221 fatal("CHAP digest 0x%x requested but not available",
226 /* Start with a random ID value */
227 ss->id = (unsigned char)(drand48() * 256);
228 ss->flags |= AUTH_STARTED;
229 if (ss->flags & LOWERUP)
230 chap_server_timeout(ss);
234 * chap_auth_with_peer - Prepare to authenticate ourselves to the peer.
235 * There isn't much to do until we receive a challenge.
238 chap_auth_with_peer(int unit, char *our_name, int digest_code)
240 struct chap_client_state *cs = &client;
241 struct chap_digest_type *dp;
243 if (cs->flags & AUTH_STARTED) {
244 error("CHAP: authentication with peer already started!");
247 for (dp = chap_digests; dp != NULL; dp = dp->next)
248 if (dp->code == digest_code)
251 fatal("CHAP digest 0x%x requested but not available",
256 cs->flags |= AUTH_STARTED | TIMEOUT_PENDING;
257 TIMEOUT(chap_client_timeout, cs, chap_client_timeout_time);
261 * chap_server_timeout - It's time to send another challenge to the peer.
262 * This could be either a retransmission of a previous challenge,
263 * or a new challenge to start re-authentication.
266 chap_server_timeout(void *arg)
268 struct chap_server_state *ss = arg;
270 ss->flags &= ~TIMEOUT_PENDING;
271 if ((ss->flags & CHALLENGE_VALID) == 0) {
272 ss->challenge_xmits = 0;
273 chap_generate_challenge(ss);
274 ss->flags |= CHALLENGE_VALID;
275 } else if (ss->challenge_xmits >= chap_max_transmits) {
276 ss->flags &= ~CHALLENGE_VALID;
277 ss->flags |= AUTH_DONE | AUTH_FAILED;
278 auth_peer_fail(0, PPP_CHAP);
282 output(0, ss->challenge, ss->challenge_pktlen);
283 ++ss->challenge_xmits;
284 ss->flags |= TIMEOUT_PENDING;
285 TIMEOUT(chap_server_timeout, arg, chap_server_timeout_time);
288 /* chap_client_timeout - Authentication with peer timed out. */
290 chap_client_timeout(void *arg)
292 struct chap_client_state *cs = arg;
294 cs->flags &= ~TIMEOUT_PENDING;
295 cs->flags |= AUTH_DONE | AUTH_FAILED;
296 error("CHAP authentication timed out");
297 auth_withpeer_fail(0, PPP_CHAP);
301 * chap_generate_challenge - generate a challenge string and format
302 * the challenge packet in ss->challenge_pkt.
305 chap_generate_challenge(struct chap_server_state *ss)
307 int clen = 1, nlen, len;
311 MAKEHEADER(p, PPP_CHAP);
313 ss->digest->generate_challenge(p);
315 nlen = strlen(ss->name);
316 memcpy(p + 1 + clen, ss->name, nlen);
318 len = CHAP_HDRLEN + 1 + clen + nlen;
319 ss->challenge_pktlen = PPP_HDRLEN + len;
321 p = ss->challenge + PPP_HDRLEN;
322 p[0] = CHAP_CHALLENGE;
329 * chap_handle_response - check the response to our challenge.
332 chap_handle_response(struct chap_server_state *ss, int id,
333 unsigned char *pkt, int len)
335 int response_len, ok, mlen;
336 unsigned char *response, *p;
338 chap_verify_hook_fn *verifier;
339 char rname[MAXNAMELEN+1];
341 if ((ss->flags & LOWERUP) == 0)
343 if (id != ss->challenge[PPP_HDRLEN+1] || len < 2)
345 if (ss->flags & CHALLENGE_VALID) {
347 GETCHAR(response_len, pkt);
348 len -= response_len + 1; /* length of name */
349 name = (char *)pkt + response_len;
353 if (ss->flags & TIMEOUT_PENDING) {
354 ss->flags &= ~TIMEOUT_PENDING;
355 UNTIMEOUT(chap_server_timeout, ss);
358 if (explicit_remote) {
361 /* Null terminate and clean remote name. */
362 slprintf(rname, sizeof(rname), "%.*v", len, name);
365 /* strip the MS domain name */
366 if (chapms_strip_domain && strrchr(rname, '\\')) {
367 char tmp[MAXNAMELEN+1];
369 strcpy(tmp, strrchr(rname, '\\') + 1);
374 if (chap_verify_hook)
375 verifier = chap_verify_hook;
377 verifier = chap_verify_response;
378 ok = (*verifier)(name, ss->name, id, ss->digest,
379 ss->challenge + PPP_HDRLEN + CHAP_HDRLEN,
380 response, ss->message, sizeof(ss->message));
381 if (!ok || !auth_number()) {
382 ss->flags |= AUTH_FAILED;
383 warn("Peer %q failed CHAP authentication", name);
385 } else if ((ss->flags & AUTH_DONE) == 0)
388 /* send the response */
390 MAKEHEADER(p, PPP_CHAP);
391 mlen = strlen(ss->message);
392 len = CHAP_HDRLEN + mlen;
393 p[0] = (ss->flags & AUTH_FAILED)? CHAP_FAILURE: CHAP_SUCCESS;
398 memcpy(p + CHAP_HDRLEN, ss->message, mlen);
399 output(0, outpacket_buf, PPP_HDRLEN + len);
401 if (ss->flags & CHALLENGE_VALID) {
402 ss->flags &= ~CHALLENGE_VALID;
403 if (!(ss->flags & AUTH_DONE) && !(ss->flags & AUTH_FAILED)) {
405 * Auth is OK, so now we need to check session restrictions
406 * to ensure everything is OK, but only if we used a
407 * plugin, and only if we're configured to check. This
408 * allows us to do PAM checks on PPP servers that
409 * authenticate against ActiveDirectory, and use AD for
410 * account info (like when using Winbind integrated with
414 session_check(name, NULL, devnam, NULL) == 0) {
415 ss->flags |= AUTH_FAILED;
416 warn("Peer %q failed CHAP Session verification", name);
419 if (ss->flags & AUTH_FAILED) {
420 auth_peer_fail(0, PPP_CHAP);
422 if ((ss->flags & AUTH_DONE) == 0)
423 auth_peer_success(0, PPP_CHAP,
426 if (chap_rechallenge_time) {
427 ss->flags |= TIMEOUT_PENDING;
428 TIMEOUT(chap_server_timeout, ss,
429 chap_rechallenge_time);
432 ss->flags |= AUTH_DONE;
437 * chap_verify_response - check whether the peer's response matches
438 * what we think it should be. Returns 1 if it does (authentication
439 * succeeded), or 0 if it doesn't.
442 chap_verify_response(char *name, char *ourname, int id,
443 struct chap_digest_type *digest,
444 unsigned char *challenge, unsigned char *response,
445 char *message, int message_space)
448 unsigned char secret[MAXSECRETLEN];
451 /* Get the secret that the peer is supposed to know */
452 if (!get_secret(0, name, ourname, (char *)secret, &secret_len, 1)) {
453 error("No CHAP secret found for authenticating %q", name);
457 ok = digest->verify_response(id, name, secret, secret_len, challenge,
458 response, message, message_space);
459 memset(secret, 0, sizeof(secret));
465 * chap_respond - Generate and send a response to a challenge.
468 chap_respond(struct chap_client_state *cs, int id,
469 unsigned char *pkt, int len)
474 unsigned char response[RESP_MAX_PKTLEN];
475 char rname[MAXNAMELEN+1];
476 char secret[MAXSECRETLEN+1];
478 if ((cs->flags & (LOWERUP | AUTH_STARTED)) != (LOWERUP | AUTH_STARTED))
479 return; /* not ready */
480 if (len < 2 || len < pkt[0] + 1)
481 return; /* too short */
483 nlen = len - (clen + 1);
485 /* Null terminate and clean remote name. */
486 slprintf(rname, sizeof(rname), "%.*v", nlen, pkt + clen + 1);
488 /* Microsoft doesn't send their name back in the PPP packet */
489 if (explicit_remote || (remote_name[0] != 0 && rname[0] == 0))
490 strlcpy(rname, remote_name, sizeof(rname));
492 /* get secret for authenticating ourselves with the specified host */
493 if (!get_secret(0, cs->name, rname, secret, &secret_len, 0)) {
494 secret_len = 0; /* assume null secret if can't find one */
495 warn("No CHAP secret found for authenticating us to %q", rname);
499 MAKEHEADER(p, PPP_CHAP);
502 cs->digest->make_response(p, id, cs->name, pkt,
503 secret, secret_len, cs->priv);
504 memset(secret, 0, secret_len);
507 nlen = strlen(cs->name);
508 memcpy(p + clen + 1, cs->name, nlen);
510 p = response + PPP_HDRLEN;
511 len = CHAP_HDRLEN + clen + 1 + nlen;
512 p[0] = CHAP_RESPONSE;
517 output(0, response, PPP_HDRLEN + len);
521 chap_handle_status(struct chap_client_state *cs, int code, int id,
522 unsigned char *pkt, int len)
524 const char *msg = NULL;
526 if ((cs->flags & (AUTH_DONE|AUTH_STARTED|LOWERUP))
527 != (AUTH_STARTED|LOWERUP))
529 cs->flags |= AUTH_DONE;
531 UNTIMEOUT(chap_client_timeout, cs);
532 cs->flags &= ~TIMEOUT_PENDING;
534 if (code == CHAP_SUCCESS) {
535 /* used for MS-CHAP v2 mutual auth, yuck */
536 if (cs->digest->check_success != NULL) {
537 if (!(*cs->digest->check_success)(id, pkt, len))
540 msg = "CHAP authentication succeeded";
542 if (cs->digest->handle_failure != NULL)
543 (*cs->digest->handle_failure)(pkt, len);
545 msg = "CHAP authentication failed";
549 info("%s: %.*v", msg, len, pkt);
553 if (code == CHAP_SUCCESS)
554 auth_withpeer_success(0, PPP_CHAP, cs->digest->code);
556 cs->flags |= AUTH_FAILED;
557 error("CHAP authentication failed");
558 auth_withpeer_fail(0, PPP_CHAP);
563 chap_input(int unit, unsigned char *pkt, int pktlen)
565 struct chap_client_state *cs = &client;
566 struct chap_server_state *ss = &server;
567 unsigned char code, id;
570 if (pktlen < CHAP_HDRLEN)
575 if (len < CHAP_HDRLEN || len > pktlen)
581 chap_respond(cs, id, pkt, len);
584 chap_handle_response(ss, id, pkt, len);
588 chap_handle_status(cs, code, id, pkt, len);
594 chap_protrej(int unit)
596 struct chap_client_state *cs = &client;
597 struct chap_server_state *ss = &server;
599 if (ss->flags & TIMEOUT_PENDING) {
600 ss->flags &= ~TIMEOUT_PENDING;
601 UNTIMEOUT(chap_server_timeout, ss);
603 if (ss->flags & AUTH_STARTED) {
605 auth_peer_fail(0, PPP_CHAP);
607 if ((cs->flags & (AUTH_STARTED|AUTH_DONE)) == AUTH_STARTED) {
608 cs->flags &= ~AUTH_STARTED;
609 error("CHAP authentication failed due to protocol-reject");
610 auth_withpeer_fail(0, PPP_CHAP);
615 * chap_print_pkt - print the contents of a CHAP packet.
617 static char *chap_code_names[] = {
618 "Challenge", "Response", "Success", "Failure"
622 chap_print_pkt(unsigned char *p, int plen,
623 void (*printer)(void *, char *, ...), void *arg)
629 if (plen < CHAP_HDRLEN)
634 if (len < CHAP_HDRLEN || len > plen)
637 if (code >= 1 && code <= sizeof(chap_code_names) / sizeof(char *))
638 printer(arg, " %s", chap_code_names[code-1]);
640 printer(arg, " code=0x%x", code);
641 printer(arg, " id=0x%x", id);
652 nlen = len - clen - 1;
654 for (; clen > 0; --clen) {
656 printer(arg, "%.2x", x);
658 printer(arg, ">, name = ");
659 print_string((char *)p, nlen, printer, arg);
664 print_string((char *)p, len, printer, arg);
667 for (clen = len; clen > 0; --clen) {
669 printer(arg, " %.2x", x);
673 return len + CHAP_HDRLEN;
676 struct protent chap_protent = {
686 NULL, /* datainput */
687 1, /* enabled_flag */
689 NULL, /* data_name */
691 NULL, /* check_options */