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