/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 plugin-runner.c

transformed a function to a part of main

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() */
25
 
 
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
 
25
                                   asprintf() */
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>             /* strtok, strlen(), strcpy(),
55
 
                                   strcat() */
 
54
#include <string.h>             /* strsep, strlen(), asprintf() */
56
55
#include <errno.h>              /* errno */
57
56
#include <argp.h>               /* struct argp_option, struct
58
57
                                   argp_state, struct argp,
59
58
                                   argp_parse(), ARGP_ERR_UNKNOWN,
60
 
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
59
                                   ARGP_KEY_END, ARGP_KEY_ARG,
 
60
                                   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
69
 
69
 
const char *argp_program_version = "mandos-client 1.0";
 
70
const char *argp_program_version = "plugin-runner 1.0";
70
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
72
 
72
73
struct process;
78
79
  size_t buffer_size;
79
80
  size_t buffer_length;
80
81
  bool eof;
81
 
  bool completed;
82
 
  int status;
 
82
  volatile bool completed;
 
83
  volatile int status;
83
84
  struct process *next;
84
85
} process;
85
86
 
87
88
  char *name;                   /* can be NULL or any plugin name */
88
89
  char **argv;
89
90
  int argc;
 
91
  char **environ;
 
92
  int envc;
90
93
  bool disabled;
91
94
  struct plugin *next;
92
95
} plugin;
101
104
  /* Create a new plugin */
102
105
  plugin *new_plugin = malloc(sizeof(plugin));
103
106
  if (new_plugin == NULL){
104
 
    perror("malloc");
105
 
    exit(EXIT_FAILURE);
106
 
  }
107
 
  new_plugin->name = name;
 
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
  
108
123
  new_plugin->argv = malloc(sizeof(char *) * 2);
109
124
  if (new_plugin->argv == NULL){
110
 
    perror("malloc");
111
 
    exit(EXIT_FAILURE);
 
125
    free(copy_name);
 
126
    free(new_plugin);
 
127
    return NULL;
112
128
  }
113
 
  new_plugin->argv[0] = name;
 
129
  new_plugin->argv[0] = copy_name;
114
130
  new_plugin->argv[1] = NULL;
115
 
  new_plugin->argc = 1;
116
 
  new_plugin->disabled = false;
117
 
  new_plugin->next = *plugin_list;
 
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;
118
140
  /* Append the new plugin to the list */
119
141
  *plugin_list = new_plugin;
120
142
  return new_plugin;
121
143
}
122
144
 
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
 
}
 
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
 
133
184
 
134
185
/*
135
186
 * Based on the example in the GNU LibC manual chapter 13.13 "File
149
200
 
150
201
process *process_list = NULL;
151
202
 
152
 
/* Mark a process as completed when it exits, and save its exit
 
203
/* Mark processes as completed when they exit, and save their exit
153
204
   status. */
154
205
void handle_sigchld(__attribute__((unused)) int sig){
155
 
  process *proc = process_list;
156
 
  int status;
157
 
  pid_t pid = wait(&status);
158
 
  if(pid == -1){
159
 
    perror("wait");
160
 
    return;
161
 
  }
162
 
  while(proc != NULL and proc->pid != pid){
163
 
    proc = proc->next;
164
 
  }
165
 
  if(proc == NULL){
166
 
    /* Process not found in process list */
167
 
    return;
168
 
  }
169
 
  proc->status = status;
170
 
  proc->completed = true;
 
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
  }
171
233
}
172
234
 
173
235
bool print_out_password(const char *buffer, size_t length){
185
247
  return true;
186
248
}
187
249
 
 
250
static void free_plugin_list(plugin *plugin_list){
 
251
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
252
    next = plugin_list->next;
 
253
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
 
254
      free(*arg);
 
255
    }
 
256
    free(plugin_list->argv);
 
257
    for(char **env = plugin_list->environ; *env != NULL; env++){
 
258
      free(*env);
 
259
    }
 
260
    free(plugin_list->environ);
 
261
    free(plugin_list);
 
262
  }
 
263
}
 
264
 
188
265
int main(int argc, char *argv[]){
189
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
266
  const char *plugindir = "/lib/mandos/plugins.d";
 
267
  const char *argfile = ARGFILE;
 
268
  FILE *conffp;
190
269
  size_t d_name_len;
191
270
  DIR *dir = NULL;
192
271
  struct dirent *dirst;
200
279
  struct sigaction old_sigchld_action;
201
280
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
202
281
                                      .sa_flags = SA_NOCLDSTOP };
