Imported Upstream version 1.8.4p4
[debian/sudo] / include / sudo_plugin.h
1 /*
2  * Copyright (c) 2009-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_PLUGIN_H
18 #define _SUDO_PLUGIN_H
19
20 /* API version major/minor */
21 #define SUDO_API_VERSION_MAJOR 1
22 #define SUDO_API_VERSION_MINOR 1
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)
25
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); \
31 } while(0)
32 #define SUDO_VERSION_SET_MINOR(vp, n) do { \
33     *(vp) = (*(vp) & 0xffff0000) | (n); \
34 } while(0)
35
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 */
45     int msg_type;
46     int timeout;
47     const char *msg;
48 };
49
50 struct sudo_conv_reply {
51     char *reply;
52 };
53
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, ...);
57
58 /* Policy plugin type and defines */
59 struct passwd;
60 struct policy_plugin {
61 #define SUDO_POLICY_PLUGIN     1
62     unsigned int type; /* always SUDO_POLICY_PLUGIN */
63     unsigned int version; /* always SUDO_API_VERSION */
64     int (*open)(unsigned int version, sudo_conv_t conversation,
65         sudo_printf_t sudo_printf, char * const settings[],
66         char * const user_info[], char * const user_env[]);
67     void (*close)(int exit_status, int error); /* wait status or error */
68     int (*show_version)(int verbose);
69     int (*check_policy)(int argc, char * const argv[],
70         char *env_add[], char **command_info[],
71         char **argv_out[], char **user_env_out[]);
72     int (*list)(int argc, char * const argv[], int verbose,
73         const char *list_user);
74     int (*validate)(void);
75     void (*invalidate)(int remove);
76     int (*init_session)(struct passwd *pwd);
77 };
78
79 /* I/O plugin type and defines */
80 struct io_plugin {
81 #define SUDO_IO_PLUGIN      2
82     unsigned int type; /* always SUDO_IO_PLUGIN */
83     unsigned int version; /* always SUDO_API_VERSION */
84     int (*open)(unsigned int version, sudo_conv_t conversation,
85         sudo_printf_t sudo_printf, char * const settings[],
86         char * const user_info[], char * const command_info[],
87         int argc, char * const argv[], char * const user_env[]);
88     void (*close)(int exit_status, int error); /* wait status or error */
89     int (*show_version)(int verbose);
90     int (*log_ttyin)(const char *buf, unsigned int len);
91     int (*log_ttyout)(const char *buf, unsigned int len);
92     int (*log_stdin)(const char *buf, unsigned int len);
93     int (*log_stdout)(const char *buf, unsigned int len);
94     int (*log_stderr)(const char *buf, unsigned int len);
95 };
96
97 /* Sudoers group plugin version major/minor */
98 #define GROUP_API_VERSION_MAJOR 1
99 #define GROUP_API_VERSION_MINOR 0
100 #define GROUP_API_VERSION ((GROUP_API_VERSION_MAJOR << 16) | GROUP_API_VERSION_MINOR)
101
102 /* Getters and setters for group version */
103 #define GROUP_API_VERSION_GET_MAJOR(v) ((v) >> 16)
104 #define GROUP_API_VERSION_GET_MINOR(v) ((v) & 0xffff)
105 #define GROUP_API_VERSION_SET_MAJOR(vp, n) do { \
106     *(vp) = (*(vp) & 0x0000ffff) | ((n) << 16); \
107 } while(0)
108 #define GROUP_API_VERSION_SET_MINOR(vp, n) do { \
109     *(vp) = (*(vp) & 0xffff0000) | (n); \
110 } while(0)
111
112 /*
113  * version: for compatibility checking
114  * group_init: return 1 on success, 0 if unconfigured, -1 on error.
115  * group_cleanup: called to clean up resources used by provider
116  * user_in_group: returns 1 if user is in group, 0 if not.
117  *                note that pwd may be NULL if the user is not in passwd.
118  */
119 struct sudoers_group_plugin {
120     unsigned int version;
121     int (*init)(int version, sudo_printf_t sudo_printf, char *const argv[]);
122     void (*cleanup)(void);
123     int (*query)(const char *user, const char *group, const struct passwd *pwd);
124 };
125
126 #endif /* _SUDO_PLUGIN_H */