/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: 2009-01-13 04:45:33 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090113044533-de691yv0fa5w6quw
* plugins.d/mandos-client.c: Only comment changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
   or if none is found, creates a new one */
100
100
static plugin *getplugin(char *name){
101
101
  /* Check for exiting plugin with that name */
102
 
  for (plugin *p = plugin_list; p != NULL; p = p->next){
103
 
    if ((p->name == name)
104
 
        or (p->name and name and (strcmp(p->name, name) == 0))){
 
102
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
103
    if((p->name == name)
 
104
       or (p->name and name and (strcmp(p->name, name) == 0))){
105
105
      return p;
106
106
    }
107
107
  }
108
108
  /* Create a new plugin */
109
109
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if (new_plugin == NULL){
 
110
  if(new_plugin == NULL){
111
111
    return NULL;
112
112
  }
113
113
  char *copy_name = NULL;
124
124
                           .next = plugin_list };
125
125
  
126
126
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if (new_plugin->argv == NULL){
 
127
  if(new_plugin->argv == NULL){
128
128
    free(copy_name);
129
129
    free(new_plugin);
130
130
    return NULL;
230
230
      break;
231
231
    }
232
232
    if(pid == -1){
233
 
      if (errno != ECHILD){
 
233
      if(errno != ECHILD){
234
234
        perror("waitpid");
235
235
      }
236
236
      /* No child processes */
372
372
    { .name = NULL }
373
373
  };
374
374
  
375
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
376
 
                     struct argp_state *state) {
377
 
    switch (key) {
 
375
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
376
                    struct argp_state *state) {
 
377
    switch(key) {
378
378
    case 'g':                   /* --global-options */
379
 
      if (arg != NULL){
 
379
      if(arg != NULL){
380
380
        char *p;
381
381
        while((p = strsep(&arg, ",")) != NULL){
382
382
          if(p[0] == '\0'){
398
398
      }
399
399
      break;
400
400
    case 'o':                   /* --options-for */
401
 
      if (arg != NULL){
 
401
      if(arg != NULL){
402
402
        char *p_name = strsep(&arg, ":");
403
403
        if(p_name[0] == '\0' or arg == NULL){
404
404
          break;
435
435
      }
436
436
      break;
437
437
    case 'd':                   /* --disable */
438
 
      if (arg != NULL){
 
438
      if(arg != NULL){
439
439
        plugin *p = getplugin(arg);
440
440
        if(p == NULL){
441
441
          return ARGP_ERR_UNKNOWN;
444
444
      }
445
445
      break;
446
446
    case 'e':                   /* --enable */
447
 
      if (arg != NULL){
 
447
      if(arg != NULL){
448
448
        plugin *p = getplugin(arg);
449
449
        if(p == NULL){
450
450
          return ARGP_ERR_UNKNOWN;
463
463
      /* This is already done by parse_opt_config_file() */
464
464
      break;
465
465
    case 130:                   /* --userid */
466
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
466
      /* In the GNU C library, uid_t is always unsigned int */
 
467
      ret = sscanf(arg, "%u", &uid);
 
468
      if(ret != 1){
 
469
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
 
470
                uid);
 
471
      }
467
472
      break;
468
473
    case 131:                   /* --groupid */
469
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
474
      /* In the GNU C library, gid_t is always unsigned int */
 
475
      ret = sscanf(arg, "%u", &gid);
 
476
      if(ret != 1){
 
477
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
 
478
                arg, gid);
 
479
      }
470
480
      break;
471
481
    case 132:                   /* --debug */
472
482
      debug = true;
473
483
      break;
 
484
/*
 
485
 * When adding more options before this line, remember to also add a
 
486
 * "case" to the "parse_opt_config_file" function below.
 
487
 */
474
488
    case ARGP_KEY_ARG:
475
489
      /* Cryptsetup always passes an argument, which is an empty
476
490
         string if "none" was specified in /etc/crypttab.  So if
489
503
  
490
504
  /* This option parser is the same as parse_opt() above, except it
491
505
     ignores everything but the --config-file option. */
492
 
  error_t parse_opt_config_file (int key, char *arg,
493
 
                                 __attribute__((unused))
494
 
                                 struct argp_state *state) {
495
 
    switch (key) {
 
506
  error_t parse_opt_config_file(int key, char *arg,
 
507
                                __attribute__((unused))
 
508
                                struct argp_state *state) {
 
509
    switch(key) {
496
510
    case 'g':                   /* --global-options */
497
511
    case 'G':                   /* --global-env */
498
512
    case 'o':                   /* --options-for */
525
539
                       .args_doc = "",
526
540
                       .doc = "Mandos plugin runner -- Run plugins" };
527
541
  
528
 
  /* Parse using the parse_opt_config_file in order to get the custom
 
542
  /* Parse using parse_opt_config_file() in order to get the custom
529
543
     config file location, if any. */
530
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
531
 
  if (ret == ARGP_ERR_UNKNOWN){
 
544
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
545
  if(ret == ARGP_ERR_UNKNOWN){
532
546
    fprintf(stderr, "Unknown error while parsing arguments\n");
533
547
    exitstatus = EXIT_FAILURE;
534
548
    goto fallback;
538
552
  argp.parser = parse_opt;
539
553
  
540
554
  /* Open the configfile if available */
541
 
  if (argfile == NULL){
 
555
  if(argfile == NULL){
542
556
    conffp = fopen(AFILE, "r");
543
557
  } else {
544
558
    conffp = fopen(argfile, "r");
599
613
  } else {
600
614
    /* Check for harmful errors and go to fallback. Other errors might
601
615
       not affect opening plugins */
602
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
616
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
603
617
      perror("fopen");
604
618
      exitstatus = EXIT_FAILURE;
605
619
      goto fallback;
608
622
  /* If there was any arguments from configuration file,
609
623
     pass them to parser as command arguments */
610
624
  if(custom_argv != NULL){
611
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
612
 
                      0, NULL);
613
 
    if (ret == ARGP_ERR_UNKNOWN){
 
625
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
626
                     0, NULL);
 
627
    if(ret == ARGP_ERR_UNKNOWN){
614
628
      fprintf(stderr, "Unknown error while parsing arguments\n");
615
629
      exitstatus = EXIT_FAILURE;
616
630
      goto fallback;
619
633
  
620
634
  /* Parse actual command line arguments, to let them override the
621
635
     config file */
622
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
623
 
  if (ret == ARGP_ERR_UNKNOWN){
 
636
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
637
  if(ret == ARGP_ERR_UNKNOWN){
624
638
    fprintf(stderr, "Unknown error while parsing arguments\n");
625
639
    exitstatus = EXIT_FAILURE;
626
640
    goto fallback;
642
656
  
643
657
  /* Strip permissions down to nobody */
644
658
  ret = setuid(uid);
645
 
  if (ret == -1){
 
659
  if(ret == -1){
646
660
    perror("setuid");
647
661
  }  
648
662
  setgid(gid);
649
 
  if (ret == -1){
 
663
  if(ret == -1){
650
664
    perror("setgid");
651
665
  }
652
666
  
653
 
  if (plugindir == NULL){
 
667
  if(plugindir == NULL){
654
668
    dir = opendir(PDIR);
655
669
  } else {
656
670
    dir = opendir(plugindir);
683
697
    
684
698
    /* All directory entries have been processed */
685
699
    if(dirst == NULL){
686
 
      if (errno == EBADF){
 
700
      if(errno == EBADF){
687
701
        perror("readdir");
688
702
        exitstatus = EXIT_FAILURE;
689
703
        goto fallback;
749
763
    }
750
764
    
751
765
    ret = stat(filename, &st);
752
 
    if (ret == -1){
 
766
    if(ret == -1){
753
767
      perror("stat");
754
768
      free(filename);
755
769
      continue;
756
770
    }
757
771
 
758
772
    /* Ignore non-executable files */
759
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
773
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
760
774
      if(debug){
761
775
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
762
776
                " with bad type or mode\n", filename);
809
823
    
810
824
    int pipefd[2];
811
825
    ret = pipe(pipefd);
812
 
    if (ret == -1){
 
826
    if(ret == -1){
813
827
      perror("pipe");
814
828
      exitstatus = EXIT_FAILURE;
815
829
      goto fallback;
828
842
      goto fallback;
829
843
    }
830
844
    /* Block SIGCHLD until process is safely in process list */
831
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
845
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
832
846
    if(ret < 0){
833
847
      perror("sigprocmask");
834
848
      exitstatus = EXIT_FAILURE;
882
896
    close(pipefd[1]);           /* Close unused write end of pipe */
883
897
    free(filename);
884
898
    plugin *new_plugin = getplugin(dirst->d_name);
885
 
    if (new_plugin == NULL){
 
899
    if(new_plugin == NULL){
886
900
      perror("getplugin");
887
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
901
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
888
902
      if(ret < 0){
889
903
        perror("sigprocmask");
890
904
      }
897
911
    
898
912
    /* Unblock SIGCHLD so signal handler can be run if this process
899
913
       has already completed */
900
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
914
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
901
915
    if(ret < 0){
902
916
      perror("sigprocmask");
903
917
      exitstatus = EXIT_FAILURE;
906
920
    
907
921
    FD_SET(new_plugin->fd, &rfds_all);
908
922
    
909
 
    if (maxfd < new_plugin->fd){
 
923
    if(maxfd < new_plugin->fd){
910
924
      maxfd = new_plugin->fd;
911
925
    }
912
926
  }
929
943
  while(plugin_list){
930
944
    fd_set rfds = rfds_all;
931
945
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
932
 
    if (select_ret == -1){
 
946
    if(select_ret == -1){
933
947
      perror("select");
934
948
      exitstatus = EXIT_FAILURE;
935
949
      goto fallback;
954
968
                      (unsigned int) (proc->pid),
955
969
                      WTERMSIG(proc->status));
956
970
            } else if(WCOREDUMP(proc->status)){
957
 
              fprintf(stderr, "Plugin %d dumped core\n",
 
971
              fprintf(stderr, "Plugin %u dumped core\n",
958
972
                      (unsigned int) (proc->pid));
959
973
            }
960
974
          }
975
989
          proc = next_plugin;
976
990
          
977
991
          /* We are done modifying process list, so unblock signal */
978
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
 
                             NULL);
 
992
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
993
                            NULL);
980
994
          if(ret < 0){
981
995
            perror("sigprocmask");
982
996
            exitstatus = EXIT_FAILURE;
1011
1025
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1012
1026
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1013
1027
                               + (size_t) BUFFER_SIZE);
1014
 
        if (proc->buffer == NULL){
 
1028
        if(proc->buffer == NULL){
1015
1029
          perror("malloc");
1016
1030
          exitstatus = EXIT_FAILURE;
1017
1031
          goto fallback;