Run shell script as different user


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Run shell script as different user
# 1  
Old 04-16-2005
Run shell script as different user

I want to start off by saying thanks to everyone here, you're answers and suggestions are always very helpful.

I have a shell script which is invoked when an email is received. This shell script extracts any attachments that are sent with this email, and then runs a script which submits these files to a repository. The problem is that when the script is envoked, "daemon" is the owner of the files that are created, so the ci and co functions will not work properly. I'm trying to find a way where I can change the owner of the process when the script is envoked.

For example, when the script is envoked, the owner is "daemon", but I need to change the owner to "myUserName", so that when the files are created and the other scripts/commands are called, the system will think that "myUserName" is the person running all of the commands/scripts.

Is this at all possible?
# 2  
Old 04-16-2005
Depending on which OS, there are various more secure methods that what I'll suggest. Which OS?

Have you tried setting the group to be the default group of the daemon user, setting the owner to be the user you want to execute your ci/co operations and then making it a setuid (chmod 4550 <script>)?

Keith
# 3  
Old 04-19-2005
I am running on SUN/OS. I have not tried what you mentioned, really not sure how to do it. When I said that the when the script is run with "daemon" being the owner, daemon is the root.

So lets say I send an email with an attachment, and the script is invoked. The file is decoded and and saved to the current directory, then I try to submit it to the repository. Lets say I log on to the unix account to which the email was sent to, if I look at the file (ls -l), I'm not the owner of the file, the owner of the file is daemon, which is the root and is considered "other". I'm almost certain that this is what is causing the ci and co operations to fail.

So.......I need to figure out how to get the script to be invoked, and run as if the owner of the process is another account, even just my account, anything other than "daemon"!

I know when you do chmod there are 3 numbers like chmod 755. You said chmod 4550.....what is the 0 for?

Thanks for the input.

Last edited by mskarica; 04-20-2005 at 12:27 PM..
# 4  
Old 04-21-2005
Ok, I think I should rephrase my question. I did some more research on daemons, and I think I can ask my question better now.

A daemon is a process that runs in the background (like a fork), whithout user interaction (from what I read). So basically what happens is that when an email is received, and the shell script is invoked, a daemon is running the entire process. I need to find a way where I can make the daemon act as if it were a unix user so that the process is run as if the user is the one who is invoking the scripts that are called in the main script, rather than the daemon.

So my basic idea is: email is received, forwarded to a script (daemon is still the owner at the moment), in the main script first thing I want to do is change the user from the deamon to another valid unix account, so then everything that is performed from there on out is performed as if the unix user that I specify is the one who invoked everything.

Does that make more sense? Any ideas?

Thanks

Last edited by mskarica; 04-21-2005 at 12:30 PM..
# 5  
Old 04-27-2005
Do you know the name of the userid that you want to change to inside the script? If so, you can do a `chown userid` inside the script once the file has been placed in that userid's directory.
# 6  
Old 04-27-2005
It's fustrating to provide solutions which are rejected without even being tried. Smilie

chmod always uses 4 digits not 3. But the leading digit is usually zero so it looks like 3 digits. That leading 4 will turn on the suid bit and probably solve your problem provided you also follow the rest of kduffin's advice.
# 7  
Old 04-29-2005
sorry perderabo, its not that I was rejecting what kduffin said, it was really that I didn't know what the 4 meant, so I didn't know what I was doing. I did some research afterwards and I saw that the 4 was setuid, to make the script run as the owner, rather than as the daemon.

I believe this is exactly what I need, at least from everything I have read (chmod 4755). I did try it, but now what happens is that when I send an email to the account, I get an email back saying that access to the script was denied. I'm thinking that maybe setuid is disabled on our unix system, since I also read that it could be disabled because of security reasons. So my next step is to ask the sys admin if this is actually the case or not.

What I still dont understand is:
- set the group to be the default group?
- set the owner to the user I want to execute the ci/co functions (would this be the same as doing 'chown' inside the script like chuckuy said?

I am going to try the chown inside my script like chuckuy said and see if that solves my problem, because I DO know the owner of the script ahead of time, and I just need that script to run as this specific user.

I appreciate all of the help from everyone. I will be more than happy to try any other suggestions that anyone else may have for me.

Last edited by mskarica; 04-29-2005 at 01:53 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Solaris

Run commans as no login shell user

Hi All, I have problem running a command with another user who has no login shell the os is solaris 10 i am root and doing the following: su user -s /bin/bash "ls" no output .. no error messages if su - user i got No directory Please use CODE tags as required by forum... (2 Replies)
Discussion started by: BF_DEV
2 Replies

3. Shell Programming and Scripting

Best way to run a shell with different user

Hi , we got autosys for scheduling our jobs. Autosys user logs in to the Linux box and need to execute a shell script. problem here is user requires admin privileges on database to complete the task. Since scheduling is maintained by different team I don't want autosys user to have admin privileges... (1 Reply)
Discussion started by: rush143
1 Replies

4. AIX

AIX How to run a Shell Script by changing the User

Hi All, Currently our application is running on the server having AIX 5.3 OS. What we intend to do is to run a shell script owned by another user and needs to be run as that particular user. I was trying to create a shell script using the su command before running the actual script (which... (4 Replies)
Discussion started by: acoomer
4 Replies

5. Shell Programming and Scripting

Forcing another user to run a shell script (su)

I'm trying to use su (as myuser) to force another user (theuser) to run a shell script (thescript.sh): su theuser -c /home/theuser/thescript.sh However I'm running this from another script, and it is asking for theuser's password. I would rather avoid displaying it in the file (using echo... (2 Replies)
Discussion started by: asdfgg
2 Replies

6. Shell Programming and Scripting

Can root user run chmod 000 permission shell script?

Hi, I have a shell script file which is set to access permission 000. When I login as root (sudo su) and try to run this script, I am getting the Permission denied error. I have read somewhere that root admin user can execute any kind of permission script. Then why this behavior? However, I can... (1 Reply)
Discussion started by: royalibrahim
1 Replies

7. Shell Programming and Scripting

how to run script? call other script? su to another user? make a cron?

Good morning. I am searching for "how-to"'s for some particular questions: 1. How to write a script in HP-UX 11. 2. How to schedule a script. 3. How to "call" scripts from the original script. 4. How to su to another user from within a script. This is the basics of what the... (15 Replies)
Discussion started by: instant000
15 Replies

8. UNIX for Dummies Questions & Answers

shell script run by user or cron job ?

My shell script runs fine both as a cron job and when i issue it. However, I wish to differentiate when it runs as a cron-job so the "echo" statements are not issued (they get mailed to me, which i don't want). I tried checking $USER but since the cron was created in my user that does not... (5 Replies)
Discussion started by: sentinel
5 Replies

9. Shell Programming and Scripting

Have script run as a different user

Hi all, I'm a newbie to the Linux world. I have a script that needs to run under "UserB". But I don't want to give out the password to UserB. Is there a way to setup the script to run as "UserB" when UserA runs it. Thanks in advance for your assistance. (1 Reply)
Discussion started by: mikey20
1 Replies
Login or Register to Ask a Question