203
 
  char *plus_options = NULL;
204
 
  char **plus_argv = NULL;
205
 
 
 
282
  char **custom_argv = NULL;
 
283
  int custom_argc = 0;
 
284
  
206
285
  /* Establish a signal handler */
207
286
  sigemptyset(&sigchld_action.sa_mask);
208
287
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
209
 
  if(ret < 0){
 
288
  if(ret == -1){
210
289
    perror("sigaddset");
211
 
    exit(EXIT_FAILURE);
 
290
    exitstatus = EXIT_FAILURE;
 
291
    goto fallback;
212
292
  }
213
293
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
214
 
  if(ret < 0){
 
294
  if(ret == -1){
215
295
    perror("sigaction");
216
 
    exit(EXIT_FAILURE);
 
296
    exitstatus = EXIT_FAILURE;
 
297
    goto fallback;
217
298
  }
218
299
  
219
300
  /* The options we understand. */
221
302
    { .name = "global-options", .key = 'g',
222
303
      .arg = "OPTION[,OPTION[,...]]",
223
304
      .doc = "Options passed to all plugins" },
 
305
    { .name = "global-envs", .key = 'e',
 
306
      .arg = "VAR=value",
 
307
      .doc = "Environment variable passed to all plugins" },
224
308
    { .name = "options-for", .key = 'o',
225
309
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
226
310
      .doc = "Options passed only to specified plugin" },
 
311
    { .name = "envs-for", .key = 'f',
 
312
      .arg = "PLUGIN:ENV=value",
 
313
      .doc = "Environment variable passed to specified plugin" },
227
314
    { .name = "disable", .key = 'd',
228
315
      .arg = "PLUGIN",
229
316
      .doc = "Disable a specific plugin", .group = 1 },
248
335
    switch (key) {
249
336
    case 'g':
250
337
      if (arg != NULL){
251
 
        char *p = strtok(arg, ",");
252
 
        do{
253
 
          addargument(getplugin(NULL, plugins), p);
254
 
          p = strtok(NULL, ",");
255
 
        } while (p != NULL);
 
338
        char *p;
 
339
        while((p = strsep(&arg, ",")) != NULL){
 
340
          if(p[0] == '\0'){
 
341
            continue;
 
342
          }
 
343
          if(not add_argument(getplugin(NULL, plugins), p)){
 
344
            perror("add_argument");
 
345
            return ARGP_ERR_UNKNOWN;
 
346
          }
 
347
        }
 
348
      }
 
349
      break;
 
350
    case 'e':
 
351
      if(arg == NULL){
 
352
        break;
 
353
      }
 
354
      {
 
355
        char *envdef = strdup(arg);
 
356
        if(envdef == NULL){
 
357
          break;
 
358
        }
 
359
        if(not add_environment(getplugin(NULL, plugins), envdef)){
 
360
          perror("add_environment");
 
361
        }
256
362
      }
257
363
      break;
258
364
    case 'o':
259
365
      if (arg != NULL){
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);
 
366
        char *p_name = strsep(&arg, ":");
 
367
        if(p_name[0] == '\0'){
 
368
          break;
 
369
        }
 
370
        char *opt = strsep(&arg, ":");
 
371
        if(opt[0] == '\0'){
 
372
          break;
 
373
        }
 
374
        if(opt != NULL){
 
375
          char *p;
 
376
          while((p = strsep(&opt, ",")) != NULL){
 
377
            if(p[0] == '\0'){
 
378
              continue;
 
379
            }
 
380
            if(not add_argument(getplugin(p_name, plugins), p)){
 
381
              perror("add_argument");
 
382
              return ARGP_ERR_UNKNOWN;
 
383
            }
 
384
          }
 
385
        }
 
386
      }
 
387
      break;
 
388
    case 'f':
 
389
      if(arg == NULL){
 
390
        break;
 
391
      }
 
392
      {
 
393
        char *envdef = strchr(arg, ':');
 
394
        if(envdef == NULL){
 
395
          break;
 
396
        }
 
397
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
398
        if(p_name == NULL){
 
399
          break;
 
400
        }
 
401
        envdef++;
 
402
        if(not add_environment(getplugin(p_name, plugins), envdef)){
 
403
          perror("add_environment");
268
404
        }
269
405
      }
270
406
      break;
271
407
    case 'd':
272
408
      if (arg != NULL){
273
 
        getplugin(arg, plugins)->disabled = true;
 
409
        plugin *p = getplugin(arg, plugins);
 
410
        if(p == NULL){
 
411
          return ARGP_ERR_UNKNOWN;
 
412
        }
 
413
        p->disabled = true;
274
414
      }
275
415
      break;
276
416
    case 128:
286
426
      debug = true;
287
427
      break;
288
428
    case ARGP_KEY_ARG:
289
 
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
290
 
        argp_usage (state);
291
 
      }
292
 
      plus_options = arg;
 
429
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
293
430
      break;
294
431
    case ARGP_KEY_END:
295
432
      break;
309
446
  if (ret == ARGP_ERR_UNKNOWN){
310
447
    fprintf(stderr, "Unknown error while parsing arguments\n");
311
448
    exitstatus = EXIT_FAILURE;
312
 
    goto end;
 
449
    goto fallback;
313
450
  }
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){
 
451
 
 
452
  conffp = fopen(argfile, "r");
 
453
  if(conffp != NULL){
 
454
    char *org_line = NULL;
 
455
    char *p, *arg, *new_arg, *line;
 
456
    size_t size = 0;
 
457
    ssize_t sret;
 
458
    const char whitespace_delims[] = " \r\t\f\v\n";
 
459
    const char comment_delim[] = "#";
 
460
 
 
461
    custom_argc = 1;
 
462
    custom_argv = malloc(sizeof(char*) * 2);
 
463
    if(custom_argv == NULL){
325
464
      perror("malloc");
326
465
      exitstatus = EXIT_FAILURE;
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);
 
466
      goto fallback;
 
467
    }
 
468
    custom_argv[0] = argv[0];
 
469
    custom_argv[1] = NULL;
 
470
    
 
471
    while(true){
 
472
      sret = getline(&org_line, &size, conffp);
 
473
      if(sret == -1){
 
474
        break;
 
475
      }
 
476
 
 
477
      line = org_line;
 
478
      arg = strsep(&line, comment_delim);
 
479
      while((p = strsep(&arg, whitespace_delims)) != NULL){
 
480
        if(p[0] == '\0'){
 
481
          continue;
 
482
        }
 
483
        new_arg = strdup(p);
 
484
 
 
485
        custom_argc += 1;
 
486
        custom_argv = realloc(custom_argv, sizeof(char *)
 
487
                              * ((unsigned int) custom_argc + 1));
 
488
        if(custom_argv == NULL){
 
489
          perror("realloc");
 
490
          exitstatus = EXIT_FAILURE;
 
491
          free(org_line);
 
492
          goto fallback;
 
493
        }
 
494
        custom_argv[custom_argc-1] = new_arg;
 
495
        custom_argv[custom_argc] = NULL;        
 
496
      }
 
497
    }
 
498
    free(org_line);
 
499
  } else{
 
500
    /* Check for harmful errors and go to fallback. Other errors might
 
501
       not affect opening plugins */
 
502
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
503
      perror("fopen");
 
504
      exitstatus = EXIT_FAILURE;
 
505
      goto fallback;
 
506
    }
 
