From e9caf5798ac31ddb12954bfc9acf2499ed0746fb Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Sat, 2 Dec 2017 02:43:22 -0300 Subject: [PATCH] it works --- Makefile | 3 ++ log.h | 9 ++++ rsudo.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 log.h create mode 100644 rsudo.c diff --git a/Makefile b/Makefile index e69de29..c738ac7 100644 --- a/Makefile +++ b/Makefile @@ -0,0 +1,3 @@ + +all: + gcc -o ./rsudo rsudo.c diff --git a/log.h b/log.h new file mode 100644 index 0000000..d932abb --- /dev/null +++ b/log.h @@ -0,0 +1,9 @@ +#ifndef __LOG_H__ +#define __LOG_H__ + +#define LOG_DEBUG(m, s) printf("[DEBUG] [%s] %s\n", m, s) +#define LOG_INFO(m, s) printf("[INFO] [%s] %s\n", m, s) +#define LOG_WARN(m, s) printf("[WARN] [%s] %s\n", m, s) +#define LOG_ERROR(m, s) printf("[ERROR] [%s] %s\n", m, s) + +#endif diff --git a/rsudo.c b/rsudo.c new file mode 100644 index 0000000..dbb5a44 --- /dev/null +++ b/rsudo.c @@ -0,0 +1,142 @@ +/* + rsudo - request sudo + + this file and distributions are + memework property. + + unauthorized distribution of this file + and related files (like header files) + is strictly prohibited by people other than the owner. + + Copyright (C) Luna Mendes 2017 +*/ + +#include +#include +#include +#include +#include + +#include "log.h" + +const char* sock_path = "/home/luna/local.git/memed/memed.succ"; + +struct header_s { + unsigned int len; + int op; +}; + +char *join(const char* s1, const char* s2) +{ + char* result = malloc(strlen(s1) + strlen(s2) + 1); + + if (result) + { + strcpy(result, s1); + strcat(result, s2); + } + + return result; +} + +int main(int argc, char **argv) +{ + // the idea here is to open the socket + // send a message to it and run asap + struct sockaddr_un addr; + + // receiving data from socket + struct header_s header; + char buffer[1024]; + + // fd file descriptor + // rc read count + int fd, rc; + + if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) + { + perror("socket error"); + exit(-1); + } + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + + strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path)-1); + + LOG_INFO("main", "connecting"); + if ( connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1 ) + { + perror("connect error"); + exit(-1); + } + + // read the hello + // We start by reading and filling our header + if ((rc = recv(fd, &header, 8, 0)) < 0) + { + perror("recv"); + exit(-1); + } + + printf("%d %d\n", header.len, header.op); + + // Then we proceed to, with the data from header, + // read the message + if ((rc = recv(fd, &buffer, header.len, 0)) < 0) + { + perror("recv"); + exit(-1); + } + + + if(strlen(buffer) - 1 != header.len) + { + printf("%u %d\n", strlen(buffer), header.len); + LOG_ERROR("main", "hello length != header length"); + exit(-1); + } + + if(header.op != 0) + { + LOG_ERROR("main", "incorrect header op"); + exit(-1); + } + + LOG_INFO("main", "finished hello"); + + char *tot = ""; + for(int i = 1; i < argc; i++) + { + char *arg = argv[i]; + tot = join(tot, arg); + + if(i != argc - 1) tot = join(tot, " "); + } + + printf("sending command: %s\n", tot); + + // encode our data + struct header_s send_h; + send_h.len = strlen(tot); + send_h.op = 2; + + if (send(fd, &send_h, sizeof(send_h), 0) < 0) + { + perror("send req"); + exit(-1); + } + + if (send(fd, tot, strlen(tot), 0) < 0) + { + perror("send req total"); + exit(-1); + } + + shutdown(fd, SHUT_RDWR); + + printf("Successfully communicated your command to memed!\n"); + + close(fd); + exit(0); +}