Adapt to current plugin API and fix warnings.

This commit is contained in:
Todd C. Miller
2022-10-21 07:51:00 -06:00
parent 9b9404b6fa
commit 45ea248335

View File

@@ -1,7 +1,7 @@
/* /*
* SPDX-License-Identifier: ISC * SPDX-License-Identifier: ISC
* *
* Copyright (c) 2010-2016 Todd C. Miller <Todd.Miller@sudo.ws> * Copyright (c) 2010-2016, 2022 Todd C. Miller <Todd.Miller@sudo.ws>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -39,6 +39,7 @@
# include <strings.h> # include <strings.h>
#endif /* HAVE_STRINGS_H */ #endif /* HAVE_STRINGS_H */
#include <unistd.h> #include <unistd.h>
#include <ctype.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <grp.h> #include <grp.h>
@@ -72,8 +73,9 @@ static int use_sudoedit = false;
*/ */
static int static int
policy_open(unsigned int version, sudo_conv_t conversation, policy_open(unsigned int version, sudo_conv_t conversation,
sudo_printf_t sudo_printf, char * const settings[], sudo_printf_t sudo_plugin_printf, char * const settings[],
char * const user_info[], char * const user_env[], char * const args[]) char * const user_info[], char * const user_env[], char * const args[],
const char **errstr)
{ {
char * const *ui; char * const *ui;
struct passwd *pw; struct passwd *pw;
@@ -142,20 +144,20 @@ static char *
find_in_path(char *command, char **envp) find_in_path(char *command, char **envp)
{ {
struct stat sb; struct stat sb;
char *path, *path0, **ep, *cp; char *path = NULL;
char *path0, **ep, *cp;
char pathbuf[PATH_MAX], *qualified = NULL; char pathbuf[PATH_MAX], *qualified = NULL;
if (strchr(command, '/') != NULL) if (strchr(command, '/') != NULL)
return command; return command;
path = _PATH_DEFPATH;
for (ep = plugin_state.envp; *ep != NULL; ep++) { for (ep = plugin_state.envp; *ep != NULL; ep++) {
if (strncmp(*ep, "PATH=", 5) == 0) { if (strncmp(*ep, "PATH=", 5) == 0) {
path = *ep + 5; path = *ep + 5;
break; break;
} }
} }
path = path0 = strdup(path); path = path0 = strdup(path ? path : _PATH_DEFPATH);
do { do {
if ((cp = strchr(path, ':'))) if ((cp = strchr(path, ':')))
*cp = '\0'; *cp = '\0';
@@ -231,18 +233,18 @@ build_command_info(const char *command)
static char * static char *
find_editor(int nfiles, char * const files[], char **argv_out[]) find_editor(int nfiles, char * const files[], char **argv_out[])
{ {
char *cp, *last, **ep, **nargv, *editor, *editor_path; char *cp, *last, **ep, **nargv, *editor_path;
char *editor = NULL;
int ac, i, nargc, wasblank; int ac, i, nargc, wasblank;
/* Lookup EDITOR in user's environment. */ /* Lookup EDITOR in user's environment. */
editor = _PATH_VI;
for (ep = plugin_state.envp; *ep != NULL; ep++) { for (ep = plugin_state.envp; *ep != NULL; ep++) {
if (strncmp(*ep, "EDITOR=", 7) == 0) { if (strncmp(*ep, "EDITOR=", 7) == 0) {
editor = *ep + 7; editor = *ep + 7;
break; break;
} }
} }
editor = strdup(editor); editor = strdup(editor ? editor : _PATH_VI);
if (editor == NULL) { if (editor == NULL) {
sudo_log(SUDO_CONV_ERROR_MSG, "unable to allocate memory\n"); sudo_log(SUDO_CONV_ERROR_MSG, "unable to allocate memory\n");
return NULL; return NULL;
@@ -281,7 +283,7 @@ find_editor(int nfiles, char * const files[], char **argv_out[])
nargv[ac] = cp; nargv[ac] = cp;
cp = strtok_r(NULL, " \t", &last); cp = strtok_r(NULL, " \t", &last);
} }
nargv[ac++] = "--"; nargv[ac++] = (char *)"--";
for (i = 0; i < nfiles; ) for (i = 0; i < nfiles; )
nargv[ac++] = files[i++]; nargv[ac++] = files[i++];
nargv[ac] = NULL; nargv[ac] = NULL;
@@ -297,7 +299,7 @@ find_editor(int nfiles, char * const files[], char **argv_out[])
static int static int
policy_check(int argc, char * const argv[], policy_check(int argc, char * const argv[],
char *env_add[], char **command_info_out[], char *env_add[], char **command_info_out[],
char **argv_out[], char **user_env_out[]) char **argv_out[], char **user_env_out[], const char **errstr)
{ {
char *command; char *command;
@@ -348,7 +350,8 @@ policy_check(int argc, char * const argv[],
} }
static int static int
policy_list(int argc, char * const argv[], int verbose, const char *list_user) policy_list(int argc, char * const argv[], int verbose, const char *list_user,
const char **errstr)
{ {
/* /*
* List user's capabilities. * List user's capabilities.
@@ -360,7 +363,8 @@ policy_list(int argc, char * const argv[], int verbose, const char *list_user)
static int static int
policy_version(int verbose) policy_version(int verbose)
{ {
sudo_log(SUDO_CONV_INFO_MSG, "Sample policy plugin version %s\n", PACKAGE_VERSION); sudo_log(SUDO_CONV_INFO_MSG, "Sample policy plugin version %s\n",
PACKAGE_VERSION);
return true; return true;
} }
@@ -386,9 +390,10 @@ policy_close(int exit_status, int error)
static int static int
io_open(unsigned int version, sudo_conv_t conversation, io_open(unsigned int version, sudo_conv_t conversation,
sudo_printf_t sudo_printf, char * const settings[], sudo_printf_t sudo_plugin_printf, char * const settings[],
char * const user_info[], char * const command_info[], char * const user_info[], char * const command_info[],
int argc, char * const argv[], char * const user_env[], char * const args[]) int argc, char * const argv[], char * const user_env[], char * const args[],
const char **errstr)
{ {
int fd; int fd;
char path[PATH_MAX]; char path[PATH_MAX];
@@ -432,14 +437,14 @@ io_version(int verbose)
} }
static int static int
io_log_input(const char *buf, unsigned int len) io_log_input(const char *buf, unsigned int len, const char **errstr)
{ {
ignore_result(fwrite(buf, len, 1, input)); ignore_result(fwrite(buf, len, 1, input));
return true; return true;
} }
static int static int
io_log_output(const char *buf, unsigned int len) io_log_output(const char *buf, unsigned int len, const char **errstr)
{ {
const char *cp, *ep; const char *cp, *ep;
bool ret = true; bool ret = true;
@@ -471,7 +476,8 @@ sudo_dso_public struct policy_plugin sample_policy = {
NULL, /* invalidate */ NULL, /* invalidate */
NULL, /* init_session */ NULL, /* init_session */
NULL, /* register_hooks */ NULL, /* register_hooks */
NULL /* deregister_hooks */ NULL, /* deregister_hooks */
NULL /* event_alloc() filled in by sudo */
}; };
/* /*
@@ -488,5 +494,10 @@ sudo_dso_public struct io_plugin sample_io = {
io_log_output, /* tty output */ io_log_output, /* tty output */
io_log_input, /* command stdin if not tty */ io_log_input, /* command stdin if not tty */
io_log_output, /* command stdout if not tty */ io_log_output, /* command stdout if not tty */
io_log_output /* command stderr if not tty */ io_log_output, /* command stderr if not tty */
NULL, /* register_hooks */
NULL, /* deregister_hooks */
NULL, /* change_winsize */
NULL, /* log_suspend */
NULL /* event_alloc() filled in by sudo */
}; };