1 # Samba common functions
3 # Copyright (C) Matthieu Patou <mat@matws.net>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from samba import dsdb
22 from samba.ndr import ndr_pack
23 from samba.dcerpc import misc
26 from samba.compat import PY3
30 # cmp() exists only in Python 2
32 return (a > b) - (a < b)
35 def confirm(msg, forced=False, allow_all=False):
36 """confirm an action with the user
38 :param msg: A string to print to the user
39 :param forced: Are the answer forced
42 print("%s [YES]" % msg)
56 mapping['ALL'] = 'ALL'
57 mapping['NONE'] = 'NONE'
58 prompt = '[y/N/all/none]'
61 v = raw_input(msg + ' %s ' % prompt)
65 print("Unknown response '%s'" % v)
68 def normalise_int32(ivalue):
69 '''normalise a ldap integer to signed 32 bit'''
70 if int(ivalue) & 0x80000000 and int(ivalue) > 0:
71 return str(int(ivalue) - 0x100000000)
75 class dsdb_Dn(object):
76 '''a class for binary DN'''
78 def __init__(self, samdb, dnstring, syntax_oid=None):
79 '''create a dsdb_Dn'''
80 if syntax_oid is None:
81 # auto-detect based on string
82 if dnstring.startswith("B:"):
83 syntax_oid = dsdb.DSDB_SYNTAX_BINARY_DN
84 elif dnstring.startswith("S:"):
85 syntax_oid = dsdb.DSDB_SYNTAX_STRING_DN
87 syntax_oid = dsdb.DSDB_SYNTAX_OR_NAME
88 if syntax_oid in [dsdb.DSDB_SYNTAX_BINARY_DN, dsdb.DSDB_SYNTAX_STRING_DN]:
90 colons = dnstring.split(':')
92 raise RuntimeError("Invalid DN %s" % dnstring)
93 prefix_len = 4 + len(colons[1]) + int(colons[1])
94 self.prefix = dnstring[0:prefix_len]
95 self.binary = self.prefix[3+len(colons[1]):-1]
96 self.dnstring = dnstring[prefix_len:]
98 self.dnstring = dnstring
101 self.dn = ldb.Dn(samdb, self.dnstring)
104 return self.prefix + str(self.dn.extended_str(mode=1))
106 def __cmp__(self, other):
107 ''' compare dsdb_Dn values similar to parsed_dn_compare()'''
110 guid1 = dn1.dn.get_extended_component("GUID")
111 guid1b = ndr_pack(misc.GUID(guid1))
112 guid2 = dn2.dn.get_extended_component("GUID")
113 guid2b = ndr_pack(misc.GUID(guid2))
115 v = cmp(guid1, guid2)
118 v = cmp(dn1.binary, dn2.binary)
121 # In Python3, __cmp__ is replaced by these 6 methods
122 def __eq__(self, other):
123 return self.__cmp__(other) == 0
125 def __ne__(self, other):
126 return self.__cmp__(other) != 0
128 def __lt__(self, other):
129 return self.__cmp__(other) < 0
131 def __le__(self, other):
132 return self.__cmp__(other) <= 0
134 def __gt__(self, other):
135 return self.__cmp__(other) > 0
137 def __ge__(self, other):
138 return self.__cmp__(other) >= 0
140 def get_binary_integer(self):
141 '''return binary part of a dsdb_Dn as an integer, or None'''
142 if self.prefix == '':
144 return int(self.binary, 16)
147 '''return binary as a byte string'''
148 return binascii.unhexlify(self.binary)