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
)
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
;
168 if (!copy_timestamps
&& !copy_acls
&& !copy_attrs
)
171 /* open file/dir on the originating server */
173 DEBUGADD(3,("opening %s %s on originating server\n",
174 is_file
?"file":"dir", src_name
));
176 nt_status
= cli_ntcreate(cli_share_src
, src_name
, 0,
177 READ_CONTROL_ACCESS
, 0,
178 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
179 0x0, 0x0, &fnum_src
);
180 if (!NT_STATUS_IS_OK(nt_status
)) {
181 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
182 is_file
?"file":"dir", src_name
, nt_errstr(nt_status
)));
187 /* get the security descriptor */
188 sd
= cli_query_secdesc(cli_share_src
, fnum_src
, mem_ctx
);
190 DEBUG(0,("failed to get security descriptor: %s\n",
191 cli_errstr(cli_share_src
)));
192 nt_status
= cli_nt_error(cli_share_src
);
196 if (c
->opt_verbose
&& DEBUGLEVEL
>= 3)
197 display_sec_desc(sd
);
200 if (copy_attrs
|| copy_timestamps
) {
202 /* get file attributes */
203 nt_status
= cli_getattrE(cli_share_src
, fnum_src
, &attr
, NULL
,
204 &f_ctime
, &f_atime
, &f_mtime
);
205 if (!NT_STATUS_IS_OK(nt_status
)) {
206 DEBUG(0,("failed to get file-attrs: %s\n",
207 nt_errstr(nt_status
)));
212 /* open the file/dir on the destination server */
213 nt_status
= cli_ntcreate(cli_share_dst
, dst_name
, 0,
214 WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
, 0,
215 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
216 0x0, 0x0, &fnum_dst
);
217 if (!NT_STATUS_IS_OK(nt_status
)) {
218 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
219 is_file
?"file":"dir", dst_name
, nt_errstr(nt_status
)));
223 if (copy_timestamps
) {
225 nt_status
= cli_setattrE(cli_share_dst
, fnum_dst
, f_ctime
, f_atime
, f_mtime
);
226 if (!NT_STATUS_IS_OK(nt_status
)) {
227 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
228 nt_errstr(nt_status
)));
235 nt_status
= cli_set_secdesc(cli_share_dst
, fnum_dst
, sd
);
236 if (!NT_STATUS_IS_OK(nt_status
)) {
237 DEBUG(0, ("could not set secdesc on %s: %s\n",
238 dst_name
, nt_errstr(nt_status
)));
245 nt_status
= cli_setatr(cli_share_dst
, dst_name
, attr
, 0);
246 if (!NT_STATUS_IS_OK(nt_status
)) {
247 DEBUG(0,("failed to set file-attrs: %s\n",
248 nt_errstr(nt_status
)));
255 nt_status
= cli_close(cli_share_src
, fnum_src
);
256 if (!NT_STATUS_IS_OK(nt_status
)) {
258 _("could not close %s on originating server: %s\n"),
259 is_file
?"file":"dir", nt_errstr(nt_status
));
263 nt_status
= cli_close(cli_share_dst
, fnum_dst
);
264 if (!NT_STATUS_IS_OK(nt_status
)) {
266 _("could not close %s on destination server: %s\n"),
267 is_file
?"file":"dir", nt_errstr(nt_status
));
272 nt_status
= NT_STATUS_OK
;
278 cli_close(cli_share_src
, fnum_src
);
281 cli_close(cli_share_dst
, fnum_dst
);
287 * Copy a file or directory from a connected share to another connected share
289 * @param c A net_context structure
290 * @param mem_ctx A talloc-context
291 * @param cli_share_src A connected cli_state
292 * @param cli_share_dst A connected cli_state
293 * @param src_file The source file-name
294 * @param dst_file The destination file-name
295 * @param copy_acls Whether to copy acls
296 * @param copy_attrs Whether to copy DOS attributes
297 * @param copy_timestamps Whether to preserve timestamps
298 * @param is_file Whether this file is a file or a dir
300 * @return Normal NTSTATUS return.
303 NTSTATUS
net_copy_file(struct net_context
*c
,
305 struct cli_state
*cli_share_src
,
306 struct cli_state
*cli_share_dst
,
307 const char *src_name
, const char *dst_name
,
308 bool copy_acls
, bool copy_attrs
,
309 bool copy_timestamps
, bool is_file
)
311 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
312 uint16_t fnum_src
= 0;
313 uint16_t fnum_dst
= 0;
314 static int io_bufsize
= 64512;
315 int read_size
= io_bufsize
;
320 if (!src_name
|| !dst_name
)
323 if (cli_share_src
== NULL
|| cli_share_dst
== NULL
)
326 /* open on the originating server */
327 DEBUGADD(3,("opening %s %s on originating server\n",
328 is_file
? "file":"dir", src_name
));
330 nt_status
= cli_open(cli_share_src
, src_name
, O_RDONLY
, DENY_NONE
, &fnum_src
);
332 nt_status
= cli_ntcreate(cli_share_src
, src_name
, 0, READ_CONTROL_ACCESS
, 0,
333 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum_src
);
335 if (!NT_STATUS_IS_OK(nt_status
)) {
336 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
337 is_file
? "file":"dir",
338 src_name
, nt_errstr(nt_status
)));
345 /* open file on the destination server */
346 DEBUGADD(3,("opening file %s on destination server\n", dst_name
));
347 nt_status
= cli_open(cli_share_dst
, dst_name
,
348 O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
, &fnum_dst
);
350 if (!NT_STATUS_IS_OK(nt_status
)) {
351 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
352 dst_name
, nt_errstr(nt_status
)));
356 /* allocate memory */
357 if (!(data
= (char *)SMB_MALLOC(read_size
))) {
358 d_fprintf(stderr
, _("malloc fail for size %d\n"),
360 nt_status
= NT_STATUS_NO_MEMORY
;
367 if (c
->opt_verbose
) {
369 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
370 "%s ACLs and %s DOS Attributes %s\n"),
371 cli_state_remote_name(cli_share_src
),
372 cli_share_src
->share
, src_name
,
373 cli_state_remote_name(cli_share_dst
),
374 cli_share_dst
->share
, dst_name
,
375 copy_acls
? _("with") : _("without"),
376 copy_attrs
? _("with") : _("without"),
377 copy_timestamps
? _("(preserving timestamps)") : "" );
385 n
= cli_read_old(cli_share_src
, fnum_src
, data
, nread
,
391 nt_status
= cli_writeall(cli_share_dst
, fnum_dst
, 0,
392 (uint8_t *)data
, nread
, n
, NULL
);
394 if (!NT_STATUS_IS_OK(nt_status
)) {
395 d_fprintf(stderr
, _("Error writing file: %s\n"),
396 nt_errstr(nt_status
));
404 if (!is_file
&& !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst
, dst_name
))) {
407 DEBUGADD(3,("creating dir %s on the destination server\n",
410 nt_status
= cli_mkdir(cli_share_dst
, dst_name
);
411 if (!NT_STATUS_IS_OK(nt_status
)) {
412 DEBUG(0,("cannot create directory %s: %s\n",
413 dst_name
, nt_errstr(nt_status
)));
414 nt_status
= NT_STATUS_NO_SUCH_FILE
;
418 nt_status
= cli_chkpath(cli_share_dst
, dst_name
);
419 if (!NT_STATUS_IS_OK(nt_status
)) {
421 _("cannot check for directory %s: %s\n"),
422 dst_name
, nt_errstr(nt_status
));
429 nt_status
= cli_close(cli_share_src
, fnum_src
);
430 if (!NT_STATUS_IS_OK(nt_status
)) {
432 _("could not close file on originating server: %s\n"),
433 nt_errstr(nt_status
));
438 nt_status
= cli_close(cli_share_dst
, fnum_dst
);
439 if (!NT_STATUS_IS_OK(nt_status
)) {
441 _("could not close file on destination server: %s\n"),
442 nt_errstr(nt_status
));
447 /* possibly we have to copy some file-attributes / acls / sd */
448 nt_status
= net_copy_fileattr(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
449 src_name
, dst_name
, copy_acls
,
450 copy_attrs
, copy_timestamps
, is_file
);
451 if (!NT_STATUS_IS_OK(nt_status
))
455 nt_status
= NT_STATUS_OK
;
461 cli_close(cli_share_src
, fnum_src
);
464 cli_close(cli_share_dst
, fnum_dst
);
472 * Copy a driverfile from on connected share to another connected share
473 * This silently assumes that a driver-file is picked up from
475 * \\src_server\print$\{arch}\{version}\file
479 * \\dst_server\print$\{arch}\file
481 * to be added via setdriver-calls later.
482 * @param c A net_context structure
483 * @param mem_ctx A talloc-context
484 * @param cli_share_src A cli_state connected to source print$-share
485 * @param cli_share_dst A cli_state connected to destination print$-share
486 * @param file The file-name to be copied
487 * @param short_archi The name of the driver-architecture (short form)
489 * @return Normal NTSTATUS return.
492 static NTSTATUS
net_copy_driverfile(struct net_context
*c
,
494 struct cli_state
*cli_share_src
,
495 struct cli_state
*cli_share_dst
,
496 const char *file
, const char *short_archi
) {
501 char *version
= NULL
;
502 char *filename
= NULL
;
509 /* scroll through the file until we have the part
510 beyond archi_table.short_archi */
512 while (next_token_talloc(mem_ctx
, &p
, &tok
, "\\")) {
513 if (strequal(tok
, short_archi
)) {
514 next_token_talloc(mem_ctx
, &p
, &version
, "\\");
515 next_token_talloc(mem_ctx
, &p
, &filename
, "\\");
519 if (version
== NULL
|| filename
== NULL
) {
520 return NT_STATUS_UNSUCCESSFUL
;
523 /* build source file name */
524 src_name
= talloc_asprintf(mem_ctx
, "\\%s\\%s\\%s",
525 short_archi
, version
, filename
);
526 if (src_name
== NULL
) {
527 return NT_STATUS_NO_MEMORY
;
530 /* create destination file name */
531 dst_name
= talloc_asprintf(mem_ctx
, "\\%s\\%s", short_archi
, filename
);
532 if (dst_name
== NULL
) {
533 return NT_STATUS_NO_MEMORY
;
537 /* finally copy the file */
538 return net_copy_file(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
539 src_name
, dst_name
, false, false, false, true);
543 * Check for existing Architecture directory on a given server
545 * @param cli_share A cli_state connected to a print$-share
546 * @param short_archi The Architecture for the print-driver
548 * @return Normal NTSTATUS return.
551 static NTSTATUS
check_arch_dir(struct cli_state
*cli_share
, const char *short_archi
)
554 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
557 if (asprintf(&dir
, "\\%s", short_archi
) < 0) {
558 return NT_STATUS_NO_MEMORY
;
561 DEBUG(10,("creating print-driver dir for architecture: %s\n",
564 nt_status
= cli_mkdir(cli_share
, dir
);
565 if (!NT_STATUS_IS_OK(nt_status
)) {
566 DEBUG(1,("cannot create directory %s: %s\n",
567 dir
, nt_errstr(nt_status
)));
570 nt_status
= cli_chkpath(cli_share
, dir
);
571 if (!NT_STATUS_IS_OK(nt_status
)) {
572 d_fprintf(stderr
, _("cannot check %s: %s\n"),
573 dir
, nt_errstr(nt_status
));
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
,
1901 cli_state_remote_sockaddr(cli
),
1902 cli_state_remote_name(cli
),
1904 if (!NT_STATUS_IS_OK(nt_status
))
1907 got_src_driver_share
= true;
1910 /* open print$-share on the dst server */
1911 nt_status
= connect_to_service(c
, &cli_share_dst
,
1912 cli_state_remote_sockaddr(cli_dst
),
1913 cli_state_remote_name(cli_dst
),
1915 if (!NT_STATUS_IS_OK(nt_status
))
1918 got_dst_driver_share
= true;
1921 /* enum src printers */
1922 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1923 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1927 if (num_printers
== 0) {
1928 printf (_("no printers found on server.\n"));
1929 nt_status
= NT_STATUS_OK
;
1934 /* do something for all printers */
1935 for (p
= 0; p
< num_printers
; p
++) {
1937 /* do some initialization */
1938 printername
= info_enum
[p
].info2
.printername
;
1939 sharename
= info_enum
[p
].info2
.sharename
;
1941 if (!printername
|| !sharename
) {
1942 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1946 /* we can reset NT_STATUS here because we do not
1947 get any real NT_STATUS-codes anymore from now on */
1948 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1950 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1951 printername
, sharename
);
1953 /* open dst printer handle */
1954 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1955 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd_dst
))
1958 /* check for existing dst printer */
1959 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
1963 /* open src printer handle */
1964 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1965 MAXIMUM_ALLOWED_ACCESS
,
1966 pipe_hnd
->auth
->user_name
,
1970 /* in a first step call getdriver for each shared printer (per arch)
1971 to get a list of all files that have to be copied */
1973 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
1976 if (!net_spoolss_getprinterdriver(pipe_hnd
, mem_ctx
, &hnd_src
,
1977 level
, archi_table
[i
].long_archi
,
1978 archi_table
[i
].version
, &drv_info_src
))
1981 drivername
= drv_info_src
.info3
.driver_name
;
1984 display_print_driver3(&drv_info_src
.info3
);
1986 /* check arch dir */
1987 nt_status
= check_arch_dir(cli_share_dst
, archi_table
[i
].short_archi
);
1988 if (!NT_STATUS_IS_OK(nt_status
))
1992 /* copy driver-files */
1993 nt_status
= copy_print_driver_3(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
1994 archi_table
[i
].short_archi
,
1995 &drv_info_src
.info3
);
1996 if (!NT_STATUS_IS_OK(nt_status
))
2001 if (!net_spoolss_addprinterdriver(pipe_hnd_dst
, mem_ctx
, level
, &drv_info_src
)) {
2002 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2006 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
2007 drivername
, printername
));
2011 if (!drivername
|| strlen(drivername
) == 0) {
2012 DEBUGADD(1,("Did not get driver for printer %s\n",
2018 info_dst
.info2
.drivername
= drivername
;
2020 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
)) {
2021 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2025 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
2026 drivername
, printername
));
2029 if (is_valid_policy_hnd(&hnd_dst
)) {
2030 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2034 if (is_valid_policy_hnd(&hnd_src
)) {
2035 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2039 nt_status
= NT_STATUS_OK
;
2043 if (is_valid_policy_hnd(&hnd_dst
)) {
2044 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2048 if (is_valid_policy_hnd(&hnd_src
)) {
2049 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2053 cli_shutdown(cli_dst
);
2056 if (got_src_driver_share
)
2057 cli_shutdown(cli_share_src
);
2059 if (got_dst_driver_share
)
2060 cli_shutdown(cli_share_dst
);
2067 * Migrate printer-queues from a src to the dst server
2068 * (requires a working "addprinter command" to be installed for the local smbd)
2070 * All parameters are provided by the run_rpc_command function, except for
2071 * argc, argv which are passed through.
2073 * @param c A net_context structure
2074 * @param domain_sid The domain sid aquired from the remote server
2075 * @param cli A cli_state connected to the server.
2076 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2077 * @param argc Standard main() style argc
2078 * @param argv Standard main() style argv. Initial components are already
2081 * @return Normal NTSTATUS return.
2084 NTSTATUS
rpc_printer_migrate_printers_internals(struct net_context
*c
,
2085 const struct dom_sid
*domain_sid
,
2086 const char *domain_name
,
2087 struct cli_state
*cli
,
2088 struct rpc_pipe_client
*pipe_hnd
,
2089 TALLOC_CTX
*mem_ctx
,
2093 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2095 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2096 uint32_t i
= 0, num_printers
;
2098 union spoolss_PrinterInfo info_dst
, info_src
;
2099 union spoolss_PrinterInfo
*info_enum
;
2100 struct cli_state
*cli_dst
= NULL
;
2101 struct policy_handle hnd_dst
, hnd_src
;
2102 const char *printername
, *sharename
;
2103 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2104 struct dcerpc_binding_handle
*b_dst
= NULL
;
2105 struct spoolss_SetPrinterInfoCtr info_ctr
;
2107 DEBUG(3,("copying printers\n"));
2109 /* connect destination PI_SPOOLSS */
2110 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2111 &ndr_table_spoolss
.syntax_id
);
2112 if (!NT_STATUS_IS_OK(nt_status
)) {
2115 b_dst
= pipe_hnd_dst
->binding_handle
;
2118 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2119 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2123 if (!num_printers
) {
2124 printf (_("no printers found on server.\n"));
2125 nt_status
= NT_STATUS_OK
;
2129 /* do something for all printers */
2130 for (i
= 0; i
< num_printers
; i
++) {
2132 struct spoolss_SetPrinterInfo2 info2
;
2134 /* do some initialization */
2135 printername
= info_enum
[i
].info2
.printername
;
2136 sharename
= info_enum
[i
].info2
.sharename
;
2138 if (!printername
|| !sharename
) {
2139 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2142 /* we can reset NT_STATUS here because we do not
2143 get any real NT_STATUS-codes anymore from now on */
2144 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2146 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2147 printername
, sharename
);
2149 /* open dst printer handle */
2150 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2151 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd_dst
)) {
2153 DEBUG(1,("could not open printer: %s\n", sharename
));
2156 /* check for existing dst printer */
2157 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
)) {
2158 printf (_("could not get printer, creating printer.\n"));
2160 DEBUG(1,("printer already exists: %s\n", sharename
));
2161 /* close printer handle here - dst only, not got src yet. */
2162 if (is_valid_policy_hnd(&hnd_dst
)) {
2163 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2168 /* now get again src printer ctr via getprinter,
2169 we first need a handle for that */
2171 /* open src printer handle */
2172 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2173 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
2176 /* getprinter on the src server */
2177 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &info_src
))
2180 /* copy each src printer to a dst printer 1:1,
2181 maybe some values have to be changed though */
2182 d_printf(_("creating printer: %s\n"), printername
);
2184 info_ctr
.level
= level
;
2185 spoolss_printerinfo2_to_setprinterinfo2(&info_src
.info2
, &info2
);
2186 info_ctr
.info
.info2
= &info2
;
2188 result
= rpccli_spoolss_addprinterex(pipe_hnd_dst
,
2192 if (W_ERROR_IS_OK(result
))
2193 d_printf (_("printer [%s] successfully added.\n"),
2195 else if (W_ERROR_V(result
) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS
))
2196 d_fprintf (stderr
, _("printer [%s] already exists.\n"),
2199 d_fprintf (stderr
, _("could not create printer [%s]\n"),
2204 /* close printer handles here */
2205 if (is_valid_policy_hnd(&hnd_src
)) {
2206 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2209 if (is_valid_policy_hnd(&hnd_dst
)) {
2210 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2214 nt_status
= NT_STATUS_OK
;
2217 if (is_valid_policy_hnd(&hnd_src
)) {
2218 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2221 if (is_valid_policy_hnd(&hnd_dst
)) {
2222 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2226 cli_shutdown(cli_dst
);
2232 * Migrate Printer-Settings from a src server to the dst server
2233 * (for this to work, printers and drivers already have to be migrated earlier)
2235 * All parameters are provided by the run_rpc_command function, except for
2236 * argc, argv which are passed through.
2238 * @param c A net_context structure
2239 * @param domain_sid The domain sid aquired from the remote server
2240 * @param cli A cli_state connected to the server.
2241 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2242 * @param argc Standard main() style argc
2243 * @param argv Standard main() style argv. Initial components are already
2246 * @return Normal NTSTATUS return.
2249 NTSTATUS
rpc_printer_migrate_settings_internals(struct net_context
*c
,
2250 const struct dom_sid
*domain_sid
,
2251 const char *domain_name
,
2252 struct cli_state
*cli
,
2253 struct rpc_pipe_client
*pipe_hnd
,
2254 TALLOC_CTX
*mem_ctx
,
2258 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2260 /* FIXME: Here the nightmare begins */
2263 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2264 uint32_t i
= 0, j
= 0;
2265 uint32_t num_printers
;
2267 const char *printername
, *sharename
;
2268 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2269 struct dcerpc_binding_handle
*b_dst
= NULL
;
2270 struct policy_handle hnd_src
, hnd_dst
;
2271 union spoolss_PrinterInfo
*info_enum
;
2272 union spoolss_PrinterInfo info_dst_publish
;
2273 union spoolss_PrinterInfo info_dst
;
2274 struct cli_state
*cli_dst
= NULL
;
2275 char *devicename
= NULL
, *unc_name
= NULL
, *url
= NULL
;
2276 const char *longname
;
2277 const char **keylist
= NULL
;
2280 ZERO_STRUCT(info_dst_publish
);
2282 DEBUG(3,("copying printer settings\n"));
2284 /* connect destination PI_SPOOLSS */
2285 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2286 &ndr_table_spoolss
.syntax_id
);
2287 if (!NT_STATUS_IS_OK(nt_status
)) {
2290 b_dst
= pipe_hnd_dst
->binding_handle
;
2292 /* enum src printers */
2293 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2294 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2298 if (!num_printers
) {
2299 printf (_("no printers found on server.\n"));
2300 nt_status
= NT_STATUS_OK
;
2305 /* needed for dns-strings in regkeys */
2306 longname
= get_mydnsfullname();
2308 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2312 /* do something for all printers */
2313 for (i
= 0; i
< num_printers
; i
++) {
2315 uint32_t value_needed
;
2316 uint32_t data_needed
;
2317 enum winreg_Type type
;
2318 struct spoolss_EnumPrinterData r
;
2320 /* do some initialization */
2321 printername
= info_enum
[i
].info2
.printername
;
2322 sharename
= info_enum
[i
].info2
.sharename
;
2324 if (!printername
|| !sharename
) {
2325 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2328 /* we can reset NT_STATUS here because we do not
2329 get any real NT_STATUS-codes anymore from now on */
2330 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2332 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2333 printername
, sharename
);
2336 /* open src printer handle */
2337 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2338 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
2341 /* open dst printer handle */
2342 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2343 PRINTER_ALL_ACCESS
, cli_dst
->user_name
, &hnd_dst
))
2346 /* check for existing dst printer */
2347 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2352 /* STEP 1: COPY DEVICE-MODE and other
2353 PRINTER_INFO_2-attributes
2356 info_dst
.info2
= info_enum
[i
].info2
;
2358 /* why is the port always disconnected when the printer
2359 is correctly installed (incl. driver ???) */
2360 info_dst
.info2
.portname
= SAMBA_PRINTER_PORT_NAME
;
2362 /* check if printer is published */
2363 if (info_enum
[i
].info2
.attributes
& PRINTER_ATTRIBUTE_PUBLISHED
) {
2365 /* check for existing dst printer */
2366 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
))
2369 info_dst_publish
.info7
.action
= DSPRINT_PUBLISH
;
2371 /* ignore false from setprinter due to WERR_IO_PENDING */
2372 net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
);
2374 DEBUG(3,("republished printer\n"));
2377 if (info_enum
[i
].info2
.devmode
!= NULL
) {
2379 /* copy devmode (info level 2) */
2380 info_dst
.info2
.devmode
= info_enum
[i
].info2
.devmode
;
2382 /* do not copy security descriptor (we have another
2383 * command for that) */
2384 info_dst
.info2
.secdesc
= NULL
;
2387 info_dst
.info2
.devmode
.devicename
=
2388 talloc_asprintf(mem_ctx
, "\\\\%s\\%s",
2389 longname
, printername
);
2390 if (!info_dst
.info2
.devmode
.devicename
) {
2391 nt_status
= NT_STATUS_NO_MEMORY
;
2395 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2399 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2402 /* STEP 2: COPY REGISTRY VALUES */
2404 /* please keep in mind that samba parse_spools gives horribly
2405 crippled results when used to rpccli_spoolss_enumprinterdataex
2406 a win2k3-server. (Bugzilla #1851)
2407 FIXME: IIRC I've seen it too on a win2k-server
2410 r
.in
.handle
= &hnd_src
;
2411 r
.in
.enum_index
= 0;
2412 r
.in
.value_offered
= 0;
2413 r
.in
.data_offered
= 0;
2414 r
.out
.value_name
= NULL
;
2415 r
.out
.value_needed
= &value_needed
;
2418 r
.out
.data_needed
= &data_needed
;
2420 /* enumerate data on src handle */
2421 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2423 r
.in
.data_offered
= *r
.out
.data_needed
;
2424 r
.in
.value_offered
= *r
.out
.value_needed
;
2425 r
.out
.data
= talloc_zero_array(mem_ctx
, uint8_t, r
.in
.data_offered
);
2426 r
.out
.value_name
= talloc_zero_array(mem_ctx
, char, r
.in
.value_offered
);
2428 /* loop for all printerdata of "PrinterDriverData" */
2429 while (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2433 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2435 /* loop for all reg_keys */
2436 if (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2439 if (c
->opt_verbose
) {
2440 struct regval_blob
*v
;
2442 v
= regval_compose(talloc_tos(),
2448 nt_status
= NT_STATUS_NO_MEMORY
;
2452 display_reg_value(SPOOL_PRINTERDATA_KEY
, v
);
2457 if (!net_spoolss_setprinterdata(pipe_hnd_dst
, mem_ctx
,
2458 &hnd_dst
, r
.out
.value_name
,
2459 *r
.out
.type
, r
.out
.data
, r
.in
.data_offered
))
2462 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2467 /* STEP 3: COPY SUBKEY VALUES */
2469 /* here we need to enum all printer_keys and then work
2470 on the result with enum_printer_key_ex. nt4 does not
2471 respond to enumprinterkey, win2k does, so continue
2472 in case of an error */
2474 if (!net_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, &hnd_src
, "", &keylist
)) {
2475 printf(_("got no key-data\n"));
2480 /* work on a list of printer keys
2481 each key has to be enumerated to get all required
2482 information. information is then set via setprinterdataex-calls */
2484 if (keylist
== NULL
)
2487 for (i
=0; keylist
&& keylist
[i
] != NULL
; i
++) {
2489 const char *subkey
= keylist
[i
];
2491 struct spoolss_PrinterEnumValues
*info
;
2493 /* enumerate all src subkeys */
2494 if (!net_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
, 0,
2500 for (j
=0; j
< count
; j
++) {
2502 struct regval_blob
*value
;
2507 /* although samba replies with sane data in most cases we
2508 should try to avoid writing wrong registry data */
2510 if (strequal(info
[j
].value_name
, SPOOL_REG_PORTNAME
) ||
2511 strequal(info
[j
].value_name
, SPOOL_REG_UNCNAME
) ||
2512 strequal(info
[j
].value_name
, SPOOL_REG_URL
) ||
2513 strequal(info
[j
].value_name
, SPOOL_REG_SHORTSERVERNAME
) ||
2514 strequal(info
[j
].value_name
, SPOOL_REG_SERVERNAME
)) {
2516 if (strequal(info
[j
].value_name
, SPOOL_REG_PORTNAME
)) {
2518 /* although windows uses a multi-sz, we use a sz */
2519 push_reg_sz(mem_ctx
, &blob
, SAMBA_PRINTER_PORT_NAME
);
2522 if (strequal(info
[j
].value_name
, SPOOL_REG_UNCNAME
)) {
2524 if (asprintf(&unc_name
, "\\\\%s\\%s", longname
, sharename
) < 0) {
2525 nt_status
= NT_STATUS_NO_MEMORY
;
2528 push_reg_sz(mem_ctx
, &blob
, unc_name
);
2531 if (strequal(info
[j
].value_name
, SPOOL_REG_URL
)) {
2536 /* FIXME: should we really do that ??? */
2537 if (asprintf(&url
, "http://%s:631/printers/%s", longname
, sharename
) < 0) {
2538 nt_status
= NT_STATUS_NO_MEMORY
;
2541 push_reg_sz(mem_ctx
, NULL
, &blob
, url
);
2542 fstrcpy(value
.valuename
, SPOOL_REG_URL
);
2546 if (strequal(info
[j
].value_name
, SPOOL_REG_SERVERNAME
)) {
2548 push_reg_sz(mem_ctx
, &blob
, longname
);
2551 if (strequal(info
[j
].value_name
, SPOOL_REG_SHORTSERVERNAME
)) {
2553 push_reg_sz(mem_ctx
, &blob
, lp_netbios_name());
2556 value
= regval_compose(talloc_tos(),
2559 blob
.length
== 0 ? NULL
: blob
.data
,
2561 if (value
== NULL
) {
2562 nt_status
= NT_STATUS_NO_MEMORY
;
2567 display_reg_value(subkey
, value
);
2569 /* here we have to set all subkeys on the dst server */
2570 if (!net_spoolss_setprinterdataex(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2580 struct regval_blob
*v
;
2582 v
= regval_compose(talloc_tos(),
2586 info
[j
].data
->length
);
2588 nt_status
= NT_STATUS_NO_MEMORY
;
2592 if (c
->opt_verbose
) {
2593 display_reg_value(subkey
, v
);
2596 /* here we have to set all subkeys on the dst server */
2597 if (!net_spoolss_setprinterdataex(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2605 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2606 subkey
, info
[j
].value_name
));
2611 TALLOC_FREE(keylist
);
2613 /* close printer handles here */
2614 if (is_valid_policy_hnd(&hnd_src
)) {
2615 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2618 if (is_valid_policy_hnd(&hnd_dst
)) {
2619 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2623 nt_status
= NT_STATUS_OK
;
2626 SAFE_FREE(devicename
);
2628 SAFE_FREE(unc_name
);
2630 if (is_valid_policy_hnd(&hnd_src
)) {
2631 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2634 if (is_valid_policy_hnd(&hnd_dst
)) {
2635 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2639 cli_shutdown(cli_dst
);