sudo_plugin.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * SPDX-License-Identifier: ISC
  3. *
  4. * Copyright (c) 2009-2018 Todd C. Miller <Todd.Miller@sudo.ws>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef SUDO_PLUGIN_H
  19. #define SUDO_PLUGIN_H
  20. /* API version major/minor */
  21. #define SUDO_API_VERSION_MAJOR 1
  22. #define SUDO_API_VERSION_MINOR 14
  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. /* Getters and setters for plugin API versions */
  26. #define SUDO_API_VERSION_GET_MAJOR(v) ((v) >> 16)
  27. #define SUDO_API_VERSION_GET_MINOR(v) ((v) & 0xffffU)
  28. #define SUDO_API_VERSION_SET_MAJOR(vp, n) do { \
  29. *(vp) = (*(vp) & 0x0000ffffU) | ((n) << 16); \
  30. } while(0)
  31. #define SUDO_API_VERSION_SET_MINOR(vp, n) do { \
  32. *(vp) = (*(vp) & 0xffff0000U) | (n); \
  33. } while(0)
  34. /* Conversation function types and defines */
  35. struct sudo_conv_message {
  36. #define SUDO_CONV_PROMPT_ECHO_OFF 0x0001 /* do not echo user input */
  37. #define SUDO_CONV_PROMPT_ECHO_ON 0x0002 /* echo user input */
  38. #define SUDO_CONV_ERROR_MSG 0x0003 /* error message */
  39. #define SUDO_CONV_INFO_MSG 0x0004 /* informational message */
  40. #define SUDO_CONV_PROMPT_MASK 0x0005 /* mask user input */
  41. #define SUDO_CONV_PROMPT_ECHO_OK 0x1000 /* flag: allow echo if no tty */
  42. #define SUDO_CONV_PREFER_TTY 0x2000 /* flag: use tty if possible */
  43. int msg_type;
  44. int timeout;
  45. const char *msg;
  46. };
  47. /*
  48. * Maximum length of a reply (not including the trailing NUL) when
  49. * conversing with the user. In practical terms, this is the longest
  50. * password sudo will support. This means that a buffer of size
  51. * SUDO_CONV_REPL_MAX+1 is guaranteed to be able to hold any reply
  52. * from the conversation function. It is also useful as a max value
  53. * for memset_s() when clearing passwords returned by the conversation
  54. * function.
  55. */
  56. #define SUDO_CONV_REPL_MAX 255
  57. struct sudo_conv_reply {
  58. char *reply;
  59. };
  60. /* Conversation callback API version major/minor */
  61. #define SUDO_CONV_CALLBACK_VERSION_MAJOR 1
  62. #define SUDO_CONV_CALLBACK_VERSION_MINOR 0
  63. #define SUDO_CONV_CALLBACK_VERSION SUDO_API_MKVERSION(SUDO_CONV_CALLBACK_VERSION_MAJOR, SUDO_CONV_CALLBACK_VERSION_MINOR)
  64. /*
  65. * Callback struct to be passed to the conversation function.
  66. * Can be used to perform operations on suspend/resume such
  67. * as dropping/acquiring locks.
  68. */
  69. typedef int (*sudo_conv_callback_fn_t)(int signo, void *closure);
  70. struct sudo_conv_callback {
  71. unsigned int version;
  72. void *closure;
  73. sudo_conv_callback_fn_t on_suspend;
  74. sudo_conv_callback_fn_t on_resume;
  75. };
  76. typedef int (*sudo_conv_t)(int num_msgs, const struct sudo_conv_message msgs[],
  77. struct sudo_conv_reply replies[], struct sudo_conv_callback *callback);
  78. typedef int (*sudo_printf_t)(int msg_type, const char *fmt, ...);
  79. /*
  80. * Hooks allow a plugin to hook into specific sudo and/or libc functions.
  81. */
  82. /* Hook functions typedefs. */
  83. typedef int (*sudo_hook_fn_t)();
  84. typedef int (*sudo_hook_fn_setenv_t)(const char *name, const char *value, int overwrite, void *closure);
  85. typedef int (*sudo_hook_fn_putenv_t)(char *string, void *closure);
  86. typedef int (*sudo_hook_fn_getenv_t)(const char *name, char **value, void *closure);
  87. typedef int (*sudo_hook_fn_unsetenv_t)(const char *name, void *closure);
  88. /* Hook structure definition. */
  89. struct sudo_hook {
  90. unsigned int hook_version;
  91. unsigned int hook_type;
  92. sudo_hook_fn_t hook_fn;
  93. void *closure;
  94. };
  95. /* Hook API version major/minor */
  96. #define SUDO_HOOK_VERSION_MAJOR 1
  97. #define SUDO_HOOK_VERSION_MINOR 0
  98. #define SUDO_HOOK_VERSION SUDO_API_MKVERSION(SUDO_HOOK_VERSION_MAJOR, SUDO_HOOK_VERSION_MINOR)
  99. /*
  100. * Hook function return values.
  101. */
  102. #define SUDO_HOOK_RET_ERROR -1 /* error */
  103. #define SUDO_HOOK_RET_NEXT 0 /* go to the next hook in the list */
  104. #define SUDO_HOOK_RET_STOP 1 /* stop hook processing for this type */
  105. /*
  106. * Hooks for setenv/unsetenv/putenv/getenv.
  107. * This allows the plugin to be notified when a PAM module modifies
  108. * the environment so it can update the copy of the environment that
  109. * is passed to execve().
  110. */
  111. #define SUDO_HOOK_SETENV 1
  112. #define SUDO_HOOK_UNSETENV 2
  113. #define SUDO_HOOK_PUTENV 3
  114. #define SUDO_HOOK_GETENV 4
  115. /* Policy plugin type and defines */
  116. struct passwd;
  117. struct policy_plugin {
  118. #define SUDO_POLICY_PLUGIN 1
  119. unsigned int type; /* always SUDO_POLICY_PLUGIN */
  120. unsigned int version; /* always SUDO_API_VERSION */
  121. int (*open)(unsigned int version, sudo_conv_t conversation,
  122. sudo_printf_t sudo_printf, char * const settings[],
  123. char * const user_info[], char * const user_env[],
  124. char * const plugin_options[]);
  125. void (*close)(int exit_status, int error); /* wait status or error */
  126. int (*show_version)(int verbose);
  127. int (*check_policy)(int argc, char * const argv[],
  128. char *env_add[], char **command_info[],
  129. char **argv_out[], char **user_env_out[]);
  130. int (*list)(int argc, char * const argv[], int verbose,
  131. const char *list_user);
  132. int (*validate)(void);
  133. void (*invalidate)(int remove);
  134. int (*init_session)(struct passwd *pwd, char **user_env_out[]);
  135. void (*register_hooks)(int version, int (*register_hook)(struct sudo_hook *hook));
  136. void (*deregister_hooks)(int version, int (*deregister_hook)(struct sudo_hook *hook));
  137. };
  138. /* I/O plugin type and defines */
  139. struct io_plugin {
  140. #define SUDO_IO_PLUGIN 2
  141. unsigned int type; /* always SUDO_IO_PLUGIN */
  142. unsigned int version; /* always SUDO_API_VERSION */
  143. int (*open)(unsigned int version, sudo_conv_t conversation,
  144. sudo_printf_t sudo_printf, char * const settings[],
  145. char * const user_info[], char * const command_info[],
  146. int argc, char * const argv[], char * const user_env[],
  147. char * const plugin_options[]);
  148. void (*close)(int exit_status, int error); /* wait status or error */
  149. int (*show_version)(int verbose);
  150. int (*log_ttyin)(const char *buf, unsigned int len);
  151. int (*log_ttyout)(const char *buf, unsigned int len);
  152. int (*log_stdin)(const char *buf, unsigned int len);
  153. int (*log_stdout)(const char *buf, unsigned int len);
  154. int (*log_stderr)(const char *buf, unsigned int len);
  155. void (*register_hooks)(int version, int (*register_hook)(struct sudo_hook *hook));
  156. void (*deregister_hooks)(int version, int (*deregister_hook)(struct sudo_hook *hook));
  157. int (*change_winsize)(unsigned int line, unsigned int cols);
  158. int (*log_suspend)(int signo);
  159. };
  160. /* Sudoers group plugin version major/minor */
  161. #define GROUP_API_VERSION_MAJOR 1
  162. #define GROUP_API_VERSION_MINOR 0
  163. #define GROUP_API_VERSION SUDO_API_MKVERSION(GROUP_API_VERSION_MAJOR, GROUP_API_VERSION_MINOR)
  164. /* Getters and setters for group version (for source compat only) */
  165. #define GROUP_API_VERSION_GET_MAJOR(v) SUDO_API_VERSION_GET_MAJOR(v)
  166. #define GROUP_API_VERSION_GET_MINOR(v) SUDO_API_VERSION_GET_MINOR(v)
  167. #define GROUP_API_VERSION_SET_MAJOR(vp, n) SUDO_API_VERSION_SET_MAJOR(vp, n)
  168. #define GROUP_API_VERSION_SET_MINOR(vp, n) SUDO_API_VERSION_SET_MINOR(vp, n)
  169. /*
  170. * version: for compatibility checking
  171. * group_init: return 1 on success, 0 if unconfigured, -1 on error.
  172. * group_cleanup: called to clean up resources used by provider
  173. * user_in_group: returns 1 if user is in group, 0 if not.
  174. * note that pwd may be NULL if the user is not in passwd.
  175. */
  176. struct sudoers_group_plugin {
  177. unsigned int version;
  178. int (*init)(int version, sudo_printf_t sudo_printf, char *const argv[]);
  179. void (*cleanup)(void);
  180. int (*query)(const char *user, const char *group, const struct passwd *pwd);
  181. };
  182. #endif /* SUDO_PLUGIN_H */