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