/mandos/release

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

« back to all changes in this revision

Viewing changes to mandos-client.c

  • Committer: Björn Påhlsson
  • Date: 2008-08-15 20:17:32 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 77.
  • Revision ID: belorn@braxen-20080815201732-b7uux3yqs05b3fy9
Added configuration files support for mandos-client
Removed plus argument support for mandos-client

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>             /* strsep, 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,
58
59
                                   argp_parse(), ARGP_ERR_UNKNOWN,
59
 
                                   ARGP_KEY_END, ARGP_KEY_ARG,
60
 
                                   error_t */
 
60
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
61
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
62
62
                                   sigaddset(), sigaction(),
63
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
67
#define BUFFER_SIZE 256
68
 
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
68
#define CONFFILE "/conf/conf.d/mandos/client.conf"
69
69
 
70
 
const char *argp_program_version = "plugin-runner 1.0";
 
70
const char *argp_program_version = "mandos-client 1.0";
71
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
72
72
 
73
73
struct process;
79
79
  size_t buffer_size;
80
80
  size_t buffer_length;
81
81
  bool eof;
82
 
  volatile bool completed;
83
 
  volatile int status;
 
82
  bool completed;
 
83
  int status;
84
84
  struct process *next;
85
85
} process;
86
86
 
88
88
  char *name;                   /* can be NULL or any plugin name */
89
89
  char **argv;
90
90
  int argc;
91
 
  char **environ;
92
 
  int envc;
93
91
  bool disabled;
94
92
  struct plugin *next;
95
93
} plugin;
104
102
  /* Create a new plugin */
105
103
  plugin *new_plugin = malloc(sizeof(plugin));
106
104
  if (new_plugin == NULL){
107
 
    return NULL;
108
 
  }
109
 
  char *copy_name = NULL;
110
 
  if(name != NULL){
111
 
    copy_name = strdup(name);
112
 
    if(copy_name == NULL){
113
 
      return NULL;
114
 
    }
115
 
  }
116
 
  
117
 
  *new_plugin = (plugin) { .name = copy_name,
118
 
                           .argc = 1,
119
 
                           .envc = 0,
120
 
                           .disabled = false,
121
 
                           .next = *plugin_list };
122
 
  
 
105
    perror("malloc");
 
106
    exit(EXIT_FAILURE);
 
107
  }
 
108
  new_plugin->name = name;
123
109
  new_plugin->argv = malloc(sizeof(char *) * 2);
124
110
  if (new_plugin->argv == NULL){
125
 
    free(copy_name);
126
 
    free(new_plugin);
127
 
    return NULL;
 
111
    perror("malloc");
 
112
    exit(EXIT_FAILURE);
128
113
  }
129
 
  new_plugin->argv[0] = copy_name;
 
114
  new_plugin->argv[0] = name;
130
115
  new_plugin->argv[1] = NULL;
131
 
 
132
 
  new_plugin->environ = malloc(sizeof(char *));
133
 
  if(new_plugin->environ == NULL){
134
 
    free(copy_name);
135
 
    free(new_plugin->argv);
136
 
    free(new_plugin);
137
 
    return NULL;
138
 
  }
139
 
  new_plugin->environ[0] = NULL;
 
116
  new_plugin->argc = 1;
 
117
  new_plugin->disabled = false;
 
118
  new_plugin->next = *plugin_list;
140
119
  /* Append the new plugin to the list */
141
120
  *plugin_list = new_plugin;
142
121
  return new_plugin;
143
122
}
144
123
 
145
 
/* Helper function for add_argument and add_environment */
146
 
static bool add_to_char_array(const char *new, char ***array,
147
 
                              int *len){
148
 
  /* Resize the pointed-to array to hold one more pointer */
149
 
  *array = realloc(*array, sizeof(char *)
150
 
                   * (size_t) ((*len) + 2));
151
 
  /* Malloc check */
152
 
  if(*array == NULL){
153
 
    return false;
154
 
  }
155
 
  /* Make a copy of the new string */
156
 
  char *copy = strdup(new);
157
 
  if(copy == NULL){
158
 
    return false;
159
 
  }
160
 
  /* Insert the copy */
161
 
  (*array)[*len] = copy;
162
 
  (*len)++;
163
 
  /* Add a new terminating NULL pointer to the last element */
164
 
  (*array)[*len] = NULL;
165
 
  return true;
166
 
}
167
 
 
168
 
