LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2004, 07:20 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Run as different user


I am setting up my own daemon and are now working with the config-file. I wonder if I should enable running as a different user. How can I do this then?

The language is C++.

My daemon starts, forks and dies, letting the fork be the running daemon.
 
Old 12-23-2004, 07:55 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You say it is a daemon, so I suppose it is started as root, and you want to change to a different user ("drop root privileges" as they say) to increase security of your program.

You can do that like below. Though this is in C, it largely (if not entirely) applies to C++.
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    uid_t uid;   /* user to change to */
    gid_t gid;   /* group to change to */

    /* Change these to your needs */
    uid = 65534;
    gid = 65534;

    /* ... other things ... */

    /* Become daemon */
    if (daemon(0,0) < 0) {
        perror("Error becoming daemon");
        return 1;
    }

    /* Become non-root */
    setregid(gid, gid);
    if (getgid() == 0 || getegid() == 0) {
        perror("Could not drop GID root");
        return 1;
    }

    setuid(uid);
    if (getuid() == 0 || geteuid() == 0) {
        perror("Could not drop UID root");
        return 1;
    }

    /* Start doing work here */

    /*  ...  */

    return 0;
}
 
Old 12-24-2004, 03:50 PM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Ok.. Thanks a lot.

But now I am working on a config-file and I thought that maby it would be better if the user of the program just had to type in the name of the user/group instead of their IDs and then the program could get the uid/gid out of that.

Is that possible?
 
Old 12-24-2004, 03:56 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
sure, see getpwnam(3)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run as specified user? freakin'me Linux - Software 2 10-08-2005 03:00 PM
How Do I Run A Program As A Different User?? Wolf-67 Linux - Newbie 3 10-04-2004 12:26 PM
how do I make sure that the user that is going to run the script is a root user??? nikold01 Linux - General 3 09-10-2004 07:54 AM
Run as user? redhatnoob Programming 1 08-18-2004 09:12 PM
Run a script as a different user dtheorem Programming 5 11-05-2003 05:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration