s3: Fix Coverity ID 242697 Dereference before null check
[Samba/wip.git] / source3 / rpc_client / cli_winreg_spoolss.c
blobcd2cf681b86a046f152dc862b6c66f8870825487
1 /*
2 * Unix SMB/CIFS implementation.
4 * SPOOLSS RPC Pipe server / winreg client routines
6 * Copyright (c) 2010 Andreas Schneider <asn@samba.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "nt_printing.h"
24 #include "../librpc/gen_ndr/ndr_spoolss.h"
25 #include "../librpc/gen_ndr/ndr_winreg_c.h"
26 #include "../librpc/gen_ndr/ndr_security.h"
27 #include "secrets.h"
28 #include "../libcli/security/security.h"
29 #include "rpc_client/cli_winreg.h"
30 #include "../libcli/registry/util_reg.h"
31 #include "rpc_client/cli_winreg_spoolss.h"
32 #include "printing/nt_printing_os2.h"
33 #include "rpc_client/init_spoolss.h"
35 #define TOP_LEVEL_PRINT_KEY "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print"
36 #define TOP_LEVEL_PRINT_PRINTERS_KEY TOP_LEVEL_PRINT_KEY "\\Printers"
37 #define TOP_LEVEL_CONTROL_KEY "SYSTEM\\CurrentControlSet\\Control\\Print"
38 #define TOP_LEVEL_CONTROL_FORMS_KEY TOP_LEVEL_CONTROL_KEY "\\Forms"
40 #define EMPTY_STRING ""
42 #define FILL_STRING(mem_ctx, in, out) \
43 do { \
44 if (in && strlen(in)) { \
45 out = talloc_strdup(mem_ctx, in); \
46 } else { \
47 out = talloc_strdup(mem_ctx, ""); \
48 } \
49 W_ERROR_HAVE_NO_MEMORY(out); \
50 } while (0);
52 #define CHECK_ERROR(result) \
53 if (W_ERROR_IS_OK(result)) continue; \
54 if (W_ERROR_EQUAL(result, WERR_NOT_FOUND)) result = WERR_OK; \
55 if (!W_ERROR_IS_OK(result)) break
57 /* FLAGS, NAME, with, height, left, top, right, bottom */
58 static const struct spoolss_FormInfo1 builtin_forms1[] = {
59 { SPOOLSS_FORM_BUILTIN, "10x11", {0x3e030,0x44368}, {0x0,0x0,0x3e030,0x44368} },
60 { SPOOLSS_FORM_BUILTIN, "10x14", {0x3e030,0x56d10}, {0x0,0x0,0x3e030,0x56d10} },
61 { SPOOLSS_FORM_BUILTIN, "11x17", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
62 { SPOOLSS_FORM_BUILTIN, "12x11", {0x4a724,0x443e1}, {0x0,0x0,0x4a724,0x443e1} },
63 { SPOOLSS_FORM_BUILTIN, "15x11", {0x5d048,0x44368}, {0x0,0x0,0x5d048,0x44368} },
64 { SPOOLSS_FORM_BUILTIN, "6 3/4 Envelope", {0x167ab,0x284ec}, {0x0,0x0,0x167ab,0x284ec} },
65 { SPOOLSS_FORM_BUILTIN, "9x11", {0x37cf8,0x44368}, {0x0,0x0,0x37cf8,0x44368} },
66 { SPOOLSS_FORM_BUILTIN, "A0", {0xcd528,0x122488},{0x0,0x0,0xcd528,0x122488} },
67 { SPOOLSS_FORM_BUILTIN, "A1", {0x91050,0xcd528}, {0x0,0x0,0x91050,0xcd528} },
68 { SPOOLSS_FORM_BUILTIN, "A2", {0x668a0,0x91050}, {0x0,0x0,0x668a0,0x91050} },
69 { SPOOLSS_FORM_BUILTIN, "A3 Extra Transverse", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
70 { SPOOLSS_FORM_BUILTIN, "A3 Extra", {0x4e9d0,0x6ca48}, {0x0,0x0,0x4e9d0,0x6ca48} },
71 { SPOOLSS_FORM_BUILTIN, "A3 Rotated", {0x668a0,0x48828}, {0x0,0x0,0x668a0,0x48828} },
72 { SPOOLSS_FORM_BUILTIN, "A3 Transverse", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
73 { SPOOLSS_FORM_BUILTIN, "A3", {0x48828,0x668a0}, {0x0,0x0,0x48828,0x668a0} },
74 { SPOOLSS_FORM_BUILTIN, "A4 Extra", {0x397c2,0x4eb16}, {0x0,0x0,0x397c2,0x4eb16} },
75 { SPOOLSS_FORM_BUILTIN, "A4 Plus", {0x33450,0x50910}, {0x0,0x0,0x33450,0x50910} },
76 { SPOOLSS_FORM_BUILTIN, "A4 Rotated", {0x48828,0x33450}, {0x0,0x0,0x48828,0x33450} },
77 { SPOOLSS_FORM_BUILTIN, "A4 Small", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
78 { SPOOLSS_FORM_BUILTIN, "A4 Transverse", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
79 { SPOOLSS_FORM_BUILTIN, "A4", {0x33450,0x48828}, {0x0,0x0,0x33450,0x48828} },
80 { SPOOLSS_FORM_BUILTIN, "A5 Extra", {0x2a7b0,0x395f8}, {0x0,0x0,0x2a7b0,0x395f8} },
81 { SPOOLSS_FORM_BUILTIN, "A5 Rotated", {0x33450,0x24220}, {0x0,0x0,0x33450,0x24220} },
82 { SPOOLSS_FORM_BUILTIN, "A5 Transverse", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
83 { SPOOLSS_FORM_BUILTIN, "A5", {0x24220,0x33450}, {0x0,0x0,0x24220,0x33450} },
84 { SPOOLSS_FORM_BUILTIN, "A6 Rotated", {0x24220,0x19a28}, {0x0,0x0,0x24220,0x19a28} },
85 { SPOOLSS_FORM_BUILTIN, "A6", {0x19a28,0x24220}, {0x0,0x0,0x19a28,0x24220} },
86 { SPOOLSS_FORM_BUILTIN, "B4 (ISO)", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
87 { SPOOLSS_FORM_BUILTIN, "B4 (JIS) Rotated", {0x58de0,0x3ebe8}, {0x0,0x0,0x58de0,0x3ebe8} },
88 { SPOOLSS_FORM_BUILTIN, "B4 (JIS)", {0x3ebe8,0x58de0}, {0x0,0x0,0x3ebe8,0x58de0} },
89 { SPOOLSS_FORM_BUILTIN, "B5 (ISO) Extra", {0x31128,0x43620}, {0x0,0x0,0x31128,0x43620} },
90 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Rotated", {0x3ebe8,0x2c6f0}, {0x0,0x0,0x3ebe8,0x2c6f0} },
91 { SPOOLSS_FORM_BUILTIN, "B5 (JIS) Transverse", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
92 { SPOOLSS_FORM_BUILTIN, "B5 (JIS)", {0x2c6f0,0x3ebe8}, {0x0,0x0,0x2c6f0,0x3ebe8} },
93 { SPOOLSS_FORM_BUILTIN, "B6 (JIS) Rotated", {0x2c6f0,0x1f400}, {0x0,0x0,0x2c6f0,0x1f400} },
94 { SPOOLSS_FORM_BUILTIN, "B6 (JIS)", {0x1f400,0x2c6f0}, {0x0,0x0,0x1f400,0x2c6f0} },
95 { SPOOLSS_FORM_BUILTIN, "C size sheet", {0x696b8,0x886d0}, {0x0,0x0,0x696b8,0x886d0} },
96 { SPOOLSS_FORM_BUILTIN, "D size sheet", {0x886d0,0xd2d70}, {0x0,0x0,0x886d0,0xd2d70} },
97 { SPOOLSS_FORM_BUILTIN, "Double Japan Postcard Rotated", {0x24220,0x30d40}, {0x0,0x0,0x24220,0x30d40} },
98 { SPOOLSS_FORM_BUILTIN, "E size sheet", {0xd2d70,0x110da0},{0x0,0x0,0xd2d70,0x110da0} },
99 { SPOOLSS_FORM_BUILTIN, "Envelope #10", {0x19947,0x3ae94}, {0x0,0x0,0x19947,0x3ae94} },
100 { SPOOLSS_FORM_BUILTIN, "Envelope #11", {0x1be7c,0x40565}, {0x0,0x0,0x1be7c,0x40565} },
101 { SPOOLSS_FORM_BUILTIN, "Envelope #12", {0x1d74a,0x44368}, {0x0,0x0,0x1d74a,0x44368} },
102 { SPOOLSS_FORM_BUILTIN, "Envelope #14", {0x1f018,0x47504}, {0x0,0x0,0x1f018,0x47504} },
103 { SPOOLSS_FORM_BUILTIN, "Envelope #9", {0x18079,0x37091}, {0x0,0x0,0x18079,0x37091} },
104 { SPOOLSS_FORM_BUILTIN, "Envelope B4", {0x3d090,0x562e8}, {0x0,0x0,0x3d090,0x562e8} },
105 { SPOOLSS_FORM_BUILTIN, "Envelope B5", {0x2af80,0x3d090}, {0x0,0x0,0x2af80,0x3d090} },
106 { SPOOLSS_FORM_BUILTIN, "Envelope B6", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
107 { SPOOLSS_FORM_BUILTIN, "Envelope C3", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
108 { SPOOLSS_FORM_BUILTIN, "Envelope C4", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
109 { SPOOLSS_FORM_BUILTIN, "Envelope C5", {0x278d0,0x37e88}, {0x0,0x0,0x278d0,0x37e88} },
110 { SPOOLSS_FORM_BUILTIN, "Envelope C6", {0x1bd50,0x278d0}, {0x0,0x0,0x1bd50,0x278d0} },
111 { SPOOLSS_FORM_BUILTIN, "Envelope C65", {0x1bd50,0x37e88}, {0x0,0x0,0x1bd50,0x37e88} },
112 { SPOOLSS_FORM_BUILTIN, "Envelope DL", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
113 { SPOOLSS_FORM_BUILTIN, "Envelope Invite", {0x35b60,0x35b60}, {0x0,0x0,0x35b60,0x35b60} },
114 { SPOOLSS_FORM_BUILTIN, "Envelope Monarch", {0x18079,0x2e824}, {0x0,0x0,0x18079,0x2e824} },
115 { SPOOLSS_FORM_BUILTIN, "Envelope", {0x1adb0,0x38270}, {0x0,0x0,0x1adb0,0x38270} },
116 { SPOOLSS_FORM_BUILTIN, "Executive", {0x2cf56,0x411cc}, {0x0,0x0,0x2cf56,0x411cc} },
117 { SPOOLSS_FORM_BUILTIN, "Folio", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
118 { SPOOLSS_FORM_BUILTIN, "German Legal Fanfold", {0x34b5c,0x509d8}, {0x0,0x0,0x34b5c,0x509d8} },
119 { SPOOLSS_FORM_BUILTIN, "German Std Fanfold", {0x34b5c,0x4a6a0}, {0x0,0x0,0x34b5c,0x4a6a0} },
120 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #3 Rotated", {0x395f8,0x1d4c0}, {0x0,0x0,0x395f8,0x1d4c0} },
121 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Chou #4 Rotated", {0x320c8,0x15f90}, {0x0,0x0,0x320c8,0x15f90} },
122 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #2 Rotated", {0x510e0,0x3a980}, {0x0,0x0,0x510e0,0x3a980} },
123 { SPOOLSS_FORM_BUILTIN, "Japan Envelope Kaku #3 Rotated", {0x43a08,0x34bc0}, {0x0,0x0,0x43a08,0x34bc0} },
124 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4 Rotated", {0x395f8,0x19a28}, {0x0,0x0,0x395f8,0x19a28} },
125 { SPOOLSS_FORM_BUILTIN, "Japan Envelope You #4", {0x19a28,0x395f8}, {0x0,0x0,0x19a28,0x395f8} },
126 { SPOOLSS_FORM_BUILTIN, "Japanese Double Postcard", {0x30d40,0x24220}, {0x0,0x0,0x30d40,0x24220} },
127 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #3", {0x1d4c0,0x395f8}, {0x0,0x0,0x1d4c0,0x395f8} },
128 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Chou #4", {0x15f90,0x320c8}, {0x0,0x0,0x15f90,0x320c8} },
129 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #2", {0x3a980,0x510e0}, {0x0,0x0,0x3a980,0x510e0} },
130 { SPOOLSS_FORM_BUILTIN, "Japanese Envelope Kaku #3", {0x34bc0,0x43a08}, {0x0,0x0,0x34bc0,0x43a08} },
131 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard Rotated", {0x24220,0x186a0}, {0x0,0x0,0x24220,0x186a0} },
132 { SPOOLSS_FORM_BUILTIN, "Japanese Postcard", {0x186a0,0x24220}, {0x0,0x0,0x186a0,0x24220} },
133 { SPOOLSS_FORM_BUILTIN, "Ledger", {0x696b8,0x44368}, {0x0,0x0,0x696b8,0x44368} },
134 { SPOOLSS_FORM_BUILTIN, "Legal Extra", {0x3ae94,0x5d048}, {0x0,0x0,0x3ae94,0x5d048} },
135 { SPOOLSS_FORM_BUILTIN, "Legal", {0x34b5c,0x56d10}, {0x0,0x0,0x34b5c,0x56d10} },
136 { SPOOLSS_FORM_BUILTIN, "Letter Extra Transverse", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
137 { SPOOLSS_FORM_BUILTIN, "Letter Extra", {0x3ae94,0x4a6a0}, {0x0,0x0,0x3ae94,0x4a6a0} },
138 { SPOOLSS_FORM_BUILTIN, "Letter Plus", {0x34b5c,0x4eb16}, {0x0,0x0,0x34b5c,0x4eb16} },
139 { SPOOLSS_FORM_BUILTIN, "Letter Rotated", {0x44368,0x34b5c}, {0x0,0x0,0x44368,0x34b5c} },
140 { SPOOLSS_FORM_BUILTIN, "Letter Small", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
141 { SPOOLSS_FORM_BUILTIN, "Letter Transverse", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
142 { SPOOLSS_FORM_BUILTIN, "Letter", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
143 { SPOOLSS_FORM_BUILTIN, "Note", {0x34b5c,0x44368}, {0x0,0x0,0x34b5c,0x44368} },
144 { SPOOLSS_FORM_BUILTIN, "PRC 16K Rotated", {0x3f7a0,0x2de60}, {0x0,0x0,0x3f7a0,0x2de60} },
145 { SPOOLSS_FORM_BUILTIN, "PRC 16K", {0x2de60,0x3f7a0}, {0x0,0x0,0x2de60,0x3f7a0} },
146 { SPOOLSS_FORM_BUILTIN, "PRC 32K Rotated", {0x2cec0,0x1fbd0}, {0x0,0x0,0x2cec0,0x1fbd0} },
147 { SPOOLSS_FORM_BUILTIN, "PRC 32K", {0x1fbd0,0x2cec0}, {0x0,0x0,0x1fbd0,0x2cec0} },
148 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big) Rotated", {0x318f8,0x222e0}, {0x0,0x0,0x318f8,0x222e0} },
149 { SPOOLSS_FORM_BUILTIN, "PRC 32K(Big)", {0x222e0,0x318f8}, {0x0,0x0,0x222e0,0x318f8} },
150 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1 Rotated", {0x28488,0x18e70}, {0x0,0x0,0x28488,0x18e70} },
151 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #1", {0x18e70,0x28488}, {0x0,0x0,0x18e70,0x28488} },
152 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10 Rotated", {0x6fd10,0x4f1a0}, {0x0,0x0,0x6fd10,0x4f1a0} },
153 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #10", {0x4f1a0,0x6fd10}, {0x0,0x0,0x4f1a0,0x6fd10} },
154 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2 Rotated", {0x2af80,0x18e70}, {0x0,0x0,0x2af80,0x18e70} },
155 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #2", {0x18e70,0x2af80}, {0x0,0x0,0x18e70,0x2af80} },
156 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3 Rotated", {0x2af80,0x1e848}, {0x0,0x0,0x2af80,0x1e848} },
157 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #3", {0x1e848,0x2af80}, {0x0,0x0,0x1e848,0x2af80} },
158 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4 Rotated", {0x32c80,0x1adb0}, {0x0,0x0,0x32c80,0x1adb0} },
159 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #4", {0x1adb0,0x32c80}, {0x0,0x0,0x1adb0,0x32c80} },
160 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5 Rotated", {0x35b60,0x1adb0}, {0x0,0x0,0x35b60,0x1adb0} },
161 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #5", {0x1adb0,0x35b60}, {0x0,0x0,0x1adb0,0x35b60} },
162 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6 Rotated", {0x38270,0x1d4c0}, {0x0,0x0,0x38270,0x1d4c0} },
163 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #6", {0x1d4c0,0x38270}, {0x0,0x0,0x1d4c0,0x38270} },
164 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7 Rotated", {0x38270,0x27100}, {0x0,0x0,0x38270,0x27100} },
165 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #7", {0x27100,0x38270}, {0x0,0x0,0x27100,0x38270} },
166 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8 Rotated", {0x4b708,0x1d4c0}, {0x0,0x0,0x4b708,0x1d4c0} },
167 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #8", {0x1d4c0,0x4b708}, {0x0,0x0,0x1d4c0,0x4b708} },
168 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9 Rotated", {0x4f1a0,0x37e88}, {0x0,0x0,0x4f1a0,0x37e88} },
169 { SPOOLSS_FORM_BUILTIN, "PRC Envelope #9", {0x37e88,0x4f1a0}, {0x0,0x0,0x37e88,0x4f1a0} },
170 { SPOOLSS_FORM_BUILTIN, "Quarto", {0x347d8,0x43238}, {0x0,0x0,0x347d8,0x43238} },
171 { SPOOLSS_FORM_BUILTIN, "Reserved48", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
172 { SPOOLSS_FORM_BUILTIN, "Reserved49", {0x1,0x1}, {0x0,0x0,0x1,0x1} },
173 { SPOOLSS_FORM_BUILTIN, "Statement", {0x221b4,0x34b5c}, {0x0,0x0,0x221b4,0x34b5c} },
174 { SPOOLSS_FORM_BUILTIN, "Super A", {0x376b8,0x56ea0}, {0x0,0x0,0x376b8,0x56ea0} },
175 { SPOOLSS_FORM_BUILTIN, "Super B", {0x4a768,0x76e58}, {0x0,0x0,0x4a768,0x76e58} },
176 { SPOOLSS_FORM_BUILTIN, "Tabloid Extra", {0x4a6a0,0x6f9f0}, {0x0,0x0,0x4a6a0,0x6f9f0} },
177 { SPOOLSS_FORM_BUILTIN, "Tabloid", {0x44368,0x696b8}, {0x0,0x0,0x44368,0x696b8} },
178 { SPOOLSS_FORM_BUILTIN, "US Std Fanfold", {0x5c3e1,0x44368}, {0x0,0x0,0x5c3e1,0x44368} }
181 /********************************************************************
182 static helper functions
183 ********************************************************************/
185 /****************************************************************************
186 Update the changeid time.
187 ****************************************************************************/
189 * @internal
191 * @brief Update the ChangeID time of a printer.
193 * This is SO NASTY as some drivers need this to change, others need it
194 * static. This value will change every second, and I must hope that this
195 * is enough..... DON'T CHANGE THIS CODE WITHOUT A TEST MATRIX THE SIZE OF
196 * UTAH ! JRA.
198 * @return The ChangeID.
200 static uint32_t winreg_printer_rev_changeid(void)
202 struct timeval tv;
204 get_process_uptime(&tv);
206 #if 1 /* JERRY */
207 /* Return changeid as msec since spooler restart */
208 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
209 #else
211 * This setting seems to work well but is too untested
212 * to replace the above calculation. Left in for experimentation
213 * of the reader --jerry (Tue Mar 12 09:15:05 CST 2002)
215 return tv.tv_sec * 10 + tv.tv_usec / 100000;
216 #endif
219 static WERROR winreg_printer_openkey(TALLOC_CTX *mem_ctx,
220 struct dcerpc_binding_handle *binding_handle,
221 const char *path,
222 const char *key,
223 bool create_key,
224 uint32_t access_mask,
225 struct policy_handle *hive_handle,
226 struct policy_handle *key_handle)
228 struct winreg_String wkey, wkeyclass;
229 char *keyname;
230 NTSTATUS status;
231 WERROR result = WERR_OK;
233 status = dcerpc_winreg_OpenHKLM(binding_handle,
234 mem_ctx,
235 NULL,
236 access_mask,
237 hive_handle,
238 &result);
239 if (!NT_STATUS_IS_OK(status)) {
240 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
241 nt_errstr(status)));
242 return ntstatus_to_werror(status);
244 if (!W_ERROR_IS_OK(result)) {
245 DEBUG(0, ("winreg_printer_openkey: Could not open HKLM hive: %s\n",
246 win_errstr(result)));
247 return result;
250 if (key && *key) {
251 keyname = talloc_asprintf(mem_ctx, "%s\\%s", path, key);
252 } else {
253 keyname = talloc_strdup(mem_ctx, path);
255 if (keyname == NULL) {
256 return WERR_NOMEM;
259 ZERO_STRUCT(wkey);
260 wkey.name = keyname;
262 if (create_key) {
263 enum winreg_CreateAction action = REG_ACTION_NONE;
265 ZERO_STRUCT(wkeyclass);
266 wkeyclass.name = "";
268 status = dcerpc_winreg_CreateKey(binding_handle,
269 mem_ctx,
270 hive_handle,
271 wkey,
272 wkeyclass,
274 access_mask,
275 NULL,
276 key_handle,
277 &action,
278 &result);
279 switch (action) {
280 case REG_ACTION_NONE:
281 DEBUG(8, ("winreg_printer_openkey:createkey did nothing -- huh?\n"));
282 break;
283 case REG_CREATED_NEW_KEY:
284 DEBUG(8, ("winreg_printer_openkey: createkey created %s\n", keyname));
285 break;
286 case REG_OPENED_EXISTING_KEY:
287 DEBUG(8, ("winreg_printer_openkey: createkey opened existing %s\n", keyname));
288 break;
290 } else {
291 status = dcerpc_winreg_OpenKey(binding_handle,
292 mem_ctx,
293 hive_handle,
294 wkey,
296 access_mask,
297 key_handle,
298 &result);
300 if (!NT_STATUS_IS_OK(status)) {
301 result = ntstatus_to_werror(status);
303 if (!W_ERROR_IS_OK(result)) {
304 WERROR ignore;
306 if (is_valid_policy_hnd(hive_handle)) {
307 dcerpc_winreg_CloseKey(binding_handle,
308 mem_ctx,
309 hive_handle,
310 &ignore);
312 ZERO_STRUCTP(hive_handle);
314 return result;
317 return WERR_OK;
321 * @brief Create the registry keyname for the given printer.
323 * @param[in] mem_ctx The memory context to use.
325 * @param[in] printer The name of the printer to get the registry key.
327 * @return The registry key or NULL on error.
329 static char *winreg_printer_data_keyname(TALLOC_CTX *mem_ctx, const char *printer) {
330 return talloc_asprintf(mem_ctx, "%s\\%s", TOP_LEVEL_PRINT_PRINTERS_KEY, printer);
333 static WERROR winreg_printer_opendriver(TALLOC_CTX *mem_ctx,
334 struct dcerpc_binding_handle *winreg_handle,
335 const char *drivername,
336 const char *architecture,
337 uint32_t version,
338 uint32_t access_mask,
339 bool create,
340 struct policy_handle *hive_hnd,
341 struct policy_handle *key_hnd)
343 WERROR result;
344 char *key_name;
346 key_name = talloc_asprintf(mem_ctx, "%s\\Environments\\%s\\Drivers\\Version-%u",
347 TOP_LEVEL_CONTROL_KEY,
348 architecture, version);
349 if (!key_name) {
350 return WERR_NOMEM;
353 result = winreg_printer_openkey(mem_ctx,
354 winreg_handle,
355 key_name,
356 drivername,
357 create,
358 access_mask,
359 hive_hnd,
360 key_hnd);
361 return result;
364 static WERROR winreg_enumval_to_dword(TALLOC_CTX *mem_ctx,
365 struct spoolss_PrinterEnumValues *v,
366 const char *valuename, uint32_t *dw)
368 /* just return if it is not the one we are looking for */
369 if (strcmp(valuename, v->value_name) != 0) {
370 return WERR_NOT_FOUND;
373 if (v->type != REG_DWORD) {
374 return WERR_INVALID_DATATYPE;
377 if (v->data_length != 4) {
378 *dw = 0;
379 return WERR_OK;
382 *dw = IVAL(v->data->data, 0);
383 return WERR_OK;
386 static WERROR winreg_enumval_to_sz(TALLOC_CTX *mem_ctx,
387 struct spoolss_PrinterEnumValues *v,
388 const char *valuename, const char **_str)
390 /* just return if it is not the one we are looking for */
391 if (strcmp(valuename, v->value_name) != 0) {
392 return WERR_NOT_FOUND;
395 if (v->type != REG_SZ) {
396 return WERR_INVALID_DATATYPE;
399 if (v->data_length == 0) {
400 *_str = talloc_strdup(mem_ctx, EMPTY_STRING);
401 if (*_str == NULL) {
402 return WERR_NOMEM;
404 return WERR_OK;
407 if (!pull_reg_sz(mem_ctx, v->data, _str)) {
408 return WERR_NOMEM;
411 return WERR_OK;
414 static WERROR winreg_enumval_to_multi_sz(TALLOC_CTX *mem_ctx,
415 struct spoolss_PrinterEnumValues *v,
416 const char *valuename,
417 const char ***array)
419 /* just return if it is not the one we are looking for */
420 if (strcmp(valuename, v->value_name) != 0) {
421 return WERR_NOT_FOUND;
424 if (v->type != REG_MULTI_SZ) {
425 return WERR_INVALID_DATATYPE;
428 if (v->data_length == 0) {
429 *array = talloc_array(mem_ctx, const char *, 1);
430 if (*array == NULL) {
431 return WERR_NOMEM;
433 *array[0] = NULL;
434 return WERR_OK;
437 if (!pull_reg_multi_sz(mem_ctx, v->data, array)) {
438 return WERR_NOMEM;
441 return WERR_OK;
444 static WERROR winreg_printer_write_date(TALLOC_CTX *mem_ctx,
445 struct dcerpc_binding_handle *winreg_handle,
446 struct policy_handle *key_handle,
447 const char *value,
448 NTTIME data)
450 struct winreg_String wvalue = { 0, };
451 DATA_BLOB blob;
452 WERROR result = WERR_OK;
453 NTSTATUS status;
454 const char *str;
455 struct tm *tm;
456 time_t t;
458 if (data == 0) {
459 str = talloc_strdup(mem_ctx, "01/01/1601");
460 } else {
461 t = nt_time_to_unix(data);
462 tm = localtime(&t);
463 if (tm == NULL) {
464 return map_werror_from_unix(errno);
466 str = talloc_asprintf(mem_ctx, "%02d/%02d/%04d",
467 tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
469 if (!str) {
470 return WERR_NOMEM;
473 wvalue.name = value;
474 if (!push_reg_sz(mem_ctx, &blob, str)) {
475 return WERR_NOMEM;
477 status = dcerpc_winreg_SetValue(winreg_handle,
478 mem_ctx,
479 key_handle,
480 wvalue,
481 REG_SZ,
482 blob.data,
483 blob.length,
484 &result);
485 if (!NT_STATUS_IS_OK(status)) {
486 result = ntstatus_to_werror(status);
488 if (!W_ERROR_IS_OK(result)) {
489 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
490 wvalue.name, win_errstr(result)));
493 return result;
496 static WERROR winreg_printer_date_to_NTTIME(const char *str, NTTIME *data)
498 struct tm tm;
499 time_t t;
501 if (strequal(str, "01/01/1601")) {
502 *data = 0;
503 return WERR_OK;
506 ZERO_STRUCT(tm);
508 if (sscanf(str, "%d/%d/%d",
509 &tm.tm_mon, &tm.tm_mday, &tm.tm_year) != 3) {
510 return WERR_INVALID_PARAMETER;
512 tm.tm_mon -= 1;
513 tm.tm_year -= 1900;
514 tm.tm_isdst = -1;
516 t = mktime(&tm);
517 unix_to_nt_time(data, t);
519 return WERR_OK;
522 static WERROR winreg_printer_write_ver(TALLOC_CTX *mem_ctx,
523 struct dcerpc_binding_handle *winreg_handle,
524 struct policy_handle *key_handle,
525 const char *value,
526 uint64_t data)
528 struct winreg_String wvalue = { 0, };
529 DATA_BLOB blob;
530 WERROR result = WERR_OK;
531 NTSTATUS status;
532 char *str;
534 /* FIXME: check format is right,
535 * this needs to be something like: 6.1.7600.16385 */
536 str = talloc_asprintf(mem_ctx, "%u.%u.%u.%u",
537 (unsigned)((data >> 48) & 0xFFFF),
538 (unsigned)((data >> 32) & 0xFFFF),
539 (unsigned)((data >> 16) & 0xFFFF),
540 (unsigned)(data & 0xFFFF));
541 if (!str) {
542 return WERR_NOMEM;
545 wvalue.name = value;
546 if (!push_reg_sz(mem_ctx, &blob, str)) {
547 return WERR_NOMEM;
549 status = dcerpc_winreg_SetValue(winreg_handle,
550 mem_ctx,
551 key_handle,
552 wvalue,
553 REG_SZ,
554 blob.data,
555 blob.length,
556 &result);
557 if (!NT_STATUS_IS_OK(status)) {
558 result = ntstatus_to_werror(status);
560 if (!W_ERROR_IS_OK(result)) {
561 DEBUG(0, ("winreg_printer_write_date: Could not set value %s: %s\n",
562 wvalue.name, win_errstr(result)));
565 return result;
568 static WERROR winreg_printer_ver_to_dword(const char *str, uint64_t *data)
570 unsigned int v1, v2, v3, v4;
572 if (sscanf(str, "%u.%u.%u.%u", &v1, &v2, &v3, &v4) != 4) {
573 return WERR_INVALID_PARAMETER;
576 *data = ((uint64_t)(v1 & 0xFFFF) << 48) +
577 ((uint64_t)(v2 & 0xFFFF) << 32) +
578 ((uint64_t)(v3 & 0xFFFF) << 16) +
579 (uint64_t)(v2 & 0xFFFF);
581 return WERR_OK;
584 /********************************************************************
585 Public winreg function for spoolss
586 ********************************************************************/
588 WERROR winreg_create_printer(TALLOC_CTX *mem_ctx,
589 struct dcerpc_binding_handle *winreg_handle,
590 const char *sharename)
592 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
593 struct policy_handle hive_hnd, key_hnd;
594 struct spoolss_SetPrinterInfo2 *info2;
595 struct security_descriptor *secdesc;
596 struct winreg_String wkey, wkeyclass;
597 const char *path;
598 const char *subkeys[] = { SPOOL_DSDRIVER_KEY, SPOOL_DSSPOOLER_KEY, SPOOL_PRINTERDATA_KEY };
599 uint32_t i, count = ARRAY_SIZE(subkeys);
600 uint32_t info2_mask = 0;
601 WERROR result = WERR_OK;
602 WERROR ignore;
603 TALLOC_CTX *tmp_ctx;
605 tmp_ctx = talloc_stackframe();
606 if (tmp_ctx == NULL) {
607 return WERR_NOMEM;
610 path = winreg_printer_data_keyname(tmp_ctx, sharename);
611 if (path == NULL) {
612 TALLOC_FREE(tmp_ctx);
613 return WERR_NOMEM;
616 ZERO_STRUCT(hive_hnd);
617 ZERO_STRUCT(key_hnd);
619 result = winreg_printer_openkey(tmp_ctx,
620 winreg_handle,
621 path,
623 false,
624 access_mask,
625 &hive_hnd,
626 &key_hnd);
627 if (W_ERROR_IS_OK(result)) {
628 DEBUG(2, ("winreg_create_printer: Skipping, %s already exists\n", path));
629 goto done;
630 } else if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
631 DEBUG(2, ("winreg_create_printer: Creating default values in %s\n", path));
632 } else if (!W_ERROR_IS_OK(result)) {
633 DEBUG(0, ("winreg_create_printer: Could not open key %s: %s\n",
634 path, win_errstr(result)));
635 goto done;
638 if (is_valid_policy_hnd(&key_hnd)) {
639 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
641 if (is_valid_policy_hnd(&hive_hnd)) {
642 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
645 /* Create the main key */
646 result = winreg_printer_openkey(tmp_ctx,
647 winreg_handle,
648 path,
650 true,
651 access_mask,
652 &hive_hnd,
653 &key_hnd);
654 if (!W_ERROR_IS_OK(result)) {
655 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
656 path, win_errstr(result)));
657 goto done;
660 if (is_valid_policy_hnd(&key_hnd)) {
661 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
664 /* Create subkeys */
665 for (i = 0; i < count; i++) {
666 NTSTATUS status;
667 enum winreg_CreateAction action = REG_ACTION_NONE;
669 ZERO_STRUCT(key_hnd);
670 ZERO_STRUCT(wkey);
672 wkey.name = talloc_asprintf(tmp_ctx, "%s\\%s", path, subkeys[i]);
673 if (wkey.name == NULL) {
674 result = WERR_NOMEM;
675 goto done;
678 ZERO_STRUCT(wkeyclass);
679 wkeyclass.name = "";
681 status = dcerpc_winreg_CreateKey(winreg_handle,
682 tmp_ctx,
683 &hive_hnd,
684 wkey,
685 wkeyclass,
687 access_mask,
688 NULL,
689 &key_hnd,
690 &action,
691 &result);
692 if (!NT_STATUS_IS_OK(status)) {
693 result = ntstatus_to_werror(status);
695 if (!W_ERROR_IS_OK(result)) {
696 DEBUG(0, ("winreg_create_printer_keys: Could not create key %s: %s\n",
697 wkey.name, win_errstr(result)));
698 goto done;
701 if (strequal(subkeys[i], SPOOL_DSSPOOLER_KEY)) {
702 const char *dnssuffix;
703 const char *longname;
704 const char *uncname;
706 status = dcerpc_winreg_set_sz(tmp_ctx,
707 winreg_handle,
708 &key_hnd,
709 SPOOL_REG_PRINTERNAME,
710 sharename,
711 &result);
712 if (!NT_STATUS_IS_OK(status)) {
713 result = ntstatus_to_werror(status);
715 if (!W_ERROR_IS_OK(result)) {
716 goto done;
719 status = dcerpc_winreg_set_sz(tmp_ctx,
720 winreg_handle,
721 &key_hnd,
722 SPOOL_REG_SHORTSERVERNAME,
723 lp_netbios_name(),
724 &result);
725 if (!NT_STATUS_IS_OK(status)) {
726 result = ntstatus_to_werror(status);
728 if (!W_ERROR_IS_OK(result)) {
729 goto done;
732 /* We make the assumption that the netbios name
733 * is the same as the DNS name since the former
734 * will be what we used to join the domain
736 dnssuffix = get_mydnsdomname(tmp_ctx);
737 if (dnssuffix != NULL && dnssuffix[0] != '\0') {
738 longname = talloc_asprintf(tmp_ctx, "%s.%s", lp_netbios_name(), dnssuffix);
739 } else {
740 longname = talloc_strdup(tmp_ctx, lp_netbios_name());
742 if (longname == NULL) {
743 result = WERR_NOMEM;
744 goto done;
747 status = dcerpc_winreg_set_sz(tmp_ctx,
748 winreg_handle,
749 &key_hnd,
750 SPOOL_REG_SERVERNAME,
751 longname,
752 &result);
753 if (!NT_STATUS_IS_OK(status)) {
754 result = ntstatus_to_werror(status);
756 if (!W_ERROR_IS_OK(result)) {
757 goto done;
760 uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
761 longname, sharename);
762 if (uncname == NULL) {
763 result = WERR_NOMEM;
764 goto done;
767 status = dcerpc_winreg_set_sz(tmp_ctx,
768 winreg_handle,
769 &key_hnd,
770 SPOOL_REG_UNCNAME,
771 uncname,
772 &result);
773 if (!NT_STATUS_IS_OK(status)) {
774 result = ntstatus_to_werror(status);
776 if (!W_ERROR_IS_OK(result)) {
777 goto done;
780 status = dcerpc_winreg_set_dword(tmp_ctx,
781 winreg_handle,
782 &key_hnd,
783 SPOOL_REG_VERSIONNUMBER,
785 &result);
786 if (!NT_STATUS_IS_OK(status)) {
787 result = ntstatus_to_werror(status);
789 if (!W_ERROR_IS_OK(result)) {
790 goto done;
793 status = dcerpc_winreg_set_dword(tmp_ctx,
794 winreg_handle,
795 &key_hnd,
796 SPOOL_REG_PRINTSTARTTIME,
798 &result);
799 if (!NT_STATUS_IS_OK(status)) {
800 result = ntstatus_to_werror(status);
802 if (!W_ERROR_IS_OK(result)) {
803 goto done;
806 status = dcerpc_winreg_set_dword(tmp_ctx,
807 winreg_handle,
808 &key_hnd,
809 SPOOL_REG_PRINTENDTIME,
811 &result);
812 if (!NT_STATUS_IS_OK(status)) {
813 result = ntstatus_to_werror(status);
815 if (!W_ERROR_IS_OK(result)) {
816 goto done;
819 status = dcerpc_winreg_set_dword(tmp_ctx,
820 winreg_handle,
821 &key_hnd,
822 SPOOL_REG_PRIORITY,
824 &result);
825 if (!NT_STATUS_IS_OK(status)) {
826 result = ntstatus_to_werror(status);
828 if (!W_ERROR_IS_OK(result)) {
829 goto done;
832 status = dcerpc_winreg_set_dword(tmp_ctx,
833 winreg_handle,
834 &key_hnd,
835 SPOOL_REG_PRINTKEEPPRINTEDJOBS,
837 &result);
838 if (!NT_STATUS_IS_OK(status)) {
839 result = ntstatus_to_werror(status);
841 if (!W_ERROR_IS_OK(result)) {
842 goto done;
846 if (is_valid_policy_hnd(&key_hnd)) {
847 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
850 info2 = talloc_zero(tmp_ctx, struct spoolss_SetPrinterInfo2);
851 if (info2 == NULL) {
852 result = WERR_NOMEM;
853 goto done;
856 info2->printername = sharename;
857 if (info2->printername == NULL) {
858 result = WERR_NOMEM;
859 goto done;
861 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTERNAME;
863 info2->sharename = sharename;
864 info2_mask |= SPOOLSS_PRINTER_INFO_SHARENAME;
866 info2->portname = SAMBA_PRINTER_PORT_NAME;
867 info2_mask |= SPOOLSS_PRINTER_INFO_PORTNAME;
869 info2->printprocessor = "winprint";
870 info2_mask |= SPOOLSS_PRINTER_INFO_PRINTPROCESSOR;
872 info2->datatype = "RAW";
873 info2_mask |= SPOOLSS_PRINTER_INFO_DATATYPE;
875 info2->comment = "";
876 info2_mask |= SPOOLSS_PRINTER_INFO_COMMENT;
878 info2->attributes = PRINTER_ATTRIBUTE_SAMBA;
879 info2_mask |= SPOOLSS_PRINTER_INFO_ATTRIBUTES;
881 info2->starttime = 0; /* Minutes since 12:00am GMT */
882 info2_mask |= SPOOLSS_PRINTER_INFO_STARTTIME;
884 info2->untiltime = 0; /* Minutes since 12:00am GMT */
885 info2_mask |= SPOOLSS_PRINTER_INFO_UNTILTIME;
887 info2->priority = 1;
888 info2_mask |= SPOOLSS_PRINTER_INFO_PRIORITY;
890 info2->defaultpriority = 1;
891 info2_mask |= SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY;
893 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
894 if (!W_ERROR_IS_OK(result)) {
895 goto done;
897 info2_mask |= SPOOLSS_PRINTER_INFO_SECDESC;
900 * Don't write a default Device Mode to the registry! The Device Mode is
901 * only written to disk with a SetPrinter level 2 or 8.
904 result = winreg_update_printer(tmp_ctx,
905 winreg_handle,
906 sharename,
907 info2_mask,
908 info2,
909 NULL,
910 secdesc);
912 done:
913 if (is_valid_policy_hnd(&key_hnd)) {
914 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
916 if (is_valid_policy_hnd(&hive_hnd)) {
917 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
920 talloc_free(tmp_ctx);
921 return result;
924 WERROR winreg_update_printer(TALLOC_CTX *mem_ctx,
925 struct dcerpc_binding_handle *winreg_handle,
926 const char *sharename,
927 uint32_t info2_mask,
928 struct spoolss_SetPrinterInfo2 *info2,
929 struct spoolss_DeviceMode *devmode,
930 struct security_descriptor *secdesc)
932 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
933 struct policy_handle hive_hnd, key_hnd;
934 int snum = lp_servicenumber(sharename);
935 enum ndr_err_code ndr_err;
936 DATA_BLOB blob;
937 char *path;
938 WERROR result = WERR_OK;
939 WERROR ignore;
940 NTSTATUS status;
941 TALLOC_CTX *tmp_ctx;
943 tmp_ctx = talloc_stackframe();
944 if (tmp_ctx == NULL) {
945 return WERR_NOMEM;
948 path = winreg_printer_data_keyname(tmp_ctx, sharename);
949 if (path == NULL) {
950 TALLOC_FREE(tmp_ctx);
951 return WERR_NOMEM;
954 ZERO_STRUCT(hive_hnd);
955 ZERO_STRUCT(key_hnd);
957 result = winreg_printer_openkey(tmp_ctx,
958 winreg_handle,
959 path,
961 true,
962 access_mask,
963 &hive_hnd,
964 &key_hnd);
965 if (!W_ERROR_IS_OK(result)) {
966 DEBUG(0, ("winreg_update_printer: Could not open key %s: %s\n",
967 path, win_errstr(result)));
968 goto done;
971 if (info2_mask & SPOOLSS_PRINTER_INFO_ATTRIBUTES) {
972 status = dcerpc_winreg_set_dword(tmp_ctx,
973 winreg_handle,
974 &key_hnd,
975 "Attributes",
976 info2->attributes,
977 &result);
978 if (!NT_STATUS_IS_OK(status)) {
979 result = ntstatus_to_werror(status);
981 if (!W_ERROR_IS_OK(result)) {
982 goto done;
986 #if 0
987 if (info2_mask & SPOOLSS_PRINTER_INFO_AVERAGEPPM) {
988 status = dcerpc_winreg_set_dword(tmp_ctx,
989 winreg_handle,
990 &key_hnd,
991 "AveragePpm",
992 info2->attributes,
993 &result);
994 if (!NT_STATUS_IS_OK(status)) {
995 result = ntstatus_to_werror(status);
997 if (!W_ERROR_IS_OK(result)) {
998 goto done;
1001 #endif
1003 if (info2_mask & SPOOLSS_PRINTER_INFO_COMMENT) {
1004 status = dcerpc_winreg_set_sz(tmp_ctx,
1005 winreg_handle,
1006 &key_hnd,
1007 "Description",
1008 info2->comment,
1009 &result);
1010 if (!NT_STATUS_IS_OK(status)) {
1011 result = ntstatus_to_werror(status);
1013 if (!W_ERROR_IS_OK(result)) {
1014 goto done;
1018 if (info2_mask & SPOOLSS_PRINTER_INFO_DATATYPE) {
1019 status = dcerpc_winreg_set_sz(tmp_ctx,
1020 winreg_handle,
1021 &key_hnd,
1022 "Datatype",
1023 info2->datatype,
1024 &result);
1025 if (!NT_STATUS_IS_OK(status)) {
1026 result = ntstatus_to_werror(status);
1028 if (!W_ERROR_IS_OK(result)) {
1029 goto done;
1033 if (info2_mask & SPOOLSS_PRINTER_INFO_DEFAULTPRIORITY) {
1034 status = dcerpc_winreg_set_dword(tmp_ctx,
1035 winreg_handle,
1036 &key_hnd,
1037 "Default Priority",
1038 info2->defaultpriority,
1039 &result);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 result = ntstatus_to_werror(status);
1043 if (!W_ERROR_IS_OK(result)) {
1044 goto done;
1048 if (info2_mask & SPOOLSS_PRINTER_INFO_DEVMODE) {
1050 * Some client drivers freak out if there is a NULL devmode
1051 * (probably the driver is not checking before accessing
1052 * the devmode pointer) --jerry
1054 if (devmode == NULL && lp_default_devmode(snum) && info2 != NULL) {
1055 result = spoolss_create_default_devmode(tmp_ctx,
1056 info2->printername,
1057 &devmode);
1058 if (!W_ERROR_IS_OK(result)) {
1059 goto done;
1063 if (devmode->size != (ndr_size_spoolss_DeviceMode(devmode, 0) - devmode->__driverextra_length)) {
1064 result = WERR_INVALID_PARAM;
1065 goto done;
1068 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, devmode,
1069 (ndr_push_flags_fn_t) ndr_push_spoolss_DeviceMode);
1070 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1071 DEBUG(0, ("winreg_update_printer: Failed to marshall device mode\n"));
1072 result = WERR_NOMEM;
1073 goto done;
1076 status = dcerpc_winreg_set_binary(tmp_ctx,
1077 winreg_handle,
1078 &key_hnd,
1079 "Default DevMode",
1080 &blob,
1081 &result);
1082 if (!NT_STATUS_IS_OK(status)) {
1083 result = ntstatus_to_werror(status);
1085 if (!W_ERROR_IS_OK(result)) {
1086 goto done;
1090 if (info2_mask & SPOOLSS_PRINTER_INFO_DRIVERNAME) {
1091 status = dcerpc_winreg_set_sz(tmp_ctx,
1092 winreg_handle,
1093 &key_hnd,
1094 "Printer Driver",
1095 info2->drivername,
1096 &result);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 result = ntstatus_to_werror(status);
1100 if (!W_ERROR_IS_OK(result)) {
1101 goto done;
1105 if (info2_mask & SPOOLSS_PRINTER_INFO_LOCATION) {
1106 status = dcerpc_winreg_set_sz(tmp_ctx,
1107 winreg_handle,
1108 &key_hnd,
1109 "Location",
1110 info2->location,
1111 &result);
1112 if (!NT_STATUS_IS_OK(status)) {
1113 result = ntstatus_to_werror(status);
1115 if (!W_ERROR_IS_OK(result)) {
1116 goto done;
1120 if (info2_mask & SPOOLSS_PRINTER_INFO_PARAMETERS) {
1121 status = dcerpc_winreg_set_sz(tmp_ctx,
1122 winreg_handle,
1123 &key_hnd,
1124 "Parameters",
1125 info2->parameters,
1126 &result);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 result = ntstatus_to_werror(status);
1130 if (!W_ERROR_IS_OK(result)) {
1131 goto done;
1135 if (info2_mask & SPOOLSS_PRINTER_INFO_PORTNAME) {
1136 status = dcerpc_winreg_set_sz(tmp_ctx,
1137 winreg_handle,
1138 &key_hnd,
1139 "Port",
1140 info2->portname,
1141 &result);
1142 if (!NT_STATUS_IS_OK(status)) {
1143 result = ntstatus_to_werror(status);
1145 if (!W_ERROR_IS_OK(result)) {
1146 goto done;
1150 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTERNAME) {
1152 * in addprinter: no servername and the printer is the name
1153 * in setprinter: servername is \\server
1154 * and printer is \\server\\printer
1156 * Samba manages only local printers.
1157 * we currently don't support things like i
1158 * path=\\other_server\printer
1160 * We only store the printername, not \\server\printername
1162 const char *p = strrchr(info2->printername, '\\');
1163 if (p == NULL) {
1164 p = info2->printername;
1165 } else {
1166 p++;
1168 status = dcerpc_winreg_set_sz(tmp_ctx,
1169 winreg_handle,
1170 &key_hnd,
1171 "Name",
1173 &result);
1174 if (!NT_STATUS_IS_OK(status)) {
1175 result = ntstatus_to_werror(status);
1177 if (!W_ERROR_IS_OK(result)) {
1178 goto done;
1182 if (info2_mask & SPOOLSS_PRINTER_INFO_PRINTPROCESSOR) {
1183 status = dcerpc_winreg_set_sz(tmp_ctx,
1184 winreg_handle,
1185 &key_hnd,
1186 "Print Processor",
1187 info2->printprocessor,
1188 &result);
1189 if (!NT_STATUS_IS_OK(status)) {
1190 result = ntstatus_to_werror(status);
1192 if (!W_ERROR_IS_OK(result)) {
1193 goto done;
1197 if (info2_mask & SPOOLSS_PRINTER_INFO_PRIORITY) {
1198 status = dcerpc_winreg_set_dword(tmp_ctx,
1199 winreg_handle,
1200 &key_hnd,
1201 "Priority",
1202 info2->priority,
1203 &result);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 result = ntstatus_to_werror(status);
1207 if (!W_ERROR_IS_OK(result)) {
1208 goto done;
1212 if (info2_mask & SPOOLSS_PRINTER_INFO_SECDESC) {
1214 * We need a security descriptor, if it isn't specified by
1215 * AddPrinter{Ex} then create a default descriptor.
1217 if (secdesc == NULL) {
1218 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1219 if (!W_ERROR_IS_OK(result)) {
1220 goto done;
1223 result = winreg_set_printer_secdesc(tmp_ctx,
1224 winreg_handle,
1225 sharename,
1226 secdesc);
1227 if (!W_ERROR_IS_OK(result)) {
1228 goto done;
1232 if (info2_mask & SPOOLSS_PRINTER_INFO_SEPFILE) {
1233 status = dcerpc_winreg_set_sz(tmp_ctx,
1234 winreg_handle,
1235 &key_hnd,
1236 "Separator File",
1237 info2->sepfile,
1238 &result);
1239 if (!NT_STATUS_IS_OK(status)) {
1240 result = ntstatus_to_werror(status);
1242 if (!W_ERROR_IS_OK(result)) {
1243 goto done;
1247 if (info2_mask & SPOOLSS_PRINTER_INFO_SHARENAME) {
1248 status = dcerpc_winreg_set_sz(tmp_ctx,
1249 winreg_handle,
1250 &key_hnd,
1251 "Share Name",
1252 info2->sharename,
1253 &result);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 result = ntstatus_to_werror(status);
1257 if (!W_ERROR_IS_OK(result)) {
1258 goto done;
1262 if (info2_mask & SPOOLSS_PRINTER_INFO_STARTTIME) {
1263 status = dcerpc_winreg_set_dword(tmp_ctx,
1264 winreg_handle,
1265 &key_hnd,
1266 "StartTime",
1267 info2->starttime,
1268 &result);
1269 if (!NT_STATUS_IS_OK(status)) {
1270 result = ntstatus_to_werror(status);
1272 if (!W_ERROR_IS_OK(result)) {
1273 goto done;
1277 if (info2_mask & SPOOLSS_PRINTER_INFO_STATUS) {
1278 status = dcerpc_winreg_set_dword(tmp_ctx,
1279 winreg_handle,
1280 &key_hnd,
1281 "Status",
1282 info2->status,
1283 &result);
1284 if (!NT_STATUS_IS_OK(status)) {
1285 result = ntstatus_to_werror(status);
1287 if (!W_ERROR_IS_OK(result)) {
1288 goto done;
1292 if (info2_mask & SPOOLSS_PRINTER_INFO_UNTILTIME) {
1293 status = dcerpc_winreg_set_dword(tmp_ctx,
1294 winreg_handle,
1295 &key_hnd,
1296 "UntilTime",
1297 info2->untiltime,
1298 &result);
1299 if (!NT_STATUS_IS_OK(status)) {
1300 result = ntstatus_to_werror(status);
1302 if (!W_ERROR_IS_OK(result)) {
1303 goto done;
1307 status = dcerpc_winreg_set_dword(tmp_ctx,
1308 winreg_handle,
1309 &key_hnd,
1310 "ChangeID",
1311 winreg_printer_rev_changeid(),
1312 &result);
1313 if (!NT_STATUS_IS_OK(status)) {
1314 result = ntstatus_to_werror(status);
1316 if (!W_ERROR_IS_OK(result)) {
1317 goto done;
1320 result = WERR_OK;
1321 done:
1322 if (is_valid_policy_hnd(&key_hnd)) {
1323 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1325 if (is_valid_policy_hnd(&hive_hnd)) {
1326 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1329 TALLOC_FREE(tmp_ctx);
1330 return result;
1333 WERROR winreg_get_printer(TALLOC_CTX *mem_ctx,
1334 struct dcerpc_binding_handle *winreg_handle,
1335 const char *printer,
1336 struct spoolss_PrinterInfo2 **pinfo2)
1338 struct spoolss_PrinterInfo2 *info2;
1339 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1340 struct policy_handle hive_hnd, key_hnd;
1341 struct spoolss_PrinterEnumValues enum_value;
1342 struct spoolss_PrinterEnumValues *v = NULL;
1343 enum ndr_err_code ndr_err;
1344 DATA_BLOB blob;
1345 int snum = lp_servicenumber(printer);
1346 uint32_t num_values = 0;
1347 uint32_t i;
1348 char *path;
1349 NTSTATUS status;
1350 WERROR result = WERR_OK;
1351 const char **enum_names = NULL;
1352 enum winreg_Type *enum_types = NULL;
1353 DATA_BLOB *enum_data_blobs = NULL;
1354 TALLOC_CTX *tmp_ctx;
1356 tmp_ctx = talloc_stackframe();
1357 if (tmp_ctx == NULL) {
1358 return WERR_NOMEM;
1361 path = winreg_printer_data_keyname(tmp_ctx, printer);
1362 if (path == NULL) {
1363 TALLOC_FREE(tmp_ctx);
1364 return WERR_NOMEM;
1367 result = winreg_printer_openkey(tmp_ctx,
1368 winreg_handle,
1369 path,
1371 false,
1372 access_mask,
1373 &hive_hnd,
1374 &key_hnd);
1375 if (!W_ERROR_IS_OK(result)) {
1376 DEBUG(2, ("winreg_get_printer: Could not open key %s: %s\n",
1377 path, win_errstr(result)));
1378 goto done;
1381 status = dcerpc_winreg_enumvals(tmp_ctx,
1382 winreg_handle,
1383 &key_hnd,
1384 &num_values,
1385 &enum_names,
1386 &enum_types,
1387 &enum_data_blobs,
1388 &result);
1389 if (!NT_STATUS_IS_OK(status)){
1390 result = ntstatus_to_werror(status);
1393 if (!W_ERROR_IS_OK(result)) {
1394 DEBUG(0, ("winreg_get_printer: Could not enumerate values in %s: %s\n",
1395 path, win_errstr(result)));
1396 goto done;
1399 info2 = talloc_zero(tmp_ctx, struct spoolss_PrinterInfo2);
1400 if (info2 == NULL) {
1401 result = WERR_NOMEM;
1402 goto done;
1405 FILL_STRING(info2, EMPTY_STRING, info2->servername);
1406 FILL_STRING(info2, EMPTY_STRING, info2->printername);
1407 FILL_STRING(info2, EMPTY_STRING, info2->sharename);
1408 FILL_STRING(info2, EMPTY_STRING, info2->portname);
1409 FILL_STRING(info2, EMPTY_STRING, info2->drivername);
1410 FILL_STRING(info2, EMPTY_STRING, info2->comment);
1411 FILL_STRING(info2, EMPTY_STRING, info2->location);
1412 FILL_STRING(info2, EMPTY_STRING, info2->sepfile);
1413 FILL_STRING(info2, EMPTY_STRING, info2->printprocessor);
1414 FILL_STRING(info2, EMPTY_STRING, info2->datatype);
1415 FILL_STRING(info2, EMPTY_STRING, info2->parameters);
1417 for (i = 0; i < num_values; i++) {
1418 enum_value.value_name = enum_names[i];
1419 enum_value.value_name_len = 2*strlen_m_term(enum_names[i]);
1420 enum_value.type = enum_types[i];
1421 enum_value.data_length = enum_data_blobs[i].length;
1422 enum_value.data = NULL;
1423 if (enum_value.data_length != 0){
1424 enum_value.data = &enum_data_blobs[i];
1426 v = &enum_value;
1428 result = winreg_enumval_to_sz(info2,
1430 "Name",
1431 &info2->printername);
1432 CHECK_ERROR(result);
1434 result = winreg_enumval_to_sz(info2,
1436 "Share Name",
1437 &info2->sharename);
1438 CHECK_ERROR(result);
1440 result = winreg_enumval_to_sz(info2,
1442 "Port",
1443 &info2->portname);
1444 CHECK_ERROR(result);
1446 result = winreg_enumval_to_sz(info2,
1448 "Description",
1449 &info2->comment);
1450 CHECK_ERROR(result);
1452 result = winreg_enumval_to_sz(info2,
1454 "Location",
1455 &info2->location);
1456 CHECK_ERROR(result);
1458 result = winreg_enumval_to_sz(info2,
1460 "Separator File",
1461 &info2->sepfile);
1462 CHECK_ERROR(result);
1464 result = winreg_enumval_to_sz(info2,
1466 "Print Processor",
1467 &info2->printprocessor);
1468 CHECK_ERROR(result);
1470 result = winreg_enumval_to_sz(info2,
1472 "Datatype",
1473 &info2->datatype);
1474 CHECK_ERROR(result);
1476 result = winreg_enumval_to_sz(info2,
1478 "Parameters",
1479 &info2->parameters);
1480 CHECK_ERROR(result);
1482 result = winreg_enumval_to_sz(info2,
1484 "Printer Driver",
1485 &info2->drivername);
1486 CHECK_ERROR(result);
1488 result = winreg_enumval_to_dword(info2,
1490 "Attributes",
1491 &info2->attributes);
1492 CHECK_ERROR(result);
1494 result = winreg_enumval_to_dword(info2,
1496 "Priority",
1497 &info2->priority);
1498 CHECK_ERROR(result);
1500 result = winreg_enumval_to_dword(info2,
1502 "Default Priority",
1503 &info2->defaultpriority);
1504 CHECK_ERROR(result);
1506 result = winreg_enumval_to_dword(info2,
1508 "StartTime",
1509 &info2->starttime);
1510 CHECK_ERROR(result);
1512 result = winreg_enumval_to_dword(info2,
1514 "UntilTime",
1515 &info2->untiltime);
1516 CHECK_ERROR(result);
1518 result = winreg_enumval_to_dword(info2,
1520 "Status",
1521 &info2->status);
1522 CHECK_ERROR(result);
1524 result = winreg_enumval_to_dword(info2,
1526 "StartTime",
1527 &info2->starttime);
1528 CHECK_ERROR(result);
1531 if (!W_ERROR_IS_OK(result)) {
1532 DEBUG(0, ("winreg_get_printer: winreg_enumval_to_TYPE() failed "
1533 "for %s: %s\n",
1534 v->value_name,
1535 win_errstr(result)));
1536 goto done;
1539 /* Construct the Device Mode */
1540 status = dcerpc_winreg_query_binary(tmp_ctx,
1541 winreg_handle,
1542 &key_hnd,
1543 "Default DevMode",
1544 &blob,
1545 &result);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 result = ntstatus_to_werror(status);
1549 if (W_ERROR_IS_OK(result)) {
1550 info2->devmode = talloc_zero(info2, struct spoolss_DeviceMode);
1551 if (info2->devmode == NULL) {
1552 result = WERR_NOMEM;
1553 goto done;
1555 ndr_err = ndr_pull_struct_blob(&blob,
1556 info2->devmode,
1557 info2->devmode,
1558 (ndr_pull_flags_fn_t) ndr_pull_spoolss_DeviceMode);
1559 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1560 DEBUG(0, ("winreg_get_printer: Failed to unmarshall device mode\n"));
1561 result = WERR_NOMEM;
1562 goto done;
1566 if (info2->devmode == NULL && lp_default_devmode(snum)) {
1567 result = spoolss_create_default_devmode(info2,
1568 info2->printername,
1569 &info2->devmode);
1570 if (!W_ERROR_IS_OK(result)) {
1571 goto done;
1575 if (info2->devmode) {
1576 info2->devmode->size = ndr_size_spoolss_DeviceMode(info2->devmode, 0) - info2->devmode->driverextra_data.length;
1579 result = winreg_get_printer_secdesc(info2,
1580 winreg_handle,
1581 printer,
1582 &info2->secdesc);
1583 if (!W_ERROR_IS_OK(result)) {
1584 goto done;
1587 /* Fix for OS/2 drivers. */
1588 if (get_remote_arch() == RA_OS2) {
1589 spoolss_map_to_os2_driver(info2, &info2->drivername);
1592 if (pinfo2) {
1593 *pinfo2 = talloc_move(mem_ctx, &info2);
1596 result = WERR_OK;
1597 done:
1598 if (winreg_handle != NULL) {
1599 WERROR ignore;
1601 if (is_valid_policy_hnd(&key_hnd)) {
1602 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1604 if (is_valid_policy_hnd(&hive_hnd)) {
1605 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1609 TALLOC_FREE(tmp_ctx);
1610 return result;
1613 WERROR winreg_get_printer_secdesc(TALLOC_CTX *mem_ctx,
1614 struct dcerpc_binding_handle *winreg_handle,
1615 const char *sharename,
1616 struct spoolss_security_descriptor **psecdesc)
1618 struct spoolss_security_descriptor *secdesc;
1619 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1620 struct policy_handle hive_hnd, key_hnd;
1621 const char *path;
1622 TALLOC_CTX *tmp_ctx;
1623 NTSTATUS status;
1624 WERROR result;
1626 tmp_ctx = talloc_stackframe();
1627 if (tmp_ctx == NULL) {
1628 return WERR_NOMEM;
1631 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1632 if (path == NULL) {
1633 talloc_free(tmp_ctx);
1634 return WERR_NOMEM;
1637 ZERO_STRUCT(hive_hnd);
1638 ZERO_STRUCT(key_hnd);
1640 result = winreg_printer_openkey(tmp_ctx,
1641 winreg_handle,
1642 path,
1644 false,
1645 access_mask,
1646 &hive_hnd,
1647 &key_hnd);
1648 if (!W_ERROR_IS_OK(result)) {
1649 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1650 goto create_default;
1652 goto done;
1655 status = dcerpc_winreg_query_sd(tmp_ctx,
1656 winreg_handle,
1657 &key_hnd,
1658 "Security",
1659 &secdesc,
1660 &result);
1661 if (!NT_STATUS_IS_OK(status)) {
1662 result = ntstatus_to_werror(status);
1664 if (!W_ERROR_IS_OK(result)) {
1665 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1666 WERROR ignore;
1668 if (is_valid_policy_hnd(&key_hnd)) {
1669 dcerpc_winreg_CloseKey(winreg_handle,
1670 tmp_ctx,
1671 &key_hnd,
1672 &ignore);
1675 if (is_valid_policy_hnd(&hive_hnd)) {
1676 dcerpc_winreg_CloseKey(winreg_handle,
1677 tmp_ctx,
1678 &hive_hnd,
1679 &ignore);
1681 goto create_default;
1683 goto done;
1686 if (psecdesc) {
1687 *psecdesc = talloc_move(mem_ctx, &secdesc);
1690 result = WERR_OK;
1691 goto done;
1693 create_default:
1694 result = winreg_printer_openkey(tmp_ctx,
1695 winreg_handle,
1696 path,
1698 true,
1699 access_mask,
1700 &hive_hnd,
1701 &key_hnd);
1702 if (!W_ERROR_IS_OK(result)) {
1703 goto done;
1706 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1707 if (!W_ERROR_IS_OK(result)) {
1708 goto done;
1711 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
1712 this security descriptor has been created when winbindd was
1713 down. Take ownership of security descriptor. */
1714 if (dom_sid_equal(secdesc->owner_sid, &global_sid_World)) {
1715 struct dom_sid owner_sid;
1717 /* Change sd owner to workgroup administrator */
1719 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
1720 struct spoolss_security_descriptor *new_secdesc;
1721 size_t size;
1723 /* Create new sd */
1724 sid_append_rid(&owner_sid, DOMAIN_RID_ADMINISTRATOR);
1726 new_secdesc = make_sec_desc(tmp_ctx,
1727 secdesc->revision,
1728 secdesc->type,
1729 &owner_sid,
1730 secdesc->group_sid,
1731 secdesc->sacl,
1732 secdesc->dacl,
1733 &size);
1735 if (new_secdesc == NULL) {
1736 result = WERR_NOMEM;
1737 goto done;
1740 /* Swap with other one */
1741 secdesc = new_secdesc;
1745 status = dcerpc_winreg_set_sd(tmp_ctx,
1746 winreg_handle,
1747 &key_hnd,
1748 "Security",
1749 secdesc,
1750 &result);
1751 if (!NT_STATUS_IS_OK(status)) {
1752 result = ntstatus_to_werror(status);
1754 if (!W_ERROR_IS_OK(result)) {
1755 return result;
1758 if (psecdesc) {
1759 *psecdesc = talloc_move(mem_ctx, &secdesc);
1762 result = WERR_OK;
1763 done:
1764 if (winreg_handle != NULL) {
1765 WERROR ignore;
1767 if (is_valid_policy_hnd(&key_hnd)) {
1768 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1770 if (is_valid_policy_hnd(&hive_hnd)) {
1771 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1775 talloc_free(tmp_ctx);
1776 return result;
1779 WERROR winreg_set_printer_secdesc(TALLOC_CTX *mem_ctx,
1780 struct dcerpc_binding_handle *winreg_handle,
1781 const char *sharename,
1782 const struct spoolss_security_descriptor *secdesc)
1784 const struct spoolss_security_descriptor *new_secdesc = secdesc;
1785 struct spoolss_security_descriptor *old_secdesc;
1786 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1787 struct policy_handle hive_hnd, key_hnd;
1788 const char *path;
1789 TALLOC_CTX *tmp_ctx;
1790 NTSTATUS status;
1791 WERROR result;
1792 WERROR ignore;
1794 tmp_ctx = talloc_stackframe();
1795 if (tmp_ctx == NULL) {
1796 return WERR_NOMEM;
1799 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1800 if (path == NULL) {
1801 talloc_free(tmp_ctx);
1802 return WERR_NOMEM;
1806 * The old owner and group sids of the security descriptor are not
1807 * present when new ACEs are added or removed by changing printer
1808 * permissions through NT. If they are NULL in the new security
1809 * descriptor then copy them over from the old one.
1811 if (!secdesc->owner_sid || !secdesc->group_sid) {
1812 struct dom_sid *owner_sid, *group_sid;
1813 struct security_acl *dacl, *sacl;
1814 size_t size;
1816 result = winreg_get_printer_secdesc(tmp_ctx,
1817 winreg_handle,
1818 sharename,
1819 &old_secdesc);
1820 if (!W_ERROR_IS_OK(result)) {
1821 talloc_free(tmp_ctx);
1822 return result;
1825 /* Pick out correct owner and group sids */
1826 owner_sid = secdesc->owner_sid ?
1827 secdesc->owner_sid :
1828 old_secdesc->owner_sid;
1830 group_sid = secdesc->group_sid ?
1831 secdesc->group_sid :
1832 old_secdesc->group_sid;
1834 dacl = secdesc->dacl ?
1835 secdesc->dacl :
1836 old_secdesc->dacl;
1838 sacl = secdesc->sacl ?
1839 secdesc->sacl :
1840 old_secdesc->sacl;
1842 /* Make a deep copy of the security descriptor */
1843 new_secdesc = make_sec_desc(tmp_ctx,
1844 secdesc->revision,
1845 secdesc->type,
1846 owner_sid,
1847 group_sid,
1848 sacl,
1849 dacl,
1850 &size);
1851 if (new_secdesc == NULL) {
1852 talloc_free(tmp_ctx);
1853 return WERR_NOMEM;
1857 ZERO_STRUCT(hive_hnd);
1858 ZERO_STRUCT(key_hnd);
1860 result = winreg_printer_openkey(tmp_ctx,
1861 winreg_handle,
1862 path,
1864 false,
1865 access_mask,
1866 &hive_hnd,
1867 &key_hnd);
1868 if (!W_ERROR_IS_OK(result)) {
1869 goto done;
1872 status = dcerpc_winreg_set_sd(tmp_ctx,
1873 winreg_handle,
1874 &key_hnd,
1875 "Security",
1876 new_secdesc,
1877 &result);
1878 if (!NT_STATUS_IS_OK(status)) {
1879 result = ntstatus_to_werror(status);
1882 done:
1883 if (is_valid_policy_hnd(&key_hnd)) {
1884 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1886 if (is_valid_policy_hnd(&hive_hnd)) {
1887 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1890 talloc_free(tmp_ctx);
1891 return result;
1894 /* Set printer data over the winreg pipe. */
1895 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
1896 struct dcerpc_binding_handle *winreg_handle,
1897 const char *printer,
1898 const char *key,
1899 const char *value,
1900 enum winreg_Type type,
1901 uint8_t *data,
1902 uint32_t data_size)
1904 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1905 struct policy_handle hive_hnd, key_hnd;
1906 struct winreg_String wvalue = { 0, };
1907 char *path;
1908 WERROR result = WERR_OK;
1909 NTSTATUS status;
1910 TALLOC_CTX *tmp_ctx;
1912 tmp_ctx = talloc_stackframe();
1913 if (tmp_ctx == NULL) {
1914 return WERR_NOMEM;
1917 path = winreg_printer_data_keyname(tmp_ctx, printer);
1918 if (path == NULL) {
1919 TALLOC_FREE(tmp_ctx);
1920 return WERR_NOMEM;
1923 ZERO_STRUCT(hive_hnd);
1924 ZERO_STRUCT(key_hnd);
1926 DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
1927 key, value, access_mask, printer));
1928 result = winreg_printer_openkey(tmp_ctx,
1929 winreg_handle,
1930 path,
1931 key,
1932 true,
1933 access_mask,
1934 &hive_hnd,
1935 &key_hnd);
1936 if (!W_ERROR_IS_OK(result)) {
1937 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
1938 key, win_errstr(result)));
1939 goto done;
1942 wvalue.name = value;
1943 status = dcerpc_winreg_SetValue(winreg_handle,
1944 tmp_ctx,
1945 &key_hnd,
1946 wvalue,
1947 type,
1948 data,
1949 data_size,
1950 &result);
1951 if (!NT_STATUS_IS_OK(status)) {
1952 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
1953 value, nt_errstr(status)));
1954 result = ntstatus_to_werror(status);
1957 done:
1958 if (winreg_handle != NULL) {
1959 WERROR ignore;
1961 if (is_valid_policy_hnd(&key_hnd)) {
1962 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1964 if (is_valid_policy_hnd(&hive_hnd)) {
1965 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1969 TALLOC_FREE(tmp_ctx);
1970 return result;
1973 /* Get printer data over a winreg pipe. */
1974 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
1975 struct dcerpc_binding_handle *winreg_handle,
1976 const char *printer,
1977 const char *key,
1978 const char *value,
1979 enum winreg_Type *type,
1980 uint8_t **data,
1981 uint32_t *data_size)
1983 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1984 struct policy_handle hive_hnd, key_hnd;
1985 struct winreg_String wvalue;
1986 enum winreg_Type type_in = REG_NONE;
1987 char *path;
1988 uint8_t *data_in = NULL;
1989 uint32_t data_in_size = 0;
1990 uint32_t value_len = 0;
1991 WERROR result = WERR_OK;
1992 WERROR ignore;
1993 NTSTATUS status;
1994 TALLOC_CTX *tmp_ctx;
1996 tmp_ctx = talloc_stackframe();
1997 if (tmp_ctx == NULL) {
1998 return WERR_NOMEM;
2001 path = winreg_printer_data_keyname(tmp_ctx, printer);
2002 if (path == NULL) {
2003 TALLOC_FREE(tmp_ctx);
2004 return WERR_NOMEM;
2007 ZERO_STRUCT(hive_hnd);
2008 ZERO_STRUCT(key_hnd);
2010 result = winreg_printer_openkey(tmp_ctx,
2011 winreg_handle,
2012 path,
2013 key,
2014 false,
2015 access_mask,
2016 &hive_hnd,
2017 &key_hnd);
2018 if (!W_ERROR_IS_OK(result)) {
2019 DEBUG(2, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
2020 key, win_errstr(result)));
2021 goto done;
2024 wvalue.name = value;
2027 * call QueryValue once with data == NULL to get the
2028 * needed memory size to be allocated, then allocate
2029 * data buffer and call again.
2031 status = dcerpc_winreg_QueryValue(winreg_handle,
2032 tmp_ctx,
2033 &key_hnd,
2034 &wvalue,
2035 &type_in,
2036 NULL,
2037 &data_in_size,
2038 &value_len,
2039 &result);
2040 if (!NT_STATUS_IS_OK(status)) {
2041 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2042 value, nt_errstr(status)));
2043 result = ntstatus_to_werror(status);
2044 goto done;
2046 if (!W_ERROR_IS_OK(result)) {
2047 DEBUG(2, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2048 value, win_errstr(result)));
2049 goto done;
2052 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2053 if (data_in == NULL) {
2054 result = WERR_NOMEM;
2055 goto done;
2057 value_len = 0;
2059 status = dcerpc_winreg_QueryValue(winreg_handle,
2060 tmp_ctx,
2061 &key_hnd,
2062 &wvalue,
2063 &type_in,
2064 data_in,
2065 &data_in_size,
2066 &value_len,
2067 &result);
2068 if (!NT_STATUS_IS_OK(status)) {
2069 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2070 value, nt_errstr(status)));
2071 result = ntstatus_to_werror(status);
2072 goto done;
2074 if (!W_ERROR_IS_OK(result)) {
2075 DEBUG(2, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2076 value, win_errstr(result)));
2077 goto done;
2080 *type = type_in;
2081 *data_size = data_in_size;
2082 if (data_in_size) {
2083 *data = talloc_move(mem_ctx, &data_in);
2086 result = WERR_OK;
2087 done:
2088 if (is_valid_policy_hnd(&key_hnd)) {
2089 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2091 if (is_valid_policy_hnd(&hive_hnd)) {
2092 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2095 TALLOC_FREE(tmp_ctx);
2096 return result;
2099 /* Enumerate on the values of a given key and provide the data. */
2100 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
2101 struct dcerpc_binding_handle *winreg_handle,
2102 const char *printer,
2103 const char *key,
2104 uint32_t *pnum_values,
2105 struct spoolss_PrinterEnumValues **penum_values)
2107 uint32_t i;
2108 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2109 struct policy_handle hive_hnd, key_hnd;
2111 struct spoolss_PrinterEnumValues *enum_values = NULL;
2112 uint32_t num_values = 0;
2113 char *path;
2114 WERROR result = WERR_OK;
2115 WERROR ignore;
2116 NTSTATUS status;
2117 const char **enum_names = NULL;
2118 enum winreg_Type *enum_types = NULL;
2119 DATA_BLOB *enum_data_blobs = NULL;
2121 TALLOC_CTX *tmp_ctx;
2123 tmp_ctx = talloc_stackframe();
2124 if (tmp_ctx == NULL) {
2125 return WERR_NOMEM;
2128 path = winreg_printer_data_keyname(tmp_ctx, printer);
2129 if (path == NULL) {
2130 TALLOC_FREE(tmp_ctx);
2131 return WERR_NOMEM;
2134 result = winreg_printer_openkey(tmp_ctx,
2135 winreg_handle,
2136 path,
2137 key,
2138 false,
2139 access_mask,
2140 &hive_hnd,
2141 &key_hnd);
2142 if (!W_ERROR_IS_OK(result)) {
2143 DEBUG(2, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
2144 key, win_errstr(result)));
2145 goto done;
2148 status = dcerpc_winreg_enumvals(tmp_ctx,
2149 winreg_handle,
2150 &key_hnd,
2151 &num_values,
2152 &enum_names,
2153 &enum_types,
2154 &enum_data_blobs,
2155 &result);
2156 if (!NT_STATUS_IS_OK(status)){
2157 result = ntstatus_to_werror(status);
2160 if (!W_ERROR_IS_OK(result)) {
2161 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2162 key, win_errstr(result)));
2163 goto done;
2166 enum_values = talloc_array(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
2167 if (enum_values == NULL){
2168 result = WERR_NOMEM;
2169 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2170 key, win_errstr(result)));
2171 goto done;
2174 for (i = 0; i < num_values; i++){
2175 enum_values[i].value_name = enum_names[i];
2176 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
2177 enum_values[i].type = enum_types[i];
2178 enum_values[i].data_length = enum_data_blobs[i].length;
2179 enum_values[i].data = NULL;
2181 if (enum_values[i].data_length != 0){
2182 enum_values[i].data = &enum_data_blobs[i];
2186 talloc_steal(enum_values, enum_names);
2187 talloc_steal(enum_values, enum_data_blobs);
2189 *pnum_values = num_values;
2190 if (penum_values) {
2191 *penum_values = talloc_move(mem_ctx, &enum_values);
2194 result = WERR_OK;
2195 done:
2196 if (is_valid_policy_hnd(&key_hnd)) {
2197 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2199 if (is_valid_policy_hnd(&hive_hnd)) {
2200 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2203 TALLOC_FREE(tmp_ctx);
2204 return result;
2207 /* Delete printer data over a winreg pipe. */
2208 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
2209 struct dcerpc_binding_handle *winreg_handle,
2210 const char *printer,
2211 const char *key,
2212 const char *value)
2214 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2215 struct policy_handle hive_hnd, key_hnd;
2216 struct winreg_String wvalue = { 0, };
2217 char *path;
2218 WERROR result = WERR_OK;
2219 WERROR ignore;
2220 NTSTATUS status;
2222 TALLOC_CTX *tmp_ctx;
2224 tmp_ctx = talloc_stackframe();
2225 if (tmp_ctx == NULL) {
2226 return WERR_NOMEM;
2229 path = winreg_printer_data_keyname(tmp_ctx, printer);
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 winreg_handle,
2240 path,
2241 key,
2242 false,
2243 access_mask,
2244 &hive_hnd,
2245 &key_hnd);
2246 if (!W_ERROR_IS_OK(result)) {
2247 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
2248 key, win_errstr(result)));
2249 goto done;
2252 wvalue.name = value;
2253 status = dcerpc_winreg_DeleteValue(winreg_handle,
2254 tmp_ctx,
2255 &key_hnd,
2256 wvalue,
2257 &result);
2258 if (!NT_STATUS_IS_OK(status)) {
2259 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
2260 value, nt_errstr(status)));
2261 result = ntstatus_to_werror(status);
2264 done:
2265 if (is_valid_policy_hnd(&key_hnd)) {
2266 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2268 if (is_valid_policy_hnd(&hive_hnd)) {
2269 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2272 TALLOC_FREE(tmp_ctx);
2273 return result;
2276 /* Enumerate on the subkeys of a given key and provide the data. */
2277 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
2278 struct dcerpc_binding_handle *winreg_handle,
2279 const char *printer,
2280 const char *key,
2281 uint32_t *pnum_subkeys,
2282 const char ***psubkeys)
2284 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2285 struct policy_handle hive_hnd, key_hnd;
2286 char *path;
2287 const char **subkeys = NULL;
2288 uint32_t num_subkeys = -1;
2290 WERROR result = WERR_OK;
2291 WERROR ignore;
2292 NTSTATUS status;
2294 TALLOC_CTX *tmp_ctx;
2296 tmp_ctx = talloc_stackframe();
2297 if (tmp_ctx == NULL) {
2298 return WERR_NOMEM;
2301 path = winreg_printer_data_keyname(tmp_ctx, printer);
2302 if (path == NULL) {
2303 TALLOC_FREE(tmp_ctx);
2304 return WERR_NOMEM;
2307 ZERO_STRUCT(hive_hnd);
2308 ZERO_STRUCT(key_hnd);
2310 result = winreg_printer_openkey(tmp_ctx,
2311 winreg_handle,
2312 path,
2313 key,
2314 false,
2315 access_mask,
2316 &hive_hnd,
2317 &key_hnd);
2318 if (!W_ERROR_IS_OK(result)) {
2319 DEBUG(2, ("winreg_enum_printer_key: Could not open key %s: %s\n",
2320 key, win_errstr(result)));
2321 goto done;
2324 status = dcerpc_winreg_enum_keys(tmp_ctx,
2325 winreg_handle,
2326 &key_hnd,
2327 &num_subkeys,
2328 &subkeys,
2329 &result);
2330 if (!NT_STATUS_IS_OK(status)) {
2331 result = ntstatus_to_werror(status);
2333 if (!W_ERROR_IS_OK(result)) {
2334 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
2335 key, win_errstr(result)));
2336 goto done;
2339 *pnum_subkeys = num_subkeys;
2340 if (psubkeys) {
2341 *psubkeys = talloc_move(mem_ctx, &subkeys);
2344 result = WERR_OK;
2345 done:
2346 if (is_valid_policy_hnd(&key_hnd)) {
2347 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2349 if (is_valid_policy_hnd(&hive_hnd)) {
2350 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2353 TALLOC_FREE(tmp_ctx);
2354 return result;
2357 /* Delete a key with subkeys of a given printer. */
2358 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
2359 struct dcerpc_binding_handle *winreg_handle,
2360 const char *printer,
2361 const char *key)
2363 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2364 struct policy_handle hive_hnd, key_hnd;
2365 char *keyname;
2366 char *path;
2367 WERROR result;
2368 WERROR ignore;
2369 NTSTATUS status;
2370 TALLOC_CTX *tmp_ctx;
2372 tmp_ctx = talloc_stackframe();
2373 if (tmp_ctx == NULL) {
2374 return WERR_NOMEM;
2377 path = winreg_printer_data_keyname(tmp_ctx, printer);
2378 if (path == NULL) {
2379 TALLOC_FREE(tmp_ctx);
2380 return WERR_NOMEM;
2383 result = winreg_printer_openkey(tmp_ctx,
2384 winreg_handle,
2385 path,
2386 key,
2387 false,
2388 access_mask,
2389 &hive_hnd,
2390 &key_hnd);
2391 if (!W_ERROR_IS_OK(result)) {
2392 /* key doesn't exist */
2393 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2394 result = WERR_OK;
2395 goto done;
2398 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
2399 key, win_errstr(result)));
2400 goto done;
2403 if (is_valid_policy_hnd(&key_hnd)) {
2404 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
2407 if (key == NULL || key[0] == '\0') {
2408 keyname = path;
2409 } else {
2410 keyname = talloc_asprintf(tmp_ctx,
2411 "%s\\%s",
2412 path,
2413 key);
2414 if (keyname == NULL) {
2415 result = WERR_NOMEM;
2416 goto done;
2420 status = dcerpc_winreg_delete_subkeys_recursive(tmp_ctx,
2421 winreg_handle,
2422 &hive_hnd,
2423 access_mask,
2424 keyname,
2425 &result);
2427 if (!NT_STATUS_IS_OK(status)) {
2428 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2429 key, nt_errstr(status)));
2430 result = ntstatus_to_werror(status);
2431 goto done;
2434 if (!W_ERROR_IS_OK(result)) {
2435 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2436 key, win_errstr(result)));
2437 goto done;
2440 done:
2441 if (is_valid_policy_hnd(&key_hnd)) {
2442 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2444 if (is_valid_policy_hnd(&hive_hnd)) {
2445 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2448 TALLOC_FREE(tmp_ctx);
2449 return result;
2452 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
2453 struct dcerpc_binding_handle *winreg_handle,
2454 const char *printer)
2456 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2457 struct policy_handle hive_hnd, key_hnd;
2458 char *path;
2459 NTSTATUS status;
2460 WERROR result;
2461 TALLOC_CTX *tmp_ctx;
2463 tmp_ctx = talloc_stackframe();
2464 if (tmp_ctx == NULL) {
2465 return WERR_NOMEM;
2468 path = winreg_printer_data_keyname(tmp_ctx, printer);
2469 if (path == NULL) {
2470 TALLOC_FREE(tmp_ctx);
2471 return WERR_NOMEM;
2474 ZERO_STRUCT(hive_hnd);
2475 ZERO_STRUCT(key_hnd);
2477 result = winreg_printer_openkey(tmp_ctx,
2478 winreg_handle,
2479 path,
2481 false,
2482 access_mask,
2483 &hive_hnd,
2484 &key_hnd);
2485 if (!W_ERROR_IS_OK(result)) {
2486 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
2487 path, win_errstr(result)));
2488 goto done;
2491 status = dcerpc_winreg_set_dword(tmp_ctx,
2492 winreg_handle,
2493 &key_hnd,
2494 "ChangeID",
2495 winreg_printer_rev_changeid(),
2496 &result);
2497 if (!NT_STATUS_IS_OK(status)) {
2498 result = ntstatus_to_werror(status);
2500 if (!W_ERROR_IS_OK(result)) {
2501 goto done;
2504 result = WERR_OK;
2505 done:
2506 if (winreg_handle != NULL) {
2507 WERROR ignore;
2509 if (is_valid_policy_hnd(&key_hnd)) {
2510 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2512 if (is_valid_policy_hnd(&hive_hnd)) {
2513 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2517 TALLOC_FREE(tmp_ctx);
2518 return result;
2521 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
2522 struct dcerpc_binding_handle *winreg_handle,
2523 const char *printer,
2524 uint32_t *pchangeid)
2526 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2527 struct policy_handle hive_hnd, key_hnd;
2528 uint32_t changeid = 0;
2529 char *path;
2530 NTSTATUS status;
2531 WERROR result;
2532 WERROR ignore;
2533 TALLOC_CTX *tmp_ctx;
2535 tmp_ctx = talloc_stackframe();
2536 if (tmp_ctx == NULL) {
2537 return WERR_NOMEM;
2540 path = winreg_printer_data_keyname(tmp_ctx, printer);
2541 if (path == NULL) {
2542 TALLOC_FREE(tmp_ctx);
2543 return WERR_NOMEM;
2546 ZERO_STRUCT(hive_hnd);
2547 ZERO_STRUCT(key_hnd);
2549 result = winreg_printer_openkey(tmp_ctx,
2550 winreg_handle,
2551 path,
2553 false,
2554 access_mask,
2555 &hive_hnd,
2556 &key_hnd);
2557 if (!W_ERROR_IS_OK(result)) {
2558 DEBUG(2, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
2559 path, win_errstr(result)));
2560 goto done;
2563 DEBUG(10, ("winreg_printer_get_changeid: get changeid from %s\n", path));
2565 status = dcerpc_winreg_query_dword(tmp_ctx,
2566 winreg_handle,
2567 &key_hnd,
2568 "ChangeID",
2569 &changeid,
2570 &result);
2571 if (!NT_STATUS_IS_OK(status)) {
2572 result = ntstatus_to_werror(status);
2574 if (!W_ERROR_IS_OK(result)) {
2575 goto done;
2578 if (pchangeid) {
2579 *pchangeid = changeid;
2582 result = WERR_OK;
2583 done:
2584 if (is_valid_policy_hnd(&key_hnd)) {
2585 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2587 if (is_valid_policy_hnd(&hive_hnd)) {
2588 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2591 TALLOC_FREE(tmp_ctx);
2592 return result;
2596 * The special behaviour of the spoolss forms is documented at the website:
2598 * Managing Win32 Printserver Forms
2599 * http://unixwiz.net/techtips/winspooler-forms.html
2602 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
2603 struct dcerpc_binding_handle *winreg_handle,
2604 struct spoolss_AddFormInfo1 *form)
2606 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2607 struct policy_handle hive_hnd, key_hnd;
2608 struct winreg_String wvalue = { 0, };
2609 DATA_BLOB blob;
2610 uint32_t num_info = 0;
2611 union spoolss_FormInfo *info = NULL;
2612 uint32_t i;
2613 WERROR result;
2614 NTSTATUS status;
2615 TALLOC_CTX *tmp_ctx;
2617 tmp_ctx = talloc_stackframe();
2618 if (tmp_ctx == NULL) {
2619 return WERR_NOMEM;
2622 ZERO_STRUCT(hive_hnd);
2623 ZERO_STRUCT(key_hnd);
2625 result = winreg_printer_openkey(tmp_ctx,
2626 winreg_handle,
2627 TOP_LEVEL_CONTROL_FORMS_KEY,
2629 true,
2630 access_mask,
2631 &hive_hnd,
2632 &key_hnd);
2633 if (!W_ERROR_IS_OK(result)) {
2634 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
2635 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2636 goto done;
2639 result = winreg_printer_enumforms1(tmp_ctx, winreg_handle,
2640 &num_info, &info);
2641 if (!W_ERROR_IS_OK(result)) {
2642 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
2643 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2644 goto done;
2647 /* If form name already exists or is builtin return ALREADY_EXISTS */
2648 for (i = 0; i < num_info; i++) {
2649 if (strequal(info[i].info1.form_name, form->form_name)) {
2650 result = WERR_FILE_EXISTS;
2651 goto done;
2655 wvalue.name = form->form_name;
2657 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2658 SIVAL(blob.data, 0, form->size.width);
2659 SIVAL(blob.data, 4, form->size.height);
2660 SIVAL(blob.data, 8, form->area.left);
2661 SIVAL(blob.data, 12, form->area.top);
2662 SIVAL(blob.data, 16, form->area.right);
2663 SIVAL(blob.data, 20, form->area.bottom);
2664 SIVAL(blob.data, 24, num_info + 1); /* FIXME */
2665 SIVAL(blob.data, 28, form->flags);
2667 status = dcerpc_winreg_SetValue(winreg_handle,
2668 tmp_ctx,
2669 &key_hnd,
2670 wvalue,
2671 REG_BINARY,
2672 blob.data,
2673 blob.length,
2674 &result);
2675 if (!NT_STATUS_IS_OK(status)) {
2676 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
2677 wvalue.name, nt_errstr(status)));
2678 result = ntstatus_to_werror(status);
2681 done:
2682 if (winreg_handle != NULL) {
2683 WERROR ignore;
2685 if (is_valid_policy_hnd(&key_hnd)) {
2686 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2688 if (is_valid_policy_hnd(&hive_hnd)) {
2689 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2693 TALLOC_FREE(info);
2694 TALLOC_FREE(tmp_ctx);
2695 return result;
2698 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
2699 struct dcerpc_binding_handle *winreg_handle,
2700 uint32_t *pnum_info,
2701 union spoolss_FormInfo **pinfo)
2703 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2704 struct policy_handle hive_hnd, key_hnd;
2705 union spoolss_FormInfo *info;
2706 struct spoolss_PrinterEnumValues *enum_values = NULL;
2707 uint32_t num_values = 0;
2708 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2709 uint32_t i;
2710 WERROR result;
2711 NTSTATUS status;
2712 const char **enum_names = NULL;
2713 enum winreg_Type *enum_types = NULL;
2714 DATA_BLOB *enum_data_blobs = NULL;
2715 TALLOC_CTX *tmp_ctx;
2717 tmp_ctx = talloc_stackframe();
2718 if (tmp_ctx == NULL) {
2719 return WERR_NOMEM;
2722 ZERO_STRUCT(hive_hnd);
2723 ZERO_STRUCT(key_hnd);
2725 result = winreg_printer_openkey(tmp_ctx,
2726 winreg_handle,
2727 TOP_LEVEL_CONTROL_FORMS_KEY,
2729 true,
2730 access_mask,
2731 &hive_hnd,
2732 &key_hnd);
2733 if (!W_ERROR_IS_OK(result)) {
2734 /* key doesn't exist */
2735 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2736 result = WERR_OK;
2737 goto done;
2740 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
2741 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2742 goto done;
2745 status = dcerpc_winreg_enumvals(tmp_ctx,
2746 winreg_handle,
2747 &key_hnd,
2748 &num_values,
2749 &enum_names,
2750 &enum_types,
2751 &enum_data_blobs,
2752 &result);
2753 if (!NT_STATUS_IS_OK(status)){
2754 result = ntstatus_to_werror(status);
2757 if (!W_ERROR_IS_OK(result)) {
2758 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2759 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2760 goto done;
2763 enum_values = talloc_zero_array(tmp_ctx,
2764 struct spoolss_PrinterEnumValues,
2765 num_values);
2766 if (enum_values == NULL){
2767 result = WERR_NOMEM;
2768 goto done;
2771 for (i = 0; i < num_values; i++){
2772 enum_values[i].value_name = enum_names[i];
2773 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
2774 enum_values[i].type = enum_types[i];
2775 enum_values[i].data_length = enum_data_blobs[i].length;
2776 enum_values[i].data = NULL;
2777 if (enum_values[i].data_length != 0){
2778 enum_values[i].data = &enum_data_blobs[i];
2782 if (!W_ERROR_IS_OK(result)) {
2783 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2784 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2785 goto done;
2788 info = talloc_array(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
2789 if (info == NULL) {
2790 result = WERR_NOMEM;
2791 goto done;
2794 /* Enumerate BUILTIN forms */
2795 for (i = 0; i < num_builtin; i++) {
2796 info[i].info1 = builtin_forms1[i];
2799 /* Enumerate registry forms */
2800 for (i = 0; i < num_values; i++) {
2801 union spoolss_FormInfo val;
2803 if (enum_values[i].type != REG_BINARY ||
2804 enum_values[i].data_length != 32) {
2805 continue;
2808 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
2809 if (val.info1.form_name == NULL) {
2810 result = WERR_NOMEM;
2811 goto done;
2814 val.info1.size.width = IVAL(enum_values[i].data->data, 0);
2815 val.info1.size.height = IVAL(enum_values[i].data->data, 4);
2816 val.info1.area.left = IVAL(enum_values[i].data->data, 8);
2817 val.info1.area.top = IVAL(enum_values[i].data->data, 12);
2818 val.info1.area.right = IVAL(enum_values[i].data->data, 16);
2819 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
2820 /* skip form index IVAL(enum_values[i].data->data, 24)));*/
2821 val.info1.flags = (enum spoolss_FormFlags) IVAL(enum_values[i].data->data, 28);
2823 info[i + num_builtin] = val;
2826 *pnum_info = num_builtin + num_values;
2827 if (pinfo) {
2828 *pinfo = talloc_move(mem_ctx, &info);
2831 done:
2832 if (winreg_handle != NULL) {
2833 WERROR ignore;
2835 if (is_valid_policy_hnd(&key_hnd)) {
2836 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2838 if (is_valid_policy_hnd(&hive_hnd)) {
2839 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2843 TALLOC_FREE(enum_values);
2844 TALLOC_FREE(tmp_ctx);
2845 return result;
2848 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
2849 struct dcerpc_binding_handle *winreg_handle,
2850 const char *form_name)
2852 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2853 struct policy_handle hive_hnd, key_hnd;
2854 struct winreg_String wvalue = { 0, };
2855 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2856 uint32_t i;
2857 WERROR result = WERR_OK;
2858 WERROR ignore;
2859 NTSTATUS status;
2860 TALLOC_CTX *tmp_ctx;
2862 for (i = 0; i < num_builtin; i++) {
2863 if (strequal(builtin_forms1[i].form_name, form_name)) {
2864 return WERR_INVALID_PARAMETER;
2868 tmp_ctx = talloc_stackframe();
2869 if (tmp_ctx == NULL) {
2870 return WERR_NOMEM;
2873 ZERO_STRUCT(hive_hnd);
2874 ZERO_STRUCT(key_hnd);
2876 result = winreg_printer_openkey(tmp_ctx,
2877 winreg_handle,
2878 TOP_LEVEL_CONTROL_FORMS_KEY,
2880 false,
2881 access_mask,
2882 &hive_hnd,
2883 &key_hnd);
2884 if (!W_ERROR_IS_OK(result)) {
2885 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
2886 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2887 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2888 result = WERR_INVALID_FORM_NAME;
2890 goto done;
2893 wvalue.name = form_name;
2894 status = dcerpc_winreg_DeleteValue(winreg_handle,
2895 tmp_ctx,
2896 &key_hnd,
2897 wvalue,
2898 &result);
2899 if (!NT_STATUS_IS_OK(status)) {
2900 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
2901 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
2902 wvalue.name, nt_errstr(status)));
2903 result = ntstatus_to_werror(status);
2904 goto done;
2907 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2908 result = WERR_INVALID_FORM_NAME;
2911 done:
2912 if (is_valid_policy_hnd(&key_hnd)) {
2913 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2915 if (is_valid_policy_hnd(&hive_hnd)) {
2916 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2919 TALLOC_FREE(tmp_ctx);
2920 return result;
2923 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
2924 struct dcerpc_binding_handle *winreg_handle,
2925 const char *form_name,
2926 struct spoolss_AddFormInfo1 *form)
2928 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2929 struct policy_handle hive_hnd, key_hnd;
2930 struct winreg_String wvalue = { 0, };
2931 DATA_BLOB blob;
2932 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2933 uint32_t i;
2934 WERROR result;
2935 NTSTATUS status;
2936 TALLOC_CTX *tmp_ctx = NULL;
2938 for (i = 0; i < num_builtin; i++) {
2939 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
2940 result = WERR_INVALID_PARAM;
2941 goto done;
2945 tmp_ctx = talloc_stackframe();
2946 if (tmp_ctx == NULL) {
2947 return WERR_NOMEM;
2950 ZERO_STRUCT(hive_hnd);
2951 ZERO_STRUCT(key_hnd);
2953 result = winreg_printer_openkey(tmp_ctx,
2954 winreg_handle,
2955 TOP_LEVEL_CONTROL_FORMS_KEY,
2957 true,
2958 access_mask,
2959 &hive_hnd,
2960 &key_hnd);
2961 if (!W_ERROR_IS_OK(result)) {
2962 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2963 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2964 goto done;
2967 /* If form_name != form->form_name then we renamed the form */
2968 if (strequal(form_name, form->form_name)) {
2969 result = winreg_printer_deleteform1(tmp_ctx, winreg_handle,
2970 form_name);
2971 if (!W_ERROR_IS_OK(result)) {
2972 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2973 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2974 goto done;
2978 wvalue.name = form->form_name;
2980 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2981 SIVAL(blob.data, 0, form->size.width);
2982 SIVAL(blob.data, 4, form->size.height);
2983 SIVAL(blob.data, 8, form->area.left);
2984 SIVAL(blob.data, 12, form->area.top);
2985 SIVAL(blob.data, 16, form->area.right);
2986 SIVAL(blob.data, 20, form->area.bottom);
2987 SIVAL(blob.data, 24, 42);
2988 SIVAL(blob.data, 28, form->flags);
2990 status = dcerpc_winreg_SetValue(winreg_handle,
2991 tmp_ctx,
2992 &key_hnd,
2993 wvalue,
2994 REG_BINARY,
2995 blob.data,
2996 blob.length,
2997 &result);
2998 if (!NT_STATUS_IS_OK(status)) {
2999 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
3000 wvalue.name, nt_errstr(status)));
3001 result = ntstatus_to_werror(status);
3004 done:
3005 if (winreg_handle != NULL) {
3006 WERROR ignore;
3008 if (is_valid_policy_hnd(&key_hnd)) {
3009 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3011 if (is_valid_policy_hnd(&hive_hnd)) {
3012 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3016 TALLOC_FREE(tmp_ctx);
3017 return result;
3020 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
3021 struct dcerpc_binding_handle *winreg_handle,
3022 const char *form_name,
3023 struct spoolss_FormInfo1 *r)
3025 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3026 struct policy_handle hive_hnd, key_hnd;
3027 struct winreg_String wvalue;
3028 enum winreg_Type type_in = REG_NONE;
3029 uint8_t *data_in = NULL;
3030 uint32_t data_in_size = 0;
3031 uint32_t value_len = 0;
3032 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3033 uint32_t i;
3034 WERROR result;
3035 WERROR ignore;
3036 NTSTATUS status;
3037 TALLOC_CTX *tmp_ctx;
3039 /* check builtin forms first */
3040 for (i = 0; i < num_builtin; i++) {
3041 if (strequal(builtin_forms1[i].form_name, form_name)) {
3042 *r = builtin_forms1[i];
3043 return WERR_OK;
3047 tmp_ctx = talloc_stackframe();
3048 if (tmp_ctx == NULL) {
3049 return WERR_NOMEM;
3052 ZERO_STRUCT(hive_hnd);
3053 ZERO_STRUCT(key_hnd);
3055 result = winreg_printer_openkey(tmp_ctx,
3056 winreg_handle,
3057 TOP_LEVEL_CONTROL_FORMS_KEY,
3059 true,
3060 access_mask,
3061 &hive_hnd,
3062 &key_hnd);
3063 if (!W_ERROR_IS_OK(result)) {
3064 DEBUG(2, ("winreg_printer_getform1: Could not open key %s: %s\n",
3065 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3066 goto done;
3069 wvalue.name = form_name;
3072 * call QueryValue once with data == NULL to get the
3073 * needed memory size to be allocated, then allocate
3074 * data buffer and call again.
3076 status = dcerpc_winreg_QueryValue(winreg_handle,
3077 tmp_ctx,
3078 &key_hnd,
3079 &wvalue,
3080 &type_in,
3081 NULL,
3082 &data_in_size,
3083 &value_len,
3084 &result);
3085 if (!NT_STATUS_IS_OK(status)) {
3086 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3087 wvalue.name, nt_errstr(status)));
3088 result = ntstatus_to_werror(status);
3089 goto done;
3091 if (!W_ERROR_IS_OK(result)) {
3092 goto done;
3095 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
3096 if (data_in == NULL) {
3097 result = WERR_NOMEM;
3098 goto done;
3100 value_len = 0;
3102 status = dcerpc_winreg_QueryValue(winreg_handle,
3103 tmp_ctx,
3104 &key_hnd,
3105 &wvalue,
3106 &type_in,
3107 data_in,
3108 &data_in_size,
3109 &value_len,
3110 &result);
3111 if (!NT_STATUS_IS_OK(status)) {
3112 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3113 wvalue.name, nt_errstr(status)));
3114 result = ntstatus_to_werror(status);
3115 goto done;
3117 if (!W_ERROR_IS_OK(result)) {
3118 goto done;
3121 r->form_name = talloc_strdup(mem_ctx, form_name);
3122 if (r->form_name == NULL) {
3123 result = WERR_NOMEM;
3124 goto done;
3127 r->size.width = IVAL(data_in, 0);
3128 r->size.height = IVAL(data_in, 4);
3129 r->area.left = IVAL(data_in, 8);
3130 r->area.top = IVAL(data_in, 12);
3131 r->area.right = IVAL(data_in, 16);
3132 r->area.bottom = IVAL(data_in, 20);
3133 /* skip index IVAL(data_in, 24)));*/
3134 r->flags = (enum spoolss_FormFlags) IVAL(data_in, 28);
3136 result = WERR_OK;
3137 done:
3138 if (is_valid_policy_hnd(&key_hnd)) {
3139 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3141 if (is_valid_policy_hnd(&hive_hnd)) {
3142 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3145 TALLOC_FREE(tmp_ctx);
3146 return result;
3149 WERROR winreg_add_driver(TALLOC_CTX *mem_ctx,
3150 struct dcerpc_binding_handle *winreg_handle,
3151 struct spoolss_AddDriverInfoCtr *r,
3152 const char **driver_name,
3153 uint32_t *driver_version)
3155 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3156 struct policy_handle hive_hnd, key_hnd;
3157 struct spoolss_DriverInfo8 info8;
3158 TALLOC_CTX *tmp_ctx = NULL;
3159 NTSTATUS status;
3160 WERROR result;
3162 ZERO_STRUCT(hive_hnd);
3163 ZERO_STRUCT(key_hnd);
3164 ZERO_STRUCT(info8);
3166 if (!driver_info_ctr_to_info8(r, &info8)) {
3167 result = WERR_INVALID_PARAMETER;
3168 goto done;
3171 tmp_ctx = talloc_stackframe();
3172 if (tmp_ctx == NULL) {
3173 return WERR_NOMEM;
3176 result = winreg_printer_opendriver(tmp_ctx,
3177 winreg_handle,
3178 info8.driver_name,
3179 info8.architecture,
3180 info8.version,
3181 access_mask, true,
3182 &hive_hnd,
3183 &key_hnd);
3184 if (!W_ERROR_IS_OK(result)) {
3185 DEBUG(0, ("winreg_add_driver: "
3186 "Could not open driver key (%s,%s,%d): %s\n",
3187 info8.driver_name, info8.architecture,
3188 info8.version, win_errstr(result)));
3189 goto done;
3192 /* TODO: "Attributes" ? */
3194 status = dcerpc_winreg_set_dword(tmp_ctx,
3195 winreg_handle,
3196 &key_hnd,
3197 "Version",
3198 info8.version,
3199 &result);
3200 if (!NT_STATUS_IS_OK(status)) {
3201 result = ntstatus_to_werror(status);
3203 if (!W_ERROR_IS_OK(result)) {
3204 goto done;
3207 status = dcerpc_winreg_set_sz(tmp_ctx,
3208 winreg_handle,
3209 &key_hnd,
3210 "Driver",
3211 info8.driver_path,
3212 &result);
3213 if (!NT_STATUS_IS_OK(status)) {
3214 result = ntstatus_to_werror(status);
3216 if (!W_ERROR_IS_OK(result)) {
3217 goto done;
3220 status = dcerpc_winreg_set_sz(tmp_ctx,
3221 winreg_handle,
3222 &key_hnd,
3223 "Data File",
3224 info8.data_file,
3225 &result);
3226 if (!NT_STATUS_IS_OK(status)) {
3227 result = ntstatus_to_werror(status);
3229 if (!W_ERROR_IS_OK(result)) {
3230 goto done;
3233 status = dcerpc_winreg_set_sz(tmp_ctx,
3234 winreg_handle,
3235 &key_hnd,
3236 "Configuration File",
3237 info8.config_file,
3238 &result);
3239 if (!NT_STATUS_IS_OK(status)) {
3240 result = ntstatus_to_werror(status);
3242 if (!W_ERROR_IS_OK(result)) {
3243 goto done;
3246 status = dcerpc_winreg_set_sz(tmp_ctx,
3247 winreg_handle,
3248 &key_hnd,
3249 "Help File",
3250 info8.help_file,
3251 &result);
3252 if (!NT_STATUS_IS_OK(status)) {
3253 result = ntstatus_to_werror(status);
3255 if (!W_ERROR_IS_OK(result)) {
3256 goto done;
3259 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3260 winreg_handle,
3261 &key_hnd,
3262 "Dependent Files",
3263 info8.dependent_files,
3264 &result);
3265 if (!NT_STATUS_IS_OK(status)) {
3266 result = ntstatus_to_werror(status);
3268 if (!W_ERROR_IS_OK(result)) {
3269 goto done;
3272 status = dcerpc_winreg_set_sz(tmp_ctx,
3273 winreg_handle,
3274 &key_hnd,
3275 "Monitor",
3276 info8.monitor_name,
3277 &result);
3278 if (!NT_STATUS_IS_OK(status)) {
3279 result = ntstatus_to_werror(status);
3281 if (!W_ERROR_IS_OK(result)) {
3282 goto done;
3285 status = dcerpc_winreg_set_sz(tmp_ctx,
3286 winreg_handle,
3287 &key_hnd,
3288 "Datatype",
3289 info8.default_datatype,
3290 &result);
3291 if (!NT_STATUS_IS_OK(status)) {
3292 result = ntstatus_to_werror(status);
3294 if (!W_ERROR_IS_OK(result)) {
3295 goto done;
3298 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3299 winreg_handle,
3300 &key_hnd, "Previous Names",
3301 info8.previous_names,
3302 &result);
3303 if (!NT_STATUS_IS_OK(status)) {
3304 result = ntstatus_to_werror(status);
3306 if (!W_ERROR_IS_OK(result)) {
3307 goto done;
3310 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3311 &key_hnd, "DriverDate",
3312 info8.driver_date);
3313 if (!W_ERROR_IS_OK(result)) {
3314 goto done;
3317 result = winreg_printer_write_ver(tmp_ctx, winreg_handle,
3318 &key_hnd, "DriverVersion",
3319 info8.driver_version);
3320 if (!W_ERROR_IS_OK(result)) {
3321 goto done;
3324 status = dcerpc_winreg_set_sz(tmp_ctx,
3325 winreg_handle,
3326 &key_hnd,
3327 "Manufacturer",
3328 info8.manufacturer_name,
3329 &result);
3330 if (!NT_STATUS_IS_OK(status)) {
3331 result = ntstatus_to_werror(status);
3333 if (!W_ERROR_IS_OK(result)) {
3334 goto done;
3337 status = dcerpc_winreg_set_sz(tmp_ctx,
3338 winreg_handle,
3339 &key_hnd,
3340 "OEM URL",
3341 info8.manufacturer_url,
3342 &result);
3343 if (!NT_STATUS_IS_OK(status)) {
3344 result = ntstatus_to_werror(status);
3346 if (!W_ERROR_IS_OK(result)) {
3347 goto done;
3350 status = dcerpc_winreg_set_sz(tmp_ctx,
3351 winreg_handle,
3352 &key_hnd,
3353 "HardwareID",
3354 info8.hardware_id,
3355 &result);
3356 if (!NT_STATUS_IS_OK(status)) {
3357 result = ntstatus_to_werror(status);
3359 if (!W_ERROR_IS_OK(result)) {
3360 goto done;
3363 status = dcerpc_winreg_set_sz(tmp_ctx,
3364 winreg_handle,
3365 &key_hnd,
3366 "Provider",
3367 info8.provider,
3368 &result);
3369 if (!NT_STATUS_IS_OK(status)) {
3370 result = ntstatus_to_werror(status);
3372 if (!W_ERROR_IS_OK(result)) {
3373 goto done;
3376 status = dcerpc_winreg_set_sz(tmp_ctx,
3377 winreg_handle,
3378 &key_hnd,
3379 "Print Processor",
3380 info8.print_processor,
3381 &result);
3382 if (!NT_STATUS_IS_OK(status)) {
3383 result = ntstatus_to_werror(status);
3385 if (!W_ERROR_IS_OK(result)) {
3386 goto done;
3389 status = dcerpc_winreg_set_sz(tmp_ctx,
3390 winreg_handle,
3391 &key_hnd,
3392 "VendorSetup",
3393 info8.vendor_setup,
3394 &result);
3395 if (!NT_STATUS_IS_OK(status)) {
3396 result = ntstatus_to_werror(status);
3398 if (!W_ERROR_IS_OK(result)) {
3399 goto done;
3402 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3403 winreg_handle,
3404 &key_hnd,
3405 "Color Profiles",
3406 info8.color_profiles,
3407 &result);
3408 if (!NT_STATUS_IS_OK(status)) {
3409 result = ntstatus_to_werror(status);
3411 if (!W_ERROR_IS_OK(result)) {
3412 goto done;
3415 status = dcerpc_winreg_set_sz(tmp_ctx,
3416 winreg_handle,
3417 &key_hnd,
3418 "InfPath",
3419 info8.inf_path,
3420 &result);
3421 if (!NT_STATUS_IS_OK(status)) {
3422 result = ntstatus_to_werror(status);
3424 if (!W_ERROR_IS_OK(result)) {
3425 goto done;
3428 status = dcerpc_winreg_set_dword(tmp_ctx,
3429 winreg_handle,
3430 &key_hnd,
3431 "PrinterDriverAttributes",
3432 info8.printer_driver_attributes,
3433 &result);
3434 if (!NT_STATUS_IS_OK(status)) {
3435 result = ntstatus_to_werror(status);
3437 if (!W_ERROR_IS_OK(result)) {
3438 goto done;
3441 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3442 winreg_handle,
3443 &key_hnd,
3444 "CoreDependencies",
3445 info8.core_driver_dependencies,
3446 &result);
3447 if (!NT_STATUS_IS_OK(status)) {
3448 result = ntstatus_to_werror(status);
3450 if (!W_ERROR_IS_OK(result)) {
3451 goto done;
3454 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3455 &key_hnd, "MinInboxDriverVerDate",
3456 info8.min_inbox_driver_ver_date);
3457 if (!W_ERROR_IS_OK(result)) {
3458 goto done;
3461 result = winreg_printer_write_ver(tmp_ctx, winreg_handle, &key_hnd,
3462 "MinInboxDriverVerVersion",
3463 info8.min_inbox_driver_ver_version);
3464 if (!W_ERROR_IS_OK(result)) {
3465 goto done;
3468 *driver_name = info8.driver_name;
3469 *driver_version = info8.version;
3470 result = WERR_OK;
3471 done:
3472 if (winreg_handle != NULL) {
3473 WERROR ignore;
3475 if (is_valid_policy_hnd(&key_hnd)) {
3476 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3478 if (is_valid_policy_hnd(&hive_hnd)) {
3479 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3483 TALLOC_FREE(tmp_ctx);
3484 return result;
3487 WERROR winreg_get_driver(TALLOC_CTX *mem_ctx,
3488 struct dcerpc_binding_handle *winreg_handle,
3489 const char *architecture,
3490 const char *driver_name,
3491 uint32_t driver_version,
3492 struct spoolss_DriverInfo8 **_info8)
3494 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3495 struct policy_handle hive_hnd, key_hnd;
3496 struct spoolss_DriverInfo8 i8, *info8;
3497 struct spoolss_PrinterEnumValues *enum_values = NULL;
3498 struct spoolss_PrinterEnumValues *v;
3499 uint32_t num_values = 0;
3500 TALLOC_CTX *tmp_ctx;
3501 WERROR result;
3502 NTSTATUS status;
3503 uint32_t i;
3504 const char **enum_names = NULL;
3505 enum winreg_Type *enum_types = NULL;
3506 DATA_BLOB *enum_data_blobs = NULL;
3508 ZERO_STRUCT(hive_hnd);
3509 ZERO_STRUCT(key_hnd);
3510 ZERO_STRUCT(i8);
3512 tmp_ctx = talloc_stackframe();
3513 if (tmp_ctx == NULL) {
3514 return WERR_NOMEM;
3517 if (driver_version == DRIVER_ANY_VERSION) {
3518 /* look for Win2k first and then for NT4 */
3519 result = winreg_printer_opendriver(tmp_ctx,
3520 winreg_handle,
3521 driver_name,
3522 architecture,
3524 access_mask, false,
3525 &hive_hnd,
3526 &key_hnd);
3527 if (!W_ERROR_IS_OK(result)) {
3528 result = winreg_printer_opendriver(tmp_ctx,
3529 winreg_handle,
3530 driver_name,
3531 architecture,
3533 access_mask, false,
3534 &hive_hnd,
3535 &key_hnd);
3537 } else {
3538 /* ok normal case */
3539 result = winreg_printer_opendriver(tmp_ctx,
3540 winreg_handle,
3541 driver_name,
3542 architecture,
3543 driver_version,
3544 access_mask, false,
3545 &hive_hnd,
3546 &key_hnd);
3548 if (!W_ERROR_IS_OK(result)) {
3549 DEBUG(5, ("winreg_get_driver: "
3550 "Could not open driver key (%s,%s,%d): %s\n",
3551 driver_name, architecture,
3552 driver_version, win_errstr(result)));
3553 goto done;
3556 status = dcerpc_winreg_enumvals(tmp_ctx,
3557 winreg_handle,
3558 &key_hnd,
3559 &num_values,
3560 &enum_names,
3561 &enum_types,
3562 &enum_data_blobs,
3563 &result);
3564 if (!NT_STATUS_IS_OK(status)){
3565 result = ntstatus_to_werror(status);
3568 if (!W_ERROR_IS_OK(result)) {
3569 DEBUG(0, ("winreg_get_driver: "
3570 "Could not enumerate values for (%s,%s,%d): %s\n",
3571 driver_name, architecture,
3572 driver_version, win_errstr(result)));
3573 goto done;
3576 enum_values = talloc_zero_array(tmp_ctx,
3577 struct spoolss_PrinterEnumValues,
3578 num_values);
3579 if (enum_values == NULL){
3580 result = WERR_NOMEM;
3581 goto done;
3584 for (i = 0; i < num_values; i++){
3585 enum_values[i].value_name = enum_names[i];
3586 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
3587 enum_values[i].type = enum_types[i];
3588 enum_values[i].data_length = enum_data_blobs[i].length;
3589 enum_values[i].data = NULL;
3590 if (enum_values[i].data_length != 0){
3591 enum_values[i].data = &enum_data_blobs[i];
3595 info8 = talloc_zero(tmp_ctx, struct spoolss_DriverInfo8);
3596 if (info8 == NULL) {
3597 result = WERR_NOMEM;
3598 goto done;
3601 info8->driver_name = talloc_strdup(info8, driver_name);
3602 if (info8->driver_name == NULL) {
3603 result = WERR_NOMEM;
3604 goto done;
3607 info8->architecture = talloc_strdup(info8, architecture);
3608 if (info8->architecture == NULL) {
3609 result = WERR_NOMEM;
3610 goto done;
3613 result = WERR_OK;
3615 for (i = 0; i < num_values; i++) {
3616 const char *tmp_str;
3617 uint32_t tmp = 0;
3619 v = &enum_values[i];
3621 result = winreg_enumval_to_dword(info8, v,
3622 "Version",
3623 &tmp);
3624 if (W_ERROR_IS_OK(result)) {
3625 info8->version = (enum spoolss_DriverOSVersion) tmp;
3627 CHECK_ERROR(result);
3629 result = winreg_enumval_to_sz(info8, v,
3630 "Driver",
3631 &info8->driver_path);
3632 CHECK_ERROR(result);
3634 result = winreg_enumval_to_sz(info8, v,
3635 "Data File",
3636 &info8->data_file);
3637 CHECK_ERROR(result);
3639 result = winreg_enumval_to_sz(info8, v,
3640 "Configuration File",
3641 &info8->config_file);
3642 CHECK_ERROR(result);
3644 result = winreg_enumval_to_sz(info8, v,
3645 "Help File",
3646 &info8->help_file);
3647 CHECK_ERROR(result);
3649 result = winreg_enumval_to_multi_sz(info8, v,
3650 "Dependent Files",
3651 &info8->dependent_files);
3652 CHECK_ERROR(result);
3654 result = winreg_enumval_to_sz(info8, v,
3655 "Monitor",
3656 &info8->monitor_name);
3657 CHECK_ERROR(result);
3659 result = winreg_enumval_to_sz(info8, v,
3660 "Datatype",
3661 &info8->default_datatype);
3662 CHECK_ERROR(result);
3664 result = winreg_enumval_to_multi_sz(info8, v,
3665 "Previous Names",
3666 &info8->previous_names);
3667 CHECK_ERROR(result);
3669 result = winreg_enumval_to_sz(info8, v,
3670 "DriverDate",
3671 &tmp_str);
3672 if (W_ERROR_IS_OK(result)) {
3673 result = winreg_printer_date_to_NTTIME(tmp_str,
3674 &info8->driver_date);
3676 CHECK_ERROR(result);
3678 result = winreg_enumval_to_sz(info8, v,
3679 "DriverVersion",
3680 &tmp_str);
3681 if (W_ERROR_IS_OK(result)) {
3682 result = winreg_printer_ver_to_dword(tmp_str,
3683 &info8->driver_version);
3685 CHECK_ERROR(result);
3687 result = winreg_enumval_to_sz(info8, v,
3688 "Manufacturer",
3689 &info8->manufacturer_name);
3690 CHECK_ERROR(result);
3692 result = winreg_enumval_to_sz(info8, v,
3693 "OEM URL",
3694 &info8->manufacturer_url);
3695 CHECK_ERROR(result);
3697 result = winreg_enumval_to_sz(info8, v,
3698 "HardwareID",
3699 &info8->hardware_id);
3700 CHECK_ERROR(result);
3702 result = winreg_enumval_to_sz(info8, v,
3703 "Provider",
3704 &info8->provider);
3705 CHECK_ERROR(result);
3707 result = winreg_enumval_to_sz(info8, v,
3708 "Print Processor",
3709 &info8->print_processor);
3710 CHECK_ERROR(result);
3712 result = winreg_enumval_to_sz(info8, v,
3713 "VendorSetup",
3714 &info8->vendor_setup);
3715 CHECK_ERROR(result);
3717 result = winreg_enumval_to_multi_sz(info8, v,
3718 "Color Profiles",
3719 &info8->color_profiles);
3720 CHECK_ERROR(result);
3722 result = winreg_enumval_to_sz(info8, v,
3723 "InfPath",
3724 &info8->inf_path);
3725 CHECK_ERROR(result);
3727 result = winreg_enumval_to_dword(info8, v,
3728 "PrinterDriverAttributes",
3729 &info8->printer_driver_attributes);
3730 CHECK_ERROR(result);
3732 result = winreg_enumval_to_multi_sz(info8, v,
3733 "CoreDependencies",
3734 &info8->core_driver_dependencies);
3735 CHECK_ERROR(result);
3737 result = winreg_enumval_to_sz(info8, v,
3738 "MinInboxDriverVerDate",
3739 &tmp_str);
3740 if (W_ERROR_IS_OK(result)) {
3741 result = winreg_printer_date_to_NTTIME(tmp_str,
3742 &info8->min_inbox_driver_ver_date);
3744 CHECK_ERROR(result);
3746 result = winreg_enumval_to_sz(info8, v,
3747 "MinInboxDriverVerVersion",
3748 &tmp_str);
3749 if (W_ERROR_IS_OK(result)) {
3750 result = winreg_printer_ver_to_dword(tmp_str,
3751 &info8->min_inbox_driver_ver_version);
3753 CHECK_ERROR(result);
3756 if (!W_ERROR_IS_OK(result)) {
3757 DEBUG(0, ("winreg_enumval_to_TYPE() failed "
3758 "for %s: %s\n", v->value_name,
3759 win_errstr(result)));
3760 goto done;
3763 *_info8 = talloc_steal(mem_ctx, info8);
3764 result = WERR_OK;
3765 done:
3766 if (winreg_handle != NULL) {
3767 WERROR ignore;
3769 if (is_valid_policy_hnd(&key_hnd)) {
3770 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3772 if (is_valid_policy_hnd(&hive_hnd)) {
3773 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3777 TALLOC_FREE(tmp_ctx);
3778 return result;
3781 WERROR winreg_del_driver(TALLOC_CTX *mem_ctx,
3782 struct dcerpc_binding_handle *winreg_handle,
3783 struct spoolss_DriverInfo8 *info8,
3784 uint32_t version)
3786 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3787 struct policy_handle hive_hnd, key_hnd;
3788 TALLOC_CTX *tmp_ctx;
3789 char *key_name;
3790 WERROR result;
3791 NTSTATUS status;
3793 ZERO_STRUCT(hive_hnd);
3794 ZERO_STRUCT(key_hnd);
3796 tmp_ctx = talloc_stackframe();
3797 if (tmp_ctx == NULL) {
3798 return WERR_NOMEM;
3801 /* test that the key exists */
3802 result = winreg_printer_opendriver(tmp_ctx,
3803 winreg_handle,
3804 info8->driver_name,
3805 info8->architecture,
3806 version,
3807 access_mask, false,
3808 &hive_hnd,
3809 &key_hnd);
3810 if (!W_ERROR_IS_OK(result)) {
3811 /* key doesn't exist */
3812 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3813 result = WERR_OK;
3814 goto done;
3817 DEBUG(5, ("winreg_del_driver: "
3818 "Could not open driver (%s,%s,%u): %s\n",
3819 info8->driver_name, info8->architecture,
3820 version, win_errstr(result)));
3821 goto done;
3825 if (is_valid_policy_hnd(&key_hnd)) {
3826 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
3829 key_name = talloc_asprintf(tmp_ctx,
3830 "%s\\Environments\\%s\\Drivers\\Version-%u\\%s",
3831 TOP_LEVEL_CONTROL_KEY,
3832 info8->architecture, version,
3833 info8->driver_name);
3834 if (key_name == NULL) {
3835 result = WERR_NOMEM;
3836 goto done;
3839 status = dcerpc_winreg_delete_subkeys_recursive(tmp_ctx,
3840 winreg_handle,
3841 &hive_hnd,
3842 access_mask,
3843 key_name,
3844 &result);
3846 if (!NT_STATUS_IS_OK(status)){
3847 DEBUG(0, ("winreg_del_driver: "
3848 "Could not open driver (%s,%s,%u): %s\n",
3849 info8->driver_name, info8->architecture,
3850 version, nt_errstr(status)));
3851 goto done;
3854 if (!W_ERROR_IS_OK(result)) {
3855 DEBUG(0, ("winreg_del_driver: "
3856 "Could not open driver (%s,%s,%u): %s\n",
3857 info8->driver_name, info8->architecture,
3858 version, win_errstr(result)));
3859 goto done;
3862 result = WERR_OK;
3863 done:
3864 if (winreg_handle != NULL) {
3865 WERROR ignore;
3867 if (is_valid_policy_hnd(&key_hnd)) {
3868 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3870 if (is_valid_policy_hnd(&hive_hnd)) {
3871 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3875 TALLOC_FREE(tmp_ctx);
3876 return result;
3879 WERROR winreg_get_driver_list(TALLOC_CTX *mem_ctx,
3880 struct dcerpc_binding_handle *winreg_handle,
3881 const char *architecture,
3882 uint32_t version,
3883 uint32_t *num_drivers,
3884 const char ***drivers_p)
3886 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3887 struct policy_handle hive_hnd, key_hnd;
3888 const char **drivers;
3889 TALLOC_CTX *tmp_ctx;
3890 WERROR result;
3891 NTSTATUS status;
3893 *num_drivers = 0;
3894 *drivers_p = NULL;
3896 ZERO_STRUCT(hive_hnd);
3897 ZERO_STRUCT(key_hnd);
3899 tmp_ctx = talloc_stackframe();
3900 if (tmp_ctx == NULL) {
3901 return WERR_NOMEM;
3904 /* use NULL for the driver name so we open the key that is
3905 * parent of all drivers for this architecture and version */
3906 result = winreg_printer_opendriver(tmp_ctx,
3907 winreg_handle,
3908 NULL,
3909 architecture,
3910 version,
3911 access_mask, false,
3912 &hive_hnd,
3913 &key_hnd);
3914 if (!W_ERROR_IS_OK(result)) {
3915 DEBUG(5, ("winreg_get_driver_list: "
3916 "Could not open key (%s,%u): %s\n",
3917 architecture, version, win_errstr(result)));
3918 result = WERR_OK;
3919 goto done;
3922 status = dcerpc_winreg_enum_keys(tmp_ctx,
3923 winreg_handle,
3924 &key_hnd,
3925 num_drivers,
3926 &drivers,
3927 &result);
3928 if (!NT_STATUS_IS_OK(status)) {
3929 result = ntstatus_to_werror(status);
3931 if (!W_ERROR_IS_OK(result)) {
3932 DEBUG(0, ("winreg_get_driver_list: "
3933 "Could not enumerate drivers for (%s,%u): %s\n",
3934 architecture, version, win_errstr(result)));
3935 goto done;
3938 *drivers_p = talloc_steal(mem_ctx, drivers);
3940 result = WERR_OK;
3941 done:
3942 if (winreg_handle != NULL) {
3943 WERROR ignore;
3945 if (is_valid_policy_hnd(&key_hnd)) {
3946 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3948 if (is_valid_policy_hnd(&hive_hnd)) {
3949 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3953 TALLOC_FREE(tmp_ctx);
3954 return result;