s3-build: only include generated spoolss headers (not ndr headers).
[Samba/ekacnet.git] / source3 / rpc_server / srv_spoolss_util.c
blob4c70ce4edb86c16f6b9f90cb002eb319109364d4
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 "srv_spoolss_util.h"
24 #include "../librpc/gen_ndr/ndr_spoolss.h"
25 #include "../librpc/gen_ndr/srv_winreg.h"
26 #include "../librpc/gen_ndr/cli_winreg.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
29 #define TOP_LEVEL_PRINT_KEY "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print"
30 #define TOP_LEVEL_PRINT_PRINTERS_KEY TOP_LEVEL_PRINT_KEY "\\Printers"
31 #define TOP_LEVEL_CONTROL_KEY "SYSTEM\\CurrentControlSet\\Control\\Print"
32 #define TOP_LEVEL_CONTROL_FORMS_KEY TOP_LEVEL_CONTROL_KEY "\\Forms"
34 #define EMPTY_STRING ""
35 static const char *empty_string_array[1] = { NULL };
36 #define EMPTY_STRING_ARRAY empty_string_array
38 #define CHECK_ERROR(result) \
39 if (W_ERROR_IS_OK(result)) continue; \
40 if (W_ERROR_EQUAL(result, WERR_NOT_FOUND)) result = WERR_OK; \
41 if (!W_ERROR_IS_OK(result)) break
43 /* FLAGS, NAME, with, height, left, top, right, bottom */
44 static const struct spoolss_FormInfo1 builtin_forms1[] = {
45 { SPOOLSS_FORM_BUILTIN, "10x11", {0x3e030,0x44368}, {0x0,0x0,0x3e030,0x44368} },
46 { SPOOLSS_FORM_BUILTIN, "10x14", {0x3e030,0x56d10}, {0x0,0x0,0x3e030,0x56d10} },
47 { SPOOLSS_FORM_BUILTIN, "11x17", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
48 { SPOOLSS_FORM_BUILTIN, "12x11", {0x4a724,0x443e1}, {0x0,0x0,0x4a724,0x443e1} },
49 { SPOOLSS_FORM_BUILTIN, "15x11", {0x5d048,0x44368}, {0x0,0x0,0x5d048,0x44368} },
50 { SPOOLSS_FORM_BUILTIN, "6 3/4 Envelope", {0x167ab,0x284ec}, {0x0,0x0,0x167ab,0x284ec} },
51 { SPOOLSS_FORM_BUILTIN, "9x11", {0x37cf8,0x44368}, {0x0,0x0,0x37cf8,0x44368} },
52 { SPOOLSS_FORM_BUILTIN, "A0", {0xcd528,0x122488},{0x0,0x0,0xcd528,0x122488} },
53 { SPOOLSS_FORM_BUILTIN, "A1", {0x91050,0xcd528}, {0x0,0x0,0x91050,0xcd528} },
54 { SPOOLSS_FORM_BUILTIN, "A2", {0x668a0,0x91050}, {0x0,0x0,0x668a0,0x91050} },
55 { SPOOLSS_FORM_BUILTIN, "A3 Extra Transverse", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
56 { SPOOLSS_FORM_BUILTIN, "A3 Extra", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
57 { SPOOLSS_FORM_BUILTIN, "A3 Rotated", {0x668a0,0x48828}, {0x0,0x0,0x668a0,0x48828} },
58 { SPOOLSS_FORM_BUILTIN, "A3 Transverse", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
59 { SPOOLSS_FORM_BUILTIN, "A3", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
60 { SPOOLSS_FORM_BUILTIN, "A4 Extra", {0x397c2,0x4eb16}, {0x0,0x0,0x397c2,0x4eb16} },
61 { SPOOLSS_FORM_BUILTIN, "A4 Plus", {0x33450,0x50910}, {0x0,0x0,0x33450,0x50910} },
62 { SPOOLSS_FORM_BUILTIN, "A4 Rotated", {0x48828,0x33450}, {0x0,0x0,0x48828,0x33450} },
63 { SPOOLSS_FORM_BUILTIN, "A4 Small", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
64 { SPOOLSS_FORM_BUILTIN, "A4 Transverse", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
65 { SPOOLSS_FORM_BUILTIN, "A4", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
66 { SPOOLSS_FORM_BUILTIN, "A5 Extra", {0x2a7b0,0x395f8}, {0x0,0x0,0x2a7b0,0x395f8} },
67 { SPOOLSS_FORM_BUILTIN, "A5 Rotated", {0x33450,0x24220}, {0x0,0x0,0x33450,0x24220} },
68 { SPOOLSS_FORM_BUILTIN, "A5 Transverse", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
69 { SPOOLSS_FORM_BUILTIN, "A5", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
70 { SPOOLSS_FORM_BUILTIN, "A6 Rotated", {0x24220,0x19a28}, {0x0,0x0,0x24220,0x19a28} },
71 { SPOOLSS_FORM_BUILTIN, "A6", {0x19a28,0x24220}, {0x0,0x0,0x19a28,0x24220} },
72 { SPOOLSS_FORM_BUILTIN, "B4 (ISO)", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
73 { SPOOLSS_FORM_BUILTIN, "B4 (JIS) Rotated", {0x58de0,0x3ebe8}, {0x0,0x0,0x58de0,0x3ebe8} },
74 { SPOOLSS_FORM_BUILTIN, "B4 (JIS)", {0x3ebe8,0x58de0}, {0x0,0x0,0x3ebe8,0x58de0} },
75 { SPOOLSS_FORM_BUILTIN, "B5 (ISO) Extra", {0x31128,0x43620}, {0x0,0x0,0x31128,0x43620} },
76 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Rotated", {0x3ebe8,0x2c6f0}, {0x0,0x0,0x3ebe8,0x2c6f0} },
77 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Transverse", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
78 { SPOOLSS_FORM_BUILTIN, "B5 (JIS)", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
79 { SPOOLSS_FORM_BUILTIN, "B6 (JIS) Rotated", {0x2c6f0,0x1f400}, {0x0,0x0,0x2c6f0,0x1f400} },
80 { SPOOLSS_FORM_BUILTIN, "B6 (JIS)", {0x1f400,0x2c6f0}, {0x0,0x0,0x1f400,0x2c6f0} },
81 { SPOOLSS_FORM_BUILTIN, "C size sheet", {0x696b8,0x886d0}, {0x0,0x0,0x696b8,0x886d0} },
82 { SPOOLSS_FORM_BUILTIN, "D size sheet", {0x886d0,0xd2d70}, {0x0,0x0,0x886d0,0xd2d70} },
83 { SPOOLSS_FORM_BUILTIN, "Double Japan Postcard Rotated", {0x24220,0x30d40}, {0x0,0x0,0x24220,0x30d40} },
84 { SPOOLSS_FORM_BUILTIN, "E size sheet", {0xd2d70,0x110da0},{0x0,0x0,0xd2d70,0x110da0} },
85 { SPOOLSS_FORM_BUILTIN, "Envelope #10", {0x19947,0x3ae94}, {0x0,0x0,0x19947,0x3ae94} },
86 { SPOOLSS_FORM_BUILTIN, "Envelope #11", {0x1be7c,0x40565}, {0x0,0x0,0x1be7c,0x40565} },
87 { SPOOLSS_FORM_BUILTIN, "Envelope #12", {0x1d74a,0x44368}, {0x0,0x0,0x1d74a,0x44368} },
88 { SPOOLSS_FORM_BUILTIN, "Envelope #14", {0x1f018,0x47504}, {0x0,0x0,0x1f018,0x47504} },
89 { SPOOLSS_FORM_BUILTIN, "Envelope #9", {0x18079,0x37091}, {0x0,0x0,0x18079,0x37091} },
90 { SPOOLSS_FORM_BUILTIN, "Envelope B4", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
91 { SPOOLSS_FORM_BUILTIN, "Envelope B5", {0x2af80,0x3d090}, {0x0,0x0,0x2af80,0x3d090} },
92 { SPOOLSS_FORM_BUILTIN, "Envelope B6", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
93 { SPOOLSS_FORM_BUILTIN, "Envelope C3", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
94 { SPOOLSS_FORM_BUILTIN, "Envelope C4", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
95 { SPOOLSS_FORM_BUILTIN, "Envelope C5", {0x278d0,0x37e88}, {0x0,0x0,0x278d0,0x37e88} },
96 { SPOOLSS_FORM_BUILTIN, "Envelope C6", {0x1bd50,0x278d0}, {0x0,0x0,0x1bd50,0x278d0} },
97 { SPOOLSS_FORM_BUILTIN, "Envelope C65", {0x1bd50,0x37e88}, {0x0,0x0,0x1bd50,0x37e88} },
98 { SPOOLSS_FORM_BUILTIN, "Envelope DL", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
99 { SPOOLSS_FORM_BUILTIN, "Envelope Invite", {0x35b60,0x35b60}, {0x0,0x0,0x35b60,0x35b60} },
100 { SPOOLSS_FORM_BUILTIN, "Envelope Monarch", {0x18079,0x2e824}, {0x0,0x0,0x18079,0x2e824} },
101 { SPOOLSS_FORM_BUILTIN, "Envelope", {0x1adb0,0x38270}, {0x0,0x0,0x1adb0,0x38270} },
102 { SPOOLSS_FORM_BUILTIN, "Executive", {0x2cf56,0x411cc}, {0x0,0x0,0x2cf56,0x411cc} },
103 { SPOOLSS_FORM_BUILTIN, "Folio", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
104 { SPOOLSS_FORM_BUILTIN, "German Legal Fanfold", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
105 { SPOOLSS_FORM_BUILTIN, "German Std Fanfold", {0x34b5c,0x4a6a0}, {0x0,0x0,0x34b5c,0x4a6a0} },
106 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #3 Rotated", {0x395f8,0x1d4c0}, {0x0,0x0,0x395f8,0x1d4c0} },
107 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #4 Rotated", {0x320c8,0x15f90}, {0x0,0x0,0x320c8,0x15f90} },
108 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #2 Rotated", {0x510e0,0x3a980}, {0x0,0x0,0x510e0,0x3a980} },
109 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #3 Rotated", {0x43a08,0x34bc0}, {0x0,0x0,0x43a08,0x34bc0} },
110 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4 Rotated", {0x395f8,0x19a28}, {0x0,0x0,0x395f8,0x19a28} },
111 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4", {0x19a28,0x395f8}, {0x0,0x0,0x19a28,0x395f8} },
112 { SPOOLSS_FORM_BUILTIN, "Japanese Double Postcard", {0x30d40,0x24220}, {0x0,0x0,0x30d40,0x24220} },
113 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #3", {0x1d4c0,0x395f8}, {0x0,0x0,0x1d4c0,0x395f8} },
114 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #4", {0x15f90,0x320c8}, {0x0,0x0,0x15f90,0x320c8} },
115 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #2", {0x3a980,0x510e0}, {0x0,0x0,0x3a980,0x510e0} },
116 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #3", {0x34bc0,0x43a08}, {0x0,0x0,0x34bc0,0x43a08} },
117 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard Rotated", {0x24220,0x186a0}, {0x0,0x0,0x24220,0x186a0} },
118 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard", {0x186a0,0x24220}, {0x0,0x0,0x186a0,0x24220} },
119 { SPOOLSS_FORM_BUILTIN, "Ledger", {0x696b8,0x44368}, {0x0,0x0,0x696b8,0x44368} },
120 { SPOOLSS_FORM_BUILTIN, "Legal Extra", {0x3ae94,0x5d048}, {0x0,0x0,0x3ae94,0x5d048} },
121 { SPOOLSS_FORM_BUILTIN, "Legal", {0x34b5c,0x56d10}, {0x0,0x0,0x34b5c,0x56d10} },
122 { SPOOLSS_FORM_BUILTIN, "Letter Extra Transverse", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
123 { SPOOLSS_FORM_BUILTIN, "Letter Extra", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
124 { SPOOLSS_FORM_BUILTIN, "Letter Plus", {0x34b5c,0x4eb16}, {0x0,0x0,0x34b5c,0x4eb16} },
125 { SPOOLSS_FORM_BUILTIN, "Letter Rotated", {0x44368,0x34b5c}, {0x0,0x0,0x44368,0x34b5c} },
126 { SPOOLSS_FORM_BUILTIN, "Letter Small", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
127 { SPOOLSS_FORM_BUILTIN, "Letter Transverse", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
128 { SPOOLSS_FORM_BUILTIN, "Letter", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
129 { SPOOLSS_FORM_BUILTIN, "Note", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
130 { SPOOLSS_FORM_BUILTIN, "PRC 16K Rotated", {0x3f7a0,0x2de60}, {0x0,0x0,0x3f7a0,0x2de60} },
131 { SPOOLSS_FORM_BUILTIN, "PRC 16K", {0x2de60,0x3f7a0}, {0x0,0x0,0x2de60,0x3f7a0} },
132 { SPOOLSS_FORM_BUILTIN, "PRC 32K Rotated", {0x2cec0,0x1fbd0}, {0x0,0x0,0x2cec0,0x1fbd0} },
133 { SPOOLSS_FORM_BUILTIN, "PRC 32K", {0x1fbd0,0x2cec0}, {0x0,0x0,0x1fbd0,0x2cec0} },
134 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big) Rotated", {0x318f8,0x222e0}, {0x0,0x0,0x318f8,0x222e0} },
135 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big)", {0x222e0,0x318f8}, {0x0,0x0,0x222e0,0x318f8} },
136 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1 Rotated", {0x28488,0x18e70}, {0x0,0x0,0x28488,0x18e70} },
137 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1", {0x18e70,0x28488}, {0x0,0x0,0x18e70,0x28488} },
138 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10 Rotated", {0x6fd10,0x4f1a0}, {0x0,0x0,0x6fd10,0x4f1a0} },
139 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
140 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2 Rotated", {0x2af80,0x18e70}, {0x0,0x0,0x2af80,0x18e70} },
141 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2", {0x18e70,0x2af80}, {0x0,0x0,0x18e70,0x2af80} },
142 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3 Rotated", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
143 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3", {0x1e848,0x2af80}, {0x0,0x0,0x1e848,0x2af80} },
144 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4 Rotated", {0x32c80,0x1adb0}, {0x0,0x0,0x32c80,0x1adb0} },
145 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4", {0x1adb0,0x32c80}, {0x0,0x0,0x1adb0,0x32c80} },
146 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5 Rotated", {0x35b60,0x1adb0}, {0x0,0x0,0x35b60,0x1adb0} },
147 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
148 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6 Rotated", {0x38270,0x1d4c0}, {0x0,0x0,0x38270,0x1d4c0} },
149 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6", {0x1d4c0,0x38270}, {0x0,0x0,0x1d4c0,0x38270} },
150 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7 Rotated", {0x38270,0x27100}, {0x0,0x0,0x38270,0x27100} },
151 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7", {0x27100,0x38270}, {0x0,0x0,0x27100,0x38270} },
152 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8 Rotated", {0x4b708,0x1d4c0}, {0x0,0x0,0x4b708,0x1d4c0} },
153 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8", {0x1d4c0,0x4b708}, {0x0,0x0,0x1d4c0,0x4b708} },
154 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9 Rotated", {0x4f1a0,0x37e88}, {0x0,0x0,0x4f1a0,0x37e88} },
155 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
156 { SPOOLSS_FORM_BUILTIN, "Quarto", {0x347d8,0x43238}, {0x0,0x0,0x347d8,0x43238} },
157 { SPOOLSS_FORM_BUILTIN, "Reserved48", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
158 { SPOOLSS_FORM_BUILTIN, "Reserved49", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
159 { SPOOLSS_FORM_BUILTIN, "Statement", {0x221b4,0x34b5c}, {0x0,0x0,0x221b4,0x34b5c} },
160 { SPOOLSS_FORM_BUILTIN, "Super A", {0x376b8,0x56ea0}, {0x0,0x0,0x376b8,0x56ea0} },
161 { SPOOLSS_FORM_BUILTIN, "Super B", {0x4a768,0x76e58}, {0x0,0x0,0x4a768,0x76e58} },
162 { SPOOLSS_FORM_BUILTIN, "Tabloid Extra", {0x4a6a0,0x6f9f0}, {0x0,0x0,0x4a6a0,0x6f9f0} },
163 { SPOOLSS_FORM_BUILTIN, "Tabloid", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
164 { SPOOLSS_FORM_BUILTIN, "US Std Fanfold", {0x5c3e1,0x44368}, {0x0,0x0,0x5c3e1,0x44368} }
167 /********************************************************************
168 static helper functions
169 ********************************************************************/
171 /****************************************************************************
172 Update the changeid time.
173 ****************************************************************************/
175 * @internal
177 * @brief Update the ChangeID time of a printer.
179 * This is SO NASTY as some drivers need this to change, others need it
180 * static. This value will change every second, and I must hope that this
181 * is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
182 * UTAH ! JRA.
184 * @return The ChangeID.
186 static uint32_t winreg_printer_rev_changeid(void)
188 struct timeval tv;
190 get_process_uptime(&tv);
192 #if 1 /* JERRY */
193 /* Return changeid as msec since spooler restart */
194 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
195 #else
197 * This setting seems to work well but is too untested
198 * to replace the above calculation. Left in for experiementation
199 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
201 return tv.tv_sec * 10 + tv.tv_usec / 100000;
202 #endif
206 * @internal
208 * @brief Connect to the interal winreg server and open the given printer key.
210 * The function will create the needed subkeys if they don't exist.
212 * @param[in] mem_ctx The memory context to use.
214 * @param[in] server_info The supplied server info.
216 * @param[out] winreg_pipe A pointer for the winreg rpc client pipe.
218 * @param[in] path The path to the key to open.
220 * @param[in] key The key to open.
222 * @param[in] create_key Set to true if the key should be created if it
223 * doesn't exist.
225 * @param[in] access_mask The access mask to open the key.
227 * @param[out] hive_handle A policy handle for the opened hive.
229 * @param[out] key_handle A policy handle for the opened key.
231 * @return WERR_OK on success, the corresponding DOS error
232 * code if something gone wrong.
234 static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
235 struct auth_serversupplied_info *server_info,
236 struct rpc_pipe_client **winreg_pipe,
237 const char *path,
238 const char *key,
239 bool create_key,
240 uint32_t access_mask,
241 struct policy_handle *hive_handle,
242 struct policy_handle *key_handle)
244 struct rpc_pipe_client *pipe_handle;
245 struct winreg_String wkey, wkeyclass;
246 char *keyname;
247 NTSTATUS status;
248 WERROR result = WERR_OK;
250 /* create winreg connection */
251 status = rpc_pipe_open_internal(mem_ctx,
252 &ndr_table_winreg.syntax_id,
253 rpc_winreg_dispatch,
254 server_info,
255 &pipe_handle);
256 if (!NT_STATUS_IS_OK(status)) {
257 DEBUG(0, ("winreg_printer_openkey: Could not connect to winreg_pipe: %s\n",
258 nt_errstr(status)));
259 return ntstatus_to_werror(status);
262 status = rpccli_winreg_OpenHKLM(pipe_handle,
263 mem_ctx,
264 NULL,
265 access_mask,
266 hive_handle,
267 &result);
268 if (!NT_STATUS_IS_OK(status)) {
269 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
270 nt_errstr(status)));
271 talloc_free(pipe_handle);
272 if (!W_ERROR_IS_OK(result)) {
273 return result;
275 return ntstatus_to_werror(status);
278 if (key && *key) {
279 keyname = talloc_asprintf(mem_ctx, "%s\\%s", path, key);
280 } else {
281 keyname = talloc_strdup(mem_ctx, path);
283 if (keyname == NULL) {
284 talloc_free(pipe_handle);
285 return WERR_NOMEM;
288 ZERO_STRUCT(wkey);
289 wkey.name = keyname;
291 if (create_key) {
292 enum winreg_CreateAction action = REG_ACTION_NONE;
294 ZERO_STRUCT(wkeyclass);
295 wkeyclass.name = "";
297 status = rpccli_winreg_CreateKey(pipe_handle,
298 mem_ctx,
299 hive_handle,
300 wkey,
301 wkeyclass,
303 access_mask,
304 NULL,
305 key_handle,
306 &action,
307 &result);
308 switch (action) {
309 case REG_ACTION_NONE:
310 DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
311 break;
312 case REG_CREATED_NEW_KEY:
313 DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
314 break;
315 case REG_OPENED_EXISTING_KEY:
316 DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
317 break;
319 } else {
320 status = rpccli_winreg_OpenKey(pipe_handle,
321 mem_ctx,
322 hive_handle,
323 wkey,
325 access_mask,
326 key_handle,
327 &result);
329 if (!NT_STATUS_IS_OK(status)) {
330 talloc_free(pipe_handle);
331 if (!W_ERROR_IS_OK(result)) {
332 return result;
334 return ntstatus_to_werror(status);
337 *winreg_pipe = pipe_handle;
339 return WERR_OK;
343 * @brief Create the registry keyname for the given printer.
345 * @param[in] mem_ctx The memory context to use.
347 * @param[in] printer The name of the printer to get the registry key.
349 * @return The registry key or NULL on error.
351 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
352 return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
356 * @internal
358 * @brief Enumerate values of an opened key handle and retrieve the data.
360 * @param[in] mem_ctx The memory context to use.
362 * @param[in] pipe_handle The pipe handle for the rpc connection.
364 * @param[in] key_hnd The opened key handle.
366 * @param[out] pnum_values A pointer to store he number of values found.
368 * @param[out] pnum_values A pointer to store the number of values we found.
370 * @return WERR_OK on success, the corresponding DOS error
371 * code if something gone wrong.
373 static WERROR winreg_printer_enumvalues(TALLOC_CTX *mem_ctx,
374 struct rpc_pipe_client *pipe_handle,
375 struct policy_handle *key_hnd,
376 uint32_t *pnum_values,
377 struct spoolss_PrinterEnumValues **penum_values)
379 TALLOC_CTX *tmp_ctx;
380 uint32_t num_subkeys, max_subkeylen, max_classlen;
381 uint32_t num_values, max_valnamelen, max_valbufsize;
382 uint32_t secdescsize;
383 uint32_t i;
384 NTTIME last_changed_time;
385 struct winreg_String classname;
387 struct spoolss_PrinterEnumValues *enum_values;
389 WERROR result = WERR_OK;
390 NTSTATUS status;
392 tmp_ctx = talloc_new(mem_ctx);
393 if (tmp_ctx == NULL) {
394 return WERR_NOMEM;
397 ZERO_STRUCT(classname);
399 status = rpccli_winreg_QueryInfoKey(pipe_handle,
400 tmp_ctx,
401 key_hnd,
402 &classname,
403 &num_subkeys,
404 &max_subkeylen,
405 &max_classlen,
406 &num_values,
407 &max_valnamelen,
408 &max_valbufsize,
409 &secdescsize,
410 &last_changed_time,
411 &result);
412 if (!NT_STATUS_IS_OK(status)) {
413 DEBUG(0, ("winreg_printer_enumvalues: Could not query info: %s\n",
414 nt_errstr(status)));
415 if (!W_ERROR_IS_OK(result)) {
416 goto error;
418 result = ntstatus_to_werror(status);
419 goto error;
422 if (num_values == 0) {
423 *pnum_values = 0;
424 TALLOC_FREE(tmp_ctx);
425 return WERR_OK;
428 enum_values = TALLOC_ARRAY(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
429 if (enum_values == NULL) {
430 result = WERR_NOMEM;
431 goto error;
434 for (i = 0; i < num_values; i++) {
435 struct spoolss_PrinterEnumValues val;
436 struct winreg_ValNameBuf name_buf;
437 enum winreg_Type type = REG_NONE;
438 uint8_t *data = NULL;
439 uint32_t data_size;
440 uint32_t length;
441 char n = '\0';;
443 name_buf.name = &n;
444 name_buf.size = max_valnamelen + 2;
445 name_buf.length = 0;
447 data_size = max_valbufsize;
448 data = (uint8_t *) TALLOC(tmp_ctx, data_size);
449 length = 0;
451 status = rpccli_winreg_EnumValue(pipe_handle,
452 tmp_ctx,
453 key_hnd,
455 &name_buf,
456 &type,
457 data,
458 &data_size,
459 &length,
460 &result);
461 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
462 result = WERR_OK;
463 status = NT_STATUS_OK;
464 break;
467 if (!NT_STATUS_IS_OK(status)) {
468 DEBUG(0, ("winreg_printer_enumvalues: Could not enumerate values: %s\n",
469 nt_errstr(status)));
470 if (!W_ERROR_IS_OK(result)) {
471 goto error;
473 result = ntstatus_to_werror(status);
474 goto error;
477 if (name_buf.name == NULL) {
478 result = WERR_INVALID_PARAMETER;
479 goto error;
482 val.value_name = talloc_strdup(enum_values, name_buf.name);
483 if (val.value_name == NULL) {
484 result = WERR_NOMEM;
485 goto error;
487 val.value_name_len = strlen_m_term(val.value_name) * 2;
489 val.type = type;
490 val.data_length = data_size;
491 val.data = NULL;
492 if (val.data_length) {
493 val.data = talloc(enum_values, DATA_BLOB);
494 if (val.data == NULL) {
495 result = WERR_NOMEM;
496 goto error;
498 *val.data = data_blob_talloc(enum_values, data, data_size);
501 enum_values[i] = val;
504 *pnum_values = num_values;
505 if (penum_values) {
506 *penum_values = talloc_move(mem_ctx, &enum_values);
509 result = WERR_OK;
511 error:
512 TALLOC_FREE(tmp_ctx);
513 return result;
517 * @internal
519 * @brief Enumerate subkeys of an opened key handle and get the names.
521 * @param[in] mem_ctx The memory context to use.
523 * @param[in] pipe_handle The pipe handle for the rpc connection.
525 * @param[in] key_hnd The opened key handle.
527 * @param[in] pnum_subkeys A pointer to store the number of found subkeys.
529 * @param[in] psubkeys A pointer to an array to store the found names of
530 * subkeys.
532 * @return WERR_OK on success, the corresponding DOS error
533 * code if something gone wrong.
535 static WERROR winreg_printer_enumkeys(TALLOC_CTX *mem_ctx,
536 struct rpc_pipe_client *pipe_handle,
537 struct policy_handle *key_hnd,
538 uint32_t *pnum_subkeys,
539 const char ***psubkeys)
541 TALLOC_CTX *tmp_ctx;
542 const char **subkeys;
543 uint32_t num_subkeys, max_subkeylen, max_classlen;
544 uint32_t num_values, max_valnamelen, max_valbufsize;
545 uint32_t i;
546 NTTIME last_changed_time;
547 uint32_t secdescsize;
548 struct winreg_String classname;
549 WERROR result = WERR_OK;
550 NTSTATUS status;
552 tmp_ctx = talloc_new(mem_ctx);
553 if (tmp_ctx == NULL) {
554 return WERR_NOMEM;
557 ZERO_STRUCT(classname);
559 status = rpccli_winreg_QueryInfoKey(pipe_handle,
560 tmp_ctx,
561 key_hnd,
562 &classname,
563 &num_subkeys,
564 &max_subkeylen,
565 &max_classlen,
566 &num_values,
567 &max_valnamelen,
568 &max_valbufsize,
569 &secdescsize,
570 &last_changed_time,
571 &result);
572 if (!NT_STATUS_IS_OK(status)) {
573 DEBUG(0, ("winreg_printer_enumkeys: Could not query info: %s\n",
574 nt_errstr(status)));
575 if (!W_ERROR_IS_OK(result)) {
576 goto error;
578 result = ntstatus_to_werror(status);
579 goto error;
582 subkeys = talloc_zero_array(tmp_ctx, const char *, num_subkeys + 2);
583 if (subkeys == NULL) {
584 result = WERR_NOMEM;
585 goto error;
588 if (num_subkeys == 0) {
589 subkeys[0] = talloc_strdup(subkeys, "");
590 if (subkeys[0] == NULL) {
591 result = WERR_NOMEM;
592 goto error;
594 *pnum_subkeys = 0;
595 if (psubkeys) {
596 *psubkeys = talloc_move(mem_ctx, &subkeys);
599 TALLOC_FREE(tmp_ctx);
600 return WERR_OK;
603 for (i = 0; i < num_subkeys; i++) {
604 char c = '\0';
605 char n = '\0';
606 char *name = NULL;
607 struct winreg_StringBuf class_buf;
608 struct winreg_StringBuf name_buf;
609 NTTIME modtime;
611 class_buf.name = &c;
612 class_buf.size = max_classlen + 2;
613 class_buf.length = 0;
615 name_buf.name = &n;
616 name_buf.size = max_subkeylen + 2;
617 name_buf.length = 0;
619 ZERO_STRUCT(modtime);
621 status = rpccli_winreg_EnumKey(pipe_handle,
622 tmp_ctx,
623 key_hnd,
625 &name_buf,
626 &class_buf,
627 &modtime,
628 &result);
629 if (W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
630 result = WERR_OK;
631 status = NT_STATUS_OK;
632 break;
635 if (!NT_STATUS_IS_OK(status)) {
636 DEBUG(0, ("winreg_printer_enumkeys: Could not enumerate keys: %s\n",
637 nt_errstr(status)));
638 if (!W_ERROR_IS_OK(result)) {
639 goto error;
641 result = ntstatus_to_werror(status);
642 goto error;
645 if (name_buf.name == NULL) {
646 result = WERR_INVALID_PARAMETER;
647 goto error;
650 name = talloc_strdup(subkeys, name_buf.name);
651 if (name == NULL) {
652 result = WERR_NOMEM;
653 goto error;
656 subkeys[i] = name;
659 *pnum_subkeys = num_subkeys;
660 if (psubkeys) {
661 *psubkeys = talloc_move(mem_ctx, &subkeys);
664 error:
665 TALLOC_FREE(tmp_ctx);
666 return result;
670 * @internal
672 * @brief A function to delete a key and its subkeys recurively.
674 * @param[in] mem_ctx The memory context to use.
676 * @param[in] pipe_handle The pipe handle for the rpc connection.
678 * @param[in] hive_handle A opened hive handle to the key.
680 * @param[in] access_mask The access mask to access the key.
682 * @param[in] key The key to delete
684 * @return WERR_OK on success, the corresponding DOS error
685 * code if something gone wrong.
687 static WERROR winreg_printer_delete_subkeys(TALLOC_CTX *mem_ctx,
688 struct rpc_pipe_client *pipe_handle,
689 struct policy_handle *hive_handle,
690 uint32_t access_mask,
691 const char *key)
693 const char **subkeys = NULL;
694 uint32_t num_subkeys = 0;
695 struct policy_handle key_hnd;
696 struct winreg_String wkey;
697 WERROR result = WERR_OK;
698 NTSTATUS status;
699 uint32_t i;
701 ZERO_STRUCT(key_hnd);
702 wkey.name = key;
704 DEBUG(2, ("winreg_printer_delete_subkeys: delete key %s\n", key));
705 /* open the key */
706 status = rpccli_winreg_OpenKey(pipe_handle,
707 mem_ctx,
708 hive_handle,
709 wkey,
711 access_mask,
712 &key_hnd,
713 &result);
714 if (!NT_STATUS_IS_OK(status)) {
715 DEBUG(0, ("winreg_printer_delete_subkeys: Could not open key %s: %s\n",
716 wkey.name, nt_errstr(status)));
717 if (!W_ERROR_IS_OK(result)) {
718 return result;
720 return ntstatus_to_werror(status);
723 result = winreg_printer_enumkeys(mem_ctx,
724 pipe_handle,
725 &key_hnd,
726 &num_subkeys,
727 &subkeys);
728 if (!W_ERROR_IS_OK(result)) {
729 goto done;
732 for (i = 0; i < num_subkeys; i++) {
733 /* create key + subkey */
734 char *subkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkeys[i]);
735 if (subkey == NULL) {
736 goto done;
739 DEBUG(2, ("winreg_printer_delete_subkeys: delete subkey %s\n", subkey));
740 result = winreg_printer_delete_subkeys(mem_ctx,
741 pipe_handle,
742 hive_handle,
743 access_mask,
744 subkey);
745 if (!W_ERROR_IS_OK(result)) {
746 goto done;
750 if (is_valid_policy_hnd(&key_hnd)) {
751 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
754 wkey.name = key;
756 status = rpccli_winreg_DeleteKey(pipe_handle,
757 mem_ctx,
758 hive_handle,
759 wkey,
760 &result);
762 done:
763 if (is_valid_policy_hnd(&key_hnd)) {
764 rpccli_winreg_CloseKey(pipe_handle, mem_ctx, &key_hnd, NULL);
767 return result;
770 static WERROR winreg_printer_write_sz(TALLOC_CTX *mem_ctx,
771 struct rpc_pipe_client *pipe_handle,
772 struct policy_handle *key_handle,
773 const char *value,
774 const char *data)
776 struct winreg_String wvalue;
777 DATA_BLOB blob;
778 WERROR result = WERR_OK;
779 NTSTATUS status;
781 wvalue.name = value;
782 if (data == NULL) {
783 blob = data_blob_string_const("");
784 } else {
785 if (!push_reg_sz(mem_ctx, &blob, data)) {
786 DEBUG(0, ("winreg_printer_write_sz: Could not marshall string %s for %s\n",
787 data, wvalue.name));
788 return WERR_NOMEM;
791 status = rpccli_winreg_SetValue(pipe_handle,
792 mem_ctx,
793 key_handle,
794 wvalue,
795 REG_SZ,
796 blob.data,
797 blob.length,
798 &result);
799 if (!NT_STATUS_IS_OK(status)) {
800 DEBUG(0, ("winreg_printer_write_sz: Could not set value %s: %s\n",
801 wvalue.name, win_errstr(result)));
802 if (!W_ERROR_IS_OK(result)) {
803 result = ntstatus_to_werror(status);
807 return result;
810 static WERROR winreg_printer_write_dword(TALLOC_CTX *mem_ctx,
811 struct rpc_pipe_client *pipe_handle,
812 struct policy_handle *key_handle,
813 const char *value,
814 uint32_t data)
816 struct winreg_String wvalue;
817 DATA_BLOB blob;
818 WERROR result = WERR_OK;
819 NTSTATUS status;
821 wvalue.name = value;
822 blob = data_blob_talloc(mem_ctx, NULL, 4);
823 SIVAL(blob.data, 0, data);
825 status = rpccli_winreg_SetValue(pipe_handle,
826 mem_ctx,
827 key_handle,
828 wvalue,
829 REG_DWORD,
830 blob.data,
831 blob.length,
832 &result);
833 if (!NT_STATUS_IS_OK(status)) {
834 DEBUG(0, ("winreg_printer_write_dword: Could not set value %s: %s\n",
835 wvalue.name, win_errstr(result)));
836 if (!W_ERROR_IS_OK(result)) {
837 result = ntstatus_to_werror(status);
841 return result;
844 static WERROR winreg_printer_write_binary(TALLOC_CTX *mem_ctx,
845 struct rpc_pipe_client *pipe_handle,
846 struct policy_handle *key_handle,
847 const char *value,
848 DATA_BLOB blob)
850 struct winreg_String wvalue;
851 WERROR result = WERR_OK;
852 NTSTATUS status;
854 wvalue.name = value;
855 status = rpccli_winreg_SetValue(pipe_handle,
856 mem_ctx,
857 key_handle,
858 wvalue,
859 REG_BINARY,
860 blob.data,
861 blob.length,
862 &result);
863 if (!NT_STATUS_IS_OK(status)) {
864 DEBUG(0, ("winreg_printer_write_binary: Could not set value %s: %s\n",
865 wvalue.name, win_errstr(result)));
866 if (!W_ERROR_IS_OK(result)) {
867 result = ntstatus_to_werror(status);
871 return result;
874 static WERROR winreg_printer_query_binary(TALLOC_CTX *mem_ctx,
875 struct rpc_pipe_client *pipe_handle,
876 struct policy_handle *key_handle,
877 const char *value,
878 DATA_BLOB *data)
880 struct winreg_String wvalue;
881 enum winreg_Type type;
882 WERROR result = WERR_OK;
883 uint32_t value_len = 0;
884 uint32_t data_size = 0;
885 NTSTATUS status;
886 DATA_BLOB blob;
888 wvalue.name = value;
889 status = rpccli_winreg_QueryValue(pipe_handle,
890 mem_ctx,
891 key_handle,
892 &wvalue,
893 &type,
894 NULL,
895 &data_size,
896 &value_len,
897 &result);
898 if (!NT_STATUS_IS_OK(status)) {
899 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
900 wvalue.name, nt_errstr(status)));
901 if (!W_ERROR_IS_OK(result)) {
902 goto done;
904 result = ntstatus_to_werror(status);
905 goto done;
908 if (type != REG_BINARY) {
909 result = WERR_INVALID_DATATYPE;
910 goto done;
912 blob = data_blob_talloc(mem_ctx, NULL, data_size);
913 if (blob.data == NULL) {
914 result = WERR_NOMEM;
915 goto done;
917 value_len = 0;
919 status = rpccli_winreg_QueryValue(pipe_handle,
920 mem_ctx,
921 key_handle,
922 &wvalue,
923 &type,
924 blob.data,
925 &data_size,
926 &value_len,
927 &result);
928 if (!NT_STATUS_IS_OK(status)) {
929 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
930 wvalue.name, nt_errstr(status)));
931 if (!W_ERROR_IS_OK(result)) {
932 result = ntstatus_to_werror(status);
934 goto done;
937 if (data) {
938 data->data = blob.data;
939 data->length = blob.length;
941 done:
942 return result;
945 static WERROR winreg_printer_query_dword(TALLOC_CTX *mem_ctx,
946 struct rpc_pipe_client *pipe_handle,
947 struct policy_handle *key_handle,
948 const char *value,
949 uint32_t *data)
951 struct winreg_String wvalue;
952 enum winreg_Type type;
953 WERROR result = WERR_OK;
954 uint32_t value_len = 0;
955 uint32_t data_size = 0;
956 NTSTATUS status;
957 DATA_BLOB blob;
959 wvalue.name = value;
960 status = rpccli_winreg_QueryValue(pipe_handle,
961 mem_ctx,
962 key_handle,
963 &wvalue,
964 &type,
965 NULL,
966 &data_size,
967 &value_len,
968 &result);
969 if (!NT_STATUS_IS_OK(status)) {
970 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
971 wvalue.name, nt_errstr(status)));
972 if (!W_ERROR_IS_OK(result)) {
973 goto done;
975 result = ntstatus_to_werror(status);
976 goto done;
979 if (type != REG_DWORD) {
980 result = WERR_INVALID_DATATYPE;
981 goto done;
984 if (data_size != 4) {
985 result = WERR_INVALID_DATA;
986 goto done;
989 blob = data_blob_talloc(mem_ctx, NULL, data_size);
990 if (blob.data == NULL) {
991 result = WERR_NOMEM;
992 goto done;
994 value_len = 0;
996 status = rpccli_winreg_QueryValue(pipe_handle,
997 mem_ctx,
998 key_handle,
999 &wvalue,
1000 &type,
1001 blob.data,
1002 &data_size,
1003 &value_len,
1004 &result);
1005 if (!NT_STATUS_IS_OK(status)) {
1006 DEBUG(0, ("winreg_printer_query_dword: Could not query value %s: %s\n",
1007 wvalue.name, nt_errstr(status)));
1008 if (!W_ERROR_IS_OK(result)) {
1009 result = ntstatus_to_werror(status);
1011 goto done;
1014 if (data) {
1015 *data = IVAL(blob.data, 0);
1017 done:
1018 return result;
1021 static WERROR winreg_printer_write_multi_sz(TALLOC_CTX *mem_ctx,
1022 struct rpc_pipe_client *pipe_handle,
1023 struct policy_handle *key_handle,
1024 const char *value,
1025 const char **data)
1027 struct winreg_String wvalue;
1028 DATA_BLOB blob;
1029 WERROR result = WERR_OK;
1030 NTSTATUS status;
1032 wvalue.name = value;
1033 if (!push_reg_multi_sz(mem_ctx, &blob, data)) {
1034 return WERR_NOMEM;
1036 status = rpccli_winreg_SetValue(pipe_handle,
1037 mem_ctx,
1038 key_handle,
1039 wvalue,
1040 REG_MULTI_SZ,
1041 blob.data,
1042 blob.length,
1043 &result);
1044 if (!NT_STATUS_IS_OK(status)) {
1045 DEBUG(0, ("winreg_printer_write_multi_sz: Could not set value %s: %s\n",
1046 wvalue.name, win_errstr(result)));
1047 if (!W_ERROR_IS_OK(result)) {
1048 result = ntstatus_to_werror(status);
1052 return result;
1055 static WERROR winreg_printer_opendriver(TALLOC_CTX *mem_ctx,
1056 struct auth_serversupplied_info *server_info,
1057 const char *drivername,
1058 const char *architecture,
1059 uint32_t version,
1060 uint32_t access_mask,
1061 bool create,
1062 struct rpc_pipe_client **winreg_pipe,
1063 struct policy_handle *hive_hnd,
1064 struct policy_handle *key_hnd)
1066 WERROR result;
1067 char *key_name;
1069 key_name = talloc_asprintf(mem_ctx, "%s\\Environments\\%s\\Drivers\\Version-%u",
1070 TOP_LEVEL_CONTROL_KEY,
1071 architecture, version);
1072 if (!key_name) {
1073 return WERR_NOMEM;
1076 result = winreg_printer_openkey(mem_ctx,
1077 server_info,
1078 winreg_pipe,
1079 key_name,
1080 drivername,
1081 create,
1082 access_mask,
1083 hive_hnd,
1084 key_hnd);
1085 return result;
1088 static WERROR winreg_enumval_to_dword(TALLOC_CTX *mem_ctx,
1089 struct spoolss_PrinterEnumValues *v,
1090 const char *valuename, uint32_t *dw)
1092 /* just return if it is not the one we are looking for */
1093 if (strcmp(valuename, v->value_name) != 0) {
1094 return WERR_NOT_FOUND;
1097 if (v->type != REG_DWORD) {
1098 return WERR_INVALID_DATATYPE;
1101 *dw = IVAL(v->data->data, 0);
1102 return WERR_OK;
1105 static WERROR winreg_enumval_to_sz(TALLOC_CTX *mem_ctx,
1106 struct spoolss_PrinterEnumValues *v,
1107 const char *valuename, const char **_str)
1109 /* just return if it is not the one we are looking for */
1110 if (strcmp(valuename, v->value_name) != 0) {
1111 return WERR_NOT_FOUND;
1114 if (v->type != REG_SZ) {
1115 return WERR_INVALID_DATATYPE;
1118 if (!pull_reg_sz(mem_ctx, v->data, _str)) {
1119 return WERR_NOMEM;
1122 return WERR_OK;
1125 static WERROR winreg_enumval_to_multi_sz(TALLOC_CTX *mem_ctx,
1126 struct spoolss_PrinterEnumValues *v,
1127 const char *valuename,
1128 const char ***array)
1130 /* just return if it is not the one we are looking for */
1131 if (strcmp(valuename, v->value_name) != 0) {
1132 return WERR_NOT_FOUND;
1135 if (v->type != REG_MULTI_SZ) {
1136 return WERR_INVALID_DATATYPE;
1139 if (!pull_reg_multi_sz(mem_ctx, v->data, array)) {
1140 return WERR_NOMEM;
1143 return WERR_OK;
1146 static WERROR winreg_printer_write_date(TALLOC_CTX *mem_ctx,
1147 struct rpc_pipe_client *pipe_handle,
1148 struct policy_handle *key_handle,
1149 const char *value,
1150 NTTIME data)
1152 struct winreg_String wvalue;
1153 DATA_BLOB blob;
1154 WERROR result = WERR_OK;
1155 NTSTATUS status;
1156 const char *str;
1157 struct tm *tm;
1158 time_t t;
1160 t = nt_time_to_unix(data);
1161 tm = localtime(&t);
1162 str = talloc_asprintf(mem_ctx, "%02d/%02d/%04d",
1163 tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
1164 if (!str) {
1165 return WERR_NOMEM;
1168 wvalue.name = value;
1169 if (!push_reg_sz(mem_ctx, &blob, str)) {
1170 return WERR_NOMEM;
1172 status = rpccli_winreg_SetValue(pipe_handle,
1173 mem_ctx,
1174 key_handle,
1175 wvalue,
1176 REG_SZ,
1177 blob.data,
1178 blob.length,
1179 &result);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
1182 wvalue.name, win_errstr(result)));
1183 if (!W_ERROR_IS_OK(result)) {
1184 result = ntstatus_to_werror(status);
1188 return result;
1191 static WERROR winreg_printer_date_to_NTTIME(const char *str, NTTIME *data)
1193 struct tm tm;
1194 time_t t;
1196 ZERO_STRUCT(tm);
1198 if (sscanf(str, "%d/%d/%d",
1199 &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
1200 return WERR_INVALID_PARAMETER;
1202 tm.tm_mon -= 1;
1203 tm.tm_year -= 1900;
1204 tm.tm_isdst = -1;
1206 t = mktime(&tm);
1207 unix_to_nt_time(data, t);
1209 return WERR_OK;
1212 static WERROR winreg_printer_write_ver(TALLOC_CTX *mem_ctx,
1213 struct rpc_pipe_client *pipe_handle,
1214 struct policy_handle *key_handle,
1215 const char *value,
1216 uint64_t data)
1218 struct winreg_String wvalue;
1219 DATA_BLOB blob;
1220 WERROR result = WERR_OK;
1221 NTSTATUS status;
1222 char *str;
1224 /* FIXME: check format is right,
1225 * this needs to be something like: 6.1.7600.16385 */
1226 str = talloc_asprintf(mem_ctx, "%u.%u.%u.%u",
1227 (unsigned)((data >> 48) & 0xFFFF),
1228 (unsigned)((data >> 32) & 0xFFFF),
1229 (unsigned)((data >> 16) & 0xFFFF),
1230 (unsigned)(data & 0xFFFF));
1231 if (!str) {
1232 return WERR_NOMEM;
1235 wvalue.name = value;
1236 if (!push_reg_sz(mem_ctx, &blob, str)) {
1237 return WERR_NOMEM;
1239 status = rpccli_winreg_SetValue(pipe_handle,
1240 mem_ctx,
1241 key_handle,
1242 wvalue,
1243 REG_SZ,
1244 blob.data,
1245 blob.length,
1246 &result);
1247 if (!NT_STATUS_IS_OK(status)) {
1248 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
1249 wvalue.name, win_errstr(result)));
1250 if (!W_ERROR_IS_OK(result)) {
1251 result = ntstatus_to_werror(status);
1255 return result;
1258 static WERROR winreg_printer_ver_to_dword(const char *str, uint64_t *data)
1260 unsigned int v1, v2, v3, v4;
1262 if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
1263 return WERR_INVALID_PARAMETER;
1266 *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
1267 ((uint64_t)(v2 & 0xFFFF) << 32) +
1268 ((uint64_t)(v3 & 0xFFFF) << 16) +
1269 (uint64_t)(v2 & 0xFFFF);
1271 return WERR_OK;
1274 /********************************************************************
1275 Public winreg function for spoolss
1276 ********************************************************************/
1278 WERROR winreg_create_printer(TALLOC_CTX *mem_ctx,
1279 struct auth_serversupplied_info *server_info,
1280 const char *servername,
1281 const char *sharename)
1283 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1284 struct rpc_pipe_client *winreg_pipe = NULL;
1285 struct policy_handle hive_hnd, key_hnd;
1286 struct spoolss_SetPrinterInfo2 *info2;
1287 struct security_descriptor *secdesc;
1288 struct winreg_String wkey, wkeyclass;
1289 const char *path;
1290 const char *subkeys[] = { SPOOL_DSDRIVER_KEY, SPOOL_DSSPOOLER_KEY, SPOOL_PRINTERDATA_KEY };
1291 uint32_t i, count = ARRAY_SIZE(subkeys);
1292 uint32_t info2_mask = 0;
1293 WERROR result = WERR_OK;
1294 TALLOC_CTX *tmp_ctx;
1296 tmp_ctx = talloc_new(mem_ctx);
1297 if (tmp_ctx == NULL) {
1298 return WERR_NOMEM;
1301 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1302 if (path == NULL) {
1303 TALLOC_FREE(tmp_ctx);
1304 return WERR_NOMEM;
1307 ZERO_STRUCT(hive_hnd);
1308 ZERO_STRUCT(key_hnd);
1310 result = winreg_printer_openkey(tmp_ctx,
1311 server_info,
1312 &winreg_pipe,
1313 path,
1315 false,
1316 access_mask,
1317 &hive_hnd,
1318 &key_hnd);
1319 if (W_ERROR_IS_OK(result)) {
1320 DEBUG(2, ("winreg_create_printer: Skipping, %s already exists\n", path));
1321 goto done;
1322 } else if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1323 DEBUG(2, ("winreg_create_printer: Creating default values in %s\n", path));
1324 } else if (!W_ERROR_IS_OK(result)) {
1325 DEBUG(0, ("winreg_create_printer: Could not open key %s: %s\n",
1326 path, win_errstr(result)));
1327 goto done;
1330 /* Create the main key */
1331 result = winreg_printer_openkey(tmp_ctx,
1332 server_info,
1333 &winreg_pipe,
1334 path,
1336 true,
1337 access_mask,
1338 &hive_hnd,
1339 &key_hnd);
1340 if (!W_ERROR_IS_OK(result)) {
1341 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
1342 path, win_errstr(result)));
1343 goto done;
1346 if (is_valid_policy_hnd(&key_hnd)) {
1347 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1350 /* Create subkeys */
1351 for (i = 0; i < count; i++) {
1352 NTSTATUS status;
1353 enum winreg_CreateAction action = REG_ACTION_NONE;
1355 ZERO_STRUCT(key_hnd);
1356 ZERO_STRUCT(wkey);
1358 wkey.name = talloc_asprintf(tmp_ctx, "%s\\%s", path, subkeys[i]);
1359 if (wkey.name == NULL) {
1360 result = WERR_NOMEM;
1361 goto done;
1364 ZERO_STRUCT(wkeyclass);
1365 wkeyclass.name = "";
1367 status = rpccli_winreg_CreateKey(winreg_pipe,
1368 tmp_ctx,
1369 &hive_hnd,
1370 wkey,
1371 wkeyclass,
1373 access_mask,
1374 NULL,
1375 &key_hnd,
1376 &action,
1377 &result);
1378 if (!NT_STATUS_IS_OK(status)) {
1379 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
1380 wkey.name, win_errstr(result)));
1381 if (!W_ERROR_IS_OK(result)) {
1382 result = ntstatus_to_werror(status);
1384 goto done;
1387 switch (i) {
1388 case 1: {
1389 const char *dnssuffix;
1390 const char *longname;
1391 const char *uncname;
1393 result = winreg_printer_write_sz(tmp_ctx,
1394 winreg_pipe,
1395 &key_hnd,
1396 SPOOL_REG_PRINTERNAME,
1397 sharename);
1398 if (!W_ERROR_IS_OK(result)) {
1399 goto done;
1402 result = winreg_printer_write_sz(tmp_ctx,
1403 winreg_pipe,
1404 &key_hnd,
1405 SPOOL_REG_SHORTSERVERNAME,
1406 global_myname());
1407 if (!W_ERROR_IS_OK(result)) {
1408 goto done;
1411 /* We make the assumption that the netbios name
1412 * is the same as the DNS name since the former
1413 * will be what we used to join the domain
1415 dnssuffix = get_mydnsdomname(tmp_ctx);
1416 if (dnssuffix != NULL && dnssuffix[0] != '\0') {
1417 longname = talloc_asprintf(tmp_ctx, "%s.%s", global_myname(), dnssuffix);
1418 } else {
1419 longname = talloc_strdup(tmp_ctx, global_myname());
1421 if (longname == NULL) {
1422 result = WERR_NOMEM;
1423 goto done;
1426 result = winreg_printer_write_sz(tmp_ctx,
1427 winreg_pipe,
1428 &key_hnd,
1429 SPOOL_REG_SERVERNAME,
1430 longname);
1431 if (!W_ERROR_IS_OK(result)) {
1432 goto done;
1435 uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
1436 longname, sharename);
1437 if (uncname == NULL) {
1438 result = WERR_NOMEM;
1439 goto done;
1442 result = winreg_printer_write_sz(tmp_ctx,
1443 winreg_pipe,
1444 &key_hnd,
1445 SPOOL_REG_UNCNAME,
1446 uncname);
1447 if (!W_ERROR_IS_OK(result)) {
1448 goto done;
1451 result = winreg_printer_write_dword(tmp_ctx,
1452 winreg_pipe,
1453 &key_hnd,
1454 SPOOL_REG_VERSIONNUMBER,
1456 if (!W_ERROR_IS_OK(result)) {
1457 goto done;
1460 result = winreg_printer_write_dword(tmp_ctx,
1461 winreg_pipe,
1462 &key_hnd,
1463 SPOOL_REG_PRINTSTARTTIME,
1465 if (!W_ERROR_IS_OK(result)) {
1466 goto done;
1469 result = winreg_printer_write_dword(tmp_ctx,
1470 winreg_pipe,
1471 &key_hnd,
1472 SPOOL_REG_PRINTENDTIME,
1474 if (!W_ERROR_IS_OK(result)) {
1475 goto done;
1478 result = winreg_printer_write_dword(tmp_ctx,
1479 winreg_pipe,
1480 &key_hnd,
1481 SPOOL_REG_PRIORITY,
1483 if (!W_ERROR_IS_OK(result)) {
1484 goto done;
1487 result = winreg_printer_write_dword(tmp_ctx,
1488 winreg_pipe,
1489 &key_hnd,
1490 SPOOL_REG_PRINTKEEPPRINTEDJOBS,
1492 if (!W_ERROR_IS_OK(result)) {
1493 goto done;
1495 } /* case 1 */
1496 default:
1497 break;
1500 if (is_valid_policy_hnd(&key_hnd)) {
1501 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1504 info2 = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfo2);
1505 if (info2 == NULL) {
1506 result = WERR_NOMEM;
1507 goto done;
1510 if (servername != NULL) {
1511 info2->printername = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
1512 servername, sharename);
1513 } else {
1514 info2->printername = sharename;
1516 if (info2->printername == NULL) {
1517 result = WERR_NOMEM;
1518 goto done;
1520 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTERNAME;
1522 info2->sharename = sharename;
1523 info2_mask |= SPOOLSS_PRINTER_INFO_SHARENAME;
1525 info2->portname = SAMBA_PRINTER_PORT_NAME;
1526 info2_mask |= SPOOLSS_PRINTER_INFO_PORTNAME;
1528 info2->printprocessor = "winprint";
1529 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTPROCESSOR;
1531 info2->datatype = "RAW";
1532 info2_mask |= SPOOLSS_PRINTER_INFO_DATATYPE;
1534 info2->comment = "";
1535 info2_mask |= SPOOLSS_PRINTER_INFO_COMMENT;
1537 info2->attributes = PRINTER_ATTRIBUTE_SAMBA;
1538 info2_mask |= SPOOLSS_PRINTER_INFO_ATTRIBUTES;
1540 info2->starttime = 0; /* Minutes since 12:00am GMT */
1541 info2_mask |= SPOOLSS_PRINTER_INFO_STARTTIME;
1543 info2->untiltime = 0; /* Minutes since 12:00am GMT */
1544 info2_mask |= SPOOLSS_PRINTER_INFO_UNTILTIME;
1546 info2->priority = 1;
1547 info2_mask |= SPOOLSS_PRINTER_INFO_PRIORITY;
1549 info2->defaultpriority = 1;
1550 info2_mask |= SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY;
1552 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1553 if (!W_ERROR_IS_OK(result)) {
1554 goto done;
1556 info2_mask |= SPOOLSS_PRINTER_INFO_SECDESC;
1559 * Don't write a default Device Mode to the registry! The Device Mode is
1560 * only written to disk with a SetPrinter level 2 or 8.
1563 result = winreg_update_printer(tmp_ctx,
1564 server_info,
1565 sharename,
1566 info2_mask,
1567 info2,
1568 NULL,
1569 secdesc);
1571 done:
1572 if (winreg_pipe != NULL) {
1573 if (is_valid_policy_hnd(&key_hnd)) {
1574 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1576 if (is_valid_policy_hnd(&hive_hnd)) {
1577 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1581 talloc_free(tmp_ctx);
1582 return result;
1585 WERROR winreg_update_printer(TALLOC_CTX *mem_ctx,
1586 struct auth_serversupplied_info *server_info,
1587 const char *sharename,
1588 uint32_t info2_mask,
1589 struct spoolss_SetPrinterInfo2 *info2,
1590 struct spoolss_DeviceMode *devmode,
1591 struct security_descriptor *secdesc)
1593 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1594 struct rpc_pipe_client *winreg_pipe = NULL;
1595 struct policy_handle hive_hnd, key_hnd;
1596 int snum = lp_servicenumber(sharename);
1597 enum ndr_err_code ndr_err;
1598 DATA_BLOB blob;
1599 char *path;
1600 WERROR result = WERR_OK;
1601 TALLOC_CTX *tmp_ctx;
1603 tmp_ctx = talloc_new(mem_ctx);
1604 if (tmp_ctx == NULL) {
1605 return WERR_NOMEM;
1608 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1609 if (path == NULL) {
1610 TALLOC_FREE(tmp_ctx);
1611 return WERR_NOMEM;
1614 ZERO_STRUCT(hive_hnd);
1615 ZERO_STRUCT(key_hnd);
1617 result = winreg_printer_openkey(tmp_ctx,
1618 server_info,
1619 &winreg_pipe,
1620 path,
1622 true,
1623 access_mask,
1624 &hive_hnd,
1625 &key_hnd);
1626 if (!W_ERROR_IS_OK(result)) {
1627 DEBUG(0, ("winreg_update_printer: Could not open key %s: %s\n",
1628 path, win_errstr(result)));
1629 goto done;
1632 if (info2_mask & SPOOLSS_PRINTER_INFO_ATTRIBUTES) {
1633 result = winreg_printer_write_dword(tmp_ctx,
1634 winreg_pipe,
1635 &key_hnd,
1636 "Attributes",
1637 info2->attributes);
1638 if (!W_ERROR_IS_OK(result)) {
1639 goto done;
1643 #if 0
1644 if (info2_mask & SPOOLSS_PRINTER_INFO_AVERAGEPPM) {
1645 result = winreg_printer_write_dword(tmp_ctx,
1646 winreg_pipe,
1647 &key_hnd,
1648 "AveragePpm",
1649 info2->attributes);
1650 if (!W_ERROR_IS_OK(result)) {
1651 goto done;
1654 #endif
1656 if (info2_mask & SPOOLSS_PRINTER_INFO_COMMENT) {
1657 result = winreg_printer_write_sz(tmp_ctx,
1658 winreg_pipe,
1659 &key_hnd,
1660 "Description",
1661 info2->comment);
1662 if (!W_ERROR_IS_OK(result)) {
1663 goto done;
1667 if (info2_mask & SPOOLSS_PRINTER_INFO_DATATYPE) {
1668 result = winreg_printer_write_sz(tmp_ctx,
1669 winreg_pipe,
1670 &key_hnd,
1671 "Datatype",
1672 info2->datatype);
1673 if (!W_ERROR_IS_OK(result)) {
1674 goto done;
1678 if (info2_mask & SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY) {
1679 result = winreg_printer_write_dword(tmp_ctx,
1680 winreg_pipe,
1681 &key_hnd,
1682 "Default Priority",
1683 info2->defaultpriority);
1684 if (!W_ERROR_IS_OK(result)) {
1685 goto done;
1689 if (info2_mask & SPOOLSS_PRINTER_INFO_DEVMODE) {
1691 * Some client drivers freak out if there is a NULL devmode
1692 * (probably the driver is not checking before accessing
1693 * the devmode pointer) --jerry
1695 if (devmode == NULL && lp_default_devmode(snum) && info2 != NULL) {
1696 result = spoolss_create_default_devmode(tmp_ctx,
1697 info2->printername,
1698 &devmode);
1699 if (!W_ERROR_IS_OK(result)) {
1700 goto done;
1703 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, devmode,
1704 (ndr_push_flags_fn_t) ndr_push_spoolss_DeviceMode);
1705 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1706 DEBUG(0, ("winreg_update_printer: Failed to marshall device mode\n"));
1707 result = WERR_NOMEM;
1708 goto done;
1711 result = winreg_printer_write_binary(tmp_ctx,
1712 winreg_pipe,
1713 &key_hnd,
1714 "Default DevMode",
1715 blob);
1716 if (!W_ERROR_IS_OK(result)) {
1717 goto done;
1721 if (info2_mask & SPOOLSS_PRINTER_INFO_DRIVERNAME) {
1722 result = winreg_printer_write_sz(tmp_ctx,
1723 winreg_pipe,
1724 &key_hnd,
1725 "Printer Driver",
1726 info2->drivername);
1727 if (!W_ERROR_IS_OK(result)) {
1728 goto done;
1732 if (info2_mask & SPOOLSS_PRINTER_INFO_LOCATION) {
1733 result = winreg_printer_write_sz(tmp_ctx,
1734 winreg_pipe,
1735 &key_hnd,
1736 "Location",
1737 info2->location);
1738 if (!W_ERROR_IS_OK(result)) {
1739 goto done;
1743 if (info2_mask & SPOOLSS_PRINTER_INFO_PARAMETERS) {
1744 result = winreg_printer_write_sz(tmp_ctx,
1745 winreg_pipe,
1746 &key_hnd,
1747 "Parameters",
1748 info2->parameters);
1749 if (!W_ERROR_IS_OK(result)) {
1750 goto done;
1754 if (info2_mask & SPOOLSS_PRINTER_INFO_PORTNAME) {
1755 result = winreg_printer_write_sz(tmp_ctx,
1756 winreg_pipe,
1757 &key_hnd,
1758 "Port",
1759 info2->portname);
1760 if (!W_ERROR_IS_OK(result)) {
1761 goto done;
1765 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTERNAME) {
1767 * in addprinter: no servername and the printer is the name
1768 * in setprinter: servername is \\server
1769 * and printer is \\server\\printer
1771 * Samba manages only local printers.
1772 * we currently don't support things like i
1773 * path=\\other_server\printer
1775 * We only store the printername, not \\server\printername
1777 const char *p = strrchr(info2->printername, '\\');
1778 if (p == NULL) {
1779 p = info2->printername;
1780 } else {
1781 p++;
1783 result = winreg_printer_write_sz(tmp_ctx,
1784 winreg_pipe,
1785 &key_hnd,
1786 "Name",
1788 if (!W_ERROR_IS_OK(result)) {
1789 goto done;
1793 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTPROCESSOR) {
1794 result = winreg_printer_write_sz(tmp_ctx,
1795 winreg_pipe,
1796 &key_hnd,
1797 "Print Processor",
1798 info2->printprocessor);
1799 if (!W_ERROR_IS_OK(result)) {
1800 goto done;
1804 if (info2_mask & SPOOLSS_PRINTER_INFO_PRIORITY) {
1805 result = winreg_printer_write_dword(tmp_ctx,
1806 winreg_pipe,
1807 &key_hnd,
1808 "Priority",
1809 info2->priority);
1810 if (!W_ERROR_IS_OK(result)) {
1811 goto done;
1815 if (info2_mask & SPOOLSS_PRINTER_INFO_SECDESC) {
1817 * We need a security descriptor, if it isn't specified by
1818 * AddPrinter{Ex} then create a default descriptor.
1820 if (secdesc == NULL) {
1821 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1822 if (!W_ERROR_IS_OK(result)) {
1823 goto done;
1826 result = winreg_set_printer_secdesc(tmp_ctx,
1827 server_info,
1828 sharename,
1829 secdesc);
1830 if (!W_ERROR_IS_OK(result)) {
1831 goto done;
1835 if (info2_mask & SPOOLSS_PRINTER_INFO_SEPFILE) {
1836 result = winreg_printer_write_sz(tmp_ctx,
1837 winreg_pipe,
1838 &key_hnd,
1839 "Separator File",
1840 info2->sepfile);
1841 if (!W_ERROR_IS_OK(result)) {
1842 goto done;
1846 if (info2_mask & SPOOLSS_PRINTER_INFO_SHARENAME) {
1847 result = winreg_printer_write_sz(tmp_ctx,
1848 winreg_pipe,
1849 &key_hnd,
1850 "Share Name",
1851 info2->sharename);
1852 if (!W_ERROR_IS_OK(result)) {
1853 goto done;
1857 if (info2_mask & SPOOLSS_PRINTER_INFO_STARTTIME) {
1858 result = winreg_printer_write_dword(tmp_ctx,
1859 winreg_pipe,
1860 &key_hnd,
1861 "StartTime",
1862 info2->starttime);
1863 if (!W_ERROR_IS_OK(result)) {
1864 goto done;
1868 if (info2_mask & SPOOLSS_PRINTER_INFO_STATUS) {
1869 result = winreg_printer_write_dword(tmp_ctx,
1870 winreg_pipe,
1871 &key_hnd,
1872 "Status",
1873 info2->status);
1874 if (!W_ERROR_IS_OK(result)) {
1875 goto done;
1879 if (info2_mask & SPOOLSS_PRINTER_INFO_UNTILTIME) {
1880 result = winreg_printer_write_dword(tmp_ctx,
1881 winreg_pipe,
1882 &key_hnd,
1883 "UntilTime",
1884 info2->untiltime);
1885 if (!W_ERROR_IS_OK(result)) {
1886 goto done;
1890 result = winreg_printer_write_dword(tmp_ctx,
1891 winreg_pipe,
1892 &key_hnd,
1893 "ChangeID",
1894 winreg_printer_rev_changeid());
1895 if (!W_ERROR_IS_OK(result)) {
1896 goto done;
1899 result = WERR_OK;
1900 done:
1901 if (winreg_pipe != NULL) {
1902 if (is_valid_policy_hnd(&key_hnd)) {
1903 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
1905 if (is_valid_policy_hnd(&hive_hnd)) {
1906 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
1910 TALLOC_FREE(tmp_ctx);
1911 return result;
1914 WERROR winreg_get_printer(TALLOC_CTX *mem_ctx,
1915 struct auth_serversupplied_info *server_info,
1916 const char *servername,
1917 const char *printer,
1918 struct spoolss_PrinterInfo2 **pinfo2)
1920 struct spoolss_PrinterInfo2 *info2;
1921 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1922 struct rpc_pipe_client *winreg_pipe = NULL;
1923 struct policy_handle hive_hnd, key_hnd;
1924 struct spoolss_PrinterEnumValues *enum_values = NULL;
1925 struct spoolss_PrinterEnumValues *v;
1926 enum ndr_err_code ndr_err;
1927 DATA_BLOB blob;
1928 int snum = lp_servicenumber(printer);
1929 uint32_t num_values = 0;
1930 uint32_t i;
1931 char *path;
1932 WERROR result = WERR_OK;
1933 TALLOC_CTX *tmp_ctx;
1935 tmp_ctx = talloc_new(mem_ctx);
1936 if (tmp_ctx == NULL) {
1937 return WERR_NOMEM;
1940 path = winreg_printer_data_keyname(tmp_ctx, printer);
1941 if (path == NULL) {
1942 TALLOC_FREE(tmp_ctx);
1943 return WERR_NOMEM;
1946 result = winreg_printer_openkey(tmp_ctx,
1947 server_info,
1948 &winreg_pipe,
1949 path,
1951 false,
1952 access_mask,
1953 &hive_hnd,
1954 &key_hnd);
1955 if (!W_ERROR_IS_OK(result)) {
1956 DEBUG(0, ("winreg_get_printer: Could not open key %s: %s\n",
1957 path, win_errstr(result)));
1958 goto done;
1961 result = winreg_printer_enumvalues(tmp_ctx,
1962 winreg_pipe,
1963 &key_hnd,
1964 &num_values,
1965 &enum_values);
1966 if (!W_ERROR_IS_OK(result)) {
1967 DEBUG(0, ("winreg_get_printer: Could not enumerate values in %s: %s\n",
1968 path, win_errstr(result)));
1969 goto done;
1972 info2 = talloc_zero(tmp_ctx, struct spoolss_PrinterInfo2);
1973 if (info2 == NULL) {
1974 result = WERR_NOMEM;
1975 goto done;
1978 info2->servername = EMPTY_STRING;
1979 info2->printername = EMPTY_STRING;
1980 info2->sharename = EMPTY_STRING;
1981 info2->portname = EMPTY_STRING;
1982 info2->drivername = EMPTY_STRING;
1983 info2->comment = EMPTY_STRING;
1984 info2->location = EMPTY_STRING;
1985 info2->sepfile = EMPTY_STRING;
1986 info2->printprocessor = EMPTY_STRING;
1987 info2->datatype = EMPTY_STRING;
1988 info2->parameters = EMPTY_STRING;
1990 if (servername != NULL && servername[0] != '\0') {
1991 info2->servername = talloc_asprintf(info2, "\\\\%s", servername);
1992 if (info2->servername == NULL) {
1993 result = WERR_NOMEM;
1994 goto done;
1998 for (i = 0; i < num_values; i++) {
1999 v = &enum_values[i];
2001 result = winreg_enumval_to_sz(info2,
2003 "Name",
2004 &info2->printername);
2005 CHECK_ERROR(result);
2007 result = winreg_enumval_to_sz(info2,
2009 "Share Name",
2010 &info2->sharename);
2011 CHECK_ERROR(result);
2013 result = winreg_enumval_to_sz(info2,
2015 "Port",
2016 &info2->portname);
2017 CHECK_ERROR(result);
2019 result = winreg_enumval_to_sz(info2,
2021 "Description",
2022 &info2->comment);
2023 CHECK_ERROR(result);
2025 result = winreg_enumval_to_sz(info2,
2027 "Location",
2028 &info2->location);
2029 CHECK_ERROR(result);
2031 result = winreg_enumval_to_sz(info2,
2033 "Separator File",
2034 &info2->sepfile);
2035 CHECK_ERROR(result);
2037 result = winreg_enumval_to_sz(info2,
2039 "Print Processor",
2040 &info2->printprocessor);
2041 CHECK_ERROR(result);
2043 result = winreg_enumval_to_sz(info2,
2045 "Datatype",
2046 &info2->datatype);
2047 CHECK_ERROR(result);
2049 result = winreg_enumval_to_sz(info2,
2051 "Parameters",
2052 &info2->parameters);
2053 CHECK_ERROR(result);
2055 result = winreg_enumval_to_sz(info2,
2057 "Printer Driver",
2058 &info2->drivername);
2059 CHECK_ERROR(result);
2061 result = winreg_enumval_to_dword(info2,
2063 "Attributes",
2064 &info2->attributes);
2065 CHECK_ERROR(result);
2067 result = winreg_enumval_to_dword(info2,
2069 "Priority",
2070 &info2->priority);
2071 CHECK_ERROR(result);
2073 result = winreg_enumval_to_dword(info2,
2075 "Default Priority",
2076 &info2->defaultpriority);
2077 CHECK_ERROR(result);
2079 result = winreg_enumval_to_dword(info2,
2081 "StartTime",
2082 &info2->starttime);
2083 CHECK_ERROR(result);
2085 result = winreg_enumval_to_dword(info2,
2087 "UntilTime",
2088 &info2->untiltime);
2089 CHECK_ERROR(result);
2091 result = winreg_enumval_to_dword(info2,
2093 "Status",
2094 &info2->status);
2095 CHECK_ERROR(result);
2097 result = winreg_enumval_to_dword(info2,
2099 "StartTime",
2100 &info2->starttime);
2101 CHECK_ERROR(result);
2104 if (!W_ERROR_IS_OK(result)) {
2105 DEBUG(0, ("winreg_get_printer: winreg_enumval_to_TYPE() failed "
2106 "for %s: %s\n",
2107 v->value_name,
2108 win_errstr(result)));
2109 goto done;
2112 /* Create the printername */
2113 if (info2->servername[0] != '\0') {
2114 if (lp_force_printername(snum)) {
2115 const char *p = talloc_asprintf(info2, "%s\\%s",
2116 info2->servername,
2117 info2->sharename);
2118 if (p == NULL) {
2119 result = WERR_NOMEM;
2120 goto done;
2122 info2->printername = p;
2123 } else {
2124 char *p = talloc_asprintf(info2, "%s\\%s",
2125 info2->servername,
2126 info2->printername);
2127 if (p == NULL) {
2128 result = WERR_NOMEM;
2129 goto done;
2131 info2->printername = p;
2135 /* Construct the Device Mode */
2136 result = winreg_printer_query_binary(tmp_ctx,
2137 winreg_pipe,
2138 &key_hnd,
2139 "Default DevMode",
2140 &blob);
2141 if (W_ERROR_IS_OK(result)) {
2142 info2->devmode = talloc_zero(info2, struct spoolss_DeviceMode);
2143 if (info2->devmode == NULL) {
2144 result = WERR_NOMEM;
2145 goto done;
2147 ndr_err = ndr_pull_struct_blob(&blob,
2148 info2->devmode,
2149 info2->devmode,
2150 (ndr_pull_flags_fn_t) ndr_pull_spoolss_DeviceMode);
2151 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2152 DEBUG(0, ("winreg_get_printer: Failed to unmarshall device mode\n"));
2153 result = WERR_NOMEM;
2154 goto done;
2158 if (info2->devmode == NULL && lp_default_devmode(snum)) {
2159 result = spoolss_create_default_devmode(info2,
2160 info2->printername,
2161 &info2->devmode);
2162 if (!W_ERROR_IS_OK(result)) {
2163 goto done;
2167 if (info2->devmode != NULL) {
2168 info2->devmode->devicename = talloc_strdup(info2->devmode,
2169 info2->printername);
2170 if (info2->devmode->devicename == NULL) {
2171 DEBUG(0, ("winreg_get_printer: Failed to set devicename\n"));
2172 result = WERR_NOMEM;
2173 goto done;
2177 result = winreg_get_printer_secdesc(info2,
2178 server_info,
2179 printer,
2180 &info2->secdesc);
2181 if (!W_ERROR_IS_OK(result)) {
2182 goto done;
2185 /* Fix for OS/2 drivers. */
2186 if (get_remote_arch() == RA_OS2) {
2187 spoolss_map_to_os2_driver(info2, &info2->drivername);
2190 if (pinfo2) {
2191 *pinfo2 = talloc_move(mem_ctx, &info2);
2194 result = WERR_OK;
2195 done:
2196 if (winreg_pipe != NULL) {
2197 if (is_valid_policy_hnd(&key_hnd)) {
2198 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2200 if (is_valid_policy_hnd(&hive_hnd)) {
2201 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2205 TALLOC_FREE(tmp_ctx);
2206 return result;
2209 WERROR winreg_get_printer_secdesc(TALLOC_CTX *mem_ctx,
2210 struct auth_serversupplied_info *server_info,
2211 const char *sharename,
2212 struct spoolss_security_descriptor **psecdesc)
2214 struct spoolss_security_descriptor *secdesc;
2215 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2216 struct rpc_pipe_client *winreg_pipe = NULL;
2217 struct policy_handle hive_hnd, key_hnd;
2218 enum ndr_err_code ndr_err;
2219 const char *path;
2220 DATA_BLOB blob;
2221 TALLOC_CTX *tmp_ctx;
2222 WERROR result;
2224 tmp_ctx = talloc_new(mem_ctx);
2225 if (tmp_ctx == NULL) {
2226 return WERR_NOMEM;
2229 path = winreg_printer_data_keyname(tmp_ctx, sharename);
2230 if (path == NULL) {
2231 talloc_free(tmp_ctx);
2232 return WERR_NOMEM;
2235 ZERO_STRUCT(hive_hnd);
2236 ZERO_STRUCT(key_hnd);
2238 result = winreg_printer_openkey(tmp_ctx,
2239 server_info,
2240 &winreg_pipe,
2241 path,
2243 false,
2244 access_mask,
2245 &hive_hnd,
2246 &key_hnd);
2247 if (!W_ERROR_IS_OK(result)) {
2248 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2249 goto create_default;
2251 goto done;
2254 result = winreg_printer_query_binary(tmp_ctx,
2255 winreg_pipe,
2256 &key_hnd,
2257 "Security",
2258 &blob);
2259 if (!W_ERROR_IS_OK(result)) {
2260 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2261 goto create_default;
2263 goto done;
2266 secdesc = talloc_zero(tmp_ctx, struct spoolss_security_descriptor);
2267 if (secdesc == NULL) {
2268 result = WERR_NOMEM;
2269 goto done;
2271 ndr_err = ndr_pull_struct_blob(&blob,
2272 secdesc,
2273 secdesc,
2274 (ndr_pull_flags_fn_t) ndr_pull_security_descriptor);
2275 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2276 DEBUG(0, ("winreg_get_secdesc: Failed to unmarshall security descriptor\n"));
2277 result = WERR_NOMEM;
2278 goto done;
2281 if (psecdesc) {
2282 *psecdesc = talloc_move(mem_ctx, &secdesc);
2285 result = WERR_OK;
2286 goto done;
2288 create_default:
2289 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
2290 if (!W_ERROR_IS_OK(result)) {
2291 return result;
2294 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
2295 this security descriptor has been created when winbindd was
2296 down. Take ownership of security descriptor. */
2297 if (sid_equal(secdesc->owner_sid, &global_sid_World)) {
2298 struct dom_sid owner_sid;
2300 /* Change sd owner to workgroup administrator */
2302 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
2303 struct spoolss_security_descriptor *new_secdesc;
2304 size_t size;
2306 /* Create new sd */
2307 sid_append_rid(&owner_sid, DOMAIN_RID_ADMINISTRATOR);
2309 new_secdesc = make_sec_desc(tmp_ctx,
2310 secdesc->revision,
2311 secdesc->type,
2312 &owner_sid,
2313 secdesc->group_sid,
2314 secdesc->sacl,
2315 secdesc->dacl,
2316 &size);
2318 if (new_secdesc == NULL) {
2319 result = WERR_NOMEM;
2320 goto done;
2323 /* Swap with other one */
2324 secdesc = new_secdesc;
2328 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, secdesc,
2329 (ndr_push_flags_fn_t) ndr_push_security_descriptor);
2330 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2331 DEBUG(0, ("winreg_set_secdesc: Failed to marshall security descriptor\n"));
2332 result = WERR_NOMEM;
2333 goto done;
2336 result = winreg_printer_write_binary(tmp_ctx,
2337 winreg_pipe,
2338 &key_hnd,
2339 "Security",
2340 blob);
2341 if (!W_ERROR_IS_OK(result)) {
2342 return result;
2345 if (psecdesc) {
2346 *psecdesc = talloc_move(mem_ctx, &secdesc);
2349 result = WERR_OK;
2350 done:
2351 if (winreg_pipe != NULL) {
2352 if (is_valid_policy_hnd(&key_hnd)) {
2353 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2355 if (is_valid_policy_hnd(&hive_hnd)) {
2356 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2360 talloc_free(tmp_ctx);
2361 return result;
2364 WERROR winreg_set_printer_secdesc(TALLOC_CTX *mem_ctx,
2365 struct auth_serversupplied_info *server_info,
2366 const char *sharename,
2367 const struct spoolss_security_descriptor *secdesc)
2369 const struct spoolss_security_descriptor *new_secdesc = secdesc;
2370 struct spoolss_security_descriptor *old_secdesc;
2371 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2372 struct rpc_pipe_client *winreg_pipe = NULL;
2373 struct policy_handle hive_hnd, key_hnd;
2374 enum ndr_err_code ndr_err;
2375 const char *path;
2376 DATA_BLOB blob;
2377 TALLOC_CTX *tmp_ctx;
2378 WERROR result;
2380 tmp_ctx = talloc_new(mem_ctx);
2381 if (tmp_ctx == NULL) {
2382 return WERR_NOMEM;
2385 path = winreg_printer_data_keyname(tmp_ctx, sharename);
2386 if (path == NULL) {
2387 talloc_free(tmp_ctx);
2388 return WERR_NOMEM;
2392 * The old owner and group sids of the security descriptor are not
2393 * present when new ACEs are added or removed by changing printer
2394 * permissions through NT. If they are NULL in the new security
2395 * descriptor then copy them over from the old one.
2397 if (!secdesc->owner_sid || !secdesc->group_sid) {
2398 struct dom_sid *owner_sid, *group_sid;
2399 struct security_acl *dacl, *sacl;
2400 size_t size;
2402 result = winreg_get_printer_secdesc(tmp_ctx,
2403 server_info,
2404 sharename,
2405 &old_secdesc);
2406 if (!W_ERROR_IS_OK(result)) {
2407 talloc_free(tmp_ctx);
2408 return result;
2411 /* Pick out correct owner and group sids */
2412 owner_sid = secdesc->owner_sid ?
2413 secdesc->owner_sid :
2414 old_secdesc->owner_sid;
2416 group_sid = secdesc->group_sid ?
2417 secdesc->group_sid :
2418 old_secdesc->group_sid;
2420 dacl = secdesc->dacl ?
2421 secdesc->dacl :
2422 old_secdesc->dacl;
2424 sacl = secdesc->sacl ?
2425 secdesc->sacl :
2426 old_secdesc->sacl;
2428 /* Make a deep copy of the security descriptor */
2429 new_secdesc = make_sec_desc(tmp_ctx,
2430 secdesc->revision,
2431 secdesc->type,
2432 owner_sid,
2433 group_sid,
2434 sacl,
2435 dacl,
2436 &size);
2437 if (new_secdesc == NULL) {
2438 talloc_free(tmp_ctx);
2439 return WERR_NOMEM;
2443 ZERO_STRUCT(hive_hnd);
2444 ZERO_STRUCT(key_hnd);
2446 result = winreg_printer_openkey(tmp_ctx,
2447 server_info,
2448 &winreg_pipe,
2449 path,
2451 false,
2452 access_mask,
2453 &hive_hnd,
2454 &key_hnd);
2455 if (!W_ERROR_IS_OK(result)) {
2456 goto done;
2459 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, new_secdesc,
2460 (ndr_push_flags_fn_t) ndr_push_security_descriptor);
2461 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2462 DEBUG(0, ("winreg_set_secdesc: Failed to marshall security descriptor\n"));
2463 result = WERR_NOMEM;
2464 goto done;
2467 result = winreg_printer_write_binary(tmp_ctx,
2468 winreg_pipe,
2469 &key_hnd,
2470 "Security",
2471 blob);
2473 done:
2474 if (winreg_pipe != NULL) {
2475 if (is_valid_policy_hnd(&key_hnd)) {
2476 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2478 if (is_valid_policy_hnd(&hive_hnd)) {
2479 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2483 talloc_free(tmp_ctx);
2484 return result;
2487 /* Set printer data over the winreg pipe. */
2488 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
2489 struct auth_serversupplied_info *server_info,
2490 const char *printer,
2491 const char *key,
2492 const char *value,
2493 enum winreg_Type type,
2494 uint8_t *data,
2495 uint32_t data_size)
2497 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2498 struct rpc_pipe_client *winreg_pipe = NULL;
2499 struct policy_handle hive_hnd, key_hnd;
2500 struct winreg_String wvalue;
2501 char *path;
2502 WERROR result = WERR_OK;
2503 NTSTATUS status;
2504 TALLOC_CTX *tmp_ctx;
2506 tmp_ctx = talloc_new(mem_ctx);
2507 if (tmp_ctx == NULL) {
2508 return WERR_NOMEM;
2511 path = winreg_printer_data_keyname(tmp_ctx, printer);
2512 if (path == NULL) {
2513 TALLOC_FREE(tmp_ctx);
2514 return WERR_NOMEM;
2517 ZERO_STRUCT(hive_hnd);
2518 ZERO_STRUCT(key_hnd);
2520 DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
2521 key, value, access_mask, printer));
2522 result = winreg_printer_openkey(tmp_ctx,
2523 server_info,
2524 &winreg_pipe,
2525 path,
2526 key,
2527 true,
2528 access_mask,
2529 &hive_hnd,
2530 &key_hnd);
2531 if (!W_ERROR_IS_OK(result)) {
2532 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
2533 key, win_errstr(result)));
2534 goto done;
2537 wvalue.name = value;
2538 status = rpccli_winreg_SetValue(winreg_pipe,
2539 tmp_ctx,
2540 &key_hnd,
2541 wvalue,
2542 type,
2543 data,
2544 data_size,
2545 &result);
2546 if (!NT_STATUS_IS_OK(status)) {
2547 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
2548 value, nt_errstr(status)));
2549 if (!W_ERROR_IS_OK(result)) {
2550 goto done;
2552 result = ntstatus_to_werror(status);
2553 goto done;
2556 result = WERR_OK;
2557 done:
2558 if (winreg_pipe != NULL) {
2559 if (is_valid_policy_hnd(&key_hnd)) {
2560 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2562 if (is_valid_policy_hnd(&hive_hnd)) {
2563 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2567 TALLOC_FREE(tmp_ctx);
2568 return result;
2571 /* Get printer data over a winreg pipe. */
2572 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
2573 struct auth_serversupplied_info *server_info,
2574 const char *printer,
2575 const char *key,
2576 const char *value,
2577 enum winreg_Type *type,
2578 uint8_t **data,
2579 uint32_t *data_size)
2581 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2582 struct rpc_pipe_client *winreg_pipe = NULL;
2583 struct policy_handle hive_hnd, key_hnd;
2584 struct winreg_String wvalue;
2585 enum winreg_Type type_in;
2586 char *path;
2587 uint8_t *data_in;
2588 uint32_t data_in_size = 0;
2589 uint32_t value_len = 0;
2590 WERROR result = WERR_OK;
2591 NTSTATUS status;
2592 TALLOC_CTX *tmp_ctx;
2594 tmp_ctx = talloc_new(mem_ctx);
2595 if (tmp_ctx == NULL) {
2596 return WERR_NOMEM;
2599 path = winreg_printer_data_keyname(tmp_ctx, printer);
2600 if (path == NULL) {
2601 TALLOC_FREE(tmp_ctx);
2602 return WERR_NOMEM;
2605 ZERO_STRUCT(hive_hnd);
2606 ZERO_STRUCT(key_hnd);
2608 result = winreg_printer_openkey(tmp_ctx,
2609 server_info,
2610 &winreg_pipe,
2611 path,
2612 key,
2613 false,
2614 access_mask,
2615 &hive_hnd,
2616 &key_hnd);
2617 if (!W_ERROR_IS_OK(result)) {
2618 DEBUG(0, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
2619 key, win_errstr(result)));
2620 goto done;
2623 wvalue.name = value;
2626 * call QueryValue once with data == NULL to get the
2627 * needed memory size to be allocated, then allocate
2628 * data buffer and call again.
2630 status = rpccli_winreg_QueryValue(winreg_pipe,
2631 tmp_ctx,
2632 &key_hnd,
2633 &wvalue,
2634 &type_in,
2635 NULL,
2636 &data_in_size,
2637 &value_len,
2638 &result);
2639 if (!NT_STATUS_IS_OK(status)) {
2640 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2641 value, nt_errstr(status)));
2642 if (!W_ERROR_IS_OK(result)) {
2643 goto done;
2645 result = ntstatus_to_werror(status);
2646 goto done;
2649 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2650 if (data_in == NULL) {
2651 result = WERR_NOMEM;
2652 goto done;
2654 value_len = 0;
2656 status = rpccli_winreg_QueryValue(winreg_pipe,
2657 tmp_ctx,
2658 &key_hnd,
2659 &wvalue,
2660 &type_in,
2661 data_in,
2662 &data_in_size,
2663 &value_len,
2664 &result);
2665 if (!NT_STATUS_IS_OK(status)) {
2666 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2667 value, nt_errstr(status)));
2668 if (!W_ERROR_IS_OK(result)) {
2669 result = ntstatus_to_werror(status);
2671 goto done;
2674 *type = type_in;
2675 *data_size = data_in_size;
2676 if (data_in_size) {
2677 *data = talloc_move(mem_ctx, &data_in);
2680 result = WERR_OK;
2681 done:
2682 if (winreg_pipe != NULL) {
2683 if (is_valid_policy_hnd(&key_hnd)) {
2684 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2686 if (is_valid_policy_hnd(&hive_hnd)) {
2687 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2691 TALLOC_FREE(tmp_ctx);
2692 return result;
2695 /* Enumerate on the values of a given key and provide the data. */
2696 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
2697 struct auth_serversupplied_info *server_info,
2698 const char *printer,
2699 const char *key,
2700 uint32_t *pnum_values,
2701 struct spoolss_PrinterEnumValues **penum_values)
2703 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2704 struct rpc_pipe_client *winreg_pipe = NULL;
2705 struct policy_handle hive_hnd, key_hnd;
2707 struct spoolss_PrinterEnumValues *enum_values = NULL;
2708 uint32_t num_values = 0;
2709 char *path;
2710 WERROR result = WERR_OK;
2712 TALLOC_CTX *tmp_ctx;
2714 tmp_ctx = talloc_new(mem_ctx);
2715 if (tmp_ctx == NULL) {
2716 return WERR_NOMEM;
2719 path = winreg_printer_data_keyname(tmp_ctx, printer);
2720 if (path == NULL) {
2721 TALLOC_FREE(tmp_ctx);
2722 return WERR_NOMEM;
2725 result = winreg_printer_openkey(tmp_ctx,
2726 server_info,
2727 &winreg_pipe,
2728 path,
2729 key,
2730 false,
2731 access_mask,
2732 &hive_hnd,
2733 &key_hnd);
2734 if (!W_ERROR_IS_OK(result)) {
2735 DEBUG(0, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
2736 key, win_errstr(result)));
2737 goto done;
2740 result = winreg_printer_enumvalues(tmp_ctx,
2741 winreg_pipe,
2742 &key_hnd,
2743 &num_values,
2744 &enum_values);
2745 if (!W_ERROR_IS_OK(result)) {
2746 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2747 key, win_errstr(result)));
2748 goto done;
2751 *pnum_values = num_values;
2752 if (penum_values) {
2753 *penum_values = talloc_move(mem_ctx, &enum_values);
2756 result = WERR_OK;
2757 done:
2758 if (winreg_pipe != NULL) {
2759 if (is_valid_policy_hnd(&key_hnd)) {
2760 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2762 if (is_valid_policy_hnd(&hive_hnd)) {
2763 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2767 TALLOC_FREE(tmp_ctx);
2768 return result;
2771 /* Delete printer data over a winreg pipe. */
2772 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
2773 struct auth_serversupplied_info *server_info,
2774 const char *printer,
2775 const char *key,
2776 const char *value)
2778 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2779 struct rpc_pipe_client *winreg_pipe = NULL;
2780 struct policy_handle hive_hnd, key_hnd;
2781 struct winreg_String wvalue;
2782 char *path;
2783 WERROR result = WERR_OK;
2784 NTSTATUS status;
2786 TALLOC_CTX *tmp_ctx;
2788 tmp_ctx = talloc_new(mem_ctx);
2789 if (tmp_ctx == NULL) {
2790 return WERR_NOMEM;
2793 path = winreg_printer_data_keyname(tmp_ctx, printer);
2794 if (path == NULL) {
2795 TALLOC_FREE(tmp_ctx);
2796 return WERR_NOMEM;
2799 ZERO_STRUCT(hive_hnd);
2800 ZERO_STRUCT(key_hnd);
2802 result = winreg_printer_openkey(tmp_ctx,
2803 server_info,
2804 &winreg_pipe,
2805 path,
2806 key,
2807 false,
2808 access_mask,
2809 &hive_hnd,
2810 &key_hnd);
2811 if (!W_ERROR_IS_OK(result)) {
2812 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
2813 key, win_errstr(result)));
2814 goto done;
2817 wvalue.name = value;
2818 status = rpccli_winreg_DeleteValue(winreg_pipe,
2819 tmp_ctx,
2820 &key_hnd,
2821 wvalue,
2822 &result);
2823 if (!NT_STATUS_IS_OK(status)) {
2824 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
2825 value, nt_errstr(status)));
2826 if (!W_ERROR_IS_OK(result)) {
2827 goto done;
2829 result = ntstatus_to_werror(status);
2830 goto done;
2833 result = WERR_OK;
2834 done:
2835 if (winreg_pipe != NULL) {
2836 if (is_valid_policy_hnd(&key_hnd)) {
2837 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2839 if (is_valid_policy_hnd(&hive_hnd)) {
2840 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2844 TALLOC_FREE(tmp_ctx);
2845 return result;
2848 /* Enumerate on the subkeys of a given key and provide the data. */
2849 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
2850 struct auth_serversupplied_info *server_info,
2851 const char *printer,
2852 const char *key,
2853 uint32_t *pnum_subkeys,
2854 const char ***psubkeys)
2856 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2857 struct rpc_pipe_client *winreg_pipe = NULL;
2858 struct policy_handle hive_hnd, key_hnd;
2859 char *path;
2860 const char **subkeys = NULL;
2861 uint32_t num_subkeys = -1;
2863 WERROR result = WERR_OK;
2865 TALLOC_CTX *tmp_ctx;
2867 tmp_ctx = talloc_new(mem_ctx);
2868 if (tmp_ctx == NULL) {
2869 return WERR_NOMEM;
2872 path = winreg_printer_data_keyname(tmp_ctx, printer);
2873 if (path == NULL) {
2874 TALLOC_FREE(tmp_ctx);
2875 return WERR_NOMEM;
2878 ZERO_STRUCT(hive_hnd);
2879 ZERO_STRUCT(key_hnd);
2881 result = winreg_printer_openkey(tmp_ctx,
2882 server_info,
2883 &winreg_pipe,
2884 path,
2885 key,
2886 false,
2887 access_mask,
2888 &hive_hnd,
2889 &key_hnd);
2890 if (!W_ERROR_IS_OK(result)) {
2891 DEBUG(0, ("winreg_enum_printer_key: Could not open key %s: %s\n",
2892 key, win_errstr(result)));
2893 goto done;
2896 result = winreg_printer_enumkeys(tmp_ctx,
2897 winreg_pipe,
2898 &key_hnd,
2899 &num_subkeys,
2900 &subkeys);
2901 if (!W_ERROR_IS_OK(result)) {
2902 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
2903 key, win_errstr(result)));
2904 goto done;
2907 *pnum_subkeys = num_subkeys;
2908 if (psubkeys) {
2909 *psubkeys = talloc_move(mem_ctx, &subkeys);
2912 result = WERR_OK;
2913 done:
2914 if (winreg_pipe != NULL) {
2915 if (is_valid_policy_hnd(&key_hnd)) {
2916 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2918 if (is_valid_policy_hnd(&hive_hnd)) {
2919 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
2923 TALLOC_FREE(tmp_ctx);
2924 return result;
2927 /* Delete a key with subkeys of a given printer. */
2928 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
2929 struct auth_serversupplied_info *server_info,
2930 const char *printer,
2931 const char *key)
2933 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2934 struct rpc_pipe_client *winreg_pipe = NULL;
2935 struct policy_handle hive_hnd, key_hnd;
2936 char *keyname;
2937 char *path;
2938 WERROR result;
2939 TALLOC_CTX *tmp_ctx;
2941 tmp_ctx = talloc_new(mem_ctx);
2942 if (tmp_ctx == NULL) {
2943 return WERR_NOMEM;
2946 path = winreg_printer_data_keyname(tmp_ctx, printer);
2947 if (path == NULL) {
2948 TALLOC_FREE(tmp_ctx);
2949 return WERR_NOMEM;
2952 result = winreg_printer_openkey(tmp_ctx,
2953 server_info,
2954 &winreg_pipe,
2955 path,
2956 key,
2957 false,
2958 access_mask,
2959 &hive_hnd,
2960 &key_hnd);
2961 if (!W_ERROR_IS_OK(result)) {
2962 /* key doesn't exist */
2963 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2964 result = WERR_OK;
2965 goto done;
2968 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
2969 key, win_errstr(result)));
2970 goto done;
2973 if (is_valid_policy_hnd(&key_hnd)) {
2974 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
2977 if (key == NULL || key[0] == '\0') {
2978 keyname = path;
2979 } else {
2980 keyname = talloc_asprintf(tmp_ctx,
2981 "%s\\%s",
2982 path,
2983 key);
2984 if (keyname == NULL) {
2985 result = WERR_NOMEM;
2986 goto done;
2990 result = winreg_printer_delete_subkeys(tmp_ctx,
2991 winreg_pipe,
2992 &hive_hnd,
2993 access_mask,
2994 keyname);
2995 if (!W_ERROR_IS_OK(result)) {
2996 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2997 key, win_errstr(result)));
2998 goto done;
3001 done:
3002 if (winreg_pipe != NULL) {
3003 if (is_valid_policy_hnd(&key_hnd)) {
3004 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3006 if (is_valid_policy_hnd(&hive_hnd)) {
3007 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3011 TALLOC_FREE(tmp_ctx);
3012 return result;
3015 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
3016 struct auth_serversupplied_info *server_info,
3017 const char *printer)
3019 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3020 struct rpc_pipe_client *winreg_pipe = NULL;
3021 struct policy_handle hive_hnd, key_hnd;
3022 char *path;
3023 WERROR result;
3024 TALLOC_CTX *tmp_ctx;
3026 tmp_ctx = talloc_new(mem_ctx);
3027 if (tmp_ctx == NULL) {
3028 return WERR_NOMEM;
3031 path = winreg_printer_data_keyname(tmp_ctx, printer);
3032 if (path == NULL) {
3033 TALLOC_FREE(tmp_ctx);
3034 return WERR_NOMEM;
3037 ZERO_STRUCT(hive_hnd);
3038 ZERO_STRUCT(key_hnd);
3040 result = winreg_printer_openkey(tmp_ctx,
3041 server_info,
3042 &winreg_pipe,
3043 path,
3045 false,
3046 access_mask,
3047 &hive_hnd,
3048 &key_hnd);
3049 if (!W_ERROR_IS_OK(result)) {
3050 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
3051 path, win_errstr(result)));
3052 goto done;
3055 result = winreg_printer_write_dword(tmp_ctx,
3056 winreg_pipe,
3057 &key_hnd,
3058 "ChangeID",
3059 winreg_printer_rev_changeid());
3060 if (!W_ERROR_IS_OK(result)) {
3061 goto done;
3064 result = WERR_OK;
3065 done:
3066 if (winreg_pipe != NULL) {
3067 if (is_valid_policy_hnd(&key_hnd)) {
3068 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3070 if (is_valid_policy_hnd(&hive_hnd)) {
3071 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3075 TALLOC_FREE(tmp_ctx);
3076 return result;
3079 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
3080 struct auth_serversupplied_info *server_info,
3081 const char *printer,
3082 uint32_t *pchangeid)
3084 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3085 struct rpc_pipe_client *winreg_pipe = NULL;
3086 struct policy_handle hive_hnd, key_hnd;
3087 uint32_t changeid = 0;
3088 char *path;
3089 WERROR result;
3090 TALLOC_CTX *tmp_ctx;
3092 tmp_ctx = talloc_new(mem_ctx);
3093 if (tmp_ctx == NULL) {
3094 return WERR_NOMEM;
3097 path = winreg_printer_data_keyname(tmp_ctx, printer);
3098 if (path == NULL) {
3099 TALLOC_FREE(tmp_ctx);
3100 return WERR_NOMEM;
3103 ZERO_STRUCT(hive_hnd);
3104 ZERO_STRUCT(key_hnd);
3106 result = winreg_printer_openkey(tmp_ctx,
3107 server_info,
3108 &winreg_pipe,
3109 path,
3111 false,
3112 access_mask,
3113 &hive_hnd,
3114 &key_hnd);
3115 if (!W_ERROR_IS_OK(result)) {
3116 DEBUG(0, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
3117 path, win_errstr(result)));
3118 goto done;
3121 DEBUG(0, ("winreg_printer_get_changeid: get changeid from %s\n", path));
3122 result = winreg_printer_query_dword(tmp_ctx,
3123 winreg_pipe,
3124 &key_hnd,
3125 "ChangeID",
3126 &changeid);
3127 if (!W_ERROR_IS_OK(result)) {
3128 goto done;
3131 if (pchangeid) {
3132 *pchangeid = changeid;
3135 result = WERR_OK;
3136 done:
3137 if (winreg_pipe != NULL) {
3138 if (is_valid_policy_hnd(&key_hnd)) {
3139 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3141 if (is_valid_policy_hnd(&hive_hnd)) {
3142 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3146 TALLOC_FREE(tmp_ctx);
3147 return result;
3151 * The special behaviour of the spoolss forms is documented at the website:
3153 * Managing Win32 Printserver Forms
3154 * http://unixwiz.net/techtips/winspooler-forms.html
3157 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
3158 struct auth_serversupplied_info *server_info,
3159 struct spoolss_AddFormInfo1 *form)
3161 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3162 struct rpc_pipe_client *winreg_pipe = NULL;
3163 struct policy_handle hive_hnd, key_hnd;
3164 struct winreg_String wvalue;
3165 DATA_BLOB blob;
3166 uint32_t num_info = 0;
3167 union spoolss_FormInfo *info = NULL;
3168 uint32_t i;
3169 WERROR result;
3170 NTSTATUS status;
3171 TALLOC_CTX *tmp_ctx;
3173 tmp_ctx = talloc_new(mem_ctx);
3174 if (tmp_ctx == NULL) {
3175 return WERR_NOMEM;
3178 ZERO_STRUCT(hive_hnd);
3179 ZERO_STRUCT(key_hnd);
3181 result = winreg_printer_openkey(tmp_ctx,
3182 server_info,
3183 &winreg_pipe,
3184 TOP_LEVEL_CONTROL_FORMS_KEY,
3186 true,
3187 access_mask,
3188 &hive_hnd,
3189 &key_hnd);
3190 if (!W_ERROR_IS_OK(result)) {
3191 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
3192 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3193 goto done;
3196 result = winreg_printer_enumforms1(tmp_ctx, server_info, &num_info, &info);
3197 if (!W_ERROR_IS_OK(result)) {
3198 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
3199 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3200 goto done;
3203 /* If form name already exists or is builtin return ALREADY_EXISTS */
3204 for (i = 0; i < num_info; i++) {
3205 if (strequal(info[i].info1.form_name, form->form_name)) {
3206 result = WERR_FILE_EXISTS;
3207 goto done;
3211 wvalue.name = form->form_name;
3213 blob = data_blob_talloc(tmp_ctx, NULL, 32);
3214 SIVAL(blob.data, 0, form->size.width);
3215 SIVAL(blob.data, 4, form->size.height);
3216 SIVAL(blob.data, 8, form->area.left);
3217 SIVAL(blob.data, 12, form->area.top);
3218 SIVAL(blob.data, 16, form->area.right);
3219 SIVAL(blob.data, 20, form->area.bottom);
3220 SIVAL(blob.data, 24, num_info + 1); /* FIXME */
3221 SIVAL(blob.data, 28, form->flags);
3223 status = rpccli_winreg_SetValue(winreg_pipe,
3224 tmp_ctx,
3225 &key_hnd,
3226 wvalue,
3227 REG_BINARY,
3228 blob.data,
3229 blob.length,
3230 &result);
3231 if (!NT_STATUS_IS_OK(status)) {
3232 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
3233 wvalue.name, nt_errstr(status)));
3234 if (!W_ERROR_IS_OK(result)) {
3235 goto done;
3237 result = ntstatus_to_werror(status);
3238 goto done;
3241 result = WERR_OK;
3242 done:
3243 if (winreg_pipe != NULL) {
3244 if (is_valid_policy_hnd(&key_hnd)) {
3245 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3247 if (is_valid_policy_hnd(&hive_hnd)) {
3248 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3252 TALLOC_FREE(info);
3253 TALLOC_FREE(tmp_ctx);
3254 return result;
3257 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
3258 struct auth_serversupplied_info *server_info,
3259 uint32_t *pnum_info,
3260 union spoolss_FormInfo **pinfo)
3262 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3263 struct rpc_pipe_client *winreg_pipe = NULL;
3264 struct policy_handle hive_hnd, key_hnd;
3265 union spoolss_FormInfo *info;
3266 struct spoolss_PrinterEnumValues *enum_values = NULL;
3267 uint32_t num_values = 0;
3268 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3269 uint32_t i;
3270 WERROR result;
3271 TALLOC_CTX *tmp_ctx;
3273 tmp_ctx = talloc_new(mem_ctx);
3274 if (tmp_ctx == NULL) {
3275 return WERR_NOMEM;
3278 ZERO_STRUCT(hive_hnd);
3279 ZERO_STRUCT(key_hnd);
3281 result = winreg_printer_openkey(tmp_ctx,
3282 server_info,
3283 &winreg_pipe,
3284 TOP_LEVEL_CONTROL_FORMS_KEY,
3286 true,
3287 access_mask,
3288 &hive_hnd,
3289 &key_hnd);
3290 if (!W_ERROR_IS_OK(result)) {
3291 /* key doesn't exist */
3292 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3293 result = WERR_OK;
3294 goto done;
3297 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
3298 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3299 goto done;
3302 result = winreg_printer_enumvalues(tmp_ctx,
3303 winreg_pipe,
3304 &key_hnd,
3305 &num_values,
3306 &enum_values);
3307 if (!W_ERROR_IS_OK(result)) {
3308 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
3309 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3310 goto done;
3313 info = TALLOC_ARRAY(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
3314 if (info == NULL) {
3315 result = WERR_NOMEM;
3316 goto done;
3319 /* Enumerate BUILTIN forms */
3320 for (i = 0; i < num_builtin; i++) {
3321 info[i].info1 = builtin_forms1[i];
3324 /* Enumerate registry forms */
3325 for (i = 0; i < num_values; i++) {
3326 union spoolss_FormInfo val;
3328 if (enum_values[i].type != REG_BINARY ||
3329 enum_values[i].data_length != 32) {
3330 continue;
3333 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
3334 if (val.info1.form_name == NULL) {
3335 result = WERR_NOMEM;
3336 goto done;
3339 val.info1.size.width = IVAL(enum_values[i].data->data, 0);
3340 val.info1.size.height = IVAL(enum_values[i].data->data, 4);
3341 val.info1.area.left = IVAL(enum_values[i].data->data, 8);
3342 val.info1.area.top = IVAL(enum_values[i].data->data, 12);
3343 val.info1.area.right = IVAL(enum_values[i].data->data, 16);
3344 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
3345 /* skip form index IVAL(enum_values[i].data->data, 24)));*/
3346 val.info1.flags = IVAL(enum_values[i].data->data, 28);
3348 info[i + num_builtin] = val;
3351 *pnum_info = num_builtin + num_values;
3352 if (pinfo) {
3353 *pinfo = talloc_move(mem_ctx, &info);
3356 done:
3357 if (winreg_pipe != NULL) {
3358 if (is_valid_policy_hnd(&key_hnd)) {
3359 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3361 if (is_valid_policy_hnd(&hive_hnd)) {
3362 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3366 TALLOC_FREE(enum_values);
3367 TALLOC_FREE(tmp_ctx);
3368 return result;
3371 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
3372 struct auth_serversupplied_info *server_info,
3373 const char *form_name)
3375 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3376 struct rpc_pipe_client *winreg_pipe = NULL;
3377 struct policy_handle hive_hnd, key_hnd;
3378 struct winreg_String wvalue;
3379 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3380 uint32_t i;
3381 WERROR result = WERR_OK;
3382 NTSTATUS status;
3383 TALLOC_CTX *tmp_ctx;
3385 for (i = 0; i < num_builtin; i++) {
3386 if (strequal(builtin_forms1[i].form_name, form_name)) {
3387 return WERR_INVALID_PARAMETER;
3391 tmp_ctx = talloc_new(mem_ctx);
3392 if (tmp_ctx == NULL) {
3393 return WERR_NOMEM;
3396 ZERO_STRUCT(hive_hnd);
3397 ZERO_STRUCT(key_hnd);
3399 result = winreg_printer_openkey(tmp_ctx,
3400 server_info,
3401 &winreg_pipe,
3402 TOP_LEVEL_CONTROL_FORMS_KEY,
3404 false,
3405 access_mask,
3406 &hive_hnd,
3407 &key_hnd);
3408 if (!W_ERROR_IS_OK(result)) {
3409 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
3410 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3411 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3412 result = WERR_INVALID_FORM_NAME;
3414 goto done;
3417 wvalue.name = form_name;
3418 status = rpccli_winreg_DeleteValue(winreg_pipe,
3419 tmp_ctx,
3420 &key_hnd,
3421 wvalue,
3422 &result);
3423 if (!NT_STATUS_IS_OK(status)) {
3424 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
3425 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3426 result = WERR_INVALID_FORM_NAME;
3427 goto done;
3429 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
3430 wvalue.name, nt_errstr(status)));
3431 if (!W_ERROR_IS_OK(result)) {
3432 goto done;
3434 result = ntstatus_to_werror(status);
3435 goto done;
3438 result = WERR_OK;
3439 done:
3440 if (winreg_pipe != NULL) {
3441 if (is_valid_policy_hnd(&key_hnd)) {
3442 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3444 if (is_valid_policy_hnd(&hive_hnd)) {
3445 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3449 TALLOC_FREE(tmp_ctx);
3450 return result;
3453 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
3454 struct auth_serversupplied_info *server_info,
3455 const char *form_name,
3456 struct spoolss_AddFormInfo1 *form)
3458 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3459 struct rpc_pipe_client *winreg_pipe = NULL;
3460 struct policy_handle hive_hnd, key_hnd;
3461 struct winreg_String wvalue;
3462 DATA_BLOB blob;
3463 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3464 uint32_t i;
3465 WERROR result;
3466 NTSTATUS status;
3467 TALLOC_CTX *tmp_ctx = NULL;
3469 for (i = 0; i < num_builtin; i++) {
3470 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
3471 result = WERR_INVALID_PARAM;
3472 goto done;
3476 tmp_ctx = talloc_new(mem_ctx);
3477 if (tmp_ctx == NULL) {
3478 return WERR_NOMEM;
3481 ZERO_STRUCT(hive_hnd);
3482 ZERO_STRUCT(key_hnd);
3484 result = winreg_printer_openkey(tmp_ctx,
3485 server_info,
3486 &winreg_pipe,
3487 TOP_LEVEL_CONTROL_FORMS_KEY,
3489 true,
3490 access_mask,
3491 &hive_hnd,
3492 &key_hnd);
3493 if (!W_ERROR_IS_OK(result)) {
3494 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
3495 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3496 goto done;
3499 /* If form_name != form->form_name then we renamed the form */
3500 if (strequal(form_name, form->form_name)) {
3501 result = winreg_printer_deleteform1(tmp_ctx, server_info, form_name);
3502 if (!W_ERROR_IS_OK(result)) {
3503 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
3504 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3505 goto done;
3509 wvalue.name = form->form_name;
3511 blob = data_blob_talloc(tmp_ctx, NULL, 32);
3512 SIVAL(blob.data, 0, form->size.width);
3513 SIVAL(blob.data, 4, form->size.height);
3514 SIVAL(blob.data, 8, form->area.left);
3515 SIVAL(blob.data, 12, form->area.top);
3516 SIVAL(blob.data, 16, form->area.right);
3517 SIVAL(blob.data, 20, form->area.bottom);
3518 SIVAL(blob.data, 24, 42);
3519 SIVAL(blob.data, 28, form->flags);
3521 status = rpccli_winreg_SetValue(winreg_pipe,
3522 tmp_ctx,
3523 &key_hnd,
3524 wvalue,
3525 REG_BINARY,
3526 blob.data,
3527 blob.length,
3528 &result);
3529 if (!NT_STATUS_IS_OK(status)) {
3530 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
3531 wvalue.name, nt_errstr(status)));
3532 if (!W_ERROR_IS_OK(result)) {
3533 goto done;
3535 result = ntstatus_to_werror(status);
3536 goto done;
3539 result = WERR_OK;
3540 done:
3541 if (winreg_pipe != NULL) {
3542 if (is_valid_policy_hnd(&key_hnd)) {
3543 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3545 if (is_valid_policy_hnd(&hive_hnd)) {
3546 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3550 TALLOC_FREE(tmp_ctx);
3551 return result;
3554 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
3555 struct auth_serversupplied_info *server_info,
3556 const char *form_name,
3557 struct spoolss_FormInfo1 *r)
3559 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3560 struct rpc_pipe_client *winreg_pipe = NULL;
3561 struct policy_handle hive_hnd, key_hnd;
3562 struct winreg_String wvalue;
3563 enum winreg_Type type_in;
3564 uint8_t *data_in;
3565 uint32_t data_in_size = 0;
3566 uint32_t value_len = 0;
3567 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3568 uint32_t i;
3569 WERROR result;
3570 NTSTATUS status;
3571 TALLOC_CTX *tmp_ctx;
3573 /* check builtin forms first */
3574 for (i = 0; i < num_builtin; i++) {
3575 if (strequal(builtin_forms1[i].form_name, form_name)) {
3576 *r = builtin_forms1[i];
3577 return WERR_OK;
3581 tmp_ctx = talloc_new(mem_ctx);
3582 if (tmp_ctx == NULL) {
3583 return WERR_NOMEM;
3586 ZERO_STRUCT(hive_hnd);
3587 ZERO_STRUCT(key_hnd);
3589 result = winreg_printer_openkey(tmp_ctx,
3590 server_info,
3591 &winreg_pipe,
3592 TOP_LEVEL_CONTROL_FORMS_KEY,
3594 true,
3595 access_mask,
3596 &hive_hnd,
3597 &key_hnd);
3598 if (!W_ERROR_IS_OK(result)) {
3599 DEBUG(0, ("winreg_printer_getform1: Could not open key %s: %s\n",
3600 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3601 goto done;
3604 wvalue.name = form_name;
3607 * call QueryValue once with data == NULL to get the
3608 * needed memory size to be allocated, then allocate
3609 * data buffer and call again.
3611 status = rpccli_winreg_QueryValue(winreg_pipe,
3612 tmp_ctx,
3613 &key_hnd,
3614 &wvalue,
3615 &type_in,
3616 NULL,
3617 &data_in_size,
3618 &value_len,
3619 &result);
3620 if (!NT_STATUS_IS_OK(status)) {
3621 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3622 wvalue.name, nt_errstr(status)));
3623 if (!W_ERROR_IS_OK(result)) {
3624 goto done;
3626 result = ntstatus_to_werror(status);
3627 goto done;
3630 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
3631 if (data_in == NULL) {
3632 result = WERR_NOMEM;
3633 goto done;
3635 value_len = 0;
3637 status = rpccli_winreg_QueryValue(winreg_pipe,
3638 tmp_ctx,
3639 &key_hnd,
3640 &wvalue,
3641 &type_in,
3642 data_in,
3643 &data_in_size,
3644 &value_len,
3645 &result);
3646 if (!NT_STATUS_IS_OK(status)) {
3647 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3648 wvalue.name, nt_errstr(status)));
3649 if (!W_ERROR_IS_OK(result)) {
3650 goto done;
3652 result = ntstatus_to_werror(status);
3653 goto done;
3656 r->form_name = talloc_strdup(mem_ctx, form_name);
3657 if (r->form_name == NULL) {
3658 result = WERR_NOMEM;
3659 goto done;
3662 r->size.width = IVAL(data_in, 0);
3663 r->size.height = IVAL(data_in, 4);
3664 r->area.left = IVAL(data_in, 8);
3665 r->area.top = IVAL(data_in, 12);
3666 r->area.right = IVAL(data_in, 16);
3667 r->area.bottom = IVAL(data_in, 20);
3668 /* skip index IVAL(data_in, 24)));*/
3669 r->flags = IVAL(data_in, 28);
3671 result = WERR_OK;
3672 done:
3673 if (winreg_pipe != NULL) {
3674 if (is_valid_policy_hnd(&key_hnd)) {
3675 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3677 if (is_valid_policy_hnd(&hive_hnd)) {
3678 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3682 TALLOC_FREE(tmp_ctx);
3683 return result;
3686 WERROR winreg_add_driver(TALLOC_CTX *mem_ctx,
3687 struct auth_serversupplied_info *server_info,
3688 struct spoolss_AddDriverInfoCtr *r,
3689 const char **driver_name,
3690 uint32_t *driver_version)
3692 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3693 struct rpc_pipe_client *winreg_pipe = NULL;
3694 struct policy_handle hive_hnd, key_hnd;
3695 struct spoolss_DriverInfo8 info8;
3696 TALLOC_CTX *tmp_ctx = NULL;
3697 WERROR result;
3699 ZERO_STRUCT(hive_hnd);
3700 ZERO_STRUCT(key_hnd);
3701 ZERO_STRUCT(info8);
3703 if (!driver_info_ctr_to_info8(r, &info8)) {
3704 result = WERR_INVALID_PARAMETER;
3705 goto done;
3708 tmp_ctx = talloc_new(mem_ctx);
3709 if (tmp_ctx == NULL) {
3710 return WERR_NOMEM;
3713 result = winreg_printer_opendriver(tmp_ctx,
3714 server_info,
3715 info8.driver_name,
3716 info8.architecture,
3717 info8.version,
3718 access_mask, true,
3719 &winreg_pipe,
3720 &hive_hnd,
3721 &key_hnd);
3722 if (!W_ERROR_IS_OK(result)) {
3723 DEBUG(0, ("winreg_add_driver: "
3724 "Could not open driver key (%s,%s,%d): %s\n",
3725 info8.driver_name, info8.architecture,
3726 info8.version, win_errstr(result)));
3727 goto done;
3730 /* TODO: "Attributes" ? */
3732 result = winreg_printer_write_dword(tmp_ctx, winreg_pipe,
3733 &key_hnd, "Version",
3734 info8.version);
3735 if (!W_ERROR_IS_OK(result)) {
3736 goto done;
3739 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3740 &key_hnd, "Driver",
3741 info8.driver_path);
3742 if (!W_ERROR_IS_OK(result)) {
3743 goto done;
3746 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3747 &key_hnd, "Data File",
3748 info8.data_file);
3749 if (!W_ERROR_IS_OK(result)) {
3750 goto done;
3753 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3754 &key_hnd, "Configuration File",
3755 info8.config_file);
3756 if (!W_ERROR_IS_OK(result)) {
3757 goto done;
3760 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3761 &key_hnd, "Help File",
3762 info8.help_file);
3763 if (!W_ERROR_IS_OK(result)) {
3764 goto done;
3767 result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
3768 &key_hnd, "Dependent Files",
3769 info8.dependent_files);
3770 if (!W_ERROR_IS_OK(result)) {
3771 goto done;
3774 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3775 &key_hnd, "Monitor",
3776 info8.monitor_name);
3777 if (!W_ERROR_IS_OK(result)) {
3778 goto done;
3781 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3782 &key_hnd, "Datatype",
3783 info8.default_datatype);
3784 if (!W_ERROR_IS_OK(result)) {
3785 goto done;
3788 result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
3789 &key_hnd, "Previous Names",
3790 info8.previous_names);
3791 if (!W_ERROR_IS_OK(result)) {
3792 goto done;
3795 result = winreg_printer_write_date(tmp_ctx, winreg_pipe,
3796 &key_hnd, "DriverDate",
3797 info8.driver_date);
3798 if (!W_ERROR_IS_OK(result)) {
3799 goto done;
3802 result = winreg_printer_write_ver(tmp_ctx, winreg_pipe,
3803 &key_hnd, "DriverVersion",
3804 info8.driver_version);
3805 if (!W_ERROR_IS_OK(result)) {
3806 goto done;
3809 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3810 &key_hnd, "Manufacturer",
3811 info8.manufacturer_name);
3812 if (!W_ERROR_IS_OK(result)) {
3813 goto done;
3816 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3817 &key_hnd, "OEM URL",
3818 info8.manufacturer_url);
3819 if (!W_ERROR_IS_OK(result)) {
3820 goto done;
3823 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3824 &key_hnd, "HardwareID",
3825 info8.hardware_id);
3826 if (!W_ERROR_IS_OK(result)) {
3827 goto done;
3830 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3831 &key_hnd, "Provider",
3832 info8.provider);
3833 if (!W_ERROR_IS_OK(result)) {
3834 goto done;
3837 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3838 &key_hnd, "Print Processor",
3839 info8.print_processor);
3840 if (!W_ERROR_IS_OK(result)) {
3841 goto done;
3844 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3845 &key_hnd, "VendorSetup",
3846 info8.vendor_setup);
3847 if (!W_ERROR_IS_OK(result)) {
3848 goto done;
3851 result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
3852 &key_hnd, "Color Profiles",
3853 info8.color_profiles);
3854 if (!W_ERROR_IS_OK(result)) {
3855 goto done;
3858 result = winreg_printer_write_sz(tmp_ctx, winreg_pipe,
3859 &key_hnd, "InfPath",
3860 info8.inf_path);
3861 if (!W_ERROR_IS_OK(result)) {
3862 goto done;
3865 result = winreg_printer_write_dword(tmp_ctx, winreg_pipe, &key_hnd,
3866 "PrinterDriverAttributes",
3867 info8.printer_driver_attributes);
3868 if (!W_ERROR_IS_OK(result)) {
3869 goto done;
3872 result = winreg_printer_write_multi_sz(tmp_ctx, winreg_pipe,
3873 &key_hnd, "CoreDependencies",
3874 info8.core_driver_dependencies);
3875 if (!W_ERROR_IS_OK(result)) {
3876 goto done;
3879 result = winreg_printer_write_date(tmp_ctx, winreg_pipe,
3880 &key_hnd, "MinInboxDriverVerDate",
3881 info8.min_inbox_driver_ver_date);
3882 if (!W_ERROR_IS_OK(result)) {
3883 goto done;
3886 result = winreg_printer_write_ver(tmp_ctx, winreg_pipe, &key_hnd,
3887 "MinInboxDriverVerVersion",
3888 info8.min_inbox_driver_ver_version);
3889 if (!W_ERROR_IS_OK(result)) {
3890 goto done;
3893 *driver_name = info8.driver_name;
3894 *driver_version = info8.version;
3895 result = WERR_OK;
3896 done:
3897 if (winreg_pipe != NULL) {
3898 if (is_valid_policy_hnd(&key_hnd)) {
3899 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
3901 if (is_valid_policy_hnd(&hive_hnd)) {
3902 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
3906 TALLOC_FREE(tmp_ctx);
3907 return result;
3910 WERROR winreg_get_driver(TALLOC_CTX *mem_ctx,
3911 struct auth_serversupplied_info *server_info,
3912 const char *architecture,
3913 const char *driver_name,
3914 uint32_t driver_version,
3915 struct spoolss_DriverInfo8 **_info8)
3917 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3918 struct rpc_pipe_client *winreg_pipe = NULL;
3919 struct policy_handle hive_hnd, key_hnd;
3920 struct spoolss_DriverInfo8 i8, *info8;
3921 struct spoolss_PrinterEnumValues *enum_values = NULL;
3922 struct spoolss_PrinterEnumValues *v;
3923 uint32_t num_values = 0;
3924 TALLOC_CTX *tmp_ctx;
3925 WERROR result;
3926 uint32_t i;
3928 ZERO_STRUCT(hive_hnd);
3929 ZERO_STRUCT(key_hnd);
3930 ZERO_STRUCT(i8);
3932 tmp_ctx = talloc_new(mem_ctx);
3933 if (tmp_ctx == NULL) {
3934 return WERR_NOMEM;
3937 if (driver_version == DRIVER_ANY_VERSION) {
3938 /* look for Win2k first and then for NT4 */
3939 result = winreg_printer_opendriver(tmp_ctx,
3940 server_info,
3941 driver_name,
3942 architecture,
3944 access_mask, false,
3945 &winreg_pipe,
3946 &hive_hnd,
3947 &key_hnd);
3948 if (!W_ERROR_IS_OK(result)) {
3949 result = winreg_printer_opendriver(tmp_ctx,
3950 server_info,
3951 driver_name,
3952 architecture,
3954 access_mask, false,
3955 &winreg_pipe,
3956 &hive_hnd,
3957 &key_hnd);
3959 } else {
3960 /* ok normal case */
3961 result = winreg_printer_opendriver(tmp_ctx,
3962 server_info,
3963 driver_name,
3964 architecture,
3965 driver_version,
3966 access_mask, false,
3967 &winreg_pipe,
3968 &hive_hnd,
3969 &key_hnd);
3971 if (!W_ERROR_IS_OK(result)) {
3972 DEBUG(5, ("winreg_get_driver: "
3973 "Could not open driver key (%s,%s,%d): %s\n",
3974 driver_name, architecture,
3975 driver_version, win_errstr(result)));
3976 goto done;
3979 result = winreg_printer_enumvalues(tmp_ctx,
3980 winreg_pipe,
3981 &key_hnd,
3982 &num_values,
3983 &enum_values);
3984 if (!W_ERROR_IS_OK(result)) {
3985 DEBUG(0, ("winreg_get_driver: "
3986 "Could not enumerate values for (%s,%s,%d): %s\n",
3987 driver_name, architecture,
3988 driver_version, win_errstr(result)));
3989 goto done;
3992 info8 = talloc_zero(tmp_ctx, struct spoolss_DriverInfo8);
3993 if (info8 == NULL) {
3994 result = WERR_NOMEM;
3995 goto done;
3998 info8->driver_name = talloc_strdup(info8, driver_name);
3999 if (info8->driver_name == NULL) {
4000 result = WERR_NOMEM;
4001 goto done;
4004 info8->architecture = talloc_strdup(info8, architecture);
4005 if (info8->architecture == NULL) {
4006 result = WERR_NOMEM;
4007 goto done;
4010 info8->config_file = EMPTY_STRING;
4011 info8->data_file = EMPTY_STRING;
4012 info8->default_datatype = EMPTY_STRING;
4013 info8->driver_path = EMPTY_STRING;
4014 info8->hardware_id = EMPTY_STRING;
4015 info8->help_file = EMPTY_STRING;
4016 info8->inf_path = EMPTY_STRING;
4017 info8->manufacturer_name = EMPTY_STRING;
4018 info8->manufacturer_url = EMPTY_STRING;
4019 info8->monitor_name = EMPTY_STRING;
4020 info8->print_processor = EMPTY_STRING;
4021 info8->provider = EMPTY_STRING;
4022 info8->vendor_setup = EMPTY_STRING;
4024 info8->color_profiles = empty_string_array;
4025 info8->core_driver_dependencies = EMPTY_STRING_ARRAY;
4026 info8->dependent_files = EMPTY_STRING_ARRAY;
4027 info8->previous_names = EMPTY_STRING_ARRAY;
4029 result = WERR_OK;
4031 for (i = 0; i < num_values; i++) {
4032 const char *tmp_str;
4034 v = &enum_values[i];
4036 result = winreg_enumval_to_dword(info8, v,
4037 "Version",
4038 &info8->version);
4039 CHECK_ERROR(result);
4041 result = winreg_enumval_to_sz(info8, v,
4042 "Driver",
4043 &info8->driver_path);
4044 CHECK_ERROR(result);
4046 result = winreg_enumval_to_sz(info8, v,
4047 "Data File",
4048 &info8->data_file);
4049 CHECK_ERROR(result);
4051 result = winreg_enumval_to_sz(info8, v,
4052 "Configuration File",
4053 &info8->config_file);
4054 CHECK_ERROR(result);
4056 result = winreg_enumval_to_sz(info8, v,
4057 "Help File",
4058 &info8->help_file);
4059 CHECK_ERROR(result);
4061 result = winreg_enumval_to_multi_sz(info8, v,
4062 "Dependent Files",
4063 &info8->dependent_files);
4064 CHECK_ERROR(result);
4066 result = winreg_enumval_to_sz(info8, v,
4067 "Monitor",
4068 &info8->monitor_name);
4069 CHECK_ERROR(result);
4071 result = winreg_enumval_to_sz(info8, v,
4072 "Datatype",
4073 &info8->default_datatype);
4074 CHECK_ERROR(result);
4076 result = winreg_enumval_to_multi_sz(info8, v,
4077 "Previous Names",
4078 &info8->previous_names);
4079 CHECK_ERROR(result);
4081 result = winreg_enumval_to_sz(info8, v,
4082 "DriverDate",
4083 &tmp_str);
4084 if (W_ERROR_IS_OK(result)) {
4085 result = winreg_printer_date_to_NTTIME(tmp_str,
4086 &info8->driver_date);
4088 CHECK_ERROR(result);
4090 result = winreg_enumval_to_sz(info8, v,
4091 "DriverVersion",
4092 &tmp_str);
4093 if (W_ERROR_IS_OK(result)) {
4094 result = winreg_printer_ver_to_dword(tmp_str,
4095 &info8->driver_version);
4097 CHECK_ERROR(result);
4099 result = winreg_enumval_to_sz(info8, v,
4100 "Manufacturer",
4101 &info8->manufacturer_name);
4102 CHECK_ERROR(result);
4104 result = winreg_enumval_to_sz(info8, v,
4105 "OEM URL",
4106 &info8->manufacturer_url);
4107 CHECK_ERROR(result);
4109 result = winreg_enumval_to_sz(info8, v,
4110 "HardwareID",
4111 &info8->hardware_id);
4112 CHECK_ERROR(result);
4114 result = winreg_enumval_to_sz(info8, v,
4115 "Provider",
4116 &info8->provider);
4117 CHECK_ERROR(result);
4119 result = winreg_enumval_to_sz(info8, v,
4120 "Print Processor",
4121 &info8->print_processor);
4122 CHECK_ERROR(result);
4124 result = winreg_enumval_to_sz(info8, v,
4125 "VendorSetup",
4126 &info8->vendor_setup);
4127 CHECK_ERROR(result);
4129 result = winreg_enumval_to_multi_sz(info8, v,
4130 "Color Profiles",
4131 &info8->color_profiles);
4132 CHECK_ERROR(result);
4134 result = winreg_enumval_to_sz(info8, v,
4135 "InfPath",
4136 &info8->inf_path);
4137 CHECK_ERROR(result);
4139 result = winreg_enumval_to_dword(info8, v,
4140 "PrinterDriverAttributes",
4141 &info8->printer_driver_attributes);
4142 CHECK_ERROR(result);
4144 result = winreg_enumval_to_multi_sz(info8, v,
4145 "CoreDependencies",
4146 &info8->core_driver_dependencies);
4147 CHECK_ERROR(result);
4149 result = winreg_enumval_to_sz(info8, v,
4150 "MinInboxDriverVerDate",
4151 &tmp_str);
4152 if (W_ERROR_IS_OK(result)) {
4153 result = winreg_printer_date_to_NTTIME(tmp_str,
4154 &info8->min_inbox_driver_ver_date);
4156 CHECK_ERROR(result);
4158 result = winreg_enumval_to_sz(info8, v,
4159 "MinInboxDriverVerVersion",
4160 &tmp_str);
4161 if (W_ERROR_IS_OK(result)) {
4162 result = winreg_printer_ver_to_dword(tmp_str,
4163 &info8->min_inbox_driver_ver_version);
4165 CHECK_ERROR(result);
4168 if (!W_ERROR_IS_OK(result)) {
4169 DEBUG(0, ("winreg_enumval_to_TYPE() failed "
4170 "for %s: %s\n", v->value_name,
4171 win_errstr(result)));
4172 goto done;
4175 *_info8 = talloc_steal(mem_ctx, info8);
4176 result = WERR_OK;
4177 done:
4178 if (winreg_pipe != NULL) {
4179 if (is_valid_policy_hnd(&key_hnd)) {
4180 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
4182 if (is_valid_policy_hnd(&hive_hnd)) {
4183 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
4187 TALLOC_FREE(tmp_ctx);
4188 return result;
4191 WERROR winreg_del_driver(TALLOC_CTX *mem_ctx,
4192 struct auth_serversupplied_info *server_info,
4193 struct spoolss_DriverInfo8 *info8,
4194 uint32_t version)
4196 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
4197 struct rpc_pipe_client *winreg_pipe = NULL;
4198 struct policy_handle hive_hnd, key_hnd;
4199 TALLOC_CTX *tmp_ctx;
4200 char *key_name;
4201 WERROR result;
4203 ZERO_STRUCT(hive_hnd);
4204 ZERO_STRUCT(key_hnd);
4206 tmp_ctx = talloc_new(mem_ctx);
4207 if (tmp_ctx == NULL) {
4208 return WERR_NOMEM;
4211 /* test that the key exists */
4212 result = winreg_printer_opendriver(tmp_ctx,
4213 server_info,
4214 info8->driver_name,
4215 info8->architecture,
4216 version,
4217 access_mask, false,
4218 &winreg_pipe,
4219 &hive_hnd,
4220 &key_hnd);
4221 if (!W_ERROR_IS_OK(result)) {
4222 /* key doesn't exist */
4223 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
4224 result = WERR_OK;
4225 goto done;
4228 DEBUG(5, ("winreg_del_driver: "
4229 "Could not open driver (%s,%s,%u): %s\n",
4230 info8->driver_name, info8->architecture,
4231 version, win_errstr(result)));
4232 goto done;
4236 if (is_valid_policy_hnd(&key_hnd)) {
4237 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
4240 key_name = talloc_asprintf(tmp_ctx,
4241 "%s\\Environments\\%s\\Drivers\\Version-%u",
4242 TOP_LEVEL_CONTROL_KEY,
4243 info8->architecture, version);
4244 if (key_name == NULL) {
4245 result = WERR_NOMEM;
4246 goto done;
4249 result = winreg_printer_delete_subkeys(tmp_ctx,
4250 winreg_pipe,
4251 &hive_hnd,
4252 access_mask,
4253 key_name);
4254 if (!W_ERROR_IS_OK(result)) {
4255 DEBUG(0, ("winreg_del_driver: "
4256 "Could not open driver (%s,%s,%u): %s\n",
4257 info8->driver_name, info8->architecture,
4258 version, win_errstr(result)));
4259 goto done;
4262 result = WERR_OK;
4263 done:
4264 if (winreg_pipe != NULL) {
4265 if (is_valid_policy_hnd(&key_hnd)) {
4266 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
4268 if (is_valid_policy_hnd(&hive_hnd)) {
4269 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
4273 TALLOC_FREE(tmp_ctx);
4274 return result;
4277 WERROR winreg_get_driver_list(TALLOC_CTX *mem_ctx,
4278 struct auth_serversupplied_info *server_info,
4279 const char *architecture,
4280 uint32_t version,
4281 uint32_t *num_drivers,
4282 const char ***drivers_p)
4284 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
4285 struct rpc_pipe_client *winreg_pipe = NULL;
4286 struct policy_handle hive_hnd, key_hnd;
4287 const char **drivers;
4288 TALLOC_CTX *tmp_ctx;
4289 WERROR result;
4291 *num_drivers = 0;
4292 *drivers_p = NULL;
4294 ZERO_STRUCT(hive_hnd);
4295 ZERO_STRUCT(key_hnd);
4297 tmp_ctx = talloc_new(mem_ctx);
4298 if (tmp_ctx == NULL) {
4299 return WERR_NOMEM;
4302 /* use NULL for the driver name so we open the key that is
4303 * parent of all drivers for this architecture and version */
4304 result = winreg_printer_opendriver(tmp_ctx,
4305 server_info,
4306 NULL,
4307 architecture,
4308 version,
4309 access_mask, false,
4310 &winreg_pipe,
4311 &hive_hnd,
4312 &key_hnd);
4313 if (!W_ERROR_IS_OK(result)) {
4314 DEBUG(5, ("winreg_get_driver_list: "
4315 "Could not open key (%s,%u): %s\n",
4316 architecture, version, win_errstr(result)));
4317 result = WERR_OK;
4318 goto done;
4321 result = winreg_printer_enumkeys(tmp_ctx,
4322 winreg_pipe,
4323 &key_hnd,
4324 num_drivers,
4325 &drivers);
4326 if (!W_ERROR_IS_OK(result)) {
4327 DEBUG(0, ("winreg_get_driver_list: "
4328 "Could not enumerate drivers for (%s,%u): %s\n",
4329 architecture, version, win_errstr(result)));
4330 goto done;
4333 *drivers_p = talloc_steal(mem_ctx, drivers);
4335 result = WERR_OK;
4336 done:
4337 if (winreg_pipe != NULL) {
4338 if (is_valid_policy_hnd(&key_hnd)) {
4339 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &key_hnd, NULL);
4341 if (is_valid_policy_hnd(&hive_hnd)) {
4342 rpccli_winreg_CloseKey(winreg_pipe, tmp_ctx, &hive_hnd, NULL);
4346 TALLOC_FREE(tmp_ctx);
4347 return result;