/usr/lib/python3/dist-packages/keyring/backends/_OS_X_API.py is in python3-keyring 10.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | import contextlib
import ctypes
import struct
from ctypes import c_void_p, c_uint16, c_uint32, c_int32, c_char_p, POINTER
from keyring.py27compat import string_types, add_metaclass
sec_keychain_ref = sec_keychain_item_ref = c_void_p
OS_status = c_int32
class error:
item_not_found = -25300
fw = '/System/Library/Frameworks/{name}.framework/Versions/A/{name}'.format
_sec = ctypes.CDLL(fw(name='Security'))
_core = ctypes.CDLL(fw(name='CoreServices'))
SecKeychainOpen = _sec.SecKeychainOpen
SecKeychainOpen.argtypes = (
c_char_p,
POINTER(sec_keychain_ref),
)
SecKeychainOpen.restype = OS_status
SecKeychainCopyDefault = _sec.SecKeychainCopyDefault
SecKeychainCopyDefault.argtypes = POINTER(sec_keychain_ref),
SecKeychainCopyDefault.restype = OS_status
class Error(Exception):
@classmethod
def raise_for_status(cls, status, msg):
if status == 0:
return
raise cls(status, msg)
class NotFound(Error):
@classmethod
def raise_for_status(cls, status, msg):
if status == error.item_not_found:
raise cls(status, msg)
Error.raise_for_status(status, msg)
@contextlib.contextmanager
def open(name):
ref = sec_keychain_ref()
if name is None:
status = SecKeychainCopyDefault(ref)
msg = "Unable to open default keychain"
else:
status = SecKeychainOpen(name.encode('utf-8'), ref)
msg = "Unable to open keychain {name}".format(**locals())
Error.raise_for_status(status, msg)
try:
yield ref
finally:
_core.CFRelease(ref)
SecKeychainFindGenericPassword = _sec.SecKeychainFindGenericPassword
SecKeychainFindGenericPassword.argtypes = (
sec_keychain_ref,
c_uint32,
c_char_p,
c_uint32,
c_char_p,
POINTER(c_uint32), # passwordLength
POINTER(c_void_p), # passwordData
POINTER(sec_keychain_item_ref), # itemRef
)
SecKeychainFindGenericPassword.restype = OS_status
def find_generic_password(kc_name, service, username):
username = username.encode('utf-8')
service = service.encode('utf-8')
with open(kc_name) as keychain:
length = c_uint32()
data = c_void_p()
status = SecKeychainFindGenericPassword(
keychain,
len(service),
service,
len(username),
username,
length,
data,
None,
)
msg = "Can't fetch password from system"
NotFound.raise_for_status(status, msg)
password = ctypes.create_string_buffer(length.value)
ctypes.memmove(password, data.value, length.value)
SecKeychainItemFreeContent(None, data)
return password.raw.decode('utf-8')
SecKeychainFindInternetPassword = _sec.SecKeychainFindInternetPassword
SecKeychainFindInternetPassword.argtypes = (
sec_keychain_ref, # keychainOrArray
c_uint32, # serverNameLength
c_char_p, # serverName
c_uint32, # securityDomainLength
c_char_p, # securityDomain
c_uint32, # accountNameLength
c_char_p, # accountName
c_uint32, # pathLength
c_char_p, # path
c_uint16, # port
c_uint32, # SecProtocolType protocol,
c_uint32, # SecAuthenticationType authenticationType,
POINTER(c_uint32), # passwordLength
POINTER(c_void_p), # passwordData
POINTER(sec_keychain_item_ref), # itemRef
)
SecKeychainFindInternetPassword.restype = OS_status
class PackedAttributes(type):
"""
Take the attributes which use magic words
to represent enumerated constants and generate
the constants.
"""
def __new__(cls, name, bases, dict):
dict.update(
(key, cls.unpack(val))
for key, val in dict.items()
if not key.startswith('_')
)
return super(PackedAttributes, cls).__new__(cls, name, bases, dict)
@staticmethod
def unpack(word):
r"""
>>> PackedAttributes.unpack(0)
0
>>> PackedAttributes.unpack('\x00\x00\x00\x01')
1
>>> PackedAttributes.unpack('abcd')
1633837924
"""
if not isinstance(word, string_types):
return word
val, = struct.unpack('!I', word.encode('ascii'))
return val
@add_metaclass(PackedAttributes)
class SecProtocolType(object):
kSecProtocolTypeHTTP = 'http'
kSecProtocolTypeHTTPS = 'htps'
kSecProtocolTypeFTP = 'ftp '
@add_metaclass(PackedAttributes)
class SecAuthenticationType(object):
"""
>>> SecAuthenticationType.kSecAuthenticationTypeDefault
1684434036
"""
kSecAuthenticationTypeDefault = 'dflt'
kSecAuthenticationTypeAny = 0
def find_internet_password(kc_name, service, username):
username = username.encode('utf-8')
domain = None
service = service.encode('utf-8')
path = None
port = 0
with open(kc_name) as keychain:
length = c_uint32()
data = c_void_p()
status = SecKeychainFindInternetPassword(
keychain,
len(service), service,
0, domain,
len(username), username,
0, path,
port,
SecProtocolType.kSecProtocolTypeHTTPS,
SecAuthenticationType.kSecAuthenticationTypeAny,
length,
data,
None,
)
msg = "Can't fetch password from system"
NotFound.raise_for_status(status, msg)
password = ctypes.create_string_buffer(length.value)
ctypes.memmove(password, data.value, length.value)
SecKeychainItemFreeContent(None, data)
return password.raw.decode('utf-8')
SecKeychainAddGenericPassword = _sec.SecKeychainAddGenericPassword
SecKeychainAddGenericPassword.argtypes = (
sec_keychain_ref,
c_uint32,
c_char_p,
c_uint32,
c_char_p,
c_uint32,
c_char_p,
POINTER(sec_keychain_item_ref),
)
SecKeychainAddGenericPassword.restype = OS_status
def set_generic_password(name, service, username, password):
username = username.encode('utf-8')
service = service.encode('utf-8')
password = password.encode('utf-8')
with open(name) as keychain:
item = sec_keychain_item_ref()
status = SecKeychainFindGenericPassword(
keychain,
len(service), service,
len(username), username, None,
None, item)
if status:
if status == error.item_not_found:
status = SecKeychainAddGenericPassword(
keychain,
len(service), service,
len(username), username,
len(password), password, None)
else:
status = SecKeychainItemModifyAttributesAndData(
item, None, len(password), password)
_core.CFRelease(item)
NotFound.raise_for_status(status, "Unable to set password")
SecKeychainAddInternetPassword = _sec.SecKeychainAddInternetPassword
SecKeychainAddInternetPassword.argtypes = (
sec_keychain_ref, # keychainOrArray
c_uint32, # serverNameLength
c_char_p, # serverName
c_uint32, # securityDomainLength
c_char_p, # securityDomain
c_uint32, # accountNameLength
c_char_p, # accountName
c_uint32, # pathLength
c_char_p, # path
c_uint16, # port
c_uint32, # SecProtocolType protocol,
c_uint32, # SecAuthenticationType authenticationType,
c_uint32, # passwordLength
c_void_p, # passwordData
POINTER(sec_keychain_item_ref), # itemRef
)
SecKeychainAddInternetPassword.restype = OS_status
def set_internet_password(name, service, username, password):
username = username.encode('utf-8')
domain = None
service = service.encode('utf-8')
password = password.encode('utf-8')
path = None
port = 0
with open(name) as keychain:
# TODO: Use update or set technique as seen in set_generic_password
status = SecKeychainAddInternetPassword(
keychain,
len(service), service,
0, domain,
len(username), username,
0, path,
port,
SecProtocolType.kSecProtocolTypeHTTPS,
SecAuthenticationType.kSecAuthenticationTypeAny,
len(password), password,
None,
)
NotFound.raise_for_status(status, "Unable to set password")
SecKeychainItemModifyAttributesAndData = _sec.SecKeychainItemModifyAttributesAndData
SecKeychainItemModifyAttributesAndData.argtypes = (
sec_keychain_item_ref, c_void_p, c_uint32, c_void_p,
)
SecKeychainItemModifyAttributesAndData.restype = OS_status
SecKeychainItemFreeContent = _sec.SecKeychainItemFreeContent
SecKeychainItemFreeContent.argtypes = (
c_void_p, c_void_p,
)
SecKeychainItemFreeContent.restype = OS_status
SecKeychainItemDelete = _sec.SecKeychainItemDelete
SecKeychainItemDelete.argtypes = sec_keychain_item_ref,
SecKeychainItemDelete.restype = OS_status
def delete_generic_password(name, service, username):
username = username.encode('utf-8')
service = service.encode('utf-8')
with open(name) as keychain:
length = c_uint32()
data = c_void_p()
item = sec_keychain_item_ref()
status = SecKeychainFindGenericPassword(
keychain,
len(service),
service,
len(username),
username,
length,
data,
item,
)
Error.raise_for_status(status, "Unable to delete password")
SecKeychainItemDelete(item)
_core.CFRelease(item)
|