1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
3 # Copyright 2012 OpenStack LLC
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
24 class _voms(ctypes.Structure):
26 ("siglen", ctypes.c_int32),
27 ("signature", ctypes.c_char_p),
28 ("user", ctypes.c_char_p),
29 ("userca", ctypes.c_char_p),
30 ("server", ctypes.c_char_p),
31 ("serverca", ctypes.c_char_p),
32 ("voname", ctypes.c_char_p),
33 ("uri", ctypes.c_char_p),
34 ("date1", ctypes.c_char_p),
35 ("date2", ctypes.c_char_p),
36 ("type", ctypes.c_int32),
37 ("std", ctypes.c_void_p),
38 ("custom", ctypes.c_char_p),
39 ("datalen", ctypes.c_int32),
40 ("version", ctypes.c_int32),
41 ("fqan", ctypes.POINTER(ctypes.c_char_p)),
42 ("serial", ctypes.c_char_p),
43 ("ac", ctypes.c_void_p),
44 ("holder", ctypes.c_void_p),
48 class _vomsdata(ctypes.Structure):
50 ("cdir", ctypes.c_char_p),
51 ("vdir", ctypes.c_char_p),
52 ("data", ctypes.POINTER(ctypes.POINTER(_voms))),
53 ("workvo", ctypes.c_char_p),
54 ("extra_data", ctypes.c_char_p),
55 ("volen", ctypes.c_int32),
56 ("extralen", ctypes.c_int32),
57 ("real", ctypes.c_void_p),
62 """Context Manager for VOMS handling"""
64 def __init__(self, vomsdir_path, ca_path, vomsapi_lib):
65 self.VOMSApi = ctypes.CDLL(vomsapi_lib)
66 self.VOMSApi.VOMS_Init.restype = ctypes.POINTER(_vomsdata)
68 self.VOMSDIR = vomsdir_path
74 self.vd = self.VOMSApi.VOMS_Init(self.VOMSDIR, self.CADIR).contents
77 def set_no_verify(self):
78 """Skip verification of AC.
80 This method skips the AC signature verification, this it should
81 only be used for debugging and tests.
84 error = ctypes.c_int32(0)
85 self.VOMSApi.VOMS_SetVerificationType(0x040,
86 ctypes.byref(self.vd),
89 def retrieve(self, cert, chain):
90 """Retrieve VOMS credentials from a certificate and chain."""
92 self.error = ctypes.c_int32(0)
94 cert_ptr = ctypes.cast(long(cert._ptr()), ctypes.c_void_p)
95 chain_ptr = ctypes.cast(long(chain._ptr()), ctypes.c_void_p)
97 res = self.VOMSApi.VOMS_Retrieve(cert_ptr,
100 ctypes.byref(self.vd),
101 ctypes.byref(self.error))
105 return self.vd.data.contents.contents
107 def __exit__(self, type, value, tb):
108 self.VOMSApi.VOMS_Destroy(ctypes.byref(self.vd))