/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-14 21:03:26 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080814210326-u58d7rj78g3i0ed4
* plugins.d/password-request.c (init_gnutls_global): Renamed
                               "pubkeyfile" to "pubkeyfilename" and
                               "seckeyfile" to "seckeyfilename".
  (combinepath): Changed to use asprintf instead of malloc and memcpy.

  (main): Renamed "pubkeyfile" to "pubkeyname", renamed "seckeyfile"
          to "seckeyname" and created new non-const variables
          "pubkeyfilename" and "seckeyfilename".

* mandos (AvahiService.__init__): Bug fix: initialize
                                  "self.max_renames".
  (AvahiService.rename): Bug fix: use "self.name" instead of "name".

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"
69
68
 
70
69
const char *argp_program_version = "plugin-runner 1.0";
71
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
79
78
  size_t buffer_size;
80
79
  size_t buffer_length;
81
80
  bool eof;
82
 
  volatile bool completed;
83
 
  volatile int status;
 
81
  bool completed;
 
82
  int status;
84
83
  struct process *next;
85
84
} process;
86
85
 
88
87
  char *name;                   /* can be NULL or any plugin name */
89
88
  char **argv;
90
89
  int argc;
91
 
  char **environ;
92
 
  int envc;
93
90
  bool disabled;
94
91
  struct plugin *next;
95
92
} plugin;
104
101
  /* Create a new plugin */
105
102
  plugin *new_plugin = malloc(sizeof(plugin));
106
103
  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
 
  
 
104
    perror("malloc");
 
105
    exit(EXIT_FAILURE);
 
106
  }
 
107
  new_plugin->name = name;
123
108
  new_plugin->argv = malloc(sizeof(char *) * 2);
124
109
  if (new_plugin->argv == NULL){
125
 
    free(copy_name);
126
 
    free(new_plugin);
127
 
    return NULL;
 
110
    perror("malloc");
 
111
    exit(EXIT_FAILURE);
128
112
  }
129
 
  new_plugin->argv[0] = copy_name;
 
113
  new_plugin->argv[0] = name;
130
114
  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;
 
115
  new_plugin->argc = 1;
 
116
  new_plugin->disabled = false;
 
117
  new_plugin->next = *plugin_list;
140
118
  /* Append the new plugin to the list */
141
119
  *plugin_list = new_plugin;
142
120
  return new_plugin;
143
121
}
144
122
 
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
 
 
 
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
}
184
133
 
185
134
/*
186
135
 * Based on the example in the GNU LibC manual chapter 13.13 "File
200
149
 
201
150
process *process_list = NULL;
202
151
 
203
 
/* Mark processes as completed when they exit, and save their exit
 
152
/* Mark a process as completed when it exits, and save its exit
204
153
   status. */
205
154
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
 
  }
 
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;
233
171
}
234
172
 
235
173
bool print_out_password(const char *buffer, size_t length){
247
185
  return true;
248
186
}
249
187
 
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
 
 
265
188
int main(int argc, char *argv[]){
266
189
  const char *plugindir = "/lib/mandos/plugins.d";
267
 
  const char *argfile = ARGFILE;
268
 
  FILE *conffp;
269
190
  size_t d_name_len;
270
191
  DIR *dir = NULL;
271
192
  struct dirent *dirst;
279
200
  struct sigaction old_sigchld_action;
280
201
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
281
202
                                      .sa_flags = SA_NOCLDSTOP };
282
 
  char **custom_argv = NULL;
283
 
  int custom_argc = 0;
284
 
  
 
203
  char *plus_options = NULL;
 
204
  char **plus_argv = NULL;
 
205
 
285
206
  /* Establish a signal handler */
286
207
  sigemptyset(&sigchld_action.sa_mask);
