Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Linux login

Hello,

I am developing a C++ application (in QT) that should run only when logged in as root. If the current user is not the super user I should pop up a dialog box to let the user enter the super user password and from that point onwards the application should run as the root user (like the one that would appear when we try to open 'StartMenu->SystemTools->Hardware Browser' as a non-root user). I am pretty new to Linux and I could not get much information about this in the internet. Can anybody help me with this or point me to the right direction where I can get help in this?

Thanks in advance

Comments

  • IDKIDK Member Posts: 1,784
    : Hello,
    :
    : I am developing a C++ application (in QT) that should run only when
    : logged in as root. If the current user is not the super user I
    : should pop up a dialog box to let the user enter the super user
    : password and from that point onwards the application should run as
    : the root user (like the one that would appear when we try to open
    : 'StartMenu->SystemTools->Hardware Browser' as a non-root user). I am
    : pretty new to Linux and I could not get much information about this
    : in the internet. Can anybody help me with this or point me to the
    : right direction where I can get help in this?
    :
    : Thanks in advance

    Look at the source code of your 'hardware browser'.
  • nugentnugent Member Posts: 87
    check out the gnu libc manual for some info about users and groups:
    http://www.gnu.org/software/libc/manual/html_node/Users-and-Groups.html

    firstly you need to check the username of the person who started the program. something like this should do:
    [code]
    struct passwd *me;

    me = getpwuid(getuid());

    if(strcmp(me->pw_name, "root") == 0)
    printf("I am Superuser
    ");
    else
    printf("I am %s
    ", me->pw_name);
    [/code]

    so if the user is not root, use the getpass() function to ask for the password. then use the crypt() function on the user submitted password.

    now read information about the root user using the getpwnam() function.
    the getpwnam() function will return a pointer to the passwd structure, which includes a hashed version of the password. compare this version of the password with the string returned from the crypt function. if they match you should use the setuid() function to change the persona of the process.

    your program will have to be owner by the root user (use chown to change the ownership).

    all the function i mentioned are documented in the gnu libc manual (http://www.gnu.org/software/libc/manual/html_node/index.html). there is also so information and warnings about using these functions in the user and group section.

    hope this helps


    ------
    nugent

  • srinivasansrinivasan Member Posts: 6
    Thanks both IDK and nugent very much. The explainations shed some light on how to begin with.

    nugent, I have checked crypt() function. It takes two arguments - a key and a salt. Are they the same with all linux versions? Are there functions to obtain them form the linux?

    Thanks
Sign In or Register to comment.