LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to get root privilage (https://www.linuxquestions.org/questions/programming-9/how-to-get-root-privilage-357712/)

skie_knite007 08-28-2005 02:49 AM

how to get root privilage
 
I'm creating an appilcation in C which requires root privilage.....How can I invoke root privilage amidst the execution..I mean I can ask the user to enter the root passwd, but how I can substitute the user with root using C once I got the passwd.???????????

zeropash 08-28-2005 04:41 AM

do you want to elevate the privileges while executing or at the begining itself?
if its at the begining of the execution you can check the documentation for
sudo, chmod +s ,
or if you are in a graphical environment check out kdesu or gnomesu

skie_knite007 08-28-2005 07:11 AM

But I need it while executing

dub.wav 08-28-2005 10:19 AM

Code:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#define UID_ROOT 0

int main(int argc, char *argv[]) {
    if (geteuid() != UID_ROOT) {
        execlp("su", "su", "-c", argv[0], NULL);
    }
    puts("root");
    return 0;
}

If you need to pass more arguments, RTFM.

btmiller 08-28-2005 10:30 AM

And to add on, you should really check user input very carefully before you do something with it as root. Otherwise you wilkl compromise your system's security.


All times are GMT -5. The time now is 02:53 PM.