/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-31 16:04:47 UTC
  • mfrom: (24.1.77 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080831160447-2xte5k90onspphki
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
#include <argp.h>               /* struct argp_option, struct
57
57
                                   argp_state, struct argp,
58
58
                                   argp_parse(), ARGP_ERR_UNKNOWN,
59
 
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
59
                                   ARGP_KEY_END, ARGP_KEY_ARG,
 
60
                                   error_t */
60
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
61
62
                                   sigaddset(), sigaction(),
62
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
64
65
#include <errno.h>              /* errno, EBADF */
65
66
 
66
67
#define BUFFER_SIZE 256
67
 
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
68
 
 
69
#define PDIR "/lib/mandos/plugins.d"
 
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
68
71
 
69
72
const char *argp_program_version = "plugin-runner 1.0";
70
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
74
 
72
 
struct process;
73
 
 
74
 
typedef struct process{
75
 
  pid_t pid;
76
 
  int fd;
77
 
  char *buffer;
78
 
  size_t buffer_size;
79
 
  size_t buffer_length;
80
 
  bool eof;
81
 
  bool completed;
82
 
  int status;
83
 
  struct process *next;
84
 
} process;
 
75
struct plugin;
85
76
 
86
77
typedef struct plugin{
87
78
  char *name;                   /* can be NULL or any plugin name */
90
81
  char **environ;
91
82
  int envc;
92
83
  bool disabled;
 
84
 
 
85
  /* Variables used for running processes*/
 
86
  pid_t pid;
 
87
  int fd;
 
88
  char *buffer;
 
89
  size_t buffer_size;
 
90
  size_t buffer_length;
 
91
  bool eof;
 
92
  volatile bool completed;
 
93
  volatile int status;
93
94
  struct plugin *next;
94
95
} plugin;
95
96
 
96
 
static plugin *getplugin(char *name, plugin **plugin_list){
97
 
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
 
97
static plugin *plugin_list = NULL;
 
98
 
 
99
/* Gets a existing plugin based on name,
 
100
   or if none is found, creates a new one */
 
101
static plugin *getplugin(char *name){
 
102
  /* Check for exiting plugin with that name */
 
103
  for (plugin *p = plugin_list; p != NULL; p = p->next){
98
104
    if ((p->name == name)
99
105
        or (p->name and name and (strcmp(p->name, name) == 0))){
100
106
      return p;
105
111
  if (new_plugin == NULL){
106
112
    return NULL;
107
113
  }
108
 
  char *copy_name = strdup(name);
109
 
  if(copy_name == NULL){
110
 
    return NULL;
 
114
  char *copy_name = NULL;
 
115
  if(name != NULL){
 
116
    copy_name = strdup(name);
 
117
    if(copy_name == NULL){
 
118
      return NULL;
 
119
    }
111
120
  }
112
 
  
 
121
 
113
122
  *new_plugin = (plugin) { .name = copy_name,
114
123
                           .argc = 1,
115
 
                           .envc = 0,
116
124
                           .disabled = false,
117
 
                           .next = *plugin_list };
 
125
                           .next = plugin_list };
118
126
  
119
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
120
128
  if (new_plugin->argv == NULL){
 
129
    free(copy_name);
121
130
    free(new_plugin);
122
131
    return NULL;
123
132
  }
126
135
 
127
136
  new_plugin->environ = malloc(sizeof(char *));
128
137
  if(new_plugin->environ == NULL){
 
138
    free(copy_name);
129
139
    free(new_plugin->argv);
130
140
    free(new_plugin);
131
141
    return NULL;
132
142
  }
133
143
  new_plugin->environ[0] = NULL;
 
144
 
134
145
  /* Append the new plugin to the list */
135
 
  *plugin_list = new_plugin;
 
146
  plugin_list = new_plugin;
136
147
  return new_plugin;
137
148
}
138
149
 
175
186
  return add_to_char_array(def, &(p->environ), &(p->envc));
176
187
}
177
188
 
178
 
 
179
189
/*
180
190
 * Based on the example in the GNU LibC manual chapter 13.13 "File
181
191
 * Descriptor Flags".
192
202
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
193
203
}
194
204
 
195
 
process *process_list = NULL;
196
205
 
197
 
/* Mark a process as completed when it exits, and save its exit
 
206
/* Mark processes as completed when they exit, and save their exit
198
207
   status. */
199
208
void handle_sigchld(__attribute__((unused)) int sig){
200
 
  process *proc = process_list;
201
 
  int status;
202
 
  pid_t pid = wait(&status);
203
 
  if(pid == -1){
204
 
    perror("wait");
205
 
    return;
206
 
  }
207
 
  while(proc != NULL and proc->pid != pid){
208
 
    proc = proc->next;
209
 
  }
210
 
  if(proc == NULL){
211
 
    /* Process not found in process list */
212
 
    return;
213
 
  }
214
 
  proc->status = status;
215
 
  proc->completed = true;
 
209
  while(true){
 
210
    plugin *proc = plugin_list;
 
211
    int status;
 
212
    pid_t pid = waitpid(-1, &status, WNOHANG);
 
213
    if(pid == 0){
 
214
      /* Only still running child processes */
 
215
      break;
 
216
    }
 
217
    if(pid == -1){
 
218
      if (errno != ECHILD){
 
219
        perror("waitpid");
 
220
      }
 
221
      /* No child processes */
 
222
      break;
 
223
    }
 
224
 
 
225
    /* A child exited, find it in process_list */
 
226
    while(proc != NULL and proc->pid != pid){
 
227
      proc = proc->next;
 
228
    }
 
229
    if(proc == NULL){
 
230
      /* Process not found in process list */
 
231
      continue;
 
232
    }
 
233
    proc->status = status;
 
234
    proc->completed = true;
 
235
  }
216
236
}
217
237
 
 
238
/* Prints out a password to stdout */
218
239
bool print_out_password(const char *buffer, size_t length){
219
240
  ssize_t ret;
220
241
  if(length>0 and buffer[length-1] == '\n'){
230
251
  return true;
231
252
}
232
253
 
233
 
char **add_to_argv(char **argv, int *argc, char *arg){
234
 
  if (argv == NULL){
235
 
    *argc = 1;
236
 
    argv = malloc(sizeof(char*) * 2);
237
 
    if(argv == NULL){
238
 
      return NULL;
 
254
/* Removes and free a plugin from the plugin list */
 
255
static void free_plugin(plugin *plugin_node){
 
256
  
 
257
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
 
258
    free(*arg);
 
259
  }
 
260
  free(plugin_node->argv);
 
261
  for(char **env = plugin_node->environ; *env != NULL; env++){
 
262
    free(*env);
 
263
  }
 
264
  free(plugin_node->environ);
 
265
  free(plugin_node->buffer);
 
266
 
 
267
  /* Removes the plugin from the singly-linked list */
 
268
  if(plugin_node == plugin_list){
 
269
    /* First one - simple */
 
270
    plugin_list = plugin_list->next;
 
271
  } else {
 
272
    /* Second one or later */
 
273
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
274
      if(p->next == plugin_node){
 
275
        p->next = plugin_node->next;
 
276
        break;
 
277
      }
239
278
    }
240
 
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
241
 
    argv[1] = NULL;
242
 
  }
243
 
  *argc += 1;
244
 
  argv = realloc(argv, sizeof(char *)
245
 
                  * ((unsigned int) *argc + 1));
246
 
  if(argv == NULL){
247
 
    return NULL;
248
 
  }
249
 
  argv[*argc-1] = arg;
250
 
  argv[*argc] = NULL;   
251
 
  return argv;
 
279
  }
 
280
  
 
281
  free(plugin_node);
252
282
}
253
283
 
254
 
static void free_plugin_list(plugin *plugin_list){
255
 
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
256
 
    next = plugin_list->next;
257
 
    free(plugin_list->name);
258
 
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
259
 
      free(*arg);
260
 
    }    
261
 
    free(plugin_list->argv);
262
 
    for(char **env = plugin_list->environ; *env != NULL; env++){
263
 
      free(*env);
264
 
    }
265
 
    free(plugin_list->environ);
266
 
    free(plugin_list);
267
 
  }  
 
284
static void free_plugin_list(void){
 
285
  while(plugin_list != NULL){
 
286
    free_plugin(plugin_list);
 
287
  }
268
288
}
269
289
 
270
290
int main(int argc, char *argv[]){
271
 
  const char *plugindir = "/lib/mandos/plugins.d";
272
 
  const char *argfile = ARGFILE;
 
291
  char *plugindir = NULL;
 
292
  char *argfile = NULL;
273
293
  FILE *conffp;
274
294
  size_t d_name_len;
275
295
  DIR *dir = NULL;
298
318
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
299
319
  if(ret == -1){
300
320
    perror("sigaction");
301
 
    exitstatus = EXIT_FAILURE;    
 
321
    exitstatus = EXIT_FAILURE;
302
322
    goto fallback;
303
323
  }
304
324
  
322
342
    { .name = "plugin-dir", .key = 128,
323
343
      .arg = "DIRECTORY",
324
344
      .doc = "Specify a different plugin directory", .group = 2 },
325
 
    { .name = "userid", .key = 129,
326
 
      .arg = "ID", .flags = 0,
327
 
      .doc = "User ID the plugins will run as", .group = 2 },
328
 
    { .name = "groupid", .key = 130,
329
 
      .arg = "ID", .flags = 0,
330
 
      .doc = "Group ID the plugins will run as", .group = 2 },
331
 
    { .name = "debug", .key = 131,
332
 
      .doc = "Debug mode", .group = 3 },
 
345
    { .name = "config-file", .key = 129,
 
346
      .arg = "FILE",
 
347
      .doc = "Specify a different configuration file", .group = 2 },
 
348
    { .name = "userid", .key = 130,
 
349
      .arg = "ID", .flags = 0,
 
350
      .doc = "User ID the plugins will run as", .group = 3 },
 
351
    { .name = "groupid", .key = 131,
 
352
      .arg = "ID", .flags = 0,
 
353
      .doc = "Group ID the plugins will run as", .group = 3 },
 
354
    { .name = "debug", .key = 132,
 
355
      .doc = "Debug mode", .group = 4 },
333
356
    { .name = NULL }
334
357
  };
335
358
  
336
 
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
 
359
  error_t parse_opt (int key, char *arg, __attribute__((unused)) struct argp_state *state) {
337
360
    /* Get the INPUT argument from `argp_parse', which we know is a
338
361
       pointer to our plugin list pointer. */
339
 
    plugin **plugins = state->input;
340
362
    switch (key) {
341
 
    case 'g':
 
363
    case 'g':                   /* --global-options */
342
364
      if (arg != NULL){
343
365
        char *p;
344
366
        while((p = strsep(&arg, ",")) != NULL){
345
367
          if(p[0] == '\0'){
346
368
            continue;
347
369
          }
348
 
          if(not add_argument(getplugin(NULL, plugins), p)){
 
370
          if(not add_argument(getplugin(NULL), p)){
349
371
            perror("add_argument");
350
372
            return ARGP_ERR_UNKNOWN;
351
373
          }
352
374
        }
353
375
      }
354
376
      break;
355
 
    case 'e':
 
377
    case 'e':                   /* --global-envs */
356
378
      if(arg == NULL){
357
379
        break;
358
380
      }
361
383
        if(envdef == NULL){
362
384
          break;
363
385
        }
364
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
 
386
        if(not add_environment(getplugin(NULL), envdef)){
365
387
          perror("add_environment");
366
388
        }
367
389
      }
368
390
      break;
369
 
    case 'o':
 
391
    case 'o':                   /* --options-for */
370
392
      if (arg != NULL){
371
393
        char *p_name = strsep(&arg, ":");
372
394
        if(p_name[0] == '\0'){
382
404
            if(p[0] == '\0'){
383
405
              continue;
384
406
            }
385
 
            if(not add_argument(getplugin(p_name, plugins), p)){
 
407
            if(not add_argument(getplugin(p_name), p)){
386
408
              perror("add_argument");
387
409
              return ARGP_ERR_UNKNOWN;
388
410
            }
390
412
        }
391
413
      }
392
414
      break;
393
 
    case 'f':
 
415
    case 'f':                   /* --envs-for */
394
416
      if(arg == NULL){
395
417
        break;
396
418
      }
404
426
          break;
405
427
        }
406
428
        envdef++;
407
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
 
429
        if(not add_environment(getplugin(p_name), envdef)){
408
430
          perror("add_environment");
409
431
        }
410
432
      }
411
433
      break;
412
 
    case 'd':
 
434
    case 'd':                   /* --disable */
413
435
      if (arg != NULL){
414
 
        plugin *p = getplugin(arg, plugins);
 
436
        plugin *p = getplugin(arg);
415
437
        if(p == NULL){
416
438
          return ARGP_ERR_UNKNOWN;
417
439
        }
418
440
        p->disabled = true;
419
441
      }
420
442
      break;
421
 
    case 128:
422
 
      plugindir = arg;
 
443
    case 128:                   /* --plugin-dir */
 
444
      plugindir = strdup(arg);
 
445
      if(plugindir == NULL){
 
446
        perror("strdup");
 
447
      }      
423
448
      break;
424
 
    case 129:
 
449
    case 129:                   /* --config-file */
 
450
      argfile = strdup(arg);
 
451
      if(argfile == NULL){
 
452
        perror("strdup");
 
453
      }
 
454
      break;      
 
455
    case 130:                   /* --userid */
425
456
      uid = (uid_t)strtol(arg, NULL, 10);
426
457
      break;
427
 
    case 130:
 
458
    case 131:                   /* --groupid */
428
459
      gid = (gid_t)strtol(arg, NULL, 10);
429
460
      break;
430
 
    case 131:
 
461
    case 132:                   /* --debug */
431
462
      debug = true;
432
463
      break;
433
464
    case ARGP_KEY_ARG:
441
472
    return 0;
442
473
  }
443
474
  
444
 
  plugin *plugin_list = NULL;
445
 
  
446
475
  struct argp argp = { .options = options, .parser = parse_opt,
447
476
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
448
477
                       .doc = "Mandos plugin runner -- Run plugins" };
449
478
  
450
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
479
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
451
480
  if (ret == ARGP_ERR_UNKNOWN){
452
481
    fprintf(stderr, "Unknown error while parsing arguments\n");
453
482
    exitstatus = EXIT_FAILURE;
454
483
    goto fallback;
455
484
  }
456
485
 
457
 
  conffp = fopen(argfile, "r");
 
486
  /* Opens the configfile if aviable */
 
487
  if (argfile == NULL){
 
488
    conffp = fopen(AFILE, "r");
 
489
  } else {
 
490
    conffp = fopen(argfile, "r");
 
491
  }  
458
492
  if(conffp != NULL){
459
493
    char *org_line = NULL;
460
494
    char *p, *arg, *new_arg, *line;
463
497
    const char whitespace_delims[] = " \r\t\f\v\n";
464
498
    const char comment_delim[] = "#";
465
499
 
 
500
    custom_argc = 1;
 
501
    custom_argv = malloc(sizeof(char*) * 2);
 
502
    if(custom_argv == NULL){
 
503
      perror("malloc");
 
504
      exitstatus = EXIT_FAILURE;
 
505
      goto fallback;
 
506
    }
 
507
    custom_argv[0] = argv[0];
 
508
    custom_argv[1] = NULL;
 
509
 
 
510
    /* for each line in the config file, strip whitespace and ignore commented text */
466
511
    while(true){
467
512
      sret = getline(&org_line, &size, conffp);
468
513
      if(sret == -1){
476
521
          continue;
477
522
        }
478
523
        new_arg = strdup(p);
479
 
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
480
 
        if (custom_argv == NULL){
481
 
          perror("add_to_argv");
482
 
          exitstatus = EXIT_FAILURE;
483
 
          goto fallback;
484
 
        }
 
524
        if(new_arg == NULL){
 
525
          perror("strdup");
 
526
          exitstatus = EXIT_FAILURE;
 
527
          free(org_line);
 
528
          goto fallback;
 
529
        }
 
530
        
 
531
        custom_argc += 1;
 
532
        custom_argv = realloc(custom_argv, sizeof(char *)
 
533
                              * ((unsigned int) custom_argc + 1));
 
534
        if(custom_argv == NULL){
 
535
          perror("realloc");
 
536
          exitstatus = EXIT_FAILURE;
 
537
          free(org_line);
 
538
          goto fallback;
 
539
        }
 
540
        custom_argv[custom_argc-1] = new_arg;
 
541
        custom_argv[custom_argc] = NULL;        
485
542
      }
486
543
    }
487
544
    free(org_line);
494
551
      goto fallback;
495
552
    }
496
553
  }
497
 
 
 
554
  /* If there was any arguments from configuration file,
 
555
     pass them to parser as command arguments */
498
556
  if(custom_argv != NULL){
499
 
    custom_argv[0] = argv[0];
500
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
557
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
501
558
    if (ret == ARGP_ERR_UNKNOWN){
502
559
      fprintf(stderr, "Unknown error while parsing arguments\n");
503
560
      exitstatus = EXIT_FAILURE;
518
575
      }
519
576
    }
520
577
  }
