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