Skip to Main Content
  • Questions
  • Using a environmental variable for system passwords in UNIX scripts

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Chris.

Asked: June 19, 2000 - 11:30 am UTC

Last updated: April 08, 2006 - 11:44 am UTC

Version: generic

Viewed 1000+ times

You Asked

Tom,

What are the best methods you know for passing sensitive database passwords to UNIX shell scripts.

Obviously, I don't want to hard-code them into the scripts. I also don't want to depend on passing manually entered command line arguments, so the scripts can be run in batch or executed by non-DBA operators.

I will appreciate your help.

Chris



and Tom said...

I don't use passwords. If the password is available in some way shape or form for the script to be run by a non-dba -- they'll be able to get to it somehow.

o I would try to move all "scripts" into stored procedures (anything you are doing in the scripts with SQL can be done with SQL in PLSQL). We can grant execute on these processes to individuals. So, instead of having something like:

sqlplus u/p <<EOF
alter tablespace foo begin backup;
exit
EOF

You would code:

sqlplus / <<EOF
execute begin_foo_backup;
exit
EOF

where begin_foo_backup is a procedure owned by someone who has the privilege to put a tablespace into backup mode and uses dynamic sql to issue the alter (either dbms_sql in 8.0 and before or execute immediate 'alter tablespace foo begin backup'; in 8i and up)

o I would use OS authentication to log in (eg: if I am logged in in Unix -- i can be logged into the database without using a username or password).

o I would grant execute on the stored procedures to the appropriate people. This allows me to say "user1" can run a, b, c and "user2" can run b, c, d.

Now, they can run the scripts -- the scripts log in as that user -- we can find out who ran what (and when if need be) and so on. No passwords anywhere.

When I say above "I would" -- I really me "I do".

Rating

  (15 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Shell variable in Sql scripts

sam, March 04, 2002 - 6:12 pm UTC

Hi Tom,
Is there any way we can use a value in shell variable in sql where clause ?.
Thanks,
Sam

Tom Kyte
March 04, 2002 - 8:04 pm UTC

yup.

$ cat t.sh
#!/bin/csh -f

sqlplus / <<eof

select '$HOME' from dual;

eof

$ ./t.sh

SQL*Plus: Release 8.1.7.0.0 - Production on Mon Mar 4 20:03:41 2002

(c) Copyright 2000 Oracle Corporation.  All rights reserved.


Connected to:
Oracle8i Enterprise Edition Release 8.1.7.2.0 - Production
With the Partitioning option
JServer Release 8.1.7.2.0 - Production

ops$tkyte@ORA817DEV.US.ORACLE.COM> ops$tkyte@ORA817DEV.US.ORACLE.COM> 
'/EXPORT/HOME/TKYT<b>
------------------
/export/home/tkyte</b>

ops$tkyte@ORA817DEV.US.ORACLE.COM> ops$tkyte@ORA817DEV.US.ORACLE.COM> Disconnected from Oracle8i Enterprise Edition Release 8.1.7.2.0 - Production
With the Partitioning option
JServer Release 8.1.7.2.0 - Production 

Unable to use externally identified users

Simon Holt, March 05, 2002 - 5:46 am UTC

My current assignment has a number of scripts which I am trying to tidy up and secure, so this was a very interesting question.

However, a recntly completed security review from the IT security dept. highlighted the "risk" of using os authenticated oracle users, and have recommended we remove any that exist! Given that this is the case, would you have any suggestions for wrapping up passwords somehow?

For example, there is a batch job which is kicked off by a unix script in the following style:

mybatchscript <password> <logfile_destination>

and this runs via cron - so anyone who can read this users crontab file can see the password. Also, if anyone happens to log in whilst the job is running (they would have to be pretty determined, since it runs at 4am ;-) ) would be able to grab the password from a ps listing.

I was considering putting the password in a file, making it read only for the user, and then amending the script to obtain the password from the file - at the moment I can't think of another way around this, which is a pity since someone will have to maintain this when the password is forced to change...

Tom Kyte
March 05, 2002 - 8:44 am UTC

Ask them WHY.

It is true if you use OS authenticated users over a network in a Unix environment (forcing you to set "remote_os_authent=true") that is a security hole.

