/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

Fixed fallback on error in mandos-client
fixed a bug in password-request

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,
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
 
const char *argp_program_version = "plugin-runner 1.0";
 
69
const char *argp_program_version = "mandos-client 1.0";
71
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
72
71
 
73
72
struct process;
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
 
char **add_to_argv(char **argv, int *argc, char *arg){
251
 
  if (argv == NULL){
252
 
    *argc = 1;
253
 
    argv = malloc(sizeof(char*) * 2);
254
 
    if(argv == NULL){
255
 
      return NULL;
256
 
    }
257
 
    argv[0] = NULL;     /* Will be set to argv[0] in main before
258
 
                           parsing */
259
 
    argv[1] = NULL;
260
 
  }
261
 
  *argc += 1;
262
 
  argv = realloc(argv, sizeof(char *)
263
 
                  * ((unsigned int) *argc + 1));
264
 
  if(argv == NULL){
265
 
    return NULL;
266
 
  }
267
 
  argv[*argc-1] = arg;
268
 
  argv[*argc] = NULL;
269
 
  return argv;
270
 
}
271
 
 
272
 
static void free_plugin_list(plugin *plugin_list){
273
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
274
 
    next = plugin_list->next;
275
 
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
276
 
      free(*arg);
277
 
    }
278
 
    free(plugin_list->argv);
279
 
    for(char **env = plugin_list->environ; *env != NULL; env++){
280
 
      free(*env);
281
 
    }
282
 
    free(plugin_list->environ);
283
 
    free(plugin_list);
284
 
  }
285
 
}
286
 
 
287
188
int main(int argc, char *argv[]){
288
 
  const char *plugindir = "/lib/mandos/plugins.d";
289
 
  const char *argfile = ARGFILE;
290
 
  FILE *conffp;
 
189
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
291
190
  size_t d_name_len;
292
191
  DIR *dir = NULL;
293
192
  struct dirent *dirst;
301
200
  struct sigaction old_sigchld_action;
302
201
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
303
202
                                      .sa_flags = SA_NOCLDSTOP };
304
 
  char **custom_argv = NULL;
305
 
  int custom_argc = 0;
 
203
  char *plus_options = NULL;
 
204
  char **plus_argv = NULL;
 
205
 
 
206
  errno = 0;
306
207
  
307
208
  /* Establish a signal handler */
308
209
  sigemptyset(&sigchld_action.sa_mask);
