| 1 | package VUser::ACL; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # Copyright 2004 Randy Smith | ||
| 6 | # $Id: ACL.pm,v 1.9 2006-01-04 21:57:48 perlstalker Exp $ | ||
| 7 | |||
| 8 | use Exporter; | ||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | our @EXPORT_OK = qw(ALLOW DENY UNKNOWN); | ||
| 11 | our %EXPORT_TAGS = (consts => [qw(ALLOW DENY UNKNOWN)]); | ||
| 12 | |||
| 13 | our $REVISION = (split (' ', '$Revision: 1.9 $'))[1]; | ||
| 14 | our $VERSION = "0.3.0"; | ||
| 15 | |||
| 16 | our $ALLOW = 1; | ||
| 17 | our $DENY = 0; | ||
| 18 | our $UNKNOWN = -1; | ||
| 19 | |||
| 20 | my $c_sec = 'ACL'; #conf section | ||
| 21 | |||
| 22 | my $acl = undef; | ||
| 23 | |||
| 24 | sub revision | ||
| 25 | { | ||
| 26 | my $self = shift; | ||
| 27 | my $type = ref($self) || $self; | ||
| 28 | no strict 'refs'; | ||
| 29 | return $REVISION; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub version | ||
| 33 | { | ||
| 34 | my $self = shift; | ||
| 35 | my $type = ref($self) || $self; | ||
| 36 | no strict 'refs'; | ||
| 37 | return ${$type."::VERSION"}; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub init | ||
| 41 | { | ||
| 42 | my $eh = shift; | ||
| 43 | my %cfg = @_; | ||
| 44 | |||
| 45 | if (VUser::ExtLib::check_bool($cfg{$c_sec}{'use internal auth'})) { | ||
| 46 | if (not VUser::ExtLib::strip_ws($cfg{$c_sec}{'auth modules'})) { | ||
| 47 | die "Internal auth specified but no auth modules defined."; | ||
| 48 | } | ||
| 49 | $eh->register_keyword('auth', 'Manage vuser users'); | ||
| 50 | |||
| 51 | # auth-add | ||
| 52 | $eh->register_action('auth', 'add', 'Add a new user'); | ||
| 53 | $eh->register_option('auth', 'add', 'user', '=s', 1, 'User name'); | ||
| 54 | $eh->register_option('auth', 'add', 'password', '=s', 1, 'Password'); | ||
| 55 | $eh->register_option('auth', 'add', 'ip', '=s', 0, 'Restrict to this IP'); | ||
| 56 | $eh->register_task('auth', 'add', \&auth_add, 1); | ||
| 57 | |||
| 58 | # auth-show | ||
| 59 | $eh->register_action('auth', 'show', 'Show users'); | ||
| 60 | $eh->register_option('auth', 'show', 'user', '=s', 0, 'User to get'); | ||
| 61 | $eh->register_option('auth', 'show', 'module', '=s', 0, 'Check in this module only.'); | ||
| 62 | $eh->register_task('auth', 'show', \&auth_show); | ||
| 63 | |||
| 64 | # auth-del | ||
| 65 | $eh->register_action('auth', 'del', 'Delete user'); | ||
| 66 | $eh->register_option('auth', 'del', 'user', '=s', 1, 'User to get'); | ||
| 67 | $eh->register_option('auth', 'del', 'module', '=s', 0, 'Check in this module only.'); | ||
| 68 | $eh->register_task('auth', 'del', \&auth_del); | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | $eh->register_keyword('acl', 'Manage vuser ACLs'); | ||
| 73 | |||
| 74 | # acl-add | ||
| 75 | $eh->register_action('acl', 'add', 'Add new ACL'); | ||
| 76 | $eh->register_option('acl', 'add', 'module', '=s', 0, 'Module to add to. Add to ALL modules if not specified.'); | ||
| 77 | $eh->register_option('acl', 'add', 'user', '=s', 1, 'User for ACL'); | ||
| 78 | $eh->register_option('acl', 'add', 'keyword', '=s', 1, 'Keyword'); | ||
| 79 | $eh->register_option('acl', 'add', 'action', '=s', 0); | ||
| 80 | $eh->register_option('acl', 'add', 'option', '=s', 0); | ||
| 81 | |||
| 82 | $eh->register_option('acl', 'add', 'operation', '=s', 0); | ||
| 83 | $eh->register_option('acl', 'add', 'value', '=s', 0); | ||
| 84 | $eh->register_option('acl', 'add', 'permissions', '=s', 1, 'ALLOW or DENY'); | ||
| 85 | $eh->register_task('acl', 'add', \&acl_add, 1); | ||
| 86 | |||
| 87 | $acl = new VUser::ACL (\%cfg); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub unload { return; } | ||
| 91 | |||
| 92 | sub new | ||
| 93 | { | ||
| 94 | my $class = shift; | ||
| 95 | my $cfg = shift; | ||
| 96 | |||
| 97 | return $acl if defined $acl; | ||
| 98 | |||
| 99 | my $self = { auth => [], | ||
| 100 | acl => [] | ||
| 101 | }; | ||
| 102 | bless $self, $class; | ||
| 103 | |||
| 104 | # load Auth modules | ||
| 105 | eval { $self->load_auth_modules($cfg); }; | ||
| 106 | die $@ if $@; | ||
| 107 | |||
| 108 | # load ACL modules | ||
| 109 | eval { $self->load_acl_modules($cfg); }; | ||
| 110 | die $@ if $@; | ||
| 111 | |||
| 112 | # After we've loaded the requested modules, we'll set the default | ||
| 113 | # to allow or deny. (deny is the default) | ||
| 114 | my $acl_default = VUser::ExtLib::strip_ws($cfg->{$c_sec}{'acl default'}); | ||
| 115 | $acl_default = 'DENY' unless $acl_default; | ||
| 116 | |||
| 117 | if (uc($acl_default) eq 'ALLOW') { | ||
| 118 | $self->register_acl($cfg, \&ALLOW, -1); | ||
| 119 | } else { | ||
| 120 | $self->register_acl($cfg, \&DENY, -1); | ||
| 121 | } | ||
| 122 | $acl = $self; | ||
| 123 | |||
| 124 | return $acl; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub ALLOW { return $ALLOW; }; | ||
| 128 | sub DENY { return $DENY; } | ||
| 129 | sub UNKNOWN { return $UNKNOWN; } | ||
| 130 | |||
| 131 | sub register_acl | ||
| 132 | { | ||
| 133 | my $self = shift; | ||
| 134 | my $cfg = shift; | ||
| 135 | my $acl_sub = shift; | ||
| 136 | my $priority = shift || 10; | ||
| 137 | |||
| 138 | my $pri = $priority; | ||
| 139 | if ($priority < 0) { | ||
| 140 | $pri = scalar @{$self->{acl}}; | ||
| 141 | } | ||
| 142 | |||
| 143 | if (not defined $self->{acl}[$pri]) { | ||
| 144 | $self->{acl}[$pri] = []; | ||
| 145 | } | ||
| 146 | |||
| 147 | push @{ $self->{acl}[$pri] }, $acl_sub; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub register_auth | ||
| 151 | { | ||
| 152 | my $self = shift; | ||
| 153 | my $auth_sub = shift; | ||
| 154 | my $priority = shift || 10; | ||
| 155 | |||
| 156 | if (not defined $self->{auth}[$priority]) { | ||
| 157 | $self->{auth}[$priority] = []; | ||
| 158 | } | ||
| 159 | |||
| 160 | push @{$self->{auth}[$priority]}, $auth_sub; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub load_auth_modules | ||
| 164 | { | ||
| 165 | my $self = shift; | ||
| 166 | my $cfg = shift; | ||
| 167 | |||
| 168 | # whitespace delimited | ||
| 169 | my $mods = VUser::ExtLib::strip_ws($cfg->{$c_sec}{'auth modules'}); | ||
| 170 | $mods = '' unless $mods; | ||
| 171 | |||
| 172 | foreach my $mod (split / /, $mods) { | ||
| 173 | print STDERR "Loading authmod $mod\n" if $main::DEBUG >= 1; | ||
| 174 | eval { $self->load_auth_module( "VUser::Auth::$mod", $cfg); }; | ||
| 175 | warn "Unable to load auth module ($mod): $@\n" if $@; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | sub load_auth_module | ||
| 180 | { | ||
| 181 | my $self = shift; | ||
| 182 | my $mod = shift; | ||
| 183 | my $cfg = shift; | ||
| 184 | |||
| 185 | eval ( "require $mod" ); | ||
| 186 | die $@ if $@; | ||
| 187 | |||
| 188 | no strict 'refs'; | ||
| 189 | &{ $mod.'::init'}($self, $cfg); | ||
| 190 | } | ||
| 191 | |||
| 192 | sub load_acl_modules | ||
| 193 | { | ||
| 194 | my $self = shift; | ||
| 195 | my $cfg = shift; | ||
| 196 | |||
| 197 | # whitespace delimited | ||
| 198 | my $mods = VUser::ExtLib::strip_ws($cfg->{$c_sec}{'acl modules'}); | ||
| 199 | $mods = '' unless $mods; | ||
| 200 | |||
| 201 | foreach my $mod (split / /, $mods) { | ||
| 202 | print STDERR "Loading acl mod $mod\n" if $main::DEBUG >= 1; | ||
| 203 | eval { $self->load_auth_module( "VUser::ACL::$mod", $cfg); }; | ||
| 204 | warn "Unable to load ACL module: $@\n"; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | # load_acl_module and load_auth_module are currently identical but that | ||
| 209 | # may change at some point. | ||
| 210 | sub load_acl_module { load_auth_module(@_); } | ||
| 211 | |||
| 212 | sub auth_user | ||
| 213 | { | ||
| 214 | my $self = shift; | ||
| 215 | my $cfg = shift; | ||
| 216 | my $user = shift; | ||
| 217 | my $pass = shift; | ||
| 218 | my $ip = shift; | ||
| 219 | |||
| 220 | my $result = DENY(); | ||
| 221 | |||
| 222 | PRI: foreach my $pri (@{ $self->{auth}}) { | ||
| 223 | next unless defined $pri; | ||
| 224 | foreach my $mod (@{ $pri }) { | ||
| 225 | $result = &$mod($cfg, $user, $pass, $ip); | ||
| 226 | last PRI if ($result eq ALLOW() or $result eq DENY()); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | if ($result eq ALLOW()) { | ||
| 231 | return 1; | ||
| 232 | } else { | ||
| 233 | return 0; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | sub check_acls | ||
| 238 | { | ||
| 239 | my $self = shift; | ||
| 240 | my $cfg = shift; | ||
| 241 | my $user = shift; | ||
| 242 | my $ip = shift; | ||
| 243 | my $keyword = shift; | ||
| 244 | my $action = shift; | ||
| 245 | my $option = shift; | ||
| 246 | my $value = shift; | ||
| 247 | |||
| 248 | my $result = DENY(); | ||
| 249 | |||
| 250 | PRI: foreach my $pri (@{ $self->{acl}}) { | ||
| 251 | next unless defined $pri; | ||
| 252 | foreach my $mod (@{ $pri }) { | ||
| 253 | $result = &$mod($cfg, $user, $ip, $keyword, $action, $option, $value); | ||
| 254 | last PRI if ($result eq ALLOW() or $result eq DENY()); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | if ($result eq ALLOW()) { | ||
| 258 | return 1; | ||
| 259 | } else { | ||
| 260 | return 0; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | sub plugin_tasks | ||
| 265 | { | ||
| 266 | my ($cfg, $opts, $action, $eh, $sect, $func) = @_; | ||
| 267 | |||
| 268 | my @mods = (); | ||
| 269 | if ($opts->{module}) { | ||
| 270 | @mods = ($opts->{module}); | ||
| 271 | } else { | ||
| 272 | my $mods; | ||
| 273 | if ($sect eq 'acl') { | ||
| 274 | $mods = VUser::ExtLib::strip_ws($cfg->{$c_sec}{'acl modules'}); | ||
| 275 | } elsif ($sect eq 'auth') { | ||
| 276 | $mods = VUser::ExtLib::strip_ws($cfg->{$c_sec}{'auth modules'}); | ||
| 277 | } | ||
| 278 | $mods = '' unless $mods; | ||
| 279 | @mods = split / /, $mods; | ||
| 280 | } | ||
| 281 | |||
| 282 | foreach my $mod (@mods) { | ||
| 283 | # is this what I want to call the sub in the modules? | ||
| 284 | no strict 'refs'; | ||
| 285 | &{"VUser::" | ||
| 286 | .($sect eq 'acl'? 'ACL' : 'Auth') | ||
| 287 | ."::".$mod."::".$func}($cfg, $opts, $action, $eh); | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | sub auth_add { plugin_tasks(@_, 'auth', 'auth_add') }; | ||
| 292 | sub auth_del { plugin_tasks(@_, 'auth', 'auth_del') }; | ||
| 293 | |||
| 294 | sub auth_show | ||
| 295 | { | ||
| 296 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 297 | |||
| 298 | # use Data::Dumper; print Dumper $cfg, $opts, $action, $eh; | ||
| 299 | |||
| 300 | my $mods; | ||
| 301 | |||
| 302 | if ($opts->{module}) { | ||
| 303 | $mods = $opts->{module}; | ||
| 304 | } else { | ||
| 305 | $mods = VUser::ExtLib::strip_ws($cfg->{$c_sec}{'auth modules'}); | ||
| 306 | } | ||
| 307 | |||
| 308 | my @users = (); | ||
| 309 | foreach my $mod (split / /, $mods) { | ||
| 310 | no strict 'refs'; | ||
| 311 | push @users, &{ "VUser::Auth::".$mod."::auth_get" }($cfg, $opts, $action, $eh); | ||
| 312 | } | ||
| 313 | |||
| 314 | foreach my $user (@users) { | ||
| 315 | print join (':', map { defined $_? $_ : '' } ($user->{user}, | ||
| 316 | $user->{password}, | ||
| 317 | $user->{ip})); | ||
| 318 | print "\n"; | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | sub acl_add { plugin_tasks(@_, 'acl', 'acl_add'); } | ||
| 323 | |||
| 324 | 1; | ||
| 325 | |||
| 326 | __END__ |