views:

1546

answers:

5

I have a program that is going to take a password as input and then do a shell execute to perform a "su" (switch user) command in UNIX. However, I don't know how to pass the password variable to the UNIX su command. The language I have to use for this is pretty limited (UniBasic).

Any ideas?

+10  A: 

Well, the best way to do that would be a setuid-root binary that ask for the password then execute whatever command is needed but it requires knowledge you say not to possess. I'd advise in looking at sudo(1) instead.

Keltia
I'm going to go w/ the setuid-root option. Thanks!
vg1890
+1  A: 

The version of su I have on my Linux server does not support such an option. I checked sudo, but it doesn't either. They want to do the prompting themselves, to ensure it's done in a safe manner and (I guess) to discourage people from putting passwords verbatim in scripts and so on.

unwind
sudo can be configured such that no password is asked.
PolyThinker
+2  A: 

You could ssh to localhost as another user to execute whatever command you want. Or, use sudo and edit /etc/sudoers such that sudo does not ask for a password. However, there could be security implications.

EDIT: Please let me know why when you vote it down. My answer may not be perfect but at least it works. I do that myself for some licensed software that can only be run under a weird user name.

PolyThinker
I don't know why this was voted down, seems like a perfectly good workaround.
Greg Rogers
+1  A: 

You do not want to specify the password as a command-line argument. Not so much because of @unwind's answer (scripts could be made private) but because if someone runs a list of processes, you could see the command argument and hence the password in question.

Jason S
+1 one because it boogles my mind how many times I see people asking about setting a password on the command line. So many people should have their UNIX/Linux operating licenses revoked...
Dan McGrath
A: 

You could also look at the 'expect' utility that was designed to script complex user inputs into a programs that weren't flexible enough to receive this input from places other than stdin.