KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > jacc > ContextPolicy


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.jacc;
8
9 import java.security.Permission JavaDoc;
10 import java.security.PermissionCollection JavaDoc;
11 import java.security.Permissions JavaDoc;
12 import java.security.Principal JavaDoc;
13 import java.security.ProtectionDomain JavaDoc;
14 import java.security.acl.Group JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import javax.security.jacc.PolicyContextException JavaDoc;
20
21 import org.jboss.logging.Logger;
22
23 /** The permissions for a JACC context id. This implementation is based on
24  * the 3.2.x model of associating the declarative roles with the Subject of
25  * the authenticated caller. This allows the 3.2.x login modules to be used
26  * as the source of the authentication and authorization information.
27  *
28  * @author Scott.Stark@jboss.org
29  * @version $Revison:$
30  */

31 public class ContextPolicy
32 {
33    private static Logger log = Logger.getLogger(ContextPolicy.class);
34    private String JavaDoc contextID;
35    private Permissions JavaDoc excludedPermissions = new Permissions JavaDoc();
36    private Permissions JavaDoc uncheckedPermissions = new Permissions JavaDoc();
37    /** HashMap<String, Permissions> role name to permissions mapping */
38    private HashMap JavaDoc rolePermissions = new HashMap JavaDoc();
39    /** Flag indicating if our category is at trace level for logging */
40    private boolean trace;
41
42    ContextPolicy(String JavaDoc contextID)
43    {
44       this.contextID = contextID;
45       this.trace = log.isTraceEnabled();
46    }
47
48    Permissions JavaDoc getPermissions()
49    {
50       Permissions JavaDoc perms = new Permissions JavaDoc();
51       Enumeration JavaDoc eter = uncheckedPermissions.elements();
52       while( eter.hasMoreElements() )
53       {
54          Permission JavaDoc p = (Permission JavaDoc) eter.nextElement();
55          perms.add(p);
56       }
57       Iterator JavaDoc iter = rolePermissions.values().iterator();
58       while( iter.hasNext() )
59       {
60          Permissions JavaDoc rp = (Permissions JavaDoc) iter.next();
61          eter = rp.elements();
62          while( eter.hasMoreElements() )
63          {
64             Permission JavaDoc p = (Permission JavaDoc) eter.nextElement();
65             perms.add(p);
66          }
67       }
68       return perms;
69    }
70
71    boolean implies(ProtectionDomain JavaDoc domain, Permission JavaDoc permission)
72    {
73       boolean implied = false;
74       // First check the excluded permissions
75
if( excludedPermissions.implies(permission) )
76       {
77          if( trace )
78             log.trace("Denied: Matched excluded set, permission="+permission);
79          return false;
80       }
81
82       // Next see if this matches an unchecked permission
83
if( uncheckedPermissions.implies(permission) )
84       {
85          if( trace )
86             log.trace("Allowed: Matched unchecked set, permission="+permission);
87          return true;
88       }
89
90       // Check principal to role permissions
91
Principal JavaDoc[] principals = domain.getPrincipals();
92       int length = principals != null ? principals.length : 0;
93       ArrayList JavaDoc princpalNames = new ArrayList JavaDoc();
94       for(int n = 0; n < length; n ++)
95       {
96          Principal JavaDoc p = principals[n];
97          if( p instanceof Group JavaDoc )
98          {
99             Group JavaDoc g = (Group JavaDoc) p;
100             Enumeration JavaDoc iter = g.members();
101             while( iter.hasMoreElements() )
102             {
103                p = (Principal JavaDoc) iter.nextElement();
104                String JavaDoc name = p.getName();
105                princpalNames.add(name);
106             }
107          }
108          else
109          {
110             String JavaDoc name = p.getName();
111             princpalNames.add(name);
112          }
113       }
114       if( princpalNames.size() > 0 )
115       {
116          for(int n = 0; implied == false && n < princpalNames.size(); n ++)
117          {
118             String JavaDoc name = (String JavaDoc) princpalNames.get(n);
119             Permissions JavaDoc perms = (Permissions JavaDoc) rolePermissions.get(name);
120             if( trace )
121                log.trace("Checking role="+name+" perms="+perms);
122             if( perms == null )
123                continue;
124             implied = perms.implies(permission);
125             if( trace )
126                log.trace((implied ? "Allowed: " : "Denied: ")+" permission="+permission);
127          }
128       }
129       else
130       {
131          if( trace )
132             log.trace("No principals found in domain: "+domain);
133       }
134
135       return implied;
136    }
137
138    void clear()
139    {
140       excludedPermissions = new Permissions JavaDoc();
141       uncheckedPermissions = new Permissions JavaDoc();
142       rolePermissions.clear();
143    }
144
145    void addToExcludedPolicy(Permission JavaDoc permission)
146       throws PolicyContextException JavaDoc
147    {
148       excludedPermissions.add(permission);
149    }
150    
151    void addToExcludedPolicy(PermissionCollection JavaDoc permissions)
152       throws PolicyContextException JavaDoc
153    {
154       Enumeration JavaDoc iter = permissions.elements();
155       while( iter.hasMoreElements() )
156       {
157          Permission JavaDoc p = (Permission JavaDoc) iter.nextElement();
158          excludedPermissions.add(p);
159       }
160    }
161
162    void addToRole(String JavaDoc roleName, Permission JavaDoc permission)
163       throws PolicyContextException JavaDoc
164    {
165       Permissions JavaDoc perms = (Permissions JavaDoc) rolePermissions.get(roleName);
166       if( perms == null )
167       {
168          perms = new Permissions JavaDoc();
169          rolePermissions.put(roleName, perms);
170       }
171       perms.add(permission);
172    }
173
174    void addToRole(String JavaDoc roleName, PermissionCollection JavaDoc permissions)
175       throws PolicyContextException JavaDoc
176    {
177       Permissions JavaDoc perms = (Permissions JavaDoc) rolePermissions.get(roleName);
178       if( perms == null )
179       {
180          perms = new Permissions JavaDoc();
181          rolePermissions.put(roleName, perms);
182       }
183       Enumeration JavaDoc iter = permissions.elements();
184       while( iter.hasMoreElements() )
185       {
186          Permission JavaDoc p = (Permission JavaDoc) iter.nextElement();
187          perms.add(p);
188       }
189    }
190
191    void addToUncheckedPolicy(Permission JavaDoc permission)
192       throws PolicyContextException JavaDoc
193    {
194       uncheckedPermissions.add(permission);
195    }
196
197    void addToUncheckedPolicy(PermissionCollection JavaDoc permissions)
198       throws PolicyContextException JavaDoc
199    {
200       Enumeration JavaDoc iter = permissions.elements();
201       while( iter.hasMoreElements() )
202       {
203          Permission JavaDoc p = (Permission JavaDoc) iter.nextElement();
204          uncheckedPermissions.add(p);
205       }
206    }
207
208    void commit()
209       throws PolicyContextException JavaDoc
210    {
211    }
212
213    void delete()
214       throws PolicyContextException JavaDoc
215    {
216       clear();
217    }
218
219    String JavaDoc getContextID()
220       throws PolicyContextException JavaDoc
221    {
222       return contextID;
223    }
224
225    void linkConfiguration(ContextPolicy link)
226       throws PolicyContextException JavaDoc
227    {
228    }
229
230    void removeExcludedPolicy()
231       throws PolicyContextException JavaDoc
232    {
233       excludedPermissions = new Permissions JavaDoc();
234    }
235
236    void removeRole(String JavaDoc roleName)
237       throws PolicyContextException JavaDoc
238    {
239       rolePermissions.remove(roleName);
240    }
241
242    void removeUncheckedPolicy()
243       throws PolicyContextException JavaDoc
244    {
245       uncheckedPermissions = new Permissions JavaDoc();
246    }
247
248    public String JavaDoc toString()
249    {
250       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("<ContextPolicy contextID='");
251       tmp.append(contextID);
252       tmp.append("'>\n");
253       tmp.append("\t<ExcludedPermissions>\n");
254       Enumeration JavaDoc iter = excludedPermissions.elements();
255       while( iter.hasMoreElements() )
256       {
257          Permission JavaDoc p = (Permission JavaDoc) iter.nextElement();
258          tmp.append("<Permission type='");
259          tmp.append(p.getClass());
260          tmp.append("' name='");
261          tmp.append(p.getName());
262          tmp.append("' actions='");
263          tmp.append(p.getActions());
264          tmp.append("' />\n");
265       }
266       tmp.append("\t</ExcludedPermissions>\n");
267
268       tmp.append("\t<UncheckedPermissions>\n");
269       iter = uncheckedPermissions.elements();
270       while( iter.hasMoreElements() )
271       {
272          Permission JavaDoc p = (Permission JavaDoc) iter.nextElement();
273          tmp.append("<Permission type='");
274          tmp.append(p.getClass());
275          tmp.append(" name='");
276          tmp.append(p.getName());
277          tmp.append("' actions='");
278          tmp.append(p.getActions());
279          tmp.append("' />\n");
280       }
281       tmp.append("\t</UncheckedPermissions>\n");
282
283       tmp.append("\t<RolePermssions>\n");
284       Iterator JavaDoc roles = rolePermissions.keySet().iterator();
285       while( roles.hasNext() )
286       {
287          String JavaDoc role = (String JavaDoc) roles.next();
288          Permissions JavaDoc perms = (Permissions JavaDoc) rolePermissions.get(role);
289          iter = perms.elements();
290          tmp.append("\t\t<Role name='"+role+"'>\n");
291          while( iter.hasMoreElements() )
292          {
293             Permission JavaDoc p = (Permission JavaDoc) iter.nextElement();
294             tmp.append("<Permission type='");
295             tmp.append(p.getClass());
296             tmp.append(" name='");
297             tmp.append(p.getName());
298             tmp.append("' actions='");
299             tmp.append(p.getActions());
300             tmp.append("' />\n");
301          }
302          tmp.append("\t\t</Role>\n");
303       }
304       tmp.append("\t</RolePermssions>");
305       tmp.append("</ContextPolicy>\n");
306       return tmp.toString();
307    }
308 }
309
Popular Tags