The highest level of security you can have is using OS authenticated accounts on a host based system (no network).

Your security folks are badly mistaken if they "outlaw" OS authenticated accounts that use direct connections. They are just about the most secure way to do this. It is ONLY true that there are problems if you set the init.ora parameter remote_os_authent=true -- which is something you should do only in an environment where you control ALL of the machines on the entire network.

Using OS authenticated accounts is safe, much much safer then what they propose.


As for your example, they would not have to be determined, they would just have to write their own script to do a "ps >> mylog.txt" using cron themselves or just write a script:

while(1)
ps >> mylog.txt
sleep 20
end

leave that running and go home. Then use grep later to mine the data.


USE OS AUTHENTICATED ACCOUNTS. They are 100% secure (more secure then even identifiedy by accounts. Do NOT use them over the network in Unix as they are based on "trust" (just like the r* commands rsh, rexec)

script to prompt with the instances names ...

reader, April 01, 2003 - 7:17 am UTC

Tom,

i have a solaris server, where in i have 3 instances of oracle running. what i need from you is ....

when i login to the server, i need to be prompted with the instances of oracle that is running with numbering.when i choose to go into 3rd instance out of 1 or 2 or 3. i would type 3 and when i log in, i should be with the profile of the 3rd instance.

how should i set that shell script in the server. i have seen that functioality in one of the server.

do your site has the link to that script.?

thanks for your time,


Tom Kyte
April 01, 2003 - 7:58 am UTC

it is coraenv.

it should be installed already in /usr/local/bin or where ever the people installing said to put it.

it reads the /etc/oratab to see the instances.

coraenv

reader, April 01, 2003 - 8:33 am UTC

tom,

its not running... sorry i dont know how to run it. it says ./coraenv[33]: syntax error at line 39 : `set' unexpected

i tried in different servers.. but same error. the files are exactly the same in different servers. is it like we need to run it with setting anything ?

Thanks ,.



Tom Kyte
April 01, 2003 - 8:39 am UTC

$ csh coraenv

its a c-shell

coraenv

reader, April 01, 2003 - 9:44 am UTC

tom.

sorry, for my ignorance. i ran the script and it asks me this ...

$ csh coraenv
ORACLE_SID = [DEVEENGC] ? y
ORACLE_HOME = [/u01/app/oracle/product/8.1.5] ? y
$

if i say no also it says samething...

$ csh coraenv
ORACLE_SID = [DEVEENGC] ? n
ORACLE_HOME = [/u01/app/oracle/product/8.1.5] ? n

please let me know ... where/what i am missing.. should i have to add anything in profile?

thanks a lot... tom.

Tom Kyte
April 01, 2003 - 4:22 pm UTC

well, coraenv (or oraenv) just set the environment.

they prompt you for an oracle sid and then use dbhome to find the oracle home and set the oracle_home/oracle_sid for you.


read below, you can script something like that.

Choosing a instance when logging in

A reader, April 01, 2003 - 12:59 pm UTC

We have a script called oramenu which lets us choose the instance we want when we log in. We put it under /var/opt/oracle on Solaris 8.

We have a line in /etc/profile to invoke oramenu at login.

. /var/opt/oracle/oramenu

Here is the oramenu script

OPTION=0
VALID=0
while [ $VALID -ne 1 ] ; do
VALID=1
echo "\n Please choose an option\n"
echo " 1. DVLP database (11.5.2)"
echo " 2. TEST database (11.5.6)"
echo "\n Enter a number (E to exit): \c"
read OPTION
case $OPTION in
1) . /u02/app/applmgr/dvlpappl/APPSORA.env;;
2) . /u02/app/applmgr/testappl/APPSORA.env;;
e|E) exit;;
*) VALID=0 ;;
esac;
done
export PS1="|`uname -n`|$LOGNAME|$TWO_TASK> "

hey ! ...Thats great, .. thank you

Reader, April 02, 2003 - 12:36 am UTC




OS Authentication for Transportable Tablespace Exp/Imp

Lisa, May 05, 2004 - 3:23 pm UTC

Tom,

I want to be able to write a UNIX script that will automate the process of exp/imp for transportable tablespaces. This is one of the rare situations in which you have to invoke exp/imp as sysdba.

I am unable to come up with a shell trick to hide the password for an 'as sysdba' exp login (e.g. echo passwd | sqlplus trick)

I see that you recommend using OS Authentication for automated scripts. However, in order to use OS Authentication for a sysdba login, does that mean that I won't be able to database password file authentication for sysdba logins? It seems that the two are mutually-exclusive since REMOTE_LOGIN_PASSWORDFILE must be set to NONE or EXCLUSIVE, respectively.

I don't want to limit my ability to administrate this database remotely via a SQL*Net connection, but I need to script this exp/imp routine.

Please help. Am I missing something?



Tom Kyte
May 05, 2004 - 3:29 pm UTC

no, you can do OS and remote sysdba authentication simultaneously.

Whats in a Role

Brian, May 10, 2004 - 8:38 am UTC

The only thing different than the response given, is creating roles. Granting execute privs to run procedures, is no different than the table grants themselves. If user1 can run A B C, then likely he has as well the grants for the tables involved in A B C as well. Add the execute priv on the procedure to the associated role the user has. It requires alot less maintenance.

Tom Kyte
May 10, 2004 - 8:48 am UTC

<quote>
If user1 can run A B C, then likely he has as well the grants for
the tables involved in A B C as well.
</quote>

really? not in my experience. we use procedures for the exact reason that we don't want to grant access to the base tables.

how to use os authent user for exporting

Vipin, December 07, 2004 - 11:10 am UTC

Tom ,
I got ur points about os authentication being a secured way of connecting to the database.
I have created a user to be authenticated externally. I can easily connect to sqlplus using only the userid as long as i m connected as the same os user . But when i tried to invoke exp(or imp) , it always ask for a password . Probably i m not using the right syntax - Can you please tell what should be the right syntax if we want to use exp from an os authent user so that exp will not ask for the password .

Thanks for your help as always.

Tom Kyte
December 07, 2004 - 8:16 pm UTC

you just use "/"

[tkyte@localhost tkyte]$ exp userid=/ tables=t

Export: Release 10.1.0.3.0 - Production on Tue Dec 7 20:14:57 2004

Copyright (c) 1982, 2004, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.3.0 - Production
With the Partitioning, OLAP and Data Mining options
Export done in WE8ISO8859P1 character set and AL16UTF16 NCHAR character set

About to export specified tables via Conventional Path ...
. . exporting table T 0 rows exported
Export terminated successfully without warnings.
[tkyte@localhost tkyte]$


Thanks Tom......

A reader, December 08, 2004 - 2:46 pm UTC


Different ways of authentication

Ashok, February 14, 2005 - 1:28 am UTC

Hi Tom,

You have described about OS authentication which I have been able to understand to some extent. But I am still not very clear about what all authentication methods exists. Which authentication method should be used in which scenerio.

Can you please explain me the following :

1. Like OS Authentication, which other authentication methods exist and which authentication method is best suitable for which scenerio?

2. I understand that there can be 2 types of users.
i) One who is directly logged into the server (where the database is installed (may be a DBA or a simple database user).

ii) One who is not logged directly logged into the server but connects through net8 (sqlplus)(may be a DBA or a simple database user).

What are the different ways that these users can connect to oracle.

3. Where does password file authentication fits in ?
4. I heard something about OPS$ accounts? What are these?

I am actually a little confused about what are the different ways of authentication, when should these be used, how to setup these authentication methods.

Your help is required to help clear these doudbts.

Many Thanks in advance.


Tom Kyte
February 14, 2005 - 8:33 am UTC

if you want to know everything about it -- suggest you look at the Oracle10g security book in the "links I like" -- David Knox covers it in great detail.

and as always, the >>>> concepts guide <<<< is a great place to peek 

http://docs.oracle.com/docs/cd/B10501_01/server.920/a96524/c23acces.htm#CNCPT323

it actually answers #1.

#2 is not "accurate".  there are users - period.  how the connect (over network or fork/exec) is not really relevant.  they are just USERS

#3 is all about "as sysdba" and "as sysoper" -- we can use a password file for remote (over the network) authentications when the database is not up and running.  so when you "connect scott/tiger as sysdba", Oracle can verify scott has been granted sysdate and uses the password tiger by using this password file.  otherwise, it is not used.  


#4 -- ops$ is OPerating System $
              ^^        ^

it is the typical prefix we use for ops$ accounts -- operating system identified, OS authenticated accounts.



 

Authentication methods in Oracle

Ashok Khatri, February 16, 2005 - 5:19 am UTC

Hi Tom,
Thanks a lot for the information. I went through a couple of documentations and tried to clear my doudbts about various authentication methods. I still have a couple of clarifications and would like your assistance to clear these. The answers would be brief and not much time taking (though the questions look lengthy) :


1. There are 2 methods of connecting to a database?
a) Authentication through database when the database is up & running
b) Authentication through OS (local or remote users) when using "as sysdba".

2. In order to connect through OS authentication localy, you need to connect like
sqlplus> connect / as sysdba

3. In order to connect locally as sysdba, you need to be logged on to the server as a user who is part of the SYSDBA or SYSOPER group (on unix the dba group).

4. Where will I be attempted to be authenticated if I use
sqlplus> connect /

5. REMOTE_AUTHENT & REMOTE_LOGIN_PASSWORDFILE parameters have no effect on connecting to database locally. Meaning, whatever the values of these parameters, if I am a member of the "dba" group, I can always connect as
sqlplus > connect / as sysdba

6. REMOTE_AUTHENT & REMOTE_LOGIN_PASSWORDFILE parameters, as the name suggests, effects only the remote users (those who attempt to connect "as sysdba" or "as sysoper" from remote machines).

7. We can specify both parameters REMOTE_AUTHENT=TRUE|FALSE & REMOTE_LOGIN_PASSWORDFILE=NONE|EXCLUSIVE|SHARED in the init.ora file?

8. REMOTE_AUTHENT=FALSE means users connecting remotely (as "sysdba" or "sysoper") shall NOT be OS authentication? I am not sure how they will be able to connect remotely without specifying a connect string?

9. REMOTE_AUTHENT=TRUE means users connecting remotely (as "sysdba" or "sysoper") shall be OS authentication? But, I am not sure how they will be able to connect remotely without specifying a connect string?

sqlplus> ????

10. REMOTE_LOGIN_PASSWORDFILE=NONE means users connecting remotely (as "sysdba" or "sysoper") will NOT be authenticated via the password file?

11. REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE means users connecting remotely (as "sysdba" or "sysoper") will be authenticated via the password file? user SYS as well as other users with sysdba or sysoper priviledges can be authenticated via the passwordfile?

Something like ?
sqlplus> connect scott/tiger@server1 as sysdba

12. For (11), should the user (scott in this case) also have a unix account on the database server machine and also be a part of the dba group?

13. what if I specify REMOTE_AUTHENT=TRUE and REMOTE_LOGIN_PASSWORDFILE=NONE?

15. If you are a remote user and is connecting (as "sysdba" or "sysoper") over a non secured network (tcp/ip), you can only be identified by a passwordfile?


Thanks in advance
Ashok Khatri














Hidding Password

Lena, April 08, 2006 - 10:39 am UTC

Hi Tom,

From above:
"You would code:
sqlplus / <<EOF
execute begin_foo_backup;
exit
EOF
where begin_foo_backup is a procedure owned by someone.."

This work greate for me.
But suppose i need to: select * from emp and put the result in emp.lst file, how this could be done using the above method ?
Thanks.

Tom Kyte
April 08, 2006 - 11:00 am UTC

sqlplus /<<EOF
spool emp.lst
select * from emp;
spool off
exit
EOF



Hidding Password

Lena, April 08, 2006 - 11:29 am UTC

Hi Tom,
Im sorry that i didnt explain my self properly.
Suppose i want to use your approch and use a procedure that return the result set (using ref cursor or whatever) to unix file, how it can be done ?

Thanks and sorry again.


Tom Kyte
April 08, 2006 - 11:44 am UTC

...<<EOF
variable x refcursor
set autoprint off
exec procedure(:x);
spool emp.lst
print x
spool off
exit
EOF


just like you would with or without the shell script - you can put any sqlplus script in there you want (well, avoid ones that expect user input of course - they'll just "hang" if you run them in the background)

More to Explore

Backup/Recovery

Check out the complete guide to all of the Backup & Recovery techniques in the Oracle Database.