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.2 2002/02/08 17:28:31 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);
64 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
65 void (*radius_pre_auth_hook)(char const *user) = NULL;
68 #define MAXSESSIONID 32
72 int accounting_started;
79 char user[MAXNAMELEN];
80 char config_file[MAXPATHLEN];
81 char session_id[MAXSESSIONID + 1];
85 static struct radius_state rstate;
87 char pppd_version[] = VERSION;
89 /**********************************************************************
90 * %FUNCTION: plugin_init
96 * Initializes RADIUS plugin.
97 ***********************************************************************/
101 pap_check_hook = radius_secret_check;
102 pap_auth_hook = radius_pap_auth;
104 chap_check_hook = radius_secret_check;
105 chap_auth_hook = radius_chap_auth;
107 ip_choose_hook = radius_choose_ip;
108 allowed_address_hook = radius_allowed_address;
110 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
111 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
113 memset(&rstate, 0, sizeof(rstate));
115 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
116 sizeof(rstate.config_file));
118 add_options(Options);
120 info("RADIUS plugin initialized.");
123 /**********************************************************************
124 * %FUNCTION: radius_secret_check
128 * 1 -- we are ALWAYS willing to supply a secret. :-)
130 * Tells pppd that we will try to authenticate the peer, and not to
131 * worry about looking in /etc/ppp/*-secrets
132 ***********************************************************************/
134 radius_secret_check(void)
139 /**********************************************************************
140 * %FUNCTION: radius_choose_ip
142 * addrp -- where to store the IP address
146 * If RADIUS server has specified an IP address, it is stored in *addrp.
147 ***********************************************************************/
149 radius_choose_ip(u_int32_t *addrp)
151 if (rstate.choose_ip) {
152 *addrp = rstate.ip_addr;
156 /**********************************************************************
157 * %FUNCTION: radius_pap_auth
159 * user -- user-name of peer
160 * passwd -- password supplied by peer
161 * msgp -- Message which will be sent in PAP response
162 * paddrs -- set to a list of possible peer IP addresses
163 * popts -- set to a list of additional pppd options
165 * 1 if we can authenticate, -1 if we cannot.
167 * Performs PAP authentication using RADIUS
168 ***********************************************************************/
170 radius_pap_auth(char *user,
173 struct wordlist **paddrs,
174 struct wordlist **popts)
176 VALUE_PAIR *send, *received;
179 static char radius_msg[BUF_LEN];
184 if (radius_init(radius_msg) < 0) {
188 /* Put user with potentially realm added in rstate.user */
189 make_username_realm(user);
191 if (radius_pre_auth_hook) {
192 radius_pre_auth_hook(rstate.user);
198 /* Hack... the "port" is the ppp interface number. Should really be
200 rstate.client_port = get_client_port(ifname);
203 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
206 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
208 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
209 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
210 if (*remote_number) {
211 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
215 result = rc_auth(rstate.client_port, send, &received, radius_msg);
217 if (result == OK_RC) {
218 if (radius_setparams(received, radius_msg) < 0) {
223 /* free value pairs */
224 rc_avpair_free(received);
225 rc_avpair_free(send);
227 return (result == OK_RC) ? 1 : 0;
230 /**********************************************************************
231 * %FUNCTION: radius_chap_auth
233 * user -- user-name of peer
234 * remmd -- hash received from peer
235 * remmd_len -- length of remmd
236 * cstate -- pppd's chap_state structure
238 * CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
240 * Performs CHAP authentication using RADIUS
241 ***********************************************************************/
243 radius_chap_auth(char *user,
248 VALUE_PAIR *send, *received;
250 static char radius_msg[BUF_LEN];
252 u_char cpassword[MD5_SIGNATURE_SIZE+1];
255 if (radius_init(radius_msg) < 0) {
256 error("%s", radius_msg);
260 /* we handle md5 digest at the moment */
261 if (cstate->chal_type != CHAP_DIGEST_MD5) {
262 error("RADIUS: Challenge type not MD5");
266 /* Put user with potentially realm added in rstate.user */
267 if (!rstate.done_chap_once) {
268 make_username_realm(user);
269 rstate.client_port = get_client_port (ifname);
270 if (radius_pre_auth_hook) {
271 radius_pre_auth_hook(rstate.user);
275 send = received = NULL;
278 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
281 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
283 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
286 * add the CHAP-Password and CHAP-Challenge fields
289 cpassword[0] = cstate->chal_id;
291 memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE);
293 rc_avpair_add(&send, PW_CHAP_PASSWORD, cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE);
295 rc_avpair_add(&send, PW_CHAP_CHALLENGE, cstate->challenge, cstate->chal_len, VENDOR_NONE);
298 * make authentication with RADIUS server
301 result = rc_auth (rstate.client_port, send, &received, radius_msg);
303 if (result == OK_RC) {
304 if (!rstate.done_chap_once) {
305 if (radius_setparams(received, radius_msg) < 0) {
306 error("%s", radius_msg);
309 rstate.done_chap_once = 1;
314 rc_avpair_free(received);
315 rc_avpair_free (send);
316 return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
319 /**********************************************************************
320 * %FUNCTION: make_username_realm
322 * user -- the user given to pppd
326 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
327 * then the default realm from the radiusclient config file is added.
328 ***********************************************************************/
330 make_username_realm(char *user)
334 if ( user != NULL ) {
335 strlcpy(rstate.user, user, sizeof(rstate.user));
340 default_realm = rc_conf_str("default_realm");
342 if (!strchr(rstate.user, '@') &&
344 (*default_realm != '\0')) {
345 strlcat(rstate.user, "@", sizeof(rstate.user));
346 strlcat(rstate.user, default_realm, sizeof(rstate.user));
350 /**********************************************************************
351 * %FUNCTION: radius_setparams
353 * vp -- received value-pairs
354 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
356 * >= 0 on success; -1 on failure
358 * Parses attributes sent by RADIUS server and sets them in pppd. Currently,
359 * used only to set IP address.
360 ***********************************************************************/
362 radius_setparams(VALUE_PAIR *vp, char *msg)
366 /* Send RADIUS attributes to anyone else who might be interested */
367 if (radius_attributes_hook) {
368 (*radius_attributes_hook)(vp);
372 * service type (if not framed then quit),
373 * new IP address (RADIUS can define static IP for some users),
377 switch (vp->attribute) {
378 case PW_SERVICE_TYPE:
379 /* check for service type */
380 /* if not FRAMED then exit */
381 if (vp->lvalue != PW_FRAMED) {
382 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
383 vp->lvalue, rstate.user);
387 case PW_FRAMED_PROTOCOL:
388 /* check for framed protocol type */
389 /* if not PPP then also exit */
390 if (vp->lvalue != PW_PPP) {
391 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
392 vp->lvalue, rstate.user);
397 case PW_FRAMED_IP_ADDRESS:
398 /* seting up remote IP addresses */
400 if (remote == 0xffffffff) {
401 /* 0xffffffff means user should be allowed to select one */
402 rstate.any_ip_addr_ok = 1;
403 } else if (remote != 0xfffffffe) {
404 /* 0xfffffffe means NAS should select an ip address */
405 remote = htonl(vp->lvalue);
406 if (bad_ip_adrs (remote)) {
407 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
408 remote, rstate.user);
411 rstate.choose_ip = 1;
412 rstate.ip_addr = remote;
421 /**********************************************************************
422 * %FUNCTION: radius_acct_start
428 * Sends a "start" accounting message to the RADIUS server.
429 ***********************************************************************/
431 radius_acct_start(void)
435 VALUE_PAIR *send = NULL;
436 ipcp_options *ho = &ipcp_hisoptions[0];
439 if (!rstate.initialized) {
443 rstate.start_time = time(NULL);
445 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
447 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
448 rstate.session_id, 0, VENDOR_NONE);
449 rc_avpair_add(&send, PW_USER_NAME,
450 rstate.user, 0, VENDOR_NONE);
452 av_type = PW_STATUS_START;
453 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
456 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
459 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
461 if (*remote_number) {
462 rc_avpair_add(&send, PW_CALLING_STATION_ID,
463 remote_number, 0, VENDOR_NONE);
467 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
471 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
473 hisaddr = ho->hisaddr;
474 av_type = htonl(hisaddr);
475 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
477 result = rc_acct(rstate.client_port, send);
479 rc_avpair_free(send);
481 if (result != OK_RC) {
482 /* RADIUS server could be down so make this a warning */
484 "Accounting START failed for %s", rstate.user);
486 rstate.accounting_started = 1;
490 /**********************************************************************
491 * %FUNCTION: radius_acct_stop
497 * Sends a "stop" accounting message to the RADIUS server.
498 ***********************************************************************/
500 radius_acct_stop(void)
503 VALUE_PAIR *send = NULL;
504 ipcp_options *ho = &ipcp_hisoptions[0];
508 if (!rstate.initialized) {
512 if (!rstate.accounting_started) {
516 rstate.accounting_started = 0;
517 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
520 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
522 av_type = PW_STATUS_STOP;
523 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
526 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
529 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
532 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
535 if (link_stats_valid) {
536 av_type = link_connect_time;
537 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
539 av_type = link_stats.bytes_out;
540 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
542 av_type = link_stats.bytes_in;
543 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
545 av_type = link_stats.pkts_out;
546 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
548 av_type = link_stats.pkts_in;
549 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
552 if (*remote_number) {
553 rc_avpair_add(&send, PW_CALLING_STATION_ID,
554 remote_number, 0, VENDOR_NONE);
558 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
560 hisaddr = ho->hisaddr;
561 av_type = htonl(hisaddr);
562 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
564 result = rc_acct(rstate.client_port, send);
565 if (result != OK_RC) {
566 /* RADIUS server could be down so make this a warning */
568 "Accounting STOP failed for %s", rstate.user);
570 rc_avpair_free(send);
573 /**********************************************************************
574 * %FUNCTION: radius_ip_up
581 * Called when IPCP is up. We'll do a start-accounting record.
582 ***********************************************************************/
584 radius_ip_up(void *opaque, int arg)
589 /**********************************************************************
590 * %FUNCTION: radius_ip_down
597 * Called when IPCP is down. We'll do a stop-accounting record.
598 ***********************************************************************/
600 radius_ip_down(void *opaque, int arg)
605 /**********************************************************************
606 * %FUNCTION: radius_init
608 * msg -- buffer of size BUF_LEN for error message
610 * negative on failure; non-negative on success
612 * Initializes radiusclient library
613 ***********************************************************************/
615 radius_init(char *msg)
617 if (rstate.initialized) {
621 if (config_file && *config_file) {
622 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
625 rstate.initialized = 1;
627 if (rc_read_config(rstate.config_file) != 0) {
628 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
633 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
634 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
635 rc_conf_str("dictionary"));
639 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
640 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
641 rc_conf_str("mapfile"));
647 /**********************************************************************
648 * %FUNCTION: get_client_port
650 * ifname -- PPP interface name (e.g. "ppp7")
652 * The NAS port number (e.g. 7)
654 * Extracts the port number from the interface name
655 ***********************************************************************/
657 get_client_port(char *ifname)
660 if (sscanf(ifname, "ppp%d", &port) == 1) {
663 return rc_map2id(ifname);
666 /**********************************************************************
667 * %FUNCTION: radius_allowed_address
671 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
673 ***********************************************************************/
675 radius_allowed_address(u_int32_t addr)
677 ipcp_options *wo = &ipcp_wantoptions[0];
679 if (!rstate.choose_ip) {
680 /* If RADIUS server said any address is OK, then fine... */
681 if (rstate.any_ip_addr_ok) {
685 /* Sigh... if an address was supplied for remote host in pppd
686 options, it has to match that. */
687 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
693 if (addr == rstate.ip_addr) return 1;
697 /* Useful for other plugins */
698 char *radius_logged_in_user(void)