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