aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoni <matzeton@googlemail.com>2016-12-25 16:56:39 +0100
committertoni <matzeton@googlemail.com>2016-12-25 16:56:39 +0100
commite6541524a01d1497349e1ed641e08b503f092eba (patch)
tree9712c9202b773efb1892eef5cc683ab16a8c29d2
parent0652259407e8bced689d01ed8d6669f41b07e51f (diff)
added setuid command wrapper
-rw-r--r--suidcmd.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/suidcmd.c b/suidcmd.c
new file mode 100644
index 0000000..0998f41
--- /dev/null
+++ b/suidcmd.c
@@ -0,0 +1,54 @@
+/*
+ * build with: gcc -std=c99 -D_GNU_SOURCE=1 -Wall -O2 -ffunction-sections -fdata-sections -fomit-frame-pointer ./suidcmd.c -o ./suidcmd
+ * strip -s ./suidcmd
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+
+#ifndef CMD
+#define CMD "/usr/sbin/ether-wake"
+#endif
+
+int main(int argc, char** argv)
+{
+ uid_t ruid, euid, suid;
+
+ if (getresuid(&ruid, &euid, &suid) != 0) {
+ perror("getresuid()");
+ } else {
+ printf("%s: RUID:%u , EUID:%u , SUID:%u\n", argv[0], ruid, euid, suid);
+ }
+
+ if (setuid(0) != 0) {
+ perror("setuid(0)");
+ } else printf("%s: setuid(0)\n", argv[0]);
+
+ char* cmd = NULL;
+ if (asprintf(&cmd, "%s", CMD) <= 0) {
+ fprintf(stderr, "%s: asprintf(\"%s\") error\n", argv[0], CMD);
+ return 1;
+ }
+
+ char* prev_cmd = NULL;
+ for (int i = 1; i < argc; ++i) {
+ prev_cmd = cmd;
+ if (asprintf(&cmd, "%s %s", prev_cmd, argv[i]) < 0) {
+ fprintf(stderr, "%s: asprintf(\"%s\") error\n", argv[0], argv[i]);
+ return 1;
+ }
+ free(prev_cmd);
+ }
+
+ printf("system(\"%s\")\n", cmd);
+ int retval = -1;
+ switch ( (retval = system(cmd)) ) {
+ case -1: fprintf(stderr, "%s: could not create child process..\n", argv[0]); return 1;
+ case 127: fprintf(stderr, "%s: could not execute shell (child process)..\n", argv[0]); return 1;
+ default:
+ printf("%s: child process returned with: %d\n", argv[0], retval);
+ }
+ return 0;
+}