LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   running as root and changing to user nobody programmatically (https://www.linuxquestions.org/questions/programming-9/running-as-root-and-changing-to-user-nobody-programmatically-937570/)

alphisb0t 04-01-2012 01:49 PM

running as root and changing to user nobody programmatically
 
Hey all.

I'm writing a program that requires being run as root (for interfacing with network card) but during runtime it no longer requires it.

I'd like to setuid and setgid to the nobody user/group but don't know how to programmatically determine the UID/GID of the corresponding nobody user/group. I'm trying to avoid having the user enter the UID/GID as an additional parameter or adding it to a conf file (as it would be the only thing in the conf file).

Is there a way to determine a uid/gid for nobody without asking the user for it?

Thanks for any input on this!

Tinkster 04-01-2012 06:15 PM

Have a look at the source of getent, maybe?
$ getent passwd nobody
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh



Cheers,
Tink

Tinkster 04-01-2012 06:25 PM

man getpwent

alphisb0t 04-01-2012 08:14 PM

Wow getpwent is precisely what I needed! Thanks much guys. I hadn't even heard of this function before.

Tinkster 04-01-2012 08:44 PM

Quote:

Originally Posted by alphisb0t (Post 4642313)
Wow getpwent is precisely what I needed! Thanks much guys. I hadn't even heard of this function before.


Neither - that's why I looked at getent's source ;}

ta0kira 04-02-2012 07:11 PM

I use getpwnam and getgrnam.
Kevin Barry


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