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/>.
20 #include "system/filesys.h"
21 #include "utils/net.h"
22 #include "rpc_client/rpc_client.h"
23 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
24 #include "rpc_client/cli_spoolss.h"
25 #include "rpc_client/init_spoolss.h"
26 #include "nt_printing.h"
27 #include "registry/reg_objects.h"
28 #include "../libcli/security/security.h"
29 #include "../libcli/registry/util_reg.h"
30 #include "libsmb/libsmb.h"
32 /* support itanium as well */
33 static const struct print_architecture_table_node archi_table
[]= {
35 {"Windows 4.0", "WIN40", 0 },
36 {"Windows NT x86", "W32X86", 2 },
37 {"Windows NT x86", "W32X86", 3 },
38 {"Windows NT R4000", "W32MIPS", 2 },
39 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
40 {"Windows NT PowerPC", "W32PPC", 2 },
41 {"Windows IA64", "IA64", 3 },
42 {"Windows x64", "x64", 3 },
48 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
49 * It is here for debugging purpose and should be removed later on.
52 /****************************************************************************
53 Printer info level 3 display function.
54 ****************************************************************************/
56 static void display_print_driver3(struct spoolss_DriverInfo3
*r
)
64 printf(_("Printer Driver Info 3:\n"));
65 printf(_("\tVersion: [%x]\n"), r
->version
);
66 printf(_("\tDriver Name: [%s]\n"), r
->driver_name
);
67 printf(_("\tArchitecture: [%s]\n"), r
->architecture
);
68 printf(_("\tDriver Path: [%s]\n"), r
->driver_path
);
69 printf(_("\tDatafile: [%s]\n"), r
->data_file
);
70 printf(_("\tConfigfile: [%s]\n\n"), r
->config_file
);
71 printf(_("\tHelpfile: [%s]\n\n"), r
->help_file
);
73 for (i
=0; r
->dependent_files
&& r
->dependent_files
[i
] != NULL
; i
++) {
74 printf(_("\tDependentfiles: [%s]\n"), r
->dependent_files
[i
]);
79 printf(_("\tMonitorname: [%s]\n"), r
->monitor_name
);
80 printf(_("\tDefaultdatatype: [%s]\n\n"), r
->default_datatype
);
83 static void display_reg_value(const char *subkey
, struct regval_blob
*value
)
88 switch(regval_type(value
)) {
90 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey
,
91 regval_name(value
), *((uint32_t *) regval_data_p(value
)));
95 blob
= data_blob_const(regval_data_p(value
), regval_size(value
));
96 pull_reg_sz(talloc_tos(), &blob
, &text
);
100 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey
, regval_name(value
),
105 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
107 subkey
, regval_name(value
));
113 blob
= data_blob_const(regval_data_p(value
), regval_size(value
));
115 if (!pull_reg_multi_sz(NULL
, &blob
, &values
)) {
116 d_printf("pull_reg_multi_sz failed\n");
120 printf("%s: REG_MULTI_SZ: \n", regval_name(value
));
121 for (i
=0; values
[i
] != NULL
; i
++) {
122 d_printf("%s\n", values
[i
]);
129 d_printf(_("\t%s: unknown type %d\n"), regval_name(value
),
136 * Copies ACLs, DOS-attributes and timestamps from one
137 * file or directory from one connected share to another connected share
139 * @param c A net_context structure
140 * @param mem_ctx A talloc-context
141 * @param cli_share_src A connected cli_state
142 * @param cli_share_dst A connected cli_state
143 * @param src_file The source file-name
144 * @param dst_file The destination file-name
145 * @param copy_acls Whether to copy acls
146 * @param copy_attrs Whether to copy DOS attributes
147 * @param copy_timestamps Whether to preserve timestamps
148 * @param is_file Whether this file is a file or a dir
150 * @return Normal NTSTATUS return.
153 NTSTATUS
net_copy_fileattr(struct net_context
*c
,
155 struct cli_state
*cli_share_src
,
156 struct cli_state
*cli_share_dst
,
157 const char *src_name
, const char *dst_name
,
158 bool copy_acls
, bool copy_attrs
,
159 bool copy_timestamps
, bool is_file
)
161 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
162 uint16_t fnum_src
= 0;
163 uint16_t fnum_dst
= 0;
164 struct security_descriptor
*sd
= NULL
;
166 time_t f_atime
, f_ctime
, f_mtime
;
169 if (!copy_timestamps
&& !copy_acls
&& !copy_attrs
)
172 /* open file/dir on the originating server */
174 DEBUGADD(3,("opening %s %s on originating server\n",
175 is_file
?"file":"dir", src_name
));
177 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_src
, src_name
, 0, READ_CONTROL_ACCESS
, 0,
178 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum_src
))) {
179 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
180 is_file
?"file":"dir", src_name
, cli_errstr(cli_share_src
)));
181 nt_status
= cli_nt_error(cli_share_src
);
188 /* get the security descriptor */
189 sd
= cli_query_secdesc(cli_share_src
, fnum_src
, mem_ctx
);
191 DEBUG(0,("failed to get security descriptor: %s\n",
192 cli_errstr(cli_share_src
)));
193 nt_status
= cli_nt_error(cli_share_src
);
197 if (c
->opt_verbose
&& DEBUGLEVEL
>= 3)
198 display_sec_desc(sd
);
202 if (copy_attrs
|| copy_timestamps
) {
204 /* get file attributes */
205 if (!NT_STATUS_IS_OK(cli_getattrE(cli_share_src
, fnum_src
, &attr
, NULL
,
206 &f_ctime
, &f_atime
, &f_mtime
))) {
207 DEBUG(0,("failed to get file-attrs: %s\n",
208 cli_errstr(cli_share_src
)));
209 nt_status
= cli_nt_error(cli_share_src
);
215 /* open the file/dir on the destination server */
217 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_dst
, dst_name
, 0, WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
, 0,
218 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum_dst
))) {
219 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
220 is_file
?"file":"dir", dst_name
, cli_errstr(cli_share_dst
)));
221 nt_status
= cli_nt_error(cli_share_dst
);
225 if (copy_timestamps
) {
228 if (!NT_STATUS_IS_OK(cli_setattrE(cli_share_dst
, fnum_dst
, f_ctime
, f_atime
, f_mtime
))) {
229 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
230 cli_errstr(cli_share_dst
)));
231 nt_status
= cli_nt_error(cli_share_dst
);
240 status
= cli_set_secdesc(cli_share_dst
, fnum_dst
, sd
);
241 if (!NT_STATUS_IS_OK(status
)) {
242 DEBUG(0, ("could not set secdesc on %s: %s\n",
243 dst_name
, nt_errstr(status
)));
252 if (!NT_STATUS_IS_OK(cli_setatr(cli_share_dst
, dst_name
, attr
, 0))) {
253 DEBUG(0,("failed to set file-attrs: %s\n",
254 cli_errstr(cli_share_dst
)));
255 nt_status
= cli_nt_error(cli_share_dst
);
263 if (!NT_STATUS_IS_OK(cli_close(cli_share_src
, fnum_src
))) {
265 _("could not close %s on originating server: %s\n"),
266 is_file
?"file":"dir", cli_errstr(cli_share_src
));
267 nt_status
= cli_nt_error(cli_share_src
);
271 if (!NT_STATUS_IS_OK(cli_close(cli_share_dst
, fnum_dst
))) {
273 _("could not close %s on destination server: %s\n"),
274 is_file
?"file":"dir", cli_errstr(cli_share_dst
));
275 nt_status
= cli_nt_error(cli_share_dst
);
280 nt_status
= NT_STATUS_OK
;
286 cli_close(cli_share_src
, fnum_src
);
289 cli_close(cli_share_dst
, fnum_dst
);
295 * Copy a file or directory from a connected share to another connected share
297 * @param c A net_context structure
298 * @param mem_ctx A talloc-context
299 * @param cli_share_src A connected cli_state
300 * @param cli_share_dst A connected cli_state
301 * @param src_file The source file-name
302 * @param dst_file The destination file-name
303 * @param copy_acls Whether to copy acls
304 * @param copy_attrs Whether to copy DOS attributes
305 * @param copy_timestamps Whether to preserve timestamps
306 * @param is_file Whether this file is a file or a dir
308 * @return Normal NTSTATUS return.
311 NTSTATUS
net_copy_file(struct net_context
*c
,
313 struct cli_state
*cli_share_src
,
314 struct cli_state
*cli_share_dst
,
315 const char *src_name
, const char *dst_name
,
316 bool copy_acls
, bool copy_attrs
,
317 bool copy_timestamps
, bool is_file
)
319 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
320 uint16_t fnum_src
= 0;
321 uint16_t fnum_dst
= 0;
322 static int io_bufsize
= 64512;
323 int read_size
= io_bufsize
;
328 if (!src_name
|| !dst_name
)
331 if (cli_share_src
== NULL
|| cli_share_dst
== NULL
)
334 /* open on the originating server */
335 DEBUGADD(3,("opening %s %s on originating server\n",
336 is_file
? "file":"dir", src_name
));
338 nt_status
= cli_open(cli_share_src
, src_name
, O_RDONLY
, DENY_NONE
, &fnum_src
);
340 nt_status
= cli_ntcreate(cli_share_src
, src_name
, 0, READ_CONTROL_ACCESS
, 0,
341 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum_src
);
343 if (!NT_STATUS_IS_OK(nt_status
)) {
344 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
345 is_file
? "file":"dir",
346 src_name
, cli_errstr(cli_share_src
)));
353 /* open file on the destination server */
354 DEBUGADD(3,("opening file %s on destination server\n", dst_name
));
355 nt_status
= cli_open(cli_share_dst
, dst_name
,
356 O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
, &fnum_dst
);
358 if (!NT_STATUS_IS_OK(nt_status
)) {
359 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
360 dst_name
, cli_errstr(cli_share_dst
)));
364 /* allocate memory */
365 if (!(data
= (char *)SMB_MALLOC(read_size
))) {
366 d_fprintf(stderr
, _("malloc fail for size %d\n"),
368 nt_status
= NT_STATUS_NO_MEMORY
;
375 if (c
->opt_verbose
) {
377 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
378 "%s ACLs and %s DOS Attributes %s\n"),
379 cli_share_src
->desthost
, cli_share_src
->share
, src_name
,
380 cli_share_dst
->desthost
, cli_share_dst
->share
, dst_name
,
381 copy_acls
? _("with") : _("without"),
382 copy_attrs
? _("with") : _("without"),
383 copy_timestamps
? _("(preserving timestamps)") : "" );
391 n
= cli_read(cli_share_src
, fnum_src
, data
, nread
,
397 nt_status
= cli_writeall(cli_share_dst
, fnum_dst
, 0,
398 (uint8_t *)data
, nread
, n
, NULL
);
400 if (!NT_STATUS_IS_OK(nt_status
)) {
401 d_fprintf(stderr
, _("Error writing file: %s\n"),
402 nt_errstr(nt_status
));
410 if (!is_file
&& !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst
, dst_name
))) {
413 DEBUGADD(3,("creating dir %s on the destination server\n",
416 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share_dst
, dst_name
))) {
417 DEBUG(0,("cannot create directory %s: %s\n",
418 dst_name
, cli_errstr(cli_share_dst
)));
419 nt_status
= NT_STATUS_NO_SUCH_FILE
;
422 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share_dst
, dst_name
))) {
424 _("cannot check for directory %s: %s\n"),
425 dst_name
, cli_errstr(cli_share_dst
));
432 if (!NT_STATUS_IS_OK(cli_close(cli_share_src
, fnum_src
))) {
434 _("could not close file on originating server: %s\n"),
435 cli_errstr(cli_share_src
));
436 nt_status
= cli_nt_error(cli_share_src
);
440 if (is_file
&& !NT_STATUS_IS_OK(cli_close(cli_share_dst
, fnum_dst
))) {
442 _("could not close file on destination server: %s\n"),
443 cli_errstr(cli_share_dst
));
444 nt_status
= cli_nt_error(cli_share_dst
);
448 /* possibly we have to copy some file-attributes / acls / sd */
449 nt_status
= net_copy_fileattr(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
450 src_name
, dst_name
, copy_acls
,
451 copy_attrs
, copy_timestamps
, is_file
);
452 if (!NT_STATUS_IS_OK(nt_status
))
456 nt_status
= NT_STATUS_OK
;
462 cli_close(cli_share_src
, fnum_src
);
465 cli_close(cli_share_dst
, fnum_dst
);
473 * Copy a driverfile from on connected share to another connected share
474 * This silently assumes that a driver-file is picked up from
476 * \\src_server\print$\{arch}\{version}\file
480 * \\dst_server\print$\{arch}\file
482 * to be added via setdriver-calls later.
483 * @param c A net_context structure
484 * @param mem_ctx A talloc-context
485 * @param cli_share_src A cli_state connected to source print$-share
486 * @param cli_share_dst A cli_state connected to destination print$-share
487 * @param file The file-name to be copied
488 * @param short_archi The name of the driver-architecture (short form)
490 * @return Normal NTSTATUS return.
493 static NTSTATUS
net_copy_driverfile(struct net_context
*c
,
495 struct cli_state
*cli_share_src
,
496 struct cli_state
*cli_share_dst
,
497 const char *file
, const char *short_archi
) {
502 char *version
= NULL
;
503 char *filename
= NULL
;
510 /* scroll through the file until we have the part
511 beyond archi_table.short_archi */
513 while (next_token_talloc(mem_ctx
, &p
, &tok
, "\\")) {
514 if (strequal(tok
, short_archi
)) {
515 next_token_talloc(mem_ctx
, &p
, &version
, "\\");
516 next_token_talloc(mem_ctx
, &p
, &filename
, "\\");
520 if (version
== NULL
|| filename
== NULL
) {
521 return NT_STATUS_UNSUCCESSFUL
;
524 /* build source file name */
525 src_name
= talloc_asprintf(mem_ctx
, "\\%s\\%s\\%s",
526 short_archi
, version
, filename
);
527 if (src_name
== NULL
) {
528 return NT_STATUS_NO_MEMORY
;
531 /* create destination file name */
532 dst_name
= talloc_asprintf(mem_ctx
, "\\%s\\%s", short_archi
, filename
);
533 if (dst_name
== NULL
) {
534 return NT_STATUS_NO_MEMORY
;
538 /* finally copy the file */
539 return net_copy_file(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
540 src_name
, dst_name
, false, false, false, true);
544 * Check for existing Architecture directory on a given server
546 * @param cli_share A cli_state connected to a print$-share
547 * @param short_archi The Architecture for the print-driver
549 * @return Normal NTSTATUS return.
552 static NTSTATUS
check_arch_dir(struct cli_state
*cli_share
, const char *short_archi
)
555 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
558 if (asprintf(&dir
, "\\%s", short_archi
) < 0) {
559 return NT_STATUS_NO_MEMORY
;
562 DEBUG(10,("creating print-driver dir for architecture: %s\n",
565 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share
, dir
))) {
566 DEBUG(1,("cannot create directory %s: %s\n",
567 dir
, cli_errstr(cli_share
)));
568 nt_status
= NT_STATUS_NO_SUCH_FILE
;
571 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share
, dir
))) {
572 d_fprintf(stderr
, _("cannot check %s: %s\n"),
573 dir
, cli_errstr(cli_share
));
577 nt_status
= NT_STATUS_OK
;
585 * Copy a print-driver (level 3) from one connected print$-share to another
586 * connected print$-share
588 * @param c A net_context structure
589 * @param mem_ctx A talloc-context
590 * @param cli_share_src A cli_state connected to a print$-share
591 * @param cli_share_dst A cli_state connected to a print$-share
592 * @param short_archi The Architecture for the print-driver
593 * @param i1 The DRIVER_INFO_3-struct
595 * @return Normal NTSTATUS return.
598 static NTSTATUS
copy_print_driver_3(struct net_context
*c
,
600 struct cli_state
*cli_share_src
,
601 struct cli_state
*cli_share_dst
,
602 const char *short_archi
,
603 struct spoolss_DriverInfo3
*r
)
605 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
613 d_printf(_("copying driver: [%s], for architecture: [%s], "
615 r
->driver_name
, short_archi
, r
->version
);
617 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
618 r
->driver_path
, short_archi
);
619 if (!NT_STATUS_IS_OK(nt_status
))
622 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
623 r
->data_file
, short_archi
);
624 if (!NT_STATUS_IS_OK(nt_status
))
627 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
628 r
->config_file
, short_archi
);
629 if (!NT_STATUS_IS_OK(nt_status
))
632 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
633 r
->help_file
, short_archi
);
634 if (!NT_STATUS_IS_OK(nt_status
))
637 for (i
=0; r
->dependent_files
[i
] != NULL
; i
++) {
639 nt_status
= net_copy_driverfile(c
, mem_ctx
,
640 cli_share_src
, cli_share_dst
,
641 r
->dependent_files
[i
], short_archi
);
642 if (!NT_STATUS_IS_OK(nt_status
)) {
651 * net_spoolss-functions
652 * =====================
654 * the net_spoolss-functions aim to simplify spoolss-client-functions
655 * required during the migration-process wrt buffer-sizes, returned
658 * this greatly reduces the complexitiy of the migrate-functions.
662 static bool net_spoolss_enum_printers(struct rpc_pipe_client
*pipe_hnd
,
667 uint32_t *num_printers
,
668 union spoolss_PrinterInfo
**info
)
674 result
= rpccli_spoolss_enumprinters(pipe_hnd
, mem_ctx
,
681 if (!W_ERROR_IS_OK(result
)) {
682 printf(_("cannot enum printers: %s\n"), win_errstr(result
));
689 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client
*pipe_hnd
,
691 const char *printername
,
692 uint32_t access_required
,
693 const char *username
,
694 struct policy_handle
*hnd
)
697 fstring printername2
;
699 fstrcpy(printername2
, pipe_hnd
->srv_name_slash
);
700 fstrcat(printername2
, "\\");
701 fstrcat(printername2
, printername
);
703 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
704 pipe_hnd
->srv_name_slash
, username
, printername2
, access_required
));
707 result
= rpccli_spoolss_openprinter_ex(pipe_hnd
, mem_ctx
,
712 /* be more verbose */
713 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
715 _("no access to printer [%s] on [%s] for user [%s] "
717 printername2
, pipe_hnd
->srv_name_slash
, username
);
721 if (!W_ERROR_IS_OK(result
)) {
722 d_fprintf(stderr
,_("cannot open printer %s on server %s: %s\n"),
723 printername2
, pipe_hnd
->srv_name_slash
, win_errstr(result
));
727 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
728 printername2
, pipe_hnd
->srv_name_slash
));
733 static bool net_spoolss_getprinter(struct rpc_pipe_client
*pipe_hnd
,
735 struct policy_handle
*hnd
,
737 union spoolss_PrinterInfo
*info
)
741 /* getprinter call */
742 result
= rpccli_spoolss_getprinter(pipe_hnd
, mem_ctx
,
747 if (!W_ERROR_IS_OK(result
)) {
748 printf(_("cannot get printer-info: %s\n"), win_errstr(result
));
755 static bool net_spoolss_setprinter(struct rpc_pipe_client
*pipe_hnd
,
757 struct policy_handle
*hnd
,
759 union spoolss_PrinterInfo
*info
)
761 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
764 struct spoolss_SetPrinterInfoCtr info_ctr
;
765 struct spoolss_SetPrinterInfo2 info2
;
766 struct spoolss_DevmodeContainer devmode_ctr
;
767 struct sec_desc_buf secdesc_ctr
;
769 ZERO_STRUCT(devmode_ctr
);
770 ZERO_STRUCT(secdesc_ctr
);
772 /* setprinter call */
774 info_ctr
.level
= level
;
777 info_ctr
.info
.info0
= (struct spoolss_SetPrinterInfo0
*)
778 (void *)&info
->info0
;
781 info_ctr
.info
.info1
= (struct spoolss_SetPrinterInfo1
*)
782 (void *)&info
->info1
;
785 spoolss_printerinfo2_to_setprinterinfo2(&info
->info2
, &info2
);
786 info_ctr
.info
.info2
= &info2
;
789 info_ctr
.info
.info3
= (struct spoolss_SetPrinterInfo3
*)
790 (void *)&info
->info3
;
793 info_ctr
.info
.info4
= (struct spoolss_SetPrinterInfo4
*)
794 (void *)&info
->info4
;
797 info_ctr
.info
.info5
= (struct spoolss_SetPrinterInfo5
*)
798 (void *)&info
->info5
;
801 info_ctr
.info
.info6
= (struct spoolss_SetPrinterInfo6
*)
802 (void *)&info
->info6
;
805 info_ctr
.info
.info7
= (struct spoolss_SetPrinterInfo7
*)
806 (void *)&info
->info7
;
810 info_ctr
.info
.info8
= (struct spoolss_SetPrinterInfo8
*)
811 (void *)&info
->info8
;
814 info_ctr
.info
.info9
= (struct spoolss_SetPrinterInfo9
*)
815 (void *)&info
->info9
;
822 status
= dcerpc_spoolss_SetPrinter(b
, mem_ctx
,
829 if (!NT_STATUS_IS_OK(status
)) {
830 printf(_("cannot set printer-info: %s\n"), nt_errstr(status
));
833 if (!W_ERROR_IS_OK(result
)) {
834 printf(_("cannot set printer-info: %s\n"), win_errstr(result
));
842 static bool net_spoolss_setprinterdata(struct rpc_pipe_client
*pipe_hnd
,
844 struct policy_handle
*hnd
,
845 const char *value_name
,
846 enum winreg_Type type
,
850 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
854 /* setprinterdata call */
855 status
= dcerpc_spoolss_SetPrinterData(b
, mem_ctx
,
862 if (!NT_STATUS_IS_OK(status
)) {
863 printf (_("unable to set printerdata: %s\n"),
867 if (!W_ERROR_IS_OK(result
)) {
868 printf (_("unable to set printerdata: %s\n"),
877 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client
*pipe_hnd
,
879 struct policy_handle
*hnd
,
881 const char ***keylist
)
885 /* enumprinterkey call */
886 result
= rpccli_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, hnd
, keyname
, keylist
, 0);
888 if (!W_ERROR_IS_OK(result
)) {
889 printf(_("enumprinterkey failed: %s\n"), win_errstr(result
));
896 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
899 struct policy_handle
*hnd
,
902 struct spoolss_PrinterEnumValues
**info
)
906 /* enumprinterdataex call */
907 result
= rpccli_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
,
914 if (!W_ERROR_IS_OK(result
)) {
915 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result
));
923 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
925 struct policy_handle
*hnd
,
927 struct regval_blob
*value
)
929 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
933 /* setprinterdataex call */
934 status
= dcerpc_spoolss_SetPrinterDataEx(b
, mem_ctx
,
939 regval_data_p(value
),
942 if (!NT_STATUS_IS_OK(status
)) {
943 printf(_("could not set printerdataex: %s\n"),
947 if (!W_ERROR_IS_OK(result
)) {
948 printf(_("could not set printerdataex: %s\n"),
956 static bool net_spoolss_enumforms(struct rpc_pipe_client
*pipe_hnd
,
958 struct policy_handle
*hnd
,
961 union spoolss_FormInfo
**forms
)
966 result
= rpccli_spoolss_enumforms(pipe_hnd
, mem_ctx
,
972 if (!W_ERROR_IS_OK(result
)) {
973 printf(_("could not enum forms: %s\n"), win_errstr(result
));
980 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client
*pipe_hnd
,
982 uint32_t level
, const char *env
,
984 union spoolss_DriverInfo
**info
)
988 /* enumprinterdrivers call */
989 result
= rpccli_spoolss_enumprinterdrivers(pipe_hnd
, mem_ctx
,
990 pipe_hnd
->srv_name_slash
,
996 if (!W_ERROR_IS_OK(result
)) {
997 if (W_ERROR_V(result
) != W_ERROR_V(WERR_INVALID_ENVIRONMENT
)) {
998 printf(_("cannot enum drivers for environment %s: %s\n"), env
,
1002 printf(_("Server does not support environment [%s]\n"),
1010 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1011 TALLOC_CTX
*mem_ctx
,
1012 struct policy_handle
*hnd
, uint32_t level
,
1013 const char *env
, int version
,
1014 union spoolss_DriverInfo
*info
)
1017 uint32_t server_major_version
;
1018 uint32_t server_minor_version
;
1020 /* getprinterdriver call */
1021 result
= rpccli_spoolss_getprinterdriver2(pipe_hnd
, mem_ctx
,
1029 &server_major_version
,
1030 &server_minor_version
);
1031 if (!W_ERROR_IS_OK(result
)) {
1032 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1033 env
, win_errstr(result
)));
1034 if (W_ERROR_V(result
) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER
) &&
1035 W_ERROR_V(result
) != W_ERROR_V(WERR_INVALID_ENVIRONMENT
)) {
1036 printf(_("cannot get driver: %s\n"),
1037 win_errstr(result
));
1046 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1047 TALLOC_CTX
*mem_ctx
, uint32_t level
,
1048 union spoolss_DriverInfo
*info
)
1050 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1053 struct spoolss_AddDriverInfoCtr info_ctr
;
1055 info_ctr
.level
= level
;
1059 info_ctr
.info
.info2
= (struct spoolss_AddDriverInfo2
*)
1060 (void *)&info
->info2
;
1063 info_ctr
.info
.info3
= (struct spoolss_AddDriverInfo3
*)
1064 (void *)&info
->info3
;
1067 printf(_("unsupported info level: %d\n"), level
);
1071 /* addprinterdriver call */
1072 status
= dcerpc_spoolss_AddPrinterDriver(b
, mem_ctx
,
1073 pipe_hnd
->srv_name_slash
,
1076 if (!NT_STATUS_IS_OK(status
)) {
1077 printf(_("cannot add driver: %s\n"), nt_errstr(status
));
1080 /* be more verbose */
1081 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
1082 printf(_("You are not allowed to add drivers\n"));
1085 if (!W_ERROR_IS_OK(result
)) {
1086 printf(_("cannot add driver: %s\n"), win_errstr(result
));
1094 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1095 * for a single printer or for all printers depending on argc/argv
1098 static bool get_printer_info(struct rpc_pipe_client
*pipe_hnd
,
1099 TALLOC_CTX
*mem_ctx
,
1103 uint32_t *num_printers
,
1104 union spoolss_PrinterInfo
**info_p
)
1106 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1107 struct policy_handle hnd
;
1110 /* no arguments given, enumerate all printers */
1113 if (!net_spoolss_enum_printers(pipe_hnd
, mem_ctx
, NULL
,
1114 PRINTER_ENUM_LOCAL
|PRINTER_ENUM_SHARED
,
1115 level
, num_printers
, info_p
))
1121 /* argument given, get a single printer by name */
1122 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, argv
[0],
1123 MAXIMUM_ALLOWED_ACCESS
,
1124 pipe_hnd
->auth
->user_name
,
1128 *info_p
= talloc_zero(mem_ctx
, union spoolss_PrinterInfo
);
1129 if (*info_p
== NULL
) {
1133 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, *info_p
)) {
1134 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1138 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1143 DEBUG(3,("got %d printers\n", *num_printers
));
1150 * List print-queues (including local printers that are not shared)
1152 * All parameters are provided by the run_rpc_command function, except for
1153 * argc, argv which are passed through.
1155 * @param c A net_context structure
1156 * @param domain_sid The domain sid aquired from the remote server
1157 * @param cli A cli_state connected to the server.
1158 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1159 * @param argc Standard main() style argc
1160 * @param argv Standard main() style argv. Initial components are already
1163 * @return Normal NTSTATUS return.
1166 NTSTATUS
rpc_printer_list_internals(struct net_context
*c
,
1167 const struct dom_sid
*domain_sid
,
1168 const char *domain_name
,
1169 struct cli_state
*cli
,
1170 struct rpc_pipe_client
*pipe_hnd
,
1171 TALLOC_CTX
*mem_ctx
,
1175 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1176 uint32_t i
, num_printers
;
1178 const char *printername
, *sharename
;
1179 union spoolss_PrinterInfo
*info
;
1181 printf("listing printers\n");
1183 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info
))
1186 for (i
= 0; i
< num_printers
; i
++) {
1188 /* do some initialization */
1189 printername
= info
[i
].info2
.printername
;
1190 sharename
= info
[i
].info2
.sharename
;
1192 if (printername
&& sharename
) {
1193 d_printf(_("printer %d: %s, shared as: %s\n"),
1194 i
+1, printername
, sharename
);
1198 return NT_STATUS_OK
;
1202 * List printer-drivers from a server
1204 * All parameters are provided by the run_rpc_command function, except for
1205 * argc, argv which are passed through.
1207 * @param c A net_context structure
1208 * @param domain_sid The domain sid aquired from the remote server
1209 * @param cli A cli_state connected to the server.
1210 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1211 * @param argc Standard main() style argc
1212 * @param argv Standard main() style argv. Initial components are already
1215 * @return Normal NTSTATUS return.
1218 NTSTATUS
rpc_printer_driver_list_internals(struct net_context
*c
,
1219 const struct dom_sid
*domain_sid
,
1220 const char *domain_name
,
1221 struct cli_state
*cli
,
1222 struct rpc_pipe_client
*pipe_hnd
,
1223 TALLOC_CTX
*mem_ctx
,
1227 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1230 union spoolss_DriverInfo
*info
;
1233 printf(_("listing printer-drivers\n"));
1235 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
1237 uint32_t num_drivers
;
1239 /* enum remote drivers */
1240 if (!net_spoolss_enumprinterdrivers(pipe_hnd
, mem_ctx
, level
,
1241 archi_table
[i
].long_archi
,
1242 &num_drivers
, &info
)) {
1243 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1247 if (num_drivers
== 0) {
1248 d_printf(_("no drivers found on server for "
1249 "architecture: [%s].\n"),
1250 archi_table
[i
].long_archi
);
1254 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1255 num_drivers
, archi_table
[i
].long_archi
);
1258 /* do something for all drivers for architecture */
1259 for (d
= 0; d
< num_drivers
; d
++) {
1260 display_print_driver3(&info
[d
].info3
);
1264 nt_status
= NT_STATUS_OK
;
1272 * Publish print-queues with args-wrapper
1274 * @param cli A cli_state connected to the server.
1275 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1276 * @param argc Standard main() style argc
1277 * @param argv Standard main() style argv. Initial components are already
1281 * @return Normal NTSTATUS return.
1284 static NTSTATUS
rpc_printer_publish_internals_args(struct rpc_pipe_client
*pipe_hnd
,
1285 TALLOC_CTX
*mem_ctx
,
1290 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1291 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1292 uint32_t i
, num_printers
;
1294 const char *printername
, *sharename
;
1295 union spoolss_PrinterInfo
*info_enum
;
1296 union spoolss_PrinterInfo info
;
1297 struct spoolss_SetPrinterInfoCtr info_ctr
;
1298 struct spoolss_DevmodeContainer devmode_ctr
;
1299 struct sec_desc_buf secdesc_ctr
;
1300 struct policy_handle hnd
;
1302 const char *action_str
;
1304 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
))
1307 for (i
= 0; i
< num_printers
; i
++) {
1309 /* do some initialization */
1310 printername
= info_enum
[i
].info2
.printername
;
1311 sharename
= info_enum
[i
].info2
.sharename
;
1312 if (!printername
|| !sharename
) {
1316 /* open printer handle */
1317 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1318 PRINTER_ALL_ACCESS
, pipe_hnd
->auth
->user_name
, &hnd
))
1321 /* check for existing dst printer */
1322 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &info
))
1325 /* check action and set string */
1327 case DSPRINT_PUBLISH
:
1328 action_str
= N_("published");
1330 case DSPRINT_UPDATE
:
1331 action_str
= N_("updated");
1333 case DSPRINT_UNPUBLISH
:
1334 action_str
= N_("unpublished");
1337 action_str
= N_("unknown action");
1338 printf(_("unknown action: %d\n"), action
);
1342 info
.info7
.action
= action
;
1344 info_ctr
.info
.info7
= (struct spoolss_SetPrinterInfo7
*)
1345 (void *)&info
.info7
;
1347 ZERO_STRUCT(devmode_ctr
);
1348 ZERO_STRUCT(secdesc_ctr
);
1350 nt_status
= dcerpc_spoolss_SetPrinter(b
, mem_ctx
,
1357 if (!NT_STATUS_IS_OK(nt_status
)) {
1358 printf(_("cannot set printer-info: %s\n"),
1359 nt_errstr(nt_status
));
1362 if (!W_ERROR_IS_OK(result
) && !W_ERROR_EQUAL(result
, WERR_IO_PENDING
)) {
1363 if ((action
== DSPRINT_UPDATE
) && W_ERROR_EQUAL(result
, W_ERROR(0x80070002))) {
1364 printf(_("printer not published yet\n"));
1366 printf(_("cannot set printer-info: %s\n"),
1367 win_errstr(result
));
1369 nt_status
= werror_to_ntstatus(result
);
1373 printf(_("successfully %s printer %s in Active Directory\n"),
1374 action_str
, sharename
);
1377 nt_status
= NT_STATUS_OK
;
1380 if (is_valid_policy_hnd(&hnd
)) {
1381 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &result
);
1387 NTSTATUS
rpc_printer_publish_publish_internals(struct net_context
*c
,
1388 const struct dom_sid
*domain_sid
,
1389 const char *domain_name
,
1390 struct cli_state
*cli
,
1391 struct rpc_pipe_client
*pipe_hnd
,
1392 TALLOC_CTX
*mem_ctx
,
1396 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, DSPRINT_PUBLISH
);
1399 NTSTATUS
rpc_printer_publish_unpublish_internals(struct net_context
*c
,
1400 const struct dom_sid
*domain_sid
,
1401 const char *domain_name
,
1402 struct cli_state
*cli
,
1403 struct rpc_pipe_client
*pipe_hnd
,
1404 TALLOC_CTX
*mem_ctx
,
1408 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, DSPRINT_UNPUBLISH
);
1411 NTSTATUS
rpc_printer_publish_update_internals(struct net_context
*c
,
1412 const struct dom_sid
*domain_sid
,
1413 const char *domain_name
,
1414 struct cli_state
*cli
,
1415 struct rpc_pipe_client
*pipe_hnd
,
1416 TALLOC_CTX
*mem_ctx
,
1420 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, DSPRINT_UPDATE
);
1424 * List print-queues w.r.t. their publishing state
1426 * All parameters are provided by the run_rpc_command function, except for
1427 * argc, argv which are passed through.
1429 * @param c A net_context structure
1430 * @param domain_sid The domain sid aquired from the remote server
1431 * @param cli A cli_state connected to the server.
1432 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1433 * @param argc Standard main() style argc
1434 * @param argv Standard main() style argv. Initial components are already
1437 * @return Normal NTSTATUS return.
1440 NTSTATUS
rpc_printer_publish_list_internals(struct net_context
*c
,
1441 const struct dom_sid
*domain_sid
,
1442 const char *domain_name
,
1443 struct cli_state
*cli
,
1444 struct rpc_pipe_client
*pipe_hnd
,
1445 TALLOC_CTX
*mem_ctx
,
1449 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1450 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1451 uint32_t i
, num_printers
;
1453 const char *printername
, *sharename
;
1454 union spoolss_PrinterInfo
*info_enum
;
1455 union spoolss_PrinterInfo info
;
1456 struct policy_handle hnd
;
1460 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
))
1463 for (i
= 0; i
< num_printers
; i
++) {
1465 /* do some initialization */
1466 printername
= info_enum
[i
].info2
.printername
;
1467 sharename
= info_enum
[i
].info2
.sharename
;
1469 if (!printername
|| !sharename
) {
1473 /* open printer handle */
1474 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1475 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd
))
1478 /* check for existing dst printer */
1479 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &info
))
1482 if (!info
.info7
.guid
) {
1485 state
= info
.info7
.action
;
1487 case DSPRINT_PUBLISH
:
1488 printf(_("printer [%s] is published"),
1491 printf(_(", guid: %s"),info
.info7
.guid
);
1494 case DSPRINT_UNPUBLISH
:
1495 printf(_("printer [%s] is unpublished\n"),
1498 case DSPRINT_UPDATE
:
1499 printf(_("printer [%s] is currently updating\n"),
1503 printf(_("unknown state: %d\n"), state
);
1508 nt_status
= NT_STATUS_OK
;
1511 if (is_valid_policy_hnd(&hnd
)) {
1512 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1519 * Migrate Printer-ACLs from a source server to the destination server
1521 * All parameters are provided by the run_rpc_command function, except for
1522 * argc, argv which are passed through.
1524 * @param c A net_context structure
1525 * @param domain_sid The domain sid aquired from the remote server
1526 * @param cli A cli_state connected to the server.
1527 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1528 * @param argc Standard main() style argc
1529 * @param argv Standard main() style argv. Initial components are already
1532 * @return Normal NTSTATUS return.
1535 NTSTATUS
rpc_printer_migrate_security_internals(struct net_context
*c
,
1536 const struct dom_sid
*domain_sid
,
1537 const char *domain_name
,
1538 struct cli_state
*cli
,
1539 struct rpc_pipe_client
*pipe_hnd
,
1540 TALLOC_CTX
*mem_ctx
,
1544 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1545 /* TODO: what now, info2 or info3 ?
1546 convince jerry that we should add clientside setacls level 3 at least
1548 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1550 uint32_t num_printers
;
1552 const char *printername
, *sharename
;
1553 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1554 struct dcerpc_binding_handle
*b_dst
= NULL
;
1555 struct policy_handle hnd_src
, hnd_dst
;
1556 union spoolss_PrinterInfo
*info_enum
;
1557 struct cli_state
*cli_dst
= NULL
;
1558 union spoolss_PrinterInfo info_src
, info_dst
;
1561 DEBUG(3,("copying printer ACLs\n"));
1563 /* connect destination PI_SPOOLSS */
1564 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1565 &ndr_table_spoolss
.syntax_id
);
1566 if (!NT_STATUS_IS_OK(nt_status
)) {
1569 b_dst
= pipe_hnd_dst
->binding_handle
;
1571 /* enum source printers */
1572 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
1573 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1577 if (!num_printers
) {
1578 printf (_("no printers found on server.\n"));
1579 nt_status
= NT_STATUS_OK
;
1583 /* do something for all printers */
1584 for (i
= 0; i
< num_printers
; i
++) {
1586 /* do some initialization */
1587 printername
= info_enum
[i
].info2
.printername
;
1588 sharename
= info_enum
[i
].info2
.sharename
;
1590 if (!printername
|| !sharename
) {
1591 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1595 /* we can reset NT_STATUS here because we do not
1596 get any real NT_STATUS-codes anymore from now on */
1597 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1599 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1600 printername
, sharename
);
1602 /* according to msdn you have specify these access-rights
1603 to see the security descriptor
1604 - READ_CONTROL (DACL)
1605 - ACCESS_SYSTEM_SECURITY (SACL)
1608 /* open src printer handle */
1609 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1610 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
1613 /* open dst printer handle */
1614 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1615 PRINTER_ALL_ACCESS
, cli_dst
->user_name
, &hnd_dst
))
1618 /* check for existing dst printer */
1619 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
))
1622 /* check for existing src printer */
1623 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, 3, &info_src
))
1626 /* Copy Security Descriptor */
1628 /* copy secdesc (info level 2) */
1629 info_dst
.info2
.devmode
= NULL
;
1630 info_dst
.info2
.secdesc
= dup_sec_desc(mem_ctx
, info_src
.info3
.secdesc
);
1633 display_sec_desc(info_dst
.info2
.secdesc
);
1635 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
1638 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1641 /* close printer handles here */
1642 if (is_valid_policy_hnd(&hnd_src
)) {
1643 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
1646 if (is_valid_policy_hnd(&hnd_dst
)) {
1647 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
1652 nt_status
= NT_STATUS_OK
;
1656 if (is_valid_policy_hnd(&hnd_src
)) {
1657 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
1660 if (is_valid_policy_hnd(&hnd_dst
)) {
1661 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
1665 cli_shutdown(cli_dst
);
1671 * Migrate printer-forms from a src server to the dst server
1673 * All parameters are provided by the run_rpc_command function, except for
1674 * argc, argv which are passed through.
1676 * @param c A net_context structure
1677 * @param domain_sid The domain sid aquired from the remote server
1678 * @param cli A cli_state connected to the server.
1679 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1680 * @param argc Standard main() style argc
1681 * @param argv Standard main() style argv. Initial components are already
1684 * @return Normal NTSTATUS return.
1687 NTSTATUS
rpc_printer_migrate_forms_internals(struct net_context
*c
,
1688 const struct dom_sid
*domain_sid
,
1689 const char *domain_name
,
1690 struct cli_state
*cli
,
1691 struct rpc_pipe_client
*pipe_hnd
,
1692 TALLOC_CTX
*mem_ctx
,
1696 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1697 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1700 uint32_t num_printers
;
1702 const char *printername
, *sharename
;
1703 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1704 struct dcerpc_binding_handle
*b_dst
= NULL
;
1705 struct policy_handle hnd_src
, hnd_dst
;
1706 union spoolss_PrinterInfo
*info_enum
;
1707 union spoolss_PrinterInfo info_dst
;
1709 union spoolss_FormInfo
*forms
;
1710 struct cli_state
*cli_dst
= NULL
;
1712 DEBUG(3,("copying forms\n"));
1714 /* connect destination PI_SPOOLSS */
1715 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1716 &ndr_table_spoolss
.syntax_id
);
1717 if (!NT_STATUS_IS_OK(nt_status
)) {
1720 b_dst
= pipe_hnd_dst
->binding_handle
;
1722 /* enum src printers */
1723 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1724 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1728 if (!num_printers
) {
1729 printf (_("no printers found on server.\n"));
1730 nt_status
= NT_STATUS_OK
;
1734 /* do something for all printers */
1735 for (i
= 0; i
< num_printers
; i
++) {
1737 /* do some initialization */
1738 printername
= info_enum
[i
].info2
.printername
;
1739 sharename
= info_enum
[i
].info2
.sharename
;
1741 if (!printername
|| !sharename
) {
1742 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1745 /* we can reset NT_STATUS here because we do not
1746 get any real NT_STATUS-codes anymore from now on */
1747 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1749 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1750 printername
, sharename
);
1753 /* open src printer handle */
1754 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1755 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
1758 /* open dst printer handle */
1759 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1760 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd_dst
))
1763 /* check for existing dst printer */
1764 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
))
1767 /* finally migrate forms */
1768 if (!net_spoolss_enumforms(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &num_forms
, &forms
))
1771 DEBUG(1,("got %d forms for printer\n", num_forms
));
1774 for (f
= 0; f
< num_forms
; f
++) {
1776 union spoolss_AddFormInfo info
;
1779 /* only migrate FORM_PRINTER types, according to jerry
1780 FORM_BUILTIN-types are hard-coded in samba */
1781 if (forms
[f
].info1
.flags
!= SPOOLSS_FORM_PRINTER
)
1785 d_printf(_("\tmigrating form # %d [%s] of type "
1787 f
, forms
[f
].info1
.form_name
,
1788 forms
[f
].info1
.flags
);
1790 info
.info1
= (struct spoolss_AddFormInfo1
*)
1791 (void *)&forms
[f
].info1
;
1793 /* FIXME: there might be something wrong with samba's
1795 status
= dcerpc_spoolss_AddForm(b_dst
, mem_ctx
,
1800 if (!NT_STATUS_IS_OK(status
)) {
1801 d_printf(_("\tdcerpc_spoolss_AddForm form %d: [%s] - %s\n"),
1802 f
, forms
[f
].info1
.form_name
, nt_errstr(status
));
1805 if (!W_ERROR_IS_OK(result
)) {
1806 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1807 f
, forms
[f
].info1
.form_name
);
1811 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1812 forms
[f
].info1
.form_name
));
1816 /* close printer handles here */
1817 if (is_valid_policy_hnd(&hnd_src
)) {
1818 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
1821 if (is_valid_policy_hnd(&hnd_dst
)) {
1822 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
1826 nt_status
= NT_STATUS_OK
;
1830 if (is_valid_policy_hnd(&hnd_src
)) {
1831 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
1834 if (is_valid_policy_hnd(&hnd_dst
)) {
1835 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
1839 cli_shutdown(cli_dst
);
1845 * Migrate printer-drivers from a src server to the dst server
1847 * All parameters are provided by the run_rpc_command function, except for
1848 * argc, argv which are passed through.
1850 * @param c A net_context structure
1851 * @param domain_sid The domain sid aquired from the remote server
1852 * @param cli A cli_state connected to the server.
1853 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1854 * @param argc Standard main() style argc
1855 * @param argv Standard main() style argv. Initial components are already
1858 * @return Normal NTSTATUS return.
1861 NTSTATUS
rpc_printer_migrate_drivers_internals(struct net_context
*c
,
1862 const struct dom_sid
*domain_sid
,
1863 const char *domain_name
,
1864 struct cli_state
*cli
,
1865 struct rpc_pipe_client
*pipe_hnd
,
1866 TALLOC_CTX
*mem_ctx
,
1870 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1871 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1873 uint32_t num_printers
;
1875 const char *printername
, *sharename
;
1876 bool got_src_driver_share
= false;
1877 bool got_dst_driver_share
= false;
1878 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1879 struct dcerpc_binding_handle
*b_dst
= NULL
;
1880 struct policy_handle hnd_src
, hnd_dst
;
1881 union spoolss_DriverInfo drv_info_src
;
1882 union spoolss_PrinterInfo
*info_enum
;
1883 union spoolss_PrinterInfo info_dst
;
1884 struct cli_state
*cli_dst
= NULL
;
1885 struct cli_state
*cli_share_src
= NULL
;
1886 struct cli_state
*cli_share_dst
= NULL
;
1887 const char *drivername
= NULL
;
1890 DEBUG(3,("copying printer-drivers\n"));
1892 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1893 &ndr_table_spoolss
.syntax_id
);
1894 if (!NT_STATUS_IS_OK(nt_status
)) {
1897 b_dst
= pipe_hnd_dst
->binding_handle
;
1899 /* open print$-share on the src server */
1900 nt_status
= connect_to_service(c
, &cli_share_src
, &cli
->dest_ss
,
1901 cli
->desthost
, "print$", "A:");
1902 if (!NT_STATUS_IS_OK(nt_status
))
1905 got_src_driver_share
= true;
1908 /* open print$-share on the dst server */
1909 nt_status
= connect_to_service(c
, &cli_share_dst
, &cli_dst
->dest_ss
,
1910 cli_dst
->desthost
, "print$", "A:");
1911 if (!NT_STATUS_IS_OK(nt_status
))
1914 got_dst_driver_share
= true;
1917 /* enum src printers */
1918 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1919 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1923 if (num_printers
== 0) {
1924 printf (_("no printers found on server.\n"));
1925 nt_status
= NT_STATUS_OK
;
1930 /* do something for all printers */
1931 for (p
= 0; p
< num_printers
; p
++) {
1933 /* do some initialization */
1934 printername
= info_enum
[p
].info2
.printername
;
1935 sharename
= info_enum
[p
].info2
.sharename
;
1937 if (!printername
|| !sharename
) {
1938 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1942 /* we can reset NT_STATUS here because we do not
1943 get any real NT_STATUS-codes anymore from now on */
1944 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1946 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1947 printername
, sharename
);
1949 /* open dst printer handle */
1950 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1951 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd_dst
))
1954 /* check for existing dst printer */
1955 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
1959 /* open src printer handle */
1960 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1961 MAXIMUM_ALLOWED_ACCESS
,
1962 pipe_hnd
->auth
->user_name
,
1966 /* in a first step call getdriver for each shared printer (per arch)
1967 to get a list of all files that have to be copied */
1969 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
1972 if (!net_spoolss_getprinterdriver(pipe_hnd
, mem_ctx
, &hnd_src
,
1973 level
, archi_table
[i
].long_archi
,
1974 archi_table
[i
].version
, &drv_info_src
))
1977 drivername
= drv_info_src
.info3
.driver_name
;
1980 display_print_driver3(&drv_info_src
.info3
);
1982 /* check arch dir */
1983 nt_status
= check_arch_dir(cli_share_dst
, archi_table
[i
].short_archi
);
1984 if (!NT_STATUS_IS_OK(nt_status
))
1988 /* copy driver-files */
1989 nt_status
= copy_print_driver_3(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
1990 archi_table
[i
].short_archi
,
1991 &drv_info_src
.info3
);
1992 if (!NT_STATUS_IS_OK(nt_status
))
1997 if (!net_spoolss_addprinterdriver(pipe_hnd_dst
, mem_ctx
, level
, &drv_info_src
)) {
1998 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2002 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
2003 drivername
, printername
));
2007 if (!drivername
|| strlen(drivername
) == 0) {
2008 DEBUGADD(1,("Did not get driver for printer %s\n",
2014 info_dst
.info2
.drivername
= drivername
;
2016 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
)) {
2017 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2021 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
2022 drivername
, printername
));
2025 if (is_valid_policy_hnd(&hnd_dst
)) {
2026 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2030 if (is_valid_policy_hnd(&hnd_src
)) {
2031 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2035 nt_status
= NT_STATUS_OK
;
2039 if (is_valid_policy_hnd(&hnd_dst
)) {
2040 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2044 if (is_valid_policy_hnd(&hnd_src
)) {
2045 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2049 cli_shutdown(cli_dst
);
2052 if (got_src_driver_share
)
2053 cli_shutdown(cli_share_src
);
2055 if (got_dst_driver_share
)
2056 cli_shutdown(cli_share_dst
);
2063 * Migrate printer-queues from a src to the dst server
2064 * (requires a working "addprinter command" to be installed for the local smbd)
2066 * All parameters are provided by the run_rpc_command function, except for
2067 * argc, argv which are passed through.
2069 * @param c A net_context structure
2070 * @param domain_sid The domain sid aquired from the remote server
2071 * @param cli A cli_state connected to the server.
2072 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2073 * @param argc Standard main() style argc
2074 * @param argv Standard main() style argv. Initial components are already
2077 * @return Normal NTSTATUS return.
2080 NTSTATUS
rpc_printer_migrate_printers_internals(struct net_context
*c
,
2081 const struct dom_sid
*domain_sid
,
2082 const char *domain_name
,
2083 struct cli_state
*cli
,
2084 struct rpc_pipe_client
*pipe_hnd
,
2085 TALLOC_CTX
*mem_ctx
,
2089 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2091 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2092 uint32_t i
= 0, num_printers
;
2094 union spoolss_PrinterInfo info_dst
, info_src
;
2095 union spoolss_PrinterInfo
*info_enum
;
2096 struct cli_state
*cli_dst
= NULL
;
2097 struct policy_handle hnd_dst
, hnd_src
;
2098 const char *printername
, *sharename
;
2099 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2100 struct dcerpc_binding_handle
*b_dst
= NULL
;
2101 struct spoolss_SetPrinterInfoCtr info_ctr
;
2103 DEBUG(3,("copying printers\n"));
2105 /* connect destination PI_SPOOLSS */
2106 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2107 &ndr_table_spoolss
.syntax_id
);
2108 if (!NT_STATUS_IS_OK(nt_status
)) {
2111 b_dst
= pipe_hnd_dst
->binding_handle
;
2114 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2115 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2119 if (!num_printers
) {
2120 printf (_("no printers found on server.\n"));
2121 nt_status
= NT_STATUS_OK
;
2125 /* do something for all printers */
2126 for (i
= 0; i
< num_printers
; i
++) {
2128 struct spoolss_SetPrinterInfo2 info2
;
2130 /* do some initialization */
2131 printername
= info_enum
[i
].info2
.printername
;
2132 sharename
= info_enum
[i
].info2
.sharename
;
2134 if (!printername
|| !sharename
) {
2135 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2138 /* we can reset NT_STATUS here because we do not
2139 get any real NT_STATUS-codes anymore from now on */
2140 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2142 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2143 printername
, sharename
);
2145 /* open dst printer handle */
2146 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2147 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd_dst
)) {
2149 DEBUG(1,("could not open printer: %s\n", sharename
));
2152 /* check for existing dst printer */
2153 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
)) {
2154 printf (_("could not get printer, creating printer.\n"));
2156 DEBUG(1,("printer already exists: %s\n", sharename
));
2157 /* close printer handle here - dst only, not got src yet. */
2158 if (is_valid_policy_hnd(&hnd_dst
)) {
2159 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2164 /* now get again src printer ctr via getprinter,
2165 we first need a handle for that */
2167 /* open src printer handle */
2168 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2169 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
2172 /* getprinter on the src server */
2173 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &info_src
))
2176 /* copy each src printer to a dst printer 1:1,
2177 maybe some values have to be changed though */
2178 d_printf(_("creating printer: %s\n"), printername
);
2180 info_ctr
.level
= level
;
2181 spoolss_printerinfo2_to_setprinterinfo2(&info_src
.info2
, &info2
);
2182 info_ctr
.info
.info2
= &info2
;
2184 result
= rpccli_spoolss_addprinterex(pipe_hnd_dst
,
2188 if (W_ERROR_IS_OK(result
))
2189 d_printf (_("printer [%s] successfully added.\n"),
2191 else if (W_ERROR_V(result
) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS
))
2192 d_fprintf (stderr
, _("printer [%s] already exists.\n"),
2195 d_fprintf (stderr
, _("could not create printer [%s]\n"),
2200 /* close printer handles here */
2201 if (is_valid_policy_hnd(&hnd_src
)) {
2202 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2205 if (is_valid_policy_hnd(&hnd_dst
)) {
2206 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2210 nt_status
= NT_STATUS_OK
;
2213 if (is_valid_policy_hnd(&hnd_src
)) {
2214 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2217 if (is_valid_policy_hnd(&hnd_dst
)) {
2218 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2222 cli_shutdown(cli_dst
);
2228 * Migrate Printer-Settings from a src server to the dst server
2229 * (for this to work, printers and drivers already have to be migrated earlier)
2231 * All parameters are provided by the run_rpc_command function, except for
2232 * argc, argv which are passed through.
2234 * @param c A net_context structure
2235 * @param domain_sid The domain sid aquired from the remote server
2236 * @param cli A cli_state connected to the server.
2237 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2238 * @param argc Standard main() style argc
2239 * @param argv Standard main() style argv. Initial components are already
2242 * @return Normal NTSTATUS return.
2245 NTSTATUS
rpc_printer_migrate_settings_internals(struct net_context
*c
,
2246 const struct dom_sid
*domain_sid
,
2247 const char *domain_name
,
2248 struct cli_state
*cli
,
2249 struct rpc_pipe_client
*pipe_hnd
,
2250 TALLOC_CTX
*mem_ctx
,
2254 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2256 /* FIXME: Here the nightmare begins */
2259 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2260 uint32_t i
= 0, j
= 0;
2261 uint32_t num_printers
;
2263 const char *printername
, *sharename
;
2264 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2265 struct dcerpc_binding_handle
*b_dst
= NULL
;
2266 struct policy_handle hnd_src
, hnd_dst
;
2267 union spoolss_PrinterInfo
*info_enum
;
2268 union spoolss_PrinterInfo info_dst_publish
;
2269 union spoolss_PrinterInfo info_dst
;
2270 struct cli_state
*cli_dst
= NULL
;
2271 char *devicename
= NULL
, *unc_name
= NULL
, *url
= NULL
;
2272 const char *longname
;
2273 const char **keylist
= NULL
;
2276 ZERO_STRUCT(info_dst_publish
);
2278 DEBUG(3,("copying printer settings\n"));
2280 /* connect destination PI_SPOOLSS */
2281 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2282 &ndr_table_spoolss
.syntax_id
);
2283 if (!NT_STATUS_IS_OK(nt_status
)) {
2286 b_dst
= pipe_hnd_dst
->binding_handle
;
2288 /* enum src printers */
2289 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2290 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2294 if (!num_printers
) {
2295 printf (_("no printers found on server.\n"));
2296 nt_status
= NT_STATUS_OK
;
2301 /* needed for dns-strings in regkeys */
2302 longname
= get_mydnsfullname();
2304 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2308 /* do something for all printers */
2309 for (i
= 0; i
< num_printers
; i
++) {
2311 uint32_t value_needed
;
2312 uint32_t data_needed
;
2313 enum winreg_Type type
;
2314 struct spoolss_EnumPrinterData r
;
2316 /* do some initialization */
2317 printername
= info_enum
[i
].info2
.printername
;
2318 sharename
= info_enum
[i
].info2
.sharename
;
2320 if (!printername
|| !sharename
) {
2321 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2324 /* we can reset NT_STATUS here because we do not
2325 get any real NT_STATUS-codes anymore from now on */
2326 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2328 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2329 printername
, sharename
);
2332 /* open src printer handle */
2333 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2334 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
2337 /* open dst printer handle */
2338 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2339 PRINTER_ALL_ACCESS
, cli_dst
->user_name
, &hnd_dst
))
2342 /* check for existing dst printer */
2343 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2348 /* STEP 1: COPY DEVICE-MODE and other
2349 PRINTER_INFO_2-attributes
2352 info_dst
.info2
= info_enum
[i
].info2
;
2354 /* why is the port always disconnected when the printer
2355 is correctly installed (incl. driver ???) */
2356 info_dst
.info2
.portname
= SAMBA_PRINTER_PORT_NAME
;
2358 /* check if printer is published */
2359 if (info_enum
[i
].info2
.attributes
& PRINTER_ATTRIBUTE_PUBLISHED
) {
2361 /* check for existing dst printer */
2362 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
))
2365 info_dst_publish
.info7
.action
= DSPRINT_PUBLISH
;
2367 /* ignore false from setprinter due to WERR_IO_PENDING */
2368 net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
);
2370 DEBUG(3,("republished printer\n"));
2373 if (info_enum
[i
].info2
.devmode
!= NULL
) {
2375 /* copy devmode (info level 2) */
2376 info_dst
.info2
.devmode
= info_enum
[i
].info2
.devmode
;
2378 /* do not copy security descriptor (we have another
2379 * command for that) */
2380 info_dst
.info2
.secdesc
= NULL
;
2383 info_dst
.info2
.devmode
.devicename
=
2384 talloc_asprintf(mem_ctx
, "\\\\%s\\%s",
2385 longname
, printername
);
2386 if (!info_dst
.info2
.devmode
.devicename
) {
2387 nt_status
= NT_STATUS_NO_MEMORY
;
2391 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2395 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2398 /* STEP 2: COPY REGISTRY VALUES */
2400 /* please keep in mind that samba parse_spools gives horribly
2401 crippled results when used to rpccli_spoolss_enumprinterdataex
2402 a win2k3-server. (Bugzilla #1851)
2403 FIXME: IIRC I've seen it too on a win2k-server
2406 r
.in
.handle
= &hnd_src
;
2407 r
.in
.enum_index
= 0;
2408 r
.in
.value_offered
= 0;
2409 r
.in
.data_offered
= 0;
2410 r
.out
.value_name
= NULL
;
2411 r
.out
.value_needed
= &value_needed
;
2414 r
.out
.data_needed
= &data_needed
;
2416 /* enumerate data on src handle */
2417 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2419 r
.in
.data_offered
= *r
.out
.data_needed
;
2420 r
.in
.value_offered
= *r
.out
.value_needed
;
2421 r
.out
.data
= talloc_zero_array(mem_ctx
, uint8_t, r
.in
.data_offered
);
2422 r
.out
.value_name
= talloc_zero_array(mem_ctx
, char, r
.in
.value_offered
);
2424 /* loop for all printerdata of "PrinterDriverData" */
2425 while (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2429 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2431 /* loop for all reg_keys */
2432 if (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2435 if (c
->opt_verbose
) {
2436 struct regval_blob
*v
;
2438 v
= regval_compose(talloc_tos(),
2444 nt_status
= NT_STATUS_NO_MEMORY
;
2448 display_reg_value(SPOOL_PRINTERDATA_KEY
, v
);
2453 if (!net_spoolss_setprinterdata(pipe_hnd_dst
, mem_ctx
,
2454 &hnd_dst
, r
.out
.value_name
,
2455 *r
.out
.type
, r
.out
.data
, r
.in
.data_offered
))
2458 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2463 /* STEP 3: COPY SUBKEY VALUES */
2465 /* here we need to enum all printer_keys and then work
2466 on the result with enum_printer_key_ex. nt4 does not
2467 respond to enumprinterkey, win2k does, so continue
2468 in case of an error */
2470 if (!net_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, &hnd_src
, "", &keylist
)) {
2471 printf(_("got no key-data\n"));
2476 /* work on a list of printer keys
2477 each key has to be enumerated to get all required
2478 information. information is then set via setprinterdataex-calls */
2480 if (keylist
== NULL
)
2483 for (i
=0; keylist
&& keylist
[i
] != NULL
; i
++) {
2485 const char *subkey
= keylist
[i
];
2487 struct spoolss_PrinterEnumValues
*info
;
2489 /* enumerate all src subkeys */
2490 if (!net_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
, 0,
2496 for (j
=0; j
< count
; j
++) {
2498 struct regval_blob
*value
;
2503 /* although samba replies with sane data in most cases we
2504 should try to avoid writing wrong registry data */
2506 if (strequal(info
[j
].value_name
, SPOOL_REG_PORTNAME
) ||
2507 strequal(info
[j
].value_name
, SPOOL_REG_UNCNAME
) ||
2508 strequal(info
[j
].value_name
, SPOOL_REG_URL
) ||
2509 strequal(info
[j
].value_name
, SPOOL_REG_SHORTSERVERNAME
) ||
2510 strequal(info
[j
].value_name
, SPOOL_REG_SERVERNAME
)) {
2512 if (strequal(info
[j
].value_name
, SPOOL_REG_PORTNAME
)) {
2514 /* although windows uses a multi-sz, we use a sz */
2515 push_reg_sz(mem_ctx
, &blob
, SAMBA_PRINTER_PORT_NAME
);
2518 if (strequal(info
[j
].value_name
, SPOOL_REG_UNCNAME
)) {
2520 if (asprintf(&unc_name
, "\\\\%s\\%s", longname
, sharename
) < 0) {
2521 nt_status
= NT_STATUS_NO_MEMORY
;
2524 push_reg_sz(mem_ctx
, &blob
, unc_name
);
2527 if (strequal(info
[j
].value_name
, SPOOL_REG_URL
)) {
2532 /* FIXME: should we really do that ??? */
2533 if (asprintf(&url
, "http://%s:631/printers/%s", longname
, sharename
) < 0) {
2534 nt_status
= NT_STATUS_NO_MEMORY
;
2537 push_reg_sz(mem_ctx
, NULL
, &blob
, url
);
2538 fstrcpy(value
.valuename
, SPOOL_REG_URL
);
2542 if (strequal(info
[j
].value_name
, SPOOL_REG_SERVERNAME
)) {
2544 push_reg_sz(mem_ctx
, &blob
, longname
);
2547 if (strequal(info
[j
].value_name
, SPOOL_REG_SHORTSERVERNAME
)) {
2549 push_reg_sz(mem_ctx
, &blob
, global_myname());
2552 value
= regval_compose(talloc_tos(),
2555 blob
.length
== 0 ? NULL
: blob
.data
,
2557 if (value
== NULL
) {
2558 nt_status
= NT_STATUS_NO_MEMORY
;
2563 display_reg_value(subkey
, value
);
2565 /* here we have to set all subkeys on the dst server */
2566 if (!net_spoolss_setprinterdataex(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2576 struct regval_blob
*v
;
2578 v
= regval_compose(talloc_tos(),
2582 info
[j
].data
->length
);
2584 nt_status
= NT_STATUS_NO_MEMORY
;
2588 if (c
->opt_verbose
) {
2589 display_reg_value(subkey
, v
);
2592 /* here we have to set all subkeys on the dst server */
2593 if (!net_spoolss_setprinterdataex(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2601 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2602 subkey
, info
[j
].value_name
));
2607 TALLOC_FREE(keylist
);
2609 /* close printer handles here */
2610 if (is_valid_policy_hnd(&hnd_src
)) {
2611 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2614 if (is_valid_policy_hnd(&hnd_dst
)) {
2615 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2619 nt_status
= NT_STATUS_OK
;
2622 SAFE_FREE(devicename
);
2624 SAFE_FREE(unc_name
);
2626 if (is_valid_policy_hnd(&hnd_src
)) {
2627 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2630 if (is_valid_policy_hnd(&hnd_dst
)) {
2631 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2635 cli_shutdown(cli_dst
);