/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-11 07:41:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080811074140-3wrl2jux1s0jy5kk
* mandos-client.c (print_out_password): Strip trailing '\n'.
  (main): Do not add trailing newline on fallback.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
22
22
 */
23
23
 
24
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
25
 
                                   asprintf() */
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
 
25
 
26
26
#include <stddef.h>             /* size_t, NULL */
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
28
                                   EXIT_SUCCESS, realloc() */
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
53
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strsep, strlen(), asprintf() */
 
54
#include <string.h>             /* strtok, strlen(), strcpy(),
 
55
                                   strcat() */
55
56
#include <errno.h>              /* errno */
56
57
#include <argp.h>               /* struct argp_option, struct
57
58
                                   argp_state, struct argp,
64
65
#include <errno.h>              /* errno, EBADF */
65
66
 
66
67
#define BUFFER_SIZE 256
67
 
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
68
68
 
69
 
const char *argp_program_version = "plugin-runner 1.0";
 
69
const char *argp_program_version = "mandos-client 1.0";
70
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
71
 
72
72
struct process;
87
87
  char *name;                   /* can be NULL or any plugin name */
88
88
  char **argv;
89
89
  int argc;
90
 
  char **environ;
91
 
  int envc;
92
90
  bool disabled;
93
91
  struct plugin *next;
94
92
} plugin;
103
101
  /* Create a new plugin */
104
102
  plugin *new_plugin = malloc(sizeof(plugin));
105
103
  if (new_plugin == NULL){
106
 
    return NULL;
107
 
  }
108
 
  char *copy_name = strdup(name);
109
 
  if(copy_name == NULL){
110
 
    return NULL;
111
 
  }
112
 
  
113
 
  *new_plugin = (plugin) { .name = copy_name,
114
 
                           .argc = 1,
115
 
                           .envc = 0,
116
 
                           .disabled = false,
117
 
                           .next = *plugin_list };
118
 
  
 
104
    perror("malloc");
 
105
    exit(EXIT_FAILURE);
 
106
  }
 
107
  new_plugin->name = name;
119
108
  new_plugin->argv = malloc(sizeof(char *) * 2);
120
109
  if (new_plugin->argv == NULL){
121
 
    free(new_plugin);
122
 
    return NULL;
 
110
    perror("malloc");
 
111
    exit(EXIT_FAILURE);
123
112
  }
124
 
  new_plugin->argv[0] = copy_name;
 
113
  new_plugin->argv[0] = name;
125
114
  new_plugin->argv[1] = NULL;
126
 
 
127
 
  new_plugin->environ = malloc(sizeof(char *));
128
 
  if(new_plugin->environ == NULL){
129
 
    free(new_plugin->argv);
130
 
    free(new_plugin);
131
 
    return NULL;
132
 
  }
133
 
  new_plugin->environ[0] = NULL;
 
115
  new_plugin->argc = 1;
 
116
  new_plugin->disabled = false;
 
117
  new_plugin->next = *plugin_list;
134
118
  /* Append the new plugin to the list */
135
119
  *plugin_list = new_plugin;
136
120
  return new_plugin;
137
121
}
138
122
 
139
 
/* Helper function for add_argument and add_environment */
140
 
static bool add_to_char_array(const char *new, char ***array,
141
 
                              int *len){
142
 
  /* Resize the pointed-to array to hold one more pointer */
143
 
  *array = realloc(*array, sizeof(char *)
144
 
                   * (size_t) ((*len) + 2));
145
 
  /* Malloc check */
146
 
  if(*array == NULL){
147
 
    return false;
148
 
  }
149
 
  /* Make a copy of the new string */
150
 
  char *copy = strdup(new);
151
 
  if(copy == NULL){
152
 
    return false;
153
 
  }
154
 
  /* Insert the copy */
155
 
  (*array)[*len] = copy;
156
 
  (*len)++;
157
 
  /* Add a new terminating NULL pointer to the last element */
158
 
  (*array)[*len] = NULL;
159
 
  return true;
160
 
}
161
 
 
162
 
/* Add to a plugin's argument vector */
163
 