/* Add to a plugin's argument vector */
169
 
static bool add_argument(plugin *p, const char *arg){
170
 
  if(p == NULL){
171
 
    return false;
172
 
  }
173
 
  return add_to_char_array(arg, &(p->argv), &(p->argc));
174
 
}
175
 
 
176
 
/* Add to a plugin's environment */
177
 
static bool add_environment(plugin *p, const char *def){
178
 
  if(p == NULL){
179
 
    return false;
180
 
  }
181
 
  return add_to_char_array(def, &(p->environ), &(p->envc));
182
 
}
183
 
 
 
124
static void addargument(plugin *p, char *arg){
 
125
  p->argv[p->argc] = arg;
 
126
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
 
127
  if (p->argv == NULL){
 
128
    perror("malloc");
 
129
    exit(EXIT_FAILURE);
 
130
  }
 
131
  p->argc++;
 
132
  p->argv[p->argc] = NULL;
 
133
}
184
134
 
185
135
/*
186
136
 * Based on the example in the GNU LibC manual chapter 13.13 "File
200
150
 
201
151
process *process_list = NULL;
202
152
 
203
 
/* Mark processes as completed when they exit, and save their exit
 
153
/* Mark a process as completed when it exits, and save its exit
204
154
   status. */
205
155
void handle_sigchld(__attribute__((unused)) int sig){
206
 
  while(true){
207
 
    process *proc = process_list;
208
 
    int status;
209
 
    pid_t pid = waitpid(-1, &status, WNOHANG);
210
 
    if(pid == 0){
211
 
      /* Only still running child processes */
212
 
      break;
213
 
    }
214
 
    if(pid == -1){
215
 
      if (errno != ECHILD){
216
 
        perror("waitpid");
217
 
      }
218
 
      /* No child processes */
219
 
      break;
220
 
    }
221
 
 
222
 
    /* A child exited, find it in process_list */
223
 
    while(proc != NULL and proc->pid != pid){
224
 
      proc = proc->next;
225
 
    }
226
 
    if(proc == NULL){
227
 
      /* Process not found in process list */
228
 
      continue;
229
 
    }
230
 
    proc->status = status;
231
 
    proc->completed = true;
232
 
  }
 
156
  process *proc = process_list;
 
157
  int status;
 
158
  pid_t pid = wait(&status);
 
159
  if(pid == -1){
 
160
    perror("wait");
 
161
    return;
 
162
  }
 
163
  while(proc != NULL and proc->pid != pid){
 
164
    proc = proc->next;
 
165
  }
 
166
  if(proc == NULL){
 
167
    /* Process not found in process list */
 
168
    return;
 
169
  }
 
170
  proc->status = status;
 
171
  proc->completed = true;
233
172
}
234
173
 
235
174
bool print_out_password(const char *buffer, size_t length){
247
186
  return true;
248
187
}
249
188
 
250
 
char **add_to_argv(char **argv, int *argc, char *arg){
 
189
char ** addcustomargument(char **argv, int *argc, char *arg){
 
190
 
251
191
  if (argv == NULL){
252
192
    *argc = 1;
253
193
    argv = malloc(sizeof(char*) * 2);
264
204
    return NULL;
265
205
  }
266
206
  argv[*argc-1] = arg;
267
 
  argv[*argc] = NULL;
 
207
  argv[*argc] = NULL;   
268
208
  return argv;
269
209
}
270
210
 
271
 
static void free_plugin_list(plugin *plugin_list){
272
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
273
 
    next = plugin_list->next;
274
 
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
275
 
      free(*arg);
276
 
    }
277
 
    free(plugin_list->argv);
278
 
    for(char **env = plugin_list->environ; *env != NULL; env++){
279
 
      free(*env);
280
 
    }
281
 
    free(plugin_list->environ);
282
 
    free(plugin_list);
283
 
  }
