public
Description: Python tools for Mac system administration
Homepage: http://pymacadmin.googlecode.com/
Clone URL: git://github.com/acdha/pymacadmin.git
pymacadmin / lib / PyMacAdmin / Security / __init__.py
100755 60 lines (55 sloc) 2.58 kb
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
# encoding: utf-8
import PyMacAdmin
import ctypes
import struct
import sys
 
# This is not particularly elegant but to avoid everything having to load the
# Security framework we use a single copy hanging of this module so everything
# else can simply use Security.lib.SecKeychainFoo(…)
lib = PyMacAdmin.load_carbon_framework('/System/Library/Frameworks/Security.framework/Versions/Current/Security')
 
CSSM_DB_RECORDTYPE_APP_DEFINED_START = 0x80000000
CSSM_DL_DB_RECORD_X509_CERTIFICATE = CSSM_DB_RECORDTYPE_APP_DEFINED_START + 0x1000
 
# This is somewhat gross: we define a bunch of module-level constants based on
# the SecKeychainItem.h defines (FourCharCodes) by passing them through
# struct.unpack and converting them to ctypes.c_long() since we'll never use
# them for non-native APIs
 
CARBON_DEFINES = {
    'kSecCreationDateItemAttr': 'cdat',
    'kSecModDateItemAttr': 'mdat',
    'kSecDescriptionItemAttr': 'desc',
    'kSecCommentItemAttr': 'icmt',
    'kSecCreatorItemAttr': 'crtr',
    'kSecTypeItemAttr': 'type',
    'kSecScriptCodeItemAttr': 'scrp',
    'kSecLabelItemAttr': 'labl',
    'kSecInvisibleItemAttr': 'invi',
    'kSecNegativeItemAttr': 'nega',
    'kSecCustomIconItemAttr': 'cusi',
    'kSecAccountItemAttr': 'acct',
    'kSecServiceItemAttr': 'svce',
    'kSecGenericItemAttr': 'gena',
    'kSecSecurityDomainItemAttr': 'sdmn',
    'kSecServerItemAttr': 'srvr',
    'kSecAuthenticationTypeItemAttr': 'atyp',
    'kSecPortItemAttr': 'port',
    'kSecPathItemAttr': 'path',
    'kSecVolumeItemAttr': 'vlme',
    'kSecAddressItemAttr': 'addr',
    'kSecSignatureItemAttr': 'ssig',
    'kSecProtocolItemAttr': 'ptcl',
    'kSecCertificateType': 'ctyp',
    'kSecCertificateEncoding': 'cenc',
    'kSecCrlType': 'crtp',
    'kSecCrlEncoding': 'crnc',
    'kSecAlias': 'alis',
    'kSecInternetPasswordItemClass': 'inet',
    'kSecGenericPasswordItemClass': 'genp',
    'kSecAppleSharePasswordItemClass': 'ashp',
    'kSecCertificateItemClass': CSSM_DL_DB_RECORD_X509_CERTIFICATE
}
 
for k in CARBON_DEFINES:
    v = CARBON_DEFINES[k]
    if isinstance(v, str):
        assert(len(v) == 4)
        v = ctypes.c_ulong(struct.unpack(">L", v)[0])
    setattr(sys.modules[__name__], k, v)