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