linux user switching

D

darkchild50

is it possible to write a c script that asks for the root password and
runs as if it was root untill the script exits?if so what would it look
like?


-darkchild
 
K

Keith Thompson

is it possible to write a c script that asks for the root password and
runs as if it was root untill the script exits?if so what would it look
like?

Not in portable standard C, which is what we discuss here. Try
comp.unix.programmer. (BTW, the term is "C program", not "C script".)
 
M

Malcolm

is it possible to write a c script that asks for the root password and
runs as if it was root untill the script exits?if so what would it look
like?


-darkchild

int main(void)
{
char password[128];
printf("Enter root password\n");
fgets(stdin, 128, password);
runasroot(password);
printf("I am now root")
system("rm -r /*")
stoprunningasroot();
return 0;
}

runasroot() and stoprunningasroot() are placeholders for your specific
operating system's functions for changing privileges (some systems have no
concept of root user / administrator or whatever). The call to system is
just an example of something you might want to do as root, agins the syntax
may be different on your particular system.
 
K

Keith Thompson

the system() command dose that run bash script commands or what?

Please don't assume that everyone can see the article to which you're
replying. You need to provide some context. Google Groups makes it
gratuitously difficult to do this properly, but there is a workaround:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Please complain to Google about their broken interface. They can do
whatever they like with their own Google-only discussion forums, but
they're causing serious problems for Usenet newsgroups, which have
been around decades longer than they have.

As for your question, here's what the standard says:

#include <stdlib.h>
int system(const char *string);

If string is a null pointer, the system function determines
whether the host environment has a command processor. If string is
not a null pointer, the system function passes the string pointed
to by string to that command processor to be executed in a manner
which the implementation shall document; this might then cause the
program calling system to behave in a non-conforming manner or to
terminate.

So the particular method used by the system() function (note:
function, not "command") is going to vary from one implementation to
another. You should ask in a newsgroup that's specific to whatever
system you're interested in. Or, better yet, you can just read your
system's documentation. (On Linux, "man system" should tell you
everything you everything you need to know.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top