309
210
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
310
 
  if(ret == -1){
 
211
  if(ret < 0){
311
212
    perror("sigaddset");
312
 
    exitstatus = EXIT_FAILURE;
313
 
    goto fallback;
 
213
    exit(EXIT_FAILURE);
314
214
  }
315
215
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
316
 
  if(ret == -1){
 
216
  if(ret < 0){
317
217
    perror("sigaction");
318
 
    exitstatus = EXIT_FAILURE;
319
 
    goto fallback;
 
218
    exit(EXIT_FAILURE);
320
219
  }
321
220
  
322
221
  /* The options we understand. */
324
223
    { .name = "global-options", .key = 'g',
325
224
      .arg = "OPTION[,OPTION[,...]]",
326
225
      .doc = "Options passed to all plugins" },
327
 
    { .name = "global-envs", .key = 'e',
328
 
      .arg = "VAR=value",
329
 
      .doc = "Environment variable passed to all plugins" },
330
226
    { .name = "options-for", .key = 'o',
331
227
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
332
228
      .doc = "Options passed only to specified plugin" },
333
 
    { .name = "envs-for", .key = 'f',
334
 
      .arg = "PLUGIN:ENV=value",
335
 
      .doc = "Environment variable passed to specified plugin" },
336
229
    { .name = "disable", .key = 'd',
337
230
      .arg = "PLUGIN",
338
231
      .doc = "Disable a specific plugin", .group = 1 },
357
250
    switch (key) {
358
251
    case 'g':
359
252
      if (arg != NULL){
360
 
        char *p;
361
 
        while((p = strsep(&arg, ",")) != NULL){
362
 
          if(p[0] == '\0'){
363
 
            continue;
364
 
          }
365
 
          if(not add_argument(getplugin(NULL, plugins), p)){
366
 
            perror("add_argument");
367
 
            return ARGP_ERR_UNKNOWN;
368
 
          }
369
 
        }
370
 
      }
371
 
      break;
372
 
    case 'e':
373
 
      if(arg == NULL){
374
 
        break;
375
 
      }
376
 
      {
377
 
        char *envdef = strdup(arg);
378
 
        if(envdef == NULL){
379
 
          break;
380
 
        }
381
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
382
 
          perror("add_environment");
383
 
        }
 
253
        char *p = strtok(arg, ",");
 
254
        do{
 
255
          addargument(getplugin(NULL, plugins), p);
 
256
          p = strtok(NULL, ",");
 
257
        } while (p != NULL);
384
258
      }
385
259
      break;
386
260
    case 'o':
387
261
      if (arg != NULL){
388
 
        char *p_name = strsep(&arg, ":");
389
 
        if(p_name[0] == '\0'){
390
 
          break;
391
 
        }
392
 
        char *opt = strsep(&arg, ":");
393
 
        if(opt[0] == '\0'){
394
 
          break;
395
 
        }
396
 
        if(opt != NULL){
397
 
          char *p;
398
 
          while((p = strsep(&opt, ",")) != NULL){
399
 
            if(p[0] == '\0'){
400
 
              continue;
401
 
            }
402
 
            if(not add_argument(getplugin(p_name, plugins), p)){
403
 
              perror("add_argument");
404
 
              return ARGP_ERR_UNKNOWN;
405
 
            }
406
 
          }
407
 
        }
408
 
      }
409
 
      break;
410
 
    case 'f':
411
 
      if(arg == NULL){
412
 
        break;
413
 
      }
414
 
      {
415
 
        char *envdef = strchr(arg, ':');
416
 
        if(envdef == NULL){
417
 
          break;
418
 
        }
419
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
420
 
        if(p_name == NULL){
421
 
          break;
422
 
        }
423
 
        envdef++;
424
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
425
 
          perror("add_environment");
 
262
        char *name = strtok(arg, ":");
 
263
        char *p = strtok(NULL, ":");
 
264
        if(p != NULL){
 
265
          p = strtok(p, ",");
 
266
          do{
 
267
            addargument(getplugin(name, plugins), p);
 
268
            p = strtok(NULL, ",");
 
269
          } while (p != NULL);
426
270
        }
427
271
      }
428
272
      break;
429
273
    case 'd':
430
274
      if (arg != NULL){
431
 
        plugin *p = getplugin(arg, plugins);
432
 
        if(p == NULL){
433
 
          return ARGP_ERR_UNKNOWN;
434
 
        }
435
 
        p->disabled = true;
 
275
        getplugin(arg, plugins)->disabled = true;
436
276
      }
437
277
      break;
438
278
    case 128:
448
288
      debug = true;
449
289
      break;
450
290
    case ARGP_KEY_ARG:
451
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
291
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
292
        argp_usage (state);
 
293
      }
 
294
      plus_options = arg;
452
295
      break;
453
296
    case ARGP_KEY_END:
454
297
      break;
468
311
  if (ret == ARGP_ERR_UNKNOWN){
469
312
    fprintf(stderr, "Unknown error while parsing arguments\n");
470
313
    exitstatus = EXIT_FAILURE;
471
 
    goto fallback;
 
314
    goto end;
472
315
  }
473
 
 
474
 
  conffp = fopen(argfile, "r");
475
 
  if(conffp != NULL){
476
 
    char *org_line = NULL;
477
 
    char *p, *arg, *new_arg, *line;
478
 
    size_t size = 0;
479
 
    ssize_t sret;
480
 
    const char whitespace_delims[] = " \r\t\f\v\n";
481
 
    const char comment_delim[] = "#";
482
 
 
483
 
    while(true){
484
 
      sret = getline(&org_line, &size, conffp);
485
 
      if(sret == -1){
486
 
        break;
487
 
      }
488
 
 
489
 
      line = org_line;
490
 
      arg = strsep(&line, comment_delim);
491
 
      while((p = strsep(&arg, whitespace_delims)) != NULL){
492
 
        if(p[0] == '\0'){
493
 
          continue;
494
 
        }
495
 
        new_arg = strdup(p);
496
 
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
497
 
        if (custom_argv == NULL){
498
 
          perror("add_to_argv");
499
 
          exitstatus = EXIT_FAILURE;
500
 
          goto fallback;
501
 
        }
502
 
      }
503
 
    }
504
 
    free(org_line);
505
 
  } else{
506
 
    /* Check for harmful errors and go to fallback. Other errors might
507
 
       not affect opening plugins */
508
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
509
 
      perror("fopen");
 
316
  
 
317
  if(plus_options){
 
318
    /* This is a mangled argument in the form of
 
319
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
320
    /* Make new argc and argv vars, and call argp_parse() again. */
 
321
    plus_options++;             /* skip the first '+' character */
 
322
    const char delims[] = "+";
 
323
    char *arg;
 
324
    int new_argc = 1;
 
325
    plus_argv = malloc(sizeof(char*) * 2);
 
326
    if(plus_argv == NULL){
 
327
      perror("malloc");
510
328
      exitstatus = EXIT_FAILURE;
511
 
      goto fallback;
512
 
    }
513
 
  }
514
 
 
515
 
  if(custom_argv != NULL){
516
 
    custom_argv[0] = argv[0];
517
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0,
518
 
                      &plugin_list);
 
329
      goto end;
 
330
    }
 
331
    plus_argv[0] = argv[0];
 
332
    plus_argv[1] = NULL;
 
333
    arg = strtok(plus_options, delims); /* Get first argument */
 
334
    while(arg != NULL){
 
335
      new_argc++;
 
336
      plus_argv = realloc(plus_argv, sizeof(char *)
 
337
                         * ((unsigned int) new_argc + 1));
 
338
      if(plus_argv == NULL){
 
339
        perror("realloc");
 
340
        exitstatus = EXIT_FAILURE;
 
341
        goto end;
 
342
      }
 
343
      plus_argv[new_argc-1] = arg;
 
344
      plus_argv[new_argc] = NULL;
 
345
      arg = strtok(NULL, delims); /* Get next argument */
 
346
    }
 
347
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
519
348
    if (ret == ARGP_ERR_UNKNOWN){
520
349
      fprintf(stderr, "Unknown error while parsing arguments\n");
521
350
      exitstatus = EXIT_FAILURE;
522
 
      goto fallback;
 
351
      goto end;
523
352
    }
524
353
  }
