/* class WshApplet demonstrates downloading a wsh script file from a signed applet, saving the script file locally, and starting the wsh script and retrieving and displaying the std output from the script. Code for Netscape or Microsoft JVM. M. Gallant 06/07/2002 */ import com.ms.security.*; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.io.* ; import java.net.*; import java.util.*; import netscape.security.PrivilegeManager; public class WshApplet extends Applet implements ActionListener { private TextArea ta = new TextArea (25, 60); private Button startbutton = new Button("Download and Execute Wsh Script") ; private String osname; private TextField tfcommand = new TextField(" "); private BufferedReader dis = null ; private String scripthost = "cscript " ; private String command = "" ; private String tempscriptfile = "_javasciencetempscript.vbs"; private String inp = ""; private final String urlstring = "http://home.istar.ca/~neutron/wsh/capicom/eval/PVKCertsInfo.vbs" ; public void init() { try { if (Class.forName("com.ms.security.PolicyEngine") != null) // required for IE PolicyEngine.assertPermission(PermissionID.SYSTEM); } catch (Throwable cnfe) { } add(new Label("Script: " + urlstring)) ; this.setBackground(Color.white) ; startbutton.addActionListener(this) ; add(startbutton) ; add(ta) ; // text area to receive console output of script startbutton.setBackground(Color.red) ; try{ PrivilegeManager.enablePrivilege("UniversalFileAccess") ; // required for NN PrivilegeManager.enablePrivilege("UniversalExecAccess") ; // required for NN } catch(Exception cnfe) { //System.out.println("netscape.security.PrivilegeManager class not found") ; } } public void actionPerformed(ActionEvent e) { if( (e.getActionCommand()).equals("Download and Execute Wsh Script")) { ta.setText("") ; //clear output byte [] urlcontent = this.rawurlcontent(urlstring); if (urlcontent.length == 0){ //if no content available, or bad url ta.setText("No content available or bad url") ; return ; } try{ PrivilegeManager.enablePrivilege("UniversalFileAccess") ; // required to read file via NN PrivilegeManager.enablePrivilege("UniversalExecAccess") ; // required to exec file via NN } catch(Exception cnfe) { System.out.println("netscape.security.PrivilegeManager class not found") ; //IE will report this } this.saveScriptFile(tempscriptfile, urlcontent); //save local script file try { command = scripthost + tempscriptfile ; Runtime rt = Runtime.getRuntime() ; Process proc = rt.exec(command) ; dis = new BufferedReader(new InputStreamReader(proc.getInputStream())) ; while((inp = dis.readLine()) !=null) ta.append(inp+ "\n") ; proc.waitFor() ; proc.destroy() ; dis.close() ; // close all streams. ta.append("Successfull reading of process output\n") ; } catch (Exception inte) { ta.append("Problem reading process: " + command) ; } } } private final byte [] rawurlcontent(String urlspec) { if(urlspec.trim().toLowerCase().indexOf("http://") < 0) urlspec = "http://" + urlspec; URL url = null; BufferedInputStream buffin = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(1024) ; try { // get raw URL content response into ByteArrayOutputStream as quickly as possible. url = new URL(urlspec) ; buffin = new BufferedInputStream(url.openStream(), 512) ; while (true) { int datum = buffin.read() ; if (datum == -1) break ; baos.write(datum) ; } buffin.close() ; byte[] ba = baos.toByteArray() ; return ba; } // end try catch (MalformedURLException mue) { byte[] mfearray = {0}; return mfearray; } catch (IOException ieo) { byte[] ioearray = {0}; return ioearray ; } finally { try { if (buffin != null) buffin.close(); } catch (IOException e) {} } } private final boolean saveScriptFile(String filename, byte[] content){ File thefile = new File(filename); if (thefile.exists()) { ta.append("File: " + filename + " already exists ... using that file\n"); ta.append("Filepath: " + thefile.getAbsolutePath()+ "\n") ; return false; } try { FileOutputStream fwri = new FileOutputStream(thefile); fwri.write(content); fwri.flush(); fwri.close(); return true; } catch(IOException ioe) { return false; } } }