001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 Networks Associates Technology, Inc
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: *
026: * CHANGE RECORD
027: * -
028: */
029: package org.cougaar.lib.web.tomcat;
030:
031: import java.beans.PropertyChangeListener;
032: import java.security.Principal;
033: import java.security.cert.X509Certificate;
034:
035: import org.apache.catalina.Container;
036: import org.apache.catalina.Realm;
037:
038: /**
039: * A Realm extension for Tomcat 4.0 that will use
040: * org.cougaar.core.security.crypto.ldap.KeyRingJNDIRealm if it
041: * exists and the System property
042: * <code>org.cougaar.lib.web.tomcat.enableAuth</code>
043: * is "true".
044: * <p>
045: * The <code>server.xml</code> should have within the <Engine> section:
046: * <pre>
047: * <Realm className="org.cougaar.lib.web.tomcat.SecureRealm" />
048: * </pre>
049: *
050: *
051: * @property org.cougaar.lib.web.tomcat.realm.class
052: * classname for realm.
053: * @property org.cougaar.lib.web.tomcat.enableAuth
054: * enable default realm if classname property is
055: * not specified.
056: */
057: public class SecureRealm implements Realm {
058:
059: private static final String PROP_ENABLE = "org.cougaar.lib.web.tomcat.enableAuth";
060: private static final String PROP_CLASS = "org.cougaar.lib.web.tomcat.realm.class";
061: private static final String DEFAULT_SECURE = "org.cougaar.core.security.crypto.ldap.KeyRingJNDIRealm";
062:
063: private Realm _secureRealm = null;
064: private Container _container = null;
065:
066: /**
067: * Default constructor.
068: */
069: public SecureRealm() {
070: String realmClass = System.getProperty(PROP_CLASS);
071:
072: if (realmClass == null && Boolean.getBoolean(PROP_ENABLE)) {
073: realmClass = DEFAULT_SECURE;
074: }
075:
076: if (realmClass != null) {
077: try {
078: Class c = Class.forName(realmClass);
079: _secureRealm = (Realm) c.newInstance();
080: } catch (ClassNotFoundException e) {
081: System.err.println("Error: could not find class "
082: + realmClass);
083: } catch (ClassCastException e) {
084: System.err.println("Error: the class " + realmClass
085: + " is not a Realm");
086: } catch (Exception e) {
087: System.err.println("Error: could not load the class "
088: + realmClass);
089: }
090: }
091: }
092:
093: /**
094: * returns the KeyRingJNDIRealm if it is available.
095: */
096: public Realm getRealm() {
097: return _secureRealm;
098: }
099:
100: /**
101: * Uses the KeyRingJNDIRealm's addPropertyChangeListener if available.
102: */
103: public void addPropertyChangeListener(
104: PropertyChangeListener listener) {
105: if (_secureRealm != null) {
106: _secureRealm.addPropertyChangeListener(listener);
107: }
108: }
109:
110: /**
111: * Authenticates using the KeyRingJNDIRealm if available.
112: */
113: public Principal authenticate(String username, String credentials) {
114: if (_secureRealm != null) {
115: return _secureRealm.authenticate(username, credentials);
116: }
117: return null;
118: }
119:
120: /**
121: * Authenticates using the KeyRingJNDIRealm if available.
122: */
123: public Principal authenticate(String username, byte[] credentials) {
124: if (_secureRealm != null) {
125: return _secureRealm.authenticate(username, credentials);
126: }
127: return null;
128: }
129:
130: /**
131: * Authenticates using the KeyRingJNDIRealm if available.
132: */
133: public Principal authenticate(X509Certificate certs[]) {
134: if (_secureRealm != null) {
135: return _secureRealm.authenticate(certs);
136: }
137: return null;
138: }
139:
140: /**
141: * Authenticates using the KeyRingJNDIRealm if available.
142: */
143: public Principal authenticate(String username, String clientDigest,
144: String nOnce, String nc, String cnonce, String qop,
145: String realm, String md5a2) {
146: if (_secureRealm != null) {
147: return _secureRealm.authenticate(username, clientDigest,
148: nOnce, nc, cnonce, qop, realm, md5a2);
149: }
150: return null;
151: }
152:
153: /**
154: * Uses the KeyRingJNDIRealm getContainer() if available
155: */
156: public Container getContainer() {
157: if (_secureRealm != null) {
158: return _secureRealm.getContainer();
159: }
160: return _container;
161: }
162:
163: /**
164: * Uses the KeyRingJNDIRealm getInfo() if available. Otherwise it returns
165: * "SecureRealm";
166: */
167: public String getInfo() {
168: if (_secureRealm != null) {
169: return _secureRealm.getInfo();
170: }
171: return "SecureRealm";
172: }
173:
174: /**
175: * Uses the KeyRingJNDIRealm hasRole() if available. Otherwise it returns
176: * false always
177: */
178: public boolean hasRole(Principal user, String role) {
179: if (_secureRealm != null) {
180: return _secureRealm.hasRole(user, role);
181: }
182: return false;
183: }
184:
185: /**
186: * Uses the KeyRingJNDIRealm removePropertyChangeListener() if available.
187: */
188: public void removePropertyChangeListener(
189: PropertyChangeListener listener) {
190: if (_secureRealm != null) {
191: _secureRealm.removePropertyChangeListener(listener);
192: }
193: }
194:
195: /**
196: * Uses the KeyRingJNDIRealm setContainer() if available. Otherwise it
197: * sets the value to be returned by getContainer()
198: */
199: public void setContainer(Container container) {
200: if (_secureRealm != null) {
201: _secureRealm.setContainer(container);
202: }
203: _container = container;
204: }
205: }
|