284
 
}
285
 
 
286
211
int main(int argc, char *argv[]){
287
 
  const char *plugindir = "/lib/mandos/plugins.d";
288
 
  const char *argfile = ARGFILE;
 
212
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
213
  const char *conffile = CONFFILE;
289
214
  FILE *conffp;
290
215
  size_t d_name_len;
291
216
  DIR *dir = NULL;
306
231
  /* Establish a signal handler */
307
232
  sigemptyset(&sigchld_action.sa_mask);
308
233
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
309
 
  if(ret == -1){
 
234
  if(ret < 0){
310
235
    perror("sigaddset");
311
 
    exitstatus = EXIT_FAILURE;
312
 
    goto fallback;
 
236
    exit(EXIT_FAILURE);
313
237
  }
314
238
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
315
 
  if(ret == -1){
 
239
  if(ret < 0){
316
240
    perror("sigaction");
317
 
    exitstatus = EXIT_FAILURE;
318
 
    goto fallback;
 
241
    exit(EXIT_FAILURE);
319
242
  }
320
243
  
321
244
  /* The options we understand. */
323
246
    { .name = "global-options", .key = 'g',
324
247
      .arg = "OPTION[,OPTION[,...]]",
325
248
      .doc = "Options passed to all plugins" },
326
 
    { .name = "global-envs", .key = 'e',
327
 
      .arg = "VAR=value",
328
 
      .doc = "Environment variable passed to all plugins" },
329
249
    { .name = "options-for", .key = 'o',
330
250
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
331
251
      .doc = "Options passed only to specified plugin" },
332
 
    { .name = "envs-for", .key = 'f',
333
 
      .arg = "PLUGIN:ENV=value",
334
 
      .doc = "Environment variable passed to specified plugin" },
335
252
    { .name = "disable", .key = 'd',
336
253
      .arg = "PLUGIN",
337
254
      .doc = "Disable a specific plugin", .group = 1 },
361
278
          if(p[0] == '\0'){
362
279
            continue;
363
280
          }
364
 
          if(not add_argument(getplugin(NULL, plugins), p)){
365
 
            perror("add_argument");
366
 
            return ARGP_ERR_UNKNOWN;
367
 
          }
368
 
        }
369
 
      }
370
 
      break;
371
 
    case 'e':
372
 
      if(arg == NULL){
373
 
        break;
374
 
      }
375
 
      {
376
 
        char *envdef = strdup(arg);
377
 
        if(envdef == NULL){
378
 
          break;
379
 
        }
380
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
381
 
          perror("add_environment");
 
281
          addargument(getplugin(NULL, plugins), p);
382
282
        }
383
283
      }
384
284
      break;
385
285
    case 'o':
386
286
      if (arg != NULL){
387
 
        char *p_name = strsep(&arg, ":");
388
 
        if(p_name[0] == '\0'){
 
287
        char *name = strsep(&arg, ":");
 
288
        if(name[0] == '\0'){
389
289
          break;
390
290
        }
391
291
        char *opt = strsep(&arg, ":");
398
298
            if(p[0] == '\0'){
399
299
              continue;
400
300
            }
401
 
            if(not add_argument(getplugin(p_name, plugins), p)){
402
 
              perror("add_argument");
403
 
              return ARGP_ERR_UNKNOWN;
404
 
            }
 
301
            addargument(getplugin(name, plugins), p);
405
302
          }
406
303
        }
407
304
      }
408
305
      break;
409
 
    case 'f':
410
 
      if(arg == NULL){
411
 
        break;
412
 
      }
413
 
      {
414
 
        char *envdef = strchr(arg, ':');
415
 
        if(envdef == NULL){
416
 
          break;
417
 
        }
418
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
419
 
        if(p_name == NULL){
420
 
          break;
421
 
        }
422
 
        envdef++;
423
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
424
 
          perror("add_environment");
425
 
        }
426
 
      }
427
 
      break;
428
306
    case 'd':
