001: package org.jacorb.demo.sas;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileReader;
006: import java.security.Principal;
007: import java.security.PrivilegedAction;
008:
009: import javax.security.auth.Subject;
010: import javax.security.auth.login.LoginContext;
011: import javax.security.auth.login.LoginException;
012:
013: import org.omg.CORBA.ORB;
014:
015: /**
016: * This is the client side of the sas demo. It just calls the single
017: * operation "printCert()" of the server. As you can see, sas is fully
018: * transparent.
019: *
020: * @author Nicolas Noffke
021: * @version $Id: KerberosClient.java,v 1.2 2004/02/05 10:49:54 nick.cross Exp $
022: */
023:
024: public class KerberosClient {
025: private static Principal myPrincipal = null;
026: private static Subject mySubject = null;
027: private static ORB orb = null;
028:
029: public KerberosClient(String args[]) {
030:
031: try {
032: // initialize the ORB.
033: orb = ORB.init(args, null);
034:
035: // get the server
036: File f = new File(args[0]);
037: if (!f.exists()) {
038: System.out.println("File " + args[0]
039: + " does not exist.");
040: System.exit(-1);
041: }
042: if (f.isDirectory()) {
043: System.out.println("File " + args[0]
044: + " is a directory.");
045: System.exit(-1);
046: }
047: BufferedReader br = new BufferedReader(new FileReader(f));
048: org.omg.CORBA.Object obj = orb.string_to_object(br
049: .readLine());
050: br.close();
051: SASDemo demo = SASDemoHelper.narrow(obj);
052:
053: //call single operation
054: demo.printSAS();
055: demo.printSAS();
056: demo.printSAS();
057:
058: System.out.println("Call to server succeeded");
059: } catch (Exception ex) {
060: ex.printStackTrace();
061: }
062: }
063:
064: public static void main(String args[]) {
065: if (args.length != 3) {
066: System.out
067: .println("Usage: java demo.sas.KerberosClient <ior_file> <username> <password>");
068: System.exit(1);
069: }
070:
071: // login - with Kerberos
072: LoginContext loginContext = null;
073: try {
074: JaasTxtCalbackHandler txtHandler = new JaasTxtCalbackHandler();
075: txtHandler.setMyUsername(args[1]);
076: txtHandler.setMyPassword(args[2].toCharArray());
077: loginContext = new LoginContext("KerberosClient",
078: txtHandler);
079: loginContext.login();
080: } catch (LoginException le) {
081: System.out.println("Login error: " + le);
082: System.exit(1);
083: }
084: mySubject = loginContext.getSubject();
085: myPrincipal = (Principal) mySubject.getPrincipals().iterator()
086: .next();
087: System.out.println("Found principal " + myPrincipal.getName());
088:
089: // run in privileged mode
090: final String[] finalArgs = args;
091: try {
092: Subject.doAs(mySubject, new PrivilegedAction() {
093: public Object run() {
094: try {
095: KerberosClient client = new KerberosClient(
096: finalArgs);
097: orb.run();
098: } catch (Exception e) {
099: System.out.println("Error running program: "
100: + e);
101: }
102: System.out.println("Exiting privileged operation");
103: return null;
104: }
105: });
106: } catch (Exception e) {
107: System.out.println("Error running privileged: " + e);
108: }
109: }
110: }
|