/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-28 14:22:10 UTC
  • Revision ID: teddy@recompile.se-20160228142210-2df89dih7mzxxr0h
Update copyright year.

* DBUS-API: Update copyright year.
* debian/copyright: - '' -
* intro.xml (COPYRIGHT): - '' -
* mandos: - '' -
* mandos-clients.conf.xml (COPYRIGHT): - '' -
* mandos-ctl: - '' -
* mandos-ctl.xml (COPYRIGHT): - '' -
* mandos-keygen: - '' -
* mandos-keygen.xml (COPYRIGHT): - '' -
* mandos-monitor: - '' -
* mandos-monitor.xml (COPYRIGHT): - '' -
* mandos.conf.xml (COPYRIGHT): - '' -
* mandos.xml (COPYRIGHT): - '' -
* plugin-helpers/mandos-client-iprouteadddel.c: - '' -
* plugin-runner.c: - '' -
* plugin-runner.xml (COPYRIGHT): - '' -
* plugins.d/askpass-fifo.c: - '' -
* plugins.d/askpass-fifo.xml (COPYRIGHT): - '' -
* plugins.d/mandos-client.c: - '' -
* plugins.d/mandos-client.xml (COPYRIGHT): - '' -
* plugins.d/password-prompt.c: - '' -
* plugins.d/password-prompt.xml (COPYRIGHT): - ''
* plugins.d/plymouth.c: - '' -
* plugins.d/plymouth.xml (COPYRIGHT): - '' -
* plugins.d/splashy.c: - '' -
* plugins.d/splashy.xml (COPYRIGHT): - '' -
* plugins.d/usplash.c: - '' -
* plugins.d/usplash.xml (COPYRIGHT): - '' -

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-2016 Teddy Hogeborn
 
6
 * Copyright © 2008-2016 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
 
  struct dirent **direntries;
 
354
  struct dirent **direntries = NULL;
353
355
  struct stat st;
354
356
  fd_set rfds_all;
355
357
  int ret, maxfd = 0;
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
  
829
860
    ret = set_cloexec_flag(dir_fd);
830
861
    if(ret < 0){
831
862
      error(0, errno, "set_cloexec_flag");
832
 
      TEMP_FAILURE_RETRY(close(dir_fd));
833
863
      exitstatus = EX_OSERR;
834
864
      goto fallback;
835
865
    }
875
905
#endif  /* not __GLIBC__ */
876
906
  if(numplugins == -1){
877
907
    error(0, errno, "Could not scan plugin dir");
878
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
908
    direntries = NULL;
879
909
    exitstatus = EX_OSERR;
880
910
    goto fallback;
881
911
  }
888
918
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
889
919
    if(plugin_fd == -1){
890
920
      error(0, errno, "Could not open plugin");
 
921
      free(direntries[i]);
891
922
      continue;
892
923
    }
893
924
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
894
925
    if(ret == -1){
895
926
      error(0, errno, "stat");
896
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
927
      close(plugin_fd);
 
928
      free(direntries[i]);
897
929
      continue;
898
930
    }
899
931
    
907
939
                plugindir != NULL ? plugindir : PDIR,
908
940
                direntries[i]->d_name);
909
941
      }
910
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
942
      close(plugin_fd);
 
943
      free(direntries[i]);
911
944
      continue;
912
945
    }
913
946
    
914
947
    plugin *p = getplugin(direntries[i]->d_name);
915
948
    if(p == NULL){
916
949
      error(0, errno, "getplugin");
917
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
950
      close(plugin_fd);
 
951
      free(direntries[i]);
918
952
      continue;
919
953
    }
920
954
    if(p->disabled){
922
956
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
923
957
                direntries[i]->d_name);
924
958
      }
925
 
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
959
      close(plugin_fd);
 
960
      free(direntries[i]);
926
961
      continue;
927
962
    }
