diff options
Diffstat (limited to 'hbbpd.c')
-rw-r--r-- | hbbpd.c | 33 |
1 files changed, 25 insertions, 8 deletions
@@ -2,19 +2,23 @@ ** listener.c -- a datagram sockets "server" demo */ +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/wait.h> + #include "common.h" +#include "crypto.h" int main(int argc, char **argv, char **envp) { int fd; int numbytes; struct sockaddr_storage their_addr; - char buf[MAXBUFLEN]; + byte buf[MAXBUFLEN]; socklen_t addr_len; /* setup socket, parse cmd line */ { struct sockaddr_in6 ba; - struct ipv6_mreq ga; if (argc > 2) { fprintf(stderr, "usage: %s [interface]\n", argv[0]); exit(1); @@ -55,15 +59,28 @@ int main(int argc, char **argv, char **envp) { addr_len = sizeof their_addr; while ((numbytes = recvfrom(fd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *) &their_addr, &addr_len)) != -1) { - /* decode packet & launch handler */ + /* decode packet */ buf[numbytes] = '\0'; - char *task = buf, - *cl_argv[2] = {task, NULL}; + char *task = (char*) buf; + int task_len = strlen(task); + byte *payload = buf + task_len + 1; + int payload_len = numbytes - task_len - 1; + if (task[0] == '/' || strstr(task, "..")) { fprintf(stderr, "payload tried directory traversal\n"); continue; } + /* TODO: check if the task exists and is executable; save cpu + cycles trying to decode messages not intended for us */ + + /* decipher packet */ + if (!decipher(task, &payload, &payload_len)) { + fprintf(stderr, "unable to decrypt payload\n"); + continue; + } + + /* launch handler */ int fd_payload[2]; ENP(pipe(fd_payload), "pipe"); @@ -76,6 +93,7 @@ int main(int argc, char **argv, char **envp) { ENP(close(fd), "close"); /* run our task task */ + char *cl_argv[2] = {task, NULL}; execve(task, cl_argv, envp); perror("exec"); exit(1); @@ -86,9 +104,8 @@ int main(int argc, char **argv, char **envp) { /* write payload to child process; the return value of write is intentionally ignored */ - int tasklen = strlen(task); - if (tasklen < numbytes) - write(fd_payload[1], buf + tasklen + 1, numbytes - tasklen - 1); + if (task_len < numbytes) + IGN(write(fd_payload[1], payload, payload_len)) ENP(close(fd_payload[1]), "close"); ENP(close(fd_payload[0]), "close"); ENP(wait(NULL), "wait"); |