Added hardcoded id to all classes involved in ARP serialiazation. Intended to fix...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / AA_Acl.java
1 /* 
2  * The Shibboleth License, Version 1. 
3  * Copyright (c) 2002 
4  * University Corporation for Advanced Internet Development, Inc. 
5  * All rights reserved
6  * 
7  * 
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions are met:
10  * 
11  * Redistributions of source code must retain the above copyright notice, this 
12  * list of conditions and the following disclaimer.
13  * 
14  * Redistributions in binary form must reproduce the above copyright notice, 
15  * this list of conditions and the following disclaimer in the documentation 
16  * and/or other materials provided with the distribution, if any, must include 
17  * the following acknowledgment: "This product includes software developed by 
18  * the University Corporation for Advanced Internet Development 
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
20  * may appear in the software itself, if and wherever such third-party 
21  * acknowledgments normally appear.
22  * 
23  * Neither the name of Shibboleth nor the names of its contributors, nor 
24  * Internet2, nor the University Corporation for Advanced Internet Development, 
25  * Inc., nor UCAID may be used to endorse or promote products derived from this 
26  * software without specific prior written permission. For written permission, 
27  * please contact shibboleth@shibboleth.org
28  * 
29  * Products derived from this software may not be called Shibboleth, Internet2, 
30  * UCAID, or the University Corporation for Advanced Internet Development, nor 
31  * may Shibboleth appear in their name, without prior written permission of the 
32  * University Corporation for Advanced Internet Development.
33  * 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 package edu.internet2.middleware.shibboleth.aa;
51
52 /**
53  *  Attribute Authority & Release Policy
54  *  Access Control List for ARPs
55  *
56  * @author     Parviz Dousti (dousti@cmu.edu)
57  * @created    June, 2002
58  */
59
60
61 import java.util.*;
62 import java.io.*;
63 import java.security.acl.*;
64
65 public class AA_Acl implements java.security.acl.Acl, Serializable{
66
67         static final long serialVersionUID = 1L;
68     HashSet positives;
69     HashSet negatives;
70     HashSet owners;
71     String name;
72
73     AA_Acl(String name, java.security.Principal root){
74         positives = new HashSet();
75         negatives = new HashSet();
76         owners = new HashSet();
77         this.name = name;
78         owners.add(root);
79     }
80
81     AA_Acl(Collection p, Collection n, String name, java.security.Principal root){
82         positives = new HashSet(p);
83         negatives = new HashSet(n);
84         owners = new HashSet();
85         this.name = name;
86         owners.add(root);
87     }
88
89     /////// Methods //////////
90
91     public boolean addEntry(java.security.Principal p, AclEntry entry)throws NotOwnerException{
92
93         if(this.isOwner(p) == false)
94             throw new NotOwnerException();
95         if(entry.isNegative()){
96             if(negatives.contains(entry)){
97                 return false;
98             }else{
99                 negatives.add(entry);
100                 return true;
101             }
102         }else{  // is positive ACL
103             if(positives.contains(entry)){
104                 return false;
105             }else{
106                 positives.add(entry);
107                 return true;
108             }
109         }
110     }
111
112     public boolean checkPermission(java.security.Principal user, java.security.acl.Permission perm){
113         for(Iterator it = positives.iterator(); it.hasNext();){
114             AclEntry entry = (AclEntry)it.next();
115             java.security.Principal p = entry.getPrincipal();
116             if(p.equals(user)){
117                 if(entry.checkPermission(perm)){
118                     //make sure it is not in negative list
119                     for(Iterator it2 = negatives.iterator(); it2.hasNext();){
120                         AclEntry entry2 = (AclEntry)it2.next();
121                         java.security.Principal p2 = entry2.getPrincipal();
122                         if(p2.equals(user)){
123                             if(entry2.checkPermission(perm)){
124                                 return false; // in both list
125                             }
126                             continue;
127                         }
128                     }
129                     // not in negative list
130                     return true;  // give permission
131                 }else{
132                     continue;
133                 }
134             }else{
135                 continue;
136             }
137         }
138         return false;  // not in any positive entry
139     }
140
141     public Enumeration entries(){
142         return new AclEntryEnumeration(positives, negatives);
143     }
144
145     public String getName(){
146         return name;
147     }
148
149     public Enumeration getPermissions(java.security.Principal user){
150         return new AclPermissionEnumeration(user, positives, negatives);
151     }
152
153     public boolean removeEntry(java.security.Principal caller, AclEntry entry)
154     throws NotOwnerException{
155         
156         if(this.isOwner(caller) == false)
157             throw new NotOwnerException();
158         if(entry.isNegative()){
159             if(negatives.contains(entry)){
160                 negatives.remove(entry);
161                 return true;
162             }else{
163                 return false;
164             }
165         }else{  // is positive ACL
166             if(positives.contains(entry)){
167                 positives.remove(entry);
168                 return true;
169             }else{
170                 return false;
171             }
172         }
173     }
174
175
176     public void setName(java.security.Principal caller, String name)
177         throws NotOwnerException{
178         if(this.isOwner(caller) == false)
179             throw new NotOwnerException();
180         this.name = name;
181     }
182
183     public String toString(){
184         return name+
185             "{"
186             +positives
187             +"}{"
188             +negatives
189             +"}";
190
191     }
192
193     /////////// Owner methods ///////////////
194     public boolean addOwner(java.security.Principal caller, java.security.Principal owner)
195         throws NotOwnerException{
196         if(owners.contains(caller) == false)
197             throw new NotOwnerException();
198         return owners.add(owner);
199     }
200
201     public boolean deleteOwner(java.security.Principal caller, java.security.Principal owner)
202         throws NotOwnerException,
203         LastOwnerException{
204         if(owners.contains(caller) == false)
205             throw new NotOwnerException();
206         return owners.remove(owner);    
207     }
208
209     public boolean isOwner(java.security.Principal owner){
210         return owners.contains(owner);
211     }
212         
213     
214 }
215  
216 class AclEntryEnumeration implements java.util.Enumeration{
217     HashSet entries;
218     Iterator it;
219
220     AclEntryEnumeration(HashSet p, HashSet n){
221         entries = p;
222         entries.addAll(n);  
223         it = entries.iterator();
224     }
225
226     public boolean hasMoreElements(){
227         return it.hasNext();
228     }
229
230     public Object nextElement(){
231         return it.next();
232     }
233 }
234
235
236
237 class AclPermissionEnumeration implements java.util.Enumeration{
238     HashSet permissions;
239     Iterator it;
240
241     AclPermissionEnumeration(java.security.Principal user, HashSet p, HashSet n){
242         permissions = new HashSet();
243         // go throu entries and find the one for this user
244         for(Iterator i = p.iterator(); i.hasNext();){
245             AclEntry ae = (AclEntry)i.next();
246             if(ae.getPrincipal().equals(user)){
247                 // go throu permissions and add it to enum
248                 for(Enumeration j=ae.permissions(); j.hasMoreElements();){
249                     permissions.add((Permission)j.nextElement());
250                 }
251             }
252         }
253         // now go throu negatives and either add it or remove positve one
254         for(Iterator i = n.iterator(); i.hasNext();){
255             AclEntry ae = (AclEntry)i.next();
256             if(ae.getPrincipal().equals(user)){
257                 // go throu permissions and check it
258                 for(Enumeration j=ae.permissions(); j.hasMoreElements();){
259                     Permission perm = (Permission)j.nextElement();
260                     if(permissions.contains(perm)){
261                         permissions.remove(perm);
262                     }else{
263                         permissions.add(perm);
264                     }
265                 }
266             }
267         }
268
269         it = permissions.iterator();
270     }
271
272     public boolean hasMoreElements(){
273         return it.hasNext();
274     }
275
276     public Object nextElement(){
277         return it.next();
278     }
279 }
280
281
282
283
284