525
354
  
530
359
      for(char **a = p->argv; *a != NULL; a++){
531
360
        fprintf(stderr, "\tArg: %s\n", *a);
532
361
      }
533
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
534
 
      for(char **a = p->environ; *a != NULL; a++){
535
 
        fprintf(stderr, "\t%s\n", *a);
536
 
      }
537
362
    }
538
363
  }
539
364
  
551
376
  if(dir == NULL){
552
377
    perror("Could not open plugin dir");
553
378
    exitstatus = EXIT_FAILURE;
554
 
    goto fallback;
 
379
    goto end;
555
380
  }
556
381
  
557
382
  /* Set the FD_CLOEXEC flag on the directory, if possible */
562
387
      if(ret < 0){
563
388
        perror("set_cloexec_flag");
564
389
        exitstatus = EXIT_FAILURE;
565
 
        goto fallback;
 
390
        goto end;
566
391
      }
567
392
    }
568
393
  }
577
402
      if (errno == EBADF){
578
403
        perror("readdir");
579
404
        exitstatus = EXIT_FAILURE;
580
 
        goto fallback;
 
405
        goto end;
581
406
      }
582
407
      break;
583
408
    }
628
453
        continue;
629
454
      }
630
455
    }
631
 
 
632
 
    char *filename;
633
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
634
 
    if(ret < 0){
635
 
      perror("asprintf");
636
 
      continue;
 
456
    
 
457
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
 
458
    if (filename == NULL){
 
459
      perror("malloc");
 
460
      exitstatus = EXIT_FAILURE;
 
461
      goto end;
637
462
    }
 
463
    strcpy(filename, plugindir); /* Spurious warning */
 
464
    strcat(filename, "/");      /* Spurious warning */
 
465
    strcat(filename, dirst->d_name); /* Spurious warning */
638
466
    
639
467
    ret = stat(filename, &st);
640
468
    if (ret == -1){
641
469
      perror("stat");
642
 
      free(filename);
643
 
      continue;
 
470
      exitstatus = EXIT_FAILURE;
 
471
      goto end;
644
472
    }
645
473
    
646
474
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
651
479
      free(filename);
652
480
      continue;
653
481
    }
654
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
655
 
    if(p == NULL){
656
 
      perror("getplugin");
657
 
      free(filename);
658
 
      continue;
659
 
    }
660
 
    if(p->disabled){
 
482
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
661
483
      if(debug){
662
484
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
663
485
                dirst->d_name);
665
487
      free(filename);
666
488
      continue;
667
489
    }
 
490
    plugin *p = getplugin(dirst->d_name, &plugin_list);
668
491
    {
669
492
      /* Add global arguments to argument list for this plugin */
670
493
      plugin *g = getplugin(NULL, &plugin_list);
671
 
      if(g != NULL){
672
 
        for(char **a = g->argv + 1; *a != NULL; a++){
673
 
          if(not add_argument(p, *a)){
674
 
            perror("add_argument");
675
 
          }
676
 
        }
677
 
        /* Add global environment variables */
678
 
        for(char **e = g->environ; *e != NULL; e++){
679
 
          if(not add_environment(p, *e)){
680
 
            perror("add_environment");
681
 
          }
682
 
        }
683
 
      }
684
 
    }
685
 
    /* If this plugin has any environment variables, we will call
686
 
       using execve and need to duplicate the environment from this
687
 
       process, too. */
688
 
    if(p->environ[0] != NULL){
689
 
      for(char **e = environ; *e != NULL; e++){
690
 
        char *copy = strdup(*e);
691
 
        if(copy == NULL){
692
 
          perror("strdup");
693
 
          continue;
694
 
        }
695
 
        if(not add_environment(p, copy)){
696
 
          perror("add_environment");
697
 
        }
698
 
      }
699
 
    }
700
 
    
701
 
    int pipefd[2];
 
494
      for(char **a = g->argv + 1; *a != NULL; a++){
 
495
        addargument(p, *a);
 
496
      }
 
497
    }
 
498
    int pipefd[2]; 
702
499
    ret = pipe(pipefd);
703
500
    if (ret == -1){
704
501
      perror("pipe");
705
502
      exitstatus = EXIT_FAILURE;
706
 
      goto fallback;
 
503
      goto end;
707
504
    }
708
505
    ret = set_cloexec_flag(pipefd[0]);
709
506
    if(ret < 0){
710
507
      perror("set_cloexec_flag");
711
508
      exitstatus = EXIT_FAILURE;
712
 
      goto fallback;
 
509
      goto end;
713
510
    }
714
511
    ret = set_cloexec_flag(pipefd[1]);
715
512
    if(ret < 0){
716
513
      perror("set_cloexec_flag");
717
514
      exitstatus = EXIT_FAILURE;
718
 
      goto fallback;
 
515
      goto end;
719
516
    }
720
517
    /* Block SIGCHLD until process is safely in process list */
721
518
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
722
519
    if(ret < 0){
723
520
      perror("sigprocmask");
724
521
      exitstatus = EXIT_FAILURE;
725
 
      goto fallback;
 
522
      goto end;
726
523
    }
727
524
    // Starting a new process to be watched
728
525
    pid_t pid = fork();
729
 
    if(pid == -1){
730
 
      perror("fork");
731
 
      exitstatus = EXIT_FAILURE;
732
 
      goto fallback;
733
 
    }
734
526
    if(pid == 0){
735
527
      /* this is the child process */
736
528
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
755
547
           above and must now close it manually here. */
756
548
        closedir(dir);
757
549
      }
758
 
      if(p->environ[0] == NULL){
759
 
        if(execv(filename, p->argv) < 0){
760
 
          perror("execv");
761
 
          _exit(EXIT_FAILURE);
762
 
        }
763
 
      } else {
764
 
        if(execve(filename, p->argv, p->environ) < 0){
765
 
          perror("execve");
766
 
          _exit(EXIT_FAILURE);
767
 
        }
 
550
      if(execv(filename, p->argv) < 0){
 
551
        perror("execv");
 
552
        _exit(EXIT_FAILURE);
768
553
      }
769
554
      /* no return */
770
555
    }
