ORACLE 觸發器控制使用者登入之許可權限制

清風艾艾發表於2017-01-04
    出於資料安全性,公司要求DBA實現控制拒絕特定的使用者登入,由於公司不同的工作樓層分屬於不同的vlan,因此單從linux主機層次依賴ACL訪問列表控制登入資料庫伺服器,已經不能實現。
因此,只能考慮從資料庫內部加以限制登入資料庫,也就是藉助oracle Trigger實現登入驗證,實現方法就是捕獲到拒絕登入的使用者就拋應用異常強制使用者退出登入,也就是使用Raise_Application_Error
實現,但是需要注意的是,被限制的使用者不能有DBA許可權,也不能有imp_full_database許可權。其實,想想也明白,如果不小心限制了dba許可權的使用者登入,那麼sys就不能登入資料庫,也就全完蛋了。
/**
功能:實現限制特定使用者登入資料庫
注意:被限制使用者不能有dba及imp_full_database許可權,否則該觸發器不能起作用
日期:2017-1-4
**/
Create Or Replace Trigger DENY_LOGIN  
 After Logon On Database
Declare
 v_Program Varchar2(48);
 v_Message Varchar2(1000);
 v_deny_Client Exception;
 v_deny_User Exception;
 v_deny_Ip Exception;
Begin
 Select Program Into v_Program From V$session Where Audsid = Sys_Context('USERENV', 'SESSIONID') And Rownum < 2;
 If  Lower(v_Program) = 'plsqldev.exe' Then
     Raise v_deny_Client;
 End If;
 If  User In ('SCOTT','scott') Then
     Raise v_deny_User;
 End If;
 If Sys_Context('USERENV', 'ip_address') = '10.117.196.52' Then
     Raise v_deny_Ip;
 End If;
Exception
 When v_deny_Client Then
   v_Message := 'Sorry!You cannot access database using this software client!';
   Raise_Application_Error(-20001, v_Message);
 When v_deny_User Then
   v_Message := 'Sorry!Database deny you('||User||') access,Contact DBAs please!';
   Raise_Application_Error(-20002, v_Message);
 When v_deny_Ip Then
   v_Message := 'Sorry!Database deny you('||Sys_Context('USERENV', 'SESSIONID') ||') access,Contact DBAs please!';
   Raise_Application_Error(-20003, v_Message);
 When Others Then
   v_Message := 'ERROR – NOT_LOGON TRIGGER- Please Contact Your DBA!!' || Sqlerrm;
   Raise_Application_Error(-20004, v_Message);
End;
部分測試結果如下:
C:\Users\localadmin>sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 1月 4 09:32:27 2017
Copyright (c) 1982, 2010, Oracle.  All rights reserved.
連線到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> revoke dba from scott;
撤銷成功。
SQL> quit
從 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 斷開
C:\Users\localadmin>sqlplus scott/oracle@oradb
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 1月 4 09:32:39 2017
Copyright (c) 1982, 2010, Oracle.  All rights reserved.
ERROR:
ORA-00604: 遞迴 SQL 級別 1 出現錯誤
ORA-20002: Sorry!Database deny you(SCOTT) access,Contact DBAs please!
ORA-06512: 在 line 24
請輸入使用者名稱:
ERROR:
ORA-01017: 使用者名稱/口令無效; 登入被拒絕
請輸入使用者名稱:
ERROR:
ORA-01017: 使用者名稱/口令無效; 登入被拒絕
SP2-0157: 在 3 次嘗試之後無法連線到 ORACLE, 退出 SQL*Plus
C:\Users\localadmin>
C:\Users\localadmin>sqlplus scott/oracle
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 1月 4 09:48:42 2017
Copyright (c) 1982, 2010, Oracle.  All rights reserved.
ERROR:
ORA-00604: 遞迴 SQL 級別 1 出現錯誤
ORA-20002: Sorry!Database deny you(SCOTT) access,Contact DBAs please!
ORA-06512: 在 line 24
請輸入使用者名稱:
ERROR:
ORA-01017: 使用者名稱/口令無效; 登入被拒絕
請輸入使用者名稱:
ERROR:
ORA-01017: 使用者名稱/口令無效; 登入被拒絕
SP2-0157: 在 3 次嘗試之後無法連線到 ORACLE, 退出 SQL*Plus
C:\Users\localadmin>

C:\Users\localadmin>sqlplus zhul/oracle
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 1月 4 09:51:43 2017
Copyright (c) 1982, 2010, Oracle.  All rights reserved.
連線到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29357786/viewspace-2131845/,如需轉載,請註明出處,否則將追究法律責任。

相關文章