Thursday

Changing Permissions on files,Changing Owners,Groups

Chmod command is used to change file permissions. chmod -- Changing The File Permissions To change the file permissions , you must be the owner of the file or you must be the root user. The root user can change any permission bit. This may not be true of the owner. The one bit that the owner may not be able to switch on is the SGID bit. To turn on that bit, the owner must be a member of the group that the file is in. If this restriction was not in place, a user could simply create an SGID program to give himself access to files controlled by groups other than those to which he belongs. Remember that, as we mentioned above, the SGID bit has been often overloaded into also being used for file locking. A version of Unix may or may not allow a file owner to switch on the SGID bit of a file in a different group if no execute bit is set. Simply turning on an execute bit may result in the SUID and SGID bits being cleared (even for root). This is a security feature to ensure that the user intends for the SUID and SGID bits to be set. The user can switch them back on explicitly. Or the user can simply explicitly set all the bit at once. Also be aware that writing to a file may, on some versions of Unix, clear the SUID and SGID bits. See below for the effect of changing the owner or group of a file. In Unix access classes and access types specifies how to establish file permissions, different types of access to a file. Access classes are groups of users, and each may be assigned specific access types. The access classes are "user", "group", "other", and "all". These refer to the user who owns the file, a specific group of users, the other are users who are not in the group, and all are three sets of users. Access types (read, write, and execute) determines the access method of file by each access class. The general syntax is chmod -R/c/f/v [u / g / o / a] [+ / - / =] [rwxXstugo..] There are two basic ways of using chmod to change file permissions: Symbolic method: --------------- The first and probably easiest way is the relative (or symbolic) method, which lets you specify access classes and types with single letter abbreviations. A chmod command with this form of syntax consists of at least three parts from the following lists: Access Class Operator Access Type u (user) + (add access) r (read) g (group) - (remove access) w (write) o (other) = (set exact access) x (execute) a (all: u, g, and o) For example, to add permission for everyone to read a file in the current directory named myfile, at the Unix prompt, you would enter: chmod a+r filename Where 'a' stands for "all", the '+' for "add", and the 'r' for "read". Note: This assumes that everyone already has access to the directory where filename is located and its parent directories; that is, you must set the directory permissions separately. If you omit the access class, it is assumed to be all, so you could also enter the previous example as: chmod +r filename You can also specify multiple classes and types with a single command. For example, to remove read and write permission for group and other users (leaving only yourself with read and write permission) on a file named filename, you would enter: chmod go-rw filename You can also specify that different permissions be added and removed in the same command. For example, to remove write permission and add execute for all users on filename, you would enter: chmod a-w+x filename In each of these examples, the access types that aren't specified are unchanged. The previous command, for example, doesn't change any existing settings specifying whether users besides yourself may have read ( r ) access to filename. You could also use the exact form to explicitly state that group and other users' access is set only to read with the = operator: chmod go=r filename The chmod command also operates on directories. For example, to remove write permission for other users on a subdirectory named mydir, you would enter: chmod o-w mydir To do the same for the current directory, you would enter: chmod o-w Be careful when setting the permissions of directories, particularly your home directory; you don't want to lock yourself out by removing your own access. Also, you must have execute permission on a directory to switch ( cd ) to it. Absolute Form: -------------- The other way to use the chmod command is the absolute form. In this case, you specify a set of three numbers that together determine all the access classes and types. Rather than being able to change only particular attributes, you must specify the entire state of the file's permissions. The three numbers are specified in the order: user (or owner), group, other. Each number is the sum of values that specify read (4), write (2), and execute (1) access, with 0 (zero) meaning no access. For example, if you wanted to give yourself read, write, and execute permissions on filename; give users in your group read and execute permissions; and give others only execute permission, the appropriate number would be calculated as (4+2+1)(4+0+1)(0+0+1) for the three digits 751. You would then enter the command as: chmod 751 filename As another example, to give only yourself read, write, and execute permission on the current directory, you would calculate the digits as (4+2+1)(0+0+0)(0+0+0) for the sequence 700, and enter the command: chmod 700 If it seems clearer to you, you can also think of the three digit sequence as the sum of attributes you select from the following table: 400 read by owner 200 write by owner 100 execute by owner 040 read by group 020 write by group 010 execute by group 004 read by others 002 write by others 001 execute by others To create an access mode, sum all the accesses to permit. For example, to give read privileges to all, and write and execute privileges to the owner only for a file, you would sum: 400+200+100+040+004 = 744. Then, at the Unix prompt, you would enter: chmod 744 filename Some other frequently used examples are: 777 anyone can do anything (read, write, or execute) 755 you can do anything; others can only read and execute 711 you can do anything; others can only execute 644 you can read and write; others can only read chown -- Changing The File Owner -------------------------------- Originally, Unix allowed a file owner to give away a file. A file's owner could change the owner to someone else. There was no way for a non-root user to undo this operation. When Unix split into a Berkeley/AT&T versions, the USG (Unix Support Group, part of AT&T) versions of Unix tended to inherit this behavior. Meanwhile BSD (Berkeley Software Distribution, part of University of California, Berkeley) removed chown from non-root users. BSD had implemented disk quotas which could limit how much disk space a user could have in a filesystem. Naughty users could give away large files to sneek past the quotas. Today, it is not easy to say if a non-root can chown a file. Many versions of Unix allow both behaviors. HP-UX has a setprivgroup facility that can control whether or not members of a particular group can invoke chown. Solaris has a global paramter rstchown which can be set to allow global chown. Setting this parameter also disables a change-group limitation described below (without affecting the SGID limitations described above). Recent Linux version have a CAP_CHOWN capability to control this feature. You will need to consult your documentation for other versions of Unix. And you will need to consult your System Administrator to see how your particular system is configured. The default with most OS's is for chown to be restricted to root only. And there is a consensus that it should stay this way for security considerations. If a non-root user does change the owner of a file and any execute bit is on, the SUID and SGID bits must be cleared. This may or may not happen with root. The chown command The chown command is used to change the user and/or group which owns one or more files or directories. Its general format is : chown [-Rcfv] [username][:.][groupname] foo.txt The flags used above are same as those used in the chmod command . The following are the different ways in which this command can be used : The username followed by a dot or colon followed by a groupname changes both the user and group ownerships to those specified. The username followed by a dot or colon and no groupname changes the user ownership as specified and changes the group ownership to the specified user?s login group. If the colon or dot and groupname are specified without a username, then only the groupownership is changed. This is effectively the same as the chgrp command. If the username is not followed by a dot or a colon, then only the user ownership is changed. chown, chgrp -- Changing The File Group The chown command can (with any modern, Posix compliant version of Unix) also attempt a group change. And there is a chgrp command. Both of these invoke the chown() system call to change a group. The fact that a common system call is involved helps explain why some OS versions jointly enforce or relax restrictions on non-root users for both owner and group changes. A non-root user can change the group of file he owns to a group of which he is a member. Posix prohibits a non-root user from changing a files group to a group of which he is not a member. But some OS's lift this restriction if the restriction against changing a file's owner has been lifted. If the group is changed by a non-root user and one or execute bits are set, the SUID and SGID bits are cleared. The chgrp command The chgrp command is used to change the group ownership of one or more files or directories. Its general syntax is : chgrp [-Rcfv] groupname foo.txt The flags used here are also the same as those in the chmod command. The changes in ownership are applied to the groupname and the filename specified.