429
307
      if (arg != NULL){
430
 
        plugin *p = getplugin(arg, plugins);
431
 
        if(p == NULL){
432
 
          return ARGP_ERR_UNKNOWN;
433
 
        }
434
 
        p->disabled = true;
 
308
        getplugin(arg, plugins)->disabled = true;
435
309
      }
436
310
      break;
437
311
    case 128:
447
321
      debug = true;
448
322
      break;
449
323
    case ARGP_KEY_ARG:
450
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
324
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
451
325
      break;
452
326
    case ARGP_KEY_END:
453
327
      break;
467
341
  if (ret == ARGP_ERR_UNKNOWN){
468
342
    fprintf(stderr, "Unknown error while parsing arguments\n");
469
343
    exitstatus = EXIT_FAILURE;
470
 
    goto fallback;
 
344
    goto end;
471
345
  }
472
346
 
473
 
  conffp = fopen(argfile, "r");
 
347
  conffp = fopen(conffile, "r");
474
348
  if(conffp != NULL){
475
349
    char *org_line = NULL;
476
 
    char *p, *arg, *new_arg, *line;
477
350
    size_t size = 0;
478
351
    ssize_t sret;
 
352
    char *p, *arg, *new_arg, *line;
479
353
    const char whitespace_delims[] = " \r\t\f\v\n";
480
354
    const char comment_delim[] = "#";
481
355
 
492
366
          continue;
493
367
        }
494
368
        new_arg = strdup(p);
495
 
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
 
369
        custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
496
370
        if (custom_argv == NULL){
497
 
          perror("add_to_argv");
 
371
          perror("addcustomargument");
498
372
          exitstatus = EXIT_FAILURE;
499
 
          goto fallback;
 
373
          goto end;
500
374
        }
501
375
      }
502
376
    }
503
377
    free(org_line);
504
378
  } else{
505
 
    /* Check for harmful errors and go to fallback. Other errors might
506
 
       not affect opening plugins */
 
379
    /* check for harmfull errors */
507
380
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
508
381
      perror("fopen");
509
382
      exitstatus = EXIT_FAILURE;
510
 
      goto fallback;
 
383
      goto end;
511
384
    }
512
385
  }
513
386
 
517
390
    if (ret == ARGP_ERR_UNKNOWN){
518
391
      fprintf(stderr, "Unknown error while parsing arguments\n");
519
392
      exitstatus = EXIT_FAILURE;
520
 
      goto fallback;
 
393
      goto end;
521
394
    }
522
395
  }
523
396
  
528
401
      for(char **a = p->argv; *a != NULL; a++){
529
402
        fprintf(stderr, "\tArg: %s\n", *a);
530
403
      }
531
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
532
 
      for(char **a = p->environ; *a != NULL; a++){
533
 
        fprintf(stderr, "\t%s\n", *a);
534
 
      }
535
404
    }
536
405
  }
537
406
  
549
418
  if(dir == NULL){
550
419
    perror("Could not open plugin dir");
551
420
    exitstatus = EXIT_FAILURE;
552
 
    goto fallback;
 
421
    goto end;
553
422
  }
554
423
  
555
424
  /* Set the FD_CLOEXEC flag on the directory, if possible */
560
429
      if(ret < 0){
561
430
        perror("set_cloexec_flag");
562
431
        exitstatus = EXIT_FAILURE;
563
 
        goto fallback;
 
432
        goto end;
564
433
      }
565
434
    }
566
435
  }
575
444
      if (errno == EBADF){
576
445
        perror("readdir");
577
446
        exitstatus = EXIT_FAILURE;
578
 
        goto fallback;
 
447
        goto end;
579
448
      }
580
449
      break;
581
450
    }
626
495
        continue;
627
496
      }
628
497
    }
629
 
 
630
 
    char *filename;
