title: Calling shell script from PL/SQL, but shell gets executed as grid user, not oracle
body: <p>I am trying to execute a shell script from inside the Oracle database using Runtime.getRuntime().exec. </p>
  
  <p>Oracle 11.2.0.4 EE running on Red Hat 5.5</p>
  
  <hr>
  
  <pre><code>CREATE OR REPLACE procedure pr_executa_host(p_cmd varchar2)
      as language java name 'Util.RunThis(java.lang.String)';
  /
  </code></pre>
  
  <hr>
  
  <pre><code>public class Util extends Object
  {
  
    public static int RunThis(java.lang.String args)
    {
    Runtime rt = Runtime.getRuntime();
    int        rc = -1;
  
    try
    {
       Process p = rt.exec(args);
  
       int bufSize = 4096;
       BufferedInputStream bis =
        new BufferedInputStream(p.getInputStream(), bufSize);
       int len;
       byte buffer[] = new byte[bufSize];
  
       // Echo back what the program spit out
       while ((len = bis.read(buffer, 0, bufSize)) != -1)
          System.out.write(buffer, 0, len);
  
       rc = p.waitFor();
    }
    catch (Exception e)
    {
       e.printStackTrace();
       rc = -1;
    }
    finally
    {
       return rc;
    }
    }
  }
  /
  </code></pre>
  
  <hr>
  
  <p>The permissions granted on java to db user SCOTT:</p>
  
  <hr>
  
  <pre><code>kind    grantee    type    name    action
  
  GRANT    SCOTT    java.io.FilePermission    /webstart/mn500/*    readFileDescriptor
  GRANT    SCOTT    java.io.FilePermission    /webstart/mn500/*    read,write,execute
  GRANT    SCOTT    java.io.FilePermission    /webstart/mn500/*    writeFileDescriptor
  GRANT    SCOTT    java.io.FilePermission  /webstart/mn500/CONCLUIDO/MN457560/executa.sh execute
  GRANT    SCOTT    java.lang.RuntimePermission    *    writeFileDescriptor
  GRANT    SCOTT    java.lang.RuntimePermission /webstart/mn500/CONCLUIDO/MN457560/executa.sh    execute
  </code></pre>
  
  <hr>
  
  <p>The shell script executa.sh, which is the one I'm trying to execute:</p>
  
  <hr>
  
  <pre><code>#!/bin/sh
  echo i am `/usr/bin/whoami`
  echo environment `/bin/env`
  /bin/date&gt;&gt;/webstart/mn500/CONCLUIDO/MN457560/test.txt
  </code></pre>
  
  <hr>
  
  <p>The permissions on the directory:</p>
  
  <hr>
  
  <pre><code>p08[oracle] $ ls -larth /webstart/mn500/CONCLUIDO/MN457560
  -rw-r--r--   1 oracle oinstall    1 Jul 29 12:03 test.txt
  -rwxr-xr-x   1 oracle orafiles  430 Jul 29 12:04 executa.sh
  drwxr-xr-x   2 oracle orafiles 4.0K Jul 29 12:04 .
  </code></pre>
  
  <hr>
  
  <p>The thing is, when I execute the procedure pr_executa_host, it runs the shell script as grid os 
  user, not oracle! (although it keeps oracle environment variables, like it did a 'su grid -m' 
  before executing the shell script)</p>
  
  <p>Since grid doesn't have write privileges on neither the directory, nor the file, the script doesn't 
  do anything, the test file stays unaltered. Take a look:</p>
  
  <hr>
  
  <pre><code>begin
    dbms_java.set_output(1000000);
    pr_executa_host('/webstart/mn500/CONCLUIDO/MN457560/executa.sh');
    dbms_lock.sleep(2);
  end;
  /
  
  i am grid
  environment HOSTNAME=p08.XXXXXXXXXXXX.com.br SHELL=/bin/bash TERM=xterm HISTSIZE=1000 
  SSH_CLIENT=10.141.112.28 56029 22 NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252 QTDIR=/usr/lib64/qt-3.3 
  QTINC=/usr/lib64/qt-3.3/include SSH_TTY=/dev/pts/0 USER=oracle 
  LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;0
  1:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01
  ;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*
  .Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31
  :*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7
  z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01
  ;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;3
  5:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*
  .mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=
  01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35
  :*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.a
  ac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=
  01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36: ORACLE_SID=sigepshm 
  ORACLE_BASE=/oracle ORACLE_HOSTNAME=P08 PATH= MAIL=/var/spool/mail/oracle 
  TNS_ADMIN=/grid/product/11.2.0/grid/network/admin PWD=/oracle/product/11.2.0/db/dbs 
  KDE_IS_PRELINKED=1 LANG=en_US.UTF-8 ORA_NET2_DESC=27,30 KDEDIRS=/usr ORACLE_TERM=xterm 
  ORACLE_SPAWNED_PROCESS=1 HISTCONTROL=ignoredups SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass 
  HOME=/home/oracle SHLVL=2 GRID_HOME=/oracle/product/11.2.0/grid LOGNAME=oracle CVS_RSH=ssh 
  QTLIB=/usr/lib64/qt-3.3/lib SSH_CONNECTION=10.141.112.28 56029 10.147.0.8 22 
  CLASSPATH=/oracle/product/11.2.0/db/JRE:/oracle/product/11.2.0/db/jlib:/oracle/product/11.2.0/db/rdb
  ms/jlib LESSOPEN=|/usr/bin/lesspipe.sh %s DISPLAY=localhost:10.0 
  ORACLE_HOME=/oracle/product/11.2.0/db G_BROKEN_FILENAMES=1 _=/bin/env
  </code></pre>
  
  <hr>
  
  <p>Why is the java inside the database calling unix commands as grid user, not oracle?</p>
  
  <p>Thanks a lot for your help,
  Stolf</p>