779
564
        perror("sigprocmask");
780
565
      }
781
566
      exitstatus = EXIT_FAILURE;
782
 
      goto fallback;
 
567
      goto end;
783
568
    }
784
569
    
785
570
    *new_process = (struct process){ .pid = pid,
793
578
    if(ret < 0){
794
579
      perror("sigprocmask");
795
580
      exitstatus = EXIT_FAILURE;
796
 
      goto fallback;
 
581
      goto end;
797
582
    }
798
583
    
799
584
    FD_SET(new_process->fd, &rfds_all);
804
589
    
805
590
  }
806
591
  
807
 
  free_plugin_list(plugin_list);
808
 
  plugin_list = NULL;
 
592
  /* Free the plugin list */
 
593
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
594
    next = plugin_list->next;
 
595
    free(plugin_list->argv);
 
596
    free(plugin_list);
 
597
  }
809
598
  
810
599
  closedir(dir);
811
600
  dir = NULL;
821
610
    if (select_ret == -1){
822
611
      perror("select");
823
612
      exitstatus = EXIT_FAILURE;
824
 
      goto fallback;
 
613
      goto end;
825
614
    }
826
615
    /* OK, now either a process completed, or something can be read
827
616
       from one of them */
849
638
          /* Remove the plugin */
850
639
          FD_CLR(proc->fd, &rfds_all);
851
640
          /* Block signal while modifying process_list */
852
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
641
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
853
642
          if(ret < 0){
854
643
            perror("sigprocmask");
855
644
            exitstatus = EXIT_FAILURE;
856
 
            goto fallback;
 
645
            goto end;
857
646
          }
858
647
          /* Delete this process entry from the list */
859
648
          if(process_list == proc){
883
672
        }
884
673
        /* This process exited nicely, so print its buffer */
885
674
 
886
 
        bool bret = print_out_password(proc->buffer,
887
 
                                       proc->buffer_length);
 
675
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
888
676
        if(not bret){
889
677
          perror("print_out_password");
890
678
          exitstatus = EXIT_FAILURE;
891
679
        }
892
 
        goto fallback;
 
680
        goto end;
893
681
      }
894
682
      /* This process has not completed.  Does it have any output? */
895
683
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
903
691
        if (proc->buffer == NULL){
904
692
          perror("malloc");
905
693
          exitstatus = EXIT_FAILURE;
906
 
          goto fallback;
 
694
          goto end;
907
695
        }
908
696
        proc->buffer_size += BUFFER_SIZE;
909
697
      }
924
712
  }
925
713
 
926
714
 
927
 
 fallback:
 
715
 end:
928
716
  
929
717
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
930
 
    /* Fallback if all plugins failed, none are found or an error
931
 
       occured */
 
718
    /* Fallback if all plugins failed or an error occured */
932
719
    bool bret;
933
720
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
934
721
    char *passwordbuffer = getpass("Password: ");
936
723
    if(not bret){
937
724
      perror("print_out_password");
938
725
      exitstatus = EXIT_FAILURE;
 
726
      goto end;
939
727
    }
940
728
  }
941
729
  
942
730
  /* Restore old signal handler */
943
 
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
944
 
  if(ret == -1){
945
 
    perror("sigaction");
946
 
    exitstatus = EXIT_FAILURE;
947
 
  }
948
 
 
949
 
  if(custom_argv != NULL){
950
 
    for(char **arg = custom_argv; *arg != NULL; arg++){
951
 
      free(*arg);
952
 
    }
953
 
    free(custom_argv);
954
 
  }
955
 
  free_plugin_list(plugin_list);
 
731
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
732
  
 
733
  free(plus_argv);
 
734
  
 
735
  /* Free the plugin list */
 
736
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
737
    next = plugin_list->next;
 
738
    free(plugin_list->argv);
 
739
    free(plugin_list);
 
740
  }
956
741
  
957
742
  if(dir != NULL){
958
743
    closedir(dir);