s3: Fix bug #9085.
[Samba.git] / source3 / utils / net_rpc_printer.c
blob8b6d73eb525f26f2aea3d37972b2d811b63f615b
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"
23 /* support itanium as well */
24 static const struct print_architecture_table_node archi_table[]= {
26 {"Windows 4.0", "WIN40", 0 },
27 {"Windows NT x86", "W32X86", 2 },
28 {"Windows NT x86", "W32X86", 3 },
29 {"Windows NT R4000", "W32MIPS", 2 },
30 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
31 {"Windows NT PowerPC", "W32PPC", 2 },
32 {"Windows IA64", "IA64", 3 },
33 {"Windows x64", "x64", 3 },
34 {NULL, "", -1 }
38 /**
39 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
40 * It is here for debugging purpose and should be removed later on.
41 **/
43 /****************************************************************************
44 Printer info level 3 display function.
45 ****************************************************************************/
47 static void display_print_driver3(struct spoolss_DriverInfo3 *r)
49 int i;
51 if (!r) {
52 return;
55 printf(_("Printer Driver Info 3:\n"));
56 printf(_("\tVersion: [%x]\n"), r->version);
57 printf(_("\tDriver Name: [%s]\n"), r->driver_name);
58 printf(_("\tArchitecture: [%s]\n"), r->architecture);
59 printf(_("\tDriver Path: [%s]\n"), r->driver_path);
60 printf(_("\tDatafile: [%s]\n"), r->data_file);
61 printf(_("\tConfigfile: [%s]\n\n"), r->config_file);
62 printf(_("\tHelpfile: [%s]\n\n"), r->help_file);
64 for (i=0; r->dependent_files[i] != NULL; i++) {
65 printf(_("\tDependentfiles: [%s]\n"), r->dependent_files[i]);
68 printf("\n");
70 printf(_("\tMonitorname: [%s]\n"), r->monitor_name);
71 printf(_("\tDefaultdatatype: [%s]\n\n"), r->default_datatype);
74 static void display_reg_value(const char *subkey, struct regval_blob value)
76 const char *text;
77 DATA_BLOB blob;
79 switch(value.type) {
80 case REG_DWORD:
81 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey,
82 value.valuename, *((uint32_t *) value.data_p));
83 break;
85 case REG_SZ:
86 blob = data_blob_const(value.data_p, value.size);
87 pull_reg_sz(talloc_tos(), &blob, &text);
88 if (!text) {
89 break;
91 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, value.valuename,
92 text);
93 break;
95 case REG_BINARY:
96 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
97 "displayed\n"),
98 subkey, value.valuename);
99 break;
101 case REG_MULTI_SZ: {
102 uint32_t i;
103 const char **values;
104 blob = data_blob_const(value.data_p, value.size);
106 if (!pull_reg_multi_sz(NULL, &blob, &values)) {
107 d_printf("pull_reg_multi_sz failed\n");
108 break;
111 printf("%s: REG_MULTI_SZ: \n", value.valuename);
112 for (i=0; values[i] != NULL; i++) {
113 d_printf("%s\n", values[i]);
115 TALLOC_FREE(values);
116 break;
119 default:
120 d_printf(_("\t%s: unknown type %d\n"), value.valuename,
121 value.type);
127 * Copies ACLs, DOS-attributes and timestamps from one
128 * file or directory from one connected share to another connected share
130 * @param c A net_context structure
131 * @param mem_ctx A talloc-context
132 * @param cli_share_src A connected cli_state
133 * @param cli_share_dst A connected cli_state
134 * @param src_file The source file-name
135 * @param dst_file The destination file-name
136 * @param copy_acls Whether to copy acls
137 * @param copy_attrs Whether to copy DOS attributes
138 * @param copy_timestamps Whether to preserve timestamps
139 * @param is_file Whether this file is a file or a dir
141 * @return Normal NTSTATUS return.
144 NTSTATUS net_copy_fileattr(struct net_context *c,
145 TALLOC_CTX *mem_ctx,
146 struct cli_state *cli_share_src,
147 struct cli_state *cli_share_dst,
148 const char *src_name, const char *dst_name,
149 bool copy_acls, bool copy_attrs,
150 bool copy_timestamps, bool is_file)
152 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
153 uint16_t fnum_src = 0;
154 uint16_t fnum_dst = 0;
155 SEC_DESC *sd = NULL;
156 uint16_t attr;
157 time_t f_atime, f_ctime, f_mtime;
160 if (!copy_timestamps && !copy_acls && !copy_attrs)
161 return NT_STATUS_OK;
163 /* open file/dir on the originating server */
165 DEBUGADD(3,("opening %s %s on originating server\n",
166 is_file?"file":"dir", src_name));
168 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
169 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src))) {
170 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
171 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
172 nt_status = cli_nt_error(cli_share_src);
173 goto out;
177 if (copy_acls) {
179 /* get the security descriptor */
180 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
181 if (!sd) {
182 DEBUG(0,("failed to get security descriptor: %s\n",
183 cli_errstr(cli_share_src)));
184 nt_status = cli_nt_error(cli_share_src);
185 goto out;
188 if (c->opt_verbose && DEBUGLEVEL >= 3)
189 display_sec_desc(sd);
193 if (copy_attrs || copy_timestamps) {
195 /* get file attributes */
196 if (!NT_STATUS_IS_OK(cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
197 &f_ctime, &f_atime, &f_mtime))) {
198 DEBUG(0,("failed to get file-attrs: %s\n",
199 cli_errstr(cli_share_src)));
200 nt_status = cli_nt_error(cli_share_src);
201 goto out;
206 /* open the file/dir on the destination server */
208 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_dst, dst_name, 0, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
209 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_dst))) {
210 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
211 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
212 nt_status = cli_nt_error(cli_share_dst);
213 goto out;
216 if (copy_timestamps) {
218 /* set timestamps */
219 if (!NT_STATUS_IS_OK(cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime))) {
220 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
221 cli_errstr(cli_share_dst)));
222 nt_status = cli_nt_error(cli_share_dst);
223 goto out;
227 if (copy_acls) {
229 /* set acls */
230 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
231 DEBUG(0,("could not set secdesc on %s: %s\n",
232 dst_name, cli_errstr(cli_share_dst)));
233 nt_status = cli_nt_error(cli_share_dst);
234 goto out;
238 if (copy_attrs) {
240 /* set attrs */
241 if (!NT_STATUS_IS_OK(cli_setatr(cli_share_dst, dst_name, attr, 0))) {
242 DEBUG(0,("failed to set file-attrs: %s\n",
243 cli_errstr(cli_share_dst)));
244 nt_status = cli_nt_error(cli_share_dst);
245 goto out;
250 /* closing files */
252 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
253 d_fprintf(stderr,
254 _("could not close %s on originating server: %s\n"),
255 is_file?"file":"dir", cli_errstr(cli_share_src));
256 nt_status = cli_nt_error(cli_share_src);
257 goto out;
260 if (!NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
261 d_fprintf(stderr,
262 _("could not close %s on destination server: %s\n"),
263 is_file?"file":"dir", cli_errstr(cli_share_dst));
264 nt_status = cli_nt_error(cli_share_dst);
265 goto out;
269 nt_status = NT_STATUS_OK;
271 out:
273 /* cleaning up */
274 if (fnum_src)
275 cli_close(cli_share_src, fnum_src);
277 if (fnum_dst)
278 cli_close(cli_share_dst, fnum_dst);
280 return nt_status;
284 * Copy a file or directory from a connected share to another connected share
286 * @param c A net_context structure
287 * @param mem_ctx A talloc-context
288 * @param cli_share_src A connected cli_state
289 * @param cli_share_dst A connected cli_state
290 * @param src_file The source file-name
291 * @param dst_file The destination file-name
292 * @param copy_acls Whether to copy acls
293 * @param copy_attrs Whether to copy DOS attributes
294 * @param copy_timestamps Whether to preserve timestamps
295 * @param is_file Whether this file is a file or a dir
297 * @return Normal NTSTATUS return.
300 NTSTATUS net_copy_file(struct net_context *c,
301 TALLOC_CTX *mem_ctx,
302 struct cli_state *cli_share_src,
303 struct cli_state *cli_share_dst,
304 const char *src_name, const char *dst_name,
305 bool copy_acls, bool copy_attrs,
306 bool copy_timestamps, bool is_file)
308 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
309 uint16_t fnum_src = 0;
310 uint16_t fnum_dst = 0;
311 static int io_bufsize = 64512;
312 int read_size = io_bufsize;
313 char *data = NULL;
314 off_t nread = 0;
317 if (!src_name || !dst_name)
318 goto out;
320 if (cli_share_src == NULL || cli_share_dst == NULL)
321 goto out;
323 /* open on the originating server */
324 DEBUGADD(3,("opening %s %s on originating server\n",
325 is_file ? "file":"dir", src_name));
326 if (is_file)
327 nt_status = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE, &fnum_src);
328 else
329 nt_status = cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
330 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src);
332 if (!NT_STATUS_IS_OK(nt_status)) {
333 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
334 is_file ? "file":"dir",
335 src_name, cli_errstr(cli_share_src)));
336 goto out;
340 if (is_file) {
342 /* open file on the destination server */
343 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
344 nt_status = cli_open(cli_share_dst, dst_name,
345 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum_dst);
347 if (!NT_STATUS_IS_OK(nt_status)) {
348 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
349 dst_name, cli_errstr(cli_share_dst)));
350 goto out;
353 /* allocate memory */
354 if (!(data = (char *)SMB_MALLOC(read_size))) {
355 d_fprintf(stderr, _("malloc fail for size %d\n"),
356 read_size);
357 nt_status = NT_STATUS_NO_MEMORY;
358 goto out;
364 if (c->opt_verbose) {
366 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
367 "%s ACLs and %s DOS Attributes %s\n"),
368 cli_share_src->desthost, cli_share_src->share, src_name,
369 cli_share_dst->desthost, cli_share_dst->share, dst_name,
370 copy_acls ? _("with") : _("without"),
371 copy_attrs ? _("with") : _("without"),
372 copy_timestamps ? _("(preserving timestamps)") : "" );
376 while (is_file) {
378 /* copying file */
379 int n, ret;
380 n = cli_read(cli_share_src, fnum_src, data, nread,
381 read_size);
383 if (n <= 0)
384 break;
386 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
387 nread, n);
389 if (n != ret) {
390 d_fprintf(stderr, _("Error writing file: %s\n"),
391 cli_errstr(cli_share_dst));
392 nt_status = cli_nt_error(cli_share_dst);
393 goto out;
396 nread += n;
400 if (!is_file && !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
402 /* creating dir */
403 DEBUGADD(3,("creating dir %s on the destination server\n",
404 dst_name));
406 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share_dst, dst_name))) {
407 DEBUG(0,("cannot create directory %s: %s\n",
408 dst_name, cli_errstr(cli_share_dst)));
409 nt_status = NT_STATUS_NO_SUCH_FILE;
412 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
413 d_fprintf(stderr,
414 _("cannot check for directory %s: %s\n"),
415 dst_name, cli_errstr(cli_share_dst));
416 goto out;
421 /* closing files */
422 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
423 d_fprintf(stderr,
424 _("could not close file on originating server: %s\n"),
425 cli_errstr(cli_share_src));
426 nt_status = cli_nt_error(cli_share_src);
427 goto out;
430 if (is_file && !NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
431 d_fprintf(stderr,
432 _("could not close file on destination server: %s\n"),
433 cli_errstr(cli_share_dst));
434 nt_status = cli_nt_error(cli_share_dst);
435 goto out;
438 /* possibly we have to copy some file-attributes / acls / sd */
439 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
440 src_name, dst_name, copy_acls,
441 copy_attrs, copy_timestamps, is_file);
442 if (!NT_STATUS_IS_OK(nt_status))
443 goto out;
446 nt_status = NT_STATUS_OK;
448 out:
450 /* cleaning up */
451 if (fnum_src)
452 cli_close(cli_share_src, fnum_src);
454 if (fnum_dst)
455 cli_close(cli_share_dst, fnum_dst);
457 SAFE_FREE(data);
459 return nt_status;
463 * Copy a driverfile from on connected share to another connected share
464 * This silently assumes that a driver-file is picked up from
466 * \\src_server\print$\{arch}\{version}\file
468 * and copied to
470 * \\dst_server\print$\{arch}\file
472 * to be added via setdriver-calls later.
473 * @param c A net_context structure
474 * @param mem_ctx A talloc-context
475 * @param cli_share_src A cli_state connected to source print$-share
476 * @param cli_share_dst A cli_state connected to destination print$-share
477 * @param file The file-name to be copied
478 * @param short_archi The name of the driver-architecture (short form)
480 * @return Normal NTSTATUS return.
483 static NTSTATUS net_copy_driverfile(struct net_context *c,
484 TALLOC_CTX *mem_ctx,
485 struct cli_state *cli_share_src,
486 struct cli_state *cli_share_dst,
487 const char *file, const char *short_archi) {
489 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
490 const char *p;
491 char *src_name;
492 char *dst_name;
493 char *version;
494 char *filename;
495 char *tok;
497 if (!file) {
498 return NT_STATUS_OK;
501 /* scroll through the file until we have the part
502 beyond archi_table.short_archi */
503 p = file;
504 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
505 if (strequal(tok, short_archi)) {
506 next_token_talloc(mem_ctx, &p, &version, "\\");
507 next_token_talloc(mem_ctx, &p, &filename, "\\");
511 /* build source file name */
512 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
513 return NT_STATUS_NO_MEMORY;
516 /* create destination file name */
517 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
518 return NT_STATUS_NO_MEMORY;
521 /* finally copy the file */
522 nt_status = net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
523 src_name, dst_name, false, false, false, true);
524 if (!NT_STATUS_IS_OK(nt_status))
525 goto out;
527 nt_status = NT_STATUS_OK;
529 out:
530 SAFE_FREE(src_name);
531 SAFE_FREE(dst_name);
533 return nt_status;
537 * Check for existing Architecture directory on a given server
539 * @param cli_share A cli_state connected to a print$-share
540 * @param short_archi The Architecture for the print-driver
542 * @return Normal NTSTATUS return.
545 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
548 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
549 char *dir;
551 if (asprintf(&dir, "\\%s", short_archi) < 0) {
552 return NT_STATUS_NO_MEMORY;
555 DEBUG(10,("creating print-driver dir for architecture: %s\n",
556 short_archi));
558 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share, dir))) {
559 DEBUG(1,("cannot create directory %s: %s\n",
560 dir, cli_errstr(cli_share)));
561 nt_status = NT_STATUS_NO_SUCH_FILE;
564 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share, dir))) {
565 d_fprintf(stderr, _("cannot check %s: %s\n"),
566 dir, cli_errstr(cli_share));
567 goto out;
570 nt_status = NT_STATUS_OK;
572 out:
573 SAFE_FREE(dir);
574 return nt_status;
578 * Copy a print-driver (level 3) from one connected print$-share to another
579 * connected print$-share
581 * @param c A net_context structure
582 * @param mem_ctx A talloc-context
583 * @param cli_share_src A cli_state connected to a print$-share
584 * @param cli_share_dst A cli_state connected to a print$-share
585 * @param short_archi The Architecture for the print-driver
586 * @param i1 The DRIVER_INFO_3-struct
588 * @return Normal NTSTATUS return.
591 static NTSTATUS copy_print_driver_3(struct net_context *c,
592 TALLOC_CTX *mem_ctx,
593 struct cli_state *cli_share_src,
594 struct cli_state *cli_share_dst,
595 const char *short_archi,
596 struct spoolss_DriverInfo3 *r)
598 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
599 int i;
601 if (r == NULL) {
602 return nt_status;
605 if (c->opt_verbose)
606 d_printf(_("copying driver: [%s], for architecture: [%s], "
607 "version: [%d]\n"),
608 r->driver_name, short_archi, r->version);
610 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
611 r->driver_path, short_archi);
612 if (!NT_STATUS_IS_OK(nt_status))
613 return nt_status;
615 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
616 r->data_file, short_archi);
617 if (!NT_STATUS_IS_OK(nt_status))
618 return nt_status;
620 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
621 r->config_file, short_archi);
622 if (!NT_STATUS_IS_OK(nt_status))
623 return nt_status;
625 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
626 r->help_file, short_archi);
627 if (!NT_STATUS_IS_OK(nt_status))
628 return nt_status;
630 for (i=0; r->dependent_files[i] != NULL; i++) {
632 nt_status = net_copy_driverfile(c, mem_ctx,
633 cli_share_src, cli_share_dst,
634 r->dependent_files[i], short_archi);
635 if (!NT_STATUS_IS_OK(nt_status)) {
636 return nt_status;
640 return NT_STATUS_OK;
644 * net_spoolss-functions
645 * =====================
647 * the net_spoolss-functions aim to simplify spoolss-client-functions
648 * required during the migration-process wrt buffer-sizes, returned
649 * error-codes, etc.
651 * this greatly reduces the complexitiy of the migrate-functions.
655 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
656 TALLOC_CTX *mem_ctx,
657 char *name,
658 uint32_t flags,
659 uint32_t level,
660 uint32_t *num_printers,
661 union spoolss_PrinterInfo **info)
663 WERROR result;
665 /* enum printers */
667 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
668 flags,
669 name,
670 level,
672 num_printers,
673 info);
674 if (!W_ERROR_IS_OK(result)) {
675 printf(_("cannot enum printers: %s\n"), win_errstr(result));
676 return false;
679 return true;
682 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
683 TALLOC_CTX *mem_ctx,
684 const char *printername,
685 uint32_t access_required,
686 const char *username,
687 struct policy_handle *hnd)
689 WERROR result;
690 fstring printername2;
692 fstrcpy(printername2, pipe_hnd->srv_name_slash);
693 fstrcat(printername2, "\\");
694 fstrcat(printername2, printername);
696 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
697 pipe_hnd->srv_name_slash, username, printername2, access_required));
699 /* open printer */
700 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
701 printername2,
702 access_required,
703 hnd);
705 /* be more verbose */
706 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
707 d_fprintf(stderr,
708 _("no access to printer [%s] on [%s] for user [%s] "
709 "granted\n"),
710 printername2, pipe_hnd->srv_name_slash, username);
711 return false;
714 if (!W_ERROR_IS_OK(result)) {
715 d_fprintf(stderr,_("cannot open printer %s on server %s: %s\n"),
716 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
717 return false;
720 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
721 printername2, pipe_hnd->srv_name_slash));
723 return true;
726 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
727 TALLOC_CTX *mem_ctx,
728 struct policy_handle *hnd,
729 uint32_t level,
730 union spoolss_PrinterInfo *info)
732 WERROR result;
734 /* getprinter call */
735 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
736 hnd,
737 level,
738 0, /* offered */
739 info);
740 if (!W_ERROR_IS_OK(result)) {
741 printf(_("cannot get printer-info: %s\n"), win_errstr(result));
742 return false;
745 return true;
748 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
749 TALLOC_CTX *mem_ctx,
750 struct policy_handle *hnd,
751 uint32_t level,
752 union spoolss_PrinterInfo *info)
754 WERROR result;
755 NTSTATUS status;
756 struct spoolss_SetPrinterInfoCtr info_ctr;
757 struct spoolss_SetPrinterInfo2 info2;
758 struct spoolss_DevmodeContainer devmode_ctr;
759 struct sec_desc_buf secdesc_ctr;
761 ZERO_STRUCT(devmode_ctr);
762 ZERO_STRUCT(secdesc_ctr);
764 /* setprinter call */
766 info_ctr.level = level;
767 switch (level) {
768 case 0:
769 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)
770 (void *)&info->info0;
771 break;
772 case 1:
773 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)
774 (void *)&info->info1;
775 break;
776 case 2:
777 spoolss_printerinfo2_to_setprinterinfo2(&info->info2, &info2);
778 info_ctr.info.info2 = &info2;
779 break;
780 case 3:
781 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
782 (void *)&info->info3;
783 break;
784 case 4:
785 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)
786 (void *)&info->info4;
787 break;
788 case 5:
789 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)
790 (void *)&info->info5;
791 break;
792 case 6:
793 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)
794 (void *)&info->info6;
795 break;
796 case 7:
797 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
798 (void *)&info->info7;
799 break;
800 #if 0 /* FIXME GD */
801 case 8:
802 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)
803 (void *)&info->info8;
804 break;
805 case 9:
806 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)
807 (void *)&info->info9;
808 break;
809 #endif
810 default:
811 break; /* FIXME */
814 status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
815 hnd,
816 &info_ctr,
817 &devmode_ctr,
818 &secdesc_ctr,
819 0, /* command */
820 &result);
822 if (!W_ERROR_IS_OK(result)) {
823 printf(_("cannot set printer-info: %s\n"), win_errstr(result));
824 return false;
827 return true;
831 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
832 TALLOC_CTX *mem_ctx,
833 struct policy_handle *hnd,
834 const char *value_name,
835 enum winreg_Type type,
836 uint8_t *data,
837 uint32_t offered)
839 WERROR result;
840 NTSTATUS status;
842 /* setprinterdata call */
843 status = rpccli_spoolss_SetPrinterData(pipe_hnd, mem_ctx,
844 hnd,
845 value_name,
846 type,
847 data,
848 offered,
849 &result);
851 if (!W_ERROR_IS_OK(result)) {
852 printf (_("unable to set printerdata: %s\n"),
853 win_errstr(result));
854 return false;
857 return true;
861 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
862 TALLOC_CTX *mem_ctx,
863 struct policy_handle *hnd,
864 const char *keyname,
865 const char ***keylist)
867 WERROR result;
869 /* enumprinterkey call */
870 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
872 if (!W_ERROR_IS_OK(result)) {
873 printf(_("enumprinterkey failed: %s\n"), win_errstr(result));
874 return false;
877 return true;
880 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
881 TALLOC_CTX *mem_ctx,
882 uint32_t offered,
883 struct policy_handle *hnd,
884 const char *keyname,
885 uint32_t *count,
886 struct spoolss_PrinterEnumValues **info)
888 WERROR result;
890 /* enumprinterdataex call */
891 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
892 hnd,
893 keyname,
894 0, /* offered */
895 count,
896 info);
898 if (!W_ERROR_IS_OK(result)) {
899 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result));
900 return false;
903 return true;
907 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
908 TALLOC_CTX *mem_ctx,
909 struct policy_handle *hnd,
910 const char *keyname,
911 struct regval_blob *value)
913 WERROR result;
914 NTSTATUS status;
916 /* setprinterdataex call */
917 status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
918 hnd,
919 keyname,
920 value->valuename,
921 value->type,
922 value->data_p,
923 value->size,
924 &result);
926 if (!W_ERROR_IS_OK(result)) {
927 printf(_("could not set printerdataex: %s\n"),
928 win_errstr(result));
929 return false;
932 return true;
935 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
936 TALLOC_CTX *mem_ctx,
937 struct policy_handle *hnd,
938 int level,
939 uint32_t *num_forms,
940 union spoolss_FormInfo **forms)
942 WERROR result;
944 /* enumforms call */
945 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
946 hnd,
947 level,
949 num_forms,
950 forms);
951 if (!W_ERROR_IS_OK(result)) {
952 printf(_("could not enum forms: %s\n"), win_errstr(result));
953 return false;
956 return true;
959 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
960 TALLOC_CTX *mem_ctx,
961 uint32_t level, const char *env,
962 uint32_t *count,
963 union spoolss_DriverInfo **info)
965 WERROR result;
967 /* enumprinterdrivers call */
968 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
969 pipe_hnd->srv_name_slash,
970 env,
971 level,
973 count,
974 info);
975 if (!W_ERROR_IS_OK(result)) {
976 printf(_("cannot enum drivers: %s\n"), win_errstr(result));
977 return false;
980 return true;
983 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
984 TALLOC_CTX *mem_ctx,
985 struct policy_handle *hnd, uint32_t level,
986 const char *env, int version,
987 union spoolss_DriverInfo *info)
989 WERROR result;
990 uint32_t server_major_version;
991 uint32_t server_minor_version;
993 /* getprinterdriver call */
994 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
995 hnd,
996 env,
997 level,
999 version,
1001 info,
1002 &server_major_version,
1003 &server_minor_version);
1004 if (!W_ERROR_IS_OK(result)) {
1005 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1006 env, win_errstr(result)));
1007 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1008 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1009 printf(_("cannot get driver: %s\n"),
1010 win_errstr(result));
1012 return false;
1015 return true;
1019 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1020 TALLOC_CTX *mem_ctx, uint32_t level,
1021 union spoolss_DriverInfo *info)
1023 WERROR result;
1024 NTSTATUS status;
1025 struct spoolss_AddDriverInfoCtr info_ctr;
1027 info_ctr.level = level;
1029 switch (level) {
1030 case 2:
1031 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)
1032 (void *)&info->info2;
1033 break;
1034 case 3:
1035 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)
1036 (void *)&info->info3;
1037 break;
1038 default:
1039 printf(_("unsupported info level: %d\n"), level);
1040 return false;
1043 /* addprinterdriver call */
1044 status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1045 pipe_hnd->srv_name_slash,
1046 &info_ctr,
1047 &result);
1048 /* be more verbose */
1049 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1050 printf(_("You are not allowed to add drivers\n"));
1051 return false;
1053 if (!W_ERROR_IS_OK(result)) {
1054 printf(_("cannot add driver: %s\n"), win_errstr(result));
1055 return false;
1058 return true;
1062 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1063 * for a single printer or for all printers depending on argc/argv
1066 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1067 TALLOC_CTX *mem_ctx,
1068 int level,
1069 int argc,
1070 const char **argv,
1071 uint32_t *num_printers,
1072 union spoolss_PrinterInfo **info_p)
1074 struct policy_handle hnd;
1076 /* no arguments given, enumerate all printers */
1077 if (argc == 0) {
1079 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1080 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1081 level, num_printers, info_p))
1082 return false;
1084 goto out;
1087 /* argument given, get a single printer by name */
1088 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1089 MAXIMUM_ALLOWED_ACCESS,
1090 pipe_hnd->auth->user_name,
1091 &hnd))
1092 return false;
1094 *info_p = talloc_zero(mem_ctx, union spoolss_PrinterInfo);
1095 if (*info_p == NULL) {
1096 return false;
1099 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1100 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1101 return false;
1104 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1106 *num_printers = 1;
1108 out:
1109 DEBUG(3,("got %d printers\n", *num_printers));
1111 return true;
1116 * List print-queues (including local printers that are not shared)
1118 * All parameters are provided by the run_rpc_command function, except for
1119 * argc, argv which are passed through.
1121 * @param c A net_context structure
1122 * @param domain_sid The domain sid aquired from the remote server
1123 * @param cli A cli_state connected to the server.
1124 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1125 * @param argc Standard main() style argc
1126 * @param argv Standard main() style argv. Initial components are already
1127 * stripped
1129 * @return Normal NTSTATUS return.
1132 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1133 const DOM_SID *domain_sid,
1134 const char *domain_name,
1135 struct cli_state *cli,
1136 struct rpc_pipe_client *pipe_hnd,
1137 TALLOC_CTX *mem_ctx,
1138 int argc,
1139 const char **argv)
1141 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1142 uint32_t i, num_printers;
1143 uint32_t level = 2;
1144 const char *printername, *sharename;
1145 union spoolss_PrinterInfo *info;
1147 printf("listing printers\n");
1149 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1150 return nt_status;
1152 for (i = 0; i < num_printers; i++) {
1154 /* do some initialization */
1155 printername = info[i].info2.printername;
1156 sharename = info[i].info2.sharename;
1158 if (printername && sharename) {
1159 d_printf(_("printer %d: %s, shared as: %s\n"),
1160 i+1, printername, sharename);
1164 return NT_STATUS_OK;
1168 * List printer-drivers from a server
1170 * All parameters are provided by the run_rpc_command function, except for
1171 * argc, argv which are passed through.
1173 * @param c A net_context structure
1174 * @param domain_sid The domain sid aquired from the remote server
1175 * @param cli A cli_state connected to the server.
1176 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1177 * @param argc Standard main() style argc
1178 * @param argv Standard main() style argv. Initial components are already
1179 * stripped
1181 * @return Normal NTSTATUS return.
1184 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1185 const DOM_SID *domain_sid,
1186 const char *domain_name,
1187 struct cli_state *cli,
1188 struct rpc_pipe_client *pipe_hnd,
1189 TALLOC_CTX *mem_ctx,
1190 int argc,
1191 const char **argv)
1193 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1194 uint32_t i;
1195 uint32_t level = 3;
1196 union spoolss_DriverInfo *info;
1197 int d;
1199 printf(_("listing printer-drivers\n"));
1201 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1203 uint32_t num_drivers;
1205 /* enum remote drivers */
1206 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1207 archi_table[i].long_archi,
1208 &num_drivers, &info)) {
1209 nt_status = NT_STATUS_UNSUCCESSFUL;
1210 goto done;
1213 if (num_drivers == 0) {
1214 d_printf(_("no drivers found on server for "
1215 "architecture: [%s].\n"),
1216 archi_table[i].long_archi);
1217 continue;
1220 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1221 num_drivers, archi_table[i].long_archi);
1224 /* do something for all drivers for architecture */
1225 for (d = 0; d < num_drivers; d++) {
1226 display_print_driver3(&info[d].info3);
1230 nt_status = NT_STATUS_OK;
1232 done:
1233 return nt_status;
1238 * Publish print-queues with args-wrapper
1240 * @param cli A cli_state connected to the server.
1241 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1242 * @param argc Standard main() style argc
1243 * @param argv Standard main() style argv. Initial components are already
1244 * stripped
1245 * @param action
1247 * @return Normal NTSTATUS return.
1250 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1251 TALLOC_CTX *mem_ctx,
1252 int argc,
1253 const char **argv,
1254 uint32_t action)
1256 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1257 uint32_t i, num_printers;
1258 uint32_t level = 7;
1259 const char *printername, *sharename;
1260 union spoolss_PrinterInfo *info_enum;
1261 union spoolss_PrinterInfo info;
1262 struct spoolss_SetPrinterInfoCtr info_ctr;
1263 struct spoolss_DevmodeContainer devmode_ctr;
1264 struct sec_desc_buf secdesc_ctr;
1265 struct policy_handle hnd;
1266 WERROR result;
1267 const char *action_str;
1269 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1270 return nt_status;
1272 for (i = 0; i < num_printers; i++) {
1274 /* do some initialization */
1275 printername = info_enum[i].info2.printername;
1276 sharename = info_enum[i].info2.sharename;
1277 if (!printername || !sharename) {
1278 goto done;
1281 /* open printer handle */
1282 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1283 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1284 goto done;
1286 /* check for existing dst printer */
1287 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1288 goto done;
1290 /* check action and set string */
1291 switch (action) {
1292 case DSPRINT_PUBLISH:
1293 action_str = N_("published");
1294 break;
1295 case DSPRINT_UPDATE:
1296 action_str = N_("updated");
1297 break;
1298 case DSPRINT_UNPUBLISH:
1299 action_str = N_("unpublished");
1300 break;
1301 default:
1302 action_str = N_("unknown action");
1303 printf(_("unkown action: %d\n"), action);
1304 break;
1307 info.info7.action = action;
1308 info_ctr.level = 7;
1309 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
1310 (void *)&info.info7;
1312 ZERO_STRUCT(devmode_ctr);
1313 ZERO_STRUCT(secdesc_ctr);
1315 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1316 &hnd,
1317 &info_ctr,
1318 &devmode_ctr,
1319 &secdesc_ctr,
1320 0, /* command */
1321 &result);
1323 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1324 printf(_("cannot set printer-info: %s\n"),
1325 win_errstr(result));
1326 goto done;
1329 printf(_("successfully %s printer %s in Active Directory\n"),
1330 action_str, sharename);
1333 nt_status = NT_STATUS_OK;
1335 done:
1336 if (is_valid_policy_hnd(&hnd))
1337 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1339 return nt_status;
1342 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1343 const DOM_SID *domain_sid,
1344 const char *domain_name,
1345 struct cli_state *cli,
1346 struct rpc_pipe_client *pipe_hnd,
1347 TALLOC_CTX *mem_ctx,
1348 int argc,
1349 const char **argv)
1351 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1354 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1355 const DOM_SID *domain_sid,
1356 const char *domain_name,
1357 struct cli_state *cli,
1358 struct rpc_pipe_client *pipe_hnd,
1359 TALLOC_CTX *mem_ctx,
1360 int argc,
1361 const char **argv)
1363 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1366 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1367 const DOM_SID *domain_sid,
1368 const char *domain_name,
1369 struct cli_state *cli,
1370 struct rpc_pipe_client *pipe_hnd,
1371 TALLOC_CTX *mem_ctx,
1372 int argc,
1373 const char **argv)
1375 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1379 * List print-queues w.r.t. their publishing state
1381 * All parameters are provided by the run_rpc_command function, except for
1382 * argc, argv which are passed through.
1384 * @param c A net_context structure
1385 * @param domain_sid The domain sid aquired from the remote server
1386 * @param cli A cli_state connected to the server.
1387 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1388 * @param argc Standard main() style argc
1389 * @param argv Standard main() style argv. Initial components are already
1390 * stripped
1392 * @return Normal NTSTATUS return.
1395 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1396 const DOM_SID *domain_sid,
1397 const char *domain_name,
1398 struct cli_state *cli,
1399 struct rpc_pipe_client *pipe_hnd,
1400 TALLOC_CTX *mem_ctx,
1401 int argc,
1402 const char **argv)
1404 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1405 uint32_t i, num_printers;
1406 uint32_t level = 7;
1407 const char *printername, *sharename;
1408 union spoolss_PrinterInfo *info_enum;
1409 union spoolss_PrinterInfo info;
1410 struct policy_handle hnd;
1411 int state;
1413 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1414 return nt_status;
1416 for (i = 0; i < num_printers; i++) {
1418 /* do some initialization */
1419 printername = info_enum[i].info2.printername;
1420 sharename = info_enum[i].info2.sharename;
1422 if (!printername || !sharename) {
1423 goto done;
1426 /* open printer handle */
1427 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1428 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1429 goto done;
1431 /* check for existing dst printer */
1432 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1433 goto done;
1435 if (!info.info7.guid) {
1436 goto done;
1438 state = info.info7.action;
1439 switch (state) {
1440 case DSPRINT_PUBLISH:
1441 printf(_("printer [%s] is published"),
1442 sharename);
1443 if (c->opt_verbose)
1444 printf(_(", guid: %s"),info.info7.guid);
1445 printf("\n");
1446 break;
1447 case DSPRINT_UNPUBLISH:
1448 printf(_("printer [%s] is unpublished\n"),
1449 sharename);
1450 break;
1451 case DSPRINT_UPDATE:
1452 printf(_("printer [%s] is currently updating\n"),
1453 sharename);
1454 break;
1455 default:
1456 printf(_("unkown state: %d\n"), state);
1457 break;
1461 nt_status = NT_STATUS_OK;
1463 done:
1464 if (is_valid_policy_hnd(&hnd))
1465 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1467 return nt_status;
1471 * Migrate Printer-ACLs from a source server to the destination server
1473 * All parameters are provided by the run_rpc_command function, except for
1474 * argc, argv which are passed through.
1476 * @param c A net_context structure
1477 * @param domain_sid The domain sid aquired from the remote server
1478 * @param cli A cli_state connected to the server.
1479 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1480 * @param argc Standard main() style argc
1481 * @param argv Standard main() style argv. Initial components are already
1482 * stripped
1484 * @return Normal NTSTATUS return.
1487 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1488 const DOM_SID *domain_sid,
1489 const char *domain_name,
1490 struct cli_state *cli,
1491 struct rpc_pipe_client *pipe_hnd,
1492 TALLOC_CTX *mem_ctx,
1493 int argc,
1494 const char **argv)
1496 /* TODO: what now, info2 or info3 ?
1497 convince jerry that we should add clientside setacls level 3 at least
1499 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1500 uint32_t i = 0;
1501 uint32_t num_printers;
1502 uint32_t level = 2;
1503 const char *printername, *sharename;
1504 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1505 struct policy_handle hnd_src, hnd_dst;
1506 union spoolss_PrinterInfo *info_enum;
1507 struct cli_state *cli_dst = NULL;
1508 union spoolss_PrinterInfo info_src, info_dst;
1510 DEBUG(3,("copying printer ACLs\n"));
1512 /* connect destination PI_SPOOLSS */
1513 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1514 &ndr_table_spoolss.syntax_id);
1515 if (!NT_STATUS_IS_OK(nt_status))
1516 return nt_status;
1519 /* enum source printers */
1520 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1521 nt_status = NT_STATUS_UNSUCCESSFUL;
1522 goto done;
1525 if (!num_printers) {
1526 printf (_("no printers found on server.\n"));
1527 nt_status = NT_STATUS_OK;
1528 goto done;
1531 /* do something for all printers */
1532 for (i = 0; i < num_printers; i++) {
1534 /* do some initialization */
1535 printername = info_enum[i].info2.printername;
1536 sharename = info_enum[i].info2.sharename;
1538 if (!printername || !sharename) {
1539 nt_status = NT_STATUS_UNSUCCESSFUL;
1540 goto done;
1543 /* we can reset NT_STATUS here because we do not
1544 get any real NT_STATUS-codes anymore from now on */
1545 nt_status = NT_STATUS_UNSUCCESSFUL;
1547 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1548 printername, sharename);
1550 /* according to msdn you have specify these access-rights
1551 to see the security descriptor
1552 - READ_CONTROL (DACL)
1553 - ACCESS_SYSTEM_SECURITY (SACL)
1556 /* open src printer handle */
1557 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1558 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1559 goto done;
1561 /* open dst printer handle */
1562 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1563 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1564 goto done;
1566 /* check for existing dst printer */
1567 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1568 goto done;
1570 /* check for existing src printer */
1571 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1572 goto done;
1574 /* Copy Security Descriptor */
1576 /* copy secdesc (info level 2) */
1577 info_dst.info2.devmode = NULL;
1578 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1580 if (c->opt_verbose)
1581 display_sec_desc(info_dst.info2.secdesc);
1583 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1584 goto done;
1586 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1589 /* close printer handles here */
1590 if (is_valid_policy_hnd(&hnd_src)) {
1591 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1594 if (is_valid_policy_hnd(&hnd_dst)) {
1595 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1600 nt_status = NT_STATUS_OK;
1602 done:
1604 if (is_valid_policy_hnd(&hnd_src)) {
1605 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1608 if (is_valid_policy_hnd(&hnd_dst)) {
1609 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1612 if (cli_dst) {
1613 cli_shutdown(cli_dst);
1615 return nt_status;
1619 * Migrate printer-forms from a src server to the dst server
1621 * All parameters are provided by the run_rpc_command function, except for
1622 * argc, argv which are passed through.
1624 * @param c A net_context structure
1625 * @param domain_sid The domain sid aquired from the remote server
1626 * @param cli A cli_state connected to the server.
1627 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1628 * @param argc Standard main() style argc
1629 * @param argv Standard main() style argv. Initial components are already
1630 * stripped
1632 * @return Normal NTSTATUS return.
1635 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1636 const DOM_SID *domain_sid,
1637 const char *domain_name,
1638 struct cli_state *cli,
1639 struct rpc_pipe_client *pipe_hnd,
1640 TALLOC_CTX *mem_ctx,
1641 int argc,
1642 const char **argv)
1644 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1645 WERROR result;
1646 uint32_t i, f;
1647 uint32_t num_printers;
1648 uint32_t level = 1;
1649 const char *printername, *sharename;
1650 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1651 struct policy_handle hnd_src, hnd_dst;
1652 union spoolss_PrinterInfo *info_enum;
1653 union spoolss_PrinterInfo info_dst;
1654 uint32_t num_forms;
1655 union spoolss_FormInfo *forms;
1656 struct cli_state *cli_dst = NULL;
1658 DEBUG(3,("copying forms\n"));
1660 /* connect destination PI_SPOOLSS */
1661 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1662 &ndr_table_spoolss.syntax_id);
1663 if (!NT_STATUS_IS_OK(nt_status))
1664 return nt_status;
1666 /* enum src printers */
1667 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1668 nt_status = NT_STATUS_UNSUCCESSFUL;
1669 goto done;
1672 if (!num_printers) {
1673 printf (_("no printers found on server.\n"));
1674 nt_status = NT_STATUS_OK;
1675 goto done;
1678 /* do something for all printers */
1679 for (i = 0; i < num_printers; i++) {
1681 /* do some initialization */
1682 printername = info_enum[i].info2.printername;
1683 sharename = info_enum[i].info2.sharename;
1685 if (!printername || !sharename) {
1686 nt_status = NT_STATUS_UNSUCCESSFUL;
1687 goto done;
1689 /* we can reset NT_STATUS here because we do not
1690 get any real NT_STATUS-codes anymore from now on */
1691 nt_status = NT_STATUS_UNSUCCESSFUL;
1693 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1694 printername, sharename);
1697 /* open src printer handle */
1698 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1699 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1700 goto done;
1702 /* open dst printer handle */
1703 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1704 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1705 goto done;
1707 /* check for existing dst printer */
1708 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1709 goto done;
1711 /* finally migrate forms */
1712 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1713 goto done;
1715 DEBUG(1,("got %d forms for printer\n", num_forms));
1718 for (f = 0; f < num_forms; f++) {
1720 union spoolss_AddFormInfo info;
1721 NTSTATUS status;
1723 /* only migrate FORM_PRINTER types, according to jerry
1724 FORM_BUILTIN-types are hard-coded in samba */
1725 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1726 continue;
1728 if (c->opt_verbose)
1729 d_printf(_("\tmigrating form # %d [%s] of type "
1730 "[%d]\n"),
1731 f, forms[f].info1.form_name,
1732 forms[f].info1.flags);
1734 info.info1 = (struct spoolss_AddFormInfo1 *)
1735 (void *)&forms[f].info1;
1737 /* FIXME: there might be something wrong with samba's
1738 builtin-forms */
1739 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1740 &hnd_dst,
1742 info,
1743 &result);
1744 if (!W_ERROR_IS_OK(result)) {
1745 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1746 f, forms[f].info1.form_name);
1747 continue;
1750 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1751 forms[f].info1.form_name));
1755 /* close printer handles here */
1756 if (is_valid_policy_hnd(&hnd_src)) {
1757 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1760 if (is_valid_policy_hnd(&hnd_dst)) {
1761 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1765 nt_status = NT_STATUS_OK;
1767 done:
1769 if (is_valid_policy_hnd(&hnd_src))
1770 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1772 if (is_valid_policy_hnd(&hnd_dst))
1773 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1775 if (cli_dst) {
1776 cli_shutdown(cli_dst);
1778 return nt_status;
1782 * Migrate printer-drivers from a src server to the dst server
1784 * All parameters are provided by the run_rpc_command function, except for
1785 * argc, argv which are passed through.
1787 * @param c A net_context structure
1788 * @param domain_sid The domain sid aquired from the remote server
1789 * @param cli A cli_state connected to the server.
1790 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1791 * @param argc Standard main() style argc
1792 * @param argv Standard main() style argv. Initial components are already
1793 * stripped
1795 * @return Normal NTSTATUS return.
1798 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1799 const DOM_SID *domain_sid,
1800 const char *domain_name,
1801 struct cli_state *cli,
1802 struct rpc_pipe_client *pipe_hnd,
1803 TALLOC_CTX *mem_ctx,
1804 int argc,
1805 const char **argv)
1807 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1808 uint32_t i, p;
1809 uint32_t num_printers;
1810 uint32_t level = 3;
1811 const char *printername, *sharename;
1812 bool got_src_driver_share = false;
1813 bool got_dst_driver_share = false;
1814 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1815 struct policy_handle hnd_src, hnd_dst;
1816 union spoolss_DriverInfo drv_info_src;
1817 union spoolss_PrinterInfo *info_enum;
1818 union spoolss_PrinterInfo info_dst;
1819 struct cli_state *cli_dst = NULL;
1820 struct cli_state *cli_share_src = NULL;
1821 struct cli_state *cli_share_dst = NULL;
1822 const char *drivername = NULL;
1824 DEBUG(3,("copying printer-drivers\n"));
1826 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1827 &ndr_table_spoolss.syntax_id);
1828 if (!NT_STATUS_IS_OK(nt_status))
1829 return nt_status;
1831 /* open print$-share on the src server */
1832 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1833 cli->desthost, "print$", "A:");
1834 if (!NT_STATUS_IS_OK(nt_status))
1835 goto done;
1837 got_src_driver_share = true;
1840 /* open print$-share on the dst server */
1841 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1842 cli_dst->desthost, "print$", "A:");
1843 if (!NT_STATUS_IS_OK(nt_status))
1844 return nt_status;
1846 got_dst_driver_share = true;
1849 /* enum src printers */
1850 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1851 nt_status = NT_STATUS_UNSUCCESSFUL;
1852 goto done;
1855 if (num_printers == 0) {
1856 printf (_("no printers found on server.\n"));
1857 nt_status = NT_STATUS_OK;
1858 goto done;
1862 /* do something for all printers */
1863 for (p = 0; p < num_printers; p++) {
1865 /* do some initialization */
1866 printername = info_enum[p].info2.printername;
1867 sharename = info_enum[p].info2.sharename;
1869 if (!printername || !sharename) {
1870 nt_status = NT_STATUS_UNSUCCESSFUL;
1871 goto done;
1874 /* we can reset NT_STATUS here because we do not
1875 get any real NT_STATUS-codes anymore from now on */
1876 nt_status = NT_STATUS_UNSUCCESSFUL;
1878 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1879 printername, sharename);
1881 /* open dst printer handle */
1882 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1883 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1884 goto done;
1886 /* check for existing dst printer */
1887 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1888 goto done;
1891 /* open src printer handle */
1892 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1893 MAXIMUM_ALLOWED_ACCESS,
1894 pipe_hnd->auth->user_name,
1895 &hnd_src))
1896 goto done;
1898 /* in a first step call getdriver for each shared printer (per arch)
1899 to get a list of all files that have to be copied */
1901 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1903 /* getdriver src */
1904 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1905 level, archi_table[i].long_archi,
1906 archi_table[i].version, &drv_info_src))
1907 continue;
1909 drivername = drv_info_src.info3.driver_name;
1911 if (c->opt_verbose)
1912 display_print_driver3(&drv_info_src.info3);
1914 /* check arch dir */
1915 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1916 if (!NT_STATUS_IS_OK(nt_status))
1917 goto done;
1920 /* copy driver-files */
1921 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1922 archi_table[i].short_archi,
1923 &drv_info_src.info3);
1924 if (!NT_STATUS_IS_OK(nt_status))
1925 goto done;
1928 /* adddriver dst */
1929 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1930 nt_status = NT_STATUS_UNSUCCESSFUL;
1931 goto done;
1934 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1935 drivername, printername));
1939 if (!drivername || strlen(drivername) == 0) {
1940 DEBUGADD(1,("Did not get driver for printer %s\n",
1941 printername));
1942 goto done;
1945 /* setdriver dst */
1946 info_dst.info2.drivername = drivername;
1948 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1949 nt_status = NT_STATUS_UNSUCCESSFUL;
1950 goto done;
1953 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1954 drivername, printername));
1956 /* close dst */
1957 if (is_valid_policy_hnd(&hnd_dst)) {
1958 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1961 /* close src */
1962 if (is_valid_policy_hnd(&hnd_src)) {
1963 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1967 nt_status = NT_STATUS_OK;
1969 done:
1971 if (is_valid_policy_hnd(&hnd_src))
1972 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1974 if (is_valid_policy_hnd(&hnd_dst))
1975 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1977 if (cli_dst) {
1978 cli_shutdown(cli_dst);
1981 if (got_src_driver_share)
1982 cli_shutdown(cli_share_src);
1984 if (got_dst_driver_share)
1985 cli_shutdown(cli_share_dst);
1987 return nt_status;
1992 * Migrate printer-queues from a src to the dst server
1993 * (requires a working "addprinter command" to be installed for the local smbd)
1995 * All parameters are provided by the run_rpc_command function, except for
1996 * argc, argv which are passed through.
1998 * @param c A net_context structure
1999 * @param domain_sid The domain sid aquired from the remote server
2000 * @param cli A cli_state connected to the server.
2001 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2002 * @param argc Standard main() style argc
2003 * @param argv Standard main() style argv. Initial components are already
2004 * stripped
2006 * @return Normal NTSTATUS return.
2009 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
2010 const DOM_SID *domain_sid,
2011 const char *domain_name,
2012 struct cli_state *cli,
2013 struct rpc_pipe_client *pipe_hnd,
2014 TALLOC_CTX *mem_ctx,
2015 int argc,
2016 const char **argv)
2018 WERROR result;
2019 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2020 uint32_t i = 0, num_printers;
2021 uint32_t level = 2;
2022 union spoolss_PrinterInfo info_dst, info_src;
2023 union spoolss_PrinterInfo *info_enum;
2024 struct cli_state *cli_dst = NULL;
2025 struct policy_handle hnd_dst, hnd_src;
2026 const char *printername, *sharename;
2027 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2028 struct spoolss_SetPrinterInfoCtr info_ctr;
2030 DEBUG(3,("copying printers\n"));
2032 /* connect destination PI_SPOOLSS */
2033 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2034 &ndr_table_spoolss.syntax_id);
2035 if (!NT_STATUS_IS_OK(nt_status))
2036 return nt_status;
2038 /* enum printers */
2039 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2040 nt_status = NT_STATUS_UNSUCCESSFUL;
2041 goto done;
2044 if (!num_printers) {
2045 printf (_("no printers found on server.\n"));
2046 nt_status = NT_STATUS_OK;
2047 goto done;
2050 /* do something for all printers */
2051 for (i = 0; i < num_printers; i++) {
2053 struct spoolss_SetPrinterInfo2 info2;
2055 /* do some initialization */
2056 printername = info_enum[i].info2.printername;
2057 sharename = info_enum[i].info2.sharename;
2059 if (!printername || !sharename) {
2060 nt_status = NT_STATUS_UNSUCCESSFUL;
2061 goto done;
2063 /* we can reset NT_STATUS here because we do not
2064 get any real NT_STATUS-codes anymore from now on */
2065 nt_status = NT_STATUS_UNSUCCESSFUL;
2067 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2068 printername, sharename);
2070 /* open dst printer handle */
2071 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2072 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2074 DEBUG(1,("could not open printer: %s\n", sharename));
2077 /* check for existing dst printer */
2078 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2079 printf (_("could not get printer, creating printer.\n"));
2080 } else {
2081 DEBUG(1,("printer already exists: %s\n", sharename));
2082 /* close printer handle here - dst only, not got src yet. */
2083 if (is_valid_policy_hnd(&hnd_dst)) {
2084 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2086 continue;
2089 /* now get again src printer ctr via getprinter,
2090 we first need a handle for that */
2092 /* open src printer handle */
2093 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2094 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2095 goto done;
2097 /* getprinter on the src server */
2098 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2099 goto done;
2101 /* copy each src printer to a dst printer 1:1,
2102 maybe some values have to be changed though */
2103 d_printf(_("creating printer: %s\n"), printername);
2105 info_ctr.level = level;
2106 spoolss_printerinfo2_to_setprinterinfo2(&info_src.info2, &info2);
2107 info_ctr.info.info2 = &info2;
2109 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2110 mem_ctx,
2111 &info_ctr);
2113 if (W_ERROR_IS_OK(result))
2114 d_printf (_("printer [%s] successfully added.\n"),
2115 printername);
2116 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2117 d_fprintf (stderr, _("printer [%s] already exists.\n"),
2118 printername);
2119 else {
2120 d_fprintf (stderr, _("could not create printer [%s]\n"),
2121 printername);
2122 goto done;
2125 /* close printer handles here */
2126 if (is_valid_policy_hnd(&hnd_src)) {
2127 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2130 if (is_valid_policy_hnd(&hnd_dst)) {
2131 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2135 nt_status = NT_STATUS_OK;
2137 done:
2138 if (is_valid_policy_hnd(&hnd_src))
2139 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2141 if (is_valid_policy_hnd(&hnd_dst))
2142 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2144 if (cli_dst) {
2145 cli_shutdown(cli_dst);
2147 return nt_status;
2151 * Migrate Printer-Settings from a src server to the dst server
2152 * (for this to work, printers and drivers already have to be migrated earlier)
2154 * All parameters are provided by the run_rpc_command function, except for
2155 * argc, argv which are passed through.
2157 * @param c A net_context structure
2158 * @param domain_sid The domain sid aquired from the remote server
2159 * @param cli A cli_state connected to the server.
2160 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2161 * @param argc Standard main() style argc
2162 * @param argv Standard main() style argv. Initial components are already
2163 * stripped
2165 * @return Normal NTSTATUS return.
2168 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2169 const DOM_SID *domain_sid,
2170 const char *domain_name,
2171 struct cli_state *cli,
2172 struct rpc_pipe_client *pipe_hnd,
2173 TALLOC_CTX *mem_ctx,
2174 int argc,
2175 const char **argv)
2178 /* FIXME: Here the nightmare begins */
2180 WERROR result;
2181 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2182 uint32_t i = 0, p = 0, j = 0;
2183 uint32_t num_printers;
2184 uint32_t level = 2;
2185 const char *printername, *sharename;
2186 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2187 struct policy_handle hnd_src, hnd_dst;
2188 union spoolss_PrinterInfo *info_enum;
2189 union spoolss_PrinterInfo info_dst_publish;
2190 union spoolss_PrinterInfo info_dst;
2191 struct cli_state *cli_dst = NULL;
2192 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2193 const char *longname;
2194 const char **keylist = NULL;
2196 /* FIXME GD */
2197 ZERO_STRUCT(info_dst_publish);
2199 DEBUG(3,("copying printer settings\n"));
2201 /* connect destination PI_SPOOLSS */
2202 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2203 &ndr_table_spoolss.syntax_id);
2204 if (!NT_STATUS_IS_OK(nt_status))
2205 return nt_status;
2207 /* enum src printers */
2208 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2209 nt_status = NT_STATUS_UNSUCCESSFUL;
2210 goto done;
2213 if (!num_printers) {
2214 printf (_("no printers found on server.\n"));
2215 nt_status = NT_STATUS_OK;
2216 goto done;
2220 /* needed for dns-strings in regkeys */
2221 longname = get_mydnsfullname();
2222 if (!longname) {
2223 nt_status = NT_STATUS_UNSUCCESSFUL;
2224 goto done;
2227 /* do something for all printers */
2228 for (i = 0; i < num_printers; i++) {
2230 uint32_t value_offered = 0, value_needed;
2231 uint32_t data_offered = 0, data_needed;
2232 enum winreg_Type type;
2233 uint8_t *buffer = NULL;
2234 const char *value_name = NULL;
2236 /* do some initialization */
2237 printername = info_enum[i].info2.printername;
2238 sharename = info_enum[i].info2.sharename;
2240 if (!printername || !sharename) {
2241 nt_status = NT_STATUS_UNSUCCESSFUL;
2242 goto done;
2244 /* we can reset NT_STATUS here because we do not
2245 get any real NT_STATUS-codes anymore from now on */
2246 nt_status = NT_STATUS_UNSUCCESSFUL;
2248 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2249 printername, sharename);
2252 /* open src printer handle */
2253 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2254 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2255 goto done;
2257 /* open dst printer handle */
2258 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2259 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2260 goto done;
2262 /* check for existing dst printer */
2263 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2264 level, &info_dst))
2265 goto done;
2268 /* STEP 1: COPY DEVICE-MODE and other
2269 PRINTER_INFO_2-attributes
2272 info_dst.info2 = info_enum[i].info2;
2274 /* why is the port always disconnected when the printer
2275 is correctly installed (incl. driver ???) */
2276 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2278 /* check if printer is published */
2279 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2281 /* check for existing dst printer */
2282 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2283 goto done;
2285 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2287 /* ignore false from setprinter due to WERR_IO_PENDING */
2288 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2290 DEBUG(3,("republished printer\n"));
2293 if (info_enum[i].info2.devmode != NULL) {
2295 /* copy devmode (info level 2) */
2296 info_dst.info2.devmode = info_enum[i].info2.devmode;
2298 /* do not copy security descriptor (we have another
2299 * command for that) */
2300 info_dst.info2.secdesc = NULL;
2302 #if 0
2303 info_dst.info2.devmode.devicename =
2304 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2305 longname, printername);
2306 if (!info_dst.info2.devmode.devicename) {
2307 nt_status = NT_STATUS_NO_MEMORY;
2308 goto done;
2310 #endif
2311 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2312 level, &info_dst))
2313 goto done;
2315 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2318 /* STEP 2: COPY REGISTRY VALUES */
2320 /* please keep in mind that samba parse_spools gives horribly
2321 crippled results when used to rpccli_spoolss_enumprinterdataex
2322 a win2k3-server. (Bugzilla #1851)
2323 FIXME: IIRC I've seen it too on a win2k-server
2326 /* enumerate data on src handle */
2327 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2328 &hnd_src,
2330 value_name,
2331 value_offered,
2332 &value_needed,
2333 &type,
2334 buffer,
2335 data_offered,
2336 &data_needed,
2337 &result);
2339 data_offered = data_needed;
2340 value_offered = value_needed;
2341 buffer = talloc_zero_array(mem_ctx, uint8_t, data_needed);
2342 value_name = talloc_zero_array(mem_ctx, char, value_needed);
2344 /* loop for all printerdata of "PrinterDriverData" */
2345 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2347 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2348 &hnd_src,
2349 p++,
2350 value_name,
2351 value_offered,
2352 &value_needed,
2353 &type,
2354 buffer,
2355 data_offered,
2356 &data_needed,
2357 &result);
2358 /* loop for all reg_keys */
2359 if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2361 struct regval_blob v;
2363 /* display_value */
2364 if (c->opt_verbose) {
2365 fstrcpy(v.valuename, value_name);
2366 v.type = type;
2367 v.size = data_offered;
2368 v.data_p = buffer;
2369 display_reg_value(SPOOL_PRINTERDATA_KEY, v);
2372 /* set_value */
2373 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2374 &hnd_dst, value_name,
2375 type, buffer, data_offered))
2376 goto done;
2378 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2379 v.valuename));
2383 /* STEP 3: COPY SUBKEY VALUES */
2385 /* here we need to enum all printer_keys and then work
2386 on the result with enum_printer_key_ex. nt4 does not
2387 respond to enumprinterkey, win2k does, so continue
2388 in case of an error */
2390 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2391 printf(_("got no key-data\n"));
2392 continue;
2396 /* work on a list of printer keys
2397 each key has to be enumerated to get all required
2398 information. information is then set via setprinterdataex-calls */
2400 if (keylist == NULL)
2401 continue;
2403 for (i=0; keylist && keylist[i] != NULL; i++) {
2405 const char *subkey = keylist[i];
2406 uint32_t count;
2407 struct spoolss_PrinterEnumValues *info;
2409 /* enumerate all src subkeys */
2410 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2411 &hnd_src, subkey,
2412 &count, &info)) {
2413 goto done;
2416 for (j=0; j < count; j++) {
2418 struct regval_blob value;
2419 DATA_BLOB blob;
2421 /* although samba replies with sane data in most cases we
2422 should try to avoid writing wrong registry data */
2424 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME) ||
2425 strequal(info[j].value_name, SPOOL_REG_UNCNAME) ||
2426 strequal(info[j].value_name, SPOOL_REG_URL) ||
2427 strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME) ||
2428 strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2430 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
2432 /* although windows uses a multi-sz, we use a sz */
2433 push_reg_sz(mem_ctx, &blob, SAMBA_PRINTER_PORT_NAME);
2434 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2437 if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
2439 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2440 nt_status = NT_STATUS_NO_MEMORY;
2441 goto done;
2443 push_reg_sz(mem_ctx, &blob, unc_name);
2444 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2447 if (strequal(info[j].value_name, SPOOL_REG_URL)) {
2449 continue;
2451 #if 0
2452 /* FIXME: should we really do that ??? */
2453 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2454 nt_status = NT_STATUS_NO_MEMORY;
2455 goto done;
2457 push_reg_sz(mem_ctx, &blob, url);
2458 fstrcpy(value.valuename, SPOOL_REG_URL);
2459 #endif
2462 if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2464 push_reg_sz(mem_ctx, &blob, longname);
2465 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2468 if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
2470 push_reg_sz(mem_ctx, &blob, global_myname());
2471 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2474 value.type = REG_SZ;
2475 value.size = blob.length;
2476 if (value.size) {
2477 value.data_p = blob.data;
2478 } else {
2479 value.data_p = NULL;
2482 if (c->opt_verbose)
2483 display_reg_value(subkey, value);
2485 /* here we have to set all subkeys on the dst server */
2486 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2487 subkey, &value))
2488 goto done;
2490 } else {
2492 struct regval_blob v;
2494 fstrcpy(v.valuename, info[j].value_name);
2495 v.type = info[j].type;
2496 v.data_p = info[j].data->data;
2497 v.size = info[j].data->length;
2499 if (c->opt_verbose) {
2500 display_reg_value(subkey, v);
2503 /* here we have to set all subkeys on the dst server */
2504 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2505 subkey, &v)) {
2506 goto done;
2511 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2512 subkey, info[j].value_name));
2517 TALLOC_FREE(keylist);
2519 /* close printer handles here */
2520 if (is_valid_policy_hnd(&hnd_src)) {
2521 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2524 if (is_valid_policy_hnd(&hnd_dst)) {
2525 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2530 nt_status = NT_STATUS_OK;
2532 done:
2533 SAFE_FREE(devicename);
2534 SAFE_FREE(url);
2535 SAFE_FREE(unc_name);
2537 if (is_valid_policy_hnd(&hnd_src))
2538 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2540 if (is_valid_policy_hnd(&hnd_dst))
2541 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2543 if (cli_dst) {
2544 cli_shutdown(cli_dst);
2546 return nt_status;