source: branches/samba-3.0/source/python/examples/spoolss/psec.py

Last change on this file was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 2.1 KB
Line 
1#!/usr/bin/env python
2#
3# Get or set the security descriptor on a printer
4#
5
6import sys, re, string
7from samba import spoolss
8
9if len(sys.argv) != 3:
10 print "Usage: psec.py getsec|setsec printername"
11 sys.exit(1)
12
13op = sys.argv[1]
14printername = sys.argv[2]
15
16# Display security descriptor
17
18if op == "getsec":
19
20 try:
21 hnd = spoolss.openprinter(printername)
22 except:
23 print "error opening printer %s" % printername
24 sys.exit(1)
25
26 secdesc = hnd.getprinter(level = 3)["security_descriptor"]
27
28 print secdesc["owner_sid"]
29 print secdesc["group_sid"]
30
31 for acl in secdesc["dacl"]["ace_list"]:
32 print "%d %d 0x%08x %s" % (acl["type"], acl["flags"],
33 acl["mask"], acl["trustee"])
34
35 spoolss.closeprinter(hnd)
36
37 sys.exit(0)
38
39# Set security descriptor
40
41if op == "setsec":
42
43 # Open printer
44
45 try:
46 hnd = spoolss.openprinter(printername,
47 creds = {"domain": "NPSD-TEST2",
48 "username": "Administrator",
49 "password": "penguin"})
50 except:
51 print "error opening printer %s" % printername
52 sys.exit(1)
53
54 # Read lines from standard input and build security descriptor
55
56 lines = sys.stdin.readlines()
57
58 secdesc = {}
59
60 secdesc["owner_sid"] = lines[0]
61 secdesc["group_sid"] = lines[1]
62
63 secdesc["revision"] = 1
64 secdesc["dacl"] = {}
65 secdesc["dacl"]["revision"] = 2
66 secdesc["dacl"]["ace_list"] = []
67
68 for acl in lines[2:]:
69 match = re.match("(\d+) (\d+) (0[xX][\dA-Fa-f]+) (\S+)", acl)
70 secdesc["dacl"]["ace_list"].append(
71 {"type": int(match.group(1)), "flags": int(match.group(2)),
72 "mask": string.atoi(match.group(3), 0), "trustee": match.group(4)})
73
74 # Build info3 structure
75
76 info3 = {}
77
78 info3["flags"] = 0x8004 # self-relative, dacl present
79 info3["level"] = 3
80 info3["security_descriptor"] = secdesc
81
82 hnd.setprinter(info3)
83
84 spoolss.closeprinter(hnd)
85 sys.exit(0)
86
87print "invalid operation %s" % op
88sys.exit(1)
Note: See TracBrowser for help on using the repository browser.