summaryrefslogtreecommitdiffstats
path: root/src/sss_client/sudo_plugin/sss_sudoplugin.c
blob: 9e2dfce0eb490d595efe363246f0ec78840fd043 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2011 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sudo_plugin.h>

#include "sss_client/sudo_plugin/sss_sudoplugin.h"
#include "sss_client/sudo_plugin/sss_sudoplugin_private.h"
#include "sss_client/sss_cli.h"

struct plugin_state {
    sudo_conv_t conversation;
    sudo_printf_t printf;
    char * const *settings;
    char * const *user_info;
    char * const *user_env;
};

int policy_open(unsigned int version,
                sudo_conv_t sudo_conversation,
                sudo_printf_t sudo_printf,
                char * const settings[],
                char * const user_info[],
                char * const user_env[]);

void policy_close(int exit_status,
                  int error);

int policy_version(int verbose);

int policy_list(int argc,
                char * const argv[],
                int verbose,
                const char *list_user);

int policy_check(int argc,
                 char * const argv[],
                 char *env_add[],
                 char **command_out[],
                 char **argv_out[],
                 char **user_env_out[]);

static errno_t policy_check_create_query(const char *qualified_command_path,
                                         char * const *settings,
                                         char * const *user_info,
                                         char * const *user_env,
                                         char * const *env_add,
                                         int argc,
                                         char * const argv[],
                                         char **query,
                                         int *query_length);

static errno_t policy_check_parse_response(char *response,
                                           int response_length,
                                           int *result,
                                           char ***_command_out,
                                           char ***_argv_out,
                                           char ***_user_env_out);


/* SUDO Plugin structure */
struct policy_plugin sss_sudo_policy = {
    SUDO_POLICY_PLUGIN,
    SUDO_API_VERSION,
    policy_open,
    policy_close,
    policy_version,
    policy_check,
    policy_list,
    NULL, /* validate */
    NULL, /* invalidate */
    NULL /* session init */
};

/* Global plugin state initialized from policy_check with data passed by SUDO */
static struct plugin_state plugin = {NULL, NULL, NULL, NULL, NULL};

int policy_open(unsigned int version,
                sudo_conv_t sudo_conversation,
                sudo_printf_t sudo_printf,
                char * const settings[],
                char * const user_info[],
                char * const user_env[])
{
    if (sudo_conversation == NULL || settings == NULL) {
        return SSS_SUDO_USAGE_ERROR;
    }

    /* check Plugin API version */

    /*
     * TODO: Do we need to check major and minor version or major is enough?
     */
    if (version != SUDO_API_VERSION) {
        sudo_printf(SUDO_CONV_ERROR_MSG, "The SSS SUDO plugin requires "
                                         "API version %d. Your version is %d",
                                         SUDO_API_VERSION, version);

        return SSS_SUDO_GENERAL_ERROR;
    }

    /* save input variables */

    plugin.conversation = sudo_conversation;
    plugin.printf = sudo_printf;
    plugin.settings = settings;
    plugin.user_info = user_info;
    plugin.user_env = user_env;

    return SSS_SUDO_SUCCESS;
}

void policy_close(int exit_status, int error)
{
    if (error) {
        plugin.printf(SUDO_CONV_ERROR_MSG, "Command error: %s\n", strerror(error));
    } else if (WIFEXITED(exit_status)) {
        plugin.printf(SUDO_CONV_INFO_MSG, "Command exited with status %d\n",
                    WEXITSTATUS(exit_status));
    } else if (WIFSIGNALED(exit_status)) {
        plugin.printf(SUDO_CONV_INFO_MSG, "Command was killed by signal %d\n",
                    WTERMSIG(exit_status));
    }

    return;
}

int policy_version(int verbose)
{
    return SSS_SUDO_SUCCESS;
}

int policy_list(int argc,
                char * const argv[],
                int verbose,
                const char *list_user)
{
    return SSS_SUDO_SUCCESS;
}