static bool add_argument(plugin *p, const char *arg){
164
 
  if(p == NULL){
165
 
    return false;
166
 
  }
167
 
  return add_to_char_array(arg, &(p->argv), &(p->argc));
168
 
}
169
 
 
170
 
/* Add to a plugin's environment */
171
 
static bool add_environment(plugin *p, const char *def){
172
 
  if(p == NULL){
173
 
    return false;
174
 
  }
175
 
  return add_to_char_array(def, &(p->environ), &(p->envc));
176
 
}
177
 
 
 
123
static void addargument(plugin *p, char *arg){
 
124
  p->argv[p->argc] = arg;
 
125
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
 
126
  if (p->argv == NULL){
 
127
    perror("malloc");
 
128
    exit(EXIT_FAILURE);
 
129
  }
 
130
  p->argc++;
 
131
  p->argv[p->argc] = NULL;
 
132
}
178
133
 
179
134
/*
180
135
 * Based on the example in the GNU LibC manual chapter 13.13 "File
230
185
  return true;
231
186
}
232
187
 
233
 
char **add_to_argv(char **argv, int *argc, char *arg){
234
 
  if (argv == NULL){
235
 
    *argc = 1;
236
 
    argv = malloc(sizeof(char*) * 2);
237
 
    if(argv == NULL){
238
 
      return NULL;
239
 
    }
240
 
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
241
 
    argv[1] = NULL;
242
 
  }
243
 
  *argc += 1;
244
 
  argv = realloc(argv, sizeof(char *)
245
 
                  * ((unsigned int) *argc + 1));
246
 
  if(argv == NULL){
247
 
    return NULL;
248
 
  }
249
 
  argv[*argc-1] = arg;
250
 
  argv[*argc] = NULL;   
251
 
  return argv;
252
 
}
253
 
 
254
 
static void free_plugin_list(plugin *plugin_list){
255
 
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
256
 
    next = plugin_list->next;
257
 
    free(plugin_list->name);
258
 
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
259
 
      free(*arg);
260
 
    }    
261
 
    free(plugin_list->argv);
262
 
    for(char **env = plugin_list->environ; *env != NULL; env++){
263
 
      free(*env);
264
 
    }
265
 
    free(plugin_list->environ);
266
 
    free(plugin_list);
267
 
  }  
268
 
}
269
 
 
270
188
int main(int argc, char *argv[]){
271
 
  const char *plugindir = "/lib/mandos/plugins.d";
272
 
  const char *argfile = ARGFILE;
273
 
  FILE *conffp;
 
189
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
274
190
  size_t d_name_len;
275
191
  DIR *dir = NULL;
276
192
  struct dirent *dirst;
284
200
  struct sigaction old_sigchld_action;
285
201
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
286
202
                                      .sa_flags = SA_NOCLDSTOP };
287
 
  char **custom_argv = NULL;
288
 
  int custom_argc = 0;
 
203
  char *plus_options = NULL;
 
204
  char **plus_argv = NULL;
289
205
  
290
206
  /* Establish a signal handler */
291
207
  sigemptyset(&sigchld_action.sa_mask);
