1 package org.kde.kjas.server;
2 
3 import java.io.*;
4 import java.security.*;
5 import java.net.*;
6 
7 /**
8  *  KJAS server recognizes these variablers:
9  *    kjas.debug - makes server actions verbose
10  *    kjas.showConsole - shows Java Console window
11  *    kjas.log - save a transcript of the debug output to /tmp/kjas.log
12  */
13 
14 public class Main
15 {
16     //We need to save a reference to the original stdout
17     //for sending messages back
18     static final KJASProtocolHandler         protocol;
19     static       Console                     console = null;
20     private static final boolean             show_console;
21     public  static final boolean             Debug;
22     public  static final boolean             log;
23     static final boolean                     cacheImages;
24     static float                             java_version = (float) 0.0;
25     static String                            proxyHost = null;
26     static int                               proxyPort = 0;
27     private static boolean                   good_jdk = true;
28 
29     /**************************************************************************
30      * Initialization
31      **************************************************************************/
32     static
33     {
34         Debug = System.getProperty( "kjas.debug" ) != null;
35 
36         show_console = System.getProperty( "kjas.showConsole" ) != null;
37 
38         if( System.getProperty( "kjas.useKio" ) != null )
URL.setURLStreamHandlerFactory( new KJASURLStreamHandlerFactory() )39             URL.setURLStreamHandlerFactory( new KJASURLStreamHandlerFactory() );
40 
41         log = System.getProperty( "kjas.log" ) != null;
42 
43         cacheImages = System.getProperty( "kjas.noImageCache" ) != null;
44 
45         // determine system proxy
46         proxyHost = System.getProperty( "http.proxyHost" );
47         String proxyPortString = System.getProperty( "http.proxyPort" );
48         try {
49             proxyPort = Integer.parseInt(proxyPortString);
50         } catch (Exception e) {
51         }
52         //Main.debug( "JVM version = " + System.getProperty( "java.version" ) );
53         String version = System.getProperty("java.version").substring( 0, 3 );
54         // Hack for SGI Java2 runtime
55         if (version == "Jav") {     // Skip over JavaVM-  (the first 7 chars)
56             version = System.getProperty("java.version").substring(7,3);
57         }
58         //Main.debug( "JVM numerical version = " + version );
59         try {
60             java_version = Float.parseFloat( version );
61             if( java_version < 1.2 )
62                 good_jdk = false;
63         } catch( NumberFormatException e ) {
64             good_jdk = false;
65         }
66         PrintStream protocol_stdout = System.out;
67         console = new KJASSwingConsole();
68         protocol = new KJASProtocolHandler( System.in, protocol_stdout );
69     }
70 
71     /**************************************************************************
72      * Public Utility functions available to the KJAS framework
73      **************************************************************************/
debug( String msg )74     public static void debug( String msg )
75     {
76         if( Debug )
77         {
78             System.out.println( "KJAS: " + msg );
79         }
80     }
info(String msg )81     public static void info (String msg ) {
82         System.err.println( "KJAS: " + msg );
83     }
84 
kjas_err( String msg, Exception e )85     public static void kjas_err( String msg, Exception e )
86     {
87         System.err.println( msg );
88         System.err.println( "Backtrace: " );
89         e.printStackTrace();
90     }
91 
kjas_err( String msg, Throwable t )92     public static void kjas_err( String msg, Throwable t )
93     {
94         System.err.println( msg );
95         t.printStackTrace();
96     }
Main()97     private Main() {
98     }
99 
100     /**************************************************************************
101      * Main- create the command loop
102      **************************************************************************/
main( String[] args )103     public static void main( String[] args )
104     {
105         if( !good_jdk )
106         {
107             console.setVisible( true );
108             System.err.println( "ERROR: This version of Java is not supported for security reasons." );
109             System.err.println( "\t\tPlease use Java version 1.2 or higher." );
110             return;
111         }
112 
113         if( show_console )
114             console.setVisible( true );
115 
116         // set up https
117         boolean hasHTTPS = true;
118 
119         try {
120             // https needs a secure socket provider
121             Provider[] sslProviders = Security.getProviders("SSLContext.SSL");
122 
123             if (sslProviders == null || sslProviders.length == 0) {
124                 // as a fallback, try to dynamically install Sun's jsse
125                 Class provider = Class.forName("com.sun.net.ssl.internal.ssl.Provider");
126 
127                 if (provider != null) {
128                     Main.debug("adding Security Provider");
129                     Provider p = (Provider) provider.newInstance();
130                     Security.addProvider(p);
131                 } else {
132                     // Try jessie (http://www.nongnu.org/jessie/) as a fallback
133                     // available in the Free World
134                     provider = Class.forName("org.metastatic.jessie.provider.Jessie");
135                     if (provider != null) {
136                         Main.debug("adding Jessie as Security Provider");
137                         Provider p = (Provider) provider.newInstance();
138                         Security.addProvider(p);
139                     } else {
140                         Main.debug("could not get class: com.sun.net.ssl.internal.ssl.Provider");
141                         hasHTTPS = false;
142                     }
143                 }
144             }
145 
146             if (hasHTTPS) {
147                 // allow user to provide own protocol handler
148                 // -Djava.protocol.handler.pkgs = user.package.name
149                 // getting and setting of properties might generate SecurityExceptions
150                 // so this needs to be in a try block
151                 String handlerPkgs = System.getProperty("java.protocol.handler.pkgs");
152 
153                 if (handlerPkgs == null) {
154                     // set default packages for Sun and IBM
155                     handlerPkgs = "com.sun.net.ssl.internal.www.protocol" +
156                                   "|com.ibm.net.ssl.www.protocol";
157                 } else {
158                     // add default packages for Sun and IBM as fallback
159                     handlerPkgs += "|com.sun.net.ssl.internal.www.protocol" +
160                                    "|com.ibm.net.ssl.www.protocol";
161                 }
162 
163                 System.setProperty("java.protocol.handler.pkgs", handlerPkgs);
164             }
165         } catch (Exception e) {
166             hasHTTPS = false;
167         }
168 
169         if (hasHTTPS == false) {
170             System.out.println("Unable to load JSSE SSL stream handler, https support not available");
171             System.out.println("For more information see http://java.sun.com/products/jsse/");
172         }
173 
174         //start the command parsing
175         protocol.commandLoop();
176     }
177 
178 }
179