Revert "pidl: Use non-existent function dissect_ndr_int64()"
[Samba.git] / python / samba / netcmd / gpcommon.py
blobb8ac09e79685b6e78898f519def2c860fe2029d1
1 # Samba common group policy functions
3 # Copyright Andrew Tridgell 2010
4 # Copyright Amitay Isaacs 2011-2012 <amitay@gmail.com>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import ldb
20 from samba.credentials import SMB_SIGNING_REQUIRED
21 from samba.samba3 import param as s3param
22 from samba.samba3 import libsmb_samba_internal as libsmb
23 from samba.netcmd import CommandError
25 def get_gpo_dn(samdb, gpo):
26 """Construct the DN for gpo"""
28 dn = samdb.get_default_basedn()
29 dn.add_child(ldb.Dn(samdb, "CN=Policies,CN=System"))
30 dn.add_child(ldb.Dn(samdb, "CN=%s" % gpo))
31 return dn
33 def create_directory_hier(conn, remotedir):
34 elems = remotedir.replace('/', '\\').split('\\')
35 path = ""
36 for e in elems:
37 path = path + '\\' + e
38 if not conn.chkpath(path):
39 conn.mkdir(path)
41 def smb_connection(dc_hostname, service, lp, creds):
42 # SMB connect to DC
43 # Force signing for the smb connection
44 saved_signing_state = creds.get_smb_signing()
45 creds.set_smb_signing(SMB_SIGNING_REQUIRED)
46 try:
47 # the SMB bindings rely on having a s3 loadparm
48 s3_lp = s3param.get_context()
49 s3_lp.load(lp.configfile)
50 conn = libsmb.Conn(dc_hostname, service, lp=s3_lp, creds=creds)
51 except Exception:
52 raise CommandError("Error connecting to '%s' using SMB" % dc_hostname)
53 # Reset signing state
54 creds.set_smb_signing(saved_signing_state)
55 return conn