I’m writing a simple package manager and I’d like to automatically try sudo if the program isn’t run as root. I found a function called seteuid, which looks likes it’s exactly what I need, but I don’t have the permissions to run it. So far all I can think of is a bash script to check before they get to the actual binary, but I’d like to do this all as C++ if possible.
Is there any method of changing a processes’s euid after it starts executing? Or a way to call sudo?
As bmargulies says in his answer, you can do this if you binary is owned by root and has the setuid bit set – but then you will need to implement the authentication part (checking that the user is actually allowed to become root) yourself too.
You’d be essentially rewriting
sudowithin your application – the best way to handle this is to do what you suggested yourself, and install your application with a wrapper script that uses/usr/bin/idto check if it is root, and if not, call out tosudo(orsu).