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