521
 
  
 
578
 
 
579
  /* Strip permissions down to nobody */
522
580
  ret = setuid(uid);
523
581
  if (ret == -1){
524
582
    perror("setuid");
525
 
  }
526
 
  
 
583
  }  
527
584
  setgid(gid);
528
585
  if (ret == -1){
529
586
    perror("setgid");
530
587
  }
 
588
 
 
589
  if (plugindir == NULL){
 
590
    dir = opendir(PDIR);
 
591
  } else {
 
592
    dir = opendir(plugindir);
 
593
  }
531
594
  
532
 
  dir = opendir(plugindir);
533
595
  if(dir == NULL){
534
596
    perror("Could not open plugin dir");
535
597
    exitstatus = EXIT_FAILURE;
550
612
  }
551
613
  
552
614
  FD_ZERO(&rfds_all);
553
 
  
 
615
 
 
616
  /* Read and execute any executable in the plugin directory*/
554
617
  while(true){
555
618
    dirst = readdir(dir);
556
619
    
587
650
          break;
588
651
        }
589
652
      }
590
 
      
591
653
      if(bad_name){
592
654
        continue;
593
655
      }
594
 
      
595
656
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
596
657
        size_t suf_len = strlen(*suf);
597
658
        if((d_name_len >= suf_len)
624
685
      free(filename);
625
686
      continue;
626
687
    }