631
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
632
 
    if(ret < 0){
633
 
      perror("asprintf");
 
498
    
 
499
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
 
500
    if (filename == NULL){
 
501
      perror("malloc");
634
502
      continue;
635
503
    }
 
504
    strcpy(filename, plugindir); /* Spurious warning */
 
505
    strcat(filename, "/");      /* Spurious warning */
 
506
    strcat(filename, dirst->d_name); /* Spurious warning */
636
507
    
637
508
    ret = stat(filename, &st);
638
509
    if (ret == -1){
649
520
      free(filename);
650
521
      continue;
651
522
    }
652
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
653
 
    if(p == NULL){
654
 
      perror("getplugin");
655
 
      free(filename);
656
 
      continue;
657
 
    }
658
 
    if(p->disabled){
 
523
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
659
524
      if(debug){
660
525
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
661
526
                dirst->d_name);
663
528
      free(filename);
664
529
      continue;
665
530
    }
 
531
    plugin *p = getplugin(dirst->d_name, &plugin_list);
666
532
    {
667
533
      /* Add global arguments to argument list for this plugin */
668
534
      plugin *g = getplugin(NULL, &plugin_list);
669
 
      if(g != NULL){
670
 
        for(char **a = g->argv + 1; *a != NULL; a++){
671
 
          if(not add_argument(p, *a)){
672
 
            perror("add_argument");
673
 
          }
674
 
        }
675
 
        /* Add global environment variables */
676
 
        for(char **e = g->environ; *e != NULL; e++){
677
 
          if(not add_environment(p, *e)){
678
 
            perror("add_environment");
679
 
          }
680
 
        }
681
 
      }
682
 
    }
683
 
    /* If this plugin has any environment variables, we will call
684
 
       using execve and need to duplicate the environment from this
685
 
       process, too. */
686
 
    if(p->environ[0] != NULL){
687
 
      for(char **e = environ; *e != NULL; e++){
688
 
        char *copy = strdup(*e);
689
 
        if(copy == NULL){
690
 
          perror("strdup");
691
 
          continue;
692
 
        }
693
 
        if(not add_environment(p, copy)){
694
 
          perror("add_environment");
695
 
        }
696
 
      }
697
 
    }
698
 
    
699
 
    int pipefd[2];
 
535
      for(char **a = g->argv + 1; *a != NULL; a++){
 
536
        addargument(p, *a);
 
537
      }
 
538
    }
 
539
    int pipefd[2]; 
700
540
    ret = pipe(pipefd);
701
541
    if (ret == -1){
702
542
      perror("pipe");
703
543
      exitstatus = EXIT_FAILURE;
704
 
      goto fallback;
 
544
      goto end;
705
545
    }
706
546
    ret = set_cloexec_flag(pipefd[0]);
707
547
    if(ret < 0){
708
548
      perror("set_cloexec_flag");
709
549
      exitstatus = EXIT_FAILURE;
710
 
      goto fallback;
 
550
      goto end;
711
551
    }
712
552
    ret = set_cloexec_flag(pipefd[1]);
713
553
    if(ret < 0){
714
554
      perror("set_cloexec_flag");
715
555
      exitstatus = EXIT_FAILURE;
716
 
      goto fallback;
 
556
      goto end;
717
557
    }
718
558
    /* Block SIGCHLD until process is safely in process list */
719
559
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
720
560
    if(ret < 0){
721
561
      perror("sigprocmask");
722
562
      exitstatus = EXIT_FAILURE;
723
 
      goto fallback;
 
563
      goto end;
724
564
    }
725
565
    // Starting a new process to be watched
726
566
    pid_t pid = fork();
727
567
    if(pid == -1){
728
568
      perror("fork");
729
569
      exitstatus = EXIT_FAILURE;
730
 
      goto fallback;
 
570
      goto end;
731
571
    }
732
572
    if(pid == 0){
733
573
      /* this is the child process */
753
593
           above and must now close it manually here. */
754
594
        closedir(dir);
755
595
      }
756
 
      if(p->environ[0] == NULL){
757
 
        if(execv(filename, p->argv) < 0){
758
 
          perror("execv");
759
 
          _exit(EXIT_FAILURE);
760
 
        }
761
 
      } else {
762
 
        if(execve(filename, p->argv, p->environ) < 0){
763
 
          perror("execve");
764
 
          _exit(EXIT_FAILURE);
765
 
        }
 
596
      if(execv(filename, p->argv) < 0){
 
597
        perror("execv");
 
598
        _exit(EXIT_FAILURE);
766
599
      }
767
600
      /* no return */
768
601
    }
