]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radius.c
pppd: Add support for registering ppp interface via Linux rtnetlink API
[ppp.git] / pppd / plugins / radius / radius.c
1 /***********************************************************************
2 *
3 * radius.c
4 *
5 * RADIUS plugin for pppd.  Performs PAP, CHAP, MS-CHAP, MS-CHAPv2
6 * authentication using RADIUS.
7 *
8 * Copyright (C) 2002 Roaring Penguin Software Inc.
9 *
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>
14 *
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.
18 *
19 * MPPE support is by Ralf Hofmann, <ralf.hofmann@elvido.net>, with
20 * modification from Frank Cusack, <frank@google.com>.
21 *
22 * This plugin may be distributed according to the terms of the GNU
23 * General Public License, version 2 or (at your option) any later version.
24 *
25 ***********************************************************************/
26 static char const RCSID[] =
27 "$Id: radius.c,v 1.32 2008/05/26 09:18:08 paulus Exp $";
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include "pppd.h"
34 #include "chap-new.h"
35 #ifdef CHAPMS
36 #include "chap_ms.h"
37 #ifdef MPPE
38 #include "mppe.h"
39 #include "md5.h"
40 #endif
41 #endif
42 #include "radiusclient.h"
43 #include "fsm.h"
44 #include "ipcp.h"
45 #include <syslog.h>
46 #include <sys/types.h>
47 #include <sys/time.h>
48 #include <string.h>
49 #include <netinet/in.h>
50 #include <stdlib.h>
51
52 #define BUF_LEN 1024
53
54 #define MD5_HASH_SIZE   16
55
56 #define MSDNS 1
57
58 static char *config_file = NULL;
59 static int add_avp(char **);
60 static struct avpopt {
61     char *vpstr;
62     struct avpopt *next;
63 } *avpopt = NULL;
64 static bool portnummap = 0;
65
66 static option_t Options[] = {
67     { "radius-config-file", o_string, &config_file },
68     { "avpair", o_special, add_avp },
69     { "map-to-ttyname", o_bool, &portnummap,
70         "Set Radius NAS-Port attribute value via libradiusclient library", OPT_PRIO | 1 },
71     { "map-to-ifname", o_bool, &portnummap,
72         "Set Radius NAS-Port attribute to number as in interface name (Default)", OPT_PRIOSUB | 0 },
73     { NULL }
74 };
75
76 static int radius_secret_check(void);
77 static int radius_pap_auth(char *user,
78                            char *passwd,
79                            char **msgp,
80                            struct wordlist **paddrs,
81                            struct wordlist **popts);
82 static int radius_chap_verify(char *user, char *ourname, int id,
83                               struct chap_digest_type *digest,
84                               unsigned char *challenge,
85                               unsigned char *response,
86                               char *message, int message_space);
87
88 static void radius_ip_up(void *opaque, int arg);
89 static void radius_ip_down(void *opaque, int arg);
90 static void make_username_realm(char *user);
91 static int radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
92                             struct chap_digest_type *digest,
93                             unsigned char *challenge,
94                             char *message, int message_space);
95 static void radius_choose_ip(u_int32_t *addrp);
96 static int radius_init(char *msg);
97 static int get_client_port(char *ifname);
98 static int radius_allowed_address(u_int32_t addr);
99 static void radius_acct_interim(void *);
100 #ifdef MPPE
101 static int radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
102                               unsigned char *);
103 static int radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info);
104 #endif
105
106 #ifndef MAXSESSIONID
107 #define MAXSESSIONID 32
108 #endif
109
110 #ifndef MAXCLASSLEN
111 #define MAXCLASSLEN 500
112 #endif
113
114 struct radius_state {
115     int initialized;
116     int client_port;
117     int choose_ip;
118     int any_ip_addr_ok;
119     int done_chap_once;
120     u_int32_t ip_addr;
121     char user[MAXNAMELEN];
122     char config_file[MAXPATHLEN];
123     char session_id[MAXSESSIONID + 1];
124     time_t start_time;
125     int acct_interim_interval;
126     SERVER *authserver;         /* Authentication server to use */
127     SERVER *acctserver;         /* Accounting server to use */
128     int class_len;
129     char class[MAXCLASSLEN];
130     VALUE_PAIR *avp;    /* Additional (user supplied) vp's to send to server */
131 };
132
133 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
134
135 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
136    In that case, they override the values in the radiusclient.conf file */
137 void (*radius_pre_auth_hook)(char const *user,
138                              SERVER **authserver,
139                              SERVER **acctserver) = NULL;
140
141 static struct radius_state rstate;
142
143 char pppd_version[] = VERSION;
144
145 /**********************************************************************
146 * %FUNCTION: plugin_init
147 * %ARGUMENTS:
148 *  None
149 * %RETURNS:
150 *  Nothing
151 * %DESCRIPTION:
152 *  Initializes RADIUS plugin.
153 ***********************************************************************/
154 void
155 plugin_init(void)
156 {
157     pap_check_hook = radius_secret_check;
158     pap_auth_hook = radius_pap_auth;
159
160     chap_check_hook = radius_secret_check;
161     chap_verify_hook = radius_chap_verify;
162
163     ip_choose_hook = radius_choose_ip;
164     allowed_address_hook = radius_allowed_address;
165
166     add_notifier(&ip_up_notifier, radius_ip_up, NULL);
167     add_notifier(&ip_down_notifier, radius_ip_down, NULL);
168
169     memset(&rstate, 0, sizeof(rstate));
170
171     strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
172             sizeof(rstate.config_file));
173
174     add_options(Options);
175
176     info("RADIUS plugin initialized.");
177 }
178
179 /**********************************************************************
180 * %FUNCTION: add_avp
181 * %ARGUMENTS:
182 *  argv -- the <attribute=value> pair to add
183 * %RETURNS:
184 *  1
185 * %DESCRIPTION:
186 *  Adds an av pair to be passed on to the RADIUS server on each request.
187 ***********************************************************************/
188 static int
189 add_avp(char **argv)
190 {
191     struct avpopt *p = malloc(sizeof(struct avpopt));
192
193     /* Append to a list of vp's for later parsing */
194     p->vpstr = strdup(*argv);
195     p->next = avpopt;
196     avpopt = p;
197
198     return 1;
199 }
200
201 /**********************************************************************
202 * %FUNCTION: radius_secret_check
203 * %ARGUMENTS:
204 *  None
205 * %RETURNS:
206 *  1 -- we are ALWAYS willing to supply a secret. :-)
207 * %DESCRIPTION:
208 * Tells pppd that we will try to authenticate the peer, and not to
209 * worry about looking in *-secrets file(s)
210 ***********************************************************************/
211 static int
212 radius_secret_check(void)
213 {
214     return 1;
215 }
216
217 /**********************************************************************
218 * %FUNCTION: radius_choose_ip
219 * %ARGUMENTS:
220 *  addrp -- where to store the IP address
221 * %RETURNS:
222 *  Nothing
223 * %DESCRIPTION:
224 *  If RADIUS server has specified an IP address, it is stored in *addrp.
225 ***********************************************************************/
226 static void
227 radius_choose_ip(u_int32_t *addrp)
228 {
229     if (rstate.choose_ip) {
230         *addrp = rstate.ip_addr;
231     }
232 }
233
234 /**********************************************************************
235 * %FUNCTION: radius_pap_auth
236 * %ARGUMENTS:
237 *  user -- user-name of peer
238 *  passwd -- password supplied by peer
239 *  msgp -- Message which will be sent in PAP response
240 *  paddrs -- set to a list of possible peer IP addresses
241 *  popts -- set to a list of additional pppd options
242 * %RETURNS:
243 *  1 if we can authenticate, -1 if we cannot.
244 * %DESCRIPTION:
245 * Performs PAP authentication using RADIUS
246 ***********************************************************************/
247 static int
248 radius_pap_auth(char *user,
249                 char *passwd,
250                 char **msgp,
251                 struct wordlist **paddrs,
252                 struct wordlist **popts)
253 {
254     VALUE_PAIR *send, *received;
255     UINT4 av_type;
256     int result;
257     static char radius_msg[BUF_LEN];
258
259     radius_msg[0] = 0;
260     *msgp = radius_msg;
261
262     if (radius_init(radius_msg) < 0) {
263         return 0;
264     }
265
266     /* Put user with potentially realm added in rstate.user */
267     make_username_realm(user);
268
269     if (radius_pre_auth_hook) {
270         radius_pre_auth_hook(rstate.user,
271                              &rstate.authserver,
272                              &rstate.acctserver);
273     }
274
275     send = NULL;
276     received = NULL;
277
278     /* Hack... the "port" is the ppp interface number.  Should really be
279        the tty */
280     rstate.client_port = get_client_port(portnummap ? devnam : ifname);
281
282     av_type = PW_FRAMED;
283     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
284
285     av_type = PW_PPP;
286     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
287
288     rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
289     rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
290     if (*remote_number) {
291         rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
292                        VENDOR_NONE);
293     } else if (ipparam)
294         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
295
296     /* Add user specified vp's */
297     if (rstate.avp)
298         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
299
300     if (rstate.authserver) {
301         result = rc_auth_using_server(rstate.authserver,
302                                       rstate.client_port, send,
303                                       &received, radius_msg, NULL);
304     } else {
305         result = rc_auth(rstate.client_port, send, &received, radius_msg, NULL);
306     }
307
308     if (result == OK_RC) {
309         if (radius_setparams(received, radius_msg, NULL, NULL, NULL, NULL, 0) < 0) {
310             result = ERROR_RC;
311         }
312     }
313
314     /* free value pairs */
315     rc_avpair_free(received);
316     rc_avpair_free(send);
317
318     return (result == OK_RC) ? 1 : 0;
319 }
320
321 /**********************************************************************
322 * %FUNCTION: radius_chap_verify
323 * %ARGUMENTS:
324 *  user -- name of the peer
325 *  ourname -- name for this machine
326 *  id -- the ID byte in the challenge
327 *  digest -- points to the structure representing the digest type
328 *  challenge -- the challenge string we sent (length in first byte)
329 *  response -- the response (hash) the peer sent back (length in 1st byte)
330 *  message -- space for a message to be returned to the peer
331 *  message_space -- number of bytes available at *message.
332 * %RETURNS:
333 *  1 if the response is good, 0 if it is bad
334 * %DESCRIPTION:
335 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS.
336 ***********************************************************************/
337 static int
338 radius_chap_verify(char *user, char *ourname, int id,
339                    struct chap_digest_type *digest,
340                    unsigned char *challenge, unsigned char *response,
341                    char *message, int message_space)
342 {
343     VALUE_PAIR *send, *received;
344     UINT4 av_type;
345     static char radius_msg[BUF_LEN];
346     int result;
347     int challenge_len, response_len;
348     u_char cpassword[MAX_RESPONSE_LEN + 1];
349 #ifdef MPPE
350     /* Need the RADIUS secret and Request Authenticator to decode MPPE */
351     REQUEST_INFO request_info, *req_info = &request_info;
352 #else
353     REQUEST_INFO *req_info = NULL;
354 #endif
355
356     challenge_len = *challenge++;
357     response_len = *response++;
358
359     radius_msg[0] = 0;
360
361     if (radius_init(radius_msg) < 0) {
362         error("%s", radius_msg);
363         return 0;
364     }
365
366     /* return error for types we can't handle */
367     if ((digest->code != CHAP_MD5)
368 #ifdef CHAPMS
369         && (digest->code != CHAP_MICROSOFT)
370         && (digest->code != CHAP_MICROSOFT_V2)
371 #endif
372         ) {
373         error("RADIUS: Challenge type %u unsupported", digest->code);
374         return 0;
375     }
376
377     /* Put user with potentially realm added in rstate.user */
378     if (!rstate.done_chap_once) {
379         make_username_realm(user);
380         rstate.client_port = get_client_port (portnummap ? devnam : ifname);
381         if (radius_pre_auth_hook) {
382             radius_pre_auth_hook(rstate.user,
383                                  &rstate.authserver,
384                                  &rstate.acctserver);
385         }
386     }
387
388     send = received = NULL;
389
390     av_type = PW_FRAMED;
391     rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
392
393     av_type = PW_PPP;
394     rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
395
396     rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
397
398     /*
399      * add the challenge and response fields
400      */
401     switch (digest->code) {
402     case CHAP_MD5:
403         /* CHAP-Challenge and CHAP-Password */
404         if (response_len != MD5_HASH_SIZE)
405             return 0;
406         cpassword[0] = id;
407         memcpy(&cpassword[1], response, MD5_HASH_SIZE);
408
409         rc_avpair_add(&send, PW_CHAP_CHALLENGE,
410                       challenge, challenge_len, VENDOR_NONE);
411         rc_avpair_add(&send, PW_CHAP_PASSWORD,
412                       cpassword, MD5_HASH_SIZE + 1, VENDOR_NONE);
413         break;
414
415 #ifdef CHAPMS
416     case CHAP_MICROSOFT:
417     {
418         /* MS-CHAP-Challenge and MS-CHAP-Response */
419         u_char *p = cpassword;
420
421         if (response_len != MS_CHAP_RESPONSE_LEN)
422             return 0;
423         *p++ = id;
424         /* The idiots use a different field order in RADIUS than PPP */
425         *p++ = response[MS_CHAP_USENT];
426         memcpy(p, response, MS_CHAP_LANMANRESP_LEN + MS_CHAP_NTRESP_LEN);
427
428         rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
429                       challenge, challenge_len, VENDOR_MICROSOFT);
430         rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
431                       cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
432         break;
433     }
434
435     case CHAP_MICROSOFT_V2:
436     {
437         /* MS-CHAP-Challenge and MS-CHAP2-Response */
438         u_char *p = cpassword;
439
440         if (response_len != MS_CHAP2_RESPONSE_LEN)
441             return 0;
442         *p++ = id;
443         /* The idiots use a different field order in RADIUS than PPP */
444         *p++ = response[MS_CHAP2_FLAGS];
445         memcpy(p, response, (MS_CHAP2_PEER_CHAL_LEN + MS_CHAP2_RESERVED_LEN
446                              + MS_CHAP2_NTRESP_LEN));
447
448         rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
449                       challenge, challenge_len, VENDOR_MICROSOFT);
450         rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
451                       cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
452         break;
453     }
454 #endif
455     }
456
457     if (*remote_number) {
458         rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
459                        VENDOR_NONE);
460     } else if (ipparam)
461         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
462
463     /* Add user specified vp's */
464     if (rstate.avp)
465         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
466
467     /*
468      * make authentication with RADIUS server
469      */
470
471     if (rstate.authserver) {
472         result = rc_auth_using_server(rstate.authserver,
473                                       rstate.client_port, send,
474                                       &received, radius_msg, req_info);
475     } else {
476         result = rc_auth(rstate.client_port, send, &received, radius_msg,
477                          req_info);
478     }
479
480     strlcpy(message, radius_msg, message_space);
481
482     if (result == OK_RC) {
483         if (!rstate.done_chap_once) {
484             if (radius_setparams(received, radius_msg, req_info, digest,
485                                  challenge, message, message_space) < 0) {
486                 error("%s", radius_msg);
487                 result = ERROR_RC;
488             } else {
489                 rstate.done_chap_once = 1;
490             }
491         }
492     }
493
494     rc_avpair_free(received);
495     rc_avpair_free (send);
496     return (result == OK_RC);
497 }
498
499 /**********************************************************************
500 * %FUNCTION: make_username_realm
501 * %ARGUMENTS:
502 *  user -- the user given to pppd
503 * %RETURNS:
504 *  Nothing
505 * %DESCRIPTION:
506 *  Copies user into rstate.user.  If it lacks a realm (no "@domain" part),
507 * then the default realm from the radiusclient config file is added.
508 ***********************************************************************/
509 static void
510 make_username_realm(char *user)
511 {
512     char *default_realm;
513
514     if ( user != NULL ) {
515         strlcpy(rstate.user, user, sizeof(rstate.user));
516     }  else {
517         rstate.user[0] = 0;
518     }
519
520     default_realm = rc_conf_str("default_realm");
521
522     if (!strchr(rstate.user, '@') &&
523         default_realm &&
524         (*default_realm != '\0')) {
525         strlcat(rstate.user, "@", sizeof(rstate.user));
526         strlcat(rstate.user, default_realm, sizeof(rstate.user));
527     }
528 }
529
530 /**********************************************************************
531 * %FUNCTION: radius_setparams
532 * %ARGUMENTS:
533 *  vp -- received value-pairs
534 *  msg -- buffer in which to place error message.  Holds up to BUF_LEN chars
535 * %RETURNS:
536 *  >= 0 on success; -1 on failure
537 * %DESCRIPTION:
538 *  Parses attributes sent by RADIUS server and sets them in pppd.
539 ***********************************************************************/
540 static int
541 radius_setparams(VALUE_PAIR *vp, char *msg, REQUEST_INFO *req_info,
542                  struct chap_digest_type *digest, unsigned char *challenge,
543                  char *message, int message_space)
544 {
545     u_int32_t remote;
546     int ms_chap2_success = 0;
547 #ifdef MPPE
548     int mppe_enc_keys = 0;      /* whether or not these were received */
549     int mppe_enc_policy = 0;
550     int mppe_enc_types = 0;
551 #endif
552 #ifdef MSDNS
553     ipcp_options *wo = &ipcp_wantoptions[0];
554     ipcp_options *ao = &ipcp_allowoptions[0];
555     int got_msdns_1 = 0;
556     int got_msdns_2 = 0;
557     int got_wins_1 = 0;
558     int got_wins_2 = 0;
559 #endif
560
561     /* Send RADIUS attributes to anyone else who might be interested */
562     if (radius_attributes_hook) {
563         (*radius_attributes_hook)(vp);
564     }
565
566     /*
567      * service type (if not framed then quit),
568      * new IP address (RADIUS can define static IP for some users),
569      */
570
571     while (vp) {
572         if (vp->vendorcode == VENDOR_NONE) {
573             switch (vp->attribute) {
574             case PW_SERVICE_TYPE:
575                 /* check for service type       */
576                 /* if not FRAMED then exit      */
577                 if (vp->lvalue != PW_FRAMED) {
578                     slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
579                              vp->lvalue, rstate.user);
580                     return -1;
581                 }
582                 break;
583
584             case PW_FRAMED_PROTOCOL:
585                 /* check for framed protocol type       */
586                 /* if not PPP then also exit            */
587                 if (vp->lvalue != PW_PPP) {
588                     slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
589                              vp->lvalue, rstate.user);
590                     return -1;
591                 }
592                 break;
593
594             case PW_SESSION_TIMEOUT:
595                 /* Session timeout */
596                 maxconnect = vp->lvalue;
597                 break;
598            case PW_FILTER_ID:
599                /* packet filter, will be handled via ip-(up|down) script */
600                script_setenv("RADIUS_FILTER_ID", (char*) vp->strvalue, 1);
601                break;
602            case PW_FRAMED_ROUTE:
603                /* route, will be handled via ip-(up|down) script */
604                script_setenv("RADIUS_FRAMED_ROUTE", (char*) vp->strvalue, 1);
605                break;
606            case PW_IDLE_TIMEOUT:
607                /* idle parameter */
608                idle_time_limit = vp->lvalue;
609                break;
610 #ifdef MAXOCTETS
611             case PW_SESSION_OCTETS_LIMIT:
612                 /* Session traffic limit */
613                 maxoctets = vp->lvalue;
614                 break;
615             case PW_OCTETS_DIRECTION:
616                 /* Session traffic limit direction check */
617                 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
618                 break;
619 #endif
620             case PW_ACCT_INTERIM_INTERVAL:
621                 /* Send accounting updates every few seconds */
622                 rstate.acct_interim_interval = vp->lvalue;
623                 /* RFC says it MUST NOT be less than 60 seconds */
624                 /* We use "0" to signify not sending updates */
625                 if (rstate.acct_interim_interval &&
626                     rstate.acct_interim_interval < 60) {
627                     rstate.acct_interim_interval = 60;
628                 }
629                 break;
630             case PW_FRAMED_IP_ADDRESS:
631                 /* seting up remote IP addresses */
632                 remote = vp->lvalue;
633                 if (remote == 0xffffffff) {
634                     /* 0xffffffff means user should be allowed to select one */
635                     rstate.any_ip_addr_ok = 1;
636                 } else if (remote != 0xfffffffe) {
637                     /* 0xfffffffe means NAS should select an ip address */
638                     remote = htonl(vp->lvalue);
639                     if (bad_ip_adrs (remote)) {
640                         slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
641                                  remote, rstate.user);
642                         return -1;
643                     }
644                     rstate.choose_ip = 1;
645                     rstate.ip_addr = remote;
646                 }
647                 break;
648             case PW_NAS_IP_ADDRESS:
649                 wo->ouraddr = htonl(vp->lvalue);
650                 break;
651             case PW_CLASS:
652                 /* Save Class attribute to pass it in accounting request */
653                 if (vp->lvalue <= MAXCLASSLEN) {
654                     rstate.class_len=vp->lvalue;
655                     memcpy(rstate.class, vp->strvalue, rstate.class_len);
656                 } /* else too big for our buffer - ignore it */
657                 break;
658             case PW_FRAMED_MTU:
659                 netif_set_mtu(rstate.client_port,MIN(netif_get_mtu(rstate.client_port),vp->lvalue));
660                 break;
661             }
662
663
664         } else if (vp->vendorcode == VENDOR_MICROSOFT) {
665 #ifdef CHAPMS
666             switch (vp->attribute) {
667             case PW_MS_CHAP2_SUCCESS:
668                 if ((vp->lvalue != 43) || strncmp((char*) vp->strvalue + 1, "S=", 2)) {
669                     slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
670                     return -1;
671                 }
672                 if (message != NULL)
673                     strlcpy(message, (char*) vp->strvalue + 1, message_space);
674                 ms_chap2_success = 1;
675                 break;
676
677 #ifdef MPPE
678             case PW_MS_CHAP_MPPE_KEYS:
679                 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
680                     slprintf(msg, BUF_LEN,
681                              "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
682                     return -1;
683                 }
684                 mppe_enc_keys = 1;
685                 break;
686
687             case PW_MS_MPPE_SEND_KEY:
688             case PW_MS_MPPE_RECV_KEY:
689                 if (radius_setmppekeys2(vp, req_info) < 0) {
690                     slprintf(msg, BUF_LEN,
691                              "RADIUS: bad MS-MPPE-%s-Key attribute",
692                              (vp->attribute == PW_MS_MPPE_SEND_KEY)?
693                              "Send": "Recv");
694                     return -1;
695                 }
696                 mppe_enc_keys = 1;
697                 break;
698
699             case PW_MS_MPPE_ENCRYPTION_POLICY:
700                 mppe_enc_policy = vp->lvalue;   /* save for later */
701                 break;
702
703             case PW_MS_MPPE_ENCRYPTION_TYPES:
704                 mppe_enc_types = vp->lvalue;    /* save for later */
705                 break;
706
707 #endif /* MPPE */
708 #ifdef MSDNS
709             case PW_MS_PRIMARY_DNS_SERVER:
710                 ao->dnsaddr[0] = htonl(vp->lvalue);
711                 got_msdns_1 = 1;
712                 if (!got_msdns_2)
713                     ao->dnsaddr[1] = ao->dnsaddr[0];
714                 break;
715             case PW_MS_SECONDARY_DNS_SERVER:
716                 ao->dnsaddr[1] = htonl(vp->lvalue);
717                 got_msdns_2 = 1;
718                 if (!got_msdns_1)
719                     ao->dnsaddr[0] = ao->dnsaddr[1];
720                 break;
721             case PW_MS_PRIMARY_NBNS_SERVER:
722                 ao->winsaddr[0] = htonl(vp->lvalue);
723                 got_wins_1 = 1;
724                 if (!got_wins_2)
725                     ao->winsaddr[1] = ao->winsaddr[0];
726                 break;
727             case PW_MS_SECONDARY_NBNS_SERVER:
728                 ao->winsaddr[1] = htonl(vp->lvalue);
729                 got_wins_2 = 1;
730                 if (!got_wins_1)
731                     ao->winsaddr[0] = ao->winsaddr[1];
732                 break;
733 #endif /* MSDNS */
734             }
735 #endif /* CHAPMS */
736         }
737         vp = vp->next;
738     }
739
740     /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
741     if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
742         return -1;
743
744 #ifdef MPPE
745     /*
746      * Require both policy and key attributes to indicate a valid key.
747      * Note that if the policy value was '0' we don't set the key!
748      */
749     if (mppe_enc_policy && mppe_enc_keys) {
750         /* Set/modify allowed encryption types. */
751         if (mppe_enc_types)
752             mppe_set_enc_types(mppe_enc_policy, mppe_enc_types);
753         return 0;
754     }
755     mppe_clear_keys();
756 #endif
757
758     return 0;
759 }
760
761 #ifdef MPPE
762 /**********************************************************************
763 * %FUNCTION: radius_setmppekeys
764 * %ARGUMENTS:
765 *  vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
766 *  req_info -- radius request information used for encryption
767 * %RETURNS:
768 *  >= 0 on success; -1 on failure
769 * %DESCRIPTION:
770 *  Decrypt the "key" provided by the RADIUS server for MPPE encryption.
771 *  See RFC 2548.
772 ***********************************************************************/
773 static int
774 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
775                    unsigned char *challenge)
776 {
777     int i;
778     MD5_CTX Context;
779     u_char  plain[32];
780     u_char  buf[16];
781
782     if (vp->lvalue != 32) {
783         error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
784               vp->lvalue);
785         return -1;
786     }
787
788     memcpy(plain, vp->strvalue, sizeof(plain));
789
790     MD5_Init(&Context);
791     MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
792     MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
793     MD5_Final(buf, &Context);
794
795     for (i = 0; i < 16; i++)
796         plain[i] ^= buf[i];
797
798     MD5_Init(&Context);
799     MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
800     MD5_Update(&Context, vp->strvalue, 16);
801     MD5_Final(buf, &Context);
802
803     for(i = 0; i < 16; i++)
804         plain[i + 16] ^= buf[i];
805
806     /*
807      * Annoying.  The "key" returned is just the NTPasswordHashHash, which
808      * the NAS (us) doesn't need; we only need the start key.  So we have
809      * to generate the start key, sigh.  NB: We do not support the LM-Key.
810      */
811     mppe_set_chapv1(challenge, &plain[8]);
812
813     return 0;    
814 }
815
816 /**********************************************************************
817 * %FUNCTION: radius_setmppekeys2
818 * %ARGUMENTS:
819 *  vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
820 *  req_info -- radius request information used for encryption
821 * %RETURNS:
822 *  >= 0 on success; -1 on failure
823 * %DESCRIPTION:
824 *  Decrypt the key provided by the RADIUS server for MPPE encryption.
825 *  See RFC 2548.
826 ***********************************************************************/
827 static int
828 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
829 {
830     int i;
831     MD5_CTX Context;
832     u_char  *salt = vp->strvalue;
833     u_char  *crypt = vp->strvalue + 2;
834     u_char  plain[32];
835     u_char  buf[MD5_HASH_SIZE];
836     char    *type = "Send";
837
838     if (vp->attribute == PW_MS_MPPE_RECV_KEY)
839         type = "Recv";
840
841     if (vp->lvalue != 34) {
842         error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
843               vp->lvalue, type);
844         return -1;
845     }
846
847     if ((salt[0] & 0x80) == 0) {
848         error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
849         return -1;
850     }
851
852     memcpy(plain, crypt, 32);
853
854     MD5_Init(&Context);
855     MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
856     MD5_Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
857     MD5_Update(&Context, salt, 2);
858     MD5_Final(buf, &Context);
859
860     for (i = 0; i < 16; i++)
861         plain[i] ^= buf[i];
862
863     if (plain[0] != 16) {
864         error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
865               (int) plain[0], type);
866         return -1;
867     }
868
869     MD5_Init(&Context);
870     MD5_Update(&Context, req_info->secret, strlen(req_info->secret));
871     MD5_Update(&Context, crypt, 16);
872     MD5_Final(buf, &Context);
873
874     plain[16] ^= buf[0]; /* only need the first byte */
875
876     if (vp->attribute == PW_MS_MPPE_SEND_KEY)
877         mppe_set_keys(plain + 1, NULL, 16);
878     else
879         mppe_set_keys(NULL, plain + 1, 16);
880
881     return 0;
882 }
883 #endif /* MPPE */
884
885 /**********************************************************************
886 * %FUNCTION: radius_acct_start
887 * %ARGUMENTS:
888 *  None
889 * %RETURNS:
890 *  Nothing
891 * %DESCRIPTION:
892 *  Sends a "start" accounting message to the RADIUS server.
893 ***********************************************************************/
894 static void
895 radius_acct_start(void)
896 {
897     UINT4 av_type;
898     int result;
899     VALUE_PAIR *send = NULL;
900     ipcp_options *ho = &ipcp_hisoptions[0];
901     u_int32_t hisaddr;
902
903     if (!rstate.initialized) {
904         return;
905     }
906
907     rstate.start_time = time(NULL);
908
909     strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
910
911     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
912                    rstate.session_id, 0, VENDOR_NONE);
913     rc_avpair_add(&send, PW_USER_NAME,
914                    rstate.user, 0, VENDOR_NONE);
915
916     if (rstate.class_len > 0)
917         rc_avpair_add(&send, PW_CLASS,
918                       rstate.class, rstate.class_len, VENDOR_NONE);
919
920     av_type = PW_STATUS_START;
921     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
922
923     av_type = PW_FRAMED;
924     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
925
926     av_type = PW_PPP;
927     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
928
929     if (*remote_number) {
930         rc_avpair_add(&send, PW_CALLING_STATION_ID,
931                        remote_number, 0, VENDOR_NONE);
932     } else if (ipparam)
933         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
934
935     av_type = PW_RADIUS;
936     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
937
938
939     av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
940     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
941
942     hisaddr = ho->hisaddr;
943     av_type = htonl(hisaddr);
944     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
945
946     /* Add user specified vp's */
947     if (rstate.avp)
948         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
949
950     if (rstate.acctserver) {
951         result = rc_acct_using_server(rstate.acctserver,
952                                       rstate.client_port, send);
953     } else {
954         result = rc_acct(rstate.client_port, send);
955     }
956
957     rc_avpair_free(send);
958
959     if (result != OK_RC) {
960         /* RADIUS server could be down so make this a warning */
961         syslog(LOG_WARNING,
962                 "Accounting START failed for %s", rstate.user);
963     }
964
965     /* Kick off periodic accounting reports */
966     if (rstate.acct_interim_interval) {
967         TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
968     }
969 }
970
971 /**********************************************************************
972 * %FUNCTION: radius_acct_stop
973 * %ARGUMENTS:
974 *  None
975 * %RETURNS:
976 *  Nothing
977 * %DESCRIPTION:
978 *  Sends a "stop" accounting message to the RADIUS server.
979 ***********************************************************************/
980 static void
981 radius_acct_stop(void)
982 {
983     UINT4 av_type;
984     VALUE_PAIR *send = NULL;
985     ipcp_options *ho = &ipcp_hisoptions[0];
986     u_int32_t hisaddr;
987     int result;
988
989     if (!rstate.initialized) {
990         return;
991     }
992
993     if (rstate.acct_interim_interval)
994         UNTIMEOUT(radius_acct_interim, NULL);
995
996     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
997                    0, VENDOR_NONE);
998
999     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1000
1001     if (rstate.class_len > 0)
1002         rc_avpair_add(&send, PW_CLASS,
1003                       rstate.class, rstate.class_len, VENDOR_NONE);
1004
1005     av_type = PW_STATUS_STOP;
1006     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1007
1008     av_type = PW_FRAMED;
1009     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1010
1011     av_type = PW_PPP;
1012     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1013
1014     av_type = PW_RADIUS;
1015     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1016
1017
1018     if (link_stats_valid) {
1019         av_type = link_connect_time;
1020         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1021
1022         av_type = link_stats.bytes_out & 0xFFFFFFFF;
1023         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1024
1025         if (link_stats.bytes_out > 0xFFFFFFFF) {
1026             av_type = link_stats.bytes_out >> 32;
1027             rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1028         }
1029
1030         av_type = link_stats.bytes_in & 0xFFFFFFFF;
1031         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1032
1033         if (link_stats.bytes_in > 0xFFFFFFFF) {
1034             av_type = link_stats.bytes_in >> 32;
1035             rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1036         }
1037
1038         av_type = link_stats.pkts_out;
1039         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1040
1041         av_type = link_stats.pkts_in;
1042         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1043     }
1044
1045     if (*remote_number) {
1046         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1047                        remote_number, 0, VENDOR_NONE);
1048     } else if (ipparam)
1049         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1050
1051     av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1052     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1053
1054     av_type = PW_NAS_ERROR;
1055     switch( status ) {
1056         case EXIT_OK:
1057         case EXIT_USER_REQUEST:
1058             av_type = PW_USER_REQUEST;
1059             break;
1060
1061         case EXIT_HANGUP:
1062         case EXIT_PEER_DEAD:
1063         case EXIT_CONNECT_FAILED:
1064             av_type = PW_LOST_CARRIER;
1065             break;
1066
1067         case EXIT_INIT_FAILED:
1068         case EXIT_OPEN_FAILED:
1069         case EXIT_LOCK_FAILED:
1070         case EXIT_PTYCMD_FAILED:
1071             av_type = PW_PORT_ERROR;
1072             break;
1073
1074         case EXIT_PEER_AUTH_FAILED:
1075         case EXIT_AUTH_TOPEER_FAILED:
1076         case EXIT_NEGOTIATION_FAILED:
1077         case EXIT_CNID_AUTH_FAILED:
1078             av_type = PW_SERVICE_UNAVAILABLE;
1079             break;
1080
1081         case EXIT_IDLE_TIMEOUT:
1082             av_type = PW_ACCT_IDLE_TIMEOUT;
1083             break;
1084
1085         case EXIT_CALLBACK:
1086             av_type = PW_CALLBACK;
1087             break;
1088             
1089         case EXIT_CONNECT_TIME:
1090             av_type = PW_ACCT_SESSION_TIMEOUT;
1091             break;
1092             
1093 #ifdef MAXOCTETS
1094         case EXIT_TRAFFIC_LIMIT:
1095             av_type = PW_NAS_REQUEST;
1096             break;
1097 #endif
1098
1099         default:
1100             av_type = PW_NAS_ERROR;
1101             break;
1102     }
1103     rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1104
1105     hisaddr = ho->hisaddr;
1106     av_type = htonl(hisaddr);
1107     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1108
1109     /* Add user specified vp's */
1110     if (rstate.avp)
1111         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1112
1113     if (rstate.acctserver) {
1114         result = rc_acct_using_server(rstate.acctserver,
1115                                       rstate.client_port, send);
1116     } else {
1117         result = rc_acct(rstate.client_port, send);
1118     }
1119
1120     if (result != OK_RC) {
1121         /* RADIUS server could be down so make this a warning */
1122         syslog(LOG_WARNING,
1123                 "Accounting STOP failed for %s", rstate.user);
1124     }
1125     rc_avpair_free(send);
1126 }
1127
1128 /**********************************************************************
1129 * %FUNCTION: radius_acct_interim
1130 * %ARGUMENTS:
1131 *  None
1132 * %RETURNS:
1133 *  Nothing
1134 * %DESCRIPTION:
1135 *  Sends an interim accounting message to the RADIUS server
1136 ***********************************************************************/
1137 static void
1138 radius_acct_interim(void *ignored)
1139 {
1140     UINT4 av_type;
1141     VALUE_PAIR *send = NULL;
1142     ipcp_options *ho = &ipcp_hisoptions[0];
1143     u_int32_t hisaddr;
1144     int result;
1145
1146     if (!rstate.initialized) {
1147         return;
1148     }
1149
1150     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1151                    0, VENDOR_NONE);
1152
1153     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1154
1155     if (rstate.class_len > 0)
1156         rc_avpair_add(&send, PW_CLASS,
1157                       rstate.class, rstate.class_len, VENDOR_NONE);
1158
1159     av_type = PW_STATUS_ALIVE;
1160     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1161
1162     av_type = PW_FRAMED;
1163     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1164
1165     av_type = PW_PPP;
1166     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1167
1168     av_type = PW_RADIUS;
1169     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1170
1171     /* Update link stats */
1172     update_link_stats(0);
1173
1174     if (link_stats_valid) {
1175         link_stats_valid = 0; /* Force later code to update */
1176
1177         av_type = link_connect_time;
1178         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1179
1180         av_type = link_stats.bytes_out & 0xFFFFFFFF;
1181         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1182
1183         if (link_stats.bytes_out > 0xFFFFFFFF) {
1184             av_type = link_stats.bytes_out >> 32;
1185             rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1186         }
1187
1188         av_type = link_stats.bytes_in & 0xFFFFFFFF;
1189         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1190
1191         if (link_stats.bytes_in > 0xFFFFFFFF) {
1192             av_type = link_stats.bytes_in >> 32;
1193             rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1194         }
1195
1196         av_type = link_stats.pkts_out;
1197         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1198
1199         av_type = link_stats.pkts_in;
1200         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1201     }
1202
1203     if (*remote_number) {
1204         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1205                        remote_number, 0, VENDOR_NONE);
1206     } else if (ipparam)
1207         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1208
1209     av_type = ( using_pty ? PW_VIRTUAL : ( sync_serial ? PW_SYNC : PW_ASYNC ) );
1210     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1211
1212     hisaddr = ho->hisaddr;
1213     av_type = htonl(hisaddr);
1214     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1215
1216     /* Add user specified vp's */
1217     if (rstate.avp)
1218         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1219
1220     if (rstate.acctserver) {
1221         result = rc_acct_using_server(rstate.acctserver,
1222                                       rstate.client_port, send);
1223     } else {
1224         result = rc_acct(rstate.client_port, send);
1225     }
1226
1227     if (result != OK_RC) {
1228         /* RADIUS server could be down so make this a warning */
1229         syslog(LOG_WARNING,
1230                 "Interim accounting failed for %s", rstate.user);
1231     }
1232     rc_avpair_free(send);
1233
1234     /* Schedule another one */
1235     TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1236 }
1237
1238 /**********************************************************************
1239 * %FUNCTION: radius_ip_up
1240 * %ARGUMENTS:
1241 *  opaque -- ignored
1242 *  arg -- ignored
1243 * %RETURNS:
1244 *  Nothing
1245 * %DESCRIPTION:
1246 *  Called when IPCP is up.  We'll do a start-accounting record.
1247 ***********************************************************************/
1248 static void
1249 radius_ip_up(void *opaque, int arg)
1250 {
1251     radius_acct_start();
1252 }
1253
1254 /**********************************************************************
1255 * %FUNCTION: radius_ip_down
1256 * %ARGUMENTS:
1257 *  opaque -- ignored
1258 *  arg -- ignored
1259 * %RETURNS:
1260 *  Nothing
1261 * %DESCRIPTION:
1262 *  Called when IPCP is down.  We'll do a stop-accounting record.
1263 ***********************************************************************/
1264 static void
1265 radius_ip_down(void *opaque, int arg)
1266 {
1267     radius_acct_stop();
1268 }
1269
1270 /**********************************************************************
1271 * %FUNCTION: radius_init
1272 * %ARGUMENTS:
1273 *  msg -- buffer of size BUF_LEN for error message
1274 * %RETURNS:
1275 *  negative on failure; non-negative on success
1276 * %DESCRIPTION:
1277 *  Initializes radiusclient library
1278 ***********************************************************************/
1279 static int
1280 radius_init(char *msg)
1281 {
1282     if (rstate.initialized) {
1283         return 0;
1284     }
1285
1286     if (config_file && *config_file) {
1287         strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1288     }
1289
1290     rstate.initialized = 1;
1291
1292     if (rc_read_config(rstate.config_file) != 0) {
1293         slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1294                  rstate.config_file);
1295         return -1;
1296     }
1297
1298     if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1299         slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1300                  rc_conf_str("dictionary"));
1301         return -1;
1302     }
1303
1304     if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)   {
1305         slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1306                  rc_conf_str("mapfile"));
1307         return -1;
1308     }
1309
1310     /* Add av pairs saved during option parsing */
1311     while (avpopt) {
1312         struct avpopt *n = avpopt->next;
1313
1314         rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1315         free(avpopt->vpstr);
1316         free(avpopt);
1317         avpopt = n;
1318     }
1319     return 0;
1320 }
1321
1322 /**********************************************************************
1323 * %FUNCTION: get_client_port
1324 * %ARGUMENTS:
1325 *  ifname -- PPP interface name (e.g. "ppp7")
1326 * %RETURNS:
1327 *  The NAS port number (e.g. 7)
1328 * %DESCRIPTION:
1329 *  Extracts the port number from the interface name
1330 ***********************************************************************/
1331 static int
1332 get_client_port(char *ifname)
1333 {
1334     int port;
1335     if (sscanf(ifname, "ppp%d", &port) == 1) {
1336         return port;
1337     }
1338     return rc_map2id(ifname);
1339 }
1340
1341 /**********************************************************************
1342 * %FUNCTION: radius_allowed_address
1343 * %ARGUMENTS:
1344 *  addr -- IP address
1345 * %RETURNS:
1346 *  1 if we're allowed to use that IP address; 0 if not; -1 if we do
1347 *  not know.
1348 ***********************************************************************/
1349 static int
1350 radius_allowed_address(u_int32_t addr)
1351 {
1352     ipcp_options *wo = &ipcp_wantoptions[0];
1353
1354     if (!rstate.choose_ip) {
1355         /* If RADIUS server said any address is OK, then fine... */
1356         if (rstate.any_ip_addr_ok) {
1357             return 1;
1358         }
1359
1360         /* Sigh... if an address was supplied for remote host in pppd
1361            options, it has to match that.  */
1362         if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1363             return 1;
1364         }
1365
1366         return 0;
1367     }
1368     if (addr == rstate.ip_addr) return 1;
1369     return 0;
1370 }
1371
1372 /* Useful for other plugins */
1373 char *radius_logged_in_user(void)
1374 {
1375     return rstate.user;
1376 }