1 package org.jacorb.demo.sas; 2 3 import java.io.FileWriter ; 4 import java.io.PrintWriter ; 5 import java.security.Principal ; 6 import java.security.PrivilegedAction ; 7 8 import javax.security.auth.Subject ; 9 import javax.security.auth.login.LoginContext ; 10 import javax.security.auth.login.LoginException ; 11 12 import org.jacorb.sasPolicy.SASPolicyValues; 13 import org.jacorb.sasPolicy.SAS_POLICY_TYPE; 14 import org.jacorb.sasPolicy.SASPolicyValuesHelper; 15 import org.omg.PortableServer.IdAssignmentPolicyValue ; 16 import org.omg.PortableServer.LifespanPolicyValue ; 17 import org.omg.PortableServer.POA ; 18 import org.omg.CORBA.ORB ; 19 import org.omg.CORBA.Any ; 20 import org.omg.CSIIOP.EstablishTrustInClient; 21 22 31 32 public class KerberosServer extends SASDemoPOA { 33 private static Principal myPrincipal = null; 34 private static Subject mySubject = null; 35 private ORB orb; 36 37 public KerberosServer(ORB orb) { 38 this.orb = orb; 39 } 40 41 public void printSAS() { 42 try { 43 org.omg.PortableInterceptor.Current current = (org.omg.PortableInterceptor.Current ) orb.resolve_initial_references("PICurrent"); 44 org.omg.CORBA.Any anyName = current.get_slot(org.jacorb.security.sas.SASInitializer.sasPrincipalNamePIC); 45 String name = anyName.extract_string(); 46 System.out.println("printSAS for user " + name); 47 } catch (Exception e) { 48 System.out.println("printSAS Error: " + e); 49 } 50 } 51 52 public KerberosServer(String [] args) { 53 try { 54 orb = ORB.init(args, null); 56 POA rootPOA = (POA ) orb.resolve_initial_references("RootPOA"); 57 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [3]; 58 policies[0] = rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID); 59 policies[1] = rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT); 60 Any sasAny = orb.create_any(); 61 SASPolicyValuesHelper.insert( sasAny, new SASPolicyValues(EstablishTrustInClient.value, EstablishTrustInClient.value, true) ); 62 policies[2] = orb.create_policy(SAS_POLICY_TYPE.value, sasAny); 63 POA securePOA = rootPOA.create_POA("SecurePOA", rootPOA.the_POAManager(), policies); 64 rootPOA.the_POAManager().activate(); 65 66 securePOA.activate_object_with_id("SecureObject".getBytes(), this); 68 org.omg.CORBA.Object demo = securePOA.servant_to_reference(this); 69 PrintWriter pw = new PrintWriter (new FileWriter (args[0])); 70 pw.println(orb.object_to_string(demo)); 71 pw.flush(); 72 pw.close(); 73 } catch (Exception e) { 74 e.printStackTrace(); 75 } 76 } 77 78 public static void main(String [] args) { 79 if (args.length != 2) { 80 System.out.println("Usage: java demo.sas.KerberosServer <ior_file> <password>"); 81 System.exit(-1); 82 } 83 84 LoginContext loginContext = null; 86 try { 87 JaasTxtCalbackHandler cbHandler = new JaasTxtCalbackHandler(); 88 cbHandler.setMyPassword(args[1].toCharArray()); 89 loginContext = new LoginContext ("KerberosService", cbHandler); 90 loginContext.login(); 91 } catch (LoginException le) { 92 System.out.println("Login error: " + le); 93 System.exit(1); 94 } 95 mySubject = loginContext.getSubject(); 96 myPrincipal = (Principal ) mySubject.getPrincipals().iterator().next(); 97 System.out.println("Found principal " + myPrincipal.getName()); 98 99 final String [] finalArgs = args; 101 try { 102 Subject.doAs(mySubject, new PrivilegedAction () { 103 public Object run() { 104 try { 105 KerberosServer app = new KerberosServer(finalArgs); 107 app.orb.run(); 108 } catch (Exception e) { 109 System.out.println("Error running program: "+e); 110 } 111 return null; 112 } 113 }); 114 } catch (Exception e) { 115 System.out.println("Error running privileged: "+e); 116 } 117 } 118 } 119 | Popular Tags |