627
 
    
 
688
 
 
689
    /* Ignore non-executable files */
628
690
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
629
691
      if(debug){
630
692
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
633
695
      free(filename);
634
696
      continue;
635
697
    }
636
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
 
698
    
 
699
    plugin *p = getplugin(dirst->d_name);
637
700
    if(p == NULL){
638
701
      perror("getplugin");
639
702
      free(filename);
649
712
    }
650
713
    {
651
714
      /* Add global arguments to argument list for this plugin */
652
 
      plugin *g = getplugin(NULL, &plugin_list);
 
715
      plugin *g = getplugin(NULL);
653
716
      if(g != NULL){
654
717
        for(char **a = g->argv + 1; *a != NULL; a++){
655
718
          if(not add_argument(p, *a)){
680
743
      }
681
744
    }
682
745
    
683
 
    int pipefd[2]; 
 
746
    int pipefd[2];
684
747
    ret = pipe(pipefd);
685
748
    if (ret == -1){
686
749
      perror("pipe");
687
750
      exitstatus = EXIT_FAILURE;
688
751
      goto fallback;
689
752
    }
 
753
    /* Ask OS to automatic close the pipe on exec */
690
754
    ret = set_cloexec_flag(pipefd[0]);
691
755
    if(ret < 0){
692
756
      perror("set_cloexec_flag");
750
814
      }
751
815
      /* no return */
752
816
    }
753
 
    /* parent process */
 
817
    /* Parent process */
 
818
    close(pipefd[1]);           /* Close unused write end of pipe */
754
819
    free(filename);
755
 
    close(pipefd[1]);           /* close unused write end of pipe */
756
 
    process *new_process = malloc(sizeof(process));
757
 
    if (new_process == NULL){
758
 
      perror("malloc");
 
820
    plugin *new_plugin = getplugin(dirst->d_name);
 
821
    if (new_plugin == NULL){
 
822
      perror("getplugin");
759
823
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
760
824
      if(ret < 0){
761
 
        perror("sigprocmask");
 
825
        perror("sigprocmask");
762
826
      }
763
827
      exitstatus = EXIT_FAILURE;
764
828
      goto fallback;
765
829
    }
766
830
    
767
 
    *new_process = (struct process){ .pid = pid,
768
 
                                     .fd = pipefd[0],
769
 
                                     .next = process_list };
770
 
    // List handling
771
 
    process_list = new_process;
 
831
    new_plugin->pid = pid;
 
832
    new_plugin->fd = pipefd[0];
 
833
    
772
834
    /* Unblock SIGCHLD so signal handler can be run if this process
773
835
       has already completed */
774
836
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
778
840
      goto fallback;
779
841
    }
780
842
    
781
 
    FD_SET(new_process->fd, &rfds_all);
 
843
    FD_SET(new_plugin->fd, &rfds_all);
782
844
    
783
 
    if (maxfd < new_process->fd){
784
 
      maxfd = new_process->fd;
 
845
    if (maxfd < new_plugin->fd){
 
846
      maxfd = new_plugin->fd;
785
847
    }
786
848
    
787
849
  }
788
850
  
789
 
  free_plugin_list(plugin_list);
790
 
  
791
851
  closedir(dir);
792
852
  dir = NULL;
793
 
    
794
 
  if (process_list == NULL){
795
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
796
 
            " directory?\n");
797
 
    process_list = NULL;
 
853
 
 
854
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
855
    if(p->pid != 0){
 
856
      break;
 
857
    }
 
858
    if(p->next == NULL){
 
859
      fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
860
              " directory?\n");
 
861
      free_plugin_list();
 
862
    }
