python selftest: enabled samba.tests.s3registry to run with py3
[samba.git] / python / samba / common.py
1 # Samba common functions
2 #
3 # Copyright (C) Matthieu Patou <mat@matws.net>
4 #
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.
9 #
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.
14 #
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/>.
17 #
18
19
20 import ldb
21 from samba import dsdb
22 from samba.ndr import ndr_pack
23 from samba.dcerpc import misc
24 import binascii
25
26
27 def confirm(msg, forced=False, allow_all=False):
28     """confirm an action with the user
29
30     :param msg: A string to print to the user
31     :param forced: Are the answer forced
32     """
33     if forced:
34         print("%s [YES]" % msg)
35         return True
36
37     mapping = {
38         'Y': True,
39         'YES': True,
40         '': False,
41         'N': False,
42         'NO': False,
43         }
44
45     prompt = '[y/N]'
46
47     if allow_all:
48         mapping['ALL'] = 'ALL'
49         mapping['NONE'] = 'NONE'
50         prompt = '[y/N/all/none]'
51
52     while True:
53         v = raw_input(msg + ' %s ' % prompt)
54         v = v.upper()
55         if v in mapping:
56             return mapping[v]
57         print("Unknown response '%s'" % v)
58
59
60 def normalise_int32(ivalue):
61     '''normalise a ldap integer to signed 32 bit'''
62     if int(ivalue) & 0x80000000 and int(ivalue) > 0:
63         return str(int(ivalue) - 0x100000000)
64     return str(ivalue)
65
66
67 class dsdb_Dn(object):
68     '''a class for binary DN'''
69
70     def __init__(self, samdb, dnstring, syntax_oid=None):
71         '''create a dsdb_Dn'''
72         if syntax_oid is None:
73             # auto-detect based on string
74             if dnstring.startswith("B:"):
75                 syntax_oid = dsdb.DSDB_SYNTAX_BINARY_DN
76             elif dnstring.startswith("S:"):
77                 syntax_oid = dsdb.DSDB_SYNTAX_STRING_DN
78             else:
79                 syntax_oid = dsdb.DSDB_SYNTAX_OR_NAME
80         if syntax_oid in [dsdb.DSDB_SYNTAX_BINARY_DN, dsdb.DSDB_SYNTAX_STRING_DN]:
81             # it is a binary DN
82             colons = dnstring.split(':')
83             if len(colons) < 4:
84                 raise RuntimeError("Invalid DN %s" % dnstring)
85             prefix_len = 4 + len(colons[1]) + int(colons[1])
86             self.prefix = dnstring[0:prefix_len]
87             self.binary = self.prefix[3+len(colons[1]):-1]
88             self.dnstring = dnstring[prefix_len:]
89         else:
90             self.dnstring = dnstring
91             self.prefix = ''
92             self.binary = ''
93         self.dn = ldb.Dn(samdb, self.dnstring)
94
95     def __str__(self):
96         return self.prefix + str(self.dn.extended_str(mode=1))
97
98     def __cmp__(self, other):
99         ''' compare dsdb_Dn values similar to parsed_dn_compare()'''
100         dn1 = self
101         dn2 = other
102         guid1 = dn1.dn.get_extended_component("GUID")
103         guid1b = ndr_pack(misc.GUID(guid1))
104         guid2 = dn2.dn.get_extended_component("GUID")
105         guid2b = ndr_pack(misc.GUID(guid2))
106
107         v = cmp(guid1, guid2)
108         if v != 0:
109             return v
110         v = cmp(dn1.binary, dn2.binary)
111         return v
112
113     def get_binary_integer(self):
114         '''return binary part of a dsdb_Dn as an integer, or None'''
115         if self.prefix == '':
116             return None
117         return int(self.binary, 16)
118
119     def get_bytes(self):
120         '''return binary as a byte string'''
121         return binascii.unhexlify(self.binary)