This commit is contained in:
wt
2024-12-28 00:49:44 +07:00
commit 6dfe3bac94
3 changed files with 56 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// #include <sys/types.h>
#include <unistd.h>
void die(char *argv0, char *obj, char *msg) {
if (msg == NULL) msg = strerror(errno);
fprintf(stderr, "%s: %s: %s\n", argv0, obj, msg);
exit(127);
}
int main(int argc, char *argv[]) {
uid_t uid;
gid_t gid;
// char *endp;
/* Command line parsing */
if (argc <= 1) {
fprintf(stderr, "Usage: %s COMMAND [ARG...]\n",
argv[0]==NULL ? "sugexec" : argv[0]);
exit(127);
}
uid = 0;//strtoul(argv[1], &endp, 0);
// if (*endp) die(argv[0], argv[1], "Bad uid");
gid = 0;//strtoul(argv[2], &endp, 0);
// if (*endp) die(argv[0], argv[2], "Bad gid");
/* Drop to specified user and group */
if (setgid(gid)) die(argv[0], "setgid", NULL);
if (setgroups(0, NULL)) die(argv[0], "setgroups", NULL);
if (setuid(uid)) die(argv[0], "setuid", NULL);
/* Execute the command */
execvp(argv[1], argv+1);
die(argv[0], argv[1], NULL);
}