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