Unix Code - Detail View


Date

Name

Plattform

Language

Kurzbeschreibung

Owner

Link

20.10.2002

xmpDB2Secure.pl

Unix

Perl

DB2 Security relevante Informationen anzeigen, Quelle ist der DB2 Catalog

Gernot Ruban

 

DB2 ist ein Produkt der
IBM Corporation. Bitte
Copyright-  und Trademark-Hinweise beachten!

#!/usr/local/bin/perl -w
################################################################################
# xmpDB2Secure.pl - DB2 Object Security Utility
# Called by - Commend Level
# Calls     - DBI::DB2     - DB2 Perl Interace 
# Parameter - -d DB        - Database
#             -u USER      - User ID
#             -p PSW       - Password
#             -l DATABASE  - List DB Authorizations
#                SCHEMA    - List Schema Authorizations
#                PACKAGE   - List Package Authorizations
#                TABLE %.% - List Table Authorizations
#                ALL       - List all Authorizations
#             -c CREATOR   - Package or Table Creator
#             -n NAME      - Package or Table Name
#             -g GRANTEE   - Grantee Name
#             -g GRANTOR   - Grantor Name
# Returns   - 0|1          - exit Code [0=OK|1=Fehler]
#-------------------------------------------------------------------------------
# Autor     - GR /07.06.00 - V1.0 erste Version
# Changes   - GR /16.06.00 - V1.0.1 Korrektur bei zusätzl. Predicates
#-------------------------------------------------------------------------------
# Comments  - $drh = DBI Driver Handle
#             $dbh = DBD Database Handle
#             $sth = SQL Statement Handle
################################################################################

################################################################################
# Modules
################################################################################
use strict;
use Getopt::Std;
use vars qw($opt_d $opt_u $opt_p $opt_l $opt_c $opt_n $opt_g $opt_r);
use xmpPerl;
use DBI;
use DBD::DB2::Constants;
use DBD::DB2;
use Env;
use English;

################################################################################
# Declare/Init
################################################################################
my $VER  = "1.0.1";
my ($dbh, $sth);
my ($c1,$c2,$c3,$c4,$c5,$c6,$c7,$c8,$c9) = "";
my ($ca,$cb,$cc,$cd,$ce,$cf,$cg,$ch,$ci) = "";
my $stmt;

my $pred_c = "";# Predicate Creator
my $pred_n = "";# Predicate Name
my $pred_g = "";# Predicate Grantee
my $pred_r = "";# Predicate Grantor


################################################################################
# Ausgabe-Formate
################################################################################
format HEAD_DB =
-------------LIST OF DATABASE AUTHORIZATIONS------------------------------------
                  DB  CREATE BIND       NO   IMPL GRNT
GRANTOR  GRANTEE  ADM  TAB   ADD  CONN FENCE SCHM TYPE
--------------------------------------------------------------------------------
.

format LINE_DB =
@<<<<<<< @<<<<<<<  @    @     @    @    @     @    @
$c1,     $c2,      $c3, $c4,  $c5, $c6, $c7,  $c8, $c9
.
################################################################################
format HEAD_SCH =
-----------LIST OF SCHEMA AUTHORIZATIONS----------------------------------------
                                             GRNT                          
SCHEMA   GRANTOR  GRANTEE  CREATE ALTER DROP TYPE
--------------------------------------------------------------------------------
.

format LINE_SCH =
@<<<<<<< @<<<<<<< @<<<<<<<   @      @    @    @  
$c1,     $c2,     $c3,       $c4,   $c5,$c6,  $c7
.

################################################################################
format HEAD_PKG =
---------LIST OF PACKAGE AUTHORIZATIONS-----------------------------------------
                                                   GRNT                          
CREATOR  PACKAGE  GRANTOR  GRANTEE  CTRL BIND EXEC TYPE
--------------------------------------------------------------------------------
.

format LINE_PKG =
@<<<<<<< @<<<<<<< @<<<<<<< @<<<<<<<  @    @    @    @  
$c1,     $c2,     $c3,     $c4,      $c5, $c6, $c7, $c8
.

################################################################################
format HEAD_TB =
-----------------------LIST OF TABLE AUTHORIZATIONS-----------------------------
OWNER                                                                       GRNT
SCHEMA  TABLE              GRANTOR  GRANTEE CTL ALT DEL IDX INS SEL REF UPD TYPE
--------------------------------------------------------------------------------
.

format LINE_TB =
@<<<<<< @<<<<<<<<<<<<<<<<< @<<<<<<< @<<<<<<< @   @   @   @   @   @   @   @   @
$c1,    $c2,               $c3,     $c4,     $c5,$c6,$c7,$c8,$c9,$ca,$cb,$cd,$ce
.