Friday

Oracle Explain Plan

The EXPLAIN PLAN statement displays execution plans chosen by the Oracle optimizer for SELECT, UPDATE, INSERT, and DELETE statements. A statement's execution plan is the sequence of operations Oracle performs to execute the statement. An execution plan defines how Oracle finds or writes the data. The components of execution plans include:

  • An ordering of the tables referenced by the statement
  • An access method for each table mentioned in the statement
  • A join method for tables affected by join operations in the statement

The general syntax of EXPLAIN PLAN is: explain plan for your- sql-statement;

EXPLAIN PLAN output shows how Oracle executes SQL statements. To determine the execution plan Oracle follows to execute a specified SQL statement. This statement inserts a row describing each step of the execution plan into a specified table. EXPLAIN PLAN results alone, however, cannot differentiate between well-tuned statements and those that perform poorly. For example, if EXPLAIN PLAN output shows that a statement uses an index, this does not mean the statement runs efficiently. Sometimes using indexes can be extremely inefficient. It is thus best to use EXPLAIN PLAN to determine an access plan and later prove it is the optimal plan through testing. If you execute an EXPLAIN PLAN, Oracle will analyze the statement and fill a special table with the Execution plan for that statement. Before issuing an EXPLAIN PLAN statement, verify whether PLAN_TABLE exits or create a table to hold its output . Use one of the following approaches:

1)Run the SQL script UTLXPLAN.SQL to create a sample output table called PLAN_TABLE in your schema. The exact name and location of this script depends on your operating system. PLAN_TABLE is the default table into which the EXPLAIN PLAN statement inserts rows describing execution plans.

2)Issue a CREATE TABLE statement to create an output table with any name you choose. When you issue an EXPLAIN PLAN statement you can direct its output to this table.

Any table used to store the output of the EXPLAIN PLAN statement must have the same column names and datatypes as the PLAN_TABLE: CREATE TABLE plan_table (statement_id VARCHAR2(30), timestamp DATE, remarks VARCHAR2(80), operation VARCHAR2(30), options VARCHAR2(30), object_node VARCHAR2(128), object_owner VARCHAR2(30), object_name VARCHAR2(30), object_instance NUMERIC, object_type VARCHAR2(30), optimizer VARCHAR2(255), search_columns NUMERIC, id NUMERIC, parent_id NUMERIC, position NUMERIC, cost NUMERIC, cardinality NUMERIC, bytes NUMERIC, other_tag VARCHAR2(255) other LONG); To issue an EXPLAIN PLAN statement, you must have the privileges necessary to insert rows into an existing output table that you specify to hold the execution plan. To examine the execution plan produced by an EXPLAIN PLAN statement, you must have the privileges necessary to query the output table. EXPLAIN PLAN SET STATEMENT_ID (optional) INTO TABLE_NAME FOR YOUR- SQL-STATEMENT; If you omit the INTO TABLE_NAME clause, Oracle fills a table named PLAN_TABLE by default.

Keywords and Parameters :

SET STATEMENT_ID : Specifies the value of the STATEMENT_ID column for the rows of the execution plan in the output table. You can then use this value to identify these rows among others in the output table. Be sure to specify a STATEMENT_ID value if your output table contains rows from many execution plans. If you omit this clause, the STATEMENT_ID value defaults to null.

INTO : Specifies name of the output table, and optionally its schema and database. This table must exist before you use the EXPLAIN PLAN statement.

FOR statement : Specifies a SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, or CREATE INDEX statement for which the execution plan is generated. Note: If statement includes the parallel_clause, the resulting execution plan will indicate parallel execution. However, EXPLAIN PLAN actually inserts the statement into the plan table, so that the parallel DML statement you submit is no longer the first DML statement in the transaction. This violates the Oracle restriction of one parallel DML statement per transaction, and the statement will be executed serially. To maintain parallel execution of the statements, you must commit or roll back the EXPLAIN PLAN statement, and then submit the parallel DML statement.

