aboutsummaryrefslogtreecommitdiff
path: root/bastion/state.c
blob: 6784c0ab8b77ec7e7c1b29db9d2a410943d4bf5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "common/config.h"
#include "state.h"

struct state {
    unsigned long long session_id;
    char * destination;
    char * bastion_username;
};

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_bastion_username(const char * name)
{
    if (state.bastion_username != NULL) {
        fprintf(stderr, "BUG found, attempting to overwrite state.bastion_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.bastion_username = malloc(len+1);
    strncpy(state.bastion_username, name, len+1);
    return 0;
}

const char * state_get_bastion_username(void)
{
    return state.bastion_username;
}

char // return 0 if ok, greater than 0 otherwise
state_set_session_id(const unsigned long long id)
{
    if (state.session_id != 0) {
        fprintf(stderr, "BUG found, attempting to set a state.session_id that has already been set\n");
        return 1;
    }
    state.session_id = id;
    return 0;
}

unsigned long long state_get_session_id(void)
{
    return state.session_id;
}

void state_clean(void)
{
    free(state.destination);
    state.destination = NULL;
}