292
208
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
293
 
  if(ret == -1){
 
209
  if(ret < 0){
294
210
    perror("sigaddset");
295
 
    exitstatus = EXIT_FAILURE;
296
 
    goto fallback;
 
211
    exit(EXIT_FAILURE);
297
212
  }
298
213
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
299
 
  if(ret == -1){
 
214
  if(ret < 0){
300
215
    perror("sigaction");
301
 
    exitstatus = EXIT_FAILURE;    
302
 
    goto fallback;
 
216
    exit(EXIT_FAILURE);
303
217
  }
304
218
  
305
219
  /* The options we understand. */
307
221
    { .name = "global-options", .key = 'g',
308
222
      .arg = "OPTION[,OPTION[,...]]",
309
223
      .doc = "Options passed to all plugins" },
310
 
    { .name = "global-envs", .key = 'e',
311
 
      .arg = "VAR=value",
312
 
      .doc = "Environment variable passed to all plugins" },
313
224
    { .name = "options-for", .key = 'o',
314
225
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
315
226
      .doc = "Options passed only to specified plugin" },
316
 
    { .name = "envs-for", .key = 'f',
317
 
      .arg = "PLUGIN:ENV=value",
318
 
      .doc = "Environment variable passed to specified plugin" },
319
227
    { .name = "disable", .key = 'd',
320
228
      .arg = "PLUGIN",
321
229
      .doc = "Disable a specific plugin", .group = 1 },
340
248
    switch (key) {
341
249
    case 'g':
342
250
      if (arg != NULL){
343
 
        char *p;
344
 
        while((p = strsep(&arg, ",")) != NULL){
345
 
          if(p[0] == '\0'){
346
 
            continue;
347
 
          }
348
 
          if(not add_argument(getplugin(NULL, plugins), p)){
349
 
            perror("add_argument");
350
 
            return ARGP_ERR_UNKNOWN;
351
 
          }
352
 
        }
353
 
      }
354
 
      break;
355
 
    case 'e':
356
 
      if(arg == NULL){
357
 
        break;
358
 
      }
359
 
      {
360
 
        char *envdef = strdup(arg);
361
 
        if(envdef == NULL){
362
 
          break;
363
 
        }
364
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
365
 
          perror("add_environment");
366
 
        }
 
251
        char *p = strtok(arg, ",");
 
252
        do{
 
253
          addargument(getplugin(NULL, plugins), p);
 
254
          p = strtok(NULL, ",");
 
255
        } while (p != NULL);
367
256
      }
368
257
      break;
369
258
    case 'o':
370
259
      if (arg != NULL){
371
 
        char *p_name = strsep(&arg, ":");
372
 
        if(p_name[0] == '\0'){
373
 
          break;
374
 
        }
375
 
        char *opt = strsep(&arg, ":");
376
 
        if(opt[0] == '\0'){
377
 
          break;
378
 
        }
379
 
        if(opt != NULL){
380
 
          char *p;
381
 
          while((p = strsep(&opt, ",")) != NULL){
382
 
            if(p[0] == '\0'){
383
 
              continue;
384
 
            }
385
 
            if(not add_argument(getplugin(p_name, plugins), p)){
386
 
              perror("add_argument");
387
 
              return ARGP_ERR_UNKNOWN;
388
 
            }
389
 
          }
390
 
        }
391
 
      }
392
 
      break;
393
 
    case 'f':
394
 
      if(arg == NULL){
395
 
        break;
396
 
      }
397
 
      {
398
 
        char *envdef = strchr(arg, ':');
399
 
        if(envdef == NULL){
400
 
          break;
401
 
        }
402
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
403
 
        if(p_name == NULL){
404
 
          break;
405
 
        }
406
 
        envdef++;
407
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
408
 
          perror("add_environment");
 
260
        char *name = strtok(arg, ":");
 
261
        char *p = strtok(NULL, ":");
 
262
        if(p != NULL){
 
263
          p = strtok(p, ",");
 
264
          do{
 
265
            addargument(getplugin(name, plugins), p);
 
266
            p = strtok(NULL, ",");
 
267
          } while (p != NULL);
409
268
        }
410
269
      }
411
270
      break;
412
271
    case 'd':
413
272
      if (arg != NULL){
414
 
        plugin *p = getplugin(arg, plugins);
415
 
        if(p == NULL){
416
 
          return ARGP_ERR_UNKNOWN;
417
 
        }
418
 
        p->disabled = true;
 
273
        getplugin(arg, plugins)->disabled = true;
419
274
      }
420
275
      break;
421
276
    case 128:
431
286
      debug = true;
432
287
      break;
433
288
    case ARGP_KEY_ARG:
434
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
289
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
290
        argp_usage (state);
 
291
      }
 
292
      plus_options = arg;
435
293
      break;
436
294
    case ARGP_KEY_END:
437
295
      break;
451
309
  if (ret == ARGP_ERR_UNKNOWN){
452
310
    fprintf(stderr, "Unknown error while parsing arguments\n");
453
311
    exitstatus = EXIT_FAILURE;
454
 
    goto fallback;
 
312
    goto end;
455
313
  }
456
 
 
457
 
  conffp = fopen(argfile, "r");
458
 
  if(conffp != NULL){
459
 
    char *org_line = NULL;
460
 
    char *p, *arg, *new_arg, *line;
461
 
    size_t size = 0;
462
 
    ssize_t sret;
463
 
    const char whitespace_delims[] = " \r\t\f\v\n";
464
 
    const char comment_delim[] = "#";
465
 
 
466
 
    while(true){
467
 
      sret = getline(&org_line, &size, conffp);
468
 
      if(sret == -1){
469
 
        break;
470
 
      }
471
 
 
472
 
      line = org_line;
473
 
      arg = strsep(&line, comment_delim);
474
 
      while((p = strsep(&arg, whitespace_delims)) != NULL){
475
 
        if(p[0] == '\0'){
476
 
          continue;
477
 
        }
478
 
        new_arg = strdup(p);
479
 
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
480
 
        if (custom_argv == NULL){
481
 
          perror("add_to_argv");
482
 
          exitstatus = EXIT_FAILURE;
483
 
          goto fallback;
484
 
        }
485
 
      }
486
 
    }
487
 
    free(org_line);
488
 
  } else{
489
 
    /* Check for harmful errors and go to fallback. Other errors might
490
 
       not affect opening plugins */
491
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
492
 
      perror("fopen");
 
314
  
 
315
  if(plus_options){
 
316
    /* This is a mangled argument in the form of
 
317
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
318
    /* Make new argc and argv vars, and call argp_parse() again. */
 
319
    plus_options++;             /* skip the first '+' character */
 
320
    const char delims[] = "+";
 
321
    char *arg;
 
322
    int new_argc = 1;
 
323
    plus_argv = malloc(sizeof(char*) * 2);
 
324
    if(plus_argv == NULL){
 
325
      perror("malloc");
493
326
      exitstatus = EXIT_FAILURE;
494
 
      goto fallback;
495
 
    }
496
 
  }
497
 
 
498
 
  if(custom_argv != NULL){
499
 
    custom_argv[0] = argv[0];
500
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
327
      goto end;
 
328
    }
 
329
    plus_argv[0] = argv[0];
 
330
    plus_argv[1] = NULL;
 
331
    arg = strtok(plus_options, delims); /* Get first argument */
 
332
    while(arg != NULL){
 
333
      new_argc++;
 
334
      plus_argv = realloc(plus_argv, sizeof(char *)
 
335
                         * ((unsigned int) new_argc + 1));
 
336
      if(plus_argv == NULL){
 
337
        perror("realloc");
 
338
        exitstatus = EXIT_FAILURE;
 
339
        goto end;
 
340
      }
 
341
      plus_argv[new_argc-1] = arg;
 
342
      plus_argv[new_argc] = NULL;
 
343
      arg = strtok(NULL, delims); /* Get next argument */
 
344
    }
 
345
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
501
346
    if (ret == ARGP_ERR_UNKNOWN){
502
347
      fprintf(stderr, "Unknown error while parsing arguments\n");
503
348
      exitstatus = EXIT_FAILURE;
504
 
      goto fallback;
 
349
      goto end;
505
350
    }
506
351
  }
507
352
  
512
357
      for(char **a = p->argv; *a != NULL; a++){
513
358
        fprintf(stderr, "\tArg: %s\n", *a);
514
359
      }
515
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
516
 
      for(char **a = p->environ; *a != NULL; a++){
517
 
        fprintf(stderr, "\t%s\n", *a);
518
 
      }
519
360
    }
