2 * Copyright (c) 2009-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.
17 #ifndef _SUDO_PLUGIN_H
18 #define _SUDO_PLUGIN_H
20 /* API version major/minor */
21 #define SUDO_API_VERSION_MAJOR 1
22 #define SUDO_API_VERSION_MINOR 2
23 #define SUDO_API_MKVERSION(x, y) ((x << 16) | y)
24 #define SUDO_API_VERSION SUDO_API_MKVERSION(SUDO_API_VERSION_MAJOR, SUDO_API_VERSION_MINOR)
26 /* Getters and setters for API version */
27 #define SUDO_API_VERSION_GET_MAJOR(v) ((v) >> 16)
28 #define SUDO_API_VERSION_GET_MINOR(v) ((v) & 0xffff)
29 #define SUDO_API_VERSION_SET_MAJOR(vp, n) do { \
30 *(vp) = (*(vp) & 0x0000ffff) | ((n) << 16); \
32 #define SUDO_API_VERSION_SET_MINOR(vp, n) do { \
33 *(vp) = (*(vp) & 0xffff0000) | (n); \
36 /* Conversation function types and defines */
37 struct sudo_conv_message {
38 #define SUDO_CONV_PROMPT_ECHO_OFF 0x0001 /* do not echo user input */
39 #define SUDO_CONV_PROMPT_ECHO_ON 0x0002 /* echo user input */
40 #define SUDO_CONV_ERROR_MSG 0x0003 /* error message */
41 #define SUDO_CONV_INFO_MSG 0x0004 /* informational message */
42 #define SUDO_CONV_PROMPT_MASK 0x0005 /* mask user input */
43 #define SUDO_CONV_DEBUG_MSG 0x0006 /* debugging message */
44 #define SUDO_CONV_PROMPT_ECHO_OK 0x1000 /* flag: allow echo if no tty */
50 struct sudo_conv_reply {
54 typedef int (*sudo_conv_t)(int num_msgs, const struct sudo_conv_message msgs[],
55 struct sudo_conv_reply replies[]);
56 typedef int (*sudo_printf_t)(int msg_type, const char *fmt, ...);
59 * Hooks allow a plugin to hook into specific sudo and/or libc functions.
62 /* Hook functions typedefs. */
63 typedef int (*sudo_hook_fn_t)();
64 typedef int (*sudo_hook_fn_setenv_t)(const char *name, const char *value, int overwrite, void *closure);
65 typedef int (*sudo_hook_fn_putenv_t)(char *string, void *closure);
66 typedef int (*sudo_hook_fn_getenv_t)(const char *name, char **value, void *closure);
67 typedef int (*sudo_hook_fn_unsetenv_t)(const char *name, void *closure);
69 /* Hook structure definition. */
73 sudo_hook_fn_t hook_fn;
77 /* Hook API version major/minor */
78 #define SUDO_HOOK_VERSION_MAJOR 1
79 #define SUDO_HOOK_VERSION_MINOR 0
80 #define SUDO_HOOK_MKVERSION(x, y) ((x << 16) | y)
81 #define SUDO_HOOK_VERSION SUDO_HOOK_MKVERSION(SUDO_HOOK_VERSION_MAJOR, SUDO_HOOK_VERSION_MINOR)
83 /* Getters and setters for hook API version */
84 #define SUDO_HOOK_VERSION_GET_MAJOR(v) ((v) >> 16)
85 #define SUDO_HOOK_VERSION_GET_MINOR(v) ((v) & 0xffff)
86 #define SUDO_HOOK_VERSION_SET_MAJOR(vp, n) do { \
87 *(vp) = (*(vp) & 0x0000ffff) | ((n) << 16); \
89 #define SUDO_HOOK_VERSION_SET_MINOR(vp, n) do { \
90 *(vp) = (*(vp) & 0xffff0000) | (n); \
94 * Hook function return values.
96 #define SUDO_HOOK_RET_ERROR -1 /* error */
97 #define SUDO_HOOK_RET_NEXT 0 /* go to the next hook in the list */
98 #define SUDO_HOOK_RET_STOP 1 /* stop hook processing for this type */
101 * Hooks for setenv/unsetenv/putenv/getenv.
102 * This allows the plugin to be notified when a PAM module modifies
103 * the environment so it can update the copy of the environment that
104 * is passed to execve().
106 #define SUDO_HOOK_SETENV 1
107 #define SUDO_HOOK_UNSETENV 2
108 #define SUDO_HOOK_PUTENV 3
109 #define SUDO_HOOK_GETENV 4
111 /* Policy plugin type and defines */
113 struct policy_plugin {
114 #define SUDO_POLICY_PLUGIN 1
115 unsigned int type; /* always SUDO_POLICY_PLUGIN */
116 unsigned int version; /* always SUDO_API_VERSION */
117 int (*open)(unsigned int version, sudo_conv_t conversation,
118 sudo_printf_t sudo_printf, char * const settings[],
119 char * const user_info[], char * const user_env[],
120 char * const plugin_plugins[]);
121 void (*close)(int exit_status, int error); /* wait status or error */
122 int (*show_version)(int verbose);
123 int (*check_policy)(int argc, char * const argv[],
124 char *env_add[], char **command_info[],
125 char **argv_out[], char **user_env_out[]);
126 int (*list)(int argc, char * const argv[], int verbose,
127 const char *list_user);
128 int (*validate)(void);
129 void (*invalidate)(int remove);
130 int (*init_session)(struct passwd *pwd, char **user_env_out[]);
131 void (*register_hooks)(int version, int (*register_hook)(struct sudo_hook *hook));
132 void (*deregister_hooks)(int version, int (*deregister_hook)(struct sudo_hook *hook));
135 /* I/O plugin type and defines */
137 #define SUDO_IO_PLUGIN 2
138 unsigned int type; /* always SUDO_IO_PLUGIN */
139 unsigned int version; /* always SUDO_API_VERSION */
140 int (*open)(unsigned int version, sudo_conv_t conversation,
141 sudo_printf_t sudo_printf, char * const settings[],
142 char * const user_info[], char * const command_info[],
143 int argc, char * const argv[], char * const user_env[],
144 char * const plugin_plugins[]);
145 void (*close)(int exit_status, int error); /* wait status or error */
146 int (*show_version)(int verbose);
147 int (*log_ttyin)(const char *buf, unsigned int len);
148 int (*log_ttyout)(const char *buf, unsigned int len);
149 int (*log_stdin)(const char *buf, unsigned int len);
150 int (*log_stdout)(const char *buf, unsigned int len);
151 int (*log_stderr)(const char *buf, unsigned int len);
152 void (*register_hooks)(int version, int (*register_hook)(struct sudo_hook *hook));
153 void (*deregister_hooks)(int version, int (*deregister_hook)(struct sudo_hook *hook));
156 /* Sudoers group plugin version major/minor */
157 #define GROUP_API_VERSION_MAJOR 1
158 #define GROUP_API_VERSION_MINOR 0
159 #define GROUP_API_VERSION ((GROUP_API_VERSION_MAJOR << 16) | GROUP_API_VERSION_MINOR)
161 /* Getters and setters for group version */
162 #define GROUP_API_VERSION_GET_MAJOR(v) ((v) >> 16)
163 #define GROUP_API_VERSION_GET_MINOR(v) ((v) & 0xffff)
164 #define GROUP_API_VERSION_SET_MAJOR(vp, n) do { \
165 *(vp) = (*(vp) & 0x0000ffff) | ((n) << 16); \
167 #define GROUP_API_VERSION_SET_MINOR(vp, n) do { \
168 *(vp) = (*(vp) & 0xffff0000) | (n); \
172 * version: for compatibility checking
173 * group_init: return 1 on success, 0 if unconfigured, -1 on error.
174 * group_cleanup: called to clean up resources used by provider
175 * user_in_group: returns 1 if user is in group, 0 if not.
176 * note that pwd may be NULL if the user is not in passwd.
178 struct sudoers_group_plugin {
179 unsigned int version;
180 int (*init)(int version, sudo_printf_t sudo_printf, char *const argv[]);
181 void (*cleanup)(void);
182 int (*query)(const char *user, const char *group, const struct passwd *pwd);
185 #endif /* _SUDO_PLUGIN_H */