int policy_check(int argc,
                 char * const argv[],
                 char *env_add[],
                 char **command_out[],
                 char **argv_out[],
                 char **user_env_out[])
{
    char *qualified_command_path = NULL;
    char *query = NULL;
    int query_length = 0;
    int ret = 0;
    int errnop;
    int sudo_result = SSS_SUDO_FAILURE;
    struct sss_cli_req_data request_data;
    uint8_t *reply_buf = NULL;
    size_t reply_len;

    if (argc <= 0) {
        plugin.printf(SUDO_CONV_ERROR_MSG, "sudo: no command specified!\n");
        return SSS_SUDO_USAGE_ERROR;
    }

    /* Does command exists? */
    errno = 0;
    qualified_command_path = get_qualified_command_path(argv[0]);
    if (errno) {
        if (qualified_command_path == NULL) {
            plugin.printf(SUDO_CONV_ERROR_MSG,
                          "sudo: %s: %s\n", argv[0], strerror(errno));
        } else {
            plugin.printf(SUDO_CONV_ERROR_MSG,
                          "sudo: %s: %s\n", qualified_command_path, strerror(errno));
        }
        return SSS_SUDO_GENERAL_ERROR;
    }

    /* create query data */
    ret = policy_check_create_query(qualified_command_path, plugin.settings,
                                    plugin.user_info, plugin.user_env,
                                    env_add, argc, argv,
                                    &query, &query_length);
    if (ret != EOK) {
        plugin.printf(SUDO_CONV_ERROR_MSG,
                      "sudo: unable to create query string: %s\n",
                      strerror(ret));
        ret = SSS_SUDO_GENERAL_ERROR;
        goto done;
    }
    request_data.len = query_length;
    request_data.data = (const void*)query;

    /* send query */
    errnop = 0;
    ret = sss_sudo_make_request(SSS_SUDO_CHECK, &request_data, &reply_buf,
                                &reply_len, &errnop);
    if (errnop != 0) {
        plugin.printf(SUDO_CONV_ERROR_MSG,
                      "Unable contact SSSD responder: %s\n", strerror(errnop));
        ret = SSS_SUDO_GENERAL_ERROR;
        goto done;
    }

    /* process response */
    ret = policy_check_parse_response((char*)reply_buf, reply_len, &sudo_result,
                                      command_out, argv_out, user_env_out);
    if (ret != EOK) {
        plugin.printf(SUDO_CONV_ERROR_MSG,
                      "sudo: unable to parse response: %s\n",
                      strerror(ret));
        ret = SSS_SUDO_GENERAL_ERROR;
        goto done;
    }

    plugin.printf(SUDO_CONV_INFO_MSG, "CMD Return code: %d\n", sudo_result);
    plugin.printf(SUDO_CONV_INFO_MSG, "errnop: %d\n", errnop);

    switch (sudo_result) {
    case SSS_SUDO_RESPONSE_ALLOW:
        ret = SSS_SUDO_SUCCESS;
        break;
    case SSS_SUDO_RESPONSE_AUTHENTICATE:
        /* user can run the command, but must authenticate himself first */
        /* TODO */
        ret = SSS_SUDO_FAILURE;
        break;
    case SSS_SUDO_RESPONSE_DENY:
        ret = SSS_SUDO_FAILURE;
        break;
    case SSS_SUDO_RESPONSE_UNKNOWN:
        /* possibly local user, run sudoers plugin */
        /* TODO */
        ret = SSS_SUDO_GENERAL_ERROR;
        plugin.printf(SUDO_CONV_ERROR_MSG, "sudo: Unknown user.\n");
        break;
    }

done:
    free(query);

    return ret;
}

/*
 * Creates query string in format:
 * qualified_command_path\0argv[0]\0argv[i]\0\0
 * env_add\0\0user_env\0\0settings\0\0user_info\0\0
 *
 * where env_add, user_env, settings and user_info are in the form of
 * NAME=VALUE pairs.
 */
errno_t policy_check_create_query(const char *qualified_command_path,
                                  char * const *settings,
                                  char * const *user_info,
                                  char * const *user_env,
                                  char * const *env_add,
                                  int argc,
                                  char * const argv[],
                                  char **query,
                                  int *query_length)
{
#define APPEND_ELEMENT(element) do { \
    iter_length = strlen(element) + 1; /* with ending \0 */ \
    data = (char*)realloc(data, data_length + iter_length); \
    if (data == NULL) { \
        return ENOMEM; \
    } \
    memcpy(data + data_length, element, iter_length); \
    data_length += iter_length; \
} while(0)

#define APPEND_ZERO() do { \
    data_length++; \
    data = (char*)realloc(data, data_length); \
    data[data_length] = '\0'; \
} while(0)

    char *data = NULL;
    int data_length = 0;
    char * const *iter = NULL;
    int iter_length = 0;
    int i = 0;
    char * const *fields[] = { env_add, user_env, settings, user_info, NULL };
    char * const **field = NULL;

    /* append qualified_command_path */

    APPEND_ELEMENT(qualified_command_path);

    /* append argv */

    for (i = 0; i < argc; i++) {
        APPEND_ELEMENT(argv[i]);
    }
    APPEND_ZERO();

    /* append NAME=VALUE fields */
    for (field = fields; *field != NULL; field++) {
        if (**field == NULL) {
            APPEND_ZERO();
            APPEND_ZERO();
        } else {
            for (iter = *field; *iter != NULL; iter++) {
                APPEND_ELEMENT(*iter);
            }
            APPEND_ZERO();
        }
    }

    *query_length = data_length;
    *query = data;

    return EOK;

#undef APPEND_ELEMENT
#undef APPEND_ZERO
}

errno_t policy_check_parse_response(char *response,
                                    int response_length,
                                    int *result,
                                    char ***_command_out,
                                    char ***_argv_out,
                                    char ***_user_env_out)
{
#define LOAD_ARRAY(element) do { \
    i = 0; \
    while (*current_position != '\0') { \
        i++; \
        element = (char**)realloc(element, i * sizeof(char*)); \
        element[i - 1] = current_position; \
        current_position = strchr(current_position, '\0'); \
        if (current_position == NULL) { \
            ret = ESPIPE; \
            goto done; \
        } \
        current_position++; \
    } \
    i++; \
    element = (char**)realloc(element, i * sizeof(char*)); \
    element[i - 1] = NULL; \
    current_position++; \
} while(0)

    char *current_position = NULL;
    char **command_out = NULL;
    char **argv_out = NULL;
    char **user_env_out = NULL;
    int i = 0;
    int ret;

    if (response_length <= 0 || response == NULL) {
        return EINVAL;
    }

    /* get responder result */
    memcpy(result, response, sizeof(int));
    current_position = response + sizeof(int);

    /* get argv_out */
    LOAD_ARRAY(argv_out);

    /* get command_out */
    LOAD_ARRAY(command_out);

    /* get user_env_out */
    LOAD_ARRAY(user_env_out);

    /* copy output */
    *_command_out = command_out;
    *_argv_out = argv_out;
    *_user_env_out = user_env_out;

    ret = EOK;

done:
    return ret;

#undef LOAD_ARRAY
}