Imported Upstream version 1.8.4p4
[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 28 bits are the subsystem.
25  * This allows for 16 priorities and a very large number of subsystems.
26  */
27
28 /*
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[]
32  */
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 */
41
42 /*
43  * Sudo debug subsystems.
44  * This includes subsystems in the sudoers plugin.
45  * Note: order must match sudo_debug_subsystems[]
46  */
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 */
72
73 /* Extract priority and convert to an index. */
74 #define SUDO_DEBUG_PRI(n) (((n) & 0xf) - 1)
75
76 /* Extract subsystem and convert to an index. */
77 #define SUDO_DEBUG_SUBSYS(n) (((n) >> 4) - 1)
78
79 /*
80  * Wrapper for sudo_debug_enter() that declares __func__ as needed
81  * and sets sudo_debug_subsys for sudo_debug_exit().
82  */
83 #ifdef HAVE___FUNC__
84 # define debug_decl(funcname, subsys)                                          \
85     const int sudo_debug_subsys = (subsys);                                    \
86     sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
87 #else
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);
92 #endif
93
94 /*
95  * Wrappers for sudo_debug_exit() and friends.
96  */
97 #define debug_return                                                           \
98     do {                                                                       \
99         sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);      \
100         return;                                                                \
101     } while (0)
102
103 #define debug_return_int(rval)                                                 \
104     do {                                                                       \
105         int sudo_debug_rval = (rval);                                          \
106         sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
107             sudo_debug_rval);                                                  \
108         return sudo_debug_rval;                                                \
109     } while (0)
110
111 #define debug_return_size_t(rval)                                              \
112     do {                                                                       \
113         size_t sudo_debug_rval = (rval);                                       \
114         sudo_debug_exit_size_t(__func__, __FILE__, __LINE__, sudo_debug_subsys,\
115             sudo_debug_rval);                                                  \
116         return sudo_debug_rval;                                                \
117     } while (0)
118
119 #define debug_return_long(rval)                                                \
120     do {                                                                       \
121         long sudo_debug_rval = (rval);                                         \
122         sudo_debug_exit_long(__func__, __FILE__, __LINE__, sudo_debug_subsys,  \
123             sudo_debug_rval);                                                  \
124         return sudo_debug_rval;                                                \
125     } while (0)
126
127 #define debug_return_bool(rval)                                                \
128     do {                                                                       \
129         int sudo_debug_rval = (rval);                                          \
130         sudo_debug_exit_bool(__func__, __FILE__, __LINE__, sudo_debug_subsys,  \
131             sudo_debug_rval);                                                  \
132         return sudo_debug_rval;                                                \
133     } while (0)
134
135 #define debug_return_str(rval)                                                 \
136     do {                                                                       \
137         const char *sudo_debug_rval = (rval);                                  \
138         sudo_debug_exit_str(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
139             sudo_debug_rval);                                                  \
140         return (char *)sudo_debug_rval;                                        \
141     } while (0)
142
143 #define debug_return_str_masked(rval)                                                  \
144     do {                                                                       \
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;                                        \
149     } while (0)
150
151 #define debug_return_ptr(rval)                                                 \
152     do {                                                                       \
153         const void *sudo_debug_rval = (rval);                                  \
154         sudo_debug_exit_ptr(__func__, __FILE__, __LINE__, sudo_debug_subsys,   \
155             sudo_debug_rval);                                                  \
156         return (void *)sudo_debug_rval;                                        \
157     } while (0)
158
159 /*
160  * Variadic macros are a C99 feature but GNU cpp has supported
161  * a (different) version of them for a long time.
162  */
163 #if defined(__GNUC__) && __GNUC__ == 2
164 # define sudo_debug_printf(pri, fmt...) \
165     sudo_debug_printf2((pri)|sudo_debug_subsys, (fmt))
166 #else
167 # define sudo_debug_printf(pri, ...) \
168     sudo_debug_printf2((pri)|sudo_debug_subsys, __VA_ARGS__)
169 #endif
170
171 #define sudo_debug_execve(pri, path, argv, envp) \
172     sudo_debug_execve2((pri)|sudo_debug_subsys, (path), (argv), (envp))
173
174 /*
175  * NULL-terminated string lists of priorities and subsystems.
176  */
177 extern const char *const sudo_debug_priorities[];
178 extern const char *const sudo_debug_subsystems[];
179
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);
194
195 #endif /* _SUDO_DEBUG_H */