001: package org.jacorb.demo.sas;
002:
003: import java.io.FileWriter;
004: import java.io.PrintWriter;
005: import java.security.Principal;
006: import java.security.PrivilegedAction;
007:
008: import javax.security.auth.Subject;
009: import javax.security.auth.login.LoginContext;
010: import javax.security.auth.login.LoginException;
011:
012: import org.jacorb.sasPolicy.SASPolicyValues;
013: import org.jacorb.sasPolicy.SAS_POLICY_TYPE;
014: import org.jacorb.sasPolicy.SASPolicyValuesHelper;
015: import org.omg.PortableServer.IdAssignmentPolicyValue;
016: import org.omg.PortableServer.LifespanPolicyValue;
017: import org.omg.PortableServer.POA;
018: import org.omg.CORBA.ORB;
019: import org.omg.CORBA.Any;
020: import org.omg.CSIIOP.EstablishTrustInClient;
021:
022: /**
023: * This is the server part of the sas demo. It demonstrates
024: * how to get access to the certificates that the client sent
025: * for mutual authentication. The certificate chain can be
026: * accessed via the Security Level 2 interfaces.
027: *
028: * @author Nicolas Noffke
029: * @version $Id: KerberosServer.java,v 1.2 2004/02/05 10:49:54 nick.cross Exp $
030: */
031:
032: public class KerberosServer extends SASDemoPOA {
033: private static Principal myPrincipal = null;
034: private static Subject mySubject = null;
035: private ORB orb;
036:
037: public KerberosServer(ORB orb) {
038: this .orb = orb;
039: }
040:
041: public void printSAS() {
042: try {
043: org.omg.PortableInterceptor.Current current = (org.omg.PortableInterceptor.Current) orb
044: .resolve_initial_references("PICurrent");
045: org.omg.CORBA.Any anyName = current
046: .get_slot(org.jacorb.security.sas.SASInitializer.sasPrincipalNamePIC);
047: String name = anyName.extract_string();
048: System.out.println("printSAS for user " + name);
049: } catch (Exception e) {
050: System.out.println("printSAS Error: " + e);
051: }
052: }
053:
054: public KerberosServer(String[] args) {
055: try {
056: // initialize the ORB and POA.
057: orb = ORB.init(args, null);
058: POA rootPOA = (POA) orb
059: .resolve_initial_references("RootPOA");
060: org.omg.CORBA.Policy[] policies = new org.omg.CORBA.Policy[3];
061: policies[0] = rootPOA
062: .create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
063: policies[1] = rootPOA
064: .create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
065: Any sasAny = orb.create_any();
066: SASPolicyValuesHelper.insert(sasAny, new SASPolicyValues(
067: EstablishTrustInClient.value,
068: EstablishTrustInClient.value, true));
069: policies[2] = orb.create_policy(SAS_POLICY_TYPE.value,
070: sasAny);
071: POA securePOA = rootPOA.create_POA("SecurePOA", rootPOA
072: .the_POAManager(), policies);
073: rootPOA.the_POAManager().activate();
074:
075: // create object and write out IOR
076: securePOA.activate_object_with_id(
077: "SecureObject".getBytes(), this );
078: org.omg.CORBA.Object demo = securePOA
079: .servant_to_reference(this );
080: PrintWriter pw = new PrintWriter(new FileWriter(args[0]));
081: pw.println(orb.object_to_string(demo));
082: pw.flush();
083: pw.close();
084: } catch (Exception e) {
085: e.printStackTrace();
086: }
087: }
088:
089: public static void main(String[] args) {
090: if (args.length != 2) {
091: System.out
092: .println("Usage: java demo.sas.KerberosServer <ior_file> <password>");
093: System.exit(-1);
094: }
095:
096: // login - with Kerberos
097: LoginContext loginContext = null;
098: try {
099: JaasTxtCalbackHandler cbHandler = new JaasTxtCalbackHandler();
100: cbHandler.setMyPassword(args[1].toCharArray());
101: loginContext = new LoginContext("KerberosService",
102: cbHandler);
103: loginContext.login();
104: } catch (LoginException le) {
105: System.out.println("Login error: " + le);
106: System.exit(1);
107: }
108: mySubject = loginContext.getSubject();
109: myPrincipal = (Principal) mySubject.getPrincipals().iterator()
110: .next();
111: System.out.println("Found principal " + myPrincipal.getName());
112:
113: // run in privileged mode
114: final String[] finalArgs = args;
115: try {
116: Subject.doAs(mySubject, new PrivilegedAction() {
117: public Object run() {
118: try {
119: // create application
120: KerberosServer app = new KerberosServer(
121: finalArgs);
122: app.orb.run();
123: } catch (Exception e) {
124: System.out.println("Error running program: "
125: + e);
126: }
127: return null;
128: }
129: });
130: } catch (Exception e) {
131: System.out.println("Error running privileged: " + e);
132: }
133: }
134: }
|