summaryrefslogtreecommitdiffstats
path: root/plugins/python/pyhelpers.c
blob: 882b31a6baf4da5aa9b0d7823c0e4c9494d76f49 (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2019-2020 Robert Manner <robert.manner@oneidentity.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include "pyhelpers.h"

#include <pwd.h>
#include <signal.h>
#include "pathnames.h"

static int
_sudo_printf_default(int msg_type, const char *fmt, ...)
{
    FILE *fp = stdout;
    FILE *ttyfp = NULL;
    va_list ap;
    int len;

    if (ISSET(msg_type, SUDO_CONV_PREFER_TTY)) {
	/* Try writing to /dev/tty first. */
	ttyfp = fopen(_PATH_TTY, "w");
    }

    switch (msg_type & 0xff) {
    case SUDO_CONV_ERROR_MSG:
	fp = stderr;
	FALLTHROUGH;
    case SUDO_CONV_INFO_MSG:
	va_start(ap, fmt);
	len = vfprintf(ttyfp ? ttyfp : fp, fmt, ap);
	va_end(ap);
	break;
    default:
	len = -1;
	errno = EINVAL;
	break;
    }

    if (ttyfp != NULL)
	fclose(ttyfp);

    return len;
}


struct PythonContext py_ctx = {
    .sudo_log = &_sudo_printf_default,
};


char *
py_join_str_list(PyObject *py_str_list, const char *separator)
{
    debug_decl(py_join_str_list, PYTHON_DEBUG_INTERNAL);

    char *result = NULL;
    PyObject *py_separator = NULL;
    PyObject *py_str = NULL;

    py_separator = PyUnicode_FromString(separator);
    if (py_separator == NULL)
        goto cleanup;

    py_str = PyObject_CallMethod(py_separator, "join", "(O)", py_str_list);
    if (py_str == NULL) {
        goto cleanup;
    }

    const char *str = PyUnicode_AsUTF8(py_str);
    if (str != NULL) {
        result = strdup(str);
    }

cleanup:
    Py_XDECREF(py_str);
    Py_XDECREF(py_separator);

    debug_return_str(result);
}

static char *
py_create_traceback_string(PyObject *py_traceback)
{
    debug_decl(py_create_traceback_string, PYTHON_DEBUG_INTERNAL);
    if (py_traceback == NULL)
        debug_return_str(strdup(""));

    char* traceback = NULL;


    PyObject *py_traceback_module = PyImport_ImportModule("traceback");
    if (py_traceback_module == NULL) {
        PyErr_Clear(); // do not care, we just won't show backtrace
    } else {
        PyObject *py_traceback_str_list = PyObject_CallMethod(py_traceback_module, "format_tb", "(O)", py_traceback);

        if (py_traceback_str_list != NULL) {
            traceback = py_join_str_list(py_traceback_str_list, "");
            Py_DECREF(py_traceback_str_list);
        }

        Py_CLEAR(py_traceback_module);
    }

    debug_return_str(traceback ? traceback : strdup(""));
}

void
py_log_last_error(const char *context_message)
{
    debug_decl(py_log_last_error, PYTHON_DEBUG_INTERNAL);
    if (!PyErr_Occurred()) {
        py_sudo_log(SUDO_CONV_ERROR_MSG, "%s\n", context_message);
        debug_return;
    }

    PyObject *py_type = NULL, *py_message = NULL, *py_traceback = NULL;
    PyErr_Fetch(&py_type, &py_message, &py_traceback);

    char *message = py_message ? py_create_string_rep(py_message) : NULL;

    py_sudo_log(SUDO_CONV_ERROR_MSG, "%s%s%s\n",
                context_message ? context_message : "",
                context_message && *context_message ? ": " : "",
                message ? message : "(NULL)");
    free(message);

    if (py_traceback != NULL) {
        char *traceback = py_create_traceback_string(py_traceback);
        py_sudo_log(SUDO_CONV_INFO_MSG, "Traceback:\n%s\n", traceback);
        free(traceback);
    }

    Py_XDECREF(py_type);
    Py_XDECREF(py_message);
    Py_XDECREF(py_traceback);
    debug_return;
}

PyObject *
py_str_array_to_tuple_with_count(Py_ssize_t count, char * const strings[])
{
    debug_decl(py_str_array_to_tuple_with_count, PYTHON_DEBUG_INTERNAL);

    PyObject *py_argv = PyTuple_New(count);
    if (py_argv == NULL)
        debug_return_ptr(NULL);

    for (int i = 0; i < count; ++i) {
        PyObject *py_arg = PyUnicode_FromString(strings[i]);
        if (py_arg == NULL || PyTuple_SetItem(py_argv, i, py_arg) != 0) {
            Py_CLEAR(py_argv);
            break;
        }
    }

    debug_return_ptr(py_argv);
}

PyObject *
py_str_array_to_tuple(char * const strings[])
{
    debug_decl(py_str_array_to_tuple, PYTHON_DEBUG_INTERNAL);

    // find the item count ("strings" ends with NULL terminator):
    Py_ssize_t count = 0;
    if (strings != NULL) {
        while (strings[count] != NULL)
            ++count;
    }

    debug_return_ptr(py_str_array_to_tuple_with_count(count, strings));
}

char **
py_str_array_from_tuple(PyObject *py_tuple)
{
    debug_decl(py_str_array_from_tuple, PYTHON_DEBUG_INTERNAL);

    if (!PyTuple_Check(py_tuple)) {
        PyErr_Format(PyExc_ValueError, "%s: value error, argument should be a tuple but it is '%s'",
                     __func__, Py_TYPENAME(py_tuple));
        debug_return_ptr(NULL);
    }

    Py_ssize_t tuple_size = PyTuple_Size(py_tuple);

    // we need an extra 0 at the end
    char **result = calloc(tuple_size + 1, sizeof(char *));
    if (result == NULL) {
        debug_return_ptr(NULL);
    }

    for (int i = 0; i < tuple_size; ++i) {
        PyObject *py_value = PyTuple_GetItem(py_tuple, i);
        if (py_value == NULL) {
            str_array_free(&result);
            debug_return_ptr(NULL);
        }

        // Note that it can be an "int" or something else as well
        char *value = py_create_string_rep(py_value);
        if (value == NULL) {
            // conversion error is already set
            str_array_free(&result);
            debug_return_ptr(NULL);
        }
        result[i] = value;
    }

    debug_return_ptr(result);
}

PyObject *
py_tuple_get(PyObject *py_tuple, Py_ssize_t idx, PyTypeObject *expected_type)
{
    debug_decl(py_tuple_get, PYTHON_DEBUG_INTERNAL);

    PyObject *py_item = PyTuple_GetItem(py_tuple, idx);
    if (py_item == NULL) {
        debug_return_ptr(NULL);
    }

    if (!PyObject_TypeCheck(py_item, expected_type)) {
        PyErr_Format(PyExc_ValueError, "Value error: tuple element %d should "
                                       "be a '%s' (but it is '%s')",
                     idx, expected_type->tp_name, Py_TYPENAME(py_item));
        debug_return_ptr(NULL);
    }

    debug_return_ptr(py_item);
}

PyObject *
py_create_version(unsigned int version)
{
    debug_decl(py_create_version, PYTHON_DEBUG_INTERNAL);
    debug_return_ptr(PyUnicode_FromFormat("%d.%d", SUDO_API_VERSION_GET_MAJOR(version),
                                          SUDO_API_VERSION_GET_MINOR(version)));
}

PyObject *
py_from_passwd(const struct passwd *pwd)
{
    debug_decl(py_from_passwd, PYTHON_DEBUG_INTERNAL);

    if (pwd == NULL) {
        debug_return_ptr_pynone;
    }

    // Create a tuple similar and convertible to python "struct_passwd" of "pwd" module
    debug_return_ptr(
        Py_BuildValue("(zziizzz)", pwd->pw_name, pwd->pw_passwd,
                      pwd->pw_uid, pwd->pw_gid, pwd->pw_gecos,
                      pwd->pw_dir, pwd->pw_shell)
    );
}

char *
py_create_string_rep(PyObject *py_object)
{
    debug_decl(py_create_string_rep, PYTHON_DEBUG_INTERNAL);
    char *result = NULL;

    if (py_object == NULL)
        debug_return_ptr(NULL);

    PyObject *py_string = PyObject_Str(py_object);
    if (py_string != NULL) {
        const char *bytes = PyUnicode_AsUTF8(py_string);
        if (bytes != NULL) {
	    /*
	     * Convert from old format w/ numeric value to new without it.
	     * Old: (<DEBUG.ERROR: 2>, 'ERROR level debug message')
	     * New: (DEBUG.ERROR, 'ERROR level debug message')
	     */
	    if (bytes[0] == '(' && bytes[1] == '<') {
		const char *colon = strchr(bytes + 2, ':');
		if (colon != NULL && colon[1] == ' ') {
		    const char *cp = colon + 2;
		    while (isdigit((unsigned char)*cp))
			cp++;
		    if (cp[0] == '>' && (cp[1] == ',' || cp[1] == '\0')) {
			bytes += 2;
			if (asprintf(&result, "(%.*s%s", (int)(colon - bytes),
				bytes, cp + 1) == -1) {
			    result = NULL;
			    goto done;
			}
		    }
		}
	    }
	    if (result == NULL)
		result = strdup(bytes);
        }
    }

done:
    Py_XDECREF(py_string);
    debug_return_ptr(result);
}

static void
_py_debug_python_function(const char *class_name, const char *function_name, const char *message,
                          PyObject *py_args, PyObject *py_kwargs, int subsystem_id)
{
    debug_decl_vars(_py_debug_python_function, subsystem_id);

    if (sudo_debug_needed(SUDO_DEBUG_DIAG)) {
        char *args_str = NULL;
        char *kwargs_str = NULL;
        if (py_args != NULL) {
            /* Sort by key for consistent output on Python < 3.6 */
	    PyObject *py_args_sorted = NULL;
	    if (PyDict_Check(py_args)) {
		py_args_sorted = PyDict_Items(py_args);
		if (py_args_sorted != NULL) {
		    if (PyList_Sort(py_args_sorted) == 0) {
			py_args = py_args_sorted;
		    }
		}
	    }
            args_str = py_create_string_rep(py_args);
	    if (args_str != NULL && strncmp(args_str, "RC.", 3) == 0) {
		/* Strip leading RC. to match python 3.10 behavior. */
		memmove(args_str, args_str + 3, strlen(args_str + 3) + 1);
	    }
            if (py_args_sorted != NULL)
                Py_DECREF(py_args_sorted);
        }
        if (py_kwargs != NULL) {
            /* Sort by key for consistent output on Python < 3.6 */
            PyObject *py_kwargs_sorted = NULL;
	    if (PyDict_Check(py_kwargs)) {
		py_kwargs_sorted = PyDict_Items(py_kwargs);
		if (py_kwargs_sorted != NULL) {
		    if (PyList_Sort(py_kwargs_sorted) == 0) {
			py_kwargs = py_kwargs_sorted;
		    }
		}
	    }
            kwargs_str = py_create_string_rep(py_kwargs);
            if (py_kwargs_sorted != NULL)
                Py_DECREF(py_kwargs_sorted);
        }

        sudo_debug_printf(SUDO_DEBUG_DIAG, "%s.%s %s: %s %s\n", class_name,
                          function_name, message, args_str ? args_str : "()",
                          kwargs_str ? kwargs_str : "");
        free(args_str);
        free(kwargs_str);
    }
}

void
py_debug_python_call(const char *class_name, const char *function_name,
                     PyObject *py_args, PyObject *py_kwargs, int subsystem_id)
{
    debug_decl_vars(py_debug_python_call, subsystem_id);

    if (subsystem_id == PYTHON_DEBUG_C_CALLS && sudo_debug_needed(SUDO_DEBUG_INFO)) {
        // at this level we also output the callee python script
        char *callee_func_name = NULL, *callee_file_name = NULL;
        long callee_line_number = -1;

        if (py_get_current_execution_frame(&callee_file_name, &callee_line_number, &callee_func_name) == SUDO_RC_OK) {
            sudo_debug_printf(SUDO_DEBUG_INFO, "%s @ %s:%ld calls C function:\n",
                              callee_func_name, callee_file_name, callee_line_number);
        }

        free(callee_func_name);
        free(callee_file_name);
    }

    _py_debug_python_function(class_name, function_name, "was called with arguments",
                              py_args, py_kwargs, subsystem_id);
}

void
py_debug_python_result(const char *class_name, const char *function_name,
                       PyObject *py_result, int subsystem_id)
{
    if (py_result == NULL) {
        debug_decl_vars(py_debug_python_result, subsystem_id);
        sudo_debug_printf(SUDO_CONV_ERROR_MSG, "%s.%s call failed\n",
                          class_name, function_name);
    } else {
        _py_debug_python_function(class_name, function_name, "returned result",
                                  py_result, NULL, subsystem_id);
    }
}

void
str_array_free(char ***array)
{
    debug_decl(str_array_free, PYTHON_DEBUG_INTERNAL);

    if (*array == NULL)
        debug_return;

    for (char **item_ptr = *array; *item_ptr != NULL; ++item_ptr)
        free(*item_ptr);

    free(*array);
    *array = NULL;

    debug_return;
}

int
py_get_current_execution_frame(char **file_name, long *line_number, char **function_name)
{
    *file_name = NULL;
    *line_number = (long)-1;
    *function_name = NULL;

    PyObject *py_err_type = NULL, *py_err_value = NULL, *py_err_traceback = NULL;
    PyErr_Fetch(&py_err_type, &py_err_value, &py_err_traceback);

    PyObject *py_frame = NULL, *py_f_code = NULL,
              *py_filename = NULL, *py_function_name = NULL;

    PyObject *py_getframe = PySys_GetObject("_getframe");
    if (py_getframe == NULL)
        goto cleanup;

    py_frame = PyObject_CallFunction(py_getframe, "i", 0);
    if (py_frame == NULL)
        goto cleanup;

    *line_number = py_object_get_optional_attr_number(py_frame, "f_lineno");

    py_f_code = py_object_get_optional_attr(py_frame, "f_code", NULL);
    if (py_f_code != NULL) {
        py_filename = py_object_get_optional_attr(py_f_code, "co_filename", NULL);
        if (py_filename != NULL)
            *file_name = strdup(PyUnicode_AsUTF8(py_filename));

        py_function_name = py_object_get_optional_attr(py_f_code, "co_name", NULL);
        if (py_function_name != NULL)
            *function_name = strdup(PyUnicode_AsUTF8(py_function_name));
    }

cleanup:
    Py_CLEAR(py_frame);
    Py_CLEAR(py_f_code);
    Py_CLEAR(py_filename);
    Py_CLEAR(py_function_name);

    // we hide every error happening inside this function
    PyErr_Restore(py_err_type, py_err_value, py_err_traceback);

    return (*file_name && *function_name && (*line_number >= 0)) ?
                SUDO_RC_OK : SUDO_RC_ERROR;
}

void
py_ctx_reset()
{
    memset(&py_ctx, 0, sizeof(py_ctx));
    py_ctx.sudo_log = &_sudo_printf_default;
}

int
py_sudo_conv(int num_msgs, const struct sudo_conv_message msgs[],
             struct sudo_conv_reply replies[], struct sudo_conv_callback *callback)
{
    /* Enable suspend during password entry. */
    struct sigaction sa, saved_sigtstp;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    sa.sa_handler = SIG_DFL;
    (void) sigaction(SIGTSTP, &sa, &saved_sigtstp);

    int rc = SUDO_RC_ERROR;
    if (py_ctx.sudo_conv != NULL)
        rc = py_ctx.sudo_conv((int)num_msgs, msgs, replies, callback);

    /* Restore signal handlers and signal mask. */
    (void) sigaction(SIGTSTP, &saved_sigtstp, NULL);

    return rc;
}

PyObject *
py_object_get_optional_attr(PyObject *py_object, const char *attr, PyObject *py_default)
{
    if (PyObject_HasAttrString(py_object, attr)) {
        return PyObject_GetAttrString(py_object, attr);
    }
    Py_XINCREF(py_default);  // whatever we return will have its refcount incremented
    return py_default;
}

const char *
py_object_get_optional_attr_string(PyObject *py_object, const char *attr_name)
{
    PyObject *py_value = py_object_get_optional_attr(py_object, attr_name, NULL);
    if (py_value == NULL)
        return NULL;

    const char *value = PyUnicode_AsUTF8(py_value);
    Py_CLEAR(py_value); // Note, the object still has reference to the attribute
    return value;
}

long long
py_object_get_optional_attr_number(PyObject *py_object, const char *attr_name)
{
    PyObject *py_value = py_object_get_optional_attr(py_object, attr_name, NULL);
    if (py_value == NULL)
        return -1;

    long long value = PyLong_AsLongLong(py_value);
    Py_CLEAR(py_value);
    return value;
}

void
py_object_set_attr_number(PyObject *py_object, const char *attr_name, long long number)
{
    PyObject *py_number = PyLong_FromLong(number);
    if (py_number == NULL)
        return;

    PyObject_SetAttrString(py_object, attr_name, py_number);
    Py_CLEAR(py_number);
}

void
py_object_set_attr_string(PyObject *py_object, const char *attr_name, const char *value)
{
    PyObject *py_value = PyUnicode_FromString(value);
    if (py_value == NULL)
        return;

    PyObject_SetAttrString(py_object, attr_name, py_value);
    Py_CLEAR(py_value);
}

PyObject *
py_dict_create_string_int(size_t count, struct key_value_str_int *key_values)
{
    debug_decl(py_dict_create_string_int, PYTHON_DEBUG_INTERNAL);

    PyObject *py_value = NULL;
    PyObject *py_dict = PyDict_New();
    if (py_dict == NULL)
        goto cleanup;

    for (size_t i = 0; i < count; ++i) {
        py_value = PyLong_FromLong(key_values[i].value);
        if (py_value == NULL)
            goto cleanup;

        if (PyDict_SetItemString(py_dict, key_values[i].key, py_value) < 0)
            goto cleanup;

        Py_CLEAR(py_value);
    }

cleanup:
    if (PyErr_Occurred()) {
        Py_CLEAR(py_dict);
    }
    Py_CLEAR(py_value);

    debug_return_ptr(py_dict);
}