Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/python/sudo_python_debug.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2019-2020 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
#include <string.h>
21
22
#include <ctype.h>
23
#include <stdlib.h>
24
25
#include <sudo_gettext.h>
26
#include <sudo_compat.h>
27
#include "sudo_python_debug.h"
28
#include <sudo_queue.h>
29
#include <sudo_conf.h>
30
#include <sudo_fatal.h>
31
32
33
static int python_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
34
static unsigned int python_debug_refcnt;
35
36
static const char *const python_subsystem_names[] = {
37
"py_calls", // logs c -> py calls
38
"c_calls", // logs py -> c calls
39
"load", // logs python plugin loading / unloading
40
"sudo_cb", // logs sudo callback calls
41
"internal", // logs internal functions of the language wrapper plugin
42
"plugin", // logs whatever log the python module would like to log through sudo.debug API
43
NULL
44
};
45
46
#define NUM_SUBSYSTEMS sizeof(python_subsystem_names) / sizeof(*python_subsystem_names) - 1
47
48
/* Subsystem IDs assigned at registration time. */
49
unsigned int python_subsystem_ids[NUM_SUBSYSTEMS];
50
51
/*
52
* Parse the "filename flags,..." debug_flags entry and insert a new
53
* sudo_debug_file struct into debug_files.
54
*/
55
bool
56
python_debug_parse_flags(struct sudo_conf_debug_file_list *debug_files,
57
const char *entry)
58
{
59
/* Already initialized? */
60
if (python_debug_instance != SUDO_DEBUG_INSTANCE_INITIALIZER)
61
return true;
62
63
return sudo_debug_parse_flags(debug_files, entry) != -1;
64
}
65
66
/*
67
* Register the specified debug files and program with the
68
* debug subsystem, freeing the debug list when done.
69
* Sets the active debug instance as a side effect.
70
*/
71
bool
72
python_debug_register(const char *program,
73
struct sudo_conf_debug_file_list *debug_files)
74
{
75
int instance = python_debug_instance;
76
struct sudo_debug_file *debug_file, *debug_next;
77
78
/* Setup debugging if indicated. */
79
if (debug_files != NULL && !TAILQ_EMPTY(debug_files)) {
80
if (program != NULL) {
81
instance = sudo_debug_register(program, python_subsystem_names,
82
python_subsystem_ids, debug_files, -1);
83
}
84
TAILQ_FOREACH_SAFE(debug_file, debug_files, entries, debug_next) {
85
TAILQ_REMOVE(debug_files, debug_file, entries);
86
free(debug_file->debug_file);
87
free(debug_file->debug_flags);
88
free(debug_file);
89
}
90
}
91
92
switch (instance) {
93
case SUDO_DEBUG_INSTANCE_ERROR:
94
return false;
95
case SUDO_DEBUG_INSTANCE_INITIALIZER:
96
/* Nothing to do */
97
break;
98
default:
99
/* New debug instance or additional reference on existing one. */
100
python_debug_instance = instance;
101
sudo_debug_set_active_instance(python_debug_instance);
102
python_debug_refcnt++;
103
break;
104
}
105
106
return true;
107
}
108
109
/*
110
* Deregister python_debug_instance if it is registered.
111
*/
112
void
113
python_debug_deregister(void)
114
{
115
debug_decl(python_debug_deregister, PYTHON_DEBUG_INTERNAL);
116
117
if (python_debug_refcnt != 0) {
118
sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
119
if (--python_debug_refcnt == 0) {
120
if (sudo_debug_deregister(python_debug_instance) < 1)
121
python_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
122
}
123
}
124
}
125
126