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