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

 * 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 Server 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 server 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 viariable to default to true

 * so that JGSS will acquire credentials from the JAAS Subject

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

 *

 

 * The JAASServer is equivalent to its superclass {@link Server Server}

 * in all other respects, and it

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

 *

 * @author Thomas Owusu

 */

 

class JAASServer extends Server

{

    JAASServer(String programName) throws Exception

    {

        super(programName);

    }

 

    static class JAASServerAction implements PrivilegedExceptionAction

    {

        private JAASServer server = null;

 

        JAASServerAction(JAASServer server)

        {

            this.server = server;

        }

 

        public Object run() throws Exception

        {

            server.initialize();

            server.processRequests();

          

            return null;

        }

    }

 

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

    {

        String programName     = "JAASServer";

        Debug dbg              = new Debug();

        try {

            // Don't set useSubjectCredsOnly.

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

 

            JAASServer server = new JAASServer(programName);

 

            server.processArgs(args);

   

            LoginContext loginCtxt = new LoginContext(programName,

                                           new Krb5JAASCallbackHandler());

   

            dbg.out(Debug.OPTS_CAT_APPLICATION, programName + ": Login in ...");

 

            loginCtxt.login();

   

            dbg.out(Debug.OPTS_CAT_APPLICATION, programName + ": Login successful");

   

            Subject subject = loginCtxt.getSubject();

   

            JAASServerAction serverAction = new JAASServerAction(server);

   

            Subject.doAsPrivileged(subject, serverAction, null);

        } catch (Exception exc) {

            dbg.out(Debug.OPTS_CAT_APPLICATION, programName +  " EXCEPTION");

            exc.printStackTrace();

            throw exc;

        }

    }

}