520
361
  }
521
362
  
533
374
  if(dir == NULL){
534
375
    perror("Could not open plugin dir");
535
376
    exitstatus = EXIT_FAILURE;
536
 
    goto fallback;
 
377
    goto end;
537
378
  }
538
379
  
539
380
  /* Set the FD_CLOEXEC flag on the directory, if possible */
544
385
      if(ret < 0){
545
386
        perror("set_cloexec_flag");
546
387
        exitstatus = EXIT_FAILURE;
547
 
        goto fallback;
 
388
        goto end;
548
389
      }
549
390
    }
550
391
  }
559
400
      if (errno == EBADF){
560
401
        perror("readdir");
561
402
        exitstatus = EXIT_FAILURE;
562
 
        goto fallback;
 
403
        goto end;
563
404
      }
564
405
      break;
565
406
    }
610
451
        continue;
611
452
      }
612
453
    }
613
 
 
614
 
    char *filename;
615
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
616
 
    if(ret < 0){
617
 
      perror("asprintf");
618
 
      continue;
 
454
    
 
455
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
 
456
    if (filename == NULL){
 
457
      perror("malloc");
 
458
      exitstatus = EXIT_FAILURE;
 
459
      goto end;
619
460
    }
 
461
    strcpy(filename, plugindir); /* Spurious warning */
 
462
    strcat(filename, "/");      /* Spurious warning */
 
463
    strcat(filename, dirst->d_name); /* Spurious warning */
620
464
    
621
465
    ret = stat(filename, &st);
622
466
    if (ret == -1){
623
467
      perror("stat");
624
 
      free(filename);
625
 
      continue;
 
468
      exitstatus = EXIT_FAILURE;
 
469
      goto end;
626
470
    }
627
471
    
628
472
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
633
477
      free(filename);
634
478
      continue;
635
479
    }
