s3: Fix Coverity ID 242695 Dereference before null check
[Samba/gebeck_regimport.git] / source3 / rpc_client / cli_winreg_spoolss.c
blob1cf034e9f244b551409ac963e3c03307bd09fb84
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 WERROR ignore;
1352 const char **enum_names = NULL;
1353 enum winreg_Type *enum_types = NULL;
1354 DATA_BLOB *enum_data_blobs = NULL;
1355 TALLOC_CTX *tmp_ctx;
1357 tmp_ctx = talloc_stackframe();
1358 if (tmp_ctx == NULL) {
1359 return WERR_NOMEM;
1362 path = winreg_printer_data_keyname(tmp_ctx, printer);
1363 if (path == NULL) {
1364 TALLOC_FREE(tmp_ctx);
1365 return WERR_NOMEM;
1368 result = winreg_printer_openkey(tmp_ctx,
1369 winreg_handle,
1370 path,
1372 false,
1373 access_mask,
1374 &hive_hnd,
1375 &key_hnd);
1376 if (!W_ERROR_IS_OK(result)) {
1377 DEBUG(2, ("winreg_get_printer: Could not open key %s: %s\n",
1378 path, win_errstr(result)));
1379 goto done;
1382 status = dcerpc_winreg_enumvals(tmp_ctx,
1383 winreg_handle,
1384 &key_hnd,
1385 &num_values,
1386 &enum_names,
1387 &enum_types,
1388 &enum_data_blobs,
1389 &result);
1390 if (!NT_STATUS_IS_OK(status)){
1391 result = ntstatus_to_werror(status);
1394 if (!W_ERROR_IS_OK(result)) {
1395 DEBUG(0, ("winreg_get_printer: Could not enumerate values in %s: %s\n",
1396 path, win_errstr(result)));
1397 goto done;
1400 info2 = talloc_zero(tmp_ctx, struct spoolss_PrinterInfo2);
1401 if (info2 == NULL) {
1402 result = WERR_NOMEM;
1403 goto done;
1406 FILL_STRING(info2, EMPTY_STRING, info2->servername);
1407 FILL_STRING(info2, EMPTY_STRING, info2->printername);
1408 FILL_STRING(info2, EMPTY_STRING, info2->sharename);
1409 FILL_STRING(info2, EMPTY_STRING, info2->portname);
1410 FILL_STRING(info2, EMPTY_STRING, info2->drivername);
1411 FILL_STRING(info2, EMPTY_STRING, info2->comment);
1412 FILL_STRING(info2, EMPTY_STRING, info2->location);
1413 FILL_STRING(info2, EMPTY_STRING, info2->sepfile);
1414 FILL_STRING(info2, EMPTY_STRING, info2->printprocessor);
1415 FILL_STRING(info2, EMPTY_STRING, info2->datatype);
1416 FILL_STRING(info2, EMPTY_STRING, info2->parameters);
1418 for (i = 0; i < num_values; i++) {
1419 enum_value.value_name = enum_names[i];
1420 enum_value.value_name_len = 2*strlen_m_term(enum_names[i]);
1421 enum_value.type = enum_types[i];
1422 enum_value.data_length = enum_data_blobs[i].length;
1423 enum_value.data = NULL;
1424 if (enum_value.data_length != 0){
1425 enum_value.data = &enum_data_blobs[i];
1427 v = &enum_value;
1429 result = winreg_enumval_to_sz(info2,
1431 "Name",
1432 &info2->printername);
1433 CHECK_ERROR(result);
1435 result = winreg_enumval_to_sz(info2,
1437 "Share Name",
1438 &info2->sharename);
1439 CHECK_ERROR(result);
1441 result = winreg_enumval_to_sz(info2,
1443 "Port",
1444 &info2->portname);
1445 CHECK_ERROR(result);
1447 result = winreg_enumval_to_sz(info2,
1449 "Description",
1450 &info2->comment);
1451 CHECK_ERROR(result);
1453 result = winreg_enumval_to_sz(info2,
1455 "Location",
1456 &info2->location);
1457 CHECK_ERROR(result);
1459 result = winreg_enumval_to_sz(info2,
1461 "Separator File",
1462 &info2->sepfile);
1463 CHECK_ERROR(result);
1465 result = winreg_enumval_to_sz(info2,
1467 "Print Processor",
1468 &info2->printprocessor);
1469 CHECK_ERROR(result);
1471 result = winreg_enumval_to_sz(info2,
1473 "Datatype",
1474 &info2->datatype);
1475 CHECK_ERROR(result);
1477 result = winreg_enumval_to_sz(info2,
1479 "Parameters",
1480 &info2->parameters);
1481 CHECK_ERROR(result);
1483 result = winreg_enumval_to_sz(info2,
1485 "Printer Driver",
1486 &info2->drivername);
1487 CHECK_ERROR(result);
1489 result = winreg_enumval_to_dword(info2,
1491 "Attributes",
1492 &info2->attributes);
1493 CHECK_ERROR(result);
1495 result = winreg_enumval_to_dword(info2,
1497 "Priority",
1498 &info2->priority);
1499 CHECK_ERROR(result);
1501 result = winreg_enumval_to_dword(info2,
1503 "Default Priority",
1504 &info2->defaultpriority);
1505 CHECK_ERROR(result);
1507 result = winreg_enumval_to_dword(info2,
1509 "StartTime",
1510 &info2->starttime);
1511 CHECK_ERROR(result);
1513 result = winreg_enumval_to_dword(info2,
1515 "UntilTime",
1516 &info2->untiltime);
1517 CHECK_ERROR(result);
1519 result = winreg_enumval_to_dword(info2,
1521 "Status",
1522 &info2->status);
1523 CHECK_ERROR(result);
1525 result = winreg_enumval_to_dword(info2,
1527 "StartTime",
1528 &info2->starttime);
1529 CHECK_ERROR(result);
1532 if (!W_ERROR_IS_OK(result)) {
1533 DEBUG(0, ("winreg_get_printer: winreg_enumval_to_TYPE() failed "
1534 "for %s: %s\n",
1535 v->value_name,
1536 win_errstr(result)));
1537 goto done;
1540 /* Construct the Device Mode */
1541 status = dcerpc_winreg_query_binary(tmp_ctx,
1542 winreg_handle,
1543 &key_hnd,
1544 "Default DevMode",
1545 &blob,
1546 &result);
1547 if (!NT_STATUS_IS_OK(status)) {
1548 result = ntstatus_to_werror(status);
1550 if (W_ERROR_IS_OK(result)) {
1551 info2->devmode = talloc_zero(info2, struct spoolss_DeviceMode);
1552 if (info2->devmode == NULL) {
1553 result = WERR_NOMEM;
1554 goto done;
1556 ndr_err = ndr_pull_struct_blob(&blob,
1557 info2->devmode,
1558 info2->devmode,
1559 (ndr_pull_flags_fn_t) ndr_pull_spoolss_DeviceMode);
1560 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1561 DEBUG(0, ("winreg_get_printer: Failed to unmarshall device mode\n"));
1562 result = WERR_NOMEM;
1563 goto done;
1567 if (info2->devmode == NULL && lp_default_devmode(snum)) {
1568 result = spoolss_create_default_devmode(info2,
1569 info2->printername,
1570 &info2->devmode);
1571 if (!W_ERROR_IS_OK(result)) {
1572 goto done;
1576 if (info2->devmode) {
1577 info2->devmode->size = ndr_size_spoolss_DeviceMode(info2->devmode, 0) - info2->devmode->driverextra_data.length;
1580 result = winreg_get_printer_secdesc(info2,
1581 winreg_handle,
1582 printer,
1583 &info2->secdesc);
1584 if (!W_ERROR_IS_OK(result)) {
1585 goto done;
1588 /* Fix for OS/2 drivers. */
1589 if (get_remote_arch() == RA_OS2) {
1590 spoolss_map_to_os2_driver(info2, &info2->drivername);
1593 if (pinfo2) {
1594 *pinfo2 = talloc_move(mem_ctx, &info2);
1597 result = WERR_OK;
1598 done:
1599 if (is_valid_policy_hnd(&key_hnd)) {
1600 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1602 if (is_valid_policy_hnd(&hive_hnd)) {
1603 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1606 TALLOC_FREE(tmp_ctx);
1607 return result;
1610 WERROR winreg_get_printer_secdesc(TALLOC_CTX *mem_ctx,
1611 struct dcerpc_binding_handle *winreg_handle,
1612 const char *sharename,
1613 struct spoolss_security_descriptor **psecdesc)
1615 struct spoolss_security_descriptor *secdesc;
1616 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1617 struct policy_handle hive_hnd, key_hnd;
1618 const char *path;
1619 TALLOC_CTX *tmp_ctx;
1620 NTSTATUS status;
1621 WERROR result;
1623 tmp_ctx = talloc_stackframe();
1624 if (tmp_ctx == NULL) {
1625 return WERR_NOMEM;
1628 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1629 if (path == NULL) {
1630 talloc_free(tmp_ctx);
1631 return WERR_NOMEM;
1634 ZERO_STRUCT(hive_hnd);
1635 ZERO_STRUCT(key_hnd);
1637 result = winreg_printer_openkey(tmp_ctx,
1638 winreg_handle,
1639 path,
1641 false,
1642 access_mask,
1643 &hive_hnd,
1644 &key_hnd);
1645 if (!W_ERROR_IS_OK(result)) {
1646 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1647 goto create_default;
1649 goto done;
1652 status = dcerpc_winreg_query_sd(tmp_ctx,
1653 winreg_handle,
1654 &key_hnd,
1655 "Security",
1656 &secdesc,
1657 &result);
1658 if (!NT_STATUS_IS_OK(status)) {
1659 result = ntstatus_to_werror(status);
1661 if (!W_ERROR_IS_OK(result)) {
1662 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
1663 WERROR ignore;
1665 if (is_valid_policy_hnd(&key_hnd)) {
1666 dcerpc_winreg_CloseKey(winreg_handle,
1667 tmp_ctx,
1668 &key_hnd,
1669 &ignore);
1672 if (is_valid_policy_hnd(&hive_hnd)) {
1673 dcerpc_winreg_CloseKey(winreg_handle,
1674 tmp_ctx,
1675 &hive_hnd,
1676 &ignore);
1678 goto create_default;
1680 goto done;
1683 if (psecdesc) {
1684 *psecdesc = talloc_move(mem_ctx, &secdesc);
1687 result = WERR_OK;
1688 goto done;
1690 create_default:
1691 result = winreg_printer_openkey(tmp_ctx,
1692 winreg_handle,
1693 path,
1695 true,
1696 access_mask,
1697 &hive_hnd,
1698 &key_hnd);
1699 if (!W_ERROR_IS_OK(result)) {
1700 goto done;
1703 result = spoolss_create_default_secdesc(tmp_ctx, &secdesc);
1704 if (!W_ERROR_IS_OK(result)) {
1705 goto done;
1708 /* If security descriptor is owned by S-1-1-0 and winbindd is up,
1709 this security descriptor has been created when winbindd was
1710 down. Take ownership of security descriptor. */
1711 if (dom_sid_equal(secdesc->owner_sid, &global_sid_World)) {
1712 struct dom_sid owner_sid;
1714 /* Change sd owner to workgroup administrator */
1716 if (secrets_fetch_domain_sid(lp_workgroup(), &owner_sid)) {
1717 struct spoolss_security_descriptor *new_secdesc;
1718 size_t size;
1720 /* Create new sd */
1721 sid_append_rid(&owner_sid, DOMAIN_RID_ADMINISTRATOR);
1723 new_secdesc = make_sec_desc(tmp_ctx,
1724 secdesc->revision,
1725 secdesc->type,
1726 &owner_sid,
1727 secdesc->group_sid,
1728 secdesc->sacl,
1729 secdesc->dacl,
1730 &size);
1732 if (new_secdesc == NULL) {
1733 result = WERR_NOMEM;
1734 goto done;
1737 /* Swap with other one */
1738 secdesc = new_secdesc;
1742 status = dcerpc_winreg_set_sd(tmp_ctx,
1743 winreg_handle,
1744 &key_hnd,
1745 "Security",
1746 secdesc,
1747 &result);
1748 if (!NT_STATUS_IS_OK(status)) {
1749 result = ntstatus_to_werror(status);
1751 if (!W_ERROR_IS_OK(result)) {
1752 return result;
1755 if (psecdesc) {
1756 *psecdesc = talloc_move(mem_ctx, &secdesc);
1759 result = WERR_OK;
1760 done:
1761 if (winreg_handle != NULL) {
1762 WERROR ignore;
1764 if (is_valid_policy_hnd(&key_hnd)) {
1765 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1767 if (is_valid_policy_hnd(&hive_hnd)) {
1768 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1772 talloc_free(tmp_ctx);
1773 return result;
1776 WERROR winreg_set_printer_secdesc(TALLOC_CTX *mem_ctx,
1777 struct dcerpc_binding_handle *winreg_handle,
1778 const char *sharename,
1779 const struct spoolss_security_descriptor *secdesc)
1781 const struct spoolss_security_descriptor *new_secdesc = secdesc;
1782 struct spoolss_security_descriptor *old_secdesc;
1783 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1784 struct policy_handle hive_hnd, key_hnd;
1785 const char *path;
1786 TALLOC_CTX *tmp_ctx;
1787 NTSTATUS status;
1788 WERROR result;
1789 WERROR ignore;
1791 tmp_ctx = talloc_stackframe();
1792 if (tmp_ctx == NULL) {
1793 return WERR_NOMEM;
1796 path = winreg_printer_data_keyname(tmp_ctx, sharename);
1797 if (path == NULL) {
1798 talloc_free(tmp_ctx);
1799 return WERR_NOMEM;
1803 * The old owner and group sids of the security descriptor are not
1804 * present when new ACEs are added or removed by changing printer
1805 * permissions through NT. If they are NULL in the new security
1806 * descriptor then copy them over from the old one.
1808 if (!secdesc->owner_sid || !secdesc->group_sid) {
1809 struct dom_sid *owner_sid, *group_sid;
1810 struct security_acl *dacl, *sacl;
1811 size_t size;
1813 result = winreg_get_printer_secdesc(tmp_ctx,
1814 winreg_handle,
1815 sharename,
1816 &old_secdesc);
1817 if (!W_ERROR_IS_OK(result)) {
1818 talloc_free(tmp_ctx);
1819 return result;
1822 /* Pick out correct owner and group sids */
1823 owner_sid = secdesc->owner_sid ?
1824 secdesc->owner_sid :
1825 old_secdesc->owner_sid;
1827 group_sid = secdesc->group_sid ?
1828 secdesc->group_sid :
1829 old_secdesc->group_sid;
1831 dacl = secdesc->dacl ?
1832 secdesc->dacl :
1833 old_secdesc->dacl;
1835 sacl = secdesc->sacl ?
1836 secdesc->sacl :
1837 old_secdesc->sacl;
1839 /* Make a deep copy of the security descriptor */
1840 new_secdesc = make_sec_desc(tmp_ctx,
1841 secdesc->revision,
1842 secdesc->type,
1843 owner_sid,
1844 group_sid,
1845 sacl,
1846 dacl,
1847 &size);
1848 if (new_secdesc == NULL) {
1849 talloc_free(tmp_ctx);
1850 return WERR_NOMEM;
1854 ZERO_STRUCT(hive_hnd);
1855 ZERO_STRUCT(key_hnd);
1857 result = winreg_printer_openkey(tmp_ctx,
1858 winreg_handle,
1859 path,
1861 false,
1862 access_mask,
1863 &hive_hnd,
1864 &key_hnd);
1865 if (!W_ERROR_IS_OK(result)) {
1866 goto done;
1869 status = dcerpc_winreg_set_sd(tmp_ctx,
1870 winreg_handle,
1871 &key_hnd,
1872 "Security",
1873 new_secdesc,
1874 &result);
1875 if (!NT_STATUS_IS_OK(status)) {
1876 result = ntstatus_to_werror(status);
1879 done:
1880 if (is_valid_policy_hnd(&key_hnd)) {
1881 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1883 if (is_valid_policy_hnd(&hive_hnd)) {
1884 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1887 talloc_free(tmp_ctx);
1888 return result;
1891 /* Set printer data over the winreg pipe. */
1892 WERROR winreg_set_printer_dataex(TALLOC_CTX *mem_ctx,
1893 struct dcerpc_binding_handle *winreg_handle,
1894 const char *printer,
1895 const char *key,
1896 const char *value,
1897 enum winreg_Type type,
1898 uint8_t *data,
1899 uint32_t data_size)
1901 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1902 struct policy_handle hive_hnd, key_hnd;
1903 struct winreg_String wvalue = { 0, };
1904 char *path;
1905 WERROR result = WERR_OK;
1906 WERROR ignore;
1907 NTSTATUS status;
1908 TALLOC_CTX *tmp_ctx;
1910 tmp_ctx = talloc_stackframe();
1911 if (tmp_ctx == NULL) {
1912 return WERR_NOMEM;
1915 path = winreg_printer_data_keyname(tmp_ctx, printer);
1916 if (path == NULL) {
1917 TALLOC_FREE(tmp_ctx);
1918 return WERR_NOMEM;
1921 ZERO_STRUCT(hive_hnd);
1922 ZERO_STRUCT(key_hnd);
1924 DEBUG(8, ("winreg_set_printer_dataex: Open printer key %s, value %s, access_mask: 0x%05x for [%s]\n",
1925 key, value, access_mask, printer));
1926 result = winreg_printer_openkey(tmp_ctx,
1927 winreg_handle,
1928 path,
1929 key,
1930 true,
1931 access_mask,
1932 &hive_hnd,
1933 &key_hnd);
1934 if (!W_ERROR_IS_OK(result)) {
1935 DEBUG(0, ("winreg_set_printer_dataex: Could not open key %s: %s\n",
1936 key, win_errstr(result)));
1937 goto done;
1940 wvalue.name = value;
1941 status = dcerpc_winreg_SetValue(winreg_handle,
1942 tmp_ctx,
1943 &key_hnd,
1944 wvalue,
1945 type,
1946 data,
1947 data_size,
1948 &result);
1949 if (!NT_STATUS_IS_OK(status)) {
1950 DEBUG(0, ("winreg_set_printer_dataex: Could not set value %s: %s\n",
1951 value, nt_errstr(status)));
1952 result = ntstatus_to_werror(status);
1955 done:
1956 if (is_valid_policy_hnd(&key_hnd)) {
1957 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
1959 if (is_valid_policy_hnd(&hive_hnd)) {
1960 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
1963 TALLOC_FREE(tmp_ctx);
1964 return result;
1967 /* Get printer data over a winreg pipe. */
1968 WERROR winreg_get_printer_dataex(TALLOC_CTX *mem_ctx,
1969 struct dcerpc_binding_handle *winreg_handle,
1970 const char *printer,
1971 const char *key,
1972 const char *value,
1973 enum winreg_Type *type,
1974 uint8_t **data,
1975 uint32_t *data_size)
1977 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1978 struct policy_handle hive_hnd, key_hnd;
1979 struct winreg_String wvalue;
1980 enum winreg_Type type_in = REG_NONE;
1981 char *path;
1982 uint8_t *data_in = NULL;
1983 uint32_t data_in_size = 0;
1984 uint32_t value_len = 0;
1985 WERROR result = WERR_OK;
1986 WERROR ignore;
1987 NTSTATUS status;
1988 TALLOC_CTX *tmp_ctx;
1990 tmp_ctx = talloc_stackframe();
1991 if (tmp_ctx == NULL) {
1992 return WERR_NOMEM;
1995 path = winreg_printer_data_keyname(tmp_ctx, printer);
1996 if (path == NULL) {
1997 TALLOC_FREE(tmp_ctx);
1998 return WERR_NOMEM;
2001 ZERO_STRUCT(hive_hnd);
2002 ZERO_STRUCT(key_hnd);
2004 result = winreg_printer_openkey(tmp_ctx,
2005 winreg_handle,
2006 path,
2007 key,
2008 false,
2009 access_mask,
2010 &hive_hnd,
2011 &key_hnd);
2012 if (!W_ERROR_IS_OK(result)) {
2013 DEBUG(2, ("winreg_get_printer_dataex: Could not open key %s: %s\n",
2014 key, win_errstr(result)));
2015 goto done;
2018 wvalue.name = value;
2021 * call QueryValue once with data == NULL to get the
2022 * needed memory size to be allocated, then allocate
2023 * data buffer and call again.
2025 status = dcerpc_winreg_QueryValue(winreg_handle,
2026 tmp_ctx,
2027 &key_hnd,
2028 &wvalue,
2029 &type_in,
2030 NULL,
2031 &data_in_size,
2032 &value_len,
2033 &result);
2034 if (!NT_STATUS_IS_OK(status)) {
2035 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2036 value, nt_errstr(status)));
2037 result = ntstatus_to_werror(status);
2038 goto done;
2040 if (!W_ERROR_IS_OK(result)) {
2041 DEBUG(2, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2042 value, win_errstr(result)));
2043 goto done;
2046 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
2047 if (data_in == NULL) {
2048 result = WERR_NOMEM;
2049 goto done;
2051 value_len = 0;
2053 status = dcerpc_winreg_QueryValue(winreg_handle,
2054 tmp_ctx,
2055 &key_hnd,
2056 &wvalue,
2057 &type_in,
2058 data_in,
2059 &data_in_size,
2060 &value_len,
2061 &result);
2062 if (!NT_STATUS_IS_OK(status)) {
2063 DEBUG(0, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2064 value, nt_errstr(status)));
2065 result = ntstatus_to_werror(status);
2066 goto done;
2068 if (!W_ERROR_IS_OK(result)) {
2069 DEBUG(2, ("winreg_get_printer_dataex: Could not query value %s: %s\n",
2070 value, win_errstr(result)));
2071 goto done;
2074 *type = type_in;
2075 *data_size = data_in_size;
2076 if (data_in_size) {
2077 *data = talloc_move(mem_ctx, &data_in);
2080 result = WERR_OK;
2081 done:
2082 if (is_valid_policy_hnd(&key_hnd)) {
2083 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2085 if (is_valid_policy_hnd(&hive_hnd)) {
2086 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2089 TALLOC_FREE(tmp_ctx);
2090 return result;
2093 /* Enumerate on the values of a given key and provide the data. */
2094 WERROR winreg_enum_printer_dataex(TALLOC_CTX *mem_ctx,
2095 struct dcerpc_binding_handle *winreg_handle,
2096 const char *printer,
2097 const char *key,
2098 uint32_t *pnum_values,
2099 struct spoolss_PrinterEnumValues **penum_values)
2101 uint32_t i;
2102 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2103 struct policy_handle hive_hnd, key_hnd;
2105 struct spoolss_PrinterEnumValues *enum_values = NULL;
2106 uint32_t num_values = 0;
2107 char *path;
2108 WERROR result = WERR_OK;
2109 WERROR ignore;
2110 NTSTATUS status;
2111 const char **enum_names = NULL;
2112 enum winreg_Type *enum_types = NULL;
2113 DATA_BLOB *enum_data_blobs = NULL;
2115 TALLOC_CTX *tmp_ctx;
2117 tmp_ctx = talloc_stackframe();
2118 if (tmp_ctx == NULL) {
2119 return WERR_NOMEM;
2122 path = winreg_printer_data_keyname(tmp_ctx, printer);
2123 if (path == NULL) {
2124 TALLOC_FREE(tmp_ctx);
2125 return WERR_NOMEM;
2128 result = winreg_printer_openkey(tmp_ctx,
2129 winreg_handle,
2130 path,
2131 key,
2132 false,
2133 access_mask,
2134 &hive_hnd,
2135 &key_hnd);
2136 if (!W_ERROR_IS_OK(result)) {
2137 DEBUG(2, ("winreg_enum_printer_dataex: Could not open key %s: %s\n",
2138 key, win_errstr(result)));
2139 goto done;
2142 status = dcerpc_winreg_enumvals(tmp_ctx,
2143 winreg_handle,
2144 &key_hnd,
2145 &num_values,
2146 &enum_names,
2147 &enum_types,
2148 &enum_data_blobs,
2149 &result);
2150 if (!NT_STATUS_IS_OK(status)){
2151 result = ntstatus_to_werror(status);
2154 if (!W_ERROR_IS_OK(result)) {
2155 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2156 key, win_errstr(result)));
2157 goto done;
2160 enum_values = talloc_array(tmp_ctx, struct spoolss_PrinterEnumValues, num_values);
2161 if (enum_values == NULL){
2162 result = WERR_NOMEM;
2163 DEBUG(0, ("winreg_enum_printer_dataex: Could not enumerate values in %s: %s\n",
2164 key, win_errstr(result)));
2165 goto done;
2168 for (i = 0; i < num_values; i++){
2169 enum_values[i].value_name = enum_names[i];
2170 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
2171 enum_values[i].type = enum_types[i];
2172 enum_values[i].data_length = enum_data_blobs[i].length;
2173 enum_values[i].data = NULL;
2175 if (enum_values[i].data_length != 0){
2176 enum_values[i].data = &enum_data_blobs[i];
2180 talloc_steal(enum_values, enum_names);
2181 talloc_steal(enum_values, enum_data_blobs);
2183 *pnum_values = num_values;
2184 if (penum_values) {
2185 *penum_values = talloc_move(mem_ctx, &enum_values);
2188 result = WERR_OK;
2189 done:
2190 if (is_valid_policy_hnd(&key_hnd)) {
2191 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2193 if (is_valid_policy_hnd(&hive_hnd)) {
2194 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2197 TALLOC_FREE(tmp_ctx);
2198 return result;
2201 /* Delete printer data over a winreg pipe. */
2202 WERROR winreg_delete_printer_dataex(TALLOC_CTX *mem_ctx,
2203 struct dcerpc_binding_handle *winreg_handle,
2204 const char *printer,
2205 const char *key,
2206 const char *value)
2208 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2209 struct policy_handle hive_hnd, key_hnd;
2210 struct winreg_String wvalue = { 0, };
2211 char *path;
2212 WERROR result = WERR_OK;
2213 WERROR ignore;
2214 NTSTATUS status;
2216 TALLOC_CTX *tmp_ctx;
2218 tmp_ctx = talloc_stackframe();
2219 if (tmp_ctx == NULL) {
2220 return WERR_NOMEM;
2223 path = winreg_printer_data_keyname(tmp_ctx, printer);
2224 if (path == NULL) {
2225 TALLOC_FREE(tmp_ctx);
2226 return WERR_NOMEM;
2229 ZERO_STRUCT(hive_hnd);
2230 ZERO_STRUCT(key_hnd);
2232 result = winreg_printer_openkey(tmp_ctx,
2233 winreg_handle,
2234 path,
2235 key,
2236 false,
2237 access_mask,
2238 &hive_hnd,
2239 &key_hnd);
2240 if (!W_ERROR_IS_OK(result)) {
2241 DEBUG(0, ("winreg_delete_printer_dataex: Could not open key %s: %s\n",
2242 key, win_errstr(result)));
2243 goto done;
2246 wvalue.name = value;
2247 status = dcerpc_winreg_DeleteValue(winreg_handle,
2248 tmp_ctx,
2249 &key_hnd,
2250 wvalue,
2251 &result);
2252 if (!NT_STATUS_IS_OK(status)) {
2253 DEBUG(0, ("winreg_delete_printer_dataex: Could not delete value %s: %s\n",
2254 value, nt_errstr(status)));
2255 result = ntstatus_to_werror(status);
2258 done:
2259 if (is_valid_policy_hnd(&key_hnd)) {
2260 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2262 if (is_valid_policy_hnd(&hive_hnd)) {
2263 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2266 TALLOC_FREE(tmp_ctx);
2267 return result;
2270 /* Enumerate on the subkeys of a given key and provide the data. */
2271 WERROR winreg_enum_printer_key(TALLOC_CTX *mem_ctx,
2272 struct dcerpc_binding_handle *winreg_handle,
2273 const char *printer,
2274 const char *key,
2275 uint32_t *pnum_subkeys,
2276 const char ***psubkeys)
2278 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2279 struct policy_handle hive_hnd, key_hnd;
2280 char *path;
2281 const char **subkeys = NULL;
2282 uint32_t num_subkeys = -1;
2284 WERROR result = WERR_OK;
2285 WERROR ignore;
2286 NTSTATUS status;
2288 TALLOC_CTX *tmp_ctx;
2290 tmp_ctx = talloc_stackframe();
2291 if (tmp_ctx == NULL) {
2292 return WERR_NOMEM;
2295 path = winreg_printer_data_keyname(tmp_ctx, printer);
2296 if (path == NULL) {
2297 TALLOC_FREE(tmp_ctx);
2298 return WERR_NOMEM;
2301 ZERO_STRUCT(hive_hnd);
2302 ZERO_STRUCT(key_hnd);
2304 result = winreg_printer_openkey(tmp_ctx,
2305 winreg_handle,
2306 path,
2307 key,
2308 false,
2309 access_mask,
2310 &hive_hnd,
2311 &key_hnd);
2312 if (!W_ERROR_IS_OK(result)) {
2313 DEBUG(2, ("winreg_enum_printer_key: Could not open key %s: %s\n",
2314 key, win_errstr(result)));
2315 goto done;
2318 status = dcerpc_winreg_enum_keys(tmp_ctx,
2319 winreg_handle,
2320 &key_hnd,
2321 &num_subkeys,
2322 &subkeys,
2323 &result);
2324 if (!NT_STATUS_IS_OK(status)) {
2325 result = ntstatus_to_werror(status);
2327 if (!W_ERROR_IS_OK(result)) {
2328 DEBUG(0, ("winreg_enum_printer_key: Could not enumerate subkeys in %s: %s\n",
2329 key, win_errstr(result)));
2330 goto done;
2333 *pnum_subkeys = num_subkeys;
2334 if (psubkeys) {
2335 *psubkeys = talloc_move(mem_ctx, &subkeys);
2338 result = WERR_OK;
2339 done:
2340 if (is_valid_policy_hnd(&key_hnd)) {
2341 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2343 if (is_valid_policy_hnd(&hive_hnd)) {
2344 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2347 TALLOC_FREE(tmp_ctx);
2348 return result;
2351 /* Delete a key with subkeys of a given printer. */
2352 WERROR winreg_delete_printer_key(TALLOC_CTX *mem_ctx,
2353 struct dcerpc_binding_handle *winreg_handle,
2354 const char *printer,
2355 const char *key)
2357 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2358 struct policy_handle hive_hnd, key_hnd;
2359 char *keyname;
2360 char *path;
2361 WERROR result;
2362 WERROR ignore;
2363 NTSTATUS status;
2364 TALLOC_CTX *tmp_ctx;
2366 tmp_ctx = talloc_stackframe();
2367 if (tmp_ctx == NULL) {
2368 return WERR_NOMEM;
2371 path = winreg_printer_data_keyname(tmp_ctx, printer);
2372 if (path == NULL) {
2373 TALLOC_FREE(tmp_ctx);
2374 return WERR_NOMEM;
2377 result = winreg_printer_openkey(tmp_ctx,
2378 winreg_handle,
2379 path,
2380 key,
2381 false,
2382 access_mask,
2383 &hive_hnd,
2384 &key_hnd);
2385 if (!W_ERROR_IS_OK(result)) {
2386 /* key doesn't exist */
2387 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2388 result = WERR_OK;
2389 goto done;
2392 DEBUG(0, ("winreg_delete_printer_key: Could not open key %s: %s\n",
2393 key, win_errstr(result)));
2394 goto done;
2397 if (is_valid_policy_hnd(&key_hnd)) {
2398 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
2401 if (key == NULL || key[0] == '\0') {
2402 keyname = path;
2403 } else {
2404 keyname = talloc_asprintf(tmp_ctx,
2405 "%s\\%s",
2406 path,
2407 key);
2408 if (keyname == NULL) {
2409 result = WERR_NOMEM;
2410 goto done;
2414 status = dcerpc_winreg_delete_subkeys_recursive(tmp_ctx,
2415 winreg_handle,
2416 &hive_hnd,
2417 access_mask,
2418 keyname,
2419 &result);
2421 if (!NT_STATUS_IS_OK(status)) {
2422 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2423 key, nt_errstr(status)));
2424 result = ntstatus_to_werror(status);
2425 goto done;
2428 if (!W_ERROR_IS_OK(result)) {
2429 DEBUG(0, ("winreg_delete_printer_key: Could not delete key %s: %s\n",
2430 key, win_errstr(result)));
2431 goto done;
2434 done:
2435 if (is_valid_policy_hnd(&key_hnd)) {
2436 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2438 if (is_valid_policy_hnd(&hive_hnd)) {
2439 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2442 TALLOC_FREE(tmp_ctx);
2443 return result;
2446 WERROR winreg_printer_update_changeid(TALLOC_CTX *mem_ctx,
2447 struct dcerpc_binding_handle *winreg_handle,
2448 const char *printer)
2450 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2451 struct policy_handle hive_hnd, key_hnd;
2452 char *path;
2453 NTSTATUS status;
2454 WERROR result;
2455 TALLOC_CTX *tmp_ctx;
2457 tmp_ctx = talloc_stackframe();
2458 if (tmp_ctx == NULL) {
2459 return WERR_NOMEM;
2462 path = winreg_printer_data_keyname(tmp_ctx, printer);
2463 if (path == NULL) {
2464 TALLOC_FREE(tmp_ctx);
2465 return WERR_NOMEM;
2468 ZERO_STRUCT(hive_hnd);
2469 ZERO_STRUCT(key_hnd);
2471 result = winreg_printer_openkey(tmp_ctx,
2472 winreg_handle,
2473 path,
2475 false,
2476 access_mask,
2477 &hive_hnd,
2478 &key_hnd);
2479 if (!W_ERROR_IS_OK(result)) {
2480 DEBUG(0, ("winreg_printer_update_changeid: Could not open key %s: %s\n",
2481 path, win_errstr(result)));
2482 goto done;
2485 status = dcerpc_winreg_set_dword(tmp_ctx,
2486 winreg_handle,
2487 &key_hnd,
2488 "ChangeID",
2489 winreg_printer_rev_changeid(),
2490 &result);
2491 if (!NT_STATUS_IS_OK(status)) {
2492 result = ntstatus_to_werror(status);
2494 if (!W_ERROR_IS_OK(result)) {
2495 goto done;
2498 result = WERR_OK;
2499 done:
2500 if (winreg_handle != NULL) {
2501 WERROR ignore;
2503 if (is_valid_policy_hnd(&key_hnd)) {
2504 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2506 if (is_valid_policy_hnd(&hive_hnd)) {
2507 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2511 TALLOC_FREE(tmp_ctx);
2512 return result;
2515 WERROR winreg_printer_get_changeid(TALLOC_CTX *mem_ctx,
2516 struct dcerpc_binding_handle *winreg_handle,
2517 const char *printer,
2518 uint32_t *pchangeid)
2520 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2521 struct policy_handle hive_hnd, key_hnd;
2522 uint32_t changeid = 0;
2523 char *path;
2524 NTSTATUS status;
2525 WERROR result;
2526 WERROR ignore;
2527 TALLOC_CTX *tmp_ctx;
2529 tmp_ctx = talloc_stackframe();
2530 if (tmp_ctx == NULL) {
2531 return WERR_NOMEM;
2534 path = winreg_printer_data_keyname(tmp_ctx, printer);
2535 if (path == NULL) {
2536 TALLOC_FREE(tmp_ctx);
2537 return WERR_NOMEM;
2540 ZERO_STRUCT(hive_hnd);
2541 ZERO_STRUCT(key_hnd);
2543 result = winreg_printer_openkey(tmp_ctx,
2544 winreg_handle,
2545 path,
2547 false,
2548 access_mask,
2549 &hive_hnd,
2550 &key_hnd);
2551 if (!W_ERROR_IS_OK(result)) {
2552 DEBUG(2, ("winreg_printer_get_changeid: Could not open key %s: %s\n",
2553 path, win_errstr(result)));
2554 goto done;
2557 DEBUG(10, ("winreg_printer_get_changeid: get changeid from %s\n", path));
2559 status = dcerpc_winreg_query_dword(tmp_ctx,
2560 winreg_handle,
2561 &key_hnd,
2562 "ChangeID",
2563 &changeid,
2564 &result);
2565 if (!NT_STATUS_IS_OK(status)) {
2566 result = ntstatus_to_werror(status);
2568 if (!W_ERROR_IS_OK(result)) {
2569 goto done;
2572 if (pchangeid) {
2573 *pchangeid = changeid;
2576 result = WERR_OK;
2577 done:
2578 if (is_valid_policy_hnd(&key_hnd)) {
2579 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2581 if (is_valid_policy_hnd(&hive_hnd)) {
2582 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2585 TALLOC_FREE(tmp_ctx);
2586 return result;
2590 * The special behaviour of the spoolss forms is documented at the website:
2592 * Managing Win32 Printserver Forms
2593 * http://unixwiz.net/techtips/winspooler-forms.html
2596 WERROR winreg_printer_addform1(TALLOC_CTX *mem_ctx,
2597 struct dcerpc_binding_handle *winreg_handle,
2598 struct spoolss_AddFormInfo1 *form)
2600 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2601 struct policy_handle hive_hnd, key_hnd;
2602 struct winreg_String wvalue = { 0, };
2603 DATA_BLOB blob;
2604 uint32_t num_info = 0;
2605 union spoolss_FormInfo *info = NULL;
2606 uint32_t i;
2607 WERROR result;
2608 NTSTATUS status;
2609 TALLOC_CTX *tmp_ctx;
2611 tmp_ctx = talloc_stackframe();
2612 if (tmp_ctx == NULL) {
2613 return WERR_NOMEM;
2616 ZERO_STRUCT(hive_hnd);
2617 ZERO_STRUCT(key_hnd);
2619 result = winreg_printer_openkey(tmp_ctx,
2620 winreg_handle,
2621 TOP_LEVEL_CONTROL_FORMS_KEY,
2623 true,
2624 access_mask,
2625 &hive_hnd,
2626 &key_hnd);
2627 if (!W_ERROR_IS_OK(result)) {
2628 DEBUG(0, ("winreg_printer_addform1: Could not open key %s: %s\n",
2629 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2630 goto done;
2633 result = winreg_printer_enumforms1(tmp_ctx, winreg_handle,
2634 &num_info, &info);
2635 if (!W_ERROR_IS_OK(result)) {
2636 DEBUG(0, ("winreg_printer_addform: Could not enum keys %s: %s\n",
2637 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2638 goto done;
2641 /* If form name already exists or is builtin return ALREADY_EXISTS */
2642 for (i = 0; i < num_info; i++) {
2643 if (strequal(info[i].info1.form_name, form->form_name)) {
2644 result = WERR_FILE_EXISTS;
2645 goto done;
2649 wvalue.name = form->form_name;
2651 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2652 SIVAL(blob.data, 0, form->size.width);
2653 SIVAL(blob.data, 4, form->size.height);
2654 SIVAL(blob.data, 8, form->area.left);
2655 SIVAL(blob.data, 12, form->area.top);
2656 SIVAL(blob.data, 16, form->area.right);
2657 SIVAL(blob.data, 20, form->area.bottom);
2658 SIVAL(blob.data, 24, num_info + 1); /* FIXME */
2659 SIVAL(blob.data, 28, form->flags);
2661 status = dcerpc_winreg_SetValue(winreg_handle,
2662 tmp_ctx,
2663 &key_hnd,
2664 wvalue,
2665 REG_BINARY,
2666 blob.data,
2667 blob.length,
2668 &result);
2669 if (!NT_STATUS_IS_OK(status)) {
2670 DEBUG(0, ("winreg_printer_addform1: Could not set value %s: %s\n",
2671 wvalue.name, nt_errstr(status)));
2672 result = ntstatus_to_werror(status);
2675 done:
2676 if (winreg_handle != NULL) {
2677 WERROR ignore;
2679 if (is_valid_policy_hnd(&key_hnd)) {
2680 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2682 if (is_valid_policy_hnd(&hive_hnd)) {
2683 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2687 TALLOC_FREE(info);
2688 TALLOC_FREE(tmp_ctx);
2689 return result;
2692 WERROR winreg_printer_enumforms1(TALLOC_CTX *mem_ctx,
2693 struct dcerpc_binding_handle *winreg_handle,
2694 uint32_t *pnum_info,
2695 union spoolss_FormInfo **pinfo)
2697 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2698 struct policy_handle hive_hnd, key_hnd;
2699 union spoolss_FormInfo *info;
2700 struct spoolss_PrinterEnumValues *enum_values = NULL;
2701 uint32_t num_values = 0;
2702 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2703 uint32_t i;
2704 WERROR result;
2705 NTSTATUS status;
2706 const char **enum_names = NULL;
2707 enum winreg_Type *enum_types = NULL;
2708 DATA_BLOB *enum_data_blobs = NULL;
2709 TALLOC_CTX *tmp_ctx;
2711 tmp_ctx = talloc_stackframe();
2712 if (tmp_ctx == NULL) {
2713 return WERR_NOMEM;
2716 ZERO_STRUCT(hive_hnd);
2717 ZERO_STRUCT(key_hnd);
2719 result = winreg_printer_openkey(tmp_ctx,
2720 winreg_handle,
2721 TOP_LEVEL_CONTROL_FORMS_KEY,
2723 true,
2724 access_mask,
2725 &hive_hnd,
2726 &key_hnd);
2727 if (!W_ERROR_IS_OK(result)) {
2728 /* key doesn't exist */
2729 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2730 result = WERR_OK;
2731 goto done;
2734 DEBUG(0, ("winreg_printer_enumforms1: Could not open key %s: %s\n",
2735 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2736 goto done;
2739 status = dcerpc_winreg_enumvals(tmp_ctx,
2740 winreg_handle,
2741 &key_hnd,
2742 &num_values,
2743 &enum_names,
2744 &enum_types,
2745 &enum_data_blobs,
2746 &result);
2747 if (!NT_STATUS_IS_OK(status)){
2748 result = ntstatus_to_werror(status);
2751 if (!W_ERROR_IS_OK(result)) {
2752 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2753 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2754 goto done;
2757 enum_values = talloc_zero_array(tmp_ctx,
2758 struct spoolss_PrinterEnumValues,
2759 num_values);
2760 if (enum_values == NULL){
2761 result = WERR_NOMEM;
2762 goto done;
2765 for (i = 0; i < num_values; i++){
2766 enum_values[i].value_name = enum_names[i];
2767 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
2768 enum_values[i].type = enum_types[i];
2769 enum_values[i].data_length = enum_data_blobs[i].length;
2770 enum_values[i].data = NULL;
2771 if (enum_values[i].data_length != 0){
2772 enum_values[i].data = &enum_data_blobs[i];
2776 if (!W_ERROR_IS_OK(result)) {
2777 DEBUG(0, ("winreg_printer_enumforms1: Could not enumerate values in %s: %s\n",
2778 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2779 goto done;
2782 info = talloc_array(tmp_ctx, union spoolss_FormInfo, num_builtin + num_values);
2783 if (info == NULL) {
2784 result = WERR_NOMEM;
2785 goto done;
2788 /* Enumerate BUILTIN forms */
2789 for (i = 0; i < num_builtin; i++) {
2790 info[i].info1 = builtin_forms1[i];
2793 /* Enumerate registry forms */
2794 for (i = 0; i < num_values; i++) {
2795 union spoolss_FormInfo val;
2797 if (enum_values[i].type != REG_BINARY ||
2798 enum_values[i].data_length != 32) {
2799 continue;
2802 val.info1.form_name = talloc_strdup(info, enum_values[i].value_name);
2803 if (val.info1.form_name == NULL) {
2804 result = WERR_NOMEM;
2805 goto done;
2808 val.info1.size.width = IVAL(enum_values[i].data->data, 0);
2809 val.info1.size.height = IVAL(enum_values[i].data->data, 4);
2810 val.info1.area.left = IVAL(enum_values[i].data->data, 8);
2811 val.info1.area.top = IVAL(enum_values[i].data->data, 12);
2812 val.info1.area.right = IVAL(enum_values[i].data->data, 16);
2813 val.info1.area.bottom = IVAL(enum_values[i].data->data, 20);
2814 /* skip form index IVAL(enum_values[i].data->data, 24)));*/
2815 val.info1.flags = (enum spoolss_FormFlags) IVAL(enum_values[i].data->data, 28);
2817 info[i + num_builtin] = val;
2820 *pnum_info = num_builtin + num_values;
2821 if (pinfo) {
2822 *pinfo = talloc_move(mem_ctx, &info);
2825 done:
2826 if (winreg_handle != NULL) {
2827 WERROR ignore;
2829 if (is_valid_policy_hnd(&key_hnd)) {
2830 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2832 if (is_valid_policy_hnd(&hive_hnd)) {
2833 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2837 TALLOC_FREE(enum_values);
2838 TALLOC_FREE(tmp_ctx);
2839 return result;
2842 WERROR winreg_printer_deleteform1(TALLOC_CTX *mem_ctx,
2843 struct dcerpc_binding_handle *winreg_handle,
2844 const char *form_name)
2846 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2847 struct policy_handle hive_hnd, key_hnd;
2848 struct winreg_String wvalue = { 0, };
2849 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2850 uint32_t i;
2851 WERROR result = WERR_OK;
2852 WERROR ignore;
2853 NTSTATUS status;
2854 TALLOC_CTX *tmp_ctx;
2856 for (i = 0; i < num_builtin; i++) {
2857 if (strequal(builtin_forms1[i].form_name, form_name)) {
2858 return WERR_INVALID_PARAMETER;
2862 tmp_ctx = talloc_stackframe();
2863 if (tmp_ctx == NULL) {
2864 return WERR_NOMEM;
2867 ZERO_STRUCT(hive_hnd);
2868 ZERO_STRUCT(key_hnd);
2870 result = winreg_printer_openkey(tmp_ctx,
2871 winreg_handle,
2872 TOP_LEVEL_CONTROL_FORMS_KEY,
2874 false,
2875 access_mask,
2876 &hive_hnd,
2877 &key_hnd);
2878 if (!W_ERROR_IS_OK(result)) {
2879 DEBUG(0, ("winreg_printer_deleteform1: Could not open key %s: %s\n",
2880 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2881 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2882 result = WERR_INVALID_FORM_NAME;
2884 goto done;
2887 wvalue.name = form_name;
2888 status = dcerpc_winreg_DeleteValue(winreg_handle,
2889 tmp_ctx,
2890 &key_hnd,
2891 wvalue,
2892 &result);
2893 if (!NT_STATUS_IS_OK(status)) {
2894 /* If the value doesn't exist, return WERR_INVALID_FORM_NAME */
2895 DEBUG(0, ("winreg_printer_delteform1: Could not delete value %s: %s\n",
2896 wvalue.name, nt_errstr(status)));
2897 result = ntstatus_to_werror(status);
2898 goto done;
2901 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
2902 result = WERR_INVALID_FORM_NAME;
2905 done:
2906 if (is_valid_policy_hnd(&key_hnd)) {
2907 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
2909 if (is_valid_policy_hnd(&hive_hnd)) {
2910 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
2913 TALLOC_FREE(tmp_ctx);
2914 return result;
2917 WERROR winreg_printer_setform1(TALLOC_CTX *mem_ctx,
2918 struct dcerpc_binding_handle *winreg_handle,
2919 const char *form_name,
2920 struct spoolss_AddFormInfo1 *form)
2922 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2923 struct policy_handle hive_hnd, key_hnd;
2924 struct winreg_String wvalue = { 0, };
2925 DATA_BLOB blob;
2926 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
2927 uint32_t i;
2928 WERROR result;
2929 NTSTATUS status;
2930 TALLOC_CTX *tmp_ctx = NULL;
2932 for (i = 0; i < num_builtin; i++) {
2933 if (strequal(builtin_forms1[i].form_name, form->form_name)) {
2934 result = WERR_INVALID_PARAM;
2935 goto done;
2939 tmp_ctx = talloc_stackframe();
2940 if (tmp_ctx == NULL) {
2941 return WERR_NOMEM;
2944 ZERO_STRUCT(hive_hnd);
2945 ZERO_STRUCT(key_hnd);
2947 result = winreg_printer_openkey(tmp_ctx,
2948 winreg_handle,
2949 TOP_LEVEL_CONTROL_FORMS_KEY,
2951 true,
2952 access_mask,
2953 &hive_hnd,
2954 &key_hnd);
2955 if (!W_ERROR_IS_OK(result)) {
2956 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2957 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2958 goto done;
2961 /* If form_name != form->form_name then we renamed the form */
2962 if (strequal(form_name, form->form_name)) {
2963 result = winreg_printer_deleteform1(tmp_ctx, winreg_handle,
2964 form_name);
2965 if (!W_ERROR_IS_OK(result)) {
2966 DEBUG(0, ("winreg_printer_setform1: Could not open key %s: %s\n",
2967 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
2968 goto done;
2972 wvalue.name = form->form_name;
2974 blob = data_blob_talloc(tmp_ctx, NULL, 32);
2975 SIVAL(blob.data, 0, form->size.width);
2976 SIVAL(blob.data, 4, form->size.height);
2977 SIVAL(blob.data, 8, form->area.left);
2978 SIVAL(blob.data, 12, form->area.top);
2979 SIVAL(blob.data, 16, form->area.right);
2980 SIVAL(blob.data, 20, form->area.bottom);
2981 SIVAL(blob.data, 24, 42);
2982 SIVAL(blob.data, 28, form->flags);
2984 status = dcerpc_winreg_SetValue(winreg_handle,
2985 tmp_ctx,
2986 &key_hnd,
2987 wvalue,
2988 REG_BINARY,
2989 blob.data,
2990 blob.length,
2991 &result);
2992 if (!NT_STATUS_IS_OK(status)) {
2993 DEBUG(0, ("winreg_printer_setform1: Could not set value %s: %s\n",
2994 wvalue.name, nt_errstr(status)));
2995 result = ntstatus_to_werror(status);
2998 done:
2999 if (winreg_handle != NULL) {
3000 WERROR ignore;
3002 if (is_valid_policy_hnd(&key_hnd)) {
3003 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3005 if (is_valid_policy_hnd(&hive_hnd)) {
3006 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3010 TALLOC_FREE(tmp_ctx);
3011 return result;
3014 WERROR winreg_printer_getform1(TALLOC_CTX *mem_ctx,
3015 struct dcerpc_binding_handle *winreg_handle,
3016 const char *form_name,
3017 struct spoolss_FormInfo1 *r)
3019 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3020 struct policy_handle hive_hnd, key_hnd;
3021 struct winreg_String wvalue;
3022 enum winreg_Type type_in = REG_NONE;
3023 uint8_t *data_in = NULL;
3024 uint32_t data_in_size = 0;
3025 uint32_t value_len = 0;
3026 uint32_t num_builtin = ARRAY_SIZE(builtin_forms1);
3027 uint32_t i;
3028 WERROR result;
3029 WERROR ignore;
3030 NTSTATUS status;
3031 TALLOC_CTX *tmp_ctx;
3033 /* check builtin forms first */
3034 for (i = 0; i < num_builtin; i++) {
3035 if (strequal(builtin_forms1[i].form_name, form_name)) {
3036 *r = builtin_forms1[i];
3037 return WERR_OK;
3041 tmp_ctx = talloc_stackframe();
3042 if (tmp_ctx == NULL) {
3043 return WERR_NOMEM;
3046 ZERO_STRUCT(hive_hnd);
3047 ZERO_STRUCT(key_hnd);
3049 result = winreg_printer_openkey(tmp_ctx,
3050 winreg_handle,
3051 TOP_LEVEL_CONTROL_FORMS_KEY,
3053 true,
3054 access_mask,
3055 &hive_hnd,
3056 &key_hnd);
3057 if (!W_ERROR_IS_OK(result)) {
3058 DEBUG(2, ("winreg_printer_getform1: Could not open key %s: %s\n",
3059 TOP_LEVEL_CONTROL_FORMS_KEY, win_errstr(result)));
3060 goto done;
3063 wvalue.name = form_name;
3066 * call QueryValue once with data == NULL to get the
3067 * needed memory size to be allocated, then allocate
3068 * data buffer and call again.
3070 status = dcerpc_winreg_QueryValue(winreg_handle,
3071 tmp_ctx,
3072 &key_hnd,
3073 &wvalue,
3074 &type_in,
3075 NULL,
3076 &data_in_size,
3077 &value_len,
3078 &result);
3079 if (!NT_STATUS_IS_OK(status)) {
3080 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3081 wvalue.name, nt_errstr(status)));
3082 result = ntstatus_to_werror(status);
3083 goto done;
3085 if (!W_ERROR_IS_OK(result)) {
3086 goto done;
3089 data_in = (uint8_t *) TALLOC(tmp_ctx, data_in_size);
3090 if (data_in == NULL) {
3091 result = WERR_NOMEM;
3092 goto done;
3094 value_len = 0;
3096 status = dcerpc_winreg_QueryValue(winreg_handle,
3097 tmp_ctx,
3098 &key_hnd,
3099 &wvalue,
3100 &type_in,
3101 data_in,
3102 &data_in_size,
3103 &value_len,
3104 &result);
3105 if (!NT_STATUS_IS_OK(status)) {
3106 DEBUG(0, ("winreg_printer_getform1: Could not query value %s: %s\n",
3107 wvalue.name, nt_errstr(status)));
3108 result = ntstatus_to_werror(status);
3109 goto done;
3111 if (!W_ERROR_IS_OK(result)) {
3112 goto done;
3115 r->form_name = talloc_strdup(mem_ctx, form_name);
3116 if (r->form_name == NULL) {
3117 result = WERR_NOMEM;
3118 goto done;
3121 r->size.width = IVAL(data_in, 0);
3122 r->size.height = IVAL(data_in, 4);
3123 r->area.left = IVAL(data_in, 8);
3124 r->area.top = IVAL(data_in, 12);
3125 r->area.right = IVAL(data_in, 16);
3126 r->area.bottom = IVAL(data_in, 20);
3127 /* skip index IVAL(data_in, 24)));*/
3128 r->flags = (enum spoolss_FormFlags) IVAL(data_in, 28);
3130 result = WERR_OK;
3131 done:
3132 if (is_valid_policy_hnd(&key_hnd)) {
3133 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3135 if (is_valid_policy_hnd(&hive_hnd)) {
3136 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3139 TALLOC_FREE(tmp_ctx);
3140 return result;
3143 WERROR winreg_add_driver(TALLOC_CTX *mem_ctx,
3144 struct dcerpc_binding_handle *winreg_handle,
3145 struct spoolss_AddDriverInfoCtr *r,
3146 const char **driver_name,
3147 uint32_t *driver_version)
3149 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3150 struct policy_handle hive_hnd, key_hnd;
3151 struct spoolss_DriverInfo8 info8;
3152 TALLOC_CTX *tmp_ctx = NULL;
3153 NTSTATUS status;
3154 WERROR result;
3156 ZERO_STRUCT(hive_hnd);
3157 ZERO_STRUCT(key_hnd);
3158 ZERO_STRUCT(info8);
3160 if (!driver_info_ctr_to_info8(r, &info8)) {
3161 result = WERR_INVALID_PARAMETER;
3162 goto done;
3165 tmp_ctx = talloc_stackframe();
3166 if (tmp_ctx == NULL) {
3167 return WERR_NOMEM;
3170 result = winreg_printer_opendriver(tmp_ctx,
3171 winreg_handle,
3172 info8.driver_name,
3173 info8.architecture,
3174 info8.version,
3175 access_mask, true,
3176 &hive_hnd,
3177 &key_hnd);
3178 if (!W_ERROR_IS_OK(result)) {
3179 DEBUG(0, ("winreg_add_driver: "
3180 "Could not open driver key (%s,%s,%d): %s\n",
3181 info8.driver_name, info8.architecture,
3182 info8.version, win_errstr(result)));
3183 goto done;
3186 /* TODO: "Attributes" ? */
3188 status = dcerpc_winreg_set_dword(tmp_ctx,
3189 winreg_handle,
3190 &key_hnd,
3191 "Version",
3192 info8.version,
3193 &result);
3194 if (!NT_STATUS_IS_OK(status)) {
3195 result = ntstatus_to_werror(status);
3197 if (!W_ERROR_IS_OK(result)) {
3198 goto done;
3201 status = dcerpc_winreg_set_sz(tmp_ctx,
3202 winreg_handle,
3203 &key_hnd,
3204 "Driver",
3205 info8.driver_path,
3206 &result);
3207 if (!NT_STATUS_IS_OK(status)) {
3208 result = ntstatus_to_werror(status);
3210 if (!W_ERROR_IS_OK(result)) {
3211 goto done;
3214 status = dcerpc_winreg_set_sz(tmp_ctx,
3215 winreg_handle,
3216 &key_hnd,
3217 "Data File",
3218 info8.data_file,
3219 &result);
3220 if (!NT_STATUS_IS_OK(status)) {
3221 result = ntstatus_to_werror(status);
3223 if (!W_ERROR_IS_OK(result)) {
3224 goto done;
3227 status = dcerpc_winreg_set_sz(tmp_ctx,
3228 winreg_handle,
3229 &key_hnd,
3230 "Configuration File",
3231 info8.config_file,
3232 &result);
3233 if (!NT_STATUS_IS_OK(status)) {
3234 result = ntstatus_to_werror(status);
3236 if (!W_ERROR_IS_OK(result)) {
3237 goto done;
3240 status = dcerpc_winreg_set_sz(tmp_ctx,
3241 winreg_handle,
3242 &key_hnd,
3243 "Help File",
3244 info8.help_file,
3245 &result);
3246 if (!NT_STATUS_IS_OK(status)) {
3247 result = ntstatus_to_werror(status);
3249 if (!W_ERROR_IS_OK(result)) {
3250 goto done;
3253 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3254 winreg_handle,
3255 &key_hnd,
3256 "Dependent Files",
3257 info8.dependent_files,
3258 &result);
3259 if (!NT_STATUS_IS_OK(status)) {
3260 result = ntstatus_to_werror(status);
3262 if (!W_ERROR_IS_OK(result)) {
3263 goto done;
3266 status = dcerpc_winreg_set_sz(tmp_ctx,
3267 winreg_handle,
3268 &key_hnd,
3269 "Monitor",
3270 info8.monitor_name,
3271 &result);
3272 if (!NT_STATUS_IS_OK(status)) {
3273 result = ntstatus_to_werror(status);
3275 if (!W_ERROR_IS_OK(result)) {
3276 goto done;
3279 status = dcerpc_winreg_set_sz(tmp_ctx,
3280 winreg_handle,
3281 &key_hnd,
3282 "Datatype",
3283 info8.default_datatype,
3284 &result);
3285 if (!NT_STATUS_IS_OK(status)) {
3286 result = ntstatus_to_werror(status);
3288 if (!W_ERROR_IS_OK(result)) {
3289 goto done;
3292 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3293 winreg_handle,
3294 &key_hnd, "Previous Names",
3295 info8.previous_names,
3296 &result);
3297 if (!NT_STATUS_IS_OK(status)) {
3298 result = ntstatus_to_werror(status);
3300 if (!W_ERROR_IS_OK(result)) {
3301 goto done;
3304 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3305 &key_hnd, "DriverDate",
3306 info8.driver_date);
3307 if (!W_ERROR_IS_OK(result)) {
3308 goto done;
3311 result = winreg_printer_write_ver(tmp_ctx, winreg_handle,
3312 &key_hnd, "DriverVersion",
3313 info8.driver_version);
3314 if (!W_ERROR_IS_OK(result)) {
3315 goto done;
3318 status = dcerpc_winreg_set_sz(tmp_ctx,
3319 winreg_handle,
3320 &key_hnd,
3321 "Manufacturer",
3322 info8.manufacturer_name,
3323 &result);
3324 if (!NT_STATUS_IS_OK(status)) {
3325 result = ntstatus_to_werror(status);
3327 if (!W_ERROR_IS_OK(result)) {
3328 goto done;
3331 status = dcerpc_winreg_set_sz(tmp_ctx,
3332 winreg_handle,
3333 &key_hnd,
3334 "OEM URL",
3335 info8.manufacturer_url,
3336 &result);
3337 if (!NT_STATUS_IS_OK(status)) {
3338 result = ntstatus_to_werror(status);
3340 if (!W_ERROR_IS_OK(result)) {
3341 goto done;
3344 status = dcerpc_winreg_set_sz(tmp_ctx,
3345 winreg_handle,
3346 &key_hnd,
3347 "HardwareID",
3348 info8.hardware_id,
3349 &result);
3350 if (!NT_STATUS_IS_OK(status)) {
3351 result = ntstatus_to_werror(status);
3353 if (!W_ERROR_IS_OK(result)) {
3354 goto done;
3357 status = dcerpc_winreg_set_sz(tmp_ctx,
3358 winreg_handle,
3359 &key_hnd,
3360 "Provider",
3361 info8.provider,
3362 &result);
3363 if (!NT_STATUS_IS_OK(status)) {
3364 result = ntstatus_to_werror(status);
3366 if (!W_ERROR_IS_OK(result)) {
3367 goto done;
3370 status = dcerpc_winreg_set_sz(tmp_ctx,
3371 winreg_handle,
3372 &key_hnd,
3373 "Print Processor",
3374 info8.print_processor,
3375 &result);
3376 if (!NT_STATUS_IS_OK(status)) {
3377 result = ntstatus_to_werror(status);
3379 if (!W_ERROR_IS_OK(result)) {
3380 goto done;
3383 status = dcerpc_winreg_set_sz(tmp_ctx,
3384 winreg_handle,
3385 &key_hnd,
3386 "VendorSetup",
3387 info8.vendor_setup,
3388 &result);
3389 if (!NT_STATUS_IS_OK(status)) {
3390 result = ntstatus_to_werror(status);
3392 if (!W_ERROR_IS_OK(result)) {
3393 goto done;
3396 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3397 winreg_handle,
3398 &key_hnd,
3399 "Color Profiles",
3400 info8.color_profiles,
3401 &result);
3402 if (!NT_STATUS_IS_OK(status)) {
3403 result = ntstatus_to_werror(status);
3405 if (!W_ERROR_IS_OK(result)) {
3406 goto done;
3409 status = dcerpc_winreg_set_sz(tmp_ctx,
3410 winreg_handle,
3411 &key_hnd,
3412 "InfPath",
3413 info8.inf_path,
3414 &result);
3415 if (!NT_STATUS_IS_OK(status)) {
3416 result = ntstatus_to_werror(status);
3418 if (!W_ERROR_IS_OK(result)) {
3419 goto done;
3422 status = dcerpc_winreg_set_dword(tmp_ctx,
3423 winreg_handle,
3424 &key_hnd,
3425 "PrinterDriverAttributes",
3426 info8.printer_driver_attributes,
3427 &result);
3428 if (!NT_STATUS_IS_OK(status)) {
3429 result = ntstatus_to_werror(status);
3431 if (!W_ERROR_IS_OK(result)) {
3432 goto done;
3435 status = dcerpc_winreg_set_multi_sz(tmp_ctx,
3436 winreg_handle,
3437 &key_hnd,
3438 "CoreDependencies",
3439 info8.core_driver_dependencies,
3440 &result);
3441 if (!NT_STATUS_IS_OK(status)) {
3442 result = ntstatus_to_werror(status);
3444 if (!W_ERROR_IS_OK(result)) {
3445 goto done;
3448 result = winreg_printer_write_date(tmp_ctx, winreg_handle,
3449 &key_hnd, "MinInboxDriverVerDate",
3450 info8.min_inbox_driver_ver_date);
3451 if (!W_ERROR_IS_OK(result)) {
3452 goto done;
3455 result = winreg_printer_write_ver(tmp_ctx, winreg_handle, &key_hnd,
3456 "MinInboxDriverVerVersion",
3457 info8.min_inbox_driver_ver_version);
3458 if (!W_ERROR_IS_OK(result)) {
3459 goto done;
3462 *driver_name = info8.driver_name;
3463 *driver_version = info8.version;
3464 result = WERR_OK;
3465 done:
3466 if (winreg_handle != NULL) {
3467 WERROR ignore;
3469 if (is_valid_policy_hnd(&key_hnd)) {
3470 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3472 if (is_valid_policy_hnd(&hive_hnd)) {
3473 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3477 TALLOC_FREE(tmp_ctx);
3478 return result;
3481 WERROR winreg_get_driver(TALLOC_CTX *mem_ctx,
3482 struct dcerpc_binding_handle *winreg_handle,
3483 const char *architecture,
3484 const char *driver_name,
3485 uint32_t driver_version,
3486 struct spoolss_DriverInfo8 **_info8)
3488 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3489 struct policy_handle hive_hnd, key_hnd;
3490 struct spoolss_DriverInfo8 i8, *info8;
3491 struct spoolss_PrinterEnumValues *enum_values = NULL;
3492 struct spoolss_PrinterEnumValues *v;
3493 uint32_t num_values = 0;
3494 TALLOC_CTX *tmp_ctx;
3495 WERROR result;
3496 NTSTATUS status;
3497 uint32_t i;
3498 const char **enum_names = NULL;
3499 enum winreg_Type *enum_types = NULL;
3500 DATA_BLOB *enum_data_blobs = NULL;
3502 ZERO_STRUCT(hive_hnd);
3503 ZERO_STRUCT(key_hnd);
3504 ZERO_STRUCT(i8);
3506 tmp_ctx = talloc_stackframe();
3507 if (tmp_ctx == NULL) {
3508 return WERR_NOMEM;
3511 if (driver_version == DRIVER_ANY_VERSION) {
3512 /* look for Win2k first and then for NT4 */
3513 result = winreg_printer_opendriver(tmp_ctx,
3514 winreg_handle,
3515 driver_name,
3516 architecture,
3518 access_mask, false,
3519 &hive_hnd,
3520 &key_hnd);
3521 if (!W_ERROR_IS_OK(result)) {
3522 result = winreg_printer_opendriver(tmp_ctx,
3523 winreg_handle,
3524 driver_name,
3525 architecture,
3527 access_mask, false,
3528 &hive_hnd,
3529 &key_hnd);
3531 } else {
3532 /* ok normal case */
3533 result = winreg_printer_opendriver(tmp_ctx,
3534 winreg_handle,
3535 driver_name,
3536 architecture,
3537 driver_version,
3538 access_mask, false,
3539 &hive_hnd,
3540 &key_hnd);
3542 if (!W_ERROR_IS_OK(result)) {
3543 DEBUG(5, ("winreg_get_driver: "
3544 "Could not open driver key (%s,%s,%d): %s\n",
3545 driver_name, architecture,
3546 driver_version, win_errstr(result)));
3547 goto done;
3550 status = dcerpc_winreg_enumvals(tmp_ctx,
3551 winreg_handle,
3552 &key_hnd,
3553 &num_values,
3554 &enum_names,
3555 &enum_types,
3556 &enum_data_blobs,
3557 &result);
3558 if (!NT_STATUS_IS_OK(status)){
3559 result = ntstatus_to_werror(status);
3562 if (!W_ERROR_IS_OK(result)) {
3563 DEBUG(0, ("winreg_get_driver: "
3564 "Could not enumerate values for (%s,%s,%d): %s\n",
3565 driver_name, architecture,
3566 driver_version, win_errstr(result)));
3567 goto done;
3570 enum_values = talloc_zero_array(tmp_ctx,
3571 struct spoolss_PrinterEnumValues,
3572 num_values);
3573 if (enum_values == NULL){
3574 result = WERR_NOMEM;
3575 goto done;
3578 for (i = 0; i < num_values; i++){
3579 enum_values[i].value_name = enum_names[i];
3580 enum_values[i].value_name_len = strlen_m_term(enum_names[i]) * 2;
3581 enum_values[i].type = enum_types[i];
3582 enum_values[i].data_length = enum_data_blobs[i].length;
3583 enum_values[i].data = NULL;
3584 if (enum_values[i].data_length != 0){
3585 enum_values[i].data = &enum_data_blobs[i];
3589 info8 = talloc_zero(tmp_ctx, struct spoolss_DriverInfo8);
3590 if (info8 == NULL) {
3591 result = WERR_NOMEM;
3592 goto done;
3595 info8->driver_name = talloc_strdup(info8, driver_name);
3596 if (info8->driver_name == NULL) {
3597 result = WERR_NOMEM;
3598 goto done;
3601 info8->architecture = talloc_strdup(info8, architecture);
3602 if (info8->architecture == NULL) {
3603 result = WERR_NOMEM;
3604 goto done;
3607 result = WERR_OK;
3609 for (i = 0; i < num_values; i++) {
3610 const char *tmp_str;
3611 uint32_t tmp = 0;
3613 v = &enum_values[i];
3615 result = winreg_enumval_to_dword(info8, v,
3616 "Version",
3617 &tmp);
3618 if (W_ERROR_IS_OK(result)) {
3619 info8->version = (enum spoolss_DriverOSVersion) tmp;
3621 CHECK_ERROR(result);
3623 result = winreg_enumval_to_sz(info8, v,
3624 "Driver",
3625 &info8->driver_path);
3626 CHECK_ERROR(result);
3628 result = winreg_enumval_to_sz(info8, v,
3629 "Data File",
3630 &info8->data_file);
3631 CHECK_ERROR(result);
3633 result = winreg_enumval_to_sz(info8, v,
3634 "Configuration File",
3635 &info8->config_file);
3636 CHECK_ERROR(result);
3638 result = winreg_enumval_to_sz(info8, v,
3639 "Help File",
3640 &info8->help_file);
3641 CHECK_ERROR(result);
3643 result = winreg_enumval_to_multi_sz(info8, v,
3644 "Dependent Files",
3645 &info8->dependent_files);
3646 CHECK_ERROR(result);
3648 result = winreg_enumval_to_sz(info8, v,
3649 "Monitor",
3650 &info8->monitor_name);
3651 CHECK_ERROR(result);
3653 result = winreg_enumval_to_sz(info8, v,
3654 "Datatype",
3655 &info8->default_datatype);
3656 CHECK_ERROR(result);
3658 result = winreg_enumval_to_multi_sz(info8, v,
3659 "Previous Names",
3660 &info8->previous_names);
3661 CHECK_ERROR(result);
3663 result = winreg_enumval_to_sz(info8, v,
3664 "DriverDate",
3665 &tmp_str);
3666 if (W_ERROR_IS_OK(result)) {
3667 result = winreg_printer_date_to_NTTIME(tmp_str,
3668 &info8->driver_date);
3670 CHECK_ERROR(result);
3672 result = winreg_enumval_to_sz(info8, v,
3673 "DriverVersion",
3674 &tmp_str);
3675 if (W_ERROR_IS_OK(result)) {
3676 result = winreg_printer_ver_to_dword(tmp_str,
3677 &info8->driver_version);
3679 CHECK_ERROR(result);
3681 result = winreg_enumval_to_sz(info8, v,
3682 "Manufacturer",
3683 &info8->manufacturer_name);
3684 CHECK_ERROR(result);
3686 result = winreg_enumval_to_sz(info8, v,
3687 "OEM URL",
3688 &info8->manufacturer_url);
3689 CHECK_ERROR(result);
3691 result = winreg_enumval_to_sz(info8, v,
3692 "HardwareID",
3693 &info8->hardware_id);
3694 CHECK_ERROR(result);
3696 result = winreg_enumval_to_sz(info8, v,
3697 "Provider",
3698 &info8->provider);
3699 CHECK_ERROR(result);
3701 result = winreg_enumval_to_sz(info8, v,
3702 "Print Processor",
3703 &info8->print_processor);
3704 CHECK_ERROR(result);
3706 result = winreg_enumval_to_sz(info8, v,
3707 "VendorSetup",
3708 &info8->vendor_setup);
3709 CHECK_ERROR(result);
3711 result = winreg_enumval_to_multi_sz(info8, v,
3712 "Color Profiles",
3713 &info8->color_profiles);
3714 CHECK_ERROR(result);
3716 result = winreg_enumval_to_sz(info8, v,
3717 "InfPath",
3718 &info8->inf_path);
3719 CHECK_ERROR(result);
3721 result = winreg_enumval_to_dword(info8, v,
3722 "PrinterDriverAttributes",
3723 &info8->printer_driver_attributes);
3724 CHECK_ERROR(result);
3726 result = winreg_enumval_to_multi_sz(info8, v,
3727 "CoreDependencies",
3728 &info8->core_driver_dependencies);
3729 CHECK_ERROR(result);
3731 result = winreg_enumval_to_sz(info8, v,
3732 "MinInboxDriverVerDate",
3733 &tmp_str);
3734 if (W_ERROR_IS_OK(result)) {
3735 result = winreg_printer_date_to_NTTIME(tmp_str,
3736 &info8->min_inbox_driver_ver_date);
3738 CHECK_ERROR(result);
3740 result = winreg_enumval_to_sz(info8, v,
3741 "MinInboxDriverVerVersion",
3742 &tmp_str);
3743 if (W_ERROR_IS_OK(result)) {
3744 result = winreg_printer_ver_to_dword(tmp_str,
3745 &info8->min_inbox_driver_ver_version);
3747 CHECK_ERROR(result);
3750 if (!W_ERROR_IS_OK(result)) {
3751 DEBUG(0, ("winreg_enumval_to_TYPE() failed "
3752 "for %s: %s\n", v->value_name,
3753 win_errstr(result)));
3754 goto done;
3757 *_info8 = talloc_steal(mem_ctx, info8);
3758 result = WERR_OK;
3759 done:
3760 if (winreg_handle != NULL) {
3761 WERROR ignore;
3763 if (is_valid_policy_hnd(&key_hnd)) {
3764 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3766 if (is_valid_policy_hnd(&hive_hnd)) {
3767 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3771 TALLOC_FREE(tmp_ctx);
3772 return result;
3775 WERROR winreg_del_driver(TALLOC_CTX *mem_ctx,
3776 struct dcerpc_binding_handle *winreg_handle,
3777 struct spoolss_DriverInfo8 *info8,
3778 uint32_t version)
3780 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3781 struct policy_handle hive_hnd, key_hnd;
3782 TALLOC_CTX *tmp_ctx;
3783 char *key_name;
3784 WERROR result;
3785 NTSTATUS status;
3787 ZERO_STRUCT(hive_hnd);
3788 ZERO_STRUCT(key_hnd);
3790 tmp_ctx = talloc_stackframe();
3791 if (tmp_ctx == NULL) {
3792 return WERR_NOMEM;
3795 /* test that the key exists */
3796 result = winreg_printer_opendriver(tmp_ctx,
3797 winreg_handle,
3798 info8->driver_name,
3799 info8->architecture,
3800 version,
3801 access_mask, false,
3802 &hive_hnd,
3803 &key_hnd);
3804 if (!W_ERROR_IS_OK(result)) {
3805 /* key doesn't exist */
3806 if (W_ERROR_EQUAL(result, WERR_BADFILE)) {
3807 result = WERR_OK;
3808 goto done;
3811 DEBUG(5, ("winreg_del_driver: "
3812 "Could not open driver (%s,%s,%u): %s\n",
3813 info8->driver_name, info8->architecture,
3814 version, win_errstr(result)));
3815 goto done;
3819 if (is_valid_policy_hnd(&key_hnd)) {
3820 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &result);
3823 key_name = talloc_asprintf(tmp_ctx,
3824 "%s\\Environments\\%s\\Drivers\\Version-%u\\%s",
3825 TOP_LEVEL_CONTROL_KEY,
3826 info8->architecture, version,
3827 info8->driver_name);
3828 if (key_name == NULL) {
3829 result = WERR_NOMEM;
3830 goto done;
3833 status = dcerpc_winreg_delete_subkeys_recursive(tmp_ctx,
3834 winreg_handle,
3835 &hive_hnd,
3836 access_mask,
3837 key_name,
3838 &result);
3840 if (!NT_STATUS_IS_OK(status)){
3841 DEBUG(0, ("winreg_del_driver: "
3842 "Could not open driver (%s,%s,%u): %s\n",
3843 info8->driver_name, info8->architecture,
3844 version, nt_errstr(status)));
3845 goto done;
3848 if (!W_ERROR_IS_OK(result)) {
3849 DEBUG(0, ("winreg_del_driver: "
3850 "Could not open driver (%s,%s,%u): %s\n",
3851 info8->driver_name, info8->architecture,
3852 version, win_errstr(result)));
3853 goto done;
3856 result = WERR_OK;
3857 done:
3858 if (winreg_handle != NULL) {
3859 WERROR ignore;
3861 if (is_valid_policy_hnd(&key_hnd)) {
3862 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3864 if (is_valid_policy_hnd(&hive_hnd)) {
3865 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3869 TALLOC_FREE(tmp_ctx);
3870 return result;
3873 WERROR winreg_get_driver_list(TALLOC_CTX *mem_ctx,
3874 struct dcerpc_binding_handle *winreg_handle,
3875 const char *architecture,
3876 uint32_t version,
3877 uint32_t *num_drivers,
3878 const char ***drivers_p)
3880 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
3881 struct policy_handle hive_hnd, key_hnd;
3882 const char **drivers;
3883 TALLOC_CTX *tmp_ctx;
3884 WERROR result;
3885 NTSTATUS status;
3887 *num_drivers = 0;
3888 *drivers_p = NULL;
3890 ZERO_STRUCT(hive_hnd);
3891 ZERO_STRUCT(key_hnd);
3893 tmp_ctx = talloc_stackframe();
3894 if (tmp_ctx == NULL) {
3895 return WERR_NOMEM;
3898 /* use NULL for the driver name so we open the key that is
3899 * parent of all drivers for this architecture and version */
3900 result = winreg_printer_opendriver(tmp_ctx,
3901 winreg_handle,
3902 NULL,
3903 architecture,
3904 version,
3905 access_mask, false,
3906 &hive_hnd,
3907 &key_hnd);
3908 if (!W_ERROR_IS_OK(result)) {
3909 DEBUG(5, ("winreg_get_driver_list: "
3910 "Could not open key (%s,%u): %s\n",
3911 architecture, version, win_errstr(result)));
3912 result = WERR_OK;
3913 goto done;
3916 status = dcerpc_winreg_enum_keys(tmp_ctx,
3917 winreg_handle,
3918 &key_hnd,
3919 num_drivers,
3920 &drivers,
3921 &result);
3922 if (!NT_STATUS_IS_OK(status)) {
3923 result = ntstatus_to_werror(status);
3925 if (!W_ERROR_IS_OK(result)) {
3926 DEBUG(0, ("winreg_get_driver_list: "
3927 "Could not enumerate drivers for (%s,%u): %s\n",
3928 architecture, version, win_errstr(result)));
3929 goto done;
3932 *drivers_p = talloc_steal(mem_ctx, drivers);
3934 result = WERR_OK;
3935 done:
3936 if (winreg_handle != NULL) {
3937 WERROR ignore;
3939 if (is_valid_policy_hnd(&key_hnd)) {
3940 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &key_hnd, &ignore);
3942 if (is_valid_policy_hnd(&hive_hnd)) {
3943 dcerpc_winreg_CloseKey(winreg_handle, tmp_ctx, &hive_hnd, &ignore);
3947 TALLOC_FREE(tmp_ctx);
3948 return result;