/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

  • Committer: Björn Påhlsson
  • Date: 2008-08-24 23:13:57 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 102.
  • Revision ID: belorn@braxen-20080824231357-e304pw7ls2dnnvw5
* plugin-runner.c (handle_sigchld): Loop until all exited children
                                    have been wait()ed for.

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,
60
 
                                   error_t */
 
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 */
66
65
 
67
66
#define BUFFER_SIZE 256
68
 
 
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"
71
68
 
72
69
const char *argp_program_version = "plugin-runner 1.0";
73
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
81
78
  size_t buffer_size;
82
79
  size_t buffer_length;
83
80
  bool eof;
84
 
  volatile bool completed;
85
 
  volatile int status;
 
81
  bool completed;
 
82
  int status;
86
83
  struct process *next;
87
84
} process;
88
85
 
202
199
 
203
200
process *process_list = NULL;
204
201
 
205
 
/* Mark processes as completed when they exit, and save their exit
 
202
/* Mark processes as completed when it exits, and save its exit
206
203
   status. */
207
204
void handle_sigchld(__attribute__((unused)) int sig){
 
205
  process *proc = process_list;
208
206
  while(true){
209
 
    process *proc = process_list;
210
207
    int status;
211
208
    pid_t pid = waitpid(-1, &status, WNOHANG);
212
209
    if(pid == 0){
213
 
      /* Only still running child processes */
214
210
      break;
215
211
    }
216
212
    if(pid == -1){
217
213
      if (errno != ECHILD){
218
214
        perror("waitpid");
219
215
      }
220
 
      /* No child processes */
221
 
      break;
 
216
      return;
222
217
    }
223
218
 
224
 
    /* A child exited, find it in process_list */
225
219
    while(proc != NULL and proc->pid != pid){
226
220
      proc = proc->next;
227
221
    }
249
243
  return true;
250
244
}
251
245
 
 
246
char **add_to_argv(char **argv, int *argc, char *arg){
 
247
  if (argv == NULL){
 
248
    *argc = 1;
 
249
    argv = malloc(sizeof(char*) * 2);
 
250
    if(argv == NULL){
 
251
      return NULL;
 
252
    }
 
253
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
254
    argv[1] = NULL;
 
255
  }
 
256
  *argc += 1;
 
257
  argv = realloc(argv, sizeof(char *)
 
258
                  * ((unsigned int) *argc + 1));
 
259
  if(argv == NULL){
 
260
    return NULL;
 
261
  }
 
262
  argv[*argc-1] = arg;
 
263
  argv[*argc] = NULL;
 
264
  return argv;
 
265
}
 
266
 
252
267
static void free_plugin_list(plugin *plugin_list){
253
268
  for(plugin *next; plugin_list != NULL; plugin_list = next){
254
269
    next = plugin_list->next;
265
280
}
266
281
 
267
282
int main(int argc, char *argv[]){
268
 
  char *plugindir = NULL;
269
 
  char *argfile = NULL;
 
283
  const char *plugindir = "/lib/mandos/plugins.d";
 
284
  const char *argfile = ARGFILE;
270
285
  FILE *conffp;
271
286
  size_t d_name_len;
272
287
  DIR *dir = NULL;
319
334
    { .name = "plugin-dir", .key = 128,
320
335
      .arg = "DIRECTORY",
321
336
      .doc = "Specify a different plugin directory", .group = 2 },
322
 
    { .name = "config-file", .key = 129,
323
 
      .arg = "FILE",
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 },
 
337
    { .name = "userid", .key = 129,
 
338
      .arg = "ID", .flags = 0,
 
339
      .doc = "User ID the plugins will run as", .group = 2 },
 
340
    { .name = "groupid", .key = 130,
 
341
      .arg = "ID", .flags = 0,
 
342
      .doc = "Group ID the plugins will run as", .group = 2 },
 
343
    { .name = "debug", .key = 131,
 
344
      .doc = "Debug mode", .group = 3 },
333
345
    { .name = NULL }
334
346
  };
335
347
  
419
431
      }
420
432
      break;
421
433
    case 128:
422
 
      plugindir = strdup(arg);
423
 
      if(plugindir == NULL){
424
 
        perror("strdup");
425
 
      }      
 
434
      plugindir = arg;
426
435
      break;
427
436
    case 129:
428
 
      argfile = strdup(arg);
429
 
      if(argfile == NULL){
430
 
        perror("strdup");
431
 
      }
432
 
      break;      
 
437
      uid = (uid_t)strtol(arg, NULL, 10);
 
438
      break;
433
439
    case 130:
434
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
440
      gid = (gid_t)strtol(arg, NULL, 10);
435
441
      break;
436
442
    case 131:
437
 
      gid = (gid_t)strtol(arg, NULL, 10);
438
 
      break;
439
 
    case 132:
440
443
      debug = true;
441
444
      break;
442
445
    case ARGP_KEY_ARG:
463
466
    goto fallback;
464
467
  }
