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"
31 #include "libsmb/clirap.h"
32 #include "../libcli/smb/smbXcli_base.h"
33 #include "auth/gensec/gensec.h"
34 #include "auth/credentials/credentials.h"
35 #include "lib/util/string_wrappers.h"
39 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
40 * It is here for debugging purpose and should be removed later on.
43 /****************************************************************************
44 Printer info level 3 display function.
45 ****************************************************************************/
47 static void display_print_driver3(struct spoolss_DriverInfo3
*r
)
55 printf(_("Printer Driver Info 3:\n"));
56 printf(_("\tVersion: [%x]\n"), r
->version
);
57 printf(_("\tDriver Name: [%s]\n"), r
->driver_name
);
58 printf(_("\tArchitecture: [%s]\n"), r
->architecture
);
59 printf(_("\tDriver Path: [%s]\n"), r
->driver_path
);
60 printf(_("\tDatafile: [%s]\n"), r
->data_file
);
61 printf(_("\tConfigfile: [%s]\n\n"), r
->config_file
);
62 printf(_("\tHelpfile: [%s]\n\n"), r
->help_file
);
64 for (i
=0; r
->dependent_files
&& r
->dependent_files
[i
] != NULL
; i
++) {
65 printf(_("\tDependentfiles: [%s]\n"), r
->dependent_files
[i
]);
70 printf(_("\tMonitorname: [%s]\n"), r
->monitor_name
);
71 printf(_("\tDefaultdatatype: [%s]\n\n"), r
->default_datatype
);
74 static void display_reg_value(const char *subkey
, const char *name
, struct registry_value
*value
)
80 if (value
->data
.length
== sizeof(uint32_t)) {
81 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey
,
82 name
, IVAL(value
->data
.data
,0));
84 d_printf(_("\t[%s:%s]: REG_DWORD: <invalid>\n"), subkey
,
90 pull_reg_sz(talloc_tos(), &value
->data
, &text
);
94 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey
, name
, text
);
98 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
107 if (!pull_reg_multi_sz(NULL
, &value
->data
, &values
)) {
108 d_printf("pull_reg_multi_sz failed\n");
112 printf("%s: REG_MULTI_SZ: \n", name
);
113 for (i
=0; values
[i
] != NULL
; i
++) {
114 d_printf("%s\n", values
[i
]);
121 d_printf(_("\t%s: unknown type %d\n"), name
, value
->type
);
127 * Copies ACLs, DOS-attributes and timestamps from one
128 * file or directory from one connected share to another connected share
130 * @param c A net_context structure
131 * @param mem_ctx A talloc-context
132 * @param cli_share_src A connected cli_state
133 * @param cli_share_dst A connected cli_state
134 * @param src_file The source file-name
135 * @param dst_file The destination file-name
136 * @param copy_acls Whether to copy acls
137 * @param copy_attrs Whether to copy DOS attributes
138 * @param copy_timestamps Whether to preserve timestamps
139 * @param is_file Whether this file is a file or a dir
141 * @return Normal NTSTATUS return.
144 NTSTATUS
net_copy_fileattr(struct net_context
*c
,
146 struct cli_state
*cli_share_src
,
147 struct cli_state
*cli_share_dst
,
148 const char *src_name
, const char *dst_name
,
149 bool copy_acls
, bool copy_attrs
,
150 bool copy_timestamps
, bool is_file
)
153 uint16_t fnum_src
= 0;
154 uint16_t fnum_dst
= 0;
155 struct security_descriptor
*sd
= NULL
;
156 uint32_t attr
= (uint32_t)-1;
157 struct timespec f_create_time
= { .tv_nsec
= SAMBA_UTIME_OMIT
};
158 struct timespec f_access_time
= { .tv_nsec
= SAMBA_UTIME_OMIT
};
159 struct timespec f_write_time
= { .tv_nsec
= SAMBA_UTIME_OMIT
};
160 struct timespec f_change_time
= { .tv_nsec
= SAMBA_UTIME_OMIT
};
162 if (!copy_timestamps
&& !copy_acls
&& !copy_attrs
)
165 /* open file/dir on the originating server */
167 DEBUGADD(3,("opening %s %s on originating server\n",
168 is_file
?"file":"dir", src_name
));
170 nt_status
= cli_ntcreate(cli_share_src
, src_name
, 0,
171 READ_CONTROL_ACCESS
, 0,
172 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
173 0x0, 0x0, &fnum_src
, NULL
);
174 if (!NT_STATUS_IS_OK(nt_status
)) {
175 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
176 is_file
?"file":"dir", src_name
, nt_errstr(nt_status
)));
181 /* get the security descriptor */
182 nt_status
= cli_query_secdesc(cli_share_src
, fnum_src
,
184 if (!NT_STATUS_IS_OK(nt_status
)) {
185 DEBUG(0,("failed to get security descriptor: %s\n",
186 nt_errstr(nt_status
)));
190 if (c
->opt_verbose
&& DEBUGLEVEL
>= 3)
191 display_sec_desc(sd
);
194 if (copy_attrs
|| copy_timestamps
) {
196 /* get file attributes */
197 nt_status
= cli_qfileinfo_basic(
200 copy_attrs
? &attr
: NULL
,
202 copy_timestamps
? &f_create_time
: NULL
,
203 copy_timestamps
? &f_access_time
: NULL
,
204 copy_timestamps
? &f_write_time
: NULL
,
205 copy_timestamps
? &f_change_time
: NULL
,
207 if (!NT_STATUS_IS_OK(nt_status
)) {
208 DEBUG(0,("failed to get file-attrs: %s\n",
209 nt_errstr(nt_status
)));
214 /* open the file/dir on the destination server */
215 nt_status
= cli_ntcreate(cli_share_dst
, dst_name
, 0,
216 WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
, 0,
217 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
218 0x0, 0x0, &fnum_dst
, NULL
);
219 if (!NT_STATUS_IS_OK(nt_status
)) {
220 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
221 is_file
?"file":"dir", dst_name
, nt_errstr(nt_status
)));
227 nt_status
= cli_set_secdesc(cli_share_dst
, fnum_dst
, sd
);
228 if (!NT_STATUS_IS_OK(nt_status
)) {
229 DEBUG(0, ("could not set secdesc on %s: %s\n",
230 dst_name
, nt_errstr(nt_status
)));
235 if (copy_timestamps
|| copy_attrs
) {
237 nt_status
= cli_setfileinfo_ext(
245 if (!NT_STATUS_IS_OK(nt_status
)) {
246 DBG_ERR("failed to set file-attrs: %s\n",
247 nt_errstr(nt_status
));
254 nt_status
= cli_close(cli_share_src
, fnum_src
);
255 if (!NT_STATUS_IS_OK(nt_status
)) {
257 _("could not close %s on originating server: %s\n"),
258 is_file
?"file":"dir", nt_errstr(nt_status
));
262 nt_status
= cli_close(cli_share_dst
, fnum_dst
);
263 if (!NT_STATUS_IS_OK(nt_status
)) {
265 _("could not close %s on destination server: %s\n"),
266 is_file
?"file":"dir", nt_errstr(nt_status
));
271 nt_status
= NT_STATUS_OK
;
277 cli_close(cli_share_src
, fnum_src
);
280 cli_close(cli_share_dst
, fnum_dst
);
286 * Copy a file or directory from a connected share to another connected share
288 * @param c A net_context structure
289 * @param mem_ctx A talloc-context
290 * @param cli_share_src A connected cli_state
291 * @param cli_share_dst A connected cli_state
292 * @param src_file The source file-name
293 * @param dst_file The destination file-name
294 * @param copy_acls Whether to copy acls
295 * @param copy_attrs Whether to copy DOS attributes
296 * @param copy_timestamps Whether to preserve timestamps
297 * @param is_file Whether this file is a file or a dir
299 * @return Normal NTSTATUS return.
302 NTSTATUS
net_copy_file(struct net_context
*c
,
304 struct cli_state
*cli_share_src
,
305 struct cli_state
*cli_share_dst
,
306 const char *src_name
, const char *dst_name
,
307 bool copy_acls
, bool copy_attrs
,
308 bool copy_timestamps
, bool is_file
)
310 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
311 uint16_t fnum_src
= 0;
312 uint16_t fnum_dst
= 0;
313 static int io_bufsize
= 64512;
314 int read_size
= io_bufsize
;
319 if (!src_name
|| !dst_name
)
322 if (cli_share_src
== NULL
|| cli_share_dst
== NULL
)
325 /* open on the originating server */
326 DEBUGADD(3,("opening %s %s on originating server\n",
327 is_file
? "file":"dir", src_name
));
329 nt_status
= cli_open(cli_share_src
, src_name
, O_RDONLY
, DENY_NONE
, &fnum_src
);
331 nt_status
= cli_ntcreate(cli_share_src
, src_name
, 0, READ_CONTROL_ACCESS
, 0,
332 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
333 FILE_OPEN
, 0x0, 0x0, &fnum_src
, NULL
);
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 smbXcli_conn_remote_name(cli_share_src
->conn
),
372 cli_share_src
->share
, src_name
,
373 smbXcli_conn_remote_name(cli_share_dst
->conn
),
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 smbXcli_conn_remote_name(cli_share_src
->conn
),
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 smbXcli_conn_remote_name(cli_share_dst
->conn
),
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 struct policy_handle
*hnd
)
707 struct cli_credentials
*creds
= gensec_get_credentials(pipe_hnd
->auth
->auth_ctx
);
708 const char *username
= cli_credentials_get_username(creds
);
710 fstring printername2
;
712 fstrcpy(printername2
, pipe_hnd
->srv_name_slash
);
713 fstrcat(printername2
, "\\");
714 fstrcat(printername2
, printername
);
716 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
717 pipe_hnd
->srv_name_slash
, username
, printername2
, access_required
));
720 result
= rpccli_spoolss_openprinter_ex(pipe_hnd
, mem_ctx
,
725 /* be more verbose */
726 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
728 _("no access to printer [%s] on [%s] for user [%s] "
730 printername2
, pipe_hnd
->srv_name_slash
, username
);
734 if (!W_ERROR_IS_OK(result
)) {
735 d_fprintf(stderr
,_("cannot open printer %s on server %s: %s\n"),
736 printername2
, pipe_hnd
->srv_name_slash
, win_errstr(result
));
740 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
741 printername2
, pipe_hnd
->srv_name_slash
));
746 static bool net_spoolss_getprinter(struct rpc_pipe_client
*pipe_hnd
,
748 struct policy_handle
*hnd
,
750 union spoolss_PrinterInfo
*info
)
754 /* getprinter call */
755 result
= rpccli_spoolss_getprinter(pipe_hnd
, mem_ctx
,
760 if (!W_ERROR_IS_OK(result
)) {
761 printf(_("cannot get printer-info: %s\n"), win_errstr(result
));
768 static bool net_spoolss_setprinter(struct rpc_pipe_client
*pipe_hnd
,
770 struct policy_handle
*hnd
,
772 union spoolss_PrinterInfo
*info
)
774 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
777 struct spoolss_SetPrinterInfoCtr info_ctr
;
778 struct spoolss_SetPrinterInfo2 info2
;
779 struct spoolss_DevmodeContainer devmode_ctr
;
780 struct sec_desc_buf secdesc_ctr
;
782 ZERO_STRUCT(devmode_ctr
);
783 ZERO_STRUCT(secdesc_ctr
);
785 /* setprinter call */
787 info_ctr
.level
= level
;
790 info_ctr
.info
.info0
= (struct spoolss_SetPrinterInfo0
*)
791 (void *)&info
->info0
;
794 info_ctr
.info
.info1
= (struct spoolss_SetPrinterInfo1
*)
795 (void *)&info
->info1
;
798 spoolss_printerinfo2_to_setprinterinfo2(&info
->info2
, &info2
);
799 info_ctr
.info
.info2
= &info2
;
802 info_ctr
.info
.info3
= (struct spoolss_SetPrinterInfo3
*)
803 (void *)&info
->info3
;
806 info_ctr
.info
.info4
= (struct spoolss_SetPrinterInfo4
*)
807 (void *)&info
->info4
;
810 info_ctr
.info
.info5
= (struct spoolss_SetPrinterInfo5
*)
811 (void *)&info
->info5
;
814 info_ctr
.info
.info6
= (struct spoolss_SetPrinterInfo6
*)
815 (void *)&info
->info6
;
818 info_ctr
.info
.info7
= (struct spoolss_SetPrinterInfo7
*)
819 (void *)&info
->info7
;
823 info_ctr
.info
.info8
= (struct spoolss_SetPrinterInfo8
*)
824 (void *)&info
->info8
;
827 info_ctr
.info
.info9
= (struct spoolss_SetPrinterInfo9
*)
828 (void *)&info
->info9
;
835 status
= dcerpc_spoolss_SetPrinter(b
, mem_ctx
,
842 if (!NT_STATUS_IS_OK(status
)) {
843 printf(_("cannot set printer-info: %s\n"), nt_errstr(status
));
846 if (!W_ERROR_IS_OK(result
)) {
847 printf(_("cannot set printer-info: %s\n"), win_errstr(result
));
855 static bool net_spoolss_setprinterdata(struct rpc_pipe_client
*pipe_hnd
,
857 struct policy_handle
*hnd
,
858 const char *value_name
,
859 enum winreg_Type type
,
863 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
867 /* setprinterdata call */
868 status
= dcerpc_spoolss_SetPrinterData(b
, mem_ctx
,
875 if (!NT_STATUS_IS_OK(status
)) {
876 printf (_("unable to set printerdata: %s\n"),
880 if (!W_ERROR_IS_OK(result
)) {
881 printf (_("unable to set printerdata: %s\n"),
890 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client
*pipe_hnd
,
892 struct policy_handle
*hnd
,
894 const char ***keylist
)
898 /* enumprinterkey call */
899 result
= rpccli_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, hnd
, keyname
, keylist
, 0);
901 if (!W_ERROR_IS_OK(result
)) {
902 printf(_("enumprinterkey failed: %s\n"), win_errstr(result
));
909 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
912 struct policy_handle
*hnd
,
915 struct spoolss_PrinterEnumValues
**info
)
919 /* enumprinterdataex call */
920 result
= rpccli_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
,
927 if (!W_ERROR_IS_OK(result
)) {
928 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result
));
936 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
938 struct policy_handle
*hnd
,
941 struct registry_value
*value
)
943 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
947 /* setprinterdataex call */
948 status
= dcerpc_spoolss_SetPrinterDataEx(b
, mem_ctx
,
956 if (!NT_STATUS_IS_OK(status
)) {
957 printf(_("could not set printerdataex: %s\n"),
961 if (!W_ERROR_IS_OK(result
)) {
962 printf(_("could not set printerdataex: %s\n"),
970 static bool net_spoolss_enumforms(struct rpc_pipe_client
*pipe_hnd
,
972 struct policy_handle
*hnd
,
975 union spoolss_FormInfo
**forms
)
980 result
= rpccli_spoolss_enumforms(pipe_hnd
, mem_ctx
,
986 if (!W_ERROR_IS_OK(result
)) {
987 printf(_("could not enum forms: %s\n"), win_errstr(result
));
994 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client
*pipe_hnd
,
996 uint32_t level
, const char *env
,
998 union spoolss_DriverInfo
**info
)
1002 /* enumprinterdrivers call */
1003 result
= rpccli_spoolss_enumprinterdrivers(pipe_hnd
, mem_ctx
,
1004 pipe_hnd
->srv_name_slash
,
1010 if (!W_ERROR_IS_OK(result
)) {
1011 if (W_ERROR_V(result
) != W_ERROR_V(WERR_INVALID_ENVIRONMENT
)) {
1012 printf(_("cannot enum drivers for environment %s: %s\n"), env
,
1013 win_errstr(result
));
1016 printf(_("Server does not support environment [%s]\n"),
1024 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1025 TALLOC_CTX
*mem_ctx
,
1026 struct policy_handle
*hnd
, uint32_t level
,
1027 const char *env
, int version
,
1028 union spoolss_DriverInfo
*info
)
1031 uint32_t server_major_version
;
1032 uint32_t server_minor_version
;
1034 /* getprinterdriver call */
1035 result
= rpccli_spoolss_getprinterdriver2(pipe_hnd
, mem_ctx
,
1043 &server_major_version
,
1044 &server_minor_version
);
1045 if (!W_ERROR_IS_OK(result
)) {
1046 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1047 env
, win_errstr(result
)));
1048 if (W_ERROR_V(result
) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER
) &&
1049 W_ERROR_V(result
) != W_ERROR_V(WERR_INVALID_ENVIRONMENT
)) {
1050 printf(_("cannot get driver: %s\n"),
1051 win_errstr(result
));
1060 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1061 TALLOC_CTX
*mem_ctx
, uint32_t level
,
1062 union spoolss_DriverInfo
*info
)
1064 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1067 struct spoolss_AddDriverInfoCtr info_ctr
;
1069 info_ctr
.level
= level
;
1073 info_ctr
.info
.info2
= (struct spoolss_AddDriverInfo2
*)
1074 (void *)&info
->info2
;
1077 info_ctr
.info
.info3
= (struct spoolss_AddDriverInfo3
*)
1078 (void *)&info
->info3
;
1081 printf(_("unsupported info level: %d\n"), level
);
1085 /* addprinterdriver call */
1086 status
= dcerpc_spoolss_AddPrinterDriver(b
, mem_ctx
,
1087 pipe_hnd
->srv_name_slash
,
1090 if (!NT_STATUS_IS_OK(status
)) {
1091 printf(_("cannot add driver: %s\n"), nt_errstr(status
));
1094 /* be more verbose */
1095 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
1096 printf(_("You are not allowed to add drivers\n"));
1099 if (!W_ERROR_IS_OK(result
)) {
1100 printf(_("cannot add driver: %s\n"), win_errstr(result
));
1108 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1109 * for a single printer or for all printers depending on argc/argv
1112 static bool get_printer_info(struct rpc_pipe_client
*pipe_hnd
,
1113 TALLOC_CTX
*mem_ctx
,
1117 uint32_t *num_printers
,
1118 union spoolss_PrinterInfo
**info_p
)
1120 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1121 struct policy_handle hnd
;
1124 /* no arguments given, enumerate all printers */
1127 if (!net_spoolss_enum_printers(pipe_hnd
, mem_ctx
, NULL
,
1128 PRINTER_ENUM_LOCAL
|PRINTER_ENUM_SHARED
,
1129 level
, num_printers
, info_p
))
1135 /* argument given, get a single printer by name */
1136 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, argv
[0],
1137 MAXIMUM_ALLOWED_ACCESS
,
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
;
1245 printf(_("listing printer-drivers\n"));
1247 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
1249 uint32_t d
, num_drivers
;
1251 /* enum remote drivers */
1252 if (!net_spoolss_enumprinterdrivers(pipe_hnd
, mem_ctx
, level
,
1253 archi_table
[i
].long_archi
,
1254 &num_drivers
, &info
)) {
1255 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1259 if (num_drivers
== 0) {
1260 d_printf(_("no drivers found on server for "
1261 "architecture: [%s].\n"),
1262 archi_table
[i
].long_archi
);
1266 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1267 num_drivers
, archi_table
[i
].long_archi
);
1270 /* do something for all drivers for architecture */
1271 for (d
= 0; d
< num_drivers
; d
++) {
1272 display_print_driver3(&info
[d
].info3
);
1276 nt_status
= NT_STATUS_OK
;
1284 * Publish print-queues with args-wrapper
1286 * @param cli A cli_state connected to the server.
1287 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1288 * @param argc Standard main() style argc
1289 * @param argv Standard main() style argv. Initial components are already
1293 * @return Normal NTSTATUS return.
1296 static NTSTATUS
rpc_printer_publish_internals_args(struct rpc_pipe_client
*pipe_hnd
,
1297 TALLOC_CTX
*mem_ctx
,
1302 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1303 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1304 uint32_t i
, num_printers
;
1306 const char *printername
, *sharename
;
1307 union spoolss_PrinterInfo
*info_enum
;
1308 union spoolss_PrinterInfo info
;
1309 struct spoolss_SetPrinterInfoCtr info_ctr
;
1310 struct spoolss_DevmodeContainer devmode_ctr
;
1311 struct sec_desc_buf secdesc_ctr
;
1312 struct policy_handle hnd
= { 0, };
1314 const char *action_str
;
1316 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
))
1319 for (i
= 0; i
< num_printers
; i
++) {
1321 /* do some initialization */
1322 printername
= info_enum
[i
].info2
.printername
;
1323 sharename
= info_enum
[i
].info2
.sharename
;
1324 if (!printername
|| !sharename
) {
1328 /* open printer handle */
1329 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1330 PRINTER_ALL_ACCESS
, &hnd
))
1333 /* check for existing dst printer */
1334 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &info
))
1337 /* check action and set string */
1339 case DSPRINT_PUBLISH
:
1340 action_str
= N_("published");
1342 case DSPRINT_UPDATE
:
1343 action_str
= N_("updated");
1345 case DSPRINT_UNPUBLISH
:
1346 action_str
= N_("unpublished");
1349 action_str
= N_("unknown action");
1350 printf(_("unknown action: %d\n"), action
);
1354 info
.info7
.action
= action
;
1356 info_ctr
.info
.info7
= (struct spoolss_SetPrinterInfo7
*)
1357 (void *)&info
.info7
;
1359 ZERO_STRUCT(devmode_ctr
);
1360 ZERO_STRUCT(secdesc_ctr
);
1362 nt_status
= dcerpc_spoolss_SetPrinter(b
, mem_ctx
,
1369 if (!NT_STATUS_IS_OK(nt_status
)) {
1370 printf(_("cannot set printer-info: %s\n"),
1371 nt_errstr(nt_status
));
1374 if (!W_ERROR_IS_OK(result
) && !W_ERROR_EQUAL(result
, WERR_IO_PENDING
)) {
1375 if ((action
== DSPRINT_UPDATE
) && W_ERROR_EQUAL(result
, W_ERROR(0x80070002))) {
1376 printf(_("printer not published yet\n"));
1378 printf(_("cannot set printer-info: %s\n"),
1379 win_errstr(result
));
1381 nt_status
= werror_to_ntstatus(result
);
1385 printf(_("successfully %s printer %s in Active Directory\n"),
1386 action_str
, sharename
);
1389 nt_status
= NT_STATUS_OK
;
1392 if (is_valid_policy_hnd(&hnd
)) {
1393 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &result
);
1399 NTSTATUS
rpc_printer_publish_publish_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_PUBLISH
);
1411 NTSTATUS
rpc_printer_publish_unpublish_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_UNPUBLISH
);
1423 NTSTATUS
rpc_printer_publish_update_internals(struct net_context
*c
,
1424 const struct dom_sid
*domain_sid
,
1425 const char *domain_name
,
1426 struct cli_state
*cli
,
1427 struct rpc_pipe_client
*pipe_hnd
,
1428 TALLOC_CTX
*mem_ctx
,
1432 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, DSPRINT_UPDATE
);
1436 * List print-queues w.r.t. their publishing state
1438 * All parameters are provided by the run_rpc_command function, except for
1439 * argc, argv which are passed through.
1441 * @param c A net_context structure
1442 * @param domain_sid The domain sid aquired from the remote server
1443 * @param cli A cli_state connected to the server.
1444 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1445 * @param argc Standard main() style argc
1446 * @param argv Standard main() style argv. Initial components are already
1449 * @return Normal NTSTATUS return.
1452 NTSTATUS
rpc_printer_publish_list_internals(struct net_context
*c
,
1453 const struct dom_sid
*domain_sid
,
1454 const char *domain_name
,
1455 struct cli_state
*cli
,
1456 struct rpc_pipe_client
*pipe_hnd
,
1457 TALLOC_CTX
*mem_ctx
,
1461 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1462 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1463 uint32_t i
, num_printers
;
1465 const char *printername
, *sharename
;
1466 union spoolss_PrinterInfo
*info_enum
;
1467 union spoolss_PrinterInfo info
;
1468 struct policy_handle hnd
= { 0, };
1472 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
))
1475 for (i
= 0; i
< num_printers
; i
++) {
1477 /* do some initialization */
1478 printername
= info_enum
[i
].info2
.printername
;
1479 sharename
= info_enum
[i
].info2
.sharename
;
1481 if (!printername
|| !sharename
) {
1485 /* open printer handle */
1486 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1487 PRINTER_ALL_ACCESS
, &hnd
))
1490 /* check for existing dst printer */
1491 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &info
))
1494 if (!info
.info7
.guid
) {
1497 state
= info
.info7
.action
;
1499 case DSPRINT_PUBLISH
:
1500 printf(_("printer [%s] is published"),
1503 printf(_(", guid: %s"),info
.info7
.guid
);
1506 case DSPRINT_UNPUBLISH
:
1507 printf(_("printer [%s] is unpublished\n"),
1510 case DSPRINT_UPDATE
:
1511 printf(_("printer [%s] is currently updating\n"),
1515 printf(_("unknown state: %d\n"), state
);
1520 nt_status
= NT_STATUS_OK
;
1523 if (is_valid_policy_hnd(&hnd
)) {
1524 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1531 * Migrate Printer-ACLs from a source server to the destination server
1533 * All parameters are provided by the run_rpc_command function, except for
1534 * argc, argv which are passed through.
1536 * @param c A net_context structure
1537 * @param domain_sid The domain sid aquired from the remote server
1538 * @param cli A cli_state connected to the server.
1539 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1540 * @param argc Standard main() style argc
1541 * @param argv Standard main() style argv. Initial components are already
1544 * @return Normal NTSTATUS return.
1547 NTSTATUS
rpc_printer_migrate_security_internals(struct net_context
*c
,
1548 const struct dom_sid
*domain_sid
,
1549 const char *domain_name
,
1550 struct cli_state
*cli
,
1551 struct rpc_pipe_client
*pipe_hnd
,
1552 TALLOC_CTX
*mem_ctx
,
1556 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1557 /* TODO: what now, info2 or info3 ?
1558 convince jerry that we should add clientside setacls level 3 at least
1560 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1562 uint32_t num_printers
;
1564 const char *printername
, *sharename
;
1565 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1566 struct dcerpc_binding_handle
*b_dst
= NULL
;
1567 struct policy_handle hnd_src
= { 0, };
1568 struct policy_handle hnd_dst
= { 0, };
1569 union spoolss_PrinterInfo
*info_enum
;
1570 struct cli_state
*cli_dst
= NULL
;
1571 union spoolss_PrinterInfo info_src
, info_dst
;
1574 DEBUG(3,("copying printer ACLs\n"));
1576 /* connect destination PI_SPOOLSS */
1577 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1578 &ndr_table_spoolss
);
1579 if (!NT_STATUS_IS_OK(nt_status
)) {
1582 b_dst
= pipe_hnd_dst
->binding_handle
;
1584 /* enum source printers */
1585 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
1586 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1590 if (!num_printers
) {
1591 printf (_("no printers found on server.\n"));
1592 nt_status
= NT_STATUS_OK
;
1596 /* do something for all printers */
1597 for (i
= 0; i
< num_printers
; i
++) {
1599 /* do some initialization */
1600 printername
= info_enum
[i
].info2
.printername
;
1601 sharename
= info_enum
[i
].info2
.sharename
;
1603 if (!printername
|| !sharename
) {
1604 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1608 /* we can reset NT_STATUS here because we do not
1609 get any real NT_STATUS-codes anymore from now on */
1610 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1612 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1613 printername
, sharename
);
1615 /* according to msdn you have specify these access-rights
1616 to see the security descriptor
1617 - READ_CONTROL (DACL)
1618 - ACCESS_SYSTEM_SECURITY (SACL)
1621 /* open src printer handle */
1622 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1623 MAXIMUM_ALLOWED_ACCESS
, &hnd_src
))
1626 /* open dst printer handle */
1627 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1628 PRINTER_ALL_ACCESS
, &hnd_dst
))
1631 /* check for existing dst printer */
1632 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
))
1635 /* check for existing src printer */
1636 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, 3, &info_src
))
1639 /* Copy Security Descriptor */
1641 /* copy secdesc (info level 2) */
1642 info_dst
.info2
.devmode
= NULL
;
1643 if (info_src
.info3
.secdesc
== NULL
) {
1644 info_dst
.info2
.secdesc
= NULL
;
1646 info_dst
.info2
.secdesc
1647 = security_descriptor_copy(mem_ctx
,
1648 info_src
.info3
.secdesc
);
1649 if (info_dst
.info2
.secdesc
== NULL
) {
1650 nt_status
= NT_STATUS_NO_MEMORY
;
1656 display_sec_desc(info_dst
.info2
.secdesc
);
1658 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
1661 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1664 /* close printer handles here */
1665 if (is_valid_policy_hnd(&hnd_src
)) {
1666 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
1669 if (is_valid_policy_hnd(&hnd_dst
)) {
1670 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
1675 nt_status
= NT_STATUS_OK
;
1679 if (is_valid_policy_hnd(&hnd_src
)) {
1680 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
1683 if (is_valid_policy_hnd(&hnd_dst
)) {
1684 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
1688 cli_shutdown(cli_dst
);
1694 * Migrate printer-forms from a src server to the dst server
1696 * All parameters are provided by the run_rpc_command function, except for
1697 * argc, argv which are passed through.
1699 * @param c A net_context structure
1700 * @param domain_sid The domain sid aquired from the remote server
1701 * @param cli A cli_state connected to the server.
1702 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1703 * @param argc Standard main() style argc
1704 * @param argv Standard main() style argv. Initial components are already
1707 * @return Normal NTSTATUS return.
1710 NTSTATUS
rpc_printer_migrate_forms_internals(struct net_context
*c
,
1711 const struct dom_sid
*domain_sid
,
1712 const char *domain_name
,
1713 struct cli_state
*cli
,
1714 struct rpc_pipe_client
*pipe_hnd
,
1715 TALLOC_CTX
*mem_ctx
,
1719 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1720 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1723 uint32_t num_printers
;
1725 const char *printername
, *sharename
;
1726 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1727 struct dcerpc_binding_handle
*b_dst
= NULL
;
1728 struct policy_handle hnd_src
= { 0, };
1729 struct policy_handle hnd_dst
= { 0, };
1730 union spoolss_PrinterInfo
*info_enum
;
1731 union spoolss_PrinterInfo info_dst
;
1733 union spoolss_FormInfo
*forms
;
1734 struct cli_state
*cli_dst
= NULL
;
1736 DEBUG(3,("copying forms\n"));
1738 /* connect destination PI_SPOOLSS */
1739 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1740 &ndr_table_spoolss
);
1741 if (!NT_STATUS_IS_OK(nt_status
)) {
1744 b_dst
= pipe_hnd_dst
->binding_handle
;
1746 /* enum src printers */
1747 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1748 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1752 if (!num_printers
) {
1753 printf (_("no printers found on server.\n"));
1754 nt_status
= NT_STATUS_OK
;
1758 /* do something for all printers */
1759 for (i
= 0; i
< num_printers
; i
++) {
1761 /* do some initialization */
1762 printername
= info_enum
[i
].info2
.printername
;
1763 sharename
= info_enum
[i
].info2
.sharename
;
1765 if (!printername
|| !sharename
) {
1766 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1769 /* we can reset NT_STATUS here because we do not
1770 get any real NT_STATUS-codes anymore from now on */
1771 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1773 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1774 printername
, sharename
);
1777 /* open src printer handle */
1778 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1779 MAXIMUM_ALLOWED_ACCESS
, &hnd_src
))
1782 /* open dst printer handle */
1783 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1784 PRINTER_ALL_ACCESS
, &hnd_dst
))
1787 /* check for existing dst printer */
1788 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
))
1791 /* finally migrate forms */
1792 if (!net_spoolss_enumforms(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &num_forms
, &forms
))
1795 DEBUG(1,("got %d forms for printer\n", num_forms
));
1798 for (f
= 0; f
< num_forms
; f
++) {
1800 struct spoolss_AddFormInfoCtr info_ctr
;
1803 /* only migrate FORM_PRINTER types, according to jerry
1804 FORM_BUILTIN-types are hard-coded in samba */
1805 if (forms
[f
].info1
.flags
!= SPOOLSS_FORM_PRINTER
)
1809 d_printf(_("\tmigrating form # %d [%s] of type "
1811 f
, forms
[f
].info1
.form_name
,
1812 forms
[f
].info1
.flags
);
1814 info_ctr
.info
.info1
= (struct spoolss_AddFormInfo1
*)
1815 (void *)&forms
[f
].info1
;
1817 /* FIXME: there might be something wrong with samba's
1819 status
= dcerpc_spoolss_AddForm(b_dst
, mem_ctx
,
1823 if (!NT_STATUS_IS_OK(status
)) {
1824 d_printf(_("\tdcerpc_spoolss_AddForm form %d: [%s] - %s\n"),
1825 f
, forms
[f
].info1
.form_name
, nt_errstr(status
));
1828 if (!W_ERROR_IS_OK(result
)) {
1829 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1830 f
, forms
[f
].info1
.form_name
);
1834 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1835 forms
[f
].info1
.form_name
));
1839 /* close printer handles here */
1840 if (is_valid_policy_hnd(&hnd_src
)) {
1841 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
1844 if (is_valid_policy_hnd(&hnd_dst
)) {
1845 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
1849 nt_status
= NT_STATUS_OK
;
1853 if (is_valid_policy_hnd(&hnd_src
)) {
1854 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
1857 if (is_valid_policy_hnd(&hnd_dst
)) {
1858 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
1862 cli_shutdown(cli_dst
);
1868 * Migrate printer-drivers from a src server to the dst server
1870 * All parameters are provided by the run_rpc_command function, except for
1871 * argc, argv which are passed through.
1873 * @param c A net_context structure
1874 * @param domain_sid The domain sid aquired from the remote server
1875 * @param cli A cli_state connected to the server.
1876 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1877 * @param argc Standard main() style argc
1878 * @param argv Standard main() style argv. Initial components are already
1881 * @return Normal NTSTATUS return.
1884 NTSTATUS
rpc_printer_migrate_drivers_internals(struct net_context
*c
,
1885 const struct dom_sid
*domain_sid
,
1886 const char *domain_name
,
1887 struct cli_state
*cli
,
1888 struct rpc_pipe_client
*pipe_hnd
,
1889 TALLOC_CTX
*mem_ctx
,
1893 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1894 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1896 uint32_t num_printers
;
1898 const char *printername
, *sharename
;
1899 bool got_src_driver_share
= false;
1900 bool got_dst_driver_share
= false;
1901 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1902 struct dcerpc_binding_handle
*b_dst
= NULL
;
1903 struct policy_handle hnd_src
= { 0, };
1904 struct policy_handle hnd_dst
= { 0, };
1905 union spoolss_DriverInfo drv_info_src
;
1906 union spoolss_PrinterInfo
*info_enum
;
1907 union spoolss_PrinterInfo info_dst
;
1908 struct cli_state
*cli_dst
= NULL
;
1909 struct cli_state
*cli_share_src
= NULL
;
1910 struct cli_state
*cli_share_dst
= NULL
;
1911 const char *drivername
= NULL
;
1914 DEBUG(3,("copying printer-drivers\n"));
1916 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1917 &ndr_table_spoolss
);
1918 if (!NT_STATUS_IS_OK(nt_status
)) {
1921 b_dst
= pipe_hnd_dst
->binding_handle
;
1923 /* open print$-share on the src server */
1924 nt_status
= connect_to_service(c
, &cli_share_src
,
1925 smbXcli_conn_remote_sockaddr(cli
->conn
),
1926 smbXcli_conn_remote_name(cli
->conn
),
1928 if (!NT_STATUS_IS_OK(nt_status
))
1931 got_src_driver_share
= true;
1934 /* open print$-share on the dst server */
1935 nt_status
= connect_to_service(c
, &cli_share_dst
,
1936 smbXcli_conn_remote_sockaddr(cli_dst
->conn
),
1937 smbXcli_conn_remote_name(cli_dst
->conn
),
1939 if (!NT_STATUS_IS_OK(nt_status
))
1942 got_dst_driver_share
= true;
1945 /* enum src printers */
1946 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1947 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1951 if (num_printers
== 0) {
1952 printf (_("no printers found on server.\n"));
1953 nt_status
= NT_STATUS_OK
;
1958 /* do something for all printers */
1959 for (p
= 0; p
< num_printers
; p
++) {
1961 /* do some initialization */
1962 printername
= info_enum
[p
].info2
.printername
;
1963 sharename
= info_enum
[p
].info2
.sharename
;
1965 if (!printername
|| !sharename
) {
1966 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1970 /* we can reset NT_STATUS here because we do not
1971 get any real NT_STATUS-codes anymore from now on */
1972 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1974 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1975 printername
, sharename
);
1977 /* open dst printer handle */
1978 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1979 PRINTER_ALL_ACCESS
, &hnd_dst
))
1982 /* check for existing dst printer */
1983 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
1987 /* open src printer handle */
1988 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1989 MAXIMUM_ALLOWED_ACCESS
,
1993 /* in a first step call getdriver for each shared printer (per arch)
1994 to get a list of all files that have to be copied */
1996 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
1999 if (!net_spoolss_getprinterdriver(pipe_hnd
, mem_ctx
, &hnd_src
,
2000 level
, archi_table
[i
].long_archi
,
2001 archi_table
[i
].version
, &drv_info_src
))
2004 drivername
= drv_info_src
.info3
.driver_name
;
2007 display_print_driver3(&drv_info_src
.info3
);
2009 /* check arch dir */
2010 nt_status
= check_arch_dir(cli_share_dst
, archi_table
[i
].short_archi
);
2011 if (!NT_STATUS_IS_OK(nt_status
))
2015 /* copy driver-files */
2016 nt_status
= copy_print_driver_3(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
2017 archi_table
[i
].short_archi
,
2018 &drv_info_src
.info3
);
2019 if (!NT_STATUS_IS_OK(nt_status
))
2024 if (!net_spoolss_addprinterdriver(pipe_hnd_dst
, mem_ctx
, level
, &drv_info_src
)) {
2025 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2029 DEBUGADD(1,("Successfully added driver [%s] for printer [%s]\n",
2030 drivername
, printername
));
2034 if (!drivername
|| strlen(drivername
) == 0) {
2035 DEBUGADD(1,("Did not get driver for printer %s\n",
2041 info_dst
.info2
.drivername
= drivername
;
2043 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
)) {
2044 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2048 DEBUGADD(1,("Successfully set driver %s for printer %s\n",
2049 drivername
, printername
));
2052 if (is_valid_policy_hnd(&hnd_dst
)) {
2053 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2057 if (is_valid_policy_hnd(&hnd_src
)) {
2058 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2062 nt_status
= NT_STATUS_OK
;
2066 if (is_valid_policy_hnd(&hnd_dst
)) {
2067 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2071 if (is_valid_policy_hnd(&hnd_src
)) {
2072 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2076 cli_shutdown(cli_dst
);
2079 if (got_src_driver_share
)
2080 cli_shutdown(cli_share_src
);
2082 if (got_dst_driver_share
)
2083 cli_shutdown(cli_share_dst
);
2090 * Migrate printer-queues from a src to the dst server
2091 * (requires a working "addprinter command" to be installed for the local smbd)
2093 * All parameters are provided by the run_rpc_command function, except for
2094 * argc, argv which are passed through.
2096 * @param c A net_context structure
2097 * @param domain_sid The domain sid aquired from the remote server
2098 * @param cli A cli_state connected to the server.
2099 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2100 * @param argc Standard main() style argc
2101 * @param argv Standard main() style argv. Initial components are already
2104 * @return Normal NTSTATUS return.
2107 NTSTATUS
rpc_printer_migrate_printers_internals(struct net_context
*c
,
2108 const struct dom_sid
*domain_sid
,
2109 const char *domain_name
,
2110 struct cli_state
*cli
,
2111 struct rpc_pipe_client
*pipe_hnd
,
2112 TALLOC_CTX
*mem_ctx
,
2116 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2118 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2119 uint32_t i
= 0, num_printers
;
2121 union spoolss_PrinterInfo info_dst
, info_src
;
2122 union spoolss_PrinterInfo
*info_enum
;
2123 struct cli_state
*cli_dst
= NULL
;
2124 struct policy_handle hnd_src
= { 0, };
2125 struct policy_handle hnd_dst
= { 0, };
2126 const char *printername
, *sharename
;
2127 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2128 struct dcerpc_binding_handle
*b_dst
= NULL
;
2129 struct spoolss_SetPrinterInfoCtr info_ctr
;
2131 DEBUG(3,("copying printers\n"));
2133 /* connect destination PI_SPOOLSS */
2134 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2135 &ndr_table_spoolss
);
2136 if (!NT_STATUS_IS_OK(nt_status
)) {
2139 b_dst
= pipe_hnd_dst
->binding_handle
;
2142 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2143 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2147 if (!num_printers
) {
2148 printf (_("no printers found on server.\n"));
2149 nt_status
= NT_STATUS_OK
;
2153 /* do something for all printers */
2154 for (i
= 0; i
< num_printers
; i
++) {
2156 struct spoolss_SetPrinterInfo2 info2
;
2158 /* do some initialization */
2159 printername
= info_enum
[i
].info2
.printername
;
2160 sharename
= info_enum
[i
].info2
.sharename
;
2162 if (!printername
|| !sharename
) {
2163 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2166 /* we can reset NT_STATUS here because we do not
2167 get any real NT_STATUS-codes anymore from now on */
2168 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2170 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2171 printername
, sharename
);
2173 /* open dst printer handle */
2174 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2175 PRINTER_ALL_ACCESS
, &hnd_dst
)) {
2177 DEBUG(1,("could not open printer: %s\n", sharename
));
2180 /* check for existing dst printer */
2181 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
)) {
2182 printf (_("could not get printer, creating printer.\n"));
2184 DEBUG(1,("printer already exists: %s\n", sharename
));
2185 /* close printer handle here - dst only, not got src yet. */
2186 if (is_valid_policy_hnd(&hnd_dst
)) {
2187 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2192 /* now get again src printer ctr via getprinter,
2193 we first need a handle for that */
2195 /* open src printer handle */
2196 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2197 MAXIMUM_ALLOWED_ACCESS
, &hnd_src
))
2200 /* getprinter on the src server */
2201 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &info_src
))
2204 /* copy each src printer to a dst printer 1:1,
2205 maybe some values have to be changed though */
2206 d_printf(_("creating printer: %s\n"), printername
);
2208 info_ctr
.level
= level
;
2209 spoolss_printerinfo2_to_setprinterinfo2(&info_src
.info2
, &info2
);
2210 info_ctr
.info
.info2
= &info2
;
2212 result
= rpccli_spoolss_addprinterex(pipe_hnd_dst
,
2216 if (W_ERROR_IS_OK(result
))
2217 d_printf (_("printer [%s] successfully added.\n"),
2219 else if (W_ERROR_V(result
) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS
))
2220 d_fprintf (stderr
, _("printer [%s] already exists.\n"),
2223 d_fprintf (stderr
, _("could not create printer [%s]\n"),
2228 /* close printer handles here */
2229 if (is_valid_policy_hnd(&hnd_src
)) {
2230 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2233 if (is_valid_policy_hnd(&hnd_dst
)) {
2234 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2238 nt_status
= NT_STATUS_OK
;
2241 if (is_valid_policy_hnd(&hnd_src
)) {
2242 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2245 if (is_valid_policy_hnd(&hnd_dst
)) {
2246 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2250 cli_shutdown(cli_dst
);
2256 * Migrate Printer-Settings from a src server to the dst server
2257 * (for this to work, printers and drivers already have to be migrated earlier)
2259 * All parameters are provided by the run_rpc_command function, except for
2260 * argc, argv which are passed through.
2262 * @param c A net_context structure
2263 * @param domain_sid The domain sid aquired from the remote server
2264 * @param cli A cli_state connected to the server.
2265 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2266 * @param argc Standard main() style argc
2267 * @param argv Standard main() style argv. Initial components are already
2270 * @return Normal NTSTATUS return.
2273 NTSTATUS
rpc_printer_migrate_settings_internals(struct net_context
*c
,
2274 const struct dom_sid
*domain_sid
,
2275 const char *domain_name
,
2276 struct cli_state
*cli
,
2277 struct rpc_pipe_client
*pipe_hnd
,
2278 TALLOC_CTX
*mem_ctx
,
2282 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2284 /* FIXME: Here the nightmare begins */
2287 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2288 uint32_t i
= 0, j
= 0;
2289 uint32_t num_printers
;
2291 const char *printername
, *sharename
;
2292 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2293 struct dcerpc_binding_handle
*b_dst
= NULL
;
2294 struct policy_handle hnd_src
= { 0, };
2295 struct policy_handle hnd_dst
= { 0, };
2296 union spoolss_PrinterInfo
*info_enum
;
2297 union spoolss_PrinterInfo info_dst_publish
;
2298 union spoolss_PrinterInfo info_dst
;
2299 struct cli_state
*cli_dst
= NULL
;
2300 const char *longname
;
2301 const char **keylist
= NULL
;
2304 ZERO_STRUCT(info_dst_publish
);
2306 DEBUG(3,("copying printer settings\n"));
2308 /* connect destination PI_SPOOLSS */
2309 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2310 &ndr_table_spoolss
);
2311 if (!NT_STATUS_IS_OK(nt_status
)) {
2314 b_dst
= pipe_hnd_dst
->binding_handle
;
2316 /* enum src printers */
2317 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2318 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2322 if (!num_printers
) {
2323 printf (_("no printers found on server.\n"));
2324 nt_status
= NT_STATUS_OK
;
2329 /* needed for dns-strings in regkeys */
2330 longname
= get_mydnsfullname();
2332 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2336 /* do something for all printers */
2337 for (i
= 0; i
< num_printers
; i
++) {
2339 uint32_t value_needed
;
2340 uint32_t data_needed
;
2341 enum winreg_Type type
;
2342 struct spoolss_EnumPrinterData r
;
2344 /* do some initialization */
2345 printername
= info_enum
[i
].info2
.printername
;
2346 sharename
= info_enum
[i
].info2
.sharename
;
2348 if (!printername
|| !sharename
) {
2349 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2352 /* we can reset NT_STATUS here because we do not
2353 get any real NT_STATUS-codes anymore from now on */
2354 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2356 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2357 printername
, sharename
);
2360 /* open src printer handle */
2361 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2362 MAXIMUM_ALLOWED_ACCESS
, &hnd_src
))
2365 /* open dst printer handle */
2366 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2367 PRINTER_ALL_ACCESS
, &hnd_dst
))
2370 /* check for existing dst printer */
2371 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2376 /* STEP 1: COPY DEVICE-MODE and other
2377 PRINTER_INFO_2-attributes
2380 info_dst
.info2
= info_enum
[i
].info2
;
2382 /* why is the port always disconnected when the printer
2383 is correctly installed (incl. driver ???) */
2384 info_dst
.info2
.portname
= SAMBA_PRINTER_PORT_NAME
;
2386 /* check if printer is published */
2387 if (info_enum
[i
].info2
.attributes
& PRINTER_ATTRIBUTE_PUBLISHED
) {
2389 /* check for existing dst printer */
2390 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
))
2393 info_dst_publish
.info7
.action
= DSPRINT_PUBLISH
;
2395 /* ignore false from setprinter due to WERR_IO_PENDING */
2396 net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
);
2398 DEBUG(3,("republished printer\n"));
2401 if (info_enum
[i
].info2
.devmode
!= NULL
) {
2403 /* copy devmode (info level 2) */
2404 info_dst
.info2
.devmode
= info_enum
[i
].info2
.devmode
;
2406 /* do not copy security descriptor (we have another
2407 * command for that) */
2408 info_dst
.info2
.secdesc
= NULL
;
2411 info_dst
.info2
.devmode
.devicename
=
2412 talloc_asprintf(mem_ctx
, "\\\\%s\\%s",
2413 longname
, printername
);
2414 if (!info_dst
.info2
.devmode
.devicename
) {
2415 nt_status
= NT_STATUS_NO_MEMORY
;
2419 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2423 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2426 /* STEP 2: COPY REGISTRY VALUES */
2428 /* please keep in mind that samba parse_spools gives horribly
2429 crippled results when used to rpccli_spoolss_enumprinterdataex
2430 a win2k3-server. (Bugzilla #1851)
2431 FIXME: IIRC I've seen it too on a win2k-server
2434 r
.in
.handle
= &hnd_src
;
2435 r
.in
.enum_index
= 0;
2436 r
.in
.value_offered
= 0;
2437 r
.in
.data_offered
= 0;
2438 r
.out
.value_name
= NULL
;
2439 r
.out
.value_needed
= &value_needed
;
2442 r
.out
.data_needed
= &data_needed
;
2444 /* enumerate data on src handle */
2445 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2447 r
.in
.data_offered
= *r
.out
.data_needed
;
2448 r
.in
.value_offered
= *r
.out
.value_needed
;
2449 r
.out
.data
= talloc_zero_array(mem_ctx
, uint8_t, r
.in
.data_offered
);
2450 r
.out
.value_name
= talloc_zero_array(mem_ctx
, char, r
.in
.value_offered
);
2452 /* loop for all printerdata of "PrinterDriverData" */
2453 while (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2457 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2459 /* loop for all reg_keys */
2460 if (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2463 if (c
->opt_verbose
) {
2464 struct registry_value v
;
2465 v
.type
= *r
.out
.type
;
2466 v
.data
= data_blob_const(
2467 r
.out
.data
, r
.in
.data_offered
);
2469 display_reg_value(SPOOL_PRINTERDATA_KEY
,
2470 r
.out
.value_name
, &v
);
2474 if (!net_spoolss_setprinterdata(pipe_hnd_dst
, mem_ctx
,
2475 &hnd_dst
, r
.out
.value_name
,
2476 *r
.out
.type
, r
.out
.data
, r
.in
.data_offered
))
2479 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2484 /* STEP 3: COPY SUBKEY VALUES */
2486 /* here we need to enum all printer_keys and then work
2487 on the result with enum_printer_key_ex. nt4 does not
2488 respond to enumprinterkey, win2k does, so continue
2489 in case of an error */
2491 if (!net_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, &hnd_src
, "", &keylist
)) {
2492 printf(_("got no key-data\n"));
2497 /* work on a list of printer keys
2498 each key has to be enumerated to get all required
2499 information. information is then set via setprinterdataex-calls */
2501 if (keylist
== NULL
)
2504 for (i
=0; keylist
&& keylist
[i
] != NULL
; i
++) {
2506 const char *subkey
= keylist
[i
];
2508 struct spoolss_PrinterEnumValues
*info
;
2510 /* enumerate all src subkeys */
2511 if (!net_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
, 0,
2517 for (j
=0; j
< count
; j
++) {
2519 struct registry_value value
;
2520 const char *value_name
= info
[j
].value_name
;
2523 value
.type
= REG_SZ
;
2525 /* although samba replies with sane data in most cases we
2526 should try to avoid writing wrong registry data */
2528 if (strequal(value_name
, SPOOL_REG_PORTNAME
)) {
2529 /* although windows uses a multi-sz, we use a sz */
2530 ok
= push_reg_sz(mem_ctx
, &value
.data
, SAMBA_PRINTER_PORT_NAME
);
2532 nt_status
= NT_STATUS_NO_MEMORY
;
2536 else if (strequal(value_name
, SPOOL_REG_UNCNAME
)) {
2538 if (asprintf(&unc_name
, "\\\\%s\\%s", longname
, sharename
) < 0) {
2539 nt_status
= NT_STATUS_NO_MEMORY
;
2542 ok
= push_reg_sz(mem_ctx
, &value
.data
, unc_name
);
2544 nt_status
= NT_STATUS_NO_MEMORY
;
2549 else if (strequal(value_name
, SPOOL_REG_URL
)) {
2552 /* FIXME: should we really do that ??? */
2553 if (asprintf(&url
, "http://%s:631/printers/%s", longname
, sharename
) < 0) {
2554 nt_status
= NT_STATUS_NO_MEMORY
;
2557 push_reg_sz(mem_ctx
, NULL
, &value
.data
, url
);
2561 else if (strequal(value_name
, SPOOL_REG_SERVERNAME
)) {
2562 ok
= push_reg_sz(mem_ctx
, &value
.data
, longname
);
2564 nt_status
= NT_STATUS_NO_MEMORY
;
2568 else if (strequal(value_name
, SPOOL_REG_SHORTSERVERNAME
)) {
2569 ok
= push_reg_sz(mem_ctx
, &value
.data
, lp_netbios_name());
2571 nt_status
= NT_STATUS_NO_MEMORY
;
2576 value
.type
= info
[j
].type
;
2577 value
.data
= *info
[j
].data
;
2580 if (c
->opt_verbose
) {
2581 display_reg_value(subkey
, value_name
, &value
);
2584 /* here we have to set all subkeys on the dst server */
2585 if (!net_spoolss_setprinterdataex(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2586 subkey
, value_name
, &value
))
2591 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2592 subkey
, info
[j
].value_name
));
2597 TALLOC_FREE(keylist
);
2599 /* close printer handles here */
2600 if (is_valid_policy_hnd(&hnd_src
)) {
2601 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2604 if (is_valid_policy_hnd(&hnd_dst
)) {
2605 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2609 nt_status
= NT_STATUS_OK
;
2612 if (is_valid_policy_hnd(&hnd_src
)) {
2613 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2616 if (is_valid_policy_hnd(&hnd_dst
)) {
2617 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2621 cli_shutdown(cli_dst
);