287
208
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
288
 
  if(ret == -1){
 
209
  if(ret < 0){
289
210
    perror("sigaddset");
290
 
    exitstatus = EXIT_FAILURE;
291
 
    goto fallback;
 
211
    exit(EXIT_FAILURE);
292
212
  }
293
213
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
294
 
  if(ret == -1){
 
214
  if(ret < 0){
295
215
    perror("sigaction");
296
 
    exitstatus = EXIT_FAILURE;
297
 
    goto fallback;
 
216
    exit(EXIT_FAILURE);
298
217
  }
299
218
  
300
219
  /* The options we understand. */
302
221
    { .name = "global-options", .key = 'g',
303
222
      .arg = "OPTION[,OPTION[,...]]",
304
223
      .doc = "Options passed to all plugins" },
305
 
    { .name = "global-envs", .key = 'e',
306
 
      .arg = "VAR=value",
307
 
      .doc = "Environment variable passed to all plugins" },
308
224
    { .name = "options-for", .key = 'o',
309
225
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
310
226
      .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" },
314
227
    { .name = "disable", .key = 'd',
315
228
      .arg = "PLUGIN",
316
229
      .doc = "Disable a specific plugin", .group = 1 },
337
250
      if (arg != NULL){
338
251
        char *p;
339
252
        while((p = strsep(&arg, ",")) != NULL){
340
 
          if(p[0] == '\0'){
 
253
          if(strcmp(p, "") == 0){
341
254
            continue;
342
255
          }
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");
 
256
          addargument(getplugin(NULL, plugins), p);
361
257
        }
362
258
      }
363
259
      break;
364
260
    case 'o':
365
261
      if (arg != NULL){
366
 
        char *p_name = strsep(&arg, ":");
367
 
        if(p_name[0] == '\0'){
 
262
        char *name = strsep(&arg, ":");
 
263
        if(strcmp(name, "") == 0){
368
264
          break;
369
265
        }
370
266
        char *opt = strsep(&arg, ":");
371
 
        if(opt[0] == '\0'){
 
267
        if(strcmp(opt, "") == 0){
372
268
          break;
373
269
        }
374
270
        if(opt != NULL){
375
271
          char *p;
376
272
          while((p = strsep(&opt, ",")) != NULL){
377
 
            if(p[0] == '\0'){
 
273
            if(strcmp(p, "") == 0){
378
274
              continue;
379
275
            }
380
 
            if(not add_argument(getplugin(p_name, plugins), p)){
381
 
              perror("add_argument");
382
 
              return ARGP_ERR_UNKNOWN;
383
 
            }
 
276
            addargument(getplugin(name, plugins), p);
384
277
          }
385
278
        }
386
279
      }
387
280
      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");
404
 
        }
405
 
      }
406
 
      break;
407
281
    case 'd':
408
282
      if (arg != NULL){
409
 
        plugin *p = getplugin(arg, plugins);
410
 
        if(p == NULL){
411
 
          return ARGP_ERR_UNKNOWN;
412
 
        }
413
 
        p->disabled = true;
 
283
        getplugin(arg, plugins)->disabled = true;
414
284
      }
415
285
      break;
416
286
    case 128:
426
296
      debug = true;
427
297
      break;
428
298
    case ARGP_KEY_ARG:
429
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
299
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
300
        argp_usage (state);
 
301
      }
 
302
      plus_options = arg;
430
303
      break;
431
304
    case ARGP_KEY_END:
432
305
      break;
446
319
  if (ret == ARGP_ERR_UNKNOWN){
447
320
    fprintf(stderr, "Unknown error while parsing arguments\n");
448
321
    exitstatus = EXIT_FAILURE;
449
 
    goto fallback;
 
322
    goto end;
450
323
  }
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){
 
324
  
 
325
  if(plus_options){
 
326
    /* This is a mangled argument in the form of
 
327
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
328
    /* Make new argc and argv vars, and call argp_parse() again. */
 
329
    plus_options++;             /* skip the first '+' character */
 
330
    const char delims[] = "+";
 
331
    char *arg;
 
332
    int new_argc = 1;
 
333
    plus_argv = malloc(sizeof(char*) * 2);
 
334
    if(plus_argv == NULL){
464
335
      perror("malloc");
465
336
      exitstatus = EXIT_FAILURE;
466
 
      goto fallback;
467
 
    }
468
 
    custom_argv[0] = argv[0];
469
 
    custom_argv[1] = NULL;
 
337
      goto end;
 
338
    }
 
339
    plus_argv[0] = argv[0];
 
340
    plus_argv[1] = NULL;
 
341
 
 
342
    while((arg = strsep(&plus_options, delims)) != NULL){
 
343
      new_argc++;
 
344
      plus_argv = realloc(plus_argv, sizeof(char *)
 
345
                         * ((unsigned int) new_argc + 1));
 
346
      if(plus_argv == NULL){
 
347
        perror("realloc");
 
348
        exitstatus = EXIT_FAILURE;
 
349
        goto end;
 
350
      }
 
351
      plus_argv[new_argc-1] = arg;
 
352
      plus_argv[new_argc] = NULL;
 
353
    }
470
354
    
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);
 
355
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
511
356
    if (ret == ARGP_ERR_UNKNOWN){
512
357
      fprintf(stderr, "Unknown error while parsing arguments\n");
513
358
      exitstatus = EXIT_FAILURE;
514
 
      goto fallback;
 
359
      goto end;
515
360
    }
516
361
  }
517
362
  
522
367
      for(char **a = p->argv; *a != NULL; a++){
523
368
        fprintf(stderr, "\tArg: %s\n", *a);
524
369
      }
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
 
      }
529
370
    }
