/mandos/release

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

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,
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
 
199
200
 
200
201
process *process_list = NULL;
201
202
 
202
 
/* Mark a process as completed when it exits, and save its exit
 
203
/* Mark processes as completed when they exit, and save their exit
203
204
   status. */
204
205
void handle_sigchld(__attribute__((unused)) int sig){
205
 
  process *proc = process_list;
206
 
  int status;
207
 
  pid_t pid = wait(&status);
208
 
  if(pid == -1){
209
 
    perror("wait");
210
 
    return;
211
 
  }
212
 
  while(proc != NULL and proc->pid != pid){
213
 
    proc = proc->next;
214
 
  }
215
 
  if(proc == NULL){
216
 
    /* Process not found in process list */
217
 
    return;
218
 
  }
219
 
  proc->status = status;
220
 
  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
  }
221
233
}
222
234
 
223
235
bool print_out_password(const char *buffer, size_t length){
235
247
  return true;
236
248
}
237
249
 
238
 
char **add_to_argv(char **argv, int *argc, char *arg){
239
 
  if (argv == NULL){
240
 
    *argc = 1;
241
 
    argv = malloc(sizeof(char*) * 2);
242
 
    if(argv == NULL){
243
 
      return NULL;
244
 
    }
245
 
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
246
 
    argv[1] = NULL;
247
 
  }
248
 
  *argc += 1;
249
 
  argv = realloc(argv, sizeof(char *)
250
 
                  * ((unsigned int) *argc + 1));
251
 
  if(argv == NULL){
252
 
    return NULL;
253
 
  }
254
 
  argv[*argc-1] = arg;
255
 
  argv[*argc] = NULL;
256
 
  return argv;
257
 
}
258
 
 
259
250
static void free_plugin_list(plugin *plugin_list){
260
251
  for(plugin *next; plugin_list != NULL; plugin_list = next){
261
252
    next = plugin_list->next;
467
458
    const char whitespace_delims[] = " \r\t\f\v\n";
468
459
    const char comment_delim[] = "#";
469
460
 
 
461
    custom_argc = 1;
 
462
    custom_argv = malloc(sizeof(char*) * 2);
 
463
    if(custom_argv == NULL){
 
464
      perror("malloc");
 
465
      exitstatus = EXIT_FAILURE;
 
466
      goto fallback;
 
467
    }
 
468
    custom_argv[0] = argv[0];
 
469
    custom_argv[1] = NULL;
 
470
    
470
471
    while(true){
471
472
      sret = getline(&org_line, &size, conffp);
472
473
      if(sret == -1){
480
481
          continue;
481
482
        }
482
483
        new_arg = strdup(p);
483
 
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
484
 
        if (custom_argv == NULL){
485
 
          perror("add_to_argv");
 
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");
486
490
          exitstatus = EXIT_FAILURE;
 
491
          free(org_line);
487
492
          goto fallback;
488
493
        }
 
494
        custom_argv[custom_argc-1] = new_arg;
 
495
        custom_argv[custom_argc] = NULL;        
489
496
      }
490
497
    }
491
498
    free(org_line);
500
507
  }
501
508
 
502
509
  if(custom_argv != NULL){
503
 
    custom_argv[0] = argv[0];
504
510
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
505
511
    if (ret == ARGP_ERR_UNKNOWN){
506
512
      fprintf(stderr, "Unknown error while parsing arguments\n");
835
841
          /* Remove the plugin */
836
842
          FD_CLR(proc->fd, &rfds_all);
837
843
          /* Block signal while modifying process_list */
838
 
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
844
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
839
845
          if(ret < 0){
840
846
            perror("sigprocmask");
841
847
            exitstatus = EXIT_FAILURE;
869
875
        }
870
876
        /* This process exited nicely, so print its buffer */
871
877
 
872
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
878
        bool bret = print_out_password(proc->buffer,
 
879
                                       proc->buffer_length);
873
880
        if(not bret){
874
881
          perror("print_out_password");
875
882
          exitstatus = EXIT_FAILURE;
912
919
 fallback:
913
920
  
914
921
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
915
 
    /* Fallback if all plugins failed, none are found or an error occured */
 
922
    /* Fallback if all plugins failed, none are found or an error
 
923
       occured */
916
924
    bool bret;
917
925
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
918
926
    char *passwordbuffer = getpass("Password: ");
931
939
  }
932
940
 
933
941
  if(custom_argv != NULL){
934
 
    for(char **arg = custom_argv; *arg != NULL; arg++){
 
942
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
935
943
      free(*arg);
936
944
    }
937
945
    free(custom_argv);