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_timeout_time
= 3;
59 int chap_max_transmits
= 10;
60 int chap_rechallenge_time
= 0;
63 * Command-line options.
65 static option_t chap_option_list
[] = {
66 { "chap-restart", o_int
, &chap_timeout_time
,
67 "Set timeout for CHAP", OPT_PRIO
},
68 { "chap-max-challenge", o_int
, &chap_max_transmits
,
69 "Set max #xmits for challenge", OPT_PRIO
},
70 { "chap-interval", o_int
, &chap_rechallenge_time
,
71 "Set interval for rechallenge", OPT_PRIO
},
78 static struct chap_client_state
{
81 struct chap_digest_type
*digest
;
82 unsigned char priv
[64]; /* private area for digest's use */
86 * These limits apply to challenge and response packets we send.
87 * The +4 is the +1 that we actually need rounded up.
89 #define CHAL_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_CHALLENGE_LEN + MAXNAMELEN)
90 #define RESP_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_RESPONSE_LEN + MAXNAMELEN)
92 static struct chap_server_state
{
96 struct chap_digest_type
*digest
;
99 unsigned char challenge
[CHAL_MAX_PKTLEN
];
103 /* Values for flags in chap_client_state and chap_server_state */
105 #define AUTH_STARTED 2
107 #define AUTH_FAILED 8
108 #define TIMEOUT_PENDING 0x10
109 #define CHALLENGE_VALID 0x20
114 static void chap_init(int unit
);
115 static void chap_lowerup(int unit
);
116 static void chap_lowerdown(int unit
);
117 static void chap_timeout(void *arg
);
118 static void chap_generate_challenge(struct chap_server_state
*ss
);
119 static void chap_handle_response(struct chap_server_state
*ss
, int code
,
120 unsigned char *pkt
, int len
);
121 static int chap_verify_response(char *name
, char *ourname
, int id
,
122 struct chap_digest_type
*digest
,
123 unsigned char *challenge
, unsigned char *response
,
124 char *message
, int message_space
);
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
) __P((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
));
153 * Add a new digest type to the list.
156 chap_register_digest(struct chap_digest_type
*dp
)
158 dp
->next
= chap_digests
;
163 * chap_lowerup - we can start doing stuff now.
166 chap_lowerup(int unit
)
168 struct chap_client_state
*cs
= &client
;
169 struct chap_server_state
*ss
= &server
;
171 cs
->flags
|= LOWERUP
;
172 ss
->flags
|= LOWERUP
;
173 if (ss
->flags
& AUTH_STARTED
)
178 chap_lowerdown(int unit
)
180 struct chap_client_state
*cs
= &client
;
181 struct chap_server_state
*ss
= &server
;
184 if (ss
->flags
& TIMEOUT_PENDING
)
185 UNTIMEOUT(chap_timeout
, ss
);
190 * chap_auth_peer - Start authenticating the peer.
191 * If the lower layer is already up, we start sending challenges,
192 * otherwise we wait for the lower layer to come up.
195 chap_auth_peer(int unit
, char *our_name
, int digest_code
)
197 struct chap_server_state
*ss
= &server
;
198 struct chap_digest_type
*dp
;
200 if (ss
->flags
& AUTH_STARTED
) {
201 error("CHAP: peer authentication already started!");
204 for (dp
= chap_digests
; dp
!= NULL
; dp
= dp
->next
)
205 if (dp
->code
== digest_code
)
208 fatal("CHAP digest 0x%x requested but not available",
213 /* Start with a random ID value */
214 ss
->id
= (unsigned char)(drand48() * 256);
215 ss
->flags
|= AUTH_STARTED
;
216 if (ss
->flags
& LOWERUP
)
221 * chap_auth_with_peer - Prepare to authenticate ourselves to the peer.
222 * There isn't much to do until we receive a challenge.
225 chap_auth_with_peer(int unit
, char *our_name
, int digest_code
)
227 struct chap_client_state
*cs
= &client
;
228 struct chap_digest_type
*dp
;
230 if (cs
->flags
& AUTH_STARTED
) {
231 error("CHAP: authentication with peer already started!");
234 for (dp
= chap_digests
; dp
!= NULL
; dp
= dp
->next
)
235 if (dp
->code
== digest_code
)
238 fatal("CHAP digest 0x%x requested but not available",
243 cs
->flags
|= AUTH_STARTED
;
247 * chap_timeout - It's time to send another challenge to the peer.
248 * This could be either a retransmission of a previous challenge,
249 * or a new challenge to start re-authentication.
252 chap_timeout(void *arg
)
254 struct chap_server_state
*ss
= arg
;
256 ss
->flags
&= ~TIMEOUT_PENDING
;
257 if ((ss
->flags
& CHALLENGE_VALID
) == 0) {
258 ss
->challenge_xmits
= 0;
259 chap_generate_challenge(ss
);
260 ss
->flags
|= CHALLENGE_VALID
;
261 } else if (ss
->challenge_xmits
>= chap_max_transmits
) {
262 ss
->flags
&= ~CHALLENGE_VALID
;
263 ss
->flags
|= AUTH_DONE
| AUTH_FAILED
;
264 auth_peer_fail(0, PPP_CHAP
);
268 output(0, ss
->challenge
, ss
->challenge_pktlen
);
269 ++ss
->challenge_xmits
;
270 ss
->flags
|= TIMEOUT_PENDING
;
271 TIMEOUT(chap_timeout
, arg
, chap_timeout_time
);
275 * chap_generate_challenge - generate a challenge string and format
276 * the challenge packet in ss->challenge_pkt.
279 chap_generate_challenge(struct chap_server_state
*ss
)
281 int clen
= 1, nlen
, len
;
285 MAKEHEADER(p
, PPP_CHAP
);
287 ss
->digest
->generate_challenge(p
);
289 nlen
= strlen(ss
->name
);
290 memcpy(p
+ 1 + clen
, ss
->name
, nlen
);
292 len
= CHAP_HDRLEN
+ 1 + clen
+ nlen
;
293 ss
->challenge_pktlen
= PPP_HDRLEN
+ len
;
295 p
= ss
->challenge
+ PPP_HDRLEN
;
296 p
[0] = CHAP_CHALLENGE
;
303 * chap_handle_response - check the response to our challenge.
306 chap_handle_response(struct chap_server_state
*ss
, int id
,
307 unsigned char *pkt
, int len
)
309 int response_len
, ok
, mlen
;
310 unsigned char *response
, *p
;
311 char *name
= NULL
; /* initialized to shut gcc up */
312 int (*verifier
)(char *, char *, int, struct chap_digest_type
*,
313 unsigned char *, unsigned char *, char *, int);
314 char rname
[MAXNAMELEN
+1];
316 if ((ss
->flags
& LOWERUP
) == 0)
318 if (id
!= ss
->challenge
[PPP_HDRLEN
+1] || len
< 2)
320 if (ss
->flags
& CHALLENGE_VALID
) {
322 GETCHAR(response_len
, pkt
);
323 len
-= response_len
+ 1; /* length of name */
324 name
= (char *)pkt
+ response_len
;
328 if (ss
->flags
& TIMEOUT_PENDING
) {
329 ss
->flags
&= ~TIMEOUT_PENDING
;
330 UNTIMEOUT(chap_timeout
, ss
);
333 if (explicit_remote
) {
336 /* Null terminate and clean remote name. */
337 slprintf(rname
, sizeof(rname
), "%.*v", len
, name
);
341 if (chap_verify_hook
)
342 verifier
= chap_verify_hook
;
344 verifier
= chap_verify_response
;
345 ok
= (*verifier
)(name
, ss
->name
, id
, ss
->digest
,
346 ss
->challenge
+ PPP_HDRLEN
+ CHAP_HDRLEN
,
347 response
, ss
->message
, sizeof(ss
->message
));
348 if (!ok
|| !auth_number()) {
349 ss
->flags
|= AUTH_FAILED
;
350 warn("Peer %q failed CHAP authentication", name
);
352 } else if ((ss
->flags
& AUTH_DONE
) == 0)
355 /* send the response */
357 MAKEHEADER(p
, PPP_CHAP
);
358 mlen
= strlen(ss
->message
);
359 len
= CHAP_HDRLEN
+ mlen
;
360 p
[0] = (ss
->flags
& AUTH_FAILED
)? CHAP_FAILURE
: CHAP_SUCCESS
;
365 memcpy(p
+ CHAP_HDRLEN
, ss
->message
, mlen
);
366 output(0, outpacket_buf
, PPP_HDRLEN
+ len
);
368 if (ss
->flags
& CHALLENGE_VALID
) {
369 ss
->flags
&= ~CHALLENGE_VALID
;
370 if (!(ss
->flags
& AUTH_DONE
) && !(ss
->flags
& AUTH_FAILED
)) {
372 * Auth is OK, so now we need to check session restrictions
373 * to ensure everything is OK, but only if we used a
374 * plugin, and only if we're configured to check. This
375 * allows us to do PAM checks on PPP servers that
376 * authenticate against ActiveDirectory, and use AD for
377 * account info (like when using Winbind integrated with
381 session_check(name
, NULL
, devnam
, NULL
) == 0) {
382 ss
->flags
|= AUTH_FAILED
;
383 warn("Peer %q failed CHAP Session verification", name
);
386 if (ss
->flags
& AUTH_FAILED
) {
387 auth_peer_fail(0, PPP_CHAP
);
389 if ((ss
->flags
& AUTH_DONE
) == 0)
390 auth_peer_success(0, PPP_CHAP
,
393 if (chap_rechallenge_time
) {
394 ss
->flags
|= TIMEOUT_PENDING
;
395 TIMEOUT(chap_timeout
, ss
,
396 chap_rechallenge_time
);
399 ss
->flags
|= AUTH_DONE
;
404 * chap_verify_response - check whether the peer's response matches
405 * what we think it should be. Returns 1 if it does (authentication
406 * succeeded), or 0 if it doesn't.
409 chap_verify_response(char *name
, char *ourname
, int id
,
410 struct chap_digest_type
*digest
,
411 unsigned char *challenge
, unsigned char *response
,
412 char *message
, int message_space
)
415 unsigned char secret
[MAXSECRETLEN
];
418 /* Get the secret that the peer is supposed to know */
419 if (!get_secret(0, name
, ourname
, (char *)secret
, &secret_len
, 1)) {
420 error("No CHAP secret found for authenticating %q", name
);
424 ok
= digest
->verify_response(id
, name
, secret
, secret_len
, challenge
,
425 response
, message
, message_space
);
426 memset(secret
, 0, sizeof(secret
));
432 * chap_respond - Generate and send a response to a challenge.
435 chap_respond(struct chap_client_state
*cs
, int id
,
436 unsigned char *pkt
, int len
)
441 unsigned char response
[RESP_MAX_PKTLEN
];
442 char rname
[MAXNAMELEN
+1];
443 char secret
[MAXSECRETLEN
+1];
445 if ((cs
->flags
& (LOWERUP
| AUTH_STARTED
)) != (LOWERUP
| AUTH_STARTED
))
446 return; /* not ready */
447 if (len
< 2 || len
< pkt
[0] + 1)
448 return; /* too short */
450 nlen
= len
- (clen
+ 1);
452 /* Null terminate and clean remote name. */
453 slprintf(rname
, sizeof(rname
), "%.*v", nlen
, pkt
+ clen
+ 1);
455 /* Microsoft doesn't send their name back in the PPP packet */
456 if (explicit_remote
|| (remote_name
[0] != 0 && rname
[0] == 0))
457 strlcpy(rname
, remote_name
, sizeof(rname
));
459 /* get secret for authenticating ourselves with the specified host */
460 if (!get_secret(0, cs
->name
, rname
, secret
, &secret_len
, 0)) {
461 secret_len
= 0; /* assume null secret if can't find one */
462 warn("No CHAP secret found for authenticating us to %q", rname
);
466 MAKEHEADER(p
, PPP_CHAP
);
469 cs
->digest
->make_response(p
, id
, cs
->name
, pkt
,
470 secret
, secret_len
, cs
->priv
);
471 memset(secret
, 0, secret_len
);
474 nlen
= strlen(cs
->name
);
475 memcpy(p
+ clen
+ 1, cs
->name
, nlen
);
477 p
= response
+ PPP_HDRLEN
;
478 len
= CHAP_HDRLEN
+ clen
+ 1 + nlen
;
479 p
[0] = CHAP_RESPONSE
;
484 output(0, response
, PPP_HDRLEN
+ len
);
488 chap_handle_status(struct chap_client_state
*cs
, int code
, int id
,
489 unsigned char *pkt
, int len
)
491 const char *msg
= NULL
;
493 if ((cs
->flags
& (AUTH_DONE
|AUTH_STARTED
|LOWERUP
))
494 != (AUTH_STARTED
|LOWERUP
))
496 cs
->flags
|= AUTH_DONE
;
498 if (code
== CHAP_SUCCESS
) {
499 /* used for MS-CHAP v2 mutual auth, yuck */
500 if (cs
->digest
->check_success
!= NULL
) {
501 if (!(*cs
->digest
->check_success
)(pkt
, len
, cs
->priv
))
504 msg
= "CHAP authentication succeeded";
506 if (cs
->digest
->handle_failure
!= NULL
)
507 (*cs
->digest
->handle_failure
)(pkt
, len
);
509 msg
= "CHAP authentication failed";
513 info("%s: %.*v", msg
, len
, pkt
);
517 if (code
== CHAP_SUCCESS
)
518 auth_withpeer_success(0, PPP_CHAP
, cs
->digest
->code
);
520 cs
->flags
|= AUTH_FAILED
;
521 error("CHAP authentication failed");
522 auth_withpeer_fail(0, PPP_CHAP
);
527 chap_input(int unit
, unsigned char *pkt
, int pktlen
)
529 struct chap_client_state
*cs
= &client
;
530 struct chap_server_state
*ss
= &server
;
531 unsigned char code
, id
;
534 if (pktlen
< CHAP_HDRLEN
)
539 if (len
< CHAP_HDRLEN
|| len
> pktlen
)
545 chap_respond(cs
, id
, pkt
, len
);
548 chap_handle_response(ss
, id
, pkt
, len
);
552 chap_handle_status(cs
, code
, id
, pkt
, len
);
558 chap_protrej(int unit
)
560 struct chap_client_state
*cs
= &client
;
561 struct chap_server_state
*ss
= &server
;
563 if (ss
->flags
& TIMEOUT_PENDING
) {
564 ss
->flags
&= ~TIMEOUT_PENDING
;
565 UNTIMEOUT(chap_timeout
, ss
);
567 if (ss
->flags
& AUTH_STARTED
) {
569 auth_peer_fail(0, PPP_CHAP
);
571 if ((cs
->flags
& (AUTH_STARTED
|AUTH_DONE
)) == AUTH_STARTED
) {
572 cs
->flags
&= ~AUTH_STARTED
;
573 error("CHAP authentication failed due to protocol-reject");
574 auth_withpeer_fail(0, PPP_CHAP
);
579 * chap_print_pkt - print the contents of a CHAP packet.
581 static char *chap_code_names
[] = {
582 "Challenge", "Response", "Success", "Failure"
586 chap_print_pkt(unsigned char *p
, int plen
,
587 void (*printer
) __P((void *, char *, ...)), void *arg
)
593 if (plen
< CHAP_HDRLEN
)
598 if (len
< CHAP_HDRLEN
|| len
> plen
)
601 if (code
>= 1 && code
<= sizeof(chap_code_names
) / sizeof(char *))
602 printer(arg
, " %s", chap_code_names
[code
-1]);
604 printer(arg
, " code=0x%x", code
);
605 printer(arg
, " id=0x%x", id
);
616 nlen
= len
- clen
- 1;
618 for (; clen
> 0; --clen
) {
620 printer(arg
, "%.2x", x
);
622 printer(arg
, ">, name = ");
623 print_string((char *)p
, nlen
, printer
, arg
);
628 print_string((char *)p
, len
, printer
, arg
);
631 for (clen
= len
; clen
> 0; --clen
) {
633 printer(arg
, " %.2x", x
);
637 return len
+ CHAP_HDRLEN
;
640 struct protent chap_protent
= {
650 NULL
, /* datainput */
651 1, /* enabled_flag */
653 NULL
, /* data_name */
655 NULL
, /* check_options */