798
863
  }
799
 
  while(process_list){
 
864
 
 
865
  /* Main loop while running plugins exist */
 
866
  while(plugin_list){
800
867
    fd_set rfds = rfds_all;
801
868
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
802
869
    if (select_ret == -1){
806
873
    }
807
874
    /* OK, now either a process completed, or something can be read
808
875
       from one of them */
809
 
    for(process *proc = process_list; proc ; proc = proc->next){
 
876
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
810
877
      /* Is this process completely done? */
811
878
      if(proc->eof and proc->completed){
812
879
        /* Only accept the plugin output if it exited cleanly */
813
880
        if(not WIFEXITED(proc->status)
814
881
           or WEXITSTATUS(proc->status) != 0){
815
882
          /* Bad exit by plugin */
 
883
 
816
884
          if(debug){
817
885
            if(WIFEXITED(proc->status)){
818
886
              fprintf(stderr, "Plugin %u exited with status %d\n",
827
895
                      (unsigned int) (proc->pid));
828
896
            }
829
897
          }
 
898
          
830
899
          /* Remove the plugin */
831
900
          FD_CLR(proc->fd, &rfds_all);
 
901
 
832
902
          /* Block signal while modifying process_list */
833
 
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
903
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
834
904
          if(ret < 0){
835
905
            perror("sigprocmask");
836
906
            exitstatus = EXIT_FAILURE;
837
907
            goto fallback;
838
908
          }
839
 
          /* Delete this process entry from the list */
840
 
          if(process_list == proc){
841
 
            /* First one - simple */
842
 
            process_list = proc->next;
843
 
          } else {
844
 
            /* Second one or later */
845
 
            for(process *p = process_list; p != NULL; p = p->next){
846
 
              if(p->next == proc){
847
 
                p->next = proc->next;
848
 
                break;
849
 
              }
850
 
            }
851
 
          }
 
909
          free_plugin(proc);
852
910
          /* We are done modifying process list, so unblock signal */
853
911
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
854
912
                             NULL);
855
913
          if(ret < 0){
856
914
            perror("sigprocmask");
857
 
          }
858
 
          free(proc->buffer);
859
 
          free(proc);
860
 
          /* We deleted this process from the list, so we can't go
861
 
             proc->next.  Therefore, start over from the beginning of
862
 
             the process list */
863
 
          break;
 
915
            exitstatus = EXIT_FAILURE;
 
916
            goto fallback;
 
917
          }
 
918
          
 
919
          if(plugin_list == NULL){
 
920
            break;
 
921
          }
 
922
          continue;
864
923
        }
 
924
        
865
925
        /* This process exited nicely, so print its buffer */
866
926
 
867
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
927
        bool bret = print_out_password(proc->buffer,
 
928
                                       proc->buffer_length);
868
929
        if(not bret){
869
930
          perror("print_out_password");
870
931
          exitstatus = EXIT_FAILURE;
871
932
        }
872
933
        goto fallback;
873
934
      }
 
935
      
874
936
      /* This process has not completed.  Does it have any output? */
875
937
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
876
938
        /* This process had nothing to say at this time */
906
968
 
907
969
 fallback:
908
970
  
909
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
910
 
    /* Fallback if all plugins failed, none are found or an error occured */
 
971
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
972
    /* Fallback if all plugins failed, none are found or an error
 
973
       occured */
911
974
    bool bret;
912
975
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
913
976
    char *passwordbuffer = getpass("Password: ");
926
989
  }
