2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2004 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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "utils/net.h"
24 const char *long_archi;
25 const char *short_archi;
30 /* support itanium as well */
31 static const struct table_node archi_table[]= {
33 {"Windows 4.0", "WIN40", 0 },
34 {"Windows NT x86", "W32X86", 2 },
35 {"Windows NT x86", "W32X86", 3 },
36 {"Windows NT R4000", "W32MIPS", 2 },
37 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
38 {"Windows NT PowerPC", "W32PPC", 2 },
39 {"Windows IA64", "IA64", 3 },
40 {"Windows x64", "x64", 3 },
46 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
47 * It is here for debugging purpose and should be removed later on.
50 /****************************************************************************
51 Printer info level 3 display function.
52 ****************************************************************************/
54 static void display_print_driver_3(DRIVER_INFO_3 *i1)
57 fstring architecture = "";
58 fstring driverpath = "";
59 fstring datafile = "";
60 fstring configfile = "";
61 fstring helpfile = "";
62 fstring dependentfiles = "";
63 fstring monitorname = "";
64 fstring defaultdatatype = "";
72 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
73 rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), -1, STR_TERMINATE);
74 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
75 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
76 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
77 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
78 rpcstr_pull(monitorname, i1->monitorname.buffer, sizeof(monitorname), -1, STR_TERMINATE);
79 rpcstr_pull(defaultdatatype, i1->defaultdatatype.buffer, sizeof(defaultdatatype), -1, STR_TERMINATE);
81 d_printf ("Printer Driver Info 3:\n");
82 d_printf ("\tVersion: [%x]\n", i1->version);
83 d_printf ("\tDriver Name: [%s]\n",name);
84 d_printf ("\tArchitecture: [%s]\n", architecture);
85 d_printf ("\tDriver Path: [%s]\n", driverpath);
86 d_printf ("\tDatafile: [%s]\n", datafile);
87 d_printf ("\tConfigfile: [%s]\n", configfile);
88 d_printf ("\tHelpfile: [%s]\n\n", helpfile);
91 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
93 length+=strlen(dependentfiles)+1;
95 if (strlen(dependentfiles) > 0) {
96 d_printf ("\tDependentfiles: [%s]\n", dependentfiles);
104 d_printf ("\tMonitorname: [%s]\n", monitorname);
105 d_printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype);
110 static void display_reg_value(const char *subkey, REGISTRY_VALUE value)
116 d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename,
117 *((uint32 *) value.data_p));
121 rpcstr_pull(text, value.data_p, sizeof(text), value.size,
123 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
127 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
128 subkey, value.valuename);
132 uint16 *curstr = (uint16 *) value.data_p;
133 uint8 *start = value.data_p;
134 d_printf("\t[%s:%s]: REG_MULTI_SZ:\n", subkey, value.valuename);
135 while ((*curstr != 0) &&
136 ((uint8 *) curstr < start + value.size)) {
137 rpcstr_pull(text, curstr, sizeof(text), -1,
139 d_printf("%s\n", text);
140 curstr += strlen(text) + 1;
146 d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
152 * Copies ACLs, DOS-attributes and timestamps from one
153 * file or directory from one connected share to another connected share
155 * @param mem_ctx A talloc-context
156 * @param cli_share_src A connected cli_state
157 * @param cli_share_dst A connected cli_state
158 * @param src_file The source file-name
159 * @param dst_file The destination file-name
160 * @param copy_acls Whether to copy acls
161 * @param copy_attrs Whether to copy DOS attributes
162 * @param copy_timestamps Whether to preserve timestamps
163 * @param is_file Whether this file is a file or a dir
165 * @return Normal NTSTATUS return.
168 NTSTATUS net_copy_fileattr(TALLOC_CTX *mem_ctx,
169 struct cli_state *cli_share_src,
170 struct cli_state *cli_share_dst,
171 const char *src_name, const char *dst_name,
172 BOOL copy_acls, BOOL copy_attrs,
173 BOOL copy_timestamps, BOOL is_file)
175 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
180 time_t f_atime, f_ctime, f_mtime;
183 if (!copy_timestamps && !copy_acls && !copy_attrs)
186 /* open file/dir on the originating server */
188 DEBUGADD(3,("opening %s %s on originating server\n",
189 is_file?"file":"dir", src_name));
191 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
192 if (fnum_src == -1) {
193 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
194 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
195 nt_status = cli_nt_error(cli_share_src);
202 /* get the security descriptor */
203 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
205 DEBUG(0,("failed to get security descriptor: %s\n",
206 cli_errstr(cli_share_src)));
207 nt_status = cli_nt_error(cli_share_src);
211 if (opt_verbose && DEBUGLEVEL >= 3)
212 display_sec_desc(sd);
216 if (copy_attrs || copy_timestamps) {
218 /* get file attributes */
219 if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
220 &f_ctime, &f_atime, &f_mtime)) {
221 DEBUG(0,("failed to get file-attrs: %s\n",
222 cli_errstr(cli_share_src)));
223 nt_status = cli_nt_error(cli_share_src);
229 /* open the file/dir on the destination server */
231 fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
232 if (fnum_dst == -1) {
233 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
234 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
235 nt_status = cli_nt_error(cli_share_dst);
239 if (copy_timestamps) {
242 if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
243 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
244 cli_errstr(cli_share_dst)));
245 nt_status = cli_nt_error(cli_share_dst);
253 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
254 DEBUG(0,("could not set secdesc on %s: %s\n",
255 dst_name, cli_errstr(cli_share_dst)));
256 nt_status = cli_nt_error(cli_share_dst);
264 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
265 DEBUG(0,("failed to set file-attrs: %s\n",
266 cli_errstr(cli_share_dst)));
267 nt_status = cli_nt_error(cli_share_dst);
275 if (!cli_close(cli_share_src, fnum_src)) {
276 d_fprintf(stderr, "could not close %s on originating server: %s\n",
277 is_file?"file":"dir", cli_errstr(cli_share_src));
278 nt_status = cli_nt_error(cli_share_src);
282 if (!cli_close(cli_share_dst, fnum_dst)) {
283 d_fprintf(stderr, "could not close %s on destination server: %s\n",
284 is_file?"file":"dir", cli_errstr(cli_share_dst));
285 nt_status = cli_nt_error(cli_share_dst);
290 nt_status = NT_STATUS_OK;
296 cli_close(cli_share_src, fnum_src);
299 cli_close(cli_share_dst, fnum_dst);
305 * Copy a file or directory from a connected share to another connected share
307 * @param mem_ctx A talloc-context
308 * @param cli_share_src A connected cli_state
309 * @param cli_share_dst A connected cli_state
310 * @param src_file The source file-name
311 * @param dst_file The destination file-name
312 * @param copy_acls Whether to copy acls
313 * @param copy_attrs Whether to copy DOS attributes
314 * @param copy_timestamps Whether to preserve timestamps
315 * @param is_file Whether this file is a file or a dir
317 * @return Normal NTSTATUS return.
320 NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
321 struct cli_state *cli_share_src,
322 struct cli_state *cli_share_dst,
323 const char *src_name, const char *dst_name,
324 BOOL copy_acls, BOOL copy_attrs,
325 BOOL copy_timestamps, BOOL is_file)
327 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
330 static int io_bufsize = 64512;
331 int read_size = io_bufsize;
337 if (!src_name || !dst_name)
340 if (cli_share_src == NULL || cli_share_dst == NULL)
344 /* open on the originating server */
345 DEBUGADD(3,("opening %s %s on originating server\n",
346 is_file ? "file":"dir", src_name));
348 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
350 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
352 if (fnum_src == -1) {
353 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
354 is_file ? "file":"dir",
355 src_name, cli_errstr(cli_share_src)));
356 nt_status = cli_nt_error(cli_share_src);
363 /* open file on the destination server */
364 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
365 fnum_dst = cli_open(cli_share_dst, dst_name,
366 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
368 if (fnum_dst == -1) {
369 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
370 dst_name, cli_errstr(cli_share_dst)));
371 nt_status = cli_nt_error(cli_share_dst);
375 /* allocate memory */
376 if (!(data = (char *)SMB_MALLOC(read_size))) {
377 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
378 nt_status = NT_STATUS_NO_MEMORY;
387 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
388 "%s ACLs and %s DOS Attributes %s\n",
389 cli_share_src->desthost, cli_share_src->share, src_name,
390 cli_share_dst->desthost, cli_share_dst->share, dst_name,
391 copy_acls ? "with" : "without",
392 copy_attrs ? "with" : "without",
393 copy_timestamps ? "(preserving timestamps)" : "" );
401 n = cli_read(cli_share_src, fnum_src, data, nread + start,
407 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
411 d_fprintf(stderr, "Error writing file: %s\n",
412 cli_errstr(cli_share_dst));
413 nt_status = cli_nt_error(cli_share_dst);
421 if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
424 DEBUGADD(3,("creating dir %s on the destination server\n",
427 if (!cli_mkdir(cli_share_dst, dst_name)) {
428 DEBUG(0,("cannot create directory %s: %s\n",
429 dst_name, cli_errstr(cli_share_dst)));
430 nt_status = NT_STATUS_NO_SUCH_FILE;
433 if (!cli_chkpath(cli_share_dst, dst_name)) {
434 d_fprintf(stderr, "cannot check for directory %s: %s\n",
435 dst_name, cli_errstr(cli_share_dst));
442 if (!cli_close(cli_share_src, fnum_src)) {
443 d_fprintf(stderr, "could not close file on originating server: %s\n",
444 cli_errstr(cli_share_src));
445 nt_status = cli_nt_error(cli_share_src);
449 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
450 d_fprintf(stderr, "could not close file on destination server: %s\n",
451 cli_errstr(cli_share_dst));
452 nt_status = cli_nt_error(cli_share_dst);
456 /* possibly we have to copy some file-attributes / acls / sd */
457 nt_status = net_copy_fileattr(mem_ctx, cli_share_src, cli_share_dst,
458 src_name, dst_name, copy_acls,
459 copy_attrs, copy_timestamps, is_file);
460 if (!NT_STATUS_IS_OK(nt_status))
464 nt_status = NT_STATUS_OK;
470 cli_close(cli_share_src, fnum_src);
473 cli_close(cli_share_dst, fnum_dst);
481 * Copy a driverfile from on connected share to another connected share
482 * This silently assumes that a driver-file is picked up from
484 * \\src_server\print$\{arch}\{version}\file
488 * \\dst_server\print$\{arch}\file
490 * to be added via setdriver-calls later.
491 * @param mem_ctx A talloc-context
492 * @param cli_share_src A cli_state connected to source print$-share
493 * @param cli_share_dst A cli_state connected to destination print$-share
494 * @param file The file-name to be copied
495 * @param short_archi The name of the driver-architecture (short form)
497 * @return Normal NTSTATUS return.
500 static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
501 struct cli_state *cli_share_src,
502 struct cli_state *cli_share_dst,
503 char *file, const char *short_archi) {
505 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
513 /* scroll through the file until we have the part
514 beyond archi_table.short_archi */
516 while (next_token(&p, tok, "\\", sizeof(tok))) {
517 if (strequal(tok, short_archi)) {
518 next_token(&p, version, "\\", sizeof(version));
519 next_token(&p, filename, "\\", sizeof(filename));
523 /* build source file name */
524 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
525 return NT_STATUS_NO_MEMORY;
528 /* create destination file name */
529 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
530 return NT_STATUS_NO_MEMORY;
533 /* finally copy the file */
534 nt_status = net_copy_file(mem_ctx, cli_share_src, cli_share_dst,
535 src_name, dst_name, False, False, False, True);
536 if (!NT_STATUS_IS_OK(nt_status))
539 nt_status = NT_STATUS_OK;
549 * Check for existing Architecture directory on a given server
551 * @param cli_share A cli_state connected to a print$-share
552 * @param short_archi The Architecture for the print-driver
554 * @return Normal NTSTATUS return.
557 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
560 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
563 if (asprintf(&dir, "\\%s", short_archi) < 0) {
564 return NT_STATUS_NO_MEMORY;
567 DEBUG(10,("creating print-driver dir for architecture: %s\n",
570 if (!cli_mkdir(cli_share, dir)) {
571 DEBUG(1,("cannot create directory %s: %s\n",
572 dir, cli_errstr(cli_share)));
573 nt_status = NT_STATUS_NO_SUCH_FILE;
576 if (!cli_chkpath(cli_share, dir)) {
577 d_fprintf(stderr, "cannot check %s: %s\n",
578 dir, cli_errstr(cli_share));
582 nt_status = NT_STATUS_OK;
590 * Copy a print-driver (level 3) from one connected print$-share to another
591 * connected print$-share
593 * @param mem_ctx A talloc-context
594 * @param cli_share_src A cli_state connected to a print$-share
595 * @param cli_share_dst A cli_state connected to a print$-share
596 * @param short_archi The Architecture for the print-driver
597 * @param i1 The DRIVER_INFO_3-struct
599 * @return Normal NTSTATUS return.
602 static NTSTATUS copy_print_driver_3(TALLOC_CTX *mem_ctx,
603 struct cli_state *cli_share_src,
604 struct cli_state *cli_share_dst,
605 const char *short_archi, DRIVER_INFO_3 *i1)
607 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
612 fstring driverpath = "";
613 fstring datafile = "";
614 fstring configfile = "";
615 fstring helpfile = "";
616 fstring dependentfiles = "";
621 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
622 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
623 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
624 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
625 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
629 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
630 name, short_archi, i1->version);
632 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
633 driverpath, short_archi);
634 if (!NT_STATUS_IS_OK(nt_status))
637 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
638 datafile, short_archi);
639 if (!NT_STATUS_IS_OK(nt_status))
642 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
643 configfile, short_archi);
644 if (!NT_STATUS_IS_OK(nt_status))
647 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
648 helpfile, short_archi);
649 if (!NT_STATUS_IS_OK(nt_status))
654 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
655 length += strlen(dependentfiles)+1;
657 if (strlen(dependentfiles) > 0) {
659 nt_status = net_copy_driverfile(mem_ctx,
660 cli_share_src, cli_share_dst,
661 dependentfiles, short_archi);
662 if (!NT_STATUS_IS_OK(nt_status))
673 * net_spoolss-functions
674 * =====================
676 * the net_spoolss-functions aim to simplify spoolss-client-functions
677 * required during the migration-process wrt buffer-sizes, returned
680 * this greatly reduces the complexitiy of the migrate-functions.
684 static BOOL net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
689 uint32 *num_printers,
690 PRINTER_INFO_CTR *ctr)
695 result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
696 level, num_printers, ctr);
698 if (!W_ERROR_IS_OK(result)) {
699 printf("cannot enum printers: %s\n", dos_errstr(result));
706 static BOOL net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
708 const char *printername,
709 uint32 access_required,
710 const char *username,
714 fstring servername, printername2;
716 slprintf(servername, sizeof(servername)-1, "\\\\%s", pipe_hnd->cli->desthost);
718 fstrcpy(printername2, servername);
719 fstrcat(printername2, "\\");
720 fstrcat(printername2, printername);
722 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
723 servername, username, printername2, access_required));
726 result = rpccli_spoolss_open_printer_ex(pipe_hnd, mem_ctx, printername2,
728 servername, username, hnd);
730 /* be more verbose */
731 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
732 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
733 printername2, servername, username);
737 if (!W_ERROR_IS_OK(result)) {
738 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
739 printername2, servername, dos_errstr(result));
743 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
744 printername2, servername));
749 static BOOL net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
753 PRINTER_INFO_CTR *ctr)
757 /* getprinter call */
758 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx, hnd, level, ctr);
760 if (!W_ERROR_IS_OK(result)) {
761 printf("cannot get printer-info: %s\n", dos_errstr(result));
768 static BOOL net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
772 PRINTER_INFO_CTR *ctr)
776 /* setprinter call */
777 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, hnd, level, ctr, 0);
779 if (!W_ERROR_IS_OK(result)) {
780 printf("cannot set printer-info: %s\n", dos_errstr(result));
788 static BOOL net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
791 REGISTRY_VALUE *value)
795 /* setprinterdata call */
796 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
798 if (!W_ERROR_IS_OK(result)) {
799 printf ("unable to set printerdata: %s\n", dos_errstr(result));
807 static BOOL net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
815 /* enumprinterkey call */
816 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
818 if (!W_ERROR_IS_OK(result)) {
819 printf("enumprinterkey failed: %s\n", dos_errstr(result));
826 static BOOL net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
835 /* enumprinterdataex call */
836 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
838 if (!W_ERROR_IS_OK(result)) {
839 printf("enumprinterdataex failed: %s\n", dos_errstr(result));
847 static BOOL net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
851 REGISTRY_VALUE *value)
855 /* setprinterdataex call */
856 result = rpccli_spoolss_setprinterdataex(pipe_hnd, mem_ctx, hnd,
859 if (!W_ERROR_IS_OK(result)) {
860 printf("could not set printerdataex: %s\n", dos_errstr(result));
867 static BOOL net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
878 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
880 if (!W_ERROR_IS_OK(result)) {
881 printf("could not enum forms: %s\n", dos_errstr(result));
888 static BOOL net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
890 uint32 level, const char *env,
892 PRINTER_DRIVER_CTR *ctr)
896 /* enumprinterdrivers call */
897 result = rpccli_spoolss_enumprinterdrivers(
898 pipe_hnd, mem_ctx, level,
899 env, num_drivers, ctr);
901 if (!W_ERROR_IS_OK(result)) {
902 printf("cannot enum drivers: %s\n", dos_errstr(result));
909 static BOOL net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
911 POLICY_HND *hnd, uint32 level,
912 const char *env, int version,
913 PRINTER_DRIVER_CTR *ctr)
917 /* getprinterdriver call */
918 result = rpccli_spoolss_getprinterdriver(
919 pipe_hnd, mem_ctx, hnd, level,
922 if (!W_ERROR_IS_OK(result)) {
923 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
924 env, dos_errstr(result)));
925 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
926 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
927 printf("cannot get driver: %s\n", dos_errstr(result));
936 static BOOL net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
937 TALLOC_CTX *mem_ctx, uint32 level,
938 PRINTER_DRIVER_CTR *ctr)
942 /* addprinterdriver call */
943 result = rpccli_spoolss_addprinterdriver(pipe_hnd, mem_ctx, level, ctr);
945 /* be more verbose */
946 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
947 printf("You are not allowed to add drivers\n");
950 if (!W_ERROR_IS_OK(result)) {
951 printf("cannot add driver: %s\n", dos_errstr(result));
959 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
960 * for a single printer or for all printers depending on argc/argv
963 static BOOL get_printer_info(struct rpc_pipe_client *pipe_hnd,
968 uint32 *num_printers,
969 PRINTER_INFO_CTR *ctr)
974 /* no arguments given, enumerate all printers */
977 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
978 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
979 level, num_printers, ctr))
986 /* argument given, get a single printer by name */
987 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
988 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd))
991 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, ctr)) {
992 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
996 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1001 DEBUG(3,("got %d printers\n", *num_printers));
1008 * List print-queues (including local printers that are not shared)
1010 * All parameters are provided by the run_rpc_command function, except for
1011 * argc, argv which are passed through.
1013 * @param domain_sid The domain sid aquired from the remote server
1014 * @param cli A cli_state connected to the server.
1015 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1016 * @param argc Standard main() style argc
1017 * @param argv Standard main() style argv. Initial components are already
1020 * @return Normal NTSTATUS return.
1023 NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid,
1024 const char *domain_name,
1025 struct cli_state *cli,
1026 struct rpc_pipe_client *pipe_hnd,
1027 TALLOC_CTX *mem_ctx,
1031 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1032 uint32 i, num_printers;
1034 pstring printername, sharename;
1035 PRINTER_INFO_CTR ctr;
1037 printf("listing printers\n");
1039 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1042 for (i = 0; i < num_printers; i++) {
1044 /* do some initialization */
1045 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1046 sizeof(printername), -1, STR_TERMINATE);
1047 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1048 sizeof(sharename), -1, STR_TERMINATE);
1050 d_printf("printer %d: %s, shared as: %s\n",
1051 i+1, printername, sharename);
1054 return NT_STATUS_OK;
1058 * List printer-drivers from a server
1060 * All parameters are provided by the run_rpc_command function, except for
1061 * argc, argv which are passed through.
1063 * @param domain_sid The domain sid aquired from the remote server
1064 * @param cli A cli_state connected to the server.
1065 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1066 * @param argc Standard main() style argc
1067 * @param argv Standard main() style argv. Initial components are already
1070 * @return Normal NTSTATUS return.
1073 NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid,
1074 const char *domain_name,
1075 struct cli_state *cli,
1076 struct rpc_pipe_client *pipe_hnd,
1077 TALLOC_CTX *mem_ctx,
1081 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1084 PRINTER_DRIVER_CTR drv_ctr_enum;
1087 ZERO_STRUCT(drv_ctr_enum);
1089 printf("listing printer-drivers\n");
1091 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1095 /* enum remote drivers */
1096 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1097 archi_table[i].long_archi,
1098 &num_drivers, &drv_ctr_enum)) {
1100 nt_status = NT_STATUS_UNSUCCESSFUL;
1104 if (num_drivers == 0) {
1105 d_printf ("no drivers found on server for architecture: [%s].\n",
1106 archi_table[i].long_archi);
1110 d_printf("got %d printer-drivers for architecture: [%s]\n",
1111 num_drivers, archi_table[i].long_archi);
1114 /* do something for all drivers for architecture */
1115 for (d = 0; d < num_drivers; d++) {
1116 display_print_driver_3(&(drv_ctr_enum.info3[d]));
1120 nt_status = NT_STATUS_OK;
1128 * Publish print-queues with args-wrapper
1130 * @param cli A cli_state connected to the server.
1131 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1132 * @param argc Standard main() style argc
1133 * @param argv Standard main() style argv. Initial components are already
1137 * @return Normal NTSTATUS return.
1140 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1141 TALLOC_CTX *mem_ctx,
1146 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1147 uint32 i, num_printers;
1149 pstring printername, sharename;
1150 PRINTER_INFO_CTR ctr, ctr_pub;
1152 BOOL got_hnd = False;
1154 const char *action_str;
1156 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1159 for (i = 0; i < num_printers; i++) {
1161 /* do some initialization */
1162 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1163 sizeof(printername), -1, STR_TERMINATE);
1164 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1165 sizeof(sharename), -1, STR_TERMINATE);
1167 /* open printer handle */
1168 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1169 PRINTER_ALL_ACCESS, pipe_hnd->cli->user_name, &hnd))
1174 /* check for existing dst printer */
1175 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1178 /* check action and set string */
1180 case SPOOL_DS_PUBLISH:
1181 action_str = "published";
1183 case SPOOL_DS_UPDATE:
1184 action_str = "updated";
1186 case SPOOL_DS_UNPUBLISH:
1187 action_str = "unpublished";
1190 action_str = "unknown action";
1191 printf("unkown action: %d\n", action);
1195 ctr_pub.printers_7->action = action;
1197 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub, 0);
1198 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1199 printf("cannot set printer-info: %s\n", dos_errstr(result));
1203 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1206 nt_status = NT_STATUS_OK;
1210 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1215 NTSTATUS rpc_printer_publish_publish_internals(const DOM_SID *domain_sid,
1216 const char *domain_name,
1217 struct cli_state *cli,
1218 struct rpc_pipe_client *pipe_hnd,
1219 TALLOC_CTX *mem_ctx,
1223 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_PUBLISH);
1226 NTSTATUS rpc_printer_publish_unpublish_internals(const DOM_SID *domain_sid,
1227 const char *domain_name,
1228 struct cli_state *cli,
1229 struct rpc_pipe_client *pipe_hnd,
1230 TALLOC_CTX *mem_ctx,
1234 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
1237 NTSTATUS rpc_printer_publish_update_internals(const DOM_SID *domain_sid,
1238 const char *domain_name,
1239 struct cli_state *cli,
1240 struct rpc_pipe_client *pipe_hnd,
1241 TALLOC_CTX *mem_ctx,
1245 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
1249 * List print-queues w.r.t. their publishing state
1251 * All parameters are provided by the run_rpc_command function, except for
1252 * argc, argv which are passed through.
1254 * @param domain_sid The domain sid aquired from the remote server
1255 * @param cli A cli_state connected to the server.
1256 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1257 * @param argc Standard main() style argc
1258 * @param argv Standard main() style argv. Initial components are already
1261 * @return Normal NTSTATUS return.
1264 NTSTATUS rpc_printer_publish_list_internals(const DOM_SID *domain_sid,
1265 const char *domain_name,
1266 struct cli_state *cli,
1267 struct rpc_pipe_client *pipe_hnd,
1268 TALLOC_CTX *mem_ctx,
1272 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1273 uint32 i, num_printers;
1275 pstring printername, sharename;
1277 PRINTER_INFO_CTR ctr, ctr_pub;
1279 BOOL got_hnd = False;
1282 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1285 for (i = 0; i < num_printers; i++) {
1287 ZERO_STRUCT(ctr_pub);
1289 /* do some initialization */
1290 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1291 sizeof(printername), -1, STR_TERMINATE);
1292 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1293 sizeof(sharename), -1, STR_TERMINATE);
1295 /* open printer handle */
1296 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1297 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1302 /* check for existing dst printer */
1303 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1306 rpcstr_pull(guid, ctr_pub.printers_7->guid.buffer, sizeof(guid), -1, STR_TERMINATE);
1308 state = ctr_pub.printers_7->action;
1310 case SPOOL_DS_PUBLISH:
1311 printf("printer [%s] is published", sharename);
1313 printf(", guid: %s", guid);
1316 case SPOOL_DS_UNPUBLISH:
1317 printf("printer [%s] is unpublished\n", sharename);
1319 case SPOOL_DS_UPDATE:
1320 printf("printer [%s] is currently updating\n", sharename);
1323 printf("unkown state: %d\n", state);
1328 nt_status = NT_STATUS_OK;
1332 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1338 * Migrate Printer-ACLs from a source server to the destination server
1340 * All parameters are provided by the run_rpc_command function, except for
1341 * argc, argv which are passed through.
1343 * @param domain_sid The domain sid aquired from the remote server
1344 * @param cli A cli_state connected to the server.
1345 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1346 * @param argc Standard main() style argc
1347 * @param argv Standard main() style argv. Initial components are already
1350 * @return Normal NTSTATUS return.
1353 NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid,
1354 const char *domain_name,
1355 struct cli_state *cli,
1356 struct rpc_pipe_client *pipe_hnd,
1357 TALLOC_CTX *mem_ctx,
1361 /* TODO: what now, info2 or info3 ?
1362 convince jerry that we should add clientside setacls level 3 at least
1364 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1366 uint32 num_printers;
1368 pstring printername = "", sharename = "";
1369 BOOL got_hnd_src = False;
1370 BOOL got_hnd_dst = False;
1371 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1372 POLICY_HND hnd_src, hnd_dst;
1373 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1374 struct cli_state *cli_dst = NULL;
1376 ZERO_STRUCT(ctr_src);
1378 DEBUG(3,("copying printer ACLs\n"));
1380 /* connect destination PI_SPOOLSS */
1381 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1382 if (!NT_STATUS_IS_OK(nt_status))
1386 /* enum source printers */
1387 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1388 nt_status = NT_STATUS_UNSUCCESSFUL;
1392 if (!num_printers) {
1393 printf ("no printers found on server.\n");
1394 nt_status = NT_STATUS_OK;
1398 /* do something for all printers */
1399 for (i = 0; i < num_printers; i++) {
1401 /* do some initialization */
1402 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1403 sizeof(printername), -1, STR_TERMINATE);
1404 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1405 sizeof(sharename), -1, STR_TERMINATE);
1406 /* we can reset NT_STATUS here because we do not
1407 get any real NT_STATUS-codes anymore from now on */
1408 nt_status = NT_STATUS_UNSUCCESSFUL;
1410 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1411 printername, sharename);
1413 /* according to msdn you have specify these access-rights
1414 to see the security descriptor
1415 - READ_CONTROL (DACL)
1416 - ACCESS_SYSTEM_SECURITY (SACL)
1419 /* open src printer handle */
1420 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1421 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1426 /* open dst printer handle */
1427 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1428 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1433 /* check for existing dst printer */
1434 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1437 /* check for existing src printer */
1438 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &ctr_src))
1441 /* Copy Security Descriptor */
1443 /* copy secdesc (info level 2) */
1444 ctr_dst.printers_2->devmode = NULL;
1445 ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
1448 display_sec_desc(ctr_dst.printers_2->secdesc);
1450 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &ctr_dst))
1453 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1456 /* close printer handles here */
1458 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1459 got_hnd_src = False;
1463 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1464 got_hnd_dst = False;
1469 nt_status = NT_STATUS_OK;
1474 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1478 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1482 cli_shutdown(cli_dst);
1488 * Migrate printer-forms from a src server to the dst server
1490 * All parameters are provided by the run_rpc_command function, except for
1491 * argc, argv which are passed through.
1493 * @param domain_sid The domain sid aquired from the remote server
1494 * @param cli A cli_state connected to the server.
1495 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1496 * @param argc Standard main() style argc
1497 * @param argv Standard main() style argv. Initial components are already
1500 * @return Normal NTSTATUS return.
1503 NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid,
1504 const char *domain_name,
1505 struct cli_state *cli,
1506 struct rpc_pipe_client *pipe_hnd,
1507 TALLOC_CTX *mem_ctx,
1511 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1514 uint32 num_printers;
1516 pstring printername = "", sharename = "";
1517 BOOL got_hnd_src = False;
1518 BOOL got_hnd_dst = False;
1519 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1520 POLICY_HND hnd_src, hnd_dst;
1521 PRINTER_INFO_CTR ctr_enum, ctr_dst;
1524 struct cli_state *cli_dst = NULL;
1526 ZERO_STRUCT(ctr_enum);
1528 DEBUG(3,("copying forms\n"));
1530 /* connect destination PI_SPOOLSS */
1531 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1532 if (!NT_STATUS_IS_OK(nt_status))
1536 /* enum src printers */
1537 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1538 nt_status = NT_STATUS_UNSUCCESSFUL;
1542 if (!num_printers) {
1543 printf ("no printers found on server.\n");
1544 nt_status = NT_STATUS_OK;
1549 /* do something for all printers */
1550 for (i = 0; i < num_printers; i++) {
1552 /* do some initialization */
1553 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1554 sizeof(printername), -1, STR_TERMINATE);
1555 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1556 sizeof(sharename), -1, STR_TERMINATE);
1557 /* we can reset NT_STATUS here because we do not
1558 get any real NT_STATUS-codes anymore from now on */
1559 nt_status = NT_STATUS_UNSUCCESSFUL;
1561 d_printf("migrating printer forms for: [%s] / [%s]\n",
1562 printername, sharename);
1565 /* open src printer handle */
1566 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1567 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1573 /* open dst printer handle */
1574 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1575 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1581 /* check for existing dst printer */
1582 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1585 /* finally migrate forms */
1586 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1589 DEBUG(1,("got %d forms for printer\n", num_forms));
1592 for (f = 0; f < num_forms; f++) {
1597 /* only migrate FORM_PRINTER types, according to jerry
1598 FORM_BUILTIN-types are hard-coded in samba */
1599 if (forms[f].flag != FORM_PRINTER)
1602 if (forms[f].name.buffer)
1603 rpcstr_pull(form_name, forms[f].name.buffer,
1604 sizeof(form_name), -1, STR_TERMINATE);
1607 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1608 f, form_name, forms[f].flag);
1610 /* is there a more elegant way to do that ? */
1611 form.flags = FORM_PRINTER;
1612 form.size_x = forms[f].width;
1613 form.size_y = forms[f].length;
1614 form.left = forms[f].left;
1615 form.top = forms[f].top;
1616 form.right = forms[f].right;
1617 form.bottom = forms[f].bottom;
1619 init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
1621 /* FIXME: there might be something wrong with samba's
1623 result = rpccli_spoolss_addform(pipe_hnd_dst, mem_ctx,
1624 &hnd_dst, 1, &form);
1625 if (!W_ERROR_IS_OK(result)) {
1626 d_printf("\tAddForm form %d: [%s] refused.\n",
1631 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1635 /* close printer handles here */
1637 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1638 got_hnd_src = False;
1642 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1643 got_hnd_dst = False;
1647 nt_status = NT_STATUS_OK;
1652 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1655 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1658 cli_shutdown(cli_dst);
1664 * Migrate printer-drivers from a src server to the dst server
1666 * All parameters are provided by the run_rpc_command function, except for
1667 * argc, argv which are passed through.
1669 * @param domain_sid The domain sid aquired from the remote server
1670 * @param cli A cli_state connected to the server.
1671 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1672 * @param argc Standard main() style argc
1673 * @param argv Standard main() style argv. Initial components are already
1676 * @return Normal NTSTATUS return.
1679 NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid,
1680 const char *domain_name,
1681 struct cli_state *cli,
1682 struct rpc_pipe_client *pipe_hnd,
1683 TALLOC_CTX *mem_ctx,
1687 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1689 uint32 num_printers;
1691 pstring printername = "", sharename = "";
1692 BOOL got_hnd_src = False;
1693 BOOL got_hnd_dst = False;
1694 BOOL got_src_driver_share = False;
1695 BOOL got_dst_driver_share = False;
1696 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1697 POLICY_HND hnd_src, hnd_dst;
1698 PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
1699 PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1700 struct cli_state *cli_dst = NULL;
1701 struct cli_state *cli_share_src = NULL;
1702 struct cli_state *cli_share_dst = NULL;
1703 fstring drivername = "";
1705 ZERO_STRUCT(drv_ctr_src);
1706 ZERO_STRUCT(drv_ctr_dst);
1707 ZERO_STRUCT(info_ctr_enum);
1708 ZERO_STRUCT(info_ctr_dst);
1711 DEBUG(3,("copying printer-drivers\n"));
1713 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1714 if (!NT_STATUS_IS_OK(nt_status))
1718 /* open print$-share on the src server */
1719 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
1720 cli->desthost, "print$", "A:");
1721 if (!NT_STATUS_IS_OK(nt_status))
1724 got_src_driver_share = True;
1727 /* open print$-share on the dst server */
1728 nt_status = connect_to_service(&cli_share_dst, &cli_dst->dest_ip,
1729 cli_dst->desthost, "print$", "A:");
1730 if (!NT_STATUS_IS_OK(nt_status))
1733 got_dst_driver_share = True;
1736 /* enum src printers */
1737 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1738 nt_status = NT_STATUS_UNSUCCESSFUL;
1742 if (num_printers == 0) {
1743 printf ("no printers found on server.\n");
1744 nt_status = NT_STATUS_OK;
1749 /* do something for all printers */
1750 for (p = 0; p < num_printers; p++) {
1752 /* do some initialization */
1753 rpcstr_pull(printername, info_ctr_enum.printers_2[p].printername.buffer,
1754 sizeof(printername), -1, STR_TERMINATE);
1755 rpcstr_pull(sharename, info_ctr_enum.printers_2[p].sharename.buffer,
1756 sizeof(sharename), -1, STR_TERMINATE);
1757 /* we can reset NT_STATUS here because we do not
1758 get any real NT_STATUS-codes anymore from now on */
1759 nt_status = NT_STATUS_UNSUCCESSFUL;
1761 d_printf("migrating printer driver for: [%s] / [%s]\n",
1762 printername, sharename);
1764 /* open dst printer handle */
1765 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1766 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1771 /* check for existing dst printer */
1772 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst))
1776 /* open src printer handle */
1777 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1778 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd_src))
1784 /* in a first step call getdriver for each shared printer (per arch)
1785 to get a list of all files that have to be copied */
1787 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1790 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1791 level, archi_table[i].long_archi,
1792 archi_table[i].version, &drv_ctr_src))
1795 rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer,
1796 sizeof(drivername), -1, STR_TERMINATE);
1799 display_print_driver_3(drv_ctr_src.info3);
1802 /* check arch dir */
1803 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1804 if (!NT_STATUS_IS_OK(nt_status))
1808 /* copy driver-files */
1809 nt_status = copy_print_driver_3(mem_ctx, cli_share_src, cli_share_dst,
1810 archi_table[i].short_archi,
1812 if (!NT_STATUS_IS_OK(nt_status))
1817 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_ctr_src)) {
1818 nt_status = NT_STATUS_UNSUCCESSFUL;
1822 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1823 drivername, printername));
1827 if (strlen(drivername) == 0) {
1828 DEBUGADD(1,("Did not get driver for printer %s\n",
1834 init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
1836 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) {
1837 nt_status = NT_STATUS_UNSUCCESSFUL;
1841 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1842 drivername, printername));
1846 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1847 got_hnd_dst = False;
1852 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1853 got_hnd_src = False;
1857 nt_status = NT_STATUS_OK;
1862 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1865 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1868 cli_shutdown(cli_dst);
1871 if (got_src_driver_share)
1872 cli_shutdown(cli_share_src);
1874 if (got_dst_driver_share)
1875 cli_shutdown(cli_share_dst);
1882 * Migrate printer-queues from a src to the dst server
1883 * (requires a working "addprinter command" to be installed for the local smbd)
1885 * All parameters are provided by the run_rpc_command function, except for
1886 * argc, argv which are passed through.
1888 * @param domain_sid The domain sid aquired from the remote server
1889 * @param cli A cli_state connected to the server.
1890 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1891 * @param argc Standard main() style argc
1892 * @param argv Standard main() style argv. Initial components are already
1895 * @return Normal NTSTATUS return.
1898 NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid,
1899 const char *domain_name,
1900 struct cli_state *cli,
1901 struct rpc_pipe_client *pipe_hnd,
1902 TALLOC_CTX *mem_ctx,
1907 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1908 uint32 i = 0, num_printers;
1910 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1911 struct cli_state *cli_dst = NULL;
1912 POLICY_HND hnd_dst, hnd_src;
1913 pstring printername, sharename;
1914 BOOL got_hnd_src = False;
1915 BOOL got_hnd_dst = False;
1916 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1918 DEBUG(3,("copying printers\n"));
1920 /* connect destination PI_SPOOLSS */
1921 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1922 if (!NT_STATUS_IS_OK(nt_status))
1927 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1928 nt_status = NT_STATUS_UNSUCCESSFUL;
1932 if (!num_printers) {
1933 printf ("no printers found on server.\n");
1934 nt_status = NT_STATUS_OK;
1939 /* do something for all printers */
1940 for (i = 0; i < num_printers; i++) {
1942 /* do some initialization */
1943 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1944 sizeof(printername), -1, STR_TERMINATE);
1945 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1946 sizeof(sharename), -1, STR_TERMINATE);
1947 /* we can reset NT_STATUS here because we do not
1948 get any real NT_STATUS-codes anymore from now on */
1949 nt_status = NT_STATUS_UNSUCCESSFUL;
1951 d_printf("migrating printer queue for: [%s] / [%s]\n",
1952 printername, sharename);
1955 /* open dst printer handle */
1956 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1957 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
1959 DEBUG(1,("could not open printer: %s\n", sharename));
1965 /* check for existing dst printer */
1966 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) {
1967 printf ("could not get printer, creating printer.\n");
1969 DEBUG(1,("printer already exists: %s\n", sharename));
1970 /* close printer handles here */
1972 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1973 got_hnd_src = False;
1977 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1978 got_hnd_dst = False;
1984 /* now get again src printer ctr via getprinter,
1985 we first need a handle for that */
1987 /* open src printer handle */
1988 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1989 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1994 /* getprinter on the src server */
1995 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &ctr_src))
1999 /* copy each src printer to a dst printer 1:1,
2000 maybe some values have to be changed though */
2001 d_printf("creating printer: %s\n", printername);
2002 result = rpccli_spoolss_addprinterex (pipe_hnd_dst, mem_ctx, level, &ctr_src);
2004 if (W_ERROR_IS_OK(result))
2005 d_printf ("printer [%s] successfully added.\n", printername);
2006 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2007 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2009 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2013 /* close printer handles here */
2015 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2016 got_hnd_src = False;
2020 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2021 got_hnd_dst = False;
2025 nt_status = NT_STATUS_OK;
2029 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2032 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2035 cli_shutdown(cli_dst);
2041 * Migrate Printer-Settings from a src server to the dst server
2042 * (for this to work, printers and drivers already have to be migrated earlier)
2044 * All parameters are provided by the run_rpc_command function, except for
2045 * argc, argv which are passed through.
2047 * @param domain_sid The domain sid aquired from the remote server
2048 * @param cli A cli_state connected to the server.
2049 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2050 * @param argc Standard main() style argc
2051 * @param argv Standard main() style argv. Initial components are already
2054 * @return Normal NTSTATUS return.
2057 NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid,
2058 const char *domain_name,
2059 struct cli_state *cli,
2060 struct rpc_pipe_client *pipe_hnd,
2061 TALLOC_CTX *mem_ctx,
2066 /* FIXME: Here the nightmare begins */
2069 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2070 uint32 i = 0, p = 0, j = 0;
2071 uint32 num_printers, val_needed, data_needed;
2073 pstring printername = "", sharename = "";
2074 BOOL got_hnd_src = False;
2075 BOOL got_hnd_dst = False;
2076 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2077 POLICY_HND hnd_src, hnd_dst;
2078 PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
2079 REGVAL_CTR *reg_ctr;
2080 struct cli_state *cli_dst = NULL;
2081 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2084 uint16 *keylist = NULL, *curkey;
2086 ZERO_STRUCT(ctr_enum);
2088 DEBUG(3,("copying printer settings\n"));
2090 /* connect destination PI_SPOOLSS */
2091 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
2092 if (!NT_STATUS_IS_OK(nt_status))
2096 /* enum src printers */
2097 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2098 nt_status = NT_STATUS_UNSUCCESSFUL;
2102 if (!num_printers) {
2103 printf ("no printers found on server.\n");
2104 nt_status = NT_STATUS_OK;
2109 /* needed for dns-strings in regkeys */
2110 get_mydnsfullname(longname);
2112 /* do something for all printers */
2113 for (i = 0; i < num_printers; i++) {
2115 /* do some initialization */
2116 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
2117 sizeof(printername), -1, STR_TERMINATE);
2118 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
2119 sizeof(sharename), -1, STR_TERMINATE);
2121 /* we can reset NT_STATUS here because we do not
2122 get any real NT_STATUS-codes anymore from now on */
2123 nt_status = NT_STATUS_UNSUCCESSFUL;
2125 d_printf("migrating printer settings for: [%s] / [%s]\n",
2126 printername, sharename);
2129 /* open src printer handle */
2130 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2131 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2137 /* open dst printer handle */
2138 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2139 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2145 /* check for existing dst printer */
2146 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2151 /* STEP 1: COPY DEVICE-MODE and other
2152 PRINTER_INFO_2-attributes
2155 ctr_dst.printers_2 = &ctr_enum.printers_2[i];
2157 /* why is the port always disconnected when the printer
2158 is correctly installed (incl. driver ???) */
2159 init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
2161 /* check if printer is published */
2162 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2164 /* check for existing dst printer */
2165 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
2168 ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
2170 /* ignore False from setprinter due to WERR_IO_PENDING */
2171 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
2173 DEBUG(3,("republished printer\n"));
2176 if (ctr_enum.printers_2[i].devmode != NULL) {
2178 /* copy devmode (info level 2) */
2179 ctr_dst.printers_2->devmode =
2180 TALLOC_MEMDUP(mem_ctx,
2181 ctr_enum.printers_2[i].devmode,
2182 sizeof(DEVICEMODE));
2184 /* do not copy security descriptor (we have another
2185 * command for that) */
2186 ctr_dst.printers_2->secdesc = NULL;
2189 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2191 nt_status = NT_STATUS_NO_MEMORY;
2195 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2198 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2202 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2205 /* STEP 2: COPY REGISTRY VALUES */
2207 /* please keep in mind that samba parse_spools gives horribly
2208 crippled results when used to rpccli_spoolss_enumprinterdataex
2209 a win2k3-server. (Bugzilla #1851)
2210 FIXME: IIRC I've seen it too on a win2k-server
2213 /* enumerate data on src handle */
2214 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2215 &val_needed, &data_needed, NULL);
2217 /* loop for all printerdata of "PrinterDriverData" */
2218 while (W_ERROR_IS_OK(result)) {
2220 REGISTRY_VALUE value;
2222 result = rpccli_spoolss_enumprinterdata(
2223 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2224 data_needed, 0, 0, &value);
2226 /* loop for all reg_keys */
2227 if (W_ERROR_IS_OK(result)) {
2231 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2234 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2238 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2243 /* STEP 3: COPY SUBKEY VALUES */
2245 /* here we need to enum all printer_keys and then work
2246 on the result with enum_printer_key_ex. nt4 does not
2247 respond to enumprinterkey, win2k does, so continue
2248 in case of an error */
2250 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2251 printf("got no key-data\n");
2256 /* work on a list of printer keys
2257 each key has to be enumerated to get all required
2258 information. information is then set via setprinterdataex-calls */
2260 if (keylist == NULL)
2264 while (*curkey != 0) {
2267 rpcstr_pull(subkey, curkey, sizeof(subkey), -1, STR_TERMINATE);
2269 curkey += strlen(subkey) + 1;
2271 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2272 return NT_STATUS_NO_MEMORY;
2274 /* enumerate all src subkeys */
2275 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2280 for (j=0; j < reg_ctr->num_values; j++) {
2282 REGISTRY_VALUE value;
2285 /* although samba replies with sane data in most cases we
2286 should try to avoid writing wrong registry data */
2288 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2289 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2290 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2291 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2292 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2294 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2296 /* although windows uses a multi-sz, we use a sz */
2297 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2298 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2301 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2303 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2304 nt_status = NT_STATUS_NO_MEMORY;
2307 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2308 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2311 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2316 /* FIXME: should we really do that ??? */
2317 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2318 nt_status = NT_STATUS_NO_MEMORY;
2321 init_unistr2(&data, url, UNI_STR_TERMINATE);
2322 fstrcpy(value.valuename, SPOOL_REG_URL);
2326 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2328 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2329 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2332 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2334 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2335 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2338 value.type = REG_SZ;
2339 value.size = data.uni_str_len * 2;
2340 value.data_p = TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2343 display_reg_value(subkey, value);
2345 /* here we have to set all subkeys on the dst server */
2346 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2353 display_reg_value(subkey, *(reg_ctr->values[j]));
2355 /* here we have to set all subkeys on the dst server */
2356 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2357 subkey, reg_ctr->values[j]))
2362 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2363 subkey, reg_ctr->values[j]->valuename));
2367 TALLOC_FREE( reg_ctr );
2372 /* close printer handles here */
2374 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2375 got_hnd_src = False;
2379 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2380 got_hnd_dst = False;
2385 nt_status = NT_STATUS_OK;
2388 SAFE_FREE(devicename);
2390 SAFE_FREE(unc_name);
2393 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2396 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2399 cli_shutdown(cli_dst);