-
Notifications
You must be signed in to change notification settings - Fork 27
/
dsadmin_utils.py
126 lines (102 loc) · 2.95 KB
/
dsadmin_utils.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Utilities for DSAdmin.
TODO put them in a module!
"""
try:
from subprocess import Popen, PIPE
HASPOPEN = True
except ImportError:
import popen2
HASPOPEN = False
import socket
import ldap
import re
#
# Decorator
#
def static_var(varname, value):
def decorate(func):
setattr(func, varname, value)
return func
return decorate
#
# Utilities
#
def normalizeDN(dn, usespace=False):
# not great, but will do until we use a newer version of python-ldap
# that has DN utilities
ary = ldap.explode_dn(dn.lower())
joinstr = ","
if usespace:
joinstr = ", "
return joinstr.join(ary)
def escapeDNValue(dn):
'''convert special characters in a DN into LDAPv3 escapes e.g.
"dc=example,dc=com" -> \"dc\=example\,\ dc\=com\"'''
for cc in (' ', '"', '+', ',', ';', '<', '>', '='):
dn = dn.replace(cc, '\\' + cc)
return dn
def escapeDNFiltValue(dn):
'''convert special characters in a DN into LDAPv3 escapes
for use in search filters'''
for cc in (' ', '"', '+', ',', ';', '<', '>', '='):
dn = dn.replace(cc, '\\%x' % ord(cc))
return dn
def suffixfilt(suffix):
nsuffix = normalizeDN(suffix)
escapesuffix = escapeDNFiltValue(nsuffix)
spacesuffix = normalizeDN(nsuffix, True)
filt = '(|(cn=%s)(cn=%s)(cn=%s)(cn="%s")(cn="%s")(cn=%s)(cn="%s"))' % (escapesuffix, nsuffix, spacesuffix, nsuffix, spacesuffix, suffix, suffix)
return filt
def isLocalHost(hname):
# first see if this is a "well known" local hostname
if hname == 'localhost' or hname == 'localhost.localdomain':
return True
# first lookup ip addr
ipadr = None
try:
ipadr = socket.gethostbyname(hname)
except Exception, e:
pass
if not ipadr:
print "Error: no IP Address for", hname
return False
# next, see if this IP addr is one of our
# local addresses
thematch = re.compile('inet addr:' + ipadr)
found = False
if HASPOPEN:
p = Popen(['/sbin/ifconfig', '-a'], stdout=PIPE)
child_stdout = p.stdout
else:
child_stdout, child_stdin = popen2.popen2(['/sbin/ifconfig', '-a'])
for line in child_stdout:
if re.search(thematch, line):
found = True
break
if HASPOPEN:
p.wait()
return found
def getfqdn(name=''):
return socket.getfqdn(name)
def getdomainname(name=''):
fqdn = getfqdn(name)
index = fqdn.find('.')
if index >= 0:
return fqdn[index + 1:]
else:
return fqdn
def getdefaultsuffix(name=''):
dm = getdomainname(name)
if dm:
return "dc=" + dm.replace('.', ',dc=')
else:
return 'dc=localdomain'
def is_a_dn(dn):
"""Returns True if the given string is a DN, False otherwise."""
return (dn.find("=") > 0)
def get_sbin_dir(sroot, prefix):
if sroot:
return "%s/bin/slapd/admin/bin" % sroot
elif prefix:
return "%s/sbin" % prefix
return "/usr/sbin"