aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2018-08-11 13:24:36 +0200
committerToni Uhlig <matzeton@googlemail.com>2018-08-11 13:24:36 +0200
commitd6bbb0e218ea74e6205cfc0b180e768e80e1bc31 (patch)
treeca11f45c2f2e2130d0a98528ad67edc1353b414c
parent40035b83f990286767df0db170295b3f354de0d5 (diff)
setup basic jail packet structs/funcs
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--src/Makefile.am2
-rw-r--r--src/jail.c8
-rw-r--r--src/jail_packet.c57
-rw-r--r--src/jail_packet.h56
-rw-r--r--src/pevent.c6
-rw-r--r--src/pevent.h7
-rw-r--r--src/redirector.c6
7 files changed, 130 insertions, 12 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index c2fe07c..6c23607 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,5 @@
sbin_PROGRAMS = potd
-potd_SOURCES = compat.c utils.c options.c log.c log_colored.c log_file.c socket.c pevent.c capabilities.c filesystem.c jail.c forward.c redirector.c protocol.c protocol_ssh.c main.c
+potd_SOURCES = compat.c utils.c options.c log.c log_colored.c log_file.c socket.c pevent.c capabilities.c filesystem.c jail_packet.c jail.c forward.c redirector.c protocol.c protocol_ssh.c main.c
if HAVE_SECCOMP
potd_SOURCES += pseccomp.c
endif
diff --git a/src/jail.c b/src/jail.c
index 18b11c1..8d207a7 100644
--- a/src/jail.c
+++ b/src/jail.c
@@ -161,7 +161,7 @@ int jail_setup_event(jail_ctx *ctx[], size_t siz, event_ctx **ev_ctx)
return 1;
for (size_t i = 0; i < siz; ++i) {
- if (event_add_sock(*ev_ctx, &ctx[i]->fwd_ctx.sock)) {
+ if (event_add_sock(*ev_ctx, &ctx[i]->fwd_ctx.sock, NULL)) {
return 1;
}
@@ -521,12 +521,12 @@ static int jail_socket_tty(prisoner_process *ctx, int tty_fd)
ctx->host_buf, ctx->service_buf, ctx->client_psock.fd);
goto finish;
}
- if (event_add_sock(ev_ctx, &ctx->client_psock)) {
+ if (event_add_sock(ev_ctx, &ctx->client_psock, NULL)) {
E_STRERR("Jail event context for socket %s:%s",
ctx->host_buf, ctx->service_buf);
goto finish;
}
- if (event_add_fd(ev_ctx, tty_fd)) {
+ if (event_add_fd(ev_ctx, tty_fd, NULL)) {
E_STRERR("Jail event context for tty fd %d",
tty_fd);
goto finish;
@@ -544,7 +544,7 @@ static int jail_socket_tty(prisoner_process *ctx, int tty_fd)
E_STRERR("%s", "SIGNAL fd");
goto finish;
}
- if (event_add_fd(ev_ctx, ev_cli.signal_fd)) {
+ if (event_add_fd(ev_ctx, ev_cli.signal_fd, NULL)) {
E_STRERR("Jail SIGNAL fd %d", ev_cli.signal_fd);
goto finish;
}
diff --git a/src/jail_packet.c b/src/jail_packet.c
new file mode 100644
index 0000000..4e5bd64
--- /dev/null
+++ b/src/jail_packet.c
@@ -0,0 +1,57 @@
+#include <arpa/inet.h>
+
+#include "jail_packet.h"
+#include "utils.h"
+
+typedef struct jail_packet {
+ uint8_t type;
+ uint16_t size;
+} jail_packet;
+
+typedef ssize_t (*packet_callback)(jail_packet_ctx *ctx, unsigned char *data,
+ size_t siz);
+
+typedef struct jail_packet_callback {
+ uint8_t type;
+ packet_callback pc;
+} jail_packet_callback;
+
+static ssize_t pkt_header_read(unsigned char *buf, size_t siz);
+static ssize_t pkt_hello(jail_packet_ctx *ctx, unsigned char *data, size_t siz);
+
+#define PKT_CB(type, cb) \
+ { type, cb }
+static const jail_packet_callback jpc[] = {
+ PKT_CB(PKT_INVALID, NULL),
+ PKT_CB(PKT_HELLO, pkt_hello)
+};
+
+
+static ssize_t pkt_header_read(unsigned char *buf, size_t siz)
+{
+ jail_packet *pkt;
+
+ if (siz < sizeof(*pkt))
+ return -1;
+ pkt = (jail_packet *) buf;
+
+ if (pkt->type >= SIZEOF(jpc))
+ return -1;
+
+ pkt->size = ntohs(pkt->size);
+ if (siz < pkt->size)
+ return -1;
+
+ return pkt->size;
+}
+
+static ssize_t pkt_hello(jail_packet_ctx *ctx, unsigned char *data, size_t siz)
+{
+ return -1;
+}
+
+int jail_packet_loop(event_ctx *ctx, jail_packet_ctx *pkt_ctx,
+ on_data_cb on_data)
+{
+ return 1;
+}
diff --git a/src/jail_packet.h b/src/jail_packet.h
new file mode 100644
index 0000000..0fc202d
--- /dev/null
+++ b/src/jail_packet.h
@@ -0,0 +1,56 @@
+/*
+ * jail_packet.h
+ * potd is licensed under the BSD license:
+ *
+ * Copyright (c) 2018 Toni Uhlig <matzeton@googlemail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * - The names of its contributors may not be used to endorse or promote
+ * products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef POTD_JAIL_PACKET_H
+#define POTD_JAIL_PACKET_H 1
+
+#include <stdint.h>
+
+#include "pevent.h"
+
+#define PKT_INVALID 0x0
+#define PKT_HELLO 0x1
+
+typedef enum jail_packet_state {
+ JP_NONE, JP_INVALID, JP_HELLO
+} jail_packet_state;
+
+typedef struct jail_packet_ctx {
+ jail_packet_state pstate;
+} jail_packet_ctx;
+
+
+int jail_packet_loop(event_ctx *ctx, jail_packet_ctx *pkt_ctx,
+ on_data_cb on_data);
+
+#endif
diff --git a/src/pevent.c b/src/pevent.c
index 224c805..d0565f3 100644
--- a/src/pevent.c
+++ b/src/pevent.c
@@ -140,7 +140,7 @@ add_eventbuf(event_ctx *ctx)
return &ctx->buffer_array[ctx->buffer_used - 1];
}
-int event_add_sock(event_ctx *ctx, psocket *sock)
+int event_add_sock(event_ctx *ctx, psocket *sock, void *buf_user_data)
{
int s;
struct epoll_event ev = {0,{0}};
@@ -150,6 +150,7 @@ int event_add_sock(event_ctx *ctx, psocket *sock)
eb = add_eventbuf(ctx);
eb->fd = sock->fd;
+ eb->buf_user_data = buf_user_data;
assert(eb->buf_used == 0);
ev.data.ptr = eb;
@@ -161,7 +162,7 @@ int event_add_sock(event_ctx *ctx, psocket *sock)
return 0;
}
-int event_add_fd(event_ctx *ctx, int fd)
+int event_add_fd(event_ctx *ctx, int fd, void *buf_user_data)
{
int s;
struct epoll_event ev = {0,{0}};
@@ -171,6 +172,7 @@ int event_add_fd(event_ctx *ctx, int fd)
eb = add_eventbuf(ctx);
eb->fd = fd;
+ eb->buf_user_data = buf_user_data;
assert(eb->buf_used == 0);
ev.data.ptr = eb;
diff --git a/src/pevent.h b/src/pevent.h
index 0f387aa..1c50073 100644
--- a/src/pevent.h
+++ b/src/pevent.h
@@ -34,6 +34,7 @@
#ifndef POTD_EVENT_H
#define POTD_EVENT_H 1
+#include <stdio.h>
#include <sys/epoll.h>
#include "socket.h"
@@ -52,6 +53,8 @@ typedef struct event_buf {
char buf[BUFSIZ];
size_t buf_used;
+
+ void *buf_user_data;
} event_buf;
typedef struct event_ctx {
@@ -81,9 +84,9 @@ int event_setup(event_ctx *ctx);
int event_validate_ctx(event_ctx *ctx);
-int event_add_sock(event_ctx *ctx, psocket *sock);
+int event_add_sock(event_ctx *ctx, psocket *sock, void *buf_user_data);
-int event_add_fd(event_ctx *ctx, int fd);
+int event_add_fd(event_ctx *ctx, int fd, void *buf_user_data);
int event_loop(event_ctx *ctx, on_event_cb on_event, void *user_data);
diff --git a/src/redirector.c b/src/redirector.c
index e418224..da7bbd1 100644
--- a/src/redirector.c
+++ b/src/redirector.c
@@ -177,7 +177,7 @@ int redirector_setup_event(redirector_ctx *rdr_ctx[], size_t siz, event_ctx **ev
return 1;
for (size_t i = 0; i < siz; ++i) {
- if (event_add_sock(*ev_ctx, &rdr_ctx[i]->sock)) {
+ if (event_add_sock(*ev_ctx, &rdr_ctx[i]->sock, NULL)) {
return 1;
}
@@ -412,7 +412,7 @@ client_mainloop(void *arg)
args->rdr_ctx->fwd_ctx.host_buf,
args->rdr_ctx->fwd_ctx.service_buf, fwd.fd);
- if (event_add_sock(ev_ctx, &fwd)) {
+ if (event_add_sock(ev_ctx, &fwd, NULL)) {
E_STRERR("Forward event context add to %s:%s forward fd %d",
args->rdr_ctx->fwd_ctx.host_buf,
args->rdr_ctx->fwd_ctx.service_buf, fwd.fd);
@@ -430,7 +430,7 @@ client_mainloop(void *arg)
args->rdr_ctx->fwd_ctx.service_buf, fwd.fd);
goto finish;
}
- if (event_add_sock(ev_ctx, &args->client_sock)) {
+ if (event_add_sock(ev_ctx, &args->client_sock, NULL)) {
E_STRERR("Forward event context add to %s:%s forward fd %d",
args->rdr_ctx->fwd_ctx.host_buf,
args->rdr_ctx->fwd_ctx.service_buf, fwd.fd);