507
  }
 
508
 
 
509
  if(custom_argv != NULL){
 
510
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
346
511
    if (ret == ARGP_ERR_UNKNOWN){
347
512
      fprintf(stderr, "Unknown error while parsing arguments\n");
348
513
      exitstatus = EXIT_FAILURE;
349
 
      goto end;
 
514
      goto fallback;
350
515
    }
351
516
  }
352
517
  
357
522
      for(char **a = p->argv; *a != NULL; a++){
358
523
        fprintf(stderr, "\tArg: %s\n", *a);
359
524
      }
 
525
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
526
      for(char **a = p->environ; *a != NULL; a++){
 
527
        fprintf(stderr, "\t%s\n", *a);
 
528
      }
360
529
    }
361
530
  }
362
531
  
374
543
  if(dir == NULL){
375
544
    perror("Could not open plugin dir");
376
545
    exitstatus = EXIT_FAILURE;
377
 
    goto end;
 
546
    goto fallback;
378
547
  }
379
548
  
380
549
  /* Set the FD_CLOEXEC flag on the directory, if possible */
385
554
      if(ret < 0){
386
555
        perror("set_cloexec_flag");
387
556
        exitstatus = EXIT_FAILURE;
388
 
        goto end;
 
557
        goto fallback;
389
558
      }
