/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: 2016-02-21 12:43:51 UTC
  • Revision ID: teddy@recompile.se-20160221124351-3g9bc5eu90at8yis
plugin-runner.c: Use size_t to realloc() for custom plugin args.

* plugin-runner.c (main): Cast argument to realloc() to size_t when
                          allocating additional custom plugin command
                          line arguments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008-2014 Teddy Hogeborn
6
 
 * Copyright © 2008-2014 Björn Påhlsson
 
5
 * Copyright © 2008-2015 Teddy Hogeborn
 
6
 * Copyright © 2008-2015 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
76
76
#define BUFFER_SIZE 256
77
77
 
78
78
#define PDIR "/lib/mandos/plugins.d"
 
79
#define PHDIR "/lib/mandos/plugin-helpers"
79
80
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
80
81
 
81
82
const char *argp_program_version = "plugin-runner " VERSION;
347
348
 
348
349
int main(int argc, char *argv[]){
349
350
  char *plugindir = NULL;
 
351
  char *pluginhelperdir = NULL;
350
352
  char *argfile = NULL;
351
353
  FILE *conffp;
352
354
  struct dirent **direntries = NULL;
414
416
      .doc = "Group ID the plugins will run as", .group = 3 },
415
417
    { .name = "debug", .key = 132,
416
418
      .doc = "Debug mode", .group = 4 },
 
419
    { .name = "plugin-helper-dir", .key = 133,
 
420
      .arg = "DIRECTORY",
 
421
      .doc = "Specify a different plugin helper directory",
 
422
      .group = 2 },
417
423
    /*
418
424
     * These reproduce what we would get without ARGP_NO_HELP
419
425
     */
545
551
    case 132:                   /* --debug */
546
552
      debug = true;
547
553
      break;
 
554
    case 133:                   /* --plugin-helper-dir */
 
555
      free(pluginhelperdir);
 
556
      pluginhelperdir = strdup(arg);
 
557
      if(pluginhelperdir != NULL){
 
558
        errno = 0;
 
559
      }
 
560
      break;
548
561
      /*
549
562
       * These reproduce what we would get without ARGP_NO_HELP
550
563
       */
601
614
    case 130:                   /* --userid */
602
615
    case 131:                   /* --groupid */
603
616
    case 132:                   /* --debug */
 
617
    case 133:                   /* --plugin-helper-dir */
604
618
    case '?':                   /* --help */
605
619
    case -3:                    /* --usage */
606
620
    case 'V':                   /* --version */
687
701
        custom_argc += 1;
688
702
        {
689
703
          char **new_argv = realloc(custom_argv, sizeof(char *)
690
 
                                    * ((unsigned int)
691
 
                                       custom_argc + 1));
 
704
                                    * ((size_t)custom_argc + 1));
692
705
          if(new_argv == NULL){
693
706
            error(0, errno, "realloc");
694
707
            exitstatus = EX_OSERR;
761
774
    goto fallback;
762
775
  }
763
776
  
 
777
  {
 
778
    char *pluginhelperenv;
 
779
    bool bret = true;
 
780
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
 
781
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
782
    if(ret != -1){
 
783
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
 
784
    }
 
785
    if(ret == -1 or not bret){
 
786
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
 
787
            " environment variable to \"%s\" for all plugins\n",
 
788
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
789
    }
 
790
    if(ret != -1){
 
791
      free(pluginhelperenv);
 
792
    }
 
793
  }
 
794
  
764
795
  if(debug){
765
796
    for(plugin *p = plugin_list; p != NULL; p=p->next){
766
797
      fprintf(stderr, "Plugin: %s has %d arguments\n",
795
826
          }
796
827
        }
797
828
      }
798
 
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
829
      close(plugindir_fd);
799
830
    }
800
831
  }
801
832
  
887
918
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
888
919
    if(plugin_fd == -1){
889
920
      error(0, errno, "Could not open plugin");
 
921
      free(direntries[i]);
890
922
      continue;
891
923
    }
892
924
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
893
925
    if(ret == -1){
894
926
      error(0, errno, "stat");
895
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
927
      close(plugin_fd);
 
928
      free(direntries[i]);
896
929
      continue;
897
930
    }
898
931
    
906
939
                plugindir != NULL ? plugindir : PDIR,
907
940
                direntries[i]->d_name);
908
941
      }
909
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
942
      close(plugin_fd);
 
943
      free(direntries[i]);
910
944
      continue;
