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