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.4 2002/03/01 15:16:51 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 if (vp->vendorcode == VENDOR_NONE) {
401 switch (vp->attribute) {
402 case PW_SERVICE_TYPE:
403 /* check for service type */
404 /* if not FRAMED then exit */
405 if (vp->lvalue != PW_FRAMED) {
406 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
407 vp->lvalue, rstate.user);
411 case PW_FRAMED_PROTOCOL:
412 /* check for framed protocol type */
413 /* if not PPP then also exit */
414 if (vp->lvalue != PW_PPP) {
415 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
416 vp->lvalue, rstate.user);
421 case PW_FRAMED_IP_ADDRESS:
422 /* seting up remote IP addresses */
424 if (remote == 0xffffffff) {
425 /* 0xffffffff means user should be allowed to select one */
426 rstate.any_ip_addr_ok = 1;
427 } else if (remote != 0xfffffffe) {
428 /* 0xfffffffe means NAS should select an ip address */
429 remote = htonl(vp->lvalue);
430 if (bad_ip_adrs (remote)) {
431 slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
432 remote, rstate.user);
435 rstate.choose_ip = 1;
436 rstate.ip_addr = remote;
446 /**********************************************************************
447 * %FUNCTION: radius_acct_start
453 * Sends a "start" accounting message to the RADIUS server.
454 ***********************************************************************/
456 radius_acct_start(void)
460 VALUE_PAIR *send = NULL;
461 ipcp_options *ho = &ipcp_hisoptions[0];
464 if (!rstate.initialized) {
468 rstate.start_time = time(NULL);
470 strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
472 rc_avpair_add(&send, PW_ACCT_SESSION_ID,
473 rstate.session_id, 0, VENDOR_NONE);
474 rc_avpair_add(&send, PW_USER_NAME,
475 rstate.user, 0, VENDOR_NONE);
477 av_type = PW_STATUS_START;
478 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
481 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
484 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
486 if (*remote_number) {
487 rc_avpair_add(&send, PW_CALLING_STATION_ID,
488 remote_number, 0, VENDOR_NONE);
492 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
496 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
498 hisaddr = ho->hisaddr;
499 av_type = htonl(hisaddr);
500 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
502 if (rstate.acctserver) {
503 result = rc_acct_using_server(rstate.acctserver,
504 rstate.client_port, send);
506 result = rc_acct(rstate.client_port, send);
509 rc_avpair_free(send);
511 if (result != OK_RC) {
512 /* RADIUS server could be down so make this a warning */
514 "Accounting START failed for %s", rstate.user);
516 rstate.accounting_started = 1;
520 /**********************************************************************
521 * %FUNCTION: radius_acct_stop
527 * Sends a "stop" accounting message to the RADIUS server.
528 ***********************************************************************/
530 radius_acct_stop(void)
533 VALUE_PAIR *send = NULL;
534 ipcp_options *ho = &ipcp_hisoptions[0];
538 if (!rstate.initialized) {
542 if (!rstate.accounting_started) {
546 rstate.accounting_started = 0;
547 rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
550 rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
552 av_type = PW_STATUS_STOP;
553 rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
556 rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
559 rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
562 rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
565 if (link_stats_valid) {
566 av_type = link_connect_time;
567 rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
569 av_type = link_stats.bytes_out;
570 rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
572 av_type = link_stats.bytes_in;
573 rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
575 av_type = link_stats.pkts_out;
576 rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
578 av_type = link_stats.pkts_in;
579 rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
582 if (*remote_number) {
583 rc_avpair_add(&send, PW_CALLING_STATION_ID,
584 remote_number, 0, VENDOR_NONE);
588 rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
590 hisaddr = ho->hisaddr;
591 av_type = htonl(hisaddr);
592 rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
594 if (rstate.acctserver) {
595 result = rc_acct_using_server(rstate.acctserver,
596 rstate.client_port, send);
598 result = rc_acct(rstate.client_port, send);
601 if (result != OK_RC) {
602 /* RADIUS server could be down so make this a warning */
604 "Accounting STOP failed for %s", rstate.user);
606 rc_avpair_free(send);
609 /**********************************************************************
610 * %FUNCTION: radius_ip_up
617 * Called when IPCP is up. We'll do a start-accounting record.
618 ***********************************************************************/
620 radius_ip_up(void *opaque, int arg)
625 /**********************************************************************
626 * %FUNCTION: radius_ip_down
633 * Called when IPCP is down. We'll do a stop-accounting record.
634 ***********************************************************************/
636 radius_ip_down(void *opaque, int arg)
641 /**********************************************************************
642 * %FUNCTION: radius_init
644 * msg -- buffer of size BUF_LEN for error message
646 * negative on failure; non-negative on success
648 * Initializes radiusclient library
649 ***********************************************************************/
651 radius_init(char *msg)
653 if (rstate.initialized) {
657 if (config_file && *config_file) {
658 strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
661 rstate.initialized = 1;
663 if (rc_read_config(rstate.config_file) != 0) {
664 slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
669 if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
670 slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
671 rc_conf_str("dictionary"));
675 if (rc_read_mapfile(rc_conf_str("mapfile")) != 0) {
676 slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
677 rc_conf_str("mapfile"));
683 /**********************************************************************
684 * %FUNCTION: get_client_port
686 * ifname -- PPP interface name (e.g. "ppp7")
688 * The NAS port number (e.g. 7)
690 * Extracts the port number from the interface name
691 ***********************************************************************/
693 get_client_port(char *ifname)
696 if (sscanf(ifname, "ppp%d", &port) == 1) {
699 return rc_map2id(ifname);
702 /**********************************************************************
703 * %FUNCTION: radius_allowed_address
707 * 1 if we're allowed to use that IP address; 0 if not; -1 if we do
709 ***********************************************************************/
711 radius_allowed_address(u_int32_t addr)
713 ipcp_options *wo = &ipcp_wantoptions[0];
715 if (!rstate.choose_ip) {
716 /* If RADIUS server said any address is OK, then fine... */
717 if (rstate.any_ip_addr_ok) {
721 /* Sigh... if an address was supplied for remote host in pppd
722 options, it has to match that. */
723 if (wo->hisaddr != 0 && wo->hisaddr == addr) {
729 if (addr == rstate.ip_addr) return 1;
733 /* Useful for other plugins */
734 char *radius_logged_in_user(void)