530
371
  }
531
372
  
543
384
  if(dir == NULL){
544
385
    perror("Could not open plugin dir");
545
386
    exitstatus = EXIT_FAILURE;
546
 
    goto fallback;
 
387
    goto end;
547
388
  }
548
389
  
549
390
  /* Set the FD_CLOEXEC flag on the directory, if possible */
554
395
      if(ret < 0){
555
396
        perror("set_cloexec_flag");
556
397
        exitstatus = EXIT_FAILURE;
557
 
        goto fallback;
 
398
        goto end;
558
399
      }
559
400
    }
560
401
  }
569
410
      if (errno == EBADF){
570
411
        perror("readdir");
571
412
        exitstatus = EXIT_FAILURE;
572
 
        goto fallback;
 
413
        goto end;
573
414
      }
574
415
      break;
575
416
    }
620
461
        continue;
621
462
      }
622
463
    }
623
 
 
624
 
    char *filename;
625
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
626
 
    if(ret < 0){
627
 
      perror("asprintf");
 
464
    
 
465
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
 
466
    if (filename == NULL){
 
467
      perror("malloc");
628
468
      continue;
629
469
    }
 
470
    strcpy(filename, plugindir); /* Spurious warning */
 
471
    strcat(filename, "/");      /* Spurious warning */
 
472
    strcat(filename, dirst->d_name); /* Spurious warning */
630
473
    
631
474
    ret = stat(filename, &st);
632
475
    if (ret == -1){
643
486
      free(filename);
644
487
      continue;
645
488
    }
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){
 
489
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
653
490
      if(debug){
654
491
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
655
492
                dirst->d_name);
657
494
      free(filename);
658
495
      continue;
659
496
    }
 
497
    plugin *p = getplugin(dirst->d_name, &plugin_list);
660
498
    {
661
499
      /* Add global arguments to argument list for this plugin */
662
500
      plugin *g = getplugin(NULL, &plugin_list);
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];
 
501
      for(char **a = g->argv + 1; *a != NULL; a++){
 
502
        addargument(p, *a);
 
503
      }
 
504
    }
 
505
    int pipefd[2]; 
694
506
    ret = pipe(pipefd);
695
507
    if (ret == -1){
696
508
      perror("pipe");
697
509
      exitstatus = EXIT_FAILURE;
698
 
      goto fallback;
 
510
      goto end;
699
511
    }
700
512
    ret = set_cloexec_flag(pipefd[0]);
701
513
    if(ret < 0){
702
514
      perror("set_cloexec_flag");
703
515
      exitstatus = EXIT_FAILURE;
704
 
      goto fallback;
 
516
      goto end;
705
517
    }
706
518
    ret = set_cloexec_flag(pipefd[1]);
707
519
    if(ret < 0){
708
520
      perror("set_cloexec_flag");
709
521
      exitstatus = EXIT_FAILURE;
710
 
      goto fallback;
 
522
      goto end;
711
523
    }
712
524
    /* Block SIGCHLD until process is safely in process list */
713
525
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
714
526
    if(ret < 0){
715
527
      perror("sigprocmask");
716
528
      exitstatus = EXIT_FAILURE;
717
 
      goto fallback;
 
529
      goto end;
718
530
    }
