001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * Sudo.java
020: *
021: * The object responsible for running code as a specified user.
022: */
023:
024: // package path
025: package com.rift.coad.lib.security.sudo;
026:
027: // log 4 j imports
028: import org.apache.log4j.Logger;
029:
030: // coadunation imports
031: import com.rift.coad.lib.configuration.Configuration;
032: import com.rift.coad.lib.configuration.ConfigurationFactory;
033: import com.rift.coad.lib.security.AuthorizationException;
034: import com.rift.coad.lib.security.UserSession;
035: import com.rift.coad.lib.security.ThreadPermissionSession;
036: import com.rift.coad.lib.security.ThreadsPermissionContainer;
037: import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
038: import com.rift.coad.lib.security.Validator;
039: import com.rift.coad.lib.security.user.UserSessionManager;
040: import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
041: import com.rift.coad.lib.security.user.UserStoreManager;
042: import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
043: import com.rift.coad.lib.thread.BasicThread;
044:
045: /**
046: * The object responsible for running code as a specified user.
047: *
048: * @author Brett Chaldecott
049: */
050: public class Sudo {
051:
052: // class constants
053: private final static String ROLE = "role";
054:
055: // static member variables
056: private static Logger log = Logger.getLogger(Sudo.class.getName());
057: private static String role = null;
058:
059: // setup the role
060: static {
061: try {
062: Configuration configuration = ConfigurationFactory
063: .getInstance().getConfig(Sudo.class);
064: role = configuration.getString(ROLE);
065: } catch (Exception ex) {
066: log.error("Failed to retrieve the sudo role : "
067: + ex.getMessage(), ex);
068: }
069: }
070:
071: /**
072: * Creates a new instance of Sudo
073: */
074: private Sudo() {
075: }
076:
077: /**
078: * This method will get called to run a thread as another user.
079: *
080: * @param username The name of the user to run the handler as.
081: * @param handler The reference to the object that will be called after the
082: * user has been set correctly.
083: * @exception SudoException
084: * @exception Exception
085: */
086: public static void sudoThreadByUser(String username,
087: SudoCallbackHandler handler) throws SudoException,
088: Exception {
089: Validator.validate(Sudo.class, role);
090: ThreadsPermissionContainer threadsPermissionContainer = ThreadsPermissionContainerAccessor
091: .getInstance().getThreadsPermissionContainer();
092: UserStoreManager userStoreManager = UserStoreManagerAccessor
093: .getInstance().getUserStoreManager();
094:
095: // retrieve the use session information
096: Thread currentThread = null;
097: ThreadPermissionSession currentPermissions = null;
098: UserSession newUserSession = null;
099: try {
100: // retrieve the current user session
101: currentThread = Thread.currentThread();
102: currentPermissions = threadsPermissionContainer
103: .getSession(new Long(currentThread.getId()));
104: newUserSession = userStoreManager.getUserInfo(username);
105: } catch (Exception ex) {
106: throw new SudoException(
107: "Failed to retrieve the necessary user information : "
108: + ex.getMessage(), ex);
109: }
110:
111: // set user
112: threadsPermissionContainer.putSession(new Long(currentThread
113: .getId()), new ThreadPermissionSession(new Long(
114: currentThread.getId()), newUserSession));
115: log.info("Set [" + currentThread.getId() + "] user from ["
116: + currentPermissions.getUser().getName() + "] to ["
117: + newUserSession.getName()
118: + "] to run the command on : "
119: + handler.getClass().getName());
120:
121: try {
122: handler.process();
123: } finally {
124: // reset the user session
125: threadsPermissionContainer.putSession(new Long(
126: currentThread.getId()), currentPermissions);
127: // set the user back
128: log.info("Set user back from [" + newUserSession.getName()
129: + "] to [" + currentPermissions.getUser().getName()
130: + "] after running command on : "
131: + handler.getClass().getName());
132: }
133: }
134:
135: /**
136: * This method will sudo a user to a user session id.
137: *
138: * @param sessionId The id of the session to sudo.
139: * @param handler The reference to the handler.
140: * @exception SudoException
141: * @exception Exception
142: */
143: public static void sudoThreadBySessionId(String sessionId,
144: SudoCallbackHandler handler) throws SudoException,
145: Exception {
146: Validator.validate(Sudo.class, role);
147: ThreadsPermissionContainer threadsPermissionContainer = ThreadsPermissionContainerAccessor
148: .getInstance().getThreadsPermissionContainer();
149: UserSessionManager userSessionManager = UserSessionManagerAccessor
150: .getInstance().getUserSessionManager();
151: // retrieve the use session information
152: Thread currentThread = null;
153: ThreadPermissionSession currentPermissions = null;
154: UserSession newUserSession = null;
155: try {
156: // retrieve the current user session
157: currentThread = Thread.currentThread();
158: currentPermissions = threadsPermissionContainer
159: .getSession(new Long(currentThread.getId()));
160: newUserSession = userSessionManager
161: .getSessionById(sessionId);
162: } catch (Exception ex) {
163: throw new SudoException(
164: "Failed to retrieve the necessary user information : "
165: + ex.getMessage(), ex);
166: }
167:
168: // set user
169: threadsPermissionContainer.putSession(new Long(currentThread
170: .getId()), new ThreadPermissionSession(new Long(
171: currentThread.getId()), newUserSession));
172:
173: log.info("Set [" + currentThread.getId() + "] user from ["
174: + currentPermissions.getUser().getName() + "] to ["
175: + newUserSession.getName()
176: + "] to run the command on : "
177: + handler.getClass().getName());
178:
179: try {
180: handler.process();
181: } finally {
182: // reset the user session
183: threadsPermissionContainer.putSession(new Long(
184: currentThread.getId()), currentPermissions);
185:
186: // set the user back
187: log.info("Set user back from [" + newUserSession.getName()
188: + "] to [" + currentPermissions.getUser().getName()
189: + "] after running command on : "
190: + handler.getClass().getName());
191: }
192: }
193: }
|