Imported Upstream version 1.8.7
[debian/sudo] / include / sudo_debug.h
1 /*
2  * Copyright (c) 2011-2012 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
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.
7  *
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.
15  */
16
17 #ifndef _SUDO_DEBUG_H
18 #define _SUDO_DEBUG_H
19
20 #include <stdarg.h>
21
22 /*
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.
28  */
29
30 /*
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[]
34  */
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 */
43
44 /*
45  * Sudo debug subsystems.
46  * This includes subsystems in the sudoers plugin.
47  * Note: order must match sudo_debug_subsystems[]
48  */
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 */
76
77 /* Flag to include string version of errno in debug info. */
78 #define SUDO_DEBUG_ERRNO        (1<<4)
79
80 /* Flag to include function, file and line number in debug info. */
81 #define SUDO_DEBUG_LINENO       (1<<5)
82
83 /* Extract priority and convert to an index. */
84 #define SUDO_DEBUG_PRI(n) (((n) & 0xf) - 1)
85
86 /* Extract subsystem and convert to an index. */
87 #define SUDO_DEBUG_SUBSYS(n) (((n) >> 6) - 1)
88
89 /*
90  * Wrapper for sudo_debug_enter() that declares __func__ as needed
91  * and sets sudo_debug_subsys for sudo_debug_exit().
92  */
93 #ifdef HAVE___FUNC__
94 # define debug_decl(funcname, subsys)                                          \
95     const int sudo_debug_subsys = (subsys);                                    \
96     sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
97 #else
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);
102 #endif
103
104 /*
105  * Wrappers for sudo_debug_exit() and friends.
106  */
107 #define debug_return                                                           \
108     do {                                                                       \
109         sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);      \
110         return;                                                                \
111     } while (0)
112
113 #define debug_return_int(rval)                                                 \
114     do {                                                                       \
115         int sudo_debug_rval = (rval);                                          \
116         sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
117             sudo_debug_rval);                                                  \
118         return sudo_debug_rval;                                                \
119     } while (0)
120
121 #define debug_return_size_t(rval)                                              \
122     do {                                                                       \
123         size_t sudo_debug_rval = (rval);                                       \
124         sudo_debug_exit_size_t(__func__, __FILE__, __LINE__, sudo_debug_subsys,\
125             sudo_debug_rval);                                                  \
126         return sudo_debug_rval;                                                \
127     } while (0)
128
129 #define debug_return_long(rval)                                                \
130     do {                                                                       \
131         long sudo_debug_rval = (rval);                                         \
132         sudo_debug_exit_long(__func__, __FILE__, __LINE__, sudo_debug_subsys,  \
133             sudo_debug_rval);                                                  \
134         return sudo_debug_rval;                                                \
135     } while (0)
136
137 #define debug_return_bool(rval)                                                \
138     do {                                                                       \
139         int sudo_debug_rval = (rval);                                          \
140         sudo_debug_exit_bool(__func__, __FILE__, __LINE__, sudo_debug_subsys,  \
141             sudo_debug_rval);                                                  \
142         return sudo_debug_rval;                                                \
143     } while (0)
144
145 #define debug_return_str(rval)                                                 \
146     do {                                                                       \
147         const char *sudo_debug_rval = (rval);                                  \
148         sudo_debug_exit_str(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
149             sudo_debug_rval);                                                  \
150         return (char *)sudo_debug_rval;                                        \
151     } while (0)
152
153 #define debug_return_str_masked(rval)                                                  \
154     do {                                                                       \
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;                                        \
159     } while (0)
160
161 #define debug_return_ptr(rval)                                                 \
162     do {                                                                       \
163         const void *sudo_debug_rval = (rval);                                  \
164         sudo_debug_exit_ptr(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
165             sudo_debug_rval);                                                  \
166         return (void *)sudo_debug_rval;                                        \
167     } while (0)
168
169 /*
170  * Variadic macros are a C99 feature but GNU cpp has supported
171  * a (different) version of them for a long time.
172  */
173 #if defined(__GNUC__) && __GNUC__ == 2
174 # define sudo_debug_printf(pri, fmt...) \
175     sudo_debug_printf2(__func__, __FILE__, __LINE__, (pri)|sudo_debug_subsys, \
176     fmt)
177 #else
178 # define sudo_debug_printf(pri, ...) \
179     sudo_debug_printf2(__func__, __FILE__, __LINE__, (pri)|sudo_debug_subsys, \
180     __VA_ARGS__)
181 #endif
182
183 #define sudo_debug_execve(pri, path, argv, envp) \
184     sudo_debug_execve2((pri)|sudo_debug_subsys, (path), (argv), (envp))
185
186 /*
187  * NULL-terminated string lists of priorities and subsystems.
188  */
189 extern const char *const sudo_debug_priorities[];
190 extern const char *const sudo_debug_subsystems[];
191
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);
209
210 #endif /* _SUDO_DEBUG_H */