Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change linux user within C code?

How do I change the user that my c program identifies itself as?

A command-line tool I want to invoke automatically requires to be run as a specific user and won't work otherwise.

I have tried using setuid(0) but I still don't get the desired results.

The user I want to imitate is not 'root', but a normal unprivileged, shell-less user. I want to be able to run the binary logged in as the user nobody. I was able to concoct a solution as 'root' using:

su -ls /bin/bash -c /binary (superuser)

However I want to be able to achieve the same logged in as user nobody

Is there something I'm missing?

like image 285
Olaseni Avatar asked Dec 06 '22 00:12

Olaseni


1 Answers

If anyone could just become root by putting setuid(0); in their program, Unix would be, well, Windows.

Some thoughts:

  1. Running external command line tools from C is almost always a mistake.
  2. If you really need this command line tool, does the tool really need root permission to work? If not, fix the tool (or go back to step 1 and incorporate the functionality into your own program).
  3. If you really need the tool and it really needs root, consider setting up sudo permissions for it and running it via sudo.

Given the very basic question you're asking, you should not even attempt to write code that will run as root, so I've omitted any details about how to setup root permissions for your program.

like image 84
R.. GitHub STOP HELPING ICE Avatar answered Dec 29 '22 04:12

R.. GitHub STOP HELPING ICE