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