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/>.
23 #include "srv_spoolss_util.h"
24 #include "../librpc/gen_ndr/srv_winreg.h"
25 #include "../librpc/gen_ndr/cli_winreg.h"
27 #define TOP_LEVEL_PRINT_KEY "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print"
28 #define TOP_LEVEL_PRINT_PRINTERS_KEY TOP_LEVEL_PRINT_KEY "\\Printers"
29 #define TOP_LEVEL_CONTROL_KEY "SYSTEM\\CurrentControlSet\\Control\\Print"
30 #define TOP_LEVEL_CONTROL_FORMS_KEY TOP_LEVEL_CONTROL_KEY "\\Forms"
32 /* FLAGS, NAME, with, height, left, top, right, bottom */
33 static const struct spoolss_FormInfo1 builtin_forms1[] = {
34 { SPOOLSS_FORM_BUILTIN, "Letter", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
35 { SPOOLSS_FORM_BUILTIN, "Letter Small", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
36 { SPOOLSS_FORM_BUILTIN, "Tabloid", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
37 { SPOOLSS_FORM_BUILTIN, "Ledger", {0x696b8,0x44368}, {0x0,0x0,0x696b8,0x44368} },
38 { SPOOLSS_FORM_BUILTIN, "Legal", {0x34b5c,0x56d10}, {0x0,0x0,0x34b5c,0x56d10} },
39 { SPOOLSS_FORM_BUILTIN, "Statement", {0x221b4,0x34b5c}, {0x0,0x0,0x221b4,0x34b5c} },
40 { SPOOLSS_FORM_BUILTIN, "Executive", {0x2cf56,0x411cc}, {0x0,0x0,0x2cf56,0x411cc} },
41 { SPOOLSS_FORM_BUILTIN, "A3", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
42 { SPOOLSS_FORM_BUILTIN, "A4", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
43 { SPOOLSS_FORM_BUILTIN, "A4 Small", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
44 { SPOOLSS_FORM_BUILTIN, "A5", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
45 { SPOOLSS_FORM_BUILTIN, "B4 (JIS)", {0x3ebe8,0x58de0}, {0x0,0x0,0x3ebe8,0x58de0} },
46 { SPOOLSS_FORM_BUILTIN, "B5 (JIS)", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
47 { SPOOLSS_FORM_BUILTIN, "Folio", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
48 { SPOOLSS_FORM_BUILTIN, "Quarto", {0x347d8,0x43238}, {0x0,0x0,0x347d8,0x43238} },
49 { SPOOLSS_FORM_BUILTIN, "10x14", {0x3e030,0x56d10}, {0x0,0x0,0x3e030,0x56d10} },
50 { SPOOLSS_FORM_BUILTIN, "11x17", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
51 { SPOOLSS_FORM_BUILTIN, "Note", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
52 { SPOOLSS_FORM_BUILTIN, "Envelope #9", {0x18079,0x37091}, {0x0,0x0,0x18079,0x37091} },
53 { SPOOLSS_FORM_BUILTIN, "Envelope #10", {0x19947,0x3ae94}, {0x0,0x0,0x19947,0x3ae94} },
54 { SPOOLSS_FORM_BUILTIN, "Envelope #11", {0x1be7c,0x40565}, {0x0,0x0,0x1be7c,0x40565} },
55 { SPOOLSS_FORM_BUILTIN, "Envelope #12", {0x1d74a,0x44368}, {0x0,0x0,0x1d74a,0x44368} },
56 { SPOOLSS_FORM_BUILTIN, "Envelope #14", {0x1f018,0x47504}, {0x0,0x0,0x1f018,0x47504} },
57 { SPOOLSS_FORM_BUILTIN, "C size sheet", {0x696b8,0x886d0}, {0x0,0x0,0x696b8,0x886d0} },
58 { SPOOLSS_FORM_BUILTIN, "D size sheet", {0x886d0,0xd2d70}, {0x0,0x0,0x886d0,0xd2d70} },
59 { SPOOLSS_FORM_BUILTIN, "E size sheet", {0xd2d70,0x110da0},{0x0,0x0,0xd2d70,0x110da0} },
60 { SPOOLSS_FORM_BUILTIN, "Envelope DL", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
61 { SPOOLSS_FORM_BUILTIN, "Envelope C5", {0x278d0,0x37e88}, {0x0,0x0,0x278d0,0x37e88} },
62 { SPOOLSS_FORM_BUILTIN, "Envelope C3", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
63 { SPOOLSS_FORM_BUILTIN, "Envelope C4", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
64 { SPOOLSS_FORM_BUILTIN, "Envelope C6", {0x1bd50,0x278d0}, {0x0,0x0,0x1bd50,0x278d0} },
65 { SPOOLSS_FORM_BUILTIN, "Envelope C65", {0x1bd50,0x37e88}, {0x0,0x0,0x1bd50,0x37e88} },
66 { SPOOLSS_FORM_BUILTIN, "Envelope B4", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
67 { SPOOLSS_FORM_BUILTIN, "Envelope B5", {0x2af80,0x3d090}, {0x0,0x0,0x2af80,0x3d090} },
68 { SPOOLSS_FORM_BUILTIN, "Envelope B6", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
69 { SPOOLSS_FORM_BUILTIN, "Envelope", {0x1adb0,0x38270}, {0x0,0x0,0x1adb0,0x38270} },
70 { SPOOLSS_FORM_BUILTIN, "Envelope Monarch", {0x18079,0x2e824}, {0x0,0x0,0x18079,0x2e824} },
71 { SPOOLSS_FORM_BUILTIN, "6 3/4 Envelope", {0x167ab,0x284ec}, {0x0,0x0,0x167ab,0x284ec} },
72 { SPOOLSS_FORM_BUILTIN, "US Std Fanfold", {0x5c3e1,0x44368}, {0x0,0x0,0x5c3e1,0x44368} },
73 { SPOOLSS_FORM_BUILTIN, "German Std Fanfold", {0x34b5c,0x4a6a0}, {0x0,0x0,0x34b5c,0x4a6a0} },
74 { SPOOLSS_FORM_BUILTIN, "German Legal Fanfold", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
75 { SPOOLSS_FORM_BUILTIN, "B4 (ISO)", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
76 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard", {0x186a0,0x24220}, {0x0,0x0,0x186a0,0x24220} },
77 { SPOOLSS_FORM_BUILTIN, "9x11", {0x37cf8,0x44368}, {0x0,0x0,0x37cf8,0x44368} },
78 { SPOOLSS_FORM_BUILTIN, "10x11", {0x3e030,0x44368}, {0x0,0x0,0x3e030,0x44368} },
79 { SPOOLSS_FORM_BUILTIN, "15x11", {0x5d048,0x44368}, {0x0,0x0,0x5d048,0x44368} },
80 { SPOOLSS_FORM_BUILTIN, "Envelope Invite", {0x35b60,0x35b60}, {0x0,0x0,0x35b60,0x35b60} },
81 { SPOOLSS_FORM_BUILTIN, "Reserved48", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
82 { SPOOLSS_FORM_BUILTIN, "Reserved49", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
83 { SPOOLSS_FORM_BUILTIN, "Letter Extra", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
84 { SPOOLSS_FORM_BUILTIN, "Legal Extra", {0x3ae94,0x5d048}, {0x0,0x0,0x3ae94,0x5d048} },
85 { SPOOLSS_FORM_BUILTIN, "Tabloid Extra", {0x4a6a0,0x6f9f0}, {0x0,0x0,0x4a6a0,0x6f9f0} },
86 { SPOOLSS_FORM_BUILTIN, "A4 Extra", {0x397c2,0x4eb16}, {0x0,0x0,0x397c2,0x4eb16} },
87 { SPOOLSS_FORM_BUILTIN, "Letter Transverse", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
88 { SPOOLSS_FORM_BUILTIN, "A4 Transverse", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
89 { SPOOLSS_FORM_BUILTIN, "Letter Extra Transverse", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
90 { SPOOLSS_FORM_BUILTIN, "Super A", {0x376b8,0x56ea0}, {0x0,0x0,0x376b8,0x56ea0} },
91 { SPOOLSS_FORM_BUILTIN, "Super B", {0x4a768,0x76e58}, {0x0,0x0,0x4a768,0x76e58} },
92 { SPOOLSS_FORM_BUILTIN, "Letter Plus", {0x34b5c,0x4eb16}, {0x0,0x0,0x34b5c,0x4eb16} },
93 { SPOOLSS_FORM_BUILTIN, "A4 Plus", {0x33450,0x50910}, {0x0,0x0,0x33450,0x50910} },
94 { SPOOLSS_FORM_BUILTIN, "A5 Transverse", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
95 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Transverse", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
96 { SPOOLSS_FORM_BUILTIN, "A3 Extra", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
97 { SPOOLSS_FORM_BUILTIN, "A5 Extra", {0x2a7b0,0x395f8}, {0x0,0x0,0x2a7b0,0x395f8} },
98 { SPOOLSS_FORM_BUILTIN, "B5 (ISO) Extra", {0x31128,0x43620}, {0x0,0x0,0x31128,0x43620} },
99 { SPOOLSS_FORM_BUILTIN, "A2", {0x668a0,0x91050}, {0x0,0x0,0x668a0,0x91050} },
100 { SPOOLSS_FORM_BUILTIN, "A3 Transverse", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
101 { SPOOLSS_FORM_BUILTIN, "A3 Extra Transverse", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
102 { SPOOLSS_FORM_BUILTIN, "Japanese Double Postcard", {0x30d40,0x24220}, {0x0,0x0,0x30d40,0x24220} },
103 { SPOOLSS_FORM_BUILTIN, "A6", {0x19a28,0x24220}, {0x0,0x0,0x19a28,0x24220} },
104 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #2", {0x3a980,0x510e0}, {0x0,0x0,0x3a980,0x510e0} },
105 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #3", {0x34bc0,0x43a08}, {0x0,0x0,0x34bc0,0x43a08} },
106 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #3", {0x1d4c0,0x395f8}, {0x0,0x0,0x1d4c0,0x395f8} },
107 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #4", {0x15f90,0x320c8}, {0x0,0x0,0x15f90,0x320c8} },
108 { SPOOLSS_FORM_BUILTIN, "Letter Rotated", {0x44368,0x34b5c}, {0x0,0x0,0x44368,0x34b5c} },
109 { SPOOLSS_FORM_BUILTIN, "A3 Rotated", {0x668a0,0x48828}, {0x0,0x0,0x668a0,0x48828} },
110 { SPOOLSS_FORM_BUILTIN, "A4 Rotated", {0x48828,0x33450}, {0x0,0x0,0x48828,0x33450} },
111 { SPOOLSS_FORM_BUILTIN, "A5 Rotated", {0x33450,0x24220}, {0x0,0x0,0x33450,0x24220} },
112 { SPOOLSS_FORM_BUILTIN, "B4 (JIS) Rotated", {0x58de0,0x3ebe8}, {0x0,0x0,0x58de0,0x3ebe8} },
113 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Rotated", {0x3ebe8,0x2c6f0}, {0x0,0x0,0x3ebe8,0x2c6f0} },
114 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard Rotated", {0x24220,0x186a0}, {0x0,0x0,0x24220,0x186a0} },
115 { SPOOLSS_FORM_BUILTIN, "Double Japan Postcard Rotated", {0x24220,0x30d40}, {0x0,0x0,0x24220,0x30d40} },
116 { SPOOLSS_FORM_BUILTIN, "A6 Rotated", {0x24220,0x19a28}, {0x0,0x0,0x24220,0x19a28} },
117 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #2 Rotated", {0x510e0,0x3a980}, {0x0,0x0,0x510e0,0x3a980} },
118 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #3 Rotated", {0x43a08,0x34bc0}, {0x0,0x0,0x43a08,0x34bc0} },
119 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #3 Rotated", {0x395f8,0x1d4c0}, {0x0,0x0,0x395f8,0x1d4c0} },
120 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #4 Rotated", {0x320c8,0x15f90}, {0x0,0x0,0x320c8,0x15f90} },
121 { SPOOLSS_FORM_BUILTIN, "B6 (JIS)", {0x1f400,0x2c6f0}, {0x0,0x0,0x1f400,0x2c6f0} },
122 { SPOOLSS_FORM_BUILTIN, "B6 (JIS) Rotated", {0x2c6f0,0x1f400}, {0x0,0x0,0x2c6f0,0x1f400} },
123 { SPOOLSS_FORM_BUILTIN, "12x11", {0x4a724,0x443e1}, {0x0,0x0,0x4a724,0x443e1} },
124 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4", {0x19a28,0x395f8}, {0x0,0x0,0x19a28,0x395f8} },
125 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4 Rotated", {0x395f8,0x19a28}, {0x0,0x0,0x395f8,0x19a28} },
126 { SPOOLSS_FORM_BUILTIN, "PRC 16K", {0x2de60,0x3f7a0}, {0x0,0x0,0x2de60,0x3f7a0} },
127 { SPOOLSS_FORM_BUILTIN, "PRC 32K", {0x1fbd0,0x2cec0}, {0x0,0x0,0x1fbd0,0x2cec0} },
128 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big)", {0x222e0,0x318f8}, {0x0,0x0,0x222e0,0x318f8} },
129 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1", {0x18e70,0x28488}, {0x0,0x0,0x18e70,0x28488} },
130 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2", {0x18e70,0x2af80}, {0x0,0x0,0x18e70,0x2af80} },
131 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3", {0x1e848,0x2af80}, {0x0,0x0,0x1e848,0x2af80} },
132 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4", {0x1adb0,0x32c80}, {0x0,0x0,0x1adb0,0x32c80} },
133 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
134 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6", {0x1d4c0,0x38270}, {0x0,0x0,0x1d4c0,0x38270} },
135 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7", {0x27100,0x38270}, {0x0,0x0,0x27100,0x38270} },
136 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8", {0x1d4c0,0x4b708}, {0x0,0x0,0x1d4c0,0x4b708} },
137 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
138 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
139 { SPOOLSS_FORM_BUILTIN, "PRC 16K Rotated", {0x3f7a0,0x2de60}, {0x0,0x0,0x3f7a0,0x2de60} },
140 { SPOOLSS_FORM_BUILTIN, "PRC 32K Rotated", {0x2cec0,0x1fbd0}, {0x0,0x0,0x2cec0,0x1fbd0} },
141 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big) Rotated", {0x318f8,0x222e0}, {0x0,0x0,0x318f8,0x222e0} },
142 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1 Rotated", {0x28488,0x18e70}, {0x0,0x0,0x28488,0x18e70} },
143 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2 Rotated", {0x2af80,0x18e70}, {0x0,0x0,0x2af80,0x18e70} },
144 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3 Rotated", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
145 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4 Rotated", {0x32c80,0x1adb0}, {0x0,0x0,0x32c80,0x1adb0} },
146 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5 Rotated", {0x35b60,0x1adb0}, {0x0,0x0,0x35b60,0x1adb0} },
147 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6 Rotated", {0x38270,0x1d4c0}, {0x0,0x0,0x38270,0x1d4c0} },
148 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7 Rotated", {0x38270,0x27100}, {0x0,0x0,0x38270,0x27100} },
149 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8 Rotated", {0x4b708,0x1d4c0}, {0x0,0x0,0x4b708,0x1d4c0} },
150 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9 Rotated", {0x4f1a0,0x37e88}, {0x0,0x0,0x4f1a0,0x37e88} },
151 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10 Rotated", {0x6fd10,0x4f1a0}, {0x0,0x0,0x6fd10,0x4f1a0} }
154 /********************************************************************
155 static helper functions
156 ********************************************************************/
158 /****************************************************************************
159 Update the changeid time.
160 ****************************************************************************/
164 * @brief Update the ChangeID time of a printer.
166 * This is SO NASTY as some drivers need this to change, others need it
167 * static. This value will change every second, and I must hope that this
168 * is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
171 * @return The ChangeID.
173 static uint32_t winreg_printer_rev_changeid(void)
177 get_process_uptime(&tv);
180 /* Return changeid as msec since spooler restart */
181 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
184 * This setting seems to work well but is too untested
185 * to replace the above calculation. Left in for experiementation
186 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
188 return tv.tv_sec * 10 + tv.tv_usec / 100000;
195 * @brief Connect to the interal winreg server and open the given printer key.
197 * The function will create the needed subkeys if they don't exist.
199 * @param[in] mem_ctx The memory context to use.
201 * @param[in] server_info The supplied server info.
203 * @param[out] winreg_pipe A pointer for the winreg rpc client pipe.
205 * @param[in] path The path to the key to open.
207 * @param[in] key The key to open.
209 * @param[in] create_key Set to true if the key should be created if it
212 * @param[in] access_mask The access mask to open the key.
214 * @param[out] hive_handle A policy handle for the opened hive.
216 * @param[out] key_handle A policy handle for the opened key.
218 * @return WERR_OK on success, the corresponding DOS error
219 * code if something gone wrong.
221 static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
222 struct auth_serversupplied_info *server_info,
223 struct rpc_pipe_client **winreg_pipe,
227 uint32_t access_mask,
228 struct policy_handle *hive_handle,
229 struct policy_handle *key_handle)
231 struct rpc_pipe_client *pipe_handle;
232 struct winreg_String wkey, wkeyclass;
235 WERROR result = WERR_OK;
237 /* create winreg connection */
238 status = rpc_pipe_open_internal(mem_ctx,
239 &ndr_table_winreg.syntax_id,
243 if (!NT_STATUS_IS_OK(status)) {
244 DEBUG(0, ("winreg_printer_openkey: Could not connect to winreg_pipe: %s\n",
246 return ntstatus_to_werror(status);
249 status = rpccli_winreg_OpenHKLM(pipe_handle,
255 if (!NT_STATUS_IS_OK(status)) {
256 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
258 talloc_free(pipe_handle);
259 if (!W_ERROR_IS_OK(result)) {
262 return ntstatus_to_werror(status);
266 keyname = talloc_asprintf(mem_ctx, "%s\\%s", path, key);
268 keyname = talloc_strdup(mem_ctx, path);
270 if (keyname == NULL) {
271 talloc_free(pipe_handle);
279 enum winreg_CreateAction action = REG_ACTION_NONE;
281 ZERO_STRUCT(wkeyclass);
284 status = rpccli_winreg_CreateKey(pipe_handle,
296 case REG_ACTION_NONE:
297 DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
299 case REG_CREATED_NEW_KEY:
300 DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
302 case REG_OPENED_EXISTING_KEY:
303 DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
307 status = rpccli_winreg_OpenKey(pipe_handle,
316 if (!NT_STATUS_IS_OK(status)) {
317 talloc_free(pipe_handle);
318 if (!W_ERROR_IS_OK(result)) {
321 return ntstatus_to_werror(status);
324 *winreg_pipe = pipe_handle;
330 * @brief Create the registry keyname for the given printer.
332 * @param[in] mem_ctx The memory context to use.
334 * @param[in] printer The name of the printer to get the registry key.
336 * @return The registry key or NULL on error.
338 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
339 return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
345 * @brief Enumerate values of an opened key handle and retrieve the data.
347 * @param[in] mem_ctx The memory context to use.
349 * @param[in] pipe_handle The pipe handle for the rpc connection.
351 * @param[in] key_hnd The opened key handle.
353 * @param[out] pnum_values A pointer to store he number of values found.
355 * @param[out] pnum_values A pointer to store the number of values we found.
357 * @return WERR_OK on success, the corresponding DOS error
358 * code if something gone wrong.
360 static WERROR winreg_printer_enumvalues(TALLOC_CTX *mem_ctx,
361 struct rpc_pipe_client *pipe_handle,
362 struct policy_handle *key_hnd,
363 uint32_t *pnum_values,
364 struct spoolss_PrinterEnumValues **penum_values)
367 uint32_t num_subkeys, max_subkeylen, max_classlen;
368 uint32_t num_values, max_valnamelen, max_valbufsize;
369 uint32_t secdescsize;
371 NTTIME last_changed_time;
372 struct winreg_String classname;
374 struct spoolss_PrinterEnumValues *enum_values;
376 WERROR result = WERR_OK;
379 tmp_ctx = talloc_new(mem_ctx);
380 if (tmp_ctx == NULL) {
384 ZERO_STRUCT(classname);
386 status = rpccli_winreg_QueryInfoKey(pipe_handle,
399 if (!NT_STATUS_IS_OK(status)) {
400 DEBUG(0, ("winreg_printer_enumvalues: Could not query info: %s\n",
402 if (!W_ERROR_IS_OK(result)) {
405 result = ntstatus_to_werror(status);
409 if (num_values == 0) {
411 TALLOC_FREE(tmp_ctx);
415 enum_values = TALLOC_ARRAY(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
416 if (enum_values == NULL) {
421 for (i = 0; i < num_values; i++) {
422 struct spoolss_PrinterEnumValues val;
423 struct winreg_ValNameBuf name_buf;
424 enum winreg_Type type = REG_NONE;
425 uint8_t *data = NULL;
431 name_buf.size = max_valnamelen + 2;
434 data_size = max_valbufsize;
435 data = (uint8_t *) TALLOC(tmp_ctx, data_size);
438 status = rpccli_winreg_EnumValue(pipe_handle,
448 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
450 status = NT_STATUS_OK;
454 if (!NT_STATUS_IS_OK(status)) {
455 DEBUG(0, ("winreg_printer_enumvalues: Could not enumerate values: %s\n",
457 if (!W_ERROR_IS_OK(result)) {
460 result = ntstatus_to_werror(status);
464 if (name_buf.name == NULL) {
465 result = WERR_INVALID_PARAMETER;
469 val.value_name = talloc_strdup(enum_values, name_buf.name);
470 if (val.value_name == NULL) {
474 val.value_name_len = strlen_m_term(val.value_name) * 2;
477 val.data_length = data_size;
479 if (val.data_length) {
480 val.data = talloc(enum_values, DATA_BLOB);
481 if (val.data == NULL) {
485 *val.data = data_blob_talloc(enum_values, data, data_size);
488 enum_values[i] = val;
491 *pnum_values = num_values;
493 *penum_values = talloc_move(mem_ctx, &enum_values);
499 TALLOC_FREE(tmp_ctx);
506 * @brief Enumerate subkeys of an opened key handle and get the names.
508 * @param[in] mem_ctx The memory context to use.
510 * @param[in] pipe_handle The pipe handle for the rpc connection.
512 * @param[in] key_hnd The opened key handle.
514 * @param[in] pnum_subkeys A pointer to store the number of found subkeys.
516 * @param[in] psubkeys A pointer to an array to store the found names of
519 * @return WERR_OK on success, the corresponding DOS error
520 * code if something gone wrong.
522 static WERROR winreg_printer_enumkeys(TALLOC_CTX *mem_ctx,
523 struct rpc_pipe_client *pipe_handle,
524 struct policy_handle *key_hnd,
525 uint32_t *pnum_subkeys,
526 const char ***psubkeys)
529 const char **subkeys;
530 uint32_t num_subkeys, max_subkeylen, max_classlen;
531 uint32_t num_values, max_valnamelen, max_valbufsize;
533 NTTIME last_changed_time;
534 uint32_t secdescsize;
535 struct winreg_String classname;
536 WERROR result = WERR_OK;
539 tmp_ctx = talloc_new(mem_ctx);
540 if (tmp_ctx == NULL) {
544 ZERO_STRUCT(classname);
546 status = rpccli_winreg_QueryInfoKey(pipe_handle,
559 if (!NT_STATUS_IS_OK(status)) {
560 DEBUG(0, ("winreg_printer_enumkeys: Could not query info: %s\n",
562 if (!W_ERROR_IS_OK(result)) {
565 result = ntstatus_to_werror(status);
569 subkeys = talloc_zero_array(tmp_ctx, const char *, num_subkeys + 2);
570 if (subkeys == NULL) {
575 if (num_subkeys == 0) {
576 subkeys[0] = talloc_strdup(subkeys, "");
577 if (subkeys[0] == NULL) {
583 *psubkeys = talloc_move(mem_ctx, &subkeys);
586 TALLOC_FREE(tmp_ctx);
590 for (i = 0; i < num_subkeys; i++) {
594 struct winreg_StringBuf class_buf;
595 struct winreg_StringBuf name_buf;
599 class_buf.size = max_classlen + 2;
600 class_buf.length = 0;
603 name_buf.size = max_subkeylen + 2;
606 ZERO_STRUCT(modtime);
608 status = rpccli_winreg_EnumKey(pipe_handle,
616 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
618 status = NT_STATUS_OK;
622 if (!NT_STATUS_IS_OK(status)) {
623 DEBUG(0, ("winreg_printer_enumkeys: Could not enumerate keys: %s\n",
625 if (!W_ERROR_IS_OK(result)) {
628 result = ntstatus_to_werror(status);
632 if (name_buf.name == NULL) {
633 result = WERR_INVALID_PARAMETER;
637 name = talloc_strdup(subkeys, name_buf.name);
646 *pnum_subkeys = num_subkeys;
648 *psubkeys = talloc_move(mem_ctx, &subkeys);
652 TALLOC_FREE(tmp_ctx);
659 * @brief A function to delete a key and its subkeys recurively.
661 * @param[in] mem_ctx The memory context to use.
663 * @param[in] pipe_handle The pipe handle for the rpc connection.
665 * @param[in] hive_handle A opened hive handle to the key.
667 * @param[in] access_mask The access mask to access the key.
669 * @param[in] key The key to delete
671 * @return WERR_OK on success, the corresponding DOS error
672 * code if something gone wrong.
674 static WERROR winreg_printer_delete_subkeys(TALLOC_CTX *mem_ctx,
675 struct rpc_pipe_client *pipe_handle,
676 struct policy_handle *hive_handle,
677 uint32_t access_mask,
680 const char **subkeys = NULL;
681 uint32_t num_subkeys = 0;
682 struct policy_handle key_hnd;
683 struct winreg_String wkey;
684 WERROR result = WERR_OK;
688 ZERO_STRUCT(key_hnd);
691 DEBUG(2, ("winreg_printer_delete_subkeys: delete key %s\n", key));
693 status = rpccli_winreg_OpenKey(pipe_handle,
701 if (!NT_STATUS_IS_OK(status)) {
702 DEBUG(0, ("winreg_printer_delete_subkeys: Could not open key %s: %s\n",
703 wkey.name, nt_errstr(status)));
704 if (!W_ERROR_IS_OK(result)) {
707 return ntstatus_to_werror(status);
710 result = winreg_printer_enumkeys(mem_ctx,
715 if (!W_ERROR_IS_OK(result)) {
719 for (i = 0; i < num_subkeys; i++) {
720 /* create key + subkey */
721 char *subkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkeys[i]);
722 if (subkey == NULL) {
726 DEBUG(2, ("winreg_printer_delete_subkeys: delete subkey %s\n", subkey));
727 result = winreg_printer_delete_subkeys(mem_ctx,
732 if (!W_ERROR_IS_OK(result)) {
737 if (is_valid_policy_hnd(&key_hnd)) {
738 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
743 status = rpccli_winreg_DeleteKey(pipe_handle,
750 if (is_valid_policy_hnd(&key_hnd)) {
751 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
757 static WERROR winreg_printer_write_sz(TALLOC_CTX *mem_ctx,
758 struct rpc_pipe_client *pipe_handle,
759 struct policy_handle *key_handle,
763 struct winreg_String wvalue;
765 WERROR result = WERR_OK;
770 blob = data_blob_string_const("");
772 if (!push_reg_sz(mem_ctx, NULL, &blob, data)) {
773 DEBUG(0, ("winreg_printer_write_sz: Could not marshall string %s for %s\n",
778 status = rpccli_winreg_SetValue(pipe_handle,
786 if (!NT_STATUS_IS_OK(status)) {
787 DEBUG(0, ("winreg_printer_write_sz: Could not set value %s: %s\n",
788 wvalue.name, win_errstr(result)));
789 if (!W_ERROR_IS_OK(result)) {
790 result = ntstatus_to_werror(status);
797 static WERROR winreg_printer_write_dword(TALLOC_CTX *mem_ctx,
798 struct rpc_pipe_client *pipe_handle,
799 struct policy_handle *key_handle,
803 struct winreg_String wvalue;
805 WERROR result = WERR_OK;
809 blob = data_blob_talloc(mem_ctx, NULL, 4);
810 SIVAL(blob.data, 0, data);
812 status = rpccli_winreg_SetValue(pipe_handle,
820 if (!NT_STATUS_IS_OK(status)) {
821 DEBUG(0, ("winreg_printer_write_dword: Could not set value %s: %s\n",
822 wvalue.name, win_errstr(result)));
823 if (!W_ERROR_IS_OK(result)) {
824 result = ntstatus_to_werror(status);
831 static WERROR winreg_printer_write_binary(TALLOC_CTX *mem_ctx,
832 struct rpc_pipe_client *pipe_handle,
833 struct policy_handle *key_handle,
837 struct winreg_String wvalue;
838 WERROR result = WERR_OK;
842 status = rpccli_winreg_SetValue(pipe_handle,
850 if (!NT_STATUS_IS_OK(status)) {
851 DEBUG(0, ("winreg_printer_write_binary: Could not set value %s: %s\n",
852 wvalue.name, win_errstr(result)));
853 if (!W_ERROR_IS_OK(result)) {
854 result = ntstatus_to_werror(status);
861 static WERROR winreg_printer_query_dword(TALLOC_CTX *mem_ctx,
862 struct rpc_pipe_client *pipe_handle,
863 struct policy_handle *key_handle,
867 struct winreg_String wvalue;
868 enum winreg_Type type;
869 WERROR result = WERR_OK;
870 uint32_t value_len = 0;
875 status = rpccli_winreg_QueryValue(pipe_handle,
881 (uint32_t *) &blob.length,
884 if (!NT_STATUS_IS_OK(status)) {
885 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
886 wvalue.name, nt_errstr(status)));
887 if (!W_ERROR_IS_OK(result)) {
890 result = ntstatus_to_werror(status);
894 if (type != REG_DWORD) {
895 result = WERR_INVALID_DATATYPE;
899 if (blob.length != 4) {
900 result = WERR_INVALID_DATA;
904 blob.data = (uint8_t *) TALLOC(mem_ctx, blob.length);
905 if (blob.data == NULL) {
911 status = rpccli_winreg_QueryValue(pipe_handle,
917 (uint32_t *) &blob.length,
920 if (!NT_STATUS_IS_OK(status)) {
921 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
922 wvalue.name, nt_errstr(status)));
923 if (!W_ERROR_IS_OK(result)) {
924 result = ntstatus_to_werror(status);
930 *data = IVAL(blob.data, 0);
936 /********************************************************************
937 Public winreg function for spoolss
938 ********************************************************************/
940 /* Set printer data over the winreg pipe. */
941 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
942 struct auth_serversupplied_info *server_info,
946 enum winreg_Type type,
950 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
951 struct rpc_pipe_client *winreg_pipe = NULL;
952 struct policy_handle hive_hnd, key_hnd;
953 struct winreg_String wvalue;
955 WERROR result = WERR_OK;
959 tmp_ctx = talloc_new(mem_ctx);
960 if (tmp_ctx == NULL) {
964 path = winreg_printer_data_keyname(tmp_ctx, printer);
966 TALLOC_FREE(tmp_ctx);
970 ZERO_STRUCT(hive_hnd);
971 ZERO_STRUCT(key_hnd);
973 DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
974 key, value, access_mask, printer));
975 result = winreg_printer_openkey(tmp_ctx,
984 if (!W_ERROR_IS_OK(result)) {
985 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
986 key, win_errstr(result)));
991 status = rpccli_winreg_SetValue(winreg_pipe,
999 if (!NT_STATUS_IS_OK(status)) {
1000 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
1001 value, nt_errstr(status)));
1002 if (!W_ERROR_IS_OK(result)) {
1005 result = ntstatus_to_werror(status);
1011 if (winreg_pipe != NULL) {
1012 if (is_valid_policy_hnd(&key_hnd)) {
1013 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1015 if (is_valid_policy_hnd(&hive_hnd)) {
1016 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1020 TALLOC_FREE(tmp_ctx);
1024 /* Get printer data over a winreg pipe. */
1025 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
1026 struct auth_serversupplied_info *server_info,
1027 const char *printer,
1030 enum winreg_Type *type,
1032 uint32_t *data_size)
1034 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1035 struct rpc_pipe_client *winreg_pipe = NULL;
1036 struct policy_handle hive_hnd, key_hnd;
1037 struct winreg_String wvalue;
1038 enum winreg_Type type_in;
1041 uint32_t data_in_size = 0;
1042 uint32_t value_len = 0;
1043 WERROR result = WERR_OK;
1045 TALLOC_CTX *tmp_ctx;
1047 tmp_ctx = talloc_new(mem_ctx);
1048 if (tmp_ctx == NULL) {
1052 path = winreg_printer_data_keyname(tmp_ctx, printer);
1054 TALLOC_FREE(tmp_ctx);
1058 ZERO_STRUCT(hive_hnd);
1059 ZERO_STRUCT(key_hnd);
1061 result = winreg_printer_openkey(tmp_ctx,
1070 if (!W_ERROR_IS_OK(result)) {
1071 DEBUG(0, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
1072 key, win_errstr(result)));
1076 wvalue.name = value;
1079 * call QueryValue once with data == NULL to get the
1080 * needed memory size to be allocated, then allocate
1081 * data buffer and call again.
1083 status = rpccli_winreg_QueryValue(winreg_pipe,
1092 if (!NT_STATUS_IS_OK(status)) {
1093 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
1094 value, nt_errstr(status)));
1095 if (!W_ERROR_IS_OK(result)) {
1098 result = ntstatus_to_werror(status);
1102 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
1103 if (data_in == NULL) {
1104 result = WERR_NOMEM;
1109 status = rpccli_winreg_QueryValue(winreg_pipe,
1118 if (!NT_STATUS_IS_OK(status)) {
1119 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
1120 value, nt_errstr(status)));
1121 if (!W_ERROR_IS_OK(result)) {
1122 result = ntstatus_to_werror(status);
1128 *data_size = data_in_size;
1130 *data = talloc_move(mem_ctx, &data_in);
1135 if (winreg_pipe != NULL) {
1136 if (is_valid_policy_hnd(&key_hnd)) {
1137 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1139 if (is_valid_policy_hnd(&hive_hnd)) {
1140 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1144 TALLOC_FREE(tmp_ctx);
1148 /* Enumerate on the values of a given key and provide the data. */
1149 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
1150 struct auth_serversupplied_info *server_info,
1151 const char *printer,
1153 uint32_t *pnum_values,
1154 struct spoolss_PrinterEnumValues **penum_values)
1156 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1157 struct rpc_pipe_client *winreg_pipe = NULL;
1158 struct policy_handle hive_hnd, key_hnd;
1160 struct spoolss_PrinterEnumValues *enum_values = NULL;
1161 uint32_t num_values = 0;
1163 WERROR result = WERR_OK;
1165 TALLOC_CTX *tmp_ctx;
1167 tmp_ctx = talloc_new(mem_ctx);
1168 if (tmp_ctx == NULL) {
1172 path = winreg_printer_data_keyname(tmp_ctx, printer);
1174 TALLOC_FREE(tmp_ctx);
1178 result = winreg_printer_openkey(tmp_ctx,
1187 if (!W_ERROR_IS_OK(result)) {
1188 DEBUG(0, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
1189 key, win_errstr(result)));
1193 result = winreg_printer_enumvalues(tmp_ctx,
1198 if (!W_ERROR_IS_OK(result)) {
1199 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
1200 key, win_errstr(result)));
1204 *pnum_values = num_values;
1206 *penum_values = talloc_move(mem_ctx, &enum_values);
1211 if (winreg_pipe != NULL) {
1212 if (is_valid_policy_hnd(&key_hnd)) {
1213 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1215 if (is_valid_policy_hnd(&hive_hnd)) {
1216 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1220 TALLOC_FREE(tmp_ctx);
1224 /* Delete printer data over a winreg pipe. */
1225 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
1226 struct auth_serversupplied_info *server_info,
1227 const char *printer,
1231 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1232 struct rpc_pipe_client *winreg_pipe = NULL;
1233 struct policy_handle hive_hnd, key_hnd;
1234 struct winreg_String wvalue;
1236 WERROR result = WERR_OK;
1239 TALLOC_CTX *tmp_ctx;
1241 tmp_ctx = talloc_new(mem_ctx);
1242 if (tmp_ctx == NULL) {
1246 path = winreg_printer_data_keyname(tmp_ctx, printer);
1248 TALLOC_FREE(tmp_ctx);
1252 ZERO_STRUCT(hive_hnd);
1253 ZERO_STRUCT(key_hnd);
1255 result = winreg_printer_openkey(tmp_ctx,
1264 if (!W_ERROR_IS_OK(result)) {
1265 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
1266 key, win_errstr(result)));
1270 wvalue.name = value;
1271 status = rpccli_winreg_DeleteValue(winreg_pipe,
1276 if (!NT_STATUS_IS_OK(status)) {
1277 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
1278 value, nt_errstr(status)));
1279 if (!W_ERROR_IS_OK(result)) {
1282 result = ntstatus_to_werror(status);
1288 if (winreg_pipe != NULL) {
1289 if (is_valid_policy_hnd(&key_hnd)) {
1290 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1292 if (is_valid_policy_hnd(&hive_hnd)) {
1293 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1297 TALLOC_FREE(tmp_ctx);
1301 /* Enumerate on the subkeys of a given key and provide the data. */
1302 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
1303 struct auth_serversupplied_info *server_info,
1304 const char *printer,
1306 uint32_t *pnum_subkeys,
1307 const char ***psubkeys)
1309 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1310 struct rpc_pipe_client *winreg_pipe = NULL;
1311 struct policy_handle hive_hnd, key_hnd;
1313 const char **subkeys = NULL;
1314 uint32_t num_subkeys = -1;
1316 WERROR result = WERR_OK;
1318 TALLOC_CTX *tmp_ctx;
1320 tmp_ctx = talloc_new(mem_ctx);
1321 if (tmp_ctx == NULL) {
1325 path = winreg_printer_data_keyname(tmp_ctx, printer);
1327 TALLOC_FREE(tmp_ctx);
1331 ZERO_STRUCT(hive_hnd);
1332 ZERO_STRUCT(key_hnd);
1334 result = winreg_printer_openkey(tmp_ctx,
1343 if (!W_ERROR_IS_OK(result)) {
1344 DEBUG(0, ("winreg_enum_printer_key: Could not open key %s: %s\n",
1345 key, win_errstr(result)));
1349 result = winreg_printer_enumkeys(tmp_ctx,
1354 if (!W_ERROR_IS_OK(result)) {
1355 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
1356 key, win_errstr(result)));
1360 *pnum_subkeys = num_subkeys;
1362 *psubkeys = talloc_move(mem_ctx, &subkeys);
1367 if (winreg_pipe != NULL) {
1368 if (is_valid_policy_hnd(&key_hnd)) {
1369 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1371 if (is_valid_policy_hnd(&hive_hnd)) {
1372 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1376 TALLOC_FREE(tmp_ctx);
1380 /* Delete a key with subkeys of a given printer. */
1381 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
1382 struct auth_serversupplied_info *server_info,
1383 const char *printer,
1386 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1387 struct rpc_pipe_client *winreg_pipe = NULL;
1388 struct policy_handle hive_hnd, key_hnd;
1392 TALLOC_CTX *tmp_ctx;
1394 tmp_ctx = talloc_new(mem_ctx);
1395 if (tmp_ctx == NULL) {
1399 path = winreg_printer_data_keyname(tmp_ctx, printer);
1401 TALLOC_FREE(tmp_ctx);
1405 result = winreg_printer_openkey(tmp_ctx,
1414 if (!W_ERROR_IS_OK(result)) {
1415 /* key doesn't exist */
1416 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1421 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
1422 key, win_errstr(result)));
1426 if (is_valid_policy_hnd(&key_hnd)) {
1427 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1430 keyname = talloc_asprintf(tmp_ctx,
1434 if (keyname == NULL) {
1435 result = WERR_NOMEM;
1439 result = winreg_printer_delete_subkeys(tmp_ctx,
1444 if (!W_ERROR_IS_OK(result)) {
1445 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
1446 key, win_errstr(result)));
1451 if (winreg_pipe != NULL) {
1452 if (is_valid_policy_hnd(&key_hnd)) {
1453 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1455 if (is_valid_policy_hnd(&hive_hnd)) {
1456 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1460 TALLOC_FREE(tmp_ctx);
1464 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
1465 struct auth_serversupplied_info *server_info,
1466 const char *printer)
1468 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1469 struct rpc_pipe_client *winreg_pipe = NULL;
1470 struct policy_handle hive_hnd, key_hnd;
1473 TALLOC_CTX *tmp_ctx;
1475 tmp_ctx = talloc_new(mem_ctx);
1476 if (tmp_ctx == NULL) {
1480 path = winreg_printer_data_keyname(tmp_ctx, printer);
1482 TALLOC_FREE(tmp_ctx);
1486 ZERO_STRUCT(hive_hnd);
1487 ZERO_STRUCT(key_hnd);
1489 result = winreg_printer_openkey(tmp_ctx,
1498 if (!W_ERROR_IS_OK(result)) {
1499 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
1500 path, win_errstr(result)));
1504 result = winreg_printer_write_dword(tmp_ctx,
1508 winreg_printer_rev_changeid());
1509 if (!W_ERROR_IS_OK(result)) {
1515 if (winreg_pipe != NULL) {
1516 if (is_valid_policy_hnd(&key_hnd)) {
1517 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1519 if (is_valid_policy_hnd(&hive_hnd)) {
1520 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1524 TALLOC_FREE(tmp_ctx);
1528 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
1529 struct auth_serversupplied_info *server_info,
1530 const char *printer,
1531 uint32_t *pchangeid)
1533 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1534 struct rpc_pipe_client *winreg_pipe = NULL;
1535 struct policy_handle hive_hnd, key_hnd;
1536 uint32_t changeid = 0;
1539 TALLOC_CTX *tmp_ctx;
1541 tmp_ctx = talloc_new(mem_ctx);
1542 if (tmp_ctx == NULL) {
1546 path = winreg_printer_data_keyname(tmp_ctx, printer);
1548 TALLOC_FREE(tmp_ctx);
1552 ZERO_STRUCT(hive_hnd);
1553 ZERO_STRUCT(key_hnd);
1555 result = winreg_printer_openkey(tmp_ctx,
1564 if (!W_ERROR_IS_OK(result)) {
1565 DEBUG(0, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
1566 path, win_errstr(result)));
1570 DEBUG(0, ("winreg_printer_get_changeid: get changeid from %s\n", path));
1571 result = winreg_printer_query_dword(tmp_ctx,
1576 if (!W_ERROR_IS_OK(result)) {
1581 *pchangeid = changeid;
1586 if (winreg_pipe != NULL) {
1587 if (is_valid_policy_hnd(&key_hnd)) {
1588 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1590 if (is_valid_policy_hnd(&hive_hnd)) {
1591 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1595 TALLOC_FREE(tmp_ctx);
1600 * The special behaviour of the spoolss forms is documented at the website:
1602 * Managing Win32 Printserver Forms
1603 * http://unixwiz.net/techtips/winspooler-forms.html
1606 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
1607 struct auth_serversupplied_info *server_info,
1608 struct spoolss_AddFormInfo1 *form)
1610 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1611 struct rpc_pipe_client *winreg_pipe = NULL;
1612 struct policy_handle hive_hnd, key_hnd;
1613 struct winreg_String wvalue;
1615 uint32_t num_info = 0;
1616 union spoolss_FormInfo *info = NULL;
1620 TALLOC_CTX *tmp_ctx;
1622 tmp_ctx = talloc_new(mem_ctx);
1623 if (tmp_ctx == NULL) {
1627 ZERO_STRUCT(hive_hnd);
1628 ZERO_STRUCT(key_hnd);
1630 result = winreg_printer_openkey(tmp_ctx,
1633 TOP_LEVEL_CONTROL_FORMS_KEY,
1639 if (!W_ERROR_IS_OK(result)) {
1640 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
1641 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1645 result = winreg_printer_enumforms1(tmp_ctx, server_info, &num_info, &info);
1646 if (!W_ERROR_IS_OK(result)) {
1647 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
1648 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1652 /* If form name already exists or is builtin return ALREADY_EXISTS */
1653 for (i = 0; i < num_info; i++) {
1654 if (strequal(info[i].info1.form_name, form->form_name)) {
1655 result = WERR_FILE_EXISTS;
1660 wvalue.name = form->form_name;
1662 blob = data_blob_talloc(tmp_ctx, NULL, 32);
1663 SIVAL(blob.data, 0, form->size.width);
1664 SIVAL(blob.data, 4, form->size.height);
1665 SIVAL(blob.data, 8, form->area.left);
1666 SIVAL(blob.data, 12, form->area.top);
1667 SIVAL(blob.data, 16, form->area.right);
1668 SIVAL(blob.data, 20, form->area.bottom);
1669 SIVAL(blob.data, 24, num_info + 1); /* FIXME */
1670 SIVAL(blob.data, 28, form->flags);
1672 status = rpccli_winreg_SetValue(winreg_pipe,
1680 if (!NT_STATUS_IS_OK(status)) {
1681 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
1682 wvalue.name, nt_errstr(status)));
1683 if (!W_ERROR_IS_OK(result)) {
1686 result = ntstatus_to_werror(status);
1692 if (winreg_pipe != NULL) {
1693 if (is_valid_policy_hnd(&key_hnd)) {
1694 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1696 if (is_valid_policy_hnd(&hive_hnd)) {
1697 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1702 TALLOC_FREE(tmp_ctx);
1706 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
1707 struct auth_serversupplied_info *server_info,
1708 uint32_t *pnum_info,
1709 union spoolss_FormInfo **pinfo)
1711 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1712 struct rpc_pipe_client *winreg_pipe = NULL;
1713 struct policy_handle hive_hnd, key_hnd;
1714 union spoolss_FormInfo *info;
1715 struct spoolss_PrinterEnumValues *enum_values = NULL;
1716 uint32_t num_values = 0;
1717 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
1720 TALLOC_CTX *tmp_ctx;
1722 tmp_ctx = talloc_new(mem_ctx);
1723 if (tmp_ctx == NULL) {
1727 ZERO_STRUCT(hive_hnd);
1728 ZERO_STRUCT(key_hnd);
1730 result = winreg_printer_openkey(tmp_ctx,
1733 TOP_LEVEL_CONTROL_FORMS_KEY,
1739 if (!W_ERROR_IS_OK(result)) {
1740 /* key doesn't exist */
1741 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1746 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
1747 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1751 result = winreg_printer_enumvalues(tmp_ctx,
1756 if (!W_ERROR_IS_OK(result)) {
1757 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
1758 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1762 info = TALLOC_ARRAY(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
1764 result = WERR_NOMEM;
1768 /* Enumerate BUILTIN forms */
1769 for (i = 0; i < num_builtin; i++) {
1770 info[i].info1 = builtin_forms1[i];
1773 /* Enumerate registry forms */
1774 for (i = 0; i < num_values; i++) {
1775 union spoolss_FormInfo val;
1777 if (enum_values[i].type != REG_BINARY ||
1778 enum_values[i].data_length != 32) {
1782 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
1783 if (val.info1.form_name == NULL) {
1784 result = WERR_NOMEM;
1788 val.info1.size.width = IVAL(enum_values[i].data->data, 0);
1789 val.info1.size.height = IVAL(enum_values[i].data->data, 4);
1790 val.info1.area.left = IVAL(enum_values[i].data->data, 8);
1791 val.info1.area.top = IVAL(enum_values[i].data->data, 12);
1792 val.info1.area.right = IVAL(enum_values[i].data->data, 16);
1793 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
1794 /* skip form index IVAL(enum_values[i].data->data, 24)));*/
1795 val.info1.flags = IVAL(enum_values[i].data->data, 28);
1797 info[i + num_builtin] = val;
1800 *pnum_info = num_builtin + num_values;
1802 *pinfo = talloc_move(mem_ctx, &info);
1806 if (winreg_pipe != NULL) {
1807 if (is_valid_policy_hnd(&key_hnd)) {
1808 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1810 if (is_valid_policy_hnd(&hive_hnd)) {
1811 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1815 TALLOC_FREE(enum_values);
1816 TALLOC_FREE(tmp_ctx);
1820 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
1821 struct auth_serversupplied_info *server_info,
1822 const char *form_name)
1824 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1825 struct rpc_pipe_client *winreg_pipe = NULL;
1826 struct policy_handle hive_hnd, key_hnd;
1827 struct winreg_String wvalue;
1828 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
1830 WERROR result = WERR_OK;
1832 TALLOC_CTX *tmp_ctx;
1834 for (i = 0; i < num_builtin; i++) {
1835 if (strequal(builtin_forms1[i].form_name, form_name)) {
1836 return WERR_INVALID_PARAMETER;
1840 tmp_ctx = talloc_new(mem_ctx);
1841 if (tmp_ctx == NULL) {
1845 ZERO_STRUCT(hive_hnd);
1846 ZERO_STRUCT(key_hnd);
1848 result = winreg_printer_openkey(tmp_ctx,
1851 TOP_LEVEL_CONTROL_FORMS_KEY,
1857 if (!W_ERROR_IS_OK(result)) {
1858 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
1859 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1860 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1861 result = WERR_INVALID_FORM_NAME;
1866 wvalue.name = form_name;
1867 status = rpccli_winreg_DeleteValue(winreg_pipe,
1872 if (!NT_STATUS_IS_OK(status)) {
1873 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
1874 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1875 result = WERR_INVALID_FORM_NAME;
1878 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
1879 wvalue.name, nt_errstr(status)));
1880 if (!W_ERROR_IS_OK(result)) {
1883 result = ntstatus_to_werror(status);
1889 if (winreg_pipe != NULL) {
1890 if (is_valid_policy_hnd(&key_hnd)) {
1891 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1893 if (is_valid_policy_hnd(&hive_hnd)) {
1894 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1898 TALLOC_FREE(tmp_ctx);
1902 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
1903 struct auth_serversupplied_info *server_info,
1904 const char *form_name,
1905 struct spoolss_AddFormInfo1 *form)
1907 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1908 struct rpc_pipe_client *winreg_pipe = NULL;
1909 struct policy_handle hive_hnd, key_hnd;
1910 struct winreg_String wvalue;
1912 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
1916 TALLOC_CTX *tmp_ctx = NULL;
1918 for (i = 0; i < num_builtin; i++) {
1919 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
1920 result = WERR_INVALID_PARAM;
1925 tmp_ctx = talloc_new(mem_ctx);
1926 if (tmp_ctx == NULL) {
1930 ZERO_STRUCT(hive_hnd);
1931 ZERO_STRUCT(key_hnd);
1933 result = winreg_printer_openkey(tmp_ctx,
1936 TOP_LEVEL_CONTROL_FORMS_KEY,
1942 if (!W_ERROR_IS_OK(result)) {
1943 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
1944 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1948 /* If form_name != form->form_name then we renamed the form */
1949 if (strequal(form_name, form->form_name)) {
1950 result = winreg_printer_deleteform1(tmp_ctx, server_info, form_name);
1951 if (!W_ERROR_IS_OK(result)) {
1952 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
1953 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
1958 wvalue.name = form->form_name;
1960 blob = data_blob_talloc(tmp_ctx, NULL, 32);
1961 SIVAL(blob.data, 0, form->size.width);
1962 SIVAL(blob.data, 4, form->size.height);
1963 SIVAL(blob.data, 8, form->area.left);
1964 SIVAL(blob.data, 12, form->area.top);
1965 SIVAL(blob.data, 16, form->area.right);
1966 SIVAL(blob.data, 20, form->area.bottom);
1967 SIVAL(blob.data, 24, 42);
1968 SIVAL(blob.data, 28, form->flags);
1970 status = rpccli_winreg_SetValue(winreg_pipe,
1978 if (!NT_STATUS_IS_OK(status)) {
1979 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
1980 wvalue.name, nt_errstr(status)));
1981 if (!W_ERROR_IS_OK(result)) {
1984 result = ntstatus_to_werror(status);
1990 if (winreg_pipe != NULL) {
1991 if (is_valid_policy_hnd(&key_hnd)) {
1992 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1994 if (is_valid_policy_hnd(&hive_hnd)) {
1995 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1999 TALLOC_FREE(tmp_ctx);
2003 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
2004 struct auth_serversupplied_info *server_info,
2005 const char *form_name,
2006 struct spoolss_FormInfo1 *r)
2008 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2009 struct rpc_pipe_client *winreg_pipe = NULL;
2010 struct policy_handle hive_hnd, key_hnd;
2011 struct winreg_String wvalue;
2012 enum winreg_Type type_in;
2014 uint32_t data_in_size = 0;
2015 uint32_t value_len = 0;
2016 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2020 TALLOC_CTX *tmp_ctx;
2022 /* check builtin forms first */
2023 for (i = 0; i < num_builtin; i++) {
2024 if (strequal(builtin_forms1[i].form_name, form_name)) {
2025 *r = builtin_forms1[i];
2030 tmp_ctx = talloc_new(mem_ctx);
2031 if (tmp_ctx == NULL) {
2035 ZERO_STRUCT(hive_hnd);
2036 ZERO_STRUCT(key_hnd);
2038 result = winreg_printer_openkey(tmp_ctx,
2041 TOP_LEVEL_CONTROL_FORMS_KEY,
2047 if (!W_ERROR_IS_OK(result)) {
2048 DEBUG(0, ("winreg_printer_getform1: Could not open key %s: %s\n",
2049 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2053 wvalue.name = form_name;
2056 * call QueryValue once with data == NULL to get the
2057 * needed memory size to be allocated, then allocate
2058 * data buffer and call again.
2060 status = rpccli_winreg_QueryValue(winreg_pipe,
2069 if (!NT_STATUS_IS_OK(status)) {
2070 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
2071 wvalue.name, nt_errstr(status)));
2072 if (!W_ERROR_IS_OK(result)) {
2075 result = ntstatus_to_werror(status);
2079 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2080 if (data_in == NULL) {
2081 result = WERR_NOMEM;
2086 status = rpccli_winreg_QueryValue(winreg_pipe,
2095 if (!NT_STATUS_IS_OK(status)) {
2096 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
2097 wvalue.name, nt_errstr(status)));
2098 if (!W_ERROR_IS_OK(result)) {
2101 result = ntstatus_to_werror(status);
2105 r->form_name = talloc_strdup(mem_ctx, form_name);
2106 if (r->form_name == NULL) {
2107 result = WERR_NOMEM;
2111 r->size.width = IVAL(data_in, 0);
2112 r->size.height = IVAL(data_in, 4);
2113 r->area.left = IVAL(data_in, 8);
2114 r->area.top = IVAL(data_in, 12);
2115 r->area.right = IVAL(data_in, 16);
2116 r->area.bottom = IVAL(data_in, 20);
2117 /* skip index IVAL(data_in, 24)));*/
2118 r->flags = IVAL(data_in, 28);
2122 if (winreg_pipe != NULL) {
2123 if (is_valid_policy_hnd(&key_hnd)) {
2124 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2126 if (is_valid_policy_hnd(&hive_hnd)) {
2127 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2131 TALLOC_FREE(tmp_ctx);