/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: 2015-07-12 01:57:54 UTC
  • Revision ID: teddy@recompile.se-20150712015754-1lq7u2ejo7fdyfvi
* debian/mandos.prerm: Don't run init script, use only invoke-rc.d.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 */
761
775
    goto fallback;
762
776
  }
763
777
  
 
778
  {
 
779
    char *pluginhelperenv;
 
780
    bool bret = true;
 
781
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
 
782
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
783
    if(ret != -1){
 
784
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
 
785
    }
 
786
    if(ret == -1 or not bret){
 
787
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
 
788
            " environment variable to \"%s\" for all plugins\n",
 
789
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
 
790
    }
 
791
    if(ret != -1){
 
792
      free(pluginhelperenv);
 
793
    }
 
794
  }
 
795
  
764
796
  if(debug){
765
797
    for(plugin *p = plugin_list; p != NULL; p=p->next){
766
798
      fprintf(stderr, "Plugin: %s has %d arguments\n",
887
919
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
888
920
    if(plugin_fd == -1){
889
921
      error(0, errno, "Could not open plugin");
 
922
      free(direntries[i]);
890
923
      continue;
891
924
    }
892
925
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
893
926
    if(ret == -1){
894
927
      error(0, errno, "stat");
895
928
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
929
      free(direntries[i]);
896
930
      continue;
897
931
    }
898
932
    
907
941
                direntries[i]->d_name);
908
942
      }
909
943
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
944
      free(direntries[i]);
910
945
      continue;
911
946
    }
912
947
    
914
949
    if(p == NULL){
915
950
      error(0, errno, "getplugin");
916
951
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
952
      free(direntries[i]);
917
953
      continue;
918
954
    }
919
955
    if(p->disabled){
922
958
                direntries[i]->d_name);
923
959
      }
924
960
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
961
      free(direntries[i]);
925
962
      continue;
926
963
    }
927
964
    {
960
997
    if(ret == -1){
961
998
      error(0, errno, "pipe");
962
999
      exitstatus = EX_OSERR;
 
1000
      free(direntries[i]);
963
1001
      goto fallback;
964
1002
    }
965
1003
    if(pipefd[0] >= FD_SETSIZE){
968
1006
      TEMP_FAILURE_RETRY(close(pipefd[0]));
969
1007
      TEMP_FAILURE_RETRY(close(pipefd[1]));
970
1008
      exitstatus = EX_OSERR;
 
1009
      free(direntries[i]);
971
1010
      goto fallback;
972
1011
    }
973
1012
#ifndef O_CLOEXEC
978
1017
      TEMP_FAILURE_RETRY(close(pipefd[0]));
979
1018
      TEMP_FAILURE_RETRY(close(pipefd[1]));
980
1019
      exitstatus = EX_OSERR;
 
1020
      free(direntries[i]);
981
1021
      goto fallback;
982
1022
    }
983
1023
    ret = set_cloexec_flag(pipefd[1]);
986
1026
      TEMP_FAILURE_RETRY(close(pipefd[0]));
987
1027
      TEMP_FAILURE_RETRY(close(pipefd[1]));
988
1028
      exitstatus = EX_OSERR;
 
1029
      free(direntries[i]);
989
1030
      goto fallback;
990
1031
    }
991
1032
#endif  /* not O_CLOEXEC */
996
1037
    if(ret < 0){
997
1038
      error(0, errno, "sigprocmask");
998
1039
      exitstatus = EX_OSERR;
 
1040
      free(direntries[i]);
999
1041
      goto fallback;
1000
1042
    }
1001
1043
    /* Starting a new process to be watched */
1010
1052
      TEMP_FAILURE_RETRY(close(pipefd[0]));
1011
1053
      TEMP_FAILURE_RETRY(close(pipefd[1]));
1012
1054
      exitstatus = EX_OSERR;
 
1055
      free(direntries[i]);
1013
1056
      goto fallback;
1014
1057
    }
1015
1058
    if(pid == 0){
1054
1097
        error(0, errno, "sigprocmask");
1055
1098
      }
1056
1099
      exitstatus = EX_OSERR;
 
1100
      free(direntries[i]);
1057
1101
      goto fallback;
1058
1102
    }
 
1103
    free(direntries[i]);
1059
1104
    
1060
1105
    new_plugin->pid = pid;
1061
1106
    new_plugin->fd = pipefd[0];
1323
1368
  free_plugin_list();
1324
1369
  
1325
1370
  free(plugindir);
 
1371
  free(pluginhelperdir);
1326
1372
  free(argfile);
1327
1373
  
1328
1374
  return exitstatus;