1 /***********************************************************************
5 * RADIUS plugin for pppd. Performs PAP and CHAP authentication using
8 * Copyright (C) 2002 Roaring Penguin Software Inc.
10 * Based on a patch for ipppd, which is:
11 * Copyright (C) 1996, Matjaz Godec <gody@elgo.si>
12 * Copyright (C) 1996, Lars Fenneberg <in5y050@public.uni-hamburg.de>
13 * Copyright (C) 1997, Miguel A.L. Paraz <map@iphil.net>
15 * Uses radiusclient library, which is:
16 * Copyright (C) 1995,1996,1997,1998 Lars Fenneberg <lf@elemental.net>
17 * Copyright (C) 2002 Roaring Penguin Software Inc.
19 * This plugin may be distributed according to the terms of the GNU
20 * General Public License, version 2 or (at your option) any later version.
22 ***********************************************************************/
23 static char const RCSID[] =
24 "$Id: radius.c,v 1.3 2002/03/01 14:39:18 dfs Exp $";
28 #include "radiusclient.h"
32 #include <sys/types.h>
37 static char *config_file = NULL;
39 static option_t Options[] = {
40 { "radius-config-file", o_string, &config_file },
44 static int radius_secret_check(void);
45 static int radius_pap_auth(char *user,
48 struct wordlist **paddrs,
49 struct wordlist **popts);
50 static int radius_chap_auth(char *user,
55 static void radius_ip_up(void *opaque, int arg);
56 static void radius_ip_down(void *opaque, int arg);
57 static void make_username_realm(char *user);
58 static int radius_setparams(VALUE_PAIR *vp, char *msg);
59 static void radius_choose_ip(u_int32_t *addrp);
60 static int radius_init(char *msg);
61 static int get_client_port(char *ifname);
62 static int radius_allowed_address(u_int32_t addr);
65 #define MAXSESSIONID 32
69 int accounting_started;
76 char user[MAXNAMELEN];
77 char config_file[MAXPATHLEN];
78 char session_id[MAXSESSIONID + 1];
80 SERVER *authserver; /* Authentication server to use */
81 SERVER *acctserver; /* Accounting server to use */
84 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
86 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
87 In that case, they override the values in the radiusclient.conf file */
88 void (*radius_pre_auth_hook)(char const *user,
90 SERVER **acctserver) = NULL;
92 static struct radius_state rstate;
94 char pppd_version[] = VERSION;
96 /**********************************************************************
97 * %FUNCTION: plugin_init
103 * Initializes RADIUS plugin.
104 ***********************************************************************/
108 pap_check_hook = radius_secret_check;
109 pap_auth_hook = radius_pap_auth;
111 chap_check_hook = radius_secret_check;
112 chap_auth_hook = radius_chap_auth;
114 ip_choose_hook = radius_choose_ip;
115 allowed_address_hook = radius_allowed_address;
117 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
118 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
120 memset(&rstate, 0, sizeof(rstate));
122 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
123 sizeof(rstate.config_file));
125 add_options(Options);
127 info("RADIUS plugin initialized.");
130 /**********************************************************************
131 * %FUNCTION: radius_secret_check
135 * 1 -- we are ALWAYS willing to supply a secret. :-)
137 * Tells pppd that we will try to authenticate the peer, and not to
138 * worry about looking in /etc/ppp/*-secrets
139 ***********************************************************************/
141 radius_secret_check(void)
146 /**********************************************************************
147 * %FUNCTION: radius_choose_ip
149 * addrp -- where to store the IP address
153 * If RADIUS server has specified an IP address, it is stored in *addrp.
154 ***********************************************************************/
156 radius_choose_ip(u_int32_t *addrp)
158 if (rstate.choose_ip) {
159 *addrp = rstate.ip_addr;
163 /**********************************************************************
164 * %FUNCTION: radius_pap_auth
166 * user -- user-name of peer
167 * passwd -- password supplied by peer
168 * msgp -- Message which will be sent in PAP response
169 * paddrs -- set to a list of possible peer IP addresses
170 * popts -- set to a list of additional pppd options
172 * 1 if we can authenticate, -1 if we cannot.
174 * Performs PAP authentication using RADIUS
175 ***********************************************************************/
177 radius_pap_auth(char *user,
180 struct wordlist **paddrs,
181 struct wordlist **popts)
183 VALUE_PAIR *send, *received;
186 static char radius_msg[BUF_LEN];
191 if (radius_init(radius_msg) < 0) {
195 /* Put user with potentially realm added in rstate.user */
196 make_username_realm(user);
198 if (radius_pre_auth_hook) {
199 radius_pre_auth_hook(rstate.user,
207 /* Hack... the "port" is the ppp interface number. Should really be
209 rstate.client_port = get_client_port(ifname);
212 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
215 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
217 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
218 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
219 if (*remote_number) {
220 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
224 if (rstate.authserver) {
225 result = rc_auth_using_server(rstate.authserver,
226 rstate.client_port, send,
227 &received, radius_msg);
229 result = rc_auth(rstate.client_port, send, &received, radius_msg);
232 if (result == OK_RC) {
233 if (radius_setparams(received, radius_msg) < 0) {
238 /* free value pairs */
239 rc_avpair_free(received);
240 rc_avpair_free(send);
242 return (result == OK_RC) ? 1 : 0;
245 /**********************************************************************
246 * %FUNCTION: radius_chap_auth
248 * user -- user-name of peer
249 * remmd -- hash received from peer
250 * remmd_len -- length of remmd
251 * cstate -- pppd's chap_state structure
253 * CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
255 * Performs CHAP authentication using RADIUS
256 ***********************************************************************/
258 radius_chap_auth(char *user,
263 VALUE_PAIR *send, *received;
265 static char radius_msg[BUF_LEN];
267 u_char cpassword[MD5_SIGNATURE_SIZE+1];
270 if (radius_init(radius_msg) < 0) {
271 error("%s", radius_msg);
275 /* we handle md5 digest at the moment */
276 if (cstate->chal_type != CHAP_DIGEST_MD5) {
277 error("RADIUS: Challenge type not MD5");
281 /* Put user with potentially realm added in rstate.user */
282 if (!rstate.done_chap_once) {
283 make_username_realm(user);
284 rstate.client_port = get_client_port (ifname);
285 if (radius_pre_auth_hook) {
286 radius_pre_auth_hook(rstate.user,
292 send = received = NULL;
295 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
298 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
300 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
303 * add the CHAP-Password and CHAP-Challenge fields
306 cpassword[0] = cstate->chal_id;
308 memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE);
310 rc_avpair_add(&send, PW_CHAP_PASSWORD, cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE);
312 rc_avpair_add(&send, PW_CHAP_CHALLENGE, cstate->challenge, cstate->chal_len, VENDOR_NONE);
315 * make authentication with RADIUS server
318 if (rstate.authserver) {
319 result = rc_auth_using_server(rstate.authserver,
320 rstate.client_port, send,
321 &received, radius_msg);
323 result = rc_auth(rstate.client_port, send, &received, radius_msg);
326 if (result == OK_RC) {
327 if (!rstate.done_chap_once) {
328 if (radius_setparams(received, radius_msg) < 0) {
329 error("%s", radius_msg);
332 rstate.done_chap_once = 1;
337 rc_avpair_free(received);
338 rc_avpair_free (send);
339 return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
342 /**********************************************************************
343 * %FUNCTION: make_username_realm
345 * user -- the user given to pppd
349 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
350 * then the default realm from the radiusclient config file is added.
351 ***********************************************************************/
353 make_username_realm(char *user)
357 if ( user != NULL ) {
358 strlcpy(rstate.user, user, sizeof(rstate.user));
363 default_realm = rc_conf_str("default_realm");
365 if (!strchr(rstate.user, '@') &&
367 (*default_realm != '\0')) {
368 strlcat(rstate.user, "@", sizeof(rstate.user));
369 strlcat(rstate.user, default_realm, sizeof(rstate.user));
373 /**********************************************************************
374 * %FUNCTION: radius_setparams
376 * vp -- received value-pairs
377 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
379 * >= 0 on success; -1 on failure
381 * Parses attributes sent by RADIUS server and sets them in pppd. Currently,
382 * used only to set IP address.
383 ***********************************************************************/
385 radius_setparams(VALUE_PAIR *vp, char *msg)
389 /* Send RADIUS attributes to anyone else who might be interested */
390 if (radius_attributes_hook) {
391 (*radius_attributes_hook)(vp);
395 * service type (if not framed then quit),
396 * new IP address (RADIUS can define static IP for some users),
400 switch (vp->attribute) {
401 case PW_SERVICE_TYPE:
402 /* check for service type */
403 /* if not FRAMED then exit */
404 if (vp->lvalue != PW_FRAMED) {
405 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
406 vp->lvalue, rstate.user);
410 case PW_FRAMED_PROTOCOL:
411 /* check for framed protocol type */
412 /* if not PPP then also exit */
413 if (vp->lvalue != PW_PPP) {
414 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
415 vp->lvalue, rstate.user);
420 case PW_FRAMED_IP_ADDRESS:
421 /* seting up remote IP addresses */
423 if (remote == 0xffffffff) {
424 /* 0xffffffff means user should be allowed to select one */
425 rstate.any_ip_addr_ok = 1;
426 } else if (remote != 0xfffffffe) {
427 /* 0xfffffffe means NAS should select an ip address */
428 remote = htonl(vp->lvalue);
429 if (bad_ip_adrs (remote)) {
430 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
431 remote, rstate.user);
434 rstate.choose_ip = 1;
435 rstate.ip_addr = remote;
444 /**********************************************************************
445 * %FUNCTION: radius_acct_start
451 * Sends a "start" accounting message to the RADIUS server.
452 ***********************************************************************/
454 radius_acct_start(void)
458 VALUE_PAIR *send = NULL;
459 ipcp_options *ho = &ipcp_hisoptions[0];
462 if (!rstate.initialized) {
466 rstate.start_time = time(NULL);
468 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
470 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
471 rstate.session_id, 0, VENDOR_NONE);
472 rc_avpair_add(&send, PW_USER_NAME,
473 rstate.user, 0, VENDOR_NONE);
475 av_type = PW_STATUS_START;
476 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
479 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
482 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
484 if (*remote_number) {
485 rc_avpair_add(&send, PW_CALLING_STATION_ID,
486 remote_number, 0, VENDOR_NONE);
490 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
494 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
496 hisaddr = ho->hisaddr;
497 av_type = htonl(hisaddr);
498 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
500 if (rstate.acctserver) {
501 result = rc_acct_using_server(rstate.acctserver,
502 rstate.client_port, send);
504 result = rc_acct(rstate.client_port, send);
507 rc_avpair_free(send);
509 if (result != OK_RC) {
510 /* RADIUS server could be down so make this a warning */
512 "Accounting START failed for %s", rstate.user);
514 rstate.accounting_started = 1;
518 /**********************************************************************
519 * %FUNCTION: radius_acct_stop
525 * Sends a "stop" accounting message to the RADIUS server.
526 ***********************************************************************/
528 radius_acct_stop(void)
531 VALUE_PAIR *send = NULL;
532 ipcp_options *ho = &ipcp_hisoptions[0];
536 if (!rstate.initialized) {
540 if (!rstate.accounting_started) {
544 rstate.accounting_started = 0;
545 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
548 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
550 av_type = PW_STATUS_STOP;
551 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
554 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
557 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
560 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
563 if (link_stats_valid) {
564 av_type = link_connect_time;
565 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
567 av_type = link_stats.bytes_out;
568 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
570 av_type = link_stats.bytes_in;
571 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
573 av_type = link_stats.pkts_out;
574 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
576 av_type = link_stats.pkts_in;
577 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
580 if (*remote_number) {
581 rc_avpair_add(&send, PW_CALLING_STATION_ID,
582 remote_number, 0, VENDOR_NONE);
586 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
588 hisaddr = ho->hisaddr;
589 av_type = htonl(hisaddr);
590 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
592 if (rstate.acctserver) {
593 result = rc_acct_using_server(rstate.acctserver,
594 rstate.client_port, send);
596 result = rc_acct(rstate.client_port, send);
599 if (result != OK_RC) {
600 /* RADIUS server could be down so make this a warning */
602 "Accounting STOP failed for %s", rstate.user);
604 rc_avpair_free(send);
607 /**********************************************************************
608 * %FUNCTION: radius_ip_up
615 * Called when IPCP is up. We'll do a start-accounting record.
616 ***********************************************************************/
618 radius_ip_up(void *opaque, int arg)
623 /**********************************************************************
624 * %FUNCTION: radius_ip_down
631 * Called when IPCP is down. We'll do a stop-accounting record.
632 ***********************************************************************/
634 radius_ip_down(void *opaque, int arg)
639 /**********************************************************************
640 * %FUNCTION: radius_init
642 * msg -- buffer of size BUF_LEN for error message
644 * negative on failure; non-negative on success
646 * Initializes radiusclient library
647 ***********************************************************************/
649 radius_init(char *msg)
651 if (rstate.initialized) {
655 if (config_file && *config_file) {
656 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
659 rstate.initialized = 1;
661 if (rc_read_config(rstate.config_file) != 0) {
662 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
667 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
668 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
669 rc_conf_str("dictionary"));
673 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
674 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
675 rc_conf_str("mapfile"));
681 /**********************************************************************
682 * %FUNCTION: get_client_port
684 * ifname -- PPP interface name (e.g. "ppp7")
686 * The NAS port number (e.g. 7)
688 * Extracts the port number from the interface name
689 ***********************************************************************/
691 get_client_port(char *ifname)
694 if (sscanf(ifname, "ppp%d", &port) == 1) {
697 return rc_map2id(ifname);
700 /**********************************************************************
701 * %FUNCTION: radius_allowed_address
705 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
707 ***********************************************************************/
709 radius_allowed_address(u_int32_t addr)
711 ipcp_options *wo = &ipcp_wantoptions[0];
713 if (!rstate.choose_ip) {
714 /* If RADIUS server said any address is OK, then fine... */
715 if (rstate.any_ip_addr_ok) {
719 /* Sigh... if an address was supplied for remote host in pppd
720 options, it has to match that. */
721 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
727 if (addr == rstate.ip_addr) return 1;
731 /* Useful for other plugins */
732 char *radius_logged_in_user(void)