| 1 | package VUser::SOAP; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # Copyright (c) 2006 Randy Smith | ||
| 6 | # $Id: SOAP.pm,v 1.10 2007/09/19 19:16:25 perlstalker Exp $ | ||
| 7 | |||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | use VUser::Log qw(:levels); | ||
| 11 | use VUser::ExtLib qw(:config); | ||
| 12 | use VUser::ExtHandler; | ||
| 13 | use VUser::ACL; | ||
| 14 | use VUser::Meta; | ||
| 15 | use Digest::MD5 qw(md5); | ||
| 16 | my $eh; | ||
| 17 | my $log; | ||
| 18 | my $cfg; | ||
| 19 | my $acl; | ||
| 20 | my $debug = 0; | ||
| 21 | |||
| 22 | my $c_sec = 'vsoapd'; | ||
| 23 | |||
| 24 | my %sessions = (); | ||
| 25 | |||
| 26 | sub init { | ||
| 27 | $cfg = shift; | ||
| 28 | |||
| 29 | $eh = VUser::ExtHandler->new($cfg); | ||
| 30 | $eh->load_extensions(%$cfg); | ||
| 31 | |||
| 32 | if (ref $main::log and UNIVERSAL::isa($main::log, 'VUser::Log')) { | ||
| 33 | $log = $main::log; | ||
| 34 | } else { | ||
| 35 | $log = VUser::Log->new($cfg, 'VUser::SOAP'); | ||
| 36 | } | ||
| 37 | |||
| 38 | if (defined $main::DEBUG) { | ||
| 39 | $debug = $main::DEBUG; | ||
| 40 | } | ||
| 41 | |||
| 42 | ## Load up the ACL and auth info | ||
| 43 | $acl = new VUser::ACL ($cfg); | ||
| 44 | $acl->load_auth_modules($cfg); | ||
| 45 | $acl->load_acl_modules($cfg); | ||
| 46 | |||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub Log { $log->log(@_); } | ||
| 51 | |||
| 52 | sub login { | ||
| 53 | my $user = shift; | ||
| 54 | my $password = shift; | ||
| 55 | my $ip = shift; | ||
| 56 | |||
| 57 | $log->log(LOG_DEBUG, "In ::SOAP::login"); | ||
| 58 | |||
| 59 | if (check_bool($cfg->{$c_sec}{'require_authentication'})) { | ||
| 60 | if (not $acl->auth_user($cfg, $user, $password, $ip)) { | ||
| 61 | if ($debug) { | ||
| 62 | $log->log(LOG_NOTICE, "Authentication failed for $user\@$ip [$password]"); | ||
| 63 | } else { | ||
| 64 | $log->log(LOG_NOTICE, "Authentication failed for $user\@$ip"); | ||
| 65 | } | ||
| 66 | return undef; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | # Expire old sessions | ||
| 71 | clean_sessions(); | ||
| 72 | |||
| 73 | my ($ticket, $expr); | ||
| 74 | |||
| 75 | my $timeout = strip_ws($cfg->{$c_sec}{'ticket lifetime'}); | ||
| 76 | # Default to 10 minutes if timeout is not a valid number | ||
| 77 | $timeout = 10 unless defined $timeout and $timeout =~ /^\d+(?:\.\d+)$/; | ||
| 78 | |||
| 79 | $log->log(LOG_DEBUG, "Doing login"); | ||
| 80 | |||
| 81 | $expr = time() + 60 * $timeout; | ||
| 82 | $ticket = calculate_ticket($user, $ip, $expr); | ||
| 83 | $sessions{$ticket} = {user => $user, ip => $ip, expires => $expr}; | ||
| 84 | return $ticket; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub check_ticket { | ||
| 88 | my $ticket = shift; | ||
| 89 | |||
| 90 | # I need to be able to check the binary ticket as well as the ticket | ||
| 91 | # converted to a hex string. | ||
| 92 | |||
| 93 | if (not defined $sessions{$ticket} | ||
| 94 | or $sessions{$ticket}{expires} > time() | ||
| 95 | ) { | ||
| 96 | $log->log(LOG_NOTICE, "Invalid ticket"); | ||
| 97 | delete $sessions{$ticket}; | ||
| 98 | return 0; | ||
| 99 | } else { | ||
| 100 | my $timeout = strip_ws($cfg->{$c_sec}{'ticket lifetime'}); | ||
| 101 | # Default to 10 minutes if timeout is not a valid number | ||
| 102 | $timeout = 10 unless defined $timeout and $timeout =~ /^\d+(?:\.\d+)$/; | ||
| 103 | |||
| 104 | $sessions{$ticket}{expires} = time() + 60 * $timeout; | ||
| 105 | } | ||
| 106 | |||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub calculate_ticket { | ||
| 111 | return md5(join '', $cfg->{$c_sec}{'digest key'}, map { defined $_? $_ : '' } @_); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub run_tasks { | ||
| 115 | my $ticket = shift; | ||
| 116 | my $keyword = shift; | ||
| 117 | my $action = shift; | ||
| 118 | my $env = pop; | ||
| 119 | my $body = $env->body; | ||
| 120 | #use Data::Dumper; print Dumper $body; | ||
| 121 | my %opts = (); | ||
| 122 | if (defined $body->{$keyword.'_'.$action} | ||
| 123 | and ref $body->{$keyword.'_'.$action} eq 'HASH' | ||
| 124 | ) { | ||
| 125 | %opts = %{ $body->{$keyword.'_'.$action} }; | ||
| 126 | } | ||
| 127 | |||
| 128 | my $user = $sessions{$ticket}{user}; | ||
| 129 | my $ip = $sessions{$ticket}{ip}; | ||
| 130 | |||
| 131 | # We need to translate the SOAP::Data params into a hash | ||
| 132 | # suitable for ::ExtHandler->run_tasks. | ||
| 133 | #my %opts = build_opts(@params); | ||
| 134 | |||
| 135 | $log->log(LOG_DEBUG, '%opts => '.Dumper(\%opts)); | ||
| 136 | |||
| 137 | if (check_bool($cfg->{$c_sec}{'require authentication'})) { | ||
| 138 | # Do all of the ACL checks. | ||
| 139 | eval {check_acls($cfg, $user, $ip, $keyword, $action, \%opts) }; | ||
| 140 | # FAULT if a check fails | ||
| 141 | # VUser::ACL logs the reason so we don't need to log it here. | ||
| 142 | die SOAP::Failt | ||
| 143 | ->faultcode('Server.Custom') | ||
| 144 | ->faultstring('Permission denied') if $@; | ||
| 145 | } | ||
| 146 | |||
| 147 | # We've passed all of the ACL checks. Run the task. | ||
| 148 | my $rs = []; | ||
| 149 | $log->log(LOG_NOTICE, "%s\@%s running %s | %s", | ||
| 150 | defined $user? $user : 'undef', | ||
| 151 | defined $ip? $ip : 'undef', | ||
| 152 | defined $keyword? $keyword : 'undef', | ||
| 153 | defined $action? $action : 'undef'); | ||
| 154 | eval { $rs = $eh->run_tasks($keyword, $action, $cfg, %opts); }; | ||
| 155 | if ($@) { | ||
| 156 | die SOAP::Fault | ||
| 157 | ->faultcode('Server.Custom') | ||
| 158 | ->faultstring($@) | ||
| 159 | ; | ||
| 160 | } | ||
| 161 | |||
| 162 | return $rs; | ||
| 163 | }; | ||
| 164 | |||
| 165 | sub clean_sessions { | ||
| 166 | my $now = time(); | ||
| 167 | foreach my $ticket (keys %sessions) { | ||
| 168 | if (defined $sessions{$ticket}{expires} | ||
| 169 | and $sessions{$ticket}{expires} > $now) | ||
| 170 | { | ||
| 171 | delete $sessions{$ticket} | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | sub check_acls { | ||
| 177 | my $user = shift; | ||
| 178 | my $ip = shift; | ||
| 179 | my $keyword = shift; | ||
| 180 | my $action = shift; | ||
| 181 | my $opts = shift; | ||
| 182 | |||
| 183 | # Check ACLs | ||
| 184 | if (not $acl->check_acls($cfg, $user, $ip, $keyword)) { | ||
| 185 | $log->log(LOG_NOTICE, "Permission denined for %s: %s", | ||
| 186 | $user, $keyword); | ||
| 187 | die "Permission denied for $user on $keyword"; | ||
| 188 | } | ||
| 189 | |||
| 190 | if ($action | ||
| 191 | and not $acl->check_acls($cfg, $user, $ip, $keyword, $action)) { | ||
| 192 | $log->log(LOG_NOTICE, "Permission denied for %s: %s %s", | ||
| 193 | $user, $keyword, $action); | ||
| 194 | die "Permission denied for $user on $keyword - $action"; | ||
| 195 | } | ||
| 196 | |||
| 197 | if ($action and $opts) { | ||
| 198 | foreach my $key (keys %$opts) { | ||
| 199 | if (not $acl->check_acls($cfg, | ||
| 200 | $user, $ip, | ||
| 201 | $keyword, $action, | ||
| 202 | $key, $opts->{$key} | ||
| 203 | ) | ||
| 204 | ) { | ||
| 205 | $log->log(LOG_NOTICE, "Permission denied for %s: %s %s - %s", | ||
| 206 | $user, $keyword, $action, $key); | ||
| 207 | die "Permission denied for $user on $keyword - $action - $key"; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | return 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub get_keywords { | ||
| 216 | my $authinfo = shift; | ||
| 217 | |||
| 218 | my @keywords = (); | ||
| 219 | foreach my $key ($eh->get_keywords()) { | ||
| 220 | if (check_bool($cfg->{$c_sec}{'require authentication'})) { | ||
| 221 | eval { $acl->check_acls($cfg, $authinfo->{'user'}, $authinfo->{'ip'}, $key); }; | ||
| 222 | next if ($@); | ||
| 223 | } | ||
| 224 | if ($key eq 'config' || $key eq 'help' || $key eq 'man') { | ||
| 225 | next; | ||
| 226 | } | ||
| 227 | push @keywords, { keyword => $key, | ||
| 228 | description => $eh->get_description($key) }; | ||
| 229 | } | ||
| 230 | #print "Keywords: "; use Data::Dumper; print Dumper \@keywords; | ||
| 231 | return @keywords; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub get_actions { | ||
| 235 | my $authinfo = shift; | ||
| 236 | my $keyword = shift; | ||
| 237 | |||
| 238 | my @actions = (); | ||
| 239 | foreach my $act ($eh->get_actions($keyword)) { | ||
| 240 | if (check_bool($cfg->{$c_sec}{'require authentication'})) { | ||
| 241 | eval { $acl->check_acls($cfg, $authinfo->{'user'}, $authinfo->{'ip'}, $keyword, $act); }; | ||
| 242 | next if ($@); | ||
| 243 | } | ||
| 244 | |||
| 245 | push @actions, {action => $act, | ||
| 246 | description => $eh->get_description($keyword, $act) }; | ||
| 247 | } | ||
| 248 | |||
| 249 | return @actions; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub get_options { | ||
| 253 | my $authinfo = shift; | ||
| 254 | my $keyword = shift; | ||
| 255 | my $action = shift; | ||
| 256 | |||
| 257 | my @options = (); | ||
| 258 | $log->log(LOG_DEBUG, "Getting options for %s | %s", $keyword, $action); | ||
| 259 | foreach my $opt ($eh->get_options($keyword, $action)) { | ||
| 260 | if (check_bool($cfg->{$c_sec}{'require authentication'})) { | ||
| 261 | eval { $acl->check_acls($cfg, | ||
| 262 | $authinfo->{'user'}, | ||
| 263 | $authinfo->{'ip'}, | ||
| 264 | $keyword, $action, $opt); }; | ||
| 265 | next if $@; | ||
| 266 | } | ||
| 267 | |||
| 268 | $log->log(LOG_DEBUG, "Sending option $opt"); | ||
| 269 | |||
| 270 | my @meta = $eh->get_meta($keyword, $opt); | ||
| 271 | push @options, { option => $opt, | ||
| 272 | description => $eh->get_description($keyword, $action, $opt), | ||
| 273 | required => $eh->is_required($keyword, $action, $opt), | ||
| 274 | type => $meta[0]->type() }; | ||
| 275 | } | ||
| 276 | #use Data::Dumper; print Dumper \@options; | ||
| 277 | return @options; | ||
| 278 | } | ||
| 279 | |||
| 280 | sub build_opts { | ||
| 281 | my $env; | ||
| 282 | if (ref $_[-1] and UNIVERSAL::isa($_[-1], "SOAP::SOM")) { | ||
| 283 | $env = pop @_; | ||
| 284 | } | ||
| 285 | my @params = @_; | ||
| 286 | my %opts = (); | ||
| 287 | |||
| 288 | foreach my $param (@params) { | ||
| 289 | $opts{$param->name()} = $param->value(); | ||
| 290 | $log->log(LOG_DEBUG, "Param: %s => %s", $param->name(), | ||
| 291 | defined ($param->value())? $param->value() : 'undef'); | ||
| 292 | } | ||
| 293 | |||
| 294 | return %opts; | ||
| 295 | } | ||
| 296 | |||
| 297 | sub cleanup { | ||
| 298 | eval { $eh->cleanup($cfg); }; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub conf { | ||
| 302 | my $section = shift; | ||
| 303 | my $key = shift; | ||
| 304 | return $cfg->{$section}{$key}; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub rs2soap { | ||
| 308 | my @result_sets = shift; | ||
| 309 | |||
| 310 | my @records = (); | ||
| 311 | foreach my $record (@result_sets) { | ||
| 312 | |||
| 313 | my @soap_rs = (); | ||
| 314 | foreach my $rs (@$record) { | ||
| 315 | my @meta = $rs->get_metadata(); | ||
| 316 | my (@cols, @types); | ||
| 317 | foreach my $meta (@meta) { | ||
| 318 | push @cols, $meta->name(); | ||
| 319 | push @types, $meta->type(); | ||
| 320 | } | ||
| 321 | ## Set columns | ||
| 322 | # It would be really nice to set the name space globally. | ||
| 323 | my $columns = SOAP::Data->name('columns' => | ||
| 324 | \SOAP::Data->name('item' => @cols)->type('string') | ||
| 325 | ); | ||
| 326 | $log->log(LOG_DEBUG, "Creating ColumnArray"); | ||
| 327 | $columns->type('tns:ColumnArray'); | ||
| 328 | #$columns->type('ArrayOf_string'); | ||
| 329 | |||
| 330 | ## Set types | ||
| 331 | my $types = SOAP::Data->name('types' => | ||
| 332 | \SOAP::Data->name('item' => @types)->type('string') | ||
| 333 | ); | ||
| 334 | $log->log(LOG_DEBUG, "Creating TypeArray"); | ||
| 335 | $types->type('tns:TypeArray'); | ||
| 336 | #$types->type('ArrayOf_string'); | ||
| 337 | |||
| 338 | ## Set values | ||
| 339 | my @table = $rs->results(); | ||
| 340 | my @vals = (); | ||
| 341 | $log->log(LOG_DEBUG, "Creating ValueArray"); | ||
| 342 | foreach my $row (@table) { | ||
| 343 | # Force conversion of values to strings | ||
| 344 | #my @entries = map { SOAP::Data->value($_)->type('string'); } @$row; | ||
| 345 | |||
| 346 | # Put strings is a 'ValueArray' | ||
| 347 | #push @vals, SOAP::Data->value(@entries)->type('tns:ValueArray'); | ||
| 348 | # Convert undefs to '' | ||
| 349 | my @clean_row = map { defined $_? $_ : 'undef'; } @$row; | ||
| 350 | push @vals, SOAP::Data->value( | ||
| 351 | \SOAP::Data->name('item' => @clean_row)->type('string') | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | my $values = SOAP::Data->name('values' => | ||
| 355 | \SOAP::Data->name('item' => @vals)->type('tns:ValueArray') | ||
| 356 | ); | ||
| 357 | $log->log(LOG_DEBUG, "Creating DataArray"); | ||
| 358 | $values->type('tns:DataArray'); | ||
| 359 | |||
| 360 | my $result_set = SOAP::Data->name('ResultSet' => | ||
| 361 | \SOAP::Data->value($columns, $types, $values) | ||
| 362 | ); | ||
| 363 | $log->log(LOG_DEBUG, "Creating ResultSet"); | ||
| 364 | $result_set->type('tns:ResultSet'); | ||
| 365 | push @soap_rs, $result_set; | ||
| 366 | } | ||
| 367 | |||
| 368 | ## Record | ||
| 369 | $log->log(LOG_DEBUG, "Creating Record"); | ||
| 370 | my $record = SOAP::Data->name('results' => | ||
| 371 | \SOAP::Data->name('item' => @soap_rs)->type('tns:ResultSet') | ||
| 372 | ); | ||
| 373 | $record->type('tns:Record'); | ||
| 374 | push @records, $record; | ||
| 375 | } | ||
| 376 | |||
| 377 | ## RecordArray | ||
| 378 | $log->log(LOG_DEBUG, "Creating RecordArray"); | ||
| 379 | my $soap_results = SOAP::Data->name('results' => | ||
| 380 | \SOAP::Data->name('item' => @records)->type('tns:Record') | ||
| 381 | )->type('tns:RecordArray'); | ||
| 382 | if ($debug) { | ||
| 383 | $log->log(LOG_DEBUG, '$soap_results => '. Dumper($soap_results)); | ||
| 384 | } | ||
| 385 | return $soap_results; | ||
| 386 | } | ||
| 387 | |||
| 388 | 1; | ||
| 389 | |||
| 390 | __END__ |