636
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
637
 
    if(p == NULL){
638
 
      perror("getplugin");
639
 
      free(filename);
640
 
      continue;
641
 
    }
642
 
    if(p->disabled){
 
480
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
643
481
      if(debug){
644
482
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
645
483
                dirst->d_name);
647
485
      free(filename);
648
486
      continue;
649
487
    }
 
488
    plugin *p = getplugin(dirst->d_name, &plugin_list);
650
489
    {
651
490
      /* Add global arguments to argument list for this plugin */
652
491
      plugin *g = getplugin(NULL, &plugin_list);
653
 
      if(g != NULL){
654
 
        for(char **a = g->argv + 1; *a != NULL; a++){
655
 
          if(not add_argument(p, *a)){
656
 
            perror("add_argument");
657
 
          }
658
 
        }
659
 
        /* Add global environment variables */
660
 
        for(char **e = g->environ; *e != NULL; e++){
661
 
          if(not add_environment(p, *e)){
662
 
            perror("add_environment");
663
 
          }
664
 
        }
665
 
      }
666
 
    }
667
 
    /* If this plugin has any environment variables, we will call
668
 
       using execve and need to duplicate the environment from this
669
 
       process, too. */
670
 
    if(p->environ[0] != NULL){
671
 
      for(char **e = environ; *e != NULL; e++){
672
 
        char *copy = strdup(*e);
673
 
        if(copy == NULL){
674
 
          perror("strdup");
675
 
          continue;
676
 
        }
677
 
        if(not add_environment(p, copy)){
678
 
          perror("add_environment");
679
 
        }
680
 
      }
681
 
    }
682
 
    
 
492
      for(char **a = g->argv + 1; *a != NULL; a++){
 
493
        addargument(p, *a);
 
494
      }
 
495
    }
683
496
    int pipefd[2]; 
684
497
    ret = pipe(pipefd);
685
498
    if (ret == -1){
686
499
      perror("pipe");
687
500
      exitstatus = EXIT_FAILURE;
688
 
      goto fallback;
 
501
      goto end;
689
502
    }
690
503
    ret = set_cloexec_flag(pipefd[0]);
691
504
    if(ret < 0){
692
505
      perror("set_cloexec_flag");
693
506
      exitstatus = EXIT_FAILURE;
694
 
      goto fallback;
 
507
      goto end;
695
508
    }
696
509
    ret = set_cloexec_flag(pipefd[1]);
697
510
    if(ret < 0){
698
511
      perror("set_cloexec_flag");
699
512
      exitstatus = EXIT_FAILURE;
700
 
      goto fallback;
 
513
      goto end;
701
514
    }
702
515
    /* Block SIGCHLD until process is safely in process list */
703
516
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
704
517
    if(ret < 0){
705
518
      perror("sigprocmask");
706
519
      exitstatus = EXIT_FAILURE;
707
 
      goto fallback;
 
520
      goto end;
708
521
    }
709
522
    // Starting a new process to be watched
710
523
    pid_t pid = fork();
711
 
    if(pid == -1){
712
 
      perror("fork");
713
 
      exitstatus = EXIT_FAILURE;
714
 
      goto fallback;
715
 
    }
