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