################################################################################
# Argumente/Optionen prüfen
################################################################################
getopts("d:u:p:l:c:n:g:r:");

if (! defined $opt_d )
{print "$0 - Database nicht angegeben\n";
         SaySyntax();  }
         
if (! defined $opt_u ) {# User Check
$opt_u = xmpPerl::getDBAUser();;
if ($opt_u eq "")
{print "$0 - User nicht angegeben und keine lP_LUS Variable definiert\n";
         SaySyntax();  }
}

if (! defined $opt_p )# User Password Check
{$opt_p = xmpPerl::getDBAPsw();
if ($opt_p eq "")
{print "$0 - User's Password fehlt und keine lP_LPW Variable definiert\n";
         SaySyntax();  }
}

if (! defined $opt_l ) {$opt_l = "DATABASE"}# List Option
if (! defined $opt_c ) {$opt_c = ""}# Package/Table Creator
if (! defined $opt_n ) {$opt_n = ""}# Package/Table Name
if (! defined $opt_g ) {$opt_g = ""}# Grantee
if (! defined $opt_r ) {$opt_r = ""}# Grantor

$opt_d = uc $opt_d;# Upper Case Database
$opt_l = uc $opt_l;# Upper Case List Option
$opt_c = uc $opt_c;# Upper Case Package/Table Creator
$opt_n = uc $opt_n;# Upper Case Package/Table Name
$opt_g = uc $opt_g;# Upper Case Grantee
$opt_r = uc $opt_r;# Upper Case Grantor

if (  ($opt_l ne "DATABASE")
   && ($opt_l ne "SCHEMA")
   && ($opt_l ne "PACKAGE")
   && ($opt_l ne "TABLE")
   && ($opt_l ne "ALL")   )
{print "$0 - List Option nicht korrekt\n";
         SaySyntax(); 
}

################################################################################
# Check DB2 Existenz und Informationsblock ausgeben
################################################################################
xmpPerl::chkDB2();
xmpPerl::prtHeaderDB2("$0 - DB2 Security Tool - $VER");
print "Database / DB2 User ID ......: $opt_d / $opt_u\n";
print "List Option .................: $opt_l\n";
print "Creator/Name/Grantee/Grantor : $opt_c/$opt_n/$opt_g/$opt_r\n";

################################################################################
# DBI Switches
################################################################################
my $switch = DBI->internal;
$switch->debug(0);                     # 1=normal debug, 2=detailed handle trace
#print "DBI Version and Attributes ..: $switch->{'Attribution'}, $switch->{'Version'}\n";
#print "DBI Drivers .................: ",join(", ",DBI->available_drivers()),"\n";

################################################################################
# Connect to Database
################################################################################
$dbh = DBI->connect("dbi:DB2:$opt_d",
                       $opt_u,
                       $opt_p,
                    { PrintError => 0, # don't print error by default
                      RaiseError => 0, # don't print error and die by default
                      AutoCommit => 0, # don't commit after each statement
                      ChopBlanks => 0, # don't remove trailing blanks on CHAR-Types
                    } )
        || die ">>>$0 Problem mit Connect to $opt_d\n>>>".$DBI::errstr;

################################################################################
# DATABASE|ALL: Execute DB2 Catalog Authorization Query
################################################################################
if (($opt_l eq "ALL") | ($opt_l eq "DATABASE")) {
########################################################################
# Argumente dem Statement hinzufügen
########################################################################
if ($opt_g ne "") {
if ($opt_g =~ /%/) {$pred_g = "AND GRANTEE LIKE '$opt_g' "}
else  {$pred_g = "AND GRANTEE = '$opt_g' "}
}
if ($opt_r ne "") {
if ($opt_r =~ /%/) {$pred_r = "AND GRANTOR LIKE '$opt_r' "}
else  {$pred_r = "AND GRANTOR = '$opt_r' "}
}

$stmt = "SELECT GRANTOR, GRANTEE,".
                "       DBADMAUTH, CREATETABAUTH, BINDADDAUTH,".
                "       CONNECTAUTH, NOFENCEAUTH, IMPLSCHEMAAUTH,".
                "       GRANTEETYPE ".
        "FROM   SYSIBM.SYSDBAUTH ".
        "WHERE  1=1 ".
$pred_g.
$pred_r.
        "ORDER  BY GRANTEE, GRANTOR";
 
########################################################################
# Statement prüfen und ausführen
########################################################################
$sth = $dbh->prepare($stmt);
$sth->execute() || die $dbh->errstr;

########################################################################
# Formular-Einstellungen
########################################################################
$FORMAT_NAME = "LINE_DB"; # $~
$FORMAT_TOP_NAME = "HEAD_DB";# $^
$FORMAT_FORMFEED= "";# $^L
$FORMAT_LINES_PER_PAGE= 60;# $=, plus Lines für Kopf
$FORMAT_LINES_LEFT      = 0;# vorhergeh. Format

########################################################################
# Get all relevant rows from DB2 catalog query
########################################################################
while ( (($c1,$c2,$c3,$c4,$c5,$c6,$c7,$c8,$c9) = $sth->fetchrow()) )
{
  write;
}

if ( $dbh->err ) {
  die "$0 abended with $dbh->errstr";
}
}

