s3: use monotonic clock for time deltas in namequery functions
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc_printer.c
blob589a569879776ee13d9707d237f8bdd6d58386a4
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2004,2009 Guenther Deschner (gd@samba.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include "utils/net.h"
21 #include "../librpc/gen_ndr/cli_spoolss.h"
22 #include "rpc_client/cli_spoolss.h"
23 #include "rpc_client/init_spoolss.h"
24 #include "nt_printing.h"
25 #include "registry.h"
26 #include "registry/reg_objects.h"
28 /* support itanium as well */
29 static const struct print_architecture_table_node archi_table[]= {
31 {"Windows 4.0", "WIN40", 0 },
32 {"Windows NT x86", "W32X86", 2 },
33 {"Windows NT x86", "W32X86", 3 },
34 {"Windows NT R4000", "W32MIPS", 2 },
35 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
36 {"Windows NT PowerPC", "W32PPC", 2 },
37 {"Windows IA64", "IA64", 3 },
38 {"Windows x64", "x64", 3 },
39 {NULL, "", -1 }
43 /**
44 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
45 * It is here for debugging purpose and should be removed later on.
46 **/
48 /****************************************************************************
49 Printer info level 3 display function.
50 ****************************************************************************/
52 static void display_print_driver3(struct spoolss_DriverInfo3 *r)
54 int i;
56 if (!r) {
57 return;
60 printf(_("Printer Driver Info 3:\n"));
61 printf(_("\tVersion: [%x]\n"), r->version);
62 printf(_("\tDriver Name: [%s]\n"), r->driver_name);
63 printf(_("\tArchitecture: [%s]\n"), r->architecture);
64 printf(_("\tDriver Path: [%s]\n"), r->driver_path);
65 printf(_("\tDatafile: [%s]\n"), r->data_file);
66 printf(_("\tConfigfile: [%s]\n\n"), r->config_file);
67 printf(_("\tHelpfile: [%s]\n\n"), r->help_file);
69 for (i=0; r->dependent_files[i] != NULL; i++) {
70 printf(_("\tDependentfiles: [%s]\n"), r->dependent_files[i]);
73 printf("\n");
75 printf(_("\tMonitorname: [%s]\n"), r->monitor_name);
76 printf(_("\tDefaultdatatype: [%s]\n\n"), r->default_datatype);
79 static void display_reg_value(const char *subkey, struct regval_blob *value)
81 const char *text;
82 DATA_BLOB blob;
84 switch(regval_type(value)) {
85 case REG_DWORD:
86 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey,
87 regval_name(value), *((uint32_t *) regval_data_p(value)));
88 break;
90 case REG_SZ:
91 blob = data_blob_const(regval_data_p(value), regval_size(value));
92 pull_reg_sz(talloc_tos(), &blob, &text);
93 if (!text) {
94 break;
96 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, regval_name(value),
97 text);
98 break;
100 case REG_BINARY:
101 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
102 "displayed\n"),
103 subkey, regval_name(value));
104 break;
106 case REG_MULTI_SZ: {
107 uint32_t i;
108 const char **values;
109 blob = data_blob_const(regval_data_p(value), regval_size(value));
111 if (!pull_reg_multi_sz(NULL, &blob, &values)) {
112 d_printf("pull_reg_multi_sz failed\n");
113 break;
116 printf("%s: REG_MULTI_SZ: \n", regval_name(value));
117 for (i=0; values[i] != NULL; i++) {
118 d_printf("%s\n", values[i]);
120 TALLOC_FREE(values);
121 break;
124 default:
125 d_printf(_("\t%s: unknown type %d\n"), regval_name(value),
126 regval_type(value));
132 * Copies ACLs, DOS-attributes and timestamps from one
133 * file or directory from one connected share to another connected share
135 * @param c A net_context structure
136 * @param mem_ctx A talloc-context
137 * @param cli_share_src A connected cli_state
138 * @param cli_share_dst A connected cli_state
139 * @param src_file The source file-name
140 * @param dst_file The destination file-name
141 * @param copy_acls Whether to copy acls
142 * @param copy_attrs Whether to copy DOS attributes
143 * @param copy_timestamps Whether to preserve timestamps
144 * @param is_file Whether this file is a file or a dir
146 * @return Normal NTSTATUS return.
149 NTSTATUS net_copy_fileattr(struct net_context *c,
150 TALLOC_CTX *mem_ctx,
151 struct cli_state *cli_share_src,
152 struct cli_state *cli_share_dst,
153 const char *src_name, const char *dst_name,
154 bool copy_acls, bool copy_attrs,
155 bool copy_timestamps, bool is_file)
157 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
158 uint16_t fnum_src = 0;
159 uint16_t fnum_dst = 0;
160 struct security_descriptor *sd = NULL;
161 uint16_t attr;
162 time_t f_atime, f_ctime, f_mtime;
165 if (!copy_timestamps && !copy_acls && !copy_attrs)
166 return NT_STATUS_OK;
168 /* open file/dir on the originating server */
170 DEBUGADD(3,("opening %s %s on originating server\n",
171 is_file?"file":"dir", src_name));
173 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
174 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src))) {
175 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
176 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
177 nt_status = cli_nt_error(cli_share_src);
178 goto out;
182 if (copy_acls) {
184 /* get the security descriptor */
185 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
186 if (!sd) {
187 DEBUG(0,("failed to get security descriptor: %s\n",
188 cli_errstr(cli_share_src)));
189 nt_status = cli_nt_error(cli_share_src);
190 goto out;
193 if (c->opt_verbose && DEBUGLEVEL >= 3)
194 display_sec_desc(sd);
198 if (copy_attrs || copy_timestamps) {
200 /* get file attributes */
201 if (!NT_STATUS_IS_OK(cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
202 &f_ctime, &f_atime, &f_mtime))) {
203 DEBUG(0,("failed to get file-attrs: %s\n",
204 cli_errstr(cli_share_src)));
205 nt_status = cli_nt_error(cli_share_src);
206 goto out;
211 /* open the file/dir on the destination server */
213 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_dst, dst_name, 0, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
214 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_dst))) {
215 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
216 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
217 nt_status = cli_nt_error(cli_share_dst);
218 goto out;
221 if (copy_timestamps) {
223 /* set timestamps */
224 if (!NT_STATUS_IS_OK(cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime))) {
225 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
226 cli_errstr(cli_share_dst)));
227 nt_status = cli_nt_error(cli_share_dst);
228 goto out;
232 if (copy_acls) {
234 /* set acls */
235 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
236 DEBUG(0,("could not set secdesc on %s: %s\n",
237 dst_name, cli_errstr(cli_share_dst)));
238 nt_status = cli_nt_error(cli_share_dst);
239 goto out;
243 if (copy_attrs) {
245 /* set attrs */
246 if (!NT_STATUS_IS_OK(cli_setatr(cli_share_dst, dst_name, attr, 0))) {
247 DEBUG(0,("failed to set file-attrs: %s\n",
248 cli_errstr(cli_share_dst)));
249 nt_status = cli_nt_error(cli_share_dst);
250 goto out;
255 /* closing files */
257 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
258 d_fprintf(stderr,
259 _("could not close %s on originating server: %s\n"),
260 is_file?"file":"dir", cli_errstr(cli_share_src));
261 nt_status = cli_nt_error(cli_share_src);
262 goto out;
265 if (!NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
266 d_fprintf(stderr,
267 _("could not close %s on destination server: %s\n"),
268 is_file?"file":"dir", cli_errstr(cli_share_dst));
269 nt_status = cli_nt_error(cli_share_dst);
270 goto out;
274 nt_status = NT_STATUS_OK;
276 out:
278 /* cleaning up */
279 if (fnum_src)
280 cli_close(cli_share_src, fnum_src);
282 if (fnum_dst)
283 cli_close(cli_share_dst, fnum_dst);
285 return nt_status;
289 * Copy a file or directory from a connected share to another connected share
291 * @param c A net_context structure
292 * @param mem_ctx A talloc-context
293 * @param cli_share_src A connected cli_state
294 * @param cli_share_dst A connected cli_state
295 * @param src_file The source file-name
296 * @param dst_file The destination file-name
297 * @param copy_acls Whether to copy acls
298 * @param copy_attrs Whether to copy DOS attributes
299 * @param copy_timestamps Whether to preserve timestamps
300 * @param is_file Whether this file is a file or a dir
302 * @return Normal NTSTATUS return.
305 NTSTATUS net_copy_file(struct net_context *c,
306 TALLOC_CTX *mem_ctx,
307 struct cli_state *cli_share_src,
308 struct cli_state *cli_share_dst,
309 const char *src_name, const char *dst_name,
310 bool copy_acls, bool copy_attrs,
311 bool copy_timestamps, bool is_file)
313 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
314 uint16_t fnum_src = 0;
315 uint16_t fnum_dst = 0;
316 static int io_bufsize = 64512;
317 int read_size = io_bufsize;
318 char *data = NULL;
319 off_t nread = 0;
322 if (!src_name || !dst_name)
323 goto out;
325 if (cli_share_src == NULL || cli_share_dst == NULL)
326 goto out;
328 /* open on the originating server */
329 DEBUGADD(3,("opening %s %s on originating server\n",
330 is_file ? "file":"dir", src_name));
331 if (is_file)
332 nt_status = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE, &fnum_src);
333 else
334 nt_status = cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
335 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src);
337 if (!NT_STATUS_IS_OK(nt_status)) {
338 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
339 is_file ? "file":"dir",
340 src_name, cli_errstr(cli_share_src)));
341 goto out;
345 if (is_file) {
347 /* open file on the destination server */
348 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
349 nt_status = cli_open(cli_share_dst, dst_name,
350 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum_dst);
352 if (!NT_STATUS_IS_OK(nt_status)) {
353 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
354 dst_name, cli_errstr(cli_share_dst)));
355 goto out;
358 /* allocate memory */
359 if (!(data = (char *)SMB_MALLOC(read_size))) {
360 d_fprintf(stderr, _("malloc fail for size %d\n"),
361 read_size);
362 nt_status = NT_STATUS_NO_MEMORY;
363 goto out;
369 if (c->opt_verbose) {
371 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
372 "%s ACLs and %s DOS Attributes %s\n"),
373 cli_share_src->desthost, cli_share_src->share, src_name,
374 cli_share_dst->desthost, cli_share_dst->share, dst_name,
375 copy_acls ? _("with") : _("without"),
376 copy_attrs ? _("with") : _("without"),
377 copy_timestamps ? _("(preserving timestamps)") : "" );
381 while (is_file) {
383 /* copying file */
384 int n, ret;
385 n = cli_read(cli_share_src, fnum_src, data, nread,
386 read_size);
388 if (n <= 0)
389 break;
391 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
392 nread, n);
394 if (n != ret) {
395 d_fprintf(stderr, _("Error writing file: %s\n"),
396 cli_errstr(cli_share_dst));
397 nt_status = cli_nt_error(cli_share_dst);
398 goto out;
401 nread += n;
405 if (!is_file && !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
407 /* creating dir */
408 DEBUGADD(3,("creating dir %s on the destination server\n",
409 dst_name));
411 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share_dst, dst_name))) {
412 DEBUG(0,("cannot create directory %s: %s\n",
413 dst_name, cli_errstr(cli_share_dst)));
414 nt_status = NT_STATUS_NO_SUCH_FILE;
417 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
418 d_fprintf(stderr,
419 _("cannot check for directory %s: %s\n"),
420 dst_name, cli_errstr(cli_share_dst));
421 goto out;
426 /* closing files */
427 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
428 d_fprintf(stderr,
429 _("could not close file on originating server: %s\n"),
430 cli_errstr(cli_share_src));
431 nt_status = cli_nt_error(cli_share_src);
432 goto out;
435 if (is_file && !NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
436 d_fprintf(stderr,
437 _("could not close file on destination server: %s\n"),
438 cli_errstr(cli_share_dst));
439 nt_status = cli_nt_error(cli_share_dst);
440 goto out;
443 /* possibly we have to copy some file-attributes / acls / sd */
444 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
445 src_name, dst_name, copy_acls,
446 copy_attrs, copy_timestamps, is_file);
447 if (!NT_STATUS_IS_OK(nt_status))
448 goto out;
451 nt_status = NT_STATUS_OK;
453 out:
455 /* cleaning up */
456 if (fnum_src)
457 cli_close(cli_share_src, fnum_src);
459 if (fnum_dst)
460 cli_close(cli_share_dst, fnum_dst);
462 SAFE_FREE(data);
464 return nt_status;
468 * Copy a driverfile from on connected share to another connected share
469 * This silently assumes that a driver-file is picked up from
471 * \\src_server\print$\{arch}\{version}\file
473 * and copied to
475 * \\dst_server\print$\{arch}\file
477 * to be added via setdriver-calls later.
478 * @param c A net_context structure
479 * @param mem_ctx A talloc-context
480 * @param cli_share_src A cli_state connected to source print$-share
481 * @param cli_share_dst A cli_state connected to destination print$-share
482 * @param file The file-name to be copied
483 * @param short_archi The name of the driver-architecture (short form)
485 * @return Normal NTSTATUS return.
488 static NTSTATUS net_copy_driverfile(struct net_context *c,
489 TALLOC_CTX *mem_ctx,
490 struct cli_state *cli_share_src,
491 struct cli_state *cli_share_dst,
492 const char *file, const char *short_archi) {
494 const char *p;
495 char *src_name;
496 char *dst_name;
497 char *version = NULL;
498 char *filename = NULL;
499 char *tok;
501 if (!file) {
502 return NT_STATUS_OK;
505 /* scroll through the file until we have the part
506 beyond archi_table.short_archi */
507 p = file;
508 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
509 if (strequal(tok, short_archi)) {
510 next_token_talloc(mem_ctx, &p, &version, "\\");
511 next_token_talloc(mem_ctx, &p, &filename, "\\");
515 if (version == NULL || filename == NULL) {
516 return NT_STATUS_UNSUCCESSFUL;
519 /* build source file name */
520 src_name = talloc_asprintf(mem_ctx, "\\%s\\%s\\%s",
521 short_archi, version, filename);
522 if (src_name == NULL) {
523 return NT_STATUS_NO_MEMORY;
526 /* create destination file name */
527 dst_name = talloc_asprintf(mem_ctx, "\\%s\\%s", short_archi, filename);
528 if (dst_name == NULL) {
529 return NT_STATUS_NO_MEMORY;
533 /* finally copy the file */
534 return net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
535 src_name, dst_name, false, false, false, true);
539 * Check for existing Architecture directory on a given server
541 * @param cli_share A cli_state connected to a print$-share
542 * @param short_archi The Architecture for the print-driver
544 * @return Normal NTSTATUS return.
547 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
550 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
551 char *dir;
553 if (asprintf(&dir, "\\%s", short_archi) < 0) {
554 return NT_STATUS_NO_MEMORY;
557 DEBUG(10,("creating print-driver dir for architecture: %s\n",
558 short_archi));
560 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share, dir))) {
561 DEBUG(1,("cannot create directory %s: %s\n",
562 dir, cli_errstr(cli_share)));
563 nt_status = NT_STATUS_NO_SUCH_FILE;
566 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share, dir))) {
567 d_fprintf(stderr, _("cannot check %s: %s\n"),
568 dir, cli_errstr(cli_share));
569 goto out;
572 nt_status = NT_STATUS_OK;
574 out:
575 SAFE_FREE(dir);
576 return nt_status;
580 * Copy a print-driver (level 3) from one connected print$-share to another
581 * connected print$-share
583 * @param c A net_context structure
584 * @param mem_ctx A talloc-context
585 * @param cli_share_src A cli_state connected to a print$-share
586 * @param cli_share_dst A cli_state connected to a print$-share
587 * @param short_archi The Architecture for the print-driver
588 * @param i1 The DRIVER_INFO_3-struct
590 * @return Normal NTSTATUS return.
593 static NTSTATUS copy_print_driver_3(struct net_context *c,
594 TALLOC_CTX *mem_ctx,
595 struct cli_state *cli_share_src,
596 struct cli_state *cli_share_dst,
597 const char *short_archi,
598 struct spoolss_DriverInfo3 *r)
600 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
601 int i;
603 if (r == NULL) {
604 return nt_status;
607 if (c->opt_verbose)
608 d_printf(_("copying driver: [%s], for architecture: [%s], "
609 "version: [%d]\n"),
610 r->driver_name, short_archi, r->version);
612 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
613 r->driver_path, short_archi);
614 if (!NT_STATUS_IS_OK(nt_status))
615 return nt_status;
617 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
618 r->data_file, short_archi);
619 if (!NT_STATUS_IS_OK(nt_status))
620 return nt_status;
622 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
623 r->config_file, short_archi);
624 if (!NT_STATUS_IS_OK(nt_status))
625 return nt_status;
627 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
628 r->help_file, short_archi);
629 if (!NT_STATUS_IS_OK(nt_status))
630 return nt_status;
632 for (i=0; r->dependent_files[i] != NULL; i++) {
634 nt_status = net_copy_driverfile(c, mem_ctx,
635 cli_share_src, cli_share_dst,
636 r->dependent_files[i], short_archi);
637 if (!NT_STATUS_IS_OK(nt_status)) {
638 return nt_status;
642 return NT_STATUS_OK;
646 * net_spoolss-functions
647 * =====================
649 * the net_spoolss-functions aim to simplify spoolss-client-functions
650 * required during the migration-process wrt buffer-sizes, returned
651 * error-codes, etc.
653 * this greatly reduces the complexitiy of the migrate-functions.
657 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
658 TALLOC_CTX *mem_ctx,
659 char *name,
660 uint32_t flags,
661 uint32_t level,
662 uint32_t *num_printers,
663 union spoolss_PrinterInfo **info)
665 WERROR result;
667 /* enum printers */
669 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
670 flags,
671 name,
672 level,
674 num_printers,
675 info);
676 if (!W_ERROR_IS_OK(result)) {
677 printf(_("cannot enum printers: %s\n"), win_errstr(result));
678 return false;
681 return true;
684 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
685 TALLOC_CTX *mem_ctx,
686 const char *printername,
687 uint32_t access_required,
688 const char *username,
689 struct policy_handle *hnd)
691 WERROR result;
692 fstring printername2;
694 fstrcpy(printername2, pipe_hnd->srv_name_slash);
695 fstrcat(printername2, "\\");
696 fstrcat(printername2, printername);
698 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
699 pipe_hnd->srv_name_slash, username, printername2, access_required));
701 /* open printer */
702 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
703 printername2,
704 access_required,
705 hnd);
707 /* be more verbose */
708 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
709 d_fprintf(stderr,
710 _("no access to printer [%s] on [%s] for user [%s] "
711 "granted\n"),
712 printername2, pipe_hnd->srv_name_slash, username);
713 return false;
716 if (!W_ERROR_IS_OK(result)) {
717 d_fprintf(stderr,_("cannot open printer %s on server %s: %s\n"),
718 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
719 return false;
722 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
723 printername2, pipe_hnd->srv_name_slash));
725 return true;
728 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
729 TALLOC_CTX *mem_ctx,
730 struct policy_handle *hnd,
731 uint32_t level,
732 union spoolss_PrinterInfo *info)
734 WERROR result;
736 /* getprinter call */
737 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
738 hnd,
739 level,
740 0, /* offered */
741 info);
742 if (!W_ERROR_IS_OK(result)) {
743 printf(_("cannot get printer-info: %s\n"), win_errstr(result));
744 return false;
747 return true;
750 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
751 TALLOC_CTX *mem_ctx,
752 struct policy_handle *hnd,
753 uint32_t level,
754 union spoolss_PrinterInfo *info)
756 WERROR result;
757 NTSTATUS status;
758 struct spoolss_SetPrinterInfoCtr info_ctr;
759 struct spoolss_SetPrinterInfo2 info2;
760 struct spoolss_DevmodeContainer devmode_ctr;
761 struct sec_desc_buf secdesc_ctr;
763 ZERO_STRUCT(devmode_ctr);
764 ZERO_STRUCT(secdesc_ctr);
766 /* setprinter call */
768 info_ctr.level = level;
769 switch (level) {
770 case 0:
771 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)
772 (void *)&info->info0;
773 break;
774 case 1:
775 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)
776 (void *)&info->info1;
777 break;
778 case 2:
779 spoolss_printerinfo2_to_setprinterinfo2(&info->info2, &info2);
780 info_ctr.info.info2 = &info2;
781 break;
782 case 3:
783 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
784 (void *)&info->info3;
785 break;
786 case 4:
787 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)
788 (void *)&info->info4;
789 break;
790 case 5:
791 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)
792 (void *)&info->info5;
793 break;
794 case 6:
795 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)
796 (void *)&info->info6;
797 break;
798 case 7:
799 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
800 (void *)&info->info7;
801 break;
802 #if 0 /* FIXME GD */
803 case 8:
804 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)
805 (void *)&info->info8;
806 break;
807 case 9:
808 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)
809 (void *)&info->info9;
810 break;
811 #endif
812 default:
813 break; /* FIXME */
816 status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
817 hnd,
818 &info_ctr,
819 &devmode_ctr,
820 &secdesc_ctr,
821 0, /* command */
822 &result);
824 if (!W_ERROR_IS_OK(result)) {
825 printf(_("cannot set printer-info: %s\n"), win_errstr(result));
826 return false;
829 return true;
833 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
834 TALLOC_CTX *mem_ctx,
835 struct policy_handle *hnd,
836 const char *value_name,
837 enum winreg_Type type,
838 uint8_t *data,
839 uint32_t offered)
841 WERROR result;
842 NTSTATUS status;
844 /* setprinterdata call */
845 status = rpccli_spoolss_SetPrinterData(pipe_hnd, mem_ctx,
846 hnd,
847 value_name,
848 type,
849 data,
850 offered,
851 &result);
853 if (!W_ERROR_IS_OK(result)) {
854 printf (_("unable to set printerdata: %s\n"),
855 win_errstr(result));
856 return false;
859 return true;
863 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
864 TALLOC_CTX *mem_ctx,
865 struct policy_handle *hnd,
866 const char *keyname,
867 const char ***keylist)
869 WERROR result;
871 /* enumprinterkey call */
872 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
874 if (!W_ERROR_IS_OK(result)) {
875 printf(_("enumprinterkey failed: %s\n"), win_errstr(result));
876 return false;
879 return true;
882 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
883 TALLOC_CTX *mem_ctx,
884 uint32_t offered,
885 struct policy_handle *hnd,
886 const char *keyname,
887 uint32_t *count,
888 struct spoolss_PrinterEnumValues **info)
890 WERROR result;
892 /* enumprinterdataex call */
893 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
894 hnd,
895 keyname,
896 0, /* offered */
897 count,
898 info);
900 if (!W_ERROR_IS_OK(result)) {
901 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result));
902 return false;
905 return true;
909 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
910 TALLOC_CTX *mem_ctx,
911 struct policy_handle *hnd,
912 const char *keyname,
913 struct regval_blob *value)
915 WERROR result;
916 NTSTATUS status;
918 /* setprinterdataex call */
919 status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
920 hnd,
921 keyname,
922 regval_name(value),
923 regval_type(value),
924 regval_data_p(value),
925 regval_size(value),
926 &result);
928 if (!W_ERROR_IS_OK(result)) {
929 printf(_("could not set printerdataex: %s\n"),
930 win_errstr(result));
931 return false;
934 return true;
937 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
938 TALLOC_CTX *mem_ctx,
939 struct policy_handle *hnd,
940 int level,
941 uint32_t *num_forms,
942 union spoolss_FormInfo **forms)
944 WERROR result;
946 /* enumforms call */
947 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
948 hnd,
949 level,
951 num_forms,
952 forms);
953 if (!W_ERROR_IS_OK(result)) {
954 printf(_("could not enum forms: %s\n"), win_errstr(result));
955 return false;
958 return true;
961 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
962 TALLOC_CTX *mem_ctx,
963 uint32_t level, const char *env,
964 uint32_t *count,
965 union spoolss_DriverInfo **info)
967 WERROR result;
969 /* enumprinterdrivers call */
970 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
971 pipe_hnd->srv_name_slash,
972 env,
973 level,
975 count,
976 info);
977 if (!W_ERROR_IS_OK(result)) {
978 printf(_("cannot enum drivers: %s\n"), win_errstr(result));
979 return false;
982 return true;
985 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
986 TALLOC_CTX *mem_ctx,
987 struct policy_handle *hnd, uint32_t level,
988 const char *env, int version,
989 union spoolss_DriverInfo *info)
991 WERROR result;
992 uint32_t server_major_version;
993 uint32_t server_minor_version;
995 /* getprinterdriver call */
996 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
997 hnd,
998 env,
999 level,
1001 version,
1003 info,
1004 &server_major_version,
1005 &server_minor_version);
1006 if (!W_ERROR_IS_OK(result)) {
1007 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1008 env, win_errstr(result)));
1009 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1010 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1011 printf(_("cannot get driver: %s\n"),
1012 win_errstr(result));
1014 return false;
1017 return true;
1021 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1022 TALLOC_CTX *mem_ctx, uint32_t level,
1023 union spoolss_DriverInfo *info)
1025 WERROR result;
1026 NTSTATUS status;
1027 struct spoolss_AddDriverInfoCtr info_ctr;
1029 info_ctr.level = level;
1031 switch (level) {
1032 case 2:
1033 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)
1034 (void *)&info->info2;
1035 break;
1036 case 3:
1037 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)
1038 (void *)&info->info3;
1039 break;
1040 default:
1041 printf(_("unsupported info level: %d\n"), level);
1042 return false;
1045 /* addprinterdriver call */
1046 status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1047 pipe_hnd->srv_name_slash,
1048 &info_ctr,
1049 &result);
1050 /* be more verbose */
1051 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1052 printf(_("You are not allowed to add drivers\n"));
1053 return false;
1055 if (!W_ERROR_IS_OK(result)) {
1056 printf(_("cannot add driver: %s\n"), win_errstr(result));
1057 return false;
1060 return true;
1064 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1065 * for a single printer or for all printers depending on argc/argv
1068 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1069 TALLOC_CTX *mem_ctx,
1070 int level,
1071 int argc,
1072 const char **argv,
1073 uint32_t *num_printers,
1074 union spoolss_PrinterInfo **info_p)
1076 struct policy_handle hnd;
1078 /* no arguments given, enumerate all printers */
1079 if (argc == 0) {
1081 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1082 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1083 level, num_printers, info_p))
1084 return false;
1086 goto out;
1089 /* argument given, get a single printer by name */
1090 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1091 MAXIMUM_ALLOWED_ACCESS,
1092 pipe_hnd->auth->user_name,
1093 &hnd))
1094 return false;
1096 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1097 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1098 return false;
1101 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1103 *num_printers = 1;
1105 out:
1106 DEBUG(3,("got %d printers\n", *num_printers));
1108 return true;
1113 * List print-queues (including local printers that are not shared)
1115 * All parameters are provided by the run_rpc_command function, except for
1116 * argc, argv which are passed through.
1118 * @param c A net_context structure
1119 * @param domain_sid The domain sid aquired from the remote server
1120 * @param cli A cli_state connected to the server.
1121 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1122 * @param argc Standard main() style argc
1123 * @param argv Standard main() style argv. Initial components are already
1124 * stripped
1126 * @return Normal NTSTATUS return.
1129 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1130 const struct dom_sid *domain_sid,
1131 const char *domain_name,
1132 struct cli_state *cli,
1133 struct rpc_pipe_client *pipe_hnd,
1134 TALLOC_CTX *mem_ctx,
1135 int argc,
1136 const char **argv)
1138 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1139 uint32_t i, num_printers;
1140 uint32_t level = 2;
1141 const char *printername, *sharename;
1142 union spoolss_PrinterInfo *info;
1144 printf("listing printers\n");
1146 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1147 return nt_status;
1149 for (i = 0; i < num_printers; i++) {
1151 /* do some initialization */
1152 printername = info[i].info2.printername;
1153 sharename = info[i].info2.sharename;
1155 if (printername && sharename) {
1156 d_printf(_("printer %d: %s, shared as: %s\n"),
1157 i+1, printername, sharename);
1161 return NT_STATUS_OK;
1165 * List printer-drivers from a server
1167 * All parameters are provided by the run_rpc_command function, except for
1168 * argc, argv which are passed through.
1170 * @param c A net_context structure
1171 * @param domain_sid The domain sid aquired from the remote server
1172 * @param cli A cli_state connected to the server.
1173 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1174 * @param argc Standard main() style argc
1175 * @param argv Standard main() style argv. Initial components are already
1176 * stripped
1178 * @return Normal NTSTATUS return.
1181 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1182 const struct dom_sid *domain_sid,
1183 const char *domain_name,
1184 struct cli_state *cli,
1185 struct rpc_pipe_client *pipe_hnd,
1186 TALLOC_CTX *mem_ctx,
1187 int argc,
1188 const char **argv)
1190 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1191 uint32_t i;
1192 uint32_t level = 3;
1193 union spoolss_DriverInfo *info;
1194 int d;
1196 printf(_("listing printer-drivers\n"));
1198 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1200 uint32_t num_drivers;
1202 /* enum remote drivers */
1203 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1204 archi_table[i].long_archi,
1205 &num_drivers, &info)) {
1206 nt_status = NT_STATUS_UNSUCCESSFUL;
1207 goto done;
1210 if (num_drivers == 0) {
1211 d_printf(_("no drivers found on server for "
1212 "architecture: [%s].\n"),
1213 archi_table[i].long_archi);
1214 continue;
1217 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1218 num_drivers, archi_table[i].long_archi);
1221 /* do something for all drivers for architecture */
1222 for (d = 0; d < num_drivers; d++) {
1223 display_print_driver3(&info[d].info3);
1227 nt_status = NT_STATUS_OK;
1229 done:
1230 return nt_status;
1235 * Publish print-queues with args-wrapper
1237 * @param cli A cli_state connected to the server.
1238 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1239 * @param argc Standard main() style argc
1240 * @param argv Standard main() style argv. Initial components are already
1241 * stripped
1242 * @param action
1244 * @return Normal NTSTATUS return.
1247 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1248 TALLOC_CTX *mem_ctx,
1249 int argc,
1250 const char **argv,
1251 uint32_t action)
1253 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1254 uint32_t i, num_printers;
1255 uint32_t level = 7;
1256 const char *printername, *sharename;
1257 union spoolss_PrinterInfo *info_enum;
1258 union spoolss_PrinterInfo info;
1259 struct spoolss_SetPrinterInfoCtr info_ctr;
1260 struct spoolss_DevmodeContainer devmode_ctr;
1261 struct sec_desc_buf secdesc_ctr;
1262 struct policy_handle hnd;
1263 WERROR result;
1264 const char *action_str;
1266 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1267 return nt_status;
1269 for (i = 0; i < num_printers; i++) {
1271 /* do some initialization */
1272 printername = info_enum[i].info2.printername;
1273 sharename = info_enum[i].info2.sharename;
1274 if (!printername || !sharename) {
1275 goto done;
1278 /* open printer handle */
1279 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1280 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1281 goto done;
1283 /* check for existing dst printer */
1284 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1285 goto done;
1287 /* check action and set string */
1288 switch (action) {
1289 case DSPRINT_PUBLISH:
1290 action_str = N_("published");
1291 break;
1292 case DSPRINT_UPDATE:
1293 action_str = N_("updated");
1294 break;
1295 case DSPRINT_UNPUBLISH:
1296 action_str = N_("unpublished");
1297 break;
1298 default:
1299 action_str = N_("unknown action");
1300 printf(_("unkown action: %d\n"), action);
1301 break;
1304 info.info7.action = action;
1305 info_ctr.level = 7;
1306 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
1307 (void *)&info.info7;
1309 ZERO_STRUCT(devmode_ctr);
1310 ZERO_STRUCT(secdesc_ctr);
1312 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1313 &hnd,
1314 &info_ctr,
1315 &devmode_ctr,
1316 &secdesc_ctr,
1317 0, /* command */
1318 &result);
1320 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1321 printf(_("cannot set printer-info: %s\n"),
1322 win_errstr(result));
1323 goto done;
1326 printf(_("successfully %s printer %s in Active Directory\n"),
1327 action_str, sharename);
1330 nt_status = NT_STATUS_OK;
1332 done:
1333 if (is_valid_policy_hnd(&hnd))
1334 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1336 return nt_status;
1339 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1340 const struct dom_sid *domain_sid,
1341 const char *domain_name,
1342 struct cli_state *cli,
1343 struct rpc_pipe_client *pipe_hnd,
1344 TALLOC_CTX *mem_ctx,
1345 int argc,
1346 const char **argv)
1348 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1351 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1352 const struct dom_sid *domain_sid,
1353 const char *domain_name,
1354 struct cli_state *cli,
1355 struct rpc_pipe_client *pipe_hnd,
1356 TALLOC_CTX *mem_ctx,
1357 int argc,
1358 const char **argv)
1360 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1363 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1364 const struct dom_sid *domain_sid,
1365 const char *domain_name,
1366 struct cli_state *cli,
1367 struct rpc_pipe_client *pipe_hnd,
1368 TALLOC_CTX *mem_ctx,
1369 int argc,
1370 const char **argv)
1372 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1376 * List print-queues w.r.t. their publishing state
1378 * All parameters are provided by the run_rpc_command function, except for
1379 * argc, argv which are passed through.
1381 * @param c A net_context structure
1382 * @param domain_sid The domain sid aquired from the remote server
1383 * @param cli A cli_state connected to the server.
1384 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1385 * @param argc Standard main() style argc
1386 * @param argv Standard main() style argv. Initial components are already
1387 * stripped
1389 * @return Normal NTSTATUS return.
1392 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1393 const struct dom_sid *domain_sid,
1394 const char *domain_name,
1395 struct cli_state *cli,
1396 struct rpc_pipe_client *pipe_hnd,
1397 TALLOC_CTX *mem_ctx,
1398 int argc,
1399 const char **argv)
1401 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1402 uint32_t i, num_printers;
1403 uint32_t level = 7;
1404 const char *printername, *sharename;
1405 union spoolss_PrinterInfo *info_enum;
1406 union spoolss_PrinterInfo info;
1407 struct policy_handle hnd;
1408 int state;
1410 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1411 return nt_status;
1413 for (i = 0; i < num_printers; i++) {
1415 /* do some initialization */
1416 printername = info_enum[i].info2.printername;
1417 sharename = info_enum[i].info2.sharename;
1419 if (!printername || !sharename) {
1420 goto done;
1423 /* open printer handle */
1424 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1425 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1426 goto done;
1428 /* check for existing dst printer */
1429 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1430 goto done;
1432 if (!info.info7.guid) {
1433 goto done;
1435 state = info.info7.action;
1436 switch (state) {
1437 case DSPRINT_PUBLISH:
1438 printf(_("printer [%s] is published"),
1439 sharename);
1440 if (c->opt_verbose)
1441 printf(_(", guid: %s"),info.info7.guid);
1442 printf("\n");
1443 break;
1444 case DSPRINT_UNPUBLISH:
1445 printf(_("printer [%s] is unpublished\n"),
1446 sharename);
1447 break;
1448 case DSPRINT_UPDATE:
1449 printf(_("printer [%s] is currently updating\n"),
1450 sharename);
1451 break;
1452 default:
1453 printf(_("unkown state: %d\n"), state);
1454 break;
1458 nt_status = NT_STATUS_OK;
1460 done:
1461 if (is_valid_policy_hnd(&hnd))
1462 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1464 return nt_status;
1468 * Migrate Printer-ACLs from a source server to the destination server
1470 * All parameters are provided by the run_rpc_command function, except for
1471 * argc, argv which are passed through.
1473 * @param c A net_context structure
1474 * @param domain_sid The domain sid aquired from the remote server
1475 * @param cli A cli_state connected to the server.
1476 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1477 * @param argc Standard main() style argc
1478 * @param argv Standard main() style argv. Initial components are already
1479 * stripped
1481 * @return Normal NTSTATUS return.
1484 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1485 const struct dom_sid *domain_sid,
1486 const char *domain_name,
1487 struct cli_state *cli,
1488 struct rpc_pipe_client *pipe_hnd,
1489 TALLOC_CTX *mem_ctx,
1490 int argc,
1491 const char **argv)
1493 /* TODO: what now, info2 or info3 ?
1494 convince jerry that we should add clientside setacls level 3 at least
1496 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1497 uint32_t i = 0;
1498 uint32_t num_printers;
1499 uint32_t level = 2;
1500 const char *printername, *sharename;
1501 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1502 struct policy_handle hnd_src, hnd_dst;
1503 union spoolss_PrinterInfo *info_enum;
1504 struct cli_state *cli_dst = NULL;
1505 union spoolss_PrinterInfo info_src, info_dst;
1507 DEBUG(3,("copying printer ACLs\n"));
1509 /* connect destination PI_SPOOLSS */
1510 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1511 &ndr_table_spoolss.syntax_id);
1512 if (!NT_STATUS_IS_OK(nt_status))
1513 return nt_status;
1516 /* enum source printers */
1517 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1518 nt_status = NT_STATUS_UNSUCCESSFUL;
1519 goto done;
1522 if (!num_printers) {
1523 printf (_("no printers found on server.\n"));
1524 nt_status = NT_STATUS_OK;
1525 goto done;
1528 /* do something for all printers */
1529 for (i = 0; i < num_printers; i++) {
1531 /* do some initialization */
1532 printername = info_enum[i].info2.printername;
1533 sharename = info_enum[i].info2.sharename;
1535 if (!printername || !sharename) {
1536 nt_status = NT_STATUS_UNSUCCESSFUL;
1537 goto done;
1540 /* we can reset NT_STATUS here because we do not
1541 get any real NT_STATUS-codes anymore from now on */
1542 nt_status = NT_STATUS_UNSUCCESSFUL;
1544 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1545 printername, sharename);
1547 /* according to msdn you have specify these access-rights
1548 to see the security descriptor
1549 - READ_CONTROL (DACL)
1550 - ACCESS_SYSTEM_SECURITY (SACL)
1553 /* open src printer handle */
1554 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1555 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1556 goto done;
1558 /* open dst printer handle */
1559 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1560 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1561 goto done;
1563 /* check for existing dst printer */
1564 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1565 goto done;
1567 /* check for existing src printer */
1568 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1569 goto done;
1571 /* Copy Security Descriptor */
1573 /* copy secdesc (info level 2) */
1574 info_dst.info2.devmode = NULL;
1575 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1577 if (c->opt_verbose)
1578 display_sec_desc(info_dst.info2.secdesc);
1580 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1581 goto done;
1583 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1586 /* close printer handles here */
1587 if (is_valid_policy_hnd(&hnd_src)) {
1588 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1591 if (is_valid_policy_hnd(&hnd_dst)) {
1592 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1597 nt_status = NT_STATUS_OK;
1599 done:
1601 if (is_valid_policy_hnd(&hnd_src)) {
1602 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1605 if (is_valid_policy_hnd(&hnd_dst)) {
1606 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1609 if (cli_dst) {
1610 cli_shutdown(cli_dst);
1612 return nt_status;
1616 * Migrate printer-forms from a src server to the dst server
1618 * All parameters are provided by the run_rpc_command function, except for
1619 * argc, argv which are passed through.
1621 * @param c A net_context structure
1622 * @param domain_sid The domain sid aquired from the remote server
1623 * @param cli A cli_state connected to the server.
1624 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1625 * @param argc Standard main() style argc
1626 * @param argv Standard main() style argv. Initial components are already
1627 * stripped
1629 * @return Normal NTSTATUS return.
1632 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1633 const struct dom_sid *domain_sid,
1634 const char *domain_name,
1635 struct cli_state *cli,
1636 struct rpc_pipe_client *pipe_hnd,
1637 TALLOC_CTX *mem_ctx,
1638 int argc,
1639 const char **argv)
1641 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1642 WERROR result;
1643 uint32_t i, f;
1644 uint32_t num_printers;
1645 uint32_t level = 1;
1646 const char *printername, *sharename;
1647 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1648 struct policy_handle hnd_src, hnd_dst;
1649 union spoolss_PrinterInfo *info_enum;
1650 union spoolss_PrinterInfo info_dst;
1651 uint32_t num_forms;
1652 union spoolss_FormInfo *forms;
1653 struct cli_state *cli_dst = NULL;
1655 DEBUG(3,("copying forms\n"));
1657 /* connect destination PI_SPOOLSS */
1658 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1659 &ndr_table_spoolss.syntax_id);
1660 if (!NT_STATUS_IS_OK(nt_status))
1661 return nt_status;
1663 /* enum src printers */
1664 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1665 nt_status = NT_STATUS_UNSUCCESSFUL;
1666 goto done;
1669 if (!num_printers) {
1670 printf (_("no printers found on server.\n"));
1671 nt_status = NT_STATUS_OK;
1672 goto done;
1675 /* do something for all printers */
1676 for (i = 0; i < num_printers; i++) {
1678 /* do some initialization */
1679 printername = info_enum[i].info2.printername;
1680 sharename = info_enum[i].info2.sharename;
1682 if (!printername || !sharename) {
1683 nt_status = NT_STATUS_UNSUCCESSFUL;
1684 goto done;
1686 /* we can reset NT_STATUS here because we do not
1687 get any real NT_STATUS-codes anymore from now on */
1688 nt_status = NT_STATUS_UNSUCCESSFUL;
1690 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1691 printername, sharename);
1694 /* open src printer handle */
1695 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1696 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1697 goto done;
1699 /* open dst printer handle */
1700 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1701 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1702 goto done;
1704 /* check for existing dst printer */
1705 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1706 goto done;
1708 /* finally migrate forms */
1709 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1710 goto done;
1712 DEBUG(1,("got %d forms for printer\n", num_forms));
1715 for (f = 0; f < num_forms; f++) {
1717 union spoolss_AddFormInfo info;
1718 NTSTATUS status;
1720 /* only migrate FORM_PRINTER types, according to jerry
1721 FORM_BUILTIN-types are hard-coded in samba */
1722 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1723 continue;
1725 if (c->opt_verbose)
1726 d_printf(_("\tmigrating form # %d [%s] of type "
1727 "[%d]\n"),
1728 f, forms[f].info1.form_name,
1729 forms[f].info1.flags);
1731 info.info1 = (struct spoolss_AddFormInfo1 *)
1732 (void *)&forms[f].info1;
1734 /* FIXME: there might be something wrong with samba's
1735 builtin-forms */
1736 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1737 &hnd_dst,
1739 info,
1740 &result);
1741 if (!W_ERROR_IS_OK(result)) {
1742 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1743 f, forms[f].info1.form_name);
1744 continue;
1747 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1748 forms[f].info1.form_name));
1752 /* close printer handles here */
1753 if (is_valid_policy_hnd(&hnd_src)) {
1754 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1757 if (is_valid_policy_hnd(&hnd_dst)) {
1758 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1762 nt_status = NT_STATUS_OK;
1764 done:
1766 if (is_valid_policy_hnd(&hnd_src))
1767 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1769 if (is_valid_policy_hnd(&hnd_dst))
1770 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1772 if (cli_dst) {
1773 cli_shutdown(cli_dst);
1775 return nt_status;
1779 * Migrate printer-drivers from a src server to the dst server
1781 * All parameters are provided by the run_rpc_command function, except for
1782 * argc, argv which are passed through.
1784 * @param c A net_context structure
1785 * @param domain_sid The domain sid aquired from the remote server
1786 * @param cli A cli_state connected to the server.
1787 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1788 * @param argc Standard main() style argc
1789 * @param argv Standard main() style argv. Initial components are already
1790 * stripped
1792 * @return Normal NTSTATUS return.
1795 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1796 const struct dom_sid *domain_sid,
1797 const char *domain_name,
1798 struct cli_state *cli,
1799 struct rpc_pipe_client *pipe_hnd,
1800 TALLOC_CTX *mem_ctx,
1801 int argc,
1802 const char **argv)
1804 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1805 uint32_t i, p;
1806 uint32_t num_printers;
1807 uint32_t level = 3;
1808 const char *printername, *sharename;
1809 bool got_src_driver_share = false;
1810 bool got_dst_driver_share = false;
1811 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1812 struct policy_handle hnd_src, hnd_dst;
1813 union spoolss_DriverInfo drv_info_src;
1814 union spoolss_PrinterInfo *info_enum;
1815 union spoolss_PrinterInfo info_dst;
1816 struct cli_state *cli_dst = NULL;
1817 struct cli_state *cli_share_src = NULL;
1818 struct cli_state *cli_share_dst = NULL;
1819 const char *drivername = NULL;
1821 DEBUG(3,("copying printer-drivers\n"));
1823 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1824 &ndr_table_spoolss.syntax_id);
1825 if (!NT_STATUS_IS_OK(nt_status))
1826 return nt_status;
1828 /* open print$-share on the src server */
1829 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1830 cli->desthost, "print$", "A:");
1831 if (!NT_STATUS_IS_OK(nt_status))
1832 goto done;
1834 got_src_driver_share = true;
1837 /* open print$-share on the dst server */
1838 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1839 cli_dst->desthost, "print$", "A:");
1840 if (!NT_STATUS_IS_OK(nt_status))
1841 return nt_status;
1843 got_dst_driver_share = true;
1846 /* enum src printers */
1847 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1848 nt_status = NT_STATUS_UNSUCCESSFUL;
1849 goto done;
1852 if (num_printers == 0) {
1853 printf (_("no printers found on server.\n"));
1854 nt_status = NT_STATUS_OK;
1855 goto done;
1859 /* do something for all printers */
1860 for (p = 0; p < num_printers; p++) {
1862 /* do some initialization */
1863 printername = info_enum[p].info2.printername;
1864 sharename = info_enum[p].info2.sharename;
1866 if (!printername || !sharename) {
1867 nt_status = NT_STATUS_UNSUCCESSFUL;
1868 goto done;
1871 /* we can reset NT_STATUS here because we do not
1872 get any real NT_STATUS-codes anymore from now on */
1873 nt_status = NT_STATUS_UNSUCCESSFUL;
1875 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1876 printername, sharename);
1878 /* open dst printer handle */
1879 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1880 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1881 goto done;
1883 /* check for existing dst printer */
1884 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1885 goto done;
1888 /* open src printer handle */
1889 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1890 MAXIMUM_ALLOWED_ACCESS,
1891 pipe_hnd->auth->user_name,
1892 &hnd_src))
1893 goto done;
1895 /* in a first step call getdriver for each shared printer (per arch)
1896 to get a list of all files that have to be copied */
1898 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1900 /* getdriver src */
1901 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1902 level, archi_table[i].long_archi,
1903 archi_table[i].version, &drv_info_src))
1904 continue;
1906 drivername = drv_info_src.info3.driver_name;
1908 if (c->opt_verbose)
1909 display_print_driver3(&drv_info_src.info3);
1911 /* check arch dir */
1912 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1913 if (!NT_STATUS_IS_OK(nt_status))
1914 goto done;
1917 /* copy driver-files */
1918 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1919 archi_table[i].short_archi,
1920 &drv_info_src.info3);
1921 if (!NT_STATUS_IS_OK(nt_status))
1922 goto done;
1925 /* adddriver dst */
1926 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1927 nt_status = NT_STATUS_UNSUCCESSFUL;
1928 goto done;
1931 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1932 drivername, printername));
1936 if (!drivername || strlen(drivername) == 0) {
1937 DEBUGADD(1,("Did not get driver for printer %s\n",
1938 printername));
1939 goto done;
1942 /* setdriver dst */
1943 info_dst.info2.drivername = drivername;
1945 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1946 nt_status = NT_STATUS_UNSUCCESSFUL;
1947 goto done;
1950 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1951 drivername, printername));
1953 /* close dst */
1954 if (is_valid_policy_hnd(&hnd_dst)) {
1955 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1958 /* close src */
1959 if (is_valid_policy_hnd(&hnd_src)) {
1960 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1964 nt_status = NT_STATUS_OK;
1966 done:
1968 if (is_valid_policy_hnd(&hnd_src))
1969 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1971 if (is_valid_policy_hnd(&hnd_dst))
1972 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1974 if (cli_dst) {
1975 cli_shutdown(cli_dst);
1978 if (got_src_driver_share)
1979 cli_shutdown(cli_share_src);
1981 if (got_dst_driver_share)
1982 cli_shutdown(cli_share_dst);
1984 return nt_status;
1989 * Migrate printer-queues from a src to the dst server
1990 * (requires a working "addprinter command" to be installed for the local smbd)
1992 * All parameters are provided by the run_rpc_command function, except for
1993 * argc, argv which are passed through.
1995 * @param c A net_context structure
1996 * @param domain_sid The domain sid aquired from the remote server
1997 * @param cli A cli_state connected to the server.
1998 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1999 * @param argc Standard main() style argc
2000 * @param argv Standard main() style argv. Initial components are already
2001 * stripped
2003 * @return Normal NTSTATUS return.
2006 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
2007 const struct dom_sid *domain_sid,
2008 const char *domain_name,
2009 struct cli_state *cli,
2010 struct rpc_pipe_client *pipe_hnd,
2011 TALLOC_CTX *mem_ctx,
2012 int argc,
2013 const char **argv)
2015 WERROR result;
2016 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2017 uint32_t i = 0, num_printers;
2018 uint32_t level = 2;
2019 union spoolss_PrinterInfo info_dst, info_src;
2020 union spoolss_PrinterInfo *info_enum;
2021 struct cli_state *cli_dst = NULL;
2022 struct policy_handle hnd_dst, hnd_src;
2023 const char *printername, *sharename;
2024 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2025 struct spoolss_SetPrinterInfoCtr info_ctr;
2027 DEBUG(3,("copying printers\n"));
2029 /* connect destination PI_SPOOLSS */
2030 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2031 &ndr_table_spoolss.syntax_id);
2032 if (!NT_STATUS_IS_OK(nt_status))
2033 return nt_status;
2035 /* enum printers */
2036 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2037 nt_status = NT_STATUS_UNSUCCESSFUL;
2038 goto done;
2041 if (!num_printers) {
2042 printf (_("no printers found on server.\n"));
2043 nt_status = NT_STATUS_OK;
2044 goto done;
2047 /* do something for all printers */
2048 for (i = 0; i < num_printers; i++) {
2050 struct spoolss_SetPrinterInfo2 info2;
2052 /* do some initialization */
2053 printername = info_enum[i].info2.printername;
2054 sharename = info_enum[i].info2.sharename;
2056 if (!printername || !sharename) {
2057 nt_status = NT_STATUS_UNSUCCESSFUL;
2058 goto done;
2060 /* we can reset NT_STATUS here because we do not
2061 get any real NT_STATUS-codes anymore from now on */
2062 nt_status = NT_STATUS_UNSUCCESSFUL;
2064 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2065 printername, sharename);
2067 /* open dst printer handle */
2068 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2069 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2071 DEBUG(1,("could not open printer: %s\n", sharename));
2074 /* check for existing dst printer */
2075 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2076 printf (_("could not get printer, creating printer.\n"));
2077 } else {
2078 DEBUG(1,("printer already exists: %s\n", sharename));
2079 /* close printer handle here - dst only, not got src yet. */
2080 if (is_valid_policy_hnd(&hnd_dst)) {
2081 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2083 continue;
2086 /* now get again src printer ctr via getprinter,
2087 we first need a handle for that */
2089 /* open src printer handle */
2090 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2091 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2092 goto done;
2094 /* getprinter on the src server */
2095 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2096 goto done;
2098 /* copy each src printer to a dst printer 1:1,
2099 maybe some values have to be changed though */
2100 d_printf(_("creating printer: %s\n"), printername);
2102 info_ctr.level = level;
2103 spoolss_printerinfo2_to_setprinterinfo2(&info_src.info2, &info2);
2104 info_ctr.info.info2 = &info2;
2106 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2107 mem_ctx,
2108 &info_ctr);
2110 if (W_ERROR_IS_OK(result))
2111 d_printf (_("printer [%s] successfully added.\n"),
2112 printername);
2113 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2114 d_fprintf (stderr, _("printer [%s] already exists.\n"),
2115 printername);
2116 else {
2117 d_fprintf (stderr, _("could not create printer [%s]\n"),
2118 printername);
2119 goto done;
2122 /* close printer handles here */
2123 if (is_valid_policy_hnd(&hnd_src)) {
2124 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2127 if (is_valid_policy_hnd(&hnd_dst)) {
2128 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2132 nt_status = NT_STATUS_OK;
2134 done:
2135 if (is_valid_policy_hnd(&hnd_src))
2136 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2138 if (is_valid_policy_hnd(&hnd_dst))
2139 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2141 if (cli_dst) {
2142 cli_shutdown(cli_dst);
2144 return nt_status;
2148 * Migrate Printer-Settings from a src server to the dst server
2149 * (for this to work, printers and drivers already have to be migrated earlier)
2151 * All parameters are provided by the run_rpc_command function, except for
2152 * argc, argv which are passed through.
2154 * @param c A net_context structure
2155 * @param domain_sid The domain sid aquired from the remote server
2156 * @param cli A cli_state connected to the server.
2157 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2158 * @param argc Standard main() style argc
2159 * @param argv Standard main() style argv. Initial components are already
2160 * stripped
2162 * @return Normal NTSTATUS return.
2165 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2166 const struct dom_sid *domain_sid,
2167 const char *domain_name,
2168 struct cli_state *cli,
2169 struct rpc_pipe_client *pipe_hnd,
2170 TALLOC_CTX *mem_ctx,
2171 int argc,
2172 const char **argv)
2175 /* FIXME: Here the nightmare begins */
2177 WERROR result;
2178 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2179 uint32_t i = 0, p = 0, j = 0;
2180 uint32_t num_printers;
2181 uint32_t level = 2;
2182 const char *printername, *sharename;
2183 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2184 struct policy_handle hnd_src, hnd_dst;
2185 union spoolss_PrinterInfo *info_enum;
2186 union spoolss_PrinterInfo info_dst_publish;
2187 union spoolss_PrinterInfo info_dst;
2188 struct cli_state *cli_dst = NULL;
2189 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2190 const char *longname;
2191 const char **keylist = NULL;
2193 /* FIXME GD */
2194 ZERO_STRUCT(info_dst_publish);
2196 DEBUG(3,("copying printer settings\n"));
2198 /* connect destination PI_SPOOLSS */
2199 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2200 &ndr_table_spoolss.syntax_id);
2201 if (!NT_STATUS_IS_OK(nt_status))
2202 return nt_status;
2204 /* enum src printers */
2205 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2206 nt_status = NT_STATUS_UNSUCCESSFUL;
2207 goto done;
2210 if (!num_printers) {
2211 printf (_("no printers found on server.\n"));
2212 nt_status = NT_STATUS_OK;
2213 goto done;
2217 /* needed for dns-strings in regkeys */
2218 longname = get_mydnsfullname();
2219 if (!longname) {
2220 nt_status = NT_STATUS_UNSUCCESSFUL;
2221 goto done;
2224 /* do something for all printers */
2225 for (i = 0; i < num_printers; i++) {
2227 uint32_t value_offered = 0, value_needed;
2228 uint32_t data_offered = 0, data_needed;
2229 enum winreg_Type type;
2230 uint8_t *buffer = NULL;
2231 const char *value_name = NULL;
2233 /* do some initialization */
2234 printername = info_enum[i].info2.printername;
2235 sharename = info_enum[i].info2.sharename;
2237 if (!printername || !sharename) {
2238 nt_status = NT_STATUS_UNSUCCESSFUL;
2239 goto done;
2241 /* we can reset NT_STATUS here because we do not
2242 get any real NT_STATUS-codes anymore from now on */
2243 nt_status = NT_STATUS_UNSUCCESSFUL;
2245 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2246 printername, sharename);
2249 /* open src printer handle */
2250 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2251 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2252 goto done;
2254 /* open dst printer handle */
2255 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2256 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2257 goto done;
2259 /* check for existing dst printer */
2260 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2261 level, &info_dst))
2262 goto done;
2265 /* STEP 1: COPY DEVICE-MODE and other
2266 PRINTER_INFO_2-attributes
2269 info_dst.info2 = info_enum[i].info2;
2271 /* why is the port always disconnected when the printer
2272 is correctly installed (incl. driver ???) */
2273 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2275 /* check if printer is published */
2276 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2278 /* check for existing dst printer */
2279 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2280 goto done;
2282 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2284 /* ignore false from setprinter due to WERR_IO_PENDING */
2285 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2287 DEBUG(3,("republished printer\n"));
2290 if (info_enum[i].info2.devmode != NULL) {
2292 /* copy devmode (info level 2) */
2293 info_dst.info2.devmode = info_enum[i].info2.devmode;
2295 /* do not copy security descriptor (we have another
2296 * command for that) */
2297 info_dst.info2.secdesc = NULL;
2299 #if 0
2300 info_dst.info2.devmode.devicename =
2301 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2302 longname, printername);
2303 if (!info_dst.info2.devmode.devicename) {
2304 nt_status = NT_STATUS_NO_MEMORY;
2305 goto done;
2307 #endif
2308 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2309 level, &info_dst))
2310 goto done;
2312 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2315 /* STEP 2: COPY REGISTRY VALUES */
2317 /* please keep in mind that samba parse_spools gives horribly
2318 crippled results when used to rpccli_spoolss_enumprinterdataex
2319 a win2k3-server. (Bugzilla #1851)
2320 FIXME: IIRC I've seen it too on a win2k-server
2323 /* enumerate data on src handle */
2324 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2325 &hnd_src,
2327 value_name,
2328 value_offered,
2329 &value_needed,
2330 &type,
2331 buffer,
2332 data_offered,
2333 &data_needed,
2334 &result);
2336 data_offered = data_needed;
2337 value_offered = value_needed;
2338 buffer = talloc_zero_array(mem_ctx, uint8_t, data_needed);
2339 value_name = talloc_zero_array(mem_ctx, char, value_needed);
2341 /* loop for all printerdata of "PrinterDriverData" */
2342 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2344 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2345 &hnd_src,
2346 p++,
2347 value_name,
2348 value_offered,
2349 &value_needed,
2350 &type,
2351 buffer,
2352 data_offered,
2353 &data_needed,
2354 &result);
2355 /* loop for all reg_keys */
2356 if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2358 /* display_value */
2359 if (c->opt_verbose) {
2360 struct regval_blob *v;
2362 v = regval_compose(talloc_tos(),
2363 value_name,
2364 type,
2365 buffer,
2366 data_offered);
2367 if (v == NULL) {
2368 nt_status = NT_STATUS_NO_MEMORY;
2369 goto done;
2372 display_reg_value(SPOOL_PRINTERDATA_KEY, v);
2373 talloc_free(v);
2376 /* set_value */
2377 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2378 &hnd_dst, value_name,
2379 type, buffer, data_offered))
2380 goto done;
2382 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2383 value_name));
2387 /* STEP 3: COPY SUBKEY VALUES */
2389 /* here we need to enum all printer_keys and then work
2390 on the result with enum_printer_key_ex. nt4 does not
2391 respond to enumprinterkey, win2k does, so continue
2392 in case of an error */
2394 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2395 printf(_("got no key-data\n"));
2396 continue;
2400 /* work on a list of printer keys
2401 each key has to be enumerated to get all required
2402 information. information is then set via setprinterdataex-calls */
2404 if (keylist == NULL)
2405 continue;
2407 for (i=0; keylist && keylist[i] != NULL; i++) {
2409 const char *subkey = keylist[i];
2410 uint32_t count;
2411 struct spoolss_PrinterEnumValues *info;
2413 /* enumerate all src subkeys */
2414 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2415 &hnd_src, subkey,
2416 &count, &info)) {
2417 goto done;
2420 for (j=0; j < count; j++) {
2422 struct regval_blob *value;
2423 DATA_BLOB blob;
2425 ZERO_STRUCT(blob);
2427 /* although samba replies with sane data in most cases we
2428 should try to avoid writing wrong registry data */
2430 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME) ||
2431 strequal(info[j].value_name, SPOOL_REG_UNCNAME) ||
2432 strequal(info[j].value_name, SPOOL_REG_URL) ||
2433 strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME) ||
2434 strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2436 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
2438 /* although windows uses a multi-sz, we use a sz */
2439 push_reg_sz(mem_ctx, &blob, SAMBA_PRINTER_PORT_NAME);
2442 if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
2444 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2445 nt_status = NT_STATUS_NO_MEMORY;
2446 goto done;
2448 push_reg_sz(mem_ctx, &blob, unc_name);
2451 if (strequal(info[j].value_name, SPOOL_REG_URL)) {
2453 continue;
2455 #if 0
2456 /* FIXME: should we really do that ??? */
2457 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2458 nt_status = NT_STATUS_NO_MEMORY;
2459 goto done;
2461 push_reg_sz(mem_ctx, NULL, &blob, url);
2462 fstrcpy(value.valuename, SPOOL_REG_URL);
2463 #endif
2466 if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2468 push_reg_sz(mem_ctx, &blob, longname);
2471 if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
2473 push_reg_sz(mem_ctx, &blob, global_myname());
2476 value = regval_compose(talloc_tos(),
2477 info[j].value_name,
2478 REG_SZ,
2479 blob.length == 0 ? NULL : blob.data,
2480 blob.length);
2481 if (value == NULL) {
2482 nt_status = NT_STATUS_NO_MEMORY;
2483 goto done;
2486 if (c->opt_verbose)
2487 display_reg_value(subkey, value);
2489 /* here we have to set all subkeys on the dst server */
2490 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2491 subkey, value))
2493 talloc_free(value);
2494 goto done;
2497 talloc_free(value);
2498 } else {
2500 struct regval_blob *v;
2502 v = regval_compose(talloc_tos(),
2503 info[j].value_name,
2504 info[j].type,
2505 info[j].data->data,
2506 info[j].data->length);
2507 if (v == NULL) {
2508 nt_status = NT_STATUS_NO_MEMORY;
2509 goto done;
2512 if (c->opt_verbose) {
2513 display_reg_value(subkey, v);
2516 /* here we have to set all subkeys on the dst server */
2517 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2518 subkey, v)) {
2519 goto done;
2522 talloc_free(v);
2525 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2526 subkey, info[j].value_name));
2531 TALLOC_FREE(keylist);
2533 /* close printer handles here */
2534 if (is_valid_policy_hnd(&hnd_src)) {
2535 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2538 if (is_valid_policy_hnd(&hnd_dst)) {
2539 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2544 nt_status = NT_STATUS_OK;
2546 done:
2547 SAFE_FREE(devicename);
2548 SAFE_FREE(url);
2549 SAFE_FREE(unc_name);
2551 if (is_valid_policy_hnd(&hnd_src))
2552 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2554 if (is_valid_policy_hnd(&hnd_dst))
2555 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2557 if (cli_dst) {
2558 cli_shutdown(cli_dst);
2560 return nt_status;