]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radattr.c
Use autoconf/automake to configure and make ppp
[ppp.git] / pppd / plugins / radius / radattr.c
1 /***********************************************************************
2 *
3 * radattr.c
4 *
5 * A plugin which is stacked on top of radius.so.  This plugin writes
6 * all RADIUS attributes from the server's authentication confirmation
7 * into /var/run/radattr.pppN.  These attributes are available for
8 * consumption by /etc/ppp/ip-{up,down} scripts.
9 *
10 * Copyright (C) 2002 Roaring Penguin Software Inc.
11 *
12 * This plugin may be distributed according to the terms of the GNU
13 * General Public License, version 2 or (at your option) any later version.
14 *
15 ***********************************************************************/
16
17 static char const RCSID[] =
18 "$Id: radattr.c,v 1.2 2004/10/28 00:24:40 paulus Exp $";
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "pppd.h"
25 #include "radiusclient.h"
26 #include <stdio.h>
27
28 extern void (*radius_attributes_hook)(VALUE_PAIR *);
29 static void print_attributes(VALUE_PAIR *);
30 static void cleanup(void *opaque, int arg);
31
32 char pppd_version[] = VERSION;
33
34 /**********************************************************************
35 * %FUNCTION: plugin_init
36 * %ARGUMENTS:
37 *  None
38 * %RETURNS:
39 *  Nothing
40 * %DESCRIPTION:
41 *  Initializes radattr plugin.
42 ***********************************************************************/
43 void
44 plugin_init(void)
45 {
46     radius_attributes_hook = print_attributes;
47
48 #if 0
49     /* calling cleanup() on link down is problematic because print_attributes()
50        is called only after PAP or CHAP authentication, but not when the link
51        should go up again for any other reason */
52     add_notifier(&link_down_notifier, cleanup, NULL);
53 #endif
54
55     /* Just in case... */
56     add_notifier(&exitnotify, cleanup, NULL);
57     info("RADATTR plugin initialized.");
58 }
59
60 /**********************************************************************
61 * %FUNCTION: print_attributes
62 * %ARGUMENTS:
63 *  vp -- linked-list of RADIUS attribute-value pairs
64 * %RETURNS:
65 *  Nothing
66 * %DESCRIPTION:
67 *  Prints the attribute pairs to /var/run/radattr.pppN.  Each line of the
68 *  file contains "name value" pairs.
69 ***********************************************************************/
70 static void
71 print_attributes(VALUE_PAIR *vp)
72 {
73     FILE *fp;
74     char fname[512];
75     char name[2048];
76     char value[2048];
77     int cnt = 0;
78
79     slprintf(fname, sizeof(fname), "/var/run/radattr.%s", ifname);
80     fp = fopen(fname, "w");
81     if (!fp) {
82         warn("radattr plugin: Could not open %s for writing: %m", fname);
83         return;
84     }
85
86     for (; vp; vp=vp->next) {
87         if (rc_avpair_tostr(vp, name, sizeof(name), value, sizeof(value)) < 0) {
88             continue;
89         }
90         fprintf(fp, "%s %s\n", name, value);
91         cnt++;
92     }
93     fclose(fp);
94     dbglog("RADATTR plugin wrote %d line(s) to file %s.", cnt, fname);
95 }
96
97 /**********************************************************************
98 * %FUNCTION: cleanup
99 * %ARGUMENTS:
100 *  opaque -- not used
101 *  arg -- not used
102 * %RETURNS:
103 *  Nothing
104 * %DESCRIPTION:
105 *  Deletes /var/run/radattr.pppN
106 ***********************************************************************/
107 static void
108 cleanup(void *opaque, int arg)
109 {
110     char fname[512];
111
112     slprintf(fname, sizeof(fname), "/var/run/radattr.%s", ifname);
113     (void) remove(fname);
114     dbglog("RADATTR plugin removed file %s.", fname);
115 }