]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radattr.c
Makefile.am: Add explicit openssl directory to pppd include path
[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 #include <stdio.h>
21 #include <sys/stat.h>
22 #include <pppd/pppd.h>
23
24 #include "radiusclient.h"
25
26 extern void (*radius_attributes_hook)(VALUE_PAIR *);
27 static void print_attributes(VALUE_PAIR *);
28 static void cleanup(void *opaque, int arg);
29
30 char pppd_version[] = PPPD_VERSION;
31
32 /**********************************************************************
33 * %FUNCTION: plugin_init
34 * %ARGUMENTS:
35 *  None
36 * %RETURNS:
37 *  Nothing
38 * %DESCRIPTION:
39 *  Initializes radattr plugin.
40 ***********************************************************************/
41 void
42 plugin_init(void)
43 {
44     radius_attributes_hook = print_attributes;
45
46 #if 0
47     /* calling cleanup() on link down is problematic because print_attributes()
48        is called only after PAP or CHAP authentication, but not when the link
49        should go up again for any other reason */
50     add_notifier(&link_down_notifier, cleanup, NULL);
51 #endif
52
53     /* Just in case... */
54     add_notifier(&exitnotify, cleanup, NULL);
55     info("RADATTR plugin initialized.");
56 }
57
58 /**********************************************************************
59 * %FUNCTION: print_attributes
60 * %ARGUMENTS:
61 *  vp -- linked-list of RADIUS attribute-value pairs
62 * %RETURNS:
63 *  Nothing
64 * %DESCRIPTION:
65 *  Prints the attribute pairs to /var/run/radattr.pppN.  Each line of the
66 *  file contains "name value" pairs.
67 ***********************************************************************/
68 static void
69 print_attributes(VALUE_PAIR *vp)
70 {
71     FILE *fp;
72     char fname[512];
73     char name[2048];
74     char value[2048];
75     int cnt = 0;
76     mode_t old_umask;
77
78     slprintf(fname, sizeof(fname), "/var/run/radattr.%s", ifname);
79     old_umask = umask(077);
80     fp = fopen(fname, "w");
81     umask(old_umask);
82     if (!fp) {
83         warn("radattr plugin: Could not open %s for writing: %m", fname);
84         return;
85     }
86
87     for (; vp; vp=vp->next) {
88         if (rc_avpair_tostr(vp, name, sizeof(name), value, sizeof(value)) < 0) {
89             continue;
90         }
91         fprintf(fp, "%s %s\n", name, value);
92         cnt++;
93     }
94     fclose(fp);
95     dbglog("RADATTR plugin wrote %d line(s) to file %s.", cnt, fname);
96 }
97
98 /**********************************************************************
99 * %FUNCTION: cleanup
100 * %ARGUMENTS:
101 *  opaque -- not used
102 *  arg -- not used
103 * %RETURNS:
104 *  Nothing
105 * %DESCRIPTION:
106 *  Deletes /var/run/radattr.pppN
107 ***********************************************************************/
108 static void
109 cleanup(void *opaque, int arg)
110 {
111     char fname[512];
112
113     slprintf(fname, sizeof(fname), "/var/run/radattr.%s", ifname);
114     (void) remove(fname);
115     dbglog("RADATTR plugin removed file %s.", fname);
116 }