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"
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
, const char *name
, struct registry_value
*value
)
89 if (value
->data
.length
== sizeof(uint32
)) {
90 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey
,
91 name
, IVAL(value
->data
.data
,0));
93 d_printf(_("\t[%s:%s]: REG_DWORD: <invalid>\n"), subkey
,
99 pull_reg_sz(talloc_tos(), &value
->data
, &text
);
103 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey
, name
, text
);
107 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
116 if (!pull_reg_multi_sz(NULL
, &value
->data
, &values
)) {
117 d_printf("pull_reg_multi_sz failed\n");
121 printf("%s: REG_MULTI_SZ: \n", name
);
122 for (i
=0; values
[i
] != NULL
; i
++) {
123 d_printf("%s\n", values
[i
]);
130 d_printf(_("\t%s: unknown type %d\n"), name
, value
->type
);
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 nt_status
= cli_query_secdesc(cli_share_src
, fnum_src
,
190 if (!NT_STATUS_IS_OK(nt_status
)) {
191 DEBUG(0,("failed to get security descriptor: %s\n",
192 nt_errstr(nt_status
)));
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)") : "" );
386 nt_status
= cli_read(cli_share_src
, fnum_src
, data
, nread
,
388 if (!NT_STATUS_IS_OK(nt_status
)) {
390 _("Error reading file [\\\\%s\\%s%s]: %s\n"),
391 cli_state_remote_name(cli_share_src
),
392 cli_share_src
->share
,
393 src_name
, nt_errstr(nt_status
));
400 nt_status
= cli_writeall(cli_share_dst
, fnum_dst
, 0,
401 (uint8_t *)data
, nread
, n
, NULL
);
403 if (!NT_STATUS_IS_OK(nt_status
)) {
405 _("Error writing file: [\\\\%s\\%s%s]: %s\n"),
406 cli_state_remote_name(cli_share_dst
),
407 cli_share_dst
->share
,
408 dst_name
, nt_errstr(nt_status
));
416 if (!is_file
&& !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst
, dst_name
))) {
419 DEBUGADD(3,("creating dir %s on the destination server\n",
422 nt_status
= cli_mkdir(cli_share_dst
, dst_name
);
423 if (!NT_STATUS_IS_OK(nt_status
)) {
424 DEBUG(0,("cannot create directory %s: %s\n",
425 dst_name
, nt_errstr(nt_status
)));
426 nt_status
= NT_STATUS_NO_SUCH_FILE
;
430 nt_status
= cli_chkpath(cli_share_dst
, dst_name
);
431 if (!NT_STATUS_IS_OK(nt_status
)) {
433 _("cannot check for directory %s: %s\n"),
434 dst_name
, nt_errstr(nt_status
));
441 nt_status
= cli_close(cli_share_src
, fnum_src
);
442 if (!NT_STATUS_IS_OK(nt_status
)) {
444 _("could not close file on originating server: %s\n"),
445 nt_errstr(nt_status
));
450 nt_status
= cli_close(cli_share_dst
, fnum_dst
);
451 if (!NT_STATUS_IS_OK(nt_status
)) {
453 _("could not close file on destination server: %s\n"),
454 nt_errstr(nt_status
));
459 /* possibly we have to copy some file-attributes / acls / sd */
460 nt_status
= net_copy_fileattr(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
461 src_name
, dst_name
, copy_acls
,
462 copy_attrs
, copy_timestamps
, is_file
);
463 if (!NT_STATUS_IS_OK(nt_status
))
467 nt_status
= NT_STATUS_OK
;
473 cli_close(cli_share_src
, fnum_src
);
476 cli_close(cli_share_dst
, fnum_dst
);
484 * Copy a driverfile from on connected share to another connected share
485 * This silently assumes that a driver-file is picked up from
487 * \\src_server\print$\{arch}\{version}\file
491 * \\dst_server\print$\{arch}\file
493 * to be added via setdriver-calls later.
494 * @param c A net_context structure
495 * @param mem_ctx A talloc-context
496 * @param cli_share_src A cli_state connected to source print$-share
497 * @param cli_share_dst A cli_state connected to destination print$-share
498 * @param file The file-name to be copied
499 * @param short_archi The name of the driver-architecture (short form)
501 * @return Normal NTSTATUS return.
504 static NTSTATUS
net_copy_driverfile(struct net_context
*c
,
506 struct cli_state
*cli_share_src
,
507 struct cli_state
*cli_share_dst
,
508 const char *file
, const char *short_archi
) {
513 char *version
= NULL
;
514 char *filename
= NULL
;
521 /* scroll through the file until we have the part
522 beyond archi_table.short_archi */
524 while (next_token_talloc(mem_ctx
, &p
, &tok
, "\\")) {
525 if (strequal(tok
, short_archi
)) {
526 next_token_talloc(mem_ctx
, &p
, &version
, "\\");
527 next_token_talloc(mem_ctx
, &p
, &filename
, "\\");
531 if (version
== NULL
|| filename
== NULL
) {
532 return NT_STATUS_UNSUCCESSFUL
;
535 /* build source file name */
536 src_name
= talloc_asprintf(mem_ctx
, "\\%s\\%s\\%s",
537 short_archi
, version
, filename
);
538 if (src_name
== NULL
) {
539 return NT_STATUS_NO_MEMORY
;
542 /* create destination file name */
543 dst_name
= talloc_asprintf(mem_ctx
, "\\%s\\%s", short_archi
, filename
);
544 if (dst_name
== NULL
) {
545 return NT_STATUS_NO_MEMORY
;
549 /* finally copy the file */
550 return net_copy_file(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
551 src_name
, dst_name
, false, false, false, true);
555 * Check for existing Architecture directory on a given server
557 * @param cli_share A cli_state connected to a print$-share
558 * @param short_archi The Architecture for the print-driver
560 * @return Normal NTSTATUS return.
563 static NTSTATUS
check_arch_dir(struct cli_state
*cli_share
, const char *short_archi
)
566 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
569 if (asprintf(&dir
, "\\%s", short_archi
) < 0) {
570 return NT_STATUS_NO_MEMORY
;
573 DEBUG(10,("creating print-driver dir for architecture: %s\n",
576 nt_status
= cli_mkdir(cli_share
, dir
);
577 if (!NT_STATUS_IS_OK(nt_status
)) {
578 DEBUG(1,("cannot create directory %s: %s\n",
579 dir
, nt_errstr(nt_status
)));
582 nt_status
= cli_chkpath(cli_share
, dir
);
583 if (!NT_STATUS_IS_OK(nt_status
)) {
584 d_fprintf(stderr
, _("cannot check %s: %s\n"),
585 dir
, nt_errstr(nt_status
));
589 nt_status
= NT_STATUS_OK
;
597 * Copy a print-driver (level 3) from one connected print$-share to another
598 * connected print$-share
600 * @param c A net_context structure
601 * @param mem_ctx A talloc-context
602 * @param cli_share_src A cli_state connected to a print$-share
603 * @param cli_share_dst A cli_state connected to a print$-share
604 * @param short_archi The Architecture for the print-driver
605 * @param i1 The DRIVER_INFO_3-struct
607 * @return Normal NTSTATUS return.
610 static NTSTATUS
copy_print_driver_3(struct net_context
*c
,
612 struct cli_state
*cli_share_src
,
613 struct cli_state
*cli_share_dst
,
614 const char *short_archi
,
615 struct spoolss_DriverInfo3
*r
)
617 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
625 d_printf(_("copying driver: [%s], for architecture: [%s], "
627 r
->driver_name
, short_archi
, r
->version
);
629 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
630 r
->driver_path
, short_archi
);
631 if (!NT_STATUS_IS_OK(nt_status
))
634 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
635 r
->data_file
, short_archi
);
636 if (!NT_STATUS_IS_OK(nt_status
))
639 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
640 r
->config_file
, short_archi
);
641 if (!NT_STATUS_IS_OK(nt_status
))
644 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
645 r
->help_file
, short_archi
);
646 if (!NT_STATUS_IS_OK(nt_status
))
649 for (i
=0; r
->dependent_files
[i
] != NULL
; i
++) {
651 nt_status
= net_copy_driverfile(c
, mem_ctx
,
652 cli_share_src
, cli_share_dst
,
653 r
->dependent_files
[i
], short_archi
);
654 if (!NT_STATUS_IS_OK(nt_status
)) {
663 * net_spoolss-functions
664 * =====================
666 * the net_spoolss-functions aim to simplify spoolss-client-functions
667 * required during the migration-process wrt buffer-sizes, returned
670 * this greatly reduces the complexitiy of the migrate-functions.
674 static bool net_spoolss_enum_printers(struct rpc_pipe_client
*pipe_hnd
,
679 uint32_t *num_printers
,
680 union spoolss_PrinterInfo
**info
)
686 result
= rpccli_spoolss_enumprinters(pipe_hnd
, mem_ctx
,
693 if (!W_ERROR_IS_OK(result
)) {
694 printf(_("cannot enum printers: %s\n"), win_errstr(result
));
701 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client
*pipe_hnd
,
703 const char *printername
,
704 uint32_t access_required
,
705 const char *username
,
706 struct policy_handle
*hnd
)
709 fstring printername2
;
711 fstrcpy(printername2
, pipe_hnd
->srv_name_slash
);
712 fstrcat(printername2
, "\\");
713 fstrcat(printername2
, printername
);
715 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
716 pipe_hnd
->srv_name_slash
, username
, printername2
, access_required
));
719 result
= rpccli_spoolss_openprinter_ex(pipe_hnd
, mem_ctx
,
724 /* be more verbose */
725 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
727 _("no access to printer [%s] on [%s] for user [%s] "
729 printername2
, pipe_hnd
->srv_name_slash
, username
);
733 if (!W_ERROR_IS_OK(result
)) {
734 d_fprintf(stderr
,_("cannot open printer %s on server %s: %s\n"),
735 printername2
, pipe_hnd
->srv_name_slash
, win_errstr(result
));
739 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
740 printername2
, pipe_hnd
->srv_name_slash
));
745 static bool net_spoolss_getprinter(struct rpc_pipe_client
*pipe_hnd
,
747 struct policy_handle
*hnd
,
749 union spoolss_PrinterInfo
*info
)
753 /* getprinter call */
754 result
= rpccli_spoolss_getprinter(pipe_hnd
, mem_ctx
,
759 if (!W_ERROR_IS_OK(result
)) {
760 printf(_("cannot get printer-info: %s\n"), win_errstr(result
));
767 static bool net_spoolss_setprinter(struct rpc_pipe_client
*pipe_hnd
,
769 struct policy_handle
*hnd
,
771 union spoolss_PrinterInfo
*info
)
773 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
776 struct spoolss_SetPrinterInfoCtr info_ctr
;
777 struct spoolss_SetPrinterInfo2 info2
;
778 struct spoolss_DevmodeContainer devmode_ctr
;
779 struct sec_desc_buf secdesc_ctr
;
781 ZERO_STRUCT(devmode_ctr
);
782 ZERO_STRUCT(secdesc_ctr
);
784 /* setprinter call */
786 info_ctr
.level
= level
;
789 info_ctr
.info
.info0
= (struct spoolss_SetPrinterInfo0
*)
790 (void *)&info
->info0
;
793 info_ctr
.info
.info1
= (struct spoolss_SetPrinterInfo1
*)
794 (void *)&info
->info1
;
797 spoolss_printerinfo2_to_setprinterinfo2(&info
->info2
, &info2
);
798 info_ctr
.info
.info2
= &info2
;
801 info_ctr
.info
.info3
= (struct spoolss_SetPrinterInfo3
*)
802 (void *)&info
->info3
;
805 info_ctr
.info
.info4
= (struct spoolss_SetPrinterInfo4
*)
806 (void *)&info
->info4
;
809 info_ctr
.info
.info5
= (struct spoolss_SetPrinterInfo5
*)
810 (void *)&info
->info5
;
813 info_ctr
.info
.info6
= (struct spoolss_SetPrinterInfo6
*)
814 (void *)&info
->info6
;
817 info_ctr
.info
.info7
= (struct spoolss_SetPrinterInfo7
*)
818 (void *)&info
->info7
;
822 info_ctr
.info
.info8
= (struct spoolss_SetPrinterInfo8
*)
823 (void *)&info
->info8
;
826 info_ctr
.info
.info9
= (struct spoolss_SetPrinterInfo9
*)
827 (void *)&info
->info9
;
834 status
= dcerpc_spoolss_SetPrinter(b
, mem_ctx
,
841 if (!NT_STATUS_IS_OK(status
)) {
842 printf(_("cannot set printer-info: %s\n"), nt_errstr(status
));
845 if (!W_ERROR_IS_OK(result
)) {
846 printf(_("cannot set printer-info: %s\n"), win_errstr(result
));
854 static bool net_spoolss_setprinterdata(struct rpc_pipe_client
*pipe_hnd
,
856 struct policy_handle
*hnd
,
857 const char *value_name
,
858 enum winreg_Type type
,
862 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
866 /* setprinterdata call */
867 status
= dcerpc_spoolss_SetPrinterData(b
, mem_ctx
,
874 if (!NT_STATUS_IS_OK(status
)) {
875 printf (_("unable to set printerdata: %s\n"),
879 if (!W_ERROR_IS_OK(result
)) {
880 printf (_("unable to set printerdata: %s\n"),
889 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client
*pipe_hnd
,
891 struct policy_handle
*hnd
,
893 const char ***keylist
)
897 /* enumprinterkey call */
898 result
= rpccli_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, hnd
, keyname
, keylist
, 0);
900 if (!W_ERROR_IS_OK(result
)) {
901 printf(_("enumprinterkey failed: %s\n"), win_errstr(result
));
908 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
911 struct policy_handle
*hnd
,
914 struct spoolss_PrinterEnumValues
**info
)
918 /* enumprinterdataex call */
919 result
= rpccli_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
,
926 if (!W_ERROR_IS_OK(result
)) {
927 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result
));
935 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
937 struct policy_handle
*hnd
,
940 struct registry_value
*value
)
942 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
946 /* setprinterdataex call */
947 status
= dcerpc_spoolss_SetPrinterDataEx(b
, mem_ctx
,
955 if (!NT_STATUS_IS_OK(status
)) {
956 printf(_("could not set printerdataex: %s\n"),
960 if (!W_ERROR_IS_OK(result
)) {
961 printf(_("could not set printerdataex: %s\n"),
969 static bool net_spoolss_enumforms(struct rpc_pipe_client
*pipe_hnd
,
971 struct policy_handle
*hnd
,
974 union spoolss_FormInfo
**forms
)
979 result
= rpccli_spoolss_enumforms(pipe_hnd
, mem_ctx
,
985 if (!W_ERROR_IS_OK(result
)) {
986 printf(_("could not enum forms: %s\n"), win_errstr(result
));
993 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client
*pipe_hnd
,
995 uint32_t level
, const char *env
,
997 union spoolss_DriverInfo
**info
)
1001 /* enumprinterdrivers call */
1002 result
= rpccli_spoolss_enumprinterdrivers(pipe_hnd
, mem_ctx
,
1003 pipe_hnd
->srv_name_slash
,
1009 if (!W_ERROR_IS_OK(result
)) {
1010 if (W_ERROR_V(result
) != W_ERROR_V(WERR_INVALID_ENVIRONMENT
)) {
1011 printf(_("cannot enum drivers for environment %s: %s\n"), env
,
1012 win_errstr(result
));
1015 printf(_("Server does not support environment [%s]\n"),
1023 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1024 TALLOC_CTX
*mem_ctx
,
1025 struct policy_handle
*hnd
, uint32_t level
,
1026 const char *env
, int version
,
1027 union spoolss_DriverInfo
*info
)
1030 uint32_t server_major_version
;
1031 uint32_t server_minor_version
;
1033 /* getprinterdriver call */
1034 result
= rpccli_spoolss_getprinterdriver2(pipe_hnd
, mem_ctx
,
1042 &server_major_version
,
1043 &server_minor_version
);
1044 if (!W_ERROR_IS_OK(result
)) {
1045 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1046 env
, win_errstr(result
)));
1047 if (W_ERROR_V(result
) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER
) &&
1048 W_ERROR_V(result
) != W_ERROR_V(WERR_INVALID_ENVIRONMENT
)) {
1049 printf(_("cannot get driver: %s\n"),
1050 win_errstr(result
));
1059 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1060 TALLOC_CTX
*mem_ctx
, uint32_t level
,
1061 union spoolss_DriverInfo
*info
)
1063 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1066 struct spoolss_AddDriverInfoCtr info_ctr
;
1068 info_ctr
.level
= level
;
1072 info_ctr
.info
.info2
= (struct spoolss_AddDriverInfo2
*)
1073 (void *)&info
->info2
;
1076 info_ctr
.info
.info3
= (struct spoolss_AddDriverInfo3
*)
1077 (void *)&info
->info3
;
1080 printf(_("unsupported info level: %d\n"), level
);
1084 /* addprinterdriver call */
1085 status
= dcerpc_spoolss_AddPrinterDriver(b
, mem_ctx
,
1086 pipe_hnd
->srv_name_slash
,
1089 if (!NT_STATUS_IS_OK(status
)) {
1090 printf(_("cannot add driver: %s\n"), nt_errstr(status
));
1093 /* be more verbose */
1094 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
1095 printf(_("You are not allowed to add drivers\n"));
1098 if (!W_ERROR_IS_OK(result
)) {
1099 printf(_("cannot add driver: %s\n"), win_errstr(result
));
1107 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1108 * for a single printer or for all printers depending on argc/argv
1111 static bool get_printer_info(struct rpc_pipe_client
*pipe_hnd
,
1112 TALLOC_CTX
*mem_ctx
,
1116 uint32_t *num_printers
,
1117 union spoolss_PrinterInfo
**info_p
)
1119 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1120 struct policy_handle hnd
;
1123 /* no arguments given, enumerate all printers */
1126 if (!net_spoolss_enum_printers(pipe_hnd
, mem_ctx
, NULL
,
1127 PRINTER_ENUM_LOCAL
|PRINTER_ENUM_SHARED
,
1128 level
, num_printers
, info_p
))
1134 /* argument given, get a single printer by name */
1135 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, argv
[0],
1136 MAXIMUM_ALLOWED_ACCESS
,
1137 pipe_hnd
->auth
->user_name
,
1141 *info_p
= talloc_zero(mem_ctx
, union spoolss_PrinterInfo
);
1142 if (*info_p
== NULL
) {
1146 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, *info_p
)) {
1147 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1151 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1156 DEBUG(3,("got %d printers\n", *num_printers
));
1163 * List print-queues (including local printers that are not shared)
1165 * All parameters are provided by the run_rpc_command function, except for
1166 * argc, argv which are passed through.
1168 * @param c A net_context structure
1169 * @param domain_sid The domain sid aquired from the remote server
1170 * @param cli A cli_state connected to the server.
1171 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1172 * @param argc Standard main() style argc
1173 * @param argv Standard main() style argv. Initial components are already
1176 * @return Normal NTSTATUS return.
1179 NTSTATUS
rpc_printer_list_internals(struct net_context
*c
,
1180 const struct dom_sid
*domain_sid
,
1181 const char *domain_name
,
1182 struct cli_state
*cli
,
1183 struct rpc_pipe_client
*pipe_hnd
,
1184 TALLOC_CTX
*mem_ctx
,
1188 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1189 uint32_t i
, num_printers
;
1191 const char *printername
, *sharename
;
1192 union spoolss_PrinterInfo
*info
;
1194 printf("listing printers\n");
1196 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info
))
1199 for (i
= 0; i
< num_printers
; i
++) {
1201 /* do some initialization */
1202 printername
= info
[i
].info2
.printername
;
1203 sharename
= info
[i
].info2
.sharename
;
1205 if (printername
&& sharename
) {
1206 d_printf(_("printer %d: %s, shared as: %s\n"),
1207 i
+1, printername
, sharename
);
1211 return NT_STATUS_OK
;
1215 * List printer-drivers from a server
1217 * All parameters are provided by the run_rpc_command function, except for
1218 * argc, argv which are passed through.
1220 * @param c A net_context structure
1221 * @param domain_sid The domain sid aquired from the remote server
1222 * @param cli A cli_state connected to the server.
1223 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1224 * @param argc Standard main() style argc
1225 * @param argv Standard main() style argv. Initial components are already
1228 * @return Normal NTSTATUS return.
1231 NTSTATUS
rpc_printer_driver_list_internals(struct net_context
*c
,
1232 const struct dom_sid
*domain_sid
,
1233 const char *domain_name
,
1234 struct cli_state
*cli
,
1235 struct rpc_pipe_client
*pipe_hnd
,
1236 TALLOC_CTX
*mem_ctx
,
1240 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1243 union spoolss_DriverInfo
*info
;
1246 printf(_("listing printer-drivers\n"));
1248 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
1250 uint32_t num_drivers
;
1252 /* enum remote drivers */
1253 if (!net_spoolss_enumprinterdrivers(pipe_hnd
, mem_ctx
, level
,
1254 archi_table
[i
].long_archi
,
1255 &num_drivers
, &info
)) {
1256 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1260 if (num_drivers
== 0) {
1261 d_printf(_("no drivers found on server for "
1262 "architecture: [%s].\n"),
1263 archi_table
[i
].long_archi
);
1267 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1268 num_drivers
, archi_table
[i
].long_archi
);
1271 /* do something for all drivers for architecture */
1272 for (d
= 0; d
< num_drivers
; d
++) {
1273 display_print_driver3(&info
[d
].info3
);
1277 nt_status
= NT_STATUS_OK
;
1285 * Publish print-queues with args-wrapper
1287 * @param cli A cli_state connected to the server.
1288 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1289 * @param argc Standard main() style argc
1290 * @param argv Standard main() style argv. Initial components are already
1294 * @return Normal NTSTATUS return.
1297 static NTSTATUS
rpc_printer_publish_internals_args(struct rpc_pipe_client
*pipe_hnd
,
1298 TALLOC_CTX
*mem_ctx
,
1303 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1304 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1305 uint32_t i
, num_printers
;
1307 const char *printername
, *sharename
;
1308 union spoolss_PrinterInfo
*info_enum
;
1309 union spoolss_PrinterInfo info
;
1310 struct spoolss_SetPrinterInfoCtr info_ctr
;
1311 struct spoolss_DevmodeContainer devmode_ctr
;
1312 struct sec_desc_buf secdesc_ctr
;
1313 struct policy_handle hnd
= { 0, };
1315 const char *action_str
;
1317 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
))
1320 for (i
= 0; i
< num_printers
; i
++) {
1322 /* do some initialization */
1323 printername
= info_enum
[i
].info2
.printername
;
1324 sharename
= info_enum
[i
].info2
.sharename
;
1325 if (!printername
|| !sharename
) {
1329 /* open printer handle */
1330 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1331 PRINTER_ALL_ACCESS
, pipe_hnd
->auth
->user_name
, &hnd
))
1334 /* check for existing dst printer */
1335 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &info
))
1338 /* check action and set string */
1340 case DSPRINT_PUBLISH
:
1341 action_str
= N_("published");
1343 case DSPRINT_UPDATE
:
1344 action_str
= N_("updated");
1346 case DSPRINT_UNPUBLISH
:
1347 action_str
= N_("unpublished");
1350 action_str
= N_("unknown action");
1351 printf(_("unknown action: %d\n"), action
);
1355 info
.info7
.action
= action
;
1357 info_ctr
.info
.info7
= (struct spoolss_SetPrinterInfo7
*)
1358 (void *)&info
.info7
;
1360 ZERO_STRUCT(devmode_ctr
);
1361 ZERO_STRUCT(secdesc_ctr
);
1363 nt_status
= dcerpc_spoolss_SetPrinter(b
, mem_ctx
,
1370 if (!NT_STATUS_IS_OK(nt_status
)) {
1371 printf(_("cannot set printer-info: %s\n"),
1372 nt_errstr(nt_status
));
1375 if (!W_ERROR_IS_OK(result
) && !W_ERROR_EQUAL(result
, WERR_IO_PENDING
)) {
1376 if ((action
== DSPRINT_UPDATE
) && W_ERROR_EQUAL(result
, W_ERROR(0x80070002))) {
1377 printf(_("printer not published yet\n"));
1379 printf(_("cannot set printer-info: %s\n"),
1380 win_errstr(result
));
1382 nt_status
= werror_to_ntstatus(result
);
1386 printf(_("successfully %s printer %s in Active Directory\n"),
1387 action_str
, sharename
);
1390 nt_status
= NT_STATUS_OK
;
1393 if (is_valid_policy_hnd(&hnd
)) {
1394 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &result
);
1400 NTSTATUS
rpc_printer_publish_publish_internals(struct net_context
*c
,
1401 const struct dom_sid
*domain_sid
,
1402 const char *domain_name
,
1403 struct cli_state
*cli
,
1404 struct rpc_pipe_client
*pipe_hnd
,
1405 TALLOC_CTX
*mem_ctx
,
1409 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, DSPRINT_PUBLISH
);
1412 NTSTATUS
rpc_printer_publish_unpublish_internals(struct net_context
*c
,
1413 const struct dom_sid
*domain_sid
,
1414 const char *domain_name
,
1415 struct cli_state
*cli
,
1416 struct rpc_pipe_client
*pipe_hnd
,
1417 TALLOC_CTX
*mem_ctx
,
1421 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, DSPRINT_UNPUBLISH
);
1424 NTSTATUS
rpc_printer_publish_update_internals(struct net_context
*c
,
1425 const struct dom_sid
*domain_sid
,
1426 const char *domain_name
,
1427 struct cli_state
*cli
,
1428 struct rpc_pipe_client
*pipe_hnd
,
1429 TALLOC_CTX
*mem_ctx
,
1433 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, DSPRINT_UPDATE
);
1437 * List print-queues w.r.t. their publishing state
1439 * All parameters are provided by the run_rpc_command function, except for
1440 * argc, argv which are passed through.
1442 * @param c A net_context structure
1443 * @param domain_sid The domain sid aquired from the remote server
1444 * @param cli A cli_state connected to the server.
1445 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1446 * @param argc Standard main() style argc
1447 * @param argv Standard main() style argv. Initial components are already
1450 * @return Normal NTSTATUS return.
1453 NTSTATUS
rpc_printer_publish_list_internals(struct net_context
*c
,
1454 const struct dom_sid
*domain_sid
,
1455 const char *domain_name
,
1456 struct cli_state
*cli
,
1457 struct rpc_pipe_client
*pipe_hnd
,
1458 TALLOC_CTX
*mem_ctx
,
1462 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1463 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1464 uint32_t i
, num_printers
;
1466 const char *printername
, *sharename
;
1467 union spoolss_PrinterInfo
*info_enum
;
1468 union spoolss_PrinterInfo info
;
1469 struct policy_handle hnd
= { 0, };
1473 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
))
1476 for (i
= 0; i
< num_printers
; i
++) {
1478 /* do some initialization */
1479 printername
= info_enum
[i
].info2
.printername
;
1480 sharename
= info_enum
[i
].info2
.sharename
;
1482 if (!printername
|| !sharename
) {
1486 /* open printer handle */
1487 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1488 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd
))
1491 /* check for existing dst printer */
1492 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &info
))
1495 if (!info
.info7
.guid
) {
1498 state
= info
.info7
.action
;
1500 case DSPRINT_PUBLISH
:
1501 printf(_("printer [%s] is published"),
1504 printf(_(", guid: %s"),info
.info7
.guid
);
1507 case DSPRINT_UNPUBLISH
:
1508 printf(_("printer [%s] is unpublished\n"),
1511 case DSPRINT_UPDATE
:
1512 printf(_("printer [%s] is currently updating\n"),
1516 printf(_("unknown state: %d\n"), state
);
1521 nt_status
= NT_STATUS_OK
;
1524 if (is_valid_policy_hnd(&hnd
)) {
1525 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1532 * Migrate Printer-ACLs from a source server to the destination server
1534 * All parameters are provided by the run_rpc_command function, except for
1535 * argc, argv which are passed through.
1537 * @param c A net_context structure
1538 * @param domain_sid The domain sid aquired from the remote server
1539 * @param cli A cli_state connected to the server.
1540 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1541 * @param argc Standard main() style argc
1542 * @param argv Standard main() style argv. Initial components are already
1545 * @return Normal NTSTATUS return.
1548 NTSTATUS
rpc_printer_migrate_security_internals(struct net_context
*c
,
1549 const struct dom_sid
*domain_sid
,
1550 const char *domain_name
,
1551 struct cli_state
*cli
,
1552 struct rpc_pipe_client
*pipe_hnd
,
1553 TALLOC_CTX
*mem_ctx
,
1557 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1558 /* TODO: what now, info2 or info3 ?
1559 convince jerry that we should add clientside setacls level 3 at least
1561 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1563 uint32_t num_printers
;
1565 const char *printername
, *sharename
;
1566 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1567 struct dcerpc_binding_handle
*b_dst
= NULL
;
1568 struct policy_handle hnd_src
= { 0, };
1569 struct policy_handle hnd_dst
= { 0, };
1570 union spoolss_PrinterInfo
*info_enum
;
1571 struct cli_state
*cli_dst
= NULL
;
1572 union spoolss_PrinterInfo info_src
, info_dst
;
1575 DEBUG(3,("copying printer ACLs\n"));
1577 /* connect destination PI_SPOOLSS */
1578 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1579 &ndr_table_spoolss
.syntax_id
);
1580 if (!NT_STATUS_IS_OK(nt_status
)) {
1583 b_dst
= pipe_hnd_dst
->binding_handle
;
1585 /* enum source printers */
1586 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
1587 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1591 if (!num_printers
) {
1592 printf (_("no printers found on server.\n"));
1593 nt_status
= NT_STATUS_OK
;
1597 /* do something for all printers */
1598 for (i
= 0; i
< num_printers
; i
++) {
1600 /* do some initialization */
1601 printername
= info_enum
[i
].info2
.printername
;
1602 sharename
= info_enum
[i
].info2
.sharename
;
1604 if (!printername
|| !sharename
) {
1605 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1609 /* we can reset NT_STATUS here because we do not
1610 get any real NT_STATUS-codes anymore from now on */
1611 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1613 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1614 printername
, sharename
);
1616 /* according to msdn you have specify these access-rights
1617 to see the security descriptor
1618 - READ_CONTROL (DACL)
1619 - ACCESS_SYSTEM_SECURITY (SACL)
1622 /* open src printer handle */
1623 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1624 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
1627 /* open dst printer handle */
1628 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1629 PRINTER_ALL_ACCESS
, cli_dst
->user_name
, &hnd_dst
))
1632 /* check for existing dst printer */
1633 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
))
1636 /* check for existing src printer */
1637 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, 3, &info_src
))
1640 /* Copy Security Descriptor */
1642 /* copy secdesc (info level 2) */
1643 info_dst
.info2
.devmode
= NULL
;
1644 info_dst
.info2
.secdesc
= dup_sec_desc(mem_ctx
, info_src
.info3
.secdesc
);
1647 display_sec_desc(info_dst
.info2
.secdesc
);
1649 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
1652 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1655 /* close printer handles here */
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
);
1666 nt_status
= NT_STATUS_OK
;
1670 if (is_valid_policy_hnd(&hnd_src
)) {
1671 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
1674 if (is_valid_policy_hnd(&hnd_dst
)) {
1675 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
1679 cli_shutdown(cli_dst
);
1685 * Migrate printer-forms from a src server to the dst server
1687 * All parameters are provided by the run_rpc_command function, except for
1688 * argc, argv which are passed through.
1690 * @param c A net_context structure
1691 * @param domain_sid The domain sid aquired from the remote server
1692 * @param cli A cli_state connected to the server.
1693 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1694 * @param argc Standard main() style argc
1695 * @param argv Standard main() style argv. Initial components are already
1698 * @return Normal NTSTATUS return.
1701 NTSTATUS
rpc_printer_migrate_forms_internals(struct net_context
*c
,
1702 const struct dom_sid
*domain_sid
,
1703 const char *domain_name
,
1704 struct cli_state
*cli
,
1705 struct rpc_pipe_client
*pipe_hnd
,
1706 TALLOC_CTX
*mem_ctx
,
1710 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1711 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1714 uint32_t num_printers
;
1716 const char *printername
, *sharename
;
1717 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1718 struct dcerpc_binding_handle
*b_dst
= NULL
;
1719 struct policy_handle hnd_src
= { 0, };
1720 struct policy_handle hnd_dst
= { 0, };
1721 union spoolss_PrinterInfo
*info_enum
;
1722 union spoolss_PrinterInfo info_dst
;
1724 union spoolss_FormInfo
*forms
;
1725 struct cli_state
*cli_dst
= NULL
;
1727 DEBUG(3,("copying forms\n"));
1729 /* connect destination PI_SPOOLSS */
1730 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1731 &ndr_table_spoolss
.syntax_id
);
1732 if (!NT_STATUS_IS_OK(nt_status
)) {
1735 b_dst
= pipe_hnd_dst
->binding_handle
;
1737 /* enum src printers */
1738 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1739 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1743 if (!num_printers
) {
1744 printf (_("no printers found on server.\n"));
1745 nt_status
= NT_STATUS_OK
;
1749 /* do something for all printers */
1750 for (i
= 0; i
< num_printers
; i
++) {
1752 /* do some initialization */
1753 printername
= info_enum
[i
].info2
.printername
;
1754 sharename
= info_enum
[i
].info2
.sharename
;
1756 if (!printername
|| !sharename
) {
1757 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1760 /* we can reset NT_STATUS here because we do not
1761 get any real NT_STATUS-codes anymore from now on */
1762 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1764 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1765 printername
, sharename
);
1768 /* open src printer handle */
1769 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1770 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
1773 /* open dst printer handle */
1774 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1775 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd_dst
))
1778 /* check for existing dst printer */
1779 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
))
1782 /* finally migrate forms */
1783 if (!net_spoolss_enumforms(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &num_forms
, &forms
))
1786 DEBUG(1,("got %d forms for printer\n", num_forms
));
1789 for (f
= 0; f
< num_forms
; f
++) {
1791 union spoolss_AddFormInfo info
;
1794 /* only migrate FORM_PRINTER types, according to jerry
1795 FORM_BUILTIN-types are hard-coded in samba */
1796 if (forms
[f
].info1
.flags
!= SPOOLSS_FORM_PRINTER
)
1800 d_printf(_("\tmigrating form # %d [%s] of type "
1802 f
, forms
[f
].info1
.form_name
,
1803 forms
[f
].info1
.flags
);
1805 info
.info1
= (struct spoolss_AddFormInfo1
*)
1806 (void *)&forms
[f
].info1
;
1808 /* FIXME: there might be something wrong with samba's
1810 status
= dcerpc_spoolss_AddForm(b_dst
, mem_ctx
,
1815 if (!NT_STATUS_IS_OK(status
)) {
1816 d_printf(_("\tdcerpc_spoolss_AddForm form %d: [%s] - %s\n"),
1817 f
, forms
[f
].info1
.form_name
, nt_errstr(status
));
1820 if (!W_ERROR_IS_OK(result
)) {
1821 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1822 f
, forms
[f
].info1
.form_name
);
1826 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1827 forms
[f
].info1
.form_name
));
1831 /* close printer handles here */
1832 if (is_valid_policy_hnd(&hnd_src
)) {
1833 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
1836 if (is_valid_policy_hnd(&hnd_dst
)) {
1837 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
1841 nt_status
= NT_STATUS_OK
;
1845 if (is_valid_policy_hnd(&hnd_src
)) {
1846 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
1849 if (is_valid_policy_hnd(&hnd_dst
)) {
1850 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
1854 cli_shutdown(cli_dst
);
1860 * Migrate printer-drivers from a src server to the dst server
1862 * All parameters are provided by the run_rpc_command function, except for
1863 * argc, argv which are passed through.
1865 * @param c A net_context structure
1866 * @param domain_sid The domain sid aquired from the remote server
1867 * @param cli A cli_state connected to the server.
1868 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1869 * @param argc Standard main() style argc
1870 * @param argv Standard main() style argv. Initial components are already
1873 * @return Normal NTSTATUS return.
1876 NTSTATUS
rpc_printer_migrate_drivers_internals(struct net_context
*c
,
1877 const struct dom_sid
*domain_sid
,
1878 const char *domain_name
,
1879 struct cli_state
*cli
,
1880 struct rpc_pipe_client
*pipe_hnd
,
1881 TALLOC_CTX
*mem_ctx
,
1885 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1886 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1888 uint32_t num_printers
;
1890 const char *printername
, *sharename
;
1891 bool got_src_driver_share
= false;
1892 bool got_dst_driver_share
= false;
1893 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1894 struct dcerpc_binding_handle
*b_dst
= NULL
;
1895 struct policy_handle hnd_src
= { 0, };
1896 struct policy_handle hnd_dst
= { 0, };
1897 union spoolss_DriverInfo drv_info_src
;
1898 union spoolss_PrinterInfo
*info_enum
;
1899 union spoolss_PrinterInfo info_dst
;
1900 struct cli_state
*cli_dst
= NULL
;
1901 struct cli_state
*cli_share_src
= NULL
;
1902 struct cli_state
*cli_share_dst
= NULL
;
1903 const char *drivername
= NULL
;
1906 DEBUG(3,("copying printer-drivers\n"));
1908 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1909 &ndr_table_spoolss
.syntax_id
);
1910 if (!NT_STATUS_IS_OK(nt_status
)) {
1913 b_dst
= pipe_hnd_dst
->binding_handle
;
1915 /* open print$-share on the src server */
1916 nt_status
= connect_to_service(c
, &cli_share_src
,
1917 cli_state_remote_sockaddr(cli
),
1918 cli_state_remote_name(cli
),
1920 if (!NT_STATUS_IS_OK(nt_status
))
1923 got_src_driver_share
= true;
1926 /* open print$-share on the dst server */
1927 nt_status
= connect_to_service(c
, &cli_share_dst
,
1928 cli_state_remote_sockaddr(cli_dst
),
1929 cli_state_remote_name(cli_dst
),
1931 if (!NT_STATUS_IS_OK(nt_status
))
1934 got_dst_driver_share
= true;
1937 /* enum src printers */
1938 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1939 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1943 if (num_printers
== 0) {
1944 printf (_("no printers found on server.\n"));
1945 nt_status
= NT_STATUS_OK
;
1950 /* do something for all printers */
1951 for (p
= 0; p
< num_printers
; p
++) {
1953 /* do some initialization */
1954 printername
= info_enum
[p
].info2
.printername
;
1955 sharename
= info_enum
[p
].info2
.sharename
;
1957 if (!printername
|| !sharename
) {
1958 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1962 /* we can reset NT_STATUS here because we do not
1963 get any real NT_STATUS-codes anymore from now on */
1964 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1966 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1967 printername
, sharename
);
1969 /* open dst printer handle */
1970 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1971 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd_dst
))
1974 /* check for existing dst printer */
1975 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
1979 /* open src printer handle */
1980 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1981 MAXIMUM_ALLOWED_ACCESS
,
1982 pipe_hnd
->auth
->user_name
,
1986 /* in a first step call getdriver for each shared printer (per arch)
1987 to get a list of all files that have to be copied */
1989 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
1992 if (!net_spoolss_getprinterdriver(pipe_hnd
, mem_ctx
, &hnd_src
,
1993 level
, archi_table
[i
].long_archi
,
1994 archi_table
[i
].version
, &drv_info_src
))
1997 drivername
= drv_info_src
.info3
.driver_name
;
2000 display_print_driver3(&drv_info_src
.info3
);
2002 /* check arch dir */
2003 nt_status
= check_arch_dir(cli_share_dst
, archi_table
[i
].short_archi
);
2004 if (!NT_STATUS_IS_OK(nt_status
))
2008 /* copy driver-files */
2009 nt_status
= copy_print_driver_3(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
2010 archi_table
[i
].short_archi
,
2011 &drv_info_src
.info3
);
2012 if (!NT_STATUS_IS_OK(nt_status
))
2017 if (!net_spoolss_addprinterdriver(pipe_hnd_dst
, mem_ctx
, level
, &drv_info_src
)) {
2018 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2022 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
2023 drivername
, printername
));
2027 if (!drivername
|| strlen(drivername
) == 0) {
2028 DEBUGADD(1,("Did not get driver for printer %s\n",
2034 info_dst
.info2
.drivername
= drivername
;
2036 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
)) {
2037 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2041 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
2042 drivername
, printername
));
2045 if (is_valid_policy_hnd(&hnd_dst
)) {
2046 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2050 if (is_valid_policy_hnd(&hnd_src
)) {
2051 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2055 nt_status
= NT_STATUS_OK
;
2059 if (is_valid_policy_hnd(&hnd_dst
)) {
2060 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2064 if (is_valid_policy_hnd(&hnd_src
)) {
2065 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2069 cli_shutdown(cli_dst
);
2072 if (got_src_driver_share
)
2073 cli_shutdown(cli_share_src
);
2075 if (got_dst_driver_share
)
2076 cli_shutdown(cli_share_dst
);
2083 * Migrate printer-queues from a src to the dst server
2084 * (requires a working "addprinter command" to be installed for the local smbd)
2086 * All parameters are provided by the run_rpc_command function, except for
2087 * argc, argv which are passed through.
2089 * @param c A net_context structure
2090 * @param domain_sid The domain sid aquired from the remote server
2091 * @param cli A cli_state connected to the server.
2092 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2093 * @param argc Standard main() style argc
2094 * @param argv Standard main() style argv. Initial components are already
2097 * @return Normal NTSTATUS return.
2100 NTSTATUS
rpc_printer_migrate_printers_internals(struct net_context
*c
,
2101 const struct dom_sid
*domain_sid
,
2102 const char *domain_name
,
2103 struct cli_state
*cli
,
2104 struct rpc_pipe_client
*pipe_hnd
,
2105 TALLOC_CTX
*mem_ctx
,
2109 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2111 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2112 uint32_t i
= 0, num_printers
;
2114 union spoolss_PrinterInfo info_dst
, info_src
;
2115 union spoolss_PrinterInfo
*info_enum
;
2116 struct cli_state
*cli_dst
= NULL
;
2117 struct policy_handle hnd_src
= { 0, };
2118 struct policy_handle hnd_dst
= { 0, };
2119 const char *printername
, *sharename
;
2120 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2121 struct dcerpc_binding_handle
*b_dst
= NULL
;
2122 struct spoolss_SetPrinterInfoCtr info_ctr
;
2124 DEBUG(3,("copying printers\n"));
2126 /* connect destination PI_SPOOLSS */
2127 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2128 &ndr_table_spoolss
.syntax_id
);
2129 if (!NT_STATUS_IS_OK(nt_status
)) {
2132 b_dst
= pipe_hnd_dst
->binding_handle
;
2135 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2136 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2140 if (!num_printers
) {
2141 printf (_("no printers found on server.\n"));
2142 nt_status
= NT_STATUS_OK
;
2146 /* do something for all printers */
2147 for (i
= 0; i
< num_printers
; i
++) {
2149 struct spoolss_SetPrinterInfo2 info2
;
2151 /* do some initialization */
2152 printername
= info_enum
[i
].info2
.printername
;
2153 sharename
= info_enum
[i
].info2
.sharename
;
2155 if (!printername
|| !sharename
) {
2156 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2159 /* we can reset NT_STATUS here because we do not
2160 get any real NT_STATUS-codes anymore from now on */
2161 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2163 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2164 printername
, sharename
);
2166 /* open dst printer handle */
2167 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2168 PRINTER_ALL_ACCESS
, cli
->user_name
, &hnd_dst
)) {
2170 DEBUG(1,("could not open printer: %s\n", sharename
));
2173 /* check for existing dst printer */
2174 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
)) {
2175 printf (_("could not get printer, creating printer.\n"));
2177 DEBUG(1,("printer already exists: %s\n", sharename
));
2178 /* close printer handle here - dst only, not got src yet. */
2179 if (is_valid_policy_hnd(&hnd_dst
)) {
2180 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2185 /* now get again src printer ctr via getprinter,
2186 we first need a handle for that */
2188 /* open src printer handle */
2189 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2190 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
2193 /* getprinter on the src server */
2194 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &info_src
))
2197 /* copy each src printer to a dst printer 1:1,
2198 maybe some values have to be changed though */
2199 d_printf(_("creating printer: %s\n"), printername
);
2201 info_ctr
.level
= level
;
2202 spoolss_printerinfo2_to_setprinterinfo2(&info_src
.info2
, &info2
);
2203 info_ctr
.info
.info2
= &info2
;
2205 result
= rpccli_spoolss_addprinterex(pipe_hnd_dst
,
2209 if (W_ERROR_IS_OK(result
))
2210 d_printf (_("printer [%s] successfully added.\n"),
2212 else if (W_ERROR_V(result
) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS
))
2213 d_fprintf (stderr
, _("printer [%s] already exists.\n"),
2216 d_fprintf (stderr
, _("could not create printer [%s]\n"),
2221 /* close printer handles here */
2222 if (is_valid_policy_hnd(&hnd_src
)) {
2223 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2226 if (is_valid_policy_hnd(&hnd_dst
)) {
2227 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2231 nt_status
= NT_STATUS_OK
;
2234 if (is_valid_policy_hnd(&hnd_src
)) {
2235 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2238 if (is_valid_policy_hnd(&hnd_dst
)) {
2239 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2243 cli_shutdown(cli_dst
);
2249 * Migrate Printer-Settings from a src server to the dst server
2250 * (for this to work, printers and drivers already have to be migrated earlier)
2252 * All parameters are provided by the run_rpc_command function, except for
2253 * argc, argv which are passed through.
2255 * @param c A net_context structure
2256 * @param domain_sid The domain sid aquired from the remote server
2257 * @param cli A cli_state connected to the server.
2258 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2259 * @param argc Standard main() style argc
2260 * @param argv Standard main() style argv. Initial components are already
2263 * @return Normal NTSTATUS return.
2266 NTSTATUS
rpc_printer_migrate_settings_internals(struct net_context
*c
,
2267 const struct dom_sid
*domain_sid
,
2268 const char *domain_name
,
2269 struct cli_state
*cli
,
2270 struct rpc_pipe_client
*pipe_hnd
,
2271 TALLOC_CTX
*mem_ctx
,
2275 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2277 /* FIXME: Here the nightmare begins */
2280 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2281 uint32_t i
= 0, j
= 0;
2282 uint32_t num_printers
;
2284 const char *printername
, *sharename
;
2285 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2286 struct dcerpc_binding_handle
*b_dst
= NULL
;
2287 struct policy_handle hnd_src
= { 0, };
2288 struct policy_handle hnd_dst
= { 0, };
2289 union spoolss_PrinterInfo
*info_enum
;
2290 union spoolss_PrinterInfo info_dst_publish
;
2291 union spoolss_PrinterInfo info_dst
;
2292 struct cli_state
*cli_dst
= NULL
;
2293 const char *longname
;
2294 const char **keylist
= NULL
;
2297 ZERO_STRUCT(info_dst_publish
);
2299 DEBUG(3,("copying printer settings\n"));
2301 /* connect destination PI_SPOOLSS */
2302 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2303 &ndr_table_spoolss
.syntax_id
);
2304 if (!NT_STATUS_IS_OK(nt_status
)) {
2307 b_dst
= pipe_hnd_dst
->binding_handle
;
2309 /* enum src printers */
2310 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2311 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2315 if (!num_printers
) {
2316 printf (_("no printers found on server.\n"));
2317 nt_status
= NT_STATUS_OK
;
2322 /* needed for dns-strings in regkeys */
2323 longname
= get_mydnsfullname();
2325 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2329 /* do something for all printers */
2330 for (i
= 0; i
< num_printers
; i
++) {
2332 uint32_t value_needed
;
2333 uint32_t data_needed
;
2334 enum winreg_Type type
;
2335 struct spoolss_EnumPrinterData r
;
2337 /* do some initialization */
2338 printername
= info_enum
[i
].info2
.printername
;
2339 sharename
= info_enum
[i
].info2
.sharename
;
2341 if (!printername
|| !sharename
) {
2342 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2345 /* we can reset NT_STATUS here because we do not
2346 get any real NT_STATUS-codes anymore from now on */
2347 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2349 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2350 printername
, sharename
);
2353 /* open src printer handle */
2354 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2355 MAXIMUM_ALLOWED_ACCESS
, cli
->user_name
, &hnd_src
))
2358 /* open dst printer handle */
2359 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2360 PRINTER_ALL_ACCESS
, cli_dst
->user_name
, &hnd_dst
))
2363 /* check for existing dst printer */
2364 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2369 /* STEP 1: COPY DEVICE-MODE and other
2370 PRINTER_INFO_2-attributes
2373 info_dst
.info2
= info_enum
[i
].info2
;
2375 /* why is the port always disconnected when the printer
2376 is correctly installed (incl. driver ???) */
2377 info_dst
.info2
.portname
= SAMBA_PRINTER_PORT_NAME
;
2379 /* check if printer is published */
2380 if (info_enum
[i
].info2
.attributes
& PRINTER_ATTRIBUTE_PUBLISHED
) {
2382 /* check for existing dst printer */
2383 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
))
2386 info_dst_publish
.info7
.action
= DSPRINT_PUBLISH
;
2388 /* ignore false from setprinter due to WERR_IO_PENDING */
2389 net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
);
2391 DEBUG(3,("republished printer\n"));
2394 if (info_enum
[i
].info2
.devmode
!= NULL
) {
2396 /* copy devmode (info level 2) */
2397 info_dst
.info2
.devmode
= info_enum
[i
].info2
.devmode
;
2399 /* do not copy security descriptor (we have another
2400 * command for that) */
2401 info_dst
.info2
.secdesc
= NULL
;
2404 info_dst
.info2
.devmode
.devicename
=
2405 talloc_asprintf(mem_ctx
, "\\\\%s\\%s",
2406 longname
, printername
);
2407 if (!info_dst
.info2
.devmode
.devicename
) {
2408 nt_status
= NT_STATUS_NO_MEMORY
;
2412 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2416 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2419 /* STEP 2: COPY REGISTRY VALUES */
2421 /* please keep in mind that samba parse_spools gives horribly
2422 crippled results when used to rpccli_spoolss_enumprinterdataex
2423 a win2k3-server. (Bugzilla #1851)
2424 FIXME: IIRC I've seen it too on a win2k-server
2427 r
.in
.handle
= &hnd_src
;
2428 r
.in
.enum_index
= 0;
2429 r
.in
.value_offered
= 0;
2430 r
.in
.data_offered
= 0;
2431 r
.out
.value_name
= NULL
;
2432 r
.out
.value_needed
= &value_needed
;
2435 r
.out
.data_needed
= &data_needed
;
2437 /* enumerate data on src handle */
2438 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2440 r
.in
.data_offered
= *r
.out
.data_needed
;
2441 r
.in
.value_offered
= *r
.out
.value_needed
;
2442 r
.out
.data
= talloc_zero_array(mem_ctx
, uint8_t, r
.in
.data_offered
);
2443 r
.out
.value_name
= talloc_zero_array(mem_ctx
, char, r
.in
.value_offered
);
2445 /* loop for all printerdata of "PrinterDriverData" */
2446 while (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2450 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2452 /* loop for all reg_keys */
2453 if (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2456 if (c
->opt_verbose
) {
2457 struct registry_value v
;
2458 v
.type
= *r
.out
.type
;
2459 v
.data
= data_blob_const(
2460 r
.out
.data
, r
.in
.data_offered
);
2462 display_reg_value(SPOOL_PRINTERDATA_KEY
,
2463 r
.out
.value_name
, &v
);
2467 if (!net_spoolss_setprinterdata(pipe_hnd_dst
, mem_ctx
,
2468 &hnd_dst
, r
.out
.value_name
,
2469 *r
.out
.type
, r
.out
.data
, r
.in
.data_offered
))
2472 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2477 /* STEP 3: COPY SUBKEY VALUES */
2479 /* here we need to enum all printer_keys and then work
2480 on the result with enum_printer_key_ex. nt4 does not
2481 respond to enumprinterkey, win2k does, so continue
2482 in case of an error */
2484 if (!net_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, &hnd_src
, "", &keylist
)) {
2485 printf(_("got no key-data\n"));
2490 /* work on a list of printer keys
2491 each key has to be enumerated to get all required
2492 information. information is then set via setprinterdataex-calls */
2494 if (keylist
== NULL
)
2497 for (i
=0; keylist
&& keylist
[i
] != NULL
; i
++) {
2499 const char *subkey
= keylist
[i
];
2501 struct spoolss_PrinterEnumValues
*info
;
2503 /* enumerate all src subkeys */
2504 if (!net_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
, 0,
2510 for (j
=0; j
< count
; j
++) {
2512 struct registry_value value
;
2513 const char *value_name
= info
[j
].value_name
;
2514 value
.type
= REG_SZ
;
2516 /* although samba replies with sane data in most cases we
2517 should try to avoid writing wrong registry data */
2519 if (strequal(value_name
, SPOOL_REG_PORTNAME
)) {
2520 /* although windows uses a multi-sz, we use a sz */
2521 push_reg_sz(mem_ctx
, &value
.data
, SAMBA_PRINTER_PORT_NAME
);
2523 else if (strequal(value_name
, SPOOL_REG_UNCNAME
)) {
2525 if (asprintf(&unc_name
, "\\\\%s\\%s", longname
, sharename
) < 0) {
2526 nt_status
= NT_STATUS_NO_MEMORY
;
2529 push_reg_sz(mem_ctx
, &value
.data
, unc_name
);
2532 else if (strequal(value_name
, SPOOL_REG_URL
)) {
2535 /* FIXME: should we really do that ??? */
2536 if (asprintf(&url
, "http://%s:631/printers/%s", longname
, sharename
) < 0) {
2537 nt_status
= NT_STATUS_NO_MEMORY
;
2540 push_reg_sz(mem_ctx
, NULL
, &value
.data
, url
);
2544 else if (strequal(value_name
, SPOOL_REG_SERVERNAME
)) {
2545 push_reg_sz(mem_ctx
, &value
.data
, longname
);
2547 else if (strequal(value_name
, SPOOL_REG_SHORTSERVERNAME
)) {
2548 push_reg_sz(mem_ctx
, &value
.data
, lp_netbios_name());
2551 value
.type
= info
[j
].type
;
2552 value
.data
= *info
[j
].data
;
2555 if (c
->opt_verbose
) {
2556 display_reg_value(subkey
, value_name
, &value
);
2559 /* here we have to set all subkeys on the dst server */
2560 if (!net_spoolss_setprinterdataex(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2561 subkey
, value_name
, &value
))
2566 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2567 subkey
, info
[j
].value_name
));
2572 TALLOC_FREE(keylist
);
2574 /* close printer handles here */
2575 if (is_valid_policy_hnd(&hnd_src
)) {
2576 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2579 if (is_valid_policy_hnd(&hnd_dst
)) {
2580 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2584 nt_status
= NT_STATUS_OK
;
2587 if (is_valid_policy_hnd(&hnd_src
)) {
2588 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2591 if (is_valid_policy_hnd(&hnd_dst
)) {
2592 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2596 cli_shutdown(cli_dst
);