aboutsummaryrefslogtreecommitdiff
path: root/src/state.c
diff options
context:
space:
mode:
authorJulien Dessaux2018-08-26 15:55:38 +0200
committerJulien Dessaux2018-08-26 16:04:11 +0200
commitbeff818f25ae69cfe8501e18271b2509320de8df (patch)
treed132e9d366aaebac9f7b91b8a63fcafbbe550af8 /src/state.c
parentAdded address sanitization and fixed found bugs (diff)
downloadbastion-beff818f25ae69cfe8501e18271b2509320de8df.tar.gz
bastion-beff818f25ae69cfe8501e18271b2509320de8df.tar.bz2
bastion-beff818f25ae69cfe8501e18271b2509320de8df.zip
Added session recording with https://github.com/kilobyte/termrec
Diffstat (limited to 'src/state.c')
-rw-r--r--src/state.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/state.c b/src/state.c
new file mode 100644
index 0000000..646fabe
--- /dev/null
+++ b/src/state.c
@@ -0,0 +1,83 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "../config.h"
+#include "state.h"
+
+struct state {
+ char * destination;
+ char * username;
+ int session_id;
+ int padding;
+};
+
+static struct state state = {0};
+
+char // returns 0 if ok, greater than 0 otherwise
+state_set_ssh_destination(const char * name)
+{
+ if (state.destination != NULL) {
+ fprintf(stderr, "BUG found, attempting to overwrite state.destination that has already been set\n");
+ return 1;
+ }
+ size_t len = strnlen(name, MAX_HOSTNAME_LENGTH + 1);
+ if (len >= MAX_HOSTNAME_LENGTH + 1) {
+ fprintf(stderr, "Hostname too long, max length is %d.\n", MAX_HOSTNAME_LENGTH);
+ return 2;
+ }
+ state.destination = malloc(len+1);
+ strncpy(state.destination, name, len+1);
+ return 0;
+}
+
+const char * state_get_ssh_destination(void)
+{
+ return state.destination;
+}
+
+char // return 0 if ok, greater than 0 otherwise
+state_set_username(const char * name)
+{
+ if (state.username != NULL) {
+ fprintf(stderr, "BUG found, attempting to overwrite state.username that has already been set\n");
+ return 1;
+ }
+ size_t len = strnlen(name, MAX_USERNAME_LENGTH + 1);
+ if (len >= MAX_USERNAME_LENGTH + 1) {
+ fprintf(stderr, "Username too long, max length is %d.\n", MAX_USERNAME_LENGTH);
+ return 1;
+ }
+ state.username = malloc(len+1);
+ strncpy(state.username, name, len+1);
+ return 0;
+}
+
+const char * state_get_username(void)
+{
+ return state.username;
+}
+
+char // return 0 if ok, greater than 0 otherwise
+state_set_session_id(const int id)
+{
+ if (state.session_id != 0) {
+ fprintf(stderr, "BUG found, attempting to overwrite state.username that has already been set\n");
+ return 1;
+ }
+ state.session_id = id;
+ return 0;
+}
+
+int state_get_session_id(void)
+{
+ return state.session_id;
+}
+
+void state_clean(void)
+{
+ free(state.destination);
+ state.destination = NULL;
+ free(state.username);
+ state.username = NULL;
+}