| 765 | | public class CPolicyContainer |
|---|
| 766 | | implements IResourceChangeListener |
|---|
| 767 | | { |
|---|
| 768 | | public static final int IF_FILE = 0; |
|---|
| 769 | | public static final int TE_FILE = 1; |
|---|
| 770 | | public static final int FC_FILE = 2; |
|---|
| 771 | | |
|---|
| 772 | | protected final String m_sModuleName; |
|---|
| 773 | | |
|---|
| 774 | | /** Three module IFiles */ |
|---|
| 775 | | protected IFile[] mModule; |
|---|
| 776 | | |
|---|
| 777 | | public CPolicyContainer(String i_sModuleName) |
|---|
| 778 | | { |
|---|
| 779 | | m_sModuleName = SELinuxPolicy.CUSTOM_PREFIX + i_sModuleName; |
|---|
| 780 | | init(); |
|---|
| 781 | | } |
|---|
| 782 | | |
|---|
| 783 | | void init() |
|---|
| 784 | | { |
|---|
| 785 | | IFolder layer = getDefaultLayer (); |
|---|
| 786 | | if (layer == null) |
|---|
| 787 | | return; |
|---|
| 788 | | |
|---|
| 789 | | String if_file = m_sModuleName + '.' + BaseProjectNature.IF_EXTENSION; |
|---|
| 790 | | String te_file = m_sModuleName + '.' + BaseProjectNature.TE_EXTENSION; |
|---|
| 791 | | String fc_file = m_sModuleName + '.' + BaseProjectNature.FC_EXTENSION; |
|---|
| 792 | | |
|---|
| 793 | | final IFile interface_file = layer.getFile (if_file); |
|---|
| 794 | | final IFile type_file = layer.getFile(te_file); |
|---|
| 795 | | final IFile file_context_file = layer.getFile (fc_file); |
|---|
| 796 | | |
|---|
| 797 | | if( interface_file.exists () && |
|---|
| 798 | | type_file.exists () && |
|---|
| 799 | | file_context_file.exists () ) |
|---|
| | 765 | public IFile[] getCustomModuleFiles() |
|---|
| | 766 | { |
|---|
| | 767 | IFolder layer = getDefaultLayer (); |
|---|
| | 768 | |
|---|
| | 769 | if (layer == null) |
|---|
| | 770 | return null; |
|---|
| | 771 | |
|---|
| | 772 | String module_name = SELinuxPolicy.CUSTOM_PREFIX + getName (); |
|---|
| | 773 | |
|---|
| | 774 | String if_filename = module_name + '.' + BaseProjectNature.IF_EXTENSION; |
|---|
| | 775 | String te_filename = module_name + '.' + BaseProjectNature.TE_EXTENSION; |
|---|
| | 776 | String fc_filename = module_name + '.' + BaseProjectNature.FC_EXTENSION; |
|---|
| | 777 | |
|---|
| | 778 | IFile if_file = layer.getFile (if_filename); |
|---|
| | 779 | IFile te_file = layer.getFile(te_filename); |
|---|
| | 780 | IFile fc_file = layer.getFile(fc_filename); |
|---|
| | 781 | |
|---|
| | 782 | if( if_file.exists () && te_file.exists () && fc_file.exists () ) |
|---|
| | 783 | { |
|---|
| | 784 | return new IFile []{if_file, te_file, fc_file}; |
|---|
| | 785 | } |
|---|
| | 786 | |
|---|
| | 787 | Object [][]array = |
|---|
| | 788 | { |
|---|
| | 789 | {if_file, |
|---|
| | 790 | "## <summary>\n" + |
|---|
| | 791 | "## Customizations for CDS Framework generated policy for " + getName () + "\n" + |
|---|
| | 792 | "## </summary>\n" + |
|---|
| | 793 | "## <desc>\n" + |
|---|
| | 794 | "## <p>\n" + |
|---|
| | 795 | "## Please only use these customizations when no other means of \n" + |
|---|
| | 796 | "## customizing your policy is working. Please use this as a \n" + |
|---|
| | 797 | "## last resort efort try to use custom dictionary entry, ability,\n" + |
|---|
| | 798 | "## base resource or base domain first.\n" + |
|---|
| | 799 | "## </p>\n" + |
|---|
| | 800 | "## </desc>\n" |
|---|
| | 801 | }, |
|---|
| | 802 | {te_file, "policy_module("+ module_name + "," + "1.0.0) \n\n"}, |
|---|
| | 803 | {fc_file, "\n"}, |
|---|
| | 804 | }; |
|---|
| | 805 | |
|---|
| | 806 | IFile []module_files = new IFile[array.length]; |
|---|
| | 807 | |
|---|
| | 808 | try |
|---|
| | 809 | { |
|---|
| | 810 | for( int i=0; i < module_files.length; i++) |
|---|
| 801 | | ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); |
|---|
| 802 | | mModule = new IFile[3]; |
|---|
| 803 | | mModule[IF_FILE] = interface_file; |
|---|
| 804 | | mModule[TE_FILE] = type_file; |
|---|
| 805 | | mModule[FC_FILE] = file_context_file; |
|---|
| 806 | | // final File changed_file = interface_file.getRawLocation ().toFile (); |
|---|
| | 812 | IFile file = (IFile)array[i][0]; |
|---|
| | 813 | InputStream stream = new ByteArrayInputStream (((String)array[i][1]).getBytes()); |
|---|
| | 814 | module_files[i] = file; |
|---|
| | 815 | //TODO::arp::run some test. |
|---|
| | 816 | if( module_files[i].exists() ) |
|---|
| | 817 | { |
|---|
| | 818 | module_files[i].appendContents (stream, IResource.KEEP_HISTORY, null); |
|---|
| | 819 | } |
|---|
| | 820 | else |
|---|
| | 821 | { |
|---|
| | 822 | module_files[i].create (stream, true, null); |
|---|
| | 823 | } |
|---|
| | 824 | } |
|---|
| | 825 | } |
|---|
| | 826 | catch(CoreException ce) |
|---|
| | 827 | { |
|---|
| | 828 | ce.printStackTrace (); |
|---|
| | 829 | return null; |
|---|
| | 830 | } |
|---|
| | 831 | return module_files; |
|---|
| | 832 | } |
|---|
| | 833 | |
|---|
| | 834 | public void resourceChanged (IResourceChangeEvent event) |
|---|
| | 835 | { |
|---|
| | 836 | IResourceDelta rootDelta = event.getDelta(); |
|---|
| | 837 | |
|---|
| | 838 | IPath path = getFolder ().getFullPath (); |
|---|
| | 839 | IResourceDelta interested_root = rootDelta.findMember(path); |
|---|
| | 840 | |
|---|
| | 841 | final String if_filename = getCustomModuleName () + "." + BaseProjectNature.IF_EXTENSION; |
|---|
| | 842 | |
|---|
| | 843 | IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() |
|---|
| | 844 | { |
|---|
| | 845 | public boolean visit(IResourceDelta delta) |
|---|
| | 846 | { |
|---|
| | 847 | //not interested in markers |
|---|
| | 848 | if ((delta.getFlags() & IResourceDelta.MARKERS) != 0) |
|---|
| | 849 | { |
|---|
| | 850 | return true; |
|---|
| | 851 | } |
|---|
| | 852 | |
|---|
| | 853 | if ((delta.getKind() & IResourceDelta.ADDED) == 0 && |
|---|
| | 854 | (delta.getKind() & IResourceDelta.CHANGED) == 0 && |
|---|
| | 855 | (delta.getKind() & IResourceDelta.REMOVED) == 0) |
|---|
| | 856 | { |
|---|
| | 857 | return true; |
|---|
| | 858 | } |
|---|
| 822 | | public IStatus run(IProgressMonitor monitor) |
|---|
| 823 | | { |
|---|
| 824 | | try |
|---|
| 825 | | { |
|---|
| 826 | | IProject proj = getProject (); |
|---|
| 827 | | if( proj == null || !proj.exists ()) |
|---|
| 828 | | return Status.CANCEL_STATUS; |
|---|
| | 898 | try |
|---|
| | 899 | { |
|---|
| | 900 | IProject proj = getProject (); |
|---|
| | 901 | if( proj == null || !proj.exists ()) |
|---|
| | 902 | return Status.CANCEL_STATUS; |
|---|
| | 903 | |
|---|
| | 904 | SLIDEProjectNature nat = SLIDEProjectNature.getNature(getProject ()); |
|---|
| | 905 | |
|---|
| | 906 | PolicyXMLContainer policyXML = nat.getPolicyXML (getFolder ()); |
|---|
| | 907 | if ( policyXML == null ) |
|---|
| | 908 | return new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), "Can not load policy"); |
|---|
| | 909 | |
|---|
| | 910 | IPolicyPartCreator part_creator = policyXML.getCreator (); |
|---|
| | 911 | if( part_creator == null ) |
|---|
| | 912 | return Status.CANCEL_STATUS; |
|---|
| | 913 | |
|---|
| | 914 | String if_filename = module_name + '.' + BaseProjectNature.IF_EXTENSION; |
|---|
| | 915 | |
|---|
| | 916 | IFile file = getFolder ().getFolder (CDS_LAYER).getFile (if_filename); |
|---|
| | 917 | if( file == null || !file.exists () ) |
|---|
| | 918 | return Status.CANCEL_STATUS; |
|---|
| 830 | | SLIDEProjectNature nat = SLIDEProjectNature.getNature(getProject ()); |
|---|
| 831 | | |
|---|
| 832 | | PolicyXMLContainer policyXML = nat.getPolicyXML (getFolder ()); |
|---|
| 833 | | if ( policyXML == null ) |
|---|
| 834 | | return new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), "Can not load policy"); |
|---|
| 835 | | |
|---|
| 836 | | if( mModule == null || mModule[IF_FILE] == null ) |
|---|
| 837 | | return Status.CANCEL_STATUS; |
|---|
| 838 | | |
|---|
| 839 | | IPath interfacePath = mModule[IF_FILE].getLocation (); |
|---|
| 840 | | File interfaceFile = interfacePath.toFile (); |
|---|
| 841 | | |
|---|
| 842 | | Map modified_templates = MacroParser.parseM4File (interfaceFile, MacroParser.TEMPLATE, policyXML.getCreator ()); |
|---|
| 843 | | CPolicyRefresher refresher = new CPolicyRefresher(modified_templates.keySet(), SELinuxPolicy.CUSTOM_PREFIX, SELinuxSystem.this.getName ()); |
|---|
| 844 | | getPolicy ().Accept(refresher); |
|---|
| 845 | | } |
|---|
| 846 | | catch (MacroParseException pe) |
|---|
| 847 | | { |
|---|
| 848 | | IStatus status = new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), pe.getLocalizedMessage ()); |
|---|
| 849 | | return status; |
|---|
| 850 | | } |
|---|
| 851 | | catch (FileNotFoundException fne) |
|---|
| 852 | | { |
|---|
| 853 | | IStatus status = new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), fne.getLocalizedMessage (), fne); |
|---|
| 854 | | return status; |
|---|
| 855 | | } |
|---|
| 856 | | return Status.OK_STATUS; |
|---|
| 857 | | } |
|---|
| 858 | | }; |
|---|
| 859 | | parseFileJob.schedule(); |
|---|
| 860 | | } |
|---|
| 861 | | |
|---|
| 862 | | public IFile[] getCustomModuleFiles() |
|---|
| 863 | | { |
|---|
| 864 | | IFolder layer = getDefaultLayer (); |
|---|
| 865 | | if (layer == null) |
|---|
| 866 | | return null; |
|---|
| 867 | | if (mModule != null) |
|---|
| 868 | | return mModule; |
|---|
| 869 | | |
|---|
| 870 | | String if_file = m_sModuleName + '.' + BaseProjectNature.IF_EXTENSION; |
|---|
| 871 | | String te_file = m_sModuleName + '.' + BaseProjectNature.TE_EXTENSION; |
|---|
| 872 | | String fc_file = m_sModuleName + '.' + BaseProjectNature.FC_EXTENSION; |
|---|
| 873 | | |
|---|
| 874 | | Object [][]array = |
|---|
| 875 | | { |
|---|
| 876 | | {layer.getFile (if_file), |
|---|
| 877 | | "## <summary>\n" + |
|---|
| 878 | | "## Customizations for CDS Framework generated policy for " + getName () + "\n" + |
|---|
| 879 | | "## </summary>\n" + |
|---|
| 880 | | "## <desc>\n" + |
|---|
| 881 | | "## <p>\n" + |
|---|
| 882 | | "## Please only use these customizations when no other means of \n" + |
|---|
| 883 | | "## customizing your policy is working. Please use this as a \n" + |
|---|
| 884 | | "## last resort efort try to use custom dictionary entry, ability,\n" + |
|---|
| 885 | | "## base resource or base domain first.\n" + |
|---|
| 886 | | "## </p>\n" + |
|---|
| 887 | | "## </desc>\n" |
|---|
| 888 | | }, |
|---|
| 889 | | {layer.getFile (te_file), "policy_module("+ m_sModuleName + "," + "1.0.0) \n\n"}, |
|---|
| 890 | | {layer.getFile (fc_file), "\n"}, |
|---|
| 891 | | }; |
|---|
| 892 | | |
|---|
| 893 | | try |
|---|
| 894 | | { |
|---|
| 895 | | mModule = new IFile[array.length]; |
|---|
| 896 | | for( int i=0; i < mModule.length; i++) |
|---|
| 897 | | { |
|---|
| 898 | | IFile file = (IFile)array[i][0]; |
|---|
| 899 | | InputStream stream = new ByteArrayInputStream (((String)array[i][1]).getBytes()); |
|---|
| 900 | | mModule[i] = file; |
|---|
| 901 | | //TODO::arp::run some test. |
|---|
| 902 | | if( mModule[i].exists() ) |
|---|
| 903 | | { |
|---|
| 904 | | mModule[i].appendContents (stream, IResource.KEEP_HISTORY, null); |
|---|
| 905 | | } |
|---|
| 906 | | else |
|---|
| 907 | | { |
|---|
| 908 | | mModule[i].create (stream, true, null); |
|---|
| 909 | | } |
|---|
| 910 | | } |
|---|
| | 920 | IPath interfacePath = file.getLocation (); |
|---|
| | 921 | File interfaceFile = interfacePath.toFile (); |
|---|
| | 922 | |
|---|
| | 923 | if( !interfaceFile.canRead () ) |
|---|
| | 924 | return Status.CANCEL_STATUS; |
|---|
| | 925 | Map modified_templates = MacroParser.parseM4File (interfaceFile, MacroParser.TEMPLATE, part_creator ); |
|---|
| | 926 | CPolicyRefresher refresher = new CPolicyRefresher(modified_templates.keySet(), SELinuxPolicy.CUSTOM_PREFIX, SELinuxSystem.this.getName ()); |
|---|
| | 927 | getPolicy ().Accept(refresher); |
|---|
| | 928 | } |
|---|
| | 929 | catch (MacroParseException pe) |
|---|
| | 930 | { |
|---|
| | 931 | IStatus status = new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), pe.getLocalizedMessage ()); |
|---|
| | 932 | return status; |
|---|
| | 933 | } |
|---|
| | 934 | catch (FileNotFoundException fne) |
|---|
| | 935 | { |
|---|
| | 936 | IStatus status = new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), fne.getLocalizedMessage (), fne); |
|---|
| | 937 | return status; |
|---|
| | 938 | } |
|---|
| | 939 | return Status.OK_STATUS; |
|---|
| 912 | | catch(CoreException ce) |
|---|
| 913 | | { |
|---|
| 914 | | ce.printStackTrace (); |
|---|
| 915 | | mModule = null; |
|---|
| 916 | | return mModule; |
|---|
| 917 | | } |
|---|
| 918 | | ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); |
|---|
| 919 | | return mModule; |
|---|
| 920 | | } |
|---|
| 921 | | |
|---|
| 922 | | public void resourceChanged (IResourceChangeEvent event) |
|---|
| 923 | | { |
|---|
| 924 | | IResourceDelta rootDelta = event.getDelta(); |
|---|
| 925 | | |
|---|
| 926 | | if (mModule == null) |
|---|
| 927 | | return; |
|---|
| 928 | | |
|---|
| 929 | | IPath path = mModule[0].getFullPath(); |
|---|
| 930 | | IResourceDelta interested_root = rootDelta.findMember(path); |
|---|
| 931 | | |
|---|
| 932 | | IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() |
|---|
| 933 | | { |
|---|
| 934 | | public boolean visit(IResourceDelta delta) |
|---|
| 935 | | { |
|---|
| 936 | | //not interested in markers |
|---|
| 937 | | if ((delta.getFlags() & IResourceDelta.MARKERS) != 0) |
|---|
| 938 | | { |
|---|
| 939 | | return true; |
|---|
| 940 | | } |
|---|
| 941 | | |
|---|
| 942 | | if ((delta.getKind() & IResourceDelta.ADDED) == 0 && |
|---|
| 943 | | (delta.getKind() & IResourceDelta.CHANGED) == 0 && |
|---|
| 944 | | (delta.getKind() & IResourceDelta.REMOVED) == 0) |
|---|
| 945 | | { |
|---|
| 946 | | return true; |
|---|
| 947 | | } |
|---|
| 948 | | |
|---|
| 949 | | IResource resource = delta.getResource(); |
|---|
| 950 | | if (resource.getType() == IResource.FILE |
|---|
| 951 | | && resource.getName ().equals (mModule[IF_FILE].getName ())) |
|---|
| 952 | | { |
|---|
| 953 | | if( delta.getKind () == IResourceDelta.REMOVED ) |
|---|
| 954 | | { |
|---|
| 955 | | dispose (); |
|---|
| 956 | | return false; |
|---|
| 957 | | } |
|---|
| 958 | | refreshPolicy (); |
|---|
| 959 | | } |
|---|
| 960 | | return true; |
|---|
| 961 | | } |
|---|
| 962 | | }; |
|---|
| 963 | | try |
|---|
| 964 | | { |
|---|
| 965 | | if( interested_root != null) |
|---|
| 966 | | interested_root.accept(visitor); |
|---|
| 967 | | } |
|---|
| 968 | | catch (CoreException e) |
|---|
| 969 | | { |
|---|
| 970 | | e.printStackTrace(); |
|---|
| 971 | | } |
|---|
| 972 | | } |
|---|
| | 941 | }; |
|---|
| | 942 | parseFileJob.schedule(); |
|---|