summaryrefslogtreecommitdiff
path: root/server/server-ssh.c
blob: 07bfb012b6729e7bda1b72ebfd01341a5ab08107 (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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * ssh protocol, server implementation.
 *
 * Implements remctl over ssh using regular commands and none of the remctl
 * protocol.  The only part of the normal remctl server reused here is the
 * configuration and command running code.
 *
 * Written by Russ Allbery
 * Copyright 2016 Dropbox, Inc.
 *
 * See LICENSE for licensing terms.
 */
 
#include <config.h>
#include <portable/event.h>
#include <portable/system.h>

#include <ctype.h>

#include <server/internal.h>
#include <util/buffer.h>
#include <util/macros.h>
#include <util/messages.h>
#include <util/vector.h>
#include <util/xmalloc.h>
#include <util/xwrite.h>


/*
 * Parse a command string into a remctl command.  This is much more complex
 * for remctl-shell than it is for remctld since we get the command as a
 * string with shell quoting and have to understand and undo the quoting.
 *
 * Implements single and double quotes, with backslash escaping any character.
 */
struct iovec **
server_ssh_parse_command(const char *command)
{
    struct vector *args;
    struct buffer *arg;
    struct iovec **argv;
    const char *p;
    size_t i, length;
    char quote = '\0';
    enum state {
        SEPARATOR,
        ARG,
        QUOTE
    } state;

    /*
     * Parse the string using a state engine.  We can be in one of three
     * states: in the separator between arguments, or inside a quoted string.
     * If inside a quoted string, the quote used to terminate the string is
     * stored in quote.
     *
     * Backslash escapes any character inside or outside quotes.  If backslash
     * is at the end of the string, we just treat it as a literal backslash.
     */
    args = vector_new();
    arg = buffer_new();
    state = SEPARATOR;
    for (p = command; p != '\0'; p++) {
        if (*p == '\\' && p[1] != '\0') {
            buffer_append(arg, p + 1, 1);
            if (state == SEPARATOR)
                state = ARG;
            continue;
        }
        switch (state) {
        case SEPARATOR:
            if (!isspace((int) *p)) {
                switch (*p) {
                case '\'':
                case '"':
                    state = QUOTE;
                    quote = *p;
                    break;
                default:
                    state = ARG;
                    buffer_append(arg, p, 1);
                    break;
                }
            }
            break;
        case QUOTE:
            if (*p == quote)
                state = ARG;
            else
                buffer_append(arg, p, 1);
            break;
        case ARG:
            if (isspace((int) *p)) {
                vector_addn(args, arg->data, arg->left);
                buffer_set(arg, NULL, 0);
                state = SEPARATOR;
            } else {
                switch (*p) {
                case '\'':
                case '"':
                    state = QUOTE;
                    quote = *p;
                    break;
                default:
                    buffer_append(arg, p, 1);
                    break;
                }
            }
            break;
        }
    }

    /*
     * Ending inside a quoted string is an error.  Otherwise, recover the last
     * argument and clean up.
     */
    if (state == QUOTE) {
        warn("unterminated %c quote in command", quote);
        goto fail;
    } else if (state == ARG) {
        vector_addn(args, arg->data, arg->left);
    }
    buffer_free(arg);

    /* Turn the vector into the iovec we need for everything else. */
    argv = xcalloc(args->count + 1, sizeof(struct iovec *));
    for (i = 0; i < args->count; i++) {
        argv[i] = xcalloc(1, sizeof(struct iovec));
        length = strlen(args->strings[i]);
        argv[i]->iov_base = xmalloc(length);
        memcpy(argv[i]->iov_base, args->strings[i], length);
        argv[i]->iov_len = length;
    }
    argv[args->count] = NULL;
    vector_free(args);
    return argv;

fail:
    vector_free(args);
    buffer_free(arg);
    return NULL;
}


/*
 * Handle one block of output from the running command.
 */
static void
handle_output(struct bufferevent *bev, void *data)
{
    int fd;
    struct evbuffer *buf;
    struct process *process = data;
    struct client *client = process->client;

    process->saw_output = true;
    fd = (bev == process->inout) ? client->fd : client->stderr_fd;
    buf = bufferevent_get_input(bev);
    if (evbuffer_write(buf, fd) < 0) {
        syswarn("error sending output");
        client->fatal = true;
        process->saw_error = true;
        event_base_loopbreak(process->loop);
    }
}


/*
 * Set up to execute a command.  For the ssh protocol, all we need to do is
 * install output handlers for both stdout and stderr that just send the
 * output back to our stdout and stderr.
 */
static void
command_setup(struct process *process)
{
    bufferevent_data_cb writecb;

    writecb = (process->input == NULL) ? NULL : server_handle_input_end;
    bufferevent_setcb(process->inout, handle_output, writecb,
                      server_handle_io_event, process);
    bufferevent_setwatermark(process->inout, EV_READ, 0, TOKEN_MAX_OUTPUT);
    bufferevent_enable(process->err, EV_READ);
    bufferevent_setcb(process->err, handle_output, NULL,
                      server_handle_io_event, process);
    bufferevent_setwatermark(process->err, EV_READ, 0, TOKEN_MAX_OUTPUT);
}


/*
 * Handle the end of the command.  For the ssh protocol, we do nothing, since
 * the main program will collect the exit status and exit with the appropriate
 * status in order to communicate it back to the caller.
 */
static bool
command_finish(struct client *client UNUSED, struct evbuffer *output UNUSED,
               int exit_status UNUSED)
{
    return true;
}


/*
 * Send an error back over an ssh channel.  This just writes the error message
 * with a trailing newline to standard error.
 */
static bool
send_error(struct client *client, enum error_codes code UNUSED,
           const char *message)
{
    ssize_t status;

    status = xwrite(client->stderr_fd, message, strlen(message));
    if (status >= 0)
        status = xwrite(client->stderr_fd, "\n", 1);
    if (status < 0) {
        syswarn("error sending error message");
        client->fatal = true;
        return false;
    }
    return true;
}


/*
 * Create a client struct for a remctl-shell invocation based on the ssh
 * environment.  Abort here if the expected ssh environment variables aren't
 * set.  Caller is responsible for freeing the allocated client struct.
 */
struct client *
server_ssh_new_client(void)
{
    struct client *client;
    const char *ssh_client, *user;
    struct vector *client_info;

    /* Parse client identity from ssh environment variables. */
    user = getenv("REMCTL_USER");
    if (user == NULL)
        die("REMCTL_USER must be set in the environment via authorized_keys");
    ssh_client = getenv("SSH_CLIENT");
    if (ssh_client == NULL)
        die("SSH_CLIENT not set (remctl-shell must be run via ssh)");
    client_info = vector_split_space(ssh_client, NULL);

    /* Create basic client struct. */
    client = xcalloc(1, sizeof(struct client));
    client->fd = STDOUT_FILENO;
    client->stderr_fd = STDERR_FILENO;
    client->ipaddress = xstrdup(client_info->strings[0]);
    client->protocol = 3;
    client->user = xstrdup(user);

    /* Add ssh protocol callbacks. */
    client->setup = command_setup;
    client->finish = command_finish;
    client->error = send_error;

    /* Free allocated data and return. */
    vector_free(client_info);
    return client;
}


/*
 * Free a client struct, including any resources that it holds.  This is a
 * subset of server_free_client that doesn't do the GSS-API actions.
 */
void
server_ssh_free_client(struct client *client)
{
    if (client == NULL)
        return;
    if (client->fd >= 0)
        close(client->fd);
    if (client->stderr_fd >= 0)
        close(client->stderr_fd);
    free(client->user);
    free(client->hostname);
    free(client->ipaddress);
    free(client);
}