blib/lib/VUser/Radius/SQL.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 232 CC 24 SCAR 44.0
CC
24
Cov
0%
do_sql
210.0
radius_moduser
30.0
radius_listattrib
20.0
CRAP 81.0
1 package VUser::Radius::SQL;
2 use warnings;
3 use strict;
4  
5 # Copyright 2006 Randy Smith <perlstalker@vuser.org>
6 # $Id: SQL.pm,v 1.9 2007/09/21 14:36:53 perlstalker Exp $
7  
8 use VUser::ExtLib qw(:config);
9 use VUser::Log qw(:levels);
10 use VUser::ResultSet;
11 # Should be use()d by vuser when extensions are loaded.
12 # Explicitly include here to be sure
13 use VUser::Radius;
14 use VUser::ExtLib::SQL;
15  
16 my $log;
17 my %meta;
18  
19 our $VERSION = '0.1.1';
20 our $c_sec = 'Extension Radius::SQL';
21  
22 my $extlib;
23 my $dsn;
24 my $username;
25 my $password;
26  
27 sub c_sec { return $c_sec; }
28  
29 sub depends { return qw(Radius); }
30  
31 sub init {
32 my $eh = shift;
33 my %cfg = @_;
34  
35 if ( defined $main::log ) {
36 $log = $main::log;
37 } else {
38 $log = VUser::Log->new( \%cfg, 'vuser' );
39 }
40  
41 $dsn = strip_ws( $cfg{$c_sec}{'dsn'} );
42 $username = strip_ws( $cfg{$c_sec}{'username'} );
43 $password = strip_ws( $cfg{$c_sec}{'password'} );
44  
45 $extlib = VUser::ExtLib::SQL->new(\%cfg,
46 {'dsn' => $dsn,
47 'user' => $username,
48 'password' => $password,
49 'macros' => { 'u' => 'username',
50 'p' => 'password',
51 'r' => 'realm',
52 'a' => 'attribute',
53 't' => 'type',
54 'v' => 'value'
55 }
56 });
57  
58 $eh->register_task( 'radius', 'adduser', \&do_sql );
59 $eh->register_task( 'radius', 'rmuser', \&do_sql );
60 $eh->register_task( 'radius', 'moduser', \&radius_moduser );
61 $eh->register_task( 'radius', 'listusers', \&radius_listusers );
62 $eh->register_task( 'radius', 'userinfo', \&radius_userinfo );
63  
64 $eh->register_task( 'radius', 'addattrib', \&do_sql );
65 $eh->register_task( 'radius', 'modattrib', \&do_sql );
66 $eh->register_task( 'radius', 'rmattrib', \&do_sql );
67 $eh->register_task( 'radius', 'listattrib', \&radius_listattrib );
68 }
69  
70 sub unload { }
71  
72 sub user_exists {
73 my ($cfg, $opts) = @_;
74  
75 my $sql = strip_ws($cfg->{$c_sec}{'userinfo_query'});
76 my $sth;
77 eval { $sth = $extlib->execute($opts, $sql); };
78 if ($@) {
79 $log->log(LOG_ERROR, "Unable to get user info: $@");
80 die "Unable to get user info: $@";
81 }
82  
83 my $found;
84 my @results;
85 if (@results = $sth->fetchrow_array()) {
86 $found = 1;
87 $log->log(LOG_DEBUG, "User exists");
88 } else {
89 $found = 0;
90 $log->log(LOG_DEBUG, "User does not exist");
91 }
92  
93 $sth->finish;
94 return $found;
95 }
96  
97 sub do_sql {
98 my ( $cfg, $opts, $action, $eh ) = @_;
99  
100 my $query;
101  
102 if ( $action eq 'adduser' ) {
103 die "User ".$opts->{username}." exists\n" if user_exists($cfg, $opts);
104 $query = 'adduser_query';
105 } elsif ( $action eq 'rmuser' ) {
106 $query = 'rmuser_query';
107 } elsif ( $action eq 'addattrib' ) {
108 if ( $opts->{'type'} == 'check' ) {
109 $query = 'addattrib_check_query';
110 } elsif ( $opts->{'type'} == 'reply' ) {
111 $query = 'addattrib_reply_query';
112 }
113 } elsif ( $action eq 'rmattrib' ) {
114 if ( $opts->{'type'} == 'check' ) {
115 $query = 'rmattrib_check_query';
116 } elsif ( $opts->{'type'} == 'reply' ) {
117 $query = 'rmattrib_reply_query';
118 }
119 } elsif ( $action eq 'modattrib' ) {
120 if ( $opts->{'type'} == 'check' ) {
121 $query = 'modattrib_check_query';
122 } elsif ( $opts->{'type'} == 'reply' ) {
123 $query = 'modattrib_reply_query';
124 }
125 }
126  
127 my $sql = strip_ws( $cfg->{$c_sec}{$query} );
128  
129 if (not $sql) {
130 $log->log(LOG_WARN, "No query specified for $query. Skipping.");
131 return;
132 }
133  
134 my $sth = $extlib->execute( $opts, $sql );
135 $sth->finish;
136 }
137  
138 sub radius_moduser {
139 my ( $cfg, $opts, $action, $eh ) = @_;
140  
141 ## change password
142 if ( $opts->{password} ) {
143 $extlib->execute($opts,
144 strip_ws( $cfg->{$c_sec}{'moduser_password_query'} ));
145 }
146 ## change realm only
147 if ( $opts->{newrealm} and not $opts->{newusername} ) {
148 $extlib->execute($opts,
149 strip_ws( $cfg->{$c_sec}{'moduser_realm_query'} ) );
150 }
151 ## change username only
152 if ( $opts->{newusername} and not $opts->{newrealm} ) {
153 $extlib->execute($opts,
154 strip_ws( $cfg->{$c_sec}{'moduser_username_query'} ));
155 }
156 ## change username and realm
157 if ( $opts->{newusername} and $opts->{newrealm} ) {
158 $extlib->execute($opts,
159 strip_ws( $cfg->{$c_sec}{'moduser_userrealm_query'} ));
160 }
161 }
162  
163 sub radius_listusers {
164 my ( $cfg, $opts, $action, $eh ) = @_;
165  
166 my $rs = VUser::ResultSet->new();
167 $rs->add_meta( $VUser::Radius::meta{'username'} );
168 $rs->add_meta( $VUser::Radius::meta{'realm'} );
169  
170 my $sth =
171 $extlib->execute( $opts, strip_ws( $cfg->{$c_sec}{'listusers_query'} ) );
172 my @results;
173 while ( @results = $sth->fetchrow_array ) {
174 $rs->add_data( [ @results[ 0 .. 1 ] ] );
175 }
176  
177 $sth->finish;
178 return $rs;
179 }
180  
181 sub radius_userinfo {
182 my ( $cfg, $opts, $action, $eh ) = @_;
183  
184 my $rs = VUser::ResultSet->new();
185 $rs->add_meta( $VUser::Radius::meta{'username'} );
186 $rs->add_meta( $VUser::Radius::meta{'realm'} );
187 $rs->add_meta( $VUser::Radius::meta{'password'} );
188  
189 my $sth =
190 $extlib->execute( $opts, strip_ws( $cfg->{$c_sec}{'userinfo_query'} ) );
191 my @results;
192 while ( @results = $sth->fetchrow_array ) {
193 $rs->add_data( [ @results[ 0 .. 2 ] ] );
194 }
195  
196 $sth->finish;
197 return $rs;
198 }
199  
200 sub radius_listattrib {
201 my ( $cfg, $opts, $action, $eh ) = @_;
202  
203 my $sql;
204 if ( $opts->{'type'} eq 'check' ) {
205 $sql = strip_ws( $cfg->{$c_sec}{'listattrib_check_query'} );
206 } elsif ( $opts->{'type'} eq 'reply' ) {
207 $sql = strip_ws( $cfg->{$c_sec}{'listattrib_reply_query'} );
208 }
209  
210 return unless $sql;
211  
212 ## Build resultset
213 my $rs = VUser::ResultSet->new();
214 $rs->add_meta( $VUser::Radius::meta{'username'} );
215 $rs->add_meta( $VUser::Radius::meta{'realm'} );
216 $rs->add_meta( $VUser::Radius::meta{'attribute'} );
217 $rs->add_meta( $VUser::Radius::meta{'type'} );
218 $rs->add_meta( $VUser::Radius::meta{'value'} );
219  
220 my $sth = $extlib->execute( $opts, $sql );
221  
222 my @results;
223 while ( @results = $sth->fetchrow_array ) {
224 $rs->add_data( [ @results[ 0 .. 2 ], $opts->{'type'}, $results[3] ] );
225 }
226  
227 $sth->finish;
228 return $rs;
229 }
230  
231 1;
232  
233 __END__