summaryrefslogtreecommitdiff
path: root/privs.c
diff options
context:
space:
mode:
Diffstat (limited to 'privs.c')
-rw-r--r--privs.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/privs.c b/privs.c
new file mode 100644
index 0000000..ac4ad25
--- /dev/null
+++ b/privs.c
@@ -0,0 +1,19 @@
+#include <unistd.h>
+#include <sys/types.h>
+
+#include "privs.h"
+#include "die.h"
+
+void drop_privileges(bool enforce, uid_t uid, gid_t gid)
+{
+ if (enforce) {
+ if (uid == getuid())
+ panic("Uid cannot be the same as the current user!\n");
+ if (gid == getgid())
+ panic("Gid cannot be the same as the current user!\n");
+ }
+ if (setgid(gid) != 0)
+ panic("Unable to drop group privileges: %s!\n", strerror(errno));
+ if (setuid(uid) != 0)
+ panic("Unable to drop user privileges: %s!\n", strerror(errno));
+}