summaryrefslogtreecommitdiff
path: root/source3/python/examples
diff options
context:
space:
mode:
Diffstat (limited to 'source3/python/examples')
-rwxr-xr-xsource3/python/examples/spoolss/changeid.py34
-rwxr-xr-xsource3/python/examples/spoolss/enumprinters.py36
-rwxr-xr-xsource3/python/examples/spoolss/psec.py88
-rwxr-xr-xsource3/python/examples/tdbpack/tdbtimetrial.py12
-rwxr-xr-xsource3/python/examples/tdbpack/test_tdbpack.py195
5 files changed, 365 insertions, 0 deletions
diff --git a/source3/python/examples/spoolss/changeid.py b/source3/python/examples/spoolss/changeid.py
new file mode 100755
index 0000000000..85fe0efe8a
--- /dev/null
+++ b/source3/python/examples/spoolss/changeid.py
@@ -0,0 +1,34 @@
+#!/usr/bin/python
+#
+# Display the changeid for a list of printers given on the command line
+#
+# Sample usage:
+#
+# changeid.py '\\win2kdc1\magpie'
+#
+
+import sys
+from samba import spoolss
+
+if len(sys.argv) == 1:
+ print "Usage: changeid.py <printername>"
+ sys.exit(1)
+
+for printer in sys.argv[1:]:
+
+ # Open printer handle
+
+ try:
+ hnd = spoolss.openprinter(printer)
+ except:
+ print "error opening printer %s" % printer
+ sys.exit(1)
+
+ # Fetch and display changeid
+
+ info = hnd.getprinter(level = 0)
+ print info["change_id"]
+
+ # Clean up
+
+ spoolss.closeprinter(hnd)
diff --git a/source3/python/examples/spoolss/enumprinters.py b/source3/python/examples/spoolss/enumprinters.py
new file mode 100755
index 0000000000..478c46bc24
--- /dev/null
+++ b/source3/python/examples/spoolss/enumprinters.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+#
+# Display information on all printers on a print server. Defaults to
+# printer info level 1.
+#
+# Example: enumprinters.py win2kdc1
+#
+
+import sys
+from samba import spoolss
+
+if len(sys.argv) < 2 or len(sys.argv) > 3:
+ print "Usage: enumprinters.py <servername> [infolevel]"
+ sys.exit(1)
+
+printserver = sys.argv[1]
+
+level = 1
+if len(sys.argv) == 3:
+ level = int(sys.argv[2])
+
+# Get list of printers
+
+try:
+ printer_list = spoolss.enumprinters("\\\\%s" % printserver)
+except:
+ print "error enumerating printers on %s" % printserver
+ sys.exit(1)
+
+# Display basic info
+
+for printer in printer_list:
+ h = spoolss.openprinter("\\\\%s\\%s" % (printserver, printer))
+ info = h.getprinter(level = level)
+ print "Printer info %d for %s: %s" % (level, printer, info)
+ print
diff --git a/source3/python/examples/spoolss/psec.py b/source3/python/examples/spoolss/psec.py
new file mode 100755
index 0000000000..498a0ef174
--- /dev/null
+++ b/source3/python/examples/spoolss/psec.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python
+#
+# Get or set the security descriptor on a printer
+#
+
+import sys, re, string
+from samba import spoolss
+
+if len(sys.argv) != 3:
+ print "Usage: psec.py getsec|setsec printername"
+ sys.exit(1)
+
+op = sys.argv[1]
+printername = sys.argv[2]
+
+# Display security descriptor
+
+if op == "getsec":
+
+ try:
+ hnd = spoolss.openprinter(printername)
+ except:
+ print "error opening printer %s" % printername
+ sys.exit(1)
+
+ secdesc = hnd.getprinter(level = 3)["security_descriptor"]
+
+ print secdesc["owner_sid"]
+ print secdesc["group_sid"]
+
+ for acl in secdesc["dacl"]["ace_list"]:
+ print "%d %d 0x%08x %s" % (acl["type"], acl["flags"],
+ acl["mask"], acl["trustee"])
+
+ spoolss.closeprinter(hnd)
+
+ sys.exit(0)
+
+# Set security descriptor
+
+if op == "setsec":
+
+ # Open printer
+
+ try:
+ hnd = spoolss.openprinter(printername,
+ creds = {"domain": "NPSD-TEST2",
+ "username": "Administrator",
+ "password": "penguin"})
+ except:
+ print "error opening printer %s" % printername
+ sys.exit(1)
+
+ # Read lines from standard input and build security descriptor
+
+ lines = sys.stdin.readlines()
+
+ secdesc = {}
+
+ secdesc["owner_sid"] = lines[0]
+ secdesc["group_sid"] = lines[1]
+
+ secdesc["revision"] = 1
+ secdesc["dacl"] = {}
+ secdesc["dacl"]["revision"] = 2
+ secdesc["dacl"]["ace_list"] = []
+
+ for acl in lines[2:]:
+ match = re.match("(\d+) (\d+) (0[xX][\dA-Fa-f]+) (\S+)", acl)
+ secdesc["dacl"]["ace_list"].append(
+ {"type": int(match.group(1)), "flags": int(match.group(2)),
+ "mask": string.atoi(match.group(3), 0), "trustee": match.group(4)})
+
+ # Build info3 structure
+
+ info3 = {}
+
+ info3["flags"] = 0x8004 # self-relative, dacl present
+ info3["level"] = 3
+ info3["security_descriptor"] = secdesc
+
+ hnd.setprinter(info3)
+
+ spoolss.closeprinter(hnd)
+ sys.exit(0)
+
+print "invalid operation %s" % op
+sys.exit(1)
diff --git a/source3/python/examples/tdbpack/tdbtimetrial.py b/source3/python/examples/tdbpack/tdbtimetrial.py
new file mode 100755
index 0000000000..be6404899d
--- /dev/null
+++ b/source3/python/examples/tdbpack/tdbtimetrial.py
@@ -0,0 +1,12 @@
+#! /usr/bin/python2.2
+
+def run_trial():
+ # import tdbutil
+ from samba.tdbpack import pack
+
+ for i in xrange(500000):
+ pack("ddffd", (10, 2, "mbp", "martin", 0))
+ #s = "\n\0\0\0" + "\x02\0\0\0" + "mbp\0" + "martin\0" + "\0\0\0\0"
+
+if __name__ == '__main__':
+ run_trial()
diff --git a/source3/python/examples/tdbpack/test_tdbpack.py b/source3/python/examples/tdbpack/test_tdbpack.py
new file mode 100755
index 0000000000..36fed881e3
--- /dev/null
+++ b/source3/python/examples/tdbpack/test_tdbpack.py
@@ -0,0 +1,195 @@
+#! /usr/bin/env python2.2
+
+__doc__ = """test case for samba.tdbkpack functions
+
+tdbpack provides a means of pickling values into binary formats
+compatible with that used by the samba tdbpack()/tdbunpack()
+functions.
+
+Numbers are always stored in little-endian format; strings are stored
+in either DOS or Unix codepage as appropriate.
+
+The format for any particular element is encoded as a short ASCII
+string, with one character per field."""
+
+# Copyright (C) 2002 Hewlett-Packard.
+
+__author__ = 'Martin Pool <mbp@sourcefrog.net>'
+
+import unittest
+# import tdbutil
+import samba.tdbpack
+
+packer = samba.tdbpack.pack
+unpacker = samba.tdbpack.unpack
+
+
+class PackTests(unittest.TestCase):
+ symm_cases = [('B', ['hello' * 51], '\xff\0\0\0' + 'hello' * 51),
+ ('w', [42], '\x2a\0'),
+ ('www', [42, 2, 69], '\x2a\0\x02\0\x45\0'),
+ ('wd', [42, 256], '\x2a\0\0\x01\0\0'),
+ ('w', [0], '\0\0'),
+ ('w', [255], '\xff\0'),
+ ('w', [256], '\0\x01'),
+ ('w', [0xdead], '\xad\xde'),
+ ('w', [0xffff], '\xff\xff'),
+ ('p', [0], '\0\0\0\0'),
+ ('p', [1], '\x01\0\0\0'),
+ ('d', [0x01020304], '\x04\x03\x02\x01'),
+ ('d', [0x7fffffff], '\xff\xff\xff\x7f'),
+ ('d', [0x80000000], '\x00\x00\x00\x80'),
+ ('d', [-1], '\xff\xff\xff\xff'),
+ ('d', [-255], '\x01\xff\xff\xff'),
+ ('d', [-256], '\x00\xff\xff\xff'),
+ ('ddd', [1, 10, 50], '\x01\0\0\0\x0a\0\0\0\x32\0\0\0'),
+ ('ff', ['hello', 'world'], 'hello\0world\0'),
+ ('fP', ['hello', 'world'], 'hello\0world\0'),
+ ('PP', ['hello', 'world'], 'hello\0world\0'),
+ ('B', [''], '\0\0\0\0'),
+ ('B', ['hello'], '\x05\0\0\0hello'),
+ ('BB', ['hello\0world', 'now'],
+ '\x0b\0\0\0hello\0world\x03\0\0\0now'),
+ ('pd', [1, 10], '\x01\0\0\0\x0a\0\0\0'),
+ ('BBB', ['hello', '', 'world'],
+ '\x05\0\0\0hello\0\0\0\0\x05\0\0\0world'),
+
+ # strings are sequences in Python, there's no getting away
+ # from it
+ ('ffff', 'evil', 'e\0v\0i\0l\0'),
+ ('BBBB', 'evil',
+ '\x01\0\0\0e'
+ '\x01\0\0\0v'
+ '\x01\0\0\0i'
+ '\x01\0\0\0l'),
+
+ ('', [], ''),
+
+ # exercise some long strings
+ ('PP', ['hello' * 255, 'world' * 255],
+ 'hello' * 255 + '\0' + 'world' * 255 + '\0'),
+ ('PP', ['hello' * 40000, 'world' * 50000],
+ 'hello' * 40000 + '\0' + 'world' * 50000 + '\0'),
+ ('B', ['hello' * 51], '\xff\0\0\0' + 'hello' * 51),
+ ('BB', ['hello' * 40000, 'world' * 50000],
+ '\x40\x0d\x03\0' + 'hello' * 40000 + '\x90\xd0\x03\x00' + 'world' * 50000),
+ ]
+
+ def test_symmetric(self):
+ """Cookbook of symmetric pack/unpack tests
+ """
+ for format, values, expected in self.symm_cases:
+ self.assertEquals(packer(format, values), expected)
+ out, rest = unpacker(format, expected)
+ self.assertEquals(rest, '')
+ self.assertEquals(list(values), list(out))
+
+
+ def test_pack(self):
+ """Cookbook of expected pack values
+
+ These can't be used for the symmetric test because the unpacked value is
+ not "canonical".
+ """
+ cases = [('w', (42,), '\x2a\0'),
+ ('p', [None], '\0\0\0\0'),
+ ('p', ['true'], '\x01\0\0\0'),
+
+ ('w', {1: 'fruit'}, '\x01\0'),
+ # passing a dictionary is dodgy, but it gets coerced to keys
+ # as if you called list()
+ ]
+
+ for format, values, expected in cases:
+ self.assertEquals(packer(format, values), expected)
+
+ def test_unpack_extra(self):
+ # Test leftover data
+ for format, values, packed in self.symm_cases:
+ out, rest = unpacker(format, packed + 'hello sailor!')
+ self.assertEquals(rest, 'hello sailor!')
+ self.assertEquals(list(values), list(out))
+
+
+ def test_unpack(self):
+ """Cookbook of tricky unpack tests"""
+ cases = [
+ ]
+ for format, values, expected in cases:
+ out, rest = unpacker(format, expected)
+ self.assertEquals(rest, '')
+ self.assertEquals(list(values), list(out))
+
+
+ def test_pack_failures(self):
+ """Expected errors for incorrect packing"""
+ cases = [('w', [], IndexError),
+ ('w', (), IndexError),
+ ('w', {}, IndexError),
+ ('ww', [2], IndexError),
+ ('w', 2, TypeError),
+ ('', [1, 2, 3], IndexError),
+ ('w', None, TypeError),
+ ('wwwwwwwwwwww', [], IndexError),
+ ('w', [2, 3], IndexError),
+ ('w', [0x60A15EC5L], TypeError),
+ ('w', [None], TypeError),
+ ('w', xrange(10000), IndexError),
+ ('d', [], IndexError),
+ ('d', [0L], TypeError),
+ ('p', [], IndexError),
+ ('f', [2], TypeError),
+ ('P', [None], TypeError),
+ ('P', (), IndexError),
+ ('f', [packer], TypeError),
+ ('fw', ['hello'], IndexError),
+ ('f', [u'hello'], TypeError),
+ ('B', [2], TypeError),
+ (None, [2, 3, 4], TypeError),
+ (ord('f'), [20], TypeError),
+ (['w', 'w'], [2, 2], TypeError),
+ ('Q', [2], ValueError),
+ ('fQ', ['2', 3], ValueError),
+ ('fQ', ['2'], IndexError),
+ (2, [2], TypeError),
+ ({}, {}, TypeError)]
+ for format, values, throwable_class in cases:
+ def do_pack():
+ packer(format, values)
+ self.assertRaises(throwable_class, do_pack)
+
+
+ def test_unpack_failures(self):
+ """Expected errors for incorrect unpacking"""
+ cases = [('$', '', ValueError),
+ ('Q', '', ValueError),
+ ('Q$', '', ValueError),
+ ('f', '', IndexError),
+ ('d', '', IndexError),
+ ('d', '2', IndexError),
+ ('d', '22', IndexError),
+ ('d', '222', IndexError),
+ ('w', '', IndexError),
+ ('w', '2', IndexError),
+ ('f', 'hello', IndexError),
+ ('f', '', IndexError),
+ ('p', '\x01\0', IndexError),
+ ('B', '\xff\0\0\0hello', IndexError),
+ ('B', '\xff\0', IndexError),
+ ('B', '\x01\0\0\0', IndexError),
+ ('B', '\x05\0\0\0hell', IndexError),
+ ('B', '\xff\xff\xff\xff', ValueError),
+ ('B', 'foobar', IndexError),
+ ('BB', '\x01\0\0\0a\x01', IndexError),
+ ]
+
+ for format, values, throwable_class in cases:
+ def do_unpack():
+ unpacker(format, values)
+ self.assertRaises(throwable_class, do_unpack)
+
+
+
+if __name__ == '__main__':
+ unittest.main()
+