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