commit 1857fc0fdb233ffff4d9c6c365cf44dc24d0cf82
parent 921d563a92567c81a7f8597c140afbe9adaf17b7
Author: hhvn <dev@hhvn.uk>
Date: Sat, 5 Jun 2021 20:08:14 +0100
makefile handler.{c,h} main.{c,h}: split handlers into seperate file
Diffstat:
A | handler.c | | | 76 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | handler.h | | | 7 | +++++++ |
M | main.c | | | 65 | +---------------------------------------------------------------- |
A | main.h | | | 12 | ++++++++++++ |
M | makefile | | | 6 | +++--- |
5 files changed, 99 insertions(+), 67 deletions(-)
diff --git a/handler.c b/handler.c
@@ -0,0 +1,76 @@
+/* Copyright (c) 2021 hhvn <dev@hhvn.uk */
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <pwd.h>
+#include <sys/types.h>
+#include "handler.h"
+#include "main.h"
+
+extern char *planfile;
+
+void
+get_userlist(int fd) {
+ /* TODO */
+ return;
+}
+
+void
+get_plan(int fd, char *user) {
+ struct passwd *udata;
+ char path[1024];
+ char buf[4096];
+ int serrno;
+ FILE *plan;
+
+ errno = 0;
+ if ((udata = getpwnam(user)) == NULL) {
+ serrno = errno;
+ if (serrno == 0) {
+ dprintf(fd, "Error: no such user: %s\n", user);
+ } else {
+ dprintf(fd, "Server-side error: %s\n", strerror(serrno));
+ error("getpwnam(): %s\n", strerror(serrno));
+ }
+ }
+
+ /* drop priviledges to user */
+ if (setuid(udata->pw_uid) == -1)
+ error("setuid(): %s\n", strerror(errno));
+
+ snprintf(path, sizeof(path), "%s/%s", udata->pw_dir, planfile);
+ if (access(path, R_OK) == -1) {
+ serrno = errno;
+ switch (serrno) {
+ case ENOTDIR:
+ case ENOENT:
+ dprintf(fd, "Error: user %s does not have a %s file\n",
+ user, planfile);
+ break;
+ default:
+ dprintf(fd, "Error: cannot access user's %s file: %s\n",
+ planfile, strerror(serrno));
+ error("access(): %s: %s\n", path, strerror(serrno));
+ }
+ return;
+ }
+
+ /* TODO: check if executable
+ * and implement "CGI" */
+
+ 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 */
+}
diff --git a/handler.h b/handler.h
@@ -0,0 +1,7 @@
+/* Copyright (c) 2021 hhvn <dev@hhvn.uk> */
+
+#ifndef H_HANDLER
+#define H_HANDLER
+void get_userlist(int fd);
+void get_plan(int fd, char *user);
+#endif /* H_HANDLER */
diff --git a/main.c b/main.c
@@ -12,6 +12,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include "arg.h"
+#include "handler.h"
#define EXIT_USAGE 2
#define CQUEUE 255
@@ -104,70 +105,6 @@ read_line(int fd, char *dest, size_t len) {
return 1;
}
-void
-get_userlist(int fd) {
- /* TODO */
- return;
-}
-
-void
-get_plan(int fd, char *user) {
- struct passwd *udata;
- char path[1024];
- char buf[4096];
- int serrno;
- FILE *plan;
-
- errno = 0;
- if ((udata = getpwnam(user)) == NULL) {
- serrno = errno;
- if (serrno == 0) {
- dprintf(fd, "Error: no such user: %s\n", user);
- } else {
- dprintf(fd, "Server-side error: %s\n", strerror(serrno));
- error("getpwnam(): %s\n", strerror(serrno));
- }
- }
-
- /* drop priviledges to user */
- if (setuid(udata->pw_uid) == -1)
- error("setuid(): %s\n", strerror(errno));
-
- snprintf(path, sizeof(path), "%s/%s", udata->pw_dir, planfile);
- if (access(path, R_OK) == -1) {
- serrno = errno;
- switch (serrno) {
- case ENOTDIR:
- case ENOENT:
- dprintf(fd, "Error: user %s does not have a %s file\n",
- user, planfile);
- break;
- default:
- dprintf(fd, "Error: cannot access user's %s file: %s\n",
- planfile, strerror(serrno));
- error("access(): %s: %s\n", path, strerror(serrno));
- }
- return;
- }
-
- /* TODO: check if executable
- * and implement "CGI" */
-
- 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 */
-}
-
void
handler(int fd) {
char user[1024];
diff --git a/main.h b/main.h
@@ -0,0 +1,12 @@
+/* Copyright (c) 2021 hhvn <dev@hhvn.uk> */
+
+#ifndef H_MAIN
+#define H_MAIN
+void usage(void);
+int verbose(const char *format, ...);
+int error(const char *format, ...);
+int die(const int exitc, const char *format, ...);
+int getsock(struct addrinfo *hints, char *host, char *port);
+int read_line(int fd, char *dest, size_t len);
+void handler(int fd);
+#endif /* H_MAIN */
diff --git a/makefile b/makefile
@@ -1,12 +1,12 @@
CC ?= cc
-OBJ = main.o
+OBJ = main.o handler.o
BIN = hfingerd
$(BIN): $(OBJ)
- $(CC) -g $(CFLAGS) $(LDFLAGS) -o $@ $<
+ $(CC) -g $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ)
.c.o:
- $(CC) -g $(CFLAGS) $(LDFLAGS) -c $<
+ $(CC) -g $(CFLAGS) $(LDFLAGS) -o $@ -c $<
clean:
rm $(BIN) $(OBJ)