]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radius.c
6d65b96e22b7e86e3a63be6afc310763e72b6de1
[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.12 2002/07/24 20:13:12 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 #ifdef MAXOCTETS
508             case PW_SESSION_OCTETS_LIMIT:
509                 /* Session traffic limit */
510                 maxoctets = vp->lvalue;
511                 break;
512             case PW_OCTETS_DIRECTION:
513                 /* Session traffic limit direction check */
514                 maxoctets_dir = ( vp->lvalue > 4 ) 0 : vp->lvalue ;
515                 break;
516 #endif          
517             case PW_FRAMED_IP_ADDRESS:
518                 /* seting up remote IP addresses */
519                 remote = vp->lvalue;
520                 if (remote == 0xffffffff) {
521                     /* 0xffffffff means user should be allowed to select one */
522                     rstate.any_ip_addr_ok = 1;
523                 } else if (remote != 0xfffffffe) {
524                     /* 0xfffffffe means NAS should select an ip address */
525                     remote = htonl(vp->lvalue);
526                     if (bad_ip_adrs (remote)) {
527                         slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
528                                  remote, rstate.user);
529                         return -1;
530                     }
531                     rstate.choose_ip = 1;
532                     rstate.ip_addr = remote;
533                 }
534                 break;
535             }
536 #ifdef CHAPMS
537         } else if (vp->vendorcode == VENDOR_MICROSOFT) {
538             switch (vp->attribute) {
539             case PW_MS_CHAP2_SUCCESS:
540                 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
541                     slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
542                     return -1;
543                 }
544                 memcpy(cstate->saresponse, vp->strvalue + 3,
545                        MS_AUTH_RESPONSE_LENGTH);
546                 cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
547                 ms_chap2_success = 1;
548                 break;
549
550 #ifdef MPPE
551             case PW_MS_CHAP_MPPE_KEYS:
552                 if (radius_setmppekeys(vp, req_info, cstate) < 0) {
553                     slprintf(msg, BUF_LEN,
554                              "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
555                     return -1;
556                 }
557                 break;
558
559             case PW_MS_MPPE_SEND_KEY:
560             case PW_MS_MPPE_RECV_KEY:
561                 if (radius_setmppekeys2(vp, req_info) < 0) {
562                     slprintf(msg, BUF_LEN,
563                              "RADIUS: bad MS-MPPE-%s-Key attribute",
564                              (vp->attribute == PW_MS_MPPE_SEND_KEY)?
565                              "Send": "Recv");
566                     return -1;
567                 }
568                 break;
569 #endif /* MPPE */
570 #if 0
571             case PW_MS_MPPE_ENCRYPTION_POLICY:
572             case PW_MS_MPPE_ENCRYPTION_TYPES:
573             case PW_MS_PRIMARY_DNS_SERVER:
574             case PW_MS_SECONDARY_DNS_SERVER:
575             case PW_MS_PRIMARY_NBNS_SERVER:
576             case PW_MS_SECONDARY_NBNS_SERVER:
577                 break;
578 #endif
579             }
580 #endif /* CHAPMS */
581         }
582         vp = vp->next;
583     }
584
585     /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
586     if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
587         return -1;
588
589     return 0;
590 }
591
592 #ifdef MPPE
593 /**********************************************************************
594 * %FUNCTION: radius_setmppekeys
595 * %ARGUMENTS:
596 *  vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
597 *  req_info -- radius request information used for encryption
598 *  cstate -- chap_state structure for challenge info
599 * %RETURNS:
600 *  >= 0 on success; -1 on failure
601 * %DESCRIPTION:
602 *  Decrypt the "key" provided by the RADIUS server for MPPE encryption.
603 *  See RFC 2548.
604 ***********************************************************************/
605 static int
606 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
607 {
608     int i;
609     MD5_CTX Context;
610     u_char  plain[32];
611     u_char  buf[16];
612
613     if (vp->lvalue != 32) {
614         error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
615               vp->lvalue);
616         return -1;
617     }
618
619     memcpy(plain, vp->strvalue, sizeof(plain));
620
621     MD5Init(&Context);
622     MD5Update(&Context, req_info->secret, strlen(req_info->secret));
623     MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
624     MD5Final(buf, &Context);
625
626     for (i = 0; i < 16; i++)
627         plain[i] ^= buf[i];
628
629     MD5Init(&Context);
630     MD5Update(&Context, req_info->secret, strlen(req_info->secret));
631     MD5Update(&Context, vp->strvalue, 16);
632     MD5Final(buf, &Context);
633
634     for(i = 0; i < 16; i++)
635         plain[i + 16] ^= buf[i];
636
637     /*
638      * Annoying.  The "key" returned is just the NTPasswordHashHash, which
639      * the NAS (us) doesn't need; we only need the start key.  So we have
640      * to generate the start key, sigh.  NB: We do not support the LM-Key.
641      */
642     mppe_set_keys(cstate->challenge, &plain[8]);
643
644     return 0;    
645 }
646
647 /**********************************************************************
648 * %FUNCTION: radius_setmppekeys2
649 * %ARGUMENTS:
650 *  vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
651 *  req_info -- radius request information used for encryption
652 * %RETURNS:
653 *  >= 0 on success; -1 on failure
654 * %DESCRIPTION:
655 *  Decrypt the key provided by the RADIUS server for MPPE encryption.
656 *  See RFC 2548.
657 ***********************************************************************/
658 static int
659 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
660 {
661     int i;
662     MD5_CTX Context;
663     u_char  *salt = vp->strvalue;
664     u_char  *crypt = vp->strvalue + 2;
665     u_char  plain[32];
666     u_char  buf[MD5_SIGNATURE_SIZE];
667     char    *type = "Send";
668
669     if (vp->attribute == PW_MS_MPPE_RECV_KEY)
670         type = "Recv";
671
672     if (vp->lvalue != 34) {
673         error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
674               vp->lvalue, type);
675         return -1;
676     }
677
678     if ((salt[0] & 0x80) == 0) {
679         error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
680         return -1;
681     }
682
683     memcpy(plain, crypt, 32);
684
685     MD5Init(&Context);
686     MD5Update(&Context, req_info->secret, strlen(req_info->secret));
687     MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
688     MD5Update(&Context, salt, 2);
689     MD5Final(buf, &Context);
690
691     for (i = 0; i < 16; i++)
692         plain[i] ^= buf[i];
693
694     if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
695         error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
696               (int) plain[0], type);
697         return -1;
698     }
699
700     MD5Init(&Context);
701     MD5Update(&Context, req_info->secret, strlen(req_info->secret));
702     MD5Update(&Context, crypt, 16);
703     MD5Final(buf, &Context);
704
705     plain[16] ^= buf[0]; /* only need the first byte */
706
707     if (vp->attribute == PW_MS_MPPE_SEND_KEY)
708         memcpy(mppe_send_key, plain + 1, 16);
709     else
710         memcpy(mppe_recv_key, plain + 1, 16);
711
712     return 0;
713 }
714 #endif /* MPPE */
715
716 /**********************************************************************
717 * %FUNCTION: radius_acct_start
718 * %ARGUMENTS:
719 *  None
720 * %RETURNS:
721 *  Nothing
722 * %DESCRIPTION:
723 *  Sends a "start" accounting message to the RADIUS server.
724 ***********************************************************************/
725 static void
726 radius_acct_start(void)
727 {
728     UINT4 av_type;
729     int result;
730     VALUE_PAIR *send = NULL;
731     ipcp_options *ho = &ipcp_hisoptions[0];
732     u_int32_t hisaddr;
733
734     if (!rstate.initialized) {
735         return;
736     }
737
738     rstate.start_time = time(NULL);
739
740     strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
741
742     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
743                    rstate.session_id, 0, VENDOR_NONE);
744     rc_avpair_add(&send, PW_USER_NAME,
745                    rstate.user, 0, VENDOR_NONE);
746
747     av_type = PW_STATUS_START;
748     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
749
750     av_type = PW_FRAMED;
751     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
752
753     av_type = PW_PPP;
754     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
755
756     if (*remote_number) {
757         rc_avpair_add(&send, PW_CALLING_STATION_ID,
758                        remote_number, 0, VENDOR_NONE);
759     }
760
761     av_type = PW_RADIUS;
762     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
763
764
765     av_type = PW_ASYNC;
766     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
767
768     hisaddr = ho->hisaddr;
769     av_type = htonl(hisaddr);
770     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
771
772     if (rstate.acctserver) {
773         result = rc_acct_using_server(rstate.acctserver,
774                                       rstate.client_port, send);
775     } else {
776         result = rc_acct(rstate.client_port, send);
777     }
778
779     rc_avpair_free(send);
780
781     if (result != OK_RC) {
782         /* RADIUS server could be down so make this a warning */
783         syslog(LOG_WARNING,
784                 "Accounting START failed for %s", rstate.user);
785     } else {
786         rstate.accounting_started = 1;
787     }
788 }
789
790 /**********************************************************************
791 * %FUNCTION: radius_acct_stop
792 * %ARGUMENTS:
793 *  None
794 * %RETURNS:
795 *  Nothing
796 * %DESCRIPTION:
797 *  Sends a "stop" accounting message to the RADIUS server.
798 ***********************************************************************/
799 static void
800 radius_acct_stop(void)
801 {
802     UINT4 av_type;
803     VALUE_PAIR *send = NULL;
804     ipcp_options *ho = &ipcp_hisoptions[0];
805     u_int32_t hisaddr;
806     int result;
807
808     if (!rstate.initialized) {
809         return;
810     }
811
812     if (!rstate.accounting_started) {
813         return;
814     }
815
816     rstate.accounting_started = 0;
817     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
818                    0, VENDOR_NONE);
819
820     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
821
822     av_type = PW_STATUS_STOP;
823     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
824
825     av_type = PW_FRAMED;
826     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
827
828     av_type = PW_PPP;
829     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
830
831     av_type = PW_RADIUS;
832     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
833
834
835     if (link_stats_valid) {
836         av_type = link_connect_time;
837         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
838
839         av_type = link_stats.bytes_out;
840         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
841
842         av_type = link_stats.bytes_in;
843         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
844
845         av_type = link_stats.pkts_out;
846         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
847
848         av_type = link_stats.pkts_in;
849         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
850     }
851
852     if (*remote_number) {
853         rc_avpair_add(&send, PW_CALLING_STATION_ID,
854                        remote_number, 0, VENDOR_NONE);
855     }
856
857     av_type = PW_ASYNC;
858     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
859
860     hisaddr = ho->hisaddr;
861     av_type = htonl(hisaddr);
862     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
863
864     if (rstate.acctserver) {
865         result = rc_acct_using_server(rstate.acctserver,
866                                       rstate.client_port, send);
867     } else {
868         result = rc_acct(rstate.client_port, send);
869     }
870
871     if (result != OK_RC) {
872         /* RADIUS server could be down so make this a warning */
873         syslog(LOG_WARNING,
874                 "Accounting STOP failed for %s", rstate.user);
875     }
876     rc_avpair_free(send);
877 }
878
879 /**********************************************************************
880 * %FUNCTION: radius_acct_interim
881 * %ARGUMENTS:
882 *  None
883 * %RETURNS:
884 *  Nothing
885 * %DESCRIPTION:
886 *  Sends an interim accounting message to the RADIUS server
887 ***********************************************************************/
888 static void
889 radius_acct_interim(void)
890 {
891     UINT4 av_type;
892     VALUE_PAIR *send = NULL;
893     ipcp_options *ho = &ipcp_hisoptions[0];
894     u_int32_t hisaddr;
895     int result;
896
897     if (!rstate.initialized) {
898         return;
899     }
900
901     if (!rstate.accounting_started) {
902         return;
903     }
904
905     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
906                    0, VENDOR_NONE);
907
908     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
909
910     av_type = PW_STATUS_ALIVE;
911     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
912
913     av_type = PW_FRAMED;
914     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
915
916     av_type = PW_PPP;
917     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
918
919     av_type = PW_RADIUS;
920     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
921
922     /* Update link stats */
923     update_link_stats(0);
924
925     if (link_stats_valid) {
926         link_stats_valid = 0; /* Force later code to update */
927
928         av_type = link_connect_time;
929         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
930
931         av_type = link_stats.bytes_out;
932         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
933
934         av_type = link_stats.bytes_in;
935         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
936
937         av_type = link_stats.pkts_out;
938         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
939
940         av_type = link_stats.pkts_in;
941         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
942     }
943
944     if (*remote_number) {
945         rc_avpair_add(&send, PW_CALLING_STATION_ID,
946                        remote_number, 0, VENDOR_NONE);
947     }
948
949     av_type = PW_ASYNC;
950     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
951
952     hisaddr = ho->hisaddr;
953     av_type = htonl(hisaddr);
954     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
955
956     if (rstate.acctserver) {
957         result = rc_acct_using_server(rstate.acctserver,
958                                       rstate.client_port, send);
959     } else {
960         result = rc_acct(rstate.client_port, send);
961     }
962
963     if (result != OK_RC) {
964         /* RADIUS server could be down so make this a warning */
965         syslog(LOG_WARNING,
966                 "Interim accounting failed for %s", rstate.user);
967     }
968     rc_avpair_free(send);
969 }
970
971 /**********************************************************************
972 * %FUNCTION: radius_ip_up
973 * %ARGUMENTS:
974 *  opaque -- ignored
975 *  arg -- ignored
976 * %RETURNS:
977 *  Nothing
978 * %DESCRIPTION:
979 *  Called when IPCP is up.  We'll do a start-accounting record.
980 ***********************************************************************/
981 static void
982 radius_ip_up(void *opaque, int arg)
983 {
984     radius_acct_start();
985 }
986
987 /**********************************************************************
988 * %FUNCTION: radius_ip_down
989 * %ARGUMENTS:
990 *  opaque -- ignored
991 *  arg -- ignored
992 * %RETURNS:
993 *  Nothing
994 * %DESCRIPTION:
995 *  Called when IPCP is down.  We'll do a stop-accounting record.
996 ***********************************************************************/
997 static void
998 radius_ip_down(void *opaque, int arg)
999 {
1000     radius_acct_stop();
1001 }
1002
1003 /**********************************************************************
1004 * %FUNCTION: radius_init
1005 * %ARGUMENTS:
1006 *  msg -- buffer of size BUF_LEN for error message
1007 * %RETURNS:
1008 *  negative on failure; non-negative on success
1009 * %DESCRIPTION:
1010 *  Initializes radiusclient library
1011 ***********************************************************************/
1012 static int
1013 radius_init(char *msg)
1014 {
1015     if (rstate.initialized) {
1016         return 0;
1017     }
1018
1019     if (config_file && *config_file) {
1020         strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1021     }
1022
1023     rstate.initialized = 1;
1024
1025     if (rc_read_config(rstate.config_file) != 0) {
1026         slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1027                  rstate.config_file);
1028         return -1;
1029     }
1030
1031     if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1032         slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1033                  rc_conf_str("dictionary"));
1034         return -1;
1035     }
1036
1037     if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)   {
1038         slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1039                  rc_conf_str("mapfile"));
1040         return -1;
1041     }
1042     return 0;
1043 }
1044
1045 /**********************************************************************
1046 * %FUNCTION: get_client_port
1047 * %ARGUMENTS:
1048 *  ifname -- PPP interface name (e.g. "ppp7")
1049 * %RETURNS:
1050 *  The NAS port number (e.g. 7)
1051 * %DESCRIPTION:
1052 *  Extracts the port number from the interface name
1053 ***********************************************************************/
1054 static int
1055 get_client_port(char *ifname)
1056 {
1057     int port;
1058     if (sscanf(ifname, "ppp%d", &port) == 1) {
1059         return port;
1060     }
1061     return rc_map2id(ifname);
1062 }
1063
1064 /**********************************************************************
1065 * %FUNCTION: radius_allowed_address
1066 * %ARGUMENTS:
1067 *  addr -- IP address
1068 * %RETURNS:
1069 *  1 if we're allowed to use that IP address; 0 if not; -1 if we do
1070 *  not know.
1071 ***********************************************************************/
1072 static int
1073 radius_allowed_address(u_int32_t addr)
1074 {
1075     ipcp_options *wo = &ipcp_wantoptions[0];
1076
1077     if (!rstate.choose_ip) {
1078         /* If RADIUS server said any address is OK, then fine... */
1079         if (rstate.any_ip_addr_ok) {
1080             return 1;
1081         }
1082
1083         /* Sigh... if an address was supplied for remote host in pppd
1084            options, it has to match that.  */
1085         if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1086             return 1;
1087         }
1088
1089         return 0;
1090     }
1091     if (addr == rstate.ip_addr) return 1;
1092     return 0;
1093 }
1094
1095 /* Useful for other plugins */
1096 char *radius_logged_in_user(void)
1097 {
1098     return rstate.user;
1099 }