exec as root

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dark_knight
Forum Newbie
Posts: 2
Joined: Wed Jul 10, 2002 9:06 pm

exec as root

Post by dark_knight »

Hi there,

I am just starting out some php programming, with some c experience.

I am attempting to build a web interface for some common unix administration tasks, but while apache runs as user 'nobody', the commands can't get executed due to lack of root privilege.

The only way I could get it to work is by dumping the dynamic command into a file, logging on as root to the console and executing it.

I do have an idea, but it is a little far fetched. One question is, how do I output a string to a file so that the filename will never be the same. (ie. increment ++ the filename, so php will recognize the last filename output?) eg. file00001, file00002

If someone has some ideas, please help!

knight
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

For Progressive filenames

Post by EricS »

One quick way to get progressive file names that aren't the same could be to use date and time the file was created for the name. The only hitch I can see is if two files get created at the exact same second.

Another way is to use a file that simply stores a number, for example call it "counter_file.cnt". When you need a file name, read the "counter_file.cnt" to a variable, use that as the file name, then increment that number and write over "counter_file.cnt" with the new number in it.

Either way should be pretty simple to implement.

Hope that helps.
dark_knight
Forum Newbie
Posts: 2
Joined: Wed Jul 10, 2002 9:06 pm

Post by dark_knight »

Thank you.

I will use the file update method. A couple of years ago I would of just stored it in the registry, but not any more!

knight
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

exec as root

Post by haagen »

Hi there

As I see it there are two ways for you to solve the exec problem.

You can put the commands you need to run as root into a shell script. When you have this working as it should (as user root) set it setuid root. This means that the program will run as root even if it's another user that starts it. Search google or man pages (man chmod/chown) for more info about this.

Another way is to run you program / script with sudo. This means actually that you run the program through sudo, which execute it's as root. Search google or check a man page (man sudo) for more information.

I hope this helpes.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

or, for the filename, you can use tempnam(). from the manual:
tempnam -- Create file with unique file name
Post Reply