################################################################################
# SCHEMA|ALL: Execute DB2 Catalog Authorization Query
################################################################################
if (($opt_l eq "ALL") || ($opt_l eq "SCHEMA")) {

########################################################################
# Argumente dem Statement hinzufügen
########################################################################
if ($opt_c ne "") {
if ($opt_c =~ /%/) {$pred_c = "AND SCHEMANAME LIKE '$opt_c' "}
else  {$pred_c = "AND SCHEMANAME = '$opt_c' "}
}
if ($opt_n ne "") {
if ($opt_n =~ /%/) {$pred_n = "AND SCHEMANAME LIKE '$opt_n' "}
else  {$pred_n = "AND SCHEMANAME = '$opt_n' "}
}
if ($opt_g ne "") {
if ($opt_g =~ /%/) {$pred_g = "AND GRANTEE LIKE '$opt_g' "}
else  {$pred_g = "AND GRANTEE = '$opt_g' "}
}
if ($opt_r ne "") {
if ($opt_r =~ /%/) {$pred_r = "AND GRANTOR LIKE '$opt_r' "}
else  {$pred_r = "AND GRANTOR = '$opt_r' "}
}

$stmt = "SELECT SCHEMANAME, GRANTOR, GRANTEE,".
                "       CREATEINAUTH, ALTERINAUTH, DROPINAUTH,".
                "       GRANTEETYPE ".
          "FROM   SYSIBM.SYSSCHEMAAUTH ".
        "WHERE  1=1 ".
$pred_c.
$pred_n.
$pred_g.
$pred_r.
        "ORDER  BY SCHEMANAME, GRANTEE, GRANTOR";
 
########################################################################
# Statement prüfen und ausführen
########################################################################
$sth = $dbh->prepare($stmt);
$sth->execute() || die $dbh->errstr;

########################################################################
# Formular-Einstellungen
########################################################################
$FORMAT_NAME = "LINE_SCH"; # $~
$FORMAT_TOP_NAME = "HEAD_SCH";# $^
$FORMAT_FORMFEED= "";# $^L
$FORMAT_LINES_PER_PAGE= 60;# $=, plus Lines für Kopf
$FORMAT_LINES_LEFT      = 0;# vorhergeh. Format

########################################################################
# Get all relevant rows from DB2 catalog query
########################################################################
while ( (($c1,$c2,$c3,$c4,$c5,$c6,$c7) = $sth->fetchrow()) )
{
  write;
}

if ( $dbh->err ) {
  die "$0 abended with $dbh->errstr";
}
}

################################################################################
# PACKAGE|ALL: Execute DB2 Catalog Authorization Query
################################################################################
if (($opt_l eq "ALL") || ($opt_l eq "PACKAGE")) {
########################################################################
# Argumente dem Statement hinzufügen
########################################################################
if ($opt_c ne "") {
if ($opt_c =~ /%/) {$pred_c = "AND CREATOR LIKE '$opt_c' "}
else  {$pred_c = "AND CREATOR = '$opt_c' "}
}
if ($opt_n ne "") {
if ($opt_n =~ /%/) {$pred_n = "AND NAME LIKE '$opt_n' "}
else  {$pred_n = "AND NAME = '$opt_n' "}
}
if ($opt_g ne "") {
if ($opt_g =~ /%/) {$pred_g = "AND GRANTEE LIKE '$opt_g' "}
else  {$pred_g = "AND GRANTEE = '$opt_g' "}
}
if ($opt_r ne "") {
if ($opt_r =~ /%/) {$pred_r = "AND GRANTOR LIKE '$opt_r' "}
else  {$pred_r = "AND GRANTOR = '$opt_r' "}
}

$stmt = "SELECT CREATOR, NAME, GRANTOR, GRANTEE,".
                "       CONTROLAUTH, BINDAUTH, EXECUTEAUTH,".
                "       GRANTEETYPE ".
        "FROM   SYSIBM.SYSPLANAUTH ".
        "WHERE  1=1 ".
$pred_c.
$pred_n.
$pred_g.
$pred_r.
        "ORDER  BY CREATOR, NAME, GRANTEE, GRANTOR";
 
########################################################################
# Statement prüfen und ausführen
########################################################################
$sth = $dbh->prepare($stmt);
$sth->execute() || die $dbh->errstr;

########################################################################
# Formular-Einstellungen
########################################################################
$FORMAT_NAME = "LINE_PKG"; # $~
$FORMAT_TOP_NAME = "HEAD_PKG";# $^
$FORMAT_FORMFEED= "";# $^L
$FORMAT_LINES_PER_PAGE= 60;# $=, plus Lines für Kopf
$FORMAT_LINES_LEFT      = 0;# vorhergeh. Format

########################################################################
# Get all relevant rows from DB2 catalog query
########################################################################
while ( (($c1,$c2,$c3,$c4,$c5,$c6,$c7,$c8) = $sth->fetchrow()) )
{
  write;
}

if ( $dbh->err ) {
  die "$0 abended with $dbh->errstr";
}
}

