Debian Codegraph @

ipapython/session_storage.py

raw
1 #
2 # Copyright (C) 2017 FreeIPA Contributors see COPYING for license
3 #
4
5 import ctypes
6 import sys
7
8
9 KRB5_CC_NOSUPP = -1765328137
10
11 if sys.platform == 'darwin':
12 LIBKRB5_FILENAME = 'libkrb5.dylib'
13 else:
14 LIBKRB5_FILENAME = 'libkrb5.so.3'
15
16 try:
17 LIBKRB5 = ctypes.CDLL(LIBKRB5_FILENAME)
18 except OSError as e: # pragma: no cover
19 raise ImportError(str(e))
20
21 krb5_int32 = ctypes.c_int32
22 krb5_error_code = krb5_int32
23 krb5_magic = krb5_error_code
24 krb5_enctype = krb5_int32
25 krb5_octet = ctypes.c_uint8
26 krb5_timestamp = krb5_int32
27
28 class _krb5_context(ctypes.Structure): # noqa
29 """krb5/krb5.h struct _krb5_context"""
30 _fields_ = []
31
32
33 class _krb5_ccache(ctypes.Structure): # noqa
34 """krb5/krb5.h struct _krb5_ccache"""
35 _fields_ = []
36
37
38 class _krb5_data(ctypes.Structure): # noqa
39 """krb5/krb5.h struct _krb5_data"""
40 _fields_ = [
41 ("magic", krb5_magic),
42 ("length", ctypes.c_uint),
43 ("data", ctypes.c_char_p),
44 ]
45
46
47 class krb5_principal_data(ctypes.Structure): # noqa
48 """krb5/krb5.h struct krb5_principal_data"""
49 _fields_ = []
50
51
52 class _krb5_keyblock(ctypes.Structure): # noqa
53 """krb5/krb5.h struct _krb5_keyblock"""
54 _fields_ = [
55 ("magic", krb5_magic),
56 ("enctype", krb5_enctype),
57 ("length", ctypes.c_uint),
58 ("contents", ctypes.POINTER(krb5_octet))
59 ]
60
61
62 class _krb5_ticket_times(ctypes.Structure): # noqa
63 """krb5/krb5.h struct _krb5_ticket_times"""
64 _fields_ = [
65 ("authtime", krb5_timestamp),
66 ("starttime", krb5_timestamp),
67 ("endtime", krb5_timestamp),
68 ("renew_till", krb5_timestamp),
69 ]
70
71
72 class _krb5_address(ctypes.Structure): # noqa
73 """krb5/krb5.h struct _krb5_address"""
74 _fields_ = []
75
76
77 class _krb5_authdata(ctypes.Structure): # noqa
78 """krb5/krb5.h struct _krb5_authdata"""
79 _fields_ = []
80
81
82 krb5_principal = ctypes.POINTER(krb5_principal_data)
83 krb5_keyblock = _krb5_keyblock
84 krb5_ticket_times = _krb5_ticket_times
85 krb5_boolean = ctypes.c_uint
86 krb5_flags = krb5_int32
87 krb5_data = _krb5_data
88 krb5_address_p = ctypes.POINTER(_krb5_address)
89 krb5_authdata_p = ctypes.POINTER(_krb5_authdata)
90
91
92 class _krb5_creds(ctypes.Structure): # noqa
93 """krb5/krb5.h struct _krb5_creds"""
94 _fields_ = [
95 ("magic", krb5_magic),
96 ("client", krb5_principal),
97 ("server", krb5_principal),
98 ("keyblock", krb5_keyblock),
99 ("times", krb5_ticket_times),
100 ("is_skey", krb5_boolean),
101 ("ticket_flags", krb5_flags),
102 ("addresses", ctypes.POINTER(krb5_address_p)),
103 ("ticket", krb5_data),
104 ("second_ticket", krb5_data),
105 ("authdata", ctypes.POINTER(krb5_authdata_p))
106 ]
107
108
109 class KRB5Error(Exception):
110 pass
111
112
113 def krb5_errcheck(result, func, arguments):
114 """Error checker for krb5_error_code return value"""
115 if result != 0:
116 raise KRB5Error(result, func.__name__, arguments)
117
118
119 krb5_context = ctypes.POINTER(_krb5_context)
120 krb5_ccache = ctypes.POINTER(_krb5_ccache)
121 krb5_data_p = ctypes.POINTER(_krb5_data)
122 krb5_creds = _krb5_creds
123 krb5_pointer = ctypes.c_void_p
124 krb5_cc_cursor = krb5_pointer
125
126 krb5_init_context = LIBKRB5.krb5_init_context
127 krb5_init_context.argtypes = (ctypes.POINTER(krb5_context), )
128 krb5_init_context.restype = krb5_error_code
129 krb5_init_context.errcheck = krb5_errcheck
130
131 krb5_free_context = LIBKRB5.krb5_free_context
132 krb5_free_context.argtypes = (krb5_context, )
133 krb5_free_context.restype = None
134
135 krb5_free_principal = LIBKRB5.krb5_free_principal
136 krb5_free_principal.argtypes = (krb5_context, krb5_principal)
137 krb5_free_principal.restype = None
138
139 krb5_free_data_contents = LIBKRB5.krb5_free_data_contents
140 krb5_free_data_contents.argtypes = (krb5_context, krb5_data_p)
141 krb5_free_data_contents.restype = None
142
143 krb5_cc_default = LIBKRB5.krb5_cc_default
144 krb5_cc_default.argtypes = (krb5_context, ctypes.POINTER(krb5_ccache), )
145 krb5_cc_default.restype = krb5_error_code
146 krb5_cc_default.errcheck = krb5_errcheck
147
148 krb5_cc_close = LIBKRB5.krb5_cc_close
149 krb5_cc_close.argtypes = (krb5_context, krb5_ccache, )
150 krb5_cc_close.restype = krb5_error_code
151 krb5_cc_close.errcheck = krb5_errcheck
152
153 krb5_parse_name = LIBKRB5.krb5_parse_name
154 krb5_parse_name.argtypes = (krb5_context, ctypes.c_char_p,
155 ctypes.POINTER(krb5_principal), )
156 krb5_parse_name.restype = krb5_error_code
157 krb5_parse_name.errcheck = krb5_errcheck
158
159 krb5_cc_set_config = LIBKRB5.krb5_cc_set_config
160 krb5_cc_set_config.argtypes = (krb5_context, krb5_ccache, krb5_principal,
161 ctypes.c_char_p, krb5_data_p, )
162 krb5_cc_set_config.restype = krb5_error_code
163 krb5_cc_set_config.errcheck = krb5_errcheck
164
165 krb5_cc_get_principal = LIBKRB5.krb5_cc_get_principal
166 krb5_cc_get_principal.argtypes = (krb5_context, krb5_ccache,
167 ctypes.POINTER(krb5_principal), )
168 krb5_cc_get_principal.restype = krb5_error_code
169 krb5_cc_get_principal.errcheck = krb5_errcheck
170
171 # krb5_build_principal is a variadic function but that can't be expressed
172 # in a ctypes argtypes definition, so I explicitly listed the number of
173 # arguments we actually use through the code for type checking purposes
174 krb5_build_principal = LIBKRB5.krb5_build_principal
175 krb5_build_principal.argtypes = (krb5_context, ctypes.POINTER(krb5_principal),
176 ctypes.c_uint, ctypes.c_char_p,
177 ctypes.c_char_p, ctypes.c_char_p,
178 ctypes.c_char_p, ctypes.c_char_p, )
179 krb5_build_principal.restype = krb5_error_code
180 krb5_build_principal.errcheck = krb5_errcheck
181
182 krb5_cc_start_seq_get = LIBKRB5.krb5_cc_start_seq_get
183 krb5_cc_start_seq_get.argtypes = (krb5_context, krb5_ccache,
184 ctypes.POINTER(krb5_cc_cursor), )
185 krb5_cc_start_seq_get.restype = krb5_error_code
186 krb5_cc_start_seq_get.errcheck = krb5_errcheck
187
188 krb5_cc_next_cred = LIBKRB5.krb5_cc_next_cred
189 krb5_cc_next_cred.argtypes = (krb5_context, krb5_ccache,
190 ctypes.POINTER(krb5_cc_cursor),
191 ctypes.POINTER(krb5_creds), )
192 krb5_cc_next_cred.restype = krb5_error_code
193 krb5_cc_next_cred.errcheck = krb5_errcheck
194
195 krb5_cc_end_seq_get = LIBKRB5.krb5_cc_end_seq_get
196 krb5_cc_end_seq_get.argtypes = (krb5_context, krb5_ccache,
197 ctypes.POINTER(krb5_cc_cursor), )
198 krb5_cc_end_seq_get.restype = krb5_error_code
199 krb5_cc_end_seq_get.errcheck = krb5_errcheck
200
201 krb5_free_cred_contents = LIBKRB5.krb5_free_cred_contents
202 krb5_free_cred_contents.argtypes = (krb5_context, ctypes.POINTER(krb5_creds))
203 krb5_free_cred_contents.restype = None
204
205 krb5_principal_compare = LIBKRB5.krb5_principal_compare
206 krb5_principal_compare.argtypes = (krb5_context, krb5_principal,
207 krb5_principal, )
208 krb5_principal_compare.restype = krb5_boolean
209
210 krb5_unparse_name = LIBKRB5.krb5_unparse_name
211 krb5_unparse_name.argtypes = (krb5_context, krb5_principal,
212 ctypes.POINTER(ctypes.c_char_p), )
213 krb5_unparse_name.restype = krb5_error_code
214 krb5_unparse_name.errcheck = krb5_errcheck
215
216 krb5_free_unparsed_name = LIBKRB5.krb5_free_unparsed_name
217 krb5_free_unparsed_name.argtypes = (krb5_context, ctypes.c_char_p, )
218 krb5_free_unparsed_name.restype = None
219
220 CONF_REALM = b"X-CACHECONF:"
221 CONF_NAME = b"krb5_ccache_conf_data"
222
223
224 def store_data(princ_name, key, value):
225 """
226 Stores the session cookie in a hidden ccache entry.
227 """
228 if not isinstance(princ_name, bytes):
229 princ_name = princ_name.encode('utf-8')
230 if not isinstance(key, bytes):
231 key = key.encode('ascii')
232 if not isinstance(value, bytes):
233 value = value.encode('utf-8')
234
235 # FILE ccaches grow every time an entry is stored, so we need
236 # to avoid storing the same entry multiple times.
237 oldvalue = get_data(princ_name, key)
238 if oldvalue == value:
239 return
240
241 context = krb5_context()
242 principal = krb5_principal()
243 ccache = krb5_ccache()
244
245 try:
246 krb5_init_context(ctypes.byref(context))
247
248 krb5_parse_name(context, ctypes.c_char_p(princ_name),
249 ctypes.byref(principal))
250
251 krb5_cc_default(context, ctypes.byref(ccache))
252
253 buf = ctypes.create_string_buffer(value)
254 data = _krb5_data()
255 data.data = buf.value
256 data.length = len(buf)
257 krb5_cc_set_config(context, ccache, principal, key,
258 ctypes.byref(data))
259
260 finally:
261 if principal:
262 krb5_free_principal(context, principal)
263 if ccache:
264 krb5_cc_close(context, ccache)
265 if context:
266 krb5_free_context(context)
267
268
269 def get_data(princ_name, key):
270 """
271 Gets the session cookie in a hidden ccache entry.
272 """
273 if not isinstance(princ_name, bytes):
274 princ_name = princ_name.encode('utf-8')
275 if not isinstance(key, bytes):
276 key = key.encode('utf-8')
277
278 context = krb5_context()
279 principal = krb5_principal()
280 srv_princ = krb5_principal()
281 ccache = krb5_ccache()
282 pname_princ = krb5_principal()
283 pname = ctypes.c_char_p()
284
285 try:
286 krb5_init_context(ctypes.byref(context))
287
288 krb5_cc_default(context, ctypes.byref(ccache))
289 krb5_cc_get_principal(context, ccache, ctypes.byref(principal))
290
291 # We need to parse and then unparse the name in case the pric_name
292 # passed in comes w/o a realm attached
293 krb5_parse_name(context, ctypes.c_char_p(princ_name),
294 ctypes.byref(pname_princ))
295 krb5_unparse_name(context, pname_princ, ctypes.byref(pname))
296
297 krb5_build_principal(context, ctypes.byref(srv_princ),
298 len(CONF_REALM), ctypes.c_char_p(CONF_REALM),
299 ctypes.c_char_p(CONF_NAME), ctypes.c_char_p(key),
300 pname, ctypes.c_char_p(None))
301
302 # Unfortunately we can't just use krb5_cc_get_config()
303 # because of bugs in some ccache handling code in krb5
304 # libraries that would always return the first entry
305 # stored and not the last one, which is the one we want.
306 cursor = krb5_cc_cursor()
307 creds = krb5_creds()
308 got_creds = False
309 krb5_cc_start_seq_get(context, ccache, ctypes.byref(cursor))
310 try:
311 while True:
312 checkcreds = krb5_creds()
313 # the next function will throw an error and break out of the
314 # while loop when we try to access past the last cred
315 try:
316 krb5_cc_next_cred(context, ccache, ctypes.byref(cursor),
317 ctypes.byref(checkcreds))
318 except KRB5Error:
319 break
320
321 if (krb5_principal_compare(context, principal,
322 checkcreds.client) == 1 and
323 krb5_principal_compare(context, srv_princ,
324 checkcreds.server) == 1):
325 if got_creds:
326 krb5_free_cred_contents(context, ctypes.byref(creds))
327 creds = checkcreds
328 got_creds = True
329 # We do not stop here, as we want the LAST entry
330 # in the ccache for those ccaches that cannot delete
331 # but only always append, like FILE
332 else:
333 krb5_free_cred_contents(context,
334 ctypes.byref(checkcreds))
335 finally:
336 krb5_cc_end_seq_get(context, ccache, ctypes.byref(cursor))
337
338 if got_creds:
339 data = creds.ticket.data
340 krb5_free_cred_contents(context, ctypes.byref(creds))
341 return data
342
343 finally:
344 if principal:
345 krb5_free_principal(context, principal)
346 if srv_princ:
347 krb5_free_principal(context, srv_princ)
348 if pname_princ:
349 krb5_free_principal(context, pname_princ)
350 if pname:
351 krb5_free_unparsed_name(context, pname)
352 if ccache:
353 krb5_cc_close(context, ccache)
354 if context:
355 krb5_free_context(context)
356 return None
357
358
359 def remove_data(princ_name, key):
360 """
361 Removes the hidden ccache entry with the session cookie.
362 """
363 if not isinstance(princ_name, bytes):
364 princ_name = princ_name.encode('utf-8')
365 if not isinstance(key, bytes):
366 key = key.encode('utf-8')
367
368 context = krb5_context()
369 principal = krb5_principal()
370 ccache = krb5_ccache()
371
372 try:
373 krb5_init_context(ctypes.byref(context))
374
375 krb5_parse_name(context, ctypes.c_char_p(princ_name),
376 ctypes.byref(principal))
377
378 krb5_cc_default(context, ctypes.byref(ccache))
379
380 try:
381 krb5_cc_set_config(context, ccache, principal, key, None)
382 except KRB5Error as e:
383 if e.args[0] == KRB5_CC_NOSUPP:
384 # removal not supported with this CC type, just pass
385 pass
386
387 finally:
388 if principal:
389 krb5_free_principal(context, principal)
390 if ccache:
391 krb5_cc_close(context, ccache)
392 if context:
393 krb5_free_context(context)