package iotruntime.master; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.PrintWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; /** Class ProcessJailConfig is a class that configures the compute * nodes in our network with the relevant process jail policies; *
* We use Tomoyo 2.5 as a Mandatory Access Control (MAC) that is
* simple, easy to maintain, and lightweight (suitable for embedded
* devices).
*
* @author Rahmadi Trimananda
* This method gets the mapping from RouterConfig
*/
public void setAddressListObject(Map
* This method sends an inquiry to the router to look for
* the list of DHCP leased addresses and their mapping to MAC
* addresses
*
* @param strRouterAddress String that contains address of router
*/
public void getAddressList(String strRouterAddress) {
//HashMap
* This method configures the main policies between controller and device driver
*
* @param strConfigHost String hostname to be configured
* @param strObjectName String object name
* @param strObjectClassName String object class name
* @param strFileName String policy file path and name
* @param strMasterIPAddress String master IP address
* @param iComPort Integer communication port (controller-driver)
* @param iRMIRegPort Integer RMI registry port
* @param iRMIStubPort Integer RMI stub port
* @return void
*/
public void configureProcessJailDeviceDriverPolicies(String strConfigHost, String strObjectName, String strObjectClassName,
String strFileName, String strMasterIPAddress, int iComPort, int iRMIRegPort, int iRMIStubPort) {
PrintWriter pwConfig = getPrintWriter(strConfigHost);
String strPolicyList = readFile(strFileName);
// Replace the strings with the actual values
String strNewPolicyList = strPolicyList.replace(STR_OBJECT_NAME, strObjectName).
replace(STR_OBJECT_CLASS_NAME, strObjectClassName).
replace(STR_MASTER_IP_ADDRESS, strMasterIPAddress).
replace(STR_MASTER_COM_PORT, String.valueOf(iComPort));
//replace(STR_RMI_REG_PORT, String.valueOf(iRMIRegPort)).
//replace(STR_RMI_STUB_PORT, String.valueOf(iRMIStubPort));
pwConfig.println("\n");
pwConfig.print(strNewPolicyList);
pwConfig.println("network inet stream bind/listen :: " + iRMIRegPort);
pwConfig.println("network inet stream bind/listen :: " + iRMIStubPort);
}
/**
* configureProcessJailDevicePolicies() method configures the device MAC policies
*
* This method configures the device policies between device driver and device
*
* @param strConfigHost String hostname to be configured
* @param strProtocol String protocol name
* @param iDeviceComPort Integer device communication port
* @param strDeviceIPAddress String device IP address
* @param iDevicePort Integer device port
* @return void
*/
public void configureProcessJailDevicePolicies(String strConfigHost, String strProtocol,
int iDeviceComPort, String strDeviceIPAddress, int iDevicePort) {
PrintWriter pwConfig = getPrintWriter(strConfigHost);
if (strProtocol.equals(STR_TCP_PROTOCOL)) {
pwConfig.println("network inet stream connect ::ffff:" + strDeviceIPAddress + " " + String.valueOf(iDevicePort));
} else {
pwConfig.println("network inet dgram bind :: " + String.valueOf(iDeviceComPort));
pwConfig.println("network inet dgram send ::ffff:" + strDeviceIPAddress + " " + String.valueOf(iDevicePort));
}
}
/**
* configureProcessJailDevicePolicies() method configures the device MAC policies
*
* This method configures the device policies between device driver and device
*
* @param strConfigHost String hostname to be configured
* @param strRouterAddress String router address
* @param iPort Integer port
* @return void
*/
public void configureProcessJailGWDevicePolicies(String strConfigHost, String strRouterAddress, int iPort) {
PrintWriter pwConfig = getPrintWriter(strConfigHost);
pwConfig.println("file read /home/iotuser/iot2/iotjava/iotruntime/\\*.jks");
pwConfig.println("file read /etc/resolv.conf");
pwConfig.println("file read /etc/hosts");
pwConfig.println("network inet dgram send " + strRouterAddress + " " + String.valueOf(iPort));
}
/**
* configureProcessJailDeviceDriverInetAddressPolicies() method configures the device MAC policies
*
*
* @param strConfigHost String hostname to be configured
* @param strAddress String device IP address
* @return void
*/
public void configureProcessJailInetAddressPolicies(String strConfigHost, String strAddress) {
PrintWriter pwConfig = getPrintWriter(strConfigHost);
//System.out.println("\n\nDEBUG: Writing the config host address setup!!!\n\n");
pwConfig.println("network inet stream connect ::ffff:" + strAddress + " " + String.valueOf(80)); // HTTP access for this address
}
/**
* configureProcessJailControllerPolicies() method configures the main MAC policies for controller
*
* @param strControllerName String controller name to be configured
* @param strFileName String policy file path and name
* @param strMasterIPAddress String master IP address
* @param iComPort Integer communication port (controller-driver)
* @return void
*/
public void configureProcessJailControllerPolicies(String strControllerName, String strFileName,
String strMasterIPAddress, int iComPort) {
PrintWriter pwConfig = getPrintWriter(strControllerName);
String strPolicyList = readFile(strFileName);
// Replace the strings with the actual values
String strNewPolicyList = strPolicyList.replace(STR_OBJECT_NAME, strControllerName).
replace(STR_OBJECT_CLASS_NAME, strControllerName).
replace(STR_MASTER_IP_ADDRESS, strMasterIPAddress).
replace(STR_MASTER_COM_PORT, String.valueOf(iComPort));
pwConfig.println("\n");
pwConfig.print(strNewPolicyList);
}
/**
* configureProcessJailContRMIPolicies() method configures the MAC policies for RMI ports of controller
*
* @param strControllerName String controller name to be configured
* @param strFileName String policy file path and name
* @param strMasterIPAddress String master IP address
* @param iComPort Integer communication port (controller-driver)
* @return void
*/
public void configureProcessJailContRMIPolicies(String strControllerName, String strDeviceDriverIPAddress,
int iRMIRegPort, int iRMIStubPort) {
PrintWriter pwConfig = getPrintWriter(strControllerName);
// Replace the strings with the actual values
pwConfig.println("network inet stream connect ::ffff:" + strDeviceDriverIPAddress + " " + String.valueOf(iRMIRegPort));
pwConfig.println("network inet stream connect ::ffff:" + strDeviceDriverIPAddress + " " + String.valueOf(iRMIStubPort));
}
/**
* combineControllerMACPolicies() method combines the controller MAC policies into the right host policy file
*
* @param strConfigHost String hostname to be configured
* @param strFileName String policy file path and name
* @return void
*/
public void combineControllerMACPolicies(String strConfigHost, String strObjectControllerName, String strFileName) {
PrintWriter pwConfig = getPrintWriter(strConfigHost);
PrintWriter pwCont = getPrintWriter(strObjectControllerName);
pwCont.close();
String strPolicyList = readFile(strFileName);
pwConfig.println(strPolicyList);
runCommand("rm -rf " + strFileName);
}
}