Wednesday, April 30, 2014

Checks Oracle Locks very useful queries

Hi everyone today i come across following lock checking queries if you are stuck placing the lock at database level you can filter view the listing in order to list only those objects which have been locked by your session.

-- view all currently locked objects:

SELECT username U_NAME, owner OBJ_OWNER,
object_name, object_type, s.osuser,
DECODE(l.block,
  0, 'Not Blocking',
  1, 'Blocking',
  2, 'Global') STATUS,
  DECODE(v.locked_mode,
    0, 'None',
    1, 'Null',
    2, 'Row-S (SS)',
    3, 'Row-X (SX)',
    4, 'Share',
    5, 'S/Row-X (SSX)',
    6, 'Exclusive', TO_CHAR(lmode)
  ) MODE_HELD
FROM gv$locked_object v, dba_objects d,
gv$lock l, gv$session s
WHERE v.object_id = d.object_id
AND (v.object_id = l.id1)
AND v.session_id = s.sid
ORDER BY username, session_id;


-- list current locks

SELECT session_id,lock_type,
mode_held,
mode_requested,
blocking_others,
lock_id1
FROM dba_lock l
WHERE lock_type
NOT IN ('Media Recovery', 'Redo Thread');


-- list objects that have been
-- locked for 60 seconds or more:

SELECT SUBSTR(TO_CHAR(w.session_id),1,5) WSID, p1.spid WPID,
SUBSTR(s1.username,1,12) "WAITING User",
SUBSTR(s1.osuser,1,8) "OS User",
SUBSTR(s1.program,1,20) "WAITING Program",
s1.client_info "WAITING Client",
SUBSTR(TO_CHAR(h.session_id),1,5) HSID, p2.spid HPID,
SUBSTR(s2.username,1,12) "HOLDING User",
SUBSTR(s2.osuser,1,8) "OS User",
SUBSTR(s2.program,1,20) "HOLDING Program",
s2.client_info "HOLDING Client",
o.object_name "HOLDING Object"
FROM gv$process p1, gv$process p2, gv$session s1,
gv$session s2, dba_locks w, dba_locks h, dba_objects o
WHERE w.last_convert > 60
AND h.mode_held != 'None'
AND h.mode_held != 'Null'
AND w.mode_requested != 'None'
AND s1.row_wait_obj# = o.object_id
AND w.lock_type(+) = h.lock_type
AND w.lock_id1(+) = h.lock_id1
AND w.lock_id2 (+) = h.lock_id2
AND w.session_id = s1.sid (+)
AND h.session_id = s2.sid (+)
AND s1.paddr = p1.addr (+)
AND s2.paddr = p2.addr (+)
ORDER BY w.last_convert DESC;

Thursday, April 17, 2014

Oracle Regular Expression useful in preventing the SQL Injection through Oracle forms or other front end PUI.

The following examples taken from the Oracle Documentation http://docs.oracle.com/ for reference.


