domain.py: Add a base dir option for schema upgrades
[vlendec/samba-autobuild/.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 import dsdb
22 import binascii
23
24
25 def confirm(msg, forced=False, allow_all=False):
26     """confirm an action with the user
27
28     :param msg: A string to print to the user
29     :param forced: Are the answer forced
30     """
31     if forced:
32         print("%s [YES]" % msg)
33         return True
34
35     mapping = {
36         'Y': True,
37         'YES': True,
38         '': False,
39         'N': False,
40         'NO': False,
41         }
42
43     prompt = '[y/N]'
44
45     if allow_all:
46         mapping['ALL'] = 'ALL'
47         mapping['NONE'] = 'NONE'
48         prompt = '[y/N/all/none]'
49
50     while True:
51         v = raw_input(msg + ' %s ' % prompt)
52         v = v.upper()
53         if v in mapping:
54             return mapping[v]
55         print("Unknown response '%s'" % v)
56
57
58 def normalise_int32(ivalue):
59     '''normalise a ldap integer to signed 32 bit'''
60     if int(ivalue) & 0x80000000 and int(ivalue) > 0:
61         return str(int(ivalue) - 0x100000000)
62     return str(ivalue)
63
64
65 class dsdb_Dn(object):
66     '''a class for binary DN'''
67
68     def __init__(self, samdb, dnstring, syntax_oid=None):
69         '''create a dsdb_Dn'''
70         if syntax_oid is None:
71             # auto-detect based on string
72             if dnstring.startswith("B:"):
73                 syntax_oid = dsdb.DSDB_SYNTAX_BINARY_DN
74             elif dnstring.startswith("S:"):
75                 syntax_oid = dsdb.DSDB_SYNTAX_STRING_DN
76             else:
77                 syntax_oid = dsdb.DSDB_SYNTAX_OR_NAME
78         if syntax_oid in [dsdb.DSDB_SYNTAX_BINARY_DN, dsdb.DSDB_SYNTAX_STRING_DN]:
79             # it is a binary DN
80             colons = dnstring.split(':')
81             if len(colons) < 4:
82                 raise RuntimeError("Invalid DN %s" % dnstring)
83             prefix_len = 4 + len(colons[1]) + int(colons[1])
84             self.prefix = dnstring[0:prefix_len]
85             self.binary = self.prefix[3+len(colons[1]):-1]
86             self.dnstring = dnstring[prefix_len:]
87         else:
88             self.dnstring = dnstring
89             self.prefix = ''
90             self.binary = ''
91         self.dn = ldb.Dn(samdb, self.dnstring)
92
93     def __str__(self):
94         return self.prefix + str(self.dn.extended_str(mode=1))
95
96     def get_binary_integer(self):
97         '''return binary part of a dsdb_Dn as an integer, or None'''
98         if self.prefix == '':
99             return None
100         return int(self.binary, 16)
101
102     def get_bytes(self):
103         '''return binary as a byte string'''
104         return binascii.unhexlify(self.binary)