|
30 |
|
30 |
|
31 |
"""Univention IP-Phone Example UDM Client.""" |
31 |
"""Univention IP-Phone Example UDM Client.""" |
32 |
|
32 |
|
33 |
import sys, os |
33 |
import sys |
|
|
34 |
import os |
34 |
import univention.debug |
35 |
import univention.debug |
35 |
univention.debug.init('/var/log/univention/ip-phone-tool.log', 1, 0) |
36 |
univention.debug.init('/var/log/univention/ip-phone-tool.log', univention.debug.FLUSH, univention.debug.NO_FUNCTION) |
36 |
|
37 |
|
37 |
import univention.config_registry |
38 |
import univention.config_registry |
38 |
import univention.admin.uldap |
39 |
import univention.admin.uldap |
|
45 |
from optparse import OptionParser, OptionValueError |
46 |
from optparse import OptionParser, OptionValueError |
46 |
|
47 |
|
47 |
translation=univention.admin.localization.translation('univention.admin.handlers.test') ## missing in this example |
48 |
translation=univention.admin.localization.translation('univention.admin.handlers.test') ## missing in this example |
48 |
_=translation.translate |
49 |
_ = translation.translate |
49 |
|
50 |
|
50 |
class ipphonetool: |
51 |
class ipphonetool: |
|
|
52 |
"""Simple example demonstrating how to implement and how to use custom Univention Directory Manager modules. |
53 |
This is an example tool to manage IP phones. |
54 |
""" |
51 |
|
55 |
|
52 |
def __init__(self, options, ucr=None): |
56 |
def __init__(self, options, ucr=None): |
53 |
"""Initialize an authenticated LDAP connection |
57 |
"""Initialize an authenticated LDAP connection |
|
63 |
binddn = ','.join(('cn=admin', self.ldap_base)) |
67 |
binddn = ','.join(('cn=admin', self.ldap_base)) |
64 |
server_role = ucr.get('server/role', '') |
68 |
server_role = ucr.get('server/role', '') |
65 |
if server_role in ('domaincontroller_master', 'domaincontroller_backup'): |
69 |
if server_role in ('domaincontroller_master', 'domaincontroller_backup'): |
66 |
bindpw = open('/etc/ldap.secret','r').read().strip() |
70 |
try: |
|
|
71 |
bindpw = open('/etc/ldap.secret','r').read().strip() |
72 |
except IOError, e: |
73 |
print >>sys.stderr, "Could not read credentials." |
74 |
sys.exit(1) |
67 |
else: |
75 |
else: |
68 |
print >>sys.stderr, "No credentials available" |
76 |
print >>sys.stderr, "No credentials available" |
69 |
sys.exit(1) |
77 |
sys.exit(1) |
70 |
|
78 |
|
71 |
try: |
79 |
try: |
72 |
self.lo=univention.admin.uldap.access(host=ldap_master, base=self.ldap_base, binddn=binddn, bindpw=bindpw, start_tls=2) |
80 |
self.lo = univention.admin.uldap.access(host=ldap_master, base=self.ldap_base, binddn=binddn, bindpw=bindpw, start_tls=2) |
73 |
except Exception, e: |
81 |
except Exception, e: |
74 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: %s' % str(e)) |
82 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: %s' % str(e)) |
75 |
print 'authentication error: %s' % str(e) |
83 |
print 'authentication error: %s' % str(e) |
|
85 |
"""This uses the lookup function of the udm module, allowing filtering in terms of UDM properties |
93 |
"""This uses the lookup function of the udm module, allowing filtering in terms of UDM properties |
86 |
""" |
94 |
""" |
87 |
|
95 |
|
88 |
filter=univention.admin.filter.expression('name', name) |
96 |
filter = univention.admin.filter.expression('name', name) |
89 |
|
97 |
|
90 |
objs = self.module.lookup(self.co, self.lo, filter, scope='domain', base=self.position.getDomain(), unique=1) |
98 |
objs = self.module.lookup(self.co, self.lo, filter, scope='domain', base=self.position.getDomain(), unique=1) |
91 |
if objs: |
99 |
if objs: |
92 |
obj=objs[0] |
100 |
obj = objs[0] |
93 |
else: |
101 |
else: |
94 |
obj = self.module.object(self.co, self.lo, self.position) |
102 |
obj = self.module.object(self.co, self.lo, self.position) |
95 |
obj['name']=name |
103 |
obj['name'] = name |
96 |
|
104 |
|
97 |
if not ip == obj['ip']: |
105 |
if not ip == obj['ip']: |
98 |
obj['ip'] = ip |
106 |
obj['ip'] = ip |
|
100 |
obj['priuser'] = priuser |
108 |
obj['priuser'] = priuser |
101 |
|
109 |
|
102 |
if options.redirect: |
110 |
if options.redirect: |
103 |
obj.options.append('redirection') |
111 |
if 'redirection' not in obj.options: |
|
|
112 |
obj.options.append('redirection') |
104 |
obj['redirect_user'] = options.redirect |
113 |
obj['redirect_user'] = options.redirect |
105 |
else: ## if no redirection is given, this example removes the objectclass |
114 |
else: ## if no redirection is given, this example removes the objectclass |
106 |
if 'redirection' in obj.options: |
115 |
if 'redirection' in obj.options: |
|
108 |
obj['redirect_user'] = options.redirect |
117 |
obj['redirect_user'] = options.redirect |
109 |
|
118 |
|
110 |
if objs: |
119 |
if objs: |
111 |
obj.modify() |
120 |
try: |
|
|
121 |
obj.modify() |
122 |
except univention.admin.uexceptions.ldapError, e: |
123 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.ERROR, 'Could not modify entry: %s' % e) |
124 |
print >>sys.stderr, 'Could not modify entry: %s' % name |
125 |
sys.exit(1) |
112 |
else: |
126 |
else: |
113 |
obj.create() |
127 |
try: |
|
|
128 |
obj.create() |
129 |
except univention.admin.uexceptions.ldapError, e: |
130 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.ERROR, 'Could not create entry: %s' % e) |
131 |
print >>sys.stderr, 'Could not create entry: %s' % name |
132 |
sys.exit(1) |
114 |
|
133 |
|
115 |
def remove(self, name): |
134 |
def remove(self, name): |
116 |
"""remove the object, no safty belt in this example""" |
135 |
"""remove the object, no safty belt in this example""" |
117 |
|
136 |
|
118 |
filter=univention.admin.filter.expression('name', name) |
137 |
filter = univention.admin.filter.expression('name', name) |
119 |
|
138 |
|
120 |
objs = self.module.lookup(self.co, self.lo, filter, scope='domain', base=self.position.getDomain(), unique=1) |
139 |
objs = self.module.lookup(self.co, self.lo, filter, scope='domain', base=self.position.getDomain(), unique=1) |
121 |
if objs: |
140 |
if objs: |
122 |
obj=objs[0] |
141 |
obj = objs[0] |
123 |
obj.remove() |
142 |
try: |
|
|
143 |
obj.remove() |
144 |
except univention.admin.uexceptions.ldapError, e: |
145 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.ERROR, 'Could not remove entry: %s' % e) |
146 |
print >>sys.stderr, 'Could not remove entry: %s' % name |
147 |
sys.exit(1) |
148 |
else: |
149 |
print >>sys.stderr, 'Entry not found: %s' % name |
150 |
sys.exit(1) |
124 |
|
151 |
|
125 |
def clear_redirect(self, name): |
152 |
def clear_redirect(self, name): |
126 |
"""This example uses a raw LDAP search instead of performing a lookup to determine the dn |
153 |
"""This example uses a raw LDAP search instead of performing a lookup to determine the dn |
127 |
""" |
154 |
""" |
128 |
try: |
155 |
try: |
129 |
filter = unicode('(&(cn=%s)(objectClass=testPhoneCallRedirect))' % name, 'utf8') |
156 |
filter = unicode('(&(cn=%s)(objectClass=testPhoneCallRedirect))' % name, 'utf8') |
130 |
dn=self.lo.searchDn(filter=filter, base=self.ldap_base, unique=1) |
157 |
dn = self.lo.searchDn(filter=filter, base=self.ldap_base, unique=1) |
131 |
if not dn: |
158 |
if not dn: |
132 |
print "No object found matching filter %s" % filter |
159 |
print "No object found matching filter %s" % filter |
133 |
sys.exit(1) |
160 |
sys.exit(1) |
134 |
|
161 |
|
135 |
object=univention.admin.objects.get(self.module, self.co, self.lo, position=self.position, dn=dn[0]) |
162 |
object = univention.admin.objects.get(self.module, self.co, self.lo, position=self.position, dn=dn[0]) |
136 |
object.open() ## open the object |
163 |
object.open() ## open the object |
137 |
|
164 |
|
138 |
if 'redirection' in object.options: |
165 |
if 'redirection' in object.options: |
139 |
object.options.remove('redirection') |
166 |
object.options.remove('redirection') |
140 |
|
167 |
|
141 |
object['redirect_user']='' |
168 |
object['redirect_user'] = '' |
142 |
|
169 |
|
143 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.INFO, 'ip-phone-tool: redirect_user cleared, modify object') |
170 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.INFO, 'ip-phone-tool: redirect_user cleared, modify object') |
144 |
dn=object.modify() |
171 |
dn = object.modify() |
145 |
|
172 |
|
146 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.INFO, 'ip-phone-tool: Redirection deactivated') |
173 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.INFO, 'ip-phone-tool: Redirection deactivated') |
147 |
|
174 |
|
148 |
except univention.admin.uexceptions.valueError, e: |
175 |
except univention.admin.uexceptions.valueError, e: |
149 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.ERROR, 'error: invalid syntax (%s)' % e) |
176 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.ERROR, 'error: invalid syntax (%s)' % e) |
|
|
177 |
print >>sys.stderr, 'Could not modify entry: %s' % name |
178 |
sys.exit(1) |
179 |
except univention.admin.uexceptions.ldapError, e: |
180 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.ERROR, 'Could not modify entry: %s' % e) |
181 |
print >>sys.stderr, 'Could not modify entry: %s' % name |
182 |
sys.exit(1) |
150 |
|
183 |
|
151 |
|
184 |
|
152 |
if __name__ == '__main__': |
185 |
if __name__ == '__main__': |
|
185 |
action = 'store', dest = 'username', |
218 |
action = 'store', dest = 'username', |
186 |
help = _('Username') ) |
219 |
help = _('Username') ) |
187 |
|
220 |
|
188 |
parser.add_option( '', '--redirect', |
221 |
parser.add_option( '--redirect', |
189 |
action = 'store', dest = 'redirect', |
222 |
action = 'store', dest = 'redirect', |
190 |
help = _('Redirect address') ) |
223 |
help = _('Redirect address') ) |
191 |
(options, arguments) = parser.parse_args() |
224 |
(options, arguments) = parser.parse_args() |
|
198 |
ucr.load() |
231 |
ucr.load() |
199 |
|
232 |
|
200 |
if len(arguments) < 1: |
233 |
if len(arguments) < 1: |
201 |
parser.print_help() |
234 |
parser.print_help(sys.stderr) |
202 |
sys.exit(1) |
235 |
sys.exit(2) |
203 |
|
236 |
|
204 |
udm_ipphone = ipphonetool(options, ucr) |
237 |
udm_ipphone = ipphonetool(options, ucr) |
205 |
if arguments[0] == 'set': |
238 |
if arguments[0] == 'set': |
206 |
if len(arguments) < 4: |
239 |
if len(arguments) < 4: |
207 |
parser.print_usage() |
240 |
parser.print_usage(sys.stderr) |
208 |
sys.exit(1) |
241 |
sys.exit(2) |
209 |
udm_ipphone.set( options, arguments[1], arguments[2], arguments[3] ) |
242 |
udm_ipphone.set( options, arguments[1], arguments[2], arguments[3] ) |
210 |
elif arguments[0] == 'remove': |
243 |
elif arguments[0] == 'remove': |
211 |
if len(arguments) < 2: |
244 |
if len(arguments) < 2: |
212 |
parser.print_usage() |
245 |
parser.print_usage(sys.stderr) |
213 |
sys.exit(1) |
246 |
sys.exit(2) |
214 |
udm_ipphone.remove( arguments[1] ) |
247 |
udm_ipphone.remove( arguments[1] ) |
215 |
elif arguments[0] == 'clear_redirect': |
248 |
elif arguments[0] == 'clear_redirect': |
216 |
if len(arguments) < 2: |
249 |
if len(arguments) < 2: |
217 |
parser.print_usage() |
250 |
parser.print_usage(sys.stderr) |
218 |
sys.exit(1) |
251 |
sys.exit(2) |
219 |
udm_ipphone.clear_redirect( arguments[1] ) |
252 |
udm_ipphone.clear_redirect( arguments[1] ) |
220 |
else: |
253 |
else: |
221 |
parser.print_usage() |
254 |
parser.print_usage(sys.stderr) |
222 |
sys.exit(0) |
255 |
sys.exit(2) |