s3-printing: use winreg interface for migration, instead of spoolss.
[Samba/id10ts.git] / source3 / printing / nt_printing_migrate.c
blob5fd639e1b6b621bd38448c9359d186e3764d0a77
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
5 * Copyright (c) Andreas Schneider 2010.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "printing/nt_printing_migrate.h"
24 #include "rpc_client/rpc_client.h"
25 #include "librpc/gen_ndr/ndr_ntprinting.h"
26 #include "librpc/gen_ndr/ndr_spoolss_c.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "rpc_client/cli_winreg_spoolss.h"
30 NTSTATUS printing_tdb_migrate_form(TALLOC_CTX *mem_ctx,
31 struct rpc_pipe_client *pipe_hnd,
32 struct rpc_pipe_client *winreg_pipe,
33 const char *key_name,
34 unsigned char *data,
35 size_t length)
37 struct dcerpc_binding_handle *b = winreg_pipe->binding_handle;
38 enum ndr_err_code ndr_err;
39 struct ntprinting_form r;
40 struct spoolss_AddFormInfo1 f1;
41 DATA_BLOB blob;
42 WERROR result;
44 blob = data_blob_const(data, length);
46 ZERO_STRUCT(r);
48 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
49 (ndr_pull_flags_fn_t)ndr_pull_ntprinting_form);
50 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
51 DEBUG(2, ("Form pull failed: %s\n",
52 ndr_errstr(ndr_err)));
53 return NT_STATUS_NO_MEMORY;
56 /* Don't migrate builtin forms */
57 if (r.flag == SPOOLSS_FORM_BUILTIN) {
58 return NT_STATUS_OK;
61 DEBUG(2, ("Migrating Form: %s\n", key_name));
63 f1.form_name = key_name;
64 f1.flags = r.flag;
66 f1.size.width = r.width;
67 f1.size.height = r.length;
69 f1.area.top = r.top;
70 f1.area.right = r.right;
71 f1.area.bottom = r.bottom;
72 f1.area.left = r.left;
74 result = winreg_printer_addform1(mem_ctx,
76 &f1);
77 if (!W_ERROR_IS_OK(result)) {
78 return werror_to_ntstatus(result);
81 return NT_STATUS_OK;
84 NTSTATUS printing_tdb_migrate_driver(TALLOC_CTX *mem_ctx,
85 struct rpc_pipe_client *pipe_hnd,
86 struct rpc_pipe_client *winreg_pipe,
87 const char *key_name,
88 unsigned char *data,
89 size_t length)
91 struct dcerpc_binding_handle *b = winreg_pipe->binding_handle;
92 enum ndr_err_code ndr_err;
93 struct ntprinting_driver r;
94 struct spoolss_AddDriverInfoCtr d;
95 struct spoolss_AddDriverInfo3 d3;
96 struct spoolss_StringArray a;
97 DATA_BLOB blob;
98 WERROR result;
99 const char *driver_name;
100 uint32_t driver_version;
102 blob = data_blob_const(data, length);
104 ZERO_STRUCT(r);
106 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
107 (ndr_pull_flags_fn_t)ndr_pull_ntprinting_driver);
108 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
109 DEBUG(2, ("Driver pull failed: %s\n",
110 ndr_errstr(ndr_err)));
111 return NT_STATUS_NO_MEMORY;
114 DEBUG(2, ("Migrating Printer Driver: %s\n", key_name));
116 ZERO_STRUCT(d3);
117 ZERO_STRUCT(a);
119 a.string = r.dependent_files;
121 d3.architecture = r.environment;
122 d3.config_file = r.configfile;
123 d3.data_file = r.datafile;
124 d3.default_datatype = r.defaultdatatype;
125 d3.dependent_files = &a;
126 d3.driver_path = r.driverpath;
127 d3.help_file = r.helpfile;
128 d3.monitor_name = r.monitorname;
129 d3.driver_name = r.name;
130 d3.version = r.version;
132 d.level = 3;
133 d.info.info3 = &d3;
135 result = winreg_add_driver(mem_ctx,
138 &driver_name,
139 &driver_version);
140 if (!W_ERROR_IS_OK(result)) {
141 return werror_to_ntstatus(result);
144 return NT_STATUS_OK;
147 NTSTATUS printing_tdb_migrate_printer(TALLOC_CTX *mem_ctx,
148 struct rpc_pipe_client *pipe_hnd,
149 struct rpc_pipe_client *winreg_pipe,
150 const char *key_name,
151 unsigned char *data,
152 size_t length)
154 struct dcerpc_binding_handle *b = winreg_pipe->binding_handle;
155 enum ndr_err_code ndr_err;
156 struct ntprinting_printer r;
157 struct spoolss_SetPrinterInfo2 info2;
158 struct spoolss_DeviceMode dm;
159 struct spoolss_DevmodeContainer devmode_ctr;
160 struct sec_desc_buf secdesc_ctr;
161 DATA_BLOB blob;
162 NTSTATUS status;
163 WERROR result;
164 int j;
166 if (strequal(key_name, "printers")) {
167 return NT_STATUS_OK;
170 blob = data_blob_const(data, length);
172 ZERO_STRUCT(r);
174 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
175 (ndr_pull_flags_fn_t) ndr_pull_ntprinting_printer);
176 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
177 DEBUG(2, ("printer pull failed: %s\n",
178 ndr_errstr(ndr_err)));
179 return NT_STATUS_NO_MEMORY;
182 DEBUG(2, ("Migrating Printer: %s\n", key_name));
184 ZERO_STRUCT(devmode_ctr);
186 /* Create printer info level 2 */
187 ZERO_STRUCT(info2);
188 ZERO_STRUCT(secdesc_ctr);
190 info2.attributes = r.info.attributes;
191 info2.averageppm = r.info.averageppm;
192 info2.cjobs = r.info.cjobs;
193 info2.comment = r.info.comment;
194 info2.datatype = r.info.datatype;
195 info2.defaultpriority = r.info.default_priority;
196 info2.drivername = r.info.drivername;
197 info2.location = r.info.location;
198 info2.parameters = r.info.parameters;
199 info2.portname = r.info.portname;
200 info2.printername = r.info.printername;
201 info2.printprocessor = r.info.printprocessor;
202 info2.priority = r.info.priority;
203 info2.sepfile = r.info.sepfile;
204 info2.sharename = r.info.sharename;
205 info2.starttime = r.info.starttime;
206 info2.status = r.info.status;
207 info2.untiltime = r.info.untiltime;
209 /* Create Device Mode */
210 if (r.devmode != NULL) {
211 ZERO_STRUCT(dm);
213 dm.bitsperpel = r.devmode->bitsperpel;
214 dm.collate = r.devmode->collate;
215 dm.color = r.devmode->color;
216 dm.copies = r.devmode->copies;
217 dm.defaultsource = r.devmode->defaultsource;
218 dm.devicename = r.devmode->devicename;
219 dm.displayflags = r.devmode->displayflags;
220 dm.displayfrequency = r.devmode->displayfrequency;
221 dm.dithertype = r.devmode->dithertype;
222 dm.driverversion = r.devmode->driverversion;
223 dm.duplex = r.devmode->duplex;
224 dm.fields = r.devmode->fields;
225 dm.formname = r.devmode->formname;
226 dm.icmintent = r.devmode->icmintent;
227 dm.icmmethod = r.devmode->icmmethod;
228 dm.logpixels = r.devmode->logpixels;
229 dm.mediatype = r.devmode->mediatype;
230 dm.orientation = r.devmode->orientation;
231 dm.panningheight = r.devmode->pelsheight;
232 dm.panningwidth = r.devmode->panningwidth;
233 dm.paperlength = r.devmode->paperlength;
234 dm.papersize = r.devmode->papersize;
235 dm.paperwidth = r.devmode->paperwidth;
236 dm.pelsheight = r.devmode->pelsheight;
237 dm.pelswidth = r.devmode->pelswidth;
238 dm.printquality = r.devmode->printquality;
239 dm.size = r.devmode->size;
240 dm.scale = r.devmode->scale;
241 dm.specversion = r.devmode->specversion;
242 dm.ttoption = r.devmode->ttoption;
243 dm.yresolution = r.devmode->yresolution;
245 if (r.devmode->nt_dev_private != NULL) {
246 dm.driverextra_data.data = r.devmode->nt_dev_private->data;
247 dm.driverextra_data.length = r.devmode->nt_dev_private->length;
248 dm.__driverextra_length = r.devmode->nt_dev_private->length;
251 devmode_ctr.devmode = &dm;
253 info2.devmode_ptr = 1;
256 result = winreg_update_printer(mem_ctx, b,
257 key_name,
258 0, /// FIXME !!!!!!!!!!!!!!!!!!!!!!!
259 &info2,
260 &dm,
261 NULL);
262 if (!W_ERROR_IS_OK(result)) {
263 DEBUG(2, ("SetPrinter(%s) level 2 refused -- %s.\n",
264 key_name, win_errstr(result)));
265 status = werror_to_ntstatus(result);
266 goto done;
269 /* migrate printerdata */
270 for (j = 0; j < r.count; j++) {
271 char *valuename;
272 const char *keyname;
274 if (r.printer_data[j].type == REG_NONE) {
275 continue;
278 keyname = r.printer_data[j].name;
279 valuename = strchr(keyname, '\\');
280 if (valuename == NULL) {
281 continue;
282 } else {
283 valuename[0] = '\0';
284 valuename++;
287 result = winreg_set_printer_dataex(mem_ctx, b,
288 key_name,
289 keyname,
290 valuename,
291 r.printer_data[j].type,
292 r.printer_data[j].data.data,
293 r.printer_data[j].data.length);
294 if (!W_ERROR_IS_OK(result)) {
295 DEBUG(2, ("SetPrinterDataEx: printer [%s], keyname [%s], "
296 "valuename [%s] refused -- %s.\n",
297 key_name, keyname, valuename,
298 win_errstr(result)));
299 status = werror_to_ntstatus(result);
300 break;
304 done:
306 return status;
309 NTSTATUS printing_tdb_migrate_secdesc(TALLOC_CTX *mem_ctx,
310 struct rpc_pipe_client *pipe_hnd,
311 struct rpc_pipe_client *winreg_pipe,
312 const char *key_name,
313 unsigned char *data,
314 size_t length)
316 struct dcerpc_binding_handle *b = winreg_pipe->binding_handle;
317 enum ndr_err_code ndr_err;
318 struct sec_desc_buf secdesc_ctr;
319 DATA_BLOB blob;
320 WERROR result;
322 if (strequal(key_name, "printers")) {
323 return NT_STATUS_OK;
326 blob = data_blob_const(data, length);
328 ZERO_STRUCT(secdesc_ctr);
330 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &secdesc_ctr,
331 (ndr_pull_flags_fn_t)ndr_pull_sec_desc_buf);
332 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
333 DEBUG(2, ("security descriptor pull failed: %s\n",
334 ndr_errstr(ndr_err)));
335 return NT_STATUS_NO_MEMORY;
338 DEBUG(2, ("Migrating Security Descriptor: %s\n", key_name));
340 result = winreg_set_printer_secdesc(mem_ctx, b,
341 key_name,
342 secdesc_ctr.sd);
343 if (!W_ERROR_IS_OK(result)) {
344 return werror_to_ntstatus(result);
347 return NT_STATUS_OK;