* ===========================================================================

 * Licensed Materials - Property of IBM

 * IBM Security Software Development Kit, Java (tm) Technology Edition

 *

 * (C) Copyright IBM Corp. 2002, 2005 All Rights Reserved.

 *

 *  US Government Users Restricted Rights - Use, duplication or

 *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

 * ===========================================================================

 

// IBM JGSS 1.0.1 Sample JAAS-Enabled Client Program

 
package com.ibm.security.jgss.test;
 
import com.ibm.security.jgss.Debug;
import com.ibm.security.auth.callback.Krb5CallbackHandler;
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import java.security.PrivilegedExceptionAction;
 
/**
 * A JGSS sample client that uses JAAS.
 * 

 

 * It does a JAAS login and operates within the JAAS login context so created.

 *

 

 * It does not set the JAVA variable

 * javax.security.auth.useSubjectCredsOnly, leaving

 * the variable to default to true

 * so that JGSS will acquire credentials from the JAAS Subject

 * associated with login context (created by the client).

 *

 

 * The JAASClient is equivalent to its superclass {@link Client Client}

 * in all other respects, and it

 * can be run against the non-JAAS sample clients and servers.

 *

 * @author Thomas Owusu

 * @version %I%, %G%

 */

 

class JAASClient extends Client

{

    JAASClient(String programName) throws Exception

    {

        // Don't set useSubjectCredsOnly. Set only the program name.

        // useSubjectCredsOnly default to "true" if not set.

        super(programName);

    }

 

    static class JAASClientAction implements PrivilegedExceptionAction

    {

         private JAASClient client;

 

         public JAASClientAction(JAASClient client)

         {

             this.client = client;

         }     

 

         public Object run () throws Exception

         {

             client.initialize();

             client.interactWithAcceptor();

             return null;

         }

    }

 

    public static void main(String args[]) throws Exception

    {

        String programName = "JAASClient";

        JAASClient client = null;

        Debug dbg = new Debug();

 

        try {

            client = new JAASClient(programName);//use Subject creds

            client.processArgs(args);

 

            LoginContext loginCtxt = new LoginContext("JAASClient",

                                       new Krb5CallbackHandler());

 

            loginCtxt.login();

 

            dbg.out(Debug.OPTS_CAT_APPLICATION,

                       programName + ": Kerberos login OK");

 

            Subject subject = loginCtxt.getSubject();

 

            PrivilegedExceptionAction jaasClientAction

                            = new JAASClientAction(client);

 

            Subject.doAsPrivileged(subject, jaasClientAction, null);

     

        } catch (Exception exc) {

            dbg.out(Debug.OPTS_CAT_APPLICATION,

                        programName + " Exception: " + exc.toString());

            exc.printStackTrace();

            throw exc;

        } finally {

            try {

                if (client != null)

                    client.dispose();

            } catch (Exception exc) {}

        }

 

        dbg.out(Debug.OPTS_CAT_APPLICATION,

                       programName + ": Done ...");

    }

}