From 87afc3aee1ea593069322a49355dd8780d99e123 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Fri, 28 Dec 2012 15:37:14 +0100 Subject: Move python modules from source4/scripting/python/ to python/. Reviewed-by: Andrew Bartlett Autobuild-User(master): Andrew Bartlett Autobuild-Date(master): Sat Mar 2 03:57:34 CET 2013 on sn-devel-104 --- python/examples/dnsserver.py | 89 ++++++++++++++++++++++++++++++++ python/examples/netbios.py | 28 +++++++++++ python/examples/samr.py | 117 +++++++++++++++++++++++++++++++++++++++++++ python/examples/winreg.py | 87 ++++++++++++++++++++++++++++++++ 4 files changed, 321 insertions(+) create mode 100755 python/examples/dnsserver.py create mode 100644 python/examples/netbios.py create mode 100755 python/examples/samr.py create mode 100755 python/examples/winreg.py (limited to 'python/examples') diff --git a/python/examples/dnsserver.py b/python/examples/dnsserver.py new file mode 100755 index 0000000000..c65b1c4099 --- /dev/null +++ b/python/examples/dnsserver.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python + +# script to test the dnsserver RPC protocol + +import sys +from optparse import OptionParser + +sys.path.insert(0, "bin/python") + +import samba +import samba.getopt as options +from samba.dcerpc import dnsserver, security, dnsp + + +########### main code ########### +if __name__ == "__main__": + parser = OptionParser("dnsserver [options] server") + sambaopts = options.SambaOptions(parser) + credopts = options.CredentialsOptionsDouble(parser) + parser.add_option_group(credopts) + + (opts, args) = parser.parse_args() + + if len(args) < 3: + print("Usage: dnsserver.py [options] DNSSERVER DNSZONE NEWNAME") + sys.exit(1) + + server = args[0] + dnszone = args[1] + newname = args[2] + + lp = sambaopts.get_loadparm() + creds = credopts.get_credentials(lp) + + if not creds.authentication_requested(): + parser.error("You must supply credentials") + + binding_str = "ncacn_ip_tcp:%s[print,sign]" % server + + dns_conn = dnsserver.dnsserver(binding_str, lp, creds) + + print("querying a NS record") + res = dns_conn.DnssrvEnumRecords2(0x00070000, + 0, + server, + dnszone, + newname, + None, + dnsp.DNS_TYPE_NS, + 0x0f, + None, + None) + + print("adding a NS glue record") + name = dnsserver.DNS_RPC_NAME() + name.str = newname + + addrec = dnsserver.DNS_RPC_RECORD() + addrec.wType = dnsp.DNS_TYPE_NS + addrec.dwFlags = 0 + addrec.dwSerial = 0 + addrec.dwTtlSeconds = 3600 + addrec.dwTimeStamp = 0 + addrec.dwReserved = 0 + addrec.data = name + + addrecbuf = dnsserver.DNS_RPC_RECORD_BUF() + addrecbuf.rec = addrec + + res = dns_conn.DnssrvUpdateRecord2(0x00070000, + 0, + server, + dnszone, + newname, + addrecbuf, + None) + + + print("querying the NS record") + res = dns_conn.DnssrvEnumRecords2(0x00070000, + 0, + server, + dnszone, + newname, + None, + dnsp.DNS_TYPE_NS, + 0x0f, + None, + None) diff --git a/python/examples/netbios.py b/python/examples/netbios.py new file mode 100644 index 0000000000..a29b09f051 --- /dev/null +++ b/python/examples/netbios.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +# Unix SMB/CIFS implementation. +# Copyright (C) Jelmer Vernooij 2008 +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +from samba.netbios import Node + +n = Node() +(reply_from, names, addresses) = n.query_name("GANIEDA", "192.168.4.0", + timeout=4) + +print "Received reply from %s:" % (reply_from, ) +print "Names: %r" % (names, ) +print "Addresses: %r" % (addresses, ) diff --git a/python/examples/samr.py b/python/examples/samr.py new file mode 100755 index 0000000000..9b8e31e434 --- /dev/null +++ b/python/examples/samr.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Unix SMB/CIFS implementation. +# Copyright © Jelmer Vernooij 2008 +# +# Based on samr.js © Andrew Tridgell +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import sys + +sys.path.insert(0, "bin/python") + +from samba.dcerpc import samr, security + +def display_lsa_string(str): + return str.string + +def FillUserInfo(samr, dom_handle, users, level): + """fill a user array with user information from samrQueryUserInfo""" + for i in range(len(users)): + user_handle = samr.OpenUser(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, users[i].idx) + info = samr.QueryUserInfo(user_handle, level) + info.name = users[i].name + info.idx = users[i].idx + users[i] = info + samr.Close(user_handle) + +def toArray((handle, array, num_entries)): + ret = [] + for x in range(num_entries): + ret.append((array.entries[x].idx, array.entries[x].name)) + return ret + + +def test_Connect(samr): + """test the samr_Connect interface""" + print "Testing samr_Connect" + return samr.Connect2(None, security.SEC_FLAG_MAXIMUM_ALLOWED) + +def test_LookupDomain(samr, handle, domain): + """test the samr_LookupDomain interface""" + print "Testing samr_LookupDomain" + return samr.LookupDomain(handle, domain) + +def test_OpenDomain(samr, handle, sid): + """test the samr_OpenDomain interface""" + print "Testing samr_OpenDomain" + return samr.OpenDomain(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, sid) + +def test_EnumDomainUsers(samr, dom_handle): + """test the samr_EnumDomainUsers interface""" + print "Testing samr_EnumDomainUsers" + users = toArray(samr.EnumDomainUsers(dom_handle, 0, 0, -1)) + print "Found %d users" % len(users) + for idx, user in users: + print "\t%s\t(%d)" % (user.string, idx) + +def test_EnumDomainGroups(samr, dom_handle): + """test the samr_EnumDomainGroups interface""" + print "Testing samr_EnumDomainGroups" + groups = toArray(samr.EnumDomainGroups(dom_handle, 0, 0)) + print "Found %d groups" % len(groups) + for idx, group in groups: + print "\t%s\t(%d)" % (group.string, idx) + +def test_domain_ops(samr, dom_handle): + """test domain specific ops""" + test_EnumDomainUsers(samr, dom_handle) + test_EnumDomainGroups(samr, dom_handle) + +def test_EnumDomains(samr, handle): + """test the samr_EnumDomains interface""" + print "Testing samr_EnumDomains" + + domains = toArray(samr.EnumDomains(handle, 0, -1)) + print "Found %d domains" % len(domains) + for idx, domain in domains: + print "\t%s (%d)" % (display_lsa_string(domain), idx) + for idx, domain in domains: + print "Testing domain %s" % display_lsa_string(domain) + sid = samr.LookupDomain(handle, domain) + dom_handle = test_OpenDomain(samr, handle, sid) + test_domain_ops(samr, dom_handle) + samr.Close(dom_handle) + +if len(sys.argv) != 2: + print "Usage: samr.js " + sys.exit(1) + +binding = sys.argv[1] + +print "Connecting to %s" % binding +try: + samr = samr.samr(binding) +except Exception, e: + print "Failed to connect to %s: %s" % (binding, e.message) + sys.exit(1) + +handle = test_Connect(samr) +test_EnumDomains(samr, handle) +samr.Close(handle) + +print "All OK" diff --git a/python/examples/winreg.py b/python/examples/winreg.py new file mode 100755 index 0000000000..484a69b842 --- /dev/null +++ b/python/examples/winreg.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python +# +# tool to manipulate a remote registry +# Copyright Andrew Tridgell 2005 +# Copyright Jelmer Vernooij 2007 +# Released under the GNU GPL v3 or later +# + +import sys + +# Find right directory when running from source tree +sys.path.insert(0, "bin/python") + +from samba.dcerpc import winreg +import optparse +import samba.getopt as options + +parser = optparse.OptionParser("%s [path]" % sys.argv[0]) +sambaopts = options.SambaOptions(parser) +parser.add_option_group(sambaopts) +parser.add_option("--createkey", type="string", metavar="KEYNAME", + help="create a key") + +opts, args = parser.parse_args() + +if len(args) < 1: + parser.print_usage() + sys.exit(-1) + +binding = args[0] + +print "Connecting to " + binding +conn = winreg.winreg(binding, sambaopts.get_loadparm()) + +def list_values(key): + (num_values, max_valnamelen, max_valbufsize) = conn.QueryInfoKey(key, winreg.String())[4:8] + for i in range(num_values): + name = winreg.StringBuf() + name.size = max_valnamelen + (name, type, data, _, data_len) = conn.EnumValue(key, i, name, 0, "", max_valbufsize, 0) + print "\ttype=%-30s size=%4d '%s'" % type, len, name + if type in (winreg.REG_SZ, winreg.REG_EXPAND_SZ): + print "\t\t'%s'" % data +# if (v.type == reg.REG_MULTI_SZ) { +# for (j in v.value) { +# printf("\t\t'%s'\n", v.value[j]) +# } +# } +# if (v.type == reg.REG_DWORD || v.type == reg.REG_DWORD_BIG_ENDIAN) { +# printf("\t\t0x%08x (%d)\n", v.value, v.value) +# } +# if (v.type == reg.REG_QWORD) { +# printf("\t\t0x%llx (%lld)\n", v.value, v.value) +# } + +def list_path(key, path): + count = 0 + (num_subkeys, max_subkeylen, max_subkeysize) = conn.QueryInfoKey(key, winreg.String())[1:4] + for i in range(num_subkeys): + name = winreg.StringBuf() + name.size = max_subkeysize + keyclass = winreg.StringBuf() + keyclass.size = max_subkeysize + (name, _, _) = conn.EnumKey(key, i, name, keyclass=keyclass, last_changed_time=None)[0] + subkey = conn.OpenKey(key, name, 0, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS) + count += list_path(subkey, "%s\\%s" % (path, name)) + list_values(subkey) + return count + +if len(args) > 1: + root = args[1] +else: + root = "HKLM" + +if opts.createkey: + reg.create_key("HKLM\\SOFTWARE", opt.createkey) +else: + print "Listing registry tree '%s'" % root + try: + root_key = getattr(conn, "Open%s" % root)(None, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS) + except AttributeError: + print "Unknown root key name %s" % root + sys.exit(1) + count = list_path(root_key, root) + if count == 0: + print "No entries found" + sys.exit(1) -- cgit