928
963
    {
961
996
    if(ret == -1){
962
997
      error(0, errno, "pipe");
963
998
      exitstatus = EX_OSERR;
 
999
      free(direntries[i]);
964
1000
      goto fallback;
965
1001
    }
966
1002
    if(pipefd[0] >= FD_SETSIZE){
967
1003
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
968
1004
              FD_SETSIZE);
969
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
970
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1005
      close(pipefd[0]);
 
1006
      close(pipefd[1]);
971
1007
      exitstatus = EX_OSERR;
 
1008
      free(direntries[i]);
972
1009
      goto fallback;
973
1010
    }
974
1011
#ifndef O_CLOEXEC
976
1013
    ret = set_cloexec_flag(pipefd[0]);
977
1014
    if(ret < 0){
978
1015
      error(0, errno, "set_cloexec_flag");
979
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
980
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1016
      close(pipefd[0]);
 
1017
      close(pipefd[1]);
981
1018
      exitstatus = EX_OSERR;
 
1019
      free(direntries[i]);
982
1020
      goto fallback;
983
1021
    }
984
1022
    ret = set_cloexec_flag(pipefd[1]);
985
1023
    if(ret < 0){
986
1024
      error(0, errno, "set_cloexec_flag");
987
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
988
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1025
      close(pipefd[0]);
 
1026
      close(pipefd[1]);
989
1027
      exitstatus = EX_OSERR;
 
1028
      free(direntries[i]);
990
1029
      goto fallback;
991
1030
    }
992
1031
#endif  /* not O_CLOEXEC */
997
1036
    if(ret < 0){
998
1037
      error(0, errno, "sigprocmask");
999
1038
      exitstatus = EX_OSERR;
 
1039
      free(direntries[i]);
1000
1040
      goto fallback;
1001
1041
    }
1002
1042
    /* Starting a new process to be watched */
1008
1048
      error(0, errno, "fork");
1009
1049
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1010
1050
                                     &sigchld_action.sa_mask, NULL));
1011
 
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1012
 
      TEMP_FAILURE_RETRY(close(pipefd[1]));
 
1051
      close(pipefd[0]);
 
1052
      close(pipefd[1]);
1013
1053
      exitstatus = EX_OSERR;
 
1054
      free(direntries[i]);
1014
1055
      goto fallback;
1015
1056
    }
1016
1057
    if(pid == 0){
1042
1083
      /* no return */
1043
1084
    }
1044
1085
    /* Parent process */
1045
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1046
 
                                             pipe */
1047
 
    TEMP_FAILURE_RETRY(close(plugin_fd));
 
1086
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1087
    close(plugin_fd);
1048
1088
    plugin *new_plugin = getplugin(direntries[i]->d_name);
1049
1089
    if(new_plugin == NULL){
1050
1090
      error(0, errno, "getplugin");
1055
1095
        error(0, errno, "sigprocmask");
1056
1096
      }
1057
1097
      exitstatus = EX_OSERR;
 
1098
      free(direntries[i]);
1058
1099
      goto fallback;
1059
1100
    }
 
1101
    free(direntries[i]);
1060
1102
    
1061
1103
    new_plugin->pid = pid;
1062
1104
    new_plugin->fd = pipefd[0];
1092
1134
    }
1093
1135
  }
1094
1136
  
1095
 
  TEMP_FAILURE_RETRY(close(dir_fd));
 
1137
  free(direntries);
 
1138
  direntries = NULL;
 
1139
  close(dir_fd);
 
1140
  dir_fd = -1;
1096
1141
  free_plugin(getplugin(NULL));
1097
1142
  
1098
1143
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1292
1337
    free(custom_argv);
1293
1338
  }
1294
1339
  
 
1340
  free(direntries);
 
1341
  
1295
1342
  if(dir_fd != -1){
1296
 
    TEMP_FAILURE_RETRY(close(dir_fd));
 
1343
    close(dir_fd);
1297
1344
  }
1298
1345
  
1299
1346
  /* Kill the processes */
1319
1366
  free_plugin_list();
1320
1367
  
1321
1368
  free(plugindir);
 
1369
  free(pluginhelperdir);
1322
1370
  free(argfile);
1323
1371
  
1324
1372
  return exitstatus;