]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radius.c
b4bc896bce4941f91056e11b9b1c39ecc7a21d7c
[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 #include <pppd/crypto.h>
48 #endif
49 #endif
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) {
653                     rstate.class_len=vp->lvalue;
654                     memcpy(rstate.class, vp->strvalue, rstate.class_len);
655                 } /* else too big for our buffer - ignore it */
656                 break;
657             case PW_FRAMED_MTU:
658                 ppp_set_mtu(rstate.client_port,MIN(ppp_get_mtu(rstate.client_port),vp->lvalue));
659                 break;
660             }
661
662
663         } else if (vp->vendorcode == VENDOR_MICROSOFT) {
664 #ifdef PPP_WITH_CHAPMS
665             switch (vp->attribute) {
666             case PW_MS_CHAP2_SUCCESS:
667                 if ((vp->lvalue != 43) || strncmp((char*) vp->strvalue + 1, "S=", 2)) {
668                     slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
669                     return -1;
670                 }
671                 if (message != NULL)
672                     strlcpy(message, (char*) vp->strvalue + 1, message_space);
673                 ms_chap2_success = 1;
674                 break;
675
676 #ifdef PPP_WITH_MPPE
677             case PW_MS_CHAP_MPPE_KEYS:
678                 if (radius_setmppekeys(vp, req_info, challenge) < 0) {
679                     slprintf(msg, BUF_LEN,
680                              "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
681                     return -1;
682                 }
683                 mppe_enc_keys = 1;
684                 break;
685
686             case PW_MS_MPPE_SEND_KEY:
687             case PW_MS_MPPE_RECV_KEY:
688                 if (radius_setmppekeys2(vp, req_info) < 0) {
689                     slprintf(msg, BUF_LEN,
690                              "RADIUS: bad MS-MPPE-%s-Key attribute",
691                              (vp->attribute == PW_MS_MPPE_SEND_KEY)?
692                              "Send": "Recv");
693                     return -1;
694                 }
695                 mppe_enc_keys = 1;
696                 break;
697
698             case PW_MS_MPPE_ENCRYPTION_POLICY:
699                 mppe_enc_policy = vp->lvalue;   /* save for later */
700                 break;
701
702             case PW_MS_MPPE_ENCRYPTION_TYPES:
703                 mppe_enc_types = vp->lvalue;    /* save for later */
704                 break;
705
706 #endif /* PPP_WITH_MPPE */
707 #ifdef MSDNS
708             case PW_MS_PRIMARY_DNS_SERVER:
709                 ao->dnsaddr[0] = htonl(vp->lvalue);
710                 got_msdns_1 = 1;
711                 if (!got_msdns_2)
712                     ao->dnsaddr[1] = ao->dnsaddr[0];
713                 break;
714             case PW_MS_SECONDARY_DNS_SERVER:
715                 ao->dnsaddr[1] = htonl(vp->lvalue);
716                 got_msdns_2 = 1;
717                 if (!got_msdns_1)
718                     ao->dnsaddr[0] = ao->dnsaddr[1];
719                 break;
720             case PW_MS_PRIMARY_NBNS_SERVER:
721                 ao->winsaddr[0] = htonl(vp->lvalue);
722                 got_wins_1 = 1;
723                 if (!got_wins_2)
724                     ao->winsaddr[1] = ao->winsaddr[0];
725                 break;
726             case PW_MS_SECONDARY_NBNS_SERVER:
727                 ao->winsaddr[1] = htonl(vp->lvalue);
728                 got_wins_2 = 1;
729                 if (!got_wins_1)
730                     ao->winsaddr[0] = ao->winsaddr[1];
731                 break;
732 #endif /* MSDNS */
733             }
734 #endif /* PPP_WITH_CHAPMS */
735         }
736         vp = vp->next;
737     }
738
739     /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
740     if (digest && (digest->code == CHAP_MICROSOFT_V2) && !ms_chap2_success)
741         return -1;
742
743 #ifdef PPP_WITH_MPPE
744     /*
745      * Require both policy and key attributes to indicate a valid key.
746      * Note that if the policy value was '0' we don't set the key!
747      */
748     if (mppe_enc_policy && mppe_enc_keys) {
749         /* Set/modify allowed encryption types. */
750         if (mppe_enc_types)
751             mppe_set_enc_types(mppe_enc_policy, mppe_enc_types);
752         return 0;
753     }
754     mppe_clear_keys();
755 #endif
756
757     return 0;
758 }
759
760 #ifdef PPP_WITH_MPPE
761 /**********************************************************************
762 * %FUNCTION: radius_setmppekeys
763 * %ARGUMENTS:
764 *  vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
765 *  req_info -- radius request information used for encryption
766 * %RETURNS:
767 *  >= 0 on success; -1 on failure
768 * %DESCRIPTION:
769 *  Decrypt the "key" provided by the RADIUS server for MPPE encryption.
770 *  See RFC 2548.
771 ***********************************************************************/
772 static int
773 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info,
774                    unsigned char *challenge)
775 {
776     int i;
777     int status = 0;
778     PPP_MD_CTX *ctx;
779     unsigned char plain[32];
780     unsigned char buf[MD5_DIGEST_LENGTH];
781     unsigned int  buflen;
782
783
784     if (vp->lvalue != 32) {
785         error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
786               vp->lvalue);
787         return -1;
788     }
789
790     memcpy(plain, vp->strvalue, sizeof(plain));
791
792     ctx = PPP_MD_CTX_new();
793     if (ctx) {
794
795         if (PPP_DigestInit(ctx, PPP_md5())) {
796
797             if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
798
799                 if (PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
800
801                     buflen = sizeof(buf);
802                     if (PPP_DigestFinal(ctx, buf, &buflen)) {
803
804                         status = 1;
805                     }
806                 }
807             }
808         }
809         PPP_MD_CTX_free(ctx);
810     }
811
812     if (status) {
813
814         for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
815             plain[i] ^= buf[i];
816         }
817
818         status = 0;
819         ctx = PPP_MD_CTX_new();
820         if (ctx) {
821
822             if (PPP_DigestInit(ctx, PPP_md5())) {
823
824                 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
825
826                     if (PPP_DigestUpdate(ctx, vp->strvalue, 16)) {
827
828                         buflen = MD5_DIGEST_LENGTH;
829                         if (PPP_DigestFinal(ctx, buf, &buflen)) {
830
831                             status = 1;
832                         }
833                     }
834                 }
835             }
836             PPP_MD_CTX_free(ctx);
837         }
838
839         if (status) {
840
841             for(i = 0; i < MD5_DIGEST_LENGTH; i++) {
842                 plain[i + 16] ^= buf[i];
843             }
844
845             /*
846              * Annoying.  The "key" returned is just the NTPasswordHashHash, which
847              * the NAS (us) doesn't need; we only need the start key.  So we have
848              * to generate the start key, sigh.  NB: We do not support the LM-Key.
849              */
850             mppe_set_chapv1(challenge, &plain[8]);
851             return 0;
852         }
853     }
854
855     return -1;
856 }
857
858 /**********************************************************************
859 * %FUNCTION: radius_setmppekeys2
860 * %ARGUMENTS:
861 *  vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
862 *  req_info -- radius request information used for encryption
863 * %RETURNS:
864 *  >= 0 on success; -1 on failure
865 * %DESCRIPTION:
866 *  Decrypt the key provided by the RADIUS server for MPPE encryption.
867 *  See RFC 2548.
868 ***********************************************************************/
869 static int
870 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
871 {
872     int i;
873     int status = 0;
874     PPP_MD_CTX *ctx;
875     unsigned char *salt = vp->strvalue;
876     unsigned char *crypt = vp->strvalue + 2;
877     unsigned char plain[32];
878     unsigned char buf[MD5_DIGEST_LENGTH];
879     unsigned int  buflen;
880     char    *type = "Send";
881
882     if (vp->attribute == PW_MS_MPPE_RECV_KEY)
883         type = "Recv";
884
885     if (vp->lvalue != 34) {
886         error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
887               vp->lvalue, type);
888         return -1;
889     }
890
891     if ((salt[0] & 0x80) == 0) {
892         error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
893         return -1;
894     }
895
896     memcpy(plain, crypt, 32);
897
898     ctx = PPP_MD_CTX_new();
899     if (ctx) {
900
901         if (PPP_DigestInit(ctx, PPP_md5())) {
902
903             if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
904
905                 if (PPP_DigestUpdate(ctx, req_info->request_vector, AUTH_VECTOR_LEN)) {
906
907                     if (PPP_DigestUpdate(ctx, salt, 2)) {
908
909                         buflen = sizeof(buf);
910                         if (PPP_DigestFinal(ctx, buf, &buflen)) {
911
912                             status = 1;
913                         }
914                     }
915                 }
916             }
917         }
918
919         PPP_MD_CTX_free(ctx);
920     }
921
922     if (status) {
923
924         for (i = 0; i < 16; i++) {
925             plain[i] ^= buf[i];
926         }
927
928         if (plain[0] != 16) {
929             error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
930                   (int) plain[0], type);
931             return -1;
932         }
933
934         status = 0;
935         ctx = PPP_MD_CTX_new();
936         if (ctx) {
937
938             if (PPP_DigestInit(ctx, PPP_md5())) {
939
940                 if (PPP_DigestUpdate(ctx, req_info->secret, strlen(req_info->secret))) {
941
942                     if (PPP_DigestUpdate(ctx, crypt, 16)) {
943
944                         if (PPP_DigestUpdate(ctx, salt, 2)) {
945
946                             buflen = sizeof(buf);
947                             if (PPP_DigestFinal(ctx, buf, &buflen)) {
948
949                                 status = 1;
950                             }
951                         }
952                     }
953                 }
954             }
955
956             PPP_MD_CTX_free(ctx);
957         }
958
959         if (status) {
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     }
971
972     return -1;
973 }
974 #endif /* PPP_WITH_MPPE */
975
976 /**********************************************************************
977 * %FUNCTION: radius_acct_start
978 * %ARGUMENTS:
979 *  None
980 * %RETURNS:
981 *  Nothing
982 * %DESCRIPTION:
983 *  Sends a "start" accounting message to the RADIUS server.
984 ***********************************************************************/
985 static void
986 radius_acct_start(void)
987 {
988     UINT4 av_type;
989     int result;
990     VALUE_PAIR *send = NULL;
991     ipcp_options *ho = &ipcp_hisoptions[0];
992     u_int32_t hisaddr;
993     const char *remote_number;
994     const char *ipparam;
995
996     if (!rstate.initialized) {
997         return;
998     }
999
1000     rstate.start_time = time(NULL);
1001
1002     strlcpy(rstate.session_id, rc_mksid(), MAXSESSIONID);
1003
1004     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
1005                    rstate.session_id, 0, VENDOR_NONE);
1006     rc_avpair_add(&send, PW_USER_NAME,
1007                    rstate.user, 0, VENDOR_NONE);
1008
1009     if (rstate.class_len > 0)
1010         rc_avpair_add(&send, PW_CLASS,
1011                       rstate.class, rstate.class_len, VENDOR_NONE);
1012
1013     av_type = PW_STATUS_START;
1014     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1015
1016     av_type = PW_FRAMED;
1017     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1018
1019     av_type = PW_PPP;
1020     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1021
1022     remote_number = ppp_get_remote_number();
1023     ipparam = ppp_ipparam();
1024     if (remote_number) {
1025         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1026                        remote_number, 0, VENDOR_NONE);
1027     } else if (ipparam)
1028         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1029
1030     av_type = PW_RADIUS;
1031     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1032
1033
1034     av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1035     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1036
1037     hisaddr = ho->hisaddr;
1038     av_type = htonl(hisaddr);
1039     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1040
1041     /* Add user specified vp's */
1042     if (rstate.avp)
1043         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1044
1045     if (rstate.acctserver) {
1046         result = rc_acct_using_server(rstate.acctserver,
1047                                       rstate.client_port, send);
1048     } else {
1049         result = rc_acct(rstate.client_port, send);
1050     }
1051
1052     rc_avpair_free(send);
1053
1054     if (result != OK_RC) {
1055         /* RADIUS server could be down so make this a warning */
1056         syslog(LOG_WARNING,
1057                 "Accounting START failed for %s", rstate.user);
1058     }
1059
1060     /* Kick off periodic accounting reports */
1061     if (rstate.acct_interim_interval) {
1062         ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1063     }
1064 }
1065
1066 /**********************************************************************
1067 * %FUNCTION: radius_acct_stop
1068 * %ARGUMENTS:
1069 *  None
1070 * %RETURNS:
1071 *  Nothing
1072 * %DESCRIPTION:
1073 *  Sends a "stop" accounting message to the RADIUS server.
1074 ***********************************************************************/
1075 static void
1076 radius_acct_stop(void)
1077 {
1078     UINT4 av_type;
1079     VALUE_PAIR *send = NULL;
1080     ipcp_options *ho = &ipcp_hisoptions[0];
1081     u_int32_t hisaddr;
1082     int result;
1083     const char *remote_number;
1084     const char *ipparam;
1085     ppp_link_stats_st stats;
1086
1087     if (!rstate.initialized) {
1088         return;
1089     }
1090
1091     if (rstate.acct_interim_interval)
1092         ppp_untimeout(radius_acct_interim, NULL);
1093
1094     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1095                    0, VENDOR_NONE);
1096
1097     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1098
1099     if (rstate.class_len > 0)
1100         rc_avpair_add(&send, PW_CLASS,
1101                       rstate.class, rstate.class_len, VENDOR_NONE);
1102
1103     av_type = PW_STATUS_STOP;
1104     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1105
1106     av_type = PW_FRAMED;
1107     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1108
1109     av_type = PW_PPP;
1110     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1111
1112     av_type = PW_RADIUS;
1113     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1114
1115     if (ppp_get_link_stats(&stats)) {
1116
1117         av_type = ppp_get_link_uptime();
1118         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1119
1120         av_type = stats.bytes_out & 0xFFFFFFFF;
1121         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1122
1123         if (stats.bytes_out > 0xFFFFFFFF) {
1124             av_type = stats.bytes_out >> 32;
1125             rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1126         }
1127
1128         av_type = stats.bytes_in & 0xFFFFFFFF;
1129         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1130
1131         if (stats.bytes_in > 0xFFFFFFFF) {
1132             av_type = stats.bytes_in >> 32;
1133             rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1134         }
1135
1136         av_type = stats.pkts_out;
1137         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1138
1139         av_type = stats.pkts_in;
1140         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1141     }
1142
1143     remote_number = ppp_get_remote_number();
1144     ipparam = ppp_ipparam();
1145     if (remote_number) {
1146         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1147                        remote_number, 0, VENDOR_NONE);
1148     } else if (ipparam)
1149         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1150
1151     av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1152     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1153
1154     av_type = PW_NAS_ERROR;
1155     switch( ppp_status() ) {
1156         case EXIT_OK:
1157             av_type = PW_USER_REQUEST;
1158             break;
1159
1160         case EXIT_USER_REQUEST:
1161             av_type = PW_ADMIN_RESET;
1162             break;
1163
1164         case EXIT_HANGUP:
1165         case EXIT_PEER_DEAD:
1166         case EXIT_CONNECT_FAILED:
1167             av_type = PW_LOST_CARRIER;
1168             break;
1169
1170         case EXIT_INIT_FAILED:
1171         case EXIT_OPEN_FAILED:
1172         case EXIT_LOCK_FAILED:
1173         case EXIT_PTYCMD_FAILED:
1174             av_type = PW_PORT_ERROR;
1175             break;
1176
1177         case EXIT_PEER_AUTH_FAILED:
1178         case EXIT_AUTH_TOPEER_FAILED:
1179         case EXIT_NEGOTIATION_FAILED:
1180         case EXIT_CNID_AUTH_FAILED:
1181             av_type = PW_SERVICE_UNAVAILABLE;
1182             break;
1183
1184         case EXIT_IDLE_TIMEOUT:
1185             av_type = PW_ACCT_IDLE_TIMEOUT;
1186             break;
1187
1188         case EXIT_CALLBACK:
1189             av_type = PW_CALLBACK;
1190             break;
1191             
1192         case EXIT_CONNECT_TIME:
1193             av_type = PW_ACCT_SESSION_TIMEOUT;
1194             break;
1195             
1196         case EXIT_TRAFFIC_LIMIT:
1197             av_type = PW_NAS_REQUEST;
1198             break;
1199
1200         default:
1201             av_type = PW_NAS_ERROR;
1202             break;
1203     }
1204     rc_avpair_add(&send, PW_ACCT_TERMINATE_CAUSE, &av_type, 0, VENDOR_NONE);
1205
1206     hisaddr = ho->hisaddr;
1207     av_type = htonl(hisaddr);
1208     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1209
1210     /* Add user specified vp's */
1211     if (rstate.avp)
1212         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1213
1214     if (rstate.acctserver) {
1215         result = rc_acct_using_server(rstate.acctserver,
1216                                       rstate.client_port, send);
1217     } else {
1218         result = rc_acct(rstate.client_port, send);
1219     }
1220
1221     if (result != OK_RC) {
1222         /* RADIUS server could be down so make this a warning */
1223         syslog(LOG_WARNING,
1224                 "Accounting STOP failed for %s", rstate.user);
1225     }
1226     rc_avpair_free(send);
1227 }
1228
1229 /**********************************************************************
1230 * %FUNCTION: radius_acct_interim
1231 * %ARGUMENTS:
1232 *  None
1233 * %RETURNS:
1234 *  Nothing
1235 * %DESCRIPTION:
1236 *  Sends an interim accounting message to the RADIUS server
1237 ***********************************************************************/
1238 static void
1239 radius_acct_interim(void *ignored)
1240 {
1241     UINT4 av_type;
1242     VALUE_PAIR *send = NULL;
1243     ipcp_options *ho = &ipcp_hisoptions[0];
1244     u_int32_t hisaddr;
1245     int result;
1246     const char *remote_number;
1247     const char *ipparam;
1248     ppp_link_stats_st stats;
1249
1250     if (!rstate.initialized) {
1251         return;
1252     }
1253
1254     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
1255                    0, VENDOR_NONE);
1256
1257     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
1258
1259     if (rstate.class_len > 0)
1260         rc_avpair_add(&send, PW_CLASS,
1261                       rstate.class, rstate.class_len, VENDOR_NONE);
1262
1263     av_type = PW_STATUS_ALIVE;
1264     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
1265
1266     av_type = PW_FRAMED;
1267     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
1268
1269     av_type = PW_PPP;
1270     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
1271
1272     av_type = PW_RADIUS;
1273     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1274
1275     if (ppp_get_link_stats(&stats)) {
1276
1277         av_type = ppp_get_link_uptime();
1278         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1279
1280         av_type = stats.bytes_out & 0xFFFFFFFF;
1281         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1282
1283         if (stats.bytes_out > 0xFFFFFFFF) {
1284             av_type = stats.bytes_out >> 32;
1285             rc_avpair_add(&send, PW_ACCT_OUTPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1286         }
1287
1288         av_type = stats.bytes_in & 0xFFFFFFFF;
1289         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1290
1291         if (stats.bytes_in > 0xFFFFFFFF) {
1292             av_type = stats.bytes_in >> 32;
1293             rc_avpair_add(&send, PW_ACCT_INPUT_GIGAWORDS, &av_type, 0, VENDOR_NONE);
1294         }
1295
1296         av_type = stats.pkts_out;
1297         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1298
1299         av_type = stats.pkts_in;
1300         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1301     }
1302
1303     remote_number = ppp_get_remote_number();
1304     ipparam = ppp_ipparam();
1305     if (remote_number) {
1306         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1307                        remote_number, 0, VENDOR_NONE);
1308     } else if (ipparam)
1309         rc_avpair_add(&send, PW_CALLING_STATION_ID, ipparam, 0, VENDOR_NONE);
1310
1311     av_type = ( ppp_using_pty() ? PW_VIRTUAL : ( ppp_sync_serial() ? PW_SYNC : PW_ASYNC ) );
1312     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1313
1314     hisaddr = ho->hisaddr;
1315     av_type = htonl(hisaddr);
1316     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1317
1318     /* Add user specified vp's */
1319     if (rstate.avp)
1320         rc_avpair_insert(&send, NULL, rc_avpair_copy(rstate.avp));
1321
1322     if (rstate.acctserver) {
1323         result = rc_acct_using_server(rstate.acctserver,
1324                                       rstate.client_port, send);
1325     } else {
1326         result = rc_acct(rstate.client_port, send);
1327     }
1328
1329     if (result != OK_RC) {
1330         /* RADIUS server could be down so make this a warning */
1331         syslog(LOG_WARNING,
1332                 "Interim accounting failed for %s", rstate.user);
1333     }
1334     rc_avpair_free(send);
1335
1336     /* Schedule another one */
1337     ppp_timeout(radius_acct_interim, NULL, rstate.acct_interim_interval, 0);
1338 }
1339
1340 /**********************************************************************
1341 * %FUNCTION: radius_ip_up
1342 * %ARGUMENTS:
1343 *  opaque -- ignored
1344 *  arg -- ignored
1345 * %RETURNS:
1346 *  Nothing
1347 * %DESCRIPTION:
1348 *  Called when IPCP is up.  We'll do a start-accounting record.
1349 ***********************************************************************/
1350 static void
1351 radius_ip_up(void *opaque, int arg)
1352 {
1353     radius_acct_start();
1354 }
1355
1356 /**********************************************************************
1357 * %FUNCTION: radius_ip_down
1358 * %ARGUMENTS:
1359 *  opaque -- ignored
1360 *  arg -- ignored
1361 * %RETURNS:
1362 *  Nothing
1363 * %DESCRIPTION:
1364 *  Called when IPCP is down.  We'll do a stop-accounting record.
1365 ***********************************************************************/
1366 static void
1367 radius_ip_down(void *opaque, int arg)
1368 {
1369     radius_acct_stop();
1370 }
1371
1372 /**********************************************************************
1373 * %FUNCTION: radius_init
1374 * %ARGUMENTS:
1375 *  msg -- buffer of size BUF_LEN for error message
1376 * %RETURNS:
1377 *  negative on failure; non-negative on success
1378 * %DESCRIPTION:
1379 *  Initializes radiusclient library
1380 ***********************************************************************/
1381 static int
1382 radius_init(char *msg)
1383 {
1384     if (rstate.initialized) {
1385         return 0;
1386     }
1387
1388     if (config_file && *config_file) {
1389         strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1390     }
1391
1392     rstate.initialized = 1;
1393
1394     if (rc_read_config(rstate.config_file) != 0) {
1395         slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1396                  rstate.config_file);
1397         return -1;
1398     }
1399
1400     if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1401         slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1402                  rc_conf_str("dictionary"));
1403         return -1;
1404     }
1405
1406     if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)   {
1407         slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1408                  rc_conf_str("mapfile"));
1409         return -1;
1410     }
1411
1412     /* Add av pairs saved during option parsing */
1413     while (avpopt) {
1414         struct avpopt *n = avpopt->next;
1415
1416         rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1417         free(avpopt->vpstr);
1418         free(avpopt);
1419         avpopt = n;
1420     }
1421     return 0;
1422 }
1423
1424 /**********************************************************************
1425 * %FUNCTION: get_client_port
1426 * %ARGUMENTS:
1427 *  ifname -- PPP interface name (e.g. "ppp7")
1428 * %RETURNS:
1429 *  The NAS port number (e.g. 7)
1430 * %DESCRIPTION:
1431 *  Extracts the port number from the interface name
1432 ***********************************************************************/
1433 static int
1434 get_client_port(const char *ifname)
1435 {
1436     int port;
1437     if (sscanf(ifname, "ppp%d", &port) == 1) {
1438         return port;
1439     }
1440     return rc_map2id(ifname);
1441 }
1442
1443 /**********************************************************************
1444 * %FUNCTION: radius_allowed_address
1445 * %ARGUMENTS:
1446 *  addr -- IP address
1447 * %RETURNS:
1448 *  1 if we're allowed to use that IP address; 0 if not; -1 if we do
1449 *  not know.
1450 ***********************************************************************/
1451 static int
1452 radius_allowed_address(u_int32_t addr)
1453 {
1454     ipcp_options *wo = &ipcp_wantoptions[0];
1455
1456     if (!rstate.choose_ip) {
1457         /* If RADIUS server said any address is OK, then fine... */
1458         if (rstate.any_ip_addr_ok) {
1459             return 1;
1460         }
1461
1462         /* Sigh... if an address was supplied for remote host in pppd
1463            options, it has to match that.  */
1464         if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1465             return 1;
1466         }
1467
1468         return 0;
1469     }
1470     if (addr == rstate.ip_addr) return 1;
1471     return 0;
1472 }
1473
1474 /* Useful for other plugins */
1475 char *radius_logged_in_user(void)
1476 {
1477     return rstate.user;
1478 }