The EXPLAIN PLAN statement is a data manipulation language (DML) statement, rather than a data definition language (DDL) statement. Therefore, Oracle does not implicitly commit the changes made by an EXPLAIN PLAN statement. If you want to keep the rows generated by an EXPLAIN PLAN statement in the output table, you must commit the transaction containing the statement. The Plan Table: The plan table is the table that Oracle fills when you issue an execution plan for an SQL statement. You must make sure such a plan table exists. Oracle ships with the script UTLXPLAN.SQL which creates this table, named PLAN_TABLE (which is the default name used by EXPLAIN PLAN.You can also choose any other name for the plan table, as long as you have been granted insert on it and its table structure should be similar to Plan Table.

The PLAN_TABLE used by the EXPLAIN PLAN statement contains the following columns: PLAN_TABLE Columns :

STATEMENT_ID : The value of the optional STATEMENT_ID parameter specified in the EXPLAIN PLAN statement.

TIMESTAMP : The date and time when the EXPLAIN PLAN statement was issued.

REMARKS : Any comment (of up to 80 bytes) you wish to associate with each step of the explained plan. If you need to add or change a remark on any row of the PLAN_TABLE, use the UPDATE statement to modify the rows of the PLAN_TABLE.

OPERATION : The name of the internal operation performed in this step. In the first row generated for a statement, the column contains one of the following values: DELETE STATEMENT ,INSERT STATEMENT ,SELECT STATEMENT ,UPDATE STATEMENT .

OPTIONS : A variation on the operation described in the OPERATION column.

OBJECT_NODE : The name of the database link used to reference the object (a table name or view name). For local queries using parallel execution, this column describes the order in which output from operations is consumed.

OBJECT_OWNER : The name of the user who owns the schema containing the table or index.

OBJECT_NAME : The name of the table or index.

OBJECT_INSTANCE : A number corresponding to the ordinal position of the object as it appears in the original statement. The numbering proceeds from left to right, outer to inner with respect to the original statement text. View expansion will result in unpredictable numbers.

OBJECT_TYPE : A modifier that provides descriptive information about the object; for example, NON-UNIQUE for indexes.

OPTIMIZER : The current mode of the optimizer.

SEARCH_COLUMNS : Not currently used.

ID : A number assigned to each step in the execution plan.

PARENT_ID : The ID of the next execution step that operates on the output of the ID step.

POSITION : The order of processing for steps that all have the same PARENT_ID.

OTHER : Other information that is specific to the execution step that a user may find useful.

OTHER_TAG : Describes the contents of the OTHER column

DISTRIBUTION : Stores the method used to distribute rows from "producer" query servers to "consumer" query servers.

Pstart : The start partition of a range of accessed partitions. It can take one of the following values: n indicates that the start partition has been identified by the SQL compiler and its partition number is given by n. KEY indicates that the start partition will be identified at execution time from partitioning key values. ROW LOCATION indicates that the start partition (same as the stop partition) will be computed at execution time from the location of each record being retrieved. The record location is obtained by a user or from a global index. INVALID indicates that the range of accessed partitions is empty.

Pstop : The stop partition of a range of accessed partitions. It can take one of the following values: n indicates that the stop partition has been identified by the SQL compiler and its partition number is given by n. KEY indicates that the stop partition will be identified at execution time from partitioning key values. ROW LOCATION indicates that the stop partition (same as the start partition) will be computed at execution time from the location of each record being retrieved. The record location is obtained by a user or from a global index. INVALID indicates that the range of accessed partitions is empty.

PID : The step that has computed the pair of values of the Pstart and Pstop columns.

COST : The cost of the operation as estimated by the optimizer's cost-based approach. For statements that use the rule-based approach, this column is null. Cost is not determined for table access operations. The value of this column does not have any particular unit of measurement, it is merely a weighted value used to compare costs of execution plans.

CARDINALITY : The estimate by the cost-based approach of the number of rows accessed by the operation.

BYTES : The estimate by the cost-based approach of the number of bytes accessed by the operation.

Examples

Using the EXPLAIN PLAN Statement:

The following example shows a SQL statement and its corresponding execution plan generated by EXPLAIN PLAN. The sample query retrieves names and related information for employees whose salary is not within any range of the SALGRADE table:

SELECT ename, job, sal, dname FROM emp, dept WHERE emp.deptno = dept.deptno AND NOT EXISTS (SELECT * FROM salgrade WHERE emp.sal BETWEEN lowsal AND highsal);

This EXPLAIN PLAN statement generates an execution plan and places the output in PLAN_TABLE:

EXPLAIN PLAN SET STATEMENT_ID = 'Emp_Sal' FOR SELECT ename, job, sal, dname FROM emp, dept WHERE emp.deptno = dept.deptno AND NOT EXISTS (SELECT * FROM salgrade WHERE emp.sal BETWEEN losal AND hisal);

Selecting PLAN_TABLE Output in Table Format

This SELECT statement:

SELECT operation, options, object_name, id, parent_id, position, cost, cardinality, other_tag, optimizer FROM plan_table WHERE statement_id = 'Emp_Sal' ORDER BY id;

Generates this output:

OPERATION OPTIONS OBJECT_NAME ID PARENT_ID POSITION COST CARDINALITY BYTES OTHER_TAG OPTIMIZER

----------------------------------------------------------------------------------------------- SELECT STATEMENT 0 2 2 1 62

CHOOSE

FILTER 1 0 1

NESTED LOOPS 2 1 1 2 1 62

TABLE ACCESS FULL EMP 3 2 1 1 1 40

ANALYZED TABLE ACCESS FULL DEPT 4 2 2 4 88 ANALYZED TABLE ACCESS FULL SALGRADE 5 1 2 1 1 13 ANALYZED

Thursday

Data Stage Enterprise Edition Server Routines

Routine to Read no of records in a file: ---------------------------------------- Parameters to be passed are Arg1(path),Arg2(file name) Code: ---- vParamFile = Arg1 : "/" : Arg2 vCountVal = 0 OpenSeq vParamFile To FileVar Else Call DSLogWarn("Cannot open ":vParamFile , "Cannot Open ParamFile") End Loop ReadSeq Dummy From FileVar Else Exit ;* at end-of-file vCountVal = vCountVal + 1 Repeat CloseSeq FileVar Ans=vCountVal Return (vCountVal) To send mail: ----------------- Four parameters are to be passed for this routine: Message,Subject,Sendto(Mail id),From (lan id) command = "echo ":Message:" | mail -s ":Subject:" ":SendTo:",":From Call DSExecute("UNIX",command, output, returncode) Ans = returncode To rename the files with timestamp and move files from one directory to another: ----------------------------------------------------------------- $INCLUDE DSINCLUDE JOBCONTROL.H Call DSExecute("UNIX",'mv /path1/path2/filename.txt /newpath/newpath1/filename_`date +"%Y%m%d%H%M%S"`.txt ', Output, SystemReturnCode) if SystemReturnCode <> 0 Then Call DSLogFatal("Unix Command Error", "JobControl") Abort End Else ErrorCode = 0 To connect to db2 database from routine: --------------------------------------- $INCLUDE DSINCLUDE JOBCONTROL.H Call DSExecute("UNIX",'. /export/home/db2inst8/sqllib/db2profile', Output, SystemReturnCode) Call DSExecute("UNIX",'db2 "connect to db2 DSNNAME user USERNAME using PASSWORD"', Output, SystemReturnCode) If SystemReturnCode <> 0 Then Call DSLogFatal("Unix Command Error", "JobControl") Abort End Else ErrorCode = 0 To get record count from a table: --------------------------------------- PgmName = "CountfromTable" * Set default to empty string Ans = "" T_NAME = Oconv(TableName,"ABC") If Len(Trim(T_NAME)) = 0 Then Message = "No Table name supplied... Abort" Call DSLogFatal(Message,PgmName) ErrorCode = @TRUE Goto TheEnd End Continue: * Format SQL to select count(*) from table Ans = "SELECT COUNT(*) FROM schemaname.":Trim(T_NAME) Return = Ans To find a file in a path1 and moving the file to path2 : ------------------------------------------------------------ $INCLUDE DSINCLUDE JOBCONTROL.H Call DSExecute("UNIX",'find /path/path1/':Arg1, Output, SystemReturnCode) if SystemReturnCode <> 0 Then Call DSLogInfo("No Files found for Rename","JobControl") End Else Call DSExecute("UNIX",'mv /path/path1/':Arg1:' /path/path2/':Arg1, Output,SystemReturnCode) if SystemReturnCode <> 0 Then Call DSLogFatal("Unix Command Error","Output is " : Output, "JobControl") Abort End End ErrorCode = 0 Ans = 0