]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radius.c
53757abcbb1701f0b9952fbe64853b08c4e4b7fe
[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.16 2002/10/01 08:36:49 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
434     /* Add user specified vp's */
435     if (rstate.avp)
436         rc_avpair_insert(&send, NULL, rstate.avp);
437
438     /*
439      * make authentication with RADIUS server
440      */
441
442     if (rstate.authserver) {
443         result = rc_auth_using_server(rstate.authserver,
444                                       rstate.client_port, send,
445                                       &received, radius_msg, req_info);
446     } else {
447         result = rc_auth(rstate.client_port, send, &received, radius_msg,
448                          req_info);
449     }
450
451     if (result == OK_RC) {
452         if (!rstate.done_chap_once) {
453             if (radius_setparams(cstate, received, radius_msg, req_info) < 0) {
454                 error("%s", radius_msg);
455                 result = ERROR_RC;
456             } else {
457                 rstate.done_chap_once = 1;
458             }
459         }
460     }
461
462     rc_avpair_free(received);
463     rc_avpair_free (send);
464     return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
465 }
466
467 /**********************************************************************
468 * %FUNCTION: make_username_realm
469 * %ARGUMENTS:
470 *  user -- the user given to pppd
471 * %RETURNS:
472 *  Nothing
473 * %DESCRIPTION:
474 *  Copies user into rstate.user.  If it lacks a realm (no "@domain" part),
475 * then the default realm from the radiusclient config file is added.
476 ***********************************************************************/
477 static void
478 make_username_realm(char *user)
479 {
480     char *default_realm;
481
482     if ( user != NULL ) {
483         strlcpy(rstate.user, user, sizeof(rstate.user));
484     }  else {
485         rstate.user[0] = 0;
486     }
487
488     default_realm = rc_conf_str("default_realm");
489
490     if (!strchr(rstate.user, '@') &&
491         default_realm &&
492         (*default_realm != '\0')) {
493         strlcat(rstate.user, "@", sizeof(rstate.user));
494         strlcat(rstate.user, default_realm, sizeof(rstate.user));
495     }
496 }
497
498 /**********************************************************************
499 * %FUNCTION: radius_setparams
500 * %ARGUMENTS:
501 *  cstate -- pppd's chap_state structure
502 *  vp -- received value-pairs
503 *  msg -- buffer in which to place error message.  Holds up to BUF_LEN chars
504 * %RETURNS:
505 *  >= 0 on success; -1 on failure
506 * %DESCRIPTION:
507 *  Parses attributes sent by RADIUS server and sets them in pppd.
508 ***********************************************************************/
509 static int
510 radius_setparams(chap_state *cstate, VALUE_PAIR *vp, char *msg,
511                  REQUEST_INFO *req_info)
512 {
513     u_int32_t remote;
514     int ms_chap2_success = 0;
515
516     /* Send RADIUS attributes to anyone else who might be interested */
517     if (radius_attributes_hook) {
518         (*radius_attributes_hook)(vp);
519     }
520
521     /*
522      * service type (if not framed then quit),
523      * new IP address (RADIUS can define static IP for some users),
524      */
525
526     while (vp) {
527         if (vp->vendorcode == VENDOR_NONE) {
528             switch (vp->attribute) {
529             case PW_SERVICE_TYPE:
530                 /* check for service type       */
531                 /* if not FRAMED then exit      */
532                 if (vp->lvalue != PW_FRAMED) {
533                     slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
534                              vp->lvalue, rstate.user);
535                     return -1;
536                 }
537                 break;
538
539             case PW_FRAMED_PROTOCOL:
540                 /* check for framed protocol type       */
541                 /* if not PPP then also exit            */
542                 if (vp->lvalue != PW_PPP) {
543                     slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
544                              vp->lvalue, rstate.user);
545                     return -1;
546                 }
547                 break;
548
549             case PW_SESSION_TIMEOUT:
550                 /* Session timeout */
551                 maxconnect = vp->lvalue;
552                 break;
553 #ifdef MAXOCTETS
554             case PW_SESSION_OCTETS_LIMIT:
555                 /* Session traffic limit */
556                 maxoctets = vp->lvalue;
557                 break;
558             case PW_OCTETS_DIRECTION:
559                 /* Session traffic limit direction check */
560                 maxoctets_dir = ( vp->lvalue > 4 ) ? 0 : vp->lvalue ;
561                 break;
562 #endif
563             case PW_ACCT_INTERIM_INTERVAL:
564                 /* Send accounting updates every few seconds */
565                 rstate.acct_interim_interval = vp->lvalue;
566                 /* RFC says it MUST NOT be less than 60 seconds */
567                 /* We use "0" to signify not sending updates */
568                 if (rstate.acct_interim_interval &&
569                     rstate.acct_interim_interval < 60) {
570                     rstate.acct_interim_interval = 60;
571                 }
572                 break;
573             case PW_FRAMED_IP_ADDRESS:
574                 /* seting up remote IP addresses */
575                 remote = vp->lvalue;
576                 if (remote == 0xffffffff) {
577                     /* 0xffffffff means user should be allowed to select one */
578                     rstate.any_ip_addr_ok = 1;
579                 } else if (remote != 0xfffffffe) {
580                     /* 0xfffffffe means NAS should select an ip address */
581                     remote = htonl(vp->lvalue);
582                     if (bad_ip_adrs (remote)) {
583                         slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
584                                  remote, rstate.user);
585                         return -1;
586                     }
587                     rstate.choose_ip = 1;
588                     rstate.ip_addr = remote;
589                 }
590                 break;
591             case PW_CLASS:
592                 /* Save Class attribute to pass it in accounting request */
593                 if (vp->lvalue <= MAXCLASSLEN) {
594                     rstate.class_len=vp->lvalue;
595                     memcpy(rstate.class, vp->strvalue, rstate.class_len);
596                 } /* else too big for our buffer - ignore it */
597                 break;
598             }
599
600
601 #ifdef CHAPMS
602         } else if (vp->vendorcode == VENDOR_MICROSOFT) {
603             switch (vp->attribute) {
604             case PW_MS_CHAP2_SUCCESS:
605                 if ((vp->lvalue != 43) || strncmp(vp->strvalue + 1, "S=", 2)) {
606                     slprintf(msg,BUF_LEN,"RADIUS: bad MS-CHAP2-Success packet");
607                     return -1;
608                 }
609                 memcpy(cstate->saresponse, vp->strvalue + 3,
610                        MS_AUTH_RESPONSE_LENGTH);
611                 cstate->saresponse[MS_AUTH_RESPONSE_LENGTH] = '\0';
612                 ms_chap2_success = 1;
613                 break;
614
615 #ifdef MPPE
616             case PW_MS_CHAP_MPPE_KEYS:
617                 if (radius_setmppekeys(vp, req_info, cstate) < 0) {
618                     slprintf(msg, BUF_LEN,
619                              "RADIUS: bad MS-CHAP-MPPE-Keys attribute");
620                     return -1;
621                 }
622                 break;
623
624             case PW_MS_MPPE_SEND_KEY:
625             case PW_MS_MPPE_RECV_KEY:
626                 if (radius_setmppekeys2(vp, req_info) < 0) {
627                     slprintf(msg, BUF_LEN,
628                              "RADIUS: bad MS-MPPE-%s-Key attribute",
629                              (vp->attribute == PW_MS_MPPE_SEND_KEY)?
630                              "Send": "Recv");
631                     return -1;
632                 }
633                 break;
634 #endif /* MPPE */
635 #if 0
636             case PW_MS_MPPE_ENCRYPTION_POLICY:
637             case PW_MS_MPPE_ENCRYPTION_TYPES:
638             case PW_MS_PRIMARY_DNS_SERVER:
639             case PW_MS_SECONDARY_DNS_SERVER:
640             case PW_MS_PRIMARY_NBNS_SERVER:
641             case PW_MS_SECONDARY_NBNS_SERVER:
642                 break;
643 #endif
644             }
645 #endif /* CHAPMS */
646         }
647         vp = vp->next;
648     }
649
650     /* Require a valid MS-CHAP2-SUCCESS for MS-CHAPv2 auth */
651     if (cstate && (cstate->chal_type == CHAP_MICROSOFT_V2) && !ms_chap2_success)
652         return -1;
653
654     return 0;
655 }
656
657 #ifdef MPPE
658 /**********************************************************************
659 * %FUNCTION: radius_setmppekeys
660 * %ARGUMENTS:
661 *  vp -- value pair holding MS-CHAP-MPPE-KEYS attribute
662 *  req_info -- radius request information used for encryption
663 *  cstate -- chap_state structure for challenge info
664 * %RETURNS:
665 *  >= 0 on success; -1 on failure
666 * %DESCRIPTION:
667 *  Decrypt the "key" provided by the RADIUS server for MPPE encryption.
668 *  See RFC 2548.
669 ***********************************************************************/
670 static int
671 radius_setmppekeys(VALUE_PAIR *vp, REQUEST_INFO *req_info, chap_state *cstate)
672 {
673     int i;
674     MD5_CTX Context;
675     u_char  plain[32];
676     u_char  buf[16];
677
678     if (vp->lvalue != 32) {
679         error("RADIUS: Incorrect attribute length (%d) for MS-CHAP-MPPE-Keys",
680               vp->lvalue);
681         return -1;
682     }
683
684     memcpy(plain, vp->strvalue, sizeof(plain));
685
686     MD5Init(&Context);
687     MD5Update(&Context, req_info->secret, strlen(req_info->secret));
688     MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
689     MD5Final(buf, &Context);
690
691     for (i = 0; i < 16; i++)
692         plain[i] ^= buf[i];
693
694     MD5Init(&Context);
695     MD5Update(&Context, req_info->secret, strlen(req_info->secret));
696     MD5Update(&Context, vp->strvalue, 16);
697     MD5Final(buf, &Context);
698
699     for(i = 0; i < 16; i++)
700         plain[i + 16] ^= buf[i];
701
702     /*
703      * Annoying.  The "key" returned is just the NTPasswordHashHash, which
704      * the NAS (us) doesn't need; we only need the start key.  So we have
705      * to generate the start key, sigh.  NB: We do not support the LM-Key.
706      */
707     mppe_set_keys(cstate->challenge, &plain[8]);
708
709     return 0;    
710 }
711
712 /**********************************************************************
713 * %FUNCTION: radius_setmppekeys2
714 * %ARGUMENTS:
715 *  vp -- value pair holding MS-MPPE-SEND-KEY or MS-MPPE-RECV-KEY attribute
716 *  req_info -- radius request information used for encryption
717 * %RETURNS:
718 *  >= 0 on success; -1 on failure
719 * %DESCRIPTION:
720 *  Decrypt the key provided by the RADIUS server for MPPE encryption.
721 *  See RFC 2548.
722 ***********************************************************************/
723 static int
724 radius_setmppekeys2(VALUE_PAIR *vp, REQUEST_INFO *req_info)
725 {
726     int i;
727     MD5_CTX Context;
728     u_char  *salt = vp->strvalue;
729     u_char  *crypt = vp->strvalue + 2;
730     u_char  plain[32];
731     u_char  buf[MD5_SIGNATURE_SIZE];
732     char    *type = "Send";
733
734     if (vp->attribute == PW_MS_MPPE_RECV_KEY)
735         type = "Recv";
736
737     if (vp->lvalue != 34) {
738         error("RADIUS: Incorrect attribute length (%d) for MS-MPPE-%s-Key",
739               vp->lvalue, type);
740         return -1;
741     }
742
743     if ((salt[0] & 0x80) == 0) {
744         error("RADIUS: Illegal salt value for MS-MPPE-%s-Key attribute", type);
745         return -1;
746     }
747
748     memcpy(plain, crypt, 32);
749
750     MD5Init(&Context);
751     MD5Update(&Context, req_info->secret, strlen(req_info->secret));
752     MD5Update(&Context, req_info->request_vector, AUTH_VECTOR_LEN);
753     MD5Update(&Context, salt, 2);
754     MD5Final(buf, &Context);
755
756     for (i = 0; i < 16; i++)
757         plain[i] ^= buf[i];
758
759     if (plain[0] != sizeof(mppe_send_key) /* 16 */) {
760         error("RADIUS: Incorrect key length (%d) for MS-MPPE-%s-Key attribute",
761               (int) plain[0], type);
762         return -1;
763     }
764
765     MD5Init(&Context);
766     MD5Update(&Context, req_info->secret, strlen(req_info->secret));
767     MD5Update(&Context, crypt, 16);
768     MD5Final(buf, &Context);
769
770     plain[16] ^= buf[0]; /* only need the first byte */
771
772     if (vp->attribute == PW_MS_MPPE_SEND_KEY)
773         memcpy(mppe_send_key, plain + 1, 16);
774     else
775         memcpy(mppe_recv_key, plain + 1, 16);
776
777     return 0;
778 }
779 #endif /* MPPE */
780
781 /**********************************************************************
782 * %FUNCTION: radius_acct_start
783 * %ARGUMENTS:
784 *  None
785 * %RETURNS:
786 *  Nothing
787 * %DESCRIPTION:
788 *  Sends a "start" accounting message to the RADIUS server.
789 ***********************************************************************/
790 static void
791 radius_acct_start(void)
792 {
793     UINT4 av_type;
794     int result;
795     VALUE_PAIR *send = NULL;
796     ipcp_options *ho = &ipcp_hisoptions[0];
797     u_int32_t hisaddr;
798
799     if (!rstate.initialized) {
800         return;
801     }
802
803     rstate.start_time = time(NULL);
804
805     strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
806
807     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
808                    rstate.session_id, 0, VENDOR_NONE);
809     rc_avpair_add(&send, PW_USER_NAME,
810                    rstate.user, 0, VENDOR_NONE);
811
812     if (rstate.class_len > 0)
813         rc_avpair_add(&send, PW_CLASS,
814                       rstate.class, rstate.class_len, VENDOR_NONE);
815
816     av_type = PW_STATUS_START;
817     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
818
819     av_type = PW_FRAMED;
820     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
821
822     av_type = PW_PPP;
823     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
824
825     if (*remote_number) {
826         rc_avpair_add(&send, PW_CALLING_STATION_ID,
827                        remote_number, 0, VENDOR_NONE);
828     }
829
830     av_type = PW_RADIUS;
831     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
832
833
834     av_type = PW_ASYNC;
835     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
836
837     hisaddr = ho->hisaddr;
838     av_type = htonl(hisaddr);
839     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
840
841     /* Add user specified vp's */
842     if (rstate.avp)
843         rc_avpair_insert(&send, NULL, rstate.avp);
844
845     if (rstate.acctserver) {
846         result = rc_acct_using_server(rstate.acctserver,
847                                       rstate.client_port, send);
848     } else {
849         result = rc_acct(rstate.client_port, send);
850     }
851
852     rc_avpair_free(send);
853
854     if (result != OK_RC) {
855         /* RADIUS server could be down so make this a warning */
856         syslog(LOG_WARNING,
857                 "Accounting START failed for %s", rstate.user);
858     } else {
859         rstate.accounting_started = 1;
860         /* Kick off periodic accounting reports */
861         if (rstate.acct_interim_interval) {
862             TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
863         }
864     }
865 }
866
867 /**********************************************************************
868 * %FUNCTION: radius_acct_stop
869 * %ARGUMENTS:
870 *  None
871 * %RETURNS:
872 *  Nothing
873 * %DESCRIPTION:
874 *  Sends a "stop" accounting message to the RADIUS server.
875 ***********************************************************************/
876 static void
877 radius_acct_stop(void)
878 {
879     UINT4 av_type;
880     VALUE_PAIR *send = NULL;
881     ipcp_options *ho = &ipcp_hisoptions[0];
882     u_int32_t hisaddr;
883     int result;
884
885     if (!rstate.initialized) {
886         return;
887     }
888
889     if (!rstate.accounting_started) {
890         return;
891     }
892
893     rstate.accounting_started = 0;
894     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
895                    0, VENDOR_NONE);
896
897     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
898
899     av_type = PW_STATUS_STOP;
900     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
901
902     av_type = PW_FRAMED;
903     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
904
905     av_type = PW_PPP;
906     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
907
908     av_type = PW_RADIUS;
909     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
910
911
912     if (link_stats_valid) {
913         av_type = link_connect_time;
914         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
915
916         av_type = link_stats.bytes_out;
917         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
918
919         av_type = link_stats.bytes_in;
920         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
921
922         av_type = link_stats.pkts_out;
923         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
924
925         av_type = link_stats.pkts_in;
926         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
927     }
928
929     if (*remote_number) {
930         rc_avpair_add(&send, PW_CALLING_STATION_ID,
931                        remote_number, 0, VENDOR_NONE);
932     }
933
934     av_type = PW_ASYNC;
935     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
936
937     hisaddr = ho->hisaddr;
938     av_type = htonl(hisaddr);
939     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
940
941     /* Add user specified vp's */
942     if (rstate.avp)
943         rc_avpair_insert(&send, NULL, rstate.avp);
944
945     if (rstate.acctserver) {
946         result = rc_acct_using_server(rstate.acctserver,
947                                       rstate.client_port, send);
948     } else {
949         result = rc_acct(rstate.client_port, send);
950     }
951
952     if (result != OK_RC) {
953         /* RADIUS server could be down so make this a warning */
954         syslog(LOG_WARNING,
955                 "Accounting STOP failed for %s", rstate.user);
956     }
957     rc_avpair_free(send);
958 }
959
960 /**********************************************************************
961 * %FUNCTION: radius_acct_interim
962 * %ARGUMENTS:
963 *  None
964 * %RETURNS:
965 *  Nothing
966 * %DESCRIPTION:
967 *  Sends an interim accounting message to the RADIUS server
968 ***********************************************************************/
969 static void
970 radius_acct_interim(void *ignored)
971 {
972     UINT4 av_type;
973     VALUE_PAIR *send = NULL;
974     ipcp_options *ho = &ipcp_hisoptions[0];
975     u_int32_t hisaddr;
976     int result;
977
978     if (!rstate.initialized) {
979         return;
980     }
981
982     if (!rstate.accounting_started) {
983         return;
984     }
985
986     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
987                    0, VENDOR_NONE);
988
989     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
990
991     av_type = PW_STATUS_ALIVE;
992     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
993
994     av_type = PW_FRAMED;
995     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
996
997     av_type = PW_PPP;
998     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
999
1000     av_type = PW_RADIUS;
1001     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
1002
1003     /* Update link stats */
1004     update_link_stats(0);
1005
1006     if (link_stats_valid) {
1007         link_stats_valid = 0; /* Force later code to update */
1008
1009         av_type = link_connect_time;
1010         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
1011
1012         av_type = link_stats.bytes_out;
1013         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1014
1015         av_type = link_stats.bytes_in;
1016         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
1017
1018         av_type = link_stats.pkts_out;
1019         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1020
1021         av_type = link_stats.pkts_in;
1022         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
1023     }
1024
1025     if (*remote_number) {
1026         rc_avpair_add(&send, PW_CALLING_STATION_ID,
1027                        remote_number, 0, VENDOR_NONE);
1028     }
1029
1030     av_type = PW_ASYNC;
1031     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
1032
1033     hisaddr = ho->hisaddr;
1034     av_type = htonl(hisaddr);
1035     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
1036
1037     /* Add user specified vp's */
1038     if (rstate.avp)
1039         rc_avpair_insert(&send, NULL, rstate.avp);
1040
1041     if (rstate.acctserver) {
1042         result = rc_acct_using_server(rstate.acctserver,
1043                                       rstate.client_port, send);
1044     } else {
1045         result = rc_acct(rstate.client_port, send);
1046     }
1047
1048     if (result != OK_RC) {
1049         /* RADIUS server could be down so make this a warning */
1050         syslog(LOG_WARNING,
1051                 "Interim accounting failed for %s", rstate.user);
1052     }
1053     rc_avpair_free(send);
1054
1055     /* Schedule another one */
1056     TIMEOUT(radius_acct_interim, NULL, rstate.acct_interim_interval);
1057 }
1058
1059 /**********************************************************************
1060 * %FUNCTION: radius_ip_up
1061 * %ARGUMENTS:
1062 *  opaque -- ignored
1063 *  arg -- ignored
1064 * %RETURNS:
1065 *  Nothing
1066 * %DESCRIPTION:
1067 *  Called when IPCP is up.  We'll do a start-accounting record.
1068 ***********************************************************************/
1069 static void
1070 radius_ip_up(void *opaque, int arg)
1071 {
1072     radius_acct_start();
1073 }
1074
1075 /**********************************************************************
1076 * %FUNCTION: radius_ip_down
1077 * %ARGUMENTS:
1078 *  opaque -- ignored
1079 *  arg -- ignored
1080 * %RETURNS:
1081 *  Nothing
1082 * %DESCRIPTION:
1083 *  Called when IPCP is down.  We'll do a stop-accounting record.
1084 ***********************************************************************/
1085 static void
1086 radius_ip_down(void *opaque, int arg)
1087 {
1088     radius_acct_stop();
1089 }
1090
1091 /**********************************************************************
1092 * %FUNCTION: radius_init
1093 * %ARGUMENTS:
1094 *  msg -- buffer of size BUF_LEN for error message
1095 * %RETURNS:
1096 *  negative on failure; non-negative on success
1097 * %DESCRIPTION:
1098 *  Initializes radiusclient library
1099 ***********************************************************************/
1100 static int
1101 radius_init(char *msg)
1102 {
1103     if (rstate.initialized) {
1104         return 0;
1105     }
1106
1107     if (config_file && *config_file) {
1108         strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
1109     }
1110
1111     rstate.initialized = 1;
1112
1113     if (rc_read_config(rstate.config_file) != 0) {
1114         slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
1115                  rstate.config_file);
1116         return -1;
1117     }
1118
1119     if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
1120         slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
1121                  rc_conf_str("dictionary"));
1122         return -1;
1123     }
1124
1125     if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)   {
1126         slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
1127                  rc_conf_str("mapfile"));
1128         return -1;
1129     }
1130
1131     /* Add av pairs saved during option parsing */
1132     while (avpopt) {
1133         struct avpopt *n = avpopt->next;
1134
1135         rc_avpair_parse(avpopt->vpstr, &rstate.avp);
1136         free(avpopt->vpstr);
1137         free(avpopt);
1138         avpopt = n;
1139     }
1140     return 0;
1141 }
1142
1143 /**********************************************************************
1144 * %FUNCTION: get_client_port
1145 * %ARGUMENTS:
1146 *  ifname -- PPP interface name (e.g. "ppp7")
1147 * %RETURNS:
1148 *  The NAS port number (e.g. 7)
1149 * %DESCRIPTION:
1150 *  Extracts the port number from the interface name
1151 ***********************************************************************/
1152 static int
1153 get_client_port(char *ifname)
1154 {
1155     int port;
1156     if (sscanf(ifname, "ppp%d", &port) == 1) {
1157         return port;
1158     }
1159     return rc_map2id(ifname);
1160 }
1161
1162 /**********************************************************************
1163 * %FUNCTION: radius_allowed_address
1164 * %ARGUMENTS:
1165 *  addr -- IP address
1166 * %RETURNS:
1167 *  1 if we're allowed to use that IP address; 0 if not; -1 if we do
1168 *  not know.
1169 ***********************************************************************/
1170 static int
1171 radius_allowed_address(u_int32_t addr)
1172 {
1173     ipcp_options *wo = &ipcp_wantoptions[0];
1174
1175     if (!rstate.choose_ip) {
1176         /* If RADIUS server said any address is OK, then fine... */
1177         if (rstate.any_ip_addr_ok) {
1178             return 1;
1179         }
1180
1181         /* Sigh... if an address was supplied for remote host in pppd
1182            options, it has to match that.  */
1183         if (wo->hisaddr != 0 && wo->hisaddr == addr) {
1184             return 1;
1185         }
1186
1187         return 0;
1188     }
1189     if (addr == rstate.ip_addr) return 1;
1190     return 0;
1191 }
1192
1193 /* Useful for other plugins */
1194 char *radius_logged_in_user(void)
1195 {
1196     return rstate.user;
1197 }