760f3d25dc63d53a18bb5928f0f365941874c975
[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          * getAddressListObject() method returns the map from this class
164          * <p>
165          * This method is useful for MAC policy class so that it doesn't have
166          * to query the router again
167          */
168         public Map<String, String> getAddressListObject() {
169
170                 return mapMACtoIPAdd;
171         }
172
173         /**
174          * getAddressList() method gets list of IP addresses
175          * <p>
176          * This method sends an inquiry to the router to look for
177          * the list of DHCP leased addresses and their mapping to MAC
178          * addresses
179          *
180          * @param  strRouterAddress  String that contains address of router
181          */
182         public void getAddressList(String strRouterAddress) {
183
184                 //HashMap<String,String> hmMACToIPAdd = new HashMap<String,String>();
185                 try {
186                         // We can replace "cat /tmp/dhcp.leases" with "cat /proc/net/arp"
187                         String cmd = "ssh " + STR_SSH_USERNAME_ROUTER + "@" + strRouterAddress +
188                          " cat /tmp/dhcp.leases";
189                         Runtime runtime = Runtime.getRuntime();
190                         Process process = runtime.exec(cmd);
191
192                         InputStream inStream = process.getInputStream();
193                         InputStreamReader isReader = new InputStreamReader(inStream);
194                         BufferedReader bReader = new BufferedReader(isReader);
195                         String strRead = null;
196                         while((strRead = bReader.readLine()) != null){
197                                 String[] str = strRead.split(" ");
198                                 mapMACtoIPAdd.put(str[1], str[2]);
199                         }
200                 } catch (IOException ex) {
201                         System.out.println("RouterConfig: IOException: " + ex.getMessage());
202                         ex.printStackTrace();
203                 }
204         }
205
206         /**
207          * getIPFromMACAddress() method gets IP from MAC address
208          *
209          * @return  String  String that contains IP address from the MAC-IP mapping
210          */      
211         public String getIPFromMACAddress(String strMACAddress) {
212
213                 String strIPAddress = mapMACtoIPAdd.get(strMACAddress);
214                 if (strIPAddress == null) {
215                         throw new Error("RouterConfig: MAC address " + strMACAddress + " not found on the list! Please check if device is present in /tmp/dhcp.leases!");
216                 }
217                 return strIPAddress;
218         }
219
220         /**
221          * configureRouterMainPolicies() method configures the router
222          * <p>
223          * This method configures the router's main policies
224          * This method creates a command line using 'ssh' and 'iptables'
225          * to access the router and create Netfilter statements
226          *
227          * @param   strConfigHost        String hostname to be configured
228          * @param   strFirstHost     String first host
229          * @param   strSecondHost    String second host
230          * @param   strProtocol      String protocol TCP/UDP
231          * @param   iSrcPort         Integer source port number
232          * @param   iDstPort         Integer destination port number
233          * @return  void
234          */
235         public void configureRouterMainPolicies(String strConfigHost, String strFirstHost,
236                 String strSecondHost, String strProtocol, int iSrcPort, int iDstPort) {
237
238                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
239                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + 
240                         strSecondHost + " -p " + strProtocol + " --dport " + iDstPort);
241                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + 
242                         strSecondHost + " -p " + strProtocol + " --sport " + iSrcPort);
243                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + 
244                         strFirstHost + " -p " + strProtocol + " --sport " + iDstPort);
245                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + 
246                         strFirstHost + " -p " + strProtocol + " --dport " + iSrcPort);
247         }
248
249         /**
250          * configureRouterMainPolicies() method configures the router
251          * <p>
252          * This method configures the router's main policies
253          * This method creates a command line using 'ssh' and 'iptables'
254          * to access the router and create Netfilter statements
255          *
256          * @param   strConfigHost        String hostname to be configured
257          * @param   strFirstHost     String first host
258          * @param   strSecondHost    String second host
259          * @param   strProtocol      String protocol TCP/UDP
260          * @param   iPort            Integer port number
261          * @return  void
262          */
263         public void configureRouterMainPolicies(String strConfigHost, String strFirstHost,
264                 String strSecondHost, String strProtocol, int iPort) {
265
266                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
267                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
268                         " -p " + strProtocol + " --dport " + iPort);
269                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
270                         " -p " + strProtocol + " --sport " + iPort);
271                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
272                         " -p " + strProtocol + " --dport " + iPort);
273                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
274                         " -p " + strProtocol + " --sport " + iPort);
275         }
276
277         /**
278          * configureRouterMainPolicies() method configures the router
279          * <p>
280          * This method is the same as the first configureRouterMainPolicies(),
281          * but it doesn't specify a certain port for the communication
282          *
283          * @param   strConfigHost        String hostname to be configured
284          * @param   strFirstHost     String first host
285          * @param   strSecondHost    String second host
286          * @param   strProtocol      String protocol TCP/UDP
287          * @return  void
288          */
289         public void configureRouterMainPolicies(String strConfigHost, String strFirstHost,
290                 String strSecondHost, String strProtocol) {
291
292                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
293                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + 
294                         " -d " + strSecondHost + " -p " + strProtocol);
295                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + 
296                         " -d " + strFirstHost + " -p " + strProtocol);
297
298         }
299
300         /**
301          * configureRouterMainPolicies() method configures the router
302          * <p>
303          * This method is the same as the first configureRouterMainPolicies(),
304          * but it doesn't specify a certain port and protocol for the communication
305          *
306          * @param   strConfigHost        String hostname to be configured
307          * @param   strFirstHost     String first host
308          * @param   strSecondHost    String second host
309          * @return  void
310          */
311         public void configureRouterMainPolicies(String strConfigHost, String strFirstHost, String strSecondHost) {
312
313                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
314                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost);
315                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost);
316
317         }
318
319         /**
320          * configureHostMainPolicies() method configures the host
321          * <p>
322          * This method configures the host with router's policies
323          *
324          * @param   strConfigHost        String hostname to be configured
325          * @param   strFirstHost     String first host
326          * @param   strSecondHost    String second host
327          * @param   strProtocol      String protocol TCP/UDP
328          * @param   iSrcPort         Integer source port number
329          * @param   iDstPort         Integer destination port number
330          * @return  void
331          */
332         public void configureHostMainPolicies(String strConfigHost, String strFirstHost,
333                 String strSecondHost, String strProtocol, int iSrcPort, int iDstPort) {
334
335                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
336                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
337                         " -p " + strProtocol + " --dport " + iDstPort);
338                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
339                         " -p " + strProtocol + " --sport " + iSrcPort);
340                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
341                         " -p " + strProtocol + " --sport " + iDstPort);
342                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
343                         " -p " + strProtocol + " --dport " + iSrcPort);
344                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
345                         " -p " + strProtocol + " --dport " + iDstPort);
346                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
347                         " -p " + strProtocol + " --sport " + iSrcPort);
348                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
349                         " -p " + strProtocol + " --sport " + iDstPort);
350                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
351                         " -p " + strProtocol + " --dport " + iSrcPort);
352
353         }
354
355         /**
356          * configureHostMainPolicies() method configures the host
357          * <p>
358          * This method configures the host with router's policies
359          *
360          * @param   strConfigHost        String hostname to be configured
361          * @param   strFirstHost     String first host
362          * @param   strSecondHost    String second host
363          * @param   strProtocol      String protocol TCP/UDP
364          * @param   iPort            Integer port number
365          * @return  void
366          */
367         public void configureHostMainPolicies(String strConfigHost, String strFirstHost,
368                 String strSecondHost, String strProtocol, int iPort) {
369
370                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
371                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
372                         " -p " + strProtocol + " --dport " + iPort);
373                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
374                         " -p " + strProtocol + " --sport " + iPort);
375                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
376                         " -p " + strProtocol + " --dport " + iPort);
377                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
378                         " -p " + strProtocol + " --sport " + iPort);
379                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
380                         " -p " + strProtocol + " --dport " + iPort);
381                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
382                         " -p " + strProtocol + " --sport " + iPort);
383                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
384                         " -p " + strProtocol + " --dport " + iPort);
385                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
386                         " -p " + strProtocol + " --sport " + iPort);
387         }
388
389         /**
390          * configureHostMainPolicies() method configures the host
391          * <p>
392          * This method is the same as the first configureHostMainPolicies(),
393          * but it doesn't specify a certain port for the communication
394          *
395          * @param   strConfigHost        String hostname to be configured
396          * @param   strFirstHost     String first host
397          * @param   strSecondHost    String second host
398          * @param   strProtocol      String protocol TCP/UDP
399          * @return  void
400          */
401         public void configureHostMainPolicies(String strConfigHost, String strFirstHost, 
402                 String strSecondHost, String strProtocol) {
403
404                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
405                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
406                         " -p " + strProtocol);
407                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
408                         " -p " + strProtocol);
409                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
410                         " -p " + strProtocol);
411                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
412                         " -p " + strProtocol);
413         }
414
415         /**
416          * configureHostMainPolicies() method configures the host
417          * <p>
418          * This method is the same as the first configureHostMainPolicies(),
419          * but it doesn't specify a certain port and protocol for the communication
420          *
421          * @param   strConfigHost        String hostname to be configured
422          * @param   strFirstHost     String first host
423          * @param   strSecondHost    String second host
424          * @return  void
425          */
426         public void configureHostMainPolicies(String strConfigHost, String strFirstHost, String strSecondHost) {
427
428                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
429                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost);
430                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost);
431                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost);
432                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost);
433         }
434
435
436         /**
437          * configureRouterHTTPPolicies() method configures the router
438          * <p>
439          * This method configures the router's basic policies
440          * This method creates a command line using 'ssh' and 'iptables'
441          * to access the router and create Netfilter statements
442          *
443          * @param   strConfigHost        String hostname to be configured
444          * @param   strFirstHost  String first host address (source)
445          * @param   strSecondHost   String second host address (destination)
446          * @return  void
447          */
448         public void configureRouterHTTPPolicies(String strConfigHost, String strFirstHost, String strSecondHost) {
449
450                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
451                 // Allow HTTP
452                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
453                         " -p tcp --dport http");
454                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
455                         " -p tcp --sport http");
456                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
457                         " -p tcp --dport http");
458                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
459                         " -p tcp --sport http");
460                 // Allow HTTPS
461                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
462                         " -p tcp --dport https");
463                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
464                         " -p tcp --sport https");
465                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
466                         " -p tcp --dport https");
467                 pwConfig.println("-I FORWARD -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
468                         " -p tcp --sport https");
469         }
470
471
472
473         /**
474          * configureRouterICMPPolicies() method configures the router
475          * <p>
476          * This method configures the router's basic policies
477          * This method creates a command line using 'ssh' and 'iptables'
478          * to access the router and create Netfilter statements
479          *
480          * @param   strConfigHost        String hostname to be configured
481          * @return  void
482          */
483         public void configureRouterICMPPolicies(String strConfigHost) {
484
485                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
486                 // Allow ICMP
487                 pwConfig.println("-A FORWARD -j ACCEPT -p icmp");
488                 pwConfig.println("-A INPUT -j ACCEPT -p icmp");
489                 pwConfig.println("-A OUTPUT -j ACCEPT -p icmp");
490         }
491
492         /**
493          * configureRouterICMPPolicies() method configures the router
494          * <p>
495          * This method configures the router's basic policies
496          * This method creates a command line using 'ssh' and 'iptables'
497          * to access the router and create Netfilter statements
498          *
499          * @param   strConfigHost               String hostname to be configured
500          * @param   strMonitorHost              String monitor address
501          * @return  void
502          */
503         public void configureRouterICMPPolicies(String strConfigHost, String strMonitorHost) {
504
505                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
506                 // Allow ICMP
507                 pwConfig.println("-A FORWARD -j ACCEPT -p icmp");
508                 pwConfig.println("-A INPUT -j ACCEPT -s " + strMonitorHost + 
509                         " -d " + strConfigHost + " -p icmp");
510                 pwConfig.println("-A INPUT -j ACCEPT -s " + strConfigHost + 
511                         " -d " + strMonitorHost + " -p icmp");
512                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + strMonitorHost + 
513                         " -d " + strConfigHost + " -p icmp");
514                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + strConfigHost + 
515                         " -d " + strMonitorHost + " -p icmp");
516         }
517
518         /**
519          * configureRouterSSHPolicies() method configures the router
520          * <p>
521          * This method configures the router's basic policies
522          * This method creates a command line using 'ssh' and 'iptables'
523          * to access the router and create Netfilter statements
524          *
525          * @param   strConfigHost               String hostname to be configured
526          * @param   strMonitorHost              String monitor address
527          * @return  void
528          */
529         public void configureRouterSSHPolicies(String strConfigHost, String strMonitorHost) {
530
531                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
532                 // Allow SSH - port 22 (only from monitor host)
533                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
534                         strMonitorHost + " -d " + strConfigHost + " -p tcp --dport ssh");
535                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
536                         strMonitorHost + " -d " + strConfigHost + " -p tcp --sport ssh");
537                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
538                         strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
539                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
540                         strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
541                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
542                         strMonitorHost + " -d " + strConfigHost + " -p tcp --dport ssh");
543                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
544                         strMonitorHost + " -d " + strConfigHost + " -p tcp --sport ssh");
545                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
546                         strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
547                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
548                         strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
549                 pwConfig.println("-A FORWARD -j ACCEPT -p tcp --dport ssh");
550                 pwConfig.println("-A FORWARD -j ACCEPT -p tcp --sport ssh");
551
552         }
553
554         /**
555          * configureRouterDHCPPolicies() method configures the router
556          * <p>
557          * This method configures the router's basic policies
558          * This method creates a command line using 'ssh' and 'iptables'
559          * to access the router and create Netfilter statements
560          *
561          * @param   strConfigHost               String hostname to be configured
562          * @return  void
563          */
564         public void configureRouterDHCPPolicies(String strConfigHost) {
565
566                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
567                 // Allow DHCP renew - BOOTP Client port 68 / BOOTP Server port 67
568                 pwConfig.println("-A INPUT -j ACCEPT -p udp --dport bootpc");
569                 pwConfig.println("-A INPUT -j ACCEPT -p udp --sport bootpc");
570                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport bootps");
571                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport bootps");
572         }
573
574         /**
575          * configureRouterDNSPolicies() method configures the router
576          * <p>
577          * This method configures the router's basic policies
578          * This method creates a command line using 'ssh' and 'iptables'
579          * to access the router and create Netfilter statements
580          *
581          * @param   strConfigHost               String hostname to be configured
582          * @return  void
583          */
584         public void configureRouterDNSPolicies(String strConfigHost) {
585
586                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
587                 // Allow DNS UDP and TCP port 53
588                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --dport domain");
589                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --sport domain");
590                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --dport domain");
591                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --sport domain");
592                 pwConfig.println("-A INPUT -j ACCEPT -p udp --dport domain");
593                 pwConfig.println("-A INPUT -j ACCEPT -p udp --sport domain");
594                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport domain");
595                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport domain");
596         }
597
598         /**
599          * configureRejectPolicies() method configures the router
600          * <p>
601          * This method configures the router's basic policies
602          * This method creates a command line using 'ssh' and 'iptables'
603          * to access the router and create Netfilter statements
604          *
605          * @param   strConfigHost               String hostname to be configured
606          * @return  void
607          */
608         public void configureRejectPolicies(String strConfigHost) {
609
610                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
611                 // Reject every other thing
612                 pwConfig.println("-A FORWARD -j REJECT");
613                 pwConfig.println("-A INPUT -j REJECT");
614                 pwConfig.println("-A OUTPUT -j REJECT");
615         }
616
617         /**
618          * configureRouterNATPolicy() method configures the router
619          * <p>
620          * This method configures the NAT policy separately.
621          * Somehow SSH in Java is not able to combine other commands for
622          * iptables rules configuration and NAT configuration.
623          *
624          * @param   strConfigHost               String hostname to be configured
625          * @return  void
626          */
627         public void configureRouterNATPolicy(String strConfigHost) {
628
629                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
630                 // Configure NAT
631                 pwConfig.println("-t nat -A POSTROUTING -o eth0 -j MASQUERADE");
632                 // Add the following 2 lines
633                 pwConfig.println("-A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT");
634                 pwConfig.println("-A FORWARD -i wlan0 -o eth0 -j ACCEPT");
635         }
636
637         /**
638          * configureHostHTTPPolicies() method configures the host
639          * <p>
640          * This method configures the host with HTTP policies
641          *
642          * @param   strConfigHost       String hostname to be configured
643          * @param   strFirstHost        String first host address (source)
644          * @param   strSecondHost       String second host address (destination)
645          * @return  void
646          */
647         public void configureHostHTTPPolicies(String strConfigHost, String strFirstHost, String strSecondHost) {
648
649                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
650                 // Allow HTTP
651                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
652                         " -p tcp --dport http");
653                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
654                         " -p tcp --sport http");
655                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
656                         " -p tcp --dport http");
657                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
658                         " -p tcp --sport http");
659                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
660                         " -p tcp --dport http");
661                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
662                         " -p tcp --sport http");
663                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
664                         " -p tcp --dport http");
665                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
666                         " -p tcp --sport http");
667                 // Allow HTTPS
668                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
669                         " -p tcp --dport https");
670                 pwConfig.println("-I INPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
671                         " -p tcp --sport https");
672                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
673                         " -p tcp --dport https");
674                 pwConfig.println("-I INPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
675                         " -p tcp --sport https");
676                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
677                         " -p tcp --dport https");
678                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strFirstHost + " -d " + strSecondHost +
679                         " -p tcp --sport https");
680                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
681                         " -p tcp --dport https");
682                 pwConfig.println("-I OUTPUT -j ACCEPT -s " + strSecondHost + " -d " + strFirstHost +
683                         " -p tcp --sport https");
684         }
685
686         /**
687          * configureHostICMPPolicies() method configures the host
688          * <p>
689          * This method configures the host with router's policies
690          *
691          * @param   strConfigHost               String hostname to be configured
692          * @return  void
693          */
694         public void configureHostICMPPolicies(String strConfigHost) {
695
696                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
697                 // Allow ICMP
698                 pwConfig.println("-A INPUT -j ACCEPT -p icmp");
699                 pwConfig.println("-A OUTPUT -j ACCEPT -p icmp");
700         }
701
702         /**
703          * configureHostSQLPolicies() method configures the host
704          * <p>
705          * This method configures the host with router's policies
706          *
707          * @param   strConfigHost               String hostname to be configured
708          * @return  void
709          */
710         public void configureHostSQLPolicies(String strConfigHost) {
711
712                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
713                 // Allow ICMP
714                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --dport mysql");
715                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --sport mysql");
716                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --dport mysql");
717                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --sport mysql");
718         }
719
720         /**
721          * configureHostICMPPolicies() method configures the host
722          * <p>
723          * This method configures the host with router's policies
724          *
725          * @param   strConfigHost               String hostname to be configured
726          * @param   strMonitorHost              String monitor address
727          * @return  void
728          */
729         public void configureHostICMPPolicies(String strConfigHost, String strMonitorHost) {
730
731                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
732                 // Allow ICMP
733                 pwConfig.println("-A INPUT -j ACCEPT -s " + strMonitorHost + 
734                         " -d " + strConfigHost + " -p icmp");
735                 pwConfig.println("-A INPUT -j ACCEPT -s " + strConfigHost + 
736                         " -d " + strMonitorHost + " -p icmp");
737                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + strMonitorHost + 
738                         " -d " + strConfigHost + " -p icmp");
739                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + strConfigHost + 
740                         " -d " + strMonitorHost + " -p icmp");
741         }
742
743
744         /**
745          * configureHostSSHPolicies() method configures the host
746          * <p>
747          * This method configures the host with router's policies
748          *
749          * @param   strConfigHost               String hostname to be configured
750          * @return  void
751          */
752         public void configureHostSSHPolicies(String strConfigHost) {
753
754                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
755                 // Allow SSH - port 22
756                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --dport ssh");
757                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --sport ssh");
758                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --dport ssh");
759                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --sport ssh");
760                 pwConfig.println("-A FORWARD -j ACCEPT -p tcp --dport ssh");
761                 pwConfig.println("-A FORWARD -j ACCEPT -p tcp --sport ssh");
762         }
763
764
765         /**
766          * configureHostSSHPolicies() method configures the host
767          * <p>
768          * This method configures the host with router's policies
769          *
770          * @param   strConfigHost               String hostname to be configured
771          * @param   strMonitorHost              String monitor address
772          * @return  void
773          */
774         public void configureHostSSHPolicies(String strConfigHost, String strMonitorHost) {
775
776                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
777                 // Allow SSH - port 22
778                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
779                         strMonitorHost + " -d " + strConfigHost + " -p tcp --dport ssh");
780                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
781                         strMonitorHost + " -d " + strConfigHost + " -p tcp --sport ssh");
782                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
783                         strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
784                 pwConfig.println("-A INPUT -j ACCEPT -s " + 
785                         strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
786                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
787                         strMonitorHost + " -d " + strConfigHost + " -p tcp --dport ssh");
788                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
789                         strMonitorHost + " -d " + strConfigHost + " -p tcp --sport ssh");
790                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
791                         strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
792                 pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
793                         strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
794         }
795
796
797         /**
798          * configureHostDHCPPolicies() method configures the host
799          * <p>
800          * This method configures the host with router's policies
801          *
802          * @param   strConfigHost               String hostname to be configured
803          * @return  void
804          */
805         public void configureHostDHCPPolicies(String strConfigHost) {
806
807                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
808                 // Allow DHCP renew - BOOTP Client port 68 / BOOTP Server port 67
809                 pwConfig.println("-A INPUT -j ACCEPT -p udp --dport bootpc");
810                 pwConfig.println("-A INPUT -j ACCEPT -p udp --sport bootpc");
811                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport bootps");
812                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport bootps");
813         }
814
815
816         /**
817          * configureHostDNSPolicies() method configures the host
818          * <p>
819          * This method configures the host with router's policies
820          *
821          * @param   strConfigHost               String hostname to be configured
822          * @return  void
823          */
824         public void configureHostDNSPolicies(String strConfigHost) {
825
826                 PrintWriter pwConfig = getPrintWriter(strConfigHost);
827                 // Allow DNS UDP and TCP port 53
828                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --dport domain");
829                 pwConfig.println("-A INPUT -j ACCEPT -p tcp --sport domain");
830                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --dport domain");
831                 pwConfig.println("-A OUTPUT -j ACCEPT -p tcp --sport domain");
832                 pwConfig.println("-A INPUT -j ACCEPT -p udp --dport domain");
833                 pwConfig.println("-A INPUT -j ACCEPT -p udp --sport domain");
834                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport domain");
835                 pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport domain");
836
837         }
838 }