001    /**
002     * Copyright (c) 2000-2013 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.security.pacl.jndi;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    import com.liferay.portal.security.pacl.PACLPolicy;
019    
020    import java.util.Hashtable;
021    
022    import javax.naming.Binding;
023    import javax.naming.Context;
024    import javax.naming.Name;
025    import javax.naming.NameClassPair;
026    import javax.naming.NameParser;
027    import javax.naming.NamingEnumeration;
028    import javax.naming.NamingException;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class PACLContext implements Context {
034    
035            public PACLContext(Context context, PACLPolicy paclPolicy) {
036                    _context = context;
037                    _paclPolicy = paclPolicy;
038            }
039    
040            public Object addToEnvironment(String propName, Object propVal)
041                    throws NamingException {
042    
043                    return _context.addToEnvironment(propName, propVal);
044            }
045    
046            public void bind(Name name, Object obj) throws NamingException {
047                    checkPACLPolicy(name);
048    
049                    _context.bind(name, obj);
050            }
051    
052            public void bind(String name, Object obj) throws NamingException {
053                    checkPACLPolicy(name);
054    
055                    _context.bind(name, obj);
056            }
057    
058            public void close() throws NamingException {
059                    _context.close();
060            }
061    
062            public Name composeName(Name name, Name prefix) throws NamingException {
063                    checkPACLPolicy(name);
064    
065                    return _context.composeName(name, prefix);
066            }
067    
068            public String composeName(String name, String prefix)
069                    throws NamingException {
070    
071                    checkPACLPolicy(name);
072    
073                    return _context.composeName(name, prefix);
074            }
075    
076            public Context createSubcontext(Name name) throws NamingException {
077                    checkPACLPolicy(name);
078    
079                    return _context.createSubcontext(name);
080            }
081    
082            public Context createSubcontext(String name) throws NamingException {
083                    checkPACLPolicy(name);
084    
085                    return _context.createSubcontext(name);
086            }
087    
088            public void destroySubcontext(Name name) throws NamingException {
089                    checkPACLPolicy(name);
090    
091                    _context.destroySubcontext(name);
092            }
093    
094            public void destroySubcontext(String name) throws NamingException {
095                    checkPACLPolicy(name);
096    
097                    _context.destroySubcontext(name);
098            }
099    
100            @Override
101            public boolean equals(Object obj) {
102                    if (this == obj) {
103                            return true;
104                    }
105    
106                    if (!(obj instanceof Context)) {
107                            return false;
108                    }
109    
110                    Context context = (Context)obj;
111    
112                    if (Validator.equals(_context, context)) {
113                            return true;
114                    }
115    
116                    return false;
117            }
118    
119            public Hashtable<?, ?> getEnvironment() throws NamingException {
120                    return _context.getEnvironment();
121            }
122    
123            public String getNameInNamespace() throws NamingException {
124                    return _context.getNameInNamespace();
125            }
126    
127            public NameParser getNameParser(Name name) throws NamingException {
128                    checkPACLPolicy(name);
129    
130                    return _context.getNameParser(name);
131            }
132    
133            public NameParser getNameParser(String name) throws NamingException {
134                    checkPACLPolicy(name);
135    
136                    return _context.getNameParser(name);
137            }
138    
139            @Override
140            public int hashCode() {
141                    return _context.hashCode();
142            }
143    
144            public NamingEnumeration<NameClassPair> list(Name name)
145                    throws NamingException {
146    
147                    checkPACLPolicy(name);
148    
149                    return _context.list(name);
150            }
151    
152            public NamingEnumeration<NameClassPair> list(String name)
153                    throws NamingException {
154    
155                    checkPACLPolicy(name);
156    
157                    return _context.list(name);
158            }
159    
160            public NamingEnumeration<Binding> listBindings(Name name)
161                    throws NamingException {
162    
163                    checkPACLPolicy(name);
164    
165                    return _context.listBindings(name);
166            }
167    
168            public NamingEnumeration<Binding> listBindings(String name)
169                    throws NamingException {
170    
171                    checkPACLPolicy(name);
172    
173                    return _context.listBindings(name);
174            }
175    
176            public Object lookup(Name name) throws NamingException {
177                    checkPACLPolicy(name);
178    
179                    return _context.lookup(name);
180            }
181    
182            public Object lookup(String name) throws NamingException {
183                    checkPACLPolicy(name);
184    
185                    return _context.lookup(name);
186            }
187    
188            public Object lookupLink(Name name) throws NamingException {
189                    checkPACLPolicy(name);
190    
191                    return _context.lookupLink(name);
192            }
193    
194            public Object lookupLink(String name) throws NamingException {
195                    checkPACLPolicy(name);
196    
197                    return _context.lookupLink(name);
198            }
199    
200            public void rebind(Name name, Object obj) throws NamingException {
201                    checkPACLPolicy(name);
202    
203                    _context.rebind(name, obj);
204            }
205    
206            public void rebind(String name, Object obj) throws NamingException {
207                    checkPACLPolicy(name);
208    
209                    _context.rebind(name, obj);
210            }
211    
212            public Object removeFromEnvironment(String propName)
213                    throws NamingException {
214    
215                    return _context.removeFromEnvironment(propName);
216            }
217    
218            public void rename(Name oldName, Name newName) throws NamingException {
219                    checkPACLPolicy(oldName);
220                    checkPACLPolicy(newName);
221    
222                    _context.rename(oldName, newName);
223            }
224    
225            public void rename(String oldName, String newName) throws NamingException {
226                    checkPACLPolicy(oldName);
227                    checkPACLPolicy(newName);
228    
229                    _context.rename(oldName, newName);
230            }
231    
232            public void unbind(Name name) throws NamingException {
233                    checkPACLPolicy(name);
234    
235                    _context.unbind(name);
236            }
237    
238            public void unbind(String name) throws NamingException {
239                    checkPACLPolicy(name);
240    
241                    _context.unbind(name);
242            }
243    
244            protected void checkPACLPolicy(Name name) {
245                    checkPACLPolicy(name.toString());
246            }
247    
248            protected void checkPACLPolicy(String name) {
249                    if (!_paclPolicy.hasJNDI(name)) {
250                            throw new SecurityException(
251                                    "Attempted to use unapproved JNDI " + name);
252                    }
253            }
254    
255            private Context _context;
256            private PACLPolicy _paclPolicy;
257    
258    }