| 1 | package CGI::AppBuilder::Security; | ||
| 2 | |||
| 3 | # Perl standard modules | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Getopt::Std; | ||
| 7 | use POSIX qw(strftime); | ||
| 8 | use Carp; | ||
| 9 | use CGI ':standard'; | ||
| 10 | use CGI::Cookie; | ||
| 11 | use CGI::AppBuilder; | ||
| 12 | use CGI::AppBuilder::Message qw(:echo_msg); | ||
| 13 | |||
| 14 | our $VERSION = 0.12; | ||
| 15 | require Exporter; | ||
| 16 | our @ISA = qw(Exporter CGI::AppBuilder); | ||
| 17 | our @EXPORT = qw(); | ||
| 18 | our @EXPORT_OK = qw(access_ok get_cookies set_cookies | ||
| 19 | ); | ||
| 20 | our %EXPORT_TAGS = ( | ||
| 21 | access => [qw(access_ok)], | ||
| 22 | all => [@EXPORT_OK] | ||
| 23 | ); | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | CGI::AppBuilder::Security - Security Procedures | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use CGI::AppBuilder::Security; | ||
| 32 | |||
| 33 | my $sec = CGI::AppBuilder::Security->new(); | ||
| 34 | my ($sta, $msg) = $sec->access_ok($ar); | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | This class provides methods for reading and parsing configuration | ||
| 39 | files. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | =head2 new (ifn => 'file.cfg', opt => 'hvS:') | ||
| 44 | |||
| 45 | This is a inherited method from CGI::AppBuilder. See the same method | ||
| 46 | in CGI::AppBuilder for more details. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my ($s, %args) = @_; | ||
| 52 | return $s->SUPER::new(%args); | ||
| 53 | } | ||
| 54 | |||
| 55 | =head2 access_ok($ar) | ||
| 56 | |||
| 57 | Input variables: | ||
| 58 | |||
| 59 | $ar - array ref containing the following variables: | ||
| 60 | task : task name required ($t) | ||
| 61 | sel_sn1 : select one (DB/server name) | ||
| 62 | sel_sn2 : select two (Argument) | ||
| 63 | allowed_ip : allowed ip address for each task | ||
| 64 | roles : roles allowed to access a list of tasks | ||
| 65 | svr_allowed : server allowed for each task | ||
| 66 | arg_required : required argument for each task | ||
| 67 | |||
| 68 | Variables used or routines called: | ||
| 69 | |||
| 70 | None | ||
| 71 | |||
| 72 | How to use: | ||
| 73 | |||
| 74 | First define the parameters in the initial file or define all the parameters | ||
| 75 | in a hash array reference as $ar->{$p} where $p are | ||
| 76 | |||
| 77 | task = task_name | ||
| 78 | sel_sn1 = a_db_name_or_server_name | ||
| 79 | app_user = logname_or_logid | ||
| 80 | usr_role = { | ||
| 81 | usr1 = [qw(dba owb)], | ||
| 82 | usr2 = [qw(r1 r2)], | ||
| 83 | } | ||
| 84 | usr_task = { | ||
| 85 | usr1 = [qw(task1 task2)], | ||
| 86 | usr2 = [qw(task3 task4)], | ||
| 87 | } | ||
| 88 | rol_task = { | ||
| 89 | dba => [qw(task1 task3 task5)], | ||
| 90 | owb => [qw(task2 task4)], | ||
| 91 | } | ||
| 92 | allowed_ip = { | ||
| 93 | all => [10.0.0.127,10.0.0.185,10.0.1.125)], | ||
| 94 | task1 => [192.168.1.1,192.168.1.2], | ||
| 95 | task2 => [10.0.0.5], | ||
| 96 | } | ||
| 97 | arg_required = { | ||
| 98 | task1 => 'input1:input2', | ||
| 99 | task2 => 'p_dnm:p_prj:p_tab:p_own', | ||
| 100 | } | ||
| 101 | svr_allowed = { | ||
| 102 | task1 => {svr1=>1}, | ||
| 103 | task2 => {svr1=>1,svr2=>1,svr3=>1}, | ||
| 104 | } | ||
| 105 | # $time = timelocal($sec,$min,$hour,$mday,$mon,$year); | ||
| 106 | # $sec : 0~59 | ||
| 107 | # $min : 0~59 | ||
| 108 | # $hour: 0~23 | ||
| 109 | # $mday: 1~31 | ||
| 110 | # $mon : 0~11 | ||
| 111 | # $year: yyyy-1900 | ||
| 112 | # ex : [0,0,0,16,8,109] = 2009/09/16 00:00:00 | ||
| 113 | task_expired = { | ||
| 114 | task1 => [0,0,0,25,0,109], # 2009/01/25 | ||
| 115 | task1 => [0,0,0,17,8,119], # 2019/09/17 | ||
| 116 | } | ||
| 117 | |||
| 118 | my ($q, $ar, $ar_log) = $self->start_app($0, \@ARGV); | ||
| 119 | or | ||
| 120 | my $ar = $self->read_init_file('/tmp/my_init.cfg'); | ||
| 121 | my ($status, $err_msg) = $self->access_ok($ar); | ||
| 122 | if ($status > 0) { | ||
| 123 | print "OK\n"; | ||
| 124 | } else { | ||
| 125 | print "Failed: $err_msg\n"; | ||
| 126 | } | ||
| 127 | |||
| 128 | my ($ok, $msg) = $self->access_ok($task, $ar); | ||
| 129 | if ($ok) { | ||
| 130 | $self->exe_sql($q, $ar); | ||
| 131 | } else { | ||
| 132 | print $self->disp_form($q, $ar); | ||
| 133 | print "<font color=red>$msg</font>\n" if $ar->{write_log}; | ||
| 134 | $self->echo_msg($msg,0); | ||
| 135 | } | ||
| 136 | |||
| 137 | Return: ($status, $msg) where $status is 1 (ok) or 0 (not), and the msg | ||
| 138 | is the error message. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub access_ok { | ||
| 143 | my ($s, $ar) = @_; | ||
| 144 | |||
| 145 | # $s->disp_param($ar); | ||
| 146 | |||
| 147 | # 0. get parameters | ||
| 148 | my $vs = 'REMOTE_ADDR'; | ||
| 149 | my ($ip) = $s->get_params($vs, \%ENV); | ||
| 150 | $vs = 'task,sel_sn1,sel_sn2,app_user'; | ||
| 151 | my ($tsk,$sn,$s2,$usr) = $s->get_params($vs, $ar); | ||
| 152 | $usr = $ENV->{LOGNAME} if !$usr; | ||
| 153 | my ($pn, $ok, $msg) = ('',1,''); | ||
| 154 | |||
| 155 | # 1. check required inputs | ||
| 156 | return (0, "ERR: missing task name.") if !$tsk; | ||
| 157 | return (0, "ERR: missing server/DB name.") if !$sn; | ||
| 158 | |||
| 159 | # 2. check if the task is allowed for the specified server | ||
| 160 | $pn = 'svr_allowed'; | ||
| 161 | if (exists $ar->{$pn}) { | ||
| 162 | my $sa = eval $s->set_param($pn, $ar); | ||
| 163 | $ok = (!$sa || ! exists $sa->{$tsk}) ? 1 : ( | ||
| 164 | (exists $sa->{$tsk}{$sn}) ? $sa->{$tsk}{$sn} : 0); | ||
| 165 | return ($ok, "ERR: Action $tsk is not allowed in DB $sn") if !$ok; | ||
| 166 | } | ||
| 167 | |||
| 168 | # 3. check arguments | ||
| 169 | $pn = 'arg_required'; | ||
| 170 | if (exists $ar->{$pn}) { | ||
| 171 | my $amr = eval $s->set_param($pn, $ar); # ARG is required | ||
| 172 | my @a = (); | ||
| 173 | @a = split /:/, $amr->{$tsk} if exists $amr->{$tsk} && $amr->{$tsk}; | ||
| 174 | my $arg = {}; | ||
| 175 | if ($s2) { | ||
| 176 | my @b = split /:/, $s2; | ||
| 177 | for my $i (0..$#b) { | ||
| 178 | $arg->{"a$i"} = $b[$i]; | ||
| 179 | $ar->{$a[$i]} = $b[$i] if $a[$i]; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | $ok = (exists $amr->{$tsk} && (!$arg || ! exists $arg->{a0})) ? 0 : 1; | ||
| 183 | return ($ok, "ERR: Task ($tsk) requires ARGS ($amr->{$tsk})") if !$ok; | ||
| 184 | } | ||
| 185 | |||
| 186 | # 4. task expiration | ||
| 187 | $pn = 'task_expired'; | ||
| 188 | if (exists $ar->{$pn}) { | ||
| 189 | my $texp = eval $s->set_param($pn, $ar); # task expiration date | ||
| 190 | # $s->echo_msg($texp,0); | ||
| 191 | if (exists $texp->{$tsk}) { | ||
| 192 | my $exp_tm = timelocal (@{$texp->{$tsk}}); | ||
| 193 | my $exp_dt = strftime "%Y/%m/%d %H:%M:%S", (@{$texp->{$tsk}}); | ||
| 194 | $ok = (exists $texp->{$tsk} && (time > $exp_tm)) ? 0 : 1; | ||
| 195 | return ($ok, "ERR: Task ($tsk) expired on $exp_dt") if !$ok; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | # 5. check if it is allowed to run from the IP address | ||
| 200 | $pn = 'allowed_ip'; | ||
| 201 | if (exists $ar->{$pn}) { | ||
| 202 | my $aip = eval $s->set_param($pn, $ar); | ||
| 203 | my ($ok1, $ok2); | ||
| 204 | if (exists $aip->{all}) { | ||
| 205 | my $alp = {}; | ||
| 206 | for my $i (0..$#{$aip->{all}}) { | ||
| 207 | my $k = $aip->{all}[$i]; | ||
| 208 | $k =~ s/(^\s*|\s*$)//; $alp->{$k} = 1; | ||
| 209 | } | ||
| 210 | $ok1 = (exists $alp->{$ip}) ? 1 : 0; | ||
| 211 | $s->echo_msg("INFO: 1 - $aip->{all},$ip,$ok1",2); | ||
| 212 | # $s->echo_msg($alp,0); | ||
| 213 | } | ||
| 214 | if (exists $aip->{$tsk}) { # ip addrs are provided for indivisual task | ||
| 215 | my $ap = {}; | ||
| 216 | for my $i (0..$#{$aip->{$tsk}}) { | ||
| 217 | my $k = $aip->{$tsk}[$i]; | ||
| 218 | $k =~ s/(^\s*|\s*$)//; $ap->{$k} = 1; | ||
| 219 | } | ||
| 220 | # print "$ip,$ap->{$ip}<br>\n"; | ||
| 221 | $ok2 = (exists $ap->{$ip}) ? 1 : 0; | ||
| 222 | $s->echo_msg("INFO: 2 - $aip->{$tsk},$ip,$ok2",2); | ||
| 223 | # $s->echo_msg($ap,0); | ||
| 224 | } | ||
| 225 | $ok = $ok1 + $ok2; | ||
| 226 | if (!$ok) { | ||
| 227 | my $msg = "ERR: You are not allowed to run $tsk from IP address $ip"; | ||
| 228 | # $s->echo_msg($msg,0); | ||
| 229 | # $s->echo_msg($aip,0); | ||
| 230 | return ($ok, $msg); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # 6. check if the task is a DBA task | ||
| 235 | # we do not check this if no application user is specified. | ||
| 236 | return (1,'') if !$usr; | ||
| 237 | if (!exists $ar->{rol_task} && !exists $ar->{usr_task}) { | ||
| 238 | return (1,'WARN: rol_task and usr_task is not defined'); | ||
| 239 | } | ||
| 240 | return (1,'WARN: usr_role is not defined') if !exists $ar->{usr_role} && | ||
| 241 | exists $ar->{rol_task}; | ||
| 242 | return (1,'WARN: rol_task is not defined') if exists $ar->{usr_role} && | ||
| 243 | !exists $ar->{rol_task}; | ||
| 244 | my $usr_role = eval $s->set_param('usr_role', $ar); | ||
| 245 | my $usr_task = eval $s->set_param('usr_task', $ar); | ||
| 246 | my $rol_task = eval $s->set_param('usr_task', $ar); | ||
| 247 | |||
| 248 | # build user and task list | ||
| 249 | my $utk = {}; # hash array for user and task list | ||
| 250 | if (exists $ar->{usr_task}) { | ||
| 251 | foreach my $u (keys %{$usr_task}) { | ||
| 252 | map { my $t = $usr_task->{$u}[$_]; $utk->{$t} = 1; } | ||
| 253 | 0..$#{$usr_task->{$u}}; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | if (exists $ar->{usr_role}) { | ||
| 257 | foreach my $u (keys %{$usr_role}) { # user | ||
| 258 | for my $i (0..$#{$usr_role->{$u}}) { # role | ||
| 259 | my $r = $usr_role->{$u}[$i]; | ||
| 260 | next if ! exists $rol_task->{$r}; | ||
| 261 | map { my $t = $rol_task->{$u}[$_]; $utk->{$t} = 1; } | ||
| 262 | 0..$#{$rol_task->{$u}}; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | $ok = ( (exists $usr_role->{$usr} || exists $usr_task->{$usr}) && | ||
| 267 | (!exists $utk->{$tsk} || !$utk->{$tsk} ) ) ? 0 : 1; | ||
| 268 | return ($ok, "ERR: User $usr is not allowed to run Task $tsk!") if !$ok; | ||
| 269 | |||
| 270 | return ($ok,$msg); | ||
| 271 | } | ||
| 272 | |||
| 273 | # --------------------------------------------------------------------------------- | ||
| 274 | |||
| 275 | =head3 get_cookies ($cgi,$ar) | ||
| 276 | |||
| 277 | Input variables: | ||
| 278 | |||
| 279 | $cgi - CGI object | ||
| 280 | $ar - Array ref containing all the parameters | ||
| 281 | |||
| 282 | Variables used or routines called: | ||
| 283 | |||
| 284 | disp_param - display parameters | ||
| 285 | |||
| 286 | How to use: | ||
| 287 | |||
| 288 | my $q = new CGI; | ||
| 289 | my %cfg = (usr=>'jsmith', pwd=>'jojo'); | ||
| 290 | my @names = $q->param; | ||
| 291 | foreach my $k (@names) { $cfg{$k} = $q->param($k) if ! exists $cfg{$k}; } | ||
| 292 | $self->get_cookies($q, \%cfg); | ||
| 293 | |||
| 294 | Return: ($ck_ar, $ck1, $ck2, $ck3) - hash array reference for cookies | ||
| 295 | (${$ck_ar}{$ck}{$ck}) and cookie names. | ||
| 296 | |||
| 297 | This method retrieves and parses cookies set by previous process and | ||
| 298 | returns them in a hash array reference. | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub get_cookies { | ||
| 303 | my $s = shift; | ||
| 304 | my ($q, $ar) = @_; | ||
| 305 | |||
| 306 | # retrieve cookies | ||
| 307 | # my %cookies = fetch CGI::Cookie; | ||
| 308 | my %cookies = CGI::Cookie->fetch; | ||
| 309 | $s->disp_param(\%cookies); | ||
| 310 | |||
| 311 | my %cks = (); # parsed cookies | ||
| 312 | foreach my $k (sort keys %cookies) { | ||
| 313 | foreach my $rec (split /;/, $cookies{$k}) { | ||
| 314 | my ($k1, $v1) = split /=/, $rec; | ||
| 315 | $cks{$k}{$k1} = $v1; | ||
| 316 | } | ||
| 317 | } | ||
| 318 | $s->disp_param(\%cks) if exists $ar->{v} && $ar->{v}; | ||
| 319 | wantarray ? %cks : \%cks; | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | =head3 set_cookies ($cgi,$ar, $cr, $dr) | ||
| 324 | |||
| 325 | Input variables: | ||
| 326 | |||
| 327 | $cgi - CGI object | ||
| 328 | $ar - Array ref containing all the parameters | ||
| 329 | $cr - cookie array ref | ||
| 330 | $dr - access array ref | ||
| 331 | |||
| 332 | Variables used or routines called: | ||
| 333 | |||
| 334 | get_cookies - get cookie hash array ref | ||
| 335 | get_access - get access hash array ref | ||
| 336 | |||
| 337 | How to use: | ||
| 338 | |||
| 339 | my $q = new CGI; | ||
| 340 | my %cfg = (usr=>'jsmith', pwd=>'jojo'); | ||
| 341 | my @names = $q->param; | ||
| 342 | foreach my $k (@names) { $cfg{$k} = $q->param($k) if ! exists $cfg{$k}; } | ||
| 343 | $self->get_cookies($q, \%cfg); | ||
| 344 | |||
| 345 | Return: 1 or 0 to indicates whether setting cookies is sucessfull. | ||
| 346 | |||
| 347 | This method retrieves and parses cookies set by previous process and | ||
| 348 | returns them in a hash array reference. | ||
| 349 | |||
| 350 | =cut | ||
| 351 | |||
| 352 | sub set_cookies { | ||
| 353 | my $s = shift; | ||
| 354 | my ($q, $ar, $cr, $dr) = @_; | ||
| 355 | |||
| 356 | # $cr = $s->get_cookies($q, $ar) if ! $cr; | ||
| 357 | # $dr = $s->get_access($q, $ar) if ! $dr; | ||
| 358 | $cr = $s->get_cookies($q, $ar); | ||
| 359 | # $s->echo_msg($cr, 0); | ||
| 360 | |||
| 361 | my $dn = $ENV{HTTP_HOST}; | ||
| 362 | my $vs = 'UID,PWD,SID'; | ||
| 363 | my $kv = {}; | ||
| 364 | my $ck = []; | ||
| 365 | foreach my $k (split ',',$vs) { | ||
| 366 | my $k1 = "ck$k"; | ||
| 367 | my $k2 = "user_" . lc($k); | ||
| 368 | my $v = (exists $ar->{$k2}) ? $ar->{$k2} : ''; | ||
| 369 | if ($k =~ /^timeout/i && $v) { | ||
| 370 | # convert YYYYMMDD.HH24MISS to perl time | ||
| 371 | # $time = timelocal($sec,$min,$hour,$mday,$mon,$year); | ||
| 372 | my $yr = substr($v,1,4) - 1900; | ||
| 373 | my $mn = substr($v,5,2); | ||
| 374 | my $dd = substr($v,7,2); | ||
| 375 | my $hh = substr($v,10,2); | ||
| 376 | my $mm = substr($v,12,2); | ||
| 377 | my $ss = substr($v,14,2); | ||
| 378 | $v = timelocal($ss,$mm,$hh,$dd,$mn,$yr); | ||
| 379 | } | ||
| 380 | $v = $cr->{$k1} if !$v && exists $cr->{$k1}; | ||
| 381 | $kv->{$k1} = $v; | ||
| 382 | push @$ck, $q->cookie(-name=>$k1,-value=>$v,-domain=>$dn, | ||
| 383 | -expires=>'+3M'); | ||
| 384 | } | ||
| 385 | $ar->{_cookie} = $ck; | ||
| 386 | # print header(-cookie=>$ck); | ||
| 387 | # for my $i (0..$#$ck) { my $c = $ck->[$i]; print "Set-Cookie: $c\n"; } | ||
| 388 | # print "Content-Type: text/html\n\n"; | ||
| 389 | |||
| 390 | # $s->echo_msg($kv, 3); | ||
| 391 | # $s->echo_msg($ck, 0); | ||
| 392 | # my $c2 = $s->get_cookies($q, $ar); | ||
| 393 | # $s->echo_msg($c2, 0); | ||
| 394 | return 0 if !$kv->{ckUID} || !$kv->{ckPWD}; | ||
| 395 | return 1; | ||
| 396 | } | ||
| 397 | |||
| 398 | 1; | ||
| 399 | |||
| 400 | =head1 HISTORY | ||
| 401 | |||
| 402 | =over 4 | ||
| 403 | |||
| 404 | =item * Version 0.10 | ||
| 405 | |||
| 406 | This version ported from ora_jobs.pl on 9/17/2009. | ||
| 407 | |||
| 408 | =item * Version 0.20 | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | =head1 SEE ALSO (some of docs that I check often) | ||
| 413 | |||
| 414 | Oracle::Loader, Oracle::Trigger, CGI::AppBuilder, File::Xcopy, | ||
| 415 | CGI::AppBuilder::Message | ||
| 416 | |||
| 417 | =head1 AUTHOR | ||
| 418 | |||
| 419 | Copyright (c) 2009 Hanming Tu. All rights reserved. | ||
| 420 | |||
| 421 | This package is free software and is provided "as is" without express | ||
| 422 | or implied warranty. It may be used, redistributed and/or modified | ||
| 423 | under the terms of the Perl Artistic License (see | ||
| 424 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 425 | |||
| 426 | =cut | ||
| 427 |