-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenDirectory.py
106 lines (83 loc) · 2.67 KB
/
OpenDirectory.py
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
# -*- coding: utf-8 -*-
'''
Support for Directory Object Manipulation via the Open Directory Objective-C API
:maintainer: Mosen <mosen@github.com>
:maturity: new
:depends: objc
:platform: darwin
'''
import logging
from pprint import pprint
log = logging.getLogger(__name__)
has_imports = False
try:
import objc
from OpenDirectory import ODSession, ODQuery, ODNode, \
kODRecordTypeUsers, kODAttributeTypeRecordName, kODMatchContains, kODAttributeTypeStandardOnly
from Foundation import NSRunLoop, NSDefaultRunLoopMode, NSObject
has_imports = True
except ImportError:
pass
def __virtual__():
'''
Module only loads on Darwin
'''
if not has_imports:
return False, 'Open Directory only available on Darwin'
else:
return True
def _get_node(path):
'''
Get a reference to an ODNode instance given a path string eg. /LDAPv3/127.0.0.1
'''
session = ODSession.defaultSession()
node, err = ODNode.nodeWithSession_name_error_(session, path, None)
if err:
log.error(err)
return None
return node
def nodes():
'''
Get a list of registered nodes eg. /Local/Default
CLI Example::
salt '*' opendirectory.nodes
'''
session = ODSession.defaultSession()
names, err = session.nodeNamesAndReturnError_(None)
if err is not None:
log.error(err)
return None
# The method returns with a tuple so it is converted to a list here.
return list(names)
def search(path, searchValue):
'''
List records that match the given query.
'''
node = _get_node(path)
if not node:
log.error('Query not possible, cannot get reference to node at path: {}'.format(path))
return None
# @objc.callbackFor("CFOpenDirectory.ODQuerySetCallback")
# def query_callback(query, value, context, error, info):
# log.warning('got callback')
# pass
query, err = ODQuery.queryWithNode_forRecordTypes_attribute_matchType_queryValues_returnAttributes_maximumResults_error_(
node,
kODRecordTypeUsers,
kODAttributeTypeRecordName,
kODMatchContains,
searchValue,
kODAttributeTypeStandardOnly,
0,
None
)
if err:
log.error('Failed to construct query: {}'.format(err))
return None
ODQueryDelegate = objc.protocolNamed('ODQueryDelegate')
class QueryDelegate(NSObject, ODQueryDelegate):
def query_foundResults_error_(self, inQuery, inResults, inError):
log.error('FOUND RESULTS')
qd = QueryDelegate()
query.setDelegate_(qd)
query.scheduleInRunLoop_forMode_(NSRunLoop.currentRunLoop(), NSDefaultRunLoopMode)