0

I am developing in C++ on NetBeans 6.9 on Ubuntu 11.04. I am making a system() call which I would like to be called as user "peter" rather than as root. I understand that

setuid(0);

sets the user to root but how does one set the user to peter?

Thanks, Peter.

OtagoHarbour
  • 3,969
  • 6
  • 43
  • 81
  • 2
    "peter" is not a user. It is a string associated with a user. Users are identified by number. What you have to do is find out which user ID has a string "peter" associated with it. `getpwnam` will help you with that. – Ben Voigt Aug 19 '12 at 01:37

1 Answers1

2

You perhaps want to search the password file for the correct user id via, e.g. getpwnam(). Something like:

// look up peter's uid
uid_t peter_uid=getpwnam("peter")->pw_uid;

// Become peter
setuid(peter_uid);
seteuid(peter_uid);
Managu
  • 8,849
  • 2
  • 30
  • 36