You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by jf...@apache.org on 2002/10/18 23:39:57 UTC

cvs commit: jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session DistributedManager.java PersistentManagerBase.java StandardManager.java StandardSessionFacade.java

jfarcand    2002/10/18 14:39:57

  Modified:    catalina/src/share/org/apache/catalina/session
                        DistributedManager.java PersistentManagerBase.java
                        StandardManager.java StandardSessionFacade.java
  Log:
  Security Audit. Manager now needs to call a doPrivilege block when used with the SecurityManager and if they need extra permissions.
  
  Revision  Changes    Path
  1.2       +57 -4     jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/DistributedManager.java
  
  Index: DistributedManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/DistributedManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DistributedManager.java	18 Jul 2002 16:47:51 -0000	1.1
  +++ DistributedManager.java	18 Oct 2002 21:39:57 -0000	1.2
  @@ -74,6 +74,10 @@
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
   import java.io.ObjectStreamClass;
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
  +import java.security.PrivilegedExceptionAction;
  +import java.security.PrivilegedActionException;
   import org.apache.catalina.Cluster;
   import org.apache.catalina.Container;
   import org.apache.catalina.LifecycleException;
  @@ -90,11 +94,34 @@
    * Store to make Sessions persistence.
    *
    * @author Bip Thelin
  + * @author Jean-Francois Arcand
    * @version $Revision$, $Date$
    */
   
   public final class DistributedManager extends PersistentManagerBase {
  +    // ---------------------------------------------------- Security Classes
  +     private class PrivilegedDoCreateSession
  +        implements PrivilegedAction {
   
  +        PrivilegedDoCreateSession() {            
  +        }
  +
  +        public Object run(){
  +           return doCreateSession();
  +        }                       
  +    }   
  +     
  +    private class PrivilegedDoProcessClusterReceiver
  +        implements PrivilegedAction {
  +
  +        PrivilegedDoProcessClusterReceiver() {            
  +        }
  +
  +        public Object run(){
  +           doProcessClusterReceiver();
  +           return null;
  +        }                       
  +    }   
   
       // ----------------------------------------------------- Instance Variables
   
  @@ -149,6 +176,20 @@
        * @return The newly created Session
        */
       public Session createSession() {
  +        if (System.getSecurityManager() != null){   
  +           return (Session) AccessController.doPrivileged( new PrivilegedDoCreateSession() );
  +        } else {
  +            return doCreateSession();
  +        }
  +    }
  +        
  +        
  +    /**
  +     * Create a Session and replicate it in our Cluster
  +     *
  +     * @return The newly created Session
  +     */
  +    private Session doCreateSession(){   
           Session session = super.createSession();
           ObjectOutputStream oos = null;
           ByteArrayOutputStream bos = null;
  @@ -198,6 +239,18 @@
        *
        */
       public void processClusterReceiver() {
  +        if (System.getSecurityManager() != null){   
  +           AccessController.doPrivileged( new PrivilegedDoProcessClusterReceiver() );
  +        } else {
  +            doProcessClusterReceiver();
  +        }        
  +    }
  +    
  +    /**
  +     * Called from our background thread to process new received Sessions
  +     *
  +     */
  +    private void doProcessClusterReceiver() {
           Object[] objs = clusterReceiver.getObjects();
           StandardSession _session = null;
           ByteArrayInputStream bis = null;
  
  
  
  1.3       +150 -29   jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/PersistentManagerBase.java
  
  Index: PersistentManagerBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/PersistentManagerBase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PersistentManagerBase.java	27 Aug 2002 19:11:20 -0000	1.2
  +++ PersistentManagerBase.java	18 Oct 2002 21:39:57 -0000	1.3
  @@ -67,17 +67,11 @@
   import java.beans.PropertyChangeEvent;
   import java.beans.PropertyChangeListener;
   import java.beans.PropertyChangeSupport;
  -import java.io.BufferedInputStream;
  -import java.io.BufferedOutputStream;
  -import java.io.File;
  -import java.io.FileInputStream;
  -import java.io.FileNotFoundException;
  -import java.io.FileOutputStream;
  -import java.io.InputStream;
   import java.io.IOException;
  -import java.io.ObjectInputStream;
  -import java.io.ObjectOutputStream;
  -import java.io.ObjectStreamClass;
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
  +import java.security.PrivilegedExceptionAction;
  +import java.security.PrivilegedActionException;
   import java.util.ArrayList;
   import java.util.Iterator;
   import javax.servlet.ServletContext;
  @@ -105,6 +99,7 @@
    * <code>stop()</code> methods of this class at the correct times.
    *
    * @author Craig R. McClanahan
  + * @author Jean-Francois Arcand
    * @version $Revision$ $Date$
    */
   
  @@ -112,7 +107,74 @@
       extends ManagerBase
       implements Lifecycle, PropertyChangeListener, Runnable {
   
  +    // ---------------------------------------------------- Security Classes
  +     private class PrivilegedStoreClear
  +        implements PrivilegedExceptionAction {
   
  +        PrivilegedStoreClear() {            
  +        }
  +
  +        public Object run() throws Exception{
  +           store.clear();
  +           return null;
  +        }                       
  +    }   
  +     
  +     private class PrivilegedStoreRemove
  +        implements PrivilegedExceptionAction {
  +
  +        private String id;    
  +            
  +        PrivilegedStoreRemove(String id) {     
  +            this.id = id;
  +        }
  +
  +        public Object run() throws Exception{
  +           store.remove(id);
  +           return null;
  +        }                       
  +    }   
  +     
  +    private class PrivilegedStoreLoad
  +        implements PrivilegedExceptionAction {
  +
  +        private String id;    
  +            
  +        PrivilegedStoreLoad(String id) {     
  +            this.id = id;
  +        }
  +
  +        public Object run() throws Exception{
  +           store.load(id);
  +           return null;
  +        }                       
  +    }   
  +          
  +    private class PrivilegedStoreSave
  +        implements PrivilegedExceptionAction {
  +
  +        private Session session;    
  +            
  +        PrivilegedStoreSave(Session session) {     
  +            this.session = session;
  +        }
  +
  +        public Object run() throws Exception{
  +           store.save(session);
  +           return null;
  +        }                       
  +    }   
  +     
  +    private class PrivilegedStoreKeys
  +        implements PrivilegedExceptionAction {
  +
  +        PrivilegedStoreKeys() {     
  +        }
  +
  +        public Object run() throws Exception{
  +           return store.keys();
  +        }                       
  +    }   
       // ----------------------------------------------------- Instance Variables
   
   
  @@ -461,7 +523,6 @@
        * @param store the associated Store
        */
       public void setStore(Store store) {
  -
           this.store = store;
           store.setManager(this);
   
  @@ -525,8 +586,18 @@
           if (store == null)
               return;
   
  -        try {
  -            store.clear();
  +        try {     
  +            if (System.getSecurityManager() != null){
  +                try{
  +                    AccessController.doPrivileged(new PrivilegedStoreClear());
  +                }catch(PrivilegedActionException ex){
  +                    Exception exception = ex.getException();
  +                    log("Exception clearing the Store: " + exception);
  +                    exception.printStackTrace();                        
  +                }
  +            } else {
  +                store.clear();
  +            }
           } catch (IOException e) {
               log("Exception clearing the Store: " + e);
               e.printStackTrace();
  @@ -617,7 +688,17 @@
   
           String[] ids = null;
           try {
  -            ids = store.keys();
  +            if (System.getSecurityManager() != null){
  +                try{
  +                    ids = (String[])AccessController.doPrivileged(new PrivilegedStoreKeys());
  +                }catch(PrivilegedActionException ex){
  +                    Exception exception = ex.getException();
  +                    log("Exception clearing the Store: " + exception);
  +                    exception.printStackTrace();                        
  +                }
  +            } else {
  +                ids = store.keys();
  +            }
           } catch (IOException e) {
               log("Can't load sessions from store, " + e.getMessage(), e);
               return;
  @@ -650,16 +731,36 @@
   
           super.remove (session);
   
  -        if (store != null)
  -            try {
  -                store.remove(session.getId());
  -            } catch (IOException e) {
  -                log("Exception removing session  " + e.getMessage());
  -                e.printStackTrace();
  -            }
  -
  +        if (store != null){
  +            removeSession(session.getId());
  +        }
       }
   
  +    
  +    /**
  +     * Remove this Session from the active Sessions for this Manager,
  +     * and from the Store.
  +     *
  +     * @param is Session's id to be removed
  +     */    
  +    private void removeSession(String id){
  +        try {
  +            if (System.getSecurityManager() != null){
  +                try{
  +                    AccessController.doPrivileged(new PrivilegedStoreRemove(id));
  +                }catch(PrivilegedActionException ex){
  +                    Exception exception = ex.getException();
  +                    log("Exception clearing the Store: " + exception);
  +                    exception.printStackTrace();                        
  +                }
  +            } else {
  +                 store.remove(id);
  +            }               
  +        } catch (IOException e) {
  +            log("Exception removing session  " + e.getMessage());
  +            e.printStackTrace();
  +        }        
  +    }
   
       /**
        * Save all currently active sessions in the appropriate persistence
  @@ -711,7 +812,17 @@
   
           Session session = null;
           try {
  -            session = store.load(id);
  +            if (System.getSecurityManager() != null){
  +                try{
  +                    AccessController.doPrivileged(new PrivilegedStoreLoad(id));
  +                }catch(PrivilegedActionException ex){
  +                    Exception exception = ex.getException();
  +                    log("Exception clearing the Store: " + exception);
  +                    exception.printStackTrace();                        
  +                }
  +            } else {
  +                 store.load(id);
  +            }   
           } catch (ClassNotFoundException e) {
               log(sm.getString("persistentManager.deserializeError", id, e));
               throw new IllegalStateException
  @@ -725,7 +836,7 @@
                   || isSessionStale(session, System.currentTimeMillis())) {
               log("session swapped in is invalid or expired");
               session.expire();
  -            store.remove(id);
  +            removeSession(id);
               return (null);
           }
   
  @@ -777,7 +888,17 @@
               return;
   
           try {
  -            store.save(session);
  +            if (System.getSecurityManager() != null){
  +                try{
  +                    AccessController.doPrivileged(new PrivilegedStoreSave(session));
  +                }catch(PrivilegedActionException ex){
  +                    Exception exception = ex.getException();
  +                    log("Exception clearing the Store: " + exception);
  +                    exception.printStackTrace();                        
  +                }
  +            } else {
  +                 store.save(session);
  +            }   
           } catch (IOException e) {
               log(sm.getString
                   ("persistentManager.serializeError", session.getId(), e));
  
  
  
  1.3       +90 -5     jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/StandardManager.java
  
  Index: StandardManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/StandardManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StandardManager.java	11 Oct 2002 06:58:19 -0000	1.2
  +++ StandardManager.java	18 Oct 2002 21:39:57 -0000	1.3
  @@ -78,6 +78,10 @@
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
   import java.io.ObjectStreamClass;
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
  +import java.security.PrivilegedExceptionAction;
  +import java.security.PrivilegedActionException;
   import java.util.ArrayList;
   import java.util.Iterator;
   import javax.servlet.ServletContext;
  @@ -106,6 +110,7 @@
    * <code>stop()</code> methods of this class at the correct times.
    *
    * @author Craig R. McClanahan
  + * @author Jean-Francois Arcand
    * @version $Revision$ $Date$
    */
   
  @@ -113,7 +118,33 @@
       extends ManagerBase
       implements Lifecycle, PropertyChangeListener, Runnable {
   
  +    // ---------------------------------------------------- Security Classes
  +    private class PrivilegedDoLoad
  +        implements PrivilegedExceptionAction {
   
  +        PrivilegedDoLoad() {           
  +        }
  +
  +        public Object run() throws Exception{
  +           doLoad();
  +           return null;
  +        }                       
  +    }
  +        
  +    private class PrivilegedDoUnload
  +        implements PrivilegedExceptionAction {
  +
  +        PrivilegedDoUnload() {
  +        }
  +
  +        public Object run() throws Exception{
  +            doUnload();
  +            return null;
  +        }            
  +           
  +    }        
  +
  +    
       // ----------------------------------------------------- Instance Variables
   
   
  @@ -347,7 +378,35 @@
        * @exception IOException if an input/output error occurs
        */
       public void load() throws ClassNotFoundException, IOException {
  -
  +        if (System.getSecurityManager() != null){   
  +            try{
  +                AccessController.doPrivileged( new PrivilegedDoLoad() );
  +            } catch (PrivilegedActionException ex){
  +                Exception exception = ex.getException();
  +                if (exception instanceof ClassNotFoundException){
  +                    throw (ClassNotFoundException)exception;
  +                } else if (exception instanceof IOException){
  +                    throw (IOException)exception;
  +                }
  +                if (debug >= 1)
  +                    log("Unreported exception in load() "
  +                        + exception);                
  +            }
  +        } else {
  +            doLoad();
  +        }       
  +    }
  +        
  +    /**
  +     * Load any currently active sessions that were previously unloaded
  +     * to the appropriate persistence mechanism, if any.  If persistence is not
  +     * supported, this method returns without doing anything.
  +     *
  +     * @exception ClassNotFoundException if a serialized class cannot be
  +     *  found during the reload
  +     * @exception IOException if an input/output error occurs
  +     */
  +    private void doLoad() throws ClassNotFoundException, IOException {    
           if (debug >= 1)
               log("Start: Loading persisted sessions");
   
  @@ -463,6 +522,32 @@
        * @exception IOException if an input/output error occurs
        */
       public void unload() throws IOException {
  +        if (System.getSecurityManager() != null){       
  +            try{
  +                AccessController.doPrivileged( new PrivilegedDoUnload() );
  +            } catch (PrivilegedActionException ex){
  +                Exception exception = ex.getException();
  +                if (exception instanceof IOException){
  +                    throw (IOException)exception;
  +                }
  +                if (debug >= 1)
  +                    log("Unreported exception in unLoad() "
  +                        + exception);                
  +            }        
  +        } else {
  +            doUnload();
  +        }       
  +    }
  +        
  +        
  +    /**
  +     * Save any currently active sessions in the appropriate persistence
  +     * mechanism, if any.  If persistence is not supported, this method
  +     * returns without doing anything.
  +     *
  +     * @exception IOException if an input/output error occurs
  +     */
  +    private void doUnload() throws IOException {   
   
           if (debug >= 1)
               log("Unloading persisted sessions");
  
  
  
  1.3       +4 -9      jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/StandardSessionFacade.java
  
  Index: StandardSessionFacade.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/StandardSessionFacade.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StandardSessionFacade.java	8 Aug 2002 04:03:44 -0000	1.2
  +++ StandardSessionFacade.java	18 Oct 2002 21:39:57 -0000	1.3
  @@ -66,11 +66,6 @@
   
   
   import java.io.IOException;
  -import java.io.NotSerializableException;
  -import java.io.ObjectInputStream;
  -import java.io.ObjectOutputStream;
  -import java.io.Serializable;
  -import java.security.Principal;
   import java.util.ArrayList;
   import java.util.Enumeration;
   import java.util.HashMap;
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>