465
468
 
466
 
  if (argfile == NULL){
467
 
    conffp = fopen(AFILE, "r");
468
 
  } else {
469
 
    conffp = fopen(argfile, "r");
470
 
  }
471
 
  
 
469
  conffp = fopen(argfile, "r");
472
470
  if(conffp != NULL){
473
471
    char *org_line = NULL;
474
472
    char *p, *arg, *new_arg, *line;
477
475
    const char whitespace_delims[] = " \r\t\f\v\n";
478
476
    const char comment_delim[] = "#";
479
477
 
480
 
    custom_argc = 1;
481
 
    custom_argv = malloc(sizeof(char*) * 2);
482
 
    if(custom_argv == NULL){
483
 
      perror("malloc");
484
 
      exitstatus = EXIT_FAILURE;
485
 
      goto fallback;
486
 
    }
487
 
    custom_argv[0] = argv[0];
488
 
    custom_argv[1] = NULL;
489
 
    
490
478
    while(true){
491
479
      sret = getline(&org_line, &size, conffp);
492
480
      if(sret == -1){
500
488
          continue;
501
489
        }
502
490
        new_arg = strdup(p);
503
 
        if(new_arg == NULL){
504
 
          perror("strdup");
505
 
          exitstatus = EXIT_FAILURE;
506
 
          free(org_line);
507
 
          goto fallback;
508
 
        }
509
 
        
510
 
        custom_argc += 1;
511
 
        custom_argv = realloc(custom_argv, sizeof(char *)
512
 
                              * ((unsigned int) custom_argc + 1));
513
 
        if(custom_argv == NULL){
514
 
          perror("realloc");
515
 
          exitstatus = EXIT_FAILURE;
516
 
          free(org_line);
517
 
          goto fallback;
518
 
        }
519
 
        custom_argv[custom_argc-1] = new_arg;
520
 
        custom_argv[custom_argc] = NULL;        
 
491
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
 
492
        if (custom_argv == NULL){
 
493
          perror("add_to_argv");
 
494
          exitstatus = EXIT_FAILURE;
 
495
          goto fallback;
 
496
        }
521
497
      }
522
498
    }
523
499
    free(org_line);
532
508
  }
533
509
 
534
510
  if(custom_argv != NULL){
 
511
    custom_argv[0] = argv[0];
535
512
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
536
513
    if (ret == ARGP_ERR_UNKNOWN){
537
514
      fprintf(stderr, "Unknown error while parsing arguments\n");
563
540
  if (ret == -1){
564
541
    perror("setgid");
565
542
  }
566
 
 
567
 
  if (plugindir == NULL){
568
 
    dir = opendir(PDIR);
569
 
  } else {
570
 
    dir = opendir(plugindir);
571
 
  }
572
543
  
 
544
  dir = opendir(plugindir);
573
545
  if(dir == NULL){
574
546
    perror("Could not open plugin dir");
575
547
    exitstatus = EXIT_FAILURE;
825
797
    }
826
798
    
827
799
  }
828
 
 
 
800
  
829
801
  free_plugin_list(plugin_list);
830
802
  plugin_list = NULL;
831
803
  
871
843
          /* Remove the plugin */
872
844
          FD_CLR(proc->fd, &rfds_all);
873
845
          /* Block signal while modifying process_list */
874
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
846
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
875
847
          if(ret < 0){
876
848
            perror("sigprocmask");
877
849
            exitstatus = EXIT_FAILURE;
905
877
        }
906
878
        /* This process exited nicely, so print its buffer */
907
879
 
908
 
        bool bret = print_out_password(proc->buffer,
909
 
                                       proc->buffer_length);
 
880
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
910
881
        if(not bret){
911
882
          perror("print_out_password");
912
883
          exitstatus = EXIT_FAILURE;
949
920
 fallback:
950
921
  
951
922
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
952
 
    /* Fallback if all plugins failed, none are found or an error
953
 
       occured */
 
923
    /* Fallback if all plugins failed, none are found or an error occured */
954
924
    bool bret;
955
925
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
956
926
    char *passwordbuffer = getpass("Password: ");
969
939
  }
970
940
 
971
941
  if(custom_argv != NULL){
972
 
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
 
942
    for(char **arg = custom_argv; *arg != NULL; arg++){
973
943
      free(*arg);
974
944
    }
975
945
    free(custom_argv);
1000
970
  if(errno != ECHILD){
1001
971
    perror("wait");
1002
972
  }
1003
 
 
1004
 
  free(plugindir);
1005
 
  free(argfile);
1006
973
  
1007
974
  return exitstatus;
1008
975
}