dos2unix'ed
[sfa.git] / sfa / trust / credential_legacy.py
1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
13 #
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS 
21 # IN THE WORK.
22 #----------------------------------------------------------------------
23 ##
24 # Implements SFA Credentials
25 #
26 # Credentials are layered on top of certificates, and are essentially a
27 # certificate that stores a tuple of parameters.
28 ##
29
30
31 import xmlrpclib
32
33 from sfa.util.faults import MissingDelegateBit, ChildRightsNotSubsetOfParent
34 from sfa.trust.certificate import Certificate
35 from sfa.trust.gid import GID
36
37 ##
38 # Credential is a tuple:
39 #     (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
40 #
41 # These fields are encoded using xmlrpc into the subjectAltName field of the
42 # x509 certificate. Note: Call encode() once the fields have been filled in
43 # to perform this encoding.
44
45 class CredentialLegacy(Certificate):
46     gidCaller = None
47     gidObject = None
48     lifeTime = None
49     privileges = None
50     delegate = False
51
52     ##
53     # Create a Credential object
54     #
55     # @param create If true, create a blank x509 certificate
56     # @param subject If subject!=None, create an x509 cert with the subject name
57     # @param string If string!=None, load the credential from the string
58     # @param filename If filename!=None, load the credential from the file
59
60     def __init__(self, create=False, subject=None, string=None, filename=None):
61         Certificate.__init__(self, create, subject, string, filename)
62
63     ##
64     # set the GID of the caller
65     #
66     # @param gid GID object of the caller
67
68     def set_gid_caller(self, gid):
69         self.gidCaller = gid
70         # gid origin caller is the caller's gid by default
71         self.gidOriginCaller = gid
72
73     ##
74     # get the GID of the object
75
76     def get_gid_caller(self):
77         if not self.gidCaller:
78             self.decode()
79         return self.gidCaller
80
81     ##
82     # set the GID of the object
83     #
84     # @param gid GID object of the object
85
86     def set_gid_object(self, gid):
87         self.gidObject = gid
88
89     ##
90     # get the GID of the object
91
92     def get_gid_object(self):
93         if not self.gidObject:
94             self.decode()
95         return self.gidObject
96
97     ##
98     # set the lifetime of this credential
99     #
100     # @param lifetime lifetime of credential
101
102     def set_lifetime(self, lifeTime):
103         self.lifeTime = lifeTime
104
105     ##
106     # get the lifetime of the credential
107
108     def get_lifetime(self):
109         if not self.lifeTime:
110             self.decode()
111         return self.lifeTime
112
113     ##
114     # set the delegate bit
115     #
116     # @param delegate boolean (True or False)
117
118     def set_delegate(self, delegate):
119         self.delegate = delegate
120
121     ##
122     # get the delegate bit
123
124     def get_delegate(self):
125         if not self.delegate:
126             self.decode()
127         return self.delegate
128
129     ##
130     # set the privileges
131     #
132     # @param privs either a comma-separated list of privileges of a Rights object
133
134     def set_privileges(self, privs):
135         if isinstance(privs, str):
136             self.privileges = Rights(string = privs)
137         else:
138             self.privileges = privs
139
140     ##
141     # return the privileges as a Rights object
142
143     def get_privileges(self):
144         if not self.privileges:
145             self.decode()
146         return self.privileges
147
148     ##
149     # determine whether the credential allows a particular operation to be
150     # performed
151     #
152     # @param op_name string specifying name of operation ("lookup", "update", etc)
153
154     def can_perform(self, op_name):
155         rights = self.get_privileges()
156         if not rights:
157             return False
158         return rights.can_perform(op_name)
159
160     ##
161     # Encode the attributes of the credential into a string and store that
162     # string in the alt-subject-name field of the X509 object. This should be
163     # done immediately before signing the credential.
164
165     def encode(self):
166         dict = {"gidCaller": None,
167                 "gidObject": None,
168                 "lifeTime": self.lifeTime,
169                 "privileges": None,
170                 "delegate": self.delegate}
171         if self.gidCaller:
172             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
173         if self.gidObject:
174             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
175         if self.privileges:
176             dict["privileges"] = self.privileges.save_to_string()
177         str = xmlrpclib.dumps((dict,), allow_none=True)
178         self.set_data('URI:http://' + str)
179
180     ##
181     # Retrieve the attributes of the credential from the alt-subject-name field
182     # of the X509 certificate. This is automatically done by the various
183     # get_* methods of this class and should not need to be called explicitly.
184
185     def decode(self):
186         data = self.get_data().lstrip('URI:http://')
187         
188         if data:
189             dict = xmlrpclib.loads(data)[0][0]
190         else:
191             dict = {}
192
193         self.lifeTime = dict.get("lifeTime", None)
194         self.delegate = dict.get("delegate", None)
195
196         privStr = dict.get("privileges", None)
197         if privStr:
198             self.privileges = Rights(string = privStr)
199         else:
200             self.privileges = None
201
202         gidCallerStr = dict.get("gidCaller", None)
203         if gidCallerStr:
204             self.gidCaller = GID(string=gidCallerStr)
205         else:
206             self.gidCaller = None
207
208         gidObjectStr = dict.get("gidObject", None)
209         if gidObjectStr:
210             self.gidObject = GID(string=gidObjectStr)
211         else:
212             self.gidObject = None
213
214     ##
215     # Verify that a chain of credentials is valid (see cert.py:verify). In
216     # addition to the checks for ordinary certificates, verification also
217     # ensures that the delegate bit was set by each parent in the chain. If
218     # a delegate bit was not set, then an exception is thrown.
219     #
220     # Each credential must be a subset of the rights of the parent.
221
222     def verify_chain(self, trusted_certs = None):
223         # do the normal certificate verification stuff
224         Certificate.verify_chain(self, trusted_certs)
225
226         if self.parent:
227             # make sure the parent delegated rights to the child
228             if not self.parent.get_delegate():
229                 raise MissingDelegateBit(self.parent.get_subject())
230
231             # make sure the rights given to the child are a subset of the
232             # parents rights
233             if not self.parent.get_privileges().is_superset(self.get_privileges()):
234                 raise ChildRightsNotSubsetOfParent(self.get_subject() 
235                                                    + " " + self.parent.get_privileges().save_to_string()
236                                                    + " " + self.get_privileges().save_to_string())
237
238         return
239
240     ##
241     # Dump the contents of a credential to stdout in human-readable format
242     #
243     # @param dump_parents If true, also dump the parent certificates
244
245     def dump(self, *args, **kwargs):
246         print self.dump_string(*args,**kwargs)
247
248     def dump_string(self, dump_parents=False):
249         result=""
250         result += "CREDENTIAL %s\n" % self.get_subject()
251
252         result += "      privs: %s\n" % self.get_privileges().save_to_string()
253
254         gidCaller = self.get_gid_caller()
255         if gidCaller:
256             result += "  gidCaller:\n"
257             gidCaller.dump(8, dump_parents)
258
259         gidObject = self.get_gid_object()
260         if gidObject:
261             result += "  gidObject:\n"
262             result += gidObject.dump_string(8, dump_parents)
263
264         result += "   delegate: %s" % self.get_delegate()
265
266         if self.parent and dump_parents:
267             result += "PARENT\n"
268             result += self.parent.dump_string(dump_parents)
269
270         return result