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"
23 const char *long_archi;
24 const char *short_archi;
29 /* support itanium as well */
30 static const struct table_node archi_table[]= {
32 {"Windows 4.0", "WIN40", 0 },
33 {"Windows NT x86", "W32X86", 2 },
34 {"Windows NT x86", "W32X86", 3 },
35 {"Windows NT R4000", "W32MIPS", 2 },
36 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
37 {"Windows NT PowerPC", "W32PPC", 2 },
38 {"Windows IA64", "IA64", 3 },
39 {"Windows x64", "x64", 3 },
45 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
46 * It is here for debugging purpose and should be removed later on.
49 /****************************************************************************
50 Printer info level 3 display function.
51 ****************************************************************************/
53 static void display_print_driver3(struct spoolss_DriverInfo3 *r)
61 printf("Printer Driver Info 3:\n");
62 printf("\tVersion: [%x]\n", r->version);
63 printf("\tDriver Name: [%s]\n", r->driver_name);
64 printf("\tArchitecture: [%s]\n", r->architecture);
65 printf("\tDriver Path: [%s]\n", r->driver_path);
66 printf("\tDatafile: [%s]\n", r->data_file);
67 printf("\tConfigfile: [%s]\n\n", r->config_file);
68 printf("\tHelpfile: [%s]\n\n", r->help_file);
70 for (i=0; r->dependent_files[i] != NULL; i++) {
71 printf("\tDependentfiles: [%s]\n", r->dependent_files[i]);
76 printf("\tMonitorname: [%s]\n", r->monitor_name);
77 printf("\tDefaultdatatype: [%s]\n\n", r->default_datatype);
80 static void display_reg_value(const char *subkey, struct regval_blob value)
86 d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename,
87 *((uint32_t *) value.data_p));
91 rpcstr_pull_talloc(talloc_tos(),
99 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
103 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
104 subkey, value.valuename);
108 uint32_t i, num_values;
111 if (!W_ERROR_IS_OK(reg_pull_multi_sz(NULL, value.data_p,
112 value.size, &num_values,
114 d_printf("reg_pull_multi_sz failed\n");
118 for (i=0; i<num_values; i++) {
119 d_printf("%s\n", values[i]);
126 d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
132 * Copies ACLs, DOS-attributes and timestamps from one
133 * file or directory from one connected share to another connected share
135 * @param c A net_context structure
136 * @param mem_ctx A talloc-context
137 * @param cli_share_src A connected cli_state
138 * @param cli_share_dst A connected cli_state
139 * @param src_file The source file-name
140 * @param dst_file The destination file-name
141 * @param copy_acls Whether to copy acls
142 * @param copy_attrs Whether to copy DOS attributes
143 * @param copy_timestamps Whether to preserve timestamps
144 * @param is_file Whether this file is a file or a dir
146 * @return Normal NTSTATUS return.
149 NTSTATUS net_copy_fileattr(struct net_context *c,
151 struct cli_state *cli_share_src,
152 struct cli_state *cli_share_dst,
153 const char *src_name, const char *dst_name,
154 bool copy_acls, bool copy_attrs,
155 bool copy_timestamps, bool is_file)
157 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
158 uint16_t fnum_src = 0;
159 uint16_t fnum_dst = 0;
162 time_t f_atime, f_ctime, f_mtime;
165 if (!copy_timestamps && !copy_acls && !copy_attrs)
168 /* open file/dir on the originating server */
170 DEBUGADD(3,("opening %s %s on originating server\n",
171 is_file?"file":"dir", src_name));
173 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
174 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src))) {
175 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
176 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
177 nt_status = cli_nt_error(cli_share_src);
184 /* get the security descriptor */
185 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
187 DEBUG(0,("failed to get security descriptor: %s\n",
188 cli_errstr(cli_share_src)));
189 nt_status = cli_nt_error(cli_share_src);
193 if (c->opt_verbose && DEBUGLEVEL >= 3)
194 display_sec_desc(sd);
198 if (copy_attrs || copy_timestamps) {
200 /* get file attributes */
201 if (!NT_STATUS_IS_OK(cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
202 &f_ctime, &f_atime, &f_mtime))) {
203 DEBUG(0,("failed to get file-attrs: %s\n",
204 cli_errstr(cli_share_src)));
205 nt_status = cli_nt_error(cli_share_src);
211 /* open the file/dir on the destination server */
213 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_dst, dst_name, 0, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
214 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_dst))) {
215 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
216 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
217 nt_status = cli_nt_error(cli_share_dst);
221 if (copy_timestamps) {
224 if (!NT_STATUS_IS_OK(cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime))) {
225 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
226 cli_errstr(cli_share_dst)));
227 nt_status = cli_nt_error(cli_share_dst);
235 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
236 DEBUG(0,("could not set secdesc on %s: %s\n",
237 dst_name, cli_errstr(cli_share_dst)));
238 nt_status = cli_nt_error(cli_share_dst);
246 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
247 DEBUG(0,("failed to set file-attrs: %s\n",
248 cli_errstr(cli_share_dst)));
249 nt_status = cli_nt_error(cli_share_dst);
257 if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
258 d_fprintf(stderr, "could not close %s on originating server: %s\n",
259 is_file?"file":"dir", cli_errstr(cli_share_src));
260 nt_status = cli_nt_error(cli_share_src);
264 if (!NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
265 d_fprintf(stderr, "could not close %s on destination server: %s\n",
266 is_file?"file":"dir", cli_errstr(cli_share_dst));
267 nt_status = cli_nt_error(cli_share_dst);
272 nt_status = NT_STATUS_OK;
278 cli_close(cli_share_src, fnum_src);
281 cli_close(cli_share_dst, fnum_dst);
287 * Copy a file or directory from a connected share to another connected share
289 * @param c A net_context structure
290 * @param mem_ctx A talloc-context
291 * @param cli_share_src A connected cli_state
292 * @param cli_share_dst A connected cli_state
293 * @param src_file The source file-name
294 * @param dst_file The destination file-name
295 * @param copy_acls Whether to copy acls
296 * @param copy_attrs Whether to copy DOS attributes
297 * @param copy_timestamps Whether to preserve timestamps
298 * @param is_file Whether this file is a file or a dir
300 * @return Normal NTSTATUS return.
303 NTSTATUS net_copy_file(struct net_context *c,
305 struct cli_state *cli_share_src,
306 struct cli_state *cli_share_dst,
307 const char *src_name, const char *dst_name,
308 bool copy_acls, bool copy_attrs,
309 bool copy_timestamps, bool is_file)
311 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
312 uint16_t fnum_src = 0;
313 uint16_t fnum_dst = 0;
314 static int io_bufsize = 64512;
315 int read_size = io_bufsize;
320 if (!src_name || !dst_name)
323 if (cli_share_src == NULL || cli_share_dst == NULL)
326 /* open on the originating server */
327 DEBUGADD(3,("opening %s %s on originating server\n",
328 is_file ? "file":"dir", src_name));
330 nt_status = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE, &fnum_src);
332 nt_status = cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
333 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src);
335 if (!NT_STATUS_IS_OK(nt_status)) {
336 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
337 is_file ? "file":"dir",
338 src_name, cli_errstr(cli_share_src)));
345 /* open file on the destination server */
346 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
347 nt_status = cli_open(cli_share_dst, dst_name,
348 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum_dst);
350 if (!NT_STATUS_IS_OK(nt_status)) {
351 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
352 dst_name, cli_errstr(cli_share_dst)));
356 /* allocate memory */
357 if (!(data = (char *)SMB_MALLOC(read_size))) {
358 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
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))) {
415 d_fprintf(stderr, "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))) {
424 d_fprintf(stderr, "could not close file on originating server: %s\n",
425 cli_errstr(cli_share_src));
426 nt_status = cli_nt_error(cli_share_src);
430 if (is_file && !NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
431 d_fprintf(stderr, "could not close file on destination server: %s\n",
432 cli_errstr(cli_share_dst));
433 nt_status = cli_nt_error(cli_share_dst);
437 /* possibly we have to copy some file-attributes / acls / sd */
438 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
439 src_name, dst_name, copy_acls,
440 copy_attrs, copy_timestamps, is_file);
441 if (!NT_STATUS_IS_OK(nt_status))
445 nt_status = NT_STATUS_OK;
451 cli_close(cli_share_src, fnum_src);
454 cli_close(cli_share_dst, fnum_dst);
462 * Copy a driverfile from on connected share to another connected share
463 * This silently assumes that a driver-file is picked up from
465 * \\src_server\print$\{arch}\{version}\file
469 * \\dst_server\print$\{arch}\file
471 * to be added via setdriver-calls later.
472 * @param c A net_context structure
473 * @param mem_ctx A talloc-context
474 * @param cli_share_src A cli_state connected to source print$-share
475 * @param cli_share_dst A cli_state connected to destination print$-share
476 * @param file The file-name to be copied
477 * @param short_archi The name of the driver-architecture (short form)
479 * @return Normal NTSTATUS return.
482 static NTSTATUS net_copy_driverfile(struct net_context *c,
484 struct cli_state *cli_share_src,
485 struct cli_state *cli_share_dst,
486 const char *file, const char *short_archi) {
488 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
500 /* scroll through the file until we have the part
501 beyond archi_table.short_archi */
503 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
504 if (strequal(tok, short_archi)) {
505 next_token_talloc(mem_ctx, &p, &version, "\\");
506 next_token_talloc(mem_ctx, &p, &filename, "\\");
510 /* build source file name */
511 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
512 return NT_STATUS_NO_MEMORY;
515 /* create destination file name */
516 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
517 return NT_STATUS_NO_MEMORY;
520 /* finally copy the file */
521 nt_status = net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
522 src_name, dst_name, false, false, false, true);
523 if (!NT_STATUS_IS_OK(nt_status))
526 nt_status = NT_STATUS_OK;
536 * Check for existing Architecture directory on a given server
538 * @param cli_share A cli_state connected to a print$-share
539 * @param short_archi The Architecture for the print-driver
541 * @return Normal NTSTATUS return.
544 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
547 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
550 if (asprintf(&dir, "\\%s", short_archi) < 0) {
551 return NT_STATUS_NO_MEMORY;
554 DEBUG(10,("creating print-driver dir for architecture: %s\n",
557 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share, dir))) {
558 DEBUG(1,("cannot create directory %s: %s\n",
559 dir, cli_errstr(cli_share)));
560 nt_status = NT_STATUS_NO_SUCH_FILE;
563 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share, dir))) {
564 d_fprintf(stderr, "cannot check %s: %s\n",
565 dir, cli_errstr(cli_share));
569 nt_status = NT_STATUS_OK;
577 * Copy a print-driver (level 3) from one connected print$-share to another
578 * connected print$-share
580 * @param c A net_context structure
581 * @param mem_ctx A talloc-context
582 * @param cli_share_src A cli_state connected to a print$-share
583 * @param cli_share_dst A cli_state connected to a print$-share
584 * @param short_archi The Architecture for the print-driver
585 * @param i1 The DRIVER_INFO_3-struct
587 * @return Normal NTSTATUS return.
590 static NTSTATUS copy_print_driver_3(struct net_context *c,
592 struct cli_state *cli_share_src,
593 struct cli_state *cli_share_dst,
594 const char *short_archi,
595 struct spoolss_DriverInfo3 *r)
597 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
605 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
606 r->driver_name, short_archi, r->version);
608 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
609 r->driver_path, short_archi);
610 if (!NT_STATUS_IS_OK(nt_status))
613 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
614 r->data_file, short_archi);
615 if (!NT_STATUS_IS_OK(nt_status))
618 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
619 r->config_file, short_archi);
620 if (!NT_STATUS_IS_OK(nt_status))
623 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
624 r->help_file, short_archi);
625 if (!NT_STATUS_IS_OK(nt_status))
628 for (i=0; r->dependent_files[i] != NULL; i++) {
630 nt_status = net_copy_driverfile(c, mem_ctx,
631 cli_share_src, cli_share_dst,
632 r->dependent_files[i], short_archi);
633 if (!NT_STATUS_IS_OK(nt_status)) {
642 * net_spoolss-functions
643 * =====================
645 * the net_spoolss-functions aim to simplify spoolss-client-functions
646 * required during the migration-process wrt buffer-sizes, returned
649 * this greatly reduces the complexitiy of the migrate-functions.
653 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
658 uint32_t *num_printers,
659 union spoolss_PrinterInfo **info)
665 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
672 if (!W_ERROR_IS_OK(result)) {
673 printf("cannot enum printers: %s\n", win_errstr(result));
680 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
682 const char *printername,
683 uint32_t access_required,
684 const char *username,
685 struct policy_handle *hnd)
688 fstring printername2;
690 fstrcpy(printername2, pipe_hnd->srv_name_slash);
691 fstrcat(printername2, "\\");
692 fstrcat(printername2, printername);
694 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
695 pipe_hnd->srv_name_slash, username, printername2, access_required));
698 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
703 /* be more verbose */
704 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
705 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
706 printername2, pipe_hnd->srv_name_slash, username);
710 if (!W_ERROR_IS_OK(result)) {
711 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
712 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
716 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
717 printername2, pipe_hnd->srv_name_slash));
722 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
724 struct policy_handle *hnd,
726 union spoolss_PrinterInfo *info)
730 /* getprinter call */
731 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
736 if (!W_ERROR_IS_OK(result)) {
737 printf("cannot get printer-info: %s\n", win_errstr(result));
744 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
746 struct policy_handle *hnd,
748 union spoolss_PrinterInfo *info)
752 struct spoolss_SetPrinterInfoCtr info_ctr;
753 struct spoolss_DevmodeContainer devmode_ctr;
754 struct sec_desc_buf secdesc_ctr;
756 ZERO_STRUCT(devmode_ctr);
757 ZERO_STRUCT(secdesc_ctr);
759 /* setprinter call */
761 info_ctr.level = level;
764 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)
765 (void *)&info->info0;
768 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)
769 (void *)&info->info1;
772 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)
773 (void *)&info->info2;
776 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
777 (void *)&info->info3;
780 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)
781 (void *)&info->info4;
784 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)
785 (void *)&info->info5;
788 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)
789 (void *)&info->info6;
792 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
793 (void *)&info->info7;
797 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)
798 (void *)&info->info8;
801 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)
802 (void *)&info->info9;
809 status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
817 if (!W_ERROR_IS_OK(result)) {
818 printf("cannot set printer-info: %s\n", win_errstr(result));
826 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
828 struct policy_handle *hnd,
829 const char *value_name,
830 enum winreg_Type type,
831 union spoolss_PrinterData data)
836 /* setprinterdata call */
837 status = rpccli_spoolss_SetPrinterData(pipe_hnd, mem_ctx,
842 0, /* autocalculated */
845 if (!W_ERROR_IS_OK(result)) {
846 printf ("unable to set printerdata: %s\n", win_errstr(result));
854 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
856 struct policy_handle *hnd,
858 const char ***keylist)
862 /* enumprinterkey call */
863 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
865 if (!W_ERROR_IS_OK(result)) {
866 printf("enumprinterkey failed: %s\n", win_errstr(result));
873 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
876 struct policy_handle *hnd,
879 struct spoolss_PrinterEnumValues **info)
883 /* enumprinterdataex call */
884 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
891 if (!W_ERROR_IS_OK(result)) {
892 printf("enumprinterdataex failed: %s\n", win_errstr(result));
900 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
902 struct policy_handle *hnd,
904 struct regval_blob *value)
909 /* setprinterdataex call */
910 status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
919 if (!W_ERROR_IS_OK(result)) {
920 printf("could not set printerdataex: %s\n", win_errstr(result));
927 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
929 struct policy_handle *hnd,
932 union spoolss_FormInfo **forms)
937 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
943 if (!W_ERROR_IS_OK(result)) {
944 printf("could not enum forms: %s\n", win_errstr(result));
951 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
953 uint32_t level, const char *env,
955 union spoolss_DriverInfo **info)
959 /* enumprinterdrivers call */
960 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
961 pipe_hnd->srv_name_slash,
967 if (!W_ERROR_IS_OK(result)) {
968 printf("cannot enum drivers: %s\n", win_errstr(result));
975 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
977 struct policy_handle *hnd, uint32_t level,
978 const char *env, int version,
979 union spoolss_DriverInfo *info)
982 uint32_t server_major_version;
983 uint32_t server_minor_version;
985 /* getprinterdriver call */
986 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
994 &server_major_version,
995 &server_minor_version);
996 if (!W_ERROR_IS_OK(result)) {
997 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
998 env, win_errstr(result)));
999 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1000 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1001 printf("cannot get driver: %s\n", win_errstr(result));
1010 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1011 TALLOC_CTX *mem_ctx, uint32_t level,
1012 union spoolss_DriverInfo *info)
1016 struct spoolss_AddDriverInfoCtr info_ctr;
1018 info_ctr.level = level;
1022 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)
1023 (void *)&info->info2;
1026 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)
1027 (void *)&info->info3;
1030 printf("unsupported info level: %d\n", level);
1034 /* addprinterdriver call */
1035 status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1036 pipe_hnd->srv_name_slash,
1039 /* be more verbose */
1040 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1041 printf("You are not allowed to add drivers\n");
1044 if (!W_ERROR_IS_OK(result)) {
1045 printf("cannot add driver: %s\n", win_errstr(result));
1053 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1054 * for a single printer or for all printers depending on argc/argv
1057 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1058 TALLOC_CTX *mem_ctx,
1062 uint32_t *num_printers,
1063 union spoolss_PrinterInfo **info_p)
1065 struct policy_handle hnd;
1067 /* no arguments given, enumerate all printers */
1070 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1071 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1072 level, num_printers, info_p))
1078 /* argument given, get a single printer by name */
1079 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1080 MAXIMUM_ALLOWED_ACCESS,
1081 pipe_hnd->auth->user_name,
1085 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1086 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1090 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1095 DEBUG(3,("got %d printers\n", *num_printers));
1102 * List print-queues (including local printers that are not shared)
1104 * All parameters are provided by the run_rpc_command function, except for
1105 * argc, argv which are passed through.
1107 * @param c A net_context structure
1108 * @param domain_sid The domain sid aquired from the remote server
1109 * @param cli A cli_state connected to the server.
1110 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1111 * @param argc Standard main() style argc
1112 * @param argv Standard main() style argv. Initial components are already
1115 * @return Normal NTSTATUS return.
1118 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1119 const DOM_SID *domain_sid,
1120 const char *domain_name,
1121 struct cli_state *cli,
1122 struct rpc_pipe_client *pipe_hnd,
1123 TALLOC_CTX *mem_ctx,
1127 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1128 uint32_t i, num_printers;
1130 const char *printername, *sharename;
1131 union spoolss_PrinterInfo *info;
1133 printf("listing printers\n");
1135 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1138 for (i = 0; i < num_printers; i++) {
1140 /* do some initialization */
1141 printername = info[i].info2.printername;
1142 sharename = info[i].info2.sharename;
1144 if (printername && sharename) {
1145 d_printf("printer %d: %s, shared as: %s\n",
1146 i+1, printername, sharename);
1150 return NT_STATUS_OK;
1154 * List printer-drivers from a server
1156 * All parameters are provided by the run_rpc_command function, except for
1157 * argc, argv which are passed through.
1159 * @param c A net_context structure
1160 * @param domain_sid The domain sid aquired from the remote server
1161 * @param cli A cli_state connected to the server.
1162 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1163 * @param argc Standard main() style argc
1164 * @param argv Standard main() style argv. Initial components are already
1167 * @return Normal NTSTATUS return.
1170 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1171 const DOM_SID *domain_sid,
1172 const char *domain_name,
1173 struct cli_state *cli,
1174 struct rpc_pipe_client *pipe_hnd,
1175 TALLOC_CTX *mem_ctx,
1179 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1182 union spoolss_DriverInfo *info;
1185 printf("listing printer-drivers\n");
1187 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1189 uint32_t num_drivers;
1191 /* enum remote drivers */
1192 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1193 archi_table[i].long_archi,
1194 &num_drivers, &info)) {
1195 nt_status = NT_STATUS_UNSUCCESSFUL;
1199 if (num_drivers == 0) {
1200 d_printf ("no drivers found on server for architecture: [%s].\n",
1201 archi_table[i].long_archi);
1205 d_printf("got %d printer-drivers for architecture: [%s]\n",
1206 num_drivers, archi_table[i].long_archi);
1209 /* do something for all drivers for architecture */
1210 for (d = 0; d < num_drivers; d++) {
1211 display_print_driver3(&info[d].info3);
1215 nt_status = NT_STATUS_OK;
1223 * Publish print-queues with args-wrapper
1225 * @param cli A cli_state connected to the server.
1226 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1227 * @param argc Standard main() style argc
1228 * @param argv Standard main() style argv. Initial components are already
1232 * @return Normal NTSTATUS return.
1235 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1236 TALLOC_CTX *mem_ctx,
1241 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1242 uint32_t i, num_printers;
1244 const char *printername, *sharename;
1245 union spoolss_PrinterInfo *info_enum;
1246 union spoolss_PrinterInfo info;
1247 struct spoolss_SetPrinterInfoCtr info_ctr;
1248 struct spoolss_DevmodeContainer devmode_ctr;
1249 struct sec_desc_buf secdesc_ctr;
1250 struct policy_handle hnd;
1252 const char *action_str;
1254 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1257 for (i = 0; i < num_printers; i++) {
1259 /* do some initialization */
1260 printername = info_enum[i].info2.printername;
1261 sharename = info_enum[i].info2.sharename;
1262 if (!printername || !sharename) {
1266 /* open printer handle */
1267 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1268 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1271 /* check for existing dst printer */
1272 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1275 /* check action and set string */
1277 case DSPRINT_PUBLISH:
1278 action_str = "published";
1280 case DSPRINT_UPDATE:
1281 action_str = "updated";
1283 case DSPRINT_UNPUBLISH:
1284 action_str = "unpublished";
1287 action_str = "unknown action";
1288 printf("unkown action: %d\n", action);
1292 info.info7.action = action;
1294 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
1295 (void *)&info.info7;
1297 ZERO_STRUCT(devmode_ctr);
1298 ZERO_STRUCT(secdesc_ctr);
1300 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1308 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1309 printf("cannot set printer-info: %s\n", win_errstr(result));
1313 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1316 nt_status = NT_STATUS_OK;
1319 if (is_valid_policy_hnd(&hnd))
1320 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1325 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1326 const DOM_SID *domain_sid,
1327 const char *domain_name,
1328 struct cli_state *cli,
1329 struct rpc_pipe_client *pipe_hnd,
1330 TALLOC_CTX *mem_ctx,
1334 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1337 NTSTATUS rpc_printer_publish_unpublish_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_UNPUBLISH);
1349 NTSTATUS rpc_printer_publish_update_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_UPDATE);
1362 * List print-queues w.r.t. their publishing state
1364 * All parameters are provided by the run_rpc_command function, except for
1365 * argc, argv which are passed through.
1367 * @param c A net_context structure
1368 * @param domain_sid The domain sid aquired from the remote server
1369 * @param cli A cli_state connected to the server.
1370 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1371 * @param argc Standard main() style argc
1372 * @param argv Standard main() style argv. Initial components are already
1375 * @return Normal NTSTATUS return.
1378 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1379 const DOM_SID *domain_sid,
1380 const char *domain_name,
1381 struct cli_state *cli,
1382 struct rpc_pipe_client *pipe_hnd,
1383 TALLOC_CTX *mem_ctx,
1387 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1388 uint32_t i, num_printers;
1390 const char *printername, *sharename;
1391 union spoolss_PrinterInfo *info_enum;
1392 union spoolss_PrinterInfo info;
1393 struct policy_handle hnd;
1396 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1399 for (i = 0; i < num_printers; i++) {
1401 /* do some initialization */
1402 printername = info_enum[i].info2.printername;
1403 sharename = info_enum[i].info2.sharename;
1405 if (!printername || !sharename) {
1409 /* open printer handle */
1410 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1411 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1414 /* check for existing dst printer */
1415 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1418 if (!info.info7.guid) {
1421 state = info.info7.action;
1423 case DSPRINT_PUBLISH:
1424 printf("printer [%s] is published", sharename);
1426 printf(", guid: %s", info.info7.guid);
1429 case DSPRINT_UNPUBLISH:
1430 printf("printer [%s] is unpublished\n", sharename);
1432 case DSPRINT_UPDATE:
1433 printf("printer [%s] is currently updating\n", sharename);
1436 printf("unkown state: %d\n", state);
1441 nt_status = NT_STATUS_OK;
1444 if (is_valid_policy_hnd(&hnd))
1445 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1451 * Migrate Printer-ACLs from a source server to the destination server
1453 * All parameters are provided by the run_rpc_command function, except for
1454 * argc, argv which are passed through.
1456 * @param c A net_context structure
1457 * @param domain_sid The domain sid aquired from the remote server
1458 * @param cli A cli_state connected to the server.
1459 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1460 * @param argc Standard main() style argc
1461 * @param argv Standard main() style argv. Initial components are already
1464 * @return Normal NTSTATUS return.
1467 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1468 const DOM_SID *domain_sid,
1469 const char *domain_name,
1470 struct cli_state *cli,
1471 struct rpc_pipe_client *pipe_hnd,
1472 TALLOC_CTX *mem_ctx,
1476 /* TODO: what now, info2 or info3 ?
1477 convince jerry that we should add clientside setacls level 3 at least
1479 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1481 uint32_t num_printers;
1483 const char *printername, *sharename;
1484 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1485 struct policy_handle hnd_src, hnd_dst;
1486 union spoolss_PrinterInfo *info_enum;
1487 struct cli_state *cli_dst = NULL;
1488 union spoolss_PrinterInfo info_src, info_dst;
1490 DEBUG(3,("copying printer ACLs\n"));
1492 /* connect destination PI_SPOOLSS */
1493 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1494 &ndr_table_spoolss.syntax_id);
1495 if (!NT_STATUS_IS_OK(nt_status))
1499 /* enum source printers */
1500 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1501 nt_status = NT_STATUS_UNSUCCESSFUL;
1505 if (!num_printers) {
1506 printf ("no printers found on server.\n");
1507 nt_status = NT_STATUS_OK;
1511 /* do something for all printers */
1512 for (i = 0; i < num_printers; i++) {
1514 /* do some initialization */
1515 printername = info_enum[i].info2.printername;
1516 sharename = info_enum[i].info2.sharename;
1518 if (!printername || !sharename) {
1519 nt_status = NT_STATUS_UNSUCCESSFUL;
1523 /* we can reset NT_STATUS here because we do not
1524 get any real NT_STATUS-codes anymore from now on */
1525 nt_status = NT_STATUS_UNSUCCESSFUL;
1527 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1528 printername, sharename);
1530 /* according to msdn you have specify these access-rights
1531 to see the security descriptor
1532 - READ_CONTROL (DACL)
1533 - ACCESS_SYSTEM_SECURITY (SACL)
1536 /* open src printer handle */
1537 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1538 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1541 /* open dst printer handle */
1542 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1543 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1546 /* check for existing dst printer */
1547 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1550 /* check for existing src printer */
1551 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1554 /* Copy Security Descriptor */
1556 /* copy secdesc (info level 2) */
1557 info_dst.info2.devmode = NULL;
1558 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1561 display_sec_desc(info_dst.info2.secdesc);
1563 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1566 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1569 /* close printer handles here */
1570 if (is_valid_policy_hnd(&hnd_src)) {
1571 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1574 if (is_valid_policy_hnd(&hnd_dst)) {
1575 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1580 nt_status = NT_STATUS_OK;
1584 if (is_valid_policy_hnd(&hnd_src)) {
1585 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1588 if (is_valid_policy_hnd(&hnd_dst)) {
1589 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1593 cli_shutdown(cli_dst);
1599 * Migrate printer-forms from a src server to the dst server
1601 * All parameters are provided by the run_rpc_command function, except for
1602 * argc, argv which are passed through.
1604 * @param c A net_context structure
1605 * @param domain_sid The domain sid aquired from the remote server
1606 * @param cli A cli_state connected to the server.
1607 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1608 * @param argc Standard main() style argc
1609 * @param argv Standard main() style argv. Initial components are already
1612 * @return Normal NTSTATUS return.
1615 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1616 const DOM_SID *domain_sid,
1617 const char *domain_name,
1618 struct cli_state *cli,
1619 struct rpc_pipe_client *pipe_hnd,
1620 TALLOC_CTX *mem_ctx,
1624 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1627 uint32_t num_printers;
1629 const char *printername, *sharename;
1630 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1631 struct policy_handle hnd_src, hnd_dst;
1632 union spoolss_PrinterInfo *info_enum;
1633 union spoolss_PrinterInfo info_dst;
1635 union spoolss_FormInfo *forms;
1636 struct cli_state *cli_dst = NULL;
1638 DEBUG(3,("copying forms\n"));
1640 /* connect destination PI_SPOOLSS */
1641 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1642 &ndr_table_spoolss.syntax_id);
1643 if (!NT_STATUS_IS_OK(nt_status))
1646 /* enum src printers */
1647 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1648 nt_status = NT_STATUS_UNSUCCESSFUL;
1652 if (!num_printers) {
1653 printf ("no printers found on server.\n");
1654 nt_status = NT_STATUS_OK;
1658 /* do something for all printers */
1659 for (i = 0; i < num_printers; i++) {
1661 /* do some initialization */
1662 printername = info_enum[i].info2.printername;
1663 sharename = info_enum[i].info2.sharename;
1665 if (!printername || !sharename) {
1666 nt_status = NT_STATUS_UNSUCCESSFUL;
1669 /* we can reset NT_STATUS here because we do not
1670 get any real NT_STATUS-codes anymore from now on */
1671 nt_status = NT_STATUS_UNSUCCESSFUL;
1673 d_printf("migrating printer forms for: [%s] / [%s]\n",
1674 printername, sharename);
1677 /* open src printer handle */
1678 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1679 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1682 /* open dst printer handle */
1683 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1684 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1687 /* check for existing dst printer */
1688 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1691 /* finally migrate forms */
1692 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1695 DEBUG(1,("got %d forms for printer\n", num_forms));
1698 for (f = 0; f < num_forms; f++) {
1700 union spoolss_AddFormInfo info;
1703 /* only migrate FORM_PRINTER types, according to jerry
1704 FORM_BUILTIN-types are hard-coded in samba */
1705 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1709 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1710 f, forms[f].info1.form_name,
1711 forms[f].info1.flags);
1713 info.info1 = (struct spoolss_AddFormInfo1 *)
1714 (void *)&forms[f].info1;
1716 /* FIXME: there might be something wrong with samba's
1718 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1723 if (!W_ERROR_IS_OK(result)) {
1724 d_printf("\tAddForm form %d: [%s] refused.\n",
1725 f, forms[f].info1.form_name);
1729 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1730 forms[f].info1.form_name));
1734 /* close printer handles here */
1735 if (is_valid_policy_hnd(&hnd_src)) {
1736 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1739 if (is_valid_policy_hnd(&hnd_dst)) {
1740 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1744 nt_status = NT_STATUS_OK;
1748 if (is_valid_policy_hnd(&hnd_src))
1749 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1751 if (is_valid_policy_hnd(&hnd_dst))
1752 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1755 cli_shutdown(cli_dst);
1761 * Migrate printer-drivers from a src server to the dst server
1763 * All parameters are provided by the run_rpc_command function, except for
1764 * argc, argv which are passed through.
1766 * @param c A net_context structure
1767 * @param domain_sid The domain sid aquired from the remote server
1768 * @param cli A cli_state connected to the server.
1769 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1770 * @param argc Standard main() style argc
1771 * @param argv Standard main() style argv. Initial components are already
1774 * @return Normal NTSTATUS return.
1777 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1778 const DOM_SID *domain_sid,
1779 const char *domain_name,
1780 struct cli_state *cli,
1781 struct rpc_pipe_client *pipe_hnd,
1782 TALLOC_CTX *mem_ctx,
1786 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1788 uint32_t num_printers;
1790 const char *printername, *sharename;
1791 bool got_src_driver_share = false;
1792 bool got_dst_driver_share = false;
1793 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1794 struct policy_handle hnd_src, hnd_dst;
1795 union spoolss_DriverInfo drv_info_src;
1796 union spoolss_PrinterInfo *info_enum;
1797 union spoolss_PrinterInfo info_dst;
1798 struct cli_state *cli_dst = NULL;
1799 struct cli_state *cli_share_src = NULL;
1800 struct cli_state *cli_share_dst = NULL;
1801 const char *drivername = NULL;
1803 DEBUG(3,("copying printer-drivers\n"));
1805 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1806 &ndr_table_spoolss.syntax_id);
1807 if (!NT_STATUS_IS_OK(nt_status))
1810 /* open print$-share on the src server */
1811 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1812 cli->desthost, "print$", "A:");
1813 if (!NT_STATUS_IS_OK(nt_status))
1816 got_src_driver_share = true;
1819 /* open print$-share on the dst server */
1820 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1821 cli_dst->desthost, "print$", "A:");
1822 if (!NT_STATUS_IS_OK(nt_status))
1825 got_dst_driver_share = true;
1828 /* enum src printers */
1829 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1830 nt_status = NT_STATUS_UNSUCCESSFUL;
1834 if (num_printers == 0) {
1835 printf ("no printers found on server.\n");
1836 nt_status = NT_STATUS_OK;
1841 /* do something for all printers */
1842 for (p = 0; p < num_printers; p++) {
1844 /* do some initialization */
1845 printername = info_enum[p].info2.printername;
1846 sharename = info_enum[p].info2.sharename;
1848 if (!printername || !sharename) {
1849 nt_status = NT_STATUS_UNSUCCESSFUL;
1853 /* we can reset NT_STATUS here because we do not
1854 get any real NT_STATUS-codes anymore from now on */
1855 nt_status = NT_STATUS_UNSUCCESSFUL;
1857 d_printf("migrating printer driver for: [%s] / [%s]\n",
1858 printername, sharename);
1860 /* open dst printer handle */
1861 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1862 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1865 /* check for existing dst printer */
1866 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1870 /* open src printer handle */
1871 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1872 MAXIMUM_ALLOWED_ACCESS,
1873 pipe_hnd->auth->user_name,
1877 /* in a first step call getdriver for each shared printer (per arch)
1878 to get a list of all files that have to be copied */
1880 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1883 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1884 level, archi_table[i].long_archi,
1885 archi_table[i].version, &drv_info_src))
1888 drivername = drv_info_src.info3.driver_name;
1891 display_print_driver3(&drv_info_src.info3);
1893 /* check arch dir */
1894 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1895 if (!NT_STATUS_IS_OK(nt_status))
1899 /* copy driver-files */
1900 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1901 archi_table[i].short_archi,
1902 &drv_info_src.info3);
1903 if (!NT_STATUS_IS_OK(nt_status))
1908 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1909 nt_status = NT_STATUS_UNSUCCESSFUL;
1913 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1914 drivername, printername));
1918 if (!drivername || strlen(drivername) == 0) {
1919 DEBUGADD(1,("Did not get driver for printer %s\n",
1925 info_dst.info2.drivername = drivername;
1927 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1928 nt_status = NT_STATUS_UNSUCCESSFUL;
1932 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1933 drivername, printername));
1936 if (is_valid_policy_hnd(&hnd_dst)) {
1937 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1941 if (is_valid_policy_hnd(&hnd_src)) {
1942 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1946 nt_status = NT_STATUS_OK;
1950 if (is_valid_policy_hnd(&hnd_src))
1951 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1953 if (is_valid_policy_hnd(&hnd_dst))
1954 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1957 cli_shutdown(cli_dst);
1960 if (got_src_driver_share)
1961 cli_shutdown(cli_share_src);
1963 if (got_dst_driver_share)
1964 cli_shutdown(cli_share_dst);
1971 * Migrate printer-queues from a src to the dst server
1972 * (requires a working "addprinter command" to be installed for the local smbd)
1974 * All parameters are provided by the run_rpc_command function, except for
1975 * argc, argv which are passed through.
1977 * @param c A net_context structure
1978 * @param domain_sid The domain sid aquired from the remote server
1979 * @param cli A cli_state connected to the server.
1980 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1981 * @param argc Standard main() style argc
1982 * @param argv Standard main() style argv. Initial components are already
1985 * @return Normal NTSTATUS return.
1988 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
1989 const DOM_SID *domain_sid,
1990 const char *domain_name,
1991 struct cli_state *cli,
1992 struct rpc_pipe_client *pipe_hnd,
1993 TALLOC_CTX *mem_ctx,
1998 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1999 uint32_t i = 0, num_printers;
2001 union spoolss_PrinterInfo info_dst, info_src;
2002 union spoolss_PrinterInfo *info_enum;
2003 struct cli_state *cli_dst = NULL;
2004 struct policy_handle hnd_dst, hnd_src;
2005 const char *printername, *sharename;
2006 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2007 struct spoolss_SetPrinterInfoCtr info_ctr;
2009 DEBUG(3,("copying printers\n"));
2011 /* connect destination PI_SPOOLSS */
2012 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2013 &ndr_table_spoolss.syntax_id);
2014 if (!NT_STATUS_IS_OK(nt_status))
2018 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2019 nt_status = NT_STATUS_UNSUCCESSFUL;
2023 if (!num_printers) {
2024 printf ("no printers found on server.\n");
2025 nt_status = NT_STATUS_OK;
2029 /* do something for all printers */
2030 for (i = 0; i < num_printers; i++) {
2032 /* do some initialization */
2033 printername = info_enum[i].info2.printername;
2034 sharename = info_enum[i].info2.sharename;
2036 if (!printername || !sharename) {
2037 nt_status = NT_STATUS_UNSUCCESSFUL;
2040 /* we can reset NT_STATUS here because we do not
2041 get any real NT_STATUS-codes anymore from now on */
2042 nt_status = NT_STATUS_UNSUCCESSFUL;
2044 d_printf("migrating printer queue for: [%s] / [%s]\n",
2045 printername, sharename);
2047 /* open dst printer handle */
2048 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2049 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2051 DEBUG(1,("could not open printer: %s\n", sharename));
2054 /* check for existing dst printer */
2055 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2056 printf ("could not get printer, creating printer.\n");
2058 DEBUG(1,("printer already exists: %s\n", sharename));
2059 /* close printer handle here - dst only, not got src yet. */
2060 if (is_valid_policy_hnd(&hnd_dst)) {
2061 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2066 /* now get again src printer ctr via getprinter,
2067 we first need a handle for that */
2069 /* open src printer handle */
2070 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2071 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2074 /* getprinter on the src server */
2075 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2078 /* copy each src printer to a dst printer 1:1,
2079 maybe some values have to be changed though */
2080 d_printf("creating printer: %s\n", printername);
2082 info_ctr.level = level;
2083 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)
2084 (void *)&info_src.info2;
2086 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2090 if (W_ERROR_IS_OK(result))
2091 d_printf ("printer [%s] successfully added.\n", printername);
2092 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2093 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2095 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2099 /* close printer handles here */
2100 if (is_valid_policy_hnd(&hnd_src)) {
2101 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2104 if (is_valid_policy_hnd(&hnd_dst)) {
2105 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2109 nt_status = NT_STATUS_OK;
2112 if (is_valid_policy_hnd(&hnd_src))
2113 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2115 if (is_valid_policy_hnd(&hnd_dst))
2116 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2119 cli_shutdown(cli_dst);
2125 * Migrate Printer-Settings from a src server to the dst server
2126 * (for this to work, printers and drivers already have to be migrated earlier)
2128 * All parameters are provided by the run_rpc_command function, except for
2129 * argc, argv which are passed through.
2131 * @param c A net_context structure
2132 * @param domain_sid The domain sid aquired from the remote server
2133 * @param cli A cli_state connected to the server.
2134 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2135 * @param argc Standard main() style argc
2136 * @param argv Standard main() style argv. Initial components are already
2139 * @return Normal NTSTATUS return.
2142 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2143 const DOM_SID *domain_sid,
2144 const char *domain_name,
2145 struct cli_state *cli,
2146 struct rpc_pipe_client *pipe_hnd,
2147 TALLOC_CTX *mem_ctx,
2152 /* FIXME: Here the nightmare begins */
2155 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2156 uint32_t i = 0, p = 0, j = 0;
2157 uint32_t num_printers;
2159 const char *printername, *sharename;
2160 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2161 struct policy_handle hnd_src, hnd_dst;
2162 union spoolss_PrinterInfo *info_enum;
2163 union spoolss_PrinterInfo info_dst_publish;
2164 union spoolss_PrinterInfo info_dst;
2165 struct cli_state *cli_dst = NULL;
2166 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2167 const char *longname;
2168 const char **keylist = NULL;
2171 ZERO_STRUCT(info_dst_publish);
2173 DEBUG(3,("copying printer settings\n"));
2175 /* connect destination PI_SPOOLSS */
2176 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2177 &ndr_table_spoolss.syntax_id);
2178 if (!NT_STATUS_IS_OK(nt_status))
2181 /* enum src printers */
2182 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2183 nt_status = NT_STATUS_UNSUCCESSFUL;
2187 if (!num_printers) {
2188 printf ("no printers found on server.\n");
2189 nt_status = NT_STATUS_OK;
2194 /* needed for dns-strings in regkeys */
2195 longname = get_mydnsfullname();
2197 nt_status = NT_STATUS_UNSUCCESSFUL;
2201 /* do something for all printers */
2202 for (i = 0; i < num_printers; i++) {
2204 uint32_t value_offered = 0, value_needed;
2205 uint32_t data_offered = 0, data_needed;
2206 enum winreg_Type type;
2207 uint8_t *buffer = NULL;
2208 const char *value_name = NULL;
2210 /* do some initialization */
2211 printername = info_enum[i].info2.printername;
2212 sharename = info_enum[i].info2.sharename;
2214 if (!printername || !sharename) {
2215 nt_status = NT_STATUS_UNSUCCESSFUL;
2218 /* we can reset NT_STATUS here because we do not
2219 get any real NT_STATUS-codes anymore from now on */
2220 nt_status = NT_STATUS_UNSUCCESSFUL;
2222 d_printf("migrating printer settings for: [%s] / [%s]\n",
2223 printername, sharename);
2226 /* open src printer handle */
2227 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2228 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2231 /* open dst printer handle */
2232 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2233 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2236 /* check for existing dst printer */
2237 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2242 /* STEP 1: COPY DEVICE-MODE and other
2243 PRINTER_INFO_2-attributes
2246 info_dst.info2 = info_enum[i].info2;
2248 /* why is the port always disconnected when the printer
2249 is correctly installed (incl. driver ???) */
2250 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2252 /* check if printer is published */
2253 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2255 /* check for existing dst printer */
2256 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2259 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2261 /* ignore false from setprinter due to WERR_IO_PENDING */
2262 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2264 DEBUG(3,("republished printer\n"));
2267 if (info_enum[i].info2.devmode != NULL) {
2269 /* copy devmode (info level 2) */
2270 info_dst.info2.devmode = info_enum[i].info2.devmode;
2272 /* do not copy security descriptor (we have another
2273 * command for that) */
2274 info_dst.info2.secdesc = NULL;
2277 info_dst.info2.devmode.devicename =
2278 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2279 longname, printername);
2280 if (!info_dst.info2.devmode.devicename) {
2281 nt_status = NT_STATUS_NO_MEMORY;
2285 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2289 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2292 /* STEP 2: COPY REGISTRY VALUES */
2294 /* please keep in mind that samba parse_spools gives horribly
2295 crippled results when used to rpccli_spoolss_enumprinterdataex
2296 a win2k3-server. (Bugzilla #1851)
2297 FIXME: IIRC I've seen it too on a win2k-server
2300 /* enumerate data on src handle */
2301 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2313 data_offered = data_needed;
2314 value_offered = value_needed;
2315 buffer = talloc_zero_array(mem_ctx, uint8_t, data_needed);
2316 value_name = talloc_zero_array(mem_ctx, char, value_needed);
2318 /* loop for all printerdata of "PrinterDriverData" */
2319 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2321 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2332 /* loop for all reg_keys */
2333 if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2335 struct regval_blob v;
2337 union spoolss_PrinterData printer_data;
2340 if (c->opt_verbose) {
2341 fstrcpy(v.valuename, value_name);
2343 v.size = data_offered;
2345 display_reg_value(SPOOL_PRINTERDATA_KEY, v);
2348 result = pull_spoolss_PrinterData(mem_ctx,
2352 if (!W_ERROR_IS_OK(result)) {
2357 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2358 &hnd_dst, value_name,
2359 type, printer_data))
2362 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2367 /* STEP 3: COPY SUBKEY VALUES */
2369 /* here we need to enum all printer_keys and then work
2370 on the result with enum_printer_key_ex. nt4 does not
2371 respond to enumprinterkey, win2k does, so continue
2372 in case of an error */
2374 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2375 printf("got no key-data\n");
2380 /* work on a list of printer keys
2381 each key has to be enumerated to get all required
2382 information. information is then set via setprinterdataex-calls */
2384 if (keylist == NULL)
2387 for (i=0; keylist && keylist[i] != NULL; i++) {
2389 const char *subkey = keylist[i];
2391 struct spoolss_PrinterEnumValues *info;
2393 /* enumerate all src subkeys */
2394 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2400 for (j=0; j < count; j++) {
2402 struct regval_blob value;
2405 /* although samba replies with sane data in most cases we
2406 should try to avoid writing wrong registry data */
2408 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME) ||
2409 strequal(info[j].value_name, SPOOL_REG_UNCNAME) ||
2410 strequal(info[j].value_name, SPOOL_REG_URL) ||
2411 strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME) ||
2412 strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2414 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
2416 /* although windows uses a multi-sz, we use a sz */
2417 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2418 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2421 if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
2423 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2424 nt_status = NT_STATUS_NO_MEMORY;
2427 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2428 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2431 if (strequal(info[j].value_name, SPOOL_REG_URL)) {
2436 /* FIXME: should we really do that ??? */
2437 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2438 nt_status = NT_STATUS_NO_MEMORY;
2441 init_unistr2(&data, url, UNI_STR_TERMINATE);
2442 fstrcpy(value.valuename, SPOOL_REG_URL);
2446 if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2448 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2449 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2452 if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
2454 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2455 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2458 value.type = REG_SZ;
2459 value.size = data.uni_str_len * 2;
2461 value.data_p = (uint8_t *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2463 value.data_p = NULL;
2467 display_reg_value(subkey, value);
2469 /* here we have to set all subkeys on the dst server */
2470 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2476 struct regval_blob v;
2479 result = push_spoolss_PrinterData(mem_ctx, &blob,
2482 if (!W_ERROR_IS_OK(result)) {
2486 fstrcpy(v.valuename, info[j].value_name);
2487 v.type = info[j].type;
2488 v.data_p = blob.data;
2489 v.size = blob.length;
2491 if (c->opt_verbose) {
2492 display_reg_value(subkey, v);
2495 /* here we have to set all subkeys on the dst server */
2496 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2503 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2504 subkey, info[j].value_name));
2509 TALLOC_FREE(keylist);
2511 /* close printer handles here */
2512 if (is_valid_policy_hnd(&hnd_src)) {
2513 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2516 if (is_valid_policy_hnd(&hnd_dst)) {
2517 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2522 nt_status = NT_STATUS_OK;
2525 SAFE_FREE(devicename);
2527 SAFE_FREE(unc_name);
2529 if (is_valid_policy_hnd(&hnd_src))
2530 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2532 if (is_valid_policy_hnd(&hnd_dst))
2533 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2536 cli_shutdown(cli_dst);