Skip to content

Commit

Permalink
Some cosmetic changes to code
Browse files Browse the repository at this point in the history
Now there is no code in main method
  • Loading branch information
Dexer125 committed May 24, 2020
1 parent 1f0696e commit 4617a7b
Showing 1 changed file with 78 additions and 60 deletions.
138 changes: 78 additions & 60 deletions src/com/dexer125/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.dexer125;

import java.awt.*;
import java.io.*;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
public static void main(String[] args) throws IOException {
ReadDenialsFromFile();
}

private static void ReadDenialsFromFile() throws IOException {
//Initialize values
BufferedReader reader;
String path;
Expand All @@ -18,6 +23,7 @@ public static void main(String[] args) {
String output;
String filename;
String answer = "y";
String allow = "allow ";
StringBuilder outpuTxt = new StringBuilder("output");
int i = 0;
int fileNameIndex;
Expand Down Expand Up @@ -46,7 +52,6 @@ public static void main(String[] args) {
if (!outPath.endsWith("/")) {
outPath += "/";
}
try {
//Read file line by line
reader = new BufferedReader(new FileReader(path));
//Write to output file
Expand All @@ -61,7 +66,7 @@ public static void main(String[] args) {
scontext = line.substring(line.indexOf("scontext=u:r:") + 13, line.indexOf(":s0 tcontext"));
tcontext = line.substring(line.indexOf("tcontext=u:object_r:") + 20, line.indexOf(":s0 tclass"));
tclass = line.substring(line.indexOf("tclass=") + 7, line.indexOf("permissive"));
output = "allow " + scontext + " " + tcontext + ":" + tclass + command + ";";
output = allow + scontext + " " + tcontext + ":" + tclass + command + ";";

writer.write(output);
count++;
Expand All @@ -76,43 +81,48 @@ else if (line.contains("avc: denied") && line.contains("tcontext=u:r:") && !line
scontext = line.substring(line.indexOf("scontext=u:r:") + 13, line.indexOf(":s0 tcontext"));
tcontext = line.substring(line.indexOf("tcontext=u:r:") + 13, line.indexOf(":s0 tclass"));
tclass = line.substring(line.indexOf("tclass=") + 7, line.indexOf("permissive"));
output = "allow " + scontext + " " + tcontext + ":" + tclass + command + ";";
output = allow + scontext + " " + tcontext + ":" + tclass + command + ";";

writer.write(output);
count++;
writer.newLine();
writer.flush();

}

//Prevent errors because of :s0:
else if (line.contains("avc: denied") && line.contains("tcontext=u:r:") && line.contains(":s0:") && line.contains(":s0 tclass")) {
command = line.substring(line.indexOf("denied") + 7, line.indexOf(" for"));
scontext = line.substring(line.indexOf("scontext=u:r:") + 13, line.indexOf(":s0:"));
tcontext = line.substring(line.indexOf("tcontext=u:r:") + 13, line.indexOf(":s0 tclass"));
tclass = line.substring(line.indexOf("tclass=") + 7, line.indexOf("permissive"));
output = "allow " + scontext + " " + tcontext + ":" + tclass + command + ";";
output = allow + scontext + " " + tcontext + ":" + tclass + command + ";";

writer.write(output);
count++;
writer.newLine();
writer.flush();
} else if (line.contains("avc: denied") && line.contains("tcontext=u:object_r:") && line.contains(":s0:") && line.contains(":s0 tclass")) {
}

else if (line.contains("avc: denied") && line.contains("tcontext=u:object_r:") && line.contains(":s0:") && line.contains(":s0 tclass")) {
command = line.substring(line.indexOf("denied") + 7, line.indexOf(" for"));
scontext = line.substring(line.indexOf("scontext=u:r:") + 13, line.indexOf(":s0:"));
tcontext = line.substring(line.indexOf("tcontext=u:object_r:") + 20, line.indexOf(":s0 tclass"));
tclass = line.substring(line.indexOf("tclass=") + 7, line.indexOf("permissive"));
output = "allow " + scontext + " " + tcontext + ":" + tclass + command + ";";
output = allow + scontext + " " + tcontext + ":" + tclass + command + ";";

writer.write(output);
count++;
writer.newLine();
writer.flush();
} else if (line.contains("avc: denied") && line.contains("tcontext=u:r:") && line.contains(":s0:") && !line.contains(":s0 tclass")) {
}

else if (line.contains("avc: denied") && line.contains("tcontext=u:r:") && line.contains(":s0:") && !line.contains(":s0 tclass")) {
command = line.substring(line.indexOf("denied") + 7, line.indexOf(" for"));
scontext = line.substring(line.indexOf("scontext=u:r:") + 13, line.indexOf(":s0:"));
tcontext = line.substring(line.indexOf("tcontext=u:r:") + 13, line.indexOf(" tclass"));
tclass = line.substring(line.indexOf("tclass=") + 7, line.indexOf("permissive"));
output = "allow " + scontext + " " + tcontext + ":" + tclass + command + ";";
output = allow + scontext + " " + tcontext + ":" + tclass + command + ";";

// Delete unwanted characters in output
if (output.contains(":s0:")) {
Expand All @@ -124,12 +134,14 @@ else if (line.contains("avc: denied") && line.contains("tcontext=u:r:") && line.
count++;
writer.newLine();
writer.flush();
} else if (line.contains("avc: denied") && line.contains("tcontext=u:object_r:") && line.contains(":s0:") && !line.contains(":s0 tclass")) {
}

else if (line.contains("avc: denied") && line.contains("tcontext=u:object_r:") && line.contains(":s0:") && !line.contains(":s0 tclass")) {
command = line.substring(line.indexOf("denied") + 7, line.indexOf(" for"));
scontext = line.substring(line.indexOf("scontext=u:r:") + 13, line.indexOf(":s0:"));
tcontext = line.substring(line.indexOf("tcontext=u:object_r:") + 20, line.indexOf(" tclass"));
tclass = line.substring(line.indexOf("tclass=") + 7, line.indexOf("permissive"));
output = "allow " + scontext + " " + tcontext + ":" + tclass + command + ";";
output = allow + scontext + " " + tcontext + ":" + tclass + command + ";";

if (output.contains(":s0:")) {
output = output.replace(output.substring(output.indexOf(":s0:"), output.indexOf(";") + 1), "");
Expand All @@ -148,9 +160,6 @@ else if (line.contains("avc: denied") && line.contains("tcontext=u:r:") && line.
writer.close();
reader.close();

} catch (IOException e) {
e.printStackTrace();
}

// Prevent from running duplicates removing if no denials in log
if (count == 0) {
Expand All @@ -162,61 +171,70 @@ else if (line.contains("avc: denied") && line.contains("tcontext=u:r:") && line.
System.out.println("\nResolving denials...");
System.out.println("\nTemporary file created");

try {
// Code for duplicate line removing
System.out.println("Removing duplicates...");
PrintWriter printWriter = new PrintWriter(outPath + outpuTxt + ".txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(outPath + "outputTemp.txt"));
String line = bufferedReader.readLine();
RemoveDuplicates(outPath, outpuTxt);

HashSet<String> hashSet = new HashSet<>();
System.out.println("\nWould you like to open another log? y/n");
answer = scanner.nextLine();

while (line != null) {
if (answer.equals("y")) {
i++;
outpuTxt.deleteCharAt(outpuTxt.length() - 1);
outpuTxt.append(i);
}
else if (answer.equals("n")) {
System.out.println("\nTool by @Dexer125");
System.out.println("\nOpening output file...");
OpenFile(outPath, outpuTxt);
System.out.println("\nEnding script...");
}
else{
System.out.println("\nWrong argument.\nEnding script...");
return;
}

// Write only if not present in hashset
if (hashSet.add(line)) {
printWriter.println(line);
}
line = bufferedReader.readLine();
}

printWriter.flush();
bufferedReader.close();
printWriter.close();
File tempFile = new File(outPath + "outputTemp.txt");
if (tempFile.delete()) {
System.out.println("Temporary file removed.");
}
System.out.println("Duplicates removed.");
if (outPath.contains("//")) {
outPath = outPath.replace("//", "/");
}
System.out.println("\nYou can find your fixed denials under " + outPath + outpuTxt + ".txt");

System.out.println("\nWould you like to open another log? y/n");
answer = scanner.nextLine();
if (answer.equals("y")){
i++;
outpuTxt.deleteCharAt(outpuTxt.length() -1);
outpuTxt.append(i);
}
else if (answer.equals("n")){
System.out.println("\nTool by @Dexer125");
System.out.println("\nEnding script...");
}
else {
System.out.println("\nWrong argument.\nEnding script...");
return;
}
}

} catch (IOException e) {
e.printStackTrace();
}

}
}

private static void RemoveDuplicates(String outPath, StringBuilder outpuTxt) throws IOException {
System.out.println("Removing duplicates...");
PrintWriter printWriter = new PrintWriter(outPath + outpuTxt + ".txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(outPath + "outputTemp.txt"));
String line = bufferedReader.readLine();

HashSet<String> hashSet = new HashSet<>();

while (line != null) {

// Write only if not present in hashset
if (hashSet.add(line)) {
printWriter.println(line);
}
line = bufferedReader.readLine();
}

printWriter.flush();
bufferedReader.close();
printWriter.close();
File tempFile = new File(outPath + "outputTemp.txt");
if (tempFile.delete()) {
System.out.println("Temporary file removed.");
}
System.out.println("Duplicates removed.");
if (outPath.contains("//")) {
outPath = outPath.replace("//", "/");
}
System.out.println("\nYou can find your fixed denials under " + outPath + outpuTxt + ".txt");
}

private static void OpenFile(String outpath, StringBuilder outputTxt) throws IOException {
File file = new File(outpath + outputTxt +".txt");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
}
}

/* Coded by Dexer125 */
/* Coded by @Dexer125 */

0 comments on commit 4617a7b

Please sign in to comment.