Sourcing Env file with eval works with ksh but not BASH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sourcing Env file with eval works with ksh but not BASH
# 22  
Old 09-10-2015
Quote:
Originally Posted by Corona688
I suspect it's paths and passwords for a database, and that the shell script in question just uses the database and doesn't give the user a shell prompt to play with.
Hi Corona688,
I think you're missing the point.

You (Corona688) are writing a shell script that you (or other people) will run to access a database. Your database administrator (waavman) wants to create a file that you can't read that will contain the paths and passwords to the database you are trying to update with your script. If people other than you are going to run your script, they need to be able to read your script. So, if they are going to run your script they can also copy it and make changes to it and run their own copy of your script.

Some of us don't see what advantage waavman gains from keeping you from reading the file containing this data since you (or anyone else running your script) can change your script and see the hidden paths and passwords being used by your script anytime you (they) want.

We see the need to restrict reading that file to the legitimate users of that database. That can easily be handled by creating a group with members being the list of users who need to access that database. Then assign that group as the group ID of the file and make the file mode 640 with the database owner being the owner ID. No need for set-UID or set-GID code and no need for sudo; same security.
# 23  
Old 09-10-2015
Quote:
Originally Posted by Don Cragun
Hi Corona688,
I think you're missing the point.

You (Corona688) are writing a shell script that you (or other people) will run to access a database. Your database administrator (waavman) wants to create a file that you can't read that will contain the paths and passwords to the database you are trying to update with your script. If people other than you are going to run your script, they need to be able to read your script. So, if they are going to run your script they can also copy it and make changes to it and run their own copy of your script.
Which is why I spent the last 4 pages or so suggesting a sudo-based solution to isolate the env file and the script from its users permission-wise instead of letting him rube-goldberg it.
# 24  
Old 09-11-2015
This Response is to both Don and Corona.
First to Don's response that I set the envfile to 640 and make only people who can see the envfile be part of the owner's group.
The requirement at my place is slightly different. There are about 25 support folks who will be running several scripts that source this envfile daily. They should be able to run these scripts without any issues. These scripts do several things in the background like connect to Database, connect to informatica server, run ETL workflows write to database etc. However none of the these 25 support personnel should be able to login to the database and do any manual updates. Their job is just running the scripts if any of them have failed or stopped for some reason.
Only the admin and a small group of users who code the scripts and DB stored procs and ETL workflows, should be able to view the envfile.txt since they would have to login to the database and perform DML and DDL operations. This is where the requirement of 25 users being able to source the envfile.txt despite not having read access to it comes up.

Next part of this response is directed to Corona.

Corona,

He is an extract from my previous post 3 days ago where in I tried the set -x option on Darwin OS 10.8

Quote:
Now when I invoked this script by 'otheruser' I got the following output which shows that even though the admin script got execute fine, envfile.txt contents are hidden as sudo does not inherit -x oiption


Code:
$./call_adminusers_restrictedsudocommand.ksh
+ sudo /Users/admin/test_xtrace_withsudoaccess.ksh
I am referring to my post 3 days ago in which I tested that sudo does not inherit set -x option. That is fine. But i noticed another problem.
As you can see from my previous post above, the script invoked by 'otheruser' which calls the admin users's script with sudo is call_adminusers_restrictedsudocommand.ksh . While I got overwhelmed with trying to verify that set -x is not inherited by Sudo, I forgot to confirm at that time that sourcing the env file using sudo call to the admin script DOES actually propagate the environment variable exported inside the envfile.txt to the 'otheruser' script. When i checked now it actually DOES NOT propagate. Am i missing something here ?

Here is a demo:

Contents of Env file testenvfile.txt that exports env variable ADMINHOMEDIR
Code:
$ cat testenvfile.txt 
export ADMINHOMEDIR="/Users/admin"

Admin user's script which sources the environment file and to which sudo access is added in the /etc/sudoers file so that 'otheruser' can run it through sudo

Code:
$ cat test_xtrace_withsudoaccess.ksh 
#!/bin/ksh
. /Users/admin/testenvfile.txt

'Otheruser' script which calls above admin script with sudo and tries to
print the value of env variable ADMINHOMEDIR

Code:
$ cat call_adminusers_restrictedsudocommand.ksh 
#!/bin/sh
set -x
sudo /Users/admin/test_xtrace_withsudoaccess.ksh
echo "ADMINHOMEDIR============$ADMINHOMEDIR"

