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