95
97
static plugin *plugin_list = NULL;
97
/* Gets an existing plugin based on name,
99
/* Gets a existing plugin based on name,
98
100
or if none is found, creates a new one */
99
101
static plugin *getplugin(char *name){
100
102
/* Check for exiting plugin with that name */
185
187
size_t namelen = (size_t)(strchrnul(def, '=') - def);
186
188
/* Search for this environment variable */
187
189
for(char **e = p->environ; *e != NULL; e++){
188
if(strncmp(*e, def, namelen + 1) == 0){
190
if(strncmp(*e, def, namelen+1) == 0){
189
191
/* It already exists */
191
char *new = realloc(*e, strlen(def) + 1);
193
char *new = realloc(*e, strlen(def));
206
208
* Descriptor Flags".
207
209
* *Note File Descriptor Flags:(libc)Descriptor Flags.
209
static int set_cloexec_flag(int fd){
211
static int set_cloexec_flag(int fd)
210
213
int ret = fcntl(fd, F_GETFD, 0);
211
214
/* If reading the flags failed, return error indication now. */
220
223
/* Mark processes as completed when they exit, and save their exit
222
static void handle_sigchld(__attribute__((unused)) int sig){
225
void handle_sigchld(__attribute__((unused)) int sig){
224
227
plugin *proc = plugin_list;
252
255
/* Prints out a password to stdout */
253
static bool print_out_password(const char *buffer, size_t length){
256
bool print_out_password(const char *buffer, size_t length){
258
if(length>0 and buffer[length-1] == '\n'){
255
261
for(size_t written = 0; written < length; written += (size_t)ret){
256
262
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
257
263
length - written));
338
344
{ .name = "global-options", .key = 'g',
339
345
.arg = "OPTION[,OPTION[,...]]",
340
346
.doc = "Options passed to all plugins" },
341
{ .name = "global-env", .key = 'G',
347
{ .name = "global-env", .key = 'e',
342
348
.arg = "VAR=value",
343
349
.doc = "Environment variable passed to all plugins" },
344
350
{ .name = "options-for", .key = 'o',
345
351
.arg = "PLUGIN:OPTION[,OPTION[,...]]",
346
352
.doc = "Options passed only to specified plugin" },
347
{ .name = "env-for", .key = 'E',
353
{ .name = "env-for", .key = 'f',
348
354
.arg = "PLUGIN:ENV=value",
349
355
.doc = "Environment variable passed to specified plugin" },
350
356
{ .name = "disable", .key = 'd',
352
358
.doc = "Disable a specific plugin", .group = 1 },
353
{ .name = "enable", .key = 'e',
355
.doc = "Enable a specific plugin", .group = 1 },
356
359
{ .name = "plugin-dir", .key = 128,
357
360
.arg = "DIRECTORY",
358
361
.doc = "Specify a different plugin directory", .group = 2 },
390
case 'G': /* --global-env */
393
case 'e': /* --global-env */
394
if(not add_environment(getplugin(NULL), arg, true)){
395
perror("add_environment");
398
char *envdef = strdup(arg);
402
if(not add_environment(getplugin(NULL), envdef, true)){
403
perror("add_environment");
398
407
case 'o': /* --options-for */
399
408
if (arg != NULL){
400
409
char *p_name = strsep(&arg, ":");
401
if(p_name[0] == '\0' or arg == NULL){
410
if(p_name[0] == '\0'){
404
413
char *opt = strsep(&arg, ":");
405
if(opt[0] == '\0' or opt == NULL){
409
while((p = strsep(&opt, ",")) != NULL){
413
if(not add_argument(getplugin(p_name), p)){
414
perror("add_argument");
415
return ARGP_ERR_UNKNOWN;
419
while((p = strsep(&opt, ",")) != NULL){
423
if(not add_argument(getplugin(p_name), p)){
424
perror("add_argument");
425
return ARGP_ERR_UNKNOWN;
420
case 'E': /* --env-for */
431
case 'f': /* --env-for */
426
437
if(envdef == NULL){
430
if(not add_environment(getplugin(arg), envdef+1, true)){
440
char *p_name = strndup(arg, (size_t) (envdef-arg));
445
if(not add_environment(getplugin(p_name), envdef, true)){
431
446
perror("add_environment");
472
477
case ARGP_KEY_ARG:
473
/* Cryptsetup always passes an argument, which is an empty
474
string if "none" was specified in /etc/crypttab. So if
475
argument was empty, we ignore it silently. */
477
fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
478
fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
480
480
case ARGP_KEY_END:
492
492
struct argp_state *state) {
494
494
case 'g': /* --global-options */
495
case 'G': /* --global-env */
495
case 'e': /* --global-env */
496
496
case 'o': /* --options-for */
497
case 'E': /* --env-for */
497
case 'f': /* --env-for */
498
498
case 'd': /* --disable */
499
case 'e': /* --enable */
500
499
case 128: /* --plugin-dir */
502
501
case 129: /* --config-file */
504
502
argfile = strdup(arg);
505
503
if(argfile == NULL){
506
504
perror("strdup");
739
if(plugindir == NULL){
740
ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
742
ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
737
ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
745
739
perror("asprintf");
936
930
/* OK, now either a process completed, or something can be read
937
931
from one of them */
938
for(plugin *proc = plugin_list; proc != NULL;){
932
for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
939
933
/* Is this process completely done? */
940
934
if(proc->eof and proc->completed){
941
935
/* Only accept the plugin output if it exited cleanly */
968
962
exitstatus = EXIT_FAILURE;
972
966
/* We are done modifying process list, so unblock signal */
973
967
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
1002
992
/* This process has not completed. Does it have any output? */
1003
993
if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1004
994
/* This process had nothing to say at this time */
1008
997
/* Before reading, make the process' data buffer large enough */
1043
1031
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1044
1032
char *passwordbuffer = getpass("Password: ");
1045
size_t len = strlen(passwordbuffer);
1046
/* Strip trailing newline */
1047
if(len > 0 and passwordbuffer[len-1] == '\n'){
1048
passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1051
bret = print_out_password(passwordbuffer, len);
1033
bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1053
1035
perror("print_out_password");
1054
1036
exitstatus = EXIT_FAILURE;