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.1 2002/01/22 16:03:00 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;
67 #define MAXSESSIONID 32
71 int accounting_started;
78 char user[MAXNAMELEN];
79 char config_file[MAXPATHLEN];
80 char session_id[MAXSESSIONID + 1];
84 static struct radius_state rstate;
86 char pppd_version[] = VERSION;
88 /**********************************************************************
89 * %FUNCTION: plugin_init
95 * Initializes RADIUS plugin.
96 ***********************************************************************/
100 pap_check_hook = radius_secret_check;
101 pap_auth_hook = radius_pap_auth;
103 chap_check_hook = radius_secret_check;
104 chap_auth_hook = radius_chap_auth;
106 ip_choose_hook = radius_choose_ip;
107 allowed_address_hook = radius_allowed_address;
109 add_notifier(&ip_up_notifier, radius_ip_up, NULL);
110 add_notifier(&ip_down_notifier, radius_ip_down, NULL);
112 memset(&rstate, 0, sizeof(rstate));
114 strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
115 sizeof(rstate.config_file));
117 add_options(Options);
119 info("RADIUS plugin initialized.");
122 /**********************************************************************
123 * %FUNCTION: radius_secret_check
127 * 1 -- we are ALWAYS willing to supply a secret. :-)
129 * Tells pppd that we will try to authenticate the peer, and not to
130 * worry about looking in /etc/ppp/*-secrets
131 ***********************************************************************/
133 radius_secret_check(void)
138 /**********************************************************************
139 * %FUNCTION: radius_choose_ip
141 * addrp -- where to store the IP address
145 * If RADIUS server has specified an IP address, it is stored in *addrp.
146 ***********************************************************************/
148 radius_choose_ip(u_int32_t *addrp)
150 if (rstate.choose_ip) {
151 *addrp = rstate.ip_addr;
155 /**********************************************************************
156 * %FUNCTION: radius_pap_auth
158 * user -- user-name of peer
159 * passwd -- password supplied by peer
160 * msgp -- Message which will be sent in PAP response
161 * paddrs -- set to a list of possible peer IP addresses
162 * popts -- set to a list of additional pppd options
164 * 1 if we can authenticate, -1 if we cannot.
166 * Performs PAP authentication using RADIUS
167 ***********************************************************************/
169 radius_pap_auth(char *user,
172 struct wordlist **paddrs,
173 struct wordlist **popts)
175 VALUE_PAIR *send, *received;
178 static char radius_msg[BUF_LEN];
183 if (radius_init(radius_msg) < 0) {
190 /* Hack... the "port" is the ppp interface number. Should really be
192 rstate.client_port = get_client_port(ifname);
195 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
198 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
200 /* Put user with potentially realm added in rstate.user */
201 make_username_realm(user);
203 rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
204 rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
205 if (*remote_number) {
206 rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
210 result = rc_auth(rstate.client_port, send, &received, radius_msg);
212 if (result == OK_RC) {
213 if (radius_setparams(received, radius_msg) < 0) {
218 /* free value pairs */
219 rc_avpair_free(received);
220 rc_avpair_free(send);
222 return (result == OK_RC) ? 1 : 0;
225 /**********************************************************************
226 * %FUNCTION: radius_chap_auth
228 * user -- user-name of peer
229 * remmd -- hash received from peer
230 * remmd_len -- length of remmd
231 * cstate -- pppd's chap_state structure
233 * CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
235 * Performs CHAP authentication using RADIUS
236 ***********************************************************************/
238 radius_chap_auth(char *user,
243 VALUE_PAIR *send, *received;
245 static char radius_msg[BUF_LEN];
247 u_char cpassword[MD5_SIGNATURE_SIZE+1];
250 if (radius_init(radius_msg) < 0) {
251 error("%s", radius_msg);
255 /* we handle md5 digest at the moment */
256 if (cstate->chal_type != CHAP_DIGEST_MD5) {
257 error("RADIUS: Challenge type not MD5");
261 /* Put user with potentially realm added in rstate.user */
262 if (!rstate.done_chap_once) {
263 make_username_realm(user);
264 rstate.client_port = get_client_port (ifname);
267 send = received = NULL;
270 rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
273 rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
275 rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
278 * add the CHAP-Password and CHAP-Challenge fields
281 cpassword[0] = cstate->chal_id;
283 memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE);
285 rc_avpair_add(&send, PW_CHAP_PASSWORD, cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE);
287 rc_avpair_add(&send, PW_CHAP_CHALLENGE, cstate->challenge, cstate->chal_len, VENDOR_NONE);
290 * make authentication with RADIUS server
293 result = rc_auth (rstate.client_port, send, &received, radius_msg);
295 if (result == OK_RC) {
296 if (!rstate.done_chap_once) {
297 if (radius_setparams(received, radius_msg) < 0) {
298 error("%s", radius_msg);
301 rstate.done_chap_once = 1;
306 rc_avpair_free(received);
307 rc_avpair_free (send);
308 return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
311 /**********************************************************************
312 * %FUNCTION: make_username_realm
314 * user -- the user given to pppd
318 * Copies user into rstate.user. If it lacks a realm (no "@domain" part),
319 * then the default realm from the radiusclient config file is added.
320 ***********************************************************************/
322 make_username_realm(char *user)
326 if ( user != NULL ) {
327 strlcpy(rstate.user, user, sizeof(rstate.user));
332 default_realm = rc_conf_str("default_realm");
334 if (!strchr(rstate.user, '@') &&
336 (*default_realm != '\0')) {
337 strlcat(rstate.user, "@", sizeof(rstate.user));
338 strlcat(rstate.user, default_realm, sizeof(rstate.user));
342 /**********************************************************************
343 * %FUNCTION: radius_setparams
345 * vp -- received value-pairs
346 * msg -- buffer in which to place error message. Holds up to BUF_LEN chars
348 * >= 0 on success; -1 on failure
350 * Parses attributes sent by RADIUS server and sets them in pppd. Currently,
351 * used only to set IP address.
352 ***********************************************************************/
354 radius_setparams(VALUE_PAIR *vp, char *msg)
358 /* Send RADIUS attributes to anyone else who might be interested */
359 if (radius_attributes_hook) {
360 (*radius_attributes_hook)(vp);
364 * service type (if not framed then quit),
365 * new IP address (RADIUS can define static IP for some users),
369 switch (vp->attribute) {
370 case PW_SERVICE_TYPE:
371 /* check for service type */
372 /* if not FRAMED then exit */
373 if (vp->lvalue != PW_FRAMED) {
374 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
375 vp->lvalue, rstate.user);
379 case PW_FRAMED_PROTOCOL:
380 /* check for framed protocol type */
381 /* if not PPP then also exit */
382 if (vp->lvalue != PW_PPP) {
383 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
384 vp->lvalue, rstate.user);
389 case PW_FRAMED_IP_ADDRESS:
390 /* seting up remote IP addresses */
392 if (remote == 0xffffffff) {
393 /* 0xffffffff means user should be allowed to select one */
394 rstate.any_ip_addr_ok = 1;
395 } else if (remote != 0xfffffffe) {
396 /* 0xfffffffe means NAS should select an ip address */
397 remote = htonl(vp->lvalue);
398 if (bad_ip_adrs (remote)) {
399 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
400 remote, rstate.user);
403 rstate.choose_ip = 1;
404 rstate.ip_addr = remote;
413 /**********************************************************************
414 * %FUNCTION: radius_acct_start
420 * Sends a "start" accounting message to the RADIUS server.
421 ***********************************************************************/
423 radius_acct_start(void)
427 VALUE_PAIR *send = NULL;
428 ipcp_options *ho = &ipcp_hisoptions[0];
431 if (!rstate.initialized) {
435 rstate.start_time = time(NULL);
437 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
439 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
440 rstate.session_id, 0, VENDOR_NONE);
441 rc_avpair_add(&send, PW_USER_NAME,
442 rstate.user, 0, VENDOR_NONE);
444 av_type = PW_STATUS_START;
445 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
448 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
451 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
453 if (*remote_number) {
454 rc_avpair_add(&send, PW_CALLING_STATION_ID,
455 remote_number, 0, VENDOR_NONE);
459 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
463 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
465 hisaddr = ho->hisaddr;
466 av_type = htonl(hisaddr);
467 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
469 result = rc_acct(rstate.client_port, send);
471 rc_avpair_free(send);
473 if (result != OK_RC) {
474 /* RADIUS server could be down so make this a warning */
476 "Accounting START failed for %s", rstate.user);
478 rstate.accounting_started = 1;
482 /**********************************************************************
483 * %FUNCTION: radius_acct_stop
489 * Sends a "stop" accounting message to the RADIUS server.
490 ***********************************************************************/
492 radius_acct_stop(void)
495 VALUE_PAIR *send = NULL;
496 ipcp_options *ho = &ipcp_hisoptions[0];
500 if (!rstate.initialized) {
504 if (!rstate.accounting_started) {
508 rstate.accounting_started = 0;
509 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
512 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
514 av_type = PW_STATUS_STOP;
515 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
518 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
521 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
524 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
527 if (link_stats_valid) {
528 av_type = link_connect_time;
529 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
531 av_type = link_stats.bytes_out;
532 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
534 av_type = link_stats.bytes_in;
535 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
537 av_type = link_stats.pkts_out;
538 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
540 av_type = link_stats.pkts_in;
541 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
544 if (*remote_number) {
545 rc_avpair_add(&send, PW_CALLING_STATION_ID,
546 remote_number, 0, VENDOR_NONE);
550 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
552 hisaddr = ho->hisaddr;
553 av_type = htonl(hisaddr);
554 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
556 result = rc_acct(rstate.client_port, send);
557 if (result != OK_RC) {
558 /* RADIUS server could be down so make this a warning */
560 "Accounting STOP failed for %s", rstate.user);
562 rc_avpair_free(send);
565 /**********************************************************************
566 * %FUNCTION: radius_ip_up
573 * Called when IPCP is up. We'll do a start-accounting record.
574 ***********************************************************************/
576 radius_ip_up(void *opaque, int arg)
581 /**********************************************************************
582 * %FUNCTION: radius_ip_down
589 * Called when IPCP is down. We'll do a stop-accounting record.
590 ***********************************************************************/
592 radius_ip_down(void *opaque, int arg)
597 /**********************************************************************
598 * %FUNCTION: radius_init
600 * msg -- buffer of size BUF_LEN for error message
602 * negative on failure; non-negative on success
604 * Initializes radiusclient library
605 ***********************************************************************/
607 radius_init(char *msg)
609 if (rstate.initialized) {
613 if (config_file && *config_file) {
614 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
617 rstate.initialized = 1;
619 if (rc_read_config(rstate.config_file) != 0) {
620 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
625 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
626 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
627 rc_conf_str("dictionary"));
631 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
632 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
633 rc_conf_str("mapfile"));
639 /**********************************************************************
640 * %FUNCTION: get_client_port
642 * ifname -- PPP interface name (e.g. "ppp7")
644 * The NAS port number (e.g. 7)
646 * Extracts the port number from the interface name
647 ***********************************************************************/
649 get_client_port(char *ifname)
652 if (sscanf(ifname, "ppp%d", &port) == 1) {
655 return rc_map2id(ifname);
658 /**********************************************************************
659 * %FUNCTION: radius_allowed_address
663 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
665 ***********************************************************************/
667 radius_allowed_address(u_int32_t addr)
669 ipcp_options *wo = &ipcp_wantoptions[0];
671 if (!rstate.choose_ip) {
672 /* If RADIUS server said any address is OK, then fine... */
673 if (rstate.any_ip_addr_ok) {
677 /* Sigh... if an address was supplied for remote host in pppd
678 options, it has to match that. */
679 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
685 if (addr == rstate.ip_addr) return 1;
689 /* Useful for other plugins */
690 char *radius_logged_in_user(void)