have the sudo-ldap package init.d script be named sudo-ldap
[debian/sudo] / include / sudo_debug.h
1 /*
2  * Copyright (c) 2011 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_ALL          0xfff0  /* all subsystems */
75
76 /* Flag to include string version of errno in debug info. */
77 #define SUDO_DEBUG_ERRNO        (1<<4)
78
79 /* Flag to include function, file and line number in debug info. */
80 #define SUDO_DEBUG_LINENO       (1<<5)
81
82 /* Extract priority and convert to an index. */
83 #define SUDO_DEBUG_PRI(n) (((n) & 0xf) - 1)
84
85 /* Extract subsystem and convert to an index. */
86 #define SUDO_DEBUG_SUBSYS(n) (((n) >> 6) - 1)
87
88 /*
89  * Wrapper for sudo_debug_enter() that declares __func__ as needed
90  * and sets sudo_debug_subsys for sudo_debug_exit().
91  */
92 #ifdef HAVE___FUNC__
93 # define debug_decl(funcname, subsys)                                          \
94     const int sudo_debug_subsys = (subsys);                                    \
95     sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
96 #else
97 # define debug_decl(funcname, subsys)                                          \
98     const int sudo_debug_subsys = (subsys);                                    \
99     const char *__func__ = #funcname;                                          \
100     sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
101 #endif
102
103 /*
104  * Wrappers for sudo_debug_exit() and friends.
105  */
106 #define debug_return                                                           \
107     do {                                                                       \
108         sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);      \
109         return;                                                                \
110     } while (0)
111
112 #define debug_return_int(rval)                                                 \
113     do {                                                                       \
114         int sudo_debug_rval = (rval);                                          \
115         sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
116             sudo_debug_rval);                                                  \
117         return sudo_debug_rval;                                                \
118     } while (0)
119
120 #define debug_return_size_t(rval)                                              \
121     do {                                                                       \
122         size_t sudo_debug_rval = (rval);                                       \
123         sudo_debug_exit_size_t(__func__, __FILE__, __LINE__, sudo_debug_subsys,\
124             sudo_debug_rval);                                                  \
125         return sudo_debug_rval;                                                \
126     } while (0)
127
128 #define debug_return_long(rval)                                                \
129     do {                                                                       \
130         long sudo_debug_rval = (rval);                                         \
131         sudo_debug_exit_long(__func__, __FILE__, __LINE__, sudo_debug_subsys,  \
132             sudo_debug_rval);                                                  \
133         return sudo_debug_rval;                                                \
134     } while (0)
135
136 #define debug_return_bool(rval)                                                \
137     do {                                                                       \
138         int sudo_debug_rval = (rval);                                          \
139         sudo_debug_exit_bool(__func__, __FILE__, __LINE__, sudo_debug_subsys,  \
140             sudo_debug_rval);                                                  \
141         return sudo_debug_rval;                                                \
142     } while (0)
143
144 #define debug_return_str(rval)                                                 \
145     do {                                                                       \
146         const char *sudo_debug_rval = (rval);                                  \
147         sudo_debug_exit_str(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
148             sudo_debug_rval);                                                  \
149         return (char *)sudo_debug_rval;                                        \
150     } while (0)
151
152 #define debug_return_str_masked(rval)                                                  \
153     do {                                                                       \
154         const char *sudo_debug_rval = (rval);                                  \
155         sudo_debug_exit_str_masked(__func__, __FILE__, __LINE__,               \
156             sudo_debug_subsys, sudo_debug_rval);                               \
157         return (char *)sudo_debug_rval;                                        \
158     } while (0)
159
160 #define debug_return_ptr(rval)                                                 \
161     do {                                                                       \
162         const void *sudo_debug_rval = (rval);                                  \
163         sudo_debug_exit_ptr(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
164             sudo_debug_rval);                                                  \
165         return (void *)sudo_debug_rval;                                        \
166     } while (0)
167
168 /*
169  * Variadic macros are a C99 feature but GNU cpp has supported
170  * a (different) version of them for a long time.
171  */
172 #if defined(__GNUC__) && __GNUC__ == 2
173 # define sudo_debug_printf(pri, fmt...) \
174     sudo_debug_printf2(__func__, __FILE__, __LINE__, (pri)|sudo_debug_subsys, \
175     (fmt))
176 #else
177 # define sudo_debug_printf(pri, ...) \
178     sudo_debug_printf2(__func__, __FILE__, __LINE__, (pri)|sudo_debug_subsys, \
179     __VA_ARGS__)
180 #endif
181
182 #define sudo_debug_execve(pri, path, argv, envp) \
183     sudo_debug_execve2((pri)|sudo_debug_subsys, (path), (argv), (envp))
184
185 /*
186  * NULL-terminated string lists of priorities and subsystems.
187  */
188 extern const char *const sudo_debug_priorities[];
189 extern const char *const sudo_debug_subsystems[];
190
191 void sudo_debug_enter(const char *func, const char *file, int line, int subsys);
192 void sudo_debug_execve2(int level, const char *path, char *const argv[], char *const envp[]);
193 void sudo_debug_exit(const char *func, const char *file, int line, int subsys);
194 void sudo_debug_exit_int(const char *func, const char *file, int line, int subsys, int rval);
195 void sudo_debug_exit_long(const char *func, const char *file, int line, int subsys, long rval);
196 void sudo_debug_exit_size_t(const char *func, const char *file, int line, int subsys, size_t rval);
197 void sudo_debug_exit_bool(const char *func, const char *file, int line, int subsys, int rval);
198 void sudo_debug_exit_str(const char *func, const char *file, int line, int subsys, const char *rval);
199 void sudo_debug_exit_str_masked(const char *func, const char *file, int line, int subsys, const char *rval);
200 void sudo_debug_exit_ptr(const char *func, const char *file, int line, int subsys, const void *rval);
201 int sudo_debug_fd_set(int fd);
202 int sudo_debug_init(const char *debugfile, const char *settings);
203 void sudo_debug_printf2(const char *func, const char *file, int line, int level, const char *format, ...) __printflike(5, 6);
204 void sudo_debug_write(const char *str, int len, int errno_val);
205 void sudo_debug_write2(const char *func, const char *file, int line, const char *str, int len, int errno_val);
206 pid_t sudo_debug_fork(void);
207
208 #endif /* _SUDO_DEBUG_H */