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 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_driver_3(DRIVER_INFO_3 *i1)
56 fstring architecture = "";
57 fstring driverpath = "";
58 fstring datafile = "";
59 fstring configfile = "";
60 fstring helpfile = "";
61 fstring dependentfiles = "";
62 fstring monitorname = "";
63 fstring defaultdatatype = "";
71 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
72 rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), -1, STR_TERMINATE);
73 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
74 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
75 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
76 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
77 rpcstr_pull(monitorname, i1->monitorname.buffer, sizeof(monitorname), -1, STR_TERMINATE);
78 rpcstr_pull(defaultdatatype, i1->defaultdatatype.buffer, sizeof(defaultdatatype), -1, STR_TERMINATE);
80 d_printf ("Printer Driver Info 3:\n");
81 d_printf ("\tVersion: [%x]\n", i1->version);
82 d_printf ("\tDriver Name: [%s]\n",name);
83 d_printf ("\tArchitecture: [%s]\n", architecture);
84 d_printf ("\tDriver Path: [%s]\n", driverpath);
85 d_printf ("\tDatafile: [%s]\n", datafile);
86 d_printf ("\tConfigfile: [%s]\n", configfile);
87 d_printf ("\tHelpfile: [%s]\n\n", helpfile);
90 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
92 length+=strlen(dependentfiles)+1;
94 if (strlen(dependentfiles) > 0) {
95 d_printf ("\tDependentfiles: [%s]\n", dependentfiles);
103 d_printf ("\tMonitorname: [%s]\n", monitorname);
104 d_printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype);
109 static void display_reg_value(const char *subkey, REGISTRY_VALUE value)
115 d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename,
116 *((uint32 *) value.data_p));
120 rpcstr_pull(text, value.data_p, sizeof(text), value.size,
122 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
126 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
127 subkey, value.valuename);
131 uint32 i, num_values;
134 if (!W_ERROR_IS_OK(reg_pull_multi_sz(NULL, value.data_p,
135 value.size, &num_values,
137 d_printf("reg_pull_multi_sz failed\n");
141 for (i=0; i<num_values; i++) {
142 d_printf("%s\n", values[i]);
149 d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
155 * Copies ACLs, DOS-attributes and timestamps from one
156 * file or directory from one connected share to another connected share
158 * @param mem_ctx A talloc-context
159 * @param cli_share_src A connected cli_state
160 * @param cli_share_dst A connected cli_state
161 * @param src_file The source file-name
162 * @param dst_file The destination file-name
163 * @param copy_acls Whether to copy acls
164 * @param copy_attrs Whether to copy DOS attributes
165 * @param copy_timestamps Whether to preserve timestamps
166 * @param is_file Whether this file is a file or a dir
168 * @return Normal NTSTATUS return.
171 NTSTATUS net_copy_fileattr(TALLOC_CTX *mem_ctx,
172 struct cli_state *cli_share_src,
173 struct cli_state *cli_share_dst,
174 const char *src_name, const char *dst_name,
175 BOOL copy_acls, BOOL copy_attrs,
176 BOOL copy_timestamps, BOOL is_file)
178 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
183 time_t f_atime, f_ctime, f_mtime;
186 if (!copy_timestamps && !copy_acls && !copy_attrs)
189 /* open file/dir on the originating server */
191 DEBUGADD(3,("opening %s %s on originating server\n",
192 is_file?"file":"dir", src_name));
194 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
195 if (fnum_src == -1) {
196 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
197 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
198 nt_status = cli_nt_error(cli_share_src);
205 /* get the security descriptor */
206 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
208 DEBUG(0,("failed to get security descriptor: %s\n",
209 cli_errstr(cli_share_src)));
210 nt_status = cli_nt_error(cli_share_src);
214 if (opt_verbose && DEBUGLEVEL >= 3)
215 display_sec_desc(sd);
219 if (copy_attrs || copy_timestamps) {
221 /* get file attributes */
222 if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
223 &f_ctime, &f_atime, &f_mtime)) {
224 DEBUG(0,("failed to get file-attrs: %s\n",
225 cli_errstr(cli_share_src)));
226 nt_status = cli_nt_error(cli_share_src);
232 /* open the file/dir on the destination server */
234 fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
235 if (fnum_dst == -1) {
236 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
237 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
238 nt_status = cli_nt_error(cli_share_dst);
242 if (copy_timestamps) {
245 if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
246 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
247 cli_errstr(cli_share_dst)));
248 nt_status = cli_nt_error(cli_share_dst);
256 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
257 DEBUG(0,("could not set secdesc on %s: %s\n",
258 dst_name, cli_errstr(cli_share_dst)));
259 nt_status = cli_nt_error(cli_share_dst);
267 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
268 DEBUG(0,("failed to set file-attrs: %s\n",
269 cli_errstr(cli_share_dst)));
270 nt_status = cli_nt_error(cli_share_dst);
278 if (!cli_close(cli_share_src, fnum_src)) {
279 d_fprintf(stderr, "could not close %s on originating server: %s\n",
280 is_file?"file":"dir", cli_errstr(cli_share_src));
281 nt_status = cli_nt_error(cli_share_src);
285 if (!cli_close(cli_share_dst, fnum_dst)) {
286 d_fprintf(stderr, "could not close %s on destination server: %s\n",
287 is_file?"file":"dir", cli_errstr(cli_share_dst));
288 nt_status = cli_nt_error(cli_share_dst);
293 nt_status = NT_STATUS_OK;
299 cli_close(cli_share_src, fnum_src);
302 cli_close(cli_share_dst, fnum_dst);
308 * Copy a file or directory from a connected share to another connected share
310 * @param mem_ctx A talloc-context
311 * @param cli_share_src A connected cli_state
312 * @param cli_share_dst A connected cli_state
313 * @param src_file The source file-name
314 * @param dst_file The destination file-name
315 * @param copy_acls Whether to copy acls
316 * @param copy_attrs Whether to copy DOS attributes
317 * @param copy_timestamps Whether to preserve timestamps
318 * @param is_file Whether this file is a file or a dir
320 * @return Normal NTSTATUS return.
323 NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
324 struct cli_state *cli_share_src,
325 struct cli_state *cli_share_dst,
326 const char *src_name, const char *dst_name,
327 BOOL copy_acls, BOOL copy_attrs,
328 BOOL copy_timestamps, BOOL is_file)
330 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
333 static int io_bufsize = 64512;
334 int read_size = io_bufsize;
340 if (!src_name || !dst_name)
343 if (cli_share_src == NULL || cli_share_dst == NULL)
347 /* open on the originating server */
348 DEBUGADD(3,("opening %s %s on originating server\n",
349 is_file ? "file":"dir", src_name));
351 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
353 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
355 if (fnum_src == -1) {
356 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
357 is_file ? "file":"dir",
358 src_name, cli_errstr(cli_share_src)));
359 nt_status = cli_nt_error(cli_share_src);
366 /* open file on the destination server */
367 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
368 fnum_dst = cli_open(cli_share_dst, dst_name,
369 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
371 if (fnum_dst == -1) {
372 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
373 dst_name, cli_errstr(cli_share_dst)));
374 nt_status = cli_nt_error(cli_share_dst);
378 /* allocate memory */
379 if (!(data = (char *)SMB_MALLOC(read_size))) {
380 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
381 nt_status = NT_STATUS_NO_MEMORY;
390 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
391 "%s ACLs and %s DOS Attributes %s\n",
392 cli_share_src->desthost, cli_share_src->share, src_name,
393 cli_share_dst->desthost, cli_share_dst->share, dst_name,
394 copy_acls ? "with" : "without",
395 copy_attrs ? "with" : "without",
396 copy_timestamps ? "(preserving timestamps)" : "" );
404 n = cli_read(cli_share_src, fnum_src, data, nread + start,
410 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
414 d_fprintf(stderr, "Error writing file: %s\n",
415 cli_errstr(cli_share_dst));
416 nt_status = cli_nt_error(cli_share_dst);
424 if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
427 DEBUGADD(3,("creating dir %s on the destination server\n",
430 if (!cli_mkdir(cli_share_dst, dst_name)) {
431 DEBUG(0,("cannot create directory %s: %s\n",
432 dst_name, cli_errstr(cli_share_dst)));
433 nt_status = NT_STATUS_NO_SUCH_FILE;
436 if (!cli_chkpath(cli_share_dst, dst_name)) {
437 d_fprintf(stderr, "cannot check for directory %s: %s\n",
438 dst_name, cli_errstr(cli_share_dst));
445 if (!cli_close(cli_share_src, fnum_src)) {
446 d_fprintf(stderr, "could not close file on originating server: %s\n",
447 cli_errstr(cli_share_src));
448 nt_status = cli_nt_error(cli_share_src);
452 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
453 d_fprintf(stderr, "could not close file on destination server: %s\n",
454 cli_errstr(cli_share_dst));
455 nt_status = cli_nt_error(cli_share_dst);
459 /* possibly we have to copy some file-attributes / acls / sd */
460 nt_status = net_copy_fileattr(mem_ctx, cli_share_src, cli_share_dst,
461 src_name, dst_name, copy_acls,
462 copy_attrs, copy_timestamps, is_file);
463 if (!NT_STATUS_IS_OK(nt_status))
467 nt_status = NT_STATUS_OK;
473 cli_close(cli_share_src, fnum_src);
476 cli_close(cli_share_dst, fnum_dst);
484 * Copy a driverfile from on connected share to another connected share
485 * This silently assumes that a driver-file is picked up from
487 * \\src_server\print$\{arch}\{version}\file
491 * \\dst_server\print$\{arch}\file
493 * to be added via setdriver-calls later.
494 * @param mem_ctx A talloc-context
495 * @param cli_share_src A cli_state connected to source print$-share
496 * @param cli_share_dst A cli_state connected to destination print$-share
497 * @param file The file-name to be copied
498 * @param short_archi The name of the driver-architecture (short form)
500 * @return Normal NTSTATUS return.
503 static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
504 struct cli_state *cli_share_src,
505 struct cli_state *cli_share_dst,
506 char *file, const char *short_archi) {
508 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
516 /* scroll through the file until we have the part
517 beyond archi_table.short_archi */
519 while (next_token(&p, tok, "\\", sizeof(tok))) {
520 if (strequal(tok, short_archi)) {
521 next_token(&p, version, "\\", sizeof(version));
522 next_token(&p, filename, "\\", sizeof(filename));
526 /* build source file name */
527 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
528 return NT_STATUS_NO_MEMORY;
531 /* create destination file name */
532 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
533 return NT_STATUS_NO_MEMORY;
536 /* finally copy the file */
537 nt_status = net_copy_file(mem_ctx, cli_share_src, cli_share_dst,
538 src_name, dst_name, False, False, False, True);
539 if (!NT_STATUS_IS_OK(nt_status))
542 nt_status = NT_STATUS_OK;
552 * Check for existing Architecture directory on a given server
554 * @param cli_share A cli_state connected to a print$-share
555 * @param short_archi The Architecture for the print-driver
557 * @return Normal NTSTATUS return.
560 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
563 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
566 if (asprintf(&dir, "\\%s", short_archi) < 0) {
567 return NT_STATUS_NO_MEMORY;
570 DEBUG(10,("creating print-driver dir for architecture: %s\n",
573 if (!cli_mkdir(cli_share, dir)) {
574 DEBUG(1,("cannot create directory %s: %s\n",
575 dir, cli_errstr(cli_share)));
576 nt_status = NT_STATUS_NO_SUCH_FILE;
579 if (!cli_chkpath(cli_share, dir)) {
580 d_fprintf(stderr, "cannot check %s: %s\n",
581 dir, cli_errstr(cli_share));
585 nt_status = NT_STATUS_OK;
593 * Copy a print-driver (level 3) from one connected print$-share to another
594 * connected print$-share
596 * @param mem_ctx A talloc-context
597 * @param cli_share_src A cli_state connected to a print$-share
598 * @param cli_share_dst A cli_state connected to a print$-share
599 * @param short_archi The Architecture for the print-driver
600 * @param i1 The DRIVER_INFO_3-struct
602 * @return Normal NTSTATUS return.
605 static NTSTATUS copy_print_driver_3(TALLOC_CTX *mem_ctx,
606 struct cli_state *cli_share_src,
607 struct cli_state *cli_share_dst,
608 const char *short_archi, DRIVER_INFO_3 *i1)
610 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
615 fstring driverpath = "";
616 fstring datafile = "";
617 fstring configfile = "";
618 fstring helpfile = "";
619 fstring dependentfiles = "";
624 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
625 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
626 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
627 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
628 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
632 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
633 name, short_archi, i1->version);
635 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
636 driverpath, short_archi);
637 if (!NT_STATUS_IS_OK(nt_status))
640 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
641 datafile, short_archi);
642 if (!NT_STATUS_IS_OK(nt_status))
645 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
646 configfile, short_archi);
647 if (!NT_STATUS_IS_OK(nt_status))
650 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
651 helpfile, short_archi);
652 if (!NT_STATUS_IS_OK(nt_status))
657 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
658 length += strlen(dependentfiles)+1;
660 if (strlen(dependentfiles) > 0) {
662 nt_status = net_copy_driverfile(mem_ctx,
663 cli_share_src, cli_share_dst,
664 dependentfiles, short_archi);
665 if (!NT_STATUS_IS_OK(nt_status))
676 * net_spoolss-functions
677 * =====================
679 * the net_spoolss-functions aim to simplify spoolss-client-functions
680 * required during the migration-process wrt buffer-sizes, returned
683 * this greatly reduces the complexitiy of the migrate-functions.
687 static BOOL net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
692 uint32 *num_printers,
693 PRINTER_INFO_CTR *ctr)
698 result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
699 level, num_printers, ctr);
701 if (!W_ERROR_IS_OK(result)) {
702 printf("cannot enum printers: %s\n", dos_errstr(result));
709 static BOOL net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
711 const char *printername,
712 uint32 access_required,
713 const char *username,
717 fstring servername, printername2;
719 slprintf(servername, sizeof(servername)-1, "\\\\%s", pipe_hnd->cli->desthost);
721 fstrcpy(printername2, servername);
722 fstrcat(printername2, "\\");
723 fstrcat(printername2, printername);
725 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
726 servername, username, printername2, access_required));
729 result = rpccli_spoolss_open_printer_ex(pipe_hnd, mem_ctx, printername2,
731 servername, username, hnd);
733 /* be more verbose */
734 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
735 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
736 printername2, servername, username);
740 if (!W_ERROR_IS_OK(result)) {
741 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
742 printername2, servername, dos_errstr(result));
746 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
747 printername2, servername));
752 static BOOL net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
756 PRINTER_INFO_CTR *ctr)
760 /* getprinter call */
761 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx, hnd, level, ctr);
763 if (!W_ERROR_IS_OK(result)) {
764 printf("cannot get printer-info: %s\n", dos_errstr(result));
771 static BOOL net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
775 PRINTER_INFO_CTR *ctr)
779 /* setprinter call */
780 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, hnd, level, ctr, 0);
782 if (!W_ERROR_IS_OK(result)) {
783 printf("cannot set printer-info: %s\n", dos_errstr(result));
791 static BOOL net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
794 REGISTRY_VALUE *value)
798 /* setprinterdata call */
799 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
801 if (!W_ERROR_IS_OK(result)) {
802 printf ("unable to set printerdata: %s\n", dos_errstr(result));
810 static BOOL net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
818 /* enumprinterkey call */
819 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
821 if (!W_ERROR_IS_OK(result)) {
822 printf("enumprinterkey failed: %s\n", dos_errstr(result));
829 static BOOL net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
838 /* enumprinterdataex call */
839 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
841 if (!W_ERROR_IS_OK(result)) {
842 printf("enumprinterdataex failed: %s\n", dos_errstr(result));
850 static BOOL net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
854 REGISTRY_VALUE *value)
858 /* setprinterdataex call */
859 result = rpccli_spoolss_setprinterdataex(pipe_hnd, mem_ctx, hnd,
862 if (!W_ERROR_IS_OK(result)) {
863 printf("could not set printerdataex: %s\n", dos_errstr(result));
870 static BOOL net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
881 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
883 if (!W_ERROR_IS_OK(result)) {
884 printf("could not enum forms: %s\n", dos_errstr(result));
891 static BOOL net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
893 uint32 level, const char *env,
895 PRINTER_DRIVER_CTR *ctr)
899 /* enumprinterdrivers call */
900 result = rpccli_spoolss_enumprinterdrivers(
901 pipe_hnd, mem_ctx, level,
902 env, num_drivers, ctr);
904 if (!W_ERROR_IS_OK(result)) {
905 printf("cannot enum drivers: %s\n", dos_errstr(result));
912 static BOOL net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
914 POLICY_HND *hnd, uint32 level,
915 const char *env, int version,
916 PRINTER_DRIVER_CTR *ctr)
920 /* getprinterdriver call */
921 result = rpccli_spoolss_getprinterdriver(
922 pipe_hnd, mem_ctx, hnd, level,
925 if (!W_ERROR_IS_OK(result)) {
926 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
927 env, dos_errstr(result)));
928 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
929 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
930 printf("cannot get driver: %s\n", dos_errstr(result));
939 static BOOL net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
940 TALLOC_CTX *mem_ctx, uint32 level,
941 PRINTER_DRIVER_CTR *ctr)
945 /* addprinterdriver call */
946 result = rpccli_spoolss_addprinterdriver(pipe_hnd, mem_ctx, level, ctr);
948 /* be more verbose */
949 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
950 printf("You are not allowed to add drivers\n");
953 if (!W_ERROR_IS_OK(result)) {
954 printf("cannot add driver: %s\n", dos_errstr(result));
962 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
963 * for a single printer or for all printers depending on argc/argv
966 static BOOL get_printer_info(struct rpc_pipe_client *pipe_hnd,
971 uint32 *num_printers,
972 PRINTER_INFO_CTR *ctr)
977 /* no arguments given, enumerate all printers */
980 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
981 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
982 level, num_printers, ctr))
989 /* argument given, get a single printer by name */
990 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
991 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd))
994 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, ctr)) {
995 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
999 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1004 DEBUG(3,("got %d printers\n", *num_printers));
1011 * List print-queues (including local printers that are not shared)
1013 * All parameters are provided by the run_rpc_command function, except for
1014 * argc, argv which are passed through.
1016 * @param domain_sid The domain sid aquired from the remote server
1017 * @param cli A cli_state connected to the server.
1018 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1019 * @param argc Standard main() style argc
1020 * @param argv Standard main() style argv. Initial components are already
1023 * @return Normal NTSTATUS return.
1026 NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid,
1027 const char *domain_name,
1028 struct cli_state *cli,
1029 struct rpc_pipe_client *pipe_hnd,
1030 TALLOC_CTX *mem_ctx,
1034 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1035 uint32 i, num_printers;
1037 pstring printername, sharename;
1038 PRINTER_INFO_CTR ctr;
1040 printf("listing printers\n");
1042 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1045 for (i = 0; i < num_printers; i++) {
1047 /* do some initialization */
1048 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1049 sizeof(printername), -1, STR_TERMINATE);
1050 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1051 sizeof(sharename), -1, STR_TERMINATE);
1053 d_printf("printer %d: %s, shared as: %s\n",
1054 i+1, printername, sharename);
1057 return NT_STATUS_OK;
1061 * List printer-drivers from a server
1063 * All parameters are provided by the run_rpc_command function, except for
1064 * argc, argv which are passed through.
1066 * @param domain_sid The domain sid aquired from the remote server
1067 * @param cli A cli_state connected to the server.
1068 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1069 * @param argc Standard main() style argc
1070 * @param argv Standard main() style argv. Initial components are already
1073 * @return Normal NTSTATUS return.
1076 NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid,
1077 const char *domain_name,
1078 struct cli_state *cli,
1079 struct rpc_pipe_client *pipe_hnd,
1080 TALLOC_CTX *mem_ctx,
1084 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1087 PRINTER_DRIVER_CTR drv_ctr_enum;
1090 ZERO_STRUCT(drv_ctr_enum);
1092 printf("listing printer-drivers\n");
1094 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1098 /* enum remote drivers */
1099 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1100 archi_table[i].long_archi,
1101 &num_drivers, &drv_ctr_enum)) {
1103 nt_status = NT_STATUS_UNSUCCESSFUL;
1107 if (num_drivers == 0) {
1108 d_printf ("no drivers found on server for architecture: [%s].\n",
1109 archi_table[i].long_archi);
1113 d_printf("got %d printer-drivers for architecture: [%s]\n",
1114 num_drivers, archi_table[i].long_archi);
1117 /* do something for all drivers for architecture */
1118 for (d = 0; d < num_drivers; d++) {
1119 display_print_driver_3(&(drv_ctr_enum.info3[d]));
1123 nt_status = NT_STATUS_OK;
1131 * Publish print-queues with args-wrapper
1133 * @param cli A cli_state connected to the server.
1134 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1135 * @param argc Standard main() style argc
1136 * @param argv Standard main() style argv. Initial components are already
1140 * @return Normal NTSTATUS return.
1143 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1144 TALLOC_CTX *mem_ctx,
1149 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1150 uint32 i, num_printers;
1152 pstring printername, sharename;
1153 PRINTER_INFO_CTR ctr, ctr_pub;
1155 BOOL got_hnd = False;
1157 const char *action_str;
1159 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1162 for (i = 0; i < num_printers; i++) {
1164 /* do some initialization */
1165 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1166 sizeof(printername), -1, STR_TERMINATE);
1167 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1168 sizeof(sharename), -1, STR_TERMINATE);
1170 /* open printer handle */
1171 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1172 PRINTER_ALL_ACCESS, pipe_hnd->cli->user_name, &hnd))
1177 /* check for existing dst printer */
1178 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1181 /* check action and set string */
1183 case SPOOL_DS_PUBLISH:
1184 action_str = "published";
1186 case SPOOL_DS_UPDATE:
1187 action_str = "updated";
1189 case SPOOL_DS_UNPUBLISH:
1190 action_str = "unpublished";
1193 action_str = "unknown action";
1194 printf("unkown action: %d\n", action);
1198 ctr_pub.printers_7->action = action;
1200 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub, 0);
1201 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1202 printf("cannot set printer-info: %s\n", dos_errstr(result));
1206 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1209 nt_status = NT_STATUS_OK;
1213 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1218 NTSTATUS rpc_printer_publish_publish_internals(const DOM_SID *domain_sid,
1219 const char *domain_name,
1220 struct cli_state *cli,
1221 struct rpc_pipe_client *pipe_hnd,
1222 TALLOC_CTX *mem_ctx,
1226 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_PUBLISH);
1229 NTSTATUS rpc_printer_publish_unpublish_internals(const DOM_SID *domain_sid,
1230 const char *domain_name,
1231 struct cli_state *cli,
1232 struct rpc_pipe_client *pipe_hnd,
1233 TALLOC_CTX *mem_ctx,
1237 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
1240 NTSTATUS rpc_printer_publish_update_internals(const DOM_SID *domain_sid,
1241 const char *domain_name,
1242 struct cli_state *cli,
1243 struct rpc_pipe_client *pipe_hnd,
1244 TALLOC_CTX *mem_ctx,
1248 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
1252 * List print-queues w.r.t. their publishing state
1254 * All parameters are provided by the run_rpc_command function, except for
1255 * argc, argv which are passed through.
1257 * @param domain_sid The domain sid aquired from the remote server
1258 * @param cli A cli_state connected to the server.
1259 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1260 * @param argc Standard main() style argc
1261 * @param argv Standard main() style argv. Initial components are already
1264 * @return Normal NTSTATUS return.
1267 NTSTATUS rpc_printer_publish_list_internals(const DOM_SID *domain_sid,
1268 const char *domain_name,
1269 struct cli_state *cli,
1270 struct rpc_pipe_client *pipe_hnd,
1271 TALLOC_CTX *mem_ctx,
1275 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1276 uint32 i, num_printers;
1278 pstring printername, sharename;
1280 PRINTER_INFO_CTR ctr, ctr_pub;
1282 BOOL got_hnd = False;
1285 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1288 for (i = 0; i < num_printers; i++) {
1290 ZERO_STRUCT(ctr_pub);
1292 /* do some initialization */
1293 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1294 sizeof(printername), -1, STR_TERMINATE);
1295 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1296 sizeof(sharename), -1, STR_TERMINATE);
1298 /* open printer handle */
1299 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1300 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1305 /* check for existing dst printer */
1306 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1309 rpcstr_pull(guid, ctr_pub.printers_7->guid.buffer, sizeof(guid), -1, STR_TERMINATE);
1311 state = ctr_pub.printers_7->action;
1313 case SPOOL_DS_PUBLISH:
1314 printf("printer [%s] is published", sharename);
1316 printf(", guid: %s", guid);
1319 case SPOOL_DS_UNPUBLISH:
1320 printf("printer [%s] is unpublished\n", sharename);
1322 case SPOOL_DS_UPDATE:
1323 printf("printer [%s] is currently updating\n", sharename);
1326 printf("unkown state: %d\n", state);
1331 nt_status = NT_STATUS_OK;
1335 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1341 * Migrate Printer-ACLs from a source server to the destination server
1343 * All parameters are provided by the run_rpc_command function, except for
1344 * argc, argv which are passed through.
1346 * @param domain_sid The domain sid aquired from the remote server
1347 * @param cli A cli_state connected to the server.
1348 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1349 * @param argc Standard main() style argc
1350 * @param argv Standard main() style argv. Initial components are already
1353 * @return Normal NTSTATUS return.
1356 NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid,
1357 const char *domain_name,
1358 struct cli_state *cli,
1359 struct rpc_pipe_client *pipe_hnd,
1360 TALLOC_CTX *mem_ctx,
1364 /* TODO: what now, info2 or info3 ?
1365 convince jerry that we should add clientside setacls level 3 at least
1367 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1369 uint32 num_printers;
1371 pstring printername = "", sharename = "";
1372 BOOL got_hnd_src = False;
1373 BOOL got_hnd_dst = False;
1374 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1375 POLICY_HND hnd_src, hnd_dst;
1376 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1377 struct cli_state *cli_dst = NULL;
1379 ZERO_STRUCT(ctr_src);
1381 DEBUG(3,("copying printer ACLs\n"));
1383 /* connect destination PI_SPOOLSS */
1384 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1385 if (!NT_STATUS_IS_OK(nt_status))
1389 /* enum source printers */
1390 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1391 nt_status = NT_STATUS_UNSUCCESSFUL;
1395 if (!num_printers) {
1396 printf ("no printers found on server.\n");
1397 nt_status = NT_STATUS_OK;
1401 /* do something for all printers */
1402 for (i = 0; i < num_printers; i++) {
1404 /* do some initialization */
1405 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1406 sizeof(printername), -1, STR_TERMINATE);
1407 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1408 sizeof(sharename), -1, STR_TERMINATE);
1409 /* we can reset NT_STATUS here because we do not
1410 get any real NT_STATUS-codes anymore from now on */
1411 nt_status = NT_STATUS_UNSUCCESSFUL;
1413 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1414 printername, sharename);
1416 /* according to msdn you have specify these access-rights
1417 to see the security descriptor
1418 - READ_CONTROL (DACL)
1419 - ACCESS_SYSTEM_SECURITY (SACL)
1422 /* open src printer handle */
1423 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1424 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1429 /* open dst printer handle */
1430 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1431 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1436 /* check for existing dst printer */
1437 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1440 /* check for existing src printer */
1441 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &ctr_src))
1444 /* Copy Security Descriptor */
1446 /* copy secdesc (info level 2) */
1447 ctr_dst.printers_2->devmode = NULL;
1448 ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
1451 display_sec_desc(ctr_dst.printers_2->secdesc);
1453 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &ctr_dst))
1456 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1459 /* close printer handles here */
1461 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1462 got_hnd_src = False;
1466 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1467 got_hnd_dst = False;
1472 nt_status = NT_STATUS_OK;
1477 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1481 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1485 cli_shutdown(cli_dst);
1491 * Migrate printer-forms from a src server to the dst server
1493 * All parameters are provided by the run_rpc_command function, except for
1494 * argc, argv which are passed through.
1496 * @param domain_sid The domain sid aquired from the remote server
1497 * @param cli A cli_state connected to the server.
1498 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1499 * @param argc Standard main() style argc
1500 * @param argv Standard main() style argv. Initial components are already
1503 * @return Normal NTSTATUS return.
1506 NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid,
1507 const char *domain_name,
1508 struct cli_state *cli,
1509 struct rpc_pipe_client *pipe_hnd,
1510 TALLOC_CTX *mem_ctx,
1514 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1517 uint32 num_printers;
1519 pstring printername = "", sharename = "";
1520 BOOL got_hnd_src = False;
1521 BOOL got_hnd_dst = False;
1522 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1523 POLICY_HND hnd_src, hnd_dst;
1524 PRINTER_INFO_CTR ctr_enum, ctr_dst;
1527 struct cli_state *cli_dst = NULL;
1529 ZERO_STRUCT(ctr_enum);
1531 DEBUG(3,("copying forms\n"));
1533 /* connect destination PI_SPOOLSS */
1534 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1535 if (!NT_STATUS_IS_OK(nt_status))
1539 /* enum src printers */
1540 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1541 nt_status = NT_STATUS_UNSUCCESSFUL;
1545 if (!num_printers) {
1546 printf ("no printers found on server.\n");
1547 nt_status = NT_STATUS_OK;
1552 /* do something for all printers */
1553 for (i = 0; i < num_printers; i++) {
1555 /* do some initialization */
1556 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1557 sizeof(printername), -1, STR_TERMINATE);
1558 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1559 sizeof(sharename), -1, STR_TERMINATE);
1560 /* we can reset NT_STATUS here because we do not
1561 get any real NT_STATUS-codes anymore from now on */
1562 nt_status = NT_STATUS_UNSUCCESSFUL;
1564 d_printf("migrating printer forms for: [%s] / [%s]\n",
1565 printername, sharename);
1568 /* open src printer handle */
1569 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1570 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1576 /* open dst printer handle */
1577 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1578 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1584 /* check for existing dst printer */
1585 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1588 /* finally migrate forms */
1589 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1592 DEBUG(1,("got %d forms for printer\n", num_forms));
1595 for (f = 0; f < num_forms; f++) {
1600 /* only migrate FORM_PRINTER types, according to jerry
1601 FORM_BUILTIN-types are hard-coded in samba */
1602 if (forms[f].flag != FORM_PRINTER)
1605 if (forms[f].name.buffer)
1606 rpcstr_pull(form_name, forms[f].name.buffer,
1607 sizeof(form_name), -1, STR_TERMINATE);
1610 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1611 f, form_name, forms[f].flag);
1613 /* is there a more elegant way to do that ? */
1614 form.flags = FORM_PRINTER;
1615 form.size_x = forms[f].width;
1616 form.size_y = forms[f].length;
1617 form.left = forms[f].left;
1618 form.top = forms[f].top;
1619 form.right = forms[f].right;
1620 form.bottom = forms[f].bottom;
1622 init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
1624 /* FIXME: there might be something wrong with samba's
1626 result = rpccli_spoolss_addform(pipe_hnd_dst, mem_ctx,
1627 &hnd_dst, 1, &form);
1628 if (!W_ERROR_IS_OK(result)) {
1629 d_printf("\tAddForm form %d: [%s] refused.\n",
1634 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1638 /* close printer handles here */
1640 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1641 got_hnd_src = False;
1645 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1646 got_hnd_dst = False;
1650 nt_status = NT_STATUS_OK;
1655 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1658 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1661 cli_shutdown(cli_dst);
1667 * Migrate printer-drivers from a src server to the dst server
1669 * All parameters are provided by the run_rpc_command function, except for
1670 * argc, argv which are passed through.
1672 * @param domain_sid The domain sid aquired from the remote server
1673 * @param cli A cli_state connected to the server.
1674 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1675 * @param argc Standard main() style argc
1676 * @param argv Standard main() style argv. Initial components are already
1679 * @return Normal NTSTATUS return.
1682 NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid,
1683 const char *domain_name,
1684 struct cli_state *cli,
1685 struct rpc_pipe_client *pipe_hnd,
1686 TALLOC_CTX *mem_ctx,
1690 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1692 uint32 num_printers;
1694 pstring printername = "", sharename = "";
1695 BOOL got_hnd_src = False;
1696 BOOL got_hnd_dst = False;
1697 BOOL got_src_driver_share = False;
1698 BOOL got_dst_driver_share = False;
1699 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1700 POLICY_HND hnd_src, hnd_dst;
1701 PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
1702 PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1703 struct cli_state *cli_dst = NULL;
1704 struct cli_state *cli_share_src = NULL;
1705 struct cli_state *cli_share_dst = NULL;
1706 fstring drivername = "";
1708 ZERO_STRUCT(drv_ctr_src);
1709 ZERO_STRUCT(drv_ctr_dst);
1710 ZERO_STRUCT(info_ctr_enum);
1711 ZERO_STRUCT(info_ctr_dst);
1714 DEBUG(3,("copying printer-drivers\n"));
1716 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1717 if (!NT_STATUS_IS_OK(nt_status))
1721 /* open print$-share on the src server */
1722 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
1723 cli->desthost, "print$", "A:");
1724 if (!NT_STATUS_IS_OK(nt_status))
1727 got_src_driver_share = True;
1730 /* open print$-share on the dst server */
1731 nt_status = connect_to_service(&cli_share_dst, &cli_dst->dest_ip,
1732 cli_dst->desthost, "print$", "A:");
1733 if (!NT_STATUS_IS_OK(nt_status))
1736 got_dst_driver_share = True;
1739 /* enum src printers */
1740 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1741 nt_status = NT_STATUS_UNSUCCESSFUL;
1745 if (num_printers == 0) {
1746 printf ("no printers found on server.\n");
1747 nt_status = NT_STATUS_OK;
1752 /* do something for all printers */
1753 for (p = 0; p < num_printers; p++) {
1755 /* do some initialization */
1756 rpcstr_pull(printername, info_ctr_enum.printers_2[p].printername.buffer,
1757 sizeof(printername), -1, STR_TERMINATE);
1758 rpcstr_pull(sharename, info_ctr_enum.printers_2[p].sharename.buffer,
1759 sizeof(sharename), -1, STR_TERMINATE);
1760 /* we can reset NT_STATUS here because we do not
1761 get any real NT_STATUS-codes anymore from now on */
1762 nt_status = NT_STATUS_UNSUCCESSFUL;
1764 d_printf("migrating printer driver for: [%s] / [%s]\n",
1765 printername, sharename);
1767 /* open dst printer handle */
1768 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1769 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1774 /* check for existing dst printer */
1775 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst))
1779 /* open src printer handle */
1780 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1781 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd_src))
1787 /* in a first step call getdriver for each shared printer (per arch)
1788 to get a list of all files that have to be copied */
1790 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1793 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1794 level, archi_table[i].long_archi,
1795 archi_table[i].version, &drv_ctr_src))
1798 rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer,
1799 sizeof(drivername), -1, STR_TERMINATE);
1802 display_print_driver_3(drv_ctr_src.info3);
1805 /* check arch dir */
1806 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1807 if (!NT_STATUS_IS_OK(nt_status))
1811 /* copy driver-files */
1812 nt_status = copy_print_driver_3(mem_ctx, cli_share_src, cli_share_dst,
1813 archi_table[i].short_archi,
1815 if (!NT_STATUS_IS_OK(nt_status))
1820 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_ctr_src)) {
1821 nt_status = NT_STATUS_UNSUCCESSFUL;
1825 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1826 drivername, printername));
1830 if (strlen(drivername) == 0) {
1831 DEBUGADD(1,("Did not get driver for printer %s\n",
1837 init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
1839 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) {
1840 nt_status = NT_STATUS_UNSUCCESSFUL;
1844 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1845 drivername, printername));
1849 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1850 got_hnd_dst = False;
1855 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1856 got_hnd_src = False;
1860 nt_status = NT_STATUS_OK;
1865 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1868 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1871 cli_shutdown(cli_dst);
1874 if (got_src_driver_share)
1875 cli_shutdown(cli_share_src);
1877 if (got_dst_driver_share)
1878 cli_shutdown(cli_share_dst);
1885 * Migrate printer-queues from a src to the dst server
1886 * (requires a working "addprinter command" to be installed for the local smbd)
1888 * All parameters are provided by the run_rpc_command function, except for
1889 * argc, argv which are passed through.
1891 * @param domain_sid The domain sid aquired from the remote server
1892 * @param cli A cli_state connected to the server.
1893 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1894 * @param argc Standard main() style argc
1895 * @param argv Standard main() style argv. Initial components are already
1898 * @return Normal NTSTATUS return.
1901 NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid,
1902 const char *domain_name,
1903 struct cli_state *cli,
1904 struct rpc_pipe_client *pipe_hnd,
1905 TALLOC_CTX *mem_ctx,
1910 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1911 uint32 i = 0, num_printers;
1913 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1914 struct cli_state *cli_dst = NULL;
1915 POLICY_HND hnd_dst, hnd_src;
1916 pstring printername, sharename;
1917 BOOL got_hnd_src = False;
1918 BOOL got_hnd_dst = False;
1919 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1921 DEBUG(3,("copying printers\n"));
1923 /* connect destination PI_SPOOLSS */
1924 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1925 if (!NT_STATUS_IS_OK(nt_status))
1929 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1930 nt_status = NT_STATUS_UNSUCCESSFUL;
1934 if (!num_printers) {
1935 printf ("no printers found on server.\n");
1936 nt_status = NT_STATUS_OK;
1941 /* do something for all printers */
1942 for (i = 0; i < num_printers; i++) {
1944 /* do some initialization */
1945 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1946 sizeof(printername), -1, STR_TERMINATE);
1947 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1948 sizeof(sharename), -1, STR_TERMINATE);
1949 /* we can reset NT_STATUS here because we do not
1950 get any real NT_STATUS-codes anymore from now on */
1951 nt_status = NT_STATUS_UNSUCCESSFUL;
1953 d_printf("migrating printer queue for: [%s] / [%s]\n",
1954 printername, sharename);
1956 /* open dst printer handle */
1957 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1958 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
1960 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 handle here - dst only, not got src yet. */
1972 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1973 got_hnd_dst = False;
1978 /* now get again src printer ctr via getprinter,
1979 we first need a handle for that */
1981 /* open src printer handle */
1982 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1983 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1988 /* getprinter on the src server */
1989 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &ctr_src))
1992 /* copy each src printer to a dst printer 1:1,
1993 maybe some values have to be changed though */
1994 d_printf("creating printer: %s\n", printername);
1995 result = rpccli_spoolss_addprinterex (pipe_hnd_dst, mem_ctx, level, &ctr_src);
1997 if (W_ERROR_IS_OK(result))
1998 d_printf ("printer [%s] successfully added.\n", printername);
1999 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2000 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2002 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2006 /* close printer handles here */
2008 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2009 got_hnd_src = False;
2013 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2014 got_hnd_dst = False;
2018 nt_status = NT_STATUS_OK;
2022 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2025 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2028 cli_shutdown(cli_dst);
2034 * Migrate Printer-Settings from a src server to the dst server
2035 * (for this to work, printers and drivers already have to be migrated earlier)
2037 * All parameters are provided by the run_rpc_command function, except for
2038 * argc, argv which are passed through.
2040 * @param domain_sid The domain sid aquired from the remote server
2041 * @param cli A cli_state connected to the server.
2042 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2043 * @param argc Standard main() style argc
2044 * @param argv Standard main() style argv. Initial components are already
2047 * @return Normal NTSTATUS return.
2050 NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid,
2051 const char *domain_name,
2052 struct cli_state *cli,
2053 struct rpc_pipe_client *pipe_hnd,
2054 TALLOC_CTX *mem_ctx,
2059 /* FIXME: Here the nightmare begins */
2062 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2063 uint32 i = 0, p = 0, j = 0;
2064 uint32 num_printers, val_needed, data_needed;
2066 pstring printername = "", sharename = "";
2067 BOOL got_hnd_src = False;
2068 BOOL got_hnd_dst = False;
2069 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2070 POLICY_HND hnd_src, hnd_dst;
2071 PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
2072 REGVAL_CTR *reg_ctr;
2073 struct cli_state *cli_dst = NULL;
2074 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2077 uint16 *keylist = NULL, *curkey;
2079 ZERO_STRUCT(ctr_enum);
2081 DEBUG(3,("copying printer settings\n"));
2083 /* connect destination PI_SPOOLSS */
2084 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
2085 if (!NT_STATUS_IS_OK(nt_status))
2089 /* enum src printers */
2090 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2091 nt_status = NT_STATUS_UNSUCCESSFUL;
2095 if (!num_printers) {
2096 printf ("no printers found on server.\n");
2097 nt_status = NT_STATUS_OK;
2102 /* needed for dns-strings in regkeys */
2103 get_mydnsfullname(longname);
2105 /* do something for all printers */
2106 for (i = 0; i < num_printers; i++) {
2108 /* do some initialization */
2109 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
2110 sizeof(printername), -1, STR_TERMINATE);
2111 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
2112 sizeof(sharename), -1, STR_TERMINATE);
2114 /* we can reset NT_STATUS here because we do not
2115 get any real NT_STATUS-codes anymore from now on */
2116 nt_status = NT_STATUS_UNSUCCESSFUL;
2118 d_printf("migrating printer settings for: [%s] / [%s]\n",
2119 printername, sharename);
2122 /* open src printer handle */
2123 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2124 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2130 /* open dst printer handle */
2131 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2132 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2138 /* check for existing dst printer */
2139 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2144 /* STEP 1: COPY DEVICE-MODE and other
2145 PRINTER_INFO_2-attributes
2148 ctr_dst.printers_2 = &ctr_enum.printers_2[i];
2150 /* why is the port always disconnected when the printer
2151 is correctly installed (incl. driver ???) */
2152 init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
2154 /* check if printer is published */
2155 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2157 /* check for existing dst printer */
2158 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
2161 ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
2163 /* ignore False from setprinter due to WERR_IO_PENDING */
2164 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
2166 DEBUG(3,("republished printer\n"));
2169 if (ctr_enum.printers_2[i].devmode != NULL) {
2171 /* copy devmode (info level 2) */
2172 ctr_dst.printers_2->devmode = (DEVICEMODE *)
2173 TALLOC_MEMDUP(mem_ctx,
2174 ctr_enum.printers_2[i].devmode,
2175 sizeof(DEVICEMODE));
2177 /* do not copy security descriptor (we have another
2178 * command for that) */
2179 ctr_dst.printers_2->secdesc = NULL;
2182 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2184 nt_status = NT_STATUS_NO_MEMORY;
2188 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2191 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2195 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2198 /* STEP 2: COPY REGISTRY VALUES */
2200 /* please keep in mind that samba parse_spools gives horribly
2201 crippled results when used to rpccli_spoolss_enumprinterdataex
2202 a win2k3-server. (Bugzilla #1851)
2203 FIXME: IIRC I've seen it too on a win2k-server
2206 /* enumerate data on src handle */
2207 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2208 &val_needed, &data_needed, NULL);
2210 /* loop for all printerdata of "PrinterDriverData" */
2211 while (W_ERROR_IS_OK(result)) {
2213 REGISTRY_VALUE value;
2215 result = rpccli_spoolss_enumprinterdata(
2216 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2217 data_needed, 0, 0, &value);
2219 /* loop for all reg_keys */
2220 if (W_ERROR_IS_OK(result)) {
2224 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2227 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2231 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2236 /* STEP 3: COPY SUBKEY VALUES */
2238 /* here we need to enum all printer_keys and then work
2239 on the result with enum_printer_key_ex. nt4 does not
2240 respond to enumprinterkey, win2k does, so continue
2241 in case of an error */
2243 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2244 printf("got no key-data\n");
2249 /* work on a list of printer keys
2250 each key has to be enumerated to get all required
2251 information. information is then set via setprinterdataex-calls */
2253 if (keylist == NULL)
2257 while (*curkey != 0) {
2260 rpcstr_pull(subkey, curkey, sizeof(subkey), -1, STR_TERMINATE);
2262 curkey += strlen(subkey) + 1;
2264 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2265 return NT_STATUS_NO_MEMORY;
2267 /* enumerate all src subkeys */
2268 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2273 for (j=0; j < reg_ctr->num_values; j++) {
2275 REGISTRY_VALUE value;
2278 /* although samba replies with sane data in most cases we
2279 should try to avoid writing wrong registry data */
2281 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2282 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2283 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2284 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2285 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2287 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2289 /* although windows uses a multi-sz, we use a sz */
2290 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2291 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2294 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2296 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2297 nt_status = NT_STATUS_NO_MEMORY;
2300 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2301 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2304 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2309 /* FIXME: should we really do that ??? */
2310 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2311 nt_status = NT_STATUS_NO_MEMORY;
2314 init_unistr2(&data, url, UNI_STR_TERMINATE);
2315 fstrcpy(value.valuename, SPOOL_REG_URL);
2319 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2321 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2322 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2325 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2327 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2328 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2331 value.type = REG_SZ;
2332 value.size = data.uni_str_len * 2;
2334 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2336 value.data_p = NULL;
2340 display_reg_value(subkey, value);
2342 /* here we have to set all subkeys on the dst server */
2343 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2350 display_reg_value(subkey, *(reg_ctr->values[j]));
2352 /* here we have to set all subkeys on the dst server */
2353 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2354 subkey, reg_ctr->values[j]))
2359 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2360 subkey, reg_ctr->values[j]->valuename));
2364 TALLOC_FREE( reg_ctr );
2369 /* close printer handles here */
2371 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2372 got_hnd_src = False;
2376 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2377 got_hnd_dst = False;
2382 nt_status = NT_STATUS_OK;
2385 SAFE_FREE(devicename);
2387 SAFE_FREE(unc_name);
2390 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2393 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2396 cli_shutdown(cli_dst);