From: Todd C. Miller Date: Wed, 6 Apr 2011 21:51:36 +0000 (-0400) Subject: Load plugins after parsing arguments and potentially printing the X-Git-Tag: SUDO_1_8_1~3^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b6d0a28efb4afbb5b15a9c6fb125430fb4173860;p=sudo Load plugins after parsing arguments and potentially printing the version. That way, an error loading or initializing a plugin doesn't break "sudo -h" or "sudo -V". --- diff --git a/src/load_plugins.c b/src/load_plugins.c index 979e7cf55..68be4237d 100644 --- a/src/load_plugins.c +++ b/src/load_plugins.c @@ -131,7 +131,7 @@ done: /* * Load the plugins listed in conf_file. */ -void +int sudo_load_plugins(const char *conf_file, struct plugin_container *policy_plugin, struct plugin_container_list *io_plugins) @@ -143,47 +143,65 @@ sudo_load_plugins(const char *conf_file, struct stat sb; void *handle; char path[PATH_MAX]; + int rval = FALSE; /* Parse sudo.conf */ plugin_list = sudo_read_conf(conf_file); tq_foreach_fwd(plugin_list, info) { if (info->path[0] == '/') { - if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) - errorx(1, "%s: %s", info->path, strerror(ENAMETOOLONG)); + if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) { + warningx("%s: %s", info->path, strerror(ENAMETOOLONG)); + goto done; + } } else { if (snprintf(path, sizeof(path), "%s%s", _PATH_SUDO_PLUGIN_DIR, info->path) >= sizeof(path)) { - errorx(1, "%s%s: %s", _PATH_SUDO_PLUGIN_DIR, info->path, + warningx("%s%s: %s", _PATH_SUDO_PLUGIN_DIR, info->path, strerror(ENAMETOOLONG)); + goto done; } } - if (stat(path, &sb) != 0) - error(1, "%s", path); - if (sb.st_uid != ROOT_UID) - errorx(1, "%s must be owned by uid %d", path, ROOT_UID); - if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) - errorx(1, "%s must be only be writable by owner", path); + if (stat(path, &sb) != 0) { + warning("%s", path); + goto done; + } + if (sb.st_uid != ROOT_UID) { + warningx("%s must be owned by uid %d", path, ROOT_UID); + goto done; + } + if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) { + warningx("%s must be only be writable by owner", path); + goto done; + } /* Open plugin and map in symbol */ handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL); - if (!handle) - errorx(1, "unable to dlopen %s: %s", path, dlerror()); + if (!handle) { + warningx("unable to dlopen %s: %s", path, dlerror()); + goto done; + } plugin = dlsym(handle, info->symbol_name); - if (!plugin) - errorx(1, "unable to find symbol %s in %s", info->symbol_name, path); + if (!plugin) { + warningx("unable to find symbol %s in %s", info->symbol_name, path); + goto done; + } if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) { - errorx(1, "%s: unknown policy type %d", path, plugin->type); + warningx("%s: unknown policy type %d", path, plugin->type); + goto done; } if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) { - errorx(1, "%s: incompatible policy major version %d, expected %d", + warningx("%s: incompatible policy major version %d, expected %d", path, SUDO_API_VERSION_GET_MAJOR(plugin->version), SUDO_API_VERSION_MAJOR); + goto done; } if (plugin->type == SUDO_POLICY_PLUGIN) { - if (policy_plugin->handle) - errorx(1, "only a single policy plugin may be loaded"); + if (policy_plugin->handle) { + warningx("only a single policy plugin may be loaded"); + goto done; + } policy_plugin->handle = handle; policy_plugin->name = info->symbol_name; policy_plugin->u.generic = plugin; @@ -197,8 +215,18 @@ sudo_load_plugins(const char *conf_file, tq_append(io_plugins, container); } } - if (policy_plugin->handle == NULL) - errorx(1, "%s: at least one policy plugin must be specified", conf_file); - if (policy_plugin->u.policy->check_policy == NULL) - errorx(1, "policy plugin %s does not include a check_policy method"); + if (policy_plugin->handle == NULL) { + warningx("%s: at least one policy plugin must be specified", conf_file); + goto done; + } + if (policy_plugin->u.policy->check_policy == NULL) { + warningx("policy plugin %s does not include a check_policy method", + policy_plugin->name); + goto done; + } + + rval = TRUE; + +done: + return rval; } diff --git a/src/sudo.c b/src/sudo.c index fed5a247a..1f9b5653a 100644 --- a/src/sudo.c +++ b/src/sudo.c @@ -199,20 +199,21 @@ main(int argc, char *argv[], char *envp[]) memset(&user_details, 0, sizeof(user_details)); user_info = get_user_info(&user_details); - /* Read sudo.conf and load plugins. */ - sudo_load_plugins(_PATH_SUDO_CONF, &policy_plugin, &io_plugins); - /* Parse command line arguments. */ sudo_mode = parse_args(argc, argv, &nargc, &nargv, &settings, &env_add); sudo_debug(9, "sudo_mode %d", sudo_mode); - /* Print sudo version early, in case policy plugin init fails. */ + /* Print sudo version early, in case of plugin init failure. */ if (ISSET(sudo_mode, MODE_VERSION)) { printf("Sudo version %s\n", PACKAGE_VERSION); if (user_details.uid == ROOT_UID) (void) printf("Configure args: %s\n", CONFIGURE_ARGS); } + /* Read sudo.conf and load plugins. */ + if (!sudo_load_plugins(_PATH_SUDO_CONF, &policy_plugin, &io_plugins)) + errorx(1, "fatal error, unable to load plugins"); + /* Open policy plugin. */ ok = policy_open(&policy_plugin, settings, user_info, envp); if (ok != TRUE) { diff --git a/src/sudo_plugin_int.h b/src/sudo_plugin_int.h index afec901f9..772cde9c6 100644 --- a/src/sudo_plugin_int.h +++ b/src/sudo_plugin_int.h @@ -78,7 +78,7 @@ int sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[], struct sudo_conv_reply replies[]); int _sudo_printf(int msg_type, const char *fmt, ...); -void sudo_load_plugins(const char *conf_file, +int sudo_load_plugins(const char *conf_file, struct plugin_container *policy_plugin, struct plugin_container_list *io_plugins);