commit 42b778257918096174bc4a0bf4b1c23f68f0b875
parent 2fbfb8b474c0be90c13f10ef5317340c06bc8717
Author: hhvn <dev@hhvn.uk>
Date: Sat, 5 Jun 2021 23:43:34 +0100
handler.{c,h}: use derror() function
Diffstat:
2 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/handler.c b/handler.c
@@ -12,6 +12,18 @@
extern char *planfile;
extern int usecgi;
+int
+derror(int fd, const char *format, ...) {
+ va_list ap;
+
+ va_start(ap, format);
+ dprintf(fd, "Error: ");
+ vdprintf(fd, format, ap);
+ va_end(ap);
+
+ return 1;
+}
+
void
get_userlist(int fd) {
struct passwd *udata;
@@ -41,9 +53,9 @@ get_plan(int fd, char *user) {
if ((udata = getpwnam(user)) == NULL) {
serrno = errno;
if (serrno == 0) {
- dprintf(fd, "Error: no such user: %s\n", user);
+ derror(fd, "no such user: %s\n", user);
} else {
- dprintf(fd, "Server-side error: %s\n", strerror(serrno));
+ derror(fd, "%s\n", strerror(serrno));
error("getpwnam(): %s\n", strerror(serrno));
return;
}
@@ -51,7 +63,7 @@ get_plan(int fd, char *user) {
/* drop priviledges to user */
if (setuid(udata->pw_uid) == -1) {
- dprintf(fd, "Server-side error: cannot drop priviledges\n");
+ derror(fd, "cannot drop priviledges\n");
error("setuid(): %s\n", strerror(errno));
return;
}
@@ -62,11 +74,11 @@ get_plan(int fd, char *user) {
switch (serrno) {
case ENOTDIR:
case ENOENT:
- dprintf(fd, "Error: user %s does not have a %s file\n",
+ derror(fd, "user %s does not have a %s file\n",
user, planfile);
break;
default:
- dprintf(fd, "Error: cannot access user's %s file: %s\n",
+ derror(fd, "cannot access user's %s file: %s\n",
planfile, strerror(serrno));
error("access(): %s: %s\n", path, strerror(serrno));
}
@@ -75,14 +87,14 @@ get_plan(int fd, char *user) {
if (access(path, X_OK) == 0) {
if (!usecgi) {
- dprintf(fd, "Error: executable files disabled in daemon\n");
+ derror(fd, "executable files disabled in daemon\n");
verbose("Ignoring executable: %s\n", path);
return;
}
/* executable */
switch (pid = fork()) {
case -1:
- dprintf(1, "Error: couldn't fork: %s\n", strerror(errno));
+ derror(fd, "couldn't fork: %s\n", strerror(errno));
error("fork(): %s\n", strerror(errno));
return;
case 0:
diff --git a/handler.h b/handler.h
@@ -2,6 +2,7 @@
#ifndef H_HANDLER
#define H_HANDLER
+int derror(int fd, const char *format, ...);
void get_userlist(int fd);
void get_plan(int fd, char *user);
#endif /* H_HANDLER */