]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radius.c
Updated config.h.in
[ppp.git] / pppd / plugins / radius / radius.c
1 /***********************************************************************
2 *
3 * radius.c
4 *
5 * RADIUS plugin for pppd.  Performs PAP and CHAP authentication using
6 * 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 * This plugin may be distributed according to the terms of the GNU
20 * General Public License, version 2 or (at your option) any later version.
21 *
22 ***********************************************************************/
23 static char const RCSID[] =
24 "$Id: radius.c,v 1.1 2002/01/22 16:03:00 dfs Exp $";
25
26 #include "pppd.h"
27 #include "chap.h"
28 #include "radiusclient.h"
29 #include "fsm.h"
30 #include "ipcp.h"
31 #include <syslog.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34
35 #define BUF_LEN 1024
36
37 static char *config_file = NULL;
38
39 static option_t Options[] = {
40     { "radius-config-file", o_string, &config_file },
41     { NULL }
42 };
43
44 static int radius_secret_check(void);
45 static int radius_pap_auth(char *user,
46                            char *passwd,
47                            char **msgp,
48                            struct wordlist **paddrs,
49                            struct wordlist **popts);
50 static int radius_chap_auth(char *user,
51                             u_char *remmd,
52                             int remmd_len,
53                             chap_state *cstate);
54
55 static void radius_ip_up(void *opaque, int arg);
56 static void radius_ip_down(void *opaque, int arg);
57 static void make_username_realm(char *user);
58 static int radius_setparams(VALUE_PAIR *vp, char *msg);
59 static void radius_choose_ip(u_int32_t *addrp);
60 static int radius_init(char *msg);
61 static int get_client_port(char *ifname);
62 static int radius_allowed_address(u_int32_t addr);
63
64 void (*radius_attributes_hook)(VALUE_PAIR *) = NULL;
65
66 #ifndef MAXSESSIONID
67 #define MAXSESSIONID 32
68 #endif
69
70 struct radius_state {
71     int accounting_started;
72     int initialized;
73     int client_port;
74     int choose_ip;
75     int any_ip_addr_ok;
76     int done_chap_once;
77     u_int32_t ip_addr;
78     char user[MAXNAMELEN];
79     char config_file[MAXPATHLEN];
80     char session_id[MAXSESSIONID + 1];
81     time_t start_time;
82 };
83
84 static struct radius_state rstate;
85
86 char pppd_version[] = VERSION;
87
88 /**********************************************************************
89 * %FUNCTION: plugin_init
90 * %ARGUMENTS:
91 *  None
92 * %RETURNS:
93 *  Nothing
94 * %DESCRIPTION:
95 *  Initializes RADIUS plugin.
96 ***********************************************************************/
97 void
98 plugin_init(void)
99 {
100     pap_check_hook = radius_secret_check;
101     pap_auth_hook = radius_pap_auth;
102
103     chap_check_hook = radius_secret_check;
104     chap_auth_hook = radius_chap_auth;
105
106     ip_choose_hook = radius_choose_ip;
107     allowed_address_hook = radius_allowed_address;
108
109     add_notifier(&ip_up_notifier, radius_ip_up, NULL);
110     add_notifier(&ip_down_notifier, radius_ip_down, NULL);
111
112     memset(&rstate, 0, sizeof(rstate));
113
114     strlcpy(rstate.config_file, "/etc/radiusclient/radiusclient.conf",
115             sizeof(rstate.config_file));
116
117     add_options(Options);
118
119     info("RADIUS plugin initialized.");
120 }
121
122 /**********************************************************************
123 * %FUNCTION: radius_secret_check
124 * %ARGUMENTS:
125 *  None
126 * %RETURNS:
127 *  1 -- we are ALWAYS willing to supply a secret. :-)
128 * %DESCRIPTION:
129 * Tells pppd that we will try to authenticate the peer, and not to
130 * worry about looking in /etc/ppp/*-secrets
131 ***********************************************************************/
132 static int
133 radius_secret_check(void)
134 {
135     return 1;
136 }
137
138 /**********************************************************************
139 * %FUNCTION: radius_choose_ip
140 * %ARGUMENTS:
141 *  addrp -- where to store the IP address
142 * %RETURNS:
143 *  Nothing
144 * %DESCRIPTION:
145 *  If RADIUS server has specified an IP address, it is stored in *addrp.
146 ***********************************************************************/
147 static void
148 radius_choose_ip(u_int32_t *addrp)
149 {
150     if (rstate.choose_ip) {
151         *addrp = rstate.ip_addr;
152     }
153 }
154
155 /**********************************************************************
156 * %FUNCTION: radius_pap_auth
157 * %ARGUMENTS:
158 *  user -- user-name of peer
159 *  passwd -- password supplied by peer
160 *  msgp -- Message which will be sent in PAP response
161 *  paddrs -- set to a list of possible peer IP addresses
162 *  popts -- set to a list of additional pppd options
163 * %RETURNS:
164 *  1 if we can authenticate, -1 if we cannot.
165 * %DESCRIPTION:
166 * Performs PAP authentication using RADIUS
167 ***********************************************************************/
168 static int
169 radius_pap_auth(char *user,
170                 char *passwd,
171                 char **msgp,
172                 struct wordlist **paddrs,
173                 struct wordlist **popts)
174 {
175     VALUE_PAIR *send, *received;
176     UINT4 av_type;
177     int result;
178     static char radius_msg[BUF_LEN];
179
180     radius_msg[0] = 0;
181     *msgp = radius_msg;
182
183     if (radius_init(radius_msg) < 0) {
184         return 0;
185     }
186
187     send = NULL;
188     received = NULL;
189
190     /* Hack... the "port" is the ppp interface number.  Should really be
191        the tty */
192     rstate.client_port = get_client_port(ifname);
193
194     av_type = PW_FRAMED;
195     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
196
197     av_type = PW_PPP;
198     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
199
200     /* Put user with potentially realm added in rstate.user */
201     make_username_realm(user);
202
203     rc_avpair_add(&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
204     rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE);
205     if (*remote_number) {
206         rc_avpair_add(&send, PW_CALLING_STATION_ID, remote_number, 0,
207                        VENDOR_NONE);
208     }
209
210     result = rc_auth(rstate.client_port, send, &received, radius_msg);
211
212     if (result == OK_RC) {
213         if (radius_setparams(received, radius_msg) < 0) {
214             result = ERROR_RC;
215         }
216     }
217
218     /* free value pairs */
219     rc_avpair_free(received);
220     rc_avpair_free(send);
221
222     return (result == OK_RC) ? 1 : 0;
223 }
224
225 /**********************************************************************
226 * %FUNCTION: radius_chap_auth
227 * %ARGUMENTS:
228 *  user -- user-name of peer
229 *  remmd -- hash received from peer
230 *  remmd_len -- length of remmd
231 *  cstate -- pppd's chap_state structure
232 * %RETURNS:
233 *  CHAP_SUCCESS if we can authenticate, CHAP_FAILURE if we cannot.
234 * %DESCRIPTION:
235 * Performs CHAP authentication using RADIUS
236 ***********************************************************************/
237 static int
238 radius_chap_auth(char *user,
239                  u_char *remmd,
240                  int remmd_len,
241                  chap_state *cstate)
242 {
243     VALUE_PAIR *send, *received;
244     UINT4 av_type;
245     static char radius_msg[BUF_LEN];
246     int result;
247     u_char cpassword[MD5_SIGNATURE_SIZE+1];
248     radius_msg[0] = 0;
249
250     if (radius_init(radius_msg) < 0) {
251         error("%s", radius_msg);
252         return CHAP_FAILURE;
253     }
254
255     /* we handle md5 digest at the moment */
256     if (cstate->chal_type != CHAP_DIGEST_MD5) {
257         error("RADIUS: Challenge type not MD5");
258         return CHAP_FAILURE;
259     }
260
261     /* Put user with potentially realm added in rstate.user */
262     if (!rstate.done_chap_once) {
263         make_username_realm(user);
264         rstate.client_port = get_client_port (ifname);
265     }
266
267     send = received = NULL;
268
269     av_type = PW_FRAMED;
270     rc_avpair_add (&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
271
272     av_type = PW_PPP;
273     rc_avpair_add (&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
274
275     rc_avpair_add (&send, PW_USER_NAME, rstate.user , 0, VENDOR_NONE);
276
277     /*
278      * add the CHAP-Password and CHAP-Challenge fields
279      */
280
281     cpassword[0] = cstate->chal_id;
282
283     memcpy(&cpassword[1], remmd, MD5_SIGNATURE_SIZE);
284
285     rc_avpair_add(&send, PW_CHAP_PASSWORD, cpassword, MD5_SIGNATURE_SIZE + 1, VENDOR_NONE);
286
287     rc_avpair_add(&send, PW_CHAP_CHALLENGE, cstate->challenge, cstate->chal_len, VENDOR_NONE);
288
289     /*
290      * make authentication with RADIUS server
291      */
292
293     result = rc_auth (rstate.client_port, send, &received, radius_msg);
294
295     if (result == OK_RC) {
296         if (!rstate.done_chap_once) {
297             if (radius_setparams(received, radius_msg) < 0) {
298                 error("%s", radius_msg);
299                 result = ERROR_RC;
300             } else {
301                 rstate.done_chap_once = 1;
302             }
303         }
304     }
305
306     rc_avpair_free(received);
307     rc_avpair_free (send);
308     return (result == OK_RC) ? CHAP_SUCCESS : CHAP_FAILURE;
309 }
310
311 /**********************************************************************
312 * %FUNCTION: make_username_realm
313 * %ARGUMENTS:
314 *  user -- the user given to pppd
315 * %RETURNS:
316 *  Nothing
317 * %DESCRIPTION:
318 *  Copies user into rstate.user.  If it lacks a realm (no "@domain" part),
319 * then the default realm from the radiusclient config file is added.
320 ***********************************************************************/
321 static void
322 make_username_realm(char *user)
323 {
324     char *default_realm;
325
326     if ( user != NULL ) {
327         strlcpy(rstate.user, user, sizeof(rstate.user));
328     }  else {
329         rstate.user[0] = 0;
330     }
331
332     default_realm = rc_conf_str("default_realm");
333
334     if (!strchr(rstate.user, '@') &&
335         default_realm &&
336         (*default_realm != '\0')) {
337         strlcat(rstate.user, "@", sizeof(rstate.user));
338         strlcat(rstate.user, default_realm, sizeof(rstate.user));
339     }
340 }
341
342 /**********************************************************************
343 * %FUNCTION: radius_setparams
344 * %ARGUMENTS:
345 *  vp -- received value-pairs
346 *  msg -- buffer in which to place error message.  Holds up to BUF_LEN chars
347 * %RETURNS:
348 *  >= 0 on success; -1 on failure
349 * %DESCRIPTION:
350 *  Parses attributes sent by RADIUS server and sets them in pppd.  Currently,
351 *  used only to set IP address.
352 ***********************************************************************/
353 static int
354 radius_setparams(VALUE_PAIR *vp, char *msg)
355 {
356     u_int32_t remote;
357
358     /* Send RADIUS attributes to anyone else who might be interested */
359     if (radius_attributes_hook) {
360         (*radius_attributes_hook)(vp);
361     }
362
363     /*
364      * service type (if not framed then quit),
365      * new IP address (RADIUS can define static IP for some users),
366      */
367
368     while (vp) {
369         switch (vp->attribute) {
370         case PW_SERVICE_TYPE:
371             /* check for service type       */
372             /* if not FRAMED then exit      */
373             if (vp->lvalue != PW_FRAMED) {
374                 slprintf(msg, BUF_LEN, "RADIUS: wrong service type %ld for %s",
375                          vp->lvalue, rstate.user);
376                 return -1;
377             }
378             break;
379         case PW_FRAMED_PROTOCOL:
380             /* check for framed protocol type       */
381             /* if not PPP then also exit            */
382             if (vp->lvalue != PW_PPP) {
383                 slprintf(msg, BUF_LEN, "RADIUS: wrong framed protocol %ld for %s",
384                          vp->lvalue, rstate.user);
385                 return -1;
386             }
387             break;
388
389         case PW_FRAMED_IP_ADDRESS:
390             /* seting up remote IP addresses */
391             remote = vp->lvalue;
392             if (remote == 0xffffffff) {
393                 /* 0xffffffff means user should be allowed to select one */
394                 rstate.any_ip_addr_ok = 1;
395             } else if (remote != 0xfffffffe) {
396                 /* 0xfffffffe means NAS should select an ip address */
397                 remote = htonl(vp->lvalue);
398                 if (bad_ip_adrs (remote)) {
399                     slprintf(msg, BUF_LEN, "RADIUS: bad remote IP address %I for %s",
400                              remote, rstate.user);
401                     return -1;
402                 }
403                 rstate.choose_ip = 1;
404                 rstate.ip_addr = remote;
405             }
406             break;
407         }
408         vp = vp->next;
409     }
410     return 0;
411 }
412
413 /**********************************************************************
414 * %FUNCTION: radius_acct_start
415 * %ARGUMENTS:
416 *  None
417 * %RETURNS:
418 *  Nothing
419 * %DESCRIPTION:
420 *  Sends a "start" accounting message to the RADIUS server.
421 ***********************************************************************/
422 static void
423 radius_acct_start(void)
424 {
425     UINT4 av_type;
426     int result;
427     VALUE_PAIR *send = NULL;
428     ipcp_options *ho = &ipcp_hisoptions[0];
429     u_int32_t hisaddr;
430
431     if (!rstate.initialized) {
432         return;
433     }
434
435     rstate.start_time = time(NULL);
436
437     strncpy(rstate.session_id, rc_mksid(), sizeof(rstate.session_id));
438
439     rc_avpair_add(&send, PW_ACCT_SESSION_ID,
440                    rstate.session_id, 0, VENDOR_NONE);
441     rc_avpair_add(&send, PW_USER_NAME,
442                    rstate.user, 0, VENDOR_NONE);
443
444     av_type = PW_STATUS_START;
445     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
446
447     av_type = PW_FRAMED;
448     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
449
450     av_type = PW_PPP;
451     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
452
453     if (*remote_number) {
454         rc_avpair_add(&send, PW_CALLING_STATION_ID,
455                        remote_number, 0, VENDOR_NONE);
456     }
457
458     av_type = PW_RADIUS;
459     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
460
461
462     av_type = PW_ASYNC;
463     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
464
465     hisaddr = ho->hisaddr;
466     av_type = htonl(hisaddr);
467     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
468
469     result = rc_acct(rstate.client_port, send);
470
471     rc_avpair_free(send);
472
473     if (result != OK_RC) {
474         /* RADIUS server could be down so make this a warning */
475         syslog(LOG_WARNING,
476                 "Accounting START failed for %s", rstate.user);
477     } else {
478         rstate.accounting_started = 1;
479     }
480 }
481
482 /**********************************************************************
483 * %FUNCTION: radius_acct_stop
484 * %ARGUMENTS:
485 *  None
486 * %RETURNS:
487 *  Nothing
488 * %DESCRIPTION:
489 *  Sends a "stop" accounting message to the RADIUS server.
490 ***********************************************************************/
491 static void
492 radius_acct_stop(void)
493 {
494     UINT4 av_type;
495     VALUE_PAIR *send = NULL;
496     ipcp_options *ho = &ipcp_hisoptions[0];
497     u_int32_t hisaddr;
498     int result;
499
500     if (!rstate.initialized) {
501         return;
502     }
503
504     if (!rstate.accounting_started) {
505         return;
506     }
507
508     rstate.accounting_started = 0;
509     rc_avpair_add(&send, PW_ACCT_SESSION_ID, rstate.session_id,
510                    0, VENDOR_NONE);
511
512     rc_avpair_add(&send, PW_USER_NAME, rstate.user, 0, VENDOR_NONE);
513
514     av_type = PW_STATUS_STOP;
515     rc_avpair_add(&send, PW_ACCT_STATUS_TYPE, &av_type, 0, VENDOR_NONE);
516
517     av_type = PW_FRAMED;
518     rc_avpair_add(&send, PW_SERVICE_TYPE, &av_type, 0, VENDOR_NONE);
519
520     av_type = PW_PPP;
521     rc_avpair_add(&send, PW_FRAMED_PROTOCOL, &av_type, 0, VENDOR_NONE);
522
523     av_type = PW_RADIUS;
524     rc_avpair_add(&send, PW_ACCT_AUTHENTIC, &av_type, 0, VENDOR_NONE);
525
526
527     if (link_stats_valid) {
528         av_type = link_connect_time;
529         rc_avpair_add(&send, PW_ACCT_SESSION_TIME, &av_type, 0, VENDOR_NONE);
530
531         av_type = link_stats.bytes_out;
532         rc_avpair_add(&send, PW_ACCT_OUTPUT_OCTETS, &av_type, 0, VENDOR_NONE);
533
534         av_type = link_stats.bytes_in;
535         rc_avpair_add(&send, PW_ACCT_INPUT_OCTETS, &av_type, 0, VENDOR_NONE);
536
537         av_type = link_stats.pkts_out;
538         rc_avpair_add(&send, PW_ACCT_OUTPUT_PACKETS, &av_type, 0, VENDOR_NONE);
539
540         av_type = link_stats.pkts_in;
541         rc_avpair_add(&send, PW_ACCT_INPUT_PACKETS, &av_type, 0, VENDOR_NONE);
542     }
543
544     if (*remote_number) {
545         rc_avpair_add(&send, PW_CALLING_STATION_ID,
546                        remote_number, 0, VENDOR_NONE);
547     }
548
549     av_type = PW_ASYNC;
550     rc_avpair_add(&send, PW_NAS_PORT_TYPE, &av_type, 0, VENDOR_NONE);
551
552     hisaddr = ho->hisaddr;
553     av_type = htonl(hisaddr);
554     rc_avpair_add(&send, PW_FRAMED_IP_ADDRESS , &av_type , 0, VENDOR_NONE);
555
556     result = rc_acct(rstate.client_port, send);
557     if (result != OK_RC) {
558         /* RADIUS server could be down so make this a warning */
559         syslog(LOG_WARNING,
560                 "Accounting STOP failed for %s", rstate.user);
561     }
562     rc_avpair_free(send);
563 }
564
565 /**********************************************************************
566 * %FUNCTION: radius_ip_up
567 * %ARGUMENTS:
568 *  opaque -- ignored
569 *  arg -- ignored
570 * %RETURNS:
571 *  Nothing
572 * %DESCRIPTION:
573 *  Called when IPCP is up.  We'll do a start-accounting record.
574 ***********************************************************************/
575 static void
576 radius_ip_up(void *opaque, int arg)
577 {
578     radius_acct_start();
579 }
580
581 /**********************************************************************
582 * %FUNCTION: radius_ip_down
583 * %ARGUMENTS:
584 *  opaque -- ignored
585 *  arg -- ignored
586 * %RETURNS:
587 *  Nothing
588 * %DESCRIPTION:
589 *  Called when IPCP is down.  We'll do a stop-accounting record.
590 ***********************************************************************/
591 static void
592 radius_ip_down(void *opaque, int arg)
593 {
594     radius_acct_stop();
595 }
596
597 /**********************************************************************
598 * %FUNCTION: radius_init
599 * %ARGUMENTS:
600 *  msg -- buffer of size BUF_LEN for error message
601 * %RETURNS:
602 *  negative on failure; non-negative on success
603 * %DESCRIPTION:
604 *  Initializes radiusclient library
605 ***********************************************************************/
606 static int
607 radius_init(char *msg)
608 {
609     if (rstate.initialized) {
610         return 0;
611     }
612
613     if (config_file && *config_file) {
614         strlcpy(rstate.config_file, config_file, MAXPATHLEN-1);
615     }
616
617     rstate.initialized = 1;
618
619     if (rc_read_config(rstate.config_file) != 0) {
620         slprintf(msg, BUF_LEN, "RADIUS: Can't read config file %s",
621                  rstate.config_file);
622         return -1;
623     }
624
625     if (rc_read_dictionary(rc_conf_str("dictionary")) != 0) {
626         slprintf(msg, BUF_LEN, "RADIUS: Can't read dictionary file %s",
627                  rc_conf_str("dictionary"));
628         return -1;
629     }
630
631     if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)   {
632         slprintf(msg, BUF_LEN, "RADIUS: Can't read map file %s",
633                  rc_conf_str("mapfile"));
634         return -1;
635     }
636     return 0;
637 }
638
639 /**********************************************************************
640 * %FUNCTION: get_client_port
641 * %ARGUMENTS:
642 *  ifname -- PPP interface name (e.g. "ppp7")
643 * %RETURNS:
644 *  The NAS port number (e.g. 7)
645 * %DESCRIPTION:
646 *  Extracts the port number from the interface name
647 ***********************************************************************/
648 static int
649 get_client_port(char *ifname)
650 {
651     int port;
652     if (sscanf(ifname, "ppp%d", &port) == 1) {
653         return port;
654     }
655     return rc_map2id(ifname);
656 }
657
658 /**********************************************************************
659 * %FUNCTION: radius_allowed_address
660 * %ARGUMENTS:
661 *  addr -- IP address
662 * %RETURNS:
663 *  1 if we're allowed to use that IP address; 0 if not; -1 if we do
664 *  not know.
665 ***********************************************************************/
666 static int
667 radius_allowed_address(u_int32_t addr)
668 {
669     ipcp_options *wo = &ipcp_wantoptions[0];
670
671     if (!rstate.choose_ip) {
672         /* If RADIUS server said any address is OK, then fine... */
673         if (rstate.any_ip_addr_ok) {
674             return 1;
675         }
676
677         /* Sigh... if an address was supplied for remote host in pppd
678            options, it has to match that.  */
679         if (wo->hisaddr != 0 && wo->hisaddr == addr) {
680             return 1;
681         }
682
683         return 0;
684     }
685     if (addr == rstate.ip_addr) return 1;
686     return 0;
687 }
688
689 /* Useful for other plugins */
690 char *radius_logged_in_user(void)
691 {
692     return rstate.user;
693 }
694