I am working on a C program for FreeBSD and I need some advice on how to execute commands as root.
This program will need to call some other ports, portmaster in particular, and for that I need root privileges. Also some other commands like cp are going to be called from within this program via popen but I need them to be executed as "normal" user.
So far I have found 3 solutions:
1)
Check the $USER environment variable and if it's not root inform the user that he needs to run this program as root. Once the user has logged in as root call portmaster normally via popen. Then use this to execute commands that need to be executed as normal user:
2)
Same as first option but use pamfor authentication. I have tried some example codes involving pamand had zero success so far.
3)
net-mgmt/wifimgr has to run some commands as root also so I took a look at source code to see how they did it. What they do is that they have a separate executable (wifimgrsu) which is, upon compilation, copied to /usr/local/libexec/. Make file puts the owner root, group wheel and sets 4511 permissions.
Main program first ask the user to input the root password and then it opens a pipe with wifimgrsu. Passes the obtained password so that wifimgrsu can perform a check:
Once it's "authenticated", wifimgrsu parses the input stream and executes commands via:
This approach is very appealing to me and makes sense BUT is this the "right" way to do it? I'm a bit skeptical since it's more like a "roll my own authentication method" approach.
Thanks for any advice!
This program will need to call some other ports, portmaster in particular, and for that I need root privileges. Also some other commands like cp are going to be called from within this program via popen but I need them to be executed as "normal" user.
So far I have found 3 solutions:
1)
Check the $USER environment variable and if it's not root inform the user that he needs to run this program as root. Once the user has logged in as root call portmaster normally via popen. Then use this to execute commands that need to be executed as normal user:
Code:
su -m normal_user -c 'cp /path/source/t.txt /path/dest/t.txt'
2)
Same as first option but use pamfor authentication. I have tried some example codes involving pamand had zero success so far.
3)
net-mgmt/wifimgr has to run some commands as root also so I took a look at source code to see how they did it. What they do is that they have a separate executable (wifimgrsu) which is, upon compilation, copied to /usr/local/libexec/. Make file puts the owner root, group wheel and sets 4511 permissions.
Main program first ask the user to input the root password and then it opens a pipe with wifimgrsu. Passes the obtained password so that wifimgrsu can perform a check:
Code:
su_pass = strdup(getpwuid(0)->pw_passwd);
if (strcmp(crypt(pass, su_pass), su_pass) != 0)
printf("FAIL %s\n", gettext("invalid password"));
else {
auth = 1;
printf("OK\n");
}
Once it's "authenticated", wifimgrsu parses the input stream and executes commands via:
Code:
system(command);
This approach is very appealing to me and makes sense BUT is this the "right" way to do it? I'm a bit skeptical since it's more like a "roll my own authentication method" approach.
Thanks for any advice!