The following example examines phone_number, looking for the pattern xxx.xxx.xxxx. Oracle reformats this pattern with (xxxxxx-xxxx.
SELECT
  REGEXP_REPLACE(phone_number,
                 '([[:digit:]]{3})\.([[:digit:]]{3})\.([[:digit:]]{4})',
                 '(\1) \2-\3') "REGEXP_REPLACE"
  FROM employees;

REGEXP_REPLACE
--------------------------------------------------------------------------------
(515) 123-4567
(515) 123-4568
(515) 123-4569
(590) 423-4567
. . .

The following example examines country_name. Oracle puts a space after each non-null character in the string.
SELECT
  REGEXP_REPLACE(country_name, '(.)', '\1 ') "REGEXP_REPLACE"
  FROM countries;

REGEXP_REPLACE
--------------------------------------------------------------------------------
A r g e n t i n a
A u s t r a l i a
B e l g i u m
B r a z i l
C a n a d a
. . .

The following example examines the string, looking for two or more spaces. Oracle replaces each occurrence of two or more spaces with a single space.
SELECT
  REGEXP_REPLACE('500   Oracle     Parkway,    Redwood  Shores, CA',
                 '( ){2,}', ' ') "REGEXP_REPLACE"
  FROM DUAL;

REGEXP_REPLACE
--------------------------------------
500 Oracle Parkway, Redwood Shores, CA





Multilingual Regular Expression Syntax
Table C-1 Regular Expression Operators and Metasymbols
OperatorDescription
aThe backslash character can have four different meanings depending on the context. It can:
  • Stand for itself
  • Quote the next character
  • Introduce an operator
  • Do nothing
*Matches zero or more occurrences
+Matches one or more occurrences
?Matches zero or one occurrence
|Alternation operator for specifying alternative matches
bMatches the beginning-of-line character
bMatches the end-of-line character
cMatches any character in the supported character set except NULL
[ ] dBracket expression for specifying a matching list that should match any one of the expressions represented in the list. A nonmatching list expression begins with a circumflex (^) and specifies a list that matches any character except for the expressions represented in the list.
( )Grouping expression, treated as a single subexpression
{m}Matches exactly m times
{m,}Matches at least m times
{m,n}Matches at least m times but no more than n times
\n eThe backreference expression (n is a digit between 1 and 9) matches the nth subexpression enclosed between '(' and ')' preceding the \n
[..] fSpecifies one collation element, and can be a multicharacter element (for example, [.ch.] in Spanish)
[: :] gSpecifies character classes (for example, [:alpha:]). It matches any character within the character class.
[==] hSpecifies equivalence classes. For example, [=a=] matches all characters having base letter 'a'.
Notes on the POSIX operators and Oracle enhancements:
a '\': The backslash operator can be used to make the character following it normal if it is an operator. For example, '\*' is interpreted as the asterisk string literal.
b '^' and '$': The characters '^' and '$' are the POSIX anchoring operators. By default, they match only the beginning or end of an entire string. Oracle lets you specify '^' and '$' to match the start or end of any line anywhere within the source string. This in turns lets you treat the source string as multiple lines.
c '.': In the POSIX standard, the "match any character" operator ('.') is defined to match any English character except NULL and the newline character. In the Oracle implementation, the '.' operator can match any character in the database character set, including the newline character.
d '[]': In the POSIX standard, a range in a regular expression includes all collation elements between the start and end points of the range in the linguistic definition of the current locale. Therefore, ranges in regular expressions are linguistic ranges rather than byte values ranges, and the semantics of the range expression are independent of character set. Oracle implements this independence by interpreting range expressions according to the linguistic definition determined by the NLS_SORT initialization parameter.
e '\n': The backreference expression '\n' matches the same string of characters as was matched by the nth subexpression. The character n must be a digit from 1 to 9, designating the nth subexpression, numbered from left to right. The expression is invalid if the source string contains fewer than n subexpressions preceding the \n. For example, the regular expression ^(.*)\1$ matches a line consisting of two adjacent appearances of the same string. Oracle supports the backreference expression in the regular expression pattern and the replacement string of the REGEXP_REPLACE function.
f '[..]': A collating element is a unit of collation and is equal to one character in most cases, but may comprise two or more characters in some languages. Historically, regular expression syntax does not support ranges containing multicharacter collation elements, such as the range 'a' through 'ch'. The POSIX standard introduces the collation element delimiter '[..]', which lets you delimit multicharacter collection elements such like this one as follows: '[a-[.ch.]]'. The collation elements supported by Oracle are determined by the setting of the NLS_SORT initialization parameter. The collation element is valid only inside the bracketed expression.
g '[::]': In English regular expressions, range expressions often indicate a character class. For example, '[a-z]' indicates any lowercase character. This convention is not useful in multilingual environments where the first and last character of a given character class may not be the same in all languages. The POSIX standard introduces a portable character class syntax, '[::]'. Oracle supports the following character classes, based on character class definitions in NLS classification data:
Character Class SyntaxMeaning
[:alnum:]All alphanumeric characters
[:alpha:]All alphabetic characters
[:blank:]All blank space characters.
[:cntrl:]All control characters (nonprinting)
[:digit:]All numeric digits
[:graph:]All [:punct:][:upper:][:lower:], and [:digit:] characters.
[:lower:]All lowercase alphabetic characters
[:print:]All printable characters
[:punct:]All punctuation characters
[:space:]All space characters (nonprinting)
[:upper:]All uppercase alphabetic characters
[:xdigit:]All valid hexadecimal characters
This character class syntax lets you make better use of NLS character definitions to write flexible regular expressions. These character classes are valid only inside the bracketed expression.
h '[==]': Oracle supports the equivalence classes through the POSIX '[==]' syntax. A base letter and all of its accented versions constitute an equivalence class. For example, the equivalence class '[=a=]' matches ä and â. The equivalence classes are valid only inside the bracketed expression.
Restriction on equivalence classes: Composed and decomposed versions of the same equivalence class do not match. For example, ä does not match a followed by umlaut.

Monday, April 14, 2014

List of Views for checking the Roles and related privilege granted in Oracle 10,11G

ViewDescription
DBA_COL_PRIVS
ALL_COL_PRIVS
USER_COL_PRIVS
DBA view describes all column object grants in the database. ALL view describes all column object grants for which the current user or PUBLIC is the object owner, grantor, or grantee. USER view describes column object grants for which the current user is the object owner, grantor, or grantee.
ALL_COL_PRIVS_MADE
USER_COL_PRIVS_MADE
ALL view lists column object grants for which the current user is object owner or grantor. USER view describes column object grants for which the current user is the grantor.
ALL_COL_PRIVS_RECD
USER_COL_PRIVS_RECD
ALL view describes column object grants for which the current user or PUBLIC is the grantee. USER view describes column object grants for which the current user is the grantee.
DBA_TAB_PRIVS
ALL_TAB_PRIVS
USER_TAB_PRIVS
DBA view lists all grants on all objects in the database. ALL view lists the grants on objects where the user or PUBLIC is the grantee. USER view lists grants on all objects where the current user is the grantee.
ALL_TAB_PRIVS_MADE
USER_TAB_PRIVS_MADE
ALL view lists the all object grants made by the current user or made on the objects owned by the current user. USER view lists grants on all objects owned by the current user.
ALL_TAB_PRIVS_RECD
USER_TAB_PRIVS_RECD
ALL view lists object grants for which the user or PUBLIC is the grantee. USER view lists object grants for which the current user is the grantee.
DBA_ROLES
This view lists all roles that exist in the database.
DBA_ROLE_PRIVS
USER_ROLE_PRIVS
DBA view lists roles granted to users and roles. USER view lists roles granted to the current user.
DBA_SYS_PRIVS
USER_SYS_PRIVS
DBA view lists system privileges granted to users and roles. USER view lists system privileges granted to the current user.
ROLE_ROLE_PRIVS
This view describes roles granted to other roles. Information is provided only about roles to which the user has access.
ROLE_SYS_PRIVS
This view contains information about system privileges granted to roles. Information is provided only about roles to which the user has access.
ROLE_TAB_PRIVS
This view contains information about object privileges granted to roles. Information is provided only about roles to which the user has access.
SESSION_PRIVS
This view lists the privileges that are currently enabled for the user.
SESSION_ROLES
This view lists the roles that are currently enabled to the user.
Some examples of using these views follow. For these examples, assume the following statements have been issued:

CREATE ROLE security_admin IDENTIFIED BY honcho;

GRANT CREATE PROFILE, ALTER PROFILE, DROP PROFILE,
    CREATE ROLE, DROP ANY ROLE, GRANT ANY ROLE, AUDIT ANY,
    AUDIT SYSTEM, CREATE USER, BECOME USER, ALTER USER, DROP USER
    TO security_admin WITH ADMIN OPTION;

GRANT SELECT, DELETE ON SYS.AUD$ TO security_admin;

GRANT security_admin, CREATE SESSION TO swilliams;

GRANT security_admin TO system_administrator;

GRANT CREATE SESSION TO jward;

GRANT SELECT, DELETE ON emp TO jward;

GRANT INSERT (ename, job) ON emp TO swilliams, jward;