################################################################################
# TABLE|ALL: Execute DB2 Catalog Authorization Query
################################################################################
if (($opt_l eq "ALL") || ($opt_l eq "TABLE")) {
########################################################################
# Argumente dem Statement hinzufügen
########################################################################
if ($opt_c ne "") {
if ($opt_c =~ /%/) {$pred_c = "AND TCREATOR LIKE '$opt_c' "}
else  {$pred_c = "AND TCREATOR = '$opt_c' "}
}
if ($opt_n ne "") {
if ($opt_n =~ /%/) {$pred_n = "AND TTNAME LIKE '$opt_n' "}
else  {$pred_n = "AND TTNAME = '$opt_n' "}
}
if ($opt_g ne "") {
if ($opt_g =~ /%/) {$pred_g = "AND GRANTEE LIKE '$opt_g' "}
else  {$pred_g = "AND GRANTEE = '$opt_g' "}
}
if ($opt_r ne "") {
if ($opt_r =~ /%/) {$pred_r = "AND GRANTOR LIKE '$opt_r' "}
else  {$pred_r = "AND GRANTOR = '$opt_r' "}
}

$stmt = "SELECT TCREATOR, TTNAME, GRANTOR, GRANTEE,".
                "       CONTROLAUTH, ALTERAUTH, DELETEAUTH, INDEXAUTH,".
        "      INSERTAUTH, SELECTAUTH, REFAUTH, UPDATEAUTH,".
                "       GRANTEETYPE ".
        "FROM   SYSIBM.SYSTABAUTH ".
        "WHERE  1=1 ".
$pred_c.
$pred_n.
$pred_g.
$pred_r.
        "ORDER  BY TCREATOR, TTNAME, GRANTEE, GRANTOR";
 
########################################################################
# Statement prüfen und ausführen
########################################################################
$sth = $dbh->prepare($stmt);
$sth->execute() || die $dbh->errstr;

########################################################################
# Formular-Einstellungen
########################################################################
$FORMAT_NAME = "LINE_TB"; # $~
$FORMAT_TOP_NAME = "HEAD_TB";# $^
$FORMAT_FORMFEED= "";# $^L
$FORMAT_LINES_PER_PAGE= 60;# $=, plus Lines für Kopf
$FORMAT_LINES_LEFT      = 0;# vorhergeh. Format

########################################################################
# Get all relevant rows from DB2 catalog query
########################################################################
while ( (($c1,$c2,$c3,$c4,$c5,$c6,$c7,$c8,$c9,$ca,$cb,$cd,$ce) = $sth->fetchrow()) )
{
  write;
}

if ( $dbh->err ) {
  die "$0 abended with $dbh->errstr";
}
}
################################################################################
# Close Statement, Disconnect from Database
################################################################################
$sth->finish()   || die $dbh->errstr;
$dbh->commit     || die $dbh->errstr;
$dbh->disconnect || die $dbh->errstr;

print "***Ende***\n";

exit 0;


#################################################################################
########################################################## Sub Routines #########
# SaySyntax #####################################################################
sub SaySyntax {
die "Usage: [perl] $0 -d db [-u user -p psw -l {option} -...]\n".
"-d database \n".
"-u user\n".
"-p password\n".
"-l List Option {DATABASE|SCHEMA|PACKAGE|TABLE|ALL} dflt=DATABASE)\n".
"Bei Option -l|-r: -c creator -n name -g grantee -r grantor ".
"abends";
}

© Gernot Ruban