]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radius.c
c73ca0b53ef82b07932fa304d667ff22e0d7db7a
[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
902         if (PPP_DigestInit(ctx, PPP_md5())) {
903
904             if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
905
906                 if (PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
907
908                     if (PPP_DigestUpdate(ctx, salt, 2)) {
909
910                         buflen = sizeof(buf);
911                         if (PPP_DigestFinal(ctx, buf, &buflen)) {
912
913                             status = 1;
914                         }
915                     }
916                 }
917             }
918         }
919
920         PPP_MD_CTX_free(ctx);
921     }
922
923     if (status) {
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
939             if (PPP_DigestInit(ctx, PPP_md5())) {
940
941                 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
942
943                     if (PPP_DigestUpdate(ctx, crypt, 16)) {
944
945                         if (PPP_DigestUpdate(ctx, salt, 2)) {
946
947                             buflen = sizeof(buf);
948                             if (PPP_DigestFinal(ctx, buf, &buflen)) {
949
950                                 status = 1;
951                             }
952                         }
953                     }
954                 }
955             }
956
957             PPP_MD_CTX_free(ctx);
958         }
959
960         if (status) {
961
962             plain[16] ^= buf[0]; /* only need the first byte */
963
964             if (vp->attribute == PW_MS_MPPE_SEND_KEY) {
965                 mppe_set_keys(plain + 1, NULL, 16);
966             } else {
967                 mppe_set_keys(NULL, plain + 1, 16);
968             }
969             return 0;
970         }
971     }
972
973     return -1;
974 }
975 #endif /* PPP_WITH_MPPE */
976
977 /**********************************************************************
978 * %FUNCTION: radius_acct_start
979 * %ARGUMENTS:
980 *  None
981 * %RETURNS:
982 *  Nothing
983 * %DESCRIPTION:
984 *  Sends a "start" accounting message to the RADIUS server.
985 ***********************************************************************/
986 static void
987 radius_acct_start(void)
988 {
989     UINT4 av_type;
990     int result;
991     VALUE_PAIR *send = NULL;
992     ipcp_options *ho = &ipcp_hisoptions[0];
993     u_int32_t hisaddr;
994     const char *remote_number;
995     const char *ipparam;
996
997     if (!rstate.initialized) {
998         return;
999     }
1000
1001     rstate.start_time = time(NULL);
1002
1003     strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
1004
1005     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
1006                    rstate.session_id, 0, VENDOR_NONE);
1007     rc_avpair_add(&send, PW_USER_NAME,
1008                    rstate.user, 0, VENDOR_NONE);
1009
1010     if (rstate.class_len > 0)
1011         rc_avpair_add(&send, PW_CLASS,
1012                       rstate.class, rstate.class_len, VENDOR_NONE);
1013
1014     av_type = PW_STATUS_START;
1015     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1016
1017     av_type = PW_FRAMED;
1018     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1019
1020     av_type = PW_PPP;
1021     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1022
1023     remote_number = ppp_get_remote_number();
1024     ipparam = ppp_ipparam();
1025     if (remote_number) {
1026         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1027                        remote_number, 0, VENDOR_NONE);
1028     } else if (ipparam)
1029         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1030
1031     av_type = PW_RADIUS;
1032     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1033
1034
1035     av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1036     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1037
1038     hisaddr = ho->hisaddr;
1039     av_type = htonl(hisaddr);
1040     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1041
1042     /* Add user specified vp's */
1043     if (rstate.avp)
1044         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1045
1046     if (rstate.acctserver) {
1047         result = rc_acct_using_server(rstate.acctserver,
1048                                       rstate.client_port, send);
1049     } else {
1050         result = rc_acct(rstate.client_port, send);
1051     }
1052
1053     rc_avpair_free(send);
1054
1055     if (result != OK_RC) {
1056         /* RADIUS server could be down so make this a warning */
1057         syslog(LOG_WARNING,
1058                 "Accounting START failed for %s", rstate.user);
1059     }
1060
1061     /* Kick off periodic accounting reports */
1062     if (rstate.acct_interim_interval) {
1063         ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1064     }
1065 }
1066
1067 /**********************************************************************
1068 * %FUNCTION: radius_acct_stop
1069 * %ARGUMENTS:
1070 *  None
1071 * %RETURNS:
1072 *  Nothing
1073 * %DESCRIPTION:
1074 *  Sends a "stop" accounting message to the RADIUS server.
1075 ***********************************************************************/
1076 static void
1077 radius_acct_stop(void)
1078 {
1079     UINT4 av_type;
1080     VALUE_PAIR *send = NULL;
1081     ipcp_options *ho = &ipcp_hisoptions[0];
1082     u_int32_t hisaddr;
1083     int result;
1084     const char *remote_number;
1085     const char *ipparam;
1086     ppp_link_stats_st stats;
1087
1088     if (!rstate.initialized) {
1089         return;
1090     }
1091
1092     if (rstate.acct_interim_interval)
1093         ppp_untimeout(radius_acct_interim, NULL);
1094
1095     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1096                    0, VENDOR_NONE);
1097
1098     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1099
1100     if (rstate.class_len > 0)
1101         rc_avpair_add(&send, PW_CLASS,
1102                       rstate.class, rstate.class_len, VENDOR_NONE);
1103
1104     av_type = PW_STATUS_STOP;
1105     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1106
1107     av_type = PW_FRAMED;
1108     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1109
1110     av_type = PW_PPP;
1111     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1112
1113     av_type = PW_RADIUS;
1114     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1115
1116     if (ppp_get_link_stats(&stats)) {
1117
1118         av_type = ppp_get_link_uptime();
1119         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1120
1121         av_type = stats.bytes_out & 0xFFFFFFFF;
1122         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1123
1124         if (stats.bytes_out > 0xFFFFFFFF) {
1125             av_type = stats.bytes_out >> 32;
1126             rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1127         }
1128
1129         av_type = stats.bytes_in & 0xFFFFFFFF;
1130         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1131
1132         if (stats.bytes_in > 0xFFFFFFFF) {
1133             av_type = stats.bytes_in >> 32;
1134             rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1135         }
1136
1137         av_type = stats.pkts_out;
1138         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1139
1140         av_type = stats.pkts_in;
1141         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1142     }
1143
1144     remote_number = ppp_get_remote_number();
1145     ipparam = ppp_ipparam();
1146     if (remote_number) {
1147         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1148                        remote_number, 0, VENDOR_NONE);
1149     } else if (ipparam)
1150         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1151
1152     av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1153     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1154
1155     av_type = PW_NAS_ERROR;
1156     switch( ppp_status() ) {
1157         case EXIT_OK:
1158             av_type = PW_USER_REQUEST;
1159             break;
1160
1161         case EXIT_USER_REQUEST:
1162             av_type = PW_ADMIN_RESET;
1163             break;
1164
1165         case EXIT_HANGUP:
1166         case EXIT_PEER_DEAD:
1167         case EXIT_CONNECT_FAILED:
1168             av_type = PW_LOST_CARRIER;
1169             break;
1170
1171         case EXIT_INIT_FAILED:
1172         case EXIT_OPEN_FAILED:
1173         case EXIT_LOCK_FAILED:
1174         case EXIT_PTYCMD_FAILED:
1175             av_type = PW_PORT_ERROR;
1176             break;
1177
1178         case EXIT_PEER_AUTH_FAILED:
1179         case EXIT_AUTH_TOPEER_FAILED:
1180         case EXIT_NEGOTIATION_FAILED:
1181         case EXIT_CNID_AUTH_FAILED:
1182             av_type = PW_SERVICE_UNAVAILABLE;
1183             break;
1184
1185         case EXIT_IDLE_TIMEOUT:
1186             av_type = PW_ACCT_IDLE_TIMEOUT;
1187             break;
1188
1189         case EXIT_CALLBACK:
1190             av_type = PW_CALLBACK;
1191             break;
1192             
1193         case EXIT_CONNECT_TIME:
1194             av_type = PW_ACCT_SESSION_TIMEOUT;
1195             break;
1196             
1197         case EXIT_TRAFFIC_LIMIT:
1198             av_type = PW_NAS_REQUEST;
1199             break;
1200
1201         default:
1202             av_type = PW_NAS_ERROR;
1203             break;
1204     }
1205     rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1206
1207     hisaddr = ho->hisaddr;
1208     av_type = htonl(hisaddr);
1209     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1210
1211     /* Add user specified vp's */
1212     if (rstate.avp)
1213         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1214
1215     if (rstate.acctserver) {
1216         result = rc_acct_using_server(rstate.acctserver,
1217                                       rstate.client_port, send);
1218     } else {
1219         result = rc_acct(rstate.client_port, send);
1220     }
1221
1222     if (result != OK_RC) {
1223         /* RADIUS server could be down so make this a warning */
1224         syslog(LOG_WARNING,
1225                 "Accounting STOP failed for %s", rstate.user);
1226     }
1227     rc_avpair_free(send);
1228 }
1229
1230 /**********************************************************************
1231 * %FUNCTION: radius_acct_interim
1232 * %ARGUMENTS:
1233 *  None
1234 * %RETURNS:
1235 *  Nothing
1236 * %DESCRIPTION:
1237 *  Sends an interim accounting message to the RADIUS server
1238 ***********************************************************************/
1239 static void
1240 radius_acct_interim(void *ignored)
1241 {
1242     UINT4 av_type;
1243     VALUE_PAIR *send = NULL;
1244     ipcp_options *ho = &ipcp_hisoptions[0];
1245     u_int32_t hisaddr;
1246     int result;
1247     const char *remote_number;
1248     const char *ipparam;
1249     ppp_link_stats_st stats;
1250
1251     if (!rstate.initialized) {
1252         return;
1253     }
1254
1255     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1256                    0, VENDOR_NONE);
1257
1258     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1259
1260     if (rstate.class_len > 0)
1261         rc_avpair_add(&send, PW_CLASS,
1262                       rstate.class, rstate.class_len, VENDOR_NONE);
1263
1264     av_type = PW_STATUS_ALIVE;
1265     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1266
1267     av_type = PW_FRAMED;
1268     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1269
1270     av_type = PW_PPP;
1271     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1272
1273     av_type = PW_RADIUS;
1274     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1275
1276     if (ppp_get_link_stats(&stats)) {
1277
1278         av_type = ppp_get_link_uptime();
1279         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1280
1281         av_type = stats.bytes_out & 0xFFFFFFFF;
1282         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1283
1284         if (stats.bytes_out > 0xFFFFFFFF) {
1285             av_type = stats.bytes_out >> 32;
1286             rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1287         }
1288
1289         av_type = stats.bytes_in & 0xFFFFFFFF;
1290         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1291
1292         if (stats.bytes_in > 0xFFFFFFFF) {
1293             av_type = stats.bytes_in >> 32;
1294             rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1295         }
1296
1297         av_type = stats.pkts_out;
1298         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1299
1300         av_type = stats.pkts_in;
1301         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1302     }
1303
1304     remote_number = ppp_get_remote_number();
1305     ipparam = ppp_ipparam();
1306     if (remote_number) {
1307         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1308                        remote_number, 0, VENDOR_NONE);
1309     } else if (ipparam)
1310         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1311
1312     av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1313     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1314
1315     hisaddr = ho->hisaddr;
1316     av_type = htonl(hisaddr);
1317     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1318
1319     /* Add user specified vp's */
1320     if (rstate.avp)
1321         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1322
1323     if (rstate.acctserver) {
1324         result = rc_acct_using_server(rstate.acctserver,
1325                                       rstate.client_port, send);
1326     } else {
1327         result = rc_acct(rstate.client_port, send);
1328     }
1329
1330     if (result != OK_RC) {
1331         /* RADIUS server could be down so make this a warning */
1332         syslog(LOG_WARNING,
1333                 "Interim accounting failed for %s", rstate.user);
1334     }
1335     rc_avpair_free(send);
1336
1337     /* Schedule another one */
1338     ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1339 }
1340
1341 /**********************************************************************
1342 * %FUNCTION: radius_ip_up
1343 * %ARGUMENTS:
1344 *  opaque -- ignored
1345 *  arg -- ignored
1346 * %RETURNS:
1347 *  Nothing
1348 * %DESCRIPTION:
1349 *  Called when IPCP is up.  We'll do a start-accounting record.
1350 ***********************************************************************/
1351 static void
1352 radius_ip_up(void *opaque, int arg)
1353 {
1354     radius_acct_start();
1355 }
1356
1357 /**********************************************************************
1358 * %FUNCTION: radius_ip_down
1359 * %ARGUMENTS:
1360 *  opaque -- ignored
1361 *  arg -- ignored
1362 * %RETURNS:
1363 *  Nothing
1364 * %DESCRIPTION:
1365 *  Called when IPCP is down.  We'll do a stop-accounting record.
1366 ***********************************************************************/
1367 static void
1368 radius_ip_down(void *opaque, int arg)
1369 {
1370     radius_acct_stop();
1371 }
1372
1373 /**********************************************************************
1374 * %FUNCTION: radius_init
1375 * %ARGUMENTS:
1376 *  msg -- buffer of size BUF_LEN for error message
1377 * %RETURNS:
1378 *  negative on failure; non-negative on success
1379 * %DESCRIPTION:
1380 *  Initializes radiusclient library
1381 ***********************************************************************/
1382 static int
1383 radius_init(char *msg)
1384 {
1385     if (rstate.initialized) {
1386         return 0;
1387     }
1388
1389     if (config_file && *config_file) {
1390         strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1391     }
1392
1393     rstate.initialized = 1;
1394
1395     if (rc_read_config(rstate.config_file) != 0) {
1396         slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1397                  rstate.config_file);
1398         return -1;
1399     }
1400
1401     if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1402         slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1403                  rc_conf_str("dictionary"));
1404         return -1;
1405     }
1406
1407     if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)   {
1408         slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1409                  rc_conf_str("mapfile"));
1410         return -1;
1411     }
1412
1413     /* Add av pairs saved during option parsing */
1414     while (avpopt) {
1415         struct avpopt *n = avpopt->next;
1416
1417         rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1418         free(avpopt->vpstr);
1419         free(avpopt);
1420         avpopt = n;
1421     }
1422     return 0;
1423 }
1424
1425 /**********************************************************************
1426 * %FUNCTION: get_client_port
1427 * %ARGUMENTS:
1428 *  ifname -- PPP interface name (e.g. "ppp7")
1429 * %RETURNS:
1430 *  The NAS port number (e.g. 7)
1431 * %DESCRIPTION:
1432 *  Extracts the port number from the interface name
1433 ***********************************************************************/
1434 static int
1435 get_client_port(const char *ifname)
1436 {
1437     int port;
1438     if (sscanf(ifname, "ppp%d", &port) == 1) {
1439         return port;
1440     }
1441     return rc_map2id(ifname);
1442 }
1443
1444 /**********************************************************************
1445 * %FUNCTION: radius_allowed_address
1446 * %ARGUMENTS:
1447 *  addr -- IP address
1448 * %RETURNS:
1449 *  1 if we're allowed to use that IP address; 0 if not; -1 if we do
1450 *  not know.
1451 ***********************************************************************/
1452 static int
1453 radius_allowed_address(u_int32_t addr)
1454 {
1455     ipcp_options *wo = &ipcp_wantoptions[0];
1456
1457     if (!rstate.choose_ip) {
1458         /* If RADIUS server said any address is OK, then fine... */
1459         if (rstate.any_ip_addr_ok) {
1460             return 1;
1461         }
1462
1463         /* Sigh... if an address was supplied for remote host in pppd
1464            options, it has to match that.  */
1465         if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1466             return 1;
1467         }
1468
1469         return 0;
1470     }
1471     if (addr == rstate.ip_addr) return 1;
1472     return 0;
1473 }
1474
1475 /* Useful for other plugins */
1476 char *radius_logged_in_user(void)
1477 {
1478     return rstate.user;
1479 }