Modifying master for Tomoyo process jailing; Adding basic policies for Tomoyo environ...
[iot2.git] / iotjava / iotruntime / master / RouterConfig.java
1 package iotruntime.master;
2
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
5 import java.io.BufferedReader;
6 import java.io.BufferedWriter;
7 import java.io.FileWriter;
8 import java.io.PrintWriter;
9 import java.io.IOException;
10 import java.util.HashMap;
11 import java.util.Map;
12
13 /** Class RouterConfig is a class that configures the router
14  *  in our compute node network with the relevant netfilter
15  *  policies;
16  *  it uses ssh to contact the router and writes policy into it
17  *  <p>
18  *  To make the implementation faster, we use "iptables-restore"
19  *  that doesn't require "iptables" command to be invoked many
20  *  times - each invocation of "iptables" will load the existing
21  *  table from the kernel space before appending the new rule.
22  *
23  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
24  * @version     2.0
25  * @since       2016-06-21
26  */
27 public final class RouterConfig {
28
29         /**
30          * RouterConfig constants
31          */
32         private static final String STR_SSH_USERNAME_ROUTER = "root";
33         private static final String STR_SSH_USERNAME_HOST   = "iotuser";
34         private static final String STR_POLICY_FILE_EXT         = ".policy";
35
36         /**
37          * RouterConfig properties
38          */
39         private Map<String, PrintWriter> mapHostToFile;
40         private Map<String, String> mapMACtoIPAdd;
41
42         /**
43          * Constructor
44          */
45         public RouterConfig() {
46                 // This maps hostname to file PrintWriter
47                 mapHostToFile = null;
48                 mapMACtoIPAdd = new HashMap<String, String>();
49         }
50
51         /**
52          * renewPrintWriter() renews the mapHostToFile object that lists all PrintWriters
53          *
54          * @return  void
55          */
56         public void renewPrintWriter() {
57
58                 mapHostToFile = new HashMap<String, PrintWriter>();
59         }
60
61         /**
62          * getPrintWriter() gets the right PrintWriter object to print policies to the right file
63          *
64          * @param   strConfigHost String hostname to be configured
65          * @return  PrintWriter
66          */
67         private PrintWriter getPrintWriter(String strConfigHost) {
68
69                 // Return object if existing
70                 if (mapHostToFile.containsKey(strConfigHost)) {
71                         return mapHostToFile.get(strConfigHost);
72                 } else {
73                 // Simply create a new one if it doesn't exist
74                         FileWriter fw = null;
75                         try {
76                                 fw = new FileWriter(strConfigHost + STR_POLICY_FILE_EXT);
77                         } catch (IOException ex) {
78                                 ex.printStackTrace();
79                         }
80                         PrintWriter pwConfig = new PrintWriter(new BufferedWriter(fw));
81                         pwConfig.println("*filter");    // Print header for iptables-restore
82                         mapHostToFile.put(strConfigHost, pwConfig);
83                         return pwConfig;
84                 }
85         }
86         
87         /**
88          * close() closes all PrintWriter objects
89          *
90          * @return  void
91          */
92         public void close() {
93
94                 for(PrintWriter pwConfig: mapHostToFile.values()) {
95                         pwConfig.println("COMMIT");             // Add "COMMIT" statement to end the list for iptables-restore
96                         pwConfig.close();
97                 }
98         }
99
100         /**
101          * sendRouterPolicies() deploys policies on router
102          *
103          * @param   strConfigHost String hostname to be configured
104          * @return  void
105          */
106         public void sendRouterPolicies(String strConfigHost) {
107
108                 String strCmdSend = "scp " + strConfigHost + STR_POLICY_FILE_EXT + " " + 
109                         STR_SSH_USERNAME_ROUTER + "@" + strConfigHost + ":~;";
110                 //System.out.println(strCmdSend);
111                 deployPolicies(strCmdSend);
112                 String strCmdDeploy = "ssh " + STR_SSH_USERNAME_ROUTER + "@" + strConfigHost +
113                         " iptables-restore < ~/" + strConfigHost + STR_POLICY_FILE_EXT + "; rm ~/" + strConfigHost + 
114                         STR_POLICY_FILE_EXT + "; ";// + 
115                         // TODO: delete these later when we apply tight initial conditions (reject everything but SSH commands)
116                         //"iptables -F startup_filter_tcp; iptables -F startup_filter_udp; " +
117                         //"iptables -t filter -D FORWARD -j startup_filter_tcp; iptables -t filter -D FORWARD -j startup_filter_udp;";
118                 //System.out.println(strCmdDeploy);
119                 deployPolicies(strCmdDeploy);
120         }
121         
122         /**
123          * sendHostPolicies() deploys policies on host
124          *
125          * @param   strConfigHost String hostname to be configured
126          * @return  void
127          */
128         public void sendHostPolicies(String strConfigHost) {
129
130                 String strCmdSend = "scp " + strConfigHost + STR_POLICY_FILE_EXT + " " + 
131                         STR_SSH_USERNAME_HOST + "@" + strConfigHost + ":~;";
132                 //System.out.println(strCmdSend);
133                 deployPolicies(strCmdSend);
134                 String strCmdDeploy = "ssh " + STR_SSH_USERNAME_HOST + "@" + strConfigHost +
135                         " sudo iptables-restore < ~/" + strConfigHost + STR_POLICY_FILE_EXT + "; rm ~/" + strConfigHost + 
136                         STR_POLICY_FILE_EXT + ";";
137                 //System.out.println(strCmdDeploy);
138                 deployPolicies(strCmdDeploy);
139         }
140
141         /**
142          * deployPolicies() method configures the policies
143          *
144          * @param   strCommand  String that contains command line
145          * @return  void
146          */
147         private void deployPolicies(String strCommand) {
148
149                 try {
150                         Runtime runtime = Runtime.getRuntime();
151                         Process process = runtime.exec(strCommand);
152                         process.waitFor();
153                 } catch (IOException ex) {
154                         System.out.println("RouterConfig: IOException: " + ex.getMessage());
155                         ex.printStackTrace();
156                 } catch (InterruptedException ex) {
157                         System.out.println("RouterConfig: InterruptException: " + ex.getMessage());
158                         ex.printStackTrace();
159                 }
160         }
161
162         /**
163          * getAddressList() method gets list of IP addresses
164          * <p>
165          * This method sends an inquiry to the router to look for
166          * the list of DHCP leased addresses and their mapping to MAC
167          * addresses
168          *
169          * @param  strRouterAddress  String that contains address of router
170          */
171         public void getAddressList(String strRouterAddress) {
172
173                 //HashMap<String,String> hmMACToIPAdd = new HashMap<String,String>();
174                 try {
175                         // We can replace "cat /tmp/dhcp.leases" with "cat /proc/net/arp"
176                         String cmd = "ssh " + STR_SSH_USERNAME_ROUTER + "@" + strRouterAddress +
177                          " cat /tmp/dhcp.leases";
178                         Runtime runtime = Runtime.getRuntime();
179                         Process process = runtime.exec(cmd);
180
181                         InputStream inStream = process.getInputStream();
182                         InputStreamReader isReader = new InputStreamReader(inStream);
183                         BufferedReader bReader = new BufferedReader(isReader);
184                         String strRead = null;
185                         while((strRead = bReader.readLine()) != null){
186                                 String[] str = strRead.split(" ");
187                                 mapMACtoIPAdd.put(str[1], str[2]);
188                         }
189                 } catch (IOException ex) {
190                         System.out.println("RouterConfig: IOException: " + ex.getMessage());
191                         ex.printStackTrace();
192                 }
193         }
194
195         /**
196          * getIPFromMACAddress() method gets IP from MAC address
197          *
198          * @return  String  String that contains IP address from the MAC-IP mapping
199          */      
200         public String getIPFromMACAddress(String strMACAddress) {
201
202                 String strIPAddress = mapMACtoIPAdd.get(strMACAddress);
203                 if (strIPAddress == null) {
204                         throw new Error("RouterConfig: MAC address " + strMACAddress + " not found on the list! Please check if device is present in /tmp/dhcp.leases!");
205                 }
206                 return strIPAddress;
207         }
208
209         /**
210          * configureRouterMainPolicies() method configures the router
211          * <p>
212          * This method configures the router's main policies
213          * This method creates a command line using 'ssh' and 'iptables'
214          * to access the router and create Netfilter statements
215          *
216          * @param   strConfigHost        String hostname to be configured
217          * @param   strFirstHost     String first host
218          * @param   strSecondHost    String second host
219          * @param   strProtocol      String protocol TCP/UDP
220          * @param   iSrcPort         Integer source port number
221          * @param   iDstPort         Integer destination port number
222          * @return  void
223          */
224         public void configureRouterMainPolicies(String strConfigHost, String strFirstHost,
225                 String strSecondHost, String strProtocol, int iSrcPort, int iDstPort) {
226
227                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
228                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + 
229                         strSecondHost + " -p " + strProtocol + " --dport " + iDstPort);
230                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + 
231                         strSecondHost + " -p " + strProtocol + " --sport " + iSrcPort);
232                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + 
233                         strFirstHost + " -p " + strProtocol + " --sport " + iDstPort);
234                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + 
235                         strFirstHost + " -p " + strProtocol + " --dport " + iSrcPort);
236         }
237
238         /**
239          * configureRouterMainPolicies() method configures the router
240          * <p>
241          * This method configures the router's main policies
242          * This method creates a command line using 'ssh' and 'iptables'
243          * to access the router and create Netfilter statements
244          *
245          * @param   strConfigHost        String hostname to be configured
246          * @param   strFirstHost     String first host
247          * @param   strSecondHost    String second host
248          * @param   strProtocol      String protocol TCP/UDP
249          * @param   iPort            Integer port number
250          * @return  void
251          */
252         public void configureRouterMainPolicies(String strConfigHost, String strFirstHost,
253                 String strSecondHost, String strProtocol, int iPort) {
254
255                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
256                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
257                         " -p " + strProtocol + " --dport " + iPort);
258                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
259                         " -p " + strProtocol + " --sport " + iPort);
260                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
261                         " -p " + strProtocol + " --dport " + iPort);
262                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
263                         " -p " + strProtocol + " --sport " + iPort);
264         }
265
266         /**
267          * configureRouterMainPolicies() method configures the router
268          * <p>
269          * This method is the same as the first configureRouterMainPolicies(),
270          * but it doesn't specify a certain port for the communication
271          *
272          * @param   strConfigHost        String hostname to be configured
273          * @param   strFirstHost     String first host
274          * @param   strSecondHost    String second host
275          * @param   strProtocol      String protocol TCP/UDP
276          * @return  void
277          */
278         public void configureRouterMainPolicies(String strConfigHost, String strFirstHost,
279                 String strSecondHost, String strProtocol) {
280
281                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
282                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + 
283                         " -d " + strSecondHost + " -p " + strProtocol);
284                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + 
285                         " -d " + strFirstHost + " -p " + strProtocol);
286
287         }
288
289         /**
290          * configureRouterMainPolicies() method configures the router
291          * <p>
292          * This method is the same as the first configureRouterMainPolicies(),
293          * but it doesn't specify a certain port and protocol for the communication
294          *
295          * @param   strConfigHost        String hostname to be configured
296          * @param   strFirstHost     String first host
297          * @param   strSecondHost    String second host
298          * @return  void
299          */
300         public void configureRouterMainPolicies(String strConfigHost, String strFirstHost, String strSecondHost) {
301
302                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
303                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost);
304                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost);
305
306         }
307
308         /**
309          * configureHostMainPolicies() method configures the host
310          * <p>
311          * This method configures the host with router's policies
312          *
313          * @param   strConfigHost        String hostname to be configured
314          * @param   strFirstHost     String first host
315          * @param   strSecondHost    String second host
316          * @param   strProtocol      String protocol TCP/UDP
317          * @param   iSrcPort         Integer source port number
318          * @param   iDstPort         Integer destination port number
319          * @return  void
320          */
321         public void configureHostMainPolicies(String strConfigHost, String strFirstHost,
322                 String strSecondHost, String strProtocol, int iSrcPort, int iDstPort) {
323
324                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
325                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
326                         " -p " + strProtocol + " --dport " + iDstPort);
327                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
328                         " -p " + strProtocol + " --sport " + iSrcPort);
329                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
330                         " -p " + strProtocol + " --sport " + iDstPort);
331                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
332                         " -p " + strProtocol + " --dport " + iSrcPort);
333                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
334                         " -p " + strProtocol + " --dport " + iDstPort);
335                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
336                         " -p " + strProtocol + " --sport " + iSrcPort);
337                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
338                         " -p " + strProtocol + " --sport " + iDstPort);
339                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
340                         " -p " + strProtocol + " --dport " + iSrcPort);
341
342         }
343
344         /**
345          * configureHostMainPolicies() method configures the host
346          * <p>
347          * This method configures the host with router's policies
348          *
349          * @param   strConfigHost        String hostname to be configured
350          * @param   strFirstHost     String first host
351          * @param   strSecondHost    String second host
352          * @param   strProtocol      String protocol TCP/UDP
353          * @param   iPort            Integer port number
354          * @return  void
355          */
356         public void configureHostMainPolicies(String strConfigHost, String strFirstHost,
357                 String strSecondHost, String strProtocol, int iPort) {
358
359                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
360                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
361                         " -p " + strProtocol + " --dport " + iPort);
362                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
363                         " -p " + strProtocol + " --sport " + iPort);
364                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
365                         " -p " + strProtocol + " --dport " + iPort);
366                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
367                         " -p " + strProtocol + " --sport " + iPort);
368                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
369                         " -p " + strProtocol + " --dport " + iPort);
370                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
371                         " -p " + strProtocol + " --sport " + iPort);
372                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
373                         " -p " + strProtocol + " --dport " + iPort);
374                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
375                         " -p " + strProtocol + " --sport " + iPort);
376         }
377
378         /**
379          * configureHostMainPolicies() method configures the host
380          * <p>
381          * This method is the same as the first configureHostMainPolicies(),
382          * but it doesn't specify a certain port for the communication
383          *
384          * @param   strConfigHost        String hostname to be configured
385          * @param   strFirstHost     String first host
386          * @param   strSecondHost    String second host
387          * @param   strProtocol      String protocol TCP/UDP
388          * @return  void
389          */
390         public void configureHostMainPolicies(String strConfigHost, String strFirstHost, 
391                 String strSecondHost, String strProtocol) {
392
393                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
394                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
395                         " -p " + strProtocol);
396                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
397                         " -p " + strProtocol);
398                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
399                         " -p " + strProtocol);
400                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
401                         " -p " + strProtocol);
402         }
403
404         /**
405          * configureHostMainPolicies() method configures the host
406          * <p>
407          * This method is the same as the first configureHostMainPolicies(),
408          * but it doesn't specify a certain port and protocol for the communication
409          *
410          * @param   strConfigHost        String hostname to be configured
411          * @param   strFirstHost     String first host
412          * @param   strSecondHost    String second host
413          * @return  void
414          */
415         public void configureHostMainPolicies(String strConfigHost, String strFirstHost, String strSecondHost) {
416
417                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
418                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost);
419                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost);
420                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost);
421                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost);
422         }
423
424
425         /**
426          * configureRouterHTTPPolicies() method configures the router
427          * <p>
428          * This method configures the router's basic policies
429          * This method creates a command line using 'ssh' and 'iptables'
430          * to access the router and create Netfilter statements
431          *
432          * @param   strConfigHost        String hostname to be configured
433          * @param   strFirstHost  String first host address (source)
434          * @param   strSecondHost   String second host address (destination)
435          * @return  void
436          */
437         public void configureRouterHTTPPolicies(String strConfigHost, String strFirstHost, String strSecondHost) {
438
439                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
440                 // Allow HTTP
441                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
442                         " -p tcp --dport http");
443                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
444                         " -p tcp --sport http");
445                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
446                         " -p tcp --dport http");
447                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
448                         " -p tcp --sport http");
449                 // Allow HTTPS
450                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
451                         " -p tcp --dport https");
452                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
453                         " -p tcp --sport https");
454                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
455                         " -p tcp --dport https");
456                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
457                         " -p tcp --sport https");
458         }
459
460
461
462         /**
463          * configureRouterICMPPolicies() method configures the router
464          * <p>
465          * This method configures the router's basic policies
466          * This method creates a command line using 'ssh' and 'iptables'
467          * to access the router and create Netfilter statements
468          *
469          * @param   strConfigHost        String hostname to be configured
470          * @return  void
471          */
472         public void configureRouterICMPPolicies(String strConfigHost) {
473
474                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
475                 // Allow ICMP
476                 pwConfig.println("-A FORWARD -j ACCEPT -p icmp");
477                 pwConfig.println("-A INPUT -j ACCEPT -p icmp");
478                 pwConfig.println("-A OUTPUT -j ACCEPT -p icmp");
479         }
480
481         /**
482          * configureRouterICMPPolicies() method configures the router
483          * <p>
484          * This method configures the router's basic policies
485          * This method creates a command line using 'ssh' and 'iptables'
486          * to access the router and create Netfilter statements
487          *
488          * @param   strConfigHost               String hostname to be configured
489          * @param   strMonitorHost              String monitor address
490          * @return  void
491          */
492         public void configureRouterICMPPolicies(String strConfigHost, String strMonitorHost) {
493
494                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
495                 // Allow ICMP
496                 pwConfig.println("-A FORWARD -j ACCEPT -p icmp");
497                 pwConfig.println("-A INPUT -j ACCEPT -s " + strMonitorHost + 
498                         " -d " + strConfigHost + " -p icmp");
499                 pwConfig.println("-A INPUT -j ACCEPT -s " + strConfigHost + 
500                         " -d " + strMonitorHost + " -p icmp");
501                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + strMonitorHost + 
502                         " -d " + strConfigHost + " -p icmp");
503                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + strConfigHost + 
504                         " -d " + strMonitorHost + " -p icmp");
505         }
506
507         /**
508          * configureRouterSSHPolicies() method configures the router
509          * <p>
510          * This method configures the router's basic policies
511          * This method creates a command line using 'ssh' and 'iptables'
512          * to access the router and create Netfilter statements
513          *
514          * @param   strConfigHost               String hostname to be configured
515          * @param   strMonitorHost              String monitor address
516          * @return  void
517          */
518         public void configureRouterSSHPolicies(String strConfigHost, String strMonitorHost) {
519
520                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
521                 // Allow SSH - port 22 (only from monitor host)
522                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
523                         strMonitorHost + " -d " + strConfigHost + " -p tcp --dport ssh");
524                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
525                         strMonitorHost + " -d " + strConfigHost + " -p tcp --sport ssh");
526                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
527                         strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
528                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
529                         strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
530                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
531                         strMonitorHost + " -d " + strConfigHost + " -p tcp --dport ssh");
532                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
533                         strMonitorHost + " -d " + strConfigHost + " -p tcp --sport ssh");
534                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
535                         strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
536                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
537                         strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
538                 pwConfig.println("-A FORWARD -j ACCEPT -p tcp --dport ssh");
539                 pwConfig.println("-A FORWARD -j ACCEPT -p tcp --sport ssh");
540
541         }
542
543         /**
544          * configureRouterDHCPPolicies() method configures the router
545          * <p>
546          * This method configures the router's basic policies
547          * This method creates a command line using 'ssh' and 'iptables'
548          * to access the router and create Netfilter statements
549          *
550          * @param   strConfigHost               String hostname to be configured
551          * @return  void
552          */
553         public void configureRouterDHCPPolicies(String strConfigHost) {
554
555                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
556                 // Allow DHCP renew - BOOTP Client port 68 / BOOTP Server port 67
557                 pwConfig.println("-A INPUT -j ACCEPT -p udp --dport bootpc");
558                 pwConfig.println("-A INPUT -j ACCEPT -p udp --sport bootpc");
559                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport bootps");
560                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport bootps");
561         }
562
563         /**
564          * configureRouterDNSPolicies() method configures the router
565          * <p>
566          * This method configures the router's basic policies
567          * This method creates a command line using 'ssh' and 'iptables'
568          * to access the router and create Netfilter statements
569          *
570          * @param   strConfigHost               String hostname to be configured
571          * @return  void
572          */
573         public void configureRouterDNSPolicies(String strConfigHost) {
574
575                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
576                 // Allow DNS UDP and TCP port 53
577                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --dport domain");
578                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --sport domain");
579                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --dport domain");
580                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --sport domain");
581                 pwConfig.println("-A INPUT -j ACCEPT -p udp --dport domain");
582                 pwConfig.println("-A INPUT -j ACCEPT -p udp --sport domain");
583                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport domain");
584                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport domain");
585         }
586
587         /**
588          * configureRejectPolicies() method configures the router
589          * <p>
590          * This method configures the router's basic policies
591          * This method creates a command line using 'ssh' and 'iptables'
592          * to access the router and create Netfilter statements
593          *
594          * @param   strConfigHost               String hostname to be configured
595          * @return  void
596          */
597         public void configureRejectPolicies(String strConfigHost) {
598
599                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
600                 // Reject every other thing
601                 pwConfig.println("-A FORWARD -j REJECT");
602                 pwConfig.println("-A INPUT -j REJECT");
603                 pwConfig.println("-A OUTPUT -j REJECT");
604         }
605
606         /**
607          * configureRouterNATPolicy() method configures the router
608          * <p>
609          * This method configures the NAT policy separately.
610          * Somehow SSH in Java is not able to combine other commands for
611          * iptables rules configuration and NAT configuration.
612          *
613          * @param   strConfigHost               String hostname to be configured
614          * @return  void
615          */
616         public void configureRouterNATPolicy(String strConfigHost) {
617
618                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
619                 // Configure NAT
620                 pwConfig.println("-t nat -A POSTROUTING -o eth0 -j MASQUERADE");
621         }
622
623         /**
624          * configureHostHTTPPolicies() method configures the host
625          * <p>
626          * This method configures the host with HTTP policies
627          *
628          * @param   strConfigHost       String hostname to be configured
629          * @param   strFirstHost        String first host address (source)
630          * @param   strSecondHost       String second host address (destination)
631          * @return  void
632          */
633         public void configureHostHTTPPolicies(String strConfigHost, String strFirstHost, String strSecondHost) {
634
635                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
636                 // Allow HTTP
637                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
638                         " -p tcp --dport http");
639                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
640                         " -p tcp --sport http");
641                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
642                         " -p tcp --dport http");
643                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
644                         " -p tcp --sport http");
645                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
646                         " -p tcp --dport http");
647                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
648                         " -p tcp --sport http");
649                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
650                         " -p tcp --dport http");
651                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
652                         " -p tcp --sport http");
653                 // Allow HTTPS
654                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
655                         " -p tcp --dport https");
656                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
657                         " -p tcp --sport https");
658                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
659                         " -p tcp --dport https");
660                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
661                         " -p tcp --sport https");
662                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
663                         " -p tcp --dport https");
664                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
665                         " -p tcp --sport https");
666                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
667                         " -p tcp --dport https");
668                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
669                         " -p tcp --sport https");
670         }
671
672         /**
673          * configureHostICMPPolicies() method configures the host
674          * <p>
675          * This method configures the host with router's policies
676          *
677          * @param   strConfigHost               String hostname to be configured
678          * @return  void
679          */
680         public void configureHostICMPPolicies(String strConfigHost) {
681
682                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
683                 // Allow ICMP
684                 pwConfig.println("-A INPUT -j ACCEPT -p icmp");
685                 pwConfig.println("-A OUTPUT -j ACCEPT -p icmp");
686         }
687
688         /**
689          * configureHostSQLPolicies() method configures the host
690          * <p>
691          * This method configures the host with router's policies
692          *
693          * @param   strConfigHost               String hostname to be configured
694          * @return  void
695          */
696         public void configureHostSQLPolicies(String strConfigHost) {
697
698                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
699                 // Allow ICMP
700                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --dport mysql");
701                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --sport mysql");
702                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --dport mysql");
703                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --sport mysql");
704         }
705
706         /**
707          * configureHostICMPPolicies() method configures the host
708          * <p>
709          * This method configures the host with router's policies
710          *
711          * @param   strConfigHost               String hostname to be configured
712          * @param   strMonitorHost              String monitor address
713          * @return  void
714          */
715         public void configureHostICMPPolicies(String strConfigHost, String strMonitorHost) {
716
717                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
718                 // Allow ICMP
719                 pwConfig.println("-A INPUT -j ACCEPT -s " + strMonitorHost + 
720                         " -d " + strConfigHost + " -p icmp");
721                 pwConfig.println("-A INPUT -j ACCEPT -s " + strConfigHost + 
722                         " -d " + strMonitorHost + " -p icmp");
723                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + strMonitorHost + 
724                         " -d " + strConfigHost + " -p icmp");
725                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + strConfigHost + 
726                         " -d " + strMonitorHost + " -p icmp");
727         }
728
729
730         /**
731          * configureHostSSHPolicies() method configures the host
732          * <p>
733          * This method configures the host with router's policies
734          *
735          * @param   strConfigHost               String hostname to be configured
736          * @return  void
737          */
738         public void configureHostSSHPolicies(String strConfigHost) {
739
740                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
741                 // Allow SSH - port 22
742                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --dport ssh");
743                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --sport ssh");
744                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --dport ssh");
745                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --sport ssh");
746                 pwConfig.println("-A FORWARD -j ACCEPT -p tcp --dport ssh");
747                 pwConfig.println("-A FORWARD -j ACCEPT -p tcp --sport ssh");
748         }
749
750
751         /**
752          * configureHostSSHPolicies() method configures the host
753          * <p>
754          * This method configures the host with router's policies
755          *
756          * @param   strConfigHost               String hostname to be configured
757          * @param   strMonitorHost              String monitor address
758          * @return  void
759          */
760         public void configureHostSSHPolicies(String strConfigHost, String strMonitorHost) {
761
762                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
763                 // Allow SSH - port 22
764                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
765                         strMonitorHost + " -d " + strConfigHost + " -p tcp --dport ssh");
766                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
767                         strMonitorHost + " -d " + strConfigHost + " -p tcp --sport ssh");
768                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
769                         strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
770                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
771                         strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
772                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
773                         strMonitorHost + " -d " + strConfigHost + " -p tcp --dport ssh");
774                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
775                         strMonitorHost + " -d " + strConfigHost + " -p tcp --sport ssh");
776                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
777                         strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
778                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
779                         strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
780         }
781
782
783         /**
784          * configureHostDHCPPolicies() method configures the host
785          * <p>
786          * This method configures the host with router's policies
787          *
788          * @param   strConfigHost               String hostname to be configured
789          * @return  void
790          */
791         public void configureHostDHCPPolicies(String strConfigHost) {
792
793                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
794                 // Allow DHCP renew - BOOTP Client port 68 / BOOTP Server port 67
795                 pwConfig.println("-A INPUT -j ACCEPT -p udp --dport bootpc");
796                 pwConfig.println("-A INPUT -j ACCEPT -p udp --sport bootpc");
797                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport bootps");
798                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport bootps");
799         }
800
801
802         /**
803          * configureHostDNSPolicies() method configures the host
804          * <p>
805          * This method configures the host with router's policies
806          *
807          * @param   strConfigHost               String hostname to be configured
808          * @return  void
809          */
810         public void configureHostDNSPolicies(String strConfigHost) {
811
812                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
813                 // Allow DNS UDP and TCP port 53
814                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --dport domain");
815                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --sport domain");
816                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --dport domain");
817                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --sport domain");
818                 pwConfig.println("-A INPUT -j ACCEPT -p udp --dport domain");
819                 pwConfig.println("-A INPUT -j ACCEPT -p udp --sport domain");
820                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport domain");
821                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport domain");
822
823         }
824 }