2 * Copyright (c) 2011-2012 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 26 bits are the subsystem.
25 * This allows for 16 priorities and a very large number of subsystems.
26 * Bit 5 is used as a flag to specify whether to log the errno value.
27 * Bit 6 specifies whether to log the function, file and line number data.
31 * Sudo debug priorities, ordered least to most verbose,
32 * in other words, highest to lowest priority. Max pri is 15.
33 * Note: order must match sudo_debug_priorities[]
35 #define SUDO_DEBUG_CRIT 1 /* critical errors */
36 #define SUDO_DEBUG_ERROR 2 /* non-critical errors */
37 #define SUDO_DEBUG_WARN 3 /* non-fatal warnings */
38 #define SUDO_DEBUG_NOTICE 4 /* non-error condition notices */
39 #define SUDO_DEBUG_DIAG 5 /* diagnostic messages */
40 #define SUDO_DEBUG_INFO 6 /* informational message */
41 #define SUDO_DEBUG_TRACE 7 /* log function enter/exit */
42 #define SUDO_DEBUG_DEBUG 8 /* very verbose debugging */
45 * Sudo debug subsystems.
46 * This includes subsystems in the sudoers plugin.
47 * Note: order must match sudo_debug_subsystems[]
49 #define SUDO_DEBUG_MAIN ( 1<<6) /* sudo main() */
50 #define SUDO_DEBUG_ARGS ( 2<<6) /* command line argument processing */
51 #define SUDO_DEBUG_EXEC ( 3<<6) /* command execution */
52 #define SUDO_DEBUG_PTY ( 4<<6) /* pseudo-tty */
53 #define SUDO_DEBUG_UTMP ( 5<<6) /* utmp file ops */
54 #define SUDO_DEBUG_CONV ( 6<<6) /* user conversation */
55 #define SUDO_DEBUG_PCOMM ( 7<<6) /* plugin communications */
56 #define SUDO_DEBUG_UTIL ( 8<<6) /* utility functions */
57 #define SUDO_DEBUG_NETIF ( 9<<6) /* network interface functions */
58 #define SUDO_DEBUG_AUDIT (10<<6) /* audit */
59 #define SUDO_DEBUG_EDIT (11<<6) /* sudoedit */
60 #define SUDO_DEBUG_SELINUX (12<<6) /* selinux */
61 #define SUDO_DEBUG_LDAP (13<<6) /* sudoers LDAP */
62 #define SUDO_DEBUG_MATCH (14<<6) /* sudoers matching */
63 #define SUDO_DEBUG_PARSER (15<<6) /* sudoers parser */
64 #define SUDO_DEBUG_ALIAS (16<<6) /* sudoers alias functions */
65 #define SUDO_DEBUG_DEFAULTS (17<<6) /* sudoers defaults settings */
66 #define SUDO_DEBUG_AUTH (18<<6) /* authentication functions */
67 #define SUDO_DEBUG_ENV (19<<6) /* environment handling */
68 #define SUDO_DEBUG_LOGGING (20<<6) /* logging functions */
69 #define SUDO_DEBUG_NSS (21<<6) /* network service switch */
70 #define SUDO_DEBUG_RBTREE (22<<6) /* red-black tree functions */
71 #define SUDO_DEBUG_PERMS (23<<6) /* uid/gid swapping functions */
72 #define SUDO_DEBUG_PLUGIN (24<<6) /* main plugin functions */
73 #define SUDO_DEBUG_HOOKS (25<<6) /* hook functions */
74 #define SUDO_DEBUG_SSSD (26<<6) /* sudoers SSSD */
75 #define SUDO_DEBUG_ALL 0xfff0 /* all subsystems */
77 /* Flag to include string version of errno in debug info. */
78 #define SUDO_DEBUG_ERRNO (1<<4)
80 /* Flag to include function, file and line number in debug info. */
81 #define SUDO_DEBUG_LINENO (1<<5)
83 /* Extract priority and convert to an index. */
84 #define SUDO_DEBUG_PRI(n) (((n) & 0xf) - 1)
86 /* Extract subsystem and convert to an index. */
87 #define SUDO_DEBUG_SUBSYS(n) (((n) >> 6) - 1)
90 * Wrapper for sudo_debug_enter() that declares __func__ as needed
91 * and sets sudo_debug_subsys for sudo_debug_exit().
94 # define debug_decl(funcname, subsys) \
95 const int sudo_debug_subsys = (subsys); \
96 sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
98 # define debug_decl(funcname, subsys) \
99 const int sudo_debug_subsys = (subsys); \
100 const char *__func__ = #funcname; \
101 sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
105 * Wrappers for sudo_debug_exit() and friends.
107 #define debug_return \
109 sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys); \
113 #define debug_return_int(rval) \
115 int sudo_debug_rval = (rval); \
116 sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
118 return sudo_debug_rval; \
121 #define debug_return_size_t(rval) \
123 size_t sudo_debug_rval = (rval); \
124 sudo_debug_exit_size_t(__func__, __FILE__, __LINE__, sudo_debug_subsys,\
126 return sudo_debug_rval; \
129 #define debug_return_long(rval) \
131 long sudo_debug_rval = (rval); \
132 sudo_debug_exit_long(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
134 return sudo_debug_rval; \
137 #define debug_return_bool(rval) \
139 int sudo_debug_rval = (rval); \
140 sudo_debug_exit_bool(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
142 return sudo_debug_rval; \
145 #define debug_return_str(rval) \
147 const char *sudo_debug_rval = (rval); \
148 sudo_debug_exit_str(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
150 return (char *)sudo_debug_rval; \
153 #define debug_return_str_masked(rval) \
155 const char *sudo_debug_rval = (rval); \
156 sudo_debug_exit_str_masked(__func__, __FILE__, __LINE__, \
157 sudo_debug_subsys, sudo_debug_rval); \
158 return (char *)sudo_debug_rval; \
161 #define debug_return_ptr(rval) \
163 const void *sudo_debug_rval = (rval); \
164 sudo_debug_exit_ptr(__func__, __FILE__, __LINE__, sudo_debug_subsys, \
166 return (void *)sudo_debug_rval; \
170 * Variadic macros are a C99 feature but GNU cpp has supported
171 * a (different) version of them for a long time.
173 #if defined(__GNUC__) && __GNUC__ == 2
174 # define sudo_debug_printf(pri, fmt...) \
175 sudo_debug_printf2(__func__, __FILE__, __LINE__, (pri)|sudo_debug_subsys, \
178 # define sudo_debug_printf(pri, ...) \
179 sudo_debug_printf2(__func__, __FILE__, __LINE__, (pri)|sudo_debug_subsys, \
183 #define sudo_debug_execve(pri, path, argv, envp) \
184 sudo_debug_execve2((pri)|sudo_debug_subsys, (path), (argv), (envp))
187 * NULL-terminated string lists of priorities and subsystems.
189 extern const char *const sudo_debug_priorities[];
190 extern const char *const sudo_debug_subsystems[];
192 void sudo_debug_enter(const char *func, const char *file, int line, int subsys);
193 void sudo_debug_execve2(int level, const char *path, char *const argv[], char *const envp[]);
194 void sudo_debug_exit(const char *func, const char *file, int line, int subsys);
195 void sudo_debug_exit_int(const char *func, const char *file, int line, int subsys, int rval);
196 void sudo_debug_exit_long(const char *func, const char *file, int line, int subsys, long rval);
197 void sudo_debug_exit_size_t(const char *func, const char *file, int line, int subsys, size_t rval);
198 void sudo_debug_exit_bool(const char *func, const char *file, int line, int subsys, int rval);
199 void sudo_debug_exit_str(const char *func, const char *file, int line, int subsys, const char *rval);
200 void sudo_debug_exit_str_masked(const char *func, const char *file, int line, int subsys, const char *rval);
201 void sudo_debug_exit_ptr(const char *func, const char *file, int line, int subsys, const void *rval);
202 int sudo_debug_fd_set(int fd);
203 int sudo_debug_init(const char *debugfile, const char *settings);
204 void sudo_debug_printf2(const char *func, const char *file, int line, int level, const char *fmt, ...) __printflike(5, 6);
205 void sudo_debug_vprintf2(const char *func, const char *file, int line, int level, const char *fmt, va_list ap);
206 void sudo_debug_write(const char *str, int len, int errno_val);
207 void sudo_debug_write2(const char *func, const char *file, int line, const char *str, int len, int errno_val);
208 pid_t sudo_debug_fork(void);
210 #endif /* _SUDO_DEBUG_H */