hfingerd

hhvn.uk:79
Log | Files | Refs | LICENSE

commit 24fa6b6d5dc38c48b5546bf5a709a6b262c8d470
parent 1ba1341703dbd0d124d2de2a666277bb69b17da4
Author: hhvn <dev@hhvn.uk>
Date:   Sat,  5 Jun 2021 23:33:19 +0100

handler.c: implement "CGI"

Diffstat:
Mhandler.c | 49++++++++++++++++++++++++++++++++++---------------
1 file changed, 34 insertions(+), 15 deletions(-)

diff --git a/handler.c b/handler.c @@ -32,8 +32,9 @@ get_plan(int fd, char *user) { struct passwd *udata; char path[1024]; char buf[4096]; - int serrno; + int serrno, status; FILE *plan; + pid_t pid; errno = 0; if ((udata = getpwnam(user)) == NULL) { @@ -71,20 +72,38 @@ get_plan(int fd, char *user) { return; } - /* TODO: check if executable - * and implement "CGI" */ + if (access(path, X_OK) == 0) { + /* executable */ + switch (pid = fork()) { + case -1: + dprintf(1, "Error: couldn't fork: %s\n", strerror(errno)); + error("fork(): %s\n", strerror(errno)); + return; + case 0: + dup2(fd, 1); + dup2(fd, 2); + execl(path, path, NULL); + break; + default: + verbose("Executing %s as PID %d\n", path, pid); + waitpid(pid, &status, 0); + verbose("PID %d finished\n", pid); + return; + } + } else { + /* raw file */ + if ((plan = fopen(path, "rb")) == NULL) { + serrno = errno; + dprintf(fd, "Error: cannot read user's %s file: %s\n", + planfile, strerror(serrno)); + error("fopen(): %s\n", strerror(serrno)); + return; + } - if ((plan = fopen(path, "rb")) == NULL) { - serrno = errno; - dprintf(fd, "Error: cannot read user's %s file: %s\n", - planfile, strerror(serrno)); - error("fopen(): %s\n", strerror(serrno)); - return; + /* copy file to fd */ + while (fgets(buf, sizeof(buf), plan) != NULL) + write(fd, buf, strlen(buf)); + /* don't use sizeof(buf), + * or you get heartbleed for free */ } - - /* copy file to fd */ - while (fgets(buf, sizeof(buf), plan) != NULL) - write(fd, buf, strlen(buf)); - /* don't use sizeof(buf), - * or you get heartbleed for free */ }