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"
24 /* support itanium as well */
25 static const struct print_architecture_table_node archi_table[]= {
27 {"Windows 4.0", "WIN40", 0 },
28 {"Windows NT x86", "W32X86", 2 },
29 {"Windows NT x86", "W32X86", 3 },
30 {"Windows NT R4000", "W32MIPS", 2 },
31 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
32 {"Windows NT PowerPC", "W32PPC", 2 },
33 {"Windows IA64", "IA64", 3 },
34 {"Windows x64", "x64", 3 },
40 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
41 * It is here for debugging purpose and should be removed later on.
44 /****************************************************************************
45 Printer info level 3 display function.
46 ****************************************************************************/
48 static void display_print_driver3(struct spoolss_DriverInfo3 *r)
56 printf(_("Printer Driver Info 3:\n"));
57 printf(_("\tVersion: [%x]\n"), r->version);
58 printf(_("\tDriver Name: [%s]\n"), r->driver_name);
59 printf(_("\tArchitecture: [%s]\n"), r->architecture);
60 printf(_("\tDriver Path: [%s]\n"), r->driver_path);
61 printf(_("\tDatafile: [%s]\n"), r->data_file);
62 printf(_("\tConfigfile: [%s]\n\n"), r->config_file);
63 printf(_("\tHelpfile: [%s]\n\n"), r->help_file);
65 for (i=0; r->dependent_files[i] != NULL; i++) {
66 printf(_("\tDependentfiles: [%s]\n"), r->dependent_files[i]);
71 printf(_("\tMonitorname: [%s]\n"), r->monitor_name);
72 printf(_("\tDefaultdatatype: [%s]\n\n"), r->default_datatype);
75 static void display_reg_value(const char *subkey, struct regval_blob value)
82 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey,
83 value.valuename, *((uint32_t *) value.data_p));
87 blob = data_blob_const(value.data_p, value.size);
88 pull_reg_sz(talloc_tos(), &blob, &text);
92 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, value.valuename,
97 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
99 subkey, value.valuename);
105 blob = data_blob_const(value.data_p, value.size);
107 if (!pull_reg_multi_sz(NULL, &blob, &values)) {
108 d_printf("pull_reg_multi_sz failed\n");
112 printf("%s: REG_MULTI_SZ: \n", value.valuename);
113 for (i=0; values[i] != NULL; i++) {
114 d_printf("%s\n", values[i]);
121 d_printf(_("\t%s: unknown type %d\n"), value.valuename,
128 * Copies ACLs, DOS-attributes and timestamps from one
129 * file or directory from one connected share to another connected share
131 * @param c A net_context structure
132 * @param mem_ctx A talloc-context
133 * @param cli_share_src A connected cli_state
134 * @param cli_share_dst A connected cli_state
135 * @param src_file The source file-name
136 * @param dst_file The destination file-name
137 * @param copy_acls Whether to copy acls
138 * @param copy_attrs Whether to copy DOS attributes
139 * @param copy_timestamps Whether to preserve timestamps
140 * @param is_file Whether this file is a file or a dir
142 * @return Normal NTSTATUS return.
145 NTSTATUS net_copy_fileattr(struct net_context *c,
147 struct cli_state *cli_share_src,
148 struct cli_state *cli_share_dst,
149 const char *src_name, const char *dst_name,
150 bool copy_acls, bool copy_attrs,
151 bool copy_timestamps, bool is_file)
153 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
154 uint16_t fnum_src = 0;
155 uint16_t fnum_dst = 0;
158 time_t f_atime, f_ctime, f_mtime;
161 if (!copy_timestamps && !copy_acls && !copy_attrs)
164 /* open file/dir on the originating server */
166 DEBUGADD(3,("opening %s %s on originating server\n",
167 is_file?"file":"dir", src_name));
169 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
170 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src))) {
171 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
172 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
173 nt_status = cli_nt_error(cli_share_src);
180 /* get the security descriptor */
181 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
183 DEBUG(0,("failed to get security descriptor: %s\n",
184 cli_errstr(cli_share_src)));
185 nt_status = cli_nt_error(cli_share_src);
189 if (c->opt_verbose && DEBUGLEVEL >= 3)
190 display_sec_desc(sd);
194 if (copy_attrs || copy_timestamps) {
196 /* get file attributes */
197 if (!NT_STATUS_IS_OK(cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
198 &f_ctime, &f_atime, &f_mtime))) {
199 DEBUG(0,("failed to get file-attrs: %s\n",
200 cli_errstr(cli_share_src)));
201 nt_status = cli_nt_error(cli_share_src);
207 /* open the file/dir on the destination server */
209 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_dst, dst_name, 0, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
210 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_dst))) {
211 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
212 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
213 nt_status = cli_nt_error(cli_share_dst);
217 if (copy_timestamps) {
220 if (!NT_STATUS_IS_OK(cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime))) {
221 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
222 cli_errstr(cli_share_dst)));
223 nt_status = cli_nt_error(cli_share_dst);
231 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
232 DEBUG(0,("could not set secdesc on %s: %s\n",
233 dst_name, cli_errstr(cli_share_dst)));
234 nt_status = cli_nt_error(cli_share_dst);
242 if (!NT_STATUS_IS_OK(cli_setatr(cli_share_dst, dst_name, attr, 0))) {
243 DEBUG(0,("failed to set file-attrs: %s\n",
244 cli_errstr(cli_share_dst)));
245 nt_status = cli_nt_error(cli_share_dst);
253 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
255 _("could not close %s on originating server: %s\n"),
256 is_file?"file":"dir", cli_errstr(cli_share_src));
257 nt_status = cli_nt_error(cli_share_src);
261 if (!NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
263 _("could not close %s on destination server: %s\n"),
264 is_file?"file":"dir", cli_errstr(cli_share_dst));
265 nt_status = cli_nt_error(cli_share_dst);
270 nt_status = NT_STATUS_OK;
276 cli_close(cli_share_src, fnum_src);
279 cli_close(cli_share_dst, fnum_dst);
285 * Copy a file or directory from a connected share to another connected share
287 * @param c A net_context structure
288 * @param mem_ctx A talloc-context
289 * @param cli_share_src A connected cli_state
290 * @param cli_share_dst A connected cli_state
291 * @param src_file The source file-name
292 * @param dst_file The destination file-name
293 * @param copy_acls Whether to copy acls
294 * @param copy_attrs Whether to copy DOS attributes
295 * @param copy_timestamps Whether to preserve timestamps
296 * @param is_file Whether this file is a file or a dir
298 * @return Normal NTSTATUS return.
301 NTSTATUS net_copy_file(struct net_context *c,
303 struct cli_state *cli_share_src,
304 struct cli_state *cli_share_dst,
305 const char *src_name, const char *dst_name,
306 bool copy_acls, bool copy_attrs,
307 bool copy_timestamps, bool is_file)
309 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
310 uint16_t fnum_src = 0;
311 uint16_t fnum_dst = 0;
312 static int io_bufsize = 64512;
313 int read_size = io_bufsize;
318 if (!src_name || !dst_name)
321 if (cli_share_src == NULL || cli_share_dst == NULL)
324 /* open on the originating server */
325 DEBUGADD(3,("opening %s %s on originating server\n",
326 is_file ? "file":"dir", src_name));
328 nt_status = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE, &fnum_src);
330 nt_status = cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
331 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src);
333 if (!NT_STATUS_IS_OK(nt_status)) {
334 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
335 is_file ? "file":"dir",
336 src_name, cli_errstr(cli_share_src)));
343 /* open file on the destination server */
344 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
345 nt_status = cli_open(cli_share_dst, dst_name,
346 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum_dst);
348 if (!NT_STATUS_IS_OK(nt_status)) {
349 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
350 dst_name, cli_errstr(cli_share_dst)));
354 /* allocate memory */
355 if (!(data = (char *)SMB_MALLOC(read_size))) {
356 d_fprintf(stderr, _("malloc fail for size %d\n"),
358 nt_status = NT_STATUS_NO_MEMORY;
365 if (c->opt_verbose) {
367 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
368 "%s ACLs and %s DOS Attributes %s\n"),
369 cli_share_src->desthost, cli_share_src->share, src_name,
370 cli_share_dst->desthost, cli_share_dst->share, dst_name,
371 copy_acls ? _("with") : _("without"),
372 copy_attrs ? _("with") : _("without"),
373 copy_timestamps ? _("(preserving timestamps)") : "" );
381 n = cli_read(cli_share_src, fnum_src, data, nread,
387 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
391 d_fprintf(stderr, _("Error writing file: %s\n"),
392 cli_errstr(cli_share_dst));
393 nt_status = cli_nt_error(cli_share_dst);
401 if (!is_file && !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
404 DEBUGADD(3,("creating dir %s on the destination server\n",
407 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share_dst, dst_name))) {
408 DEBUG(0,("cannot create directory %s: %s\n",
409 dst_name, cli_errstr(cli_share_dst)));
410 nt_status = NT_STATUS_NO_SUCH_FILE;
413 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
415 _("cannot check for directory %s: %s\n"),
416 dst_name, cli_errstr(cli_share_dst));
423 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
425 _("could not close file on originating server: %s\n"),
426 cli_errstr(cli_share_src));
427 nt_status = cli_nt_error(cli_share_src);
431 if (is_file && !NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
433 _("could not close file on destination server: %s\n"),
434 cli_errstr(cli_share_dst));
435 nt_status = cli_nt_error(cli_share_dst);
439 /* possibly we have to copy some file-attributes / acls / sd */
440 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
441 src_name, dst_name, copy_acls,
442 copy_attrs, copy_timestamps, is_file);
443 if (!NT_STATUS_IS_OK(nt_status))
447 nt_status = NT_STATUS_OK;
453 cli_close(cli_share_src, fnum_src);
456 cli_close(cli_share_dst, fnum_dst);
464 * Copy a driverfile from on connected share to another connected share
465 * This silently assumes that a driver-file is picked up from
467 * \\src_server\print$\{arch}\{version}\file
471 * \\dst_server\print$\{arch}\file
473 * to be added via setdriver-calls later.
474 * @param c A net_context structure
475 * @param mem_ctx A talloc-context
476 * @param cli_share_src A cli_state connected to source print$-share
477 * @param cli_share_dst A cli_state connected to destination print$-share
478 * @param file The file-name to be copied
479 * @param short_archi The name of the driver-architecture (short form)
481 * @return Normal NTSTATUS return.
484 static NTSTATUS net_copy_driverfile(struct net_context *c,
486 struct cli_state *cli_share_src,
487 struct cli_state *cli_share_dst,
488 const char *file, const char *short_archi) {
490 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
502 /* scroll through the file until we have the part
503 beyond archi_table.short_archi */
505 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
506 if (strequal(tok, short_archi)) {
507 next_token_talloc(mem_ctx, &p, &version, "\\");
508 next_token_talloc(mem_ctx, &p, &filename, "\\");
512 /* build source file name */
513 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
514 return NT_STATUS_NO_MEMORY;
517 /* create destination file name */
518 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
519 return NT_STATUS_NO_MEMORY;
522 /* finally copy the file */
523 nt_status = net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
524 src_name, dst_name, false, false, false, true);
525 if (!NT_STATUS_IS_OK(nt_status))
528 nt_status = NT_STATUS_OK;
538 * Check for existing Architecture directory on a given server
540 * @param cli_share A cli_state connected to a print$-share
541 * @param short_archi The Architecture for the print-driver
543 * @return Normal NTSTATUS return.
546 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
549 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
552 if (asprintf(&dir, "\\%s", short_archi) < 0) {
553 return NT_STATUS_NO_MEMORY;
556 DEBUG(10,("creating print-driver dir for architecture: %s\n",
559 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share, dir))) {
560 DEBUG(1,("cannot create directory %s: %s\n",
561 dir, cli_errstr(cli_share)));
562 nt_status = NT_STATUS_NO_SUCH_FILE;
565 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share, dir))) {
566 d_fprintf(stderr, _("cannot check %s: %s\n"),
567 dir, cli_errstr(cli_share));
571 nt_status = NT_STATUS_OK;
579 * Copy a print-driver (level 3) from one connected print$-share to another
580 * connected print$-share
582 * @param c A net_context structure
583 * @param mem_ctx A talloc-context
584 * @param cli_share_src A cli_state connected to a print$-share
585 * @param cli_share_dst A cli_state connected to a print$-share
586 * @param short_archi The Architecture for the print-driver
587 * @param i1 The DRIVER_INFO_3-struct
589 * @return Normal NTSTATUS return.
592 static NTSTATUS copy_print_driver_3(struct net_context *c,
594 struct cli_state *cli_share_src,
595 struct cli_state *cli_share_dst,
596 const char *short_archi,
597 struct spoolss_DriverInfo3 *r)
599 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
607 d_printf(_("copying driver: [%s], for architecture: [%s], "
609 r->driver_name, short_archi, r->version);
611 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
612 r->driver_path, short_archi);
613 if (!NT_STATUS_IS_OK(nt_status))
616 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
617 r->data_file, short_archi);
618 if (!NT_STATUS_IS_OK(nt_status))
621 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
622 r->config_file, short_archi);
623 if (!NT_STATUS_IS_OK(nt_status))
626 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
627 r->help_file, short_archi);
628 if (!NT_STATUS_IS_OK(nt_status))
631 for (i=0; r->dependent_files[i] != NULL; i++) {
633 nt_status = net_copy_driverfile(c, mem_ctx,
634 cli_share_src, cli_share_dst,
635 r->dependent_files[i], short_archi);
636 if (!NT_STATUS_IS_OK(nt_status)) {
645 * net_spoolss-functions
646 * =====================
648 * the net_spoolss-functions aim to simplify spoolss-client-functions
649 * required during the migration-process wrt buffer-sizes, returned
652 * this greatly reduces the complexitiy of the migrate-functions.
656 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
661 uint32_t *num_printers,
662 union spoolss_PrinterInfo **info)
668 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
675 if (!W_ERROR_IS_OK(result)) {
676 printf(_("cannot enum printers: %s\n"), win_errstr(result));
683 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
685 const char *printername,
686 uint32_t access_required,
687 const char *username,
688 struct policy_handle *hnd)
691 fstring printername2;
693 fstrcpy(printername2, pipe_hnd->srv_name_slash);
694 fstrcat(printername2, "\\");
695 fstrcat(printername2, printername);
697 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
698 pipe_hnd->srv_name_slash, username, printername2, access_required));
701 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
706 /* be more verbose */
707 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
709 _("no access to printer [%s] on [%s] for user [%s] "
711 printername2, pipe_hnd->srv_name_slash, username);
715 if (!W_ERROR_IS_OK(result)) {
716 d_fprintf(stderr,_("cannot open printer %s on server %s: %s\n"),
717 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
721 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
722 printername2, pipe_hnd->srv_name_slash));
727 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
729 struct policy_handle *hnd,
731 union spoolss_PrinterInfo *info)
735 /* getprinter call */
736 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
741 if (!W_ERROR_IS_OK(result)) {
742 printf(_("cannot get printer-info: %s\n"), win_errstr(result));
749 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
751 struct policy_handle *hnd,
753 union spoolss_PrinterInfo *info)
757 struct spoolss_SetPrinterInfoCtr info_ctr;
758 struct spoolss_DevmodeContainer devmode_ctr;
759 struct sec_desc_buf secdesc_ctr;
761 ZERO_STRUCT(devmode_ctr);
762 ZERO_STRUCT(secdesc_ctr);
764 /* setprinter call */
766 info_ctr.level = level;
769 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)
770 (void *)&info->info0;
773 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)
774 (void *)&info->info1;
777 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)
778 (void *)&info->info2;
781 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
782 (void *)&info->info3;
785 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)
786 (void *)&info->info4;
789 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)
790 (void *)&info->info5;
793 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)
794 (void *)&info->info6;
797 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
798 (void *)&info->info7;
802 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)
803 (void *)&info->info8;
806 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)
807 (void *)&info->info9;
814 status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
822 if (!W_ERROR_IS_OK(result)) {
823 printf(_("cannot set printer-info: %s\n"), win_errstr(result));
831 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
833 struct policy_handle *hnd,
834 const char *value_name,
835 enum winreg_Type type,
842 /* setprinterdata call */
843 status = rpccli_spoolss_SetPrinterData(pipe_hnd, mem_ctx,
851 if (!W_ERROR_IS_OK(result)) {
852 printf (_("unable to set printerdata: %s\n"),
861 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
863 struct policy_handle *hnd,
865 const char ***keylist)
869 /* enumprinterkey call */
870 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
872 if (!W_ERROR_IS_OK(result)) {
873 printf(_("enumprinterkey failed: %s\n"), win_errstr(result));
880 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
883 struct policy_handle *hnd,
886 struct spoolss_PrinterEnumValues **info)
890 /* enumprinterdataex call */
891 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
898 if (!W_ERROR_IS_OK(result)) {
899 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result));
907 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
909 struct policy_handle *hnd,
911 struct regval_blob *value)
916 /* setprinterdataex call */
917 status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
926 if (!W_ERROR_IS_OK(result)) {
927 printf(_("could not set printerdataex: %s\n"),
935 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
937 struct policy_handle *hnd,
940 union spoolss_FormInfo **forms)
945 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
951 if (!W_ERROR_IS_OK(result)) {
952 printf(_("could not enum forms: %s\n"), win_errstr(result));
959 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
961 uint32_t level, const char *env,
963 union spoolss_DriverInfo **info)
967 /* enumprinterdrivers call */
968 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
969 pipe_hnd->srv_name_slash,
975 if (!W_ERROR_IS_OK(result)) {
976 printf(_("cannot enum drivers: %s\n"), win_errstr(result));
983 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
985 struct policy_handle *hnd, uint32_t level,
986 const char *env, int version,
987 union spoolss_DriverInfo *info)
990 uint32_t server_major_version;
991 uint32_t server_minor_version;
993 /* getprinterdriver call */
994 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
1002 &server_major_version,
1003 &server_minor_version);
1004 if (!W_ERROR_IS_OK(result)) {
1005 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1006 env, win_errstr(result)));
1007 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1008 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1009 printf(_("cannot get driver: %s\n"),
1010 win_errstr(result));
1019 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1020 TALLOC_CTX *mem_ctx, uint32_t level,
1021 union spoolss_DriverInfo *info)
1025 struct spoolss_AddDriverInfoCtr info_ctr;
1027 info_ctr.level = level;
1031 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)
1032 (void *)&info->info2;
1035 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)
1036 (void *)&info->info3;
1039 printf(_("unsupported info level: %d\n"), level);
1043 /* addprinterdriver call */
1044 status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1045 pipe_hnd->srv_name_slash,
1048 /* be more verbose */
1049 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1050 printf(_("You are not allowed to add drivers\n"));
1053 if (!W_ERROR_IS_OK(result)) {
1054 printf(_("cannot add driver: %s\n"), win_errstr(result));
1062 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1063 * for a single printer or for all printers depending on argc/argv
1066 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1067 TALLOC_CTX *mem_ctx,
1071 uint32_t *num_printers,
1072 union spoolss_PrinterInfo **info_p)
1074 struct policy_handle hnd;
1076 /* no arguments given, enumerate all printers */
1079 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1080 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1081 level, num_printers, info_p))
1087 /* argument given, get a single printer by name */
1088 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1089 MAXIMUM_ALLOWED_ACCESS,
1090 pipe_hnd->auth->user_name,
1094 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1095 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1099 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1104 DEBUG(3,("got %d printers\n", *num_printers));
1111 * List print-queues (including local printers that are not shared)
1113 * All parameters are provided by the run_rpc_command function, except for
1114 * argc, argv which are passed through.
1116 * @param c A net_context structure
1117 * @param domain_sid The domain sid aquired from the remote server
1118 * @param cli A cli_state connected to the server.
1119 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1120 * @param argc Standard main() style argc
1121 * @param argv Standard main() style argv. Initial components are already
1124 * @return Normal NTSTATUS return.
1127 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1128 const DOM_SID *domain_sid,
1129 const char *domain_name,
1130 struct cli_state *cli,
1131 struct rpc_pipe_client *pipe_hnd,
1132 TALLOC_CTX *mem_ctx,
1136 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1137 uint32_t i, num_printers;
1139 const char *printername, *sharename;
1140 union spoolss_PrinterInfo *info;
1142 printf("listing printers\n");
1144 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1147 for (i = 0; i < num_printers; i++) {
1149 /* do some initialization */
1150 printername = info[i].info2.printername;
1151 sharename = info[i].info2.sharename;
1153 if (printername && sharename) {
1154 d_printf(_("printer %d: %s, shared as: %s\n"),
1155 i+1, printername, sharename);
1159 return NT_STATUS_OK;
1163 * List printer-drivers from a server
1165 * All parameters are provided by the run_rpc_command function, except for
1166 * argc, argv which are passed through.
1168 * @param c A net_context structure
1169 * @param domain_sid The domain sid aquired from the remote server
1170 * @param cli A cli_state connected to the server.
1171 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1172 * @param argc Standard main() style argc
1173 * @param argv Standard main() style argv. Initial components are already
1176 * @return Normal NTSTATUS return.
1179 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1180 const DOM_SID *domain_sid,
1181 const char *domain_name,
1182 struct cli_state *cli,
1183 struct rpc_pipe_client *pipe_hnd,
1184 TALLOC_CTX *mem_ctx,
1188 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1191 union spoolss_DriverInfo *info;
1194 printf(_("listing printer-drivers\n"));
1196 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1198 uint32_t num_drivers;
1200 /* enum remote drivers */
1201 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1202 archi_table[i].long_archi,
1203 &num_drivers, &info)) {
1204 nt_status = NT_STATUS_UNSUCCESSFUL;
1208 if (num_drivers == 0) {
1209 d_printf(_("no drivers found on server for "
1210 "architecture: [%s].\n"),
1211 archi_table[i].long_archi);
1215 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1216 num_drivers, archi_table[i].long_archi);
1219 /* do something for all drivers for architecture */
1220 for (d = 0; d < num_drivers; d++) {
1221 display_print_driver3(&info[d].info3);
1225 nt_status = NT_STATUS_OK;
1233 * Publish print-queues with args-wrapper
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
1242 * @return Normal NTSTATUS return.
1245 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1246 TALLOC_CTX *mem_ctx,
1251 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1252 uint32_t i, num_printers;
1254 const char *printername, *sharename;
1255 union spoolss_PrinterInfo *info_enum;
1256 union spoolss_PrinterInfo info;
1257 struct spoolss_SetPrinterInfoCtr info_ctr;
1258 struct spoolss_DevmodeContainer devmode_ctr;
1259 struct sec_desc_buf secdesc_ctr;
1260 struct policy_handle hnd;
1262 const char *action_str;
1264 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1267 for (i = 0; i < num_printers; i++) {
1269 /* do some initialization */
1270 printername = info_enum[i].info2.printername;
1271 sharename = info_enum[i].info2.sharename;
1272 if (!printername || !sharename) {
1276 /* open printer handle */
1277 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1278 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1281 /* check for existing dst printer */
1282 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1285 /* check action and set string */
1287 case DSPRINT_PUBLISH:
1288 action_str = N_("published");
1290 case DSPRINT_UPDATE:
1291 action_str = N_("updated");
1293 case DSPRINT_UNPUBLISH:
1294 action_str = N_("unpublished");
1297 action_str = N_("unknown action");
1298 printf(_("unkown action: %d\n"), action);
1302 info.info7.action = action;
1304 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
1305 (void *)&info.info7;
1307 ZERO_STRUCT(devmode_ctr);
1308 ZERO_STRUCT(secdesc_ctr);
1310 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1318 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1319 printf(_("cannot set printer-info: %s\n"),
1320 win_errstr(result));
1324 printf(_("successfully %s printer %s in Active Directory\n"),
1325 action_str, sharename);
1328 nt_status = NT_STATUS_OK;
1331 if (is_valid_policy_hnd(&hnd))
1332 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1337 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1338 const DOM_SID *domain_sid,
1339 const char *domain_name,
1340 struct cli_state *cli,
1341 struct rpc_pipe_client *pipe_hnd,
1342 TALLOC_CTX *mem_ctx,
1346 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1349 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1350 const DOM_SID *domain_sid,
1351 const char *domain_name,
1352 struct cli_state *cli,
1353 struct rpc_pipe_client *pipe_hnd,
1354 TALLOC_CTX *mem_ctx,
1358 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1361 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1362 const DOM_SID *domain_sid,
1363 const char *domain_name,
1364 struct cli_state *cli,
1365 struct rpc_pipe_client *pipe_hnd,
1366 TALLOC_CTX *mem_ctx,
1370 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1374 * List print-queues w.r.t. their publishing state
1376 * All parameters are provided by the run_rpc_command function, except for
1377 * argc, argv which are passed through.
1379 * @param c A net_context structure
1380 * @param domain_sid The domain sid aquired from the remote server
1381 * @param cli A cli_state connected to the server.
1382 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1383 * @param argc Standard main() style argc
1384 * @param argv Standard main() style argv. Initial components are already
1387 * @return Normal NTSTATUS return.
1390 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1391 const DOM_SID *domain_sid,
1392 const char *domain_name,
1393 struct cli_state *cli,
1394 struct rpc_pipe_client *pipe_hnd,
1395 TALLOC_CTX *mem_ctx,
1399 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1400 uint32_t i, num_printers;
1402 const char *printername, *sharename;
1403 union spoolss_PrinterInfo *info_enum;
1404 union spoolss_PrinterInfo info;
1405 struct policy_handle hnd;
1408 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1411 for (i = 0; i < num_printers; i++) {
1413 /* do some initialization */
1414 printername = info_enum[i].info2.printername;
1415 sharename = info_enum[i].info2.sharename;
1417 if (!printername || !sharename) {
1421 /* open printer handle */
1422 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1423 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1426 /* check for existing dst printer */
1427 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1430 if (!info.info7.guid) {
1433 state = info.info7.action;
1435 case DSPRINT_PUBLISH:
1436 printf(_("printer [%s] is published"),
1439 printf(_(", guid: %s"),info.info7.guid);
1442 case DSPRINT_UNPUBLISH:
1443 printf(_("printer [%s] is unpublished\n"),
1446 case DSPRINT_UPDATE:
1447 printf(_("printer [%s] is currently updating\n"),
1451 printf(_("unkown state: %d\n"), state);
1456 nt_status = NT_STATUS_OK;
1459 if (is_valid_policy_hnd(&hnd))
1460 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1466 * Migrate Printer-ACLs from a source server to the destination server
1468 * All parameters are provided by the run_rpc_command function, except for
1469 * argc, argv which are passed through.
1471 * @param c A net_context structure
1472 * @param domain_sid The domain sid aquired from the remote server
1473 * @param cli A cli_state connected to the server.
1474 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1475 * @param argc Standard main() style argc
1476 * @param argv Standard main() style argv. Initial components are already
1479 * @return Normal NTSTATUS return.
1482 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1483 const DOM_SID *domain_sid,
1484 const char *domain_name,
1485 struct cli_state *cli,
1486 struct rpc_pipe_client *pipe_hnd,
1487 TALLOC_CTX *mem_ctx,
1491 /* TODO: what now, info2 or info3 ?
1492 convince jerry that we should add clientside setacls level 3 at least
1494 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1496 uint32_t num_printers;
1498 const char *printername, *sharename;
1499 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1500 struct policy_handle hnd_src, hnd_dst;
1501 union spoolss_PrinterInfo *info_enum;
1502 struct cli_state *cli_dst = NULL;
1503 union spoolss_PrinterInfo info_src, info_dst;
1505 DEBUG(3,("copying printer ACLs\n"));
1507 /* connect destination PI_SPOOLSS */
1508 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1509 &ndr_table_spoolss.syntax_id);
1510 if (!NT_STATUS_IS_OK(nt_status))
1514 /* enum source printers */
1515 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1516 nt_status = NT_STATUS_UNSUCCESSFUL;
1520 if (!num_printers) {
1521 printf (_("no printers found on server.\n"));
1522 nt_status = NT_STATUS_OK;
1526 /* do something for all printers */
1527 for (i = 0; i < num_printers; i++) {
1529 /* do some initialization */
1530 printername = info_enum[i].info2.printername;
1531 sharename = info_enum[i].info2.sharename;
1533 if (!printername || !sharename) {
1534 nt_status = NT_STATUS_UNSUCCESSFUL;
1538 /* we can reset NT_STATUS here because we do not
1539 get any real NT_STATUS-codes anymore from now on */
1540 nt_status = NT_STATUS_UNSUCCESSFUL;
1542 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1543 printername, sharename);
1545 /* according to msdn you have specify these access-rights
1546 to see the security descriptor
1547 - READ_CONTROL (DACL)
1548 - ACCESS_SYSTEM_SECURITY (SACL)
1551 /* open src printer handle */
1552 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1553 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1556 /* open dst printer handle */
1557 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1558 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1561 /* check for existing dst printer */
1562 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1565 /* check for existing src printer */
1566 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1569 /* Copy Security Descriptor */
1571 /* copy secdesc (info level 2) */
1572 info_dst.info2.devmode = NULL;
1573 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1576 display_sec_desc(info_dst.info2.secdesc);
1578 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1581 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1584 /* close printer handles here */
1585 if (is_valid_policy_hnd(&hnd_src)) {
1586 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1589 if (is_valid_policy_hnd(&hnd_dst)) {
1590 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1595 nt_status = NT_STATUS_OK;
1599 if (is_valid_policy_hnd(&hnd_src)) {
1600 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1603 if (is_valid_policy_hnd(&hnd_dst)) {
1604 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1608 cli_shutdown(cli_dst);
1614 * Migrate printer-forms from a src server to the dst server
1616 * All parameters are provided by the run_rpc_command function, except for
1617 * argc, argv which are passed through.
1619 * @param c A net_context structure
1620 * @param domain_sid The domain sid aquired from the remote server
1621 * @param cli A cli_state connected to the server.
1622 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1623 * @param argc Standard main() style argc
1624 * @param argv Standard main() style argv. Initial components are already
1627 * @return Normal NTSTATUS return.
1630 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1631 const DOM_SID *domain_sid,
1632 const char *domain_name,
1633 struct cli_state *cli,
1634 struct rpc_pipe_client *pipe_hnd,
1635 TALLOC_CTX *mem_ctx,
1639 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1642 uint32_t num_printers;
1644 const char *printername, *sharename;
1645 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1646 struct policy_handle hnd_src, hnd_dst;
1647 union spoolss_PrinterInfo *info_enum;
1648 union spoolss_PrinterInfo info_dst;
1650 union spoolss_FormInfo *forms;
1651 struct cli_state *cli_dst = NULL;
1653 DEBUG(3,("copying forms\n"));
1655 /* connect destination PI_SPOOLSS */
1656 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1657 &ndr_table_spoolss.syntax_id);
1658 if (!NT_STATUS_IS_OK(nt_status))
1661 /* enum src printers */
1662 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1663 nt_status = NT_STATUS_UNSUCCESSFUL;
1667 if (!num_printers) {
1668 printf (_("no printers found on server.\n"));
1669 nt_status = NT_STATUS_OK;
1673 /* do something for all printers */
1674 for (i = 0; i < num_printers; i++) {
1676 /* do some initialization */
1677 printername = info_enum[i].info2.printername;
1678 sharename = info_enum[i].info2.sharename;
1680 if (!printername || !sharename) {
1681 nt_status = NT_STATUS_UNSUCCESSFUL;
1684 /* we can reset NT_STATUS here because we do not
1685 get any real NT_STATUS-codes anymore from now on */
1686 nt_status = NT_STATUS_UNSUCCESSFUL;
1688 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1689 printername, sharename);
1692 /* open src printer handle */
1693 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1694 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1697 /* open dst printer handle */
1698 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1699 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1702 /* check for existing dst printer */
1703 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1706 /* finally migrate forms */
1707 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1710 DEBUG(1,("got %d forms for printer\n", num_forms));
1713 for (f = 0; f < num_forms; f++) {
1715 union spoolss_AddFormInfo info;
1718 /* only migrate FORM_PRINTER types, according to jerry
1719 FORM_BUILTIN-types are hard-coded in samba */
1720 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1724 d_printf(_("\tmigrating form # %d [%s] of type "
1726 f, forms[f].info1.form_name,
1727 forms[f].info1.flags);
1729 info.info1 = (struct spoolss_AddFormInfo1 *)
1730 (void *)&forms[f].info1;
1732 /* FIXME: there might be something wrong with samba's
1734 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1739 if (!W_ERROR_IS_OK(result)) {
1740 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1741 f, forms[f].info1.form_name);
1745 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1746 forms[f].info1.form_name));
1750 /* close printer handles here */
1751 if (is_valid_policy_hnd(&hnd_src)) {
1752 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1755 if (is_valid_policy_hnd(&hnd_dst)) {
1756 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1760 nt_status = NT_STATUS_OK;
1764 if (is_valid_policy_hnd(&hnd_src))
1765 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1767 if (is_valid_policy_hnd(&hnd_dst))
1768 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1771 cli_shutdown(cli_dst);
1777 * Migrate printer-drivers from a src server to the dst server
1779 * All parameters are provided by the run_rpc_command function, except for
1780 * argc, argv which are passed through.
1782 * @param c A net_context structure
1783 * @param domain_sid The domain sid aquired from the remote server
1784 * @param cli A cli_state connected to the server.
1785 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1786 * @param argc Standard main() style argc
1787 * @param argv Standard main() style argv. Initial components are already
1790 * @return Normal NTSTATUS return.
1793 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1794 const DOM_SID *domain_sid,
1795 const char *domain_name,
1796 struct cli_state *cli,
1797 struct rpc_pipe_client *pipe_hnd,
1798 TALLOC_CTX *mem_ctx,
1802 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1804 uint32_t num_printers;
1806 const char *printername, *sharename;
1807 bool got_src_driver_share = false;
1808 bool got_dst_driver_share = false;
1809 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1810 struct policy_handle hnd_src, hnd_dst;
1811 union spoolss_DriverInfo drv_info_src;
1812 union spoolss_PrinterInfo *info_enum;
1813 union spoolss_PrinterInfo info_dst;
1814 struct cli_state *cli_dst = NULL;
1815 struct cli_state *cli_share_src = NULL;
1816 struct cli_state *cli_share_dst = NULL;
1817 const char *drivername = NULL;
1819 DEBUG(3,("copying printer-drivers\n"));
1821 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1822 &ndr_table_spoolss.syntax_id);
1823 if (!NT_STATUS_IS_OK(nt_status))
1826 /* open print$-share on the src server */
1827 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1828 cli->desthost, "print$", "A:");
1829 if (!NT_STATUS_IS_OK(nt_status))
1832 got_src_driver_share = true;
1835 /* open print$-share on the dst server */
1836 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1837 cli_dst->desthost, "print$", "A:");
1838 if (!NT_STATUS_IS_OK(nt_status))
1841 got_dst_driver_share = true;
1844 /* enum src printers */
1845 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1846 nt_status = NT_STATUS_UNSUCCESSFUL;
1850 if (num_printers == 0) {
1851 printf (_("no printers found on server.\n"));
1852 nt_status = NT_STATUS_OK;
1857 /* do something for all printers */
1858 for (p = 0; p < num_printers; p++) {
1860 /* do some initialization */
1861 printername = info_enum[p].info2.printername;
1862 sharename = info_enum[p].info2.sharename;
1864 if (!printername || !sharename) {
1865 nt_status = NT_STATUS_UNSUCCESSFUL;
1869 /* we can reset NT_STATUS here because we do not
1870 get any real NT_STATUS-codes anymore from now on */
1871 nt_status = NT_STATUS_UNSUCCESSFUL;
1873 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1874 printername, sharename);
1876 /* open dst printer handle */
1877 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1878 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1881 /* check for existing dst printer */
1882 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1886 /* open src printer handle */
1887 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1888 MAXIMUM_ALLOWED_ACCESS,
1889 pipe_hnd->auth->user_name,
1893 /* in a first step call getdriver for each shared printer (per arch)
1894 to get a list of all files that have to be copied */
1896 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1899 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1900 level, archi_table[i].long_archi,
1901 archi_table[i].version, &drv_info_src))
1904 drivername = drv_info_src.info3.driver_name;
1907 display_print_driver3(&drv_info_src.info3);
1909 /* check arch dir */
1910 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1911 if (!NT_STATUS_IS_OK(nt_status))
1915 /* copy driver-files */
1916 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1917 archi_table[i].short_archi,
1918 &drv_info_src.info3);
1919 if (!NT_STATUS_IS_OK(nt_status))
1924 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1925 nt_status = NT_STATUS_UNSUCCESSFUL;
1929 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1930 drivername, printername));
1934 if (!drivername || strlen(drivername) == 0) {
1935 DEBUGADD(1,("Did not get driver for printer %s\n",
1941 info_dst.info2.drivername = drivername;
1943 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1944 nt_status = NT_STATUS_UNSUCCESSFUL;
1948 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1949 drivername, printername));
1952 if (is_valid_policy_hnd(&hnd_dst)) {
1953 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1957 if (is_valid_policy_hnd(&hnd_src)) {
1958 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1962 nt_status = NT_STATUS_OK;
1966 if (is_valid_policy_hnd(&hnd_src))
1967 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1969 if (is_valid_policy_hnd(&hnd_dst))
1970 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1973 cli_shutdown(cli_dst);
1976 if (got_src_driver_share)
1977 cli_shutdown(cli_share_src);
1979 if (got_dst_driver_share)
1980 cli_shutdown(cli_share_dst);
1987 * Migrate printer-queues from a src to the dst server
1988 * (requires a working "addprinter command" to be installed for the local smbd)
1990 * All parameters are provided by the run_rpc_command function, except for
1991 * argc, argv which are passed through.
1993 * @param c A net_context structure
1994 * @param domain_sid The domain sid aquired from the remote server
1995 * @param cli A cli_state connected to the server.
1996 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1997 * @param argc Standard main() style argc
1998 * @param argv Standard main() style argv. Initial components are already
2001 * @return Normal NTSTATUS return.
2004 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
2005 const DOM_SID *domain_sid,
2006 const char *domain_name,
2007 struct cli_state *cli,
2008 struct rpc_pipe_client *pipe_hnd,
2009 TALLOC_CTX *mem_ctx,
2014 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2015 uint32_t i = 0, num_printers;
2017 union spoolss_PrinterInfo info_dst, info_src;
2018 union spoolss_PrinterInfo *info_enum;
2019 struct cli_state *cli_dst = NULL;
2020 struct policy_handle hnd_dst, hnd_src;
2021 const char *printername, *sharename;
2022 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2023 struct spoolss_SetPrinterInfoCtr info_ctr;
2025 DEBUG(3,("copying printers\n"));
2027 /* connect destination PI_SPOOLSS */
2028 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2029 &ndr_table_spoolss.syntax_id);
2030 if (!NT_STATUS_IS_OK(nt_status))
2034 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2035 nt_status = NT_STATUS_UNSUCCESSFUL;
2039 if (!num_printers) {
2040 printf (_("no printers found on server.\n"));
2041 nt_status = NT_STATUS_OK;
2045 /* do something for all printers */
2046 for (i = 0; i < num_printers; i++) {
2048 /* do some initialization */
2049 printername = info_enum[i].info2.printername;
2050 sharename = info_enum[i].info2.sharename;
2052 if (!printername || !sharename) {
2053 nt_status = NT_STATUS_UNSUCCESSFUL;
2056 /* we can reset NT_STATUS here because we do not
2057 get any real NT_STATUS-codes anymore from now on */
2058 nt_status = NT_STATUS_UNSUCCESSFUL;
2060 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2061 printername, sharename);
2063 /* open dst printer handle */
2064 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2065 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2067 DEBUG(1,("could not open printer: %s\n", sharename));
2070 /* check for existing dst printer */
2071 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2072 printf (_("could not get printer, creating printer.\n"));
2074 DEBUG(1,("printer already exists: %s\n", sharename));
2075 /* close printer handle here - dst only, not got src yet. */
2076 if (is_valid_policy_hnd(&hnd_dst)) {
2077 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2082 /* now get again src printer ctr via getprinter,
2083 we first need a handle for that */
2085 /* open src printer handle */
2086 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2087 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2090 /* getprinter on the src server */
2091 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2094 /* copy each src printer to a dst printer 1:1,
2095 maybe some values have to be changed though */
2096 d_printf(_("creating printer: %s\n"), printername);
2098 info_ctr.level = level;
2099 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)
2100 (void *)&info_src.info2;
2102 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2106 if (W_ERROR_IS_OK(result))
2107 d_printf (_("printer [%s] successfully added.\n"),
2109 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2110 d_fprintf (stderr, _("printer [%s] already exists.\n"),
2113 d_fprintf (stderr, _("could not create printer [%s]\n"),
2118 /* close printer handles here */
2119 if (is_valid_policy_hnd(&hnd_src)) {
2120 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2123 if (is_valid_policy_hnd(&hnd_dst)) {
2124 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2128 nt_status = NT_STATUS_OK;
2131 if (is_valid_policy_hnd(&hnd_src))
2132 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2134 if (is_valid_policy_hnd(&hnd_dst))
2135 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2138 cli_shutdown(cli_dst);
2144 * Migrate Printer-Settings from a src server to the dst server
2145 * (for this to work, printers and drivers already have to be migrated earlier)
2147 * All parameters are provided by the run_rpc_command function, except for
2148 * argc, argv which are passed through.
2150 * @param c A net_context structure
2151 * @param domain_sid The domain sid aquired from the remote server
2152 * @param cli A cli_state connected to the server.
2153 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2154 * @param argc Standard main() style argc
2155 * @param argv Standard main() style argv. Initial components are already
2158 * @return Normal NTSTATUS return.
2161 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2162 const DOM_SID *domain_sid,
2163 const char *domain_name,
2164 struct cli_state *cli,
2165 struct rpc_pipe_client *pipe_hnd,
2166 TALLOC_CTX *mem_ctx,
2171 /* FIXME: Here the nightmare begins */
2174 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2175 uint32_t i = 0, p = 0, j = 0;
2176 uint32_t num_printers;
2178 const char *printername, *sharename;
2179 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2180 struct policy_handle hnd_src, hnd_dst;
2181 union spoolss_PrinterInfo *info_enum;
2182 union spoolss_PrinterInfo info_dst_publish;
2183 union spoolss_PrinterInfo info_dst;
2184 struct cli_state *cli_dst = NULL;
2185 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2186 const char *longname;
2187 const char **keylist = NULL;
2190 ZERO_STRUCT(info_dst_publish);
2192 DEBUG(3,("copying printer settings\n"));
2194 /* connect destination PI_SPOOLSS */
2195 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2196 &ndr_table_spoolss.syntax_id);
2197 if (!NT_STATUS_IS_OK(nt_status))
2200 /* enum src printers */
2201 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2202 nt_status = NT_STATUS_UNSUCCESSFUL;
2206 if (!num_printers) {
2207 printf (_("no printers found on server.\n"));
2208 nt_status = NT_STATUS_OK;
2213 /* needed for dns-strings in regkeys */
2214 longname = get_mydnsfullname();
2216 nt_status = NT_STATUS_UNSUCCESSFUL;
2220 /* do something for all printers */
2221 for (i = 0; i < num_printers; i++) {
2223 uint32_t value_offered = 0, value_needed;
2224 uint32_t data_offered = 0, data_needed;
2225 enum winreg_Type type;
2226 uint8_t *buffer = NULL;
2227 const char *value_name = NULL;
2229 /* do some initialization */
2230 printername = info_enum[i].info2.printername;
2231 sharename = info_enum[i].info2.sharename;
2233 if (!printername || !sharename) {
2234 nt_status = NT_STATUS_UNSUCCESSFUL;
2237 /* we can reset NT_STATUS here because we do not
2238 get any real NT_STATUS-codes anymore from now on */
2239 nt_status = NT_STATUS_UNSUCCESSFUL;
2241 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2242 printername, sharename);
2245 /* open src printer handle */
2246 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2247 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2250 /* open dst printer handle */
2251 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2252 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2255 /* check for existing dst printer */
2256 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2261 /* STEP 1: COPY DEVICE-MODE and other
2262 PRINTER_INFO_2-attributes
2265 info_dst.info2 = info_enum[i].info2;
2267 /* why is the port always disconnected when the printer
2268 is correctly installed (incl. driver ???) */
2269 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2271 /* check if printer is published */
2272 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2274 /* check for existing dst printer */
2275 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2278 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2280 /* ignore false from setprinter due to WERR_IO_PENDING */
2281 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2283 DEBUG(3,("republished printer\n"));
2286 if (info_enum[i].info2.devmode != NULL) {
2288 /* copy devmode (info level 2) */
2289 info_dst.info2.devmode = info_enum[i].info2.devmode;
2291 /* do not copy security descriptor (we have another
2292 * command for that) */
2293 info_dst.info2.secdesc = NULL;
2296 info_dst.info2.devmode.devicename =
2297 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2298 longname, printername);
2299 if (!info_dst.info2.devmode.devicename) {
2300 nt_status = NT_STATUS_NO_MEMORY;
2304 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2308 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2311 /* STEP 2: COPY REGISTRY VALUES */
2313 /* please keep in mind that samba parse_spools gives horribly
2314 crippled results when used to rpccli_spoolss_enumprinterdataex
2315 a win2k3-server. (Bugzilla #1851)
2316 FIXME: IIRC I've seen it too on a win2k-server
2319 /* enumerate data on src handle */
2320 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2332 data_offered = data_needed;
2333 value_offered = value_needed;
2334 buffer = talloc_zero_array(mem_ctx, uint8_t, data_needed);
2335 value_name = talloc_zero_array(mem_ctx, char, value_needed);
2337 /* loop for all printerdata of "PrinterDriverData" */
2338 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2340 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2351 /* loop for all reg_keys */
2352 if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2354 struct regval_blob v;
2357 if (c->opt_verbose) {
2358 fstrcpy(v.valuename, value_name);
2360 v.size = data_offered;
2362 display_reg_value(SPOOL_PRINTERDATA_KEY, v);
2366 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2367 &hnd_dst, value_name,
2368 type, buffer, data_offered))
2371 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2376 /* STEP 3: COPY SUBKEY VALUES */
2378 /* here we need to enum all printer_keys and then work
2379 on the result with enum_printer_key_ex. nt4 does not
2380 respond to enumprinterkey, win2k does, so continue
2381 in case of an error */
2383 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2384 printf(_("got no key-data\n"));
2389 /* work on a list of printer keys
2390 each key has to be enumerated to get all required
2391 information. information is then set via setprinterdataex-calls */
2393 if (keylist == NULL)
2396 for (i=0; keylist && keylist[i] != NULL; i++) {
2398 const char *subkey = keylist[i];
2400 struct spoolss_PrinterEnumValues *info;
2402 /* enumerate all src subkeys */
2403 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2409 for (j=0; j < count; j++) {
2411 struct regval_blob value;
2414 /* although samba replies with sane data in most cases we
2415 should try to avoid writing wrong registry data */
2417 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME) ||
2418 strequal(info[j].value_name, SPOOL_REG_UNCNAME) ||
2419 strequal(info[j].value_name, SPOOL_REG_URL) ||
2420 strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME) ||
2421 strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2423 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
2425 /* although windows uses a multi-sz, we use a sz */
2426 push_reg_sz(mem_ctx, &blob, SAMBA_PRINTER_PORT_NAME);
2427 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2430 if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
2432 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2433 nt_status = NT_STATUS_NO_MEMORY;
2436 push_reg_sz(mem_ctx, &blob, unc_name);
2437 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2440 if (strequal(info[j].value_name, SPOOL_REG_URL)) {
2445 /* FIXME: should we really do that ??? */
2446 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2447 nt_status = NT_STATUS_NO_MEMORY;
2450 push_reg_sz(mem_ctx, NULL, &blob, url);
2451 fstrcpy(value.valuename, SPOOL_REG_URL);
2455 if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2457 push_reg_sz(mem_ctx, &blob, longname);
2458 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2461 if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
2463 push_reg_sz(mem_ctx, &blob, global_myname());
2464 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2467 value.type = REG_SZ;
2468 value.size = blob.length;
2470 value.data_p = blob.data;
2472 value.data_p = NULL;
2476 display_reg_value(subkey, value);
2478 /* here we have to set all subkeys on the dst server */
2479 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2485 struct regval_blob v;
2487 fstrcpy(v.valuename, info[j].value_name);
2488 v.type = info[j].type;
2489 v.data_p = info[j].data->data;
2490 v.size = info[j].data->length;
2492 if (c->opt_verbose) {
2493 display_reg_value(subkey, v);
2496 /* here we have to set all subkeys on the dst server */
2497 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2504 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2505 subkey, info[j].value_name));
2510 TALLOC_FREE(keylist);
2512 /* close printer handles here */
2513 if (is_valid_policy_hnd(&hnd_src)) {
2514 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2517 if (is_valid_policy_hnd(&hnd_dst)) {
2518 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2523 nt_status = NT_STATUS_OK;
2526 SAFE_FREE(devicename);
2528 SAFE_FREE(unc_name);
2530 if (is_valid_policy_hnd(&hnd_src))
2531 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2533 if (is_valid_policy_hnd(&hnd_dst))
2534 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2537 cli_shutdown(cli_dst);