lib/crypto: sync AES_cfb8_encrypt() from heimdal
[Samba.git] / source3 / rpc_client / cli_winreg_spoolss.c
blobac4fe864df439b3aaffef775fb9b414da3f149a1
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 result = ntstatus_to_werror(status);
301 if (!W_ERROR_IS_OK(result)) {
302 WERROR ignore;
304 if (is_valid_policy_hnd(hive_handle)) {
305 dcerpc_winreg_CloseKey(binding_handle,
306 mem_ctx,
307 hive_handle,
308 &ignore);
310 ZERO_STRUCTP(hive_handle);
312 return result;
315 return WERR_OK;
319 * @brief Create the registry keyname for the given printer.
321 * @param[in] mem_ctx The memory context to use.
323 * @param[in] printer The name of the printer to get the registry key.
325 * @return The registry key or NULL on error.
327 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
328 return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
331 static WERROR winreg_printer_opendriver(TALLOC_CTX *mem_ctx,
332 struct dcerpc_binding_handle *winreg_handle,
333 const char *drivername,
334 const char *architecture,
335 uint32_t version,
336 uint32_t access_mask,
337 bool create,
338 struct policy_handle *hive_hnd,
339 struct policy_handle *key_hnd)
341 WERROR result;
342 char *key_name;
344 key_name = talloc_asprintf(mem_ctx, "%s\\Environments\\%s\\Drivers\\Version-%u",
345 TOP_LEVEL_CONTROL_KEY,
346 architecture, version);
347 if (!key_name) {
348 return WERR_NOMEM;
351 result = winreg_printer_openkey(mem_ctx,
352 winreg_handle,
353 key_name,
354 drivername,
355 create,
356 access_mask,
357 hive_hnd,
358 key_hnd);
359 return result;
362 static WERROR winreg_enumval_to_dword(TALLOC_CTX *mem_ctx,
363 struct spoolss_PrinterEnumValues *v,
364 const char *valuename, uint32_t *dw)
366 /* just return if it is not the one we are looking for */
367 if (strcmp(valuename, v->value_name) != 0) {
368 return WERR_NOT_FOUND;
371 if (v->type != REG_DWORD) {
372 return WERR_INVALID_DATATYPE;
375 if (v->data_length != 4) {
376 *dw = 0;
377 return WERR_OK;
380 *dw = IVAL(v->data->data, 0);
381 return WERR_OK;
384 static WERROR winreg_enumval_to_sz(TALLOC_CTX *mem_ctx,
385 struct spoolss_PrinterEnumValues *v,
386 const char *valuename, const char **_str)
388 /* just return if it is not the one we are looking for */
389 if (strcmp(valuename, v->value_name) != 0) {
390 return WERR_NOT_FOUND;
393 if (v->type != REG_SZ) {
394 return WERR_INVALID_DATATYPE;
397 if (v->data_length == 0) {
398 *_str = talloc_strdup(mem_ctx, EMPTY_STRING);
399 if (*_str == NULL) {
400 return WERR_NOMEM;
402 return WERR_OK;
405 if (!pull_reg_sz(mem_ctx, v->data, _str)) {
406 return WERR_NOMEM;
409 return WERR_OK;
412 static WERROR winreg_enumval_to_multi_sz(TALLOC_CTX *mem_ctx,
413 struct spoolss_PrinterEnumValues *v,
414 const char *valuename,
415 const char ***array)
417 /* just return if it is not the one we are looking for */
418 if (strcmp(valuename, v->value_name) != 0) {
419 return WERR_NOT_FOUND;
422 if (v->type != REG_MULTI_SZ) {
423 return WERR_INVALID_DATATYPE;
426 if (v->data_length == 0) {
427 *array = talloc_array(mem_ctx, const char *, 1);
428 if (*array == NULL) {
429 return WERR_NOMEM;
431 *array[0] = NULL;
432 return WERR_OK;
435 if (!pull_reg_multi_sz(mem_ctx, v->data, array)) {
436 return WERR_NOMEM;
439 return WERR_OK;
442 static WERROR winreg_printer_write_date(TALLOC_CTX *mem_ctx,
443 struct dcerpc_binding_handle *winreg_handle,
444 struct policy_handle *key_handle,
445 const char *value,
446 NTTIME data)
448 struct winreg_String wvalue = { 0, };
449 DATA_BLOB blob;
450 WERROR result = WERR_OK;
451 NTSTATUS status;
452 const char *str;
453 struct tm *tm;
454 time_t t;
456 if (data == 0) {
457 str = talloc_strdup(mem_ctx, "01/01/1601");
458 } else {
459 t = nt_time_to_unix(data);
460 tm = localtime(&t);
461 if (tm == NULL) {
462 return map_werror_from_unix(errno);
464 str = talloc_asprintf(mem_ctx, "%02d/%02d/%04d",
465 tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
467 if (!str) {
468 return WERR_NOMEM;
471 wvalue.name = value;
472 if (!push_reg_sz(mem_ctx, &blob, str)) {
473 return WERR_NOMEM;
475 status = dcerpc_winreg_SetValue(winreg_handle,
476 mem_ctx,
477 key_handle,
478 wvalue,
479 REG_SZ,
480 blob.data,
481 blob.length,
482 &result);
483 if (!NT_STATUS_IS_OK(status)) {
484 result = ntstatus_to_werror(status);
486 if (!W_ERROR_IS_OK(result)) {
487 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
488 wvalue.name, win_errstr(result)));
491 return result;
494 static WERROR winreg_printer_date_to_NTTIME(const char *str, NTTIME *data)
496 struct tm tm;
497 time_t t;
499 if (strequal(str, "01/01/1601")) {
500 *data = 0;
501 return WERR_OK;
504 ZERO_STRUCT(tm);
506 if (sscanf(str, "%d/%d/%d",
507 &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
508 return WERR_INVALID_PARAMETER;
510 tm.tm_mon -= 1;
511 tm.tm_year -= 1900;
512 tm.tm_isdst = -1;
514 t = mktime(&tm);
515 unix_to_nt_time(data, t);
517 return WERR_OK;
520 static WERROR winreg_printer_write_ver(TALLOC_CTX *mem_ctx,
521 struct dcerpc_binding_handle *winreg_handle,
522 struct policy_handle *key_handle,
523 const char *value,
524 uint64_t data)
526 struct winreg_String wvalue = { 0, };
527 DATA_BLOB blob;
528 WERROR result = WERR_OK;
529 NTSTATUS status;
530 char *str;
532 /* FIXME: check format is right,
533 * this needs to be something like: 6.1.7600.16385 */
534 str = talloc_asprintf(mem_ctx, "%u.%u.%u.%u",
535 (unsigned)((data >> 48) & 0xFFFF),
536 (unsigned)((data >> 32) & 0xFFFF),
537 (unsigned)((data >> 16) & 0xFFFF),
538 (unsigned)(data & 0xFFFF));
539 if (!str) {
540 return WERR_NOMEM;
543 wvalue.name = value;
544 if (!push_reg_sz(mem_ctx, &blob, str)) {
545 return WERR_NOMEM;
547 status = dcerpc_winreg_SetValue(winreg_handle,
548 mem_ctx,
549 key_handle,
550 wvalue,
551 REG_SZ,
552 blob.data,
553 blob.length,
554 &result);
555 if (!NT_STATUS_IS_OK(status)) {
556 result = ntstatus_to_werror(status);
558 if (!W_ERROR_IS_OK(result)) {
559 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
560 wvalue.name, win_errstr(result)));
563 return result;
566 static WERROR winreg_printer_ver_to_dword(const char *str, uint64_t *data)
568 unsigned int v1, v2, v3, v4;
570 if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
571 return WERR_INVALID_PARAMETER;
574 *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
575 ((uint64_t)(v2 & 0xFFFF) << 32) +
576 ((uint64_t)(v3 & 0xFFFF) << 16) +
577 (uint64_t)(v2 & 0xFFFF);
579 return WERR_OK;
582 /********************************************************************
583 Public winreg function for spoolss
584 ********************************************************************/
586 WERROR winreg_create_printer(TALLOC_CTX *mem_ctx,
587 struct dcerpc_binding_handle *winreg_handle,
588 const char *sharename)
590 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
591 struct policy_handle hive_hnd, key_hnd;
592 struct spoolss_SetPrinterInfo2 *info2;
593 struct security_descriptor *secdesc;
594 struct winreg_String wkey, wkeyclass;
595 const char *path;
596 const char *subkeys[] = { SPOOL_DSDRIVER_KEY, SPOOL_DSSPOOLER_KEY, SPOOL_PRINTERDATA_KEY };
597 uint32_t i, count = ARRAY_SIZE(subkeys);
598 uint32_t info2_mask = 0;
599 WERROR result = WERR_OK;
600 WERROR ignore;
601 TALLOC_CTX *tmp_ctx;
603 tmp_ctx = talloc_stackframe();
604 if (tmp_ctx == NULL) {
605 return WERR_NOMEM;
608 path = winreg_printer_data_keyname(tmp_ctx, sharename);
609 if (path == NULL) {
610 TALLOC_FREE(tmp_ctx);
611 return WERR_NOMEM;
614 ZERO_STRUCT(hive_hnd);
615 ZERO_STRUCT(key_hnd);
617 result = winreg_printer_openkey(tmp_ctx,
618 winreg_handle,
619 path,
621 false,
622 access_mask,
623 &hive_hnd,
624 &key_hnd);
625 if (W_ERROR_IS_OK(result)) {
626 DEBUG(2, ("winreg_create_printer: Skipping, %s already exists\n", path));
627 goto done;
628 } else if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
629 DEBUG(2, ("winreg_create_printer: Creating default values in %s\n", path));
630 } else if (!W_ERROR_IS_OK(result)) {
631 DEBUG(0, ("winreg_create_printer: Could not open key %s: %s\n",
632 path, win_errstr(result)));
633 goto done;
636 if (is_valid_policy_hnd(&key_hnd)) {
637 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
639 if (is_valid_policy_hnd(&hive_hnd)) {
640 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
643 /* Create the main key */
644 result = winreg_printer_openkey(tmp_ctx,
645 winreg_handle,
646 path,
648 true,
649 access_mask,
650 &hive_hnd,
651 &key_hnd);
652 if (!W_ERROR_IS_OK(result)) {
653 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
654 path, win_errstr(result)));
655 goto done;
658 if (is_valid_policy_hnd(&key_hnd)) {
659 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
662 /* Create subkeys */
663 for (i = 0; i < count; i++) {
664 NTSTATUS status;
665 enum winreg_CreateAction action = REG_ACTION_NONE;
667 ZERO_STRUCT(key_hnd);
668 ZERO_STRUCT(wkey);
670 wkey.name = talloc_asprintf(tmp_ctx, "%s\\%s", path, subkeys[i]);
671 if (wkey.name == NULL) {
672 result = WERR_NOMEM;
673 goto done;
676 ZERO_STRUCT(wkeyclass);
677 wkeyclass.name = "";
679 status = dcerpc_winreg_CreateKey(winreg_handle,
680 tmp_ctx,
681 &hive_hnd,
682 wkey,
683 wkeyclass,
685 access_mask,
686 NULL,
687 &key_hnd,
688 &action,
689 &result);
690 if (!NT_STATUS_IS_OK(status)) {
691 result = ntstatus_to_werror(status);
693 if (!W_ERROR_IS_OK(result)) {
694 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
695 wkey.name, win_errstr(result)));
696 goto done;
699 if (strequal(subkeys[i], SPOOL_DSSPOOLER_KEY)) {
700 const char *dnssuffix;
701 const char *longname;
702 const char *uncname;
704 status = dcerpc_winreg_set_sz(tmp_ctx,
705 winreg_handle,
706 &key_hnd,
707 SPOOL_REG_PRINTERNAME,
708 sharename,
709 &result);
710 if (!NT_STATUS_IS_OK(status)) {
711 result = ntstatus_to_werror(status);
713 if (!W_ERROR_IS_OK(result)) {
714 goto done;
717 status = dcerpc_winreg_set_sz(tmp_ctx,
718 winreg_handle,
719 &key_hnd,
720 SPOOL_REG_SHORTSERVERNAME,
721 lp_netbios_name(),
722 &result);
723 if (!NT_STATUS_IS_OK(status)) {
724 result = ntstatus_to_werror(status);
726 if (!W_ERROR_IS_OK(result)) {
727 goto done;
730 /* We make the assumption that the netbios name
731 * is the same as the DNS name since the former
732 * will be what we used to join the domain
734 dnssuffix = get_mydnsdomname(tmp_ctx);
735 if (dnssuffix != NULL && dnssuffix[0] != '\0') {
736 longname = talloc_asprintf(tmp_ctx, "%s.%s", lp_netbios_name(), dnssuffix);
737 } else {
738 longname = talloc_strdup(tmp_ctx, lp_netbios_name());
740 if (longname == NULL) {
741 result = WERR_NOMEM;
742 goto done;
745 status = dcerpc_winreg_set_sz(tmp_ctx,
746 winreg_handle,
747 &key_hnd,
748 SPOOL_REG_SERVERNAME,
749 longname,
750 &result);
751 if (!NT_STATUS_IS_OK(status)) {
752 result = ntstatus_to_werror(status);
754 if (!W_ERROR_IS_OK(result)) {
755 goto done;
758 uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
759 longname, sharename);
760 if (uncname == NULL) {
761 result = WERR_NOMEM;
762 goto done;
765 status = dcerpc_winreg_set_sz(tmp_ctx,
766 winreg_handle,
767 &key_hnd,
768 SPOOL_REG_UNCNAME,
769 uncname,
770 &result);
771 if (!NT_STATUS_IS_OK(status)) {
772 result = ntstatus_to_werror(status);
774 if (!W_ERROR_IS_OK(result)) {
775 goto done;
778 status = dcerpc_winreg_set_dword(tmp_ctx,
779 winreg_handle,
780 &key_hnd,
781 SPOOL_REG_VERSIONNUMBER,
783 &result);
784 if (!NT_STATUS_IS_OK(status)) {
785 result = ntstatus_to_werror(status);
787 if (!W_ERROR_IS_OK(result)) {
788 goto done;
791 status = dcerpc_winreg_set_dword(tmp_ctx,
792 winreg_handle,
793 &key_hnd,
794 SPOOL_REG_PRINTSTARTTIME,
796 &result);
797 if (!NT_STATUS_IS_OK(status)) {
798 result = ntstatus_to_werror(status);
800 if (!W_ERROR_IS_OK(result)) {
801 goto done;
804 status = dcerpc_winreg_set_dword(tmp_ctx,
805 winreg_handle,
806 &key_hnd,
807 SPOOL_REG_PRINTENDTIME,
809 &result);
810 if (!NT_STATUS_IS_OK(status)) {
811 result = ntstatus_to_werror(status);
813 if (!W_ERROR_IS_OK(result)) {
814 goto done;
817 status = dcerpc_winreg_set_dword(tmp_ctx,
818 winreg_handle,
819 &key_hnd,
820 SPOOL_REG_PRIORITY,
822 &result);
823 if (!NT_STATUS_IS_OK(status)) {
824 result = ntstatus_to_werror(status);
826 if (!W_ERROR_IS_OK(result)) {
827 goto done;
830 status = dcerpc_winreg_set_dword(tmp_ctx,
831 winreg_handle,
832 &key_hnd,
833 SPOOL_REG_PRINTKEEPPRINTEDJOBS,
835 &result);
836 if (!NT_STATUS_IS_OK(status)) {
837 result = ntstatus_to_werror(status);
839 if (!W_ERROR_IS_OK(result)) {
840 goto done;
844 if (is_valid_policy_hnd(&key_hnd)) {
845 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
848 info2 = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfo2);
849 if (info2 == NULL) {
850 result = WERR_NOMEM;
851 goto done;
854 info2->printername = sharename;
855 if (info2->printername == NULL) {
856 result = WERR_NOMEM;
857 goto done;
859 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTERNAME;
861 info2->sharename = sharename;
862 info2_mask |= SPOOLSS_PRINTER_INFO_SHARENAME;
864 info2->portname = SAMBA_PRINTER_PORT_NAME;
865 info2_mask |= SPOOLSS_PRINTER_INFO_PORTNAME;
867 info2->printprocessor = "winprint";
868 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTPROCESSOR;
870 info2->datatype = "RAW";
871 info2_mask |= SPOOLSS_PRINTER_INFO_DATATYPE;
873 info2->comment = "";
874 info2_mask |= SPOOLSS_PRINTER_INFO_COMMENT;
876 info2->attributes = PRINTER_ATTRIBUTE_SAMBA;
877 info2_mask |= SPOOLSS_PRINTER_INFO_ATTRIBUTES;
879 info2->starttime = 0; /* Minutes since 12:00am GMT */
880 info2_mask |= SPOOLSS_PRINTER_INFO_STARTTIME;
882 info2->untiltime = 0; /* Minutes since 12:00am GMT */
883 info2_mask |= SPOOLSS_PRINTER_INFO_UNTILTIME;
885 info2->priority = 1;
886 info2_mask |= SPOOLSS_PRINTER_INFO_PRIORITY;
888 info2->defaultpriority = 1;
889 info2_mask |= SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY;
891 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
892 if (!W_ERROR_IS_OK(result)) {
893 goto done;
895 info2_mask |= SPOOLSS_PRINTER_INFO_SECDESC;
898 * Don't write a default Device Mode to the registry! The Device Mode is
899 * only written to disk with a SetPrinter level 2 or 8.
902 result = winreg_update_printer(tmp_ctx,
903 winreg_handle,
904 sharename,
905 info2_mask,
906 info2,
907 NULL,
908 secdesc);
910 done:
911 if (is_valid_policy_hnd(&key_hnd)) {
912 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
914 if (is_valid_policy_hnd(&hive_hnd)) {
915 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
918 talloc_free(tmp_ctx);
919 return result;
922 WERROR winreg_update_printer(TALLOC_CTX *mem_ctx,
923 struct dcerpc_binding_handle *winreg_handle,
924 const char *sharename,
925 uint32_t info2_mask,
926 struct spoolss_SetPrinterInfo2 *info2,
927 struct spoolss_DeviceMode *devmode,
928 struct security_descriptor *secdesc)
930 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
931 struct policy_handle hive_hnd, key_hnd;
932 int snum = lp_servicenumber(sharename);
933 enum ndr_err_code ndr_err;
934 DATA_BLOB blob;
935 char *path;
936 WERROR result = WERR_OK;
937 WERROR ignore;
938 NTSTATUS status;
939 TALLOC_CTX *tmp_ctx;
941 tmp_ctx = talloc_stackframe();
942 if (tmp_ctx == NULL) {
943 return WERR_NOMEM;
946 path = winreg_printer_data_keyname(tmp_ctx, sharename);
947 if (path == NULL) {
948 TALLOC_FREE(tmp_ctx);
949 return WERR_NOMEM;
952 ZERO_STRUCT(hive_hnd);
953 ZERO_STRUCT(key_hnd);
955 result = winreg_printer_openkey(tmp_ctx,
956 winreg_handle,
957 path,
959 true,
960 access_mask,
961 &hive_hnd,
962 &key_hnd);
963 if (!W_ERROR_IS_OK(result)) {
964 DEBUG(0, ("winreg_update_printer: Could not open key %s: %s\n",
965 path, win_errstr(result)));
966 goto done;
969 if (info2_mask & SPOOLSS_PRINTER_INFO_ATTRIBUTES) {
970 status = dcerpc_winreg_set_dword(tmp_ctx,
971 winreg_handle,
972 &key_hnd,
973 "Attributes",
974 info2->attributes,
975 &result);
976 if (!NT_STATUS_IS_OK(status)) {
977 result = ntstatus_to_werror(status);
979 if (!W_ERROR_IS_OK(result)) {
980 goto done;
984 #if 0
985 if (info2_mask & SPOOLSS_PRINTER_INFO_AVERAGEPPM) {
986 status = dcerpc_winreg_set_dword(tmp_ctx,
987 winreg_handle,
988 &key_hnd,
989 "AveragePpm",
990 info2->attributes,
991 &result);
992 if (!NT_STATUS_IS_OK(status)) {
993 result = ntstatus_to_werror(status);
995 if (!W_ERROR_IS_OK(result)) {
996 goto done;
999 #endif
1001 if (info2_mask & SPOOLSS_PRINTER_INFO_COMMENT) {
1002 status = dcerpc_winreg_set_sz(tmp_ctx,
1003 winreg_handle,
1004 &key_hnd,
1005 "Description",
1006 info2->comment,
1007 &result);
1008 if (!NT_STATUS_IS_OK(status)) {
1009 result = ntstatus_to_werror(status);
1011 if (!W_ERROR_IS_OK(result)) {
1012 goto done;
1016 if (info2_mask & SPOOLSS_PRINTER_INFO_DATATYPE) {
1017 status = dcerpc_winreg_set_sz(tmp_ctx,
1018 winreg_handle,
1019 &key_hnd,
1020 "Datatype",
1021 info2->datatype,
1022 &result);
1023 if (!NT_STATUS_IS_OK(status)) {
1024 result = ntstatus_to_werror(status);
1026 if (!W_ERROR_IS_OK(result)) {
1027 goto done;
1031 if (info2_mask & SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY) {
1032 status = dcerpc_winreg_set_dword(tmp_ctx,
1033 winreg_handle,
1034 &key_hnd,
1035 "Default Priority",
1036 info2->defaultpriority,
1037 &result);
1038 if (!NT_STATUS_IS_OK(status)) {
1039 result = ntstatus_to_werror(status);
1041 if (!W_ERROR_IS_OK(result)) {
1042 goto done;
1046 if (info2_mask & SPOOLSS_PRINTER_INFO_DEVMODE) {
1048 * Some client drivers freak out if there is a NULL devmode
1049 * (probably the driver is not checking before accessing
1050 * the devmode pointer) --jerry
1052 if (devmode == NULL && lp_default_devmode(snum) && info2 != NULL) {
1053 result = spoolss_create_default_devmode(tmp_ctx,
1054 info2->printername,
1055 &devmode);
1056 if (!W_ERROR_IS_OK(result)) {
1057 goto done;
1061 if (devmode->size != (ndr_size_spoolss_DeviceMode(devmode, 0) - devmode->__driverextra_length)) {
1062 result = WERR_INVALID_PARAM;
1063 goto done;
1066 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, devmode,
1067 (ndr_push_flags_fn_t) ndr_push_spoolss_DeviceMode);
1068 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1069 DEBUG(0, ("winreg_update_printer: Failed to marshall device mode\n"));
1070 result = WERR_NOMEM;
1071 goto done;
1074 status = dcerpc_winreg_set_binary(tmp_ctx,
1075 winreg_handle,
1076 &key_hnd,
1077 "Default DevMode",
1078 &blob,
1079 &result);
1080 if (!NT_STATUS_IS_OK(status)) {
1081 result = ntstatus_to_werror(status);
1083 if (!W_ERROR_IS_OK(result)) {
1084 goto done;
1088 if (info2_mask & SPOOLSS_PRINTER_INFO_DRIVERNAME) {
1089 status = dcerpc_winreg_set_sz(tmp_ctx,
1090 winreg_handle,
1091 &key_hnd,
1092 "Printer Driver",
1093 info2->drivername,
1094 &result);
1095 if (!NT_STATUS_IS_OK(status)) {
1096 result = ntstatus_to_werror(status);
1098 if (!W_ERROR_IS_OK(result)) {
1099 goto done;
1103 if (info2_mask & SPOOLSS_PRINTER_INFO_LOCATION) {
1104 status = dcerpc_winreg_set_sz(tmp_ctx,
1105 winreg_handle,
1106 &key_hnd,
1107 "Location",
1108 info2->location,
1109 &result);
1110 if (!NT_STATUS_IS_OK(status)) {
1111 result = ntstatus_to_werror(status);
1113 if (!W_ERROR_IS_OK(result)) {
1114 goto done;
1118 if (info2_mask & SPOOLSS_PRINTER_INFO_PARAMETERS) {
1119 status = dcerpc_winreg_set_sz(tmp_ctx,
1120 winreg_handle,
1121 &key_hnd,
1122 "Parameters",
1123 info2->parameters,
1124 &result);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 result = ntstatus_to_werror(status);
1128 if (!W_ERROR_IS_OK(result)) {
1129 goto done;
1133 if (info2_mask & SPOOLSS_PRINTER_INFO_PORTNAME) {
1134 status = dcerpc_winreg_set_sz(tmp_ctx,
1135 winreg_handle,
1136 &key_hnd,
1137 "Port",
1138 info2->portname,
1139 &result);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 result = ntstatus_to_werror(status);
1143 if (!W_ERROR_IS_OK(result)) {
1144 goto done;
1148 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTERNAME) {
1150 * in addprinter: no servername and the printer is the name
1151 * in setprinter: servername is \\server
1152 * and printer is \\server\\printer
1154 * Samba manages only local printers.
1155 * we currently don't support things like i
1156 * path=\\other_server\printer
1158 * We only store the printername, not \\server\printername
1160 const char *p = strrchr(info2->printername, '\\');
1161 if (p == NULL) {
1162 p = info2->printername;
1163 } else {
1164 p++;
1166 status = dcerpc_winreg_set_sz(tmp_ctx,
1167 winreg_handle,
1168 &key_hnd,
1169 "Name",
1171 &result);
1172 if (!NT_STATUS_IS_OK(status)) {
1173 result = ntstatus_to_werror(status);
1175 if (!W_ERROR_IS_OK(result)) {
1176 goto done;
1180 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTPROCESSOR) {
1181 status = dcerpc_winreg_set_sz(tmp_ctx,
1182 winreg_handle,
1183 &key_hnd,
1184 "Print Processor",
1185 info2->printprocessor,
1186 &result);
1187 if (!NT_STATUS_IS_OK(status)) {
1188 result = ntstatus_to_werror(status);
1190 if (!W_ERROR_IS_OK(result)) {
1191 goto done;
1195 if (info2_mask & SPOOLSS_PRINTER_INFO_PRIORITY) {
1196 status = dcerpc_winreg_set_dword(tmp_ctx,
1197 winreg_handle,
1198 &key_hnd,
1199 "Priority",
1200 info2->priority,
1201 &result);
1202 if (!NT_STATUS_IS_OK(status)) {
1203 result = ntstatus_to_werror(status);
1205 if (!W_ERROR_IS_OK(result)) {
1206 goto done;
1210 if (info2_mask & SPOOLSS_PRINTER_INFO_SECDESC) {
1212 * We need a security descriptor, if it isn't specified by
1213 * AddPrinter{Ex} then create a default descriptor.
1215 if (secdesc == NULL) {
1216 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1217 if (!W_ERROR_IS_OK(result)) {
1218 goto done;
1221 result = winreg_set_printer_secdesc(tmp_ctx,
1222 winreg_handle,
1223 sharename,
1224 secdesc);
1225 if (!W_ERROR_IS_OK(result)) {
1226 goto done;
1230 if (info2_mask & SPOOLSS_PRINTER_INFO_SEPFILE) {
1231 status = dcerpc_winreg_set_sz(tmp_ctx,
1232 winreg_handle,
1233 &key_hnd,
1234 "Separator File",
1235 info2->sepfile,
1236 &result);
1237 if (!NT_STATUS_IS_OK(status)) {
1238 result = ntstatus_to_werror(status);
1240 if (!W_ERROR_IS_OK(result)) {
1241 goto done;
1245 if (info2_mask & SPOOLSS_PRINTER_INFO_SHARENAME) {
1246 status = dcerpc_winreg_set_sz(tmp_ctx,
1247 winreg_handle,
1248 &key_hnd,
1249 "Share Name",
1250 info2->sharename,
1251 &result);
1252 if (!NT_STATUS_IS_OK(status)) {
1253 result = ntstatus_to_werror(status);
1255 if (!W_ERROR_IS_OK(result)) {
1256 goto done;
1260 if (info2_mask & SPOOLSS_PRINTER_INFO_STARTTIME) {
1261 status = dcerpc_winreg_set_dword(tmp_ctx,
1262 winreg_handle,
1263 &key_hnd,
1264 "StartTime",
1265 info2->starttime,
1266 &result);
1267 if (!NT_STATUS_IS_OK(status)) {
1268 result = ntstatus_to_werror(status);
1270 if (!W_ERROR_IS_OK(result)) {
1271 goto done;
1275 if (info2_mask & SPOOLSS_PRINTER_INFO_STATUS) {
1276 status = dcerpc_winreg_set_dword(tmp_ctx,
1277 winreg_handle,
1278 &key_hnd,
1279 "Status",
1280 info2->status,
1281 &result);
1282 if (!NT_STATUS_IS_OK(status)) {
1283 result = ntstatus_to_werror(status);
1285 if (!W_ERROR_IS_OK(result)) {
1286 goto done;
1290 if (info2_mask & SPOOLSS_PRINTER_INFO_UNTILTIME) {
1291 status = dcerpc_winreg_set_dword(tmp_ctx,
1292 winreg_handle,
1293 &key_hnd,
1294 "UntilTime",
1295 info2->untiltime,
1296 &result);
1297 if (!NT_STATUS_IS_OK(status)) {
1298 result = ntstatus_to_werror(status);
1300 if (!W_ERROR_IS_OK(result)) {
1301 goto done;
1305 status = dcerpc_winreg_set_dword(tmp_ctx,
1306 winreg_handle,
1307 &key_hnd,
1308 "ChangeID",
1309 winreg_printer_rev_changeid(),
1310 &result);
1311 if (!NT_STATUS_IS_OK(status)) {
1312 result = ntstatus_to_werror(status);
1314 if (!W_ERROR_IS_OK(result)) {
1315 goto done;
1318 result = WERR_OK;
1319 done:
1320 if (is_valid_policy_hnd(&key_hnd)) {
1321 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1323 if (is_valid_policy_hnd(&hive_hnd)) {
1324 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1327 TALLOC_FREE(tmp_ctx);
1328 return result;
1331 WERROR winreg_get_printer(TALLOC_CTX *mem_ctx,
1332 struct dcerpc_binding_handle *winreg_handle,
1333 const char *printer,
1334 struct spoolss_PrinterInfo2 **pinfo2)
1336 struct spoolss_PrinterInfo2 *info2;
1337 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1338 struct policy_handle hive_hnd, key_hnd;
1339 struct spoolss_PrinterEnumValues enum_value;
1340 struct spoolss_PrinterEnumValues *v = NULL;
1341 enum ndr_err_code ndr_err;
1342 DATA_BLOB blob;
1343 int snum = lp_servicenumber(printer);
1344 uint32_t num_values = 0;
1345 uint32_t i;
1346 char *path;
1347 NTSTATUS status;
1348 WERROR result = WERR_OK;
1349 WERROR ignore;
1350 const char **enum_names = NULL;
1351 enum winreg_Type *enum_types = NULL;
1352 DATA_BLOB *enum_data_blobs = NULL;
1353 TALLOC_CTX *tmp_ctx;
1355 tmp_ctx = talloc_stackframe();
1356 if (tmp_ctx == NULL) {
1357 return WERR_NOMEM;
1360 path = winreg_printer_data_keyname(tmp_ctx, printer);
1361 if (path == NULL) {
1362 TALLOC_FREE(tmp_ctx);
1363 return WERR_NOMEM;
1366 result = winreg_printer_openkey(tmp_ctx,
1367 winreg_handle,
1368 path,
1370 false,
1371 access_mask,
1372 &hive_hnd,
1373 &key_hnd);
1374 if (!W_ERROR_IS_OK(result)) {
1375 DEBUG(2, ("winreg_get_printer: Could not open key %s: %s\n",
1376 path, win_errstr(result)));
1377 goto done;
1380 status = dcerpc_winreg_enumvals(tmp_ctx,
1381 winreg_handle,
1382 &key_hnd,
1383 &num_values,
1384 &enum_names,
1385 &enum_types,
1386 &enum_data_blobs,
1387 &result);
1388 if (!NT_STATUS_IS_OK(status)){
1389 result = ntstatus_to_werror(status);
1392 if (!W_ERROR_IS_OK(result)) {
1393 DEBUG(0, ("winreg_get_printer: Could not enumerate values in %s: %s\n",
1394 path, win_errstr(result)));
1395 goto done;
1398 info2 = talloc_zero(tmp_ctx, struct spoolss_PrinterInfo2);
1399 if (info2 == NULL) {
1400 result = WERR_NOMEM;
1401 goto done;
1404 FILL_STRING(info2, EMPTY_STRING, info2->servername);
1405 FILL_STRING(info2, EMPTY_STRING, info2->printername);
1406 FILL_STRING(info2, EMPTY_STRING, info2->sharename);
1407 FILL_STRING(info2, EMPTY_STRING, info2->portname);
1408 FILL_STRING(info2, EMPTY_STRING, info2->drivername);
1409 FILL_STRING(info2, EMPTY_STRING, info2->comment);
1410 FILL_STRING(info2, EMPTY_STRING, info2->location);
1411 FILL_STRING(info2, EMPTY_STRING, info2->sepfile);
1412 FILL_STRING(info2, EMPTY_STRING, info2->printprocessor);
1413 FILL_STRING(info2, EMPTY_STRING, info2->datatype);
1414 FILL_STRING(info2, EMPTY_STRING, info2->parameters);
1416 for (i = 0; i < num_values; i++) {
1417 enum_value.value_name = enum_names[i];
1418 enum_value.value_name_len = 2*strlen_m_term(enum_names[i]);
1419 enum_value.type = enum_types[i];
1420 enum_value.data_length = enum_data_blobs[i].length;
1421 enum_value.data = NULL;
1422 if (enum_value.data_length != 0){
1423 enum_value.data = &enum_data_blobs[i];
1425 v = &enum_value;
1427 result = winreg_enumval_to_sz(info2,
1429 "Name",
1430 &info2->printername);
1431 CHECK_ERROR(result);
1433 result = winreg_enumval_to_sz(info2,
1435 "Share Name",
1436 &info2->sharename);
1437 CHECK_ERROR(result);
1439 result = winreg_enumval_to_sz(info2,
1441 "Port",
1442 &info2->portname);
1443 CHECK_ERROR(result);
1445 result = winreg_enumval_to_sz(info2,
1447 "Description",
1448 &info2->comment);
1449 CHECK_ERROR(result);
1451 result = winreg_enumval_to_sz(info2,
1453 "Location",
1454 &info2->location);
1455 CHECK_ERROR(result);
1457 result = winreg_enumval_to_sz(info2,
1459 "Separator File",
1460 &info2->sepfile);
1461 CHECK_ERROR(result);
1463 result = winreg_enumval_to_sz(info2,
1465 "Print Processor",
1466 &info2->printprocessor);
1467 CHECK_ERROR(result);
1469 result = winreg_enumval_to_sz(info2,
1471 "Datatype",
1472 &info2->datatype);
1473 CHECK_ERROR(result);
1475 result = winreg_enumval_to_sz(info2,
1477 "Parameters",
1478 &info2->parameters);
1479 CHECK_ERROR(result);
1481 result = winreg_enumval_to_sz(info2,
1483 "Printer Driver",
1484 &info2->drivername);
1485 CHECK_ERROR(result);
1487 result = winreg_enumval_to_dword(info2,
1489 "Attributes",
1490 &info2->attributes);
1491 CHECK_ERROR(result);
1493 result = winreg_enumval_to_dword(info2,
1495 "Priority",
1496 &info2->priority);
1497 CHECK_ERROR(result);
1499 result = winreg_enumval_to_dword(info2,
1501 "Default Priority",
1502 &info2->defaultpriority);
1503 CHECK_ERROR(result);
1505 result = winreg_enumval_to_dword(info2,
1507 "StartTime",
1508 &info2->starttime);
1509 CHECK_ERROR(result);
1511 result = winreg_enumval_to_dword(info2,
1513 "UntilTime",
1514 &info2->untiltime);
1515 CHECK_ERROR(result);
1517 result = winreg_enumval_to_dword(info2,
1519 "Status",
1520 &info2->status);
1521 CHECK_ERROR(result);
1523 result = winreg_enumval_to_dword(info2,
1525 "StartTime",
1526 &info2->starttime);
1527 CHECK_ERROR(result);
1530 if (!W_ERROR_IS_OK(result)) {
1531 DEBUG(0, ("winreg_get_printer: winreg_enumval_to_TYPE() failed "
1532 "for %s: %s\n",
1533 v->value_name,
1534 win_errstr(result)));
1535 goto done;
1538 /* Construct the Device Mode */
1539 status = dcerpc_winreg_query_binary(tmp_ctx,
1540 winreg_handle,
1541 &key_hnd,
1542 "Default DevMode",
1543 &blob,
1544 &result);
1545 if (!NT_STATUS_IS_OK(status)) {
1546 result = ntstatus_to_werror(status);
1548 if (W_ERROR_IS_OK(result)) {
1549 info2->devmode = talloc_zero(info2, struct spoolss_DeviceMode);
1550 if (info2->devmode == NULL) {
1551 result = WERR_NOMEM;
1552 goto done;
1554 ndr_err = ndr_pull_struct_blob(&blob,
1555 info2->devmode,
1556 info2->devmode,
1557 (ndr_pull_flags_fn_t) ndr_pull_spoolss_DeviceMode);
1558 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1559 DEBUG(0, ("winreg_get_printer: Failed to unmarshall device mode\n"));
1560 result = WERR_NOMEM;
1561 goto done;
1565 if (info2->devmode == NULL && lp_default_devmode(snum)) {
1566 result = spoolss_create_default_devmode(info2,
1567 info2->printername,
1568 &info2->devmode);
1569 if (!W_ERROR_IS_OK(result)) {
1570 goto done;
1574 if (info2->devmode) {
1575 info2->devmode->size = ndr_size_spoolss_DeviceMode(info2->devmode, 0) - info2->devmode->driverextra_data.length;
1578 result = winreg_get_printer_secdesc(info2,
1579 winreg_handle,
1580 printer,
1581 &info2->secdesc);
1582 if (!W_ERROR_IS_OK(result)) {
1583 goto done;
1586 /* Fix for OS/2 drivers. */
1587 if (get_remote_arch() == RA_OS2) {
1588 spoolss_map_to_os2_driver(info2, &info2->drivername);
1591 if (pinfo2) {
1592 *pinfo2 = talloc_move(mem_ctx, &info2);
1595 result = WERR_OK;
1596 done:
1597 if (is_valid_policy_hnd(&key_hnd)) {
1598 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1600 if (is_valid_policy_hnd(&hive_hnd)) {
1601 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1604 TALLOC_FREE(tmp_ctx);
1605 return result;
1608 WERROR winreg_get_printer_secdesc(TALLOC_CTX *mem_ctx,
1609 struct dcerpc_binding_handle *winreg_handle,
1610 const char *sharename,
1611 struct spoolss_security_descriptor **psecdesc)
1613 struct spoolss_security_descriptor *secdesc;
1614 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1615 struct policy_handle hive_hnd, key_hnd;
1616 const char *path;
1617 TALLOC_CTX *tmp_ctx;
1618 NTSTATUS status;
1619 WERROR result;
1620 WERROR ignore;
1622 tmp_ctx = talloc_stackframe();
1623 if (tmp_ctx == NULL) {
1624 return WERR_NOMEM;
1627 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1628 if (path == NULL) {
1629 talloc_free(tmp_ctx);
1630 return WERR_NOMEM;
1633 ZERO_STRUCT(hive_hnd);
1634 ZERO_STRUCT(key_hnd);
1636 result = winreg_printer_openkey(tmp_ctx,
1637 winreg_handle,
1638 path,
1640 false,
1641 access_mask,
1642 &hive_hnd,
1643 &key_hnd);
1644 if (!W_ERROR_IS_OK(result)) {
1645 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1646 goto create_default;
1648 goto done;
1651 status = dcerpc_winreg_query_sd(tmp_ctx,
1652 winreg_handle,
1653 &key_hnd,
1654 "Security",
1655 &secdesc,
1656 &result);
1657 if (!NT_STATUS_IS_OK(status)) {
1658 result = ntstatus_to_werror(status);
1660 if (!W_ERROR_IS_OK(result)) {
1661 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1663 if (is_valid_policy_hnd(&key_hnd)) {
1664 dcerpc_winreg_CloseKey(winreg_handle,
1665 tmp_ctx,
1666 &key_hnd,
1667 &ignore);
1670 if (is_valid_policy_hnd(&hive_hnd)) {
1671 dcerpc_winreg_CloseKey(winreg_handle,
1672 tmp_ctx,
1673 &hive_hnd,
1674 &ignore);
1676 goto create_default;
1678 goto done;
1681 if (psecdesc) {
1682 *psecdesc = talloc_move(mem_ctx, &secdesc);
1685 result = WERR_OK;
1686 goto done;
1688 create_default:
1689 result = winreg_printer_openkey(tmp_ctx,
1690 winreg_handle,
1691 path,
1693 true,
1694 access_mask,
1695 &hive_hnd,
1696 &key_hnd);
1697 if (!W_ERROR_IS_OK(result)) {
1698 goto done;
1701 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1702 if (!W_ERROR_IS_OK(result)) {
1703 goto done;
1706 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
1707 this security descriptor has been created when winbindd was
1708 down. Take ownership of security descriptor. */
1709 if (dom_sid_equal(secdesc->owner_sid, &global_sid_World)) {
1710 struct dom_sid owner_sid;
1712 /* Change sd owner to workgroup administrator */
1714 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
1715 struct spoolss_security_descriptor *new_secdesc;
1716 size_t size;
1718 /* Create new sd */
1719 sid_append_rid(&owner_sid, DOMAIN_RID_ADMINISTRATOR);
1721 new_secdesc = make_sec_desc(tmp_ctx,
1722 secdesc->revision,
1723 secdesc->type,
1724 &owner_sid,
1725 secdesc->group_sid,
1726 secdesc->sacl,
1727 secdesc->dacl,
1728 &size);
1730 if (new_secdesc == NULL) {
1731 result = WERR_NOMEM;
1732 goto done;
1735 /* Swap with other one */
1736 secdesc = new_secdesc;
1740 status = dcerpc_winreg_set_sd(tmp_ctx,
1741 winreg_handle,
1742 &key_hnd,
1743 "Security",
1744 secdesc,
1745 &result);
1746 if (!NT_STATUS_IS_OK(status)) {
1747 result = ntstatus_to_werror(status);
1749 if (!W_ERROR_IS_OK(result)) {
1750 return result;
1753 if (psecdesc) {
1754 *psecdesc = talloc_move(mem_ctx, &secdesc);
1757 result = WERR_OK;
1758 done:
1759 if (is_valid_policy_hnd(&key_hnd)) {
1760 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1762 if (is_valid_policy_hnd(&hive_hnd)) {
1763 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1766 talloc_free(tmp_ctx);
1767 return result;
1770 WERROR winreg_set_printer_secdesc(TALLOC_CTX *mem_ctx,
1771 struct dcerpc_binding_handle *winreg_handle,
1772 const char *sharename,
1773 const struct spoolss_security_descriptor *secdesc)
1775 const struct spoolss_security_descriptor *new_secdesc = secdesc;
1776 struct spoolss_security_descriptor *old_secdesc;
1777 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1778 struct policy_handle hive_hnd, key_hnd;
1779 const char *path;
1780 TALLOC_CTX *tmp_ctx;
1781 NTSTATUS status;
1782 WERROR result;
1783 WERROR ignore;
1785 tmp_ctx = talloc_stackframe();
1786 if (tmp_ctx == NULL) {
1787 return WERR_NOMEM;
1790 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1791 if (path == NULL) {
1792 talloc_free(tmp_ctx);
1793 return WERR_NOMEM;
1797 * The old owner and group sids of the security descriptor are not
1798 * present when new ACEs are added or removed by changing printer
1799 * permissions through NT. If they are NULL in the new security
1800 * descriptor then copy them over from the old one.
1802 if (!secdesc->owner_sid || !secdesc->group_sid) {
1803 struct dom_sid *owner_sid, *group_sid;
1804 struct security_acl *dacl, *sacl;
1805 size_t size;
1807 result = winreg_get_printer_secdesc(tmp_ctx,
1808 winreg_handle,
1809 sharename,
1810 &old_secdesc);
1811 if (!W_ERROR_IS_OK(result)) {
1812 talloc_free(tmp_ctx);
1813 return result;
1816 /* Pick out correct owner and group sids */
1817 owner_sid = secdesc->owner_sid ?
1818 secdesc->owner_sid :
1819 old_secdesc->owner_sid;
1821 group_sid = secdesc->group_sid ?
1822 secdesc->group_sid :
1823 old_secdesc->group_sid;
1825 dacl = secdesc->dacl ?
1826 secdesc->dacl :
1827 old_secdesc->dacl;
1829 sacl = secdesc->sacl ?
1830 secdesc->sacl :
1831 old_secdesc->sacl;
1833 /* Make a deep copy of the security descriptor */
1834 new_secdesc = make_sec_desc(tmp_ctx,
1835 secdesc->revision,
1836 secdesc->type,
1837 owner_sid,
1838 group_sid,
1839 sacl,
1840 dacl,
1841 &size);
1842 if (new_secdesc == NULL) {
1843 talloc_free(tmp_ctx);
1844 return WERR_NOMEM;
1848 ZERO_STRUCT(hive_hnd);
1849 ZERO_STRUCT(key_hnd);
1851 result = winreg_printer_openkey(tmp_ctx,
1852 winreg_handle,
1853 path,
1855 false,
1856 access_mask,
1857 &hive_hnd,
1858 &key_hnd);
1859 if (!W_ERROR_IS_OK(result)) {
1860 goto done;
1863 status = dcerpc_winreg_set_sd(tmp_ctx,
1864 winreg_handle,
1865 &key_hnd,
1866 "Security",
1867 new_secdesc,
1868 &result);
1869 if (!NT_STATUS_IS_OK(status)) {
1870 result = ntstatus_to_werror(status);
1873 done:
1874 if (is_valid_policy_hnd(&key_hnd)) {
1875 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1877 if (is_valid_policy_hnd(&hive_hnd)) {
1878 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1881 talloc_free(tmp_ctx);
1882 return result;
1885 /* Set printer data over the winreg pipe. */
1886 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
1887 struct dcerpc_binding_handle *winreg_handle,
1888 const char *printer,
1889 const char *key,
1890 const char *value,
1891 enum winreg_Type type,
1892 uint8_t *data,
1893 uint32_t data_size)
1895 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1896 struct policy_handle hive_hnd, key_hnd;
1897 struct winreg_String wvalue = { 0, };
1898 char *path;
1899 WERROR result = WERR_OK;
1900 WERROR ignore;
1901 NTSTATUS status;
1902 TALLOC_CTX *tmp_ctx;
1904 tmp_ctx = talloc_stackframe();
1905 if (tmp_ctx == NULL) {
1906 return WERR_NOMEM;
1909 path = winreg_printer_data_keyname(tmp_ctx, printer);
1910 if (path == NULL) {
1911 TALLOC_FREE(tmp_ctx);
1912 return WERR_NOMEM;
1915 ZERO_STRUCT(hive_hnd);
1916 ZERO_STRUCT(key_hnd);
1918 DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
1919 key, value, access_mask, printer));
1920 result = winreg_printer_openkey(tmp_ctx,
1921 winreg_handle,
1922 path,
1923 key,
1924 true,
1925 access_mask,
1926 &hive_hnd,
1927 &key_hnd);
1928 if (!W_ERROR_IS_OK(result)) {
1929 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
1930 key, win_errstr(result)));
1931 goto done;
1934 wvalue.name = value;
1935 status = dcerpc_winreg_SetValue(winreg_handle,
1936 tmp_ctx,
1937 &key_hnd,
1938 wvalue,
1939 type,
1940 data,
1941 data_size,
1942 &result);
1943 if (!NT_STATUS_IS_OK(status)) {
1944 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
1945 value, nt_errstr(status)));
1946 result = ntstatus_to_werror(status);
1949 done:
1950 if (is_valid_policy_hnd(&key_hnd)) {
1951 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1953 if (is_valid_policy_hnd(&hive_hnd)) {
1954 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1957 TALLOC_FREE(tmp_ctx);
1958 return result;
1961 /* Get printer data over a winreg pipe. */
1962 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
1963 struct dcerpc_binding_handle *winreg_handle,
1964 const char *printer,
1965 const char *key,
1966 const char *value,
1967 enum winreg_Type *type,
1968 uint8_t **data,
1969 uint32_t *data_size)
1971 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1972 struct policy_handle hive_hnd, key_hnd;
1973 struct winreg_String wvalue;
1974 enum winreg_Type type_in = REG_NONE;
1975 char *path;
1976 uint8_t *data_in = NULL;
1977 uint32_t data_in_size = 0;
1978 uint32_t value_len = 0;
1979 WERROR result = WERR_OK;
1980 WERROR ignore;
1981 NTSTATUS status;
1982 TALLOC_CTX *tmp_ctx;
1984 tmp_ctx = talloc_stackframe();
1985 if (tmp_ctx == NULL) {
1986 return WERR_NOMEM;
1989 path = winreg_printer_data_keyname(tmp_ctx, printer);
1990 if (path == NULL) {
1991 TALLOC_FREE(tmp_ctx);
1992 return WERR_NOMEM;
1995 ZERO_STRUCT(hive_hnd);
1996 ZERO_STRUCT(key_hnd);
1998 result = winreg_printer_openkey(tmp_ctx,
1999 winreg_handle,
2000 path,
2001 key,
2002 false,
2003 access_mask,
2004 &hive_hnd,
2005 &key_hnd);
2006 if (!W_ERROR_IS_OK(result)) {
2007 DEBUG(2, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
2008 key, win_errstr(result)));
2009 goto done;
2012 wvalue.name = value;
2015 * call QueryValue once with data == NULL to get the
2016 * needed memory size to be allocated, then allocate
2017 * data buffer and call again.
2019 status = dcerpc_winreg_QueryValue(winreg_handle,
2020 tmp_ctx,
2021 &key_hnd,
2022 &wvalue,
2023 &type_in,
2024 NULL,
2025 &data_in_size,
2026 &value_len,
2027 &result);
2028 if (!NT_STATUS_IS_OK(status)) {
2029 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2030 value, nt_errstr(status)));
2031 result = ntstatus_to_werror(status);
2032 goto done;
2034 if (!W_ERROR_IS_OK(result)) {
2035 DEBUG(2, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2036 value, win_errstr(result)));
2037 goto done;
2040 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2041 if (data_in == NULL) {
2042 result = WERR_NOMEM;
2043 goto done;
2045 value_len = 0;
2047 status = dcerpc_winreg_QueryValue(winreg_handle,
2048 tmp_ctx,
2049 &key_hnd,
2050 &wvalue,
2051 &type_in,
2052 data_in,
2053 &data_in_size,
2054 &value_len,
2055 &result);
2056 if (!NT_STATUS_IS_OK(status)) {
2057 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2058 value, nt_errstr(status)));
2059 result = ntstatus_to_werror(status);
2060 goto done;
2062 if (!W_ERROR_IS_OK(result)) {
2063 DEBUG(2, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2064 value, win_errstr(result)));
2065 goto done;
2068 *type = type_in;
2069 *data_size = data_in_size;
2070 if (data_in_size) {
2071 *data = talloc_move(mem_ctx, &data_in);
2074 result = WERR_OK;
2075 done:
2076 if (is_valid_policy_hnd(&key_hnd)) {
2077 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2079 if (is_valid_policy_hnd(&hive_hnd)) {
2080 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2083 TALLOC_FREE(tmp_ctx);
2084 return result;
2087 /* Enumerate on the values of a given key and provide the data. */
2088 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
2089 struct dcerpc_binding_handle *winreg_handle,
2090 const char *printer,
2091 const char *key,
2092 uint32_t *pnum_values,
2093 struct spoolss_PrinterEnumValues **penum_values)
2095 uint32_t i;
2096 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2097 struct policy_handle hive_hnd, key_hnd;
2099 struct spoolss_PrinterEnumValues *enum_values = NULL;
2100 uint32_t num_values = 0;
2101 char *path;
2102 WERROR result = WERR_OK;
2103 WERROR ignore;
2104 NTSTATUS status;
2105 const char **enum_names = NULL;
2106 enum winreg_Type *enum_types = NULL;
2107 DATA_BLOB *enum_data_blobs = NULL;
2109 TALLOC_CTX *tmp_ctx;
2111 tmp_ctx = talloc_stackframe();
2112 if (tmp_ctx == NULL) {
2113 return WERR_NOMEM;
2116 path = winreg_printer_data_keyname(tmp_ctx, printer);
2117 if (path == NULL) {
2118 TALLOC_FREE(tmp_ctx);
2119 return WERR_NOMEM;
2122 result = winreg_printer_openkey(tmp_ctx,
2123 winreg_handle,
2124 path,
2125 key,
2126 false,
2127 access_mask,
2128 &hive_hnd,
2129 &key_hnd);
2130 if (!W_ERROR_IS_OK(result)) {
2131 DEBUG(2, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
2132 key, win_errstr(result)));
2133 goto done;
2136 status = dcerpc_winreg_enumvals(tmp_ctx,
2137 winreg_handle,
2138 &key_hnd,
2139 &num_values,
2140 &enum_names,
2141 &enum_types,
2142 &enum_data_blobs,
2143 &result);
2144 if (!NT_STATUS_IS_OK(status)){
2145 result = ntstatus_to_werror(status);
2148 if (!W_ERROR_IS_OK(result)) {
2149 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2150 key, win_errstr(result)));
2151 goto done;
2154 enum_values = talloc_array(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
2155 if (enum_values == NULL){
2156 result = WERR_NOMEM;
2157 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2158 key, win_errstr(result)));
2159 goto done;
2162 for (i = 0; i < num_values; i++){
2163 enum_values[i].value_name = enum_names[i];
2164 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
2165 enum_values[i].type = enum_types[i];
2166 enum_values[i].data_length = enum_data_blobs[i].length;
2167 enum_values[i].data = NULL;
2169 if (enum_values[i].data_length != 0){
2170 enum_values[i].data = &enum_data_blobs[i];
2174 talloc_steal(enum_values, enum_names);
2175 talloc_steal(enum_values, enum_data_blobs);
2177 *pnum_values = num_values;
2178 if (penum_values) {
2179 *penum_values = talloc_move(mem_ctx, &enum_values);
2182 result = WERR_OK;
2183 done:
2184 if (is_valid_policy_hnd(&key_hnd)) {
2185 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2187 if (is_valid_policy_hnd(&hive_hnd)) {
2188 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2191 TALLOC_FREE(tmp_ctx);
2192 return result;
2195 /* Delete printer data over a winreg pipe. */
2196 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
2197 struct dcerpc_binding_handle *winreg_handle,
2198 const char *printer,
2199 const char *key,
2200 const char *value)
2202 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2203 struct policy_handle hive_hnd, key_hnd;
2204 struct winreg_String wvalue = { 0, };
2205 char *path;
2206 WERROR result = WERR_OK;
2207 WERROR ignore;
2208 NTSTATUS status;
2210 TALLOC_CTX *tmp_ctx;
2212 tmp_ctx = talloc_stackframe();
2213 if (tmp_ctx == NULL) {
2214 return WERR_NOMEM;
2217 path = winreg_printer_data_keyname(tmp_ctx, printer);
2218 if (path == NULL) {
2219 TALLOC_FREE(tmp_ctx);
2220 return WERR_NOMEM;
2223 ZERO_STRUCT(hive_hnd);
2224 ZERO_STRUCT(key_hnd);
2226 result = winreg_printer_openkey(tmp_ctx,
2227 winreg_handle,
2228 path,
2229 key,
2230 false,
2231 access_mask,
2232 &hive_hnd,
2233 &key_hnd);
2234 if (!W_ERROR_IS_OK(result)) {
2235 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
2236 key, win_errstr(result)));
2237 goto done;
2240 wvalue.name = value;
2241 status = dcerpc_winreg_DeleteValue(winreg_handle,
2242 tmp_ctx,
2243 &key_hnd,
2244 wvalue,
2245 &result);
2246 if (!NT_STATUS_IS_OK(status)) {
2247 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
2248 value, nt_errstr(status)));
2249 result = ntstatus_to_werror(status);
2252 done:
2253 if (is_valid_policy_hnd(&key_hnd)) {
2254 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2256 if (is_valid_policy_hnd(&hive_hnd)) {
2257 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2260 TALLOC_FREE(tmp_ctx);
2261 return result;
2264 /* Enumerate on the subkeys of a given key and provide the data. */
2265 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
2266 struct dcerpc_binding_handle *winreg_handle,
2267 const char *printer,
2268 const char *key,
2269 uint32_t *pnum_subkeys,
2270 const char ***psubkeys)
2272 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2273 struct policy_handle hive_hnd, key_hnd;
2274 char *path;
2275 const char **subkeys = NULL;
2276 uint32_t num_subkeys = -1;
2278 WERROR result = WERR_OK;
2279 WERROR ignore;
2280 NTSTATUS status;
2282 TALLOC_CTX *tmp_ctx;
2284 tmp_ctx = talloc_stackframe();
2285 if (tmp_ctx == NULL) {
2286 return WERR_NOMEM;
2289 path = winreg_printer_data_keyname(tmp_ctx, printer);
2290 if (path == NULL) {
2291 TALLOC_FREE(tmp_ctx);
2292 return WERR_NOMEM;
2295 ZERO_STRUCT(hive_hnd);
2296 ZERO_STRUCT(key_hnd);
2298 result = winreg_printer_openkey(tmp_ctx,
2299 winreg_handle,
2300 path,
2301 key,
2302 false,
2303 access_mask,
2304 &hive_hnd,
2305 &key_hnd);
2306 if (!W_ERROR_IS_OK(result)) {
2307 DEBUG(2, ("winreg_enum_printer_key: Could not open key %s: %s\n",
2308 key, win_errstr(result)));
2309 goto done;
2312 status = dcerpc_winreg_enum_keys(tmp_ctx,
2313 winreg_handle,
2314 &key_hnd,
2315 &num_subkeys,
2316 &subkeys,
2317 &result);
2318 if (!NT_STATUS_IS_OK(status)) {
2319 result = ntstatus_to_werror(status);
2321 if (!W_ERROR_IS_OK(result)) {
2322 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
2323 key, win_errstr(result)));
2324 goto done;
2327 *pnum_subkeys = num_subkeys;
2328 if (psubkeys) {
2329 *psubkeys = talloc_move(mem_ctx, &subkeys);
2332 result = WERR_OK;
2333 done:
2334 if (is_valid_policy_hnd(&key_hnd)) {
2335 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2337 if (is_valid_policy_hnd(&hive_hnd)) {
2338 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2341 TALLOC_FREE(tmp_ctx);
2342 return result;
2345 /* Delete a key with subkeys of a given printer. */
2346 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
2347 struct dcerpc_binding_handle *winreg_handle,
2348 const char *printer,
2349 const char *key)
2351 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2352 struct policy_handle hive_hnd, key_hnd;
2353 char *keyname;
2354 char *path;
2355 WERROR result;
2356 WERROR ignore;
2357 NTSTATUS status;
2358 TALLOC_CTX *tmp_ctx;
2360 tmp_ctx = talloc_stackframe();
2361 if (tmp_ctx == NULL) {
2362 return WERR_NOMEM;
2365 path = winreg_printer_data_keyname(tmp_ctx, printer);
2366 if (path == NULL) {
2367 TALLOC_FREE(tmp_ctx);
2368 return WERR_NOMEM;
2371 result = winreg_printer_openkey(tmp_ctx,
2372 winreg_handle,
2373 path,
2374 key,
2375 false,
2376 access_mask,
2377 &hive_hnd,
2378 &key_hnd);
2379 if (!W_ERROR_IS_OK(result)) {
2380 /* key doesn't exist */
2381 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2382 result = WERR_OK;
2383 goto done;
2386 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
2387 key, win_errstr(result)));
2388 goto done;
2391 if (is_valid_policy_hnd(&key_hnd)) {
2392 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
2395 if (key == NULL || key[0] == '\0') {
2396 keyname = path;
2397 } else {
2398 keyname = talloc_asprintf(tmp_ctx,
2399 "%s\\%s",
2400 path,
2401 key);
2402 if (keyname == NULL) {
2403 result = WERR_NOMEM;
2404 goto done;
2408 status = dcerpc_winreg_delete_subkeys_recursive(tmp_ctx,
2409 winreg_handle,
2410 &hive_hnd,
2411 access_mask,
2412 keyname,
2413 &result);
2415 if (!NT_STATUS_IS_OK(status)) {
2416 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2417 key, nt_errstr(status)));
2418 result = ntstatus_to_werror(status);
2419 goto done;
2422 if (!W_ERROR_IS_OK(result)) {
2423 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2424 key, win_errstr(result)));
2425 goto done;
2428 done:
2429 if (is_valid_policy_hnd(&key_hnd)) {
2430 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2432 if (is_valid_policy_hnd(&hive_hnd)) {
2433 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2436 TALLOC_FREE(tmp_ctx);
2437 return result;
2440 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
2441 struct dcerpc_binding_handle *winreg_handle,
2442 const char *printer)
2444 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2445 struct policy_handle hive_hnd, key_hnd;
2446 char *path;
2447 NTSTATUS status;
2448 WERROR result;
2449 WERROR ignore;
2450 TALLOC_CTX *tmp_ctx;
2452 tmp_ctx = talloc_stackframe();
2453 if (tmp_ctx == NULL) {
2454 return WERR_NOMEM;
2457 path = winreg_printer_data_keyname(tmp_ctx, printer);
2458 if (path == NULL) {
2459 TALLOC_FREE(tmp_ctx);
2460 return WERR_NOMEM;
2463 ZERO_STRUCT(hive_hnd);
2464 ZERO_STRUCT(key_hnd);
2466 result = winreg_printer_openkey(tmp_ctx,
2467 winreg_handle,
2468 path,
2470 false,
2471 access_mask,
2472 &hive_hnd,
2473 &key_hnd);
2474 if (!W_ERROR_IS_OK(result)) {
2475 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
2476 path, win_errstr(result)));
2477 goto done;
2480 status = dcerpc_winreg_set_dword(tmp_ctx,
2481 winreg_handle,
2482 &key_hnd,
2483 "ChangeID",
2484 winreg_printer_rev_changeid(),
2485 &result);
2486 if (!NT_STATUS_IS_OK(status)) {
2487 result = ntstatus_to_werror(status);
2489 if (!W_ERROR_IS_OK(result)) {
2490 goto done;
2493 result = WERR_OK;
2494 done:
2495 if (is_valid_policy_hnd(&key_hnd)) {
2496 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2498 if (is_valid_policy_hnd(&hive_hnd)) {
2499 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2502 TALLOC_FREE(tmp_ctx);
2503 return result;
2506 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
2507 struct dcerpc_binding_handle *winreg_handle,
2508 const char *printer,
2509 uint32_t *pchangeid)
2511 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2512 struct policy_handle hive_hnd, key_hnd;
2513 uint32_t changeid = 0;
2514 char *path;
2515 NTSTATUS status;
2516 WERROR result;
2517 WERROR ignore;
2518 TALLOC_CTX *tmp_ctx;
2520 tmp_ctx = talloc_stackframe();
2521 if (tmp_ctx == NULL) {
2522 return WERR_NOMEM;
2525 path = winreg_printer_data_keyname(tmp_ctx, printer);
2526 if (path == NULL) {
2527 TALLOC_FREE(tmp_ctx);
2528 return WERR_NOMEM;
2531 ZERO_STRUCT(hive_hnd);
2532 ZERO_STRUCT(key_hnd);
2534 result = winreg_printer_openkey(tmp_ctx,
2535 winreg_handle,
2536 path,
2538 false,
2539 access_mask,
2540 &hive_hnd,
2541 &key_hnd);
2542 if (!W_ERROR_IS_OK(result)) {
2543 DEBUG(2, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
2544 path, win_errstr(result)));
2545 goto done;
2548 DEBUG(10, ("winreg_printer_get_changeid: get changeid from %s\n", path));
2550 status = dcerpc_winreg_query_dword(tmp_ctx,
2551 winreg_handle,
2552 &key_hnd,
2553 "ChangeID",
2554 &changeid,
2555 &result);
2556 if (!NT_STATUS_IS_OK(status)) {
2557 result = ntstatus_to_werror(status);
2559 if (!W_ERROR_IS_OK(result)) {
2560 goto done;
2563 if (pchangeid) {
2564 *pchangeid = changeid;
2567 result = WERR_OK;
2568 done:
2569 if (is_valid_policy_hnd(&key_hnd)) {
2570 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2572 if (is_valid_policy_hnd(&hive_hnd)) {
2573 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2576 TALLOC_FREE(tmp_ctx);
2577 return result;
2581 * The special behaviour of the spoolss forms is documented at the website:
2583 * Managing Win32 Printserver Forms
2584 * http://unixwiz.net/techtips/winspooler-forms.html
2587 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
2588 struct dcerpc_binding_handle *winreg_handle,
2589 struct spoolss_AddFormInfo1 *form)
2591 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2592 struct policy_handle hive_hnd, key_hnd;
2593 struct winreg_String wvalue = { 0, };
2594 DATA_BLOB blob;
2595 uint32_t num_info = 0;
2596 union spoolss_FormInfo *info = NULL;
2597 uint32_t i;
2598 WERROR result;
2599 WERROR ignore;
2600 NTSTATUS status;
2601 TALLOC_CTX *tmp_ctx;
2603 tmp_ctx = talloc_stackframe();
2604 if (tmp_ctx == NULL) {
2605 return WERR_NOMEM;
2608 ZERO_STRUCT(hive_hnd);
2609 ZERO_STRUCT(key_hnd);
2611 result = winreg_printer_openkey(tmp_ctx,
2612 winreg_handle,
2613 TOP_LEVEL_CONTROL_FORMS_KEY,
2615 true,
2616 access_mask,
2617 &hive_hnd,
2618 &key_hnd);
2619 if (!W_ERROR_IS_OK(result)) {
2620 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
2621 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2622 goto done;
2625 result = winreg_printer_enumforms1(tmp_ctx, winreg_handle,
2626 &num_info, &info);
2627 if (!W_ERROR_IS_OK(result)) {
2628 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
2629 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2630 goto done;
2633 /* If form name already exists or is builtin return ALREADY_EXISTS */
2634 for (i = 0; i < num_info; i++) {
2635 if (strequal(info[i].info1.form_name, form->form_name)) {
2636 result = WERR_FILE_EXISTS;
2637 goto done;
2641 wvalue.name = form->form_name;
2643 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2644 SIVAL(blob.data, 0, form->size.width);
2645 SIVAL(blob.data, 4, form->size.height);
2646 SIVAL(blob.data, 8, form->area.left);
2647 SIVAL(blob.data, 12, form->area.top);
2648 SIVAL(blob.data, 16, form->area.right);
2649 SIVAL(blob.data, 20, form->area.bottom);
2650 SIVAL(blob.data, 24, num_info + 1); /* FIXME */
2651 SIVAL(blob.data, 28, form->flags);
2653 status = dcerpc_winreg_SetValue(winreg_handle,
2654 tmp_ctx,
2655 &key_hnd,
2656 wvalue,
2657 REG_BINARY,
2658 blob.data,
2659 blob.length,
2660 &result);
2661 if (!NT_STATUS_IS_OK(status)) {
2662 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
2663 wvalue.name, nt_errstr(status)));
2664 result = ntstatus_to_werror(status);
2667 done:
2668 if (is_valid_policy_hnd(&key_hnd)) {
2669 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2671 if (is_valid_policy_hnd(&hive_hnd)) {
2672 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2675 TALLOC_FREE(info);
2676 TALLOC_FREE(tmp_ctx);
2677 return result;
2680 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
2681 struct dcerpc_binding_handle *winreg_handle,
2682 uint32_t *pnum_info,
2683 union spoolss_FormInfo **pinfo)
2685 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2686 struct policy_handle hive_hnd, key_hnd;
2687 union spoolss_FormInfo *info;
2688 struct spoolss_PrinterEnumValues *enum_values = NULL;
2689 uint32_t num_values = 0;
2690 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2691 uint32_t i;
2692 WERROR result;
2693 WERROR ignore;
2694 NTSTATUS status;
2695 const char **enum_names = NULL;
2696 enum winreg_Type *enum_types = NULL;
2697 DATA_BLOB *enum_data_blobs = NULL;
2698 TALLOC_CTX *tmp_ctx;
2700 tmp_ctx = talloc_stackframe();
2701 if (tmp_ctx == NULL) {
2702 return WERR_NOMEM;
2705 ZERO_STRUCT(hive_hnd);
2706 ZERO_STRUCT(key_hnd);
2708 result = winreg_printer_openkey(tmp_ctx,
2709 winreg_handle,
2710 TOP_LEVEL_CONTROL_FORMS_KEY,
2712 true,
2713 access_mask,
2714 &hive_hnd,
2715 &key_hnd);
2716 if (!W_ERROR_IS_OK(result)) {
2717 /* key doesn't exist */
2718 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2719 result = WERR_OK;
2720 goto done;
2723 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
2724 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2725 goto done;
2728 status = dcerpc_winreg_enumvals(tmp_ctx,
2729 winreg_handle,
2730 &key_hnd,
2731 &num_values,
2732 &enum_names,
2733 &enum_types,
2734 &enum_data_blobs,
2735 &result);
2736 if (!NT_STATUS_IS_OK(status)){
2737 result = ntstatus_to_werror(status);
2740 if (!W_ERROR_IS_OK(result)) {
2741 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2742 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2743 goto done;
2746 enum_values = talloc_zero_array(tmp_ctx,
2747 struct spoolss_PrinterEnumValues,
2748 num_values);
2749 if (enum_values == NULL){
2750 result = WERR_NOMEM;
2751 goto done;
2754 for (i = 0; i < num_values; i++){
2755 enum_values[i].value_name = enum_names[i];
2756 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
2757 enum_values[i].type = enum_types[i];
2758 enum_values[i].data_length = enum_data_blobs[i].length;
2759 enum_values[i].data = NULL;
2760 if (enum_values[i].data_length != 0){
2761 enum_values[i].data = &enum_data_blobs[i];
2765 if (!W_ERROR_IS_OK(result)) {
2766 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2767 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2768 goto done;
2771 info = talloc_array(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
2772 if (info == NULL) {
2773 result = WERR_NOMEM;
2774 goto done;
2777 /* Enumerate BUILTIN forms */
2778 for (i = 0; i < num_builtin; i++) {
2779 info[i].info1 = builtin_forms1[i];
2782 /* Enumerate registry forms */
2783 for (i = 0; i < num_values; i++) {
2784 union spoolss_FormInfo val;
2786 if (enum_values[i].type != REG_BINARY ||
2787 enum_values[i].data_length != 32) {
2788 continue;
2791 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
2792 if (val.info1.form_name == NULL) {
2793 result = WERR_NOMEM;
2794 goto done;
2797 val.info1.size.width = IVAL(enum_values[i].data->data, 0);
2798 val.info1.size.height = IVAL(enum_values[i].data->data, 4);
2799 val.info1.area.left = IVAL(enum_values[i].data->data, 8);
2800 val.info1.area.top = IVAL(enum_values[i].data->data, 12);
2801 val.info1.area.right = IVAL(enum_values[i].data->data, 16);
2802 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
2803 /* skip form index IVAL(enum_values[i].data->data, 24)));*/
2804 val.info1.flags = (enum spoolss_FormFlags) IVAL(enum_values[i].data->data, 28);
2806 info[i + num_builtin] = val;
2809 *pnum_info = num_builtin + num_values;
2810 if (pinfo) {
2811 *pinfo = talloc_move(mem_ctx, &info);
2814 done:
2815 if (is_valid_policy_hnd(&key_hnd)) {
2816 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2818 if (is_valid_policy_hnd(&hive_hnd)) {
2819 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2822 TALLOC_FREE(enum_values);
2823 TALLOC_FREE(tmp_ctx);
2824 return result;
2827 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
2828 struct dcerpc_binding_handle *winreg_handle,
2829 const char *form_name)
2831 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2832 struct policy_handle hive_hnd, key_hnd;
2833 struct winreg_String wvalue = { 0, };
2834 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2835 uint32_t i;
2836 WERROR result = WERR_OK;
2837 WERROR ignore;
2838 NTSTATUS status;
2839 TALLOC_CTX *tmp_ctx;
2841 for (i = 0; i < num_builtin; i++) {
2842 if (strequal(builtin_forms1[i].form_name, form_name)) {
2843 return WERR_INVALID_PARAMETER;
2847 tmp_ctx = talloc_stackframe();
2848 if (tmp_ctx == NULL) {
2849 return WERR_NOMEM;
2852 ZERO_STRUCT(hive_hnd);
2853 ZERO_STRUCT(key_hnd);
2855 result = winreg_printer_openkey(tmp_ctx,
2856 winreg_handle,
2857 TOP_LEVEL_CONTROL_FORMS_KEY,
2859 false,
2860 access_mask,
2861 &hive_hnd,
2862 &key_hnd);
2863 if (!W_ERROR_IS_OK(result)) {
2864 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
2865 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2866 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2867 result = WERR_INVALID_FORM_NAME;
2869 goto done;
2872 wvalue.name = form_name;
2873 status = dcerpc_winreg_DeleteValue(winreg_handle,
2874 tmp_ctx,
2875 &key_hnd,
2876 wvalue,
2877 &result);
2878 if (!NT_STATUS_IS_OK(status)) {
2879 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
2880 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
2881 wvalue.name, nt_errstr(status)));
2882 result = ntstatus_to_werror(status);
2883 goto done;
2886 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2887 result = WERR_INVALID_FORM_NAME;
2890 done:
2891 if (is_valid_policy_hnd(&key_hnd)) {
2892 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2894 if (is_valid_policy_hnd(&hive_hnd)) {
2895 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2898 TALLOC_FREE(tmp_ctx);
2899 return result;
2902 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
2903 struct dcerpc_binding_handle *winreg_handle,
2904 const char *form_name,
2905 struct spoolss_AddFormInfo1 *form)
2907 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2908 struct policy_handle hive_hnd = { 0, };
2909 struct policy_handle key_hnd = { 0, };
2910 struct winreg_String wvalue = { 0, };
2911 DATA_BLOB blob;
2912 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2913 uint32_t i;
2914 WERROR result;
2915 NTSTATUS status;
2916 TALLOC_CTX *tmp_ctx = NULL;
2918 for (i = 0; i < num_builtin; i++) {
2919 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
2920 result = WERR_INVALID_PARAM;
2921 goto done;
2925 tmp_ctx = talloc_stackframe();
2926 if (tmp_ctx == NULL) {
2927 return WERR_NOMEM;
2930 ZERO_STRUCT(hive_hnd);
2931 ZERO_STRUCT(key_hnd);
2933 result = winreg_printer_openkey(tmp_ctx,
2934 winreg_handle,
2935 TOP_LEVEL_CONTROL_FORMS_KEY,
2937 true,
2938 access_mask,
2939 &hive_hnd,
2940 &key_hnd);
2941 if (!W_ERROR_IS_OK(result)) {
2942 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2943 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2944 goto done;
2947 /* If form_name != form->form_name then we renamed the form */
2948 if (strequal(form_name, form->form_name)) {
2949 result = winreg_printer_deleteform1(tmp_ctx, winreg_handle,
2950 form_name);
2951 if (!W_ERROR_IS_OK(result)) {
2952 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2953 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2954 goto done;
2958 wvalue.name = form->form_name;
2960 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2961 SIVAL(blob.data, 0, form->size.width);
2962 SIVAL(blob.data, 4, form->size.height);
2963 SIVAL(blob.data, 8, form->area.left);
2964 SIVAL(blob.data, 12, form->area.top);
2965 SIVAL(blob.data, 16, form->area.right);
2966 SIVAL(blob.data, 20, form->area.bottom);
2967 SIVAL(blob.data, 24, 42);
2968 SIVAL(blob.data, 28, form->flags);
2970 status = dcerpc_winreg_SetValue(winreg_handle,
2971 tmp_ctx,
2972 &key_hnd,
2973 wvalue,
2974 REG_BINARY,
2975 blob.data,
2976 blob.length,
2977 &result);
2978 if (!NT_STATUS_IS_OK(status)) {
2979 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
2980 wvalue.name, nt_errstr(status)));
2981 result = ntstatus_to_werror(status);
2984 done:
2985 if (winreg_handle != NULL) {
2986 WERROR ignore;
2988 if (is_valid_policy_hnd(&key_hnd)) {
2989 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2991 if (is_valid_policy_hnd(&hive_hnd)) {
2992 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2996 TALLOC_FREE(tmp_ctx);
2997 return result;
3000 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
3001 struct dcerpc_binding_handle *winreg_handle,
3002 const char *form_name,
3003 struct spoolss_FormInfo1 *r)
3005 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3006 struct policy_handle hive_hnd, key_hnd;
3007 struct winreg_String wvalue;
3008 enum winreg_Type type_in = REG_NONE;
3009 uint8_t *data_in = NULL;
3010 uint32_t data_in_size = 0;
3011 uint32_t value_len = 0;
3012 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3013 uint32_t i;
3014 WERROR result;
3015 WERROR ignore;
3016 NTSTATUS status;
3017 TALLOC_CTX *tmp_ctx;
3019 /* check builtin forms first */
3020 for (i = 0; i < num_builtin; i++) {
3021 if (strequal(builtin_forms1[i].form_name, form_name)) {
3022 *r = builtin_forms1[i];
3023 return WERR_OK;
3027 tmp_ctx = talloc_stackframe();
3028 if (tmp_ctx == NULL) {
3029 return WERR_NOMEM;
3032 ZERO_STRUCT(hive_hnd);
3033 ZERO_STRUCT(key_hnd);
3035 result = winreg_printer_openkey(tmp_ctx,
3036 winreg_handle,
3037 TOP_LEVEL_CONTROL_FORMS_KEY,
3039 true,
3040 access_mask,
3041 &hive_hnd,
3042 &key_hnd);
3043 if (!W_ERROR_IS_OK(result)) {
3044 DEBUG(2, ("winreg_printer_getform1: Could not open key %s: %s\n",
3045 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3046 goto done;
3049 wvalue.name = form_name;
3052 * call QueryValue once with data == NULL to get the
3053 * needed memory size to be allocated, then allocate
3054 * data buffer and call again.
3056 status = dcerpc_winreg_QueryValue(winreg_handle,
3057 tmp_ctx,
3058 &key_hnd,
3059 &wvalue,
3060 &type_in,
3061 NULL,
3062 &data_in_size,
3063 &value_len,
3064 &result);
3065 if (!NT_STATUS_IS_OK(status)) {
3066 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3067 wvalue.name, nt_errstr(status)));
3068 result = ntstatus_to_werror(status);
3069 goto done;
3071 if (!W_ERROR_IS_OK(result)) {
3072 goto done;
3075 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
3076 if (data_in == NULL) {
3077 result = WERR_NOMEM;
3078 goto done;
3080 value_len = 0;
3082 status = dcerpc_winreg_QueryValue(winreg_handle,
3083 tmp_ctx,
3084 &key_hnd,
3085 &wvalue,
3086 &type_in,
3087 data_in,
3088 &data_in_size,
3089 &value_len,
3090 &result);
3091 if (!NT_STATUS_IS_OK(status)) {
3092 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3093 wvalue.name, nt_errstr(status)));
3094 result = ntstatus_to_werror(status);
3095 goto done;
3097 if (!W_ERROR_IS_OK(result)) {
3098 goto done;
3101 r->form_name = talloc_strdup(mem_ctx, form_name);
3102 if (r->form_name == NULL) {
3103 result = WERR_NOMEM;
3104 goto done;
3107 r->size.width = IVAL(data_in, 0);
3108 r->size.height = IVAL(data_in, 4);
3109 r->area.left = IVAL(data_in, 8);
3110 r->area.top = IVAL(data_in, 12);
3111 r->area.right = IVAL(data_in, 16);
3112 r->area.bottom = IVAL(data_in, 20);
3113 /* skip index IVAL(data_in, 24)));*/
3114 r->flags = (enum spoolss_FormFlags) IVAL(data_in, 28);
3116 result = WERR_OK;
3117 done:
3118 if (is_valid_policy_hnd(&key_hnd)) {
3119 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3121 if (is_valid_policy_hnd(&hive_hnd)) {
3122 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3125 TALLOC_FREE(tmp_ctx);
3126 return result;
3129 WERROR winreg_add_driver(TALLOC_CTX *mem_ctx,
3130 struct dcerpc_binding_handle *winreg_handle,
3131 struct spoolss_AddDriverInfoCtr *r,
3132 const char **driver_name,
3133 uint32_t *driver_version)
3135 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3136 struct policy_handle hive_hnd, key_hnd;
3137 struct spoolss_DriverInfo8 info8;
3138 TALLOC_CTX *tmp_ctx = NULL;
3139 NTSTATUS status;
3140 WERROR result;
3142 ZERO_STRUCT(hive_hnd);
3143 ZERO_STRUCT(key_hnd);
3144 ZERO_STRUCT(info8);
3146 if (!driver_info_ctr_to_info8(r, &info8)) {
3147 result = WERR_INVALID_PARAMETER;
3148 goto done;
3151 tmp_ctx = talloc_stackframe();
3152 if (tmp_ctx == NULL) {
3153 return WERR_NOMEM;
3156 result = winreg_printer_opendriver(tmp_ctx,
3157 winreg_handle,
3158 info8.driver_name,
3159 info8.architecture,
3160 info8.version,
3161 access_mask, true,
3162 &hive_hnd,
3163 &key_hnd);
3164 if (!W_ERROR_IS_OK(result)) {
3165 DEBUG(0, ("winreg_add_driver: "
3166 "Could not open driver key (%s,%s,%d): %s\n",
3167 info8.driver_name, info8.architecture,
3168 info8.version, win_errstr(result)));
3169 goto done;
3172 /* TODO: "Attributes" ? */
3174 status = dcerpc_winreg_set_dword(tmp_ctx,
3175 winreg_handle,
3176 &key_hnd,
3177 "Version",
3178 info8.version,
3179 &result);
3180 if (!NT_STATUS_IS_OK(status)) {
3181 result = ntstatus_to_werror(status);
3183 if (!W_ERROR_IS_OK(result)) {
3184 goto done;
3187 status = dcerpc_winreg_set_sz(tmp_ctx,
3188 winreg_handle,
3189 &key_hnd,
3190 "Driver",
3191 info8.driver_path,
3192 &result);
3193 if (!NT_STATUS_IS_OK(status)) {
3194 result = ntstatus_to_werror(status);
3196 if (!W_ERROR_IS_OK(result)) {
3197 goto done;
3200 status = dcerpc_winreg_set_sz(tmp_ctx,
3201 winreg_handle,
3202 &key_hnd,
3203 "Data File",
3204 info8.data_file,
3205 &result);
3206 if (!NT_STATUS_IS_OK(status)) {
3207 result = ntstatus_to_werror(status);
3209 if (!W_ERROR_IS_OK(result)) {
3210 goto done;
3213 status = dcerpc_winreg_set_sz(tmp_ctx,
3214 winreg_handle,
3215 &key_hnd,
3216 "Configuration File",
3217 info8.config_file,
3218 &result);
3219 if (!NT_STATUS_IS_OK(status)) {
3220 result = ntstatus_to_werror(status);
3222 if (!W_ERROR_IS_OK(result)) {
3223 goto done;
3226 status = dcerpc_winreg_set_sz(tmp_ctx,
3227 winreg_handle,
3228 &key_hnd,
3229 "Help File",
3230 info8.help_file,
3231 &result);
3232 if (!NT_STATUS_IS_OK(status)) {
3233 result = ntstatus_to_werror(status);
3235 if (!W_ERROR_IS_OK(result)) {
3236 goto done;
3239 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3240 winreg_handle,
3241 &key_hnd,
3242 "Dependent Files",
3243 info8.dependent_files,
3244 &result);
3245 if (!NT_STATUS_IS_OK(status)) {
3246 result = ntstatus_to_werror(status);
3248 if (!W_ERROR_IS_OK(result)) {
3249 goto done;
3252 status = dcerpc_winreg_set_sz(tmp_ctx,
3253 winreg_handle,
3254 &key_hnd,
3255 "Monitor",
3256 info8.monitor_name,
3257 &result);
3258 if (!NT_STATUS_IS_OK(status)) {
3259 result = ntstatus_to_werror(status);
3261 if (!W_ERROR_IS_OK(result)) {
3262 goto done;
3265 status = dcerpc_winreg_set_sz(tmp_ctx,
3266 winreg_handle,
3267 &key_hnd,
3268 "Datatype",
3269 info8.default_datatype,
3270 &result);
3271 if (!NT_STATUS_IS_OK(status)) {
3272 result = ntstatus_to_werror(status);
3274 if (!W_ERROR_IS_OK(result)) {
3275 goto done;
3278 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3279 winreg_handle,
3280 &key_hnd, "Previous Names",
3281 info8.previous_names,
3282 &result);
3283 if (!NT_STATUS_IS_OK(status)) {
3284 result = ntstatus_to_werror(status);
3286 if (!W_ERROR_IS_OK(result)) {
3287 goto done;
3290 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3291 &key_hnd, "DriverDate",
3292 info8.driver_date);
3293 if (!W_ERROR_IS_OK(result)) {
3294 goto done;
3297 result = winreg_printer_write_ver(tmp_ctx, winreg_handle,
3298 &key_hnd, "DriverVersion",
3299 info8.driver_version);
3300 if (!W_ERROR_IS_OK(result)) {
3301 goto done;
3304 status = dcerpc_winreg_set_sz(tmp_ctx,
3305 winreg_handle,
3306 &key_hnd,
3307 "Manufacturer",
3308 info8.manufacturer_name,
3309 &result);
3310 if (!NT_STATUS_IS_OK(status)) {
3311 result = ntstatus_to_werror(status);
3313 if (!W_ERROR_IS_OK(result)) {
3314 goto done;
3317 status = dcerpc_winreg_set_sz(tmp_ctx,
3318 winreg_handle,
3319 &key_hnd,
3320 "OEM URL",
3321 info8.manufacturer_url,
3322 &result);
3323 if (!NT_STATUS_IS_OK(status)) {
3324 result = ntstatus_to_werror(status);
3326 if (!W_ERROR_IS_OK(result)) {
3327 goto done;
3330 status = dcerpc_winreg_set_sz(tmp_ctx,
3331 winreg_handle,
3332 &key_hnd,
3333 "HardwareID",
3334 info8.hardware_id,
3335 &result);
3336 if (!NT_STATUS_IS_OK(status)) {
3337 result = ntstatus_to_werror(status);
3339 if (!W_ERROR_IS_OK(result)) {
3340 goto done;
3343 status = dcerpc_winreg_set_sz(tmp_ctx,
3344 winreg_handle,
3345 &key_hnd,
3346 "Provider",
3347 info8.provider,
3348 &result);
3349 if (!NT_STATUS_IS_OK(status)) {
3350 result = ntstatus_to_werror(status);
3352 if (!W_ERROR_IS_OK(result)) {
3353 goto done;
3356 status = dcerpc_winreg_set_sz(tmp_ctx,
3357 winreg_handle,
3358 &key_hnd,
3359 "Print Processor",
3360 info8.print_processor,
3361 &result);
3362 if (!NT_STATUS_IS_OK(status)) {
3363 result = ntstatus_to_werror(status);
3365 if (!W_ERROR_IS_OK(result)) {
3366 goto done;
3369 status = dcerpc_winreg_set_sz(tmp_ctx,
3370 winreg_handle,
3371 &key_hnd,
3372 "VendorSetup",
3373 info8.vendor_setup,
3374 &result);
3375 if (!NT_STATUS_IS_OK(status)) {
3376 result = ntstatus_to_werror(status);
3378 if (!W_ERROR_IS_OK(result)) {
3379 goto done;
3382 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3383 winreg_handle,
3384 &key_hnd,
3385 "Color Profiles",
3386 info8.color_profiles,
3387 &result);
3388 if (!NT_STATUS_IS_OK(status)) {
3389 result = ntstatus_to_werror(status);
3391 if (!W_ERROR_IS_OK(result)) {
3392 goto done;
3395 status = dcerpc_winreg_set_sz(tmp_ctx,
3396 winreg_handle,
3397 &key_hnd,
3398 "InfPath",
3399 info8.inf_path,
3400 &result);
3401 if (!NT_STATUS_IS_OK(status)) {
3402 result = ntstatus_to_werror(status);
3404 if (!W_ERROR_IS_OK(result)) {
3405 goto done;
3408 status = dcerpc_winreg_set_dword(tmp_ctx,
3409 winreg_handle,
3410 &key_hnd,
3411 "PrinterDriverAttributes",
3412 info8.printer_driver_attributes,
3413 &result);
3414 if (!NT_STATUS_IS_OK(status)) {
3415 result = ntstatus_to_werror(status);
3417 if (!W_ERROR_IS_OK(result)) {
3418 goto done;
3421 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3422 winreg_handle,
3423 &key_hnd,
3424 "CoreDependencies",
3425 info8.core_driver_dependencies,
3426 &result);
3427 if (!NT_STATUS_IS_OK(status)) {
3428 result = ntstatus_to_werror(status);
3430 if (!W_ERROR_IS_OK(result)) {
3431 goto done;
3434 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3435 &key_hnd, "MinInboxDriverVerDate",
3436 info8.min_inbox_driver_ver_date);
3437 if (!W_ERROR_IS_OK(result)) {
3438 goto done;
3441 result = winreg_printer_write_ver(tmp_ctx, winreg_handle, &key_hnd,
3442 "MinInboxDriverVerVersion",
3443 info8.min_inbox_driver_ver_version);
3444 if (!W_ERROR_IS_OK(result)) {
3445 goto done;
3448 *driver_name = info8.driver_name;
3449 *driver_version = info8.version;
3450 result = WERR_OK;
3451 done:
3452 if (winreg_handle != NULL) {
3453 WERROR ignore;
3455 if (is_valid_policy_hnd(&key_hnd)) {
3456 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3458 if (is_valid_policy_hnd(&hive_hnd)) {
3459 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3463 TALLOC_FREE(tmp_ctx);
3464 return result;
3467 WERROR winreg_get_driver(TALLOC_CTX *mem_ctx,
3468 struct dcerpc_binding_handle *winreg_handle,
3469 const char *architecture,
3470 const char *driver_name,
3471 uint32_t driver_version,
3472 struct spoolss_DriverInfo8 **_info8)
3474 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3475 struct policy_handle hive_hnd, key_hnd;
3476 struct spoolss_DriverInfo8 i8, *info8;
3477 struct spoolss_PrinterEnumValues *enum_values = NULL;
3478 struct spoolss_PrinterEnumValues *v;
3479 uint32_t num_values = 0;
3480 TALLOC_CTX *tmp_ctx;
3481 WERROR result;
3482 NTSTATUS status;
3483 uint32_t i;
3484 const char **enum_names = NULL;
3485 enum winreg_Type *enum_types = NULL;
3486 DATA_BLOB *enum_data_blobs = NULL;
3488 ZERO_STRUCT(hive_hnd);
3489 ZERO_STRUCT(key_hnd);
3490 ZERO_STRUCT(i8);
3492 tmp_ctx = talloc_stackframe();
3493 if (tmp_ctx == NULL) {
3494 return WERR_NOMEM;
3497 if (driver_version == DRIVER_ANY_VERSION) {
3498 /* look for Win2k first and then for NT4 */
3499 result = winreg_printer_opendriver(tmp_ctx,
3500 winreg_handle,
3501 driver_name,
3502 architecture,
3504 access_mask, false,
3505 &hive_hnd,
3506 &key_hnd);
3507 if (!W_ERROR_IS_OK(result)) {
3508 result = winreg_printer_opendriver(tmp_ctx,
3509 winreg_handle,
3510 driver_name,
3511 architecture,
3513 access_mask, false,
3514 &hive_hnd,
3515 &key_hnd);
3517 } else {
3518 /* ok normal case */
3519 result = winreg_printer_opendriver(tmp_ctx,
3520 winreg_handle,
3521 driver_name,
3522 architecture,
3523 driver_version,
3524 access_mask, false,
3525 &hive_hnd,
3526 &key_hnd);
3528 if (!W_ERROR_IS_OK(result)) {
3529 DEBUG(5, ("winreg_get_driver: "
3530 "Could not open driver key (%s,%s,%d): %s\n",
3531 driver_name, architecture,
3532 driver_version, win_errstr(result)));
3533 goto done;
3536 status = dcerpc_winreg_enumvals(tmp_ctx,
3537 winreg_handle,
3538 &key_hnd,
3539 &num_values,
3540 &enum_names,
3541 &enum_types,
3542 &enum_data_blobs,
3543 &result);
3544 if (!NT_STATUS_IS_OK(status)){
3545 result = ntstatus_to_werror(status);
3548 if (!W_ERROR_IS_OK(result)) {
3549 DEBUG(0, ("winreg_get_driver: "
3550 "Could not enumerate values for (%s,%s,%d): %s\n",
3551 driver_name, architecture,
3552 driver_version, win_errstr(result)));
3553 goto done;
3556 enum_values = talloc_zero_array(tmp_ctx,
3557 struct spoolss_PrinterEnumValues,
3558 num_values);
3559 if (enum_values == NULL){
3560 result = WERR_NOMEM;
3561 goto done;
3564 for (i = 0; i < num_values; i++){
3565 enum_values[i].value_name = enum_names[i];
3566 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
3567 enum_values[i].type = enum_types[i];
3568 enum_values[i].data_length = enum_data_blobs[i].length;
3569 enum_values[i].data = NULL;
3570 if (enum_values[i].data_length != 0){
3571 enum_values[i].data = &enum_data_blobs[i];
3575 info8 = talloc_zero(tmp_ctx, struct spoolss_DriverInfo8);
3576 if (info8 == NULL) {
3577 result = WERR_NOMEM;
3578 goto done;
3581 info8->driver_name = talloc_strdup(info8, driver_name);
3582 if (info8->driver_name == NULL) {
3583 result = WERR_NOMEM;
3584 goto done;
3587 info8->architecture = talloc_strdup(info8, architecture);
3588 if (info8->architecture == NULL) {
3589 result = WERR_NOMEM;
3590 goto done;
3593 result = WERR_OK;
3595 for (i = 0; i < num_values; i++) {
3596 const char *tmp_str;
3597 uint32_t tmp = 0;
3599 v = &enum_values[i];
3601 result = winreg_enumval_to_dword(info8, v,
3602 "Version",
3603 &tmp);
3604 if (W_ERROR_IS_OK(result)) {
3605 info8->version = (enum spoolss_DriverOSVersion) tmp;
3607 CHECK_ERROR(result);
3609 result = winreg_enumval_to_sz(info8, v,
3610 "Driver",
3611 &info8->driver_path);
3612 CHECK_ERROR(result);
3614 result = winreg_enumval_to_sz(info8, v,
3615 "Data File",
3616 &info8->data_file);
3617 CHECK_ERROR(result);
3619 result = winreg_enumval_to_sz(info8, v,
3620 "Configuration File",
3621 &info8->config_file);
3622 CHECK_ERROR(result);
3624 result = winreg_enumval_to_sz(info8, v,
3625 "Help File",
3626 &info8->help_file);
3627 CHECK_ERROR(result);
3629 result = winreg_enumval_to_multi_sz(info8, v,
3630 "Dependent Files",
3631 &info8->dependent_files);
3632 CHECK_ERROR(result);
3634 result = winreg_enumval_to_sz(info8, v,
3635 "Monitor",
3636 &info8->monitor_name);
3637 CHECK_ERROR(result);
3639 result = winreg_enumval_to_sz(info8, v,
3640 "Datatype",
3641 &info8->default_datatype);
3642 CHECK_ERROR(result);
3644 result = winreg_enumval_to_multi_sz(info8, v,
3645 "Previous Names",
3646 &info8->previous_names);
3647 CHECK_ERROR(result);
3649 result = winreg_enumval_to_sz(info8, v,
3650 "DriverDate",
3651 &tmp_str);
3652 if (W_ERROR_IS_OK(result)) {
3653 result = winreg_printer_date_to_NTTIME(tmp_str,
3654 &info8->driver_date);
3656 CHECK_ERROR(result);
3658 result = winreg_enumval_to_sz(info8, v,
3659 "DriverVersion",
3660 &tmp_str);
3661 if (W_ERROR_IS_OK(result)) {
3662 result = winreg_printer_ver_to_dword(tmp_str,
3663 &info8->driver_version);
3665 CHECK_ERROR(result);
3667 result = winreg_enumval_to_sz(info8, v,
3668 "Manufacturer",
3669 &info8->manufacturer_name);
3670 CHECK_ERROR(result);
3672 result = winreg_enumval_to_sz(info8, v,
3673 "OEM URL",
3674 &info8->manufacturer_url);
3675 CHECK_ERROR(result);
3677 result = winreg_enumval_to_sz(info8, v,
3678 "HardwareID",
3679 &info8->hardware_id);
3680 CHECK_ERROR(result);
3682 result = winreg_enumval_to_sz(info8, v,
3683 "Provider",
3684 &info8->provider);
3685 CHECK_ERROR(result);
3687 result = winreg_enumval_to_sz(info8, v,
3688 "Print Processor",
3689 &info8->print_processor);
3690 CHECK_ERROR(result);
3692 result = winreg_enumval_to_sz(info8, v,
3693 "VendorSetup",
3694 &info8->vendor_setup);
3695 CHECK_ERROR(result);
3697 result = winreg_enumval_to_multi_sz(info8, v,
3698 "Color Profiles",
3699 &info8->color_profiles);
3700 CHECK_ERROR(result);
3702 result = winreg_enumval_to_sz(info8, v,
3703 "InfPath",
3704 &info8->inf_path);
3705 CHECK_ERROR(result);
3707 result = winreg_enumval_to_dword(info8, v,
3708 "PrinterDriverAttributes",
3709 &info8->printer_driver_attributes);
3710 CHECK_ERROR(result);
3712 result = winreg_enumval_to_multi_sz(info8, v,
3713 "CoreDependencies",
3714 &info8->core_driver_dependencies);
3715 CHECK_ERROR(result);
3717 result = winreg_enumval_to_sz(info8, v,
3718 "MinInboxDriverVerDate",
3719 &tmp_str);
3720 if (W_ERROR_IS_OK(result)) {
3721 result = winreg_printer_date_to_NTTIME(tmp_str,
3722 &info8->min_inbox_driver_ver_date);
3724 CHECK_ERROR(result);
3726 result = winreg_enumval_to_sz(info8, v,
3727 "MinInboxDriverVerVersion",
3728 &tmp_str);
3729 if (W_ERROR_IS_OK(result)) {
3730 result = winreg_printer_ver_to_dword(tmp_str,
3731 &info8->min_inbox_driver_ver_version);
3733 CHECK_ERROR(result);
3736 if (!W_ERROR_IS_OK(result)) {
3737 DEBUG(0, ("winreg_enumval_to_TYPE() failed "
3738 "for %s: %s\n", v->value_name,
3739 win_errstr(result)));
3740 goto done;
3743 *_info8 = talloc_steal(mem_ctx, info8);
3744 result = WERR_OK;
3745 done:
3746 if (winreg_handle != NULL) {
3747 WERROR ignore;
3749 if (is_valid_policy_hnd(&key_hnd)) {
3750 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3752 if (is_valid_policy_hnd(&hive_hnd)) {
3753 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3757 TALLOC_FREE(tmp_ctx);
3758 return result;
3761 WERROR winreg_del_driver(TALLOC_CTX *mem_ctx,
3762 struct dcerpc_binding_handle *winreg_handle,
3763 struct spoolss_DriverInfo8 *info8,
3764 uint32_t version)
3766 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3767 struct policy_handle hive_hnd, key_hnd;
3768 TALLOC_CTX *tmp_ctx;
3769 char *key_name;
3770 WERROR result;
3771 NTSTATUS status;
3773 ZERO_STRUCT(hive_hnd);
3774 ZERO_STRUCT(key_hnd);
3776 tmp_ctx = talloc_stackframe();
3777 if (tmp_ctx == NULL) {
3778 return WERR_NOMEM;
3781 /* test that the key exists */
3782 result = winreg_printer_opendriver(tmp_ctx,
3783 winreg_handle,
3784 info8->driver_name,
3785 info8->architecture,
3786 version,
3787 access_mask, false,
3788 &hive_hnd,
3789 &key_hnd);
3790 if (!W_ERROR_IS_OK(result)) {
3791 /* key doesn't exist */
3792 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3793 result = WERR_OK;
3794 goto done;
3797 DEBUG(5, ("winreg_del_driver: "
3798 "Could not open driver (%s,%s,%u): %s\n",
3799 info8->driver_name, info8->architecture,
3800 version, win_errstr(result)));
3801 goto done;
3805 if (is_valid_policy_hnd(&key_hnd)) {
3806 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
3809 key_name = talloc_asprintf(tmp_ctx,
3810 "%s\\Environments\\%s\\Drivers\\Version-%u\\%s",
3811 TOP_LEVEL_CONTROL_KEY,
3812 info8->architecture, version,
3813 info8->driver_name);
3814 if (key_name == NULL) {
3815 result = WERR_NOMEM;
3816 goto done;
3819 status = dcerpc_winreg_delete_subkeys_recursive(tmp_ctx,
3820 winreg_handle,
3821 &hive_hnd,
3822 access_mask,
3823 key_name,
3824 &result);
3826 if (!NT_STATUS_IS_OK(status)){
3827 DEBUG(0, ("winreg_del_driver: "
3828 "Could not open driver (%s,%s,%u): %s\n",
3829 info8->driver_name, info8->architecture,
3830 version, nt_errstr(status)));
3831 goto done;
3834 if (!W_ERROR_IS_OK(result)) {
3835 DEBUG(0, ("winreg_del_driver: "
3836 "Could not open driver (%s,%s,%u): %s\n",
3837 info8->driver_name, info8->architecture,
3838 version, win_errstr(result)));
3839 goto done;
3842 result = WERR_OK;
3843 done:
3844 if (winreg_handle != NULL) {
3845 WERROR ignore;
3847 if (is_valid_policy_hnd(&key_hnd)) {
3848 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3850 if (is_valid_policy_hnd(&hive_hnd)) {
3851 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3855 TALLOC_FREE(tmp_ctx);
3856 return result;
3859 WERROR winreg_get_driver_list(TALLOC_CTX *mem_ctx,
3860 struct dcerpc_binding_handle *winreg_handle,
3861 const char *architecture,
3862 uint32_t version,
3863 uint32_t *num_drivers,
3864 const char ***drivers_p)
3866 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3867 struct policy_handle hive_hnd, key_hnd;
3868 const char **drivers;
3869 TALLOC_CTX *tmp_ctx;
3870 WERROR result;
3871 NTSTATUS status;
3873 *num_drivers = 0;
3874 *drivers_p = NULL;
3876 ZERO_STRUCT(hive_hnd);
3877 ZERO_STRUCT(key_hnd);
3879 tmp_ctx = talloc_stackframe();
3880 if (tmp_ctx == NULL) {
3881 return WERR_NOMEM;
3884 /* use NULL for the driver name so we open the key that is
3885 * parent of all drivers for this architecture and version */
3886 result = winreg_printer_opendriver(tmp_ctx,
3887 winreg_handle,
3888 NULL,
3889 architecture,
3890 version,
3891 access_mask, false,
3892 &hive_hnd,
3893 &key_hnd);
3894 if (!W_ERROR_IS_OK(result)) {
3895 DEBUG(5, ("winreg_get_driver_list: "
3896 "Could not open key (%s,%u): %s\n",
3897 architecture, version, win_errstr(result)));
3898 result = WERR_OK;
3899 goto done;
3902 status = dcerpc_winreg_enum_keys(tmp_ctx,
3903 winreg_handle,
3904 &key_hnd,
3905 num_drivers,
3906 &drivers,
3907 &result);
3908 if (!NT_STATUS_IS_OK(status)) {
3909 result = ntstatus_to_werror(status);
3911 if (!W_ERROR_IS_OK(result)) {
3912 DEBUG(0, ("winreg_get_driver_list: "
3913 "Could not enumerate drivers for (%s,%u): %s\n",
3914 architecture, version, win_errstr(result)));
3915 goto done;
3918 *drivers_p = talloc_steal(mem_ctx, drivers);
3920 result = WERR_OK;
3921 done:
3922 if (winreg_handle != NULL) {
3923 WERROR ignore;
3925 if (is_valid_policy_hnd(&key_hnd)) {
3926 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3928 if (is_valid_policy_hnd(&hive_hnd)) {
3929 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3933 TALLOC_FREE(tmp_ctx);
3934 return result;