s3: Fix Coverity ID 242700 Dereference before null check
[Samba.git] / source3 / rpc_client / cli_winreg_spoolss.c
blobfa7096cf77cd855acefa4e8bedf2282eef2536db
1 /*
2 * Unix SMB/CIFS implementation.
4 * SPOOLSS RPC Pipe server / winreg client routines
6 * Copyright (c) 2010 Andreas Schneider <asn@samba.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "nt_printing.h"
24 #include "../librpc/gen_ndr/ndr_spoolss.h"
25 #include "../librpc/gen_ndr/ndr_winreg_c.h"
26 #include "../librpc/gen_ndr/ndr_security.h"
27 #include "secrets.h"
28 #include "../libcli/security/security.h"
29 #include "rpc_client/cli_winreg.h"
30 #include "../libcli/registry/util_reg.h"
31 #include "rpc_client/cli_winreg_spoolss.h"
32 #include "printing/nt_printing_os2.h"
33 #include "rpc_client/init_spoolss.h"
35 #define TOP_LEVEL_PRINT_KEY "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print"
36 #define TOP_LEVEL_PRINT_PRINTERS_KEY TOP_LEVEL_PRINT_KEY "\\Printers"
37 #define TOP_LEVEL_CONTROL_KEY "SYSTEM\\CurrentControlSet\\Control\\Print"
38 #define TOP_LEVEL_CONTROL_FORMS_KEY TOP_LEVEL_CONTROL_KEY "\\Forms"
40 #define EMPTY_STRING ""
42 #define FILL_STRING(mem_ctx, in, out) \
43 do { \
44 if (in && strlen(in)) { \
45 out = talloc_strdup(mem_ctx, in); \
46 } else { \
47 out = talloc_strdup(mem_ctx, ""); \
48 } \
49 W_ERROR_HAVE_NO_MEMORY(out); \
50 } while (0);
52 #define CHECK_ERROR(result) \
53 if (W_ERROR_IS_OK(result)) continue; \
54 if (W_ERROR_EQUAL(result, WERR_NOT_FOUND)) result = WERR_OK; \
55 if (!W_ERROR_IS_OK(result)) break
57 /* FLAGS, NAME, with, height, left, top, right, bottom */
58 static const struct spoolss_FormInfo1 builtin_forms1[] = {
59 { SPOOLSS_FORM_BUILTIN, "10x11", {0x3e030,0x44368}, {0x0,0x0,0x3e030,0x44368} },
60 { SPOOLSS_FORM_BUILTIN, "10x14", {0x3e030,0x56d10}, {0x0,0x0,0x3e030,0x56d10} },
61 { SPOOLSS_FORM_BUILTIN, "11x17", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
62 { SPOOLSS_FORM_BUILTIN, "12x11", {0x4a724,0x443e1}, {0x0,0x0,0x4a724,0x443e1} },
63 { SPOOLSS_FORM_BUILTIN, "15x11", {0x5d048,0x44368}, {0x0,0x0,0x5d048,0x44368} },
64 { SPOOLSS_FORM_BUILTIN, "6 3/4 Envelope", {0x167ab,0x284ec}, {0x0,0x0,0x167ab,0x284ec} },
65 { SPOOLSS_FORM_BUILTIN, "9x11", {0x37cf8,0x44368}, {0x0,0x0,0x37cf8,0x44368} },
66 { SPOOLSS_FORM_BUILTIN, "A0", {0xcd528,0x122488},{0x0,0x0,0xcd528,0x122488} },
67 { SPOOLSS_FORM_BUILTIN, "A1", {0x91050,0xcd528}, {0x0,0x0,0x91050,0xcd528} },
68 { SPOOLSS_FORM_BUILTIN, "A2", {0x668a0,0x91050}, {0x0,0x0,0x668a0,0x91050} },
69 { SPOOLSS_FORM_BUILTIN, "A3 Extra Transverse", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
70 { SPOOLSS_FORM_BUILTIN, "A3 Extra", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
71 { SPOOLSS_FORM_BUILTIN, "A3 Rotated", {0x668a0,0x48828}, {0x0,0x0,0x668a0,0x48828} },
72 { SPOOLSS_FORM_BUILTIN, "A3 Transverse", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
73 { SPOOLSS_FORM_BUILTIN, "A3", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
74 { SPOOLSS_FORM_BUILTIN, "A4 Extra", {0x397c2,0x4eb16}, {0x0,0x0,0x397c2,0x4eb16} },
75 { SPOOLSS_FORM_BUILTIN, "A4 Plus", {0x33450,0x50910}, {0x0,0x0,0x33450,0x50910} },
76 { SPOOLSS_FORM_BUILTIN, "A4 Rotated", {0x48828,0x33450}, {0x0,0x0,0x48828,0x33450} },
77 { SPOOLSS_FORM_BUILTIN, "A4 Small", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
78 { SPOOLSS_FORM_BUILTIN, "A4 Transverse", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
79 { SPOOLSS_FORM_BUILTIN, "A4", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
80 { SPOOLSS_FORM_BUILTIN, "A5 Extra", {0x2a7b0,0x395f8}, {0x0,0x0,0x2a7b0,0x395f8} },
81 { SPOOLSS_FORM_BUILTIN, "A5 Rotated", {0x33450,0x24220}, {0x0,0x0,0x33450,0x24220} },
82 { SPOOLSS_FORM_BUILTIN, "A5 Transverse", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
83 { SPOOLSS_FORM_BUILTIN, "A5", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
84 { SPOOLSS_FORM_BUILTIN, "A6 Rotated", {0x24220,0x19a28}, {0x0,0x0,0x24220,0x19a28} },
85 { SPOOLSS_FORM_BUILTIN, "A6", {0x19a28,0x24220}, {0x0,0x0,0x19a28,0x24220} },
86 { SPOOLSS_FORM_BUILTIN, "B4 (ISO)", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
87 { SPOOLSS_FORM_BUILTIN, "B4 (JIS) Rotated", {0x58de0,0x3ebe8}, {0x0,0x0,0x58de0,0x3ebe8} },
88 { SPOOLSS_FORM_BUILTIN, "B4 (JIS)", {0x3ebe8,0x58de0}, {0x0,0x0,0x3ebe8,0x58de0} },
89 { SPOOLSS_FORM_BUILTIN, "B5 (ISO) Extra", {0x31128,0x43620}, {0x0,0x0,0x31128,0x43620} },
90 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Rotated", {0x3ebe8,0x2c6f0}, {0x0,0x0,0x3ebe8,0x2c6f0} },
91 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Transverse", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
92 { SPOOLSS_FORM_BUILTIN, "B5 (JIS)", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
93 { SPOOLSS_FORM_BUILTIN, "B6 (JIS) Rotated", {0x2c6f0,0x1f400}, {0x0,0x0,0x2c6f0,0x1f400} },
94 { SPOOLSS_FORM_BUILTIN, "B6 (JIS)", {0x1f400,0x2c6f0}, {0x0,0x0,0x1f400,0x2c6f0} },
95 { SPOOLSS_FORM_BUILTIN, "C size sheet", {0x696b8,0x886d0}, {0x0,0x0,0x696b8,0x886d0} },
96 { SPOOLSS_FORM_BUILTIN, "D size sheet", {0x886d0,0xd2d70}, {0x0,0x0,0x886d0,0xd2d70} },
97 { SPOOLSS_FORM_BUILTIN, "Double Japan Postcard Rotated", {0x24220,0x30d40}, {0x0,0x0,0x24220,0x30d40} },
98 { SPOOLSS_FORM_BUILTIN, "E size sheet", {0xd2d70,0x110da0},{0x0,0x0,0xd2d70,0x110da0} },
99 { SPOOLSS_FORM_BUILTIN, "Envelope #10", {0x19947,0x3ae94}, {0x0,0x0,0x19947,0x3ae94} },
100 { SPOOLSS_FORM_BUILTIN, "Envelope #11", {0x1be7c,0x40565}, {0x0,0x0,0x1be7c,0x40565} },
101 { SPOOLSS_FORM_BUILTIN, "Envelope #12", {0x1d74a,0x44368}, {0x0,0x0,0x1d74a,0x44368} },
102 { SPOOLSS_FORM_BUILTIN, "Envelope #14", {0x1f018,0x47504}, {0x0,0x0,0x1f018,0x47504} },
103 { SPOOLSS_FORM_BUILTIN, "Envelope #9", {0x18079,0x37091}, {0x0,0x0,0x18079,0x37091} },
104 { SPOOLSS_FORM_BUILTIN, "Envelope B4", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
105 { SPOOLSS_FORM_BUILTIN, "Envelope B5", {0x2af80,0x3d090}, {0x0,0x0,0x2af80,0x3d090} },
106 { SPOOLSS_FORM_BUILTIN, "Envelope B6", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
107 { SPOOLSS_FORM_BUILTIN, "Envelope C3", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
108 { SPOOLSS_FORM_BUILTIN, "Envelope C4", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
109 { SPOOLSS_FORM_BUILTIN, "Envelope C5", {0x278d0,0x37e88}, {0x0,0x0,0x278d0,0x37e88} },
110 { SPOOLSS_FORM_BUILTIN, "Envelope C6", {0x1bd50,0x278d0}, {0x0,0x0,0x1bd50,0x278d0} },
111 { SPOOLSS_FORM_BUILTIN, "Envelope C65", {0x1bd50,0x37e88}, {0x0,0x0,0x1bd50,0x37e88} },
112 { SPOOLSS_FORM_BUILTIN, "Envelope DL", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
113 { SPOOLSS_FORM_BUILTIN, "Envelope Invite", {0x35b60,0x35b60}, {0x0,0x0,0x35b60,0x35b60} },
114 { SPOOLSS_FORM_BUILTIN, "Envelope Monarch", {0x18079,0x2e824}, {0x0,0x0,0x18079,0x2e824} },
115 { SPOOLSS_FORM_BUILTIN, "Envelope", {0x1adb0,0x38270}, {0x0,0x0,0x1adb0,0x38270} },
116 { SPOOLSS_FORM_BUILTIN, "Executive", {0x2cf56,0x411cc}, {0x0,0x0,0x2cf56,0x411cc} },
117 { SPOOLSS_FORM_BUILTIN, "Folio", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
118 { SPOOLSS_FORM_BUILTIN, "German Legal Fanfold", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
119 { SPOOLSS_FORM_BUILTIN, "German Std Fanfold", {0x34b5c,0x4a6a0}, {0x0,0x0,0x34b5c,0x4a6a0} },
120 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #3 Rotated", {0x395f8,0x1d4c0}, {0x0,0x0,0x395f8,0x1d4c0} },
121 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #4 Rotated", {0x320c8,0x15f90}, {0x0,0x0,0x320c8,0x15f90} },
122 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #2 Rotated", {0x510e0,0x3a980}, {0x0,0x0,0x510e0,0x3a980} },
123 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #3 Rotated", {0x43a08,0x34bc0}, {0x0,0x0,0x43a08,0x34bc0} },
124 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4 Rotated", {0x395f8,0x19a28}, {0x0,0x0,0x395f8,0x19a28} },
125 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4", {0x19a28,0x395f8}, {0x0,0x0,0x19a28,0x395f8} },
126 { SPOOLSS_FORM_BUILTIN, "Japanese Double Postcard", {0x30d40,0x24220}, {0x0,0x0,0x30d40,0x24220} },
127 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #3", {0x1d4c0,0x395f8}, {0x0,0x0,0x1d4c0,0x395f8} },
128 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #4", {0x15f90,0x320c8}, {0x0,0x0,0x15f90,0x320c8} },
129 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #2", {0x3a980,0x510e0}, {0x0,0x0,0x3a980,0x510e0} },
130 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #3", {0x34bc0,0x43a08}, {0x0,0x0,0x34bc0,0x43a08} },
131 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard Rotated", {0x24220,0x186a0}, {0x0,0x0,0x24220,0x186a0} },
132 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard", {0x186a0,0x24220}, {0x0,0x0,0x186a0,0x24220} },
133 { SPOOLSS_FORM_BUILTIN, "Ledger", {0x696b8,0x44368}, {0x0,0x0,0x696b8,0x44368} },
134 { SPOOLSS_FORM_BUILTIN, "Legal Extra", {0x3ae94,0x5d048}, {0x0,0x0,0x3ae94,0x5d048} },
135 { SPOOLSS_FORM_BUILTIN, "Legal", {0x34b5c,0x56d10}, {0x0,0x0,0x34b5c,0x56d10} },
136 { SPOOLSS_FORM_BUILTIN, "Letter Extra Transverse", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
137 { SPOOLSS_FORM_BUILTIN, "Letter Extra", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
138 { SPOOLSS_FORM_BUILTIN, "Letter Plus", {0x34b5c,0x4eb16}, {0x0,0x0,0x34b5c,0x4eb16} },
139 { SPOOLSS_FORM_BUILTIN, "Letter Rotated", {0x44368,0x34b5c}, {0x0,0x0,0x44368,0x34b5c} },
140 { SPOOLSS_FORM_BUILTIN, "Letter Small", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
141 { SPOOLSS_FORM_BUILTIN, "Letter Transverse", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
142 { SPOOLSS_FORM_BUILTIN, "Letter", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
143 { SPOOLSS_FORM_BUILTIN, "Note", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
144 { SPOOLSS_FORM_BUILTIN, "PRC 16K Rotated", {0x3f7a0,0x2de60}, {0x0,0x0,0x3f7a0,0x2de60} },
145 { SPOOLSS_FORM_BUILTIN, "PRC 16K", {0x2de60,0x3f7a0}, {0x0,0x0,0x2de60,0x3f7a0} },
146 { SPOOLSS_FORM_BUILTIN, "PRC 32K Rotated", {0x2cec0,0x1fbd0}, {0x0,0x0,0x2cec0,0x1fbd0} },
147 { SPOOLSS_FORM_BUILTIN, "PRC 32K", {0x1fbd0,0x2cec0}, {0x0,0x0,0x1fbd0,0x2cec0} },
148 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big) Rotated", {0x318f8,0x222e0}, {0x0,0x0,0x318f8,0x222e0} },
149 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big)", {0x222e0,0x318f8}, {0x0,0x0,0x222e0,0x318f8} },
150 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1 Rotated", {0x28488,0x18e70}, {0x0,0x0,0x28488,0x18e70} },
151 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1", {0x18e70,0x28488}, {0x0,0x0,0x18e70,0x28488} },
152 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10 Rotated", {0x6fd10,0x4f1a0}, {0x0,0x0,0x6fd10,0x4f1a0} },
153 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
154 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2 Rotated", {0x2af80,0x18e70}, {0x0,0x0,0x2af80,0x18e70} },
155 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2", {0x18e70,0x2af80}, {0x0,0x0,0x18e70,0x2af80} },
156 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3 Rotated", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
157 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3", {0x1e848,0x2af80}, {0x0,0x0,0x1e848,0x2af80} },
158 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4 Rotated", {0x32c80,0x1adb0}, {0x0,0x0,0x32c80,0x1adb0} },
159 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4", {0x1adb0,0x32c80}, {0x0,0x0,0x1adb0,0x32c80} },
160 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5 Rotated", {0x35b60,0x1adb0}, {0x0,0x0,0x35b60,0x1adb0} },
161 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
162 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6 Rotated", {0x38270,0x1d4c0}, {0x0,0x0,0x38270,0x1d4c0} },
163 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6", {0x1d4c0,0x38270}, {0x0,0x0,0x1d4c0,0x38270} },
164 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7 Rotated", {0x38270,0x27100}, {0x0,0x0,0x38270,0x27100} },
165 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7", {0x27100,0x38270}, {0x0,0x0,0x27100,0x38270} },
166 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8 Rotated", {0x4b708,0x1d4c0}, {0x0,0x0,0x4b708,0x1d4c0} },
167 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8", {0x1d4c0,0x4b708}, {0x0,0x0,0x1d4c0,0x4b708} },
168 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9 Rotated", {0x4f1a0,0x37e88}, {0x0,0x0,0x4f1a0,0x37e88} },
169 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
170 { SPOOLSS_FORM_BUILTIN, "Quarto", {0x347d8,0x43238}, {0x0,0x0,0x347d8,0x43238} },
171 { SPOOLSS_FORM_BUILTIN, "Reserved48", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
172 { SPOOLSS_FORM_BUILTIN, "Reserved49", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
173 { SPOOLSS_FORM_BUILTIN, "Statement", {0x221b4,0x34b5c}, {0x0,0x0,0x221b4,0x34b5c} },
174 { SPOOLSS_FORM_BUILTIN, "Super A", {0x376b8,0x56ea0}, {0x0,0x0,0x376b8,0x56ea0} },
175 { SPOOLSS_FORM_BUILTIN, "Super B", {0x4a768,0x76e58}, {0x0,0x0,0x4a768,0x76e58} },
176 { SPOOLSS_FORM_BUILTIN, "Tabloid Extra", {0x4a6a0,0x6f9f0}, {0x0,0x0,0x4a6a0,0x6f9f0} },
177 { SPOOLSS_FORM_BUILTIN, "Tabloid", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
178 { SPOOLSS_FORM_BUILTIN, "US Std Fanfold", {0x5c3e1,0x44368}, {0x0,0x0,0x5c3e1,0x44368} }
181 /********************************************************************
182 static helper functions
183 ********************************************************************/
185 /****************************************************************************
186 Update the changeid time.
187 ****************************************************************************/
189 * @internal
191 * @brief Update the ChangeID time of a printer.
193 * This is SO NASTY as some drivers need this to change, others need it
194 * static. This value will change every second, and I must hope that this
195 * is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
196 * UTAH ! JRA.
198 * @return The ChangeID.
200 static uint32_t winreg_printer_rev_changeid(void)
202 struct timeval tv;
204 get_process_uptime(&tv);
206 #if 1 /* JERRY */
207 /* Return changeid as msec since spooler restart */
208 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
209 #else
211 * This setting seems to work well but is too untested
212 * to replace the above calculation. Left in for experimentation
213 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
215 return tv.tv_sec * 10 + tv.tv_usec / 100000;
216 #endif
219 static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
220 struct dcerpc_binding_handle *binding_handle,
221 const char *path,
222 const char *key,
223 bool create_key,
224 uint32_t access_mask,
225 struct policy_handle *hive_handle,
226 struct policy_handle *key_handle)
228 struct winreg_String wkey, wkeyclass;
229 char *keyname;
230 NTSTATUS status;
231 WERROR result = WERR_OK;
233 status = dcerpc_winreg_OpenHKLM(binding_handle,
234 mem_ctx,
235 NULL,
236 access_mask,
237 hive_handle,
238 &result);
239 if (!NT_STATUS_IS_OK(status)) {
240 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
241 nt_errstr(status)));
242 return ntstatus_to_werror(status);
244 if (!W_ERROR_IS_OK(result)) {
245 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
246 win_errstr(result)));
247 return result;
250 if (key && *key) {
251 keyname = talloc_asprintf(mem_ctx, "%s\\%s", path, key);
252 } else {
253 keyname = talloc_strdup(mem_ctx, path);
255 if (keyname == NULL) {
256 return WERR_NOMEM;
259 ZERO_STRUCT(wkey);
260 wkey.name = keyname;
262 if (create_key) {
263 enum winreg_CreateAction action = REG_ACTION_NONE;
265 ZERO_STRUCT(wkeyclass);
266 wkeyclass.name = "";
268 status = dcerpc_winreg_CreateKey(binding_handle,
269 mem_ctx,
270 hive_handle,
271 wkey,
272 wkeyclass,
274 access_mask,
275 NULL,
276 key_handle,
277 &action,
278 &result);
279 switch (action) {
280 case REG_ACTION_NONE:
281 DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
282 break;
283 case REG_CREATED_NEW_KEY:
284 DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
285 break;
286 case REG_OPENED_EXISTING_KEY:
287 DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
288 break;
290 } else {
291 status = dcerpc_winreg_OpenKey(binding_handle,
292 mem_ctx,
293 hive_handle,
294 wkey,
296 access_mask,
297 key_handle,
298 &result);
300 if (!NT_STATUS_IS_OK(status)) {
301 result = ntstatus_to_werror(status);
303 if (!W_ERROR_IS_OK(result)) {
304 WERROR ignore;
306 if (is_valid_policy_hnd(hive_handle)) {
307 dcerpc_winreg_CloseKey(binding_handle,
308 mem_ctx,
309 hive_handle,
310 &ignore);
312 ZERO_STRUCTP(hive_handle);
314 return result;
317 return WERR_OK;
321 * @brief Create the registry keyname for the given printer.
323 * @param[in] mem_ctx The memory context to use.
325 * @param[in] printer The name of the printer to get the registry key.
327 * @return The registry key or NULL on error.
329 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
330 return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
333 static WERROR winreg_printer_opendriver(TALLOC_CTX *mem_ctx,
334 struct dcerpc_binding_handle *winreg_handle,
335 const char *drivername,
336 const char *architecture,
337 uint32_t version,
338 uint32_t access_mask,
339 bool create,
340 struct policy_handle *hive_hnd,
341 struct policy_handle *key_hnd)
343 WERROR result;
344 char *key_name;
346 key_name = talloc_asprintf(mem_ctx, "%s\\Environments\\%s\\Drivers\\Version-%u",
347 TOP_LEVEL_CONTROL_KEY,
348 architecture, version);
349 if (!key_name) {
350 return WERR_NOMEM;
353 result = winreg_printer_openkey(mem_ctx,
354 winreg_handle,
355 key_name,
356 drivername,
357 create,
358 access_mask,
359 hive_hnd,
360 key_hnd);
361 return result;
364 static WERROR winreg_enumval_to_dword(TALLOC_CTX *mem_ctx,
365 struct spoolss_PrinterEnumValues *v,
366 const char *valuename, uint32_t *dw)
368 /* just return if it is not the one we are looking for */
369 if (strcmp(valuename, v->value_name) != 0) {
370 return WERR_NOT_FOUND;
373 if (v->type != REG_DWORD) {
374 return WERR_INVALID_DATATYPE;
377 if (v->data_length != 4) {
378 *dw = 0;
379 return WERR_OK;
382 *dw = IVAL(v->data->data, 0);
383 return WERR_OK;
386 static WERROR winreg_enumval_to_sz(TALLOC_CTX *mem_ctx,
387 struct spoolss_PrinterEnumValues *v,
388 const char *valuename, const char **_str)
390 /* just return if it is not the one we are looking for */
391 if (strcmp(valuename, v->value_name) != 0) {
392 return WERR_NOT_FOUND;
395 if (v->type != REG_SZ) {
396 return WERR_INVALID_DATATYPE;
399 if (v->data_length == 0) {
400 *_str = talloc_strdup(mem_ctx, EMPTY_STRING);
401 if (*_str == NULL) {
402 return WERR_NOMEM;
404 return WERR_OK;
407 if (!pull_reg_sz(mem_ctx, v->data, _str)) {
408 return WERR_NOMEM;
411 return WERR_OK;
414 static WERROR winreg_enumval_to_multi_sz(TALLOC_CTX *mem_ctx,
415 struct spoolss_PrinterEnumValues *v,
416 const char *valuename,
417 const char ***array)
419 /* just return if it is not the one we are looking for */
420 if (strcmp(valuename, v->value_name) != 0) {
421 return WERR_NOT_FOUND;
424 if (v->type != REG_MULTI_SZ) {
425 return WERR_INVALID_DATATYPE;
428 if (v->data_length == 0) {
429 *array = talloc_array(mem_ctx, const char *, 1);
430 if (*array == NULL) {
431 return WERR_NOMEM;
433 *array[0] = NULL;
434 return WERR_OK;
437 if (!pull_reg_multi_sz(mem_ctx, v->data, array)) {
438 return WERR_NOMEM;
441 return WERR_OK;
444 static WERROR winreg_printer_write_date(TALLOC_CTX *mem_ctx,
445 struct dcerpc_binding_handle *winreg_handle,
446 struct policy_handle *key_handle,
447 const char *value,
448 NTTIME data)
450 struct winreg_String wvalue = { 0, };
451 DATA_BLOB blob;
452 WERROR result = WERR_OK;
453 NTSTATUS status;
454 const char *str;
455 struct tm *tm;
456 time_t t;
458 if (data == 0) {
459 str = talloc_strdup(mem_ctx, "01/01/1601");
460 } else {
461 t = nt_time_to_unix(data);
462 tm = localtime(&t);
463 if (tm == NULL) {
464 return map_werror_from_unix(errno);
466 str = talloc_asprintf(mem_ctx, "%02d/%02d/%04d",
467 tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
469 if (!str) {
470 return WERR_NOMEM;
473 wvalue.name = value;
474 if (!push_reg_sz(mem_ctx, &blob, str)) {
475 return WERR_NOMEM;
477 status = dcerpc_winreg_SetValue(winreg_handle,
478 mem_ctx,
479 key_handle,
480 wvalue,
481 REG_SZ,
482 blob.data,
483 blob.length,
484 &result);
485 if (!NT_STATUS_IS_OK(status)) {
486 result = ntstatus_to_werror(status);
488 if (!W_ERROR_IS_OK(result)) {
489 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
490 wvalue.name, win_errstr(result)));
493 return result;
496 static WERROR winreg_printer_date_to_NTTIME(const char *str, NTTIME *data)
498 struct tm tm;
499 time_t t;
501 if (strequal(str, "01/01/1601")) {
502 *data = 0;
503 return WERR_OK;
506 ZERO_STRUCT(tm);
508 if (sscanf(str, "%d/%d/%d",
509 &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
510 return WERR_INVALID_PARAMETER;
512 tm.tm_mon -= 1;
513 tm.tm_year -= 1900;
514 tm.tm_isdst = -1;
516 t = mktime(&tm);
517 unix_to_nt_time(data, t);
519 return WERR_OK;
522 static WERROR winreg_printer_write_ver(TALLOC_CTX *mem_ctx,
523 struct dcerpc_binding_handle *winreg_handle,
524 struct policy_handle *key_handle,
525 const char *value,
526 uint64_t data)
528 struct winreg_String wvalue = { 0, };
529 DATA_BLOB blob;
530 WERROR result = WERR_OK;
531 NTSTATUS status;
532 char *str;
534 /* FIXME: check format is right,
535 * this needs to be something like: 6.1.7600.16385 */
536 str = talloc_asprintf(mem_ctx, "%u.%u.%u.%u",
537 (unsigned)((data >> 48) & 0xFFFF),
538 (unsigned)((data >> 32) & 0xFFFF),
539 (unsigned)((data >> 16) & 0xFFFF),
540 (unsigned)(data & 0xFFFF));
541 if (!str) {
542 return WERR_NOMEM;
545 wvalue.name = value;
546 if (!push_reg_sz(mem_ctx, &blob, str)) {
547 return WERR_NOMEM;
549 status = dcerpc_winreg_SetValue(winreg_handle,
550 mem_ctx,
551 key_handle,
552 wvalue,
553 REG_SZ,
554 blob.data,
555 blob.length,
556 &result);
557 if (!NT_STATUS_IS_OK(status)) {
558 result = ntstatus_to_werror(status);
560 if (!W_ERROR_IS_OK(result)) {
561 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
562 wvalue.name, win_errstr(result)));
565 return result;
568 static WERROR winreg_printer_ver_to_dword(const char *str, uint64_t *data)
570 unsigned int v1, v2, v3, v4;
572 if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
573 return WERR_INVALID_PARAMETER;
576 *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
577 ((uint64_t)(v2 & 0xFFFF) << 32) +
578 ((uint64_t)(v3 & 0xFFFF) << 16) +
579 (uint64_t)(v2 & 0xFFFF);
581 return WERR_OK;
584 /********************************************************************
585 Public winreg function for spoolss
586 ********************************************************************/
588 WERROR winreg_create_printer(TALLOC_CTX *mem_ctx,
589 struct dcerpc_binding_handle *winreg_handle,
590 const char *sharename)
592 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
593 struct policy_handle hive_hnd, key_hnd;
594 struct spoolss_SetPrinterInfo2 *info2;
595 struct security_descriptor *secdesc;
596 struct winreg_String wkey, wkeyclass;
597 const char *path;
598 const char *subkeys[] = { SPOOL_DSDRIVER_KEY, SPOOL_DSSPOOLER_KEY, SPOOL_PRINTERDATA_KEY };
599 uint32_t i, count = ARRAY_SIZE(subkeys);
600 uint32_t info2_mask = 0;
601 WERROR result = WERR_OK;
602 WERROR ignore;
603 TALLOC_CTX *tmp_ctx;
605 tmp_ctx = talloc_stackframe();
606 if (tmp_ctx == NULL) {
607 return WERR_NOMEM;
610 path = winreg_printer_data_keyname(tmp_ctx, sharename);
611 if (path == NULL) {
612 TALLOC_FREE(tmp_ctx);
613 return WERR_NOMEM;
616 ZERO_STRUCT(hive_hnd);
617 ZERO_STRUCT(key_hnd);
619 result = winreg_printer_openkey(tmp_ctx,
620 winreg_handle,
621 path,
623 false,
624 access_mask,
625 &hive_hnd,
626 &key_hnd);
627 if (W_ERROR_IS_OK(result)) {
628 DEBUG(2, ("winreg_create_printer: Skipping, %s already exists\n", path));
629 goto done;
630 } else if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
631 DEBUG(2, ("winreg_create_printer: Creating default values in %s\n", path));
632 } else if (!W_ERROR_IS_OK(result)) {
633 DEBUG(0, ("winreg_create_printer: Could not open key %s: %s\n",
634 path, win_errstr(result)));
635 goto done;
638 if (is_valid_policy_hnd(&key_hnd)) {
639 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
641 if (is_valid_policy_hnd(&hive_hnd)) {
642 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
645 /* Create the main key */
646 result = winreg_printer_openkey(tmp_ctx,
647 winreg_handle,
648 path,
650 true,
651 access_mask,
652 &hive_hnd,
653 &key_hnd);
654 if (!W_ERROR_IS_OK(result)) {
655 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
656 path, win_errstr(result)));
657 goto done;
660 if (is_valid_policy_hnd(&key_hnd)) {
661 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
664 /* Create subkeys */
665 for (i = 0; i < count; i++) {
666 NTSTATUS status;
667 enum winreg_CreateAction action = REG_ACTION_NONE;
669 ZERO_STRUCT(key_hnd);
670 ZERO_STRUCT(wkey);
672 wkey.name = talloc_asprintf(tmp_ctx, "%s\\%s", path, subkeys[i]);
673 if (wkey.name == NULL) {
674 result = WERR_NOMEM;
675 goto done;
678 ZERO_STRUCT(wkeyclass);
679 wkeyclass.name = "";
681 status = dcerpc_winreg_CreateKey(winreg_handle,
682 tmp_ctx,
683 &hive_hnd,
684 wkey,
685 wkeyclass,
687 access_mask,
688 NULL,
689 &key_hnd,
690 &action,
691 &result);
692 if (!NT_STATUS_IS_OK(status)) {
693 result = ntstatus_to_werror(status);
695 if (!W_ERROR_IS_OK(result)) {
696 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
697 wkey.name, win_errstr(result)));
698 goto done;
701 if (strequal(subkeys[i], SPOOL_DSSPOOLER_KEY)) {
702 const char *dnssuffix;
703 const char *longname;
704 const char *uncname;
706 status = dcerpc_winreg_set_sz(tmp_ctx,
707 winreg_handle,
708 &key_hnd,
709 SPOOL_REG_PRINTERNAME,
710 sharename,
711 &result);
712 if (!NT_STATUS_IS_OK(status)) {
713 result = ntstatus_to_werror(status);
715 if (!W_ERROR_IS_OK(result)) {
716 goto done;
719 status = dcerpc_winreg_set_sz(tmp_ctx,
720 winreg_handle,
721 &key_hnd,
722 SPOOL_REG_SHORTSERVERNAME,
723 lp_netbios_name(),
724 &result);
725 if (!NT_STATUS_IS_OK(status)) {
726 result = ntstatus_to_werror(status);
728 if (!W_ERROR_IS_OK(result)) {
729 goto done;
732 /* We make the assumption that the netbios name
733 * is the same as the DNS name since the former
734 * will be what we used to join the domain
736 dnssuffix = get_mydnsdomname(tmp_ctx);
737 if (dnssuffix != NULL && dnssuffix[0] != '\0') {
738 longname = talloc_asprintf(tmp_ctx, "%s.%s", lp_netbios_name(), dnssuffix);
739 } else {
740 longname = talloc_strdup(tmp_ctx, lp_netbios_name());
742 if (longname == NULL) {
743 result = WERR_NOMEM;
744 goto done;
747 status = dcerpc_winreg_set_sz(tmp_ctx,
748 winreg_handle,
749 &key_hnd,
750 SPOOL_REG_SERVERNAME,
751 longname,
752 &result);
753 if (!NT_STATUS_IS_OK(status)) {
754 result = ntstatus_to_werror(status);
756 if (!W_ERROR_IS_OK(result)) {
757 goto done;
760 uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
761 longname, sharename);
762 if (uncname == NULL) {
763 result = WERR_NOMEM;
764 goto done;
767 status = dcerpc_winreg_set_sz(tmp_ctx,
768 winreg_handle,
769 &key_hnd,
770 SPOOL_REG_UNCNAME,
771 uncname,
772 &result);
773 if (!NT_STATUS_IS_OK(status)) {
774 result = ntstatus_to_werror(status);
776 if (!W_ERROR_IS_OK(result)) {
777 goto done;
780 status = dcerpc_winreg_set_dword(tmp_ctx,
781 winreg_handle,
782 &key_hnd,
783 SPOOL_REG_VERSIONNUMBER,
785 &result);
786 if (!NT_STATUS_IS_OK(status)) {
787 result = ntstatus_to_werror(status);
789 if (!W_ERROR_IS_OK(result)) {
790 goto done;
793 status = dcerpc_winreg_set_dword(tmp_ctx,
794 winreg_handle,
795 &key_hnd,
796 SPOOL_REG_PRINTSTARTTIME,
798 &result);
799 if (!NT_STATUS_IS_OK(status)) {
800 result = ntstatus_to_werror(status);
802 if (!W_ERROR_IS_OK(result)) {
803 goto done;
806 status = dcerpc_winreg_set_dword(tmp_ctx,
807 winreg_handle,
808 &key_hnd,
809 SPOOL_REG_PRINTENDTIME,
811 &result);
812 if (!NT_STATUS_IS_OK(status)) {
813 result = ntstatus_to_werror(status);
815 if (!W_ERROR_IS_OK(result)) {
816 goto done;
819 status = dcerpc_winreg_set_dword(tmp_ctx,
820 winreg_handle,
821 &key_hnd,
822 SPOOL_REG_PRIORITY,
824 &result);
825 if (!NT_STATUS_IS_OK(status)) {
826 result = ntstatus_to_werror(status);
828 if (!W_ERROR_IS_OK(result)) {
829 goto done;
832 status = dcerpc_winreg_set_dword(tmp_ctx,
833 winreg_handle,
834 &key_hnd,
835 SPOOL_REG_PRINTKEEPPRINTEDJOBS,
837 &result);
838 if (!NT_STATUS_IS_OK(status)) {
839 result = ntstatus_to_werror(status);
841 if (!W_ERROR_IS_OK(result)) {
842 goto done;
846 if (is_valid_policy_hnd(&key_hnd)) {
847 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
850 info2 = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfo2);
851 if (info2 == NULL) {
852 result = WERR_NOMEM;
853 goto done;
856 info2->printername = sharename;
857 if (info2->printername == NULL) {
858 result = WERR_NOMEM;
859 goto done;
861 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTERNAME;
863 info2->sharename = sharename;
864 info2_mask |= SPOOLSS_PRINTER_INFO_SHARENAME;
866 info2->portname = SAMBA_PRINTER_PORT_NAME;
867 info2_mask |= SPOOLSS_PRINTER_INFO_PORTNAME;
869 info2->printprocessor = "winprint";
870 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTPROCESSOR;
872 info2->datatype = "RAW";
873 info2_mask |= SPOOLSS_PRINTER_INFO_DATATYPE;
875 info2->comment = "";
876 info2_mask |= SPOOLSS_PRINTER_INFO_COMMENT;
878 info2->attributes = PRINTER_ATTRIBUTE_SAMBA;
879 info2_mask |= SPOOLSS_PRINTER_INFO_ATTRIBUTES;
881 info2->starttime = 0; /* Minutes since 12:00am GMT */
882 info2_mask |= SPOOLSS_PRINTER_INFO_STARTTIME;
884 info2->untiltime = 0; /* Minutes since 12:00am GMT */
885 info2_mask |= SPOOLSS_PRINTER_INFO_UNTILTIME;
887 info2->priority = 1;
888 info2_mask |= SPOOLSS_PRINTER_INFO_PRIORITY;
890 info2->defaultpriority = 1;
891 info2_mask |= SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY;
893 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
894 if (!W_ERROR_IS_OK(result)) {
895 goto done;
897 info2_mask |= SPOOLSS_PRINTER_INFO_SECDESC;
900 * Don't write a default Device Mode to the registry! The Device Mode is
901 * only written to disk with a SetPrinter level 2 or 8.
904 result = winreg_update_printer(tmp_ctx,
905 winreg_handle,
906 sharename,
907 info2_mask,
908 info2,
909 NULL,
910 secdesc);
912 done:
913 if (is_valid_policy_hnd(&key_hnd)) {
914 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
916 if (is_valid_policy_hnd(&hive_hnd)) {
917 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
920 talloc_free(tmp_ctx);
921 return result;
924 WERROR winreg_update_printer(TALLOC_CTX *mem_ctx,
925 struct dcerpc_binding_handle *winreg_handle,
926 const char *sharename,
927 uint32_t info2_mask,
928 struct spoolss_SetPrinterInfo2 *info2,
929 struct spoolss_DeviceMode *devmode,
930 struct security_descriptor *secdesc)
932 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
933 struct policy_handle hive_hnd, key_hnd;
934 int snum = lp_servicenumber(sharename);
935 enum ndr_err_code ndr_err;
936 DATA_BLOB blob;
937 char *path;
938 WERROR result = WERR_OK;
939 NTSTATUS status;
940 TALLOC_CTX *tmp_ctx;
942 tmp_ctx = talloc_stackframe();
943 if (tmp_ctx == NULL) {
944 return WERR_NOMEM;
947 path = winreg_printer_data_keyname(tmp_ctx, sharename);
948 if (path == NULL) {
949 TALLOC_FREE(tmp_ctx);
950 return WERR_NOMEM;
953 ZERO_STRUCT(hive_hnd);
954 ZERO_STRUCT(key_hnd);
956 result = winreg_printer_openkey(tmp_ctx,
957 winreg_handle,
958 path,
960 true,
961 access_mask,
962 &hive_hnd,
963 &key_hnd);
964 if (!W_ERROR_IS_OK(result)) {
965 DEBUG(0, ("winreg_update_printer: Could not open key %s: %s\n",
966 path, win_errstr(result)));
967 goto done;
970 if (info2_mask & SPOOLSS_PRINTER_INFO_ATTRIBUTES) {
971 status = dcerpc_winreg_set_dword(tmp_ctx,
972 winreg_handle,
973 &key_hnd,
974 "Attributes",
975 info2->attributes,
976 &result);
977 if (!NT_STATUS_IS_OK(status)) {
978 result = ntstatus_to_werror(status);
980 if (!W_ERROR_IS_OK(result)) {
981 goto done;
985 #if 0
986 if (info2_mask & SPOOLSS_PRINTER_INFO_AVERAGEPPM) {
987 status = dcerpc_winreg_set_dword(tmp_ctx,
988 winreg_handle,
989 &key_hnd,
990 "AveragePpm",
991 info2->attributes,
992 &result);
993 if (!NT_STATUS_IS_OK(status)) {
994 result = ntstatus_to_werror(status);
996 if (!W_ERROR_IS_OK(result)) {
997 goto done;
1000 #endif
1002 if (info2_mask & SPOOLSS_PRINTER_INFO_COMMENT) {
1003 status = dcerpc_winreg_set_sz(tmp_ctx,
1004 winreg_handle,
1005 &key_hnd,
1006 "Description",
1007 info2->comment,
1008 &result);
1009 if (!NT_STATUS_IS_OK(status)) {
1010 result = ntstatus_to_werror(status);
1012 if (!W_ERROR_IS_OK(result)) {
1013 goto done;
1017 if (info2_mask & SPOOLSS_PRINTER_INFO_DATATYPE) {
1018 status = dcerpc_winreg_set_sz(tmp_ctx,
1019 winreg_handle,
1020 &key_hnd,
1021 "Datatype",
1022 info2->datatype,
1023 &result);
1024 if (!NT_STATUS_IS_OK(status)) {
1025 result = ntstatus_to_werror(status);
1027 if (!W_ERROR_IS_OK(result)) {
1028 goto done;
1032 if (info2_mask & SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY) {
1033 status = dcerpc_winreg_set_dword(tmp_ctx,
1034 winreg_handle,
1035 &key_hnd,
1036 "Default Priority",
1037 info2->defaultpriority,
1038 &result);
1039 if (!NT_STATUS_IS_OK(status)) {
1040 result = ntstatus_to_werror(status);
1042 if (!W_ERROR_IS_OK(result)) {
1043 goto done;
1047 if (info2_mask & SPOOLSS_PRINTER_INFO_DEVMODE) {
1049 * Some client drivers freak out if there is a NULL devmode
1050 * (probably the driver is not checking before accessing
1051 * the devmode pointer) --jerry
1053 if (devmode == NULL && lp_default_devmode(snum) && info2 != NULL) {
1054 result = spoolss_create_default_devmode(tmp_ctx,
1055 info2->printername,
1056 &devmode);
1057 if (!W_ERROR_IS_OK(result)) {
1058 goto done;
1062 if (devmode->size != (ndr_size_spoolss_DeviceMode(devmode, 0) - devmode->__driverextra_length)) {
1063 result = WERR_INVALID_PARAM;
1064 goto done;
1067 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, devmode,
1068 (ndr_push_flags_fn_t) ndr_push_spoolss_DeviceMode);
1069 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1070 DEBUG(0, ("winreg_update_printer: Failed to marshall device mode\n"));
1071 result = WERR_NOMEM;
1072 goto done;
1075 status = dcerpc_winreg_set_binary(tmp_ctx,
1076 winreg_handle,
1077 &key_hnd,
1078 "Default DevMode",
1079 &blob,
1080 &result);
1081 if (!NT_STATUS_IS_OK(status)) {
1082 result = ntstatus_to_werror(status);
1084 if (!W_ERROR_IS_OK(result)) {
1085 goto done;
1089 if (info2_mask & SPOOLSS_PRINTER_INFO_DRIVERNAME) {
1090 status = dcerpc_winreg_set_sz(tmp_ctx,
1091 winreg_handle,
1092 &key_hnd,
1093 "Printer Driver",
1094 info2->drivername,
1095 &result);
1096 if (!NT_STATUS_IS_OK(status)) {
1097 result = ntstatus_to_werror(status);
1099 if (!W_ERROR_IS_OK(result)) {
1100 goto done;
1104 if (info2_mask & SPOOLSS_PRINTER_INFO_LOCATION) {
1105 status = dcerpc_winreg_set_sz(tmp_ctx,
1106 winreg_handle,
1107 &key_hnd,
1108 "Location",
1109 info2->location,
1110 &result);
1111 if (!NT_STATUS_IS_OK(status)) {
1112 result = ntstatus_to_werror(status);
1114 if (!W_ERROR_IS_OK(result)) {
1115 goto done;
1119 if (info2_mask & SPOOLSS_PRINTER_INFO_PARAMETERS) {
1120 status = dcerpc_winreg_set_sz(tmp_ctx,
1121 winreg_handle,
1122 &key_hnd,
1123 "Parameters",
1124 info2->parameters,
1125 &result);
1126 if (!NT_STATUS_IS_OK(status)) {
1127 result = ntstatus_to_werror(status);
1129 if (!W_ERROR_IS_OK(result)) {
1130 goto done;
1134 if (info2_mask & SPOOLSS_PRINTER_INFO_PORTNAME) {
1135 status = dcerpc_winreg_set_sz(tmp_ctx,
1136 winreg_handle,
1137 &key_hnd,
1138 "Port",
1139 info2->portname,
1140 &result);
1141 if (!NT_STATUS_IS_OK(status)) {
1142 result = ntstatus_to_werror(status);
1144 if (!W_ERROR_IS_OK(result)) {
1145 goto done;
1149 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTERNAME) {
1151 * in addprinter: no servername and the printer is the name
1152 * in setprinter: servername is \\server
1153 * and printer is \\server\\printer
1155 * Samba manages only local printers.
1156 * we currently don't support things like i
1157 * path=\\other_server\printer
1159 * We only store the printername, not \\server\printername
1161 const char *p = strrchr(info2->printername, '\\');
1162 if (p == NULL) {
1163 p = info2->printername;
1164 } else {
1165 p++;
1167 status = dcerpc_winreg_set_sz(tmp_ctx,
1168 winreg_handle,
1169 &key_hnd,
1170 "Name",
1172 &result);
1173 if (!NT_STATUS_IS_OK(status)) {
1174 result = ntstatus_to_werror(status);
1176 if (!W_ERROR_IS_OK(result)) {
1177 goto done;
1181 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTPROCESSOR) {
1182 status = dcerpc_winreg_set_sz(tmp_ctx,
1183 winreg_handle,
1184 &key_hnd,
1185 "Print Processor",
1186 info2->printprocessor,
1187 &result);
1188 if (!NT_STATUS_IS_OK(status)) {
1189 result = ntstatus_to_werror(status);
1191 if (!W_ERROR_IS_OK(result)) {
1192 goto done;
1196 if (info2_mask & SPOOLSS_PRINTER_INFO_PRIORITY) {
1197 status = dcerpc_winreg_set_dword(tmp_ctx,
1198 winreg_handle,
1199 &key_hnd,
1200 "Priority",
1201 info2->priority,
1202 &result);
1203 if (!NT_STATUS_IS_OK(status)) {
1204 result = ntstatus_to_werror(status);
1206 if (!W_ERROR_IS_OK(result)) {
1207 goto done;
1211 if (info2_mask & SPOOLSS_PRINTER_INFO_SECDESC) {
1213 * We need a security descriptor, if it isn't specified by
1214 * AddPrinter{Ex} then create a default descriptor.
1216 if (secdesc == NULL) {
1217 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1218 if (!W_ERROR_IS_OK(result)) {
1219 goto done;
1222 result = winreg_set_printer_secdesc(tmp_ctx,
1223 winreg_handle,
1224 sharename,
1225 secdesc);
1226 if (!W_ERROR_IS_OK(result)) {
1227 goto done;
1231 if (info2_mask & SPOOLSS_PRINTER_INFO_SEPFILE) {
1232 status = dcerpc_winreg_set_sz(tmp_ctx,
1233 winreg_handle,
1234 &key_hnd,
1235 "Separator File",
1236 info2->sepfile,
1237 &result);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 result = ntstatus_to_werror(status);
1241 if (!W_ERROR_IS_OK(result)) {
1242 goto done;
1246 if (info2_mask & SPOOLSS_PRINTER_INFO_SHARENAME) {
1247 status = dcerpc_winreg_set_sz(tmp_ctx,
1248 winreg_handle,
1249 &key_hnd,
1250 "Share Name",
1251 info2->sharename,
1252 &result);
1253 if (!NT_STATUS_IS_OK(status)) {
1254 result = ntstatus_to_werror(status);
1256 if (!W_ERROR_IS_OK(result)) {
1257 goto done;
1261 if (info2_mask & SPOOLSS_PRINTER_INFO_STARTTIME) {
1262 status = dcerpc_winreg_set_dword(tmp_ctx,
1263 winreg_handle,
1264 &key_hnd,
1265 "StartTime",
1266 info2->starttime,
1267 &result);
1268 if (!NT_STATUS_IS_OK(status)) {
1269 result = ntstatus_to_werror(status);
1271 if (!W_ERROR_IS_OK(result)) {
1272 goto done;
1276 if (info2_mask & SPOOLSS_PRINTER_INFO_STATUS) {
1277 status = dcerpc_winreg_set_dword(tmp_ctx,
1278 winreg_handle,
1279 &key_hnd,
1280 "Status",
1281 info2->status,
1282 &result);
1283 if (!NT_STATUS_IS_OK(status)) {
1284 result = ntstatus_to_werror(status);
1286 if (!W_ERROR_IS_OK(result)) {
1287 goto done;
1291 if (info2_mask & SPOOLSS_PRINTER_INFO_UNTILTIME) {
1292 status = dcerpc_winreg_set_dword(tmp_ctx,
1293 winreg_handle,
1294 &key_hnd,
1295 "UntilTime",
1296 info2->untiltime,
1297 &result);
1298 if (!NT_STATUS_IS_OK(status)) {
1299 result = ntstatus_to_werror(status);
1301 if (!W_ERROR_IS_OK(result)) {
1302 goto done;
1306 status = dcerpc_winreg_set_dword(tmp_ctx,
1307 winreg_handle,
1308 &key_hnd,
1309 "ChangeID",
1310 winreg_printer_rev_changeid(),
1311 &result);
1312 if (!NT_STATUS_IS_OK(status)) {
1313 result = ntstatus_to_werror(status);
1315 if (!W_ERROR_IS_OK(result)) {
1316 goto done;
1319 result = WERR_OK;
1320 done:
1321 if (winreg_handle != NULL) {
1322 WERROR ignore;
1324 if (is_valid_policy_hnd(&key_hnd)) {
1325 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1327 if (is_valid_policy_hnd(&hive_hnd)) {
1328 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1332 TALLOC_FREE(tmp_ctx);
1333 return result;
1336 WERROR winreg_get_printer(TALLOC_CTX *mem_ctx,
1337 struct dcerpc_binding_handle *winreg_handle,
1338 const char *printer,
1339 struct spoolss_PrinterInfo2 **pinfo2)
1341 struct spoolss_PrinterInfo2 *info2;
1342 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1343 struct policy_handle hive_hnd, key_hnd;
1344 struct spoolss_PrinterEnumValues enum_value;
1345 struct spoolss_PrinterEnumValues *v = NULL;
1346 enum ndr_err_code ndr_err;
1347 DATA_BLOB blob;
1348 int snum = lp_servicenumber(printer);
1349 uint32_t num_values = 0;
1350 uint32_t i;
1351 char *path;
1352 NTSTATUS status;
1353 WERROR result = WERR_OK;
1354 const char **enum_names = NULL;
1355 enum winreg_Type *enum_types = NULL;
1356 DATA_BLOB *enum_data_blobs = NULL;
1357 TALLOC_CTX *tmp_ctx;
1359 tmp_ctx = talloc_stackframe();
1360 if (tmp_ctx == NULL) {
1361 return WERR_NOMEM;
1364 path = winreg_printer_data_keyname(tmp_ctx, printer);
1365 if (path == NULL) {
1366 TALLOC_FREE(tmp_ctx);
1367 return WERR_NOMEM;
1370 result = winreg_printer_openkey(tmp_ctx,
1371 winreg_handle,
1372 path,
1374 false,
1375 access_mask,
1376 &hive_hnd,
1377 &key_hnd);
1378 if (!W_ERROR_IS_OK(result)) {
1379 DEBUG(2, ("winreg_get_printer: Could not open key %s: %s\n",
1380 path, win_errstr(result)));
1381 goto done;
1384 status = dcerpc_winreg_enumvals(tmp_ctx,
1385 winreg_handle,
1386 &key_hnd,
1387 &num_values,
1388 &enum_names,
1389 &enum_types,
1390 &enum_data_blobs,
1391 &result);
1392 if (!NT_STATUS_IS_OK(status)){
1393 result = ntstatus_to_werror(status);
1396 if (!W_ERROR_IS_OK(result)) {
1397 DEBUG(0, ("winreg_get_printer: Could not enumerate values in %s: %s\n",
1398 path, win_errstr(result)));
1399 goto done;
1402 info2 = talloc_zero(tmp_ctx, struct spoolss_PrinterInfo2);
1403 if (info2 == NULL) {
1404 result = WERR_NOMEM;
1405 goto done;
1408 FILL_STRING(info2, EMPTY_STRING, info2->servername);
1409 FILL_STRING(info2, EMPTY_STRING, info2->printername);
1410 FILL_STRING(info2, EMPTY_STRING, info2->sharename);
1411 FILL_STRING(info2, EMPTY_STRING, info2->portname);
1412 FILL_STRING(info2, EMPTY_STRING, info2->drivername);
1413 FILL_STRING(info2, EMPTY_STRING, info2->comment);
1414 FILL_STRING(info2, EMPTY_STRING, info2->location);
1415 FILL_STRING(info2, EMPTY_STRING, info2->sepfile);
1416 FILL_STRING(info2, EMPTY_STRING, info2->printprocessor);
1417 FILL_STRING(info2, EMPTY_STRING, info2->datatype);
1418 FILL_STRING(info2, EMPTY_STRING, info2->parameters);
1420 for (i = 0; i < num_values; i++) {
1421 enum_value.value_name = enum_names[i];
1422 enum_value.value_name_len = 2*strlen_m_term(enum_names[i]);
1423 enum_value.type = enum_types[i];
1424 enum_value.data_length = enum_data_blobs[i].length;
1425 enum_value.data = NULL;
1426 if (enum_value.data_length != 0){
1427 enum_value.data = &enum_data_blobs[i];
1429 v = &enum_value;
1431 result = winreg_enumval_to_sz(info2,
1433 "Name",
1434 &info2->printername);
1435 CHECK_ERROR(result);
1437 result = winreg_enumval_to_sz(info2,
1439 "Share Name",
1440 &info2->sharename);
1441 CHECK_ERROR(result);
1443 result = winreg_enumval_to_sz(info2,
1445 "Port",
1446 &info2->portname);
1447 CHECK_ERROR(result);
1449 result = winreg_enumval_to_sz(info2,
1451 "Description",
1452 &info2->comment);
1453 CHECK_ERROR(result);
1455 result = winreg_enumval_to_sz(info2,
1457 "Location",
1458 &info2->location);
1459 CHECK_ERROR(result);
1461 result = winreg_enumval_to_sz(info2,
1463 "Separator File",
1464 &info2->sepfile);
1465 CHECK_ERROR(result);
1467 result = winreg_enumval_to_sz(info2,
1469 "Print Processor",
1470 &info2->printprocessor);
1471 CHECK_ERROR(result);
1473 result = winreg_enumval_to_sz(info2,
1475 "Datatype",
1476 &info2->datatype);
1477 CHECK_ERROR(result);
1479 result = winreg_enumval_to_sz(info2,
1481 "Parameters",
1482 &info2->parameters);
1483 CHECK_ERROR(result);
1485 result = winreg_enumval_to_sz(info2,
1487 "Printer Driver",
1488 &info2->drivername);
1489 CHECK_ERROR(result);
1491 result = winreg_enumval_to_dword(info2,
1493 "Attributes",
1494 &info2->attributes);
1495 CHECK_ERROR(result);
1497 result = winreg_enumval_to_dword(info2,
1499 "Priority",
1500 &info2->priority);
1501 CHECK_ERROR(result);
1503 result = winreg_enumval_to_dword(info2,
1505 "Default Priority",
1506 &info2->defaultpriority);
1507 CHECK_ERROR(result);
1509 result = winreg_enumval_to_dword(info2,
1511 "StartTime",
1512 &info2->starttime);
1513 CHECK_ERROR(result);
1515 result = winreg_enumval_to_dword(info2,
1517 "UntilTime",
1518 &info2->untiltime);
1519 CHECK_ERROR(result);
1521 result = winreg_enumval_to_dword(info2,
1523 "Status",
1524 &info2->status);
1525 CHECK_ERROR(result);
1527 result = winreg_enumval_to_dword(info2,
1529 "StartTime",
1530 &info2->starttime);
1531 CHECK_ERROR(result);
1534 if (!W_ERROR_IS_OK(result)) {
1535 DEBUG(0, ("winreg_get_printer: winreg_enumval_to_TYPE() failed "
1536 "for %s: %s\n",
1537 v->value_name,
1538 win_errstr(result)));
1539 goto done;
1542 /* Construct the Device Mode */
1543 status = dcerpc_winreg_query_binary(tmp_ctx,
1544 winreg_handle,
1545 &key_hnd,
1546 "Default DevMode",
1547 &blob,
1548 &result);
1549 if (!NT_STATUS_IS_OK(status)) {
1550 result = ntstatus_to_werror(status);
1552 if (W_ERROR_IS_OK(result)) {
1553 info2->devmode = talloc_zero(info2, struct spoolss_DeviceMode);
1554 if (info2->devmode == NULL) {
1555 result = WERR_NOMEM;
1556 goto done;
1558 ndr_err = ndr_pull_struct_blob(&blob,
1559 info2->devmode,
1560 info2->devmode,
1561 (ndr_pull_flags_fn_t) ndr_pull_spoolss_DeviceMode);
1562 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1563 DEBUG(0, ("winreg_get_printer: Failed to unmarshall device mode\n"));
1564 result = WERR_NOMEM;
1565 goto done;
1569 if (info2->devmode == NULL && lp_default_devmode(snum)) {
1570 result = spoolss_create_default_devmode(info2,
1571 info2->printername,
1572 &info2->devmode);
1573 if (!W_ERROR_IS_OK(result)) {
1574 goto done;
1578 if (info2->devmode) {
1579 info2->devmode->size = ndr_size_spoolss_DeviceMode(info2->devmode, 0) - info2->devmode->driverextra_data.length;
1582 result = winreg_get_printer_secdesc(info2,
1583 winreg_handle,
1584 printer,
1585 &info2->secdesc);
1586 if (!W_ERROR_IS_OK(result)) {
1587 goto done;
1590 /* Fix for OS/2 drivers. */
1591 if (get_remote_arch() == RA_OS2) {
1592 spoolss_map_to_os2_driver(info2, &info2->drivername);
1595 if (pinfo2) {
1596 *pinfo2 = talloc_move(mem_ctx, &info2);
1599 result = WERR_OK;
1600 done:
1601 if (winreg_handle != NULL) {
1602 WERROR ignore;
1604 if (is_valid_policy_hnd(&key_hnd)) {
1605 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1607 if (is_valid_policy_hnd(&hive_hnd)) {
1608 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1612 TALLOC_FREE(tmp_ctx);
1613 return result;
1616 WERROR winreg_get_printer_secdesc(TALLOC_CTX *mem_ctx,
1617 struct dcerpc_binding_handle *winreg_handle,
1618 const char *sharename,
1619 struct spoolss_security_descriptor **psecdesc)
1621 struct spoolss_security_descriptor *secdesc;
1622 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1623 struct policy_handle hive_hnd, key_hnd;
1624 const char *path;
1625 TALLOC_CTX *tmp_ctx;
1626 NTSTATUS status;
1627 WERROR result;
1629 tmp_ctx = talloc_stackframe();
1630 if (tmp_ctx == NULL) {
1631 return WERR_NOMEM;
1634 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1635 if (path == NULL) {
1636 talloc_free(tmp_ctx);
1637 return WERR_NOMEM;
1640 ZERO_STRUCT(hive_hnd);
1641 ZERO_STRUCT(key_hnd);
1643 result = winreg_printer_openkey(tmp_ctx,
1644 winreg_handle,
1645 path,
1647 false,
1648 access_mask,
1649 &hive_hnd,
1650 &key_hnd);
1651 if (!W_ERROR_IS_OK(result)) {
1652 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1653 goto create_default;
1655 goto done;
1658 status = dcerpc_winreg_query_sd(tmp_ctx,
1659 winreg_handle,
1660 &key_hnd,
1661 "Security",
1662 &secdesc,
1663 &result);
1664 if (!NT_STATUS_IS_OK(status)) {
1665 result = ntstatus_to_werror(status);
1667 if (!W_ERROR_IS_OK(result)) {
1668 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1669 WERROR ignore;
1671 if (is_valid_policy_hnd(&key_hnd)) {
1672 dcerpc_winreg_CloseKey(winreg_handle,
1673 tmp_ctx,
1674 &key_hnd,
1675 &ignore);
1678 if (is_valid_policy_hnd(&hive_hnd)) {
1679 dcerpc_winreg_CloseKey(winreg_handle,
1680 tmp_ctx,
1681 &hive_hnd,
1682 &ignore);
1684 goto create_default;
1686 goto done;
1689 if (psecdesc) {
1690 *psecdesc = talloc_move(mem_ctx, &secdesc);
1693 result = WERR_OK;
1694 goto done;
1696 create_default:
1697 result = winreg_printer_openkey(tmp_ctx,
1698 winreg_handle,
1699 path,
1701 true,
1702 access_mask,
1703 &hive_hnd,
1704 &key_hnd);
1705 if (!W_ERROR_IS_OK(result)) {
1706 goto done;
1709 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1710 if (!W_ERROR_IS_OK(result)) {
1711 goto done;
1714 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
1715 this security descriptor has been created when winbindd was
1716 down. Take ownership of security descriptor. */
1717 if (dom_sid_equal(secdesc->owner_sid, &global_sid_World)) {
1718 struct dom_sid owner_sid;
1720 /* Change sd owner to workgroup administrator */
1722 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
1723 struct spoolss_security_descriptor *new_secdesc;
1724 size_t size;
1726 /* Create new sd */
1727 sid_append_rid(&owner_sid, DOMAIN_RID_ADMINISTRATOR);
1729 new_secdesc = make_sec_desc(tmp_ctx,
1730 secdesc->revision,
1731 secdesc->type,
1732 &owner_sid,
1733 secdesc->group_sid,
1734 secdesc->sacl,
1735 secdesc->dacl,
1736 &size);
1738 if (new_secdesc == NULL) {
1739 result = WERR_NOMEM;
1740 goto done;
1743 /* Swap with other one */
1744 secdesc = new_secdesc;
1748 status = dcerpc_winreg_set_sd(tmp_ctx,
1749 winreg_handle,
1750 &key_hnd,
1751 "Security",
1752 secdesc,
1753 &result);
1754 if (!NT_STATUS_IS_OK(status)) {
1755 result = ntstatus_to_werror(status);
1757 if (!W_ERROR_IS_OK(result)) {
1758 return result;
1761 if (psecdesc) {
1762 *psecdesc = talloc_move(mem_ctx, &secdesc);
1765 result = WERR_OK;
1766 done:
1767 if (winreg_handle != NULL) {
1768 WERROR ignore;
1770 if (is_valid_policy_hnd(&key_hnd)) {
1771 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1773 if (is_valid_policy_hnd(&hive_hnd)) {
1774 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1778 talloc_free(tmp_ctx);
1779 return result;
1782 WERROR winreg_set_printer_secdesc(TALLOC_CTX *mem_ctx,
1783 struct dcerpc_binding_handle *winreg_handle,
1784 const char *sharename,
1785 const struct spoolss_security_descriptor *secdesc)
1787 const struct spoolss_security_descriptor *new_secdesc = secdesc;
1788 struct spoolss_security_descriptor *old_secdesc;
1789 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1790 struct policy_handle hive_hnd, key_hnd;
1791 const char *path;
1792 TALLOC_CTX *tmp_ctx;
1793 NTSTATUS status;
1794 WERROR result;
1796 tmp_ctx = talloc_stackframe();
1797 if (tmp_ctx == NULL) {
1798 return WERR_NOMEM;
1801 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1802 if (path == NULL) {
1803 talloc_free(tmp_ctx);
1804 return WERR_NOMEM;
1808 * The old owner and group sids of the security descriptor are not
1809 * present when new ACEs are added or removed by changing printer
1810 * permissions through NT. If they are NULL in the new security
1811 * descriptor then copy them over from the old one.
1813 if (!secdesc->owner_sid || !secdesc->group_sid) {
1814 struct dom_sid *owner_sid, *group_sid;
1815 struct security_acl *dacl, *sacl;
1816 size_t size;
1818 result = winreg_get_printer_secdesc(tmp_ctx,
1819 winreg_handle,
1820 sharename,
1821 &old_secdesc);
1822 if (!W_ERROR_IS_OK(result)) {
1823 talloc_free(tmp_ctx);
1824 return result;
1827 /* Pick out correct owner and group sids */
1828 owner_sid = secdesc->owner_sid ?
1829 secdesc->owner_sid :
1830 old_secdesc->owner_sid;
1832 group_sid = secdesc->group_sid ?
1833 secdesc->group_sid :
1834 old_secdesc->group_sid;
1836 dacl = secdesc->dacl ?
1837 secdesc->dacl :
1838 old_secdesc->dacl;
1840 sacl = secdesc->sacl ?
1841 secdesc->sacl :
1842 old_secdesc->sacl;
1844 /* Make a deep copy of the security descriptor */
1845 new_secdesc = make_sec_desc(tmp_ctx,
1846 secdesc->revision,
1847 secdesc->type,
1848 owner_sid,
1849 group_sid,
1850 sacl,
1851 dacl,
1852 &size);
1853 if (new_secdesc == NULL) {
1854 talloc_free(tmp_ctx);
1855 return WERR_NOMEM;
1859 ZERO_STRUCT(hive_hnd);
1860 ZERO_STRUCT(key_hnd);
1862 result = winreg_printer_openkey(tmp_ctx,
1863 winreg_handle,
1864 path,
1866 false,
1867 access_mask,
1868 &hive_hnd,
1869 &key_hnd);
1870 if (!W_ERROR_IS_OK(result)) {
1871 goto done;
1874 status = dcerpc_winreg_set_sd(tmp_ctx,
1875 winreg_handle,
1876 &key_hnd,
1877 "Security",
1878 new_secdesc,
1879 &result);
1880 if (!NT_STATUS_IS_OK(status)) {
1881 result = ntstatus_to_werror(status);
1884 done:
1885 if (winreg_handle != NULL) {
1886 WERROR ignore;
1888 if (is_valid_policy_hnd(&key_hnd)) {
1889 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1891 if (is_valid_policy_hnd(&hive_hnd)) {
1892 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1896 talloc_free(tmp_ctx);
1897 return result;
1900 /* Set printer data over the winreg pipe. */
1901 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
1902 struct dcerpc_binding_handle *winreg_handle,
1903 const char *printer,
1904 const char *key,
1905 const char *value,
1906 enum winreg_Type type,
1907 uint8_t *data,
1908 uint32_t data_size)
1910 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1911 struct policy_handle hive_hnd, key_hnd;
1912 struct winreg_String wvalue = { 0, };
1913 char *path;
1914 WERROR result = WERR_OK;
1915 NTSTATUS status;
1916 TALLOC_CTX *tmp_ctx;
1918 tmp_ctx = talloc_stackframe();
1919 if (tmp_ctx == NULL) {
1920 return WERR_NOMEM;
1923 path = winreg_printer_data_keyname(tmp_ctx, printer);
1924 if (path == NULL) {
1925 TALLOC_FREE(tmp_ctx);
1926 return WERR_NOMEM;
1929 ZERO_STRUCT(hive_hnd);
1930 ZERO_STRUCT(key_hnd);
1932 DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
1933 key, value, access_mask, printer));
1934 result = winreg_printer_openkey(tmp_ctx,
1935 winreg_handle,
1936 path,
1937 key,
1938 true,
1939 access_mask,
1940 &hive_hnd,
1941 &key_hnd);
1942 if (!W_ERROR_IS_OK(result)) {
1943 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
1944 key, win_errstr(result)));
1945 goto done;
1948 wvalue.name = value;
1949 status = dcerpc_winreg_SetValue(winreg_handle,
1950 tmp_ctx,
1951 &key_hnd,
1952 wvalue,
1953 type,
1954 data,
1955 data_size,
1956 &result);
1957 if (!NT_STATUS_IS_OK(status)) {
1958 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
1959 value, nt_errstr(status)));
1960 result = ntstatus_to_werror(status);
1963 done:
1964 if (winreg_handle != NULL) {
1965 WERROR ignore;
1967 if (is_valid_policy_hnd(&key_hnd)) {
1968 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1970 if (is_valid_policy_hnd(&hive_hnd)) {
1971 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1975 TALLOC_FREE(tmp_ctx);
1976 return result;
1979 /* Get printer data over a winreg pipe. */
1980 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
1981 struct dcerpc_binding_handle *winreg_handle,
1982 const char *printer,
1983 const char *key,
1984 const char *value,
1985 enum winreg_Type *type,
1986 uint8_t **data,
1987 uint32_t *data_size)
1989 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1990 struct policy_handle hive_hnd, key_hnd;
1991 struct winreg_String wvalue;
1992 enum winreg_Type type_in = REG_NONE;
1993 char *path;
1994 uint8_t *data_in = NULL;
1995 uint32_t data_in_size = 0;
1996 uint32_t value_len = 0;
1997 WERROR result = WERR_OK;
1998 WERROR ignore;
1999 NTSTATUS status;
2000 TALLOC_CTX *tmp_ctx;
2002 tmp_ctx = talloc_stackframe();
2003 if (tmp_ctx == NULL) {
2004 return WERR_NOMEM;
2007 path = winreg_printer_data_keyname(tmp_ctx, printer);
2008 if (path == NULL) {
2009 TALLOC_FREE(tmp_ctx);
2010 return WERR_NOMEM;
2013 ZERO_STRUCT(hive_hnd);
2014 ZERO_STRUCT(key_hnd);
2016 result = winreg_printer_openkey(tmp_ctx,
2017 winreg_handle,
2018 path,
2019 key,
2020 false,
2021 access_mask,
2022 &hive_hnd,
2023 &key_hnd);
2024 if (!W_ERROR_IS_OK(result)) {
2025 DEBUG(2, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
2026 key, win_errstr(result)));
2027 goto done;
2030 wvalue.name = value;
2033 * call QueryValue once with data == NULL to get the
2034 * needed memory size to be allocated, then allocate
2035 * data buffer and call again.
2037 status = dcerpc_winreg_QueryValue(winreg_handle,
2038 tmp_ctx,
2039 &key_hnd,
2040 &wvalue,
2041 &type_in,
2042 NULL,
2043 &data_in_size,
2044 &value_len,
2045 &result);
2046 if (!NT_STATUS_IS_OK(status)) {
2047 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2048 value, nt_errstr(status)));
2049 result = ntstatus_to_werror(status);
2050 goto done;
2052 if (!W_ERROR_IS_OK(result)) {
2053 DEBUG(2, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2054 value, win_errstr(result)));
2055 goto done;
2058 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2059 if (data_in == NULL) {
2060 result = WERR_NOMEM;
2061 goto done;
2063 value_len = 0;
2065 status = dcerpc_winreg_QueryValue(winreg_handle,
2066 tmp_ctx,
2067 &key_hnd,
2068 &wvalue,
2069 &type_in,
2070 data_in,
2071 &data_in_size,
2072 &value_len,
2073 &result);
2074 if (!NT_STATUS_IS_OK(status)) {
2075 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2076 value, nt_errstr(status)));
2077 result = ntstatus_to_werror(status);
2078 goto done;
2080 if (!W_ERROR_IS_OK(result)) {
2081 DEBUG(2, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2082 value, win_errstr(result)));
2083 goto done;
2086 *type = type_in;
2087 *data_size = data_in_size;
2088 if (data_in_size) {
2089 *data = talloc_move(mem_ctx, &data_in);
2092 result = WERR_OK;
2093 done:
2094 if (is_valid_policy_hnd(&key_hnd)) {
2095 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2097 if (is_valid_policy_hnd(&hive_hnd)) {
2098 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2101 TALLOC_FREE(tmp_ctx);
2102 return result;
2105 /* Enumerate on the values of a given key and provide the data. */
2106 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
2107 struct dcerpc_binding_handle *winreg_handle,
2108 const char *printer,
2109 const char *key,
2110 uint32_t *pnum_values,
2111 struct spoolss_PrinterEnumValues **penum_values)
2113 uint32_t i;
2114 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2115 struct policy_handle hive_hnd, key_hnd;
2117 struct spoolss_PrinterEnumValues *enum_values = NULL;
2118 uint32_t num_values = 0;
2119 char *path;
2120 WERROR result = WERR_OK;
2121 WERROR ignore;
2122 NTSTATUS status;
2123 const char **enum_names = NULL;
2124 enum winreg_Type *enum_types = NULL;
2125 DATA_BLOB *enum_data_blobs = NULL;
2127 TALLOC_CTX *tmp_ctx;
2129 tmp_ctx = talloc_stackframe();
2130 if (tmp_ctx == NULL) {
2131 return WERR_NOMEM;
2134 path = winreg_printer_data_keyname(tmp_ctx, printer);
2135 if (path == NULL) {
2136 TALLOC_FREE(tmp_ctx);
2137 return WERR_NOMEM;
2140 result = winreg_printer_openkey(tmp_ctx,
2141 winreg_handle,
2142 path,
2143 key,
2144 false,
2145 access_mask,
2146 &hive_hnd,
2147 &key_hnd);
2148 if (!W_ERROR_IS_OK(result)) {
2149 DEBUG(2, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
2150 key, win_errstr(result)));
2151 goto done;
2154 status = dcerpc_winreg_enumvals(tmp_ctx,
2155 winreg_handle,
2156 &key_hnd,
2157 &num_values,
2158 &enum_names,
2159 &enum_types,
2160 &enum_data_blobs,
2161 &result);
2162 if (!NT_STATUS_IS_OK(status)){
2163 result = ntstatus_to_werror(status);
2166 if (!W_ERROR_IS_OK(result)) {
2167 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2168 key, win_errstr(result)));
2169 goto done;
2172 enum_values = talloc_array(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
2173 if (enum_values == NULL){
2174 result = WERR_NOMEM;
2175 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2176 key, win_errstr(result)));
2177 goto done;
2180 for (i = 0; i < num_values; i++){
2181 enum_values[i].value_name = enum_names[i];
2182 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
2183 enum_values[i].type = enum_types[i];
2184 enum_values[i].data_length = enum_data_blobs[i].length;
2185 enum_values[i].data = NULL;
2187 if (enum_values[i].data_length != 0){
2188 enum_values[i].data = &enum_data_blobs[i];
2192 talloc_steal(enum_values, enum_names);
2193 talloc_steal(enum_values, enum_data_blobs);
2195 *pnum_values = num_values;
2196 if (penum_values) {
2197 *penum_values = talloc_move(mem_ctx, &enum_values);
2200 result = WERR_OK;
2201 done:
2202 if (is_valid_policy_hnd(&key_hnd)) {
2203 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2205 if (is_valid_policy_hnd(&hive_hnd)) {
2206 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2209 TALLOC_FREE(tmp_ctx);
2210 return result;
2213 /* Delete printer data over a winreg pipe. */
2214 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
2215 struct dcerpc_binding_handle *winreg_handle,
2216 const char *printer,
2217 const char *key,
2218 const char *value)
2220 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2221 struct policy_handle hive_hnd, key_hnd;
2222 struct winreg_String wvalue = { 0, };
2223 char *path;
2224 WERROR result = WERR_OK;
2225 WERROR ignore;
2226 NTSTATUS status;
2228 TALLOC_CTX *tmp_ctx;
2230 tmp_ctx = talloc_stackframe();
2231 if (tmp_ctx == NULL) {
2232 return WERR_NOMEM;
2235 path = winreg_printer_data_keyname(tmp_ctx, printer);
2236 if (path == NULL) {
2237 TALLOC_FREE(tmp_ctx);
2238 return WERR_NOMEM;
2241 ZERO_STRUCT(hive_hnd);
2242 ZERO_STRUCT(key_hnd);
2244 result = winreg_printer_openkey(tmp_ctx,
2245 winreg_handle,
2246 path,
2247 key,
2248 false,
2249 access_mask,
2250 &hive_hnd,
2251 &key_hnd);
2252 if (!W_ERROR_IS_OK(result)) {
2253 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
2254 key, win_errstr(result)));
2255 goto done;
2258 wvalue.name = value;
2259 status = dcerpc_winreg_DeleteValue(winreg_handle,
2260 tmp_ctx,
2261 &key_hnd,
2262 wvalue,
2263 &result);
2264 if (!NT_STATUS_IS_OK(status)) {
2265 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
2266 value, nt_errstr(status)));
2267 result = ntstatus_to_werror(status);
2270 done:
2271 if (is_valid_policy_hnd(&key_hnd)) {
2272 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2274 if (is_valid_policy_hnd(&hive_hnd)) {
2275 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2278 TALLOC_FREE(tmp_ctx);
2279 return result;
2282 /* Enumerate on the subkeys of a given key and provide the data. */
2283 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
2284 struct dcerpc_binding_handle *winreg_handle,
2285 const char *printer,
2286 const char *key,
2287 uint32_t *pnum_subkeys,
2288 const char ***psubkeys)
2290 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2291 struct policy_handle hive_hnd, key_hnd;
2292 char *path;
2293 const char **subkeys = NULL;
2294 uint32_t num_subkeys = -1;
2296 WERROR result = WERR_OK;
2297 WERROR ignore;
2298 NTSTATUS status;
2300 TALLOC_CTX *tmp_ctx;
2302 tmp_ctx = talloc_stackframe();
2303 if (tmp_ctx == NULL) {
2304 return WERR_NOMEM;
2307 path = winreg_printer_data_keyname(tmp_ctx, printer);
2308 if (path == NULL) {
2309 TALLOC_FREE(tmp_ctx);
2310 return WERR_NOMEM;
2313 ZERO_STRUCT(hive_hnd);
2314 ZERO_STRUCT(key_hnd);
2316 result = winreg_printer_openkey(tmp_ctx,
2317 winreg_handle,
2318 path,
2319 key,
2320 false,
2321 access_mask,
2322 &hive_hnd,
2323 &key_hnd);
2324 if (!W_ERROR_IS_OK(result)) {
2325 DEBUG(2, ("winreg_enum_printer_key: Could not open key %s: %s\n",
2326 key, win_errstr(result)));
2327 goto done;
2330 status = dcerpc_winreg_enum_keys(tmp_ctx,
2331 winreg_handle,
2332 &key_hnd,
2333 &num_subkeys,
2334 &subkeys,
2335 &result);
2336 if (!NT_STATUS_IS_OK(status)) {
2337 result = ntstatus_to_werror(status);
2339 if (!W_ERROR_IS_OK(result)) {
2340 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
2341 key, win_errstr(result)));
2342 goto done;
2345 *pnum_subkeys = num_subkeys;
2346 if (psubkeys) {
2347 *psubkeys = talloc_move(mem_ctx, &subkeys);
2350 result = WERR_OK;
2351 done:
2352 if (is_valid_policy_hnd(&key_hnd)) {
2353 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2355 if (is_valid_policy_hnd(&hive_hnd)) {
2356 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2359 TALLOC_FREE(tmp_ctx);
2360 return result;
2363 /* Delete a key with subkeys of a given printer. */
2364 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
2365 struct dcerpc_binding_handle *winreg_handle,
2366 const char *printer,
2367 const char *key)
2369 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2370 struct policy_handle hive_hnd, key_hnd;
2371 char *keyname;
2372 char *path;
2373 WERROR result;
2374 WERROR ignore;
2375 NTSTATUS status;
2376 TALLOC_CTX *tmp_ctx;
2378 tmp_ctx = talloc_stackframe();
2379 if (tmp_ctx == NULL) {
2380 return WERR_NOMEM;
2383 path = winreg_printer_data_keyname(tmp_ctx, printer);
2384 if (path == NULL) {
2385 TALLOC_FREE(tmp_ctx);
2386 return WERR_NOMEM;
2389 result = winreg_printer_openkey(tmp_ctx,
2390 winreg_handle,
2391 path,
2392 key,
2393 false,
2394 access_mask,
2395 &hive_hnd,
2396 &key_hnd);
2397 if (!W_ERROR_IS_OK(result)) {
2398 /* key doesn't exist */
2399 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2400 result = WERR_OK;
2401 goto done;
2404 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
2405 key, win_errstr(result)));
2406 goto done;
2409 if (is_valid_policy_hnd(&key_hnd)) {
2410 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
2413 if (key == NULL || key[0] == '\0') {
2414 keyname = path;
2415 } else {
2416 keyname = talloc_asprintf(tmp_ctx,
2417 "%s\\%s",
2418 path,
2419 key);
2420 if (keyname == NULL) {
2421 result = WERR_NOMEM;
2422 goto done;
2426 status = dcerpc_winreg_delete_subkeys_recursive(tmp_ctx,
2427 winreg_handle,
2428 &hive_hnd,
2429 access_mask,
2430 keyname,
2431 &result);
2433 if (!NT_STATUS_IS_OK(status)) {
2434 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2435 key, nt_errstr(status)));
2436 result = ntstatus_to_werror(status);
2437 goto done;
2440 if (!W_ERROR_IS_OK(result)) {
2441 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2442 key, win_errstr(result)));
2443 goto done;
2446 done:
2447 if (is_valid_policy_hnd(&key_hnd)) {
2448 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2450 if (is_valid_policy_hnd(&hive_hnd)) {
2451 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2454 TALLOC_FREE(tmp_ctx);
2455 return result;
2458 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
2459 struct dcerpc_binding_handle *winreg_handle,
2460 const char *printer)
2462 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2463 struct policy_handle hive_hnd, key_hnd;
2464 char *path;
2465 NTSTATUS status;
2466 WERROR result;
2467 TALLOC_CTX *tmp_ctx;
2469 tmp_ctx = talloc_stackframe();
2470 if (tmp_ctx == NULL) {
2471 return WERR_NOMEM;
2474 path = winreg_printer_data_keyname(tmp_ctx, printer);
2475 if (path == NULL) {
2476 TALLOC_FREE(tmp_ctx);
2477 return WERR_NOMEM;
2480 ZERO_STRUCT(hive_hnd);
2481 ZERO_STRUCT(key_hnd);
2483 result = winreg_printer_openkey(tmp_ctx,
2484 winreg_handle,
2485 path,
2487 false,
2488 access_mask,
2489 &hive_hnd,
2490 &key_hnd);
2491 if (!W_ERROR_IS_OK(result)) {
2492 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
2493 path, win_errstr(result)));
2494 goto done;
2497 status = dcerpc_winreg_set_dword(tmp_ctx,
2498 winreg_handle,
2499 &key_hnd,
2500 "ChangeID",
2501 winreg_printer_rev_changeid(),
2502 &result);
2503 if (!NT_STATUS_IS_OK(status)) {
2504 result = ntstatus_to_werror(status);
2506 if (!W_ERROR_IS_OK(result)) {
2507 goto done;
2510 result = WERR_OK;
2511 done:
2512 if (winreg_handle != NULL) {
2513 WERROR ignore;
2515 if (is_valid_policy_hnd(&key_hnd)) {
2516 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2518 if (is_valid_policy_hnd(&hive_hnd)) {
2519 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2523 TALLOC_FREE(tmp_ctx);
2524 return result;
2527 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
2528 struct dcerpc_binding_handle *winreg_handle,
2529 const char *printer,
2530 uint32_t *pchangeid)
2532 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2533 struct policy_handle hive_hnd, key_hnd;
2534 uint32_t changeid = 0;
2535 char *path;
2536 NTSTATUS status;
2537 WERROR result;
2538 WERROR ignore;
2539 TALLOC_CTX *tmp_ctx;
2541 tmp_ctx = talloc_stackframe();
2542 if (tmp_ctx == NULL) {
2543 return WERR_NOMEM;
2546 path = winreg_printer_data_keyname(tmp_ctx, printer);
2547 if (path == NULL) {
2548 TALLOC_FREE(tmp_ctx);
2549 return WERR_NOMEM;
2552 ZERO_STRUCT(hive_hnd);
2553 ZERO_STRUCT(key_hnd);
2555 result = winreg_printer_openkey(tmp_ctx,
2556 winreg_handle,
2557 path,
2559 false,
2560 access_mask,
2561 &hive_hnd,
2562 &key_hnd);
2563 if (!W_ERROR_IS_OK(result)) {
2564 DEBUG(2, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
2565 path, win_errstr(result)));
2566 goto done;
2569 DEBUG(10, ("winreg_printer_get_changeid: get changeid from %s\n", path));
2571 status = dcerpc_winreg_query_dword(tmp_ctx,
2572 winreg_handle,
2573 &key_hnd,
2574 "ChangeID",
2575 &changeid,
2576 &result);
2577 if (!NT_STATUS_IS_OK(status)) {
2578 result = ntstatus_to_werror(status);
2580 if (!W_ERROR_IS_OK(result)) {
2581 goto done;
2584 if (pchangeid) {
2585 *pchangeid = changeid;
2588 result = WERR_OK;
2589 done:
2590 if (is_valid_policy_hnd(&key_hnd)) {
2591 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2593 if (is_valid_policy_hnd(&hive_hnd)) {
2594 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2597 TALLOC_FREE(tmp_ctx);
2598 return result;
2602 * The special behaviour of the spoolss forms is documented at the website:
2604 * Managing Win32 Printserver Forms
2605 * http://unixwiz.net/techtips/winspooler-forms.html
2608 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
2609 struct dcerpc_binding_handle *winreg_handle,
2610 struct spoolss_AddFormInfo1 *form)
2612 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2613 struct policy_handle hive_hnd, key_hnd;
2614 struct winreg_String wvalue = { 0, };
2615 DATA_BLOB blob;
2616 uint32_t num_info = 0;
2617 union spoolss_FormInfo *info = NULL;
2618 uint32_t i;
2619 WERROR result;
2620 NTSTATUS status;
2621 TALLOC_CTX *tmp_ctx;
2623 tmp_ctx = talloc_stackframe();
2624 if (tmp_ctx == NULL) {
2625 return WERR_NOMEM;
2628 ZERO_STRUCT(hive_hnd);
2629 ZERO_STRUCT(key_hnd);
2631 result = winreg_printer_openkey(tmp_ctx,
2632 winreg_handle,
2633 TOP_LEVEL_CONTROL_FORMS_KEY,
2635 true,
2636 access_mask,
2637 &hive_hnd,
2638 &key_hnd);
2639 if (!W_ERROR_IS_OK(result)) {
2640 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
2641 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2642 goto done;
2645 result = winreg_printer_enumforms1(tmp_ctx, winreg_handle,
2646 &num_info, &info);
2647 if (!W_ERROR_IS_OK(result)) {
2648 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
2649 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2650 goto done;
2653 /* If form name already exists or is builtin return ALREADY_EXISTS */
2654 for (i = 0; i < num_info; i++) {
2655 if (strequal(info[i].info1.form_name, form->form_name)) {
2656 result = WERR_FILE_EXISTS;
2657 goto done;
2661 wvalue.name = form->form_name;
2663 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2664 SIVAL(blob.data, 0, form->size.width);
2665 SIVAL(blob.data, 4, form->size.height);
2666 SIVAL(blob.data, 8, form->area.left);
2667 SIVAL(blob.data, 12, form->area.top);
2668 SIVAL(blob.data, 16, form->area.right);
2669 SIVAL(blob.data, 20, form->area.bottom);
2670 SIVAL(blob.data, 24, num_info + 1); /* FIXME */
2671 SIVAL(blob.data, 28, form->flags);
2673 status = dcerpc_winreg_SetValue(winreg_handle,
2674 tmp_ctx,
2675 &key_hnd,
2676 wvalue,
2677 REG_BINARY,
2678 blob.data,
2679 blob.length,
2680 &result);
2681 if (!NT_STATUS_IS_OK(status)) {
2682 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
2683 wvalue.name, nt_errstr(status)));
2684 result = ntstatus_to_werror(status);
2687 done:
2688 if (winreg_handle != NULL) {
2689 WERROR ignore;
2691 if (is_valid_policy_hnd(&key_hnd)) {
2692 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2694 if (is_valid_policy_hnd(&hive_hnd)) {
2695 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2699 TALLOC_FREE(info);
2700 TALLOC_FREE(tmp_ctx);
2701 return result;
2704 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
2705 struct dcerpc_binding_handle *winreg_handle,
2706 uint32_t *pnum_info,
2707 union spoolss_FormInfo **pinfo)
2709 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2710 struct policy_handle hive_hnd, key_hnd;
2711 union spoolss_FormInfo *info;
2712 struct spoolss_PrinterEnumValues *enum_values = NULL;
2713 uint32_t num_values = 0;
2714 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2715 uint32_t i;
2716 WERROR result;
2717 NTSTATUS status;
2718 const char **enum_names = NULL;
2719 enum winreg_Type *enum_types = NULL;
2720 DATA_BLOB *enum_data_blobs = NULL;
2721 TALLOC_CTX *tmp_ctx;
2723 tmp_ctx = talloc_stackframe();
2724 if (tmp_ctx == NULL) {
2725 return WERR_NOMEM;
2728 ZERO_STRUCT(hive_hnd);
2729 ZERO_STRUCT(key_hnd);
2731 result = winreg_printer_openkey(tmp_ctx,
2732 winreg_handle,
2733 TOP_LEVEL_CONTROL_FORMS_KEY,
2735 true,
2736 access_mask,
2737 &hive_hnd,
2738 &key_hnd);
2739 if (!W_ERROR_IS_OK(result)) {
2740 /* key doesn't exist */
2741 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2742 result = WERR_OK;
2743 goto done;
2746 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
2747 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2748 goto done;
2751 status = dcerpc_winreg_enumvals(tmp_ctx,
2752 winreg_handle,
2753 &key_hnd,
2754 &num_values,
2755 &enum_names,
2756 &enum_types,
2757 &enum_data_blobs,
2758 &result);
2759 if (!NT_STATUS_IS_OK(status)){
2760 result = ntstatus_to_werror(status);
2763 if (!W_ERROR_IS_OK(result)) {
2764 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2765 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2766 goto done;
2769 enum_values = talloc_zero_array(tmp_ctx,
2770 struct spoolss_PrinterEnumValues,
2771 num_values);
2772 if (enum_values == NULL){
2773 result = WERR_NOMEM;
2774 goto done;
2777 for (i = 0; i < num_values; i++){
2778 enum_values[i].value_name = enum_names[i];
2779 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
2780 enum_values[i].type = enum_types[i];
2781 enum_values[i].data_length = enum_data_blobs[i].length;
2782 enum_values[i].data = NULL;
2783 if (enum_values[i].data_length != 0){
2784 enum_values[i].data = &enum_data_blobs[i];
2788 if (!W_ERROR_IS_OK(result)) {
2789 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2790 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2791 goto done;
2794 info = talloc_array(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
2795 if (info == NULL) {
2796 result = WERR_NOMEM;
2797 goto done;
2800 /* Enumerate BUILTIN forms */
2801 for (i = 0; i < num_builtin; i++) {
2802 info[i].info1 = builtin_forms1[i];
2805 /* Enumerate registry forms */
2806 for (i = 0; i < num_values; i++) {
2807 union spoolss_FormInfo val;
2809 if (enum_values[i].type != REG_BINARY ||
2810 enum_values[i].data_length != 32) {
2811 continue;
2814 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
2815 if (val.info1.form_name == NULL) {
2816 result = WERR_NOMEM;
2817 goto done;
2820 val.info1.size.width = IVAL(enum_values[i].data->data, 0);
2821 val.info1.size.height = IVAL(enum_values[i].data->data, 4);
2822 val.info1.area.left = IVAL(enum_values[i].data->data, 8);
2823 val.info1.area.top = IVAL(enum_values[i].data->data, 12);
2824 val.info1.area.right = IVAL(enum_values[i].data->data, 16);
2825 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
2826 /* skip form index IVAL(enum_values[i].data->data, 24)));*/
2827 val.info1.flags = (enum spoolss_FormFlags) IVAL(enum_values[i].data->data, 28);
2829 info[i + num_builtin] = val;
2832 *pnum_info = num_builtin + num_values;
2833 if (pinfo) {
2834 *pinfo = talloc_move(mem_ctx, &info);
2837 done:
2838 if (winreg_handle != NULL) {
2839 WERROR ignore;
2841 if (is_valid_policy_hnd(&key_hnd)) {
2842 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2844 if (is_valid_policy_hnd(&hive_hnd)) {
2845 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2849 TALLOC_FREE(enum_values);
2850 TALLOC_FREE(tmp_ctx);
2851 return result;
2854 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
2855 struct dcerpc_binding_handle *winreg_handle,
2856 const char *form_name)
2858 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2859 struct policy_handle hive_hnd, key_hnd;
2860 struct winreg_String wvalue = { 0, };
2861 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2862 uint32_t i;
2863 WERROR result = WERR_OK;
2864 NTSTATUS status;
2865 TALLOC_CTX *tmp_ctx;
2867 for (i = 0; i < num_builtin; i++) {
2868 if (strequal(builtin_forms1[i].form_name, form_name)) {
2869 return WERR_INVALID_PARAMETER;
2873 tmp_ctx = talloc_stackframe();
2874 if (tmp_ctx == NULL) {
2875 return WERR_NOMEM;
2878 ZERO_STRUCT(hive_hnd);
2879 ZERO_STRUCT(key_hnd);
2881 result = winreg_printer_openkey(tmp_ctx,
2882 winreg_handle,
2883 TOP_LEVEL_CONTROL_FORMS_KEY,
2885 false,
2886 access_mask,
2887 &hive_hnd,
2888 &key_hnd);
2889 if (!W_ERROR_IS_OK(result)) {
2890 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
2891 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2892 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2893 result = WERR_INVALID_FORM_NAME;
2895 goto done;
2898 wvalue.name = form_name;
2899 status = dcerpc_winreg_DeleteValue(winreg_handle,
2900 tmp_ctx,
2901 &key_hnd,
2902 wvalue,
2903 &result);
2904 if (!NT_STATUS_IS_OK(status)) {
2905 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
2906 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
2907 wvalue.name, nt_errstr(status)));
2908 result = ntstatus_to_werror(status);
2909 goto done;
2912 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2913 result = WERR_INVALID_FORM_NAME;
2916 done:
2917 if (winreg_handle != NULL) {
2918 WERROR ignore;
2920 if (is_valid_policy_hnd(&key_hnd)) {
2921 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2923 if (is_valid_policy_hnd(&hive_hnd)) {
2924 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2928 TALLOC_FREE(tmp_ctx);
2929 return result;
2932 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
2933 struct dcerpc_binding_handle *winreg_handle,
2934 const char *form_name,
2935 struct spoolss_AddFormInfo1 *form)
2937 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2938 struct policy_handle hive_hnd, key_hnd;
2939 struct winreg_String wvalue = { 0, };
2940 DATA_BLOB blob;
2941 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2942 uint32_t i;
2943 WERROR result;
2944 NTSTATUS status;
2945 TALLOC_CTX *tmp_ctx = NULL;
2947 for (i = 0; i < num_builtin; i++) {
2948 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
2949 result = WERR_INVALID_PARAM;
2950 goto done;
2954 tmp_ctx = talloc_stackframe();
2955 if (tmp_ctx == NULL) {
2956 return WERR_NOMEM;
2959 ZERO_STRUCT(hive_hnd);
2960 ZERO_STRUCT(key_hnd);
2962 result = winreg_printer_openkey(tmp_ctx,
2963 winreg_handle,
2964 TOP_LEVEL_CONTROL_FORMS_KEY,
2966 true,
2967 access_mask,
2968 &hive_hnd,
2969 &key_hnd);
2970 if (!W_ERROR_IS_OK(result)) {
2971 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2972 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2973 goto done;
2976 /* If form_name != form->form_name then we renamed the form */
2977 if (strequal(form_name, form->form_name)) {
2978 result = winreg_printer_deleteform1(tmp_ctx, winreg_handle,
2979 form_name);
2980 if (!W_ERROR_IS_OK(result)) {
2981 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2982 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2983 goto done;
2987 wvalue.name = form->form_name;
2989 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2990 SIVAL(blob.data, 0, form->size.width);
2991 SIVAL(blob.data, 4, form->size.height);
2992 SIVAL(blob.data, 8, form->area.left);
2993 SIVAL(blob.data, 12, form->area.top);
2994 SIVAL(blob.data, 16, form->area.right);
2995 SIVAL(blob.data, 20, form->area.bottom);
2996 SIVAL(blob.data, 24, 42);
2997 SIVAL(blob.data, 28, form->flags);
2999 status = dcerpc_winreg_SetValue(winreg_handle,
3000 tmp_ctx,
3001 &key_hnd,
3002 wvalue,
3003 REG_BINARY,
3004 blob.data,
3005 blob.length,
3006 &result);
3007 if (!NT_STATUS_IS_OK(status)) {
3008 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
3009 wvalue.name, nt_errstr(status)));
3010 result = ntstatus_to_werror(status);
3013 done:
3014 if (winreg_handle != NULL) {
3015 WERROR ignore;
3017 if (is_valid_policy_hnd(&key_hnd)) {
3018 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3020 if (is_valid_policy_hnd(&hive_hnd)) {
3021 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3025 TALLOC_FREE(tmp_ctx);
3026 return result;
3029 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
3030 struct dcerpc_binding_handle *winreg_handle,
3031 const char *form_name,
3032 struct spoolss_FormInfo1 *r)
3034 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3035 struct policy_handle hive_hnd, key_hnd;
3036 struct winreg_String wvalue;
3037 enum winreg_Type type_in = REG_NONE;
3038 uint8_t *data_in = NULL;
3039 uint32_t data_in_size = 0;
3040 uint32_t value_len = 0;
3041 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3042 uint32_t i;
3043 WERROR result;
3044 WERROR ignore;
3045 NTSTATUS status;
3046 TALLOC_CTX *tmp_ctx;
3048 /* check builtin forms first */
3049 for (i = 0; i < num_builtin; i++) {
3050 if (strequal(builtin_forms1[i].form_name, form_name)) {
3051 *r = builtin_forms1[i];
3052 return WERR_OK;
3056 tmp_ctx = talloc_stackframe();
3057 if (tmp_ctx == NULL) {
3058 return WERR_NOMEM;
3061 ZERO_STRUCT(hive_hnd);
3062 ZERO_STRUCT(key_hnd);
3064 result = winreg_printer_openkey(tmp_ctx,
3065 winreg_handle,
3066 TOP_LEVEL_CONTROL_FORMS_KEY,
3068 true,
3069 access_mask,
3070 &hive_hnd,
3071 &key_hnd);
3072 if (!W_ERROR_IS_OK(result)) {
3073 DEBUG(2, ("winreg_printer_getform1: Could not open key %s: %s\n",
3074 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3075 goto done;
3078 wvalue.name = form_name;
3081 * call QueryValue once with data == NULL to get the
3082 * needed memory size to be allocated, then allocate
3083 * data buffer and call again.
3085 status = dcerpc_winreg_QueryValue(winreg_handle,
3086 tmp_ctx,
3087 &key_hnd,
3088 &wvalue,
3089 &type_in,
3090 NULL,
3091 &data_in_size,
3092 &value_len,
3093 &result);
3094 if (!NT_STATUS_IS_OK(status)) {
3095 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3096 wvalue.name, nt_errstr(status)));
3097 result = ntstatus_to_werror(status);
3098 goto done;
3100 if (!W_ERROR_IS_OK(result)) {
3101 goto done;
3104 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
3105 if (data_in == NULL) {
3106 result = WERR_NOMEM;
3107 goto done;
3109 value_len = 0;
3111 status = dcerpc_winreg_QueryValue(winreg_handle,
3112 tmp_ctx,
3113 &key_hnd,
3114 &wvalue,
3115 &type_in,
3116 data_in,
3117 &data_in_size,
3118 &value_len,
3119 &result);
3120 if (!NT_STATUS_IS_OK(status)) {
3121 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3122 wvalue.name, nt_errstr(status)));
3123 result = ntstatus_to_werror(status);
3124 goto done;
3126 if (!W_ERROR_IS_OK(result)) {
3127 goto done;
3130 r->form_name = talloc_strdup(mem_ctx, form_name);
3131 if (r->form_name == NULL) {
3132 result = WERR_NOMEM;
3133 goto done;
3136 r->size.width = IVAL(data_in, 0);
3137 r->size.height = IVAL(data_in, 4);
3138 r->area.left = IVAL(data_in, 8);
3139 r->area.top = IVAL(data_in, 12);
3140 r->area.right = IVAL(data_in, 16);
3141 r->area.bottom = IVAL(data_in, 20);
3142 /* skip index IVAL(data_in, 24)));*/
3143 r->flags = (enum spoolss_FormFlags) IVAL(data_in, 28);
3145 result = WERR_OK;
3146 done:
3147 if (is_valid_policy_hnd(&key_hnd)) {
3148 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3150 if (is_valid_policy_hnd(&hive_hnd)) {
3151 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3154 TALLOC_FREE(tmp_ctx);
3155 return result;
3158 WERROR winreg_add_driver(TALLOC_CTX *mem_ctx,
3159 struct dcerpc_binding_handle *winreg_handle,
3160 struct spoolss_AddDriverInfoCtr *r,
3161 const char **driver_name,
3162 uint32_t *driver_version)
3164 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3165 struct policy_handle hive_hnd, key_hnd;
3166 struct spoolss_DriverInfo8 info8;
3167 TALLOC_CTX *tmp_ctx = NULL;
3168 NTSTATUS status;
3169 WERROR result;
3171 ZERO_STRUCT(hive_hnd);
3172 ZERO_STRUCT(key_hnd);
3173 ZERO_STRUCT(info8);
3175 if (!driver_info_ctr_to_info8(r, &info8)) {
3176 result = WERR_INVALID_PARAMETER;
3177 goto done;
3180 tmp_ctx = talloc_stackframe();
3181 if (tmp_ctx == NULL) {
3182 return WERR_NOMEM;
3185 result = winreg_printer_opendriver(tmp_ctx,
3186 winreg_handle,
3187 info8.driver_name,
3188 info8.architecture,
3189 info8.version,
3190 access_mask, true,
3191 &hive_hnd,
3192 &key_hnd);
3193 if (!W_ERROR_IS_OK(result)) {
3194 DEBUG(0, ("winreg_add_driver: "
3195 "Could not open driver key (%s,%s,%d): %s\n",
3196 info8.driver_name, info8.architecture,
3197 info8.version, win_errstr(result)));
3198 goto done;
3201 /* TODO: "Attributes" ? */
3203 status = dcerpc_winreg_set_dword(tmp_ctx,
3204 winreg_handle,
3205 &key_hnd,
3206 "Version",
3207 info8.version,
3208 &result);
3209 if (!NT_STATUS_IS_OK(status)) {
3210 result = ntstatus_to_werror(status);
3212 if (!W_ERROR_IS_OK(result)) {
3213 goto done;
3216 status = dcerpc_winreg_set_sz(tmp_ctx,
3217 winreg_handle,
3218 &key_hnd,
3219 "Driver",
3220 info8.driver_path,
3221 &result);
3222 if (!NT_STATUS_IS_OK(status)) {
3223 result = ntstatus_to_werror(status);
3225 if (!W_ERROR_IS_OK(result)) {
3226 goto done;
3229 status = dcerpc_winreg_set_sz(tmp_ctx,
3230 winreg_handle,
3231 &key_hnd,
3232 "Data File",
3233 info8.data_file,
3234 &result);
3235 if (!NT_STATUS_IS_OK(status)) {
3236 result = ntstatus_to_werror(status);
3238 if (!W_ERROR_IS_OK(result)) {
3239 goto done;
3242 status = dcerpc_winreg_set_sz(tmp_ctx,
3243 winreg_handle,
3244 &key_hnd,
3245 "Configuration File",
3246 info8.config_file,
3247 &result);
3248 if (!NT_STATUS_IS_OK(status)) {
3249 result = ntstatus_to_werror(status);
3251 if (!W_ERROR_IS_OK(result)) {
3252 goto done;
3255 status = dcerpc_winreg_set_sz(tmp_ctx,
3256 winreg_handle,
3257 &key_hnd,
3258 "Help File",
3259 info8.help_file,
3260 &result);
3261 if (!NT_STATUS_IS_OK(status)) {
3262 result = ntstatus_to_werror(status);
3264 if (!W_ERROR_IS_OK(result)) {
3265 goto done;
3268 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3269 winreg_handle,
3270 &key_hnd,
3271 "Dependent Files",
3272 info8.dependent_files,
3273 &result);
3274 if (!NT_STATUS_IS_OK(status)) {
3275 result = ntstatus_to_werror(status);
3277 if (!W_ERROR_IS_OK(result)) {
3278 goto done;
3281 status = dcerpc_winreg_set_sz(tmp_ctx,
3282 winreg_handle,
3283 &key_hnd,
3284 "Monitor",
3285 info8.monitor_name,
3286 &result);
3287 if (!NT_STATUS_IS_OK(status)) {
3288 result = ntstatus_to_werror(status);
3290 if (!W_ERROR_IS_OK(result)) {
3291 goto done;
3294 status = dcerpc_winreg_set_sz(tmp_ctx,
3295 winreg_handle,
3296 &key_hnd,
3297 "Datatype",
3298 info8.default_datatype,
3299 &result);
3300 if (!NT_STATUS_IS_OK(status)) {
3301 result = ntstatus_to_werror(status);
3303 if (!W_ERROR_IS_OK(result)) {
3304 goto done;
3307 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3308 winreg_handle,
3309 &key_hnd, "Previous Names",
3310 info8.previous_names,
3311 &result);
3312 if (!NT_STATUS_IS_OK(status)) {
3313 result = ntstatus_to_werror(status);
3315 if (!W_ERROR_IS_OK(result)) {
3316 goto done;
3319 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3320 &key_hnd, "DriverDate",
3321 info8.driver_date);
3322 if (!W_ERROR_IS_OK(result)) {
3323 goto done;
3326 result = winreg_printer_write_ver(tmp_ctx, winreg_handle,
3327 &key_hnd, "DriverVersion",
3328 info8.driver_version);
3329 if (!W_ERROR_IS_OK(result)) {
3330 goto done;
3333 status = dcerpc_winreg_set_sz(tmp_ctx,
3334 winreg_handle,
3335 &key_hnd,
3336 "Manufacturer",
3337 info8.manufacturer_name,
3338 &result);
3339 if (!NT_STATUS_IS_OK(status)) {
3340 result = ntstatus_to_werror(status);
3342 if (!W_ERROR_IS_OK(result)) {
3343 goto done;
3346 status = dcerpc_winreg_set_sz(tmp_ctx,
3347 winreg_handle,
3348 &key_hnd,
3349 "OEM URL",
3350 info8.manufacturer_url,
3351 &result);
3352 if (!NT_STATUS_IS_OK(status)) {
3353 result = ntstatus_to_werror(status);
3355 if (!W_ERROR_IS_OK(result)) {
3356 goto done;
3359 status = dcerpc_winreg_set_sz(tmp_ctx,
3360 winreg_handle,
3361 &key_hnd,
3362 "HardwareID",
3363 info8.hardware_id,
3364 &result);
3365 if (!NT_STATUS_IS_OK(status)) {
3366 result = ntstatus_to_werror(status);
3368 if (!W_ERROR_IS_OK(result)) {
3369 goto done;
3372 status = dcerpc_winreg_set_sz(tmp_ctx,
3373 winreg_handle,
3374 &key_hnd,
3375 "Provider",
3376 info8.provider,
3377 &result);
3378 if (!NT_STATUS_IS_OK(status)) {
3379 result = ntstatus_to_werror(status);
3381 if (!W_ERROR_IS_OK(result)) {
3382 goto done;
3385 status = dcerpc_winreg_set_sz(tmp_ctx,
3386 winreg_handle,
3387 &key_hnd,
3388 "Print Processor",
3389 info8.print_processor,
3390 &result);
3391 if (!NT_STATUS_IS_OK(status)) {
3392 result = ntstatus_to_werror(status);
3394 if (!W_ERROR_IS_OK(result)) {
3395 goto done;
3398 status = dcerpc_winreg_set_sz(tmp_ctx,
3399 winreg_handle,
3400 &key_hnd,
3401 "VendorSetup",
3402 info8.vendor_setup,
3403 &result);
3404 if (!NT_STATUS_IS_OK(status)) {
3405 result = ntstatus_to_werror(status);
3407 if (!W_ERROR_IS_OK(result)) {
3408 goto done;
3411 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3412 winreg_handle,
3413 &key_hnd,
3414 "Color Profiles",
3415 info8.color_profiles,
3416 &result);
3417 if (!NT_STATUS_IS_OK(status)) {
3418 result = ntstatus_to_werror(status);
3420 if (!W_ERROR_IS_OK(result)) {
3421 goto done;
3424 status = dcerpc_winreg_set_sz(tmp_ctx,
3425 winreg_handle,
3426 &key_hnd,
3427 "InfPath",
3428 info8.inf_path,
3429 &result);
3430 if (!NT_STATUS_IS_OK(status)) {
3431 result = ntstatus_to_werror(status);
3433 if (!W_ERROR_IS_OK(result)) {
3434 goto done;
3437 status = dcerpc_winreg_set_dword(tmp_ctx,
3438 winreg_handle,
3439 &key_hnd,
3440 "PrinterDriverAttributes",
3441 info8.printer_driver_attributes,
3442 &result);
3443 if (!NT_STATUS_IS_OK(status)) {
3444 result = ntstatus_to_werror(status);
3446 if (!W_ERROR_IS_OK(result)) {
3447 goto done;
3450 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3451 winreg_handle,
3452 &key_hnd,
3453 "CoreDependencies",
3454 info8.core_driver_dependencies,
3455 &result);
3456 if (!NT_STATUS_IS_OK(status)) {
3457 result = ntstatus_to_werror(status);
3459 if (!W_ERROR_IS_OK(result)) {
3460 goto done;
3463 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3464 &key_hnd, "MinInboxDriverVerDate",
3465 info8.min_inbox_driver_ver_date);
3466 if (!W_ERROR_IS_OK(result)) {
3467 goto done;
3470 result = winreg_printer_write_ver(tmp_ctx, winreg_handle, &key_hnd,
3471 "MinInboxDriverVerVersion",
3472 info8.min_inbox_driver_ver_version);
3473 if (!W_ERROR_IS_OK(result)) {
3474 goto done;
3477 *driver_name = info8.driver_name;
3478 *driver_version = info8.version;
3479 result = WERR_OK;
3480 done:
3481 if (winreg_handle != NULL) {
3482 WERROR ignore;
3484 if (is_valid_policy_hnd(&key_hnd)) {
3485 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3487 if (is_valid_policy_hnd(&hive_hnd)) {
3488 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3492 TALLOC_FREE(tmp_ctx);
3493 return result;
3496 WERROR winreg_get_driver(TALLOC_CTX *mem_ctx,
3497 struct dcerpc_binding_handle *winreg_handle,
3498 const char *architecture,
3499 const char *driver_name,
3500 uint32_t driver_version,
3501 struct spoolss_DriverInfo8 **_info8)
3503 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3504 struct policy_handle hive_hnd, key_hnd;
3505 struct spoolss_DriverInfo8 i8, *info8;
3506 struct spoolss_PrinterEnumValues *enum_values = NULL;
3507 struct spoolss_PrinterEnumValues *v;
3508 uint32_t num_values = 0;
3509 TALLOC_CTX *tmp_ctx;
3510 WERROR result;
3511 NTSTATUS status;
3512 uint32_t i;
3513 const char **enum_names = NULL;
3514 enum winreg_Type *enum_types = NULL;
3515 DATA_BLOB *enum_data_blobs = NULL;
3517 ZERO_STRUCT(hive_hnd);
3518 ZERO_STRUCT(key_hnd);
3519 ZERO_STRUCT(i8);
3521 tmp_ctx = talloc_stackframe();
3522 if (tmp_ctx == NULL) {
3523 return WERR_NOMEM;
3526 if (driver_version == DRIVER_ANY_VERSION) {
3527 /* look for Win2k first and then for NT4 */
3528 result = winreg_printer_opendriver(tmp_ctx,
3529 winreg_handle,
3530 driver_name,
3531 architecture,
3533 access_mask, false,
3534 &hive_hnd,
3535 &key_hnd);
3536 if (!W_ERROR_IS_OK(result)) {
3537 result = winreg_printer_opendriver(tmp_ctx,
3538 winreg_handle,
3539 driver_name,
3540 architecture,
3542 access_mask, false,
3543 &hive_hnd,
3544 &key_hnd);
3546 } else {
3547 /* ok normal case */
3548 result = winreg_printer_opendriver(tmp_ctx,
3549 winreg_handle,
3550 driver_name,
3551 architecture,
3552 driver_version,
3553 access_mask, false,
3554 &hive_hnd,
3555 &key_hnd);
3557 if (!W_ERROR_IS_OK(result)) {
3558 DEBUG(5, ("winreg_get_driver: "
3559 "Could not open driver key (%s,%s,%d): %s\n",
3560 driver_name, architecture,
3561 driver_version, win_errstr(result)));
3562 goto done;
3565 status = dcerpc_winreg_enumvals(tmp_ctx,
3566 winreg_handle,
3567 &key_hnd,
3568 &num_values,
3569 &enum_names,
3570 &enum_types,
3571 &enum_data_blobs,
3572 &result);
3573 if (!NT_STATUS_IS_OK(status)){
3574 result = ntstatus_to_werror(status);
3577 if (!W_ERROR_IS_OK(result)) {
3578 DEBUG(0, ("winreg_get_driver: "
3579 "Could not enumerate values for (%s,%s,%d): %s\n",
3580 driver_name, architecture,
3581 driver_version, win_errstr(result)));
3582 goto done;
3585 enum_values = talloc_zero_array(tmp_ctx,
3586 struct spoolss_PrinterEnumValues,
3587 num_values);
3588 if (enum_values == NULL){
3589 result = WERR_NOMEM;
3590 goto done;
3593 for (i = 0; i < num_values; i++){
3594 enum_values[i].value_name = enum_names[i];
3595 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
3596 enum_values[i].type = enum_types[i];
3597 enum_values[i].data_length = enum_data_blobs[i].length;
3598 enum_values[i].data = NULL;
3599 if (enum_values[i].data_length != 0){
3600 enum_values[i].data = &enum_data_blobs[i];
3604 info8 = talloc_zero(tmp_ctx, struct spoolss_DriverInfo8);
3605 if (info8 == NULL) {
3606 result = WERR_NOMEM;
3607 goto done;
3610 info8->driver_name = talloc_strdup(info8, driver_name);
3611 if (info8->driver_name == NULL) {
3612 result = WERR_NOMEM;
3613 goto done;
3616 info8->architecture = talloc_strdup(info8, architecture);
3617 if (info8->architecture == NULL) {
3618 result = WERR_NOMEM;
3619 goto done;
3622 result = WERR_OK;
3624 for (i = 0; i < num_values; i++) {
3625 const char *tmp_str;
3626 uint32_t tmp = 0;
3628 v = &enum_values[i];
3630 result = winreg_enumval_to_dword(info8, v,
3631 "Version",
3632 &tmp);
3633 if (W_ERROR_IS_OK(result)) {
3634 info8->version = (enum spoolss_DriverOSVersion) tmp;
3636 CHECK_ERROR(result);
3638 result = winreg_enumval_to_sz(info8, v,
3639 "Driver",
3640 &info8->driver_path);
3641 CHECK_ERROR(result);
3643 result = winreg_enumval_to_sz(info8, v,
3644 "Data File",
3645 &info8->data_file);
3646 CHECK_ERROR(result);
3648 result = winreg_enumval_to_sz(info8, v,
3649 "Configuration File",
3650 &info8->config_file);
3651 CHECK_ERROR(result);
3653 result = winreg_enumval_to_sz(info8, v,
3654 "Help File",
3655 &info8->help_file);
3656 CHECK_ERROR(result);
3658 result = winreg_enumval_to_multi_sz(info8, v,
3659 "Dependent Files",
3660 &info8->dependent_files);
3661 CHECK_ERROR(result);
3663 result = winreg_enumval_to_sz(info8, v,
3664 "Monitor",
3665 &info8->monitor_name);
3666 CHECK_ERROR(result);
3668 result = winreg_enumval_to_sz(info8, v,
3669 "Datatype",
3670 &info8->default_datatype);
3671 CHECK_ERROR(result);
3673 result = winreg_enumval_to_multi_sz(info8, v,
3674 "Previous Names",
3675 &info8->previous_names);
3676 CHECK_ERROR(result);
3678 result = winreg_enumval_to_sz(info8, v,
3679 "DriverDate",
3680 &tmp_str);
3681 if (W_ERROR_IS_OK(result)) {
3682 result = winreg_printer_date_to_NTTIME(tmp_str,
3683 &info8->driver_date);
3685 CHECK_ERROR(result);
3687 result = winreg_enumval_to_sz(info8, v,
3688 "DriverVersion",
3689 &tmp_str);
3690 if (W_ERROR_IS_OK(result)) {
3691 result = winreg_printer_ver_to_dword(tmp_str,
3692 &info8->driver_version);
3694 CHECK_ERROR(result);
3696 result = winreg_enumval_to_sz(info8, v,
3697 "Manufacturer",
3698 &info8->manufacturer_name);
3699 CHECK_ERROR(result);
3701 result = winreg_enumval_to_sz(info8, v,
3702 "OEM URL",
3703 &info8->manufacturer_url);
3704 CHECK_ERROR(result);
3706 result = winreg_enumval_to_sz(info8, v,
3707 "HardwareID",
3708 &info8->hardware_id);
3709 CHECK_ERROR(result);
3711 result = winreg_enumval_to_sz(info8, v,
3712 "Provider",
3713 &info8->provider);
3714 CHECK_ERROR(result);
3716 result = winreg_enumval_to_sz(info8, v,
3717 "Print Processor",
3718 &info8->print_processor);
3719 CHECK_ERROR(result);
3721 result = winreg_enumval_to_sz(info8, v,
3722 "VendorSetup",
3723 &info8->vendor_setup);
3724 CHECK_ERROR(result);
3726 result = winreg_enumval_to_multi_sz(info8, v,
3727 "Color Profiles",
3728 &info8->color_profiles);
3729 CHECK_ERROR(result);
3731 result = winreg_enumval_to_sz(info8, v,
3732 "InfPath",
3733 &info8->inf_path);
3734 CHECK_ERROR(result);
3736 result = winreg_enumval_to_dword(info8, v,
3737 "PrinterDriverAttributes",
3738 &info8->printer_driver_attributes);
3739 CHECK_ERROR(result);
3741 result = winreg_enumval_to_multi_sz(info8, v,
3742 "CoreDependencies",
3743 &info8->core_driver_dependencies);
3744 CHECK_ERROR(result);
3746 result = winreg_enumval_to_sz(info8, v,
3747 "MinInboxDriverVerDate",
3748 &tmp_str);
3749 if (W_ERROR_IS_OK(result)) {
3750 result = winreg_printer_date_to_NTTIME(tmp_str,
3751 &info8->min_inbox_driver_ver_date);
3753 CHECK_ERROR(result);
3755 result = winreg_enumval_to_sz(info8, v,
3756 "MinInboxDriverVerVersion",
3757 &tmp_str);
3758 if (W_ERROR_IS_OK(result)) {
3759 result = winreg_printer_ver_to_dword(tmp_str,
3760 &info8->min_inbox_driver_ver_version);
3762 CHECK_ERROR(result);
3765 if (!W_ERROR_IS_OK(result)) {
3766 DEBUG(0, ("winreg_enumval_to_TYPE() failed "
3767 "for %s: %s\n", v->value_name,
3768 win_errstr(result)));
3769 goto done;
3772 *_info8 = talloc_steal(mem_ctx, info8);
3773 result = WERR_OK;
3774 done:
3775 if (winreg_handle != NULL) {
3776 WERROR ignore;
3778 if (is_valid_policy_hnd(&key_hnd)) {
3779 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3781 if (is_valid_policy_hnd(&hive_hnd)) {
3782 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3786 TALLOC_FREE(tmp_ctx);
3787 return result;
3790 WERROR winreg_del_driver(TALLOC_CTX *mem_ctx,
3791 struct dcerpc_binding_handle *winreg_handle,
3792 struct spoolss_DriverInfo8 *info8,
3793 uint32_t version)
3795 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3796 struct policy_handle hive_hnd, key_hnd;
3797 TALLOC_CTX *tmp_ctx;
3798 char *key_name;
3799 WERROR result;
3800 NTSTATUS status;
3802 ZERO_STRUCT(hive_hnd);
3803 ZERO_STRUCT(key_hnd);
3805 tmp_ctx = talloc_stackframe();
3806 if (tmp_ctx == NULL) {
3807 return WERR_NOMEM;
3810 /* test that the key exists */
3811 result = winreg_printer_opendriver(tmp_ctx,
3812 winreg_handle,
3813 info8->driver_name,
3814 info8->architecture,
3815 version,
3816 access_mask, false,
3817 &hive_hnd,
3818 &key_hnd);
3819 if (!W_ERROR_IS_OK(result)) {
3820 /* key doesn't exist */
3821 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3822 result = WERR_OK;
3823 goto done;
3826 DEBUG(5, ("winreg_del_driver: "
3827 "Could not open driver (%s,%s,%u): %s\n",
3828 info8->driver_name, info8->architecture,
3829 version, win_errstr(result)));
3830 goto done;
3834 if (is_valid_policy_hnd(&key_hnd)) {
3835 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
3838 key_name = talloc_asprintf(tmp_ctx,
3839 "%s\\Environments\\%s\\Drivers\\Version-%u\\%s",
3840 TOP_LEVEL_CONTROL_KEY,
3841 info8->architecture, version,
3842 info8->driver_name);
3843 if (key_name == NULL) {
3844 result = WERR_NOMEM;
3845 goto done;
3848 status = dcerpc_winreg_delete_subkeys_recursive(tmp_ctx,
3849 winreg_handle,
3850 &hive_hnd,
3851 access_mask,
3852 key_name,
3853 &result);
3855 if (!NT_STATUS_IS_OK(status)){
3856 DEBUG(0, ("winreg_del_driver: "
3857 "Could not open driver (%s,%s,%u): %s\n",
3858 info8->driver_name, info8->architecture,
3859 version, nt_errstr(status)));
3860 goto done;
3863 if (!W_ERROR_IS_OK(result)) {
3864 DEBUG(0, ("winreg_del_driver: "
3865 "Could not open driver (%s,%s,%u): %s\n",
3866 info8->driver_name, info8->architecture,
3867 version, win_errstr(result)));
3868 goto done;
3871 result = WERR_OK;
3872 done:
3873 if (winreg_handle != NULL) {
3874 WERROR ignore;
3876 if (is_valid_policy_hnd(&key_hnd)) {
3877 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3879 if (is_valid_policy_hnd(&hive_hnd)) {
3880 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3884 TALLOC_FREE(tmp_ctx);
3885 return result;
3888 WERROR winreg_get_driver_list(TALLOC_CTX *mem_ctx,
3889 struct dcerpc_binding_handle *winreg_handle,
3890 const char *architecture,
3891 uint32_t version,
3892 uint32_t *num_drivers,
3893 const char ***drivers_p)
3895 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3896 struct policy_handle hive_hnd, key_hnd;
3897 const char **drivers;
3898 TALLOC_CTX *tmp_ctx;
3899 WERROR result;
3900 NTSTATUS status;
3902 *num_drivers = 0;
3903 *drivers_p = NULL;
3905 ZERO_STRUCT(hive_hnd);
3906 ZERO_STRUCT(key_hnd);
3908 tmp_ctx = talloc_stackframe();
3909 if (tmp_ctx == NULL) {
3910 return WERR_NOMEM;
3913 /* use NULL for the driver name so we open the key that is
3914 * parent of all drivers for this architecture and version */
3915 result = winreg_printer_opendriver(tmp_ctx,
3916 winreg_handle,
3917 NULL,
3918 architecture,
3919 version,
3920 access_mask, false,
3921 &hive_hnd,
3922 &key_hnd);
3923 if (!W_ERROR_IS_OK(result)) {
3924 DEBUG(5, ("winreg_get_driver_list: "
3925 "Could not open key (%s,%u): %s\n",
3926 architecture, version, win_errstr(result)));
3927 result = WERR_OK;
3928 goto done;
3931 status = dcerpc_winreg_enum_keys(tmp_ctx,
3932 winreg_handle,
3933 &key_hnd,
3934 num_drivers,
3935 &drivers,
3936 &result);
3937 if (!NT_STATUS_IS_OK(status)) {
3938 result = ntstatus_to_werror(status);
3940 if (!W_ERROR_IS_OK(result)) {
3941 DEBUG(0, ("winreg_get_driver_list: "
3942 "Could not enumerate drivers for (%s,%u): %s\n",
3943 architecture, version, win_errstr(result)));
3944 goto done;
3947 *drivers_p = talloc_steal(mem_ctx, drivers);
3949 result = WERR_OK;
3950 done:
3951 if (winreg_handle != NULL) {
3952 WERROR ignore;
3954 if (is_valid_policy_hnd(&key_hnd)) {
3955 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3957 if (is_valid_policy_hnd(&hive_hnd)) {
3958 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3962 TALLOC_FREE(tmp_ctx);
3963 return result;