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 "utils/net.h"
21 #include "../librpc/gen_ndr/cli_spoolss.h"
22 #include "rpc_client/cli_spoolss.h"
25 /* support itanium as well */
26 static const struct print_architecture_table_node archi_table[]= {
28 {"Windows 4.0", "WIN40", 0 },
29 {"Windows NT x86", "W32X86", 2 },
30 {"Windows NT x86", "W32X86", 3 },
31 {"Windows NT R4000", "W32MIPS", 2 },
32 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
33 {"Windows NT PowerPC", "W32PPC", 2 },
34 {"Windows IA64", "IA64", 3 },
35 {"Windows x64", "x64", 3 },
41 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
42 * It is here for debugging purpose and should be removed later on.
45 /****************************************************************************
46 Printer info level 3 display function.
47 ****************************************************************************/
49 static void display_print_driver3(struct spoolss_DriverInfo3 *r)
57 printf(_("Printer Driver Info 3:\n"));
58 printf(_("\tVersion: [%x]\n"), r->version);
59 printf(_("\tDriver Name: [%s]\n"), r->driver_name);
60 printf(_("\tArchitecture: [%s]\n"), r->architecture);
61 printf(_("\tDriver Path: [%s]\n"), r->driver_path);
62 printf(_("\tDatafile: [%s]\n"), r->data_file);
63 printf(_("\tConfigfile: [%s]\n\n"), r->config_file);
64 printf(_("\tHelpfile: [%s]\n\n"), r->help_file);
66 for (i=0; r->dependent_files[i] != NULL; i++) {
67 printf(_("\tDependentfiles: [%s]\n"), r->dependent_files[i]);
72 printf(_("\tMonitorname: [%s]\n"), r->monitor_name);
73 printf(_("\tDefaultdatatype: [%s]\n\n"), r->default_datatype);
76 static void display_reg_value(const char *subkey, struct regval_blob value)
83 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey,
84 value.valuename, *((uint32_t *) value.data_p));
88 blob = data_blob_const(value.data_p, value.size);
89 pull_reg_sz(talloc_tos(), &blob, &text);
93 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, value.valuename,
98 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
100 subkey, value.valuename);
106 blob = data_blob_const(value.data_p, value.size);
108 if (!pull_reg_multi_sz(NULL, &blob, &values)) {
109 d_printf("pull_reg_multi_sz failed\n");
113 printf("%s: REG_MULTI_SZ: \n", value.valuename);
114 for (i=0; values[i] != NULL; i++) {
115 d_printf("%s\n", values[i]);
122 d_printf(_("\t%s: unknown type %d\n"), value.valuename,
129 * Copies ACLs, DOS-attributes and timestamps from one
130 * file or directory from one connected share to another connected share
132 * @param c A net_context structure
133 * @param mem_ctx A talloc-context
134 * @param cli_share_src A connected cli_state
135 * @param cli_share_dst A connected cli_state
136 * @param src_file The source file-name
137 * @param dst_file The destination file-name
138 * @param copy_acls Whether to copy acls
139 * @param copy_attrs Whether to copy DOS attributes
140 * @param copy_timestamps Whether to preserve timestamps
141 * @param is_file Whether this file is a file or a dir
143 * @return Normal NTSTATUS return.
146 NTSTATUS net_copy_fileattr(struct net_context *c,
148 struct cli_state *cli_share_src,
149 struct cli_state *cli_share_dst,
150 const char *src_name, const char *dst_name,
151 bool copy_acls, bool copy_attrs,
152 bool copy_timestamps, bool is_file)
154 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
155 uint16_t fnum_src = 0;
156 uint16_t fnum_dst = 0;
157 struct security_descriptor *sd = NULL;
159 time_t f_atime, f_ctime, f_mtime;
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 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
171 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src))) {
172 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
173 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
174 nt_status = cli_nt_error(cli_share_src);
181 /* get the security descriptor */
182 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
184 DEBUG(0,("failed to get security descriptor: %s\n",
185 cli_errstr(cli_share_src)));
186 nt_status = cli_nt_error(cli_share_src);
190 if (c->opt_verbose && DEBUGLEVEL >= 3)
191 display_sec_desc(sd);
195 if (copy_attrs || copy_timestamps) {
197 /* get file attributes */
198 if (!NT_STATUS_IS_OK(cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
199 &f_ctime, &f_atime, &f_mtime))) {
200 DEBUG(0,("failed to get file-attrs: %s\n",
201 cli_errstr(cli_share_src)));
202 nt_status = cli_nt_error(cli_share_src);
208 /* open the file/dir on the destination server */
210 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_dst, dst_name, 0, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
211 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_dst))) {
212 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
213 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
214 nt_status = cli_nt_error(cli_share_dst);
218 if (copy_timestamps) {
221 if (!NT_STATUS_IS_OK(cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime))) {
222 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
223 cli_errstr(cli_share_dst)));
224 nt_status = cli_nt_error(cli_share_dst);
232 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
233 DEBUG(0,("could not set secdesc on %s: %s\n",
234 dst_name, cli_errstr(cli_share_dst)));
235 nt_status = cli_nt_error(cli_share_dst);
243 if (!NT_STATUS_IS_OK(cli_setatr(cli_share_dst, dst_name, attr, 0))) {
244 DEBUG(0,("failed to set file-attrs: %s\n",
245 cli_errstr(cli_share_dst)));
246 nt_status = cli_nt_error(cli_share_dst);
254 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
256 _("could not close %s on originating server: %s\n"),
257 is_file?"file":"dir", cli_errstr(cli_share_src));
258 nt_status = cli_nt_error(cli_share_src);
262 if (!NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
264 _("could not close %s on destination server: %s\n"),
265 is_file?"file":"dir", cli_errstr(cli_share_dst));
266 nt_status = cli_nt_error(cli_share_dst);
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, FILE_OPEN, 0x0, 0x0, &fnum_src);
334 if (!NT_STATUS_IS_OK(nt_status)) {
335 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
336 is_file ? "file":"dir",
337 src_name, cli_errstr(cli_share_src)));
344 /* open file on the destination server */
345 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
346 nt_status = cli_open(cli_share_dst, dst_name,
347 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum_dst);
349 if (!NT_STATUS_IS_OK(nt_status)) {
350 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
351 dst_name, cli_errstr(cli_share_dst)));
355 /* allocate memory */
356 if (!(data = (char *)SMB_MALLOC(read_size))) {
357 d_fprintf(stderr, _("malloc fail for size %d\n"),
359 nt_status = NT_STATUS_NO_MEMORY;
366 if (c->opt_verbose) {
368 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
369 "%s ACLs and %s DOS Attributes %s\n"),
370 cli_share_src->desthost, cli_share_src->share, src_name,
371 cli_share_dst->desthost, cli_share_dst->share, dst_name,
372 copy_acls ? _("with") : _("without"),
373 copy_attrs ? _("with") : _("without"),
374 copy_timestamps ? _("(preserving timestamps)") : "" );
382 n = cli_read(cli_share_src, fnum_src, data, nread,
388 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
392 d_fprintf(stderr, _("Error writing file: %s\n"),
393 cli_errstr(cli_share_dst));
394 nt_status = cli_nt_error(cli_share_dst);
402 if (!is_file && !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
405 DEBUGADD(3,("creating dir %s on the destination server\n",
408 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share_dst, dst_name))) {
409 DEBUG(0,("cannot create directory %s: %s\n",
410 dst_name, cli_errstr(cli_share_dst)));
411 nt_status = NT_STATUS_NO_SUCH_FILE;
414 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
416 _("cannot check for directory %s: %s\n"),
417 dst_name, cli_errstr(cli_share_dst));
424 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
426 _("could not close file on originating server: %s\n"),
427 cli_errstr(cli_share_src));
428 nt_status = cli_nt_error(cli_share_src);
432 if (is_file && !NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
434 _("could not close file on destination server: %s\n"),
435 cli_errstr(cli_share_dst));
436 nt_status = cli_nt_error(cli_share_dst);
440 /* possibly we have to copy some file-attributes / acls / sd */
441 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
442 src_name, dst_name, copy_acls,
443 copy_attrs, copy_timestamps, is_file);
444 if (!NT_STATUS_IS_OK(nt_status))
448 nt_status = NT_STATUS_OK;
454 cli_close(cli_share_src, fnum_src);
457 cli_close(cli_share_dst, fnum_dst);
465 * Copy a driverfile from on connected share to another connected share
466 * This silently assumes that a driver-file is picked up from
468 * \\src_server\print$\{arch}\{version}\file
472 * \\dst_server\print$\{arch}\file
474 * to be added via setdriver-calls later.
475 * @param c A net_context structure
476 * @param mem_ctx A talloc-context
477 * @param cli_share_src A cli_state connected to source print$-share
478 * @param cli_share_dst A cli_state connected to destination print$-share
479 * @param file The file-name to be copied
480 * @param short_archi The name of the driver-architecture (short form)
482 * @return Normal NTSTATUS return.
485 static NTSTATUS net_copy_driverfile(struct net_context *c,
487 struct cli_state *cli_share_src,
488 struct cli_state *cli_share_dst,
489 const char *file, const char *short_archi) {
491 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
503 /* scroll through the file until we have the part
504 beyond archi_table.short_archi */
506 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
507 if (strequal(tok, short_archi)) {
508 next_token_talloc(mem_ctx, &p, &version, "\\");
509 next_token_talloc(mem_ctx, &p, &filename, "\\");
513 /* build source file name */
514 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
515 return NT_STATUS_NO_MEMORY;
518 /* create destination file name */
519 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
520 return NT_STATUS_NO_MEMORY;
523 /* finally copy the file */
524 nt_status = net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
525 src_name, dst_name, false, false, false, true);
526 if (!NT_STATUS_IS_OK(nt_status))
529 nt_status = NT_STATUS_OK;
539 * Check for existing Architecture directory on a given server
541 * @param cli_share A cli_state connected to a print$-share
542 * @param short_archi The Architecture for the print-driver
544 * @return Normal NTSTATUS return.
547 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
550 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
553 if (asprintf(&dir, "\\%s", short_archi) < 0) {
554 return NT_STATUS_NO_MEMORY;
557 DEBUG(10,("creating print-driver dir for architecture: %s\n",
560 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share, dir))) {
561 DEBUG(1,("cannot create directory %s: %s\n",
562 dir, cli_errstr(cli_share)));
563 nt_status = NT_STATUS_NO_SUCH_FILE;
566 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share, dir))) {
567 d_fprintf(stderr, _("cannot check %s: %s\n"),
568 dir, cli_errstr(cli_share));
572 nt_status = NT_STATUS_OK;
580 * Copy a print-driver (level 3) from one connected print$-share to another
581 * connected print$-share
583 * @param c A net_context structure
584 * @param mem_ctx A talloc-context
585 * @param cli_share_src A cli_state connected to a print$-share
586 * @param cli_share_dst A cli_state connected to a print$-share
587 * @param short_archi The Architecture for the print-driver
588 * @param i1 The DRIVER_INFO_3-struct
590 * @return Normal NTSTATUS return.
593 static NTSTATUS copy_print_driver_3(struct net_context *c,
595 struct cli_state *cli_share_src,
596 struct cli_state *cli_share_dst,
597 const char *short_archi,
598 struct spoolss_DriverInfo3 *r)
600 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
608 d_printf(_("copying driver: [%s], for architecture: [%s], "
610 r->driver_name, short_archi, r->version);
612 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
613 r->driver_path, short_archi);
614 if (!NT_STATUS_IS_OK(nt_status))
617 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
618 r->data_file, short_archi);
619 if (!NT_STATUS_IS_OK(nt_status))
622 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
623 r->config_file, short_archi);
624 if (!NT_STATUS_IS_OK(nt_status))
627 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
628 r->help_file, short_archi);
629 if (!NT_STATUS_IS_OK(nt_status))
632 for (i=0; r->dependent_files[i] != NULL; i++) {
634 nt_status = net_copy_driverfile(c, mem_ctx,
635 cli_share_src, cli_share_dst,
636 r->dependent_files[i], short_archi);
637 if (!NT_STATUS_IS_OK(nt_status)) {
646 * net_spoolss-functions
647 * =====================
649 * the net_spoolss-functions aim to simplify spoolss-client-functions
650 * required during the migration-process wrt buffer-sizes, returned
653 * this greatly reduces the complexitiy of the migrate-functions.
657 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
662 uint32_t *num_printers,
663 union spoolss_PrinterInfo **info)
669 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
676 if (!W_ERROR_IS_OK(result)) {
677 printf(_("cannot enum printers: %s\n"), win_errstr(result));
684 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
686 const char *printername,
687 uint32_t access_required,
688 const char *username,
689 struct policy_handle *hnd)
692 fstring printername2;
694 fstrcpy(printername2, pipe_hnd->srv_name_slash);
695 fstrcat(printername2, "\\");
696 fstrcat(printername2, printername);
698 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
699 pipe_hnd->srv_name_slash, username, printername2, access_required));
702 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
707 /* be more verbose */
708 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
710 _("no access to printer [%s] on [%s] for user [%s] "
712 printername2, pipe_hnd->srv_name_slash, username);
716 if (!W_ERROR_IS_OK(result)) {
717 d_fprintf(stderr,_("cannot open printer %s on server %s: %s\n"),
718 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
722 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
723 printername2, pipe_hnd->srv_name_slash));
728 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
730 struct policy_handle *hnd,
732 union spoolss_PrinterInfo *info)
736 /* getprinter call */
737 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
742 if (!W_ERROR_IS_OK(result)) {
743 printf(_("cannot get printer-info: %s\n"), win_errstr(result));
750 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
752 struct policy_handle *hnd,
754 union spoolss_PrinterInfo *info)
758 struct spoolss_SetPrinterInfoCtr info_ctr;
759 struct spoolss_DevmodeContainer devmode_ctr;
760 struct sec_desc_buf secdesc_ctr;
762 ZERO_STRUCT(devmode_ctr);
763 ZERO_STRUCT(secdesc_ctr);
765 /* setprinter call */
767 info_ctr.level = level;
770 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)
771 (void *)&info->info0;
774 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)
775 (void *)&info->info1;
778 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)
779 (void *)&info->info2;
782 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
783 (void *)&info->info3;
786 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)
787 (void *)&info->info4;
790 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)
791 (void *)&info->info5;
794 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)
795 (void *)&info->info6;
798 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
799 (void *)&info->info7;
803 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)
804 (void *)&info->info8;
807 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)
808 (void *)&info->info9;
815 status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
823 if (!W_ERROR_IS_OK(result)) {
824 printf(_("cannot set printer-info: %s\n"), win_errstr(result));
832 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
834 struct policy_handle *hnd,
835 const char *value_name,
836 enum winreg_Type type,
843 /* setprinterdata call */
844 status = rpccli_spoolss_SetPrinterData(pipe_hnd, mem_ctx,
852 if (!W_ERROR_IS_OK(result)) {
853 printf (_("unable to set printerdata: %s\n"),
862 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
864 struct policy_handle *hnd,
866 const char ***keylist)
870 /* enumprinterkey call */
871 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
873 if (!W_ERROR_IS_OK(result)) {
874 printf(_("enumprinterkey failed: %s\n"), win_errstr(result));
881 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
884 struct policy_handle *hnd,
887 struct spoolss_PrinterEnumValues **info)
891 /* enumprinterdataex call */
892 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
899 if (!W_ERROR_IS_OK(result)) {
900 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result));
908 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
910 struct policy_handle *hnd,
912 struct regval_blob *value)
917 /* setprinterdataex call */
918 status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
927 if (!W_ERROR_IS_OK(result)) {
928 printf(_("could not set printerdataex: %s\n"),
936 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
938 struct policy_handle *hnd,
941 union spoolss_FormInfo **forms)
946 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
952 if (!W_ERROR_IS_OK(result)) {
953 printf(_("could not enum forms: %s\n"), win_errstr(result));
960 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
962 uint32_t level, const char *env,
964 union spoolss_DriverInfo **info)
968 /* enumprinterdrivers call */
969 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
970 pipe_hnd->srv_name_slash,
976 if (!W_ERROR_IS_OK(result)) {
977 printf(_("cannot enum drivers: %s\n"), win_errstr(result));
984 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
986 struct policy_handle *hnd, uint32_t level,
987 const char *env, int version,
988 union spoolss_DriverInfo *info)
991 uint32_t server_major_version;
992 uint32_t server_minor_version;
994 /* getprinterdriver call */
995 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
1003 &server_major_version,
1004 &server_minor_version);
1005 if (!W_ERROR_IS_OK(result)) {
1006 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1007 env, win_errstr(result)));
1008 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1009 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1010 printf(_("cannot get driver: %s\n"),
1011 win_errstr(result));
1020 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1021 TALLOC_CTX *mem_ctx, uint32_t level,
1022 union spoolss_DriverInfo *info)
1026 struct spoolss_AddDriverInfoCtr info_ctr;
1028 info_ctr.level = level;
1032 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)
1033 (void *)&info->info2;
1036 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)
1037 (void *)&info->info3;
1040 printf(_("unsupported info level: %d\n"), level);
1044 /* addprinterdriver call */
1045 status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1046 pipe_hnd->srv_name_slash,
1049 /* be more verbose */
1050 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1051 printf(_("You are not allowed to add drivers\n"));
1054 if (!W_ERROR_IS_OK(result)) {
1055 printf(_("cannot add driver: %s\n"), win_errstr(result));
1063 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1064 * for a single printer or for all printers depending on argc/argv
1067 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1068 TALLOC_CTX *mem_ctx,
1072 uint32_t *num_printers,
1073 union spoolss_PrinterInfo **info_p)
1075 struct policy_handle hnd;
1077 /* no arguments given, enumerate all printers */
1080 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1081 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1082 level, num_printers, info_p))
1088 /* argument given, get a single printer by name */
1089 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1090 MAXIMUM_ALLOWED_ACCESS,
1091 pipe_hnd->auth->user_name,
1095 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1096 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1100 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1105 DEBUG(3,("got %d printers\n", *num_printers));
1112 * List print-queues (including local printers that are not shared)
1114 * All parameters are provided by the run_rpc_command function, except for
1115 * argc, argv which are passed through.
1117 * @param c A net_context structure
1118 * @param domain_sid The domain sid aquired from the remote server
1119 * @param cli A cli_state connected to the server.
1120 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1121 * @param argc Standard main() style argc
1122 * @param argv Standard main() style argv. Initial components are already
1125 * @return Normal NTSTATUS return.
1128 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1129 const DOM_SID *domain_sid,
1130 const char *domain_name,
1131 struct cli_state *cli,
1132 struct rpc_pipe_client *pipe_hnd,
1133 TALLOC_CTX *mem_ctx,
1137 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1138 uint32_t i, num_printers;
1140 const char *printername, *sharename;
1141 union spoolss_PrinterInfo *info;
1143 printf("listing printers\n");
1145 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1148 for (i = 0; i < num_printers; i++) {
1150 /* do some initialization */
1151 printername = info[i].info2.printername;
1152 sharename = info[i].info2.sharename;
1154 if (printername && sharename) {
1155 d_printf(_("printer %d: %s, shared as: %s\n"),
1156 i+1, printername, sharename);
1160 return NT_STATUS_OK;
1164 * List printer-drivers from a server
1166 * All parameters are provided by the run_rpc_command function, except for
1167 * argc, argv which are passed through.
1169 * @param c A net_context structure
1170 * @param domain_sid The domain sid aquired from the remote server
1171 * @param cli A cli_state connected to the server.
1172 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1173 * @param argc Standard main() style argc
1174 * @param argv Standard main() style argv. Initial components are already
1177 * @return Normal NTSTATUS return.
1180 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1181 const DOM_SID *domain_sid,
1182 const char *domain_name,
1183 struct cli_state *cli,
1184 struct rpc_pipe_client *pipe_hnd,
1185 TALLOC_CTX *mem_ctx,
1189 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1192 union spoolss_DriverInfo *info;
1195 printf(_("listing printer-drivers\n"));
1197 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1199 uint32_t num_drivers;
1201 /* enum remote drivers */
1202 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1203 archi_table[i].long_archi,
1204 &num_drivers, &info)) {
1205 nt_status = NT_STATUS_UNSUCCESSFUL;
1209 if (num_drivers == 0) {
1210 d_printf(_("no drivers found on server for "
1211 "architecture: [%s].\n"),
1212 archi_table[i].long_archi);
1216 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1217 num_drivers, archi_table[i].long_archi);
1220 /* do something for all drivers for architecture */
1221 for (d = 0; d < num_drivers; d++) {
1222 display_print_driver3(&info[d].info3);
1226 nt_status = NT_STATUS_OK;
1234 * Publish print-queues with args-wrapper
1236 * @param cli A cli_state connected to the server.
1237 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1238 * @param argc Standard main() style argc
1239 * @param argv Standard main() style argv. Initial components are already
1243 * @return Normal NTSTATUS return.
1246 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1247 TALLOC_CTX *mem_ctx,
1252 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1253 uint32_t i, num_printers;
1255 const char *printername, *sharename;
1256 union spoolss_PrinterInfo *info_enum;
1257 union spoolss_PrinterInfo info;
1258 struct spoolss_SetPrinterInfoCtr info_ctr;
1259 struct spoolss_DevmodeContainer devmode_ctr;
1260 struct sec_desc_buf secdesc_ctr;
1261 struct policy_handle hnd;
1263 const char *action_str;
1265 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1268 for (i = 0; i < num_printers; i++) {
1270 /* do some initialization */
1271 printername = info_enum[i].info2.printername;
1272 sharename = info_enum[i].info2.sharename;
1273 if (!printername || !sharename) {
1277 /* open printer handle */
1278 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1279 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1282 /* check for existing dst printer */
1283 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1286 /* check action and set string */
1288 case DSPRINT_PUBLISH:
1289 action_str = N_("published");
1291 case DSPRINT_UPDATE:
1292 action_str = N_("updated");
1294 case DSPRINT_UNPUBLISH:
1295 action_str = N_("unpublished");
1298 action_str = N_("unknown action");
1299 printf(_("unkown action: %d\n"), action);
1303 info.info7.action = action;
1305 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
1306 (void *)&info.info7;
1308 ZERO_STRUCT(devmode_ctr);
1309 ZERO_STRUCT(secdesc_ctr);
1311 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1319 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1320 printf(_("cannot set printer-info: %s\n"),
1321 win_errstr(result));
1325 printf(_("successfully %s printer %s in Active Directory\n"),
1326 action_str, sharename);
1329 nt_status = NT_STATUS_OK;
1332 if (is_valid_policy_hnd(&hnd))
1333 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1338 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1339 const DOM_SID *domain_sid,
1340 const char *domain_name,
1341 struct cli_state *cli,
1342 struct rpc_pipe_client *pipe_hnd,
1343 TALLOC_CTX *mem_ctx,
1347 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1350 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1351 const DOM_SID *domain_sid,
1352 const char *domain_name,
1353 struct cli_state *cli,
1354 struct rpc_pipe_client *pipe_hnd,
1355 TALLOC_CTX *mem_ctx,
1359 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1362 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1363 const DOM_SID *domain_sid,
1364 const char *domain_name,
1365 struct cli_state *cli,
1366 struct rpc_pipe_client *pipe_hnd,
1367 TALLOC_CTX *mem_ctx,
1371 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1375 * List print-queues w.r.t. their publishing state
1377 * All parameters are provided by the run_rpc_command function, except for
1378 * argc, argv which are passed through.
1380 * @param c A net_context structure
1381 * @param domain_sid The domain sid aquired from the remote server
1382 * @param cli A cli_state connected to the server.
1383 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1384 * @param argc Standard main() style argc
1385 * @param argv Standard main() style argv. Initial components are already
1388 * @return Normal NTSTATUS return.
1391 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1392 const DOM_SID *domain_sid,
1393 const char *domain_name,
1394 struct cli_state *cli,
1395 struct rpc_pipe_client *pipe_hnd,
1396 TALLOC_CTX *mem_ctx,
1400 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1401 uint32_t i, num_printers;
1403 const char *printername, *sharename;
1404 union spoolss_PrinterInfo *info_enum;
1405 union spoolss_PrinterInfo info;
1406 struct policy_handle hnd;
1409 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1412 for (i = 0; i < num_printers; i++) {
1414 /* do some initialization */
1415 printername = info_enum[i].info2.printername;
1416 sharename = info_enum[i].info2.sharename;
1418 if (!printername || !sharename) {
1422 /* open printer handle */
1423 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1424 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1427 /* check for existing dst printer */
1428 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1431 if (!info.info7.guid) {
1434 state = info.info7.action;
1436 case DSPRINT_PUBLISH:
1437 printf(_("printer [%s] is published"),
1440 printf(_(", guid: %s"),info.info7.guid);
1443 case DSPRINT_UNPUBLISH:
1444 printf(_("printer [%s] is unpublished\n"),
1447 case DSPRINT_UPDATE:
1448 printf(_("printer [%s] is currently updating\n"),
1452 printf(_("unkown state: %d\n"), state);
1457 nt_status = NT_STATUS_OK;
1460 if (is_valid_policy_hnd(&hnd))
1461 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1467 * Migrate Printer-ACLs from a source server to the destination server
1469 * All parameters are provided by the run_rpc_command function, except for
1470 * argc, argv which are passed through.
1472 * @param c A net_context structure
1473 * @param domain_sid The domain sid aquired from the remote server
1474 * @param cli A cli_state connected to the server.
1475 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1476 * @param argc Standard main() style argc
1477 * @param argv Standard main() style argv. Initial components are already
1480 * @return Normal NTSTATUS return.
1483 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1484 const DOM_SID *domain_sid,
1485 const char *domain_name,
1486 struct cli_state *cli,
1487 struct rpc_pipe_client *pipe_hnd,
1488 TALLOC_CTX *mem_ctx,
1492 /* TODO: what now, info2 or info3 ?
1493 convince jerry that we should add clientside setacls level 3 at least
1495 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1497 uint32_t num_printers;
1499 const char *printername, *sharename;
1500 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1501 struct policy_handle hnd_src, hnd_dst;
1502 union spoolss_PrinterInfo *info_enum;
1503 struct cli_state *cli_dst = NULL;
1504 union spoolss_PrinterInfo info_src, info_dst;
1506 DEBUG(3,("copying printer ACLs\n"));
1508 /* connect destination PI_SPOOLSS */
1509 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1510 &ndr_table_spoolss.syntax_id);
1511 if (!NT_STATUS_IS_OK(nt_status))
1515 /* enum source printers */
1516 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1517 nt_status = NT_STATUS_UNSUCCESSFUL;
1521 if (!num_printers) {
1522 printf (_("no printers found on server.\n"));
1523 nt_status = NT_STATUS_OK;
1527 /* do something for all printers */
1528 for (i = 0; i < num_printers; i++) {
1530 /* do some initialization */
1531 printername = info_enum[i].info2.printername;
1532 sharename = info_enum[i].info2.sharename;
1534 if (!printername || !sharename) {
1535 nt_status = NT_STATUS_UNSUCCESSFUL;
1539 /* we can reset NT_STATUS here because we do not
1540 get any real NT_STATUS-codes anymore from now on */
1541 nt_status = NT_STATUS_UNSUCCESSFUL;
1543 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1544 printername, sharename);
1546 /* according to msdn you have specify these access-rights
1547 to see the security descriptor
1548 - READ_CONTROL (DACL)
1549 - ACCESS_SYSTEM_SECURITY (SACL)
1552 /* open src printer handle */
1553 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1554 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1557 /* open dst printer handle */
1558 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1559 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1562 /* check for existing dst printer */
1563 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1566 /* check for existing src printer */
1567 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1570 /* Copy Security Descriptor */
1572 /* copy secdesc (info level 2) */
1573 info_dst.info2.devmode = NULL;
1574 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1577 display_sec_desc(info_dst.info2.secdesc);
1579 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1582 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1585 /* close printer handles here */
1586 if (is_valid_policy_hnd(&hnd_src)) {
1587 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1590 if (is_valid_policy_hnd(&hnd_dst)) {
1591 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1596 nt_status = NT_STATUS_OK;
1600 if (is_valid_policy_hnd(&hnd_src)) {
1601 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1604 if (is_valid_policy_hnd(&hnd_dst)) {
1605 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1609 cli_shutdown(cli_dst);
1615 * Migrate printer-forms from a src server to the dst server
1617 * All parameters are provided by the run_rpc_command function, except for
1618 * argc, argv which are passed through.
1620 * @param c A net_context structure
1621 * @param domain_sid The domain sid aquired from the remote server
1622 * @param cli A cli_state connected to the server.
1623 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1624 * @param argc Standard main() style argc
1625 * @param argv Standard main() style argv. Initial components are already
1628 * @return Normal NTSTATUS return.
1631 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1632 const DOM_SID *domain_sid,
1633 const char *domain_name,
1634 struct cli_state *cli,
1635 struct rpc_pipe_client *pipe_hnd,
1636 TALLOC_CTX *mem_ctx,
1640 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1643 uint32_t num_printers;
1645 const char *printername, *sharename;
1646 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1647 struct policy_handle hnd_src, hnd_dst;
1648 union spoolss_PrinterInfo *info_enum;
1649 union spoolss_PrinterInfo info_dst;
1651 union spoolss_FormInfo *forms;
1652 struct cli_state *cli_dst = NULL;
1654 DEBUG(3,("copying forms\n"));
1656 /* connect destination PI_SPOOLSS */
1657 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1658 &ndr_table_spoolss.syntax_id);
1659 if (!NT_STATUS_IS_OK(nt_status))
1662 /* enum src printers */
1663 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1664 nt_status = NT_STATUS_UNSUCCESSFUL;
1668 if (!num_printers) {
1669 printf (_("no printers found on server.\n"));
1670 nt_status = NT_STATUS_OK;
1674 /* do something for all printers */
1675 for (i = 0; i < num_printers; i++) {
1677 /* do some initialization */
1678 printername = info_enum[i].info2.printername;
1679 sharename = info_enum[i].info2.sharename;
1681 if (!printername || !sharename) {
1682 nt_status = NT_STATUS_UNSUCCESSFUL;
1685 /* we can reset NT_STATUS here because we do not
1686 get any real NT_STATUS-codes anymore from now on */
1687 nt_status = NT_STATUS_UNSUCCESSFUL;
1689 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1690 printername, sharename);
1693 /* open src printer handle */
1694 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1695 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1698 /* open dst printer handle */
1699 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1700 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1703 /* check for existing dst printer */
1704 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1707 /* finally migrate forms */
1708 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1711 DEBUG(1,("got %d forms for printer\n", num_forms));
1714 for (f = 0; f < num_forms; f++) {
1716 union spoolss_AddFormInfo info;
1719 /* only migrate FORM_PRINTER types, according to jerry
1720 FORM_BUILTIN-types are hard-coded in samba */
1721 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1725 d_printf(_("\tmigrating form # %d [%s] of type "
1727 f, forms[f].info1.form_name,
1728 forms[f].info1.flags);
1730 info.info1 = (struct spoolss_AddFormInfo1 *)
1731 (void *)&forms[f].info1;
1733 /* FIXME: there might be something wrong with samba's
1735 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1740 if (!W_ERROR_IS_OK(result)) {
1741 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1742 f, forms[f].info1.form_name);
1746 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1747 forms[f].info1.form_name));
1751 /* close printer handles here */
1752 if (is_valid_policy_hnd(&hnd_src)) {
1753 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1756 if (is_valid_policy_hnd(&hnd_dst)) {
1757 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1761 nt_status = NT_STATUS_OK;
1765 if (is_valid_policy_hnd(&hnd_src))
1766 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1768 if (is_valid_policy_hnd(&hnd_dst))
1769 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1772 cli_shutdown(cli_dst);
1778 * Migrate printer-drivers from a src server to the dst server
1780 * All parameters are provided by the run_rpc_command function, except for
1781 * argc, argv which are passed through.
1783 * @param c A net_context structure
1784 * @param domain_sid The domain sid aquired from the remote server
1785 * @param cli A cli_state connected to the server.
1786 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1787 * @param argc Standard main() style argc
1788 * @param argv Standard main() style argv. Initial components are already
1791 * @return Normal NTSTATUS return.
1794 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1795 const DOM_SID *domain_sid,
1796 const char *domain_name,
1797 struct cli_state *cli,
1798 struct rpc_pipe_client *pipe_hnd,
1799 TALLOC_CTX *mem_ctx,
1803 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1805 uint32_t num_printers;
1807 const char *printername, *sharename;
1808 bool got_src_driver_share = false;
1809 bool got_dst_driver_share = false;
1810 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1811 struct policy_handle hnd_src, hnd_dst;
1812 union spoolss_DriverInfo drv_info_src;
1813 union spoolss_PrinterInfo *info_enum;
1814 union spoolss_PrinterInfo info_dst;
1815 struct cli_state *cli_dst = NULL;
1816 struct cli_state *cli_share_src = NULL;
1817 struct cli_state *cli_share_dst = NULL;
1818 const char *drivername = NULL;
1820 DEBUG(3,("copying printer-drivers\n"));
1822 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1823 &ndr_table_spoolss.syntax_id);
1824 if (!NT_STATUS_IS_OK(nt_status))
1827 /* open print$-share on the src server */
1828 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1829 cli->desthost, "print$", "A:");
1830 if (!NT_STATUS_IS_OK(nt_status))
1833 got_src_driver_share = true;
1836 /* open print$-share on the dst server */
1837 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1838 cli_dst->desthost, "print$", "A:");
1839 if (!NT_STATUS_IS_OK(nt_status))
1842 got_dst_driver_share = true;
1845 /* enum src printers */
1846 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1847 nt_status = NT_STATUS_UNSUCCESSFUL;
1851 if (num_printers == 0) {
1852 printf (_("no printers found on server.\n"));
1853 nt_status = NT_STATUS_OK;
1858 /* do something for all printers */
1859 for (p = 0; p < num_printers; p++) {
1861 /* do some initialization */
1862 printername = info_enum[p].info2.printername;
1863 sharename = info_enum[p].info2.sharename;
1865 if (!printername || !sharename) {
1866 nt_status = NT_STATUS_UNSUCCESSFUL;
1870 /* we can reset NT_STATUS here because we do not
1871 get any real NT_STATUS-codes anymore from now on */
1872 nt_status = NT_STATUS_UNSUCCESSFUL;
1874 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1875 printername, sharename);
1877 /* open dst printer handle */
1878 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1879 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1882 /* check for existing dst printer */
1883 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1887 /* open src printer handle */
1888 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1889 MAXIMUM_ALLOWED_ACCESS,
1890 pipe_hnd->auth->user_name,
1894 /* in a first step call getdriver for each shared printer (per arch)
1895 to get a list of all files that have to be copied */
1897 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1900 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1901 level, archi_table[i].long_archi,
1902 archi_table[i].version, &drv_info_src))
1905 drivername = drv_info_src.info3.driver_name;
1908 display_print_driver3(&drv_info_src.info3);
1910 /* check arch dir */
1911 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1912 if (!NT_STATUS_IS_OK(nt_status))
1916 /* copy driver-files */
1917 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1918 archi_table[i].short_archi,
1919 &drv_info_src.info3);
1920 if (!NT_STATUS_IS_OK(nt_status))
1925 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1926 nt_status = NT_STATUS_UNSUCCESSFUL;
1930 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1931 drivername, printername));
1935 if (!drivername || strlen(drivername) == 0) {
1936 DEBUGADD(1,("Did not get driver for printer %s\n",
1942 info_dst.info2.drivername = drivername;
1944 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1945 nt_status = NT_STATUS_UNSUCCESSFUL;
1949 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1950 drivername, printername));
1953 if (is_valid_policy_hnd(&hnd_dst)) {
1954 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1958 if (is_valid_policy_hnd(&hnd_src)) {
1959 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1963 nt_status = NT_STATUS_OK;
1967 if (is_valid_policy_hnd(&hnd_src))
1968 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1970 if (is_valid_policy_hnd(&hnd_dst))
1971 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1974 cli_shutdown(cli_dst);
1977 if (got_src_driver_share)
1978 cli_shutdown(cli_share_src);
1980 if (got_dst_driver_share)
1981 cli_shutdown(cli_share_dst);
1988 * Migrate printer-queues from a src to the dst server
1989 * (requires a working "addprinter command" to be installed for the local smbd)
1991 * All parameters are provided by the run_rpc_command function, except for
1992 * argc, argv which are passed through.
1994 * @param c A net_context structure
1995 * @param domain_sid The domain sid aquired from the remote server
1996 * @param cli A cli_state connected to the server.
1997 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1998 * @param argc Standard main() style argc
1999 * @param argv Standard main() style argv. Initial components are already
2002 * @return Normal NTSTATUS return.
2005 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
2006 const DOM_SID *domain_sid,
2007 const char *domain_name,
2008 struct cli_state *cli,
2009 struct rpc_pipe_client *pipe_hnd,
2010 TALLOC_CTX *mem_ctx,
2015 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2016 uint32_t i = 0, num_printers;
2018 union spoolss_PrinterInfo info_dst, info_src;
2019 union spoolss_PrinterInfo *info_enum;
2020 struct cli_state *cli_dst = NULL;
2021 struct policy_handle hnd_dst, hnd_src;
2022 const char *printername, *sharename;
2023 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2024 struct spoolss_SetPrinterInfoCtr info_ctr;
2026 DEBUG(3,("copying printers\n"));
2028 /* connect destination PI_SPOOLSS */
2029 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2030 &ndr_table_spoolss.syntax_id);
2031 if (!NT_STATUS_IS_OK(nt_status))
2035 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2036 nt_status = NT_STATUS_UNSUCCESSFUL;
2040 if (!num_printers) {
2041 printf (_("no printers found on server.\n"));
2042 nt_status = NT_STATUS_OK;
2046 /* do something for all printers */
2047 for (i = 0; i < num_printers; i++) {
2049 /* do some initialization */
2050 printername = info_enum[i].info2.printername;
2051 sharename = info_enum[i].info2.sharename;
2053 if (!printername || !sharename) {
2054 nt_status = NT_STATUS_UNSUCCESSFUL;
2057 /* we can reset NT_STATUS here because we do not
2058 get any real NT_STATUS-codes anymore from now on */
2059 nt_status = NT_STATUS_UNSUCCESSFUL;
2061 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2062 printername, sharename);
2064 /* open dst printer handle */
2065 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2066 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2068 DEBUG(1,("could not open printer: %s\n", sharename));
2071 /* check for existing dst printer */
2072 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2073 printf (_("could not get printer, creating printer.\n"));
2075 DEBUG(1,("printer already exists: %s\n", sharename));
2076 /* close printer handle here - dst only, not got src yet. */
2077 if (is_valid_policy_hnd(&hnd_dst)) {
2078 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2083 /* now get again src printer ctr via getprinter,
2084 we first need a handle for that */
2086 /* open src printer handle */
2087 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2088 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2091 /* getprinter on the src server */
2092 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2095 /* copy each src printer to a dst printer 1:1,
2096 maybe some values have to be changed though */
2097 d_printf(_("creating printer: %s\n"), printername);
2099 info_ctr.level = level;
2100 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)
2101 (void *)&info_src.info2;
2103 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2107 if (W_ERROR_IS_OK(result))
2108 d_printf (_("printer [%s] successfully added.\n"),
2110 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2111 d_fprintf (stderr, _("printer [%s] already exists.\n"),
2114 d_fprintf (stderr, _("could not create printer [%s]\n"),
2119 /* close printer handles here */
2120 if (is_valid_policy_hnd(&hnd_src)) {
2121 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2124 if (is_valid_policy_hnd(&hnd_dst)) {
2125 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2129 nt_status = NT_STATUS_OK;
2132 if (is_valid_policy_hnd(&hnd_src))
2133 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2135 if (is_valid_policy_hnd(&hnd_dst))
2136 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2139 cli_shutdown(cli_dst);
2145 * Migrate Printer-Settings from a src server to the dst server
2146 * (for this to work, printers and drivers already have to be migrated earlier)
2148 * All parameters are provided by the run_rpc_command function, except for
2149 * argc, argv which are passed through.
2151 * @param c A net_context structure
2152 * @param domain_sid The domain sid aquired from the remote server
2153 * @param cli A cli_state connected to the server.
2154 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2155 * @param argc Standard main() style argc
2156 * @param argv Standard main() style argv. Initial components are already
2159 * @return Normal NTSTATUS return.
2162 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2163 const DOM_SID *domain_sid,
2164 const char *domain_name,
2165 struct cli_state *cli,
2166 struct rpc_pipe_client *pipe_hnd,
2167 TALLOC_CTX *mem_ctx,
2172 /* FIXME: Here the nightmare begins */
2175 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2176 uint32_t i = 0, p = 0, j = 0;
2177 uint32_t num_printers;
2179 const char *printername, *sharename;
2180 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2181 struct policy_handle hnd_src, hnd_dst;
2182 union spoolss_PrinterInfo *info_enum;
2183 union spoolss_PrinterInfo info_dst_publish;
2184 union spoolss_PrinterInfo info_dst;
2185 struct cli_state *cli_dst = NULL;
2186 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2187 const char *longname;
2188 const char **keylist = NULL;
2191 ZERO_STRUCT(info_dst_publish);
2193 DEBUG(3,("copying printer settings\n"));
2195 /* connect destination PI_SPOOLSS */
2196 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2197 &ndr_table_spoolss.syntax_id);
2198 if (!NT_STATUS_IS_OK(nt_status))
2201 /* enum src printers */
2202 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2203 nt_status = NT_STATUS_UNSUCCESSFUL;
2207 if (!num_printers) {
2208 printf (_("no printers found on server.\n"));
2209 nt_status = NT_STATUS_OK;
2214 /* needed for dns-strings in regkeys */
2215 longname = get_mydnsfullname();
2217 nt_status = NT_STATUS_UNSUCCESSFUL;
2221 /* do something for all printers */
2222 for (i = 0; i < num_printers; i++) {
2224 uint32_t value_offered = 0, value_needed;
2225 uint32_t data_offered = 0, data_needed;
2226 enum winreg_Type type;
2227 uint8_t *buffer = NULL;
2228 const char *value_name = NULL;
2230 /* do some initialization */
2231 printername = info_enum[i].info2.printername;
2232 sharename = info_enum[i].info2.sharename;
2234 if (!printername || !sharename) {
2235 nt_status = NT_STATUS_UNSUCCESSFUL;
2238 /* we can reset NT_STATUS here because we do not
2239 get any real NT_STATUS-codes anymore from now on */
2240 nt_status = NT_STATUS_UNSUCCESSFUL;
2242 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2243 printername, sharename);
2246 /* open src printer handle */
2247 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2248 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2251 /* open dst printer handle */
2252 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2253 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2256 /* check for existing dst printer */
2257 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2262 /* STEP 1: COPY DEVICE-MODE and other
2263 PRINTER_INFO_2-attributes
2266 info_dst.info2 = info_enum[i].info2;
2268 /* why is the port always disconnected when the printer
2269 is correctly installed (incl. driver ???) */
2270 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2272 /* check if printer is published */
2273 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2275 /* check for existing dst printer */
2276 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2279 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2281 /* ignore false from setprinter due to WERR_IO_PENDING */
2282 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2284 DEBUG(3,("republished printer\n"));
2287 if (info_enum[i].info2.devmode != NULL) {
2289 /* copy devmode (info level 2) */
2290 info_dst.info2.devmode = info_enum[i].info2.devmode;
2292 /* do not copy security descriptor (we have another
2293 * command for that) */
2294 info_dst.info2.secdesc = NULL;
2297 info_dst.info2.devmode.devicename =
2298 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2299 longname, printername);
2300 if (!info_dst.info2.devmode.devicename) {
2301 nt_status = NT_STATUS_NO_MEMORY;
2305 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2309 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2312 /* STEP 2: COPY REGISTRY VALUES */
2314 /* please keep in mind that samba parse_spools gives horribly
2315 crippled results when used to rpccli_spoolss_enumprinterdataex
2316 a win2k3-server. (Bugzilla #1851)
2317 FIXME: IIRC I've seen it too on a win2k-server
2320 /* enumerate data on src handle */
2321 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2333 data_offered = data_needed;
2334 value_offered = value_needed;
2335 buffer = talloc_zero_array(mem_ctx, uint8_t, data_needed);
2336 value_name = talloc_zero_array(mem_ctx, char, value_needed);
2338 /* loop for all printerdata of "PrinterDriverData" */
2339 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2341 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2352 /* loop for all reg_keys */
2353 if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2355 struct regval_blob v;
2358 if (c->opt_verbose) {
2359 fstrcpy(v.valuename, value_name);
2361 v.size = data_offered;
2363 display_reg_value(SPOOL_PRINTERDATA_KEY, v);
2367 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2368 &hnd_dst, value_name,
2369 type, buffer, data_offered))
2372 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2377 /* STEP 3: COPY SUBKEY VALUES */
2379 /* here we need to enum all printer_keys and then work
2380 on the result with enum_printer_key_ex. nt4 does not
2381 respond to enumprinterkey, win2k does, so continue
2382 in case of an error */
2384 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2385 printf(_("got no key-data\n"));
2390 /* work on a list of printer keys
2391 each key has to be enumerated to get all required
2392 information. information is then set via setprinterdataex-calls */
2394 if (keylist == NULL)
2397 for (i=0; keylist && keylist[i] != NULL; i++) {
2399 const char *subkey = keylist[i];
2401 struct spoolss_PrinterEnumValues *info;
2403 /* enumerate all src subkeys */
2404 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2410 for (j=0; j < count; j++) {
2412 struct regval_blob value;
2415 /* although samba replies with sane data in most cases we
2416 should try to avoid writing wrong registry data */
2418 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME) ||
2419 strequal(info[j].value_name, SPOOL_REG_UNCNAME) ||
2420 strequal(info[j].value_name, SPOOL_REG_URL) ||
2421 strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME) ||
2422 strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2424 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
2426 /* although windows uses a multi-sz, we use a sz */
2427 push_reg_sz(mem_ctx, &blob, SAMBA_PRINTER_PORT_NAME);
2428 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2431 if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
2433 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2434 nt_status = NT_STATUS_NO_MEMORY;
2437 push_reg_sz(mem_ctx, &blob, unc_name);
2438 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2441 if (strequal(info[j].value_name, SPOOL_REG_URL)) {
2446 /* FIXME: should we really do that ??? */
2447 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2448 nt_status = NT_STATUS_NO_MEMORY;
2451 push_reg_sz(mem_ctx, NULL, &blob, url);
2452 fstrcpy(value.valuename, SPOOL_REG_URL);
2456 if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2458 push_reg_sz(mem_ctx, &blob, longname);
2459 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2462 if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
2464 push_reg_sz(mem_ctx, &blob, global_myname());
2465 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2468 value.type = REG_SZ;
2469 value.size = blob.length;
2471 value.data_p = blob.data;
2473 value.data_p = NULL;
2477 display_reg_value(subkey, value);
2479 /* here we have to set all subkeys on the dst server */
2480 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2486 struct regval_blob v;
2488 fstrcpy(v.valuename, info[j].value_name);
2489 v.type = info[j].type;
2490 v.data_p = info[j].data->data;
2491 v.size = info[j].data->length;
2493 if (c->opt_verbose) {
2494 display_reg_value(subkey, v);
2497 /* here we have to set all subkeys on the dst server */
2498 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2505 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2506 subkey, info[j].value_name));
2511 TALLOC_FREE(keylist);
2513 /* close printer handles here */
2514 if (is_valid_policy_hnd(&hnd_src)) {
2515 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2518 if (is_valid_policy_hnd(&hnd_dst)) {
2519 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2524 nt_status = NT_STATUS_OK;
2527 SAFE_FREE(devicename);
2529 SAFE_FREE(unc_name);
2531 if (is_valid_policy_hnd(&hnd_src))
2532 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2534 if (is_valid_policy_hnd(&hnd_dst))
2535 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2538 cli_shutdown(cli_dst);