r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[Samba/gebeck_regimport.git] / source3 / python / examples / spoolss / psec.py
blob498a0ef1744e2d3a9d470ef70f8c369bb023eed8
1 #!/usr/bin/env python
3 # Get or set the security descriptor on a printer
6 import sys, re, string
7 from samba import spoolss
9 if len(sys.argv) != 3:
10 print "Usage: psec.py getsec|setsec printername"
11 sys.exit(1)
13 op = sys.argv[1]
14 printername = sys.argv[2]
16 # Display security descriptor
18 if op == "getsec":
20 try:
21 hnd = spoolss.openprinter(printername)
22 except:
23 print "error opening printer %s" % printername
24 sys.exit(1)
26 secdesc = hnd.getprinter(level = 3)["security_descriptor"]
28 print secdesc["owner_sid"]
29 print secdesc["group_sid"]
31 for acl in secdesc["dacl"]["ace_list"]:
32 print "%d %d 0x%08x %s" % (acl["type"], acl["flags"],
33 acl["mask"], acl["trustee"])
35 spoolss.closeprinter(hnd)
37 sys.exit(0)
39 # Set security descriptor
41 if op == "setsec":
43 # Open printer
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)
54 # Read lines from standard input and build security descriptor
56 lines = sys.stdin.readlines()
58 secdesc = {}
60 secdesc["owner_sid"] = lines[0]
61 secdesc["group_sid"] = lines[1]
63 secdesc["revision"] = 1
64 secdesc["dacl"] = {}
65 secdesc["dacl"]["revision"] = 2
66 secdesc["dacl"]["ace_list"] = []
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)})
74 # Build info3 structure
76 info3 = {}
78 info3["flags"] = 0x8004 # self-relative, dacl present
79 info3["level"] = 3
80 info3["security_descriptor"] = secdesc
82 hnd.setprinter(info3)
84 spoolss.closeprinter(hnd)
85 sys.exit(0)
87 print "invalid operation %s" % op
88 sys.exit(1)