927
990
 
928
991
  if(custom_argv != NULL){
929
 
    for(char **arg = custom_argv; *arg != NULL; arg++){
 
992
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
930
993
      free(*arg);
931
994
    }
932
995
    free(custom_argv);
933
996
  }
934
 
  free_plugin_list(plugin_list);
935
997
  
936
998
  if(dir != NULL){
937
999
    closedir(dir);
938
1000
  }
939
1001
  
940
1002
  /* Free the process list and kill the processes */
941
 
  for(process *next; process_list != NULL; process_list = next){
942
 
    next = process_list->next;
943
 
    close(process_list->fd);
944
 
    ret = kill(process_list->pid, SIGTERM);
945
 
    if(ret == -1 and errno != ESRCH){
946
 
      /* set-uid proccesses migth not get closed */
947
 
      perror("kill");
 
1003
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
1004
    if(p->pid != 0){
 
1005
      close(p->fd);
 
1006
      ret = kill(p->pid, SIGTERM);
 
1007
      if(ret == -1 and errno != ESRCH){
 
1008
        /* Set-uid proccesses might not get closed */
 
1009
        perror("kill");
 
1010
      }
948
1011
    }
949
 
    free(process_list->buffer);
950
 
    free(process_list);
951
1012
  }
952
1013
  
953
1014
  /* Wait for any remaining child processes to terminate */
957
1018
  if(errno != ECHILD){
958
1019
    perror("wait");
959
1020
  }
 
1021
 
 
1022
  free_plugin_list();
 
1023
  
 
1024
  free(plugindir);
 
1025
  free(argfile);
960
1026
  
961
1027
  return exitstatus;
962
1028
}