911
945
    }
912
946
    
913
947
    plugin *p = getplugin(direntries[i]->d_name);
914
948
    if(p == NULL){
915
949
      error(0, errno, "getplugin");
916
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
950
      close(plugin_fd);
 
951
      free(direntries[i]);
917
952
      continue;
918
953
    }
919
954
    if(p->disabled){
921
956
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
922
957
                direntries[i]->d_name);
923
958
      }
924
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
959
      close(plugin_fd);
 
960
      free(direntries[i]);
925
961
      continue;
926
962
    }
927
963
    {
960
996
    if(ret == -1){
961
997
      error(0, errno, "pipe");
962
998
      exitstatus = EX_OSERR;
 
999
      free(direntries[i]);
963
1000
      goto fallback;
964
1001
    }
965
1002
    if(pipefd[0] >= FD_SETSIZE){
966
1003
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
967
1004
              FD_SETSIZE);
968
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
969
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1005
      close(pipefd[0]);
 
1006
      close(pipefd[1]);
970
1007
      exitstatus = EX_OSERR;
 
1008
      free(direntries[i]);
971
1009
      goto fallback;
972
1010
    }
973
1011
#ifndef O_CLOEXEC
975
1013
    ret = set_cloexec_flag(pipefd[0]);
976
1014
    if(ret < 0){
977
1015
      error(0, errno, "set_cloexec_flag");
978
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
979
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1016
      close(pipefd[0]);
 
1017
      close(pipefd[1]);
980
1018
      exitstatus = EX_OSERR;
 
1019
      free(direntries[i]);
981
1020
      goto fallback;
982
1021
    }
983
1022
    ret = set_cloexec_flag(pipefd[1]);
984
1023
    if(ret < 0){
985
1024
      error(0, errno, "set_cloexec_flag");
986
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
987
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1025
      close(pipefd[0]);
 
1026
      close(pipefd[1]);
988
1027
      exitstatus = EX_OSERR;
 
1028
      free(direntries[i]);
989
1029
      goto fallback;
990
1030
    }
991
1031
#endif  /* not O_CLOEXEC */
996
1036
    if(ret < 0){
997
1037
      error(0, errno, "sigprocmask");
998
1038
      exitstatus = EX_OSERR;
 
1039
      free(direntries[i]);
999
1040
      goto fallback;
1000
1041
    }
1001
1042
    /* Starting a new process to be watched */
1007
1048
      error(0, errno, "fork");
1008
1049
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1009
1050
                                     &sigchld_action.sa_mask, NULL));
1010
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1011
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1051
      close(pipefd[0]);
 
1052
      close(pipefd[1]);
1012
1053
      exitstatus = EX_OSERR;
 
1054
      free(direntries[i]);
1013
1055
      goto fallback;
1014
1056
    }
1015
1057
    if(pid == 0){
1041
1083
      /* no return */
1042
1084
    }
1043
1085
    /* Parent process */
1044
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1045
 
                                             pipe */
1046
 
    TEMP_FAILURE_RETRY(close(plugin_fd));
 
1086
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1087
    close(plugin_fd);
1047
1088
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1048
1089
    if(new_plugin == NULL){
1049
1090
      error(0, errno, "getplugin");
1054
1095
        error(0, errno, "sigprocmask");
1055
1096
      }
1056
1097
      exitstatus = EX_OSERR;
 
1098
      free(direntries[i]);
1057
1099
      goto fallback;
1058
1100
    }
 
1101
    free(direntries[i]);
1059
1102
    
1060
1103
    new_plugin->pid = pid;
1061
1104
    new_plugin->fd = pipefd[0];
1093
1136
  
1094
1137
  free(direntries);
1095
1138
  direntries = NULL;
1096
 
  TEMP_FAILURE_RETRY(close(dir_fd));
 
1139
  close(dir_fd);
1097
1140
  dir_fd = -1;
1098
1141
  free_plugin(getplugin(NULL));
1099
1142
  
1297
1340
  free(direntries);
1298
1341
  
1299
1342
  if(dir_fd != -1){
1300
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
1343
    close(dir_fd);
1301
1344
  }
1302
1345
  
1303
1346
  /* Kill the processes */
1323
1366
  free_plugin_list();
1324
1367
  
1325
1368
  free(plugindir);
 
1369
  free(pluginhelperdir);
1326
1370
  free(argfile);
1327
1371
  
1328
1372
  return exitstatus;