719
531
    // Starting a new process to be watched
720
532
    pid_t pid = fork();
721
533
    if(pid == -1){
722
534
      perror("fork");
723
535
      exitstatus = EXIT_FAILURE;
724
 
      goto fallback;
 
536
      goto end;
725
537
    }
726
538
    if(pid == 0){
727
539
      /* this is the child process */
747
559
           above and must now close it manually here. */
748
560
        closedir(dir);
749
561
      }
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
 
        }
 
562
      if(execv(filename, p->argv) < 0){
 
563
        perror("execv");
 
564
        _exit(EXIT_FAILURE);
760
565
      }
761
566
      /* no return */
762
567
    }
771
576
        perror("sigprocmask");
772
577
      }
773
578
      exitstatus = EXIT_FAILURE;
774
 
      goto fallback;
 
579
      goto end;
775
580
    }
776
581
    
777
582
    *new_process = (struct process){ .pid = pid,
785
590
    if(ret < 0){
786
591
      perror("sigprocmask");
787
592
      exitstatus = EXIT_FAILURE;
788
 
      goto fallback;
 
593
      goto end;
789
594
    }
790
595
    
791
596
    FD_SET(new_process->fd, &rfds_all);
796
601
    
797
602
  }
798
603
  
799
 
  free_plugin_list(plugin_list);
800
 
  plugin_list = NULL;
 
604
  /* Free the plugin list */
 
605
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
606
    next = plugin_list->next;
 
607
    free(plugin_list->argv);
 
608
    free(plugin_list);
 
609
  }
801
610
  
802
611
  closedir(dir);
803
612
  dir = NULL;
813
622
    if (select_ret == -1){
814
623
      perror("select");
815
624
      exitstatus = EXIT_FAILURE;
816
 
      goto fallback;
 
625
      goto end;
817
626
    }
818
627
    /* OK, now either a process completed, or something can be read
819
628
       from one of them */
841
650
          /* Remove the plugin */
842
651
          FD_CLR(proc->fd, &rfds_all);
843
652
          /* Block signal while modifying process_list */
844
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
653
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
845
654
          if(ret < 0){
846
655
            perror("sigprocmask");
847
656
            exitstatus = EXIT_FAILURE;
848
 
            goto fallback;
 
657
            goto end;
849
658
          }
850
659
          /* Delete this process entry from the list */
851
660
          if(process_list == proc){
875
684
        }
876
685
        /* This process exited nicely, so print its buffer */
877
686
 
878
 
        bool bret = print_out_password(proc->buffer,
879
 
                                       proc->buffer_length);
 
687
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
880
688
        if(not bret){
881
689
          perror("print_out_password");
882
690
          exitstatus = EXIT_FAILURE;
883
691
        }
884
 
        goto fallback;
 
692
        goto end;
885
693
      }
886
694
      /* This process has not completed.  Does it have any output? */
887
695
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
895
703
        if (proc->buffer == NULL){
896
704
          perror("malloc");
897
705
          exitstatus = EXIT_FAILURE;
898
 
          goto fallback;
 
706
          goto end;
899
707
        }
900
708
        proc->buffer_size += BUFFER_SIZE;
901
709
      }
916
724
  }
917
725
 
918
726
 
919
 
 fallback:
 
727
 end:
920
728
  
921
729
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
922
 
    /* Fallback if all plugins failed, none are found or an error
923
 
       occured */
 
730
    /* Fallback if all plugins failed, none are found or an error occured */
924
731
    bool bret;
925
732
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
926
733
    char *passwordbuffer = getpass("Password: ");
928
735
    if(not bret){
929
736
      perror("print_out_password");
930
737
      exitstatus = EXIT_FAILURE;
 
738
      goto end;
931
739
    }
932
740
  }
933
741
  
934
742
  /* Restore old signal handler */
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);
 
743
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
744
  
 
745
  free(plus_argv);
 
746
  
 
747
  /* Free the plugin list */
 
748
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
749
    next = plugin_list->next;
 
750
    free(plugin_list->argv);
 
751
    free(plugin_list);
 
752
  }
948
753
  
949
754
  if(dir != NULL){
950
755
    closedir(dir);