390
559
    }
391
560
  }
400
569
      if (errno == EBADF){
401
570
        perror("readdir");
402
571
        exitstatus = EXIT_FAILURE;
403
 
        goto end;
 
572
        goto fallback;
404
573
      }
405
574
      break;
406
575
    }
451
620
        continue;
452
621
      }
453
622
    }
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;
 
623
 
 
624
    char *filename;
 
625
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
626
    if(ret < 0){
 
627
      perror("asprintf");
 
628
      continue;
460
629
    }
461
 
    strcpy(filename, plugindir); /* Spurious warning */
462
 
    strcat(filename, "/");      /* Spurious warning */
463
 
    strcat(filename, dirst->d_name); /* Spurious warning */
464
630
    
465
631
    ret = stat(filename, &st);
466
632
    if (ret == -1){
467
633
      perror("stat");
468
 
      exitstatus = EXIT_FAILURE;
469
 
      goto end;
 
634
      free(filename);
 
635
      continue;
470
636
    }
471
637
    
472
638
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
477
643
      free(filename);
478
644
      continue;
479
645
    }
480
 
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
 
646
    plugin *p = getplugin(dirst->d_name, &plugin_list);
 
647
    if(p == NULL){
 
648
      perror("getplugin");
 
649
      free(filename);
 
650
      continue;
 
651
    }
 
652
    if(p->disabled){
481
653
      if(debug){
482
654
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
483
655
                dirst->d_name);
485
657
      free(filename);
486
658
      continue;
487
659
    }
488
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
489
660
    {
490
661
      /* Add global arguments to argument list for this plugin */
491
662
      plugin *g = getplugin(NULL, &plugin_list);
492
 
      for(char **a = g->argv + 1; *a != NULL; a++){
493
 
        addargument(p, *a);
494
 
      }
495
 
    }
496
 
    int pipefd[2]; 
 
663
      if(g != NULL){
 
664
        for(char **a = g->argv + 1; *a != NULL; a++){
 
665
          if(not add_argument(p, *a)){
 
666
            perror("add_argument");
 
667
          }
 
668
        }
 
669
        /* Add global environment variables */
 
670
        for(char **e = g->environ; *e != NULL; e++){
 
671
          if(not add_environment(p, *e)){
 
672
            perror("add_environment");
 
673
          }
 
674
        }
 
675
      }
 
676
    }
 
677
    /* If this plugin has any environment variables, we will call
 
678
       using execve and need to duplicate the environment from this
 
679
       process, too. */
 
680
    if(p->environ[0] != NULL){
 
681
      for(char **e = environ; *e != NULL; e++){
 
682
        char *copy = strdup(*e);
 
683
        if(copy == NULL){
 
684
          perror("strdup");
 
685
          continue;
 
686
        }
 
687
        if(not add_environment(p, copy)){
 
688
          perror("add_environment");
 
689
        }
 
690
      }
 
691
    }
 
692
    
 
693
    int pipefd[2];
497
694
    ret = pipe(pipefd);
498
695
    if (ret == -1){
499
696
      perror("pipe");
500
697
      exitstatus = EXIT_FAILURE;
501
 
      goto end;
 
698
      goto fallback;
502
699
    }
503
700
    ret = set_cloexec_flag(pipefd[0]);
504
701
    if(ret < 0){
505
702
      perror("set_cloexec_flag");
506
703
      exitstatus = EXIT_FAILURE;
507
 
      goto end;
 
704
      goto fallback;
508
705
    }
509
706
    ret = set_cloexec_flag(pipefd[1]);
510
707
    if(ret < 0){
511
708
      perror("set_cloexec_flag");
512
709
      exitstatus = EXIT_FAILURE;
513
 
      goto end;
 
710
      goto fallback;
514
711
    }
515
712
    /* Block SIGCHLD until process is safely in process list */
516
713
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
517
714
    if(ret < 0){
518
715
      perror("sigprocmask");
519
716
      exitstatus = EXIT_FAILURE;
520
 
      goto end;
 
717
      goto fallback;
521
718
    }
522
719
    // Starting a new process to be watched
523
720
    pid_t pid = fork();
 
721
    if(pid == -1){
 
722
      perror("fork");
 
723
      exitstatus = EXIT_FAILURE;
 
724
      goto fallback;
 
725
    }
