]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radius.c
First large MPPE patch from Frank Cusack.
[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 * This plugin may be distributed according to the terms of the GNU
20 * General Public License, version 2 or (at your option) any later version.
21 *
22 ***********************************************************************/
23 static char const RCSID[] =
24 "$Id: radius.c,v 1.8 2002/04/02 13:55:00 dfs Exp $";
25
26 #include "pppd.h"
27 #include "chap.h"
28 #ifdef CHAPMS
29 #include "chap_ms.h"
30 #endif
31 #include "radiusclient.h"
32 #include "fsm.h"
33 #include "ipcp.h"
34 #include <syslog.h>
35 #include <sys/types.h>
36 #include <sys/time.h>
37
38 #define BUF_LEN 1024
39
40 static char *config_file = NULL;
41
42 static option_t Options[] = {
43     { "radius-config-file", o_string, &config_file },
44     { NULL }
45 };
46
47 static int radius_secret_check(void);
48 static int radius_pap_auth(char *user,
49                            char *passwd,
50                            char **msgp,
51                            struct wordlist **paddrs,
52                            struct wordlist **popts);
53 static int radius_chap_auth(char *user,
54                             u_char *remmd,
55                             int remmd_len,
56                             chap_state *cstate);
57
58 static void radius_ip_up(void *opaque, int arg);
59 static void radius_ip_down(void *opaque, int arg);
60 static void make_username_realm(char *user);
61 static int radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg);
62 static void radius_choose_ip(u_int32_t *addrp);
63 static int radius_init(char *msg);
64 static int get_client_port(char *ifname);
65 static int radius_allowed_address(u_int32_t addr);
66
67 #ifndef MAXSESSIONID
68 #define MAXSESSIONID 32
69 #endif
70
71 struct radius_state {
72     int accounting_started;
73     int initialized;
74     int client_port;
75     int choose_ip;
76     int any_ip_addr_ok;
77     int done_chap_once;
78     u_int32_t ip_addr;
79     char user[MAXNAMELEN];
80     char config_file[MAXPATHLEN];
81     char session_id[MAXSESSIONID + 1];
82     time_t start_time;
83     SERVER *authserver;         /* Authentication server to use */
84     SERVER *acctserver;         /* Accounting server to use */
85 };
86
87 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
88
89 /* The pre_auth_hook MAY set authserver and acctserver if it wants.
90    In that case, they override the values in the radiusclient.conf file */
91 void (*radius_pre_auth_hook)(char const *user,
92                              SERVER **authserver,
93                              SERVER **acctserver) = NULL;
94
95 static struct radius_state rstate;
96
97 char pppd_version[] = VERSION;
98
99 /**********************************************************************
100 * %FUNCTION: plugin_init
101 * %ARGUMENTS:
102 *  None
103 * %RETURNS:
104 *  Nothing
105 * %DESCRIPTION:
106 *  Initializes RADIUS plugin.
107 ***********************************************************************/
108 void
109 plugin_init(void)
110 {
111     pap_check_hook = radius_secret_check;
112     pap_auth_hook = radius_pap_auth;
113
114     chap_check_hook = radius_secret_check;
115     chap_auth_hook = radius_chap_auth;
116
117     ip_choose_hook = radius_choose_ip;
118     allowed_address_hook = radius_allowed_address;
119
120     add_notifier(&ip_up_notifier, radius_ip_up, NULL);
121     add_notifier(&ip_down_notifier, radius_ip_down, NULL);
122
123     memset(&rstate, 0, sizeof(rstate));
124
125     strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
126             sizeof(rstate.config_file));
127
128     add_options(Options);
129
130     info("RADIUS plugin initialized.");
131 }
132
133 /**********************************************************************
134 * %FUNCTION: radius_secret_check
135 * %ARGUMENTS:
136 *  None
137 * %RETURNS:
138 *  1 -- we are ALWAYS willing to supply a secret. :-)
139 * %DESCRIPTION:
140 * Tells pppd that we will try to authenticate the peer, and not to
141 * worry about looking in /etc/ppp/*-secrets
142 ***********************************************************************/
143 static int
144 radius_secret_check(void)
145 {
146     return 1;
147 }
148
149 /**********************************************************************
150 * %FUNCTION: radius_choose_ip
151 * %ARGUMENTS:
152 *  addrp -- where to store the IP address
153 * %RETURNS:
154 *  Nothing
155 * %DESCRIPTION:
156 *  If RADIUS server has specified an IP address, it is stored in *addrp.
157 ***********************************************************************/
158 static void
159 radius_choose_ip(u_int32_t *addrp)
160 {
161     if (rstate.choose_ip) {
162         *addrp = rstate.ip_addr;
163     }
164 }
165
166 /**********************************************************************
167 * %FUNCTION: radius_pap_auth
168 * %ARGUMENTS:
169 *  user -- user-name of peer
170 *  passwd -- password supplied by peer
171 *  msgp -- Message which will be sent in PAP response
172 *  paddrs -- set to a list of possible peer IP addresses
173 *  popts -- set to a list of additional pppd options
174 * %RETURNS:
175 *  1 if we can authenticate, -1 if we cannot.
176 * %DESCRIPTION:
177 * Performs PAP authentication using RADIUS
178 ***********************************************************************/
179 static int
180 radius_pap_auth(char *user,
181                 char *passwd,
182                 char **msgp,
183                 struct wordlist **paddrs,
184                 struct wordlist **popts)
185 {
186     VALUE_PAIR *send, *received;
187     UINT4 av_type;
188     int result;
189     static char radius_msg[BUF_LEN];
190
191     radius_msg[0] = 0;
192     *msgp = radius_msg;
193
194     if (radius_init(radius_msg) < 0) {
195         return 0;
196     }
197
198     /* Put user with potentially realm added in rstate.user */
199     make_username_realm(user);
200
201     if (radius_pre_auth_hook) {
202         radius_pre_auth_hook(rstate.user,
203                              &rstate.authserver,
204                              &rstate.acctserver);
205     }
206
207     send = NULL;
208     received = NULL;
209
210     /* Hack... the "port" is the ppp interface number.  Should really be
211        the tty */
212     rstate.client_port = get_client_port(ifname);
213
214     av_type = PW_FRAMED;
215     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
216
217     av_type = PW_PPP;
218     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
219
220     rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
221     rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
222     if (*remote_number) {
223         rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
224                        VENDOR_NONE);
225     }
226
227     if (rstate.authserver) {
228         result = rc_auth_using_server(rstate.authserver,
229                                       rstate.client_port, send,
230                                       &received, radius_msg);
231     } else {
232         result = rc_auth(rstate.client_port, send, &received, radius_msg);
233     }
234
235     if (result == OK_RC) {
236         if (radius_setparams(NULL, received, radius_msg) < 0) {
237             result = ERROR_RC;
238         }
239     }
240
241     /* free value pairs */
242     rc_avpair_free(received);
243     rc_avpair_free(send);
244
245     return (result == OK_RC) ? 1 : 0;
246 }
247
248 /**********************************************************************
249 * %FUNCTION: radius_chap_auth
250 * %ARGUMENTS:
251 *  user -- user-name of peer
252 *  remmd -- hash received from peer
253 *  remmd_len -- length of remmd
254 *  cstate -- pppd's chap_state structure
255 * %RETURNS:
256 *  CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
257 * %DESCRIPTION:
258 * Performs CHAP, MS-CHAP and MS-CHAPv2 authentication using RADIUS
259 ***********************************************************************/
260 static int
261 radius_chap_auth(char *user,
262                  u_char *remmd,
263                  int remmd_len,
264                  chap_state *cstate)
265 {
266     VALUE_PAIR *send, *received;
267     UINT4 av_type;
268     static char radius_msg[BUF_LEN];
269     int result;
270     u_char cpassword[MAX_RESPONSE_LENGTH + 1];
271     radius_msg[0] = 0;
272
273     if (radius_init(radius_msg) < 0) {
274         error("%s", radius_msg);
275         return CHAP_FAILURE;
276     }
277
278     /* return error for types we can't handle */
279     if ((cstate->chal_type != CHAP_DIGEST_MD5)
280 #ifdef CHAPMS
281         && (cstate->chal_type != CHAP_MICROSOFT)
282         && (cstate->chal_type != CHAP_MICROSOFT_V2)
283 #endif
284         ) {
285         error("RADIUS: Challenge type %u unsupported", cstate->chal_type);
286         return CHAP_FAILURE;
287     }
288
289     /* Put user with potentially realm added in rstate.user */
290     if (!rstate.done_chap_once) {
291         make_username_realm(user);
292         rstate.client_port = get_client_port (ifname);
293         if (radius_pre_auth_hook) {
294             radius_pre_auth_hook(rstate.user,
295                                  &rstate.authserver,
296                                  &rstate.acctserver);
297         }
298     }
299
300     send = received = NULL;
301
302     av_type = PW_FRAMED;
303     rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
304
305     av_type = PW_PPP;
306     rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
307
308     rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
309
310     /*
311      * add the challenge and response fields
312      */
313     switch (cstate->chal_type) {
314     case CHAP_DIGEST_MD5:
315         /* CHAP-Challenge and CHAP-Password */
316         cpassword[0] = cstate->chal_id;
317         memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE);
318
319         rc_avpair_add(&send, PW_CHAP_CHALLENGE,
320                       cstate->challenge, cstate->chal_len, VENDOR_NONE);
321         rc_avpair_add(&send, PW_CHAP_PASSWORD,
322                       cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE);
323         break;
324
325 #ifdef CHAPMS
326     case CHAP_MICROSOFT:
327     {
328         /* MS-CHAP-Challenge and MS-CHAP-Response */
329         MS_ChapResponse *rmd = (MS_ChapResponse *) remmd;
330         u_char *p = cpassword;
331
332         *p++ = cstate->chal_id;
333         /* The idiots use a different field order in RADIUS than PPP */
334         memcpy(p, rmd->UseNT, sizeof(rmd->UseNT));
335         p += sizeof(rmd->UseNT);
336         memcpy(p, rmd->LANManResp, sizeof(rmd->LANManResp));
337         p += sizeof(rmd->LANManResp);
338         memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
339
340         rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
341                       cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
342         rc_avpair_add(&send, PW_MS_CHAP_RESPONSE,
343                       cpassword, MS_CHAP_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
344         break;
345     }
346
347     case CHAP_MICROSOFT_V2:
348     {
349         /* MS-CHAP-Challenge and MS-CHAP2-Response */
350         MS_Chap2Response *rmd = (MS_Chap2Response *) remmd;
351         u_char *p = cpassword;
352
353         *p++ = cstate->chal_id;
354         /* The idiots use a different field order in RADIUS than PPP */
355         memcpy(p, rmd->Flags, sizeof(rmd->Flags));
356         p += sizeof(rmd->Flags);
357         memcpy(p, rmd->PeerChallenge, sizeof(rmd->PeerChallenge));
358         p += sizeof(rmd->PeerChallenge);
359         memcpy(p, rmd->Reserved, sizeof(rmd->Reserved));
360         p += sizeof(rmd->Reserved);
361         memcpy(p, rmd->NTResp, sizeof(rmd->NTResp));
362
363         rc_avpair_add(&send, PW_MS_CHAP_CHALLENGE,
364                       cstate->challenge, cstate->chal_len, VENDOR_MICROSOFT);
365         rc_avpair_add(&send, PW_MS_CHAP2_RESPONSE,
366                       cpassword, MS_CHAP2_RESPONSE_LEN + 1, VENDOR_MICROSOFT);
367         break;
368     }
369 #endif
370
371     }
372
373     /*
374      * make authentication with RADIUS server
375      */
376
377     if (rstate.authserver) {
378         result = rc_auth_using_server(rstate.authserver,
379                                       rstate.client_port, send,
380                                       &received, radius_msg);
381     } else {
382         result = rc_auth(rstate.client_port, send, &received, radius_msg);
383     }
384
385     if (result == OK_RC) {
386         if (!rstate.done_chap_once) {
387             if (radius_setparams(cstate, received, radius_msg) < 0) {
388                 error("%s", radius_msg);
389                 result = ERROR_RC;
390             } else {
391                 rstate.done_chap_once = 1;
392             }
393         }
394     }
395
396     rc_avpair_free(received);
397     rc_avpair_free (send);
398     return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
399 }
400
401 /**********************************************************************
402 * %FUNCTION: make_username_realm
403 * %ARGUMENTS:
404 *  user -- the user given to pppd
405 * %RETURNS:
406 *  Nothing
407 * %DESCRIPTION:
408 *  Copies user into rstate.user.  If it lacks a realm (no "@domain" part),
409 * then the default realm from the radiusclient config file is added.
410 ***********************************************************************/
411 static void
412 make_username_realm(char *user)
413 {
414     char *default_realm;
415
416     if ( user != NULL ) {
417         strlcpy(rstate.user, user, sizeof(rstate.user));
418     }  else {
419         rstate.user[0] = 0;
420     }
421
422     default_realm = rc_conf_str("default_realm");
423
424     if (!strchr(rstate.user, '@') &&
425         default_realm &&
426         (*default_realm != '\0')) {
427         strlcat(rstate.user, "@", sizeof(rstate.user));
428         strlcat(rstate.user, default_realm, sizeof(rstate.user));
429     }
430 }
431
432 /**********************************************************************
433 * %FUNCTION: radius_setparams
434 * %ARGUMENTS:
435 *  cstate -- pppd's chap_state structure
436 *  vp -- received value-pairs
437 *  msg -- buffer in which to place error message.  Holds up to BUF_LEN chars
438 * %RETURNS:
439 *  >= 0 on success; -1 on failure
440 * %DESCRIPTION:
441 *  Parses attributes sent by RADIUS server and sets them in pppd.  Currently,
442 *  used only to set IP address and MS-CHAPv2 Authenticator Response.
443 ***********************************************************************/
444 static int
445 radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg)
446 {
447     u_int32_t remote;
448     int ms_chap2_success = 0;
449
450     /* Send RADIUS attributes to anyone else who might be interested */
451     if (radius_attributes_hook) {
452         (*radius_attributes_hook)(vp);
453     }
454
455     /*
456      * service type (if not framed then quit),
457      * new IP address (RADIUS can define static IP for some users),
458      */
459
460     while (vp) {
461         if ((vp->attribute == PW_SERVICE_TYPE) &&
462             (vp->vendorcode == VENDOR_NONE)) {
463             /* check for service type       */
464             /* if not FRAMED then exit      */
465             if (vp->lvalue != PW_FRAMED) {
466                 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
467                          vp->lvalue, rstate.user);
468                 return -1;
469             }
470         } else if ((vp->attribute == PW_FRAMED_PROTOCOL) &&
471                    (vp->vendorcode == VENDOR_NONE)) {
472             /* check for framed protocol type       */
473             /* if not PPP then also exit            */
474             if (vp->lvalue != PW_PPP) {
475                 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
476                          vp->lvalue, rstate.user);
477                 return -1;
478             }
479         } else if ((vp->attribute == PW_SESSION_TIMEOUT) &&
480                    (vp->vendorcode == VENDOR_NONE)) {
481             /* Session timeout */
482             maxconnect = vp->lvalue;
483         } else if ((vp->attribute == PW_FRAMED_IP_ADDRESS) &&
484                    (vp->vendorcode == VENDOR_NONE)) {
485             /* seting up remote IP addresses */
486             remote = vp->lvalue;
487             if (remote == 0xffffffff) {
488                 /* 0xffffffff means user should be allowed to select one */
489                 rstate.any_ip_addr_ok = 1;
490             } else if (remote != 0xfffffffe) {
491                 /* 0xfffffffe means NAS should select an ip address */
492                 remote = htonl(vp->lvalue);
493                 if (bad_ip_adrs (remote)) {
494                     slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
495                              remote, rstate.user);
496                     return -1;
497                 }
498                 rstate.choose_ip = 1;
499                 rstate.ip_addr = remote;
500             }
501 #ifdef CHAPMS
502         } else if ((vp->attribute == PW_MS_CHAP2_SUCCESS) &&
503                    (vp->vendorcode == VENDOR_MICROSOFT)) {
504             if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
505                 slprintf(msg, BUF_LEN, "RADIUS: bad MS-CHAP2-Success packet");
506                 return -1;
507             }
508             memcpy(cstate->saresponse, vp->strvalue + 3,
509                    MS_AUTH_RESPONSE_LENGTH);
510             cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
511             ms_chap2_success = 1;
512 #endif /* CHAPMS */
513         }
514         vp = vp->next;
515     }
516
517     /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
518     if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
519         return -1;
520
521     return 0;
522 }
523
524 /**********************************************************************
525 * %FUNCTION: radius_acct_start
526 * %ARGUMENTS:
527 *  None
528 * %RETURNS:
529 *  Nothing
530 * %DESCRIPTION:
531 *  Sends a "start" accounting message to the RADIUS server.
532 ***********************************************************************/
533 static void
534 radius_acct_start(void)
535 {
536     UINT4 av_type;
537     int result;
538     VALUE_PAIR *send = NULL;
539     ipcp_options *ho = &ipcp_hisoptions[0];
540     u_int32_t hisaddr;
541
542     if (!rstate.initialized) {
543         return;
544     }
545
546     rstate.start_time = time(NULL);
547
548     strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
549
550     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
551                    rstate.session_id, 0, VENDOR_NONE);
552     rc_avpair_add(&send, PW_USER_NAME,
553                    rstate.user, 0, VENDOR_NONE);
554
555     av_type = PW_STATUS_START;
556     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
557
558     av_type = PW_FRAMED;
559     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
560
561     av_type = PW_PPP;
562     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
563
564     if (*remote_number) {
565         rc_avpair_add(&send, PW_CALLING_STATION_ID,
566                        remote_number, 0, VENDOR_NONE);
567     }
568
569     av_type = PW_RADIUS;
570     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
571
572
573     av_type = PW_ASYNC;
574     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
575
576     hisaddr = ho->hisaddr;
577     av_type = htonl(hisaddr);
578     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
579
580     if (rstate.acctserver) {
581         result = rc_acct_using_server(rstate.acctserver,
582                                       rstate.client_port, send);
583     } else {
584         result = rc_acct(rstate.client_port, send);
585     }
586
587     rc_avpair_free(send);
588
589     if (result != OK_RC) {
590         /* RADIUS server could be down so make this a warning */
591         syslog(LOG_WARNING,
592                 "Accounting START failed for %s", rstate.user);
593     } else {
594         rstate.accounting_started = 1;
595     }
596 }
597
598 /**********************************************************************
599 * %FUNCTION: radius_acct_stop
600 * %ARGUMENTS:
601 *  None
602 * %RETURNS:
603 *  Nothing
604 * %DESCRIPTION:
605 *  Sends a "stop" accounting message to the RADIUS server.
606 ***********************************************************************/
607 static void
608 radius_acct_stop(void)
609 {
610     UINT4 av_type;
611     VALUE_PAIR *send = NULL;
612     ipcp_options *ho = &ipcp_hisoptions[0];
613     u_int32_t hisaddr;
614     int result;
615
616     if (!rstate.initialized) {
617         return;
618     }
619
620     if (!rstate.accounting_started) {
621         return;
622     }
623
624     rstate.accounting_started = 0;
625     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
626                    0, VENDOR_NONE);
627
628     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
629
630     av_type = PW_STATUS_STOP;
631     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
632
633     av_type = PW_FRAMED;
634     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
635
636     av_type = PW_PPP;
637     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
638
639     av_type = PW_RADIUS;
640     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
641
642
643     if (link_stats_valid) {
644         av_type = link_connect_time;
645         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
646
647         av_type = link_stats.bytes_out;
648         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
649
650         av_type = link_stats.bytes_in;
651         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
652
653         av_type = link_stats.pkts_out;
654         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
655
656         av_type = link_stats.pkts_in;
657         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
658     }
659
660     if (*remote_number) {
661         rc_avpair_add(&send, PW_CALLING_STATION_ID,
662                        remote_number, 0, VENDOR_NONE);
663     }
664
665     av_type = PW_ASYNC;
666     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
667
668     hisaddr = ho->hisaddr;
669     av_type = htonl(hisaddr);
670     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
671
672     if (rstate.acctserver) {
673         result = rc_acct_using_server(rstate.acctserver,
674                                       rstate.client_port, send);
675     } else {
676         result = rc_acct(rstate.client_port, send);
677     }
678
679     if (result != OK_RC) {
680         /* RADIUS server could be down so make this a warning */
681         syslog(LOG_WARNING,
682                 "Accounting STOP failed for %s", rstate.user);
683     }
684     rc_avpair_free(send);
685 }
686
687 /**********************************************************************
688 * %FUNCTION: radius_ip_up
689 * %ARGUMENTS:
690 *  opaque -- ignored
691 *  arg -- ignored
692 * %RETURNS:
693 *  Nothing
694 * %DESCRIPTION:
695 *  Called when IPCP is up.  We'll do a start-accounting record.
696 ***********************************************************************/
697 static void
698 radius_ip_up(void *opaque, int arg)
699 {
700     radius_acct_start();
701 }
702
703 /**********************************************************************
704 * %FUNCTION: radius_ip_down
705 * %ARGUMENTS:
706 *  opaque -- ignored
707 *  arg -- ignored
708 * %RETURNS:
709 *  Nothing
710 * %DESCRIPTION:
711 *  Called when IPCP is down.  We'll do a stop-accounting record.
712 ***********************************************************************/
713 static void
714 radius_ip_down(void *opaque, int arg)
715 {
716     radius_acct_stop();
717 }
718
719 /**********************************************************************
720 * %FUNCTION: radius_init
721 * %ARGUMENTS:
722 *  msg -- buffer of size BUF_LEN for error message
723 * %RETURNS:
724 *  negative on failure; non-negative on success
725 * %DESCRIPTION:
726 *  Initializes radiusclient library
727 ***********************************************************************/
728 static int
729 radius_init(char *msg)
730 {
731     if (rstate.initialized) {
732         return 0;
733     }
734
735     if (config_file && *config_file) {
736         strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
737     }
738
739     rstate.initialized = 1;
740
741     if (rc_read_config(rstate.config_file) != 0) {
742         slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
743                  rstate.config_file);
744         return -1;
745     }
746
747     if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
748         slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
749                  rc_conf_str("dictionary"));
750         return -1;
751     }
752
753     if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)   {
754         slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
755                  rc_conf_str("mapfile"));
756         return -1;
757     }
758     return 0;
759 }
760
761 /**********************************************************************
762 * %FUNCTION: get_client_port
763 * %ARGUMENTS:
764 *  ifname -- PPP interface name (e.g. "ppp7")
765 * %RETURNS:
766 *  The NAS port number (e.g. 7)
767 * %DESCRIPTION:
768 *  Extracts the port number from the interface name
769 ***********************************************************************/
770 static int
771 get_client_port(char *ifname)
772 {
773     int port;
774     if (sscanf(ifname, "ppp%d", &port) == 1) {
775         return port;
776     }
777     return rc_map2id(ifname);
778 }
779
780 /**********************************************************************
781 * %FUNCTION: radius_allowed_address
782 * %ARGUMENTS:
783 *  addr -- IP address
784 * %RETURNS:
785 *  1 if we're allowed to use that IP address; 0 if not; -1 if we do
786 *  not know.
787 ***********************************************************************/
788 static int
789 radius_allowed_address(u_int32_t addr)
790 {
791     ipcp_options *wo = &ipcp_wantoptions[0];
792
793     if (!rstate.choose_ip) {
794         /* If RADIUS server said any address is OK, then fine... */
795         if (rstate.any_ip_addr_ok) {
796             return 1;
797         }
798
799         /* Sigh... if an address was supplied for remote host in pppd
800            options, it has to match that.  */
801         if (wo->hisaddr != 0 && wo->hisaddr == addr) {
802             return 1;
803         }
804
805         return 0;
806     }
807     if (addr == rstate.ip_addr) return 1;
808     return 0;
809 }
810
811 /* Useful for other plugins */
812 char *radius_logged_in_user(void)
813 {
814     return rstate.user;
815 }