In progress of refining the Tomoyo policies for process jailing; will define a set...
[iot2.git] / iotjava / iotruntime / master / IoTMaster.java
index ce99125..8be2fe4 100644 (file)
@@ -51,6 +51,7 @@ public class IoTMaster {
        private CommunicationHandler commHan;
        private LoadBalancer lbIoT;
        private RouterConfig routerConfig;
+       private ProcessJailConfig processJailConfig;
        private ObjectInitHandler objInitHand;
        private ObjectAddressInitHandler objAddInitHand;
        private String[] strObjectNames;
@@ -112,6 +113,7 @@ public class IoTMaster {
        private static final String STR_CFG_FILE_EXT = ".config";
        private static final String STR_CLS_FILE_EXT = ".class";
        private static final String STR_JAR_FILE_EXT = ".jar";
+       private static final String STR_MAC_POLICY_EXT = ".tomoyo.pol"; 
        private static final String STR_SHELL_FILE_EXT = ".sh";
        private static final String STR_SO_FILE_EXT = ".so";
        private static final String STR_ZIP_FILE_EXT = ".zip";
@@ -133,6 +135,7 @@ public class IoTMaster {
        private static final String STR_IOTSLAVE_CPP = "./IoTSlave.o";
        private static final String STR_SHELL_HEADER = "#!/bin/sh";
        private static final String STR_JAVA_PATH = "/usr/bin/java";
+       private static final String STR_MAC_POL_PATH = "tomoyo/";
 
        private static int INT_SIZE = 4;        // send length in the size of integer (4 bytes)
 
@@ -155,6 +158,7 @@ public class IoTMaster {
                commHan = null;
                lbIoT = null;
                routerConfig = null;
+               processJailConfig = null;
                objInitHand = null;
                objAddInitHand = null;
                strObjectNames = argObjNms;
@@ -208,6 +212,8 @@ public class IoTMaster {
                lbIoT.setupLoadBalancer();
                routerConfig = new RouterConfig();
                routerConfig.getAddressList(STR_ROUTER_ADD);
+               processJailConfig = new ProcessJailConfig();
+               //processJailConfig.setAddressListObject(routerConfig.getAddressListObject());
                objInitHand = new ObjectInitHandler(BOOL_VERBOSE);
                objAddInitHand = new ObjectAddressInitHandler(BOOL_VERBOSE);
                mapClassNameToCrim = new HashMap<String,Object>();
@@ -577,10 +583,11 @@ public class IoTMaster {
         * A private method to instrument an object on a specific machine and setting up policies
         *
         * @params  strFieldObjectID              String field object ID
+        * @params  strObjControllerName          String object controller name
         * @params  strLanguage                           String language
         * @return  void
         */
-       private void instrumentObject(String strFieldObjectID, String strLanguage) throws IOException {
+       private void instrumentObject(String strFieldObjectID, String strObjControllerName, String strLanguage) throws IOException {
 
                // Extract the interface name for RMI
                // e.g. ProximitySensorInterface, TempSensorInterface, etc.
@@ -625,6 +632,13 @@ public class IoTMaster {
                                strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
                        routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveControllerHostAdd,
                                strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
+                       // Configure MAC policies for objects
+                       String strFileName = STR_MAC_POL_PATH + strObjClassName + STR_MAC_POLICY_EXT;
+                       processJailConfig.configureProcessJailDeviceDriverPolicies(strIoTSlaveObjectHostAdd, strObjName, 
+                               strFileName, strIoTMasterHostAdd, commHan.getComPort(strObjName), commHan.getRMIRegPort(strObjName), 
+                               commHan.getRMIStubPort(strObjName));
+                       processJailConfig.configureProcessJailContRMIPolicies(strObjControllerName, strIoTSlaveObjectHostAdd, 
+                               commHan.getRMIRegPort(strObjName), commHan.getRMIStubPort(strObjName));
                        // Instrument the IoTSet declarations inside the class file
                        instrumentObjectIoTSet(strFieldObjectID, strLanguage);
                }
@@ -722,6 +736,9 @@ public class IoTMaster {
                                        commHan.getComPort(strDeviceAddressKey), iDestDeviceDriverPort);
                                routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveObjectHostAdd, strDeviceAddress, strProtocol, 
                                        commHan.getComPort(strDeviceAddressKey), iDestDeviceDriverPort);
+                               // Configure MAC policies
+                               processJailConfig.configureProcessJailDevicePolicies(strIoTSlaveObjectHostAdd, strProtocol,
+                                       commHan.getComPort(strDeviceAddressKey), strDeviceAddress, iDestDeviceDriverPort);
                        }
                }
        }
@@ -891,14 +908,6 @@ public class IoTMaster {
         *
         * @return       String
         */
-       /*private String getCmdJavaDriverIoTSlave(String strIoTMasterHostAdd, String strIoTSlaveObjectHostAdd, String strObjName) {
-
-               return STR_SSH + " " + STR_USERNAME + strIoTSlaveObjectHostAdd + " cd " + STR_RUNTIME_DIR + " sudo java " +
-                       STR_CLS_PATH + " " + STR_RMI_PATH + " " + STR_RMI_HOSTNAME +
-                       strIoTSlaveObjectHostAdd + " " + STR_IOT_SLAVE_CLS + " " + strIoTMasterHostAdd + " " +
-                       commHan.getComPort(strObjName) + " " + commHan.getRMIRegPort(strObjName) + " " +
-                       commHan.getRMIStubPort(strObjName) + " >& " + STR_LOG_FILE_PATH + strObjName + ".log &";
-       }*/
        private String getCmdJavaDriverIoTSlave(String strIoTMasterHostAdd, String strIoTSlaveObjectHostAdd, String strObjName) {
 
                // Create an Shell executable
@@ -1153,7 +1162,7 @@ public class IoTMaster {
         * @params  strLanguage                           String language
         * @return  void
         */
-       private void instrumentIoTSet(Map.Entry<String,Object> map, String strFieldName, String strLanguage) 
+       private void instrumentIoTSet(Map.Entry<String,Object> map, String strFieldName, String strObjControllerName, String strLanguage) 
                throws IOException, ClassNotFoundException, InterruptedException {
                                
                // Get information from the set
@@ -1169,7 +1178,7 @@ public class IoTMaster {
                        String strObjID = setInstrumenter.fieldObjectID(iRow);
                        strObjClassName = setInstrumenter.fieldEntryType(strObjID);
                        // Call the method to create an object
-                       instrumentObject(strObjID, strLanguage);
+                       instrumentObject(strObjID, strObjControllerName, strLanguage);
                        objInitHand.addObjectIntoField(strFieldName, strIoTSlaveObjectHostAdd, strObjName,
                                strObjClassName, strObjClassInterfaceName, strObjStubClsIntfaceName, commHan.getRMIRegPort(strObjName), 
                                commHan.getRMIStubPort(strObjName));
@@ -1185,7 +1194,7 @@ public class IoTMaster {
         * @params  strLanguage                           String language
         * @return  void
         */
-       private void instrumentIoTRelation(Map.Entry<String,Object> map, String strFieldName, String strLanguage) 
+       private void instrumentIoTRelation(Map.Entry<String,Object> map, String strFieldName, String strObjControllerName, String strLanguage) 
                throws IOException, ClassNotFoundException, InterruptedException {
 
                        // Get information from the set
@@ -1200,7 +1209,7 @@ public class IoTMaster {
                        String strObjID = relationInstrumenter.firstFieldObjectID(iRow);
                        strObjClassName = relationInstrumenter.firstEntryFieldType(strObjID);
                        // Call the method to create an object
-                       instrumentObject(strObjID, strLanguage);
+                       instrumentObject(strObjID, strObjControllerName, strLanguage);
                        // Get the first object controller host address
                        String strFirstIoTSlaveObjectHostAdd = strIoTSlaveObjectHostAdd;
                        objInitHand.addObjectIntoField(strFieldName, strIoTSlaveObjectHostAdd, strObjName,
@@ -1212,7 +1221,7 @@ public class IoTMaster {
                        strObjID = relationInstrumenter.secondFieldObjectID(iRow);
                        strObjClassName = relationInstrumenter.secondEntryFieldType(strObjID);
                        // Call the method to create an object
-                       instrumentObject(strObjID, strLanguage);
+                       instrumentObject(strObjID, strObjControllerName, strLanguage);
                        // Get the second object controller host address
                        String strSecondIoTSlaveObjectHostAdd = strIoTSlaveObjectHostAdd;
                        objInitHand.addSecondObjectIntoField(strFieldName, strIoTSlaveObjectHostAdd, strObjName,
@@ -1425,6 +1434,39 @@ public class IoTMaster {
                }
        }
 
+       /**
+        * A method to create a thread for policy deployment
+        *
+        * @param  setHostAddresses             Set of strings for host addresses to configure
+        * @return                              void
+        */
+       private void createMACPolicyThreads(Set<String> setHostAddresses) throws IOException {
+
+               // Create a list of threads
+               List<Thread> threads = new ArrayList<Thread>();
+               // Start threads for hosts
+               for(String strAddress : setHostAddresses) {
+                       Thread policyThread = new Thread(new Runnable() {
+                               public void run() {
+                                       synchronized(this) {
+                                               processJailConfig.sendMACPolicies(strAddress);
+                                       }
+                               }
+                       });
+                       threads.add(policyThread);
+                       policyThread.start();
+                       RuntimeOutput.print("Deploying MAC policies for: " + strAddress, BOOL_VERBOSE);
+               }
+               // Join all threads
+               for (Thread thread : threads) {
+                       try {
+                               thread.join();
+                       } catch (InterruptedException ex) {
+                               ex.printStackTrace();
+                       }
+               }
+       }
+
 
        /**
         * A method to send files to Java IoTSlave
@@ -1552,17 +1594,6 @@ public class IoTMaster {
         *
         * @return       String
         */
-       /*private String getCmdJavaIoTSlave(String strObjControllerName) {
-
-               return STR_SSH + " " + STR_USERNAME + strIoTSlaveControllerHostAdd + " cd " +
-                                       STR_RUNTIME_DIR + " sudo java " + STR_JVM_INIT_HEAP_SIZE + " " + 
-                                       STR_JVM_MAX_HEAP_SIZE + " " + STR_CLS_PATH + " " +
-                                       STR_RMI_PATH + " " + STR_IOT_SLAVE_CLS + " " + strIoTMasterHostAdd + " " +
-                                       commHan.getComPort(strObjControllerName) + " " +
-                                       commHan.getRMIRegPort(strObjControllerName) + " " +
-                                       commHan.getRMIStubPort(strObjControllerName) + " >& " +
-                                       STR_LOG_FILE_PATH + strObjControllerName + ".log &";
-       }*/
        private String getCmdJavaIoTSlave(String strObjControllerName) {
 
                // Create an Shell executable
@@ -2018,7 +2049,10 @@ public class IoTMaster {
                                        sendFileToCppSlave(strControllerFilePath, strControllerZipFile);
                                        createMainObjectCpp(strObjControllerName, outStream, inStream);
                                }
-
+                               // Write basic MAC policies for controller
+                               String strFileName = STR_MAC_POL_PATH + strObjControllerName + STR_MAC_POLICY_EXT;
+                               processJailConfig.configureProcessJailControllerPolicies(strObjControllerName, strFileName, 
+                                       strIoTMasterHostAdd, commHan.getComPort(strObjControllerName));
                                // PROFILING
                                result = System.currentTimeMillis()-start;
                                System.out.println("\n\n ==> From IoTSlave start until main controller object is created: " + result);
@@ -2069,12 +2103,17 @@ public class IoTMaster {
                                                        instrumentIoTSetAddress(strFieldName, strFieldName, inStream, outStream, STR_LANGUAGE_CONTROLLER);
                                                } else {
                                                // Any other cases
-                                                       instrumentIoTSet(map, strFieldName, STR_LANGUAGE_CONTROLLER);
+                                                       instrumentIoTSet(map, strFieldName, strObjControllerName, STR_LANGUAGE_CONTROLLER);
                                                }
                                        } else if (strClassName.equals(STR_REL_INSTRUMENTER_CLS)) {
-                                               instrumentIoTRelation(map, strFieldName, STR_LANGUAGE_CONTROLLER);
+                                               instrumentIoTRelation(map, strFieldName, strObjControllerName, STR_LANGUAGE_CONTROLLER);
                                        }
                                }
+                               // Combine controller MAC policies with the main policy file for the host
+                               String strTempFileName = "./" + strObjControllerName + STR_MAC_POLICY_EXT;
+                               processJailConfig.combineControllerMACPolicies(strIoTSlaveControllerHostAdd, strObjControllerName, strTempFileName);
+                               processJailConfig.close();
+
                                // PROFILING
                                result = System.currentTimeMillis()-start;
                                System.out.println("\n\n ==> Time needed to instrument device driver objects: " + result + "\n\n");
@@ -2143,6 +2182,7 @@ public class IoTMaster {
                                serverSocket.close();
                                commHan.printLists();
                                lbIoT.printHostInfo();
+                               createMACPolicyThreads(setAddresses);
                        }
 
                } catch (IOException          |