| 1 | package CGI::AppBuilder::MapSecus; | ||
| 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::AppBuilder; | ||
| 11 | use CGI::AppBuilder::Message qw(:echo_msg); | ||
| 12 | use CGI::AppBuilder::HTML qw(:all); | ||
| 13 | |||
| 14 | our $VERSION=0.13; | ||
| 15 | require Exporter; | ||
| 16 | our @ISA = qw(Exporter CGI::AppBuilder); | ||
| 17 | our @EXPORT = qw(); | ||
| 18 | our @EXPORT_OK = qw(access_ok2 get_guid set_guid sel_guid set_ids | ||
| 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_ok2($q,$ar) | ||
| 56 | |||
| 57 | Input variables: | ||
| 58 | |||
| 59 | $q - CGI object | ||
| 60 | $ar - array ref containing the following variables: | ||
| 61 | task : task name required ($t) | ||
| 62 | sel_sn1 : select one (DB/server name) | ||
| 63 | sel_sn2 : select two (Argument) | ||
| 64 | allowed_ip : allowed ip address for each task | ||
| 65 | roles : roles allowed to access a list of tasks | ||
| 66 | svr_allowed : server allowed for each task | ||
| 67 | arg_required : required argument for each task | ||
| 68 | |||
| 69 | Variables used or routines called: | ||
| 70 | |||
| 71 | None | ||
| 72 | |||
| 73 | How to use: | ||
| 74 | |||
| 75 | See access_ok | ||
| 76 | |||
| 77 | Return: ($status, $msg) where $status is 1 (ok) or 0 (not), and the msg | ||
| 78 | is the error message. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub access_ok2 { | ||
| 83 | my ($s, $q, $ar) = @_; | ||
| 84 | |||
| 85 | # $s->disp_param($ar); | ||
| 86 | my ($ok ,$msg) = (1,""); | ||
| 87 | |||
| 88 | # 0. get input parameters | ||
| 89 | my $vs = 'REMOTE_ADDR,LOGNAME'; | ||
| 90 | my ($ip,$user_log) = $s->get_params($vs, \%ENV); | ||
| 91 | $vs = 'task,sel_sn1,sel_sn2,new_task,app_user'; | ||
| 92 | my ($tsk,$sn,$s2,$ntsk,$usr_app)=$s->get_params($vs,$ar); | ||
| 93 | $vs = 'web_url,pid,guid'; | ||
| 94 | my ($url,$pid,$guid) = $s->get_params($vs, $ar); | ||
| 95 | $url .= "?pid=$pid&no_dispform=1&sel_sn1=$sn"; | ||
| 96 | my $f_a2 = "<a href=\"%s\" target=\"%s\" title=\"%s\">%s</a>\n"; | ||
| 97 | my $u1b = "$url&task=disp_new&new_task=run_login"; | ||
| 98 | my $s1b = sprintf $f_a2, $u1b, "R", "Login User", "Login ->"; | ||
| 99 | $vs = 'user_sid,user_uid,user_tmo,users_pwd'; | ||
| 100 | my ($usr_sid,$usr_uid,$usr_tmo,$usr_pwd) = $s->get_params($vs, $ar); | ||
| 101 | |||
| 102 | return (1,'OK') if (($ntsk && $ntsk =~ /(login|logout)$/i) | ||
| 103 | || $tsk =~ /(login|logout)$/i | ||
| 104 | || $tsk =~ /^disp_(client|project|study|list|job|link|frd)/i | ||
| 105 | || $tsk =~ /^(sel_stat)/i | ||
| 106 | || (exists $ar->{logout} && $ar->{logout}) ); | ||
| 107 | my $ctm = strftime "%Y%m%d.%H%M%S", localtime; | ||
| 108 | |||
| 109 | # we return OK if the tmo has more than 10 minutes remaining | ||
| 110 | return (1, 'OK') if ($usr_tmo && ($usr_tmo>$ctm) | ||
| 111 | && (($usr_tmo-$ctm) > 0.0010)); | ||
| 112 | |||
| 113 | # 1. check session id | ||
| 114 | my ($id_OK, $usr_gid) = $s->set_guid($ar); | ||
| 115 | $usr_gid = ($usr_gid) ? $usr_gid : '' ; | ||
| 116 | if (! $id_OK) { | ||
| 117 | print $q->header("text/html"); | ||
| 118 | print $q->start_html(%{$ar->{html_header}}); | ||
| 119 | print "$usr_gid<br>\n"; | ||
| 120 | print "Please $s1b<br>\n"; | ||
| 121 | print $q->end_html; | ||
| 122 | exit; | ||
| 123 | } | ||
| 124 | if (!$usr_gid) { | ||
| 125 | # $msg = "No user credential.<br>"; | ||
| 126 | print $q->header("text/html"); | ||
| 127 | print $q->start_html(%{$ar->{html_header}}); | ||
| 128 | print "Please $s1b<br>\n"; | ||
| 129 | print $q->end_html; | ||
| 130 | exit; | ||
| 131 | } else { | ||
| 132 | my @ss = split /:/, $usr_gid; | ||
| 133 | $usr_sid = $ss[0] if !$usr_sid; | ||
| 134 | $usr_uid = $ss[1] if !$usr_uid; | ||
| 135 | $usr_tmo = $ss[2] if !$usr_tmo; | ||
| 136 | } | ||
| 137 | $ar->{app_user} = $usr_uid if !$usr_app && $usr_uid; | ||
| 138 | $ar->{guid} = $usr_gid if !$guid && $usr_gid; | ||
| 139 | |||
| 140 | # 2. check timeout | ||
| 141 | |||
| 142 | $msg = "OK: "; | ||
| 143 | $msg .= "got user $usr_uid " if $usr_uid; | ||
| 144 | $msg .= "and its password " if $usr_pwd; | ||
| 145 | $msg .= "and GID = $usr_gid " if $usr_gid; | ||
| 146 | $msg .= "for task $tsk"; | ||
| 147 | $msg .= ($ntsk) ? "->$ntsk.<br>\n" : ".<br>\n"; | ||
| 148 | if ($usr_tmo && $usr_tmo > $ctm) { | ||
| 149 | $msg .= "This session will be expired at $usr_tmo.<br>"; | ||
| 150 | } else { | ||
| 151 | $msg .= "This session has expired at <b>$usr_tmo</b>.<br>" if $usr_tmo; | ||
| 152 | } | ||
| 153 | return (1,$msg) if ( ($tsk =~ /(login)$/i && $usr_uid && $usr_pwd) | ||
| 154 | || ($usr_tmo && $usr_tmo > $ctm) || $tsk =~ /(setanypwd)$/i); | ||
| 155 | |||
| 156 | # 3. check if we need to start the login page | ||
| 157 | $u1b .= "&guid=$usr_gid"; | ||
| 158 | |||
| 159 | if (!$usr_gid || $usr_tmo && $usr_tmo < $ctm ) { | ||
| 160 | print $q->header("text/html"); | ||
| 161 | print $q->start_html(%{$ar->{html_header}}); | ||
| 162 | print "$msg\nPlease $s1b<br>\n"; | ||
| 163 | print $q->end_html; | ||
| 164 | exit; | ||
| 165 | } | ||
| 166 | |||
| 167 | # 4. check required inputs | ||
| 168 | return (0, "ERR: missing task name.") if !$tsk; | ||
| 169 | return (0, "ERR: missing server/DB name.") if !$sn; | ||
| 170 | |||
| 171 | # 5. check if the task is allowed for the specified server | ||
| 172 | my $pn = 'svr_allowed'; | ||
| 173 | if (exists $ar->{$pn}) { | ||
| 174 | my $sa = eval $s->set_param($pn, $ar); | ||
| 175 | $ok = (!$sa || ! exists $sa->{$tsk}) ? 1 : ( | ||
| 176 | (exists $sa->{$tsk}{$sn}) ? $sa->{$tsk}{$sn} : 0); | ||
| 177 | return ($ok, "ERR: Action $tsk is not allowed in DB $sn") if !$ok; | ||
| 178 | } | ||
| 179 | |||
| 180 | # 6. check arguments | ||
| 181 | $pn = 'arg_required'; | ||
| 182 | if (exists $ar->{$pn}) { | ||
| 183 | my $amr = eval $s->set_param($pn, $ar); # ARG is required | ||
| 184 | my @a = (); | ||
| 185 | @a = split /:/, $amr->{$tsk} if exists $amr->{$tsk} && $amr->{$tsk}; | ||
| 186 | my $arg = {}; | ||
| 187 | if ($s2) { | ||
| 188 | my @b = split /:/, $s2; | ||
| 189 | for my $i (0..$#b) { | ||
| 190 | $arg->{"a$i"} = $b[$i]; | ||
| 191 | $ar->{$a[$i]} = $b[$i] if $a[$i]; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | $ok = (exists $amr->{$tsk} && (!$arg || ! exists $arg->{a0})) ? 0 : 1; | ||
| 195 | return ($ok, "ERR: Task ($tsk) requires ARGS ($amr->{$tsk})") if !$ok; | ||
| 196 | } | ||
| 197 | |||
| 198 | return ($ok,$msg); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | sub set_guid { | ||
| 203 | my ($s, $ar) = @_; | ||
| 204 | |||
| 205 | my $prg = 'AppBuilder::Common->set_guid'; | ||
| 206 | # 1. get current parameters | ||
| 207 | my $vs = 'user_uid,user_pwd,user_sid,user_tmo,guid,upd_tm_itv'; | ||
| 208 | my ($usr_uid,$usr_pwd,$usr_sid,$usr_tmo,$guid,$uti) = $s->get_params($vs,$ar); | ||
| 209 | my @sid = ($guid) ? (split /:/, $guid) : (); | ||
| 210 | if (@sid) { | ||
| 211 | $usr_sid = (!$usr_sid) ? $sid[0] : $usr_sid; | ||
| 212 | $usr_uid = (!$usr_uid) ? $sid[1] : $usr_uid; | ||
| 213 | $usr_tmo = (!$usr_tmo) ? $sid[2] : $usr_tmo; | ||
| 214 | } | ||
| 215 | if ($usr_sid !~ /^\d+$/) { | ||
| 216 | my $msg = "No user session ID (USER_SID)."; | ||
| 217 | print $s->disp_header(undef, $ar); | ||
| 218 | $s->echo_msg($msg, 0); return ""; | ||
| 219 | } | ||
| 220 | # 2. get parameters from DB | ||
| 221 | my $t = $s->sel_guid($ar); | ||
| 222 | my @ss = ($t) ? (split /:/, $t) : (); | ||
| 223 | $usr_sid = $ss[0] if !$usr_sid; | ||
| 224 | $usr_uid = $ss[1] if !$usr_uid; | ||
| 225 | $usr_tmo = $ss[2] if exists $ss[2] && $ss[2]; | ||
| 226 | |||
| 227 | # 3. check timeout | ||
| 228 | my $ctm = strftime "%Y%m%d.%H%M%S", localtime; | ||
| 229 | my $msg = "OK: "; | ||
| 230 | $msg .= "got user $usr_uid " if $usr_uid; | ||
| 231 | $msg .= "and its password " if $usr_pwd; | ||
| 232 | if ($usr_tmo && $usr_tmo < $ctm) { | ||
| 233 | $msg .= "This session has expired at <b>$usr_tmo</b>.<br>" if $usr_tmo; | ||
| 234 | return (0,$msg); | ||
| 235 | } | ||
| 236 | # 4. it has not expired so we need to extend the tmo | ||
| 237 | # we only reset it if it is less than 60 seconds (0.0007 day) to expire | ||
| 238 | # 0.0035 day = 300 seconds (5 minutes) | ||
| 239 | $uti = 0.0005 if ! $uti; | ||
| 240 | if ($usr_tmo && $ctm && $uti && ($usr_tmo-$ctm) < $uti) { | ||
| 241 | # get SESSION_TIMEOUT | ||
| 242 | my $cns = 'cfgvar_value'; | ||
| 243 | my $whr = " WHERE upper(CFGVAR_NAME) = 'SESSION_TIMEOUT' "; | ||
| 244 | my $r = $s->run_sqlcmd($ar, $cns, 'sp_cfgvars', $whr); | ||
| 245 | my $cfg_tmo = $r->[0]{cfgvar_value}; | ||
| 246 | my $cfg_tm2 = $cfg_tmo/(24*3600); | ||
| 247 | |||
| 248 | my $sql = "ALTER session SET nls_date_format='YYYYMMDD.HH24MISS';\n"; | ||
| 249 | $sql .= "SET linesize 999 serveroutput ON SIZE 1000000 FORMAT WRAPPED;\n"; | ||
| 250 | $sql .= "Update sp_sessions SET timeout_time = timeout_time + $cfg_tm2 "; | ||
| 251 | $sql .= "Where ses_id = $usr_sid;\n"; | ||
| 252 | $sql .= "commit;\n"; | ||
| 253 | my $rst = $s->open_cmd($sql,$ar); | ||
| 254 | } | ||
| 255 | |||
| 256 | #. 5. get the DB parameters again | ||
| 257 | if ($usr_tmo && $ctm && $uti && ($usr_tmo-$ctm) < $uti) { | ||
| 258 | $t = $s->sel_guid($ar); | ||
| 259 | @ss = ($t) ? (split /:/, $t) : (); | ||
| 260 | $usr_sid = $ss[0] if !$usr_sid; | ||
| 261 | $usr_uid = $ss[1] if !$usr_uid; | ||
| 262 | $usr_tmo = $ss[2] if exists $ss[2] && $ss[2]; | ||
| 263 | } | ||
| 264 | $usr_uid = ($usr_uid) ? $usr_uid : ''; | ||
| 265 | $usr_sid = ($usr_sid) ? $usr_sid : ''; | ||
| 266 | $usr_tmo = ($usr_tmo) ? $usr_tmo : ''; | ||
| 267 | $ar->{user_uid} = $usr_uid; | ||
| 268 | $ar->{user_sid} = $usr_sid; | ||
| 269 | $ar->{user_tmo} = $usr_tmo; | ||
| 270 | # $ar->{user_pwd} = $usr_pwd; | ||
| 271 | return (1,"$usr_sid:$usr_uid:$usr_tmo"); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub get_guid { | ||
| 275 | my ($s, $ar) = @_; | ||
| 276 | |||
| 277 | my $vs = 'user_uid,user_pwd,user_sid,user_tmo'; | ||
| 278 | my ($usr_uid,$usr_pwd,$usr_sid,$usr_tmo) = $s->get_params($vs,$ar); | ||
| 279 | |||
| 280 | return "$usr_sid:$usr_uid:$usr_tmo"; | ||
| 281 | } | ||
| 282 | |||
| 283 | sub sel_guid { | ||
| 284 | my ($s, $ar) = @_; | ||
| 285 | |||
| 286 | my $prg = 'AppBuilder::Common->sel_guid'; | ||
| 287 | # 1. check inputs | ||
| 288 | my $vs = 'user_uid,user_pwd,user_sid,user_tmo'; | ||
| 289 | my ($msg) = (""); | ||
| 290 | my ($usr_uid,$usr_pwd,$usr_sid,$usr_tmo) = $s->get_params($vs,$ar); | ||
| 291 | if ($usr_sid !~ /^\d+$/ && !$usr_uid) { | ||
| 292 | # $msg = "ERR: No USER_UID and USER_SID ($usr_uid,$usr_sid)."; | ||
| 293 | # $s->echo_msg($msg, 0); | ||
| 294 | return ""; | ||
| 295 | } | ||
| 296 | # 2. get info from sp_sessions | ||
| 297 | my $cns = 'ses_id,user_id,start_time,end_time,timeout_time,os_user'; | ||
| 298 | my $whr = " WHERE "; | ||
| 299 | my $r = []; | ||
| 300 | if ($usr_sid =~ /^\d+$/) { | ||
| 301 | $whr .= " ses_id = $usr_sid "; | ||
| 302 | } else { | ||
| 303 | $whr .= "upper(user_id) = '" . uc($usr_uid) . "' "; | ||
| 304 | $whr .= " ORDER BY start_time DESC "; | ||
| 305 | } | ||
| 306 | $r = $s->run_sqlcmd($ar, $cns, 'sp_sessions', $whr); | ||
| 307 | if (@$r) { | ||
| 308 | $usr_sid = $r->[0]{ses_id}; | ||
| 309 | $usr_uid = $r->[0]{user_id}; | ||
| 310 | $usr_tmo = $r->[0]{timeout_time} | ||
| 311 | } else { | ||
| 312 | $msg = "ERR: ($prg) no record for ses_id or user_id "; | ||
| 313 | $msg .= "($usr_sid,$usr_uid)"; | ||
| 314 | $s->echo_msg($msg,0); | ||
| 315 | return ""; | ||
| 316 | } | ||
| 317 | if (exists $ar->{user_uid} && "$ar->{user_uid}" ne "$usr_uid") { | ||
| 318 | $msg .= "$ar->{user_uid} NE $usr_uid. Set USER_ID to $usr_uid."; | ||
| 319 | $s->echo_msg($msg,1); | ||
| 320 | } | ||
| 321 | $ar->{user_uid} = $usr_uid; | ||
| 322 | $ar->{user_sid} = $usr_sid; | ||
| 323 | $ar->{user_tmo} = $usr_tmo; | ||
| 324 | # get info from sp_users | ||
| 325 | $cns = 'user_id,usr_pwd'; | ||
| 326 | $whr = " WHERE upper(user_id) = '" . uc($usr_uid) . "' "; | ||
| 327 | $r = $s->run_sqlcmd($ar, $cns, 'sp_users', $whr); | ||
| 328 | $usr_pwd = $r->[0]{usr_pwd}; | ||
| 329 | # $ar->{user_pwd} = $usr_pwd; | ||
| 330 | |||
| 331 | return "$usr_sid:$usr_uid:$usr_tmo"; | ||
| 332 | } | ||
| 333 | |||
| 334 | |||
| 335 | sub set_ids { | ||
| 336 | my ($s, $ar) = @_; | ||
| 337 | |||
| 338 | my $prg = 'AppBuilder::Common->set_ids'; | ||
| 339 | # 1. check inputs | ||
| 340 | my $vs = 'cln_id,prj_id,study_id,list_id,job_id'; | ||
| 341 | my ($cid,$pid,$sid,$lid,$jid) = $s->get_params($vs,$ar); | ||
| 342 | |||
| 343 | # 2. build sql statement | ||
| 344 | my $cns = "sp_findids_fn("; | ||
| 345 | $cns .= ($jid =~ /^\d+$/) ? $jid : 'null'; | ||
| 346 | $cns .= ($lid =~ /^\d+$/) ? $lid : ',null'; | ||
| 347 | $cns .= ($sid =~ /^\d+$/) ? $sid : ',null'; | ||
| 348 | $cns .= ($pid =~ /^\d+$/) ? $pid : ',null'; | ||
| 349 | $cns .= ') as record '; | ||
| 350 | my $sql = "ALTER session SET nls_date_format='YYYYMMDD.HH24MISS';\n"; | ||
| 351 | $sql .= "SET linesize 999 serveroutput ON SIZE 1000000 FORMAT WRAPPED;\n"; | ||
| 352 | $sql .= "SELECT '==,'||$cns FROM dual;\n"; | ||
| 353 | my $rst = $s->open_cmd($sql,$ar); | ||
| 354 | my $vr = ['cln_id','prj_id','study_id','list_id','job_id','stg_schema']; | ||
| 355 | # $ar->{var_arf} = $vr; | ||
| 356 | my $rr = $s->parse_records($rst, $vr, '==', ','); | ||
| 357 | |||
| 358 | # 3. set ids | ||
| 359 | my $r = {}; | ||
| 360 | foreach my $k (split /,/, $vs) { | ||
| 361 | $r->{$k} = $rr->[0]{$k}; | ||
| 362 | $ar->{$k} = $rr->[0]{$k} if !exists $ar->{$k} || $ar->{$k} !~ /^\d+$/; | ||
| 363 | } | ||
| 364 | my $usr_gid = (exists $ar->{guid}) ? $ar->{guid} : ""; | ||
| 365 | my $aa = ($usr_gid) ? [split /:/, $usr_gid] : []; | ||
| 366 | $ar->{user_sid} = $aa->[0] if !exists $ar->{user_sid} && exists $aa->[0]; | ||
| 367 | $ar->{user_uid} = $aa->[1] if !exists $ar->{user_uid} && exists $aa->[1]; | ||
| 368 | $ar->{user_tmo} = $aa->[2] if !exists $ar->{user_tmo} && exists $aa->[2]; | ||
| 369 | |||
| 370 | wantarray ? %$r : $r; | ||
| 371 | } | ||
| 372 | |||
| 373 | 1; | ||
| 374 | |||
| 375 | =head1 HISTORY | ||
| 376 | |||
| 377 | =over 4 | ||
| 378 | |||
| 379 | =item * Version 0.10 | ||
| 380 | |||
| 381 | This version extracted from jp2.pl on 09/08/2010. | ||
| 382 | |||
| 383 | =item * Version 0.20 | ||
| 384 | |||
| 385 | 02/08/2012 (htu): added access_ok2 | ||
| 386 | 02/10/2012 (htu): added get_guid, set_guid and sel_guid | ||
| 387 | 02/14/2012 (htu): added set_ids | ||
| 388 | |||
| 389 | =cut | ||
| 390 | |||
| 391 | =head1 SEE ALSO (some of docs that I check often) | ||
| 392 | |||
| 393 | Oracle::Loader, Oracle::Trigger, CGI::AppBuilder, File::Xcopy, | ||
| 394 | CGI::AppBuilder::Message | ||
| 395 | |||
| 396 | =head1 AUTHOR | ||
| 397 | |||
| 398 | Copyright (c) 2012 Hanming Tu. All rights reserved. | ||
| 399 | |||
| 400 | This package is free software and is provided "as is" without express | ||
| 401 | or implied warranty. It may be used, redistributed and/or modified | ||
| 402 | under the terms of the Perl Artistic License (see | ||
| 403 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 404 | |||
| 405 | =cut |