]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radius.c
Patch from Frank Cusack to add support for MSCHAPv2.
[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.6 2002/03/05 15:14:05 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_FRAMED_IP_ADDRESS) &&
480                    (vp->vendorcode == VENDOR_NONE)) {
481             /* seting up remote IP addresses */
482             remote = vp->lvalue;
483             if (remote == 0xffffffff) {
484                 /* 0xffffffff means user should be allowed to select one */
485                 rstate.any_ip_addr_ok = 1;
486             } else if (remote != 0xfffffffe) {
487                 /* 0xfffffffe means NAS should select an ip address */
488                 remote = htonl(vp->lvalue);
489                 if (bad_ip_adrs (remote)) {
490                     slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
491                              remote, rstate.user);
492                     return -1;
493                 }
494                 rstate.choose_ip = 1;
495                 rstate.ip_addr = remote;
496             }
497 #ifdef CHAPMS
498         } else if ((vp->attribute == PW_MS_CHAP2_SUCCESS) &&
499                    (vp->vendorcode == VENDOR_MICROSOFT)) {
500             if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
501                 slprintf(msg, BUF_LEN, "RADIUS: bad MS-CHAP2-Success packet");
502                 return -1;
503             }
504             memcpy(cstate->saresponse, vp->strvalue + 3,
505                    MS_AUTH_RESPONSE_LENGTH);
506             cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
507             ms_chap2_success = 1;
508 #endif /* CHAPMS */
509         }
510         vp = vp->next;
511     }
512
513     /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
514     if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
515         return -1;
516
517     return 0;
518 }
519
520 /**********************************************************************
521 * %FUNCTION: radius_acct_start
522 * %ARGUMENTS:
523 *  None
524 * %RETURNS:
525 *  Nothing
526 * %DESCRIPTION:
527 *  Sends a "start" accounting message to the RADIUS server.
528 ***********************************************************************/
529 static void
530 radius_acct_start(void)
531 {
532     UINT4 av_type;
533     int result;
534     VALUE_PAIR *send = NULL;
535     ipcp_options *ho = &ipcp_hisoptions[0];
536     u_int32_t hisaddr;
537
538     if (!rstate.initialized) {
539         return;
540     }
541
542     rstate.start_time = time(NULL);
543
544     strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
545
546     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
547                    rstate.session_id, 0, VENDOR_NONE);
548     rc_avpair_add(&send, PW_USER_NAME,
549                    rstate.user, 0, VENDOR_NONE);
550
551     av_type = PW_STATUS_START;
552     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
553
554     av_type = PW_FRAMED;
555     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
556
557     av_type = PW_PPP;
558     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
559
560     if (*remote_number) {
561         rc_avpair_add(&send, PW_CALLING_STATION_ID,
562                        remote_number, 0, VENDOR_NONE);
563     }
564
565     av_type = PW_RADIUS;
566     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
567
568
569     av_type = PW_ASYNC;
570     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
571
572     hisaddr = ho->hisaddr;
573     av_type = htonl(hisaddr);
574     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
575
576     if (rstate.acctserver) {
577         result = rc_acct_using_server(rstate.acctserver,
578                                       rstate.client_port, send);
579     } else {
580         result = rc_acct(rstate.client_port, send);
581     }
582
583     rc_avpair_free(send);
584
585     if (result != OK_RC) {
586         /* RADIUS server could be down so make this a warning */
587         syslog(LOG_WARNING,
588                 "Accounting START failed for %s", rstate.user);
589     } else {
590         rstate.accounting_started = 1;
591     }
592 }
593
594 /**********************************************************************
595 * %FUNCTION: radius_acct_stop
596 * %ARGUMENTS:
597 *  None
598 * %RETURNS:
599 *  Nothing
600 * %DESCRIPTION:
601 *  Sends a "stop" accounting message to the RADIUS server.
602 ***********************************************************************/
603 static void
604 radius_acct_stop(void)
605 {
606     UINT4 av_type;
607     VALUE_PAIR *send = NULL;
608     ipcp_options *ho = &ipcp_hisoptions[0];
609     u_int32_t hisaddr;
610     int result;
611
612     if (!rstate.initialized) {
613         return;
614     }
615
616     if (!rstate.accounting_started) {
617         return;
618     }
619
620     rstate.accounting_started = 0;
621     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
622                    0, VENDOR_NONE);
623
624     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
625
626     av_type = PW_STATUS_STOP;
627     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
628
629     av_type = PW_FRAMED;
630     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
631
632     av_type = PW_PPP;
633     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
634
635     av_type = PW_RADIUS;
636     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
637
638
639     if (link_stats_valid) {
640         av_type = link_connect_time;
641         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
642
643         av_type = link_stats.bytes_out;
644         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
645
646         av_type = link_stats.bytes_in;
647         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
648
649         av_type = link_stats.pkts_out;
650         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
651
652         av_type = link_stats.pkts_in;
653         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
654     }
655
656     if (*remote_number) {
657         rc_avpair_add(&send, PW_CALLING_STATION_ID,
658                        remote_number, 0, VENDOR_NONE);
659     }
660
661     av_type = PW_ASYNC;
662     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
663
664     hisaddr = ho->hisaddr;
665     av_type = htonl(hisaddr);
666     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
667
668     if (rstate.acctserver) {
669         result = rc_acct_using_server(rstate.acctserver,
670                                       rstate.client_port, send);
671     } else {
672         result = rc_acct(rstate.client_port, send);
673     }
674
675     if (result != OK_RC) {
676         /* RADIUS server could be down so make this a warning */
677         syslog(LOG_WARNING,
678                 "Accounting STOP failed for %s", rstate.user);
679     }
680     rc_avpair_free(send);
681 }
682
683 /**********************************************************************
684 * %FUNCTION: radius_ip_up
685 * %ARGUMENTS:
686 *  opaque -- ignored
687 *  arg -- ignored
688 * %RETURNS:
689 *  Nothing
690 * %DESCRIPTION:
691 *  Called when IPCP is up.  We'll do a start-accounting record.
692 ***********************************************************************/
693 static void
694 radius_ip_up(void *opaque, int arg)
695 {
696     radius_acct_start();
697 }
698
699 /**********************************************************************
700 * %FUNCTION: radius_ip_down
701 * %ARGUMENTS:
702 *  opaque -- ignored
703 *  arg -- ignored
704 * %RETURNS:
705 *  Nothing
706 * %DESCRIPTION:
707 *  Called when IPCP is down.  We'll do a stop-accounting record.
708 ***********************************************************************/
709 static void
710 radius_ip_down(void *opaque, int arg)
711 {
712     radius_acct_stop();
713 }
714
715 /**********************************************************************
716 * %FUNCTION: radius_init
717 * %ARGUMENTS:
718 *  msg -- buffer of size BUF_LEN for error message
719 * %RETURNS:
720 *  negative on failure; non-negative on success
721 * %DESCRIPTION:
722 *  Initializes radiusclient library
723 ***********************************************************************/
724 static int
725 radius_init(char *msg)
726 {
727     if (rstate.initialized) {
728         return 0;
729     }
730
731     if (config_file && *config_file) {
732         strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
733     }
734
735     rstate.initialized = 1;
736
737     if (rc_read_config(rstate.config_file) != 0) {
738         slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
739                  rstate.config_file);
740         return -1;
741     }
742
743     if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
744         slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
745                  rc_conf_str("dictionary"));
746         return -1;
747     }
748
749     if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)   {
750         slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
751                  rc_conf_str("mapfile"));
752         return -1;
753     }
754     return 0;
755 }
756
757 /**********************************************************************
758 * %FUNCTION: get_client_port
759 * %ARGUMENTS:
760 *  ifname -- PPP interface name (e.g. "ppp7")
761 * %RETURNS:
762 *  The NAS port number (e.g. 7)
763 * %DESCRIPTION:
764 *  Extracts the port number from the interface name
765 ***********************************************************************/
766 static int
767 get_client_port(char *ifname)
768 {
769     int port;
770     if (sscanf(ifname, "ppp%d", &port) == 1) {
771         return port;
772     }
773     return rc_map2id(ifname);
774 }
775
776 /**********************************************************************
777 * %FUNCTION: radius_allowed_address
778 * %ARGUMENTS:
779 *  addr -- IP address
780 * %RETURNS:
781 *  1 if we're allowed to use that IP address; 0 if not; -1 if we do
782 *  not know.
783 ***********************************************************************/
784 static int
785 radius_allowed_address(u_int32_t addr)
786 {
787     ipcp_options *wo = &ipcp_wantoptions[0];
788
789     if (!rstate.choose_ip) {
790         /* If RADIUS server said any address is OK, then fine... */
791         if (rstate.any_ip_addr_ok) {
792             return 1;
793         }
794
795         /* Sigh... if an address was supplied for remote host in pppd
796            options, it has to match that.  */
797         if (wo->hisaddr != 0 && wo->hisaddr == addr) {
798             return 1;
799         }
800
801         return 0;
802     }
803     if (addr == rstate.ip_addr) return 1;
804     return 0;
805 }
806
807 /* Useful for other plugins */
808 char *radius_logged_in_user(void)
809 {
810     return rstate.user;
811 }