2 * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * The priority and subsystem are encoded in a single 32-bit value.
24 * The lower 4 bits are the priority and the top 28 bits are the subsystem.
25 * This allows for 16 priorities and a very large number of subsystems.
29 * Sudo debug priorities, ordered least to most verbose,
30 * in other words, highest to lowest priority. Max pri is 15.
31 * Note: order must match sudo_debug_priorities[]
33 #define SUDO_DEBUG_CRIT 1 /* critical errors */
34 #define SUDO_DEBUG_ERROR 2 /* non-critical errors */
35 #define SUDO_DEBUG_WARN 3 /* non-fatal warnings */
36 #define SUDO_DEBUG_NOTICE 4 /* non-error condition notices */
37 #define SUDO_DEBUG_DIAG 5 /* diagnostic messages */
38 #define SUDO_DEBUG_INFO 6 /* informational message */
39 #define SUDO_DEBUG_TRACE 7 /* log function enter/exit */
40 #define SUDO_DEBUG_DEBUG 8 /* very verbose debugging */
43 * Sudo debug subsystems.
44 * This includes subsystems in the sudoers plugin.
45 * Note: order must match sudo_debug_subsystems[]
47 #define SUDO_DEBUG_MAIN (1<<4) /* sudo main() */
48 #define SUDO_DEBUG_ARGS (2<<4) /* command line argument processing */
49 #define SUDO_DEBUG_EXEC (3<<4) /* command execution */
50 #define SUDO_DEBUG_PTY (4<<4) /* pseudo-tty */
51 #define SUDO_DEBUG_UTMP (5<<4) /* utmp file ops */
52 #define SUDO_DEBUG_CONV (6<<4) /* user conversation */
53 #define SUDO_DEBUG_PCOMM (7<<4) /* plugin communications */
54 #define SUDO_DEBUG_UTIL (8<<4) /* utility functions */
55 #define SUDO_DEBUG_NETIF (9<<4) /* network interface functions */
56 #define SUDO_DEBUG_AUDIT (10<<4) /* audit */
57 #define SUDO_DEBUG_EDIT (11<<4) /* sudoedit */
58 #define SUDO_DEBUG_SELINUX (12<<4) /* selinux */
59 #define SUDO_DEBUG_LDAP (13<<4) /* sudoers LDAP */
60 #define SUDO_DEBUG_MATCH (14<<4) /* sudoers matching */
61 #define SUDO_DEBUG_PARSER (15<<4) /* sudoers parser */
62 #define SUDO_DEBUG_ALIAS (16<<4) /* sudoers alias functions */
63 #define SUDO_DEBUG_DEFAULTS (17<<4) /* sudoers defaults settings */
64 #define SUDO_DEBUG_AUTH (18<<4) /* authentication functions */
65 #define SUDO_DEBUG_ENV (19<<4) /* environment handling */
66 #define SUDO_DEBUG_LOGGING (20<<4) /* logging functions */
67 #define SUDO_DEBUG_NSS (21<<4) /* network service switch */
68 #define SUDO_DEBUG_RBTREE (22<<4) /* red-black tree functions */
69 #define SUDO_DEBUG_PERMS (23<<4) /* uid/gid swapping functions */
70 #define SUDO_DEBUG_PLUGIN (24<<4) /* main plugin functions */
71 #define SUDO_DEBUG_ALL 0xfff0 /* all subsystems */
73 /* Extract priority and convert to an index. */
74 #define SUDO_DEBUG_PRI(n) (((n) & 0xf) - 1)
76 /* Extract subsystem and convert to an index. */
77 #define SUDO_DEBUG_SUBSYS(n) (((n) >> 4) - 1)
80 * Wrapper for sudo_debug_enter() that declares __func__ as needed
81 * and sets sudo_debug_subsys for sudo_debug_exit().
84 # define debug_decl(funcname, subsys) \
85 const int sudo_debug_subsys = (subsys); \
86 sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
88 # define debug_decl(funcname, subsys) \
89 const int sudo_debug_subsys = (subsys); \
90 const char *__func__ = #funcname; \
91 sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
95 * Wrappers for sudo_debug_exit() and friends.
97 #define debug_return \
99 sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys); \
103 #define debug_return_int(rval) \
105 int sudo_debug_rval = (rval); \
106 sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
108 return sudo_debug_rval; \
111 #define debug_return_size_t(rval) \
113 size_t sudo_debug_rval = (rval); \
114 sudo_debug_exit_size_t(__func__, __FILE__, __LINE__, sudo_debug_subsys,\
116 return sudo_debug_rval; \
119 #define debug_return_long(rval) \
121 long sudo_debug_rval = (rval); \
122 sudo_debug_exit_long(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
124 return sudo_debug_rval; \
127 #define debug_return_bool(rval) \
129 int sudo_debug_rval = (rval); \
130 sudo_debug_exit_bool(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
132 return sudo_debug_rval; \
135 #define debug_return_str(rval) \
137 const char *sudo_debug_rval = (rval); \
138 sudo_debug_exit_str(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
140 return (char *)sudo_debug_rval; \
143 #define debug_return_str_masked(rval) \
145 const char *sudo_debug_rval = (rval); \
146 sudo_debug_exit_str_masked(__func__, __FILE__, __LINE__, \
147 sudo_debug_subsys, sudo_debug_rval); \
148 return (char *)sudo_debug_rval; \
151 #define debug_return_ptr(rval) \
153 const void *sudo_debug_rval = (rval); \
154 sudo_debug_exit_ptr(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
156 return (void *)sudo_debug_rval; \
160 * Variadic macros are a C99 feature but GNU cpp has supported
161 * a (different) version of them for a long time.
163 #if defined(__GNUC__) && __GNUC__ == 2
164 # define sudo_debug_printf(pri, fmt...) \
165 sudo_debug_printf2((pri)|sudo_debug_subsys, (fmt))
167 # define sudo_debug_printf(pri, ...) \
168 sudo_debug_printf2((pri)|sudo_debug_subsys, __VA_ARGS__)
171 #define sudo_debug_execve(pri, path, argv, envp) \
172 sudo_debug_execve2((pri)|sudo_debug_subsys, (path), (argv), (envp))
175 * NULL-terminated string lists of priorities and subsystems.
177 extern const char *const sudo_debug_priorities[];
178 extern const char *const sudo_debug_subsystems[];
180 void sudo_debug_enter(const char *func, const char *file, int line, int subsys);
181 void sudo_debug_execve2(int level, const char *path, char *const argv[], char *const envp[]);
182 void sudo_debug_exit(const char *func, const char *file, int line, int subsys);
183 void sudo_debug_exit_int(const char *func, const char *file, int line, int subsys, int rval);
184 void sudo_debug_exit_long(const char *func, const char *file, int line, int subsys, long rval);
185 void sudo_debug_exit_size_t(const char *func, const char *file, int line, int subsys, size_t rval);
186 void sudo_debug_exit_bool(const char *func, const char *file, int line, int subsys, int rval);
187 void sudo_debug_exit_str(const char *func, const char *file, int line, int subsys, const char *rval);
188 void sudo_debug_exit_str_masked(const char *func, const char *file, int line, int subsys, const char *rval);
189 void sudo_debug_exit_ptr(const char *func, const char *file, int line, int subsys, const void *rval);
190 int sudo_debug_fd_set(int fd);
191 int sudo_debug_init(const char *debugfile, const char *settings);
192 void sudo_debug_printf2(int level, const char *format, ...) __printflike(2, 3);
193 void sudo_debug_write(const char *str, int len);
195 #endif /* _SUDO_DEBUG_H */