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