KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > demo > sas > KerberosServer


1 package org.jacorb.demo.sas;
2
3 import java.io.FileWriter JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.security.Principal JavaDoc;
6 import java.security.PrivilegedAction JavaDoc;
7
8 import javax.security.auth.Subject JavaDoc;
9 import javax.security.auth.login.LoginContext JavaDoc;
10 import javax.security.auth.login.LoginException JavaDoc;
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 JavaDoc;
16 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
17 import org.omg.PortableServer.POA JavaDoc;
18 import org.omg.CORBA.ORB JavaDoc;
19 import org.omg.CORBA.Any JavaDoc;
20 import org.omg.CSIIOP.EstablishTrustInClient;
21
22 /**
23  * This is the server part of the sas demo. It demonstrates
24  * how to get access to the certificates that the client sent
25  * for mutual authentication. The certificate chain can be
26  * accessed via the Security Level 2 interfaces.
27  *
28  * @author Nicolas Noffke
29  * @version $Id: KerberosServer.java,v 1.2 2004/02/05 10:49:54 nick.cross Exp $
30  */

31
32 public class KerberosServer extends SASDemoPOA {
33     private static Principal JavaDoc myPrincipal = null;
34     private static Subject JavaDoc mySubject = null;
35     private ORB JavaDoc orb;
36
37     public KerberosServer(ORB JavaDoc orb) {
38         this.orb = orb;
39     }
40
41     public void printSAS() {
42         try {
43             org.omg.PortableInterceptor.Current JavaDoc current = (org.omg.PortableInterceptor.Current JavaDoc) orb.resolve_initial_references("PICurrent");
44             org.omg.CORBA.Any JavaDoc anyName = current.get_slot(org.jacorb.security.sas.SASInitializer.sasPrincipalNamePIC);
45             String JavaDoc name = anyName.extract_string();
46             System.out.println("printSAS for user " + name);
47         } catch (Exception JavaDoc e) {
48             System.out.println("printSAS Error: " + e);
49         }
50     }
51
52     public KerberosServer(String JavaDoc[] args) {
53         try {
54             // initialize the ORB and POA.
55
orb = ORB.init(args, null);
56             POA JavaDoc rootPOA = (POA JavaDoc) orb.resolve_initial_references("RootPOA");
57             org.omg.CORBA.Policy JavaDoc [] policies = new org.omg.CORBA.Policy JavaDoc[3];
58             policies[0] = rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
59             policies[1] = rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
60             Any JavaDoc 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 JavaDoc securePOA = rootPOA.create_POA("SecurePOA", rootPOA.the_POAManager(), policies);
64             rootPOA.the_POAManager().activate();
65
66             // create object and write out IOR
67
securePOA.activate_object_with_id("SecureObject".getBytes(), this);
68             org.omg.CORBA.Object JavaDoc demo = securePOA.servant_to_reference(this);
69             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(args[0]));
70             pw.println(orb.object_to_string(demo));
71             pw.flush();
72             pw.close();
73         } catch (Exception JavaDoc e) {
74             e.printStackTrace();
75         }
76     }
77
78     public static void main(String JavaDoc[] 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         // login - with Kerberos
85
LoginContext JavaDoc loginContext = null;
86         try {
87             JaasTxtCalbackHandler cbHandler = new JaasTxtCalbackHandler();
88             cbHandler.setMyPassword(args[1].toCharArray());
89             loginContext = new LoginContext JavaDoc("KerberosService", cbHandler);
90             loginContext.login();
91         } catch (LoginException JavaDoc le) {
92             System.out.println("Login error: " + le);
93             System.exit(1);
94         }
95         mySubject = loginContext.getSubject();
96         myPrincipal = (Principal JavaDoc) mySubject.getPrincipals().iterator().next();
97         System.out.println("Found principal " + myPrincipal.getName());
98
99         // run in privileged mode
100
final String JavaDoc[] finalArgs = args;
101         try {
102             Subject.doAs(mySubject, new PrivilegedAction JavaDoc() {
103                 public Object JavaDoc run() {
104                     try {
105                         // create application
106
KerberosServer app = new KerberosServer(finalArgs);
107                         app.orb.run();
108                     } catch (Exception JavaDoc e) {
109                         System.out.println("Error running program: "+e);
110                     }
111                     return null;
112                 }
113             });
114         } catch (Exception JavaDoc e) {
115             System.out.println("Error running privileged: "+e);
116         }
117     }
118 }
119
Popular Tags