Output of running above script shows that ADMINHOMEDIR is not set for some reason

Code:
$./call_adminusers_restrictedsudocommand.ksh
+ sudo /Users/admin/test_xtrace_withsudoaccess.ksh
+ echo ADMINHOMEDIR============
ADMINHOMEDIR============

So why is the environment variable set by admin script test_xtrace_withsudoaccess.ksh not available inside the 'otheruser' script that calls it through sudo ? Am i missing something here ?

thanks
waavman
# 25  
Old 09-12-2015
Quote:
Originally Posted by waavman
This Response is to both Don and Corona.
First to Don's response that I set the envfile to 640 and make only people who can see the envfile be part of the owner's group.
The requirement at my place is slightly different. There are about 25 support folks who will be running several scripts that source this envfile daily. They should be able to run these scripts without any issues. These scripts do several things in the background like connect to Database, connect to informatica server, run ETL workflows write to database etc. However none of the these 25 support personnel should be able to login to the database and do any manual updates. Their job is just running the scripts if any of them have failed or stopped for some reason.
Only the admin and a small group of users who code the scripts and DB stored procs and ETL workflows, should be able to view the envfile.txt since they would have to login to the database and perform DML and DDL operations. This is where the requirement of 25 users being able to source the envfile.txt despite not having read access to it comes up.
OK. That rules out my suggestion.
Quote:
Next part of this response is directed to Corona.

Corona,

He is an extract from my previous post 3 days ago where in I tried the set -x option on Darwin OS 10.8



I am referring to my post 3 days ago in which I tested that sudo does not inherit set -x option. That is fine. But i noticed another problem.
As you can see from my previous post above, the script invoked by 'otheruser' which calls the admin users's script with sudo is call_adminusers_restrictedsudocommand.ksh . While I got overwhelmed with trying to verify that set -x is not inherited by Sudo, I forgot to confirm at that time that sourcing the env file using sudo call to the admin script DOES actually propagate the environment variable exported inside the envfile.txt to the 'otheruser' script. When i checked now it actually DOES NOT propagate. Am i missing something here ?

... ... ...

So why is the environment variable set by admin script test_xtrace_withsudoaccess.ksh not available inside the 'otheruser' script that calls it through sudo ? Am i missing something here ?

thanks
waavman
As with any other command that you run as a separate process, sudo creates a separate shell execution environment. When sudo exits, that execution environment disappears and does not have any effect on the current shell execution environment.

If you're going to use sudo, the script that you invoke with sudo needs to handle entire database transactions (login, perform transaction, print any results that need to be returned to the rest of your script); it can't just set environment variables, unless you're willing to let unprivileged users see the all of the variables needed to access and modify the database.
# 26  
Old 09-14-2015
Hi Don,

Yes granting sudo access to the entire script would be the right solution. But as I had mentioned in thread #11 of this post this would not be practical in my case because we have around 300 scripts which source the env file. So this would mean granting sudo access in the /etc/sudoers file for each of 300 scripts to be run as masteraccount.
So that I why I tried if just granting sudo access to a single script that just sources env file would work. But that Does not seem to work. So I guess for now I will have to live with the eval `setuid perlscript option`

thanks
# 27  
Old 09-14-2015
Quote:
Originally Posted by waavman
This is where the requirement of 25 users being able to source the envfile.txt despite not having read access to it comes up.

Next part of this response is directed to Corona.

Corona,

I am referring to my post 3 days ago in which I tested that sudo does not inherit set -x option. That is fine. But i noticed another problem.
As you can see from my previous post above, the script invoked by 'otheruser' which calls the admin users's script with sudo is call_adminusers_restrictedsudocommand.ksh . While I got overwhelmed with trying to verify that set -x is not inherited by Sudo, I forgot to confirm at that time that sourcing the env file using sudo call to the admin script DOES actually propagate the environment variable exported inside the envfile.txt to the 'otheruser' script.

When i checked now it actually DOES NOT propagate. Am i missing something here ?
You must have changed how you ran it.

The correct procedure is sudo -> script, script sources file, script uses variables, script quits and takes everything with it, leaving the variables unavailable to the user and hence fully protected.

The variables are only available inside 'script'. That is the entire point. If they ended up in the user's environment, the user could just take them, so, you can't do that.
# 28  
Old 09-14-2015
Quote:
Originally Posted by waavman
Hi Don,

