Remove unneeded initializations (we already talloc_zero).
[Samba.git] / source3 / rpc_client / cli_winreg_spoolss.c
blob97b406c20fbe1ea49b300206ecdf62136a9d205f
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, "Letter", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
60 { SPOOLSS_FORM_BUILTIN, "Letter Small", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
61 { SPOOLSS_FORM_BUILTIN, "Tabloid", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
62 { SPOOLSS_FORM_BUILTIN, "Ledger", {0x696b8,0x44368}, {0x0,0x0,0x696b8,0x44368} },
63 { SPOOLSS_FORM_BUILTIN, "Legal", {0x34b5c,0x56d10}, {0x0,0x0,0x34b5c,0x56d10} },
64 { SPOOLSS_FORM_BUILTIN, "Statement", {0x221b4,0x34b5c}, {0x0,0x0,0x221b4,0x34b5c} },
65 { SPOOLSS_FORM_BUILTIN, "Executive", {0x2cf56,0x411cc}, {0x0,0x0,0x2cf56,0x411cc} },
66 { SPOOLSS_FORM_BUILTIN, "A3", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
67 { SPOOLSS_FORM_BUILTIN, "A4", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
68 { SPOOLSS_FORM_BUILTIN, "A4 Small", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
69 { SPOOLSS_FORM_BUILTIN, "A5", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
70 { SPOOLSS_FORM_BUILTIN, "B4 (JIS)", {0x3ebe8,0x58de0}, {0x0,0x0,0x3ebe8,0x58de0} },
71 { SPOOLSS_FORM_BUILTIN, "B5 (JIS)", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
72 { SPOOLSS_FORM_BUILTIN, "Folio", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
73 { SPOOLSS_FORM_BUILTIN, "Quarto", {0x347d8,0x43238}, {0x0,0x0,0x347d8,0x43238} },
74 { SPOOLSS_FORM_BUILTIN, "10x14", {0x3e030,0x56d10}, {0x0,0x0,0x3e030,0x56d10} },
75 { SPOOLSS_FORM_BUILTIN, "11x17", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
76 { SPOOLSS_FORM_BUILTIN, "Note", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
77 { SPOOLSS_FORM_BUILTIN, "Envelope #9", {0x18079,0x37091}, {0x0,0x0,0x18079,0x37091} },
78 { SPOOLSS_FORM_BUILTIN, "Envelope #10", {0x19947,0x3ae94}, {0x0,0x0,0x19947,0x3ae94} },
79 { SPOOLSS_FORM_BUILTIN, "Envelope #11", {0x1be7c,0x40565}, {0x0,0x0,0x1be7c,0x40565} },
80 { SPOOLSS_FORM_BUILTIN, "Envelope #12", {0x1d74a,0x44368}, {0x0,0x0,0x1d74a,0x44368} },
81 { SPOOLSS_FORM_BUILTIN, "Envelope #14", {0x1f018,0x47504}, {0x0,0x0,0x1f018,0x47504} },
82 { SPOOLSS_FORM_BUILTIN, "C size sheet", {0x696b8,0x886d0}, {0x0,0x0,0x696b8,0x886d0} },
83 { SPOOLSS_FORM_BUILTIN, "D size sheet", {0x886d0,0xd2d70}, {0x0,0x0,0x886d0,0xd2d70} },
84 { SPOOLSS_FORM_BUILTIN, "E size sheet", {0xd2d70,0x110da0},{0x0,0x0,0xd2d70,0x110da0} },
85 { SPOOLSS_FORM_BUILTIN, "Envelope DL", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
86 { SPOOLSS_FORM_BUILTIN, "Envelope C5", {0x278d0,0x37e88}, {0x0,0x0,0x278d0,0x37e88} },
87 { SPOOLSS_FORM_BUILTIN, "Envelope C3", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
88 { SPOOLSS_FORM_BUILTIN, "Envelope C4", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
89 { SPOOLSS_FORM_BUILTIN, "Envelope C6", {0x1bd50,0x278d0}, {0x0,0x0,0x1bd50,0x278d0} },
90 { SPOOLSS_FORM_BUILTIN, "Envelope C65", {0x1bd50,0x37e88}, {0x0,0x0,0x1bd50,0x37e88} },
91 { SPOOLSS_FORM_BUILTIN, "Envelope B4", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
92 { SPOOLSS_FORM_BUILTIN, "Envelope B5", {0x2af80,0x3d090}, {0x0,0x0,0x2af80,0x3d090} },
93 { SPOOLSS_FORM_BUILTIN, "Envelope B6", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
94 { SPOOLSS_FORM_BUILTIN, "Envelope", {0x1adb0,0x38270}, {0x0,0x0,0x1adb0,0x38270} },
95 { SPOOLSS_FORM_BUILTIN, "Envelope Monarch", {0x18079,0x2e824}, {0x0,0x0,0x18079,0x2e824} },
96 { SPOOLSS_FORM_BUILTIN, "6 3/4 Envelope", {0x167ab,0x284ec}, {0x0,0x0,0x167ab,0x284ec} },
97 { SPOOLSS_FORM_BUILTIN, "US Std Fanfold", {0x5c3e1,0x44368}, {0x0,0x0,0x5c3e1,0x44368} },
98 { SPOOLSS_FORM_BUILTIN, "German Std Fanfold", {0x34b5c,0x4a6a0}, {0x0,0x0,0x34b5c,0x4a6a0} },
99 { SPOOLSS_FORM_BUILTIN, "German Legal Fanfold", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
100 { SPOOLSS_FORM_BUILTIN, "B4 (ISO)", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
101 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard", {0x186a0,0x24220}, {0x0,0x0,0x186a0,0x24220} },
102 { SPOOLSS_FORM_BUILTIN, "9x11", {0x37cf8,0x44368}, {0x0,0x0,0x37cf8,0x44368} },
103 { SPOOLSS_FORM_BUILTIN, "10x11", {0x3e030,0x44368}, {0x0,0x0,0x3e030,0x44368} },
104 { SPOOLSS_FORM_BUILTIN, "15x11", {0x5d048,0x44368}, {0x0,0x0,0x5d048,0x44368} },
105 { SPOOLSS_FORM_BUILTIN, "Envelope Invite", {0x35b60,0x35b60}, {0x0,0x0,0x35b60,0x35b60} },
106 { SPOOLSS_FORM_BUILTIN, "Reserved48", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
107 { SPOOLSS_FORM_BUILTIN, "Reserved49", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
108 { SPOOLSS_FORM_BUILTIN, "Letter Extra", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
109 { SPOOLSS_FORM_BUILTIN, "Legal Extra", {0x3ae94,0x5d048}, {0x0,0x0,0x3ae94,0x5d048} },
110 { SPOOLSS_FORM_BUILTIN, "Tabloid Extra", {0x4a6a0,0x6f9f0}, {0x0,0x0,0x4a6a0,0x6f9f0} },
111 { SPOOLSS_FORM_BUILTIN, "A4 Extra", {0x397c2,0x4eb16}, {0x0,0x0,0x397c2,0x4eb16} },
112 { SPOOLSS_FORM_BUILTIN, "Letter Transverse", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
113 { SPOOLSS_FORM_BUILTIN, "A4 Transverse", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
114 { SPOOLSS_FORM_BUILTIN, "Letter Extra Transverse", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
115 { SPOOLSS_FORM_BUILTIN, "Super A", {0x376b8,0x56ea0}, {0x0,0x0,0x376b8,0x56ea0} },
116 { SPOOLSS_FORM_BUILTIN, "Super B", {0x4a768,0x76e58}, {0x0,0x0,0x4a768,0x76e58} },
117 { SPOOLSS_FORM_BUILTIN, "Letter Plus", {0x34b5c,0x4eb16}, {0x0,0x0,0x34b5c,0x4eb16} },
118 { SPOOLSS_FORM_BUILTIN, "A4 Plus", {0x33450,0x50910}, {0x0,0x0,0x33450,0x50910} },
119 { SPOOLSS_FORM_BUILTIN, "A5 Transverse", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
120 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Transverse", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
121 { SPOOLSS_FORM_BUILTIN, "A3 Extra", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
122 { SPOOLSS_FORM_BUILTIN, "A5 Extra", {0x2a7b0,0x395f8}, {0x0,0x0,0x2a7b0,0x395f8} },
123 { SPOOLSS_FORM_BUILTIN, "B5 (ISO) Extra", {0x31128,0x43620}, {0x0,0x0,0x31128,0x43620} },
124 { SPOOLSS_FORM_BUILTIN, "A2", {0x668a0,0x91050}, {0x0,0x0,0x668a0,0x91050} },
125 { SPOOLSS_FORM_BUILTIN, "A3 Transverse", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
126 { SPOOLSS_FORM_BUILTIN, "A3 Extra Transverse", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
127 { SPOOLSS_FORM_BUILTIN, "Japanese Double Postcard", {0x30d40,0x24220}, {0x0,0x0,0x30d40,0x24220} },
128 { SPOOLSS_FORM_BUILTIN, "A6", {0x19a28,0x24220}, {0x0,0x0,0x19a28,0x24220} },
129 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #2 Rotated", {0x510e0,0x3a980}, {0x0,0x0,0x510e0,0x3a980} },
130 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #3 Rotated", {0x43a08,0x34bc0}, {0x0,0x0,0x43a08,0x34bc0} },
131 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #3 Rotated", {0x395f8,0x1d4c0}, {0x0,0x0,0x395f8,0x1d4c0} },
132 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #4 Rotated", {0x320c8,0x15f90}, {0x0,0x0,0x320c8,0x15f90} },
133 { SPOOLSS_FORM_BUILTIN, "Letter Rotated", {0x44368,0x34b5c}, {0x0,0x0,0x44368,0x34b5c} },
134 { SPOOLSS_FORM_BUILTIN, "A3 Rotated", {0x668a0,0x48828}, {0x0,0x0,0x668a0,0x48828} },
135 { SPOOLSS_FORM_BUILTIN, "A4 Rotated", {0x48828,0x33450}, {0x0,0x0,0x48828,0x33450} },
136 { SPOOLSS_FORM_BUILTIN, "A5 Rotated", {0x33450,0x24220}, {0x0,0x0,0x33450,0x24220} },
137 { SPOOLSS_FORM_BUILTIN, "B4 (JIS) Rotated", {0x58de0,0x3ebe8}, {0x0,0x0,0x58de0,0x3ebe8} },
138 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Rotated", {0x3ebe8,0x2c6f0}, {0x0,0x0,0x3ebe8,0x2c6f0} },
139 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard Rotated", {0x24220,0x186a0}, {0x0,0x0,0x24220,0x186a0} },
140 { SPOOLSS_FORM_BUILTIN, "Double Japan Postcard Rotated", {0x24220,0x30d40}, {0x0,0x0,0x24220,0x30d40} },
141 { SPOOLSS_FORM_BUILTIN, "A6 Rotated", {0x24220,0x19a28}, {0x0,0x0,0x24220,0x19a28} },
142 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #2", {0x3a980,0x510e0}, {0x0,0x0,0x3a980,0x510e0} },
143 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #3", {0x34bc0,0x43a08}, {0x0,0x0,0x34bc0,0x43a08} },
144 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #3", {0x1d4c0,0x395f8}, {0x0,0x0,0x1d4c0,0x395f8} },
145 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #4", {0x15f90,0x320c8}, {0x0,0x0,0x15f90,0x320c8} },
146 { SPOOLSS_FORM_BUILTIN, "B6 (JIS)", {0x1f400,0x2c6f0}, {0x0,0x0,0x1f400,0x2c6f0} },
147 { SPOOLSS_FORM_BUILTIN, "B6 (JIS) Rotated", {0x2c6f0,0x1f400}, {0x0,0x0,0x2c6f0,0x1f400} },
148 { SPOOLSS_FORM_BUILTIN, "12x11", {0x4a724,0x443e1}, {0x0,0x0,0x4a724,0x443e1} },
149 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4", {0x19a28,0x395f8}, {0x0,0x0,0x19a28,0x395f8} },
150 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4 Rotated", {0x395f8,0x19a28}, {0x0,0x0,0x395f8,0x19a28} },
151 { SPOOLSS_FORM_BUILTIN, "PRC 16K", {0x2de60,0x3f7a0}, {0x0,0x0,0x2de60,0x3f7a0} },
152 { SPOOLSS_FORM_BUILTIN, "PRC 32K", {0x1fbd0,0x2cec0}, {0x0,0x0,0x1fbd0,0x2cec0} },
153 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big)", {0x222e0,0x318f8}, {0x0,0x0,0x222e0,0x318f8} },
154 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1", {0x18e70,0x28488}, {0x0,0x0,0x18e70,0x28488} },
155 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2", {0x18e70,0x2af80}, {0x0,0x0,0x18e70,0x2af80} },
156 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3", {0x1e848,0x2af80}, {0x0,0x0,0x1e848,0x2af80} },
157 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4", {0x1adb0,0x32c80}, {0x0,0x0,0x1adb0,0x32c80} },
158 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
159 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6", {0x1d4c0,0x38270}, {0x0,0x0,0x1d4c0,0x38270} },
160 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7", {0x27100,0x38270}, {0x0,0x0,0x27100,0x38270} },
161 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8", {0x1d4c0,0x4b708}, {0x0,0x0,0x1d4c0,0x4b708} },
162 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
163 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
164 { SPOOLSS_FORM_BUILTIN, "PRC 16K Rotated", {0x3f7a0,0x2de60}, {0x0,0x0,0x3f7a0,0x2de60} },
165 { SPOOLSS_FORM_BUILTIN, "PRC 32K Rotated", {0x2cec0,0x1fbd0}, {0x0,0x0,0x2cec0,0x1fbd0} },
166 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big) Rotated", {0x318f8,0x222e0}, {0x0,0x0,0x318f8,0x222e0} },
167 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1 Rotated", {0x28488,0x18e70}, {0x0,0x0,0x28488,0x18e70} },
168 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2 Rotated", {0x2af80,0x18e70}, {0x0,0x0,0x2af80,0x18e70} },
169 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3 Rotated", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
170 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4 Rotated", {0x32c80,0x1adb0}, {0x0,0x0,0x32c80,0x1adb0} },
171 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5 Rotated", {0x35b60,0x1adb0}, {0x0,0x0,0x35b60,0x1adb0} },
172 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6 Rotated", {0x38270,0x1d4c0}, {0x0,0x0,0x38270,0x1d4c0} },
173 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7 Rotated", {0x38270,0x27100}, {0x0,0x0,0x38270,0x27100} },
174 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8 Rotated", {0x4b708,0x1d4c0}, {0x0,0x0,0x4b708,0x1d4c0} },
175 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9 Rotated", {0x4f1a0,0x37e88}, {0x0,0x0,0x4f1a0,0x37e88} },
176 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10 Rotated", {0x6fd10,0x4f1a0}, {0x0,0x0,0x6fd10,0x4f1a0} }
179 /********************************************************************
180 static helper functions
181 ********************************************************************/
183 /****************************************************************************
184 Update the changeid time.
185 ****************************************************************************/
187 * @internal
189 * @brief Update the ChangeID time of a printer.
191 * This is SO NASTY as some drivers need this to change, others need it
192 * static. This value will change every second, and I must hope that this
193 * is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
194 * UTAH ! JRA.
196 * @return The ChangeID.
198 static uint32_t winreg_printer_rev_changeid(void)
200 struct timeval tv;
202 get_process_uptime(&tv);
204 #if 1 /* JERRY */
205 /* Return changeid as msec since spooler restart */
206 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
207 #else
209 * This setting seems to work well but is too untested
210 * to replace the above calculation. Left in for experimentation
211 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
213 return tv.tv_sec * 10 + tv.tv_usec / 100000;
214 #endif
217 static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
218 struct dcerpc_binding_handle *binding_handle,
219 const char *path,
220 const char *key,
221 bool create_key,
222 uint32_t access_mask,
223 struct policy_handle *hive_handle,
224 struct policy_handle *key_handle)
226 struct winreg_String wkey, wkeyclass;
227 char *keyname;
228 NTSTATUS status;
229 WERROR result = WERR_OK;
231 status = dcerpc_winreg_OpenHKLM(binding_handle,
232 mem_ctx,
233 NULL,
234 access_mask,
235 hive_handle,
236 &result);
237 if (!NT_STATUS_IS_OK(status)) {
238 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
239 nt_errstr(status)));
240 return ntstatus_to_werror(status);
242 if (!W_ERROR_IS_OK(result)) {
243 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
244 win_errstr(result)));
245 return result;
248 if (key && *key) {
249 keyname = talloc_asprintf(mem_ctx, "%s\\%s", path, key);
250 } else {
251 keyname = talloc_strdup(mem_ctx, path);
253 if (keyname == NULL) {
254 return WERR_NOMEM;
257 ZERO_STRUCT(wkey);
258 wkey.name = keyname;
260 if (create_key) {
261 enum winreg_CreateAction action = REG_ACTION_NONE;
263 ZERO_STRUCT(wkeyclass);
264 wkeyclass.name = "";
266 status = dcerpc_winreg_CreateKey(binding_handle,
267 mem_ctx,
268 hive_handle,
269 wkey,
270 wkeyclass,
272 access_mask,
273 NULL,
274 key_handle,
275 &action,
276 &result);
277 switch (action) {
278 case REG_ACTION_NONE:
279 DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
280 break;
281 case REG_CREATED_NEW_KEY:
282 DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
283 break;
284 case REG_OPENED_EXISTING_KEY:
285 DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
286 break;
288 } else {
289 status = dcerpc_winreg_OpenKey(binding_handle,
290 mem_ctx,
291 hive_handle,
292 wkey,
294 access_mask,
295 key_handle,
296 &result);
298 if (!NT_STATUS_IS_OK(status)) {
299 return ntstatus_to_werror(status);
301 if (!W_ERROR_IS_OK(result)) {
302 return result;
305 return WERR_OK;
309 * @brief Create the registry keyname for the given printer.
311 * @param[in] mem_ctx The memory context to use.
313 * @param[in] printer The name of the printer to get the registry key.
315 * @return The registry key or NULL on error.
317 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
318 return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
322 * @internal
324 * @brief Enumerate values of an opened key handle and retrieve the data.
326 * @param[in] mem_ctx The memory context to use.
328 * @param[in] winreg_handle The binding handle for the rpc connection.
330 * @param[in] key_hnd The opened key handle.
332 * @param[out] pnum_values A pointer to store he number of values found.
334 * @param[out] pnum_values A pointer to store the number of values we found.
336 * @return WERR_OK on success, the corresponding DOS error
337 * code if something gone wrong.
339 static WERROR winreg_printer_enumvalues(TALLOC_CTX *mem_ctx,
340 struct dcerpc_binding_handle *winreg_handle,
341 struct policy_handle *key_hnd,
342 uint32_t *pnum_values,
343 struct spoolss_PrinterEnumValues **penum_values)
345 TALLOC_CTX *tmp_ctx;
346 uint32_t num_subkeys, max_subkeylen, max_classlen;
347 uint32_t num_values, max_valnamelen, max_valbufsize;
348 uint32_t secdescsize;
349 uint32_t i;
350 NTTIME last_changed_time;
351 struct winreg_String classname;
353 struct spoolss_PrinterEnumValues *enum_values;
355 WERROR result = WERR_OK;
356 NTSTATUS status;
358 tmp_ctx = talloc_stackframe();
359 if (tmp_ctx == NULL) {
360 return WERR_NOMEM;
363 ZERO_STRUCT(classname);
365 status = dcerpc_winreg_QueryInfoKey(winreg_handle,
366 tmp_ctx,
367 key_hnd,
368 &classname,
369 &num_subkeys,
370 &max_subkeylen,
371 &max_classlen,
372 &num_values,
373 &max_valnamelen,
374 &max_valbufsize,
375 &secdescsize,
376 &last_changed_time,
377 &result);
378 if (!NT_STATUS_IS_OK(status)) {
379 DEBUG(0, ("winreg_printer_enumvalues: Could not query info: %s\n",
380 nt_errstr(status)));
381 result = ntstatus_to_werror(status);
382 goto error;
384 if (!W_ERROR_IS_OK(result)) {
385 DEBUG(0, ("winreg_printer_enumvalues: Could not query info: %s\n",
386 win_errstr(result)));
387 goto error;
390 if (num_values == 0) {
391 *pnum_values = 0;
392 TALLOC_FREE(tmp_ctx);
393 return WERR_OK;
396 enum_values = talloc_array(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
397 if (enum_values == NULL) {
398 result = WERR_NOMEM;
399 goto error;
402 for (i = 0; i < num_values; i++) {
403 struct spoolss_PrinterEnumValues val;
404 struct winreg_ValNameBuf name_buf;
405 enum winreg_Type type = REG_NONE;
406 uint8_t *data;
407 uint32_t data_size;
408 uint32_t length;
409 char n = '\0';
411 name_buf.name = &n;
412 name_buf.size = max_valnamelen + 2;
413 name_buf.length = 0;
415 data_size = max_valbufsize;
416 data = NULL;
417 if (data_size) {
418 data = (uint8_t *) talloc_zero_size(tmp_ctx, data_size);
420 length = 0;
422 status = dcerpc_winreg_EnumValue(winreg_handle,
423 tmp_ctx,
424 key_hnd,
426 &name_buf,
427 &type,
428 data,
429 data_size ? &data_size : NULL,
430 &length,
431 &result);
432 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
433 result = WERR_OK;
434 status = NT_STATUS_OK;
435 break;
438 if (!NT_STATUS_IS_OK(status)) {
439 DEBUG(0, ("winreg_printer_enumvalues: Could not enumerate values: %s\n",
440 nt_errstr(status)));
441 result = ntstatus_to_werror(status);
442 goto error;
444 if (!W_ERROR_IS_OK(result)) {
445 DEBUG(0, ("winreg_printer_enumvalues: Could not enumerate values: %s\n",
446 win_errstr(result)));
447 goto error;
450 if (name_buf.name == NULL) {
451 result = WERR_INVALID_PARAMETER;
452 goto error;
455 val.value_name = talloc_strdup(enum_values, name_buf.name);
456 if (val.value_name == NULL) {
457 result = WERR_NOMEM;
458 goto error;
460 val.value_name_len = strlen_m_term(val.value_name) * 2;
462 val.type = type;
463 val.data_length = length;
464 val.data = NULL;
465 if (val.data_length) {
466 val.data = talloc(enum_values, DATA_BLOB);
467 if (val.data == NULL) {
468 result = WERR_NOMEM;
469 goto error;
471 *val.data = data_blob_talloc(val.data, data, val.data_length);
474 enum_values[i] = val;
477 *pnum_values = num_values;
478 if (penum_values) {
479 *penum_values = talloc_move(mem_ctx, &enum_values);
482 result = WERR_OK;
484 error:
485 TALLOC_FREE(tmp_ctx);
486 return result;
490 * @internal
492 * @brief A function to delete a key and its subkeys recurively.
494 * @param[in] mem_ctx The memory context to use.
496 * @param[in] winreg_handle The binding handle for the rpc connection.
498 * @param[in] hive_handle A opened hive handle to the key.
500 * @param[in] access_mask The access mask to access the key.
502 * @param[in] key The key to delete
504 * @return WERR_OK on success, the corresponding DOS error
505 * code if something gone wrong.
507 static WERROR winreg_printer_delete_subkeys(TALLOC_CTX *mem_ctx,
508 struct dcerpc_binding_handle *winreg_handle,
509 struct policy_handle *hive_handle,
510 uint32_t access_mask,
511 const char *key)
513 const char **subkeys = NULL;
514 uint32_t num_subkeys = 0;
515 struct policy_handle key_hnd;
516 struct winreg_String wkey = { 0, };
517 WERROR result = WERR_OK;
518 NTSTATUS status;
519 uint32_t i;
521 ZERO_STRUCT(key_hnd);
522 wkey.name = key;
524 DEBUG(2, ("winreg_printer_delete_subkeys: delete key %s\n", key));
525 /* open the key */
526 status = dcerpc_winreg_OpenKey(winreg_handle,
527 mem_ctx,
528 hive_handle,
529 wkey,
531 access_mask,
532 &key_hnd,
533 &result);
534 if (!NT_STATUS_IS_OK(status)) {
535 DEBUG(0, ("winreg_printer_delete_subkeys: Could not open key %s: %s\n",
536 wkey.name, nt_errstr(status)));
537 return ntstatus_to_werror(status);
539 if (!W_ERROR_IS_OK(result)) {
540 DEBUG(0, ("winreg_printer_delete_subkeys: Could not open key %s: %s\n",
541 wkey.name, win_errstr(result)));
542 return result;
545 status = dcerpc_winreg_enum_keys(mem_ctx,
546 winreg_handle,
547 &key_hnd,
548 &num_subkeys,
549 &subkeys,
550 &result);
551 if (!NT_STATUS_IS_OK(status)) {
552 result = ntstatus_to_werror(status);
554 if (!W_ERROR_IS_OK(result)) {
555 goto done;
558 for (i = 0; i < num_subkeys; i++) {
559 /* create key + subkey */
560 char *subkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkeys[i]);
561 if (subkey == NULL) {
562 goto done;
565 DEBUG(2, ("winreg_printer_delete_subkeys: delete subkey %s\n", subkey));
566 result = winreg_printer_delete_subkeys(mem_ctx,
567 winreg_handle,
568 hive_handle,
569 access_mask,
570 subkey);
571 if (!W_ERROR_IS_OK(result)) {
572 goto done;
576 if (is_valid_policy_hnd(&key_hnd)) {
577 WERROR ignore;
578 dcerpc_winreg_CloseKey(winreg_handle, mem_ctx, &key_hnd, &ignore);
581 wkey.name = key;
583 status = dcerpc_winreg_DeleteKey(winreg_handle,
584 mem_ctx,
585 hive_handle,
586 wkey,
587 &result);
588 if (!NT_STATUS_IS_OK(status)) {
589 result = ntstatus_to_werror(status);
592 done:
593 if (is_valid_policy_hnd(&key_hnd)) {
594 WERROR ignore;
596 dcerpc_winreg_CloseKey(winreg_handle, mem_ctx, &key_hnd, &ignore);
599 return result;
602 static WERROR winreg_printer_opendriver(TALLOC_CTX *mem_ctx,
603 struct dcerpc_binding_handle *winreg_handle,
604 const char *drivername,
605 const char *architecture,
606 uint32_t version,
607 uint32_t access_mask,
608 bool create,
609 struct policy_handle *hive_hnd,
610 struct policy_handle *key_hnd)
612 WERROR result;
613 char *key_name;
615 key_name = talloc_asprintf(mem_ctx, "%s\\Environments\\%s\\Drivers\\Version-%u",
616 TOP_LEVEL_CONTROL_KEY,
617 architecture, version);
618 if (!key_name) {
619 return WERR_NOMEM;
622 result = winreg_printer_openkey(mem_ctx,
623 winreg_handle,
624 key_name,
625 drivername,
626 create,
627 access_mask,
628 hive_hnd,
629 key_hnd);
630 return result;
633 static WERROR winreg_enumval_to_dword(TALLOC_CTX *mem_ctx,
634 struct spoolss_PrinterEnumValues *v,
635 const char *valuename, uint32_t *dw)
637 /* just return if it is not the one we are looking for */
638 if (strcmp(valuename, v->value_name) != 0) {
639 return WERR_NOT_FOUND;
642 if (v->type != REG_DWORD) {
643 return WERR_INVALID_DATATYPE;
646 if (v->data_length != 4) {
647 *dw = 0;
648 return WERR_OK;
651 *dw = IVAL(v->data->data, 0);
652 return WERR_OK;
655 static WERROR winreg_enumval_to_sz(TALLOC_CTX *mem_ctx,
656 struct spoolss_PrinterEnumValues *v,
657 const char *valuename, const char **_str)
659 /* just return if it is not the one we are looking for */
660 if (strcmp(valuename, v->value_name) != 0) {
661 return WERR_NOT_FOUND;
664 if (v->type != REG_SZ) {
665 return WERR_INVALID_DATATYPE;
668 if (v->data_length == 0) {
669 *_str = talloc_strdup(mem_ctx, EMPTY_STRING);
670 if (*_str == NULL) {
671 return WERR_NOMEM;
673 return WERR_OK;
676 if (!pull_reg_sz(mem_ctx, v->data, _str)) {
677 return WERR_NOMEM;
680 return WERR_OK;
683 static WERROR winreg_enumval_to_multi_sz(TALLOC_CTX *mem_ctx,
684 struct spoolss_PrinterEnumValues *v,
685 const char *valuename,
686 const char ***array)
688 /* just return if it is not the one we are looking for */
689 if (strcmp(valuename, v->value_name) != 0) {
690 return WERR_NOT_FOUND;
693 if (v->type != REG_MULTI_SZ) {
694 return WERR_INVALID_DATATYPE;
697 if (v->data_length == 0) {
698 *array = talloc_array(mem_ctx, const char *, 1);
699 if (*array == NULL) {
700 return WERR_NOMEM;
702 *array[0] = NULL;
703 return WERR_OK;
706 if (!pull_reg_multi_sz(mem_ctx, v->data, array)) {
707 return WERR_NOMEM;
710 return WERR_OK;
713 static WERROR winreg_printer_write_date(TALLOC_CTX *mem_ctx,
714 struct dcerpc_binding_handle *winreg_handle,
715 struct policy_handle *key_handle,
716 const char *value,
717 NTTIME data)
719 struct winreg_String wvalue = { 0, };
720 DATA_BLOB blob;
721 WERROR result = WERR_OK;
722 NTSTATUS status;
723 const char *str;
724 struct tm *tm;
725 time_t t;
727 if (data == 0) {
728 str = talloc_strdup(mem_ctx, "01/01/1601");
729 } else {
730 t = nt_time_to_unix(data);
731 tm = localtime(&t);
732 if (tm == NULL) {
733 return map_werror_from_unix(errno);
735 str = talloc_asprintf(mem_ctx, "%02d/%02d/%04d",
736 tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
738 if (!str) {
739 return WERR_NOMEM;
742 wvalue.name = value;
743 if (!push_reg_sz(mem_ctx, &blob, str)) {
744 return WERR_NOMEM;
746 status = dcerpc_winreg_SetValue(winreg_handle,
747 mem_ctx,
748 key_handle,
749 wvalue,
750 REG_SZ,
751 blob.data,
752 blob.length,
753 &result);
754 if (!NT_STATUS_IS_OK(status)) {
755 result = ntstatus_to_werror(status);
757 if (!W_ERROR_IS_OK(result)) {
758 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
759 wvalue.name, win_errstr(result)));
762 return result;
765 static WERROR winreg_printer_date_to_NTTIME(const char *str, NTTIME *data)
767 struct tm tm;
768 time_t t;
770 if (strequal(str, "01/01/1601")) {
771 *data = 0;
772 return WERR_OK;
775 ZERO_STRUCT(tm);
777 if (sscanf(str, "%d/%d/%d",
778 &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
779 return WERR_INVALID_PARAMETER;
781 tm.tm_mon -= 1;
782 tm.tm_year -= 1900;
783 tm.tm_isdst = -1;
785 t = mktime(&tm);
786 unix_to_nt_time(data, t);
788 return WERR_OK;
791 static WERROR winreg_printer_write_ver(TALLOC_CTX *mem_ctx,
792 struct dcerpc_binding_handle *winreg_handle,
793 struct policy_handle *key_handle,
794 const char *value,
795 uint64_t data)
797 struct winreg_String wvalue = { 0, };
798 DATA_BLOB blob;
799 WERROR result = WERR_OK;
800 NTSTATUS status;
801 char *str;
803 /* FIXME: check format is right,
804 * this needs to be something like: 6.1.7600.16385 */
805 str = talloc_asprintf(mem_ctx, "%u.%u.%u.%u",
806 (unsigned)((data >> 48) & 0xFFFF),
807 (unsigned)((data >> 32) & 0xFFFF),
808 (unsigned)((data >> 16) & 0xFFFF),
809 (unsigned)(data & 0xFFFF));
810 if (!str) {
811 return WERR_NOMEM;
814 wvalue.name = value;
815 if (!push_reg_sz(mem_ctx, &blob, str)) {
816 return WERR_NOMEM;
818 status = dcerpc_winreg_SetValue(winreg_handle,
819 mem_ctx,
820 key_handle,
821 wvalue,
822 REG_SZ,
823 blob.data,
824 blob.length,
825 &result);
826 if (!NT_STATUS_IS_OK(status)) {
827 result = ntstatus_to_werror(status);
829 if (!W_ERROR_IS_OK(result)) {
830 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
831 wvalue.name, win_errstr(result)));
834 return result;
837 static WERROR winreg_printer_ver_to_dword(const char *str, uint64_t *data)
839 unsigned int v1, v2, v3, v4;
841 if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
842 return WERR_INVALID_PARAMETER;
845 *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
846 ((uint64_t)(v2 & 0xFFFF) << 32) +
847 ((uint64_t)(v3 & 0xFFFF) << 16) +
848 (uint64_t)(v2 & 0xFFFF);
850 return WERR_OK;
853 /********************************************************************
854 Public winreg function for spoolss
855 ********************************************************************/
857 WERROR winreg_create_printer(TALLOC_CTX *mem_ctx,
858 struct dcerpc_binding_handle *winreg_handle,
859 const char *sharename)
861 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
862 struct policy_handle hive_hnd, key_hnd;
863 struct spoolss_SetPrinterInfo2 *info2;
864 struct security_descriptor *secdesc;
865 struct winreg_String wkey, wkeyclass;
866 const char *path;
867 const char *subkeys[] = { SPOOL_DSDRIVER_KEY, SPOOL_DSSPOOLER_KEY, SPOOL_PRINTERDATA_KEY };
868 uint32_t i, count = ARRAY_SIZE(subkeys);
869 uint32_t info2_mask = 0;
870 WERROR result = WERR_OK;
871 TALLOC_CTX *tmp_ctx;
873 tmp_ctx = talloc_stackframe();
874 if (tmp_ctx == NULL) {
875 return WERR_NOMEM;
878 path = winreg_printer_data_keyname(tmp_ctx, sharename);
879 if (path == NULL) {
880 TALLOC_FREE(tmp_ctx);
881 return WERR_NOMEM;
884 ZERO_STRUCT(hive_hnd);
885 ZERO_STRUCT(key_hnd);
887 result = winreg_printer_openkey(tmp_ctx,
888 winreg_handle,
889 path,
891 false,
892 access_mask,
893 &hive_hnd,
894 &key_hnd);
895 if (W_ERROR_IS_OK(result)) {
896 DEBUG(2, ("winreg_create_printer: Skipping, %s already exists\n", path));
897 goto done;
898 } else if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
899 DEBUG(2, ("winreg_create_printer: Creating default values in %s\n", path));
900 } else if (!W_ERROR_IS_OK(result)) {
901 DEBUG(0, ("winreg_create_printer: Could not open key %s: %s\n",
902 path, win_errstr(result)));
903 goto done;
906 /* Create the main key */
907 result = winreg_printer_openkey(tmp_ctx,
908 winreg_handle,
909 path,
911 true,
912 access_mask,
913 &hive_hnd,
914 &key_hnd);
915 if (!W_ERROR_IS_OK(result)) {
916 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
917 path, win_errstr(result)));
918 goto done;
921 if (is_valid_policy_hnd(&key_hnd)) {
922 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
925 /* Create subkeys */
926 for (i = 0; i < count; i++) {
927 NTSTATUS status;
928 enum winreg_CreateAction action = REG_ACTION_NONE;
930 ZERO_STRUCT(key_hnd);
931 ZERO_STRUCT(wkey);
933 wkey.name = talloc_asprintf(tmp_ctx, "%s\\%s", path, subkeys[i]);
934 if (wkey.name == NULL) {
935 result = WERR_NOMEM;
936 goto done;
939 ZERO_STRUCT(wkeyclass);
940 wkeyclass.name = "";
942 status = dcerpc_winreg_CreateKey(winreg_handle,
943 tmp_ctx,
944 &hive_hnd,
945 wkey,
946 wkeyclass,
948 access_mask,
949 NULL,
950 &key_hnd,
951 &action,
952 &result);
953 if (!NT_STATUS_IS_OK(status)) {
954 result = ntstatus_to_werror(status);
956 if (!W_ERROR_IS_OK(result)) {
957 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
958 wkey.name, win_errstr(result)));
959 goto done;
962 if (strequal(subkeys[i], SPOOL_DSSPOOLER_KEY)) {
963 const char *dnssuffix;
964 const char *longname;
965 const char *uncname;
967 status = dcerpc_winreg_set_sz(tmp_ctx,
968 winreg_handle,
969 &key_hnd,
970 SPOOL_REG_PRINTERNAME,
971 sharename,
972 &result);
973 if (!NT_STATUS_IS_OK(status)) {
974 result = ntstatus_to_werror(status);
976 if (!W_ERROR_IS_OK(result)) {
977 goto done;
980 status = dcerpc_winreg_set_sz(tmp_ctx,
981 winreg_handle,
982 &key_hnd,
983 SPOOL_REG_SHORTSERVERNAME,
984 global_myname(),
985 &result);
986 if (!NT_STATUS_IS_OK(status)) {
987 result = ntstatus_to_werror(status);
989 if (!W_ERROR_IS_OK(result)) {
990 goto done;
993 /* We make the assumption that the netbios name
994 * is the same as the DNS name since the former
995 * will be what we used to join the domain
997 dnssuffix = get_mydnsdomname(tmp_ctx);
998 if (dnssuffix != NULL && dnssuffix[0] != '\0') {
999 longname = talloc_asprintf(tmp_ctx, "%s.%s", global_myname(), dnssuffix);
1000 } else {
1001 longname = talloc_strdup(tmp_ctx, global_myname());
1003 if (longname == NULL) {
1004 result = WERR_NOMEM;
1005 goto done;
1008 status = dcerpc_winreg_set_sz(tmp_ctx,
1009 winreg_handle,
1010 &key_hnd,
1011 SPOOL_REG_SERVERNAME,
1012 longname,
1013 &result);
1014 if (!NT_STATUS_IS_OK(status)) {
1015 result = ntstatus_to_werror(status);
1017 if (!W_ERROR_IS_OK(result)) {
1018 goto done;
1021 uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
1022 longname, sharename);
1023 if (uncname == NULL) {
1024 result = WERR_NOMEM;
1025 goto done;
1028 status = dcerpc_winreg_set_sz(tmp_ctx,
1029 winreg_handle,
1030 &key_hnd,
1031 SPOOL_REG_UNCNAME,
1032 uncname,
1033 &result);
1034 if (!NT_STATUS_IS_OK(status)) {
1035 result = ntstatus_to_werror(status);
1037 if (!W_ERROR_IS_OK(result)) {
1038 goto done;
1041 status = dcerpc_winreg_set_dword(tmp_ctx,
1042 winreg_handle,
1043 &key_hnd,
1044 SPOOL_REG_VERSIONNUMBER,
1046 &result);
1047 if (!NT_STATUS_IS_OK(status)) {
1048 result = ntstatus_to_werror(status);
1050 if (!W_ERROR_IS_OK(result)) {
1051 goto done;
1054 status = dcerpc_winreg_set_dword(tmp_ctx,
1055 winreg_handle,
1056 &key_hnd,
1057 SPOOL_REG_PRINTSTARTTIME,
1059 &result);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 result = ntstatus_to_werror(status);
1063 if (!W_ERROR_IS_OK(result)) {
1064 goto done;
1067 status = dcerpc_winreg_set_dword(tmp_ctx,
1068 winreg_handle,
1069 &key_hnd,
1070 SPOOL_REG_PRINTENDTIME,
1072 &result);
1073 if (!NT_STATUS_IS_OK(status)) {
1074 result = ntstatus_to_werror(status);
1076 if (!W_ERROR_IS_OK(result)) {
1077 goto done;
1080 status = dcerpc_winreg_set_dword(tmp_ctx,
1081 winreg_handle,
1082 &key_hnd,
1083 SPOOL_REG_PRIORITY,
1085 &result);
1086 if (!NT_STATUS_IS_OK(status)) {
1087 result = ntstatus_to_werror(status);
1089 if (!W_ERROR_IS_OK(result)) {
1090 goto done;
1093 status = dcerpc_winreg_set_dword(tmp_ctx,
1094 winreg_handle,
1095 &key_hnd,
1096 SPOOL_REG_PRINTKEEPPRINTEDJOBS,
1098 &result);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 result = ntstatus_to_werror(status);
1102 if (!W_ERROR_IS_OK(result)) {
1103 goto done;
1107 if (is_valid_policy_hnd(&key_hnd)) {
1108 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
1111 info2 = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfo2);
1112 if (info2 == NULL) {
1113 result = WERR_NOMEM;
1114 goto done;
1117 info2->printername = sharename;
1118 if (info2->printername == NULL) {
1119 result = WERR_NOMEM;
1120 goto done;
1122 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTERNAME;
1124 info2->sharename = sharename;
1125 info2_mask |= SPOOLSS_PRINTER_INFO_SHARENAME;
1127 info2->portname = SAMBA_PRINTER_PORT_NAME;
1128 info2_mask |= SPOOLSS_PRINTER_INFO_PORTNAME;
1130 info2->printprocessor = "winprint";
1131 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTPROCESSOR;
1133 info2->datatype = "RAW";
1134 info2_mask |= SPOOLSS_PRINTER_INFO_DATATYPE;
1136 info2->comment = "";
1137 info2_mask |= SPOOLSS_PRINTER_INFO_COMMENT;
1139 info2->attributes = PRINTER_ATTRIBUTE_SAMBA;
1140 info2_mask |= SPOOLSS_PRINTER_INFO_ATTRIBUTES;
1142 info2->starttime = 0; /* Minutes since 12:00am GMT */
1143 info2_mask |= SPOOLSS_PRINTER_INFO_STARTTIME;
1145 info2->untiltime = 0; /* Minutes since 12:00am GMT */
1146 info2_mask |= SPOOLSS_PRINTER_INFO_UNTILTIME;
1148 info2->priority = 1;
1149 info2_mask |= SPOOLSS_PRINTER_INFO_PRIORITY;
1151 info2->defaultpriority = 1;
1152 info2_mask |= SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY;
1154 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1155 if (!W_ERROR_IS_OK(result)) {
1156 goto done;
1158 info2_mask |= SPOOLSS_PRINTER_INFO_SECDESC;
1161 * Don't write a default Device Mode to the registry! The Device Mode is
1162 * only written to disk with a SetPrinter level 2 or 8.
1165 result = winreg_update_printer(tmp_ctx,
1166 winreg_handle,
1167 sharename,
1168 info2_mask,
1169 info2,
1170 NULL,
1171 secdesc);
1173 done:
1174 if (winreg_handle != NULL) {
1175 WERROR ignore;
1177 if (is_valid_policy_hnd(&key_hnd)) {
1178 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1180 if (is_valid_policy_hnd(&hive_hnd)) {
1181 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1185 talloc_free(tmp_ctx);
1186 return result;
1189 WERROR winreg_update_printer(TALLOC_CTX *mem_ctx,
1190 struct dcerpc_binding_handle *winreg_handle,
1191 const char *sharename,
1192 uint32_t info2_mask,
1193 struct spoolss_SetPrinterInfo2 *info2,
1194 struct spoolss_DeviceMode *devmode,
1195 struct security_descriptor *secdesc)
1197 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1198 struct policy_handle hive_hnd, key_hnd;
1199 int snum = lp_servicenumber(sharename);
1200 enum ndr_err_code ndr_err;
1201 DATA_BLOB blob;
1202 char *path;
1203 WERROR result = WERR_OK;
1204 NTSTATUS status;
1205 TALLOC_CTX *tmp_ctx;
1207 tmp_ctx = talloc_stackframe();
1208 if (tmp_ctx == NULL) {
1209 return WERR_NOMEM;
1212 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1213 if (path == NULL) {
1214 TALLOC_FREE(tmp_ctx);
1215 return WERR_NOMEM;
1218 ZERO_STRUCT(hive_hnd);
1219 ZERO_STRUCT(key_hnd);
1221 result = winreg_printer_openkey(tmp_ctx,
1222 winreg_handle,
1223 path,
1225 true,
1226 access_mask,
1227 &hive_hnd,
1228 &key_hnd);
1229 if (!W_ERROR_IS_OK(result)) {
1230 DEBUG(0, ("winreg_update_printer: Could not open key %s: %s\n",
1231 path, win_errstr(result)));
1232 goto done;
1235 if (info2_mask & SPOOLSS_PRINTER_INFO_ATTRIBUTES) {
1236 status = dcerpc_winreg_set_dword(tmp_ctx,
1237 winreg_handle,
1238 &key_hnd,
1239 "Attributes",
1240 info2->attributes,
1241 &result);
1242 if (!NT_STATUS_IS_OK(status)) {
1243 result = ntstatus_to_werror(status);
1245 if (!W_ERROR_IS_OK(result)) {
1246 goto done;
1250 #if 0
1251 if (info2_mask & SPOOLSS_PRINTER_INFO_AVERAGEPPM) {
1252 status = dcerpc_winreg_set_dword(tmp_ctx,
1253 winreg_handle,
1254 &key_hnd,
1255 "AveragePpm",
1256 info2->attributes,
1257 &result);
1258 if (!NT_STATUS_IS_OK(status)) {
1259 result = ntstatus_to_werror(status);
1261 if (!W_ERROR_IS_OK(result)) {
1262 goto done;
1265 #endif
1267 if (info2_mask & SPOOLSS_PRINTER_INFO_COMMENT) {
1268 status = dcerpc_winreg_set_sz(tmp_ctx,
1269 winreg_handle,
1270 &key_hnd,
1271 "Description",
1272 info2->comment,
1273 &result);
1274 if (!NT_STATUS_IS_OK(status)) {
1275 result = ntstatus_to_werror(status);
1277 if (!W_ERROR_IS_OK(result)) {
1278 goto done;
1282 if (info2_mask & SPOOLSS_PRINTER_INFO_DATATYPE) {
1283 status = dcerpc_winreg_set_sz(tmp_ctx,
1284 winreg_handle,
1285 &key_hnd,
1286 "Datatype",
1287 info2->datatype,
1288 &result);
1289 if (!NT_STATUS_IS_OK(status)) {
1290 result = ntstatus_to_werror(status);
1292 if (!W_ERROR_IS_OK(result)) {
1293 goto done;
1297 if (info2_mask & SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY) {
1298 status = dcerpc_winreg_set_dword(tmp_ctx,
1299 winreg_handle,
1300 &key_hnd,
1301 "Default Priority",
1302 info2->defaultpriority,
1303 &result);
1304 if (!NT_STATUS_IS_OK(status)) {
1305 result = ntstatus_to_werror(status);
1307 if (!W_ERROR_IS_OK(result)) {
1308 goto done;
1312 if (info2_mask & SPOOLSS_PRINTER_INFO_DEVMODE) {
1314 * Some client drivers freak out if there is a NULL devmode
1315 * (probably the driver is not checking before accessing
1316 * the devmode pointer) --jerry
1318 if (devmode == NULL && lp_default_devmode(snum) && info2 != NULL) {
1319 result = spoolss_create_default_devmode(tmp_ctx,
1320 info2->printername,
1321 &devmode);
1322 if (!W_ERROR_IS_OK(result)) {
1323 goto done;
1327 if (devmode->size != (ndr_size_spoolss_DeviceMode(devmode, 0) - devmode->__driverextra_length)) {
1328 result = WERR_INVALID_PARAM;
1329 goto done;
1332 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, devmode,
1333 (ndr_push_flags_fn_t) ndr_push_spoolss_DeviceMode);
1334 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1335 DEBUG(0, ("winreg_update_printer: Failed to marshall device mode\n"));
1336 result = WERR_NOMEM;
1337 goto done;
1340 status = dcerpc_winreg_set_binary(tmp_ctx,
1341 winreg_handle,
1342 &key_hnd,
1343 "Default DevMode",
1344 &blob,
1345 &result);
1346 if (!NT_STATUS_IS_OK(status)) {
1347 result = ntstatus_to_werror(status);
1349 if (!W_ERROR_IS_OK(result)) {
1350 goto done;
1354 if (info2_mask & SPOOLSS_PRINTER_INFO_DRIVERNAME) {
1355 status = dcerpc_winreg_set_sz(tmp_ctx,
1356 winreg_handle,
1357 &key_hnd,
1358 "Printer Driver",
1359 info2->drivername,
1360 &result);
1361 if (!NT_STATUS_IS_OK(status)) {
1362 result = ntstatus_to_werror(status);
1364 if (!W_ERROR_IS_OK(result)) {
1365 goto done;
1369 if (info2_mask & SPOOLSS_PRINTER_INFO_LOCATION) {
1370 status = dcerpc_winreg_set_sz(tmp_ctx,
1371 winreg_handle,
1372 &key_hnd,
1373 "Location",
1374 info2->location,
1375 &result);
1376 if (!NT_STATUS_IS_OK(status)) {
1377 result = ntstatus_to_werror(status);
1379 if (!W_ERROR_IS_OK(result)) {
1380 goto done;
1384 if (info2_mask & SPOOLSS_PRINTER_INFO_PARAMETERS) {
1385 status = dcerpc_winreg_set_sz(tmp_ctx,
1386 winreg_handle,
1387 &key_hnd,
1388 "Parameters",
1389 info2->parameters,
1390 &result);
1391 if (!NT_STATUS_IS_OK(status)) {
1392 result = ntstatus_to_werror(status);
1394 if (!W_ERROR_IS_OK(result)) {
1395 goto done;
1399 if (info2_mask & SPOOLSS_PRINTER_INFO_PORTNAME) {
1400 status = dcerpc_winreg_set_sz(tmp_ctx,
1401 winreg_handle,
1402 &key_hnd,
1403 "Port",
1404 info2->portname,
1405 &result);
1406 if (!NT_STATUS_IS_OK(status)) {
1407 result = ntstatus_to_werror(status);
1409 if (!W_ERROR_IS_OK(result)) {
1410 goto done;
1414 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTERNAME) {
1416 * in addprinter: no servername and the printer is the name
1417 * in setprinter: servername is \\server
1418 * and printer is \\server\\printer
1420 * Samba manages only local printers.
1421 * we currently don't support things like i
1422 * path=\\other_server\printer
1424 * We only store the printername, not \\server\printername
1426 const char *p = strrchr(info2->printername, '\\');
1427 if (p == NULL) {
1428 p = info2->printername;
1429 } else {
1430 p++;
1432 status = dcerpc_winreg_set_sz(tmp_ctx,
1433 winreg_handle,
1434 &key_hnd,
1435 "Name",
1437 &result);
1438 if (!NT_STATUS_IS_OK(status)) {
1439 result = ntstatus_to_werror(status);
1441 if (!W_ERROR_IS_OK(result)) {
1442 goto done;
1446 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTPROCESSOR) {
1447 status = dcerpc_winreg_set_sz(tmp_ctx,
1448 winreg_handle,
1449 &key_hnd,
1450 "Print Processor",
1451 info2->printprocessor,
1452 &result);
1453 if (!NT_STATUS_IS_OK(status)) {
1454 result = ntstatus_to_werror(status);
1456 if (!W_ERROR_IS_OK(result)) {
1457 goto done;
1461 if (info2_mask & SPOOLSS_PRINTER_INFO_PRIORITY) {
1462 status = dcerpc_winreg_set_dword(tmp_ctx,
1463 winreg_handle,
1464 &key_hnd,
1465 "Priority",
1466 info2->priority,
1467 &result);
1468 if (!NT_STATUS_IS_OK(status)) {
1469 result = ntstatus_to_werror(status);
1471 if (!W_ERROR_IS_OK(result)) {
1472 goto done;
1476 if (info2_mask & SPOOLSS_PRINTER_INFO_SECDESC) {
1478 * We need a security descriptor, if it isn't specified by
1479 * AddPrinter{Ex} then create a default descriptor.
1481 if (secdesc == NULL) {
1482 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1483 if (!W_ERROR_IS_OK(result)) {
1484 goto done;
1487 result = winreg_set_printer_secdesc(tmp_ctx,
1488 winreg_handle,
1489 sharename,
1490 secdesc);
1491 if (!W_ERROR_IS_OK(result)) {
1492 goto done;
1496 if (info2_mask & SPOOLSS_PRINTER_INFO_SEPFILE) {
1497 status = dcerpc_winreg_set_sz(tmp_ctx,
1498 winreg_handle,
1499 &key_hnd,
1500 "Separator File",
1501 info2->sepfile,
1502 &result);
1503 if (!NT_STATUS_IS_OK(status)) {
1504 result = ntstatus_to_werror(status);
1506 if (!W_ERROR_IS_OK(result)) {
1507 goto done;
1511 if (info2_mask & SPOOLSS_PRINTER_INFO_SHARENAME) {
1512 status = dcerpc_winreg_set_sz(tmp_ctx,
1513 winreg_handle,
1514 &key_hnd,
1515 "Share Name",
1516 info2->sharename,
1517 &result);
1518 if (!NT_STATUS_IS_OK(status)) {
1519 result = ntstatus_to_werror(status);
1521 if (!W_ERROR_IS_OK(result)) {
1522 goto done;
1526 if (info2_mask & SPOOLSS_PRINTER_INFO_STARTTIME) {
1527 status = dcerpc_winreg_set_dword(tmp_ctx,
1528 winreg_handle,
1529 &key_hnd,
1530 "StartTime",
1531 info2->starttime,
1532 &result);
1533 if (!NT_STATUS_IS_OK(status)) {
1534 result = ntstatus_to_werror(status);
1536 if (!W_ERROR_IS_OK(result)) {
1537 goto done;
1541 if (info2_mask & SPOOLSS_PRINTER_INFO_STATUS) {
1542 status = dcerpc_winreg_set_dword(tmp_ctx,
1543 winreg_handle,
1544 &key_hnd,
1545 "Status",
1546 info2->status,
1547 &result);
1548 if (!NT_STATUS_IS_OK(status)) {
1549 result = ntstatus_to_werror(status);
1551 if (!W_ERROR_IS_OK(result)) {
1552 goto done;
1556 if (info2_mask & SPOOLSS_PRINTER_INFO_UNTILTIME) {
1557 status = dcerpc_winreg_set_dword(tmp_ctx,
1558 winreg_handle,
1559 &key_hnd,
1560 "UntilTime",
1561 info2->untiltime,
1562 &result);
1563 if (!NT_STATUS_IS_OK(status)) {
1564 result = ntstatus_to_werror(status);
1566 if (!W_ERROR_IS_OK(result)) {
1567 goto done;
1571 status = dcerpc_winreg_set_dword(tmp_ctx,
1572 winreg_handle,
1573 &key_hnd,
1574 "ChangeID",
1575 winreg_printer_rev_changeid(),
1576 &result);
1577 if (!NT_STATUS_IS_OK(status)) {
1578 result = ntstatus_to_werror(status);
1580 if (!W_ERROR_IS_OK(result)) {
1581 goto done;
1584 result = WERR_OK;
1585 done:
1586 if (winreg_handle != NULL) {
1587 WERROR ignore;
1589 if (is_valid_policy_hnd(&key_hnd)) {
1590 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1592 if (is_valid_policy_hnd(&hive_hnd)) {
1593 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1597 TALLOC_FREE(tmp_ctx);
1598 return result;
1601 WERROR winreg_get_printer(TALLOC_CTX *mem_ctx,
1602 struct dcerpc_binding_handle *winreg_handle,
1603 const char *printer,
1604 struct spoolss_PrinterInfo2 **pinfo2)
1606 struct spoolss_PrinterInfo2 *info2;
1607 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1608 struct policy_handle hive_hnd, key_hnd;
1609 struct spoolss_PrinterEnumValues *enum_values = NULL;
1610 struct spoolss_PrinterEnumValues *v = NULL;
1611 enum ndr_err_code ndr_err;
1612 DATA_BLOB blob;
1613 int snum = lp_servicenumber(printer);
1614 uint32_t num_values = 0;
1615 uint32_t i;
1616 char *path;
1617 NTSTATUS status;
1618 WERROR result = WERR_OK;
1619 TALLOC_CTX *tmp_ctx;
1621 tmp_ctx = talloc_stackframe();
1622 if (tmp_ctx == NULL) {
1623 return WERR_NOMEM;
1626 path = winreg_printer_data_keyname(tmp_ctx, printer);
1627 if (path == NULL) {
1628 TALLOC_FREE(tmp_ctx);
1629 return WERR_NOMEM;
1632 result = winreg_printer_openkey(tmp_ctx,
1633 winreg_handle,
1634 path,
1636 false,
1637 access_mask,
1638 &hive_hnd,
1639 &key_hnd);
1640 if (!W_ERROR_IS_OK(result)) {
1641 DEBUG(2, ("winreg_get_printer: Could not open key %s: %s\n",
1642 path, win_errstr(result)));
1643 goto done;
1646 result = winreg_printer_enumvalues(tmp_ctx,
1647 winreg_handle,
1648 &key_hnd,
1649 &num_values,
1650 &enum_values);
1651 if (!W_ERROR_IS_OK(result)) {
1652 DEBUG(0, ("winreg_get_printer: Could not enumerate values in %s: %s\n",
1653 path, win_errstr(result)));
1654 goto done;
1657 info2 = talloc_zero(tmp_ctx, struct spoolss_PrinterInfo2);
1658 if (info2 == NULL) {
1659 result = WERR_NOMEM;
1660 goto done;
1663 FILL_STRING(info2, EMPTY_STRING, info2->servername);
1664 FILL_STRING(info2, EMPTY_STRING, info2->printername);
1665 FILL_STRING(info2, EMPTY_STRING, info2->sharename);
1666 FILL_STRING(info2, EMPTY_STRING, info2->portname);
1667 FILL_STRING(info2, EMPTY_STRING, info2->drivername);
1668 FILL_STRING(info2, EMPTY_STRING, info2->comment);
1669 FILL_STRING(info2, EMPTY_STRING, info2->location);
1670 FILL_STRING(info2, EMPTY_STRING, info2->sepfile);
1671 FILL_STRING(info2, EMPTY_STRING, info2->printprocessor);
1672 FILL_STRING(info2, EMPTY_STRING, info2->datatype);
1673 FILL_STRING(info2, EMPTY_STRING, info2->parameters);
1675 for (i = 0; i < num_values; i++) {
1676 v = &enum_values[i];
1678 result = winreg_enumval_to_sz(info2,
1680 "Name",
1681 &info2->printername);
1682 CHECK_ERROR(result);
1684 result = winreg_enumval_to_sz(info2,
1686 "Share Name",
1687 &info2->sharename);
1688 CHECK_ERROR(result);
1690 result = winreg_enumval_to_sz(info2,
1692 "Port",
1693 &info2->portname);
1694 CHECK_ERROR(result);
1696 result = winreg_enumval_to_sz(info2,
1698 "Description",
1699 &info2->comment);
1700 CHECK_ERROR(result);
1702 result = winreg_enumval_to_sz(info2,
1704 "Location",
1705 &info2->location);
1706 CHECK_ERROR(result);
1708 result = winreg_enumval_to_sz(info2,
1710 "Separator File",
1711 &info2->sepfile);
1712 CHECK_ERROR(result);
1714 result = winreg_enumval_to_sz(info2,
1716 "Print Processor",
1717 &info2->printprocessor);
1718 CHECK_ERROR(result);
1720 result = winreg_enumval_to_sz(info2,
1722 "Datatype",
1723 &info2->datatype);
1724 CHECK_ERROR(result);
1726 result = winreg_enumval_to_sz(info2,
1728 "Parameters",
1729 &info2->parameters);
1730 CHECK_ERROR(result);
1732 result = winreg_enumval_to_sz(info2,
1734 "Printer Driver",
1735 &info2->drivername);
1736 CHECK_ERROR(result);
1738 result = winreg_enumval_to_dword(info2,
1740 "Attributes",
1741 &info2->attributes);
1742 CHECK_ERROR(result);
1744 result = winreg_enumval_to_dword(info2,
1746 "Priority",
1747 &info2->priority);
1748 CHECK_ERROR(result);
1750 result = winreg_enumval_to_dword(info2,
1752 "Default Priority",
1753 &info2->defaultpriority);
1754 CHECK_ERROR(result);
1756 result = winreg_enumval_to_dword(info2,
1758 "StartTime",
1759 &info2->starttime);
1760 CHECK_ERROR(result);
1762 result = winreg_enumval_to_dword(info2,
1764 "UntilTime",
1765 &info2->untiltime);
1766 CHECK_ERROR(result);
1768 result = winreg_enumval_to_dword(info2,
1770 "Status",
1771 &info2->status);
1772 CHECK_ERROR(result);
1774 result = winreg_enumval_to_dword(info2,
1776 "StartTime",
1777 &info2->starttime);
1778 CHECK_ERROR(result);
1781 if (!W_ERROR_IS_OK(result)) {
1782 DEBUG(0, ("winreg_get_printer: winreg_enumval_to_TYPE() failed "
1783 "for %s: %s\n",
1784 v->value_name,
1785 win_errstr(result)));
1786 goto done;
1789 /* Construct the Device Mode */
1790 status = dcerpc_winreg_query_binary(tmp_ctx,
1791 winreg_handle,
1792 &key_hnd,
1793 "Default DevMode",
1794 &blob,
1795 &result);
1796 if (!NT_STATUS_IS_OK(status)) {
1797 result = ntstatus_to_werror(status);
1799 if (W_ERROR_IS_OK(result)) {
1800 info2->devmode = talloc_zero(info2, struct spoolss_DeviceMode);
1801 if (info2->devmode == NULL) {
1802 result = WERR_NOMEM;
1803 goto done;
1805 ndr_err = ndr_pull_struct_blob(&blob,
1806 info2->devmode,
1807 info2->devmode,
1808 (ndr_pull_flags_fn_t) ndr_pull_spoolss_DeviceMode);
1809 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1810 DEBUG(0, ("winreg_get_printer: Failed to unmarshall device mode\n"));
1811 result = WERR_NOMEM;
1812 goto done;
1816 if (info2->devmode == NULL && lp_default_devmode(snum)) {
1817 result = spoolss_create_default_devmode(info2,
1818 info2->printername,
1819 &info2->devmode);
1820 if (!W_ERROR_IS_OK(result)) {
1821 goto done;
1825 if (info2->devmode) {
1826 info2->devmode->size = ndr_size_spoolss_DeviceMode(info2->devmode, 0) - info2->devmode->driverextra_data.length;
1829 result = winreg_get_printer_secdesc(info2,
1830 winreg_handle,
1831 printer,
1832 &info2->secdesc);
1833 if (!W_ERROR_IS_OK(result)) {
1834 goto done;
1837 /* Fix for OS/2 drivers. */
1838 if (get_remote_arch() == RA_OS2) {
1839 spoolss_map_to_os2_driver(info2, &info2->drivername);
1842 if (pinfo2) {
1843 *pinfo2 = talloc_move(mem_ctx, &info2);
1846 result = WERR_OK;
1847 done:
1848 if (winreg_handle != NULL) {
1849 WERROR ignore;
1851 if (is_valid_policy_hnd(&key_hnd)) {
1852 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1854 if (is_valid_policy_hnd(&hive_hnd)) {
1855 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1859 TALLOC_FREE(tmp_ctx);
1860 return result;
1863 WERROR winreg_get_printer_secdesc(TALLOC_CTX *mem_ctx,
1864 struct dcerpc_binding_handle *winreg_handle,
1865 const char *sharename,
1866 struct spoolss_security_descriptor **psecdesc)
1868 struct spoolss_security_descriptor *secdesc;
1869 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1870 struct policy_handle hive_hnd, key_hnd;
1871 const char *path;
1872 TALLOC_CTX *tmp_ctx;
1873 NTSTATUS status;
1874 WERROR result;
1876 tmp_ctx = talloc_stackframe();
1877 if (tmp_ctx == NULL) {
1878 return WERR_NOMEM;
1881 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1882 if (path == NULL) {
1883 talloc_free(tmp_ctx);
1884 return WERR_NOMEM;
1887 ZERO_STRUCT(hive_hnd);
1888 ZERO_STRUCT(key_hnd);
1890 result = winreg_printer_openkey(tmp_ctx,
1891 winreg_handle,
1892 path,
1894 false,
1895 access_mask,
1896 &hive_hnd,
1897 &key_hnd);
1898 if (!W_ERROR_IS_OK(result)) {
1899 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1900 goto create_default;
1902 goto done;
1905 status = dcerpc_winreg_query_sd(tmp_ctx,
1906 winreg_handle,
1907 &key_hnd,
1908 "Security",
1909 &secdesc,
1910 &result);
1911 if (!NT_STATUS_IS_OK(status)) {
1912 result = ntstatus_to_werror(status);
1914 if (!W_ERROR_IS_OK(result)) {
1915 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1916 goto create_default;
1918 goto done;
1921 if (psecdesc) {
1922 *psecdesc = talloc_move(mem_ctx, &secdesc);
1925 result = WERR_OK;
1926 goto done;
1928 create_default:
1929 result = winreg_printer_openkey(tmp_ctx,
1930 winreg_handle,
1931 path,
1933 true,
1934 access_mask,
1935 &hive_hnd,
1936 &key_hnd);
1937 if (!W_ERROR_IS_OK(result)) {
1938 goto done;
1941 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1942 if (!W_ERROR_IS_OK(result)) {
1943 goto done;
1946 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
1947 this security descriptor has been created when winbindd was
1948 down. Take ownership of security descriptor. */
1949 if (dom_sid_equal(secdesc->owner_sid, &global_sid_World)) {
1950 struct dom_sid owner_sid;
1952 /* Change sd owner to workgroup administrator */
1954 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
1955 struct spoolss_security_descriptor *new_secdesc;
1956 size_t size;
1958 /* Create new sd */
1959 sid_append_rid(&owner_sid, DOMAIN_RID_ADMINISTRATOR);
1961 new_secdesc = make_sec_desc(tmp_ctx,
1962 secdesc->revision,
1963 secdesc->type,
1964 &owner_sid,
1965 secdesc->group_sid,
1966 secdesc->sacl,
1967 secdesc->dacl,
1968 &size);
1970 if (new_secdesc == NULL) {
1971 result = WERR_NOMEM;
1972 goto done;
1975 /* Swap with other one */
1976 secdesc = new_secdesc;
1980 status = dcerpc_winreg_set_sd(tmp_ctx,
1981 winreg_handle,
1982 &key_hnd,
1983 "Security",
1984 secdesc,
1985 &result);
1986 if (!NT_STATUS_IS_OK(status)) {
1987 result = ntstatus_to_werror(status);
1989 if (!W_ERROR_IS_OK(result)) {
1990 return result;
1993 if (psecdesc) {
1994 *psecdesc = talloc_move(mem_ctx, &secdesc);
1997 result = WERR_OK;
1998 done:
1999 if (winreg_handle != NULL) {
2000 WERROR ignore;
2002 if (is_valid_policy_hnd(&key_hnd)) {
2003 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2005 if (is_valid_policy_hnd(&hive_hnd)) {
2006 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2010 talloc_free(tmp_ctx);
2011 return result;
2014 WERROR winreg_set_printer_secdesc(TALLOC_CTX *mem_ctx,
2015 struct dcerpc_binding_handle *winreg_handle,
2016 const char *sharename,
2017 const struct spoolss_security_descriptor *secdesc)
2019 const struct spoolss_security_descriptor *new_secdesc = secdesc;
2020 struct spoolss_security_descriptor *old_secdesc;
2021 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2022 struct policy_handle hive_hnd, key_hnd;
2023 const char *path;
2024 TALLOC_CTX *tmp_ctx;
2025 NTSTATUS status;
2026 WERROR result;
2028 tmp_ctx = talloc_stackframe();
2029 if (tmp_ctx == NULL) {
2030 return WERR_NOMEM;
2033 path = winreg_printer_data_keyname(tmp_ctx, sharename);
2034 if (path == NULL) {
2035 talloc_free(tmp_ctx);
2036 return WERR_NOMEM;
2040 * The old owner and group sids of the security descriptor are not
2041 * present when new ACEs are added or removed by changing printer
2042 * permissions through NT. If they are NULL in the new security
2043 * descriptor then copy them over from the old one.
2045 if (!secdesc->owner_sid || !secdesc->group_sid) {
2046 struct dom_sid *owner_sid, *group_sid;
2047 struct security_acl *dacl, *sacl;
2048 size_t size;
2050 result = winreg_get_printer_secdesc(tmp_ctx,
2051 winreg_handle,
2052 sharename,
2053 &old_secdesc);
2054 if (!W_ERROR_IS_OK(result)) {
2055 talloc_free(tmp_ctx);
2056 return result;
2059 /* Pick out correct owner and group sids */
2060 owner_sid = secdesc->owner_sid ?
2061 secdesc->owner_sid :
2062 old_secdesc->owner_sid;
2064 group_sid = secdesc->group_sid ?
2065 secdesc->group_sid :
2066 old_secdesc->group_sid;
2068 dacl = secdesc->dacl ?
2069 secdesc->dacl :
2070 old_secdesc->dacl;
2072 sacl = secdesc->sacl ?
2073 secdesc->sacl :
2074 old_secdesc->sacl;
2076 /* Make a deep copy of the security descriptor */
2077 new_secdesc = make_sec_desc(tmp_ctx,
2078 secdesc->revision,
2079 secdesc->type,
2080 owner_sid,
2081 group_sid,
2082 sacl,
2083 dacl,
2084 &size);
2085 if (new_secdesc == NULL) {
2086 talloc_free(tmp_ctx);
2087 return WERR_NOMEM;
2091 ZERO_STRUCT(hive_hnd);
2092 ZERO_STRUCT(key_hnd);
2094 result = winreg_printer_openkey(tmp_ctx,
2095 winreg_handle,
2096 path,
2098 false,
2099 access_mask,
2100 &hive_hnd,
2101 &key_hnd);
2102 if (!W_ERROR_IS_OK(result)) {
2103 goto done;
2106 status = dcerpc_winreg_set_sd(tmp_ctx,
2107 winreg_handle,
2108 &key_hnd,
2109 "Security",
2110 new_secdesc,
2111 &result);
2112 if (!NT_STATUS_IS_OK(status)) {
2113 result = ntstatus_to_werror(status);
2116 done:
2117 if (winreg_handle != NULL) {
2118 WERROR ignore;
2120 if (is_valid_policy_hnd(&key_hnd)) {
2121 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2123 if (is_valid_policy_hnd(&hive_hnd)) {
2124 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2128 talloc_free(tmp_ctx);
2129 return result;
2132 /* Set printer data over the winreg pipe. */
2133 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
2134 struct dcerpc_binding_handle *winreg_handle,
2135 const char *printer,
2136 const char *key,
2137 const char *value,
2138 enum winreg_Type type,
2139 uint8_t *data,
2140 uint32_t data_size)
2142 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2143 struct policy_handle hive_hnd, key_hnd;
2144 struct winreg_String wvalue = { 0, };
2145 char *path;
2146 WERROR result = WERR_OK;
2147 NTSTATUS status;
2148 TALLOC_CTX *tmp_ctx;
2150 tmp_ctx = talloc_stackframe();
2151 if (tmp_ctx == NULL) {
2152 return WERR_NOMEM;
2155 path = winreg_printer_data_keyname(tmp_ctx, printer);
2156 if (path == NULL) {
2157 TALLOC_FREE(tmp_ctx);
2158 return WERR_NOMEM;
2161 ZERO_STRUCT(hive_hnd);
2162 ZERO_STRUCT(key_hnd);
2164 DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
2165 key, value, access_mask, printer));
2166 result = winreg_printer_openkey(tmp_ctx,
2167 winreg_handle,
2168 path,
2169 key,
2170 true,
2171 access_mask,
2172 &hive_hnd,
2173 &key_hnd);
2174 if (!W_ERROR_IS_OK(result)) {
2175 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
2176 key, win_errstr(result)));
2177 goto done;
2180 wvalue.name = value;
2181 status = dcerpc_winreg_SetValue(winreg_handle,
2182 tmp_ctx,
2183 &key_hnd,
2184 wvalue,
2185 type,
2186 data,
2187 data_size,
2188 &result);
2189 if (!NT_STATUS_IS_OK(status)) {
2190 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
2191 value, nt_errstr(status)));
2192 result = ntstatus_to_werror(status);
2195 done:
2196 if (winreg_handle != NULL) {
2197 WERROR ignore;
2199 if (is_valid_policy_hnd(&key_hnd)) {
2200 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2202 if (is_valid_policy_hnd(&hive_hnd)) {
2203 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2207 TALLOC_FREE(tmp_ctx);
2208 return result;
2211 /* Get printer data over a winreg pipe. */
2212 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
2213 struct dcerpc_binding_handle *winreg_handle,
2214 const char *printer,
2215 const char *key,
2216 const char *value,
2217 enum winreg_Type *type,
2218 uint8_t **data,
2219 uint32_t *data_size)
2221 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2222 struct policy_handle hive_hnd, key_hnd;
2223 struct winreg_String wvalue;
2224 enum winreg_Type type_in = REG_NONE;
2225 char *path;
2226 uint8_t *data_in = NULL;
2227 uint32_t data_in_size = 0;
2228 uint32_t value_len = 0;
2229 WERROR result = WERR_OK;
2230 NTSTATUS status;
2231 TALLOC_CTX *tmp_ctx;
2233 tmp_ctx = talloc_stackframe();
2234 if (tmp_ctx == NULL) {
2235 return WERR_NOMEM;
2238 path = winreg_printer_data_keyname(tmp_ctx, printer);
2239 if (path == NULL) {
2240 TALLOC_FREE(tmp_ctx);
2241 return WERR_NOMEM;
2244 ZERO_STRUCT(hive_hnd);
2245 ZERO_STRUCT(key_hnd);
2247 result = winreg_printer_openkey(tmp_ctx,
2248 winreg_handle,
2249 path,
2250 key,
2251 false,
2252 access_mask,
2253 &hive_hnd,
2254 &key_hnd);
2255 if (!W_ERROR_IS_OK(result)) {
2256 DEBUG(2, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
2257 key, win_errstr(result)));
2258 goto done;
2261 wvalue.name = value;
2264 * call QueryValue once with data == NULL to get the
2265 * needed memory size to be allocated, then allocate
2266 * data buffer and call again.
2268 status = dcerpc_winreg_QueryValue(winreg_handle,
2269 tmp_ctx,
2270 &key_hnd,
2271 &wvalue,
2272 &type_in,
2273 NULL,
2274 &data_in_size,
2275 &value_len,
2276 &result);
2277 if (!NT_STATUS_IS_OK(status)) {
2278 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2279 value, nt_errstr(status)));
2280 result = ntstatus_to_werror(status);
2281 goto done;
2283 if (!W_ERROR_IS_OK(result)) {
2284 goto done;
2287 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2288 if (data_in == NULL) {
2289 result = WERR_NOMEM;
2290 goto done;
2292 value_len = 0;
2294 status = dcerpc_winreg_QueryValue(winreg_handle,
2295 tmp_ctx,
2296 &key_hnd,
2297 &wvalue,
2298 &type_in,
2299 data_in,
2300 &data_in_size,
2301 &value_len,
2302 &result);
2303 if (!NT_STATUS_IS_OK(status)) {
2304 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2305 value, nt_errstr(status)));
2306 result = ntstatus_to_werror(status);
2307 goto done;
2309 if (!W_ERROR_IS_OK(result)) {
2310 goto done;
2313 *type = type_in;
2314 *data_size = data_in_size;
2315 if (data_in_size) {
2316 *data = talloc_move(mem_ctx, &data_in);
2319 result = WERR_OK;
2320 done:
2321 if (winreg_handle != NULL) {
2322 WERROR ignore;
2324 if (is_valid_policy_hnd(&key_hnd)) {
2325 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2327 if (is_valid_policy_hnd(&hive_hnd)) {
2328 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2332 TALLOC_FREE(tmp_ctx);
2333 return result;
2336 /* Enumerate on the values of a given key and provide the data. */
2337 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
2338 struct dcerpc_binding_handle *winreg_handle,
2339 const char *printer,
2340 const char *key,
2341 uint32_t *pnum_values,
2342 struct spoolss_PrinterEnumValues **penum_values)
2344 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2345 struct policy_handle hive_hnd, key_hnd;
2347 struct spoolss_PrinterEnumValues *enum_values = NULL;
2348 uint32_t num_values = 0;
2349 char *path;
2350 WERROR result = WERR_OK;
2352 TALLOC_CTX *tmp_ctx;
2354 tmp_ctx = talloc_stackframe();
2355 if (tmp_ctx == NULL) {
2356 return WERR_NOMEM;
2359 path = winreg_printer_data_keyname(tmp_ctx, printer);
2360 if (path == NULL) {
2361 TALLOC_FREE(tmp_ctx);
2362 return WERR_NOMEM;
2365 result = winreg_printer_openkey(tmp_ctx,
2366 winreg_handle,
2367 path,
2368 key,
2369 false,
2370 access_mask,
2371 &hive_hnd,
2372 &key_hnd);
2373 if (!W_ERROR_IS_OK(result)) {
2374 DEBUG(2, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
2375 key, win_errstr(result)));
2376 goto done;
2379 result = winreg_printer_enumvalues(tmp_ctx,
2380 winreg_handle,
2381 &key_hnd,
2382 &num_values,
2383 &enum_values);
2384 if (!W_ERROR_IS_OK(result)) {
2385 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2386 key, win_errstr(result)));
2387 goto done;
2390 *pnum_values = num_values;
2391 if (penum_values) {
2392 *penum_values = talloc_move(mem_ctx, &enum_values);
2395 result = WERR_OK;
2396 done:
2397 if (winreg_handle != NULL) {
2398 WERROR ignore;
2400 if (is_valid_policy_hnd(&key_hnd)) {
2401 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2403 if (is_valid_policy_hnd(&hive_hnd)) {
2404 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2408 TALLOC_FREE(tmp_ctx);
2409 return result;
2412 /* Delete printer data over a winreg pipe. */
2413 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
2414 struct dcerpc_binding_handle *winreg_handle,
2415 const char *printer,
2416 const char *key,
2417 const char *value)
2419 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2420 struct policy_handle hive_hnd, key_hnd;
2421 struct winreg_String wvalue = { 0, };
2422 char *path;
2423 WERROR result = WERR_OK;
2424 NTSTATUS status;
2426 TALLOC_CTX *tmp_ctx;
2428 tmp_ctx = talloc_stackframe();
2429 if (tmp_ctx == NULL) {
2430 return WERR_NOMEM;
2433 path = winreg_printer_data_keyname(tmp_ctx, printer);
2434 if (path == NULL) {
2435 TALLOC_FREE(tmp_ctx);
2436 return WERR_NOMEM;
2439 ZERO_STRUCT(hive_hnd);
2440 ZERO_STRUCT(key_hnd);
2442 result = winreg_printer_openkey(tmp_ctx,
2443 winreg_handle,
2444 path,
2445 key,
2446 false,
2447 access_mask,
2448 &hive_hnd,
2449 &key_hnd);
2450 if (!W_ERROR_IS_OK(result)) {
2451 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
2452 key, win_errstr(result)));
2453 goto done;
2456 wvalue.name = value;
2457 status = dcerpc_winreg_DeleteValue(winreg_handle,
2458 tmp_ctx,
2459 &key_hnd,
2460 wvalue,
2461 &result);
2462 if (!NT_STATUS_IS_OK(status)) {
2463 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
2464 value, nt_errstr(status)));
2465 result = ntstatus_to_werror(status);
2468 done:
2469 if (winreg_handle != NULL) {
2470 WERROR ignore;
2472 if (is_valid_policy_hnd(&key_hnd)) {
2473 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2475 if (is_valid_policy_hnd(&hive_hnd)) {
2476 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2480 TALLOC_FREE(tmp_ctx);
2481 return result;
2484 /* Enumerate on the subkeys of a given key and provide the data. */
2485 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
2486 struct dcerpc_binding_handle *winreg_handle,
2487 const char *printer,
2488 const char *key,
2489 uint32_t *pnum_subkeys,
2490 const char ***psubkeys)
2492 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2493 struct policy_handle hive_hnd, key_hnd;
2494 char *path;
2495 const char **subkeys = NULL;
2496 uint32_t num_subkeys = -1;
2498 WERROR result = WERR_OK;
2499 NTSTATUS status;
2501 TALLOC_CTX *tmp_ctx;
2503 tmp_ctx = talloc_stackframe();
2504 if (tmp_ctx == NULL) {
2505 return WERR_NOMEM;
2508 path = winreg_printer_data_keyname(tmp_ctx, printer);
2509 if (path == NULL) {
2510 TALLOC_FREE(tmp_ctx);
2511 return WERR_NOMEM;
2514 ZERO_STRUCT(hive_hnd);
2515 ZERO_STRUCT(key_hnd);
2517 result = winreg_printer_openkey(tmp_ctx,
2518 winreg_handle,
2519 path,
2520 key,
2521 false,
2522 access_mask,
2523 &hive_hnd,
2524 &key_hnd);
2525 if (!W_ERROR_IS_OK(result)) {
2526 DEBUG(2, ("winreg_enum_printer_key: Could not open key %s: %s\n",
2527 key, win_errstr(result)));
2528 goto done;
2531 status = dcerpc_winreg_enum_keys(tmp_ctx,
2532 winreg_handle,
2533 &key_hnd,
2534 &num_subkeys,
2535 &subkeys,
2536 &result);
2537 if (!NT_STATUS_IS_OK(status)) {
2538 result = ntstatus_to_werror(status);
2540 if (!W_ERROR_IS_OK(result)) {
2541 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
2542 key, win_errstr(result)));
2543 goto done;
2546 *pnum_subkeys = num_subkeys;
2547 if (psubkeys) {
2548 *psubkeys = talloc_move(mem_ctx, &subkeys);
2551 result = WERR_OK;
2552 done:
2553 if (winreg_handle != NULL) {
2554 WERROR ignore;
2556 if (is_valid_policy_hnd(&key_hnd)) {
2557 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2559 if (is_valid_policy_hnd(&hive_hnd)) {
2560 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2564 TALLOC_FREE(tmp_ctx);
2565 return result;
2568 /* Delete a key with subkeys of a given printer. */
2569 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
2570 struct dcerpc_binding_handle *winreg_handle,
2571 const char *printer,
2572 const char *key)
2574 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2575 struct policy_handle hive_hnd, key_hnd;
2576 char *keyname;
2577 char *path;
2578 WERROR result;
2579 TALLOC_CTX *tmp_ctx;
2581 tmp_ctx = talloc_stackframe();
2582 if (tmp_ctx == NULL) {
2583 return WERR_NOMEM;
2586 path = winreg_printer_data_keyname(tmp_ctx, printer);
2587 if (path == NULL) {
2588 TALLOC_FREE(tmp_ctx);
2589 return WERR_NOMEM;
2592 result = winreg_printer_openkey(tmp_ctx,
2593 winreg_handle,
2594 path,
2595 key,
2596 false,
2597 access_mask,
2598 &hive_hnd,
2599 &key_hnd);
2600 if (!W_ERROR_IS_OK(result)) {
2601 /* key doesn't exist */
2602 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2603 result = WERR_OK;
2604 goto done;
2607 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
2608 key, win_errstr(result)));
2609 goto done;
2612 if (is_valid_policy_hnd(&key_hnd)) {
2613 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
2616 if (key == NULL || key[0] == '\0') {
2617 keyname = path;
2618 } else {
2619 keyname = talloc_asprintf(tmp_ctx,
2620 "%s\\%s",
2621 path,
2622 key);
2623 if (keyname == NULL) {
2624 result = WERR_NOMEM;
2625 goto done;
2629 result = winreg_printer_delete_subkeys(tmp_ctx,
2630 winreg_handle,
2631 &hive_hnd,
2632 access_mask,
2633 keyname);
2634 if (!W_ERROR_IS_OK(result)) {
2635 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2636 key, win_errstr(result)));
2637 goto done;
2640 done:
2641 if (winreg_handle != NULL) {
2642 WERROR ignore;
2644 if (is_valid_policy_hnd(&key_hnd)) {
2645 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2647 if (is_valid_policy_hnd(&hive_hnd)) {
2648 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2652 TALLOC_FREE(tmp_ctx);
2653 return result;
2656 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
2657 struct dcerpc_binding_handle *winreg_handle,
2658 const char *printer)
2660 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2661 struct policy_handle hive_hnd, key_hnd;
2662 char *path;
2663 NTSTATUS status;
2664 WERROR result;
2665 TALLOC_CTX *tmp_ctx;
2667 tmp_ctx = talloc_stackframe();
2668 if (tmp_ctx == NULL) {
2669 return WERR_NOMEM;
2672 path = winreg_printer_data_keyname(tmp_ctx, printer);
2673 if (path == NULL) {
2674 TALLOC_FREE(tmp_ctx);
2675 return WERR_NOMEM;
2678 ZERO_STRUCT(hive_hnd);
2679 ZERO_STRUCT(key_hnd);
2681 result = winreg_printer_openkey(tmp_ctx,
2682 winreg_handle,
2683 path,
2685 false,
2686 access_mask,
2687 &hive_hnd,
2688 &key_hnd);
2689 if (!W_ERROR_IS_OK(result)) {
2690 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
2691 path, win_errstr(result)));
2692 goto done;
2695 status = dcerpc_winreg_set_dword(tmp_ctx,
2696 winreg_handle,
2697 &key_hnd,
2698 "ChangeID",
2699 winreg_printer_rev_changeid(),
2700 &result);
2701 if (!NT_STATUS_IS_OK(status)) {
2702 result = ntstatus_to_werror(status);
2704 if (!W_ERROR_IS_OK(result)) {
2705 goto done;
2708 result = WERR_OK;
2709 done:
2710 if (winreg_handle != NULL) {
2711 WERROR ignore;
2713 if (is_valid_policy_hnd(&key_hnd)) {
2714 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2716 if (is_valid_policy_hnd(&hive_hnd)) {
2717 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2721 TALLOC_FREE(tmp_ctx);
2722 return result;
2725 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
2726 struct dcerpc_binding_handle *winreg_handle,
2727 const char *printer,
2728 uint32_t *pchangeid)
2730 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2731 struct policy_handle hive_hnd, key_hnd;
2732 uint32_t changeid = 0;
2733 char *path;
2734 NTSTATUS status;
2735 WERROR result;
2736 TALLOC_CTX *tmp_ctx;
2738 tmp_ctx = talloc_stackframe();
2739 if (tmp_ctx == NULL) {
2740 return WERR_NOMEM;
2743 path = winreg_printer_data_keyname(tmp_ctx, printer);
2744 if (path == NULL) {
2745 TALLOC_FREE(tmp_ctx);
2746 return WERR_NOMEM;
2749 ZERO_STRUCT(hive_hnd);
2750 ZERO_STRUCT(key_hnd);
2752 result = winreg_printer_openkey(tmp_ctx,
2753 winreg_handle,
2754 path,
2756 false,
2757 access_mask,
2758 &hive_hnd,
2759 &key_hnd);
2760 if (!W_ERROR_IS_OK(result)) {
2761 DEBUG(2, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
2762 path, win_errstr(result)));
2763 goto done;
2766 DEBUG(10, ("winreg_printer_get_changeid: get changeid from %s\n", path));
2768 status = dcerpc_winreg_query_dword(tmp_ctx,
2769 winreg_handle,
2770 &key_hnd,
2771 "ChangeID",
2772 &changeid,
2773 &result);
2774 if (!NT_STATUS_IS_OK(status)) {
2775 result = ntstatus_to_werror(status);
2777 if (!W_ERROR_IS_OK(result)) {
2778 goto done;
2781 if (pchangeid) {
2782 *pchangeid = changeid;
2785 result = WERR_OK;
2786 done:
2787 if (winreg_handle != NULL) {
2788 WERROR ignore;
2790 if (is_valid_policy_hnd(&key_hnd)) {
2791 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2793 if (is_valid_policy_hnd(&hive_hnd)) {
2794 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2798 TALLOC_FREE(tmp_ctx);
2799 return result;
2803 * The special behaviour of the spoolss forms is documented at the website:
2805 * Managing Win32 Printserver Forms
2806 * http://unixwiz.net/techtips/winspooler-forms.html
2809 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
2810 struct dcerpc_binding_handle *winreg_handle,
2811 struct spoolss_AddFormInfo1 *form)
2813 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2814 struct policy_handle hive_hnd, key_hnd;
2815 struct winreg_String wvalue = { 0, };
2816 DATA_BLOB blob;
2817 uint32_t num_info = 0;
2818 union spoolss_FormInfo *info = NULL;
2819 uint32_t i;
2820 WERROR result;
2821 NTSTATUS status;
2822 TALLOC_CTX *tmp_ctx;
2824 tmp_ctx = talloc_stackframe();
2825 if (tmp_ctx == NULL) {
2826 return WERR_NOMEM;
2829 ZERO_STRUCT(hive_hnd);
2830 ZERO_STRUCT(key_hnd);
2832 result = winreg_printer_openkey(tmp_ctx,
2833 winreg_handle,
2834 TOP_LEVEL_CONTROL_FORMS_KEY,
2836 true,
2837 access_mask,
2838 &hive_hnd,
2839 &key_hnd);
2840 if (!W_ERROR_IS_OK(result)) {
2841 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
2842 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2843 goto done;
2846 result = winreg_printer_enumforms1(tmp_ctx, winreg_handle,
2847 &num_info, &info);
2848 if (!W_ERROR_IS_OK(result)) {
2849 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
2850 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2851 goto done;
2854 /* If form name already exists or is builtin return ALREADY_EXISTS */
2855 for (i = 0; i < num_info; i++) {
2856 if (strequal(info[i].info1.form_name, form->form_name)) {
2857 result = WERR_FILE_EXISTS;
2858 goto done;
2862 wvalue.name = form->form_name;
2864 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2865 SIVAL(blob.data, 0, form->size.width);
2866 SIVAL(blob.data, 4, form->size.height);
2867 SIVAL(blob.data, 8, form->area.left);
2868 SIVAL(blob.data, 12, form->area.top);
2869 SIVAL(blob.data, 16, form->area.right);
2870 SIVAL(blob.data, 20, form->area.bottom);
2871 SIVAL(blob.data, 24, num_info + 1); /* FIXME */
2872 SIVAL(blob.data, 28, form->flags);
2874 status = dcerpc_winreg_SetValue(winreg_handle,
2875 tmp_ctx,
2876 &key_hnd,
2877 wvalue,
2878 REG_BINARY,
2879 blob.data,
2880 blob.length,
2881 &result);
2882 if (!NT_STATUS_IS_OK(status)) {
2883 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
2884 wvalue.name, nt_errstr(status)));
2885 result = ntstatus_to_werror(status);
2888 done:
2889 if (winreg_handle != NULL) {
2890 WERROR ignore;
2892 if (is_valid_policy_hnd(&key_hnd)) {
2893 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2895 if (is_valid_policy_hnd(&hive_hnd)) {
2896 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2900 TALLOC_FREE(info);
2901 TALLOC_FREE(tmp_ctx);
2902 return result;
2905 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
2906 struct dcerpc_binding_handle *winreg_handle,
2907 uint32_t *pnum_info,
2908 union spoolss_FormInfo **pinfo)
2910 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2911 struct policy_handle hive_hnd, key_hnd;
2912 union spoolss_FormInfo *info;
2913 struct spoolss_PrinterEnumValues *enum_values = NULL;
2914 uint32_t num_values = 0;
2915 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2916 uint32_t i;
2917 WERROR result;
2918 TALLOC_CTX *tmp_ctx;
2920 tmp_ctx = talloc_stackframe();
2921 if (tmp_ctx == NULL) {
2922 return WERR_NOMEM;
2925 ZERO_STRUCT(hive_hnd);
2926 ZERO_STRUCT(key_hnd);
2928 result = winreg_printer_openkey(tmp_ctx,
2929 winreg_handle,
2930 TOP_LEVEL_CONTROL_FORMS_KEY,
2932 true,
2933 access_mask,
2934 &hive_hnd,
2935 &key_hnd);
2936 if (!W_ERROR_IS_OK(result)) {
2937 /* key doesn't exist */
2938 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2939 result = WERR_OK;
2940 goto done;
2943 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
2944 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2945 goto done;
2948 result = winreg_printer_enumvalues(tmp_ctx,
2949 winreg_handle,
2950 &key_hnd,
2951 &num_values,
2952 &enum_values);
2953 if (!W_ERROR_IS_OK(result)) {
2954 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2955 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2956 goto done;
2959 info = talloc_array(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
2960 if (info == NULL) {
2961 result = WERR_NOMEM;
2962 goto done;
2965 /* Enumerate BUILTIN forms */
2966 for (i = 0; i < num_builtin; i++) {
2967 info[i].info1 = builtin_forms1[i];
2970 /* Enumerate registry forms */
2971 for (i = 0; i < num_values; i++) {
2972 union spoolss_FormInfo val;
2974 if (enum_values[i].type != REG_BINARY ||
2975 enum_values[i].data_length != 32) {
2976 continue;
2979 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
2980 if (val.info1.form_name == NULL) {
2981 result = WERR_NOMEM;
2982 goto done;
2985 val.info1.size.width = IVAL(enum_values[i].data->data, 0);
2986 val.info1.size.height = IVAL(enum_values[i].data->data, 4);
2987 val.info1.area.left = IVAL(enum_values[i].data->data, 8);
2988 val.info1.area.top = IVAL(enum_values[i].data->data, 12);
2989 val.info1.area.right = IVAL(enum_values[i].data->data, 16);
2990 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
2991 /* skip form index IVAL(enum_values[i].data->data, 24)));*/
2992 val.info1.flags = (enum spoolss_FormFlags) IVAL(enum_values[i].data->data, 28);
2994 info[i + num_builtin] = val;
2997 *pnum_info = num_builtin + num_values;
2998 if (pinfo) {
2999 *pinfo = talloc_move(mem_ctx, &info);
3002 done:
3003 if (winreg_handle != NULL) {
3004 WERROR ignore;
3006 if (is_valid_policy_hnd(&key_hnd)) {
3007 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3009 if (is_valid_policy_hnd(&hive_hnd)) {
3010 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3014 TALLOC_FREE(enum_values);
3015 TALLOC_FREE(tmp_ctx);
3016 return result;
3019 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
3020 struct dcerpc_binding_handle *winreg_handle,
3021 const char *form_name)
3023 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3024 struct policy_handle hive_hnd, key_hnd;
3025 struct winreg_String wvalue = { 0, };
3026 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3027 uint32_t i;
3028 WERROR result = WERR_OK;
3029 NTSTATUS status;
3030 TALLOC_CTX *tmp_ctx;
3032 for (i = 0; i < num_builtin; i++) {
3033 if (strequal(builtin_forms1[i].form_name, form_name)) {
3034 return WERR_INVALID_PARAMETER;
3038 tmp_ctx = talloc_stackframe();
3039 if (tmp_ctx == NULL) {
3040 return WERR_NOMEM;
3043 ZERO_STRUCT(hive_hnd);
3044 ZERO_STRUCT(key_hnd);
3046 result = winreg_printer_openkey(tmp_ctx,
3047 winreg_handle,
3048 TOP_LEVEL_CONTROL_FORMS_KEY,
3050 false,
3051 access_mask,
3052 &hive_hnd,
3053 &key_hnd);
3054 if (!W_ERROR_IS_OK(result)) {
3055 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
3056 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3057 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3058 result = WERR_INVALID_FORM_NAME;
3060 goto done;
3063 wvalue.name = form_name;
3064 status = dcerpc_winreg_DeleteValue(winreg_handle,
3065 tmp_ctx,
3066 &key_hnd,
3067 wvalue,
3068 &result);
3069 if (!NT_STATUS_IS_OK(status)) {
3070 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
3071 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
3072 wvalue.name, nt_errstr(status)));
3073 result = ntstatus_to_werror(status);
3074 goto done;
3077 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3078 result = WERR_INVALID_FORM_NAME;
3081 done:
3082 if (winreg_handle != NULL) {
3083 WERROR ignore;
3085 if (is_valid_policy_hnd(&key_hnd)) {
3086 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3088 if (is_valid_policy_hnd(&hive_hnd)) {
3089 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3093 TALLOC_FREE(tmp_ctx);
3094 return result;
3097 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
3098 struct dcerpc_binding_handle *winreg_handle,
3099 const char *form_name,
3100 struct spoolss_AddFormInfo1 *form)
3102 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3103 struct policy_handle hive_hnd, key_hnd;
3104 struct winreg_String wvalue = { 0, };
3105 DATA_BLOB blob;
3106 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3107 uint32_t i;
3108 WERROR result;
3109 NTSTATUS status;
3110 TALLOC_CTX *tmp_ctx = NULL;
3112 for (i = 0; i < num_builtin; i++) {
3113 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
3114 result = WERR_INVALID_PARAM;
3115 goto done;
3119 tmp_ctx = talloc_stackframe();
3120 if (tmp_ctx == NULL) {
3121 return WERR_NOMEM;
3124 ZERO_STRUCT(hive_hnd);
3125 ZERO_STRUCT(key_hnd);
3127 result = winreg_printer_openkey(tmp_ctx,
3128 winreg_handle,
3129 TOP_LEVEL_CONTROL_FORMS_KEY,
3131 true,
3132 access_mask,
3133 &hive_hnd,
3134 &key_hnd);
3135 if (!W_ERROR_IS_OK(result)) {
3136 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
3137 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3138 goto done;
3141 /* If form_name != form->form_name then we renamed the form */
3142 if (strequal(form_name, form->form_name)) {
3143 result = winreg_printer_deleteform1(tmp_ctx, winreg_handle,
3144 form_name);
3145 if (!W_ERROR_IS_OK(result)) {
3146 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
3147 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3148 goto done;
3152 wvalue.name = form->form_name;
3154 blob = data_blob_talloc(tmp_ctx, NULL, 32);
3155 SIVAL(blob.data, 0, form->size.width);
3156 SIVAL(blob.data, 4, form->size.height);
3157 SIVAL(blob.data, 8, form->area.left);
3158 SIVAL(blob.data, 12, form->area.top);
3159 SIVAL(blob.data, 16, form->area.right);
3160 SIVAL(blob.data, 20, form->area.bottom);
3161 SIVAL(blob.data, 24, 42);
3162 SIVAL(blob.data, 28, form->flags);
3164 status = dcerpc_winreg_SetValue(winreg_handle,
3165 tmp_ctx,
3166 &key_hnd,
3167 wvalue,
3168 REG_BINARY,
3169 blob.data,
3170 blob.length,
3171 &result);
3172 if (!NT_STATUS_IS_OK(status)) {
3173 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
3174 wvalue.name, nt_errstr(status)));
3175 result = ntstatus_to_werror(status);
3178 done:
3179 if (winreg_handle != NULL) {
3180 WERROR ignore;
3182 if (is_valid_policy_hnd(&key_hnd)) {
3183 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3185 if (is_valid_policy_hnd(&hive_hnd)) {
3186 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3190 TALLOC_FREE(tmp_ctx);
3191 return result;
3194 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
3195 struct dcerpc_binding_handle *winreg_handle,
3196 const char *form_name,
3197 struct spoolss_FormInfo1 *r)
3199 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3200 struct policy_handle hive_hnd, key_hnd;
3201 struct winreg_String wvalue;
3202 enum winreg_Type type_in = REG_NONE;
3203 uint8_t *data_in = NULL;
3204 uint32_t data_in_size = 0;
3205 uint32_t value_len = 0;
3206 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3207 uint32_t i;
3208 WERROR result;
3209 NTSTATUS status;
3210 TALLOC_CTX *tmp_ctx;
3212 /* check builtin forms first */
3213 for (i = 0; i < num_builtin; i++) {
3214 if (strequal(builtin_forms1[i].form_name, form_name)) {
3215 *r = builtin_forms1[i];
3216 return WERR_OK;
3220 tmp_ctx = talloc_stackframe();
3221 if (tmp_ctx == NULL) {
3222 return WERR_NOMEM;
3225 ZERO_STRUCT(hive_hnd);
3226 ZERO_STRUCT(key_hnd);
3228 result = winreg_printer_openkey(tmp_ctx,
3229 winreg_handle,
3230 TOP_LEVEL_CONTROL_FORMS_KEY,
3232 true,
3233 access_mask,
3234 &hive_hnd,
3235 &key_hnd);
3236 if (!W_ERROR_IS_OK(result)) {
3237 DEBUG(2, ("winreg_printer_getform1: Could not open key %s: %s\n",
3238 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3239 goto done;
3242 wvalue.name = form_name;
3245 * call QueryValue once with data == NULL to get the
3246 * needed memory size to be allocated, then allocate
3247 * data buffer and call again.
3249 status = dcerpc_winreg_QueryValue(winreg_handle,
3250 tmp_ctx,
3251 &key_hnd,
3252 &wvalue,
3253 &type_in,
3254 NULL,
3255 &data_in_size,
3256 &value_len,
3257 &result);
3258 if (!NT_STATUS_IS_OK(status)) {
3259 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3260 wvalue.name, nt_errstr(status)));
3261 result = ntstatus_to_werror(status);
3262 goto done;
3264 if (!W_ERROR_IS_OK(result)) {
3265 goto done;
3268 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
3269 if (data_in == NULL) {
3270 result = WERR_NOMEM;
3271 goto done;
3273 value_len = 0;
3275 status = dcerpc_winreg_QueryValue(winreg_handle,
3276 tmp_ctx,
3277 &key_hnd,
3278 &wvalue,
3279 &type_in,
3280 data_in,
3281 &data_in_size,
3282 &value_len,
3283 &result);
3284 if (!NT_STATUS_IS_OK(status)) {
3285 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3286 wvalue.name, nt_errstr(status)));
3287 result = ntstatus_to_werror(status);
3288 goto done;
3290 if (!W_ERROR_IS_OK(result)) {
3291 goto done;
3294 r->form_name = talloc_strdup(mem_ctx, form_name);
3295 if (r->form_name == NULL) {
3296 result = WERR_NOMEM;
3297 goto done;
3300 r->size.width = IVAL(data_in, 0);
3301 r->size.height = IVAL(data_in, 4);
3302 r->area.left = IVAL(data_in, 8);
3303 r->area.top = IVAL(data_in, 12);
3304 r->area.right = IVAL(data_in, 16);
3305 r->area.bottom = IVAL(data_in, 20);
3306 /* skip index IVAL(data_in, 24)));*/
3307 r->flags = (enum spoolss_FormFlags) IVAL(data_in, 28);
3309 result = WERR_OK;
3310 done:
3311 if (winreg_handle != NULL) {
3312 WERROR ignore;
3314 if (is_valid_policy_hnd(&key_hnd)) {
3315 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3317 if (is_valid_policy_hnd(&hive_hnd)) {
3318 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3322 TALLOC_FREE(tmp_ctx);
3323 return result;
3326 WERROR winreg_add_driver(TALLOC_CTX *mem_ctx,
3327 struct dcerpc_binding_handle *winreg_handle,
3328 struct spoolss_AddDriverInfoCtr *r,
3329 const char **driver_name,
3330 uint32_t *driver_version)
3332 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3333 struct policy_handle hive_hnd, key_hnd;
3334 struct spoolss_DriverInfo8 info8;
3335 TALLOC_CTX *tmp_ctx = NULL;
3336 NTSTATUS status;
3337 WERROR result;
3339 ZERO_STRUCT(hive_hnd);
3340 ZERO_STRUCT(key_hnd);
3341 ZERO_STRUCT(info8);
3343 if (!driver_info_ctr_to_info8(r, &info8)) {
3344 result = WERR_INVALID_PARAMETER;
3345 goto done;
3348 tmp_ctx = talloc_stackframe();
3349 if (tmp_ctx == NULL) {
3350 return WERR_NOMEM;
3353 result = winreg_printer_opendriver(tmp_ctx,
3354 winreg_handle,
3355 info8.driver_name,
3356 info8.architecture,
3357 info8.version,
3358 access_mask, true,
3359 &hive_hnd,
3360 &key_hnd);
3361 if (!W_ERROR_IS_OK(result)) {
3362 DEBUG(0, ("winreg_add_driver: "
3363 "Could not open driver key (%s,%s,%d): %s\n",
3364 info8.driver_name, info8.architecture,
3365 info8.version, win_errstr(result)));
3366 goto done;
3369 /* TODO: "Attributes" ? */
3371 status = dcerpc_winreg_set_dword(tmp_ctx,
3372 winreg_handle,
3373 &key_hnd,
3374 "Version",
3375 info8.version,
3376 &result);
3377 if (!NT_STATUS_IS_OK(status)) {
3378 result = ntstatus_to_werror(status);
3380 if (!W_ERROR_IS_OK(result)) {
3381 goto done;
3384 status = dcerpc_winreg_set_sz(tmp_ctx,
3385 winreg_handle,
3386 &key_hnd,
3387 "Driver",
3388 info8.driver_path,
3389 &result);
3390 if (!NT_STATUS_IS_OK(status)) {
3391 result = ntstatus_to_werror(status);
3393 if (!W_ERROR_IS_OK(result)) {
3394 goto done;
3397 status = dcerpc_winreg_set_sz(tmp_ctx,
3398 winreg_handle,
3399 &key_hnd,
3400 "Data File",
3401 info8.data_file,
3402 &result);
3403 if (!NT_STATUS_IS_OK(status)) {
3404 result = ntstatus_to_werror(status);
3406 if (!W_ERROR_IS_OK(result)) {
3407 goto done;
3410 status = dcerpc_winreg_set_sz(tmp_ctx,
3411 winreg_handle,
3412 &key_hnd,
3413 "Configuration File",
3414 info8.config_file,
3415 &result);
3416 if (!NT_STATUS_IS_OK(status)) {
3417 result = ntstatus_to_werror(status);
3419 if (!W_ERROR_IS_OK(result)) {
3420 goto done;
3423 status = dcerpc_winreg_set_sz(tmp_ctx,
3424 winreg_handle,
3425 &key_hnd,
3426 "Help File",
3427 info8.help_file,
3428 &result);
3429 if (!NT_STATUS_IS_OK(status)) {
3430 result = ntstatus_to_werror(status);
3432 if (!W_ERROR_IS_OK(result)) {
3433 goto done;
3436 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3437 winreg_handle,
3438 &key_hnd,
3439 "Dependent Files",
3440 info8.dependent_files,
3441 &result);
3442 if (!NT_STATUS_IS_OK(status)) {
3443 result = ntstatus_to_werror(status);
3445 if (!W_ERROR_IS_OK(result)) {
3446 goto done;
3449 status = dcerpc_winreg_set_sz(tmp_ctx,
3450 winreg_handle,
3451 &key_hnd,
3452 "Monitor",
3453 info8.monitor_name,
3454 &result);
3455 if (!NT_STATUS_IS_OK(status)) {
3456 result = ntstatus_to_werror(status);
3458 if (!W_ERROR_IS_OK(result)) {
3459 goto done;
3462 status = dcerpc_winreg_set_sz(tmp_ctx,
3463 winreg_handle,
3464 &key_hnd,
3465 "Datatype",
3466 info8.default_datatype,
3467 &result);
3468 if (!NT_STATUS_IS_OK(status)) {
3469 result = ntstatus_to_werror(status);
3471 if (!W_ERROR_IS_OK(result)) {
3472 goto done;
3475 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3476 winreg_handle,
3477 &key_hnd, "Previous Names",
3478 info8.previous_names,
3479 &result);
3480 if (!NT_STATUS_IS_OK(status)) {
3481 result = ntstatus_to_werror(status);
3483 if (!W_ERROR_IS_OK(result)) {
3484 goto done;
3487 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3488 &key_hnd, "DriverDate",
3489 info8.driver_date);
3490 if (!W_ERROR_IS_OK(result)) {
3491 goto done;
3494 result = winreg_printer_write_ver(tmp_ctx, winreg_handle,
3495 &key_hnd, "DriverVersion",
3496 info8.driver_version);
3497 if (!W_ERROR_IS_OK(result)) {
3498 goto done;
3501 status = dcerpc_winreg_set_sz(tmp_ctx,
3502 winreg_handle,
3503 &key_hnd,
3504 "Manufacturer",
3505 info8.manufacturer_name,
3506 &result);
3507 if (!NT_STATUS_IS_OK(status)) {
3508 result = ntstatus_to_werror(status);
3510 if (!W_ERROR_IS_OK(result)) {
3511 goto done;
3514 status = dcerpc_winreg_set_sz(tmp_ctx,
3515 winreg_handle,
3516 &key_hnd,
3517 "OEM URL",
3518 info8.manufacturer_url,
3519 &result);
3520 if (!NT_STATUS_IS_OK(status)) {
3521 result = ntstatus_to_werror(status);
3523 if (!W_ERROR_IS_OK(result)) {
3524 goto done;
3527 status = dcerpc_winreg_set_sz(tmp_ctx,
3528 winreg_handle,
3529 &key_hnd,
3530 "HardwareID",
3531 info8.hardware_id,
3532 &result);
3533 if (!NT_STATUS_IS_OK(status)) {
3534 result = ntstatus_to_werror(status);
3536 if (!W_ERROR_IS_OK(result)) {
3537 goto done;
3540 status = dcerpc_winreg_set_sz(tmp_ctx,
3541 winreg_handle,
3542 &key_hnd,
3543 "Provider",
3544 info8.provider,
3545 &result);
3546 if (!NT_STATUS_IS_OK(status)) {
3547 result = ntstatus_to_werror(status);
3549 if (!W_ERROR_IS_OK(result)) {
3550 goto done;
3553 status = dcerpc_winreg_set_sz(tmp_ctx,
3554 winreg_handle,
3555 &key_hnd,
3556 "Print Processor",
3557 info8.print_processor,
3558 &result);
3559 if (!NT_STATUS_IS_OK(status)) {
3560 result = ntstatus_to_werror(status);
3562 if (!W_ERROR_IS_OK(result)) {
3563 goto done;
3566 status = dcerpc_winreg_set_sz(tmp_ctx,
3567 winreg_handle,
3568 &key_hnd,
3569 "VendorSetup",
3570 info8.vendor_setup,
3571 &result);
3572 if (!NT_STATUS_IS_OK(status)) {
3573 result = ntstatus_to_werror(status);
3575 if (!W_ERROR_IS_OK(result)) {
3576 goto done;
3579 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3580 winreg_handle,
3581 &key_hnd,
3582 "Color Profiles",
3583 info8.color_profiles,
3584 &result);
3585 if (!NT_STATUS_IS_OK(status)) {
3586 result = ntstatus_to_werror(status);
3588 if (!W_ERROR_IS_OK(result)) {
3589 goto done;
3592 status = dcerpc_winreg_set_sz(tmp_ctx,
3593 winreg_handle,
3594 &key_hnd,
3595 "InfPath",
3596 info8.inf_path,
3597 &result);
3598 if (!NT_STATUS_IS_OK(status)) {
3599 result = ntstatus_to_werror(status);
3601 if (!W_ERROR_IS_OK(result)) {
3602 goto done;
3605 status = dcerpc_winreg_set_dword(tmp_ctx,
3606 winreg_handle,
3607 &key_hnd,
3608 "PrinterDriverAttributes",
3609 info8.printer_driver_attributes,
3610 &result);
3611 if (!NT_STATUS_IS_OK(status)) {
3612 result = ntstatus_to_werror(status);
3614 if (!W_ERROR_IS_OK(result)) {
3615 goto done;
3618 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3619 winreg_handle,
3620 &key_hnd,
3621 "CoreDependencies",
3622 info8.core_driver_dependencies,
3623 &result);
3624 if (!NT_STATUS_IS_OK(status)) {
3625 result = ntstatus_to_werror(status);
3627 if (!W_ERROR_IS_OK(result)) {
3628 goto done;
3631 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3632 &key_hnd, "MinInboxDriverVerDate",
3633 info8.min_inbox_driver_ver_date);
3634 if (!W_ERROR_IS_OK(result)) {
3635 goto done;
3638 result = winreg_printer_write_ver(tmp_ctx, winreg_handle, &key_hnd,
3639 "MinInboxDriverVerVersion",
3640 info8.min_inbox_driver_ver_version);
3641 if (!W_ERROR_IS_OK(result)) {
3642 goto done;
3645 *driver_name = info8.driver_name;
3646 *driver_version = info8.version;
3647 result = WERR_OK;
3648 done:
3649 if (winreg_handle != NULL) {
3650 WERROR ignore;
3652 if (is_valid_policy_hnd(&key_hnd)) {
3653 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3655 if (is_valid_policy_hnd(&hive_hnd)) {
3656 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3660 TALLOC_FREE(tmp_ctx);
3661 return result;
3664 WERROR winreg_get_driver(TALLOC_CTX *mem_ctx,
3665 struct dcerpc_binding_handle *winreg_handle,
3666 const char *architecture,
3667 const char *driver_name,
3668 uint32_t driver_version,
3669 struct spoolss_DriverInfo8 **_info8)
3671 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3672 struct policy_handle hive_hnd, key_hnd;
3673 struct spoolss_DriverInfo8 i8, *info8;
3674 struct spoolss_PrinterEnumValues *enum_values = NULL;
3675 struct spoolss_PrinterEnumValues *v;
3676 uint32_t num_values = 0;
3677 TALLOC_CTX *tmp_ctx;
3678 WERROR result;
3679 uint32_t i;
3681 ZERO_STRUCT(hive_hnd);
3682 ZERO_STRUCT(key_hnd);
3683 ZERO_STRUCT(i8);
3685 tmp_ctx = talloc_stackframe();
3686 if (tmp_ctx == NULL) {
3687 return WERR_NOMEM;
3690 if (driver_version == DRIVER_ANY_VERSION) {
3691 /* look for Win2k first and then for NT4 */
3692 result = winreg_printer_opendriver(tmp_ctx,
3693 winreg_handle,
3694 driver_name,
3695 architecture,
3697 access_mask, false,
3698 &hive_hnd,
3699 &key_hnd);
3700 if (!W_ERROR_IS_OK(result)) {
3701 result = winreg_printer_opendriver(tmp_ctx,
3702 winreg_handle,
3703 driver_name,
3704 architecture,
3706 access_mask, false,
3707 &hive_hnd,
3708 &key_hnd);
3710 } else {
3711 /* ok normal case */
3712 result = winreg_printer_opendriver(tmp_ctx,
3713 winreg_handle,
3714 driver_name,
3715 architecture,
3716 driver_version,
3717 access_mask, false,
3718 &hive_hnd,
3719 &key_hnd);
3721 if (!W_ERROR_IS_OK(result)) {
3722 DEBUG(5, ("winreg_get_driver: "
3723 "Could not open driver key (%s,%s,%d): %s\n",
3724 driver_name, architecture,
3725 driver_version, win_errstr(result)));
3726 goto done;
3729 result = winreg_printer_enumvalues(tmp_ctx,
3730 winreg_handle,
3731 &key_hnd,
3732 &num_values,
3733 &enum_values);
3734 if (!W_ERROR_IS_OK(result)) {
3735 DEBUG(0, ("winreg_get_driver: "
3736 "Could not enumerate values for (%s,%s,%d): %s\n",
3737 driver_name, architecture,
3738 driver_version, win_errstr(result)));
3739 goto done;
3742 info8 = talloc_zero(tmp_ctx, struct spoolss_DriverInfo8);
3743 if (info8 == NULL) {
3744 result = WERR_NOMEM;
3745 goto done;
3748 info8->driver_name = talloc_strdup(info8, driver_name);
3749 if (info8->driver_name == NULL) {
3750 result = WERR_NOMEM;
3751 goto done;
3754 info8->architecture = talloc_strdup(info8, architecture);
3755 if (info8->architecture == NULL) {
3756 result = WERR_NOMEM;
3757 goto done;
3760 result = WERR_OK;
3762 for (i = 0; i < num_values; i++) {
3763 const char *tmp_str;
3764 uint32_t tmp = 0;
3766 v = &enum_values[i];
3768 result = winreg_enumval_to_dword(info8, v,
3769 "Version",
3770 &tmp);
3771 if (NT_STATUS_IS_OK(result)) {
3772 info8->version = (enum spoolss_DriverOSVersion) tmp;
3774 CHECK_ERROR(result);
3776 result = winreg_enumval_to_sz(info8, v,
3777 "Driver",
3778 &info8->driver_path);
3779 CHECK_ERROR(result);
3781 result = winreg_enumval_to_sz(info8, v,
3782 "Data File",
3783 &info8->data_file);
3784 CHECK_ERROR(result);
3786 result = winreg_enumval_to_sz(info8, v,
3787 "Configuration File",
3788 &info8->config_file);
3789 CHECK_ERROR(result);
3791 result = winreg_enumval_to_sz(info8, v,
3792 "Help File",
3793 &info8->help_file);
3794 CHECK_ERROR(result);
3796 result = winreg_enumval_to_multi_sz(info8, v,
3797 "Dependent Files",
3798 &info8->dependent_files);
3799 CHECK_ERROR(result);
3801 result = winreg_enumval_to_sz(info8, v,
3802 "Monitor",
3803 &info8->monitor_name);
3804 CHECK_ERROR(result);
3806 result = winreg_enumval_to_sz(info8, v,
3807 "Datatype",
3808 &info8->default_datatype);
3809 CHECK_ERROR(result);
3811 result = winreg_enumval_to_multi_sz(info8, v,
3812 "Previous Names",
3813 &info8->previous_names);
3814 CHECK_ERROR(result);
3816 result = winreg_enumval_to_sz(info8, v,
3817 "DriverDate",
3818 &tmp_str);
3819 if (W_ERROR_IS_OK(result)) {
3820 result = winreg_printer_date_to_NTTIME(tmp_str,
3821 &info8->driver_date);
3823 CHECK_ERROR(result);
3825 result = winreg_enumval_to_sz(info8, v,
3826 "DriverVersion",
3827 &tmp_str);
3828 if (W_ERROR_IS_OK(result)) {
3829 result = winreg_printer_ver_to_dword(tmp_str,
3830 &info8->driver_version);
3832 CHECK_ERROR(result);
3834 result = winreg_enumval_to_sz(info8, v,
3835 "Manufacturer",
3836 &info8->manufacturer_name);
3837 CHECK_ERROR(result);
3839 result = winreg_enumval_to_sz(info8, v,
3840 "OEM URL",
3841 &info8->manufacturer_url);
3842 CHECK_ERROR(result);
3844 result = winreg_enumval_to_sz(info8, v,
3845 "HardwareID",
3846 &info8->hardware_id);
3847 CHECK_ERROR(result);
3849 result = winreg_enumval_to_sz(info8, v,
3850 "Provider",
3851 &info8->provider);
3852 CHECK_ERROR(result);
3854 result = winreg_enumval_to_sz(info8, v,
3855 "Print Processor",
3856 &info8->print_processor);
3857 CHECK_ERROR(result);
3859 result = winreg_enumval_to_sz(info8, v,
3860 "VendorSetup",
3861 &info8->vendor_setup);
3862 CHECK_ERROR(result);
3864 result = winreg_enumval_to_multi_sz(info8, v,
3865 "Color Profiles",
3866 &info8->color_profiles);
3867 CHECK_ERROR(result);
3869 result = winreg_enumval_to_sz(info8, v,
3870 "InfPath",
3871 &info8->inf_path);
3872 CHECK_ERROR(result);
3874 result = winreg_enumval_to_dword(info8, v,
3875 "PrinterDriverAttributes",
3876 &info8->printer_driver_attributes);
3877 CHECK_ERROR(result);
3879 result = winreg_enumval_to_multi_sz(info8, v,
3880 "CoreDependencies",
3881 &info8->core_driver_dependencies);
3882 CHECK_ERROR(result);
3884 result = winreg_enumval_to_sz(info8, v,
3885 "MinInboxDriverVerDate",
3886 &tmp_str);
3887 if (W_ERROR_IS_OK(result)) {
3888 result = winreg_printer_date_to_NTTIME(tmp_str,
3889 &info8->min_inbox_driver_ver_date);
3891 CHECK_ERROR(result);
3893 result = winreg_enumval_to_sz(info8, v,
3894 "MinInboxDriverVerVersion",
3895 &tmp_str);
3896 if (W_ERROR_IS_OK(result)) {
3897 result = winreg_printer_ver_to_dword(tmp_str,
3898 &info8->min_inbox_driver_ver_version);
3900 CHECK_ERROR(result);
3903 if (!W_ERROR_IS_OK(result)) {
3904 DEBUG(0, ("winreg_enumval_to_TYPE() failed "
3905 "for %s: %s\n", v->value_name,
3906 win_errstr(result)));
3907 goto done;
3910 *_info8 = talloc_steal(mem_ctx, info8);
3911 result = WERR_OK;
3912 done:
3913 if (winreg_handle != NULL) {
3914 WERROR ignore;
3916 if (is_valid_policy_hnd(&key_hnd)) {
3917 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3919 if (is_valid_policy_hnd(&hive_hnd)) {
3920 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3924 TALLOC_FREE(tmp_ctx);
3925 return result;
3928 WERROR winreg_del_driver(TALLOC_CTX *mem_ctx,
3929 struct dcerpc_binding_handle *winreg_handle,
3930 struct spoolss_DriverInfo8 *info8,
3931 uint32_t version)
3933 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3934 struct policy_handle hive_hnd, key_hnd;
3935 TALLOC_CTX *tmp_ctx;
3936 char *key_name;
3937 WERROR result;
3939 ZERO_STRUCT(hive_hnd);
3940 ZERO_STRUCT(key_hnd);
3942 tmp_ctx = talloc_stackframe();
3943 if (tmp_ctx == NULL) {
3944 return WERR_NOMEM;
3947 /* test that the key exists */
3948 result = winreg_printer_opendriver(tmp_ctx,
3949 winreg_handle,
3950 info8->driver_name,
3951 info8->architecture,
3952 version,
3953 access_mask, false,
3954 &hive_hnd,
3955 &key_hnd);
3956 if (!W_ERROR_IS_OK(result)) {
3957 /* key doesn't exist */
3958 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3959 result = WERR_OK;
3960 goto done;
3963 DEBUG(5, ("winreg_del_driver: "
3964 "Could not open driver (%s,%s,%u): %s\n",
3965 info8->driver_name, info8->architecture,
3966 version, win_errstr(result)));
3967 goto done;
3971 if (is_valid_policy_hnd(&key_hnd)) {
3972 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
3975 key_name = talloc_asprintf(tmp_ctx,
3976 "%s\\Environments\\%s\\Drivers\\Version-%u\\%s",
3977 TOP_LEVEL_CONTROL_KEY,
3978 info8->architecture, version,
3979 info8->driver_name);
3980 if (key_name == NULL) {
3981 result = WERR_NOMEM;
3982 goto done;
3985 result = winreg_printer_delete_subkeys(tmp_ctx,
3986 winreg_handle,
3987 &hive_hnd,
3988 access_mask,
3989 key_name);
3990 if (!W_ERROR_IS_OK(result)) {
3991 DEBUG(0, ("winreg_del_driver: "
3992 "Could not open driver (%s,%s,%u): %s\n",
3993 info8->driver_name, info8->architecture,
3994 version, win_errstr(result)));
3995 goto done;
3998 result = WERR_OK;
3999 done:
4000 if (winreg_handle != NULL) {
4001 WERROR ignore;
4003 if (is_valid_policy_hnd(&key_hnd)) {
4004 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
4006 if (is_valid_policy_hnd(&hive_hnd)) {
4007 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
4011 TALLOC_FREE(tmp_ctx);
4012 return result;
4015 WERROR winreg_get_driver_list(TALLOC_CTX *mem_ctx,
4016 struct dcerpc_binding_handle *winreg_handle,
4017 const char *architecture,
4018 uint32_t version,
4019 uint32_t *num_drivers,
4020 const char ***drivers_p)
4022 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
4023 struct policy_handle hive_hnd, key_hnd;
4024 const char **drivers;
4025 TALLOC_CTX *tmp_ctx;
4026 WERROR result;
4027 NTSTATUS status;
4029 *num_drivers = 0;
4030 *drivers_p = NULL;
4032 ZERO_STRUCT(hive_hnd);
4033 ZERO_STRUCT(key_hnd);
4035 tmp_ctx = talloc_stackframe();
4036 if (tmp_ctx == NULL) {
4037 return WERR_NOMEM;
4040 /* use NULL for the driver name so we open the key that is
4041 * parent of all drivers for this architecture and version */
4042 result = winreg_printer_opendriver(tmp_ctx,
4043 winreg_handle,
4044 NULL,
4045 architecture,
4046 version,
4047 access_mask, false,
4048 &hive_hnd,
4049 &key_hnd);
4050 if (!W_ERROR_IS_OK(result)) {
4051 DEBUG(5, ("winreg_get_driver_list: "
4052 "Could not open key (%s,%u): %s\n",
4053 architecture, version, win_errstr(result)));
4054 result = WERR_OK;
4055 goto done;
4058 status = dcerpc_winreg_enum_keys(tmp_ctx,
4059 winreg_handle,
4060 &key_hnd,
4061 num_drivers,
4062 &drivers,
4063 &result);
4064 if (!NT_STATUS_IS_OK(status)) {
4065 result = ntstatus_to_werror(status);
4067 if (!W_ERROR_IS_OK(result)) {
4068 DEBUG(0, ("winreg_get_driver_list: "
4069 "Could not enumerate drivers for (%s,%u): %s\n",
4070 architecture, version, win_errstr(result)));
4071 goto done;
4074 *drivers_p = talloc_steal(mem_ctx, drivers);
4076 result = WERR_OK;
4077 done:
4078 if (winreg_handle != NULL) {
4079 WERROR ignore;
4081 if (is_valid_policy_hnd(&key_hnd)) {
4082 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
4084 if (is_valid_policy_hnd(&hive_hnd)) {
4085 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
4089 TALLOC_FREE(tmp_ctx);
4090 return result;