aboutsummaryrefslogtreecommitdiff
path: root/src/system.c
blob: a564769f013abc2b65190c948e2066c5c20b6cef (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "system.h"
#include "core.h"

int shell(struct Process *proc, char *args) {
    struct Process selfproc;
    pid_t pid;
    pid_t status;
    status = 0;
    errno = 0;

    if (!proc) {
        // provide our own proc structure
        // albeit not accessible to the user
        memset(&selfproc, 0, sizeof(selfproc));
        proc = &selfproc;
    }

    if (!args) {
        proc->returncode = -1;
        return -1;
    }

    FILE *tp = NULL;
    char *t_name;
    t_name = xmkstemp(&tp, "w");
    if (!t_name || !tp) {
        return -1;
    }

    fprintf(tp, "#!/bin/bash\n%s\n", args);
    fflush(tp);
    fclose(tp);

    // Set the script's permissions so that only the calling user can use it
    // This should help prevent eavesdropping if keys are applied in plain-text
    // somewhere.
    chmod(t_name, 0700);

    pid = fork();
    if (pid == -1) {
        fprintf(stderr, "fork failed\n");
        exit(1);
    } else if (pid == 0) {
        FILE *fp_out = NULL;
        FILE *fp_err = NULL;

        if (strlen(proc->f_stdout)) {
            fp_out = freopen(proc->f_stdout, "w+", stdout);
        }

        if (strlen(proc->f_stderr)) {
            if (!proc->redirect_stderr) {
                fp_err = freopen(proc->f_stderr, "w+", stderr);
            }
        }

        if (proc->redirect_stderr) {
            if (fp_err) {
                fclose(fp_err);
                fclose(stderr);
            }
            dup2(fileno(stdout), fileno(stderr));
        }

        return execl("/bin/bash", "bash", "--norc", t_name, (char *) NULL);
    } else {
        if (waitpid(pid, &status, WUNTRACED) > 0) {
            if (WIFEXITED(status) && WEXITSTATUS(status)) {
                if (WEXITSTATUS(status) == 127) {
                    fprintf(stderr, "execv failed\n");
                }
            } else if (WIFSIGNALED(status))  {
                fprintf(stderr, "signal received: %d\n", WIFSIGNALED(status));
            }
        } else {
            fprintf(stderr, "waitpid() failed\n");
        }
    }

    if (!access(t_name, F_OK)) {
        remove(t_name);
    }

    proc->returncode = status;
    guard_free(t_name);
    return WEXITSTATUS(status);
}

int shell_safe(struct Process *proc, char *args) {
    FILE *fp;
    char buf[1024] = {0};
    int result;

    char *invalid_ch = strpbrk(args, STASIS_SHELL_SAFE_RESTRICT);
    if (invalid_ch) {
        args = NULL;
    }

    result = shell(proc, args);
    if (strlen(proc->f_stdout)) {
        fp = fopen(proc->f_stdout, "r");
        if (fp) {
            while (fgets(buf, sizeof(buf) - 1, fp)) {
                fprintf(stdout, "%s", buf);
                buf[0] = '\0';
            }
            fclose(fp);
            fp = NULL;
        }
    }
    if (strlen(proc->f_stderr)) {
        fp = fopen(proc->f_stderr, "r");
        if (fp) {
            while (fgets(buf, sizeof(buf) - 1, fp)) {
                fprintf(stderr, "%s", buf);
                buf[0] = '\0';
            }
            fclose(fp);
            fp = NULL;
        }
    }
    return result;
}

char *shell_output(const char *command, int *status) {
    const size_t initial_size = STASIS_BUFSIZ;
    size_t current_size = initial_size;
    char *result = NULL;
    char line[STASIS_BUFSIZ];
    FILE *pp;

    errno = 0;
    *status = 0;
    pp = popen(command, "r");
    if (!pp) {
        *status = -1;
        return NULL;
    }

    if (errno) {
        *status = 1;
    }
    result = calloc(initial_size, sizeof(result));
    memset(line, 0, sizeof(line));
    while (fread(line, sizeof(char), sizeof(line) - 1, pp) != 0) {
        size_t result_len = strlen(result);
        size_t need_realloc = (result_len + strlen(line)) > current_size;
        if (need_realloc) {
            current_size += initial_size;
            char *tmp = realloc(result, sizeof(*result) * current_size);
            if (!tmp) {
                return NULL;
            } else if (tmp != result) {
                result = tmp;
            }
        }
        strcat(result, line);
        memset(line, 0, sizeof(line));
    }
    *status = pclose(pp);
    return result;
}