001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.jndi.pacl;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    import com.liferay.portal.security.pacl.PACLPolicy;
019    
020    import java.lang.Object;
021    import java.lang.String;
022    
023    import java.util.Hashtable;
024    
025    import javax.naming.Binding;
026    import javax.naming.Context;
027    import javax.naming.Name;
028    import javax.naming.NameClassPair;
029    import javax.naming.NameParser;
030    import javax.naming.NamingEnumeration;
031    import javax.naming.NamingException;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class PACLContext implements Context {
037    
038            public PACLContext(Context context, PACLPolicy paclPolicy) {
039                    _context = context;
040                    _paclPolicy = paclPolicy;
041            }
042    
043            public Object addToEnvironment(String propName, Object propVal)
044                    throws NamingException {
045    
046                    return _context.addToEnvironment(propName, propVal);
047            }
048    
049            public void bind(Name name, Object obj) throws NamingException {
050                    checkPACLPolicy(name);
051    
052                    _context.bind(name, obj);
053            }
054    
055            public void bind(String name, Object obj) throws NamingException {
056                    checkPACLPolicy(name);
057    
058                    _context.bind(name, obj);
059            }
060    
061            public void close() throws NamingException {
062                    _context.close();
063            }
064    
065            public Name composeName(Name name, Name prefix) throws NamingException {
066                    checkPACLPolicy(name);
067    
068                    return _context.composeName(name, prefix);
069            }
070    
071            public String composeName(String name, String prefix)
072                    throws NamingException {
073    
074                    checkPACLPolicy(name);
075    
076                    return _context.composeName(name, prefix);
077            }
078    
079            public Context createSubcontext(Name name) throws NamingException {
080                    checkPACLPolicy(name);
081    
082                    return _context.createSubcontext(name);
083            }
084    
085            public Context createSubcontext(String name) throws NamingException {
086                    checkPACLPolicy(name);
087    
088                    return _context.createSubcontext(name);
089            }
090    
091            public void destroySubcontext(Name name) throws NamingException {
092                    checkPACLPolicy(name);
093    
094                    _context.destroySubcontext(name);
095            }
096    
097            public void destroySubcontext(String name) throws NamingException {
098                    checkPACLPolicy(name);
099    
100                    _context.destroySubcontext(name);
101            }
102    
103            @Override
104            public boolean equals(Object obj) {
105                    if (this == obj) {
106                            return true;
107                    }
108    
109                    if (!(obj instanceof Context)) {
110                            return false;
111                    }
112    
113                    Context context = (Context)obj;
114    
115                    if (Validator.equals(_context, context)) {
116                            return true;
117                    }
118    
119                    return false;
120            }
121    
122            public Hashtable<?, ?> getEnvironment() throws NamingException {
123                    return _context.getEnvironment();
124            }
125    
126            public String getNameInNamespace() throws NamingException {
127                    return _context.getNameInNamespace();
128            }
129    
130            public NameParser getNameParser(Name name) throws NamingException {
131                    checkPACLPolicy(name);
132    
133                    return _context.getNameParser(name);
134            }
135    
136            public NameParser getNameParser(String name) throws NamingException {
137                    checkPACLPolicy(name);
138    
139                    return _context.getNameParser(name);
140            }
141    
142            @Override
143            public int hashCode() {
144                    return _context.hashCode();
145            }
146    
147            public NamingEnumeration<NameClassPair> list(Name name)
148                    throws NamingException {
149    
150                    checkPACLPolicy(name);
151    
152                    return _context.list(name);
153            }
154    
155            public NamingEnumeration<NameClassPair> list(String name)
156                    throws NamingException {
157    
158                    checkPACLPolicy(name);
159    
160                    return _context.list(name);
161            }
162    
163            public NamingEnumeration<Binding> listBindings(Name name)
164                    throws NamingException {
165    
166                    checkPACLPolicy(name);
167    
168                    return _context.listBindings(name);
169            }
170    
171            public NamingEnumeration<Binding> listBindings(String name)
172                    throws NamingException {
173    
174                    checkPACLPolicy(name);
175    
176                    return _context.listBindings(name);
177            }
178    
179            public Object lookup(Name name) throws NamingException {
180                    checkPACLPolicy(name);
181    
182                    return _context.lookup(name);
183            }
184    
185            public Object lookup(String name) throws NamingException {
186                    checkPACLPolicy(name);
187    
188                    return _context.lookup(name);
189            }
190    
191            public Object lookupLink(Name name) throws NamingException {
192                    checkPACLPolicy(name);
193    
194                    return _context.lookupLink(name);
195            }
196    
197            public Object lookupLink(String name) throws NamingException {
198                    checkPACLPolicy(name);
199    
200                    return _context.lookupLink(name);
201            }
202    
203            public void rebind(Name name, Object obj) throws NamingException {
204                    checkPACLPolicy(name);
205    
206                    _context.rebind(name, obj);
207            }
208    
209            public void rebind(String name, Object obj) throws NamingException {
210                    checkPACLPolicy(name);
211    
212                    _context.rebind(name, obj);
213            }
214    
215            public Object removeFromEnvironment(String propName)
216                    throws NamingException {
217    
218                    return _context.removeFromEnvironment(propName);
219            }
220    
221            public void rename(Name oldName, Name newName) throws NamingException {
222                    checkPACLPolicy(oldName);
223                    checkPACLPolicy(newName);
224    
225                    _context.rename(oldName, newName);
226            }
227    
228            public void rename(String oldName, String newName) throws NamingException {
229                    checkPACLPolicy(oldName);
230                    checkPACLPolicy(newName);
231    
232                    _context.rename(oldName, newName);
233            }
234    
235            public void unbind(Name name) throws NamingException {
236                    checkPACLPolicy(name);
237    
238                    _context.unbind(name);
239            }
240    
241            public void unbind(String name) throws NamingException {
242                    checkPACLPolicy(name);
243    
244                    _context.unbind(name);
245            }
246    
247            protected void checkPACLPolicy(Name name) {
248                    checkPACLPolicy(name.toString());
249            }
250    
251            protected void checkPACLPolicy(String name) {
252                    if (!_paclPolicy.hasJNDI(name)) {
253                            throw new SecurityException(
254                                    "Attempted to use unapproved JNDI " + name);
255                    }
256            }
257    
258            private Context _context;
259            private PACLPolicy _paclPolicy;
260    
261    }