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