716
524
    if(pid == 0){
717
525
      /* this is the child process */
718
526
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
737
545
           above and must now close it manually here. */
738
546
        closedir(dir);
739
547
      }
740
 
      if(p->environ[0] == NULL){
741
 
        if(execv(filename, p->argv) < 0){
742
 
          perror("execv");
743
 
          _exit(EXIT_FAILURE);
744
 
        }
745
 
      } else {
746
 
        if(execve(filename, p->argv, p->environ) < 0){
747
 
          perror("execve");
748
 
          _exit(EXIT_FAILURE);
749
 
        }
 
548
      if(execv(filename, p->argv) < 0){
 
549
        perror("execv");
 
550
        _exit(EXIT_FAILURE);
750
551
      }
751
552
      /* no return */
752
553
    }
761
562
        perror("sigprocmask");
762
563
      }
763
564
      exitstatus = EXIT_FAILURE;
764
 
      goto fallback;
 
565
      goto end;
765
566
    }
766
567
    
767
568
    *new_process = (struct process){ .pid = pid,
775
576
    if(ret < 0){
776
577
      perror("sigprocmask");
777
578
      exitstatus = EXIT_FAILURE;
778
 
      goto fallback;
 
579
      goto end;
779
580
    }
780
581
    
781
582
    FD_SET(new_process->fd, &rfds_all);
786
587
    
787
588
  }
788
589
  
789
 
  free_plugin_list(plugin_list);
 
590
  /* Free the plugin list */
 
591
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
592
    next = plugin_list->next;
 
593
    free(plugin_list->argv);
 
594
    free(plugin_list);
 
595
  }
790
596
  
791
597
  closedir(dir);
792
598
  dir = NULL;
802
608
    if (select_ret == -1){
803
609
      perror("select");
804
610
      exitstatus = EXIT_FAILURE;
805
 
      goto fallback;
 
611
      goto end;
806
612
    }
807
613
    /* OK, now either a process completed, or something can be read
808
614
       from one of them */
834
640
          if(ret < 0){
835
641
            perror("sigprocmask");
836
642
            exitstatus = EXIT_FAILURE;
837
 
            goto fallback;
 
643
            goto end;
838
644
          }
839
645
          /* Delete this process entry from the list */
840
646
          if(process_list == proc){
869
675
          perror("print_out_password");
870
676
          exitstatus = EXIT_FAILURE;
871
677
        }
872
 
        goto fallback;
 
678
        goto end;
873
679
      }
874
680
      /* This process has not completed.  Does it have any output? */
875
681
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
883
689
        if (proc->buffer == NULL){
884
690
          perror("malloc");
885
691
          exitstatus = EXIT_FAILURE;
886
 
          goto fallback;
 
692
          goto end;
887
693
        }
888
694
        proc->buffer_size += BUFFER_SIZE;
889
695
      }
902
708
      }
903
709
    }
904
710
  }
905
 
 
906
 
 
907
 
 fallback:
908
711
  
909
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
910
 
    /* Fallback if all plugins failed, none are found or an error occured */
 
712
  if(process_list == NULL){
911
713
    bool bret;
912
714
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
913
715
    char *passwordbuffer = getpass("Password: ");
915
717
    if(not bret){
916
718
      perror("print_out_password");
917
719
      exitstatus = EXIT_FAILURE;
 
720
      goto end;
918
721
    }
919
722
  }
 
723
 
 
724
 end:
920
725
  
921
726
  /* Restore old signal handler */
922
 
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
923
 
  if(ret == -1){
924
 
    perror("sigaction");
925
 
    exitstatus = EXIT_FAILURE;
926
 
  }
927
 
 
928
 
  if(custom_argv != NULL){
929
 
    for(char **arg = custom_argv; *arg != NULL; arg++){
930
 
      free(*arg);
931
 
    }
932
 
    free(custom_argv);
933
 
  }
934
 
  free_plugin_list(plugin_list);
 
727
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
728
  
 
729
  free(plus_argv);
 
730
  
 
731
  /* Free the plugin list */
 
732
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
733
    next = plugin_list->next;
 
734
    free(plugin_list->argv);
 
735
    free(plugin_list);
 
736
  }
935
737
  
936
738
  if(dir != NULL){
937
739
    closedir(dir);