/src/sudo/plugins/sudoers/sudoers_debug.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2014-2015 Todd C. Miller <Todd.Miller@sudo.ws> |
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 | | |
21 | | #include <stdio.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | |
25 | | #include <sudoers.h> |
26 | | |
27 | | static int sudoers_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER; |
28 | | static unsigned int sudoers_debug_refcnt; |
29 | | |
30 | | static const char *const sudoers_subsystem_names[] = { |
31 | | "alias", |
32 | | "audit", |
33 | | "auth", |
34 | | "defaults", |
35 | | "env", |
36 | | "event", |
37 | | "ldap", |
38 | | "logging", |
39 | | "main", |
40 | | "match", |
41 | | "netif", |
42 | | "nss", |
43 | | "parser", |
44 | | "perms", |
45 | | "plugin", |
46 | | "rbtree", |
47 | | "sssd", |
48 | | "util", |
49 | | NULL |
50 | | }; |
51 | | |
52 | | #define NUM_SUBSYSTEMS (nitems(sudoers_subsystem_names) - 1) |
53 | | |
54 | | /* Subsystem IDs assigned at registration time. */ |
55 | | unsigned int sudoers_subsystem_ids[NUM_SUBSYSTEMS]; |
56 | | |
57 | | /* |
58 | | * Parse the "filename flags,..." debug_flags entry and insert a new |
59 | | * sudo_debug_file struct into debug_files. |
60 | | */ |
61 | | bool |
62 | | sudoers_debug_parse_flags(struct sudo_conf_debug_file_list *debug_files, |
63 | | const char *entry) |
64 | 218 | { |
65 | | /* Already initialized? */ |
66 | 218 | if (sudoers_debug_instance != SUDO_DEBUG_INSTANCE_INITIALIZER) |
67 | 0 | return true; |
68 | | |
69 | 218 | return sudo_debug_parse_flags(debug_files, entry) != -1; |
70 | 218 | } |
71 | | |
72 | | /* |
73 | | * Register the specified debug files and program with the |
74 | | * debug subsystem, freeing the debug list when done. |
75 | | * Sets the active debug instance as a side effect. |
76 | | */ |
77 | | bool |
78 | | sudoers_debug_register(const char *program, |
79 | | struct sudo_conf_debug_file_list *debug_files) |
80 | 32.0k | { |
81 | 32.0k | int instance = sudoers_debug_instance; |
82 | 32.0k | struct sudo_debug_file *debug_file, *debug_next; |
83 | | |
84 | | /* Setup debugging if indicated. */ |
85 | 32.0k | if (debug_files != NULL && !TAILQ_EMPTY(debug_files)) { |
86 | 0 | if (program != NULL) { |
87 | 0 | instance = sudo_debug_register(program, sudoers_subsystem_names, |
88 | 0 | sudoers_subsystem_ids, debug_files, -1); |
89 | 0 | } |
90 | 0 | TAILQ_FOREACH_SAFE(debug_file, debug_files, entries, debug_next) { |
91 | 0 | TAILQ_REMOVE(debug_files, debug_file, entries); |
92 | 0 | free(debug_file->debug_file); |
93 | 0 | free(debug_file->debug_flags); |
94 | 0 | free(debug_file); |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | 32.0k | switch (instance) { |
99 | 0 | case SUDO_DEBUG_INSTANCE_ERROR: |
100 | 0 | return false; |
101 | 32.0k | case SUDO_DEBUG_INSTANCE_INITIALIZER: |
102 | | /* Nothing to do */ |
103 | 32.0k | break; |
104 | 0 | default: |
105 | | /* New debug instance or additional reference on existing one. */ |
106 | 0 | sudoers_debug_instance = instance; |
107 | 0 | sudo_debug_set_active_instance(sudoers_debug_instance); |
108 | 0 | sudoers_debug_refcnt++; |
109 | 0 | break; |
110 | 32.0k | } |
111 | | |
112 | 32.0k | return true; |
113 | 32.0k | } |
114 | | |
115 | | /* |
116 | | * Deregister sudoers_debug_instance if it is registered. |
117 | | */ |
118 | | void |
119 | | sudoers_debug_deregister(void) |
120 | 32.0k | { |
121 | 32.0k | debug_decl(sudoers_debug_deregister, SUDOERS_DEBUG_PLUGIN); |
122 | | |
123 | 32.0k | if (sudoers_debug_refcnt != 0) { |
124 | 0 | sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys); |
125 | 0 | if (--sudoers_debug_refcnt == 0) { |
126 | 0 | if (sudo_debug_deregister(sudoers_debug_instance) < 1) |
127 | 0 | sudoers_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER; |
128 | 0 | } |
129 | 0 | } |
130 | 32.0k | } |