=== modified file 'TODO' --- TODO 2008-08-15 20:47:22 +0000 +++ TODO 2008-08-16 03:29:08 +0000 @@ -3,10 +3,13 @@ * [#A] README file * Plugin-runner +** [#A] Free plugin name and args + [[file:plugin-runner.c::free%20plugin_list][file:plugin-runner.c::free plugin_list]] (both places) ** [#A] Change syntax for arguments ** [#B] Add more comments to code ** [#B] Add more if(debug) calls ** [#B] Seperate more code to function for more readability +** [#B] Make free_plugin_list() function ** [#A] Man page: man8/plugin-runner.8mandos *** DESCRIPTION Describe the plus sign syntax for passing options from crypttab @@ -25,13 +28,16 @@ Text needed *** SEE ALSO Explaining test on what you can read -** Use asprintf instead of malloc and strcat? ** Support in configuration file for environment variables ** Keydir move: /etc/mandos -> /etc/keys/mandos Must create in preinst if not pre-depending on cryptsetup * Password-request ** [#A] Man page: man8/password-request.8mandos +** Make prompt exactly like the normal prompt + Use environment variables: + "Enter passphrase to unlock the disk $cryptsource ($crypttarget): " + [[file:plugins.d/password-prompt.c::fprintf%20stderr%20s%20Password%20prefix][Here]] ** [#B] Temporarily lower kernel log level for less printouts during sucessfull boot. *** DESCRIPTION === modified file 'plugin-runner.c' --- plugin-runner.c 2008-08-15 21:09:25 +0000 +++ plugin-runner.c 2008-08-16 03:29:08 +0000 @@ -21,8 +21,8 @@ * Contact the authors at . */ -#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), getline() */ - +#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), getline(), + asprintf() */ #include /* size_t, NULL */ #include /* malloc(), exit(), EXIT_FAILURE, EXIT_SUCCESS, realloc() */ @@ -51,8 +51,7 @@ close() */ #include /* fcntl(), F_GETFD, F_SETFD, FD_CLOEXEC */ -#include /* strsep, strlen(), strcpy(), - strcat() */ +#include /* strsep, strlen(), asprintf() */ #include /* errno */ #include /* struct argp_option, struct argp_state, struct argp, @@ -88,6 +87,8 @@ char *name; /* can be NULL or any plugin name */ char **argv; int argc; + char **environ; + int envc; bool disabled; struct plugin *next; } plugin; @@ -102,35 +103,73 @@ /* Create a new plugin */ plugin *new_plugin = malloc(sizeof(plugin)); if (new_plugin == NULL){ - perror("malloc"); - exit(EXIT_FAILURE); + return NULL; } - new_plugin->name = name; + *new_plugin = (plugin) { .name = name, + .argc = 1, + .envc = 0, + .disabled = false, + .next = *plugin_list }; + new_plugin->argv = malloc(sizeof(char *) * 2); if (new_plugin->argv == NULL){ - perror("malloc"); - exit(EXIT_FAILURE); + free(new_plugin); + return NULL; } new_plugin->argv[0] = name; new_plugin->argv[1] = NULL; - new_plugin->argc = 1; - new_plugin->disabled = false; - new_plugin->next = *plugin_list; + + new_plugin->environ = malloc(sizeof(char *)); + if(new_plugin->environ == NULL){ + free(new_plugin->argv); + free(new_plugin); + return NULL; + } + new_plugin->environ[0] = NULL; /* Append the new plugin to the list */ *plugin_list = new_plugin; return new_plugin; } -static void addargument(plugin *p, char *arg){ - p->argv[p->argc] = arg; - p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2)); - if (p->argv == NULL){ - perror("malloc"); - exit(EXIT_FAILURE); - } - p->argc++; - p->argv[p->argc] = NULL; -} +/* Helper function for add_argument and add_environment */ +static bool add_to_char_array(const char *new, char ***array, + int *len){ + /* Resize the pointed-to array to hold one more pointer */ + *array = realloc(*array, sizeof(char *) + * (size_t) ((*len) + 2)); + /* Malloc check */ + if(*array == NULL){ + return false; + } + /* Make a copy of the new string */ + char *copy = strdup(new); + if(copy == NULL){ + return false; + } + /* Insert the copy */ + (*array)[*len] = copy; + (*len)++; + /* Add a new terminating NULL pointer to the last element */ + (*array)[*len] = NULL; + return true; +} + +/* Add to a plugin's argument vector */ +static bool add_argument(plugin *p, const char *arg){ + if(p == NULL){ + return false; + } + return add_to_char_array(arg, &(p->argv), &(p->argc)); +} + +/* Add to a plugin's environment */ +static bool add_environment(plugin *p, const char *def){ + if(p == NULL){ + return false; + } + return add_to_char_array(def, &(p->environ), &(p->envc)); +} + /* * Based on the example in the GNU LibC manual chapter 13.13 "File @@ -186,8 +225,7 @@ return true; } -char ** addcustomargument(char **argv, int *argc, char *arg){ - +char **add_to_argv(char **argv, int *argc, char *arg){ if (argv == NULL){ *argc = 1; argv = malloc(sizeof(char*) * 2); @@ -246,9 +284,15 @@ { .name = "global-options", .key = 'g', .arg = "OPTION[,OPTION[,...]]", .doc = "Options passed to all plugins" }, + { .name = "global-envs", .key = 'e', + .arg = "VAR=value", + .doc = "Environment variable passed to all plugins" }, { .name = "options-for", .key = 'o', .arg = "PLUGIN:OPTION[,OPTION[,...]]", .doc = "Options passed only to specified plugin" }, + { .name = "envs-for", .key = 'f', + .arg = "PLUGIN:ENV=value", + .doc = "Environment variable passed to specified plugin" }, { .name = "disable", .key = 'd', .arg = "PLUGIN", .doc = "Disable a specific plugin", .group = 1 }, @@ -278,14 +322,31 @@ if(p[0] == '\0'){ continue; } - addargument(getplugin(NULL, plugins), p); + if(not add_argument(getplugin(NULL, plugins), p)){ + perror("add_argument"); + return ARGP_ERR_UNKNOWN; + } + } + } + break; + case 'e': + if(arg == NULL){ + break; + } + { + char *envdef = strdup(arg); + if(envdef == NULL){ + break; + } + if(not add_environment(getplugin(NULL, plugins), envdef)){ + perror("add_environment"); } } break; case 'o': if (arg != NULL){ - char *name = strsep(&arg, ":"); - if(name[0] == '\0'){ + char *p_name = strsep(&arg, ":"); + if(p_name[0] == '\0'){ break; } char *opt = strsep(&arg, ":"); @@ -298,14 +359,40 @@ if(p[0] == '\0'){ continue; } - addargument(getplugin(name, plugins), p); + if(not add_argument(getplugin(p_name, plugins), p)){ + perror("add_argument"); + return ARGP_ERR_UNKNOWN; + } } } } break; + case 'f': + if(arg == NULL){ + break; + } + { + char *envdef = strchr(arg, ':'); + if(envdef == NULL){ + break; + } + char *p_name = strndup(arg, (size_t) (envdef-arg)); + if(p_name == NULL){ + break; + } + envdef++; + if(not add_environment(getplugin(p_name, plugins), envdef)){ + perror("add_environment"); + } + } + break; case 'd': if (arg != NULL){ - getplugin(arg, plugins)->disabled = true; + plugin *p = getplugin(arg, plugins); + if(p == NULL){ + return ARGP_ERR_UNKNOWN; + } + p->disabled = true; } break; case 128: @@ -321,7 +408,7 @@ debug = true; break; case ARGP_KEY_ARG: - fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); + fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); break; case ARGP_KEY_END: break; @@ -366,9 +453,9 @@ continue; } new_arg = strdup(p); - custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg); + custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg); if (custom_argv == NULL){ - perror("addcustomargument"); + perror("add_to_argv"); exitstatus = EXIT_FAILURE; goto end; } @@ -402,6 +489,10 @@ for(char **a = p->argv; *a != NULL; a++){ fprintf(stderr, "\tArg: %s\n", *a); } + fprintf(stderr, "...and %u environment variables\n", p->envc); + for(char **a = p->environ; *a != NULL; a++){ + fprintf(stderr, "\t%s\n", *a); + } } } @@ -496,15 +587,13 @@ continue; } } - - char *filename = malloc(d_name_len + strlen(plugindir) + 2); - if (filename == NULL){ - perror("malloc"); + + char *filename; + ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name); + if(ret < 0){ + perror("asprintf"); continue; } - strcpy(filename, plugindir); /* Spurious warning */ - strcat(filename, "/"); /* Spurious warning */ - strcat(filename, dirst->d_name); /* Spurious warning */ ret = stat(filename, &st); if (ret == -1){ @@ -521,7 +610,13 @@ free(filename); continue; } - if(getplugin(dirst->d_name, &plugin_list)->disabled){ + plugin *p = getplugin(dirst->d_name, &plugin_list); + if(p == NULL){ + perror("getplugin"); + free(filename); + continue; + } + if(p->disabled){ if(debug){ fprintf(stderr, "Ignoring disabled plugin \"%s\"\n", dirst->d_name); @@ -529,14 +624,39 @@ free(filename); continue; } - plugin *p = getplugin(dirst->d_name, &plugin_list); { /* Add global arguments to argument list for this plugin */ plugin *g = getplugin(NULL, &plugin_list); - for(char **a = g->argv + 1; *a != NULL; a++){ - addargument(p, *a); - } - } + if(g != NULL){ + for(char **a = g->argv + 1; *a != NULL; a++){ + if(not add_argument(p, *a)){ + perror("add_argument"); + } + } + /* Add global environment variables */ + for(char **e = g->environ; *e != NULL; e++){ + if(not add_environment(p, *e)){ + perror("add_environment"); + } + } + } + } + /* If this plugin has any environment variables, we will call + using execve and need to duplicate the environment from this + process, too. */ + if(p->environ[0] != NULL){ + for(char **e = environ; *e != NULL; e++){ + char *copy = strdup(*e); + if(copy == NULL){ + perror("strdup"); + continue; + } + if(not add_environment(p, copy)){ + perror("add_environment"); + } + } + } + int pipefd[2]; ret = pipe(pipefd); if (ret == -1){ @@ -594,9 +714,16 @@ above and must now close it manually here. */ closedir(dir); } - if(execv(filename, p->argv) < 0){ - perror("execv"); - _exit(EXIT_FAILURE); + if(p->environ[0] == NULL){ + if(execv(filename, p->argv) < 0){ + perror("execv"); + _exit(EXIT_FAILURE); + } + } else { + if(execve(filename, p->argv, p->environ) < 0){ + perror("execve"); + _exit(EXIT_FAILURE); + } } /* no return */ } @@ -640,6 +767,11 @@ for(plugin *next; plugin_list != NULL; plugin_list = next){ next = plugin_list->next; free(plugin_list->argv); + if(plugin_list->environ[0] != NULL){ + for(char **e = plugin_list->environ; *e != NULL; e++){ + free(*e); + } + } free(plugin_list); } @@ -783,6 +915,12 @@ for(plugin *next; plugin_list != NULL; plugin_list = next){ next = plugin_list->next; free(plugin_list->argv); + if(plugin_list->environ[0] != NULL){ + for(char **e = plugin_list->environ; *e != NULL; e++){ + free(*e); + } + } + free(plugin_list->environ); free(plugin_list); }