524
726
    if(pid == 0){
525
727
      /* this is the child process */
526
728
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
545
747
           above and must now close it manually here. */
546
748
        closedir(dir);
547
749
      }
548
 
      if(execv(filename, p->argv) < 0){
549
 
        perror("execv");
550
 
        _exit(EXIT_FAILURE);
 
750
      if(p->environ[0] == NULL){
 
751
        if(execv(filename, p->argv) < 0){
 
752
          perror("execv");
 
753
          _exit(EXIT_FAILURE);
 
754
        }
 
755
      } else {
 
756
        if(execve(filename, p->argv, p->environ) < 0){
 
757
          perror("execve");
 
758
          _exit(EXIT_FAILURE);
 
759
        }
551
760
      }
552
761
      /* no return */
553
762
    }
562
771
        perror("sigprocmask");
563
772
      }
564
773
      exitstatus = EXIT_FAILURE;
565
 
      goto end;
 
774
      goto fallback;
566
775
    }
567
776
    
568
777
    *new_process = (struct process){ .pid = pid,
576
785
    if(ret < 0){
577
786
      perror("sigprocmask");
578
787
      exitstatus = EXIT_FAILURE;
579
 
      goto end;
 
788
      goto fallback;
580
789
    }
581
790
    
582
791
    FD_SET(new_process->fd, &rfds_all);
587
796
    
588
797
  }
589
798
  
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
 
  }
 
799
  free_plugin_list(plugin_list);
 
800
  plugin_list = NULL;
596
801
  
597
802
  closedir(dir);
598
803
  dir = NULL;
608
813
    if (select_ret == -1){
609
814
      perror("select");
610
815
      exitstatus = EXIT_FAILURE;
611
 
      goto end;
 
816
      goto fallback;
612
817
    }
613
818
    /* OK, now either a process completed, or something can be read
614
819
       from one of them */
636
841
          /* Remove the plugin */
637
842
          FD_CLR(proc->fd, &rfds_all);
638
843
          /* Block signal while modifying process_list */
639
 
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
844
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
640
845
          if(ret < 0){
641
846
            perror("sigprocmask");
642
847
            exitstatus = EXIT_FAILURE;
643
 
            goto end;
 
848
            goto fallback;
644
849
          }
645
850
          /* Delete this process entry from the list */
646
851
          if(process_list == proc){
670
875
        }
671
876
        /* This process exited nicely, so print its buffer */
672
877
 
673
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
878
        bool bret = print_out_password(proc->buffer,
 
879
                                       proc->buffer_length);
674
880
        if(not bret){
675
881
          perror("print_out_password");
676
882
          exitstatus = EXIT_FAILURE;
677
883
        }
678
 
        goto end;
 
884
        goto fallback;
679
885
      }
680
886
      /* This process has not completed.  Does it have any output? */
681
887
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
689
895
        if (proc->buffer == NULL){
690
896
          perror("malloc");
691
897
          exitstatus = EXIT_FAILURE;
692
 
          goto end;
 
898
          goto fallback;
693
899
        }
694
900
        proc->buffer_size += BUFFER_SIZE;
695
901
      }
710
916
  }
711
917
 
712
918
 
713
 
 end:
 
919
 fallback:
714
920
  
715
921
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
716
 
    /* Fallback if all plugins failed or an error occured */
 
922
    /* Fallback if all plugins failed, none are found or an error
 
923
       occured */
717
924
    bool bret;
718
925
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
719
926
    char *passwordbuffer = getpass("Password: ");
721
928
    if(not bret){
722
929
      perror("print_out_password");
723
930
      exitstatus = EXIT_FAILURE;
724
 
      goto end;
725
931
    }
726
932
  }
727
933
  
728
934
  /* Restore old signal handler */
729
 
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
730
 
  
731
 
  free(plus_argv);
732
 
  
733
 
  /* Free the plugin list */
734
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
735
 
    next = plugin_list->next;
736
 
    free(plugin_list->argv);
737
 
    free(plugin_list);
738
 
  }
 
935
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
936
  if(ret == -1){
 
937
    perror("sigaction");
 
938
    exitstatus = EXIT_FAILURE;
 
939
  }
 
940
 
 
941
  if(custom_argv != NULL){
 
942
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
 
943
      free(*arg);
 
944
    }
 
945
    free(custom_argv);
 
946
  }
 
947
  free_plugin_list(plugin_list);
739
948
  
740
949
  if(dir != NULL){
741
950
    closedir(dir);