By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,951 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,951 IT Pros & Developers. It's quick & easy.

linux user switching

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
da*********@gmail.com writes:
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".)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #2

P: n/a

<da*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.
Nov 15 '05 #3

P: n/a
the system() command dose that run bash script commands or what?

Nov 15 '05 #4

P: n/a
da*********@gmail.com writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.