Yes granting sudo access to the entire script would be the right solution. But as I had mentioned in thread #11 of this post this would not be practical in my case because we have around 300 scripts which source the env file. So this would mean granting sudo access in the /etc/sudoers file for each of 300 scripts to be run as masteraccount. So that I why I tried if just granting sudo access to a single script that just sources env file would work.
Environment files don't work that way. If they did, that solution would be no good, because by definition it would be giving access to your users. You must not run these scripts as a user they control.

Quote:
But that Does not seem to work. So I guess for now I will have to live with the eval `setuid perlscript option`
It's not an "option". It's a screen door, painted safety orange in the hope that hackers will overlook it because it's just too obvious. It is dangerous and irresponsible.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sourcing file from parent directory bash

"Debian 9 64x - LXDE" I try to source a file from my parent directory: #!/bin/bash #source.bash . ../links.bash but i get "file not found". I tried . "../links.bash" and . '../links.bash'. I got on all methods the same result. If i use the absolute path it works, but i don't want to... (4 Replies)
Discussion started by: int3g3r
4 Replies

2. UNIX for Advanced & Expert Users

Dot sourcing differences in ksh, AIX vs Linux vs Solaris

Why does dot sourcing of ksh functions behave so differently between AIX, Solaris, and Linux? How can I make Linux behave the way I want in the test I show below? I have a library of interdependent functions I have developed and use in ksh in AIX. They also run in Solaris. Now I am migrating... (9 Replies)
Discussion started by: charles_n_may
9 Replies

3. Shell Programming and Scripting

Sourcing .cshrc (C shell) environment variables to bash

I have tried with the following: csh -c 'source ~/.cshrc; exec bash' # works perfectly (cat ~/.cshrc; echo exec bash) | csh # not working And, using sed, I successfully retrieved the environment variables from ~/.cshrc sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc but now... (6 Replies)
Discussion started by: royalibrahim
6 Replies

4. Shell Programming and Scripting

Setting up env variable in ksh

I am facing a very strange issue. I have script in ksh with #!/bin/ksh as shebang. This script has function which sets the env variable before running other functions of the script. by set_up_env() { CONFIG_FILE="/opt/app/tools/deepmarking/latestVersion/script/UploadEnv" if then ... (7 Replies)
Discussion started by: Tuxidow
7 Replies

5. UNIX for Dummies Questions & Answers

[solved] Where & what bash env file, Mac OS?

Hi! I wanted to simplify my bash prompt, so I edited my etc/bashrc file. I thought this was the file that would override any other env files. When I opened it, I saw that the way it was setup was not what my prompt looked like, although I forget exactly what was there. But i edited it the way I... (1 Reply)
Discussion started by: sudon't
1 Replies

6. Shell Programming and Scripting

Eval Tricky Manipulation of Arry in KSH - Help

Hi, Could any one share the intelligence to track this problem. I have any array BT_META_36 and it prints properly with contents of array. # print "BT_META_36=${BT_META_36}" # BT_META_36=cab3,cab4:HDS:052,07A cab3,cab4:HDS:052,07A Now I have a BT_META_36 assigned to a variable.... (0 Replies)
Discussion started by: ajilesh
0 Replies

7. Shell Programming and Scripting

KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions! I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am... (4 Replies)
Discussion started by: tksol
4 Replies

8. UNIX for Advanced & Expert Users

Ksh - Env. Variables ??

Hey all, I have been using Ksh and in that I am setting Environment variables. To set Env. Variables I have created my own file "BuildScript.sh" in which i have written : export CLASSPATH=/somedir/some other dir/file:. export PATH=/some dir/file:. But when i am calling this... (4 Replies)
Discussion started by: varungupta
4 Replies

9. Shell Programming and Scripting

eval in bash

hi everyone i've been reading learning the bash and there is somrthing i don;t understand what does eval do i know that it run a command or script twice but i don;t see in what for cases i can use this could somebody explain this to me (3 Replies)
Discussion started by: jetfreggel
3 Replies

10. UNIX for Dummies Questions & Answers

script sourcing problem (ksh)

I have a script "abc.sh" in /tmp which has exit 0 as its last line when I run this script from /tmp/xyz/def.sh script as . ../abc.sh then the script executes but the control doesn't return to def.sh script for subsequent commands in def.sh but if I invoke the abc.sh from inside the... (3 Replies)
Discussion started by: rakeshou
3 Replies
Login or Register to Ask a Question