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"
36 /* support itanium as well */
37 static const struct print_architecture_table_node archi_table
[]= {
39 {"Windows 4.0", "WIN40", 0 },
40 {"Windows NT x86", "W32X86", 2 },
41 {"Windows NT x86", "W32X86", 3 },
42 {"Windows NT R4000", "W32MIPS", 2 },
43 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
44 {"Windows NT PowerPC", "W32PPC", 2 },
45 {"Windows IA64", "IA64", 3 },
46 {"Windows x64", "x64", 3 },
52 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
53 * It is here for debugging purpose and should be removed later on.
56 /****************************************************************************
57 Printer info level 3 display function.
58 ****************************************************************************/
60 static void display_print_driver3(struct spoolss_DriverInfo3
*r
)
68 printf(_("Printer Driver Info 3:\n"));
69 printf(_("\tVersion: [%x]\n"), r
->version
);
70 printf(_("\tDriver Name: [%s]\n"), r
->driver_name
);
71 printf(_("\tArchitecture: [%s]\n"), r
->architecture
);
72 printf(_("\tDriver Path: [%s]\n"), r
->driver_path
);
73 printf(_("\tDatafile: [%s]\n"), r
->data_file
);
74 printf(_("\tConfigfile: [%s]\n\n"), r
->config_file
);
75 printf(_("\tHelpfile: [%s]\n\n"), r
->help_file
);
77 for (i
=0; r
->dependent_files
&& r
->dependent_files
[i
] != NULL
; i
++) {
78 printf(_("\tDependentfiles: [%s]\n"), r
->dependent_files
[i
]);
83 printf(_("\tMonitorname: [%s]\n"), r
->monitor_name
);
84 printf(_("\tDefaultdatatype: [%s]\n\n"), r
->default_datatype
);
87 static void display_reg_value(const char *subkey
, const char *name
, struct registry_value
*value
)
93 if (value
->data
.length
== sizeof(uint32_t)) {
94 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey
,
95 name
, IVAL(value
->data
.data
,0));
97 d_printf(_("\t[%s:%s]: REG_DWORD: <invalid>\n"), subkey
,
103 pull_reg_sz(talloc_tos(), &value
->data
, &text
);
107 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey
, name
, text
);
111 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
120 if (!pull_reg_multi_sz(NULL
, &value
->data
, &values
)) {
121 d_printf("pull_reg_multi_sz failed\n");
125 printf("%s: REG_MULTI_SZ: \n", name
);
126 for (i
=0; values
[i
] != NULL
; i
++) {
127 d_printf("%s\n", values
[i
]);
134 d_printf(_("\t%s: unknown type %d\n"), name
, value
->type
);
140 * Copies ACLs, DOS-attributes and timestamps from one
141 * file or directory from one connected share to another connected share
143 * @param c A net_context structure
144 * @param mem_ctx A talloc-context
145 * @param cli_share_src A connected cli_state
146 * @param cli_share_dst A connected cli_state
147 * @param src_file The source file-name
148 * @param dst_file The destination file-name
149 * @param copy_acls Whether to copy acls
150 * @param copy_attrs Whether to copy DOS attributes
151 * @param copy_timestamps Whether to preserve timestamps
152 * @param is_file Whether this file is a file or a dir
154 * @return Normal NTSTATUS return.
157 NTSTATUS
net_copy_fileattr(struct net_context
*c
,
159 struct cli_state
*cli_share_src
,
160 struct cli_state
*cli_share_dst
,
161 const char *src_name
, const char *dst_name
,
162 bool copy_acls
, bool copy_attrs
,
163 bool copy_timestamps
, bool is_file
)
166 uint16_t fnum_src
= 0;
167 uint16_t fnum_dst
= 0;
168 struct security_descriptor
*sd
= NULL
;
169 uint32_t attr
= (uint32_t)-1;
170 struct timespec f_create_time
= { .tv_nsec
= SAMBA_UTIME_OMIT
};
171 struct timespec f_access_time
= { .tv_nsec
= SAMBA_UTIME_OMIT
};
172 struct timespec f_write_time
= { .tv_nsec
= SAMBA_UTIME_OMIT
};
173 struct timespec f_change_time
= { .tv_nsec
= SAMBA_UTIME_OMIT
};
175 if (!copy_timestamps
&& !copy_acls
&& !copy_attrs
)
178 /* open file/dir on the originating server */
180 DEBUGADD(3,("opening %s %s on originating server\n",
181 is_file
?"file":"dir", src_name
));
183 nt_status
= cli_ntcreate(cli_share_src
, src_name
, 0,
184 READ_CONTROL_ACCESS
, 0,
185 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
186 0x0, 0x0, &fnum_src
, NULL
);
187 if (!NT_STATUS_IS_OK(nt_status
)) {
188 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
189 is_file
?"file":"dir", src_name
, nt_errstr(nt_status
)));
194 /* get the security descriptor */
195 nt_status
= cli_query_secdesc(cli_share_src
, fnum_src
,
197 if (!NT_STATUS_IS_OK(nt_status
)) {
198 DEBUG(0,("failed to get security descriptor: %s\n",
199 nt_errstr(nt_status
)));
203 if (c
->opt_verbose
&& DEBUGLEVEL
>= 3)
204 display_sec_desc(sd
);
207 if (copy_attrs
|| copy_timestamps
) {
209 /* get file attributes */
210 nt_status
= cli_qfileinfo_basic(
213 copy_attrs
? &attr
: NULL
,
215 copy_timestamps
? &f_create_time
: NULL
,
216 copy_timestamps
? &f_access_time
: NULL
,
217 copy_timestamps
? &f_write_time
: NULL
,
218 copy_timestamps
? &f_change_time
: NULL
,
220 if (!NT_STATUS_IS_OK(nt_status
)) {
221 DEBUG(0,("failed to get file-attrs: %s\n",
222 nt_errstr(nt_status
)));
227 /* open the file/dir on the destination server */
228 nt_status
= cli_ntcreate(cli_share_dst
, dst_name
, 0,
229 WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
, 0,
230 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
231 0x0, 0x0, &fnum_dst
, NULL
);
232 if (!NT_STATUS_IS_OK(nt_status
)) {
233 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
234 is_file
?"file":"dir", dst_name
, nt_errstr(nt_status
)));
240 nt_status
= cli_set_secdesc(cli_share_dst
, fnum_dst
, sd
);
241 if (!NT_STATUS_IS_OK(nt_status
)) {
242 DEBUG(0, ("could not set secdesc on %s: %s\n",
243 dst_name
, nt_errstr(nt_status
)));
248 if (copy_timestamps
|| copy_attrs
) {
250 nt_status
= cli_setfileinfo_ext(
258 if (!NT_STATUS_IS_OK(nt_status
)) {
259 DBG_ERR("failed to set file-attrs: %s\n",
260 nt_errstr(nt_status
));
267 nt_status
= cli_close(cli_share_src
, fnum_src
);
268 if (!NT_STATUS_IS_OK(nt_status
)) {
270 _("could not close %s on originating server: %s\n"),
271 is_file
?"file":"dir", nt_errstr(nt_status
));
275 nt_status
= cli_close(cli_share_dst
, fnum_dst
);
276 if (!NT_STATUS_IS_OK(nt_status
)) {
278 _("could not close %s on destination server: %s\n"),
279 is_file
?"file":"dir", nt_errstr(nt_status
));
284 nt_status
= NT_STATUS_OK
;
290 cli_close(cli_share_src
, fnum_src
);
293 cli_close(cli_share_dst
, fnum_dst
);
299 * Copy a file or directory from a connected share to another connected share
301 * @param c A net_context structure
302 * @param mem_ctx A talloc-context
303 * @param cli_share_src A connected cli_state
304 * @param cli_share_dst A connected cli_state
305 * @param src_file The source file-name
306 * @param dst_file The destination file-name
307 * @param copy_acls Whether to copy acls
308 * @param copy_attrs Whether to copy DOS attributes
309 * @param copy_timestamps Whether to preserve timestamps
310 * @param is_file Whether this file is a file or a dir
312 * @return Normal NTSTATUS return.
315 NTSTATUS
net_copy_file(struct net_context
*c
,
317 struct cli_state
*cli_share_src
,
318 struct cli_state
*cli_share_dst
,
319 const char *src_name
, const char *dst_name
,
320 bool copy_acls
, bool copy_attrs
,
321 bool copy_timestamps
, bool is_file
)
323 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
324 uint16_t fnum_src
= 0;
325 uint16_t fnum_dst
= 0;
326 static int io_bufsize
= 64512;
327 int read_size
= io_bufsize
;
332 if (!src_name
|| !dst_name
)
335 if (cli_share_src
== NULL
|| cli_share_dst
== NULL
)
338 /* open on the originating server */
339 DEBUGADD(3,("opening %s %s on originating server\n",
340 is_file
? "file":"dir", src_name
));
342 nt_status
= cli_open(cli_share_src
, src_name
, O_RDONLY
, DENY_NONE
, &fnum_src
);
344 nt_status
= cli_ntcreate(cli_share_src
, src_name
, 0, READ_CONTROL_ACCESS
, 0,
345 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
346 FILE_OPEN
, 0x0, 0x0, &fnum_src
, NULL
);
348 if (!NT_STATUS_IS_OK(nt_status
)) {
349 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
350 is_file
? "file":"dir",
351 src_name
, nt_errstr(nt_status
)));
358 /* open file on the destination server */
359 DEBUGADD(3,("opening file %s on destination server\n", dst_name
));
360 nt_status
= cli_open(cli_share_dst
, dst_name
,
361 O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
, &fnum_dst
);
363 if (!NT_STATUS_IS_OK(nt_status
)) {
364 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
365 dst_name
, nt_errstr(nt_status
)));
369 /* allocate memory */
370 if (!(data
= (char *)SMB_MALLOC(read_size
))) {
371 d_fprintf(stderr
, _("malloc fail for size %d\n"),
373 nt_status
= NT_STATUS_NO_MEMORY
;
380 if (c
->opt_verbose
) {
382 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
383 "%s ACLs and %s DOS Attributes %s\n"),
384 smbXcli_conn_remote_name(cli_share_src
->conn
),
385 cli_share_src
->share
, src_name
,
386 smbXcli_conn_remote_name(cli_share_dst
->conn
),
387 cli_share_dst
->share
, dst_name
,
388 copy_acls
? _("with") : _("without"),
389 copy_attrs
? _("with") : _("without"),
390 copy_timestamps
? _("(preserving timestamps)") : "" );
399 nt_status
= cli_read(cli_share_src
, fnum_src
, data
, nread
,
401 if (!NT_STATUS_IS_OK(nt_status
)) {
403 _("Error reading file [\\\\%s\\%s%s]: %s\n"),
404 smbXcli_conn_remote_name(cli_share_src
->conn
),
405 cli_share_src
->share
,
406 src_name
, nt_errstr(nt_status
));
413 nt_status
= cli_writeall(cli_share_dst
, fnum_dst
, 0,
414 (uint8_t *)data
, nread
, n
, NULL
);
416 if (!NT_STATUS_IS_OK(nt_status
)) {
418 _("Error writing file: [\\\\%s\\%s%s]: %s\n"),
419 smbXcli_conn_remote_name(cli_share_dst
->conn
),
420 cli_share_dst
->share
,
421 dst_name
, nt_errstr(nt_status
));
429 if (!is_file
&& !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst
, dst_name
))) {
432 DEBUGADD(3,("creating dir %s on the destination server\n",
435 nt_status
= cli_mkdir(cli_share_dst
, dst_name
);
436 if (!NT_STATUS_IS_OK(nt_status
)) {
437 DEBUG(0,("cannot create directory %s: %s\n",
438 dst_name
, nt_errstr(nt_status
)));
439 nt_status
= NT_STATUS_NO_SUCH_FILE
;
443 nt_status
= cli_chkpath(cli_share_dst
, dst_name
);
444 if (!NT_STATUS_IS_OK(nt_status
)) {
446 _("cannot check for directory %s: %s\n"),
447 dst_name
, nt_errstr(nt_status
));
454 nt_status
= cli_close(cli_share_src
, fnum_src
);
455 if (!NT_STATUS_IS_OK(nt_status
)) {
457 _("could not close file on originating server: %s\n"),
458 nt_errstr(nt_status
));
463 nt_status
= cli_close(cli_share_dst
, fnum_dst
);
464 if (!NT_STATUS_IS_OK(nt_status
)) {
466 _("could not close file on destination server: %s\n"),
467 nt_errstr(nt_status
));
472 /* possibly we have to copy some file-attributes / acls / sd */
473 nt_status
= net_copy_fileattr(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
474 src_name
, dst_name
, copy_acls
,
475 copy_attrs
, copy_timestamps
, is_file
);
476 if (!NT_STATUS_IS_OK(nt_status
))
480 nt_status
= NT_STATUS_OK
;
486 cli_close(cli_share_src
, fnum_src
);
489 cli_close(cli_share_dst
, fnum_dst
);
497 * Copy a driverfile from on connected share to another connected share
498 * This silently assumes that a driver-file is picked up from
500 * \\src_server\print$\{arch}\{version}\file
504 * \\dst_server\print$\{arch}\file
506 * to be added via setdriver-calls later.
507 * @param c A net_context structure
508 * @param mem_ctx A talloc-context
509 * @param cli_share_src A cli_state connected to source print$-share
510 * @param cli_share_dst A cli_state connected to destination print$-share
511 * @param file The file-name to be copied
512 * @param short_archi The name of the driver-architecture (short form)
514 * @return Normal NTSTATUS return.
517 static NTSTATUS
net_copy_driverfile(struct net_context
*c
,
519 struct cli_state
*cli_share_src
,
520 struct cli_state
*cli_share_dst
,
521 const char *file
, const char *short_archi
) {
526 char *version
= NULL
;
527 char *filename
= NULL
;
534 /* scroll through the file until we have the part
535 beyond archi_table.short_archi */
537 while (next_token_talloc(mem_ctx
, &p
, &tok
, "\\")) {
538 if (strequal(tok
, short_archi
)) {
539 next_token_talloc(mem_ctx
, &p
, &version
, "\\");
540 next_token_talloc(mem_ctx
, &p
, &filename
, "\\");
544 if (version
== NULL
|| filename
== NULL
) {
545 return NT_STATUS_UNSUCCESSFUL
;
548 /* build source file name */
549 src_name
= talloc_asprintf(mem_ctx
, "\\%s\\%s\\%s",
550 short_archi
, version
, filename
);
551 if (src_name
== NULL
) {
552 return NT_STATUS_NO_MEMORY
;
555 /* create destination file name */
556 dst_name
= talloc_asprintf(mem_ctx
, "\\%s\\%s", short_archi
, filename
);
557 if (dst_name
== NULL
) {
558 return NT_STATUS_NO_MEMORY
;
562 /* finally copy the file */
563 return net_copy_file(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
564 src_name
, dst_name
, false, false, false, true);
568 * Check for existing Architecture directory on a given server
570 * @param cli_share A cli_state connected to a print$-share
571 * @param short_archi The Architecture for the print-driver
573 * @return Normal NTSTATUS return.
576 static NTSTATUS
check_arch_dir(struct cli_state
*cli_share
, const char *short_archi
)
579 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
582 if (asprintf(&dir
, "\\%s", short_archi
) < 0) {
583 return NT_STATUS_NO_MEMORY
;
586 DEBUG(10,("creating print-driver dir for architecture: %s\n",
589 nt_status
= cli_mkdir(cli_share
, dir
);
590 if (!NT_STATUS_IS_OK(nt_status
)) {
591 DEBUG(1,("cannot create directory %s: %s\n",
592 dir
, nt_errstr(nt_status
)));
595 nt_status
= cli_chkpath(cli_share
, dir
);
596 if (!NT_STATUS_IS_OK(nt_status
)) {
597 d_fprintf(stderr
, _("cannot check %s: %s\n"),
598 dir
, nt_errstr(nt_status
));
602 nt_status
= NT_STATUS_OK
;
610 * Copy a print-driver (level 3) from one connected print$-share to another
611 * connected print$-share
613 * @param c A net_context structure
614 * @param mem_ctx A talloc-context
615 * @param cli_share_src A cli_state connected to a print$-share
616 * @param cli_share_dst A cli_state connected to a print$-share
617 * @param short_archi The Architecture for the print-driver
618 * @param i1 The DRIVER_INFO_3-struct
620 * @return Normal NTSTATUS return.
623 static NTSTATUS
copy_print_driver_3(struct net_context
*c
,
625 struct cli_state
*cli_share_src
,
626 struct cli_state
*cli_share_dst
,
627 const char *short_archi
,
628 struct spoolss_DriverInfo3
*r
)
630 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
638 d_printf(_("copying driver: [%s], for architecture: [%s], "
640 r
->driver_name
, short_archi
, r
->version
);
642 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
643 r
->driver_path
, short_archi
);
644 if (!NT_STATUS_IS_OK(nt_status
))
647 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
648 r
->data_file
, short_archi
);
649 if (!NT_STATUS_IS_OK(nt_status
))
652 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
653 r
->config_file
, short_archi
);
654 if (!NT_STATUS_IS_OK(nt_status
))
657 nt_status
= net_copy_driverfile(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
658 r
->help_file
, short_archi
);
659 if (!NT_STATUS_IS_OK(nt_status
))
662 for (i
=0; r
->dependent_files
[i
] != NULL
; i
++) {
664 nt_status
= net_copy_driverfile(c
, mem_ctx
,
665 cli_share_src
, cli_share_dst
,
666 r
->dependent_files
[i
], short_archi
);
667 if (!NT_STATUS_IS_OK(nt_status
)) {
676 * net_spoolss-functions
677 * =====================
679 * the net_spoolss-functions aim to simplify spoolss-client-functions
680 * required during the migration-process wrt buffer-sizes, returned
683 * this greatly reduces the complexitiy of the migrate-functions.
687 static bool net_spoolss_enum_printers(struct rpc_pipe_client
*pipe_hnd
,
692 uint32_t *num_printers
,
693 union spoolss_PrinterInfo
**info
)
699 result
= rpccli_spoolss_enumprinters(pipe_hnd
, mem_ctx
,
706 if (!W_ERROR_IS_OK(result
)) {
707 printf(_("cannot enum printers: %s\n"), win_errstr(result
));
714 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client
*pipe_hnd
,
716 const char *printername
,
717 uint32_t access_required
,
718 struct policy_handle
*hnd
)
720 struct cli_credentials
*creds
= gensec_get_credentials(pipe_hnd
->auth
->auth_ctx
);
721 const char *username
= cli_credentials_get_username(creds
);
723 fstring printername2
;
725 fstrcpy(printername2
, pipe_hnd
->srv_name_slash
);
726 fstrcat(printername2
, "\\");
727 fstrcat(printername2
, printername
);
729 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
730 pipe_hnd
->srv_name_slash
, username
, printername2
, access_required
));
733 result
= rpccli_spoolss_openprinter_ex(pipe_hnd
, mem_ctx
,
738 /* be more verbose */
739 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
741 _("no access to printer [%s] on [%s] for user [%s] "
743 printername2
, pipe_hnd
->srv_name_slash
, username
);
747 if (!W_ERROR_IS_OK(result
)) {
748 d_fprintf(stderr
,_("cannot open printer %s on server %s: %s\n"),
749 printername2
, pipe_hnd
->srv_name_slash
, win_errstr(result
));
753 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
754 printername2
, pipe_hnd
->srv_name_slash
));
759 static bool net_spoolss_getprinter(struct rpc_pipe_client
*pipe_hnd
,
761 struct policy_handle
*hnd
,
763 union spoolss_PrinterInfo
*info
)
767 /* getprinter call */
768 result
= rpccli_spoolss_getprinter(pipe_hnd
, mem_ctx
,
773 if (!W_ERROR_IS_OK(result
)) {
774 printf(_("cannot get printer-info: %s\n"), win_errstr(result
));
781 static bool net_spoolss_setprinter(struct rpc_pipe_client
*pipe_hnd
,
783 struct policy_handle
*hnd
,
785 union spoolss_PrinterInfo
*info
)
787 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
790 struct spoolss_SetPrinterInfoCtr info_ctr
;
791 struct spoolss_SetPrinterInfo2 info2
;
792 struct spoolss_DevmodeContainer devmode_ctr
;
793 struct sec_desc_buf secdesc_ctr
;
795 ZERO_STRUCT(devmode_ctr
);
796 ZERO_STRUCT(secdesc_ctr
);
798 /* setprinter call */
800 info_ctr
.level
= level
;
803 info_ctr
.info
.info0
= (struct spoolss_SetPrinterInfo0
*)
804 (void *)&info
->info0
;
807 info_ctr
.info
.info1
= (struct spoolss_SetPrinterInfo1
*)
808 (void *)&info
->info1
;
811 spoolss_printerinfo2_to_setprinterinfo2(&info
->info2
, &info2
);
812 info_ctr
.info
.info2
= &info2
;
815 info_ctr
.info
.info3
= (struct spoolss_SetPrinterInfo3
*)
816 (void *)&info
->info3
;
819 info_ctr
.info
.info4
= (struct spoolss_SetPrinterInfo4
*)
820 (void *)&info
->info4
;
823 info_ctr
.info
.info5
= (struct spoolss_SetPrinterInfo5
*)
824 (void *)&info
->info5
;
827 info_ctr
.info
.info6
= (struct spoolss_SetPrinterInfo6
*)
828 (void *)&info
->info6
;
831 info_ctr
.info
.info7
= (struct spoolss_SetPrinterInfo7
*)
832 (void *)&info
->info7
;
836 info_ctr
.info
.info8
= (struct spoolss_SetPrinterInfo8
*)
837 (void *)&info
->info8
;
840 info_ctr
.info
.info9
= (struct spoolss_SetPrinterInfo9
*)
841 (void *)&info
->info9
;
848 status
= dcerpc_spoolss_SetPrinter(b
, mem_ctx
,
855 if (!NT_STATUS_IS_OK(status
)) {
856 printf(_("cannot set printer-info: %s\n"), nt_errstr(status
));
859 if (!W_ERROR_IS_OK(result
)) {
860 printf(_("cannot set printer-info: %s\n"), win_errstr(result
));
868 static bool net_spoolss_setprinterdata(struct rpc_pipe_client
*pipe_hnd
,
870 struct policy_handle
*hnd
,
871 const char *value_name
,
872 enum winreg_Type type
,
876 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
880 /* setprinterdata call */
881 status
= dcerpc_spoolss_SetPrinterData(b
, mem_ctx
,
888 if (!NT_STATUS_IS_OK(status
)) {
889 printf (_("unable to set printerdata: %s\n"),
893 if (!W_ERROR_IS_OK(result
)) {
894 printf (_("unable to set printerdata: %s\n"),
903 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client
*pipe_hnd
,
905 struct policy_handle
*hnd
,
907 const char ***keylist
)
911 /* enumprinterkey call */
912 result
= rpccli_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, hnd
, keyname
, keylist
, 0);
914 if (!W_ERROR_IS_OK(result
)) {
915 printf(_("enumprinterkey failed: %s\n"), win_errstr(result
));
922 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
925 struct policy_handle
*hnd
,
928 struct spoolss_PrinterEnumValues
**info
)
932 /* enumprinterdataex call */
933 result
= rpccli_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
,
940 if (!W_ERROR_IS_OK(result
)) {
941 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result
));
949 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
951 struct policy_handle
*hnd
,
954 struct registry_value
*value
)
956 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
960 /* setprinterdataex call */
961 status
= dcerpc_spoolss_SetPrinterDataEx(b
, mem_ctx
,
969 if (!NT_STATUS_IS_OK(status
)) {
970 printf(_("could not set printerdataex: %s\n"),
974 if (!W_ERROR_IS_OK(result
)) {
975 printf(_("could not set printerdataex: %s\n"),
983 static bool net_spoolss_enumforms(struct rpc_pipe_client
*pipe_hnd
,
985 struct policy_handle
*hnd
,
988 union spoolss_FormInfo
**forms
)
993 result
= rpccli_spoolss_enumforms(pipe_hnd
, mem_ctx
,
999 if (!W_ERROR_IS_OK(result
)) {
1000 printf(_("could not enum forms: %s\n"), win_errstr(result
));
1007 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client
*pipe_hnd
,
1008 TALLOC_CTX
*mem_ctx
,
1009 uint32_t level
, const char *env
,
1011 union spoolss_DriverInfo
**info
)
1015 /* enumprinterdrivers call */
1016 result
= rpccli_spoolss_enumprinterdrivers(pipe_hnd
, mem_ctx
,
1017 pipe_hnd
->srv_name_slash
,
1023 if (!W_ERROR_IS_OK(result
)) {
1024 if (W_ERROR_V(result
) != W_ERROR_V(WERR_INVALID_ENVIRONMENT
)) {
1025 printf(_("cannot enum drivers for environment %s: %s\n"), env
,
1026 win_errstr(result
));
1029 printf(_("Server does not support environment [%s]\n"),
1037 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1038 TALLOC_CTX
*mem_ctx
,
1039 struct policy_handle
*hnd
, uint32_t level
,
1040 const char *env
, int version
,
1041 union spoolss_DriverInfo
*info
)
1044 uint32_t server_major_version
;
1045 uint32_t server_minor_version
;
1047 /* getprinterdriver call */
1048 result
= rpccli_spoolss_getprinterdriver2(pipe_hnd
, mem_ctx
,
1056 &server_major_version
,
1057 &server_minor_version
);
1058 if (!W_ERROR_IS_OK(result
)) {
1059 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1060 env
, win_errstr(result
)));
1061 if (W_ERROR_V(result
) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER
) &&
1062 W_ERROR_V(result
) != W_ERROR_V(WERR_INVALID_ENVIRONMENT
)) {
1063 printf(_("cannot get driver: %s\n"),
1064 win_errstr(result
));
1073 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1074 TALLOC_CTX
*mem_ctx
, uint32_t level
,
1075 union spoolss_DriverInfo
*info
)
1077 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1080 struct spoolss_AddDriverInfoCtr info_ctr
;
1082 info_ctr
.level
= level
;
1086 info_ctr
.info
.info2
= (struct spoolss_AddDriverInfo2
*)
1087 (void *)&info
->info2
;
1090 info_ctr
.info
.info3
= (struct spoolss_AddDriverInfo3
*)
1091 (void *)&info
->info3
;
1094 printf(_("unsupported info level: %d\n"), level
);
1098 /* addprinterdriver call */
1099 status
= dcerpc_spoolss_AddPrinterDriver(b
, mem_ctx
,
1100 pipe_hnd
->srv_name_slash
,
1103 if (!NT_STATUS_IS_OK(status
)) {
1104 printf(_("cannot add driver: %s\n"), nt_errstr(status
));
1107 /* be more verbose */
1108 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
1109 printf(_("You are not allowed to add drivers\n"));
1112 if (!W_ERROR_IS_OK(result
)) {
1113 printf(_("cannot add driver: %s\n"), win_errstr(result
));
1121 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1122 * for a single printer or for all printers depending on argc/argv
1125 static bool get_printer_info(struct rpc_pipe_client
*pipe_hnd
,
1126 TALLOC_CTX
*mem_ctx
,
1130 uint32_t *num_printers
,
1131 union spoolss_PrinterInfo
**info_p
)
1133 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1134 struct policy_handle hnd
;
1137 /* no arguments given, enumerate all printers */
1140 if (!net_spoolss_enum_printers(pipe_hnd
, mem_ctx
, NULL
,
1141 PRINTER_ENUM_LOCAL
|PRINTER_ENUM_SHARED
,
1142 level
, num_printers
, info_p
))
1148 /* argument given, get a single printer by name */
1149 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, argv
[0],
1150 MAXIMUM_ALLOWED_ACCESS
,
1154 *info_p
= talloc_zero(mem_ctx
, union spoolss_PrinterInfo
);
1155 if (*info_p
== NULL
) {
1159 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, *info_p
)) {
1160 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1164 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1169 DEBUG(3,("got %d printers\n", *num_printers
));
1176 * List print-queues (including local printers that are not shared)
1178 * All parameters are provided by the run_rpc_command function, except for
1179 * argc, argv which are passed through.
1181 * @param c A net_context structure
1182 * @param domain_sid The domain sid aquired from the remote server
1183 * @param cli A cli_state connected to the server.
1184 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1185 * @param argc Standard main() style argc
1186 * @param argv Standard main() style argv. Initial components are already
1189 * @return Normal NTSTATUS return.
1192 NTSTATUS
rpc_printer_list_internals(struct net_context
*c
,
1193 const struct dom_sid
*domain_sid
,
1194 const char *domain_name
,
1195 struct cli_state
*cli
,
1196 struct rpc_pipe_client
*pipe_hnd
,
1197 TALLOC_CTX
*mem_ctx
,
1201 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1202 uint32_t i
, num_printers
;
1204 const char *printername
, *sharename
;
1205 union spoolss_PrinterInfo
*info
;
1207 printf("listing printers\n");
1209 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info
))
1212 for (i
= 0; i
< num_printers
; i
++) {
1214 /* do some initialization */
1215 printername
= info
[i
].info2
.printername
;
1216 sharename
= info
[i
].info2
.sharename
;
1218 if (printername
&& sharename
) {
1219 d_printf(_("printer %d: %s, shared as: %s\n"),
1220 i
+1, printername
, sharename
);
1224 return NT_STATUS_OK
;
1228 * List printer-drivers from a server
1230 * All parameters are provided by the run_rpc_command function, except for
1231 * argc, argv which are passed through.
1233 * @param c A net_context structure
1234 * @param domain_sid The domain sid aquired from the remote server
1235 * @param cli A cli_state connected to the server.
1236 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1237 * @param argc Standard main() style argc
1238 * @param argv Standard main() style argv. Initial components are already
1241 * @return Normal NTSTATUS return.
1244 NTSTATUS
rpc_printer_driver_list_internals(struct net_context
*c
,
1245 const struct dom_sid
*domain_sid
,
1246 const char *domain_name
,
1247 struct cli_state
*cli
,
1248 struct rpc_pipe_client
*pipe_hnd
,
1249 TALLOC_CTX
*mem_ctx
,
1253 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1256 union spoolss_DriverInfo
*info
;
1258 printf(_("listing printer-drivers\n"));
1260 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
1262 uint32_t d
, num_drivers
;
1264 /* enum remote drivers */
1265 if (!net_spoolss_enumprinterdrivers(pipe_hnd
, mem_ctx
, level
,
1266 archi_table
[i
].long_archi
,
1267 &num_drivers
, &info
)) {
1268 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1272 if (num_drivers
== 0) {
1273 d_printf(_("no drivers found on server for "
1274 "architecture: [%s].\n"),
1275 archi_table
[i
].long_archi
);
1279 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1280 num_drivers
, archi_table
[i
].long_archi
);
1283 /* do something for all drivers for architecture */
1284 for (d
= 0; d
< num_drivers
; d
++) {
1285 display_print_driver3(&info
[d
].info3
);
1289 nt_status
= NT_STATUS_OK
;
1297 * Publish print-queues with args-wrapper
1299 * @param cli A cli_state connected to the server.
1300 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1301 * @param argc Standard main() style argc
1302 * @param argv Standard main() style argv. Initial components are already
1306 * @return Normal NTSTATUS return.
1309 static NTSTATUS
rpc_printer_publish_internals_args(struct rpc_pipe_client
*pipe_hnd
,
1310 TALLOC_CTX
*mem_ctx
,
1315 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1316 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1317 uint32_t i
, num_printers
;
1319 const char *printername
, *sharename
;
1320 union spoolss_PrinterInfo
*info_enum
;
1321 union spoolss_PrinterInfo info
;
1322 struct spoolss_SetPrinterInfoCtr info_ctr
;
1323 struct spoolss_DevmodeContainer devmode_ctr
;
1324 struct sec_desc_buf secdesc_ctr
;
1325 struct policy_handle hnd
= { 0, };
1327 const char *action_str
;
1329 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
))
1332 for (i
= 0; i
< num_printers
; i
++) {
1334 /* do some initialization */
1335 printername
= info_enum
[i
].info2
.printername
;
1336 sharename
= info_enum
[i
].info2
.sharename
;
1337 if (!printername
|| !sharename
) {
1341 /* open printer handle */
1342 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1343 PRINTER_ALL_ACCESS
, &hnd
))
1346 /* check for existing dst printer */
1347 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &info
))
1350 /* check action and set string */
1352 case DSPRINT_PUBLISH
:
1353 action_str
= N_("published");
1355 case DSPRINT_UPDATE
:
1356 action_str
= N_("updated");
1358 case DSPRINT_UNPUBLISH
:
1359 action_str
= N_("unpublished");
1362 action_str
= N_("unknown action");
1363 printf(_("unknown action: %d\n"), action
);
1367 info
.info7
.action
= action
;
1369 info_ctr
.info
.info7
= (struct spoolss_SetPrinterInfo7
*)
1370 (void *)&info
.info7
;
1372 ZERO_STRUCT(devmode_ctr
);
1373 ZERO_STRUCT(secdesc_ctr
);
1375 nt_status
= dcerpc_spoolss_SetPrinter(b
, mem_ctx
,
1382 if (!NT_STATUS_IS_OK(nt_status
)) {
1383 printf(_("cannot set printer-info: %s\n"),
1384 nt_errstr(nt_status
));
1387 if (!W_ERROR_IS_OK(result
) && !W_ERROR_EQUAL(result
, WERR_IO_PENDING
)) {
1388 if ((action
== DSPRINT_UPDATE
) && W_ERROR_EQUAL(result
, W_ERROR(0x80070002))) {
1389 printf(_("printer not published yet\n"));
1391 printf(_("cannot set printer-info: %s\n"),
1392 win_errstr(result
));
1394 nt_status
= werror_to_ntstatus(result
);
1398 printf(_("successfully %s printer %s in Active Directory\n"),
1399 action_str
, sharename
);
1402 nt_status
= NT_STATUS_OK
;
1405 if (is_valid_policy_hnd(&hnd
)) {
1406 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &result
);
1412 NTSTATUS
rpc_printer_publish_publish_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_PUBLISH
);
1424 NTSTATUS
rpc_printer_publish_unpublish_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_UNPUBLISH
);
1436 NTSTATUS
rpc_printer_publish_update_internals(struct net_context
*c
,
1437 const struct dom_sid
*domain_sid
,
1438 const char *domain_name
,
1439 struct cli_state
*cli
,
1440 struct rpc_pipe_client
*pipe_hnd
,
1441 TALLOC_CTX
*mem_ctx
,
1445 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, DSPRINT_UPDATE
);
1449 * List print-queues w.r.t. their publishing state
1451 * All parameters are provided by the run_rpc_command function, except for
1452 * argc, argv which are passed through.
1454 * @param c A net_context structure
1455 * @param domain_sid The domain sid aquired from the remote server
1456 * @param cli A cli_state connected to the server.
1457 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1458 * @param argc Standard main() style argc
1459 * @param argv Standard main() style argv. Initial components are already
1462 * @return Normal NTSTATUS return.
1465 NTSTATUS
rpc_printer_publish_list_internals(struct net_context
*c
,
1466 const struct dom_sid
*domain_sid
,
1467 const char *domain_name
,
1468 struct cli_state
*cli
,
1469 struct rpc_pipe_client
*pipe_hnd
,
1470 TALLOC_CTX
*mem_ctx
,
1474 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1475 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1476 uint32_t i
, num_printers
;
1478 const char *printername
, *sharename
;
1479 union spoolss_PrinterInfo
*info_enum
;
1480 union spoolss_PrinterInfo info
;
1481 struct policy_handle hnd
= { 0, };
1485 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
))
1488 for (i
= 0; i
< num_printers
; i
++) {
1490 /* do some initialization */
1491 printername
= info_enum
[i
].info2
.printername
;
1492 sharename
= info_enum
[i
].info2
.sharename
;
1494 if (!printername
|| !sharename
) {
1498 /* open printer handle */
1499 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1500 PRINTER_ALL_ACCESS
, &hnd
))
1503 /* check for existing dst printer */
1504 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &info
))
1507 if (!info
.info7
.guid
) {
1510 state
= info
.info7
.action
;
1512 case DSPRINT_PUBLISH
:
1513 printf(_("printer [%s] is published"),
1516 printf(_(", guid: %s"),info
.info7
.guid
);
1519 case DSPRINT_UNPUBLISH
:
1520 printf(_("printer [%s] is unpublished\n"),
1523 case DSPRINT_UPDATE
:
1524 printf(_("printer [%s] is currently updating\n"),
1528 printf(_("unknown state: %d\n"), state
);
1533 nt_status
= NT_STATUS_OK
;
1536 if (is_valid_policy_hnd(&hnd
)) {
1537 dcerpc_spoolss_ClosePrinter(b
, mem_ctx
, &hnd
, &werr
);
1544 * Migrate Printer-ACLs from a source server to the destination server
1546 * All parameters are provided by the run_rpc_command function, except for
1547 * argc, argv which are passed through.
1549 * @param c A net_context structure
1550 * @param domain_sid The domain sid aquired from the remote server
1551 * @param cli A cli_state connected to the server.
1552 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1553 * @param argc Standard main() style argc
1554 * @param argv Standard main() style argv. Initial components are already
1557 * @return Normal NTSTATUS return.
1560 NTSTATUS
rpc_printer_migrate_security_internals(struct net_context
*c
,
1561 const struct dom_sid
*domain_sid
,
1562 const char *domain_name
,
1563 struct cli_state
*cli
,
1564 struct rpc_pipe_client
*pipe_hnd
,
1565 TALLOC_CTX
*mem_ctx
,
1569 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1570 /* TODO: what now, info2 or info3 ?
1571 convince jerry that we should add clientside setacls level 3 at least
1573 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1575 uint32_t num_printers
;
1577 const char *printername
, *sharename
;
1578 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1579 struct dcerpc_binding_handle
*b_dst
= NULL
;
1580 struct policy_handle hnd_src
= { 0, };
1581 struct policy_handle hnd_dst
= { 0, };
1582 union spoolss_PrinterInfo
*info_enum
;
1583 struct cli_state
*cli_dst
= NULL
;
1584 union spoolss_PrinterInfo info_src
, info_dst
;
1587 DEBUG(3,("copying printer ACLs\n"));
1589 /* connect destination PI_SPOOLSS */
1590 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1591 &ndr_table_spoolss
);
1592 if (!NT_STATUS_IS_OK(nt_status
)) {
1595 b_dst
= pipe_hnd_dst
->binding_handle
;
1597 /* enum source printers */
1598 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
1599 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1603 if (!num_printers
) {
1604 printf (_("no printers found on server.\n"));
1605 nt_status
= NT_STATUS_OK
;
1609 /* do something for all printers */
1610 for (i
= 0; i
< num_printers
; i
++) {
1612 /* do some initialization */
1613 printername
= info_enum
[i
].info2
.printername
;
1614 sharename
= info_enum
[i
].info2
.sharename
;
1616 if (!printername
|| !sharename
) {
1617 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1621 /* we can reset NT_STATUS here because we do not
1622 get any real NT_STATUS-codes anymore from now on */
1623 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1625 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1626 printername
, sharename
);
1628 /* according to msdn you have specify these access-rights
1629 to see the security descriptor
1630 - READ_CONTROL (DACL)
1631 - ACCESS_SYSTEM_SECURITY (SACL)
1634 /* open src printer handle */
1635 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1636 MAXIMUM_ALLOWED_ACCESS
, &hnd_src
))
1639 /* open dst printer handle */
1640 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1641 PRINTER_ALL_ACCESS
, &hnd_dst
))
1644 /* check for existing dst printer */
1645 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
))
1648 /* check for existing src printer */
1649 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, 3, &info_src
))
1652 /* Copy Security Descriptor */
1654 /* copy secdesc (info level 2) */
1655 info_dst
.info2
.devmode
= NULL
;
1656 if (info_src
.info3
.secdesc
== NULL
) {
1657 info_dst
.info2
.secdesc
= NULL
;
1659 info_dst
.info2
.secdesc
1660 = security_descriptor_copy(mem_ctx
,
1661 info_src
.info3
.secdesc
);
1662 if (info_dst
.info2
.secdesc
== NULL
) {
1663 nt_status
= NT_STATUS_NO_MEMORY
;
1669 display_sec_desc(info_dst
.info2
.secdesc
);
1671 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
1674 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1677 /* close printer handles here */
1678 if (is_valid_policy_hnd(&hnd_src
)) {
1679 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
1682 if (is_valid_policy_hnd(&hnd_dst
)) {
1683 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
1688 nt_status
= NT_STATUS_OK
;
1692 if (is_valid_policy_hnd(&hnd_src
)) {
1693 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
1696 if (is_valid_policy_hnd(&hnd_dst
)) {
1697 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
1701 cli_shutdown(cli_dst
);
1707 * Migrate printer-forms from a src server to the dst server
1709 * All parameters are provided by the run_rpc_command function, except for
1710 * argc, argv which are passed through.
1712 * @param c A net_context structure
1713 * @param domain_sid The domain sid aquired from the remote server
1714 * @param cli A cli_state connected to the server.
1715 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1716 * @param argc Standard main() style argc
1717 * @param argv Standard main() style argv. Initial components are already
1720 * @return Normal NTSTATUS return.
1723 NTSTATUS
rpc_printer_migrate_forms_internals(struct net_context
*c
,
1724 const struct dom_sid
*domain_sid
,
1725 const char *domain_name
,
1726 struct cli_state
*cli
,
1727 struct rpc_pipe_client
*pipe_hnd
,
1728 TALLOC_CTX
*mem_ctx
,
1732 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1733 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1736 uint32_t num_printers
;
1738 const char *printername
, *sharename
;
1739 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1740 struct dcerpc_binding_handle
*b_dst
= NULL
;
1741 struct policy_handle hnd_src
= { 0, };
1742 struct policy_handle hnd_dst
= { 0, };
1743 union spoolss_PrinterInfo
*info_enum
;
1744 union spoolss_PrinterInfo info_dst
;
1746 union spoolss_FormInfo
*forms
;
1747 struct cli_state
*cli_dst
= NULL
;
1749 DEBUG(3,("copying forms\n"));
1751 /* connect destination PI_SPOOLSS */
1752 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1753 &ndr_table_spoolss
);
1754 if (!NT_STATUS_IS_OK(nt_status
)) {
1757 b_dst
= pipe_hnd_dst
->binding_handle
;
1759 /* enum src printers */
1760 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1761 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1765 if (!num_printers
) {
1766 printf (_("no printers found on server.\n"));
1767 nt_status
= NT_STATUS_OK
;
1771 /* do something for all printers */
1772 for (i
= 0; i
< num_printers
; i
++) {
1774 /* do some initialization */
1775 printername
= info_enum
[i
].info2
.printername
;
1776 sharename
= info_enum
[i
].info2
.sharename
;
1778 if (!printername
|| !sharename
) {
1779 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1782 /* we can reset NT_STATUS here because we do not
1783 get any real NT_STATUS-codes anymore from now on */
1784 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1786 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1787 printername
, sharename
);
1790 /* open src printer handle */
1791 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
1792 MAXIMUM_ALLOWED_ACCESS
, &hnd_src
))
1795 /* open dst printer handle */
1796 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1797 PRINTER_ALL_ACCESS
, &hnd_dst
))
1800 /* check for existing dst printer */
1801 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
))
1804 /* finally migrate forms */
1805 if (!net_spoolss_enumforms(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &num_forms
, &forms
))
1808 DEBUG(1,("got %d forms for printer\n", num_forms
));
1811 for (f
= 0; f
< num_forms
; f
++) {
1813 struct spoolss_AddFormInfoCtr info_ctr
;
1816 /* only migrate FORM_PRINTER types, according to jerry
1817 FORM_BUILTIN-types are hard-coded in samba */
1818 if (forms
[f
].info1
.flags
!= SPOOLSS_FORM_PRINTER
)
1822 d_printf(_("\tmigrating form # %d [%s] of type "
1824 f
, forms
[f
].info1
.form_name
,
1825 forms
[f
].info1
.flags
);
1827 info_ctr
.info
.info1
= (struct spoolss_AddFormInfo1
*)
1828 (void *)&forms
[f
].info1
;
1830 /* FIXME: there might be something wrong with samba's
1832 status
= dcerpc_spoolss_AddForm(b_dst
, mem_ctx
,
1836 if (!NT_STATUS_IS_OK(status
)) {
1837 d_printf(_("\tdcerpc_spoolss_AddForm form %d: [%s] - %s\n"),
1838 f
, forms
[f
].info1
.form_name
, nt_errstr(status
));
1841 if (!W_ERROR_IS_OK(result
)) {
1842 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1843 f
, forms
[f
].info1
.form_name
);
1847 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1848 forms
[f
].info1
.form_name
));
1852 /* close printer handles here */
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 nt_status
= NT_STATUS_OK
;
1866 if (is_valid_policy_hnd(&hnd_src
)) {
1867 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
1870 if (is_valid_policy_hnd(&hnd_dst
)) {
1871 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
1875 cli_shutdown(cli_dst
);
1881 * Migrate printer-drivers from a src server to the dst server
1883 * All parameters are provided by the run_rpc_command function, except for
1884 * argc, argv which are passed through.
1886 * @param c A net_context structure
1887 * @param domain_sid The domain sid aquired from the remote server
1888 * @param cli A cli_state connected to the server.
1889 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1890 * @param argc Standard main() style argc
1891 * @param argv Standard main() style argv. Initial components are already
1894 * @return Normal NTSTATUS return.
1897 NTSTATUS
rpc_printer_migrate_drivers_internals(struct net_context
*c
,
1898 const struct dom_sid
*domain_sid
,
1899 const char *domain_name
,
1900 struct cli_state
*cli
,
1901 struct rpc_pipe_client
*pipe_hnd
,
1902 TALLOC_CTX
*mem_ctx
,
1906 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
1907 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1909 uint32_t num_printers
;
1911 const char *printername
, *sharename
;
1912 bool got_src_driver_share
= false;
1913 bool got_dst_driver_share
= false;
1914 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
1915 struct dcerpc_binding_handle
*b_dst
= NULL
;
1916 struct policy_handle hnd_src
= { 0, };
1917 struct policy_handle hnd_dst
= { 0, };
1918 union spoolss_DriverInfo drv_info_src
;
1919 union spoolss_PrinterInfo
*info_enum
;
1920 union spoolss_PrinterInfo info_dst
;
1921 struct cli_state
*cli_dst
= NULL
;
1922 struct cli_state
*cli_share_src
= NULL
;
1923 struct cli_state
*cli_share_dst
= NULL
;
1924 const char *drivername
= NULL
;
1927 DEBUG(3,("copying printer-drivers\n"));
1929 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
1930 &ndr_table_spoolss
);
1931 if (!NT_STATUS_IS_OK(nt_status
)) {
1934 b_dst
= pipe_hnd_dst
->binding_handle
;
1936 /* open print$-share on the src server */
1937 nt_status
= connect_to_service(c
, &cli_share_src
,
1938 smbXcli_conn_remote_sockaddr(cli
->conn
),
1939 smbXcli_conn_remote_name(cli
->conn
),
1941 if (!NT_STATUS_IS_OK(nt_status
))
1944 got_src_driver_share
= true;
1947 /* open print$-share on the dst server */
1948 nt_status
= connect_to_service(c
, &cli_share_dst
,
1949 smbXcli_conn_remote_sockaddr(cli_dst
->conn
),
1950 smbXcli_conn_remote_name(cli_dst
->conn
),
1952 if (!NT_STATUS_IS_OK(nt_status
))
1955 got_dst_driver_share
= true;
1958 /* enum src printers */
1959 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &info_enum
)) {
1960 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1964 if (num_printers
== 0) {
1965 printf (_("no printers found on server.\n"));
1966 nt_status
= NT_STATUS_OK
;
1971 /* do something for all printers */
1972 for (p
= 0; p
< num_printers
; p
++) {
1974 /* do some initialization */
1975 printername
= info_enum
[p
].info2
.printername
;
1976 sharename
= info_enum
[p
].info2
.sharename
;
1978 if (!printername
|| !sharename
) {
1979 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1983 /* we can reset NT_STATUS here because we do not
1984 get any real NT_STATUS-codes anymore from now on */
1985 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1987 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1988 printername
, sharename
);
1990 /* open dst printer handle */
1991 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
1992 PRINTER_ALL_ACCESS
, &hnd_dst
))
1995 /* check for existing dst printer */
1996 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
))
2000 /* open src printer handle */
2001 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2002 MAXIMUM_ALLOWED_ACCESS
,
2006 /* in a first step call getdriver for each shared printer (per arch)
2007 to get a list of all files that have to be copied */
2009 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
2012 if (!net_spoolss_getprinterdriver(pipe_hnd
, mem_ctx
, &hnd_src
,
2013 level
, archi_table
[i
].long_archi
,
2014 archi_table
[i
].version
, &drv_info_src
))
2017 drivername
= drv_info_src
.info3
.driver_name
;
2020 display_print_driver3(&drv_info_src
.info3
);
2022 /* check arch dir */
2023 nt_status
= check_arch_dir(cli_share_dst
, archi_table
[i
].short_archi
);
2024 if (!NT_STATUS_IS_OK(nt_status
))
2028 /* copy driver-files */
2029 nt_status
= copy_print_driver_3(c
, mem_ctx
, cli_share_src
, cli_share_dst
,
2030 archi_table
[i
].short_archi
,
2031 &drv_info_src
.info3
);
2032 if (!NT_STATUS_IS_OK(nt_status
))
2037 if (!net_spoolss_addprinterdriver(pipe_hnd_dst
, mem_ctx
, level
, &drv_info_src
)) {
2038 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2042 DEBUGADD(1,("Successfully added driver [%s] for printer [%s]\n",
2043 drivername
, printername
));
2047 if (!drivername
|| strlen(drivername
) == 0) {
2048 DEBUGADD(1,("Did not get driver for printer %s\n",
2054 info_dst
.info2
.drivername
= drivername
;
2056 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_dst
)) {
2057 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2061 DEBUGADD(1,("Successfully set driver %s for printer %s\n",
2062 drivername
, printername
));
2065 if (is_valid_policy_hnd(&hnd_dst
)) {
2066 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2070 if (is_valid_policy_hnd(&hnd_src
)) {
2071 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2075 nt_status
= NT_STATUS_OK
;
2079 if (is_valid_policy_hnd(&hnd_dst
)) {
2080 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &werr
);
2084 if (is_valid_policy_hnd(&hnd_src
)) {
2085 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &werr
);
2089 cli_shutdown(cli_dst
);
2092 if (got_src_driver_share
)
2093 cli_shutdown(cli_share_src
);
2095 if (got_dst_driver_share
)
2096 cli_shutdown(cli_share_dst
);
2103 * Migrate printer-queues from a src to the dst server
2104 * (requires a working "addprinter command" to be installed for the local smbd)
2106 * All parameters are provided by the run_rpc_command function, except for
2107 * argc, argv which are passed through.
2109 * @param c A net_context structure
2110 * @param domain_sid The domain sid aquired from the remote server
2111 * @param cli A cli_state connected to the server.
2112 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2113 * @param argc Standard main() style argc
2114 * @param argv Standard main() style argv. Initial components are already
2117 * @return Normal NTSTATUS return.
2120 NTSTATUS
rpc_printer_migrate_printers_internals(struct net_context
*c
,
2121 const struct dom_sid
*domain_sid
,
2122 const char *domain_name
,
2123 struct cli_state
*cli
,
2124 struct rpc_pipe_client
*pipe_hnd
,
2125 TALLOC_CTX
*mem_ctx
,
2129 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2131 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2132 uint32_t i
= 0, num_printers
;
2134 union spoolss_PrinterInfo info_dst
, info_src
;
2135 union spoolss_PrinterInfo
*info_enum
;
2136 struct cli_state
*cli_dst
= NULL
;
2137 struct policy_handle hnd_src
= { 0, };
2138 struct policy_handle hnd_dst
= { 0, };
2139 const char *printername
, *sharename
;
2140 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2141 struct dcerpc_binding_handle
*b_dst
= NULL
;
2142 struct spoolss_SetPrinterInfoCtr info_ctr
;
2144 DEBUG(3,("copying printers\n"));
2146 /* connect destination PI_SPOOLSS */
2147 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2148 &ndr_table_spoolss
);
2149 if (!NT_STATUS_IS_OK(nt_status
)) {
2152 b_dst
= pipe_hnd_dst
->binding_handle
;
2155 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2156 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2160 if (!num_printers
) {
2161 printf (_("no printers found on server.\n"));
2162 nt_status
= NT_STATUS_OK
;
2166 /* do something for all printers */
2167 for (i
= 0; i
< num_printers
; i
++) {
2169 struct spoolss_SetPrinterInfo2 info2
;
2171 /* do some initialization */
2172 printername
= info_enum
[i
].info2
.printername
;
2173 sharename
= info_enum
[i
].info2
.sharename
;
2175 if (!printername
|| !sharename
) {
2176 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2179 /* we can reset NT_STATUS here because we do not
2180 get any real NT_STATUS-codes anymore from now on */
2181 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2183 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2184 printername
, sharename
);
2186 /* open dst printer handle */
2187 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2188 PRINTER_ALL_ACCESS
, &hnd_dst
)) {
2190 DEBUG(1,("could not open printer: %s\n", sharename
));
2193 /* check for existing dst printer */
2194 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &info_dst
)) {
2195 printf (_("could not get printer, creating printer.\n"));
2197 DEBUG(1,("printer already exists: %s\n", sharename
));
2198 /* close printer handle here - dst only, not got src yet. */
2199 if (is_valid_policy_hnd(&hnd_dst
)) {
2200 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2205 /* now get again src printer ctr via getprinter,
2206 we first need a handle for that */
2208 /* open src printer handle */
2209 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2210 MAXIMUM_ALLOWED_ACCESS
, &hnd_src
))
2213 /* getprinter on the src server */
2214 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &info_src
))
2217 /* copy each src printer to a dst printer 1:1,
2218 maybe some values have to be changed though */
2219 d_printf(_("creating printer: %s\n"), printername
);
2221 info_ctr
.level
= level
;
2222 spoolss_printerinfo2_to_setprinterinfo2(&info_src
.info2
, &info2
);
2223 info_ctr
.info
.info2
= &info2
;
2225 result
= rpccli_spoolss_addprinterex(pipe_hnd_dst
,
2229 if (W_ERROR_IS_OK(result
))
2230 d_printf (_("printer [%s] successfully added.\n"),
2232 else if (W_ERROR_V(result
) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS
))
2233 d_fprintf (stderr
, _("printer [%s] already exists.\n"),
2236 d_fprintf (stderr
, _("could not create printer [%s]\n"),
2241 /* close printer handles here */
2242 if (is_valid_policy_hnd(&hnd_src
)) {
2243 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2246 if (is_valid_policy_hnd(&hnd_dst
)) {
2247 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2251 nt_status
= NT_STATUS_OK
;
2254 if (is_valid_policy_hnd(&hnd_src
)) {
2255 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2258 if (is_valid_policy_hnd(&hnd_dst
)) {
2259 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2263 cli_shutdown(cli_dst
);
2269 * Migrate Printer-Settings from a src server to the dst server
2270 * (for this to work, printers and drivers already have to be migrated earlier)
2272 * All parameters are provided by the run_rpc_command function, except for
2273 * argc, argv which are passed through.
2275 * @param c A net_context structure
2276 * @param domain_sid The domain sid aquired from the remote server
2277 * @param cli A cli_state connected to the server.
2278 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2279 * @param argc Standard main() style argc
2280 * @param argv Standard main() style argv. Initial components are already
2283 * @return Normal NTSTATUS return.
2286 NTSTATUS
rpc_printer_migrate_settings_internals(struct net_context
*c
,
2287 const struct dom_sid
*domain_sid
,
2288 const char *domain_name
,
2289 struct cli_state
*cli
,
2290 struct rpc_pipe_client
*pipe_hnd
,
2291 TALLOC_CTX
*mem_ctx
,
2295 struct dcerpc_binding_handle
*b_src
= pipe_hnd
->binding_handle
;
2297 /* FIXME: Here the nightmare begins */
2300 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2301 uint32_t i
= 0, j
= 0;
2302 uint32_t num_printers
;
2304 const char *printername
, *sharename
;
2305 struct rpc_pipe_client
*pipe_hnd_dst
= NULL
;
2306 struct dcerpc_binding_handle
*b_dst
= NULL
;
2307 struct policy_handle hnd_src
= { 0, };
2308 struct policy_handle hnd_dst
= { 0, };
2309 union spoolss_PrinterInfo
*info_enum
;
2310 union spoolss_PrinterInfo info_dst_publish
;
2311 union spoolss_PrinterInfo info_dst
;
2312 struct cli_state
*cli_dst
= NULL
;
2313 const char *longname
;
2314 const char **keylist
= NULL
;
2317 ZERO_STRUCT(info_dst_publish
);
2319 DEBUG(3,("copying printer settings\n"));
2321 /* connect destination PI_SPOOLSS */
2322 nt_status
= connect_dst_pipe(c
, &cli_dst
, &pipe_hnd_dst
,
2323 &ndr_table_spoolss
);
2324 if (!NT_STATUS_IS_OK(nt_status
)) {
2327 b_dst
= pipe_hnd_dst
->binding_handle
;
2329 /* enum src printers */
2330 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &info_enum
)) {
2331 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2335 if (!num_printers
) {
2336 printf (_("no printers found on server.\n"));
2337 nt_status
= NT_STATUS_OK
;
2342 /* needed for dns-strings in regkeys */
2343 longname
= get_mydnsfullname();
2345 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2349 /* do something for all printers */
2350 for (i
= 0; i
< num_printers
; i
++) {
2352 uint32_t value_needed
;
2353 uint32_t data_needed
;
2354 enum winreg_Type type
;
2355 struct spoolss_EnumPrinterData r
;
2357 /* do some initialization */
2358 printername
= info_enum
[i
].info2
.printername
;
2359 sharename
= info_enum
[i
].info2
.sharename
;
2361 if (!printername
|| !sharename
) {
2362 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2365 /* we can reset NT_STATUS here because we do not
2366 get any real NT_STATUS-codes anymore from now on */
2367 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2369 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2370 printername
, sharename
);
2373 /* open src printer handle */
2374 if (!net_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, sharename
,
2375 MAXIMUM_ALLOWED_ACCESS
, &hnd_src
))
2378 /* open dst printer handle */
2379 if (!net_spoolss_open_printer_ex(pipe_hnd_dst
, mem_ctx
, sharename
,
2380 PRINTER_ALL_ACCESS
, &hnd_dst
))
2383 /* check for existing dst printer */
2384 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2389 /* STEP 1: COPY DEVICE-MODE and other
2390 PRINTER_INFO_2-attributes
2393 info_dst
.info2
= info_enum
[i
].info2
;
2395 /* why is the port always disconnected when the printer
2396 is correctly installed (incl. driver ???) */
2397 info_dst
.info2
.portname
= SAMBA_PRINTER_PORT_NAME
;
2399 /* check if printer is published */
2400 if (info_enum
[i
].info2
.attributes
& PRINTER_ATTRIBUTE_PUBLISHED
) {
2402 /* check for existing dst printer */
2403 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
))
2406 info_dst_publish
.info7
.action
= DSPRINT_PUBLISH
;
2408 /* ignore false from setprinter due to WERR_IO_PENDING */
2409 net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 7, &info_dst_publish
);
2411 DEBUG(3,("republished printer\n"));
2414 if (info_enum
[i
].info2
.devmode
!= NULL
) {
2416 /* copy devmode (info level 2) */
2417 info_dst
.info2
.devmode
= info_enum
[i
].info2
.devmode
;
2419 /* do not copy security descriptor (we have another
2420 * command for that) */
2421 info_dst
.info2
.secdesc
= NULL
;
2424 info_dst
.info2
.devmode
.devicename
=
2425 talloc_asprintf(mem_ctx
, "\\\\%s\\%s",
2426 longname
, printername
);
2427 if (!info_dst
.info2
.devmode
.devicename
) {
2428 nt_status
= NT_STATUS_NO_MEMORY
;
2432 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2436 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2439 /* STEP 2: COPY REGISTRY VALUES */
2441 /* please keep in mind that samba parse_spools gives horribly
2442 crippled results when used to rpccli_spoolss_enumprinterdataex
2443 a win2k3-server. (Bugzilla #1851)
2444 FIXME: IIRC I've seen it too on a win2k-server
2447 r
.in
.handle
= &hnd_src
;
2448 r
.in
.enum_index
= 0;
2449 r
.in
.value_offered
= 0;
2450 r
.in
.data_offered
= 0;
2451 r
.out
.value_name
= NULL
;
2452 r
.out
.value_needed
= &value_needed
;
2455 r
.out
.data_needed
= &data_needed
;
2457 /* enumerate data on src handle */
2458 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2460 r
.in
.data_offered
= *r
.out
.data_needed
;
2461 r
.in
.value_offered
= *r
.out
.value_needed
;
2462 r
.out
.data
= talloc_zero_array(mem_ctx
, uint8_t, r
.in
.data_offered
);
2463 r
.out
.value_name
= talloc_zero_array(mem_ctx
, char, r
.in
.value_offered
);
2465 /* loop for all printerdata of "PrinterDriverData" */
2466 while (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2470 nt_status
= dcerpc_spoolss_EnumPrinterData_r(b_src
, mem_ctx
, &r
);
2472 /* loop for all reg_keys */
2473 if (NT_STATUS_IS_OK(nt_status
) && W_ERROR_IS_OK(r
.out
.result
)) {
2476 if (c
->opt_verbose
) {
2477 struct registry_value v
;
2478 v
.type
= *r
.out
.type
;
2479 v
.data
= data_blob_const(
2480 r
.out
.data
, r
.in
.data_offered
);
2482 display_reg_value(SPOOL_PRINTERDATA_KEY
,
2483 r
.out
.value_name
, &v
);
2487 if (!net_spoolss_setprinterdata(pipe_hnd_dst
, mem_ctx
,
2488 &hnd_dst
, r
.out
.value_name
,
2489 *r
.out
.type
, r
.out
.data
, r
.in
.data_offered
))
2492 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2497 /* STEP 3: COPY SUBKEY VALUES */
2499 /* here we need to enum all printer_keys and then work
2500 on the result with enum_printer_key_ex. nt4 does not
2501 respond to enumprinterkey, win2k does, so continue
2502 in case of an error */
2504 if (!net_spoolss_enumprinterkey(pipe_hnd
, mem_ctx
, &hnd_src
, "", &keylist
)) {
2505 printf(_("got no key-data\n"));
2510 /* work on a list of printer keys
2511 each key has to be enumerated to get all required
2512 information. information is then set via setprinterdataex-calls */
2514 if (keylist
== NULL
)
2517 for (i
=0; keylist
&& keylist
[i
] != NULL
; i
++) {
2519 const char *subkey
= keylist
[i
];
2521 struct spoolss_PrinterEnumValues
*info
;
2523 /* enumerate all src subkeys */
2524 if (!net_spoolss_enumprinterdataex(pipe_hnd
, mem_ctx
, 0,
2530 for (j
=0; j
< count
; j
++) {
2532 struct registry_value value
;
2533 const char *value_name
= info
[j
].value_name
;
2536 value
.type
= REG_SZ
;
2538 /* although samba replies with sane data in most cases we
2539 should try to avoid writing wrong registry data */
2541 if (strequal(value_name
, SPOOL_REG_PORTNAME
)) {
2542 /* although windows uses a multi-sz, we use a sz */
2543 ok
= push_reg_sz(mem_ctx
, &value
.data
, SAMBA_PRINTER_PORT_NAME
);
2545 nt_status
= NT_STATUS_NO_MEMORY
;
2549 else if (strequal(value_name
, SPOOL_REG_UNCNAME
)) {
2551 if (asprintf(&unc_name
, "\\\\%s\\%s", longname
, sharename
) < 0) {
2552 nt_status
= NT_STATUS_NO_MEMORY
;
2555 ok
= push_reg_sz(mem_ctx
, &value
.data
, unc_name
);
2557 nt_status
= NT_STATUS_NO_MEMORY
;
2562 else if (strequal(value_name
, SPOOL_REG_URL
)) {
2565 /* FIXME: should we really do that ??? */
2566 if (asprintf(&url
, "http://%s:631/printers/%s", longname
, sharename
) < 0) {
2567 nt_status
= NT_STATUS_NO_MEMORY
;
2570 push_reg_sz(mem_ctx
, NULL
, &value
.data
, url
);
2574 else if (strequal(value_name
, SPOOL_REG_SERVERNAME
)) {
2575 ok
= push_reg_sz(mem_ctx
, &value
.data
, longname
);
2577 nt_status
= NT_STATUS_NO_MEMORY
;
2581 else if (strequal(value_name
, SPOOL_REG_SHORTSERVERNAME
)) {
2582 ok
= push_reg_sz(mem_ctx
, &value
.data
, lp_netbios_name());
2584 nt_status
= NT_STATUS_NO_MEMORY
;
2589 value
.type
= info
[j
].type
;
2590 value
.data
= *info
[j
].data
;
2593 if (c
->opt_verbose
) {
2594 display_reg_value(subkey
, value_name
, &value
);
2597 /* here we have to set all subkeys on the dst server */
2598 if (!net_spoolss_setprinterdataex(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
2599 subkey
, value_name
, &value
))
2604 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2605 subkey
, info
[j
].value_name
));
2610 TALLOC_FREE(keylist
);
2612 /* close printer handles here */
2613 if (is_valid_policy_hnd(&hnd_src
)) {
2614 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2617 if (is_valid_policy_hnd(&hnd_dst
)) {
2618 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2622 nt_status
= NT_STATUS_OK
;
2625 if (is_valid_policy_hnd(&hnd_src
)) {
2626 dcerpc_spoolss_ClosePrinter(b_src
, mem_ctx
, &hnd_src
, &result
);
2629 if (is_valid_policy_hnd(&hnd_dst
)) {
2630 dcerpc_spoolss_ClosePrinter(b_dst
, mem_ctx
, &hnd_dst
, &result
);
2634 cli_shutdown(cli_dst
);