aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2018-05-13 13:41:22 +0200
committerToni Uhlig <matzeton@googlemail.com>2018-05-13 13:41:22 +0200
commitaebb4b9fa16bd82fc78d850dc47484c6d733b223 (patch)
treef1334993279ddc6bc9382e375a732120450924bc
parentbc240cca7918d6e6f4f56fbe19d32eb20f07a567 (diff)
POTD skeleton #47.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--src/Makefile.am2
-rw-r--r--src/jail.c1
-rw-r--r--src/main.c26
-rw-r--r--src/redirector.c (renamed from src/server.c)102
-rw-r--r--src/redirector.h28
-rw-r--r--src/server.h27
-rw-r--r--src/server_ssh.h1
-rw-r--r--src/utils.c2
8 files changed, 94 insertions, 95 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 711632d..e3bd8fc 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,4 +2,4 @@ AM_CFLAGS = -pedantic -Wall -std=gnu99 -fstrict-aliasing -D_GNU_SOURCE=1 $(libss
AM_LDFLAGS = $(libssh_LIBS)
sbin_PROGRAMS = potd
-potd_SOURCES = utils.c log.c log_colored.c socket.c pevent.c jail.c forward.c server.c server_ssh.c main.c
+potd_SOURCES = utils.c log.c log_colored.c socket.c pevent.c jail.c forward.c redirector.c server_ssh.c main.c
diff --git a/src/jail.c b/src/jail.c
index 195356e..3de4d85 100644
--- a/src/jail.c
+++ b/src/jail.c
@@ -10,7 +10,6 @@
#include "jail.h"
#include "socket.h"
-#include "server.h"
#include "utils.h"
#include "log.h"
diff --git a/src/main.c b/src/main.c
index 5b2248c..57c5a97 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,7 +5,7 @@
#include "log.h"
#include "log_colored.h"
#include "utils.h"
-#include "server.h"
+#include "redirector.h"
#include "server_ssh.h"
#include "forward.h"
#include "jail.h"
@@ -20,13 +20,13 @@ int main(int argc, char *argv[])
const size_t jail_siz = 2;
const char *ssh_ports[srv_siz];
const char *jail_ports[jail_siz];
- server_ctx *srv[srv_siz];
+ redirector_ctx *rdr[srv_siz];
jail_ctx *jail[jail_siz];
forward_ctx *ssh_fwd = NULL;
- event_ctx *srv_event = NULL;
+ event_ctx *rdr_event = NULL;
event_ctx *jail_event = NULL;
int proc_status;
- pid_t daemon_pid, srv_pid, jail_pid, wpid;
+ pid_t daemon_pid, rdr_pid, jail_pid, wpid;
(void) argc;
(void) argv;
@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
"Forwarder validation" );
}
- memset(srv, 0, sizeof(srv));
+ memset(rdr, 0, sizeof(rdr));
ssh_ports[0] = "2222";
ssh_ports[1] = "2223";
ssh_ports[2] = "22050";
@@ -83,15 +83,15 @@ int main(int argc, char *argv[])
for (size_t i = 0; i < srv_siz; ++i) {
D("Initialising redirector service on port %s", ssh_ports[i]);
- server_init_ctx(&srv[i], ssh_fwd);
- ABORT_ON_FATAL( server_setup(srv[i], NULL, ssh_ports[i]),
+ redirector_init_ctx(&rdr[i], ssh_fwd);
+ ABORT_ON_FATAL( redirector_setup(rdr[i], NULL, ssh_ports[i]),
"Server setup" );
- ABORT_ON_FATAL( server_validate_ctx(srv[i]),
+ ABORT_ON_FATAL( redirector_validate_ctx(rdr[i]),
"Server validation" );
}
D2("%s", "Server event setup");
- ABORT_ON_FATAL( server_setup_event( srv, srv_siz, &srv_event ),
+ ABORT_ON_FATAL( redirector_setup_event( rdr, srv_siz, &rdr_event ),
"Server event setup" );
D2("Server dropping privileges to %s:%s", "nobody", "NULL");
@@ -99,16 +99,16 @@ int main(int argc, char *argv[])
"Server dropping privileges" );
N("%s", "Server epoll mainloop");
- srv_pid = server_daemonize( srv_event, srv, srv_siz );
- ABORT_ON_FATAL( srv_pid < 1, "Server epoll mainloop" );
+ rdr_pid = redirector_daemonize( rdr_event, rdr, srv_siz );
+ ABORT_ON_FATAL( rdr_pid < 1, "Server epoll mainloop" );
while (1) {
wpid = wait(&proc_status);
if (wpid == jail_pid ||
- wpid == srv_pid) {
+ wpid == rdr_pid) {
E2("%s daemon with pid %d terminated, exiting",
(wpid == jail_pid ? "Jail" : "Server"),
- (wpid == jail_pid ? jail_pid : srv_pid));
+ (wpid == jail_pid ? jail_pid : rdr_pid));
break;
}
}
diff --git a/src/server.c b/src/redirector.c
index 6df7916..9366079 100644
--- a/src/server.c
+++ b/src/redirector.c
@@ -6,7 +6,7 @@
#include <pthread.h>
#include <assert.h>
-#include "server.h"
+#include "redirector.h"
#include "socket.h"
#include "utils.h"
#include "log.h"
@@ -15,11 +15,11 @@ typedef struct client_thread {
pthread_t self;
psocket client_sock;
char host_buf[NI_MAXHOST], service_buf[NI_MAXSERV];
- const server_ctx *server_ctx;
+ const redirector_ctx *rdr_ctx;
} client_thread;
typedef struct server_event {
- const server_ctx **srv_ctx;
+ const redirector_ctx **rdr_ctx;
const size_t siz;
} server_event;
@@ -32,26 +32,26 @@ static forward_state
fwd_state_string(const forward_state c_state, const client_thread *args,
const psocket *fwd);
static int
-server_mainloop(event_ctx *ev_ctx, const server_ctx *srv_ctx[], size_t siz)
+redirector_mainloop(event_ctx *ev_ctx, const redirector_ctx *rdr_ctx[], size_t siz)
__attribute__((noreturn));
-static int server_accept_client(event_ctx *ev_ctx, int fd, void *user_data);
+static int redirector_accept_client(event_ctx *ev_ctx, int fd, void *user_data);
static void *
client_mainloop(void *arg);
static int client_io(event_ctx *ev_ctx, int src_fd, void *user_data);
-void server_init_ctx(server_ctx **ctx, forward_ctx *fwd_ctx)
+void redirector_init_ctx(redirector_ctx **ctx, forward_ctx *fwd_ctx)
{
assert(ctx && fwd_ctx);
if (!*ctx)
- *ctx = (server_ctx *) malloc(sizeof(**ctx));
+ *ctx = (redirector_ctx *) malloc(sizeof(**ctx));
assert(*ctx);
memset(*ctx, 0, sizeof(**ctx));
(*ctx)->fwd_ctx = fwd_ctx;
}
-int server_setup(server_ctx *ctx,
+int redirector_setup(redirector_ctx *ctx,
const char *listen_addr, const char *listen_port)
{
int s;
@@ -81,7 +81,7 @@ int server_setup(server_ctx *ctx,
return 0;
}
-int server_validate_ctx(const server_ctx *ctx)
+int redirector_validate_ctx(const redirector_ctx *ctx)
{
assert(ctx && ctx->fwd_ctx);
assert(ctx->sock.fd >= 0 && ctx->sock.addr_len > 0);
@@ -89,11 +89,11 @@ int server_validate_ctx(const server_ctx *ctx)
return 0;
}
-int server_setup_event(server_ctx *srv_ctx[], size_t siz, event_ctx **ev_ctx)
+int redirector_setup_event(redirector_ctx *rdr_ctx[], size_t siz, event_ctx **ev_ctx)
{
int s;
- assert(srv_ctx && ev_ctx);
+ assert(rdr_ctx && ev_ctx);
assert(siz > 0 && siz < POTD_MAXFD);
event_init(ev_ctx);
@@ -101,37 +101,37 @@ int server_setup_event(server_ctx *srv_ctx[], size_t siz, event_ctx **ev_ctx)
return 1;
for (size_t i = 0; i < siz; ++i) {
- if (event_add_sock(*ev_ctx, &srv_ctx[i]->sock)) {
+ if (event_add_sock(*ev_ctx, &rdr_ctx[i]->sock)) {
return 1;
}
- s = socket_addrtostr_in(&srv_ctx[i]->sock,
- srv_ctx[i]->host_buf, srv_ctx[i]->service_buf);
+ s = socket_addrtostr_in(&rdr_ctx[i]->sock,
+ rdr_ctx[i]->host_buf, rdr_ctx[i]->service_buf);
if (s) {
E_GAIERR(s, "Convert socket address to string");
return 1;
}
N("Redirector service listening on %s:%s",
- srv_ctx[i]->host_buf, srv_ctx[i]->service_buf);
+ rdr_ctx[i]->host_buf, rdr_ctx[i]->service_buf);
}
return 0;
}
-pid_t server_daemonize(event_ctx *ev_ctx, server_ctx *srv_ctx[], size_t siz)
+pid_t redirector_daemonize(event_ctx *ev_ctx, redirector_ctx *rdr_ctx[], size_t siz)
{
pid_t p;
int s;
size_t i;
- assert(ev_ctx && srv_ctx);
+ assert(rdr_ctx && ev_ctx);
assert(siz > 0 && siz < POTD_MAXFD);
for (i = 0; i < siz; ++i) {
- assert(srv_ctx[i]);
- s = socket_addrtostr_in(&srv_ctx[i]->sock,
- srv_ctx[i]->host_buf,
- srv_ctx[i]->service_buf);
+ assert(rdr_ctx[i]);
+ s = socket_addrtostr_in(&rdr_ctx[i]->sock,
+ rdr_ctx[i]->host_buf,
+ rdr_ctx[i]->service_buf);
if (s) {
E_GAIERR(s, "Could not initialise server daemon socket");
return 1;
@@ -145,7 +145,7 @@ pid_t server_daemonize(event_ctx *ev_ctx, server_ctx *srv_ctx[], size_t siz)
return -1;
case 0:
N("%s", "Server daemon mainloop");
- server_mainloop(ev_ctx, (const server_ctx **) srv_ctx, siz);
+ redirector_mainloop(ev_ctx, (const redirector_ctx **) rdr_ctx, siz);
break;
}
D2("Server daemon pid: %d", p);
@@ -172,14 +172,14 @@ fwd_state_string(const forward_state c_state, const client_thread *args,
break;
case CON_OUT_ERROR:
N("Lost forward connection to %s:%s: %d",
- args->server_ctx->fwd_ctx->host_buf,
- args->server_ctx->fwd_ctx->service_buf,
+ args->rdr_ctx->fwd_ctx->host_buf,
+ args->rdr_ctx->fwd_ctx->service_buf,
fwd->fd);
break;
case CON_OUT_TERMINATED:
N("Forward connection terminated: %s:%s: %d",
- args->server_ctx->fwd_ctx->host_buf,
- args->server_ctx->fwd_ctx->service_buf,
+ args->rdr_ctx->fwd_ctx->host_buf,
+ args->rdr_ctx->fwd_ctx->service_buf,
fwd->fd);
break;
}
@@ -187,46 +187,46 @@ fwd_state_string(const forward_state c_state, const client_thread *args,
return c_state;
}
-static int server_mainloop(event_ctx *ev_ctx, const server_ctx *srv_ctx[], size_t siz)
+static int redirector_mainloop(event_ctx *ev_ctx, const redirector_ctx *rdr_ctx[], size_t siz)
{
int rc;
- server_event ev_srv = { srv_ctx, siz };
+ server_event ev_srv = { rdr_ctx, siz };
- set_procname("[potd] server");
+ set_procname("[potd] redirector");
assert( set_child_sighandler() == 0 );
- rc = event_loop(ev_ctx, server_accept_client, &ev_srv);
+ rc = event_loop(ev_ctx, redirector_accept_client, &ev_srv);
event_free(&ev_ctx);
exit(rc);
}
-static int server_accept_client(event_ctx *ev_ctx, int fd, void *user_data)
+static int redirector_accept_client(event_ctx *ev_ctx, int fd, void *user_data)
{
size_t i;
int s;
server_event *ev_srv = (server_event *) user_data;
client_thread *args;
- const server_ctx *srv_ctx;
+ const redirector_ctx *rdr_ctx;
(void) ev_ctx;
assert(ev_srv);
for (i = 0; i < ev_srv->siz; ++i) {
- srv_ctx = ev_srv->srv_ctx[i];
- if (srv_ctx->sock.fd == fd) {
+ rdr_ctx = ev_srv->rdr_ctx[i];
+ if (rdr_ctx->sock.fd == fd) {
args = (client_thread *) calloc(1, sizeof(*args));
assert(args);
- if (socket_accept_in(&srv_ctx->sock,
+ if (socket_accept_in(&rdr_ctx->sock,
&args->client_sock))
{
E_STRERR("Could not accept client connection on fd %d",
- srv_ctx->sock.fd);
+ rdr_ctx->sock.fd);
goto error;
}
- args->server_ctx = srv_ctx;
+ args->rdr_ctx = rdr_ctx;
s = socket_addrtostr_in(&args->client_sock,
args->host_buf, args->service_buf);
if (s) {
@@ -235,7 +235,7 @@ static int server_accept_client(event_ctx *ev_ctx, int fd, void *user_data)
}
N2("New connection from %s:%s to %s:%s: %d",
args->host_buf, args->service_buf,
- srv_ctx->host_buf, srv_ctx->service_buf,
+ rdr_ctx->host_buf, rdr_ctx->service_buf,
args->client_sock.fd);
if (pthread_create(&args->self, NULL,
@@ -274,25 +274,25 @@ client_mainloop(void *arg)
event_init(&ev_ctx);
if (event_setup(ev_ctx)) {
E_STRERR("Client event context creation for server fd %d",
- args->server_ctx->sock.fd);
+ args->rdr_ctx->sock.fd);
goto finish;
}
- if (fwd_connect_sock(args->server_ctx->fwd_ctx, &fwd)) {
+ if (fwd_connect_sock(args->rdr_ctx->fwd_ctx, &fwd)) {
E_STRERR("Forward connection to %s:%s server fd %d",
- args->server_ctx->fwd_ctx->host_buf,
- args->server_ctx->fwd_ctx->service_buf,
- args->server_ctx->sock.fd);
+ args->rdr_ctx->fwd_ctx->host_buf,
+ args->rdr_ctx->fwd_ctx->service_buf,
+ args->rdr_ctx->sock.fd);
goto finish;
}
N("Forwarding connection to %s:%s forward fd %d",
- args->server_ctx->fwd_ctx->host_buf,
- args->server_ctx->fwd_ctx->service_buf, fwd.fd);
+ args->rdr_ctx->fwd_ctx->host_buf,
+ args->rdr_ctx->fwd_ctx->service_buf, fwd.fd);
if (event_add_sock(ev_ctx, &fwd)) {
E_STRERR("Forward event context add to %s:%s forward fd %d",
- args->server_ctx->fwd_ctx->host_buf,
- args->server_ctx->fwd_ctx->service_buf, fwd.fd);
+ args->rdr_ctx->fwd_ctx->host_buf,
+ args->rdr_ctx->fwd_ctx->service_buf, fwd.fd);
goto finish;
}
@@ -303,14 +303,14 @@ client_mainloop(void *arg)
s = socket_nonblock(&args->client_sock);
if (s) {
E_STRERR("Socket non blocking mode to %s:%s forward fd %d",
- args->server_ctx->fwd_ctx->host_buf,
- args->server_ctx->fwd_ctx->service_buf, fwd.fd);
+ args->rdr_ctx->fwd_ctx->host_buf,
+ args->rdr_ctx->fwd_ctx->service_buf, fwd.fd);
goto finish;
}
if (event_add_sock(ev_ctx, &args->client_sock)) {
E_STRERR("Forward event context add to %s:%s forward fd %d",
- args->server_ctx->fwd_ctx->host_buf,
- args->server_ctx->fwd_ctx->service_buf, fwd.fd);
+ args->rdr_ctx->fwd_ctx->host_buf,
+ args->rdr_ctx->fwd_ctx->service_buf, fwd.fd);
goto finish;
}
diff --git a/src/redirector.h b/src/redirector.h
new file mode 100644
index 0000000..3d62335
--- /dev/null
+++ b/src/redirector.h
@@ -0,0 +1,28 @@
+#ifndef POTD_SERVER_H
+#define POTD_SERVER_H 1
+
+#include "socket.h"
+#include "forward.h"
+#include "pevent.h"
+
+
+typedef struct redirector_ctx {
+ forward_ctx *fwd_ctx;
+ psocket sock;
+ char host_buf[NI_MAXHOST], service_buf[NI_MAXSERV];
+} redirector_ctx;
+
+
+void redirector_init_ctx(redirector_ctx **rdr_ctx, forward_ctx *fwd_ctx);
+
+int redirector_setup(redirector_ctx *rdr_ctx,
+ const char *listen_addr, const char *listen_port);
+
+int redirector_validate_ctx(const redirector_ctx *rdr_ctx);
+
+int redirector_setup_event(redirector_ctx *rdr_ctx[], size_t siz,
+ event_ctx **ev_ctx);
+
+pid_t redirector_daemonize(event_ctx *ev_ctx, redirector_ctx *rdr_ctx[], size_t siz);
+
+#endif
diff --git a/src/server.h b/src/server.h
deleted file mode 100644
index b1163fc..0000000
--- a/src/server.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef POTD_SERVER_H
-#define POTD_SERVER_H 1
-
-#include "socket.h"
-#include "forward.h"
-#include "pevent.h"
-
-
-typedef struct server_ctx {
- forward_ctx *fwd_ctx;
- psocket sock;
- char host_buf[NI_MAXHOST], service_buf[NI_MAXSERV];
-} server_ctx;
-
-
-void server_init_ctx(server_ctx **ctx, forward_ctx *fwd_ctx);
-
-int server_setup(server_ctx *ctx,
- const char *listen_addr, const char *listen_port);
-
-int server_validate_ctx(const server_ctx *ctx);
-
-int server_setup_event(server_ctx *srv_ctx[], size_t siz, event_ctx **ev_ctx);
-
-pid_t server_daemonize(event_ctx *ev_ctx, server_ctx *srv_ctx[], size_t siz);
-
-#endif
diff --git a/src/server_ssh.h b/src/server_ssh.h
index f1d6bcd..291478b 100644
--- a/src/server_ssh.h
+++ b/src/server_ssh.h
@@ -3,7 +3,6 @@
#include <libssh/server.h>
-#include "server.h"
#include "forward.h"
int ssh_init_cb(struct forward_ctx *ctx);
diff --git a/src/utils.c b/src/utils.c
index f91173a..9f42fd8 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -253,7 +253,7 @@ int safe_chroot(const char *newroot)
int dir_is_mountpoint(const char *path)
{
struct stat current = {0}, parent = {0};
- size_t plen = strlen(path);
+ size_t plen = strnlen(path, PATH_MAX);
char parent_path[plen + 4];
if (stat(path, &current))