777
610
        perror("sigprocmask");
778
611
      }
779
612
      exitstatus = EXIT_FAILURE;
780
 
      goto fallback;
 
613
      goto end;
781
614
    }
782
615
    
783
616
    *new_process = (struct process){ .pid = pid,
791
624
    if(ret < 0){
792
625
      perror("sigprocmask");
793
626
      exitstatus = EXIT_FAILURE;
794
 
      goto fallback;
 
627
      goto end;
795
628
    }
796
629
    
797
630
    FD_SET(new_process->fd, &rfds_all);
802
635
    
803
636
  }
804
637
  
805
 
  free_plugin_list(plugin_list);
806
 
  plugin_list = NULL;
 
638
  /* Free the plugin list */
 
639
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
640
    next = plugin_list->next;
 
641
    free(plugin_list->argv);
 
642
    free(plugin_list);
 
643
  }
807
644
  
808
645
  closedir(dir);
809
646
  dir = NULL;
819
656
    if (select_ret == -1){
820
657
      perror("select");
821
658
      exitstatus = EXIT_FAILURE;
822
 
      goto fallback;
 
659
      goto end;
823
660
    }
824
661
    /* OK, now either a process completed, or something can be read
825
662
       from one of them */
847
684
          /* Remove the plugin */
848
685
          FD_CLR(proc->fd, &rfds_all);
849
686
          /* Block signal while modifying process_list */
850
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
687
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
851
688
          if(ret < 0){
852
689
            perror("sigprocmask");
853
690
            exitstatus = EXIT_FAILURE;
854
 
            goto fallback;
 
691
            goto end;
855
692
          }
856
693
          /* Delete this process entry from the list */
857
694
          if(process_list == proc){
881
718
        }
882
719
        /* This process exited nicely, so print its buffer */
883
720
 
884
 
        bool bret = print_out_password(proc->buffer,
885
 
                                       proc->buffer_length);
 
721
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
886
722
        if(not bret){
887
723
          perror("print_out_password");
888
724
          exitstatus = EXIT_FAILURE;
889
725
        }
890
 
        goto fallback;
 
726
        goto end;
891
727
      }
892
728
      /* This process has not completed.  Does it have any output? */
893
729
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
901
737
        if (proc->buffer == NULL){
902
738
          perror("malloc");
903
739
          exitstatus = EXIT_FAILURE;
904
 
          goto fallback;
 
740
          goto end;
905
741
        }
906
742
        proc->buffer_size += BUFFER_SIZE;
907
743
      }
922
758
  }
923
759
 
924
760
 
925
 
 fallback:
 
761
 end:
926
762
  
927
763
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
928
 
    /* Fallback if all plugins failed, none are found or an error
929
 
       occured */
 
764
    /* Fallback if all plugins failed, none are found or an error occured */
930
765
    bool bret;
931
766
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
932
767
    char *passwordbuffer = getpass("Password: ");
934
769
    if(not bret){
935
770
      perror("print_out_password");
936
771
      exitstatus = EXIT_FAILURE;
 
772
      goto end;
937
773
    }
938
774
  }
939
775
  
940
776
  /* Restore old signal handler */
941
 
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
942
 
  if(ret == -1){
943
 
    perror("sigaction");
944
 
    exitstatus = EXIT_FAILURE;
945
 
  }
946
 
 
947
 
  if(custom_argv != NULL){
948
 
    for(char **arg = custom_argv; *arg != NULL; arg++){
949
 
      free(*arg);
950
 
    }
951
 
    free(custom_argv);
952
 
  }
953
 
  free_plugin_list(plugin_list);
 
777
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
778
  
 
779
  free(custom_argv);
 
780
  
 
781
  /* Free the plugin list */
 
782
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
783
    next = plugin_list->next;
 
784
    free(plugin_list->argv);
 
785
    free(plugin_list);
 
786
  }
954
787
  
955
788
  if(dir != NULL){
956
789
    closedir(dir);