Jump to content

Auto-detect su/sudo script


Recommended Posts

Greetings :cheesy:

 

To make a long story short I've been in contact with the guy who made XF-prot, a frontend to the anti-virus application F-prot. I promised him to have some with some expertise on the scripting to look at a problem with the GTK frontend to F-prot (xf-prot that is). The problem is that little bit of script I'm here showing should auto-detect if the system is a su or sudo system, but it doesn't somehow work. I've been testing the sudo part for since he doesn't have such system and I have. And I can't simple put my finger on which's wrong with it. We need some help from you guys :)

 

 

Here's the script that should auto-detect if the system is su or sudo:

 

# Check for su -or sudo needed to run the updates command
which su &> /dev/null
if test $? -eq 0; then
echo "Checking for su.....OK"
echo "#define SU_SUDO \"su -c\"" >> config.h
echo ""
else
which sudo &> /dev/null
if test $? -eq 0; then
echo "Checking for sudo.....OK"
echo "#define SU_SUDO \"sudo\"" >> config.h
echo ""
else
echo ""
echo "Error: su or sudo ......not found"
echo "The su or sudo command is not installed on"
echo "your system, or it is not in your path variable."
echo "Please install it or fix your path variable"
echo "It is possible that the update button will not "
echo "work correctly."
echo "You have to test it."
echo ""
echo "#define SU_SUDO \"\"" >> config.h
fi
fi

 

If the problem aren't solved he will add a --sudo-on switch, but hopefully some in here knows what to solve the problem.

 

 

 

 

.:=The AI Dude=:.

Link to comment
Share on other sites

The script you posted works perfectly on my systems when executed with the bash shell (version 3.0). I tried running it in both the foreground and background, both as a non-login and login shell, with different terminals, with different window managers, and with different versions of linux.

 

I simulated having either su or sudo present, but not the other, by using a non-existent filename in the which command. For example, if testing for su not being present, I used suxxx in the which statement that tests for the presence of su; in this case, sudo was correctly detected as being present. And vice versa for su being present but not sudo.

 

So in all cases, it correctly identified whether only su, or only sudo, or both, or neither existed. And it correctly created the config.h file.

 

I got error messages if I used a restricted bash shell, but I assume you are not doing that.

 

Perhaps other can try the script out and see if it works on their systems. If so, maybe you've got something strange going on in your system (permissions, path, aliases, bash profiles, a different shell than bash, ???)

 

When you say it doesn't work, what specifically is the behavior you are experiencing, and how are you invoking it?

Edited by jboy
Link to comment
Share on other sites

Well, I got feedback from other sudo users (ubuntu), they said they got the same problem.

Perhaps I should explain what it's needed for: XF-Prot doesn't need to run as root or superuser itself. The problem lies in is when choosing the 'update' the anti-virus definition to F-Prot button it needs to save files where F-Prot is installed (/usr/local/f-prot). So this script should make the XF-Prot application to autodetect if the system use su or sudo.

 

 

By the way, he just send me a prerelease of XF-Prot 1.13pre which have the --sudo-on switch when you configure the application. Works fine that way.

Link to comment
Share on other sites

I don't have Ubuntu, but I did try it on Mandrake 10.1 (bash 2.0), and bash 3.0 in LE2005, Simply Mepis 3.3.1, and SuSE 9.3. It worked on all of them.

 

But I still don't understand what the actual problem is. Are you getting error messages? Is the config.h file not getting created? Is it falsely detecting the presence of either su or sudo?

 

One thought (assuming the config.h file is not being found where you expect it): you are creating the config.h with a >> config.h. Are you sure your current directory is the right one? Maybe you should use the full path in the redirect command, or make sure you have set the current working directory to what is needed, and that the user under which this is running has the proper write permissions. Are you sure you want to append to config.h with the >>, or do you just want to overwrite config.h with >, a single right caret?

Link to comment
Share on other sites

Okay, perhaps I wasn't specific enough :P

 

It's a falsely detection we are talking about. When I click the 'update' button XF-prot ask for su instead sudo

 

There's nothing wrong with compiling, no errors at all...

Link to comment
Share on other sites

I have no Ubuntu experience, though I understand that it uses sudo instead of su, but the script is behaving like there really is a file named su <albeit non-functional> somewhere in the path.

 

Are you sure there really isn't some non-functional su file out there (please forgive me if that's a stupid question, I just don't know enough about Ubuntu, but I know that the script works ok on some non-Ubuntu systems). If you try commands like the following in a terminal, what do you get?

 

su

which su

whereis su

locate su

 

This is just a pure wild-ass guess but is there perhaps an executable su file that maybe just outputs a message saying that you must use sudo instead (or something like that?) Or may su is just a symbolic link to sudo? What I'm suggesting is, perhaps the script really is working as programmed, but there is some non-functional su file in your path somewhere that is tricking the script? At this point, that's all I can think of, because on non-Ubuntu systems that I've tried the script on - Mandrake 10.1, LE2005, Simply Mepis 3.3.1 and SuSE 9.3 - the script works and correctly identifies whether either, both, or neither of su/sudo is installed.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...