aboutsummaryrefslogtreecommitdiff
path: root/src/jail.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2018-05-06 22:22:47 +0200
committerToni Uhlig <matzeton@googlemail.com>2018-05-06 22:22:47 +0200
commit14d2b4d0134ac00a6262d5c8229ffe0d3b5ecd48 (patch)
tree1803619405ed6733390198db2f322746eb31accd /src/jail.c
parent9653d78388348ebd47b820a0d9d95bbd885973a0 (diff)
POTD skeleton #43.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/jail.c')
-rw-r--r--src/jail.c68
1 files changed, 44 insertions, 24 deletions
diff --git a/src/jail.c b/src/jail.c
index b45f7c8..749305e 100644
--- a/src/jail.c
+++ b/src/jail.c
@@ -21,6 +21,11 @@ typedef struct jail_prisoner_process {
char *newroot;
} jail_prisoner_process;
+typedef enum connection_state {
+ CON_OK, CON_IN_TERMINATED, CON_OUT_TERMINATED,
+ CON_IN_ERROR, CON_OUT_ERROR
+} connection_state;
+
static int jail_mainloop_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz)
__attribute__((noreturn));
static int jail_accept_client(jail_ctx *ctx[],
@@ -28,6 +33,8 @@ static int jail_accept_client(jail_ctx *ctx[],
static int jail_childfn(jail_prisoner_process *ctx)
__attribute__((noreturn));
static int jail_socket_tty_epoll(jail_prisoner_process *ctx, int tty_fd);
+static connection_state
+jail_socket_tty_io_epoll(struct epoll_event *ev, int dest_fd);
void jail_init_ctx(jail_ctx **ctx, size_t stacksize)
@@ -48,33 +55,20 @@ void jail_init_ctx(jail_ctx **ctx, size_t stacksize)
(*ctx)->stack_beg =
(unsigned char *) (*ctx)->stack_ptr
+ (*ctx)->stacksize;
- assert( (*ctx)->stack_ptr );
}
int jail_setup(jail_ctx *ctx,
const char *listen_addr, const char *listen_port)
{
- int s;
- struct addrinfo *srv_addr = NULL;
-
assert(ctx);
assert(listen_addr || listen_port);
D2("Try to listen on %s:%s",
(listen_addr ? listen_addr : "*"), listen_port);
- s = socket_init_in(listen_addr, listen_port, &srv_addr);
- if (s) {
- E_GAIERR(s, "Could not initialise server socket");
+ if (fwd_setup_server(&ctx->fwd_ctx, listen_addr, listen_port))
return 1;
- }
- if (socket_bind_in(&ctx->sock, &srv_addr)) {
- E_STRERR("Could not bind server socket to %s:%s", listen_addr, listen_port);
+ if (fwd_listen_sock(&ctx->fwd_ctx, NULL))
return 1;
- }
- if (socket_listen_in(&ctx->sock)) {
- E_STRERR("Could not listen on server socket on %s:%s", listen_addr, listen_port);
- return 1;
- }
return 0;
}
@@ -82,7 +76,8 @@ int jail_setup(jail_ctx *ctx,
int jail_validate_ctx(const jail_ctx *ctx)
{
assert(ctx);
- assert(ctx->sock.addr_len > 0);
+ assert(ctx->fwd_ctx.sock.addr_len > 0);
+ assert(ctx->stack_ptr);
return 0;
}
@@ -99,10 +94,10 @@ int jail_setup_epoll(jail_ctx *ctx[], size_t siz)
for (size_t i = 0; i < siz; ++i) {
memset(&ev, 0, sizeof(ev));
- ev.data.fd = ctx[i]->sock.fd;
+ ev.data.fd = ctx[i]->fwd_ctx.sock.fd;
ev.events = EPOLLIN | EPOLLET;
- s = socket_addrtostr_in(&ctx[i]->sock,
+ s = socket_addrtostr_in(&ctx[i]->fwd_ctx.sock,
ctx[i]->host_buf, ctx[i]->service_buf);
if (s) {
E_GAIERR(s, "Convert socket address to string");
@@ -111,7 +106,7 @@ int jail_setup_epoll(jail_ctx *ctx[], size_t siz)
N("Jail service listening on %s:%s: %d",
ctx[i]->host_buf, ctx[i]->service_buf, ev.data.fd);
- s = epoll_ctl(fd, EPOLL_CTL_ADD, ctx[i]->sock.fd, &ev);
+ s = epoll_ctl(fd, EPOLL_CTL_ADD, ctx[i]->fwd_ctx.sock.fd, &ev);
if (s) {
close(fd);
return -3;
@@ -132,7 +127,7 @@ pid_t jail_daemonize(int epoll_fd, jail_ctx *ctx[], size_t siz)
for (i = 0; i < siz; ++i) {
assert(ctx[i]);
- s = socket_addrtostr_in(&ctx[i]->sock,
+ s = socket_addrtostr_in(&ctx[i]->fwd_ctx.sock,
ctx[i]->host_buf, ctx[i]->service_buf);
if (s) {
E_GAIERR(s, "Could not initialise jail daemon socket");
@@ -153,7 +148,7 @@ pid_t jail_daemonize(int epoll_fd, jail_ctx *ctx[], size_t siz)
close(epoll_fd);
for (i = 0; i < siz; ++i)
- socket_close(&ctx[i]->sock);
+ socket_close(&ctx[i]->fwd_ctx.sock);
return p;
}
@@ -215,12 +210,12 @@ static int jail_accept_client(jail_ctx *ctx[],
static jail_prisoner_process *args;
for (i = 0; i < siz; ++i) {
- if (ctx[i]->sock.fd == event->data.fd) {
+ if (ctx[i]->fwd_ctx.sock.fd == event->data.fd) {
args = (jail_prisoner_process *) calloc(1, sizeof(*args));
assert(args);
args->newroot = ctx[i]->newroot;
- if (socket_accept_in(&ctx[i]->sock, &args->client_psock)) {
+ if (socket_accept_in(&ctx[i]->fwd_ctx.sock, &args->client_psock)) {
E_STRERR("Could not accept client connection for fd %d",
args->client_psock.fd);
goto error;
@@ -354,10 +349,16 @@ static int jail_childfn(jail_prisoner_process *ctx)
static int jail_socket_tty_epoll(jail_prisoner_process *ctx, int tty_fd)
{
- int s, fd = epoll_create1(0);
+ int s, fd;
struct epoll_event event = {0,{0}};
+ struct epoll_event *events;
+ sigset_t eset;
assert(ctx);
+ events = (struct epoll_event *) calloc(POTD_MAXEVENTS, sizeof(*events));
+ assert(events);
+
+ fd = epoll_create1(0);
if (fd < 0)
return -1;
@@ -373,5 +374,24 @@ static int jail_socket_tty_epoll(jail_prisoner_process *ctx, int tty_fd)
FATAL("Jail TTY Epoll for client %s:%s",
ctx->host_buf, ctx->service_buf);
+ sigemptyset(&eset);
+
+ while (1) {
+ int n, i;
+
+ n = epoll_pwait(fd, events, POTD_MAXEVENTS, -1, &eset);
+ if (n < 0)
+ break;
+
+ for (i = 0; i < n; ++i) {
+ }
+ }
+
return 0;
}
+
+static connection_state
+jail_socket_tty_io_epoll(struct epoll_event *ev, int dest_fd)
+{
+ return CON_OK;
+}