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,
 
 
59
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
61
60
#include <signal.h>             /* struct sigaction, sigemptyset(),
 
62
61
                                   sigaddset(), sigaction(),
 
63
62
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
 
 
65
64
#include <errno.h>              /* errno, EBADF */
 
67
66
#define BUFFER_SIZE 256
 
69
 
#define PDIR "/lib/mandos/plugins.d"
 
70
 
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
 
67
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
72
69
const char *argp_program_version = "plugin-runner 1.0";
 
73
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
 
203
195
process *process_list = NULL;
 
205
 
/* Mark processes as completed when they exit, and save their exit
 
 
197
/* Mark a process as completed when it exits, and save its exit
 
207
199
void handle_sigchld(__attribute__((unused)) int sig){
 
209
 
    process *proc = process_list;
 
211
 
    pid_t pid = waitpid(-1, &status, WNOHANG);
 
213
 
      /* Only still running child processes */
 
217
 
      if (errno != ECHILD){
 
220
 
      /* No child processes */
 
224
 
    /* A child exited, find it in process_list */
 
225
 
    while(proc != NULL and proc->pid != pid){
 
229
 
      /* Process not found in process list */
 
232
 
    proc->status = status;
 
233
 
    proc->completed = true;
 
 
200
  process *proc = process_list;
 
 
202
  pid_t pid = wait(&status);
 
 
207
  while(proc != NULL and proc->pid != pid){
 
 
211
    /* Process not found in process list */
 
 
214
  proc->status = status;
 
 
215
  proc->completed = true;
 
237
218
bool print_out_password(const char *buffer, size_t length){
 
 
 
233
char **add_to_argv(char **argv, int *argc, char *arg){
 
 
236
    argv = malloc(sizeof(char*) * 2);
 
 
240
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
 
244
  argv = realloc(argv, sizeof(char *)
 
 
245
                  * ((unsigned int) *argc + 1));
 
252
254
static void free_plugin_list(plugin *plugin_list){
 
253
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
 
255
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
 
254
256
    next = plugin_list->next;
 
 
257
    free(plugin_list->name);
 
255
258
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
 
258
261
    free(plugin_list->argv);
 
259
262
    for(char **env = plugin_list->environ; *env != NULL; env++){
 
262
265
    free(plugin_list->environ);
 
263
266
    free(plugin_list);
 
267
270
int main(int argc, char *argv[]){
 
268
 
  char *plugindir = NULL;
 
269
 
  char *argfile = NULL;
 
 
271
  const char *plugindir = "/lib/mandos/plugins.d";
 
 
272
  const char *argfile = ARGFILE;
 
271
274
  size_t d_name_len;
 
 
319
322
    { .name = "plugin-dir", .key = 128,
 
320
323
      .arg = "DIRECTORY",
 
321
324
      .doc = "Specify a different plugin directory", .group = 2 },
 
322
 
    { .name = "config-file", .key = 129,
 
324
 
      .doc = "Specify a different configuration file", .group = 2 },
 
325
 
    { .name = "userid", .key = 130,
 
326
 
      .arg = "ID", .flags = 0,
 
327
 
      .doc = "User ID the plugins will run as", .group = 3 },
 
328
 
    { .name = "groupid", .key = 131,
 
329
 
      .arg = "ID", .flags = 0,
 
330
 
      .doc = "Group ID the plugins will run as", .group = 3 },
 
331
 
    { .name = "debug", .key = 132,
 
332
 
      .doc = "Debug mode", .group = 4 },
 
 
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 },
 
 
422
 
      plugindir = strdup(arg);
 
423
 
      if(plugindir == NULL){
 
428
 
      argfile = strdup(arg);
 
 
425
      uid = (uid_t)strtol(arg, NULL, 10);
 
434
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
 
428
      gid = (gid_t)strtol(arg, NULL, 10);
 
437
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
442
433
    case ARGP_KEY_ARG:
 
 
466
 
  if (argfile == NULL){
 
467
 
    conffp = fopen(AFILE, "r");
 
469
 
    conffp = fopen(argfile, "r");
 
 
457
  conffp = fopen(argfile, "r");
 
472
458
  if(conffp != NULL){
 
473
459
    char *org_line = NULL;
 
474
460
    char *p, *arg, *new_arg, *line;
 
 
477
463
    const char whitespace_delims[] = " \r\t\f\v\n";
 
478
464
    const char comment_delim[] = "#";
 
481
 
    custom_argv = malloc(sizeof(char*) * 2);
 
482
 
    if(custom_argv == NULL){
 
484
 
      exitstatus = EXIT_FAILURE;
 
487
 
    custom_argv[0] = argv[0];
 
488
 
    custom_argv[1] = NULL;
 
491
467
      sret = getline(&org_line, &size, conffp);
 
 
502
478
        new_arg = strdup(p);
 
505
 
          exitstatus = EXIT_FAILURE;
 
511
 
        custom_argv = realloc(custom_argv, sizeof(char *)
 
512
 
                              * ((unsigned int) custom_argc + 1));
 
513
 
        if(custom_argv == NULL){
 
515
 
          exitstatus = EXIT_FAILURE;
 
519
 
        custom_argv[custom_argc-1] = new_arg;
 
520
 
        custom_argv[custom_argc] = NULL;        
 
 
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;
 
 
534
498
  if(custom_argv != NULL){
 
 
499
    custom_argv[0] = argv[0];
 
535
500
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
536
501
    if (ret == ARGP_ERR_UNKNOWN){
 
537
502
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
 
871
830
          /* Remove the plugin */
 
872
831
          FD_CLR(proc->fd, &rfds_all);
 
873
832
          /* Block signal while modifying process_list */
 
874
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
 
833
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
876
835
            perror("sigprocmask");
 
877
836
            exitstatus = EXIT_FAILURE;
 
 
906
865
        /* This process exited nicely, so print its buffer */
 
908
 
        bool bret = print_out_password(proc->buffer,
 
909
 
                                       proc->buffer_length);
 
 
867
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
911
869
          perror("print_out_password");
 
912
870
          exitstatus = EXIT_FAILURE;
 
 
951
909
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
952
 
    /* Fallback if all plugins failed, none are found or an error
 
 
910
    /* Fallback if all plugins failed, none are found or an error occured */
 
955
912
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
956
913
    char *passwordbuffer = getpass("Password: ");