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;
339 if (!src_name || !dst_name)
342 if (cli_share_src == NULL || cli_share_dst == NULL)
346 /* open on the originating server */
347 DEBUGADD(3,("opening %s %s on originating server\n",
348 is_file ? "file":"dir", src_name));
350 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
352 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
354 if (fnum_src == -1) {
355 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
356 is_file ? "file":"dir",
357 src_name, cli_errstr(cli_share_src)));
358 nt_status = cli_nt_error(cli_share_src);
365 /* open file on the destination server */
366 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
367 fnum_dst = cli_open(cli_share_dst, dst_name,
368 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
370 if (fnum_dst == -1) {
371 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
372 dst_name, cli_errstr(cli_share_dst)));
373 nt_status = cli_nt_error(cli_share_dst);
377 /* allocate memory */
378 if (!(data = (char *)SMB_MALLOC(read_size))) {
379 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
380 nt_status = NT_STATUS_NO_MEMORY;
389 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
390 "%s ACLs and %s DOS Attributes %s\n",
391 cli_share_src->desthost, cli_share_src->share, src_name,
392 cli_share_dst->desthost, cli_share_dst->share, dst_name,
393 copy_acls ? "with" : "without",
394 copy_attrs ? "with" : "without",
395 copy_timestamps ? "(preserving timestamps)" : "" );
403 n = cli_read(cli_share_src, fnum_src, data, nread,
409 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
413 d_fprintf(stderr, "Error writing file: %s\n",
414 cli_errstr(cli_share_dst));
415 nt_status = cli_nt_error(cli_share_dst);
423 if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
426 DEBUGADD(3,("creating dir %s on the destination server\n",
429 if (!cli_mkdir(cli_share_dst, dst_name)) {
430 DEBUG(0,("cannot create directory %s: %s\n",
431 dst_name, cli_errstr(cli_share_dst)));
432 nt_status = NT_STATUS_NO_SUCH_FILE;
435 if (!cli_chkpath(cli_share_dst, dst_name)) {
436 d_fprintf(stderr, "cannot check for directory %s: %s\n",
437 dst_name, cli_errstr(cli_share_dst));
444 if (!cli_close(cli_share_src, fnum_src)) {
445 d_fprintf(stderr, "could not close file on originating server: %s\n",
446 cli_errstr(cli_share_src));
447 nt_status = cli_nt_error(cli_share_src);
451 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
452 d_fprintf(stderr, "could not close file on destination server: %s\n",
453 cli_errstr(cli_share_dst));
454 nt_status = cli_nt_error(cli_share_dst);
458 /* possibly we have to copy some file-attributes / acls / sd */
459 nt_status = net_copy_fileattr(mem_ctx, cli_share_src, cli_share_dst,
460 src_name, dst_name, copy_acls,
461 copy_attrs, copy_timestamps, is_file);
462 if (!NT_STATUS_IS_OK(nt_status))
466 nt_status = NT_STATUS_OK;
472 cli_close(cli_share_src, fnum_src);
475 cli_close(cli_share_dst, fnum_dst);
483 * Copy a driverfile from on connected share to another connected share
484 * This silently assumes that a driver-file is picked up from
486 * \\src_server\print$\{arch}\{version}\file
490 * \\dst_server\print$\{arch}\file
492 * to be added via setdriver-calls later.
493 * @param mem_ctx A talloc-context
494 * @param cli_share_src A cli_state connected to source print$-share
495 * @param cli_share_dst A cli_state connected to destination print$-share
496 * @param file The file-name to be copied
497 * @param short_archi The name of the driver-architecture (short form)
499 * @return Normal NTSTATUS return.
502 static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
503 struct cli_state *cli_share_src,
504 struct cli_state *cli_share_dst,
505 char *file, const char *short_archi) {
507 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
515 /* scroll through the file until we have the part
516 beyond archi_table.short_archi */
518 while (next_token(&p, tok, "\\", sizeof(tok))) {
519 if (strequal(tok, short_archi)) {
520 next_token(&p, version, "\\", sizeof(version));
521 next_token(&p, filename, "\\", sizeof(filename));
525 /* build source file name */
526 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
527 return NT_STATUS_NO_MEMORY;
530 /* create destination file name */
531 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
532 return NT_STATUS_NO_MEMORY;
535 /* finally copy the file */
536 nt_status = net_copy_file(mem_ctx, cli_share_src, cli_share_dst,
537 src_name, dst_name, False, False, False, True);
538 if (!NT_STATUS_IS_OK(nt_status))
541 nt_status = NT_STATUS_OK;
551 * Check for existing Architecture directory on a given server
553 * @param cli_share A cli_state connected to a print$-share
554 * @param short_archi The Architecture for the print-driver
556 * @return Normal NTSTATUS return.
559 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
562 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
565 if (asprintf(&dir, "\\%s", short_archi) < 0) {
566 return NT_STATUS_NO_MEMORY;
569 DEBUG(10,("creating print-driver dir for architecture: %s\n",
572 if (!cli_mkdir(cli_share, dir)) {
573 DEBUG(1,("cannot create directory %s: %s\n",
574 dir, cli_errstr(cli_share)));
575 nt_status = NT_STATUS_NO_SUCH_FILE;
578 if (!cli_chkpath(cli_share, dir)) {
579 d_fprintf(stderr, "cannot check %s: %s\n",
580 dir, cli_errstr(cli_share));
584 nt_status = NT_STATUS_OK;
592 * Copy a print-driver (level 3) from one connected print$-share to another
593 * connected print$-share
595 * @param mem_ctx A talloc-context
596 * @param cli_share_src A cli_state connected to a print$-share
597 * @param cli_share_dst A cli_state connected to a print$-share
598 * @param short_archi The Architecture for the print-driver
599 * @param i1 The DRIVER_INFO_3-struct
601 * @return Normal NTSTATUS return.
604 static NTSTATUS copy_print_driver_3(TALLOC_CTX *mem_ctx,
605 struct cli_state *cli_share_src,
606 struct cli_state *cli_share_dst,
607 const char *short_archi, DRIVER_INFO_3 *i1)
609 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
614 fstring driverpath = "";
615 fstring datafile = "";
616 fstring configfile = "";
617 fstring helpfile = "";
618 fstring dependentfiles = "";
623 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
624 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
625 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
626 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
627 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
631 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
632 name, short_archi, i1->version);
634 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
635 driverpath, short_archi);
636 if (!NT_STATUS_IS_OK(nt_status))
639 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
640 datafile, short_archi);
641 if (!NT_STATUS_IS_OK(nt_status))
644 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
645 configfile, short_archi);
646 if (!NT_STATUS_IS_OK(nt_status))
649 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
650 helpfile, short_archi);
651 if (!NT_STATUS_IS_OK(nt_status))
656 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
657 length += strlen(dependentfiles)+1;
659 if (strlen(dependentfiles) > 0) {
661 nt_status = net_copy_driverfile(mem_ctx,
662 cli_share_src, cli_share_dst,
663 dependentfiles, short_archi);
664 if (!NT_STATUS_IS_OK(nt_status))
675 * net_spoolss-functions
676 * =====================
678 * the net_spoolss-functions aim to simplify spoolss-client-functions
679 * required during the migration-process wrt buffer-sizes, returned
682 * this greatly reduces the complexitiy of the migrate-functions.
686 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
691 uint32 *num_printers,
692 PRINTER_INFO_CTR *ctr)
697 result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
698 level, num_printers, ctr);
700 if (!W_ERROR_IS_OK(result)) {
701 printf("cannot enum printers: %s\n", dos_errstr(result));
708 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
710 const char *printername,
711 uint32 access_required,
712 const char *username,
716 fstring servername, printername2;
718 slprintf(servername, sizeof(servername)-1, "\\\\%s", pipe_hnd->cli->desthost);
720 fstrcpy(printername2, servername);
721 fstrcat(printername2, "\\");
722 fstrcat(printername2, printername);
724 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
725 servername, username, printername2, access_required));
728 result = rpccli_spoolss_open_printer_ex(pipe_hnd, mem_ctx, printername2,
730 servername, username, hnd);
732 /* be more verbose */
733 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
734 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
735 printername2, servername, username);
739 if (!W_ERROR_IS_OK(result)) {
740 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
741 printername2, servername, dos_errstr(result));
745 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
746 printername2, servername));
751 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
755 PRINTER_INFO_CTR *ctr)
759 /* getprinter call */
760 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx, hnd, level, ctr);
762 if (!W_ERROR_IS_OK(result)) {
763 printf("cannot get printer-info: %s\n", dos_errstr(result));
770 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
774 PRINTER_INFO_CTR *ctr)
778 /* setprinter call */
779 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, hnd, level, ctr, 0);
781 if (!W_ERROR_IS_OK(result)) {
782 printf("cannot set printer-info: %s\n", dos_errstr(result));
790 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
793 REGISTRY_VALUE *value)
797 /* setprinterdata call */
798 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
800 if (!W_ERROR_IS_OK(result)) {
801 printf ("unable to set printerdata: %s\n", dos_errstr(result));
809 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
817 /* enumprinterkey call */
818 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
820 if (!W_ERROR_IS_OK(result)) {
821 printf("enumprinterkey failed: %s\n", dos_errstr(result));
828 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
837 /* enumprinterdataex call */
838 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
840 if (!W_ERROR_IS_OK(result)) {
841 printf("enumprinterdataex failed: %s\n", dos_errstr(result));
849 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
853 REGISTRY_VALUE *value)
857 /* setprinterdataex call */
858 result = rpccli_spoolss_setprinterdataex(pipe_hnd, mem_ctx, hnd,
861 if (!W_ERROR_IS_OK(result)) {
862 printf("could not set printerdataex: %s\n", dos_errstr(result));
869 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
880 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
882 if (!W_ERROR_IS_OK(result)) {
883 printf("could not enum forms: %s\n", dos_errstr(result));
890 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
892 uint32 level, const char *env,
894 PRINTER_DRIVER_CTR *ctr)
898 /* enumprinterdrivers call */
899 result = rpccli_spoolss_enumprinterdrivers(
900 pipe_hnd, mem_ctx, level,
901 env, num_drivers, ctr);
903 if (!W_ERROR_IS_OK(result)) {
904 printf("cannot enum drivers: %s\n", dos_errstr(result));
911 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
913 POLICY_HND *hnd, uint32 level,
914 const char *env, int version,
915 PRINTER_DRIVER_CTR *ctr)
919 /* getprinterdriver call */
920 result = rpccli_spoolss_getprinterdriver(
921 pipe_hnd, mem_ctx, hnd, level,
924 if (!W_ERROR_IS_OK(result)) {
925 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
926 env, dos_errstr(result)));
927 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
928 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
929 printf("cannot get driver: %s\n", dos_errstr(result));
938 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
939 TALLOC_CTX *mem_ctx, uint32 level,
940 PRINTER_DRIVER_CTR *ctr)
944 /* addprinterdriver call */
945 result = rpccli_spoolss_addprinterdriver(pipe_hnd, mem_ctx, level, ctr);
947 /* be more verbose */
948 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
949 printf("You are not allowed to add drivers\n");
952 if (!W_ERROR_IS_OK(result)) {
953 printf("cannot add driver: %s\n", dos_errstr(result));
961 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
962 * for a single printer or for all printers depending on argc/argv
965 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
970 uint32 *num_printers,
971 PRINTER_INFO_CTR *ctr)
976 /* no arguments given, enumerate all printers */
979 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
980 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
981 level, num_printers, ctr))
988 /* argument given, get a single printer by name */
989 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
990 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd))
993 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, ctr)) {
994 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
998 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1003 DEBUG(3,("got %d printers\n", *num_printers));
1010 * List print-queues (including local printers that are not shared)
1012 * All parameters are provided by the run_rpc_command function, except for
1013 * argc, argv which are passed through.
1015 * @param domain_sid The domain sid aquired from the remote server
1016 * @param cli A cli_state connected to the server.
1017 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1018 * @param argc Standard main() style argc
1019 * @param argv Standard main() style argv. Initial components are already
1022 * @return Normal NTSTATUS return.
1025 NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid,
1026 const char *domain_name,
1027 struct cli_state *cli,
1028 struct rpc_pipe_client *pipe_hnd,
1029 TALLOC_CTX *mem_ctx,
1033 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1034 uint32 i, num_printers;
1036 pstring printername, sharename;
1037 PRINTER_INFO_CTR ctr;
1039 printf("listing printers\n");
1041 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1044 for (i = 0; i < num_printers; i++) {
1046 /* do some initialization */
1047 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1048 sizeof(printername), -1, STR_TERMINATE);
1049 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1050 sizeof(sharename), -1, STR_TERMINATE);
1052 d_printf("printer %d: %s, shared as: %s\n",
1053 i+1, printername, sharename);
1056 return NT_STATUS_OK;
1060 * List printer-drivers from a server
1062 * All parameters are provided by the run_rpc_command function, except for
1063 * argc, argv which are passed through.
1065 * @param domain_sid The domain sid aquired from the remote server
1066 * @param cli A cli_state connected to the server.
1067 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1068 * @param argc Standard main() style argc
1069 * @param argv Standard main() style argv. Initial components are already
1072 * @return Normal NTSTATUS return.
1075 NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid,
1076 const char *domain_name,
1077 struct cli_state *cli,
1078 struct rpc_pipe_client *pipe_hnd,
1079 TALLOC_CTX *mem_ctx,
1083 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1086 PRINTER_DRIVER_CTR drv_ctr_enum;
1089 ZERO_STRUCT(drv_ctr_enum);
1091 printf("listing printer-drivers\n");
1093 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1097 /* enum remote drivers */
1098 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1099 archi_table[i].long_archi,
1100 &num_drivers, &drv_ctr_enum)) {
1102 nt_status = NT_STATUS_UNSUCCESSFUL;
1106 if (num_drivers == 0) {
1107 d_printf ("no drivers found on server for architecture: [%s].\n",
1108 archi_table[i].long_archi);
1112 d_printf("got %d printer-drivers for architecture: [%s]\n",
1113 num_drivers, archi_table[i].long_archi);
1116 /* do something for all drivers for architecture */
1117 for (d = 0; d < num_drivers; d++) {
1118 display_print_driver_3(&(drv_ctr_enum.info3[d]));
1122 nt_status = NT_STATUS_OK;
1130 * Publish print-queues with args-wrapper
1132 * @param cli A cli_state connected to the server.
1133 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1134 * @param argc Standard main() style argc
1135 * @param argv Standard main() style argv. Initial components are already
1139 * @return Normal NTSTATUS return.
1142 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1143 TALLOC_CTX *mem_ctx,
1148 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1149 uint32 i, num_printers;
1151 pstring printername, sharename;
1152 PRINTER_INFO_CTR ctr, ctr_pub;
1154 bool got_hnd = False;
1156 const char *action_str;
1158 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1161 for (i = 0; i < num_printers; i++) {
1163 /* do some initialization */
1164 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1165 sizeof(printername), -1, STR_TERMINATE);
1166 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1167 sizeof(sharename), -1, STR_TERMINATE);
1169 /* open printer handle */
1170 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1171 PRINTER_ALL_ACCESS, pipe_hnd->cli->user_name, &hnd))
1176 /* check for existing dst printer */
1177 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1180 /* check action and set string */
1182 case SPOOL_DS_PUBLISH:
1183 action_str = "published";
1185 case SPOOL_DS_UPDATE:
1186 action_str = "updated";
1188 case SPOOL_DS_UNPUBLISH:
1189 action_str = "unpublished";
1192 action_str = "unknown action";
1193 printf("unkown action: %d\n", action);
1197 ctr_pub.printers_7->action = action;
1199 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub, 0);
1200 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1201 printf("cannot set printer-info: %s\n", dos_errstr(result));
1205 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1208 nt_status = NT_STATUS_OK;
1212 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1217 NTSTATUS rpc_printer_publish_publish_internals(const DOM_SID *domain_sid,
1218 const char *domain_name,
1219 struct cli_state *cli,
1220 struct rpc_pipe_client *pipe_hnd,
1221 TALLOC_CTX *mem_ctx,
1225 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_PUBLISH);
1228 NTSTATUS rpc_printer_publish_unpublish_internals(const DOM_SID *domain_sid,
1229 const char *domain_name,
1230 struct cli_state *cli,
1231 struct rpc_pipe_client *pipe_hnd,
1232 TALLOC_CTX *mem_ctx,
1236 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
1239 NTSTATUS rpc_printer_publish_update_internals(const DOM_SID *domain_sid,
1240 const char *domain_name,
1241 struct cli_state *cli,
1242 struct rpc_pipe_client *pipe_hnd,
1243 TALLOC_CTX *mem_ctx,
1247 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
1251 * List print-queues w.r.t. their publishing state
1253 * All parameters are provided by the run_rpc_command function, except for
1254 * argc, argv which are passed through.
1256 * @param domain_sid The domain sid aquired from the remote server
1257 * @param cli A cli_state connected to the server.
1258 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1259 * @param argc Standard main() style argc
1260 * @param argv Standard main() style argv. Initial components are already
1263 * @return Normal NTSTATUS return.
1266 NTSTATUS rpc_printer_publish_list_internals(const DOM_SID *domain_sid,
1267 const char *domain_name,
1268 struct cli_state *cli,
1269 struct rpc_pipe_client *pipe_hnd,
1270 TALLOC_CTX *mem_ctx,
1274 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1275 uint32 i, num_printers;
1277 pstring printername, sharename;
1279 PRINTER_INFO_CTR ctr, ctr_pub;
1281 bool got_hnd = False;
1284 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1287 for (i = 0; i < num_printers; i++) {
1289 ZERO_STRUCT(ctr_pub);
1291 /* do some initialization */
1292 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1293 sizeof(printername), -1, STR_TERMINATE);
1294 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1295 sizeof(sharename), -1, STR_TERMINATE);
1297 /* open printer handle */
1298 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1299 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1304 /* check for existing dst printer */
1305 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1308 rpcstr_pull(guid, ctr_pub.printers_7->guid.buffer, sizeof(guid), -1, STR_TERMINATE);
1310 state = ctr_pub.printers_7->action;
1312 case SPOOL_DS_PUBLISH:
1313 printf("printer [%s] is published", sharename);
1315 printf(", guid: %s", guid);
1318 case SPOOL_DS_UNPUBLISH:
1319 printf("printer [%s] is unpublished\n", sharename);
1321 case SPOOL_DS_UPDATE:
1322 printf("printer [%s] is currently updating\n", sharename);
1325 printf("unkown state: %d\n", state);
1330 nt_status = NT_STATUS_OK;
1334 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1340 * Migrate Printer-ACLs from a source server to the destination server
1342 * All parameters are provided by the run_rpc_command function, except for
1343 * argc, argv which are passed through.
1345 * @param domain_sid The domain sid aquired from the remote server
1346 * @param cli A cli_state connected to the server.
1347 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1348 * @param argc Standard main() style argc
1349 * @param argv Standard main() style argv. Initial components are already
1352 * @return Normal NTSTATUS return.
1355 NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid,
1356 const char *domain_name,
1357 struct cli_state *cli,
1358 struct rpc_pipe_client *pipe_hnd,
1359 TALLOC_CTX *mem_ctx,
1363 /* TODO: what now, info2 or info3 ?
1364 convince jerry that we should add clientside setacls level 3 at least
1366 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1368 uint32 num_printers;
1370 pstring printername = "", sharename = "";
1371 bool got_hnd_src = False;
1372 bool got_hnd_dst = False;
1373 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1374 POLICY_HND hnd_src, hnd_dst;
1375 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1376 struct cli_state *cli_dst = NULL;
1378 ZERO_STRUCT(ctr_src);
1380 DEBUG(3,("copying printer ACLs\n"));
1382 /* connect destination PI_SPOOLSS */
1383 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1384 if (!NT_STATUS_IS_OK(nt_status))
1388 /* enum source printers */
1389 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1390 nt_status = NT_STATUS_UNSUCCESSFUL;
1394 if (!num_printers) {
1395 printf ("no printers found on server.\n");
1396 nt_status = NT_STATUS_OK;
1400 /* do something for all printers */
1401 for (i = 0; i < num_printers; i++) {
1403 /* do some initialization */
1404 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1405 sizeof(printername), -1, STR_TERMINATE);
1406 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1407 sizeof(sharename), -1, STR_TERMINATE);
1408 /* we can reset NT_STATUS here because we do not
1409 get any real NT_STATUS-codes anymore from now on */
1410 nt_status = NT_STATUS_UNSUCCESSFUL;
1412 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1413 printername, sharename);
1415 /* according to msdn you have specify these access-rights
1416 to see the security descriptor
1417 - READ_CONTROL (DACL)
1418 - ACCESS_SYSTEM_SECURITY (SACL)
1421 /* open src printer handle */
1422 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1423 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1428 /* open dst printer handle */
1429 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1430 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1435 /* check for existing dst printer */
1436 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1439 /* check for existing src printer */
1440 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &ctr_src))
1443 /* Copy Security Descriptor */
1445 /* copy secdesc (info level 2) */
1446 ctr_dst.printers_2->devmode = NULL;
1447 ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
1450 display_sec_desc(ctr_dst.printers_2->secdesc);
1452 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &ctr_dst))
1455 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1458 /* close printer handles here */
1460 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1461 got_hnd_src = False;
1465 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1466 got_hnd_dst = False;
1471 nt_status = NT_STATUS_OK;
1476 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1480 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1484 cli_shutdown(cli_dst);
1490 * Migrate printer-forms from a src server to the dst server
1492 * All parameters are provided by the run_rpc_command function, except for
1493 * argc, argv which are passed through.
1495 * @param domain_sid The domain sid aquired from the remote server
1496 * @param cli A cli_state connected to the server.
1497 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1498 * @param argc Standard main() style argc
1499 * @param argv Standard main() style argv. Initial components are already
1502 * @return Normal NTSTATUS return.
1505 NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid,
1506 const char *domain_name,
1507 struct cli_state *cli,
1508 struct rpc_pipe_client *pipe_hnd,
1509 TALLOC_CTX *mem_ctx,
1513 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1516 uint32 num_printers;
1518 pstring printername = "", sharename = "";
1519 bool got_hnd_src = False;
1520 bool got_hnd_dst = False;
1521 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1522 POLICY_HND hnd_src, hnd_dst;
1523 PRINTER_INFO_CTR ctr_enum, ctr_dst;
1526 struct cli_state *cli_dst = NULL;
1528 ZERO_STRUCT(ctr_enum);
1530 DEBUG(3,("copying forms\n"));
1532 /* connect destination PI_SPOOLSS */
1533 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1534 if (!NT_STATUS_IS_OK(nt_status))
1538 /* enum src printers */
1539 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1540 nt_status = NT_STATUS_UNSUCCESSFUL;
1544 if (!num_printers) {
1545 printf ("no printers found on server.\n");
1546 nt_status = NT_STATUS_OK;
1551 /* do something for all printers */
1552 for (i = 0; i < num_printers; i++) {
1554 /* do some initialization */
1555 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1556 sizeof(printername), -1, STR_TERMINATE);
1557 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1558 sizeof(sharename), -1, STR_TERMINATE);
1559 /* we can reset NT_STATUS here because we do not
1560 get any real NT_STATUS-codes anymore from now on */
1561 nt_status = NT_STATUS_UNSUCCESSFUL;
1563 d_printf("migrating printer forms for: [%s] / [%s]\n",
1564 printername, sharename);
1567 /* open src printer handle */
1568 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1569 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1575 /* open dst printer handle */
1576 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1577 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1583 /* check for existing dst printer */
1584 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1587 /* finally migrate forms */
1588 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1591 DEBUG(1,("got %d forms for printer\n", num_forms));
1594 for (f = 0; f < num_forms; f++) {
1599 /* only migrate FORM_PRINTER types, according to jerry
1600 FORM_BUILTIN-types are hard-coded in samba */
1601 if (forms[f].flag != FORM_PRINTER)
1604 if (forms[f].name.buffer)
1605 rpcstr_pull(form_name, forms[f].name.buffer,
1606 sizeof(form_name), -1, STR_TERMINATE);
1609 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1610 f, form_name, forms[f].flag);
1612 /* is there a more elegant way to do that ? */
1613 form.flags = FORM_PRINTER;
1614 form.size_x = forms[f].width;
1615 form.size_y = forms[f].length;
1616 form.left = forms[f].left;
1617 form.top = forms[f].top;
1618 form.right = forms[f].right;
1619 form.bottom = forms[f].bottom;
1621 init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
1623 /* FIXME: there might be something wrong with samba's
1625 result = rpccli_spoolss_addform(pipe_hnd_dst, mem_ctx,
1626 &hnd_dst, 1, &form);
1627 if (!W_ERROR_IS_OK(result)) {
1628 d_printf("\tAddForm form %d: [%s] refused.\n",
1633 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1637 /* close printer handles here */
1639 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1640 got_hnd_src = False;
1644 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1645 got_hnd_dst = False;
1649 nt_status = NT_STATUS_OK;
1654 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1657 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1660 cli_shutdown(cli_dst);
1666 * Migrate printer-drivers from a src server to the dst server
1668 * All parameters are provided by the run_rpc_command function, except for
1669 * argc, argv which are passed through.
1671 * @param domain_sid The domain sid aquired from the remote server
1672 * @param cli A cli_state connected to the server.
1673 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1674 * @param argc Standard main() style argc
1675 * @param argv Standard main() style argv. Initial components are already
1678 * @return Normal NTSTATUS return.
1681 NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid,
1682 const char *domain_name,
1683 struct cli_state *cli,
1684 struct rpc_pipe_client *pipe_hnd,
1685 TALLOC_CTX *mem_ctx,
1689 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1691 uint32 num_printers;
1693 pstring printername = "", sharename = "";
1694 bool got_hnd_src = False;
1695 bool got_hnd_dst = False;
1696 bool got_src_driver_share = False;
1697 bool got_dst_driver_share = False;
1698 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1699 POLICY_HND hnd_src, hnd_dst;
1700 PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
1701 PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1702 struct cli_state *cli_dst = NULL;
1703 struct cli_state *cli_share_src = NULL;
1704 struct cli_state *cli_share_dst = NULL;
1705 fstring drivername = "";
1707 ZERO_STRUCT(drv_ctr_src);
1708 ZERO_STRUCT(drv_ctr_dst);
1709 ZERO_STRUCT(info_ctr_enum);
1710 ZERO_STRUCT(info_ctr_dst);
1713 DEBUG(3,("copying printer-drivers\n"));
1715 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1716 if (!NT_STATUS_IS_OK(nt_status))
1720 /* open print$-share on the src server */
1721 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
1722 cli->desthost, "print$", "A:");
1723 if (!NT_STATUS_IS_OK(nt_status))
1726 got_src_driver_share = True;
1729 /* open print$-share on the dst server */
1730 nt_status = connect_to_service(&cli_share_dst, &cli_dst->dest_ip,
1731 cli_dst->desthost, "print$", "A:");
1732 if (!NT_STATUS_IS_OK(nt_status))
1735 got_dst_driver_share = True;
1738 /* enum src printers */
1739 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1740 nt_status = NT_STATUS_UNSUCCESSFUL;
1744 if (num_printers == 0) {
1745 printf ("no printers found on server.\n");
1746 nt_status = NT_STATUS_OK;
1751 /* do something for all printers */
1752 for (p = 0; p < num_printers; p++) {
1754 /* do some initialization */
1755 rpcstr_pull(printername, info_ctr_enum.printers_2[p].printername.buffer,
1756 sizeof(printername), -1, STR_TERMINATE);
1757 rpcstr_pull(sharename, info_ctr_enum.printers_2[p].sharename.buffer,
1758 sizeof(sharename), -1, STR_TERMINATE);
1759 /* we can reset NT_STATUS here because we do not
1760 get any real NT_STATUS-codes anymore from now on */
1761 nt_status = NT_STATUS_UNSUCCESSFUL;
1763 d_printf("migrating printer driver for: [%s] / [%s]\n",
1764 printername, sharename);
1766 /* open dst printer handle */
1767 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1768 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1773 /* check for existing dst printer */
1774 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst))
1778 /* open src printer handle */
1779 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1780 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd_src))
1786 /* in a first step call getdriver for each shared printer (per arch)
1787 to get a list of all files that have to be copied */
1789 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1792 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1793 level, archi_table[i].long_archi,
1794 archi_table[i].version, &drv_ctr_src))
1797 rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer,
1798 sizeof(drivername), -1, STR_TERMINATE);
1801 display_print_driver_3(drv_ctr_src.info3);
1804 /* check arch dir */
1805 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1806 if (!NT_STATUS_IS_OK(nt_status))
1810 /* copy driver-files */
1811 nt_status = copy_print_driver_3(mem_ctx, cli_share_src, cli_share_dst,
1812 archi_table[i].short_archi,
1814 if (!NT_STATUS_IS_OK(nt_status))
1819 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_ctr_src)) {
1820 nt_status = NT_STATUS_UNSUCCESSFUL;
1824 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1825 drivername, printername));
1829 if (strlen(drivername) == 0) {
1830 DEBUGADD(1,("Did not get driver for printer %s\n",
1836 init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
1838 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) {
1839 nt_status = NT_STATUS_UNSUCCESSFUL;
1843 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1844 drivername, printername));
1848 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1849 got_hnd_dst = False;
1854 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1855 got_hnd_src = False;
1859 nt_status = NT_STATUS_OK;
1864 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1867 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1870 cli_shutdown(cli_dst);
1873 if (got_src_driver_share)
1874 cli_shutdown(cli_share_src);
1876 if (got_dst_driver_share)
1877 cli_shutdown(cli_share_dst);
1884 * Migrate printer-queues from a src to the dst server
1885 * (requires a working "addprinter command" to be installed for the local smbd)
1887 * All parameters are provided by the run_rpc_command function, except for
1888 * argc, argv which are passed through.
1890 * @param domain_sid The domain sid aquired from the remote server
1891 * @param cli A cli_state connected to the server.
1892 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1893 * @param argc Standard main() style argc
1894 * @param argv Standard main() style argv. Initial components are already
1897 * @return Normal NTSTATUS return.
1900 NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid,
1901 const char *domain_name,
1902 struct cli_state *cli,
1903 struct rpc_pipe_client *pipe_hnd,
1904 TALLOC_CTX *mem_ctx,
1909 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1910 uint32 i = 0, num_printers;
1912 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1913 struct cli_state *cli_dst = NULL;
1914 POLICY_HND hnd_dst, hnd_src;
1915 pstring printername, sharename;
1916 bool got_hnd_src = False;
1917 bool got_hnd_dst = False;
1918 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1920 DEBUG(3,("copying printers\n"));
1922 /* connect destination PI_SPOOLSS */
1923 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1924 if (!NT_STATUS_IS_OK(nt_status))
1928 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1929 nt_status = NT_STATUS_UNSUCCESSFUL;
1933 if (!num_printers) {
1934 printf ("no printers found on server.\n");
1935 nt_status = NT_STATUS_OK;
1940 /* do something for all printers */
1941 for (i = 0; i < num_printers; i++) {
1943 /* do some initialization */
1944 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1945 sizeof(printername), -1, STR_TERMINATE);
1946 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1947 sizeof(sharename), -1, STR_TERMINATE);
1948 /* we can reset NT_STATUS here because we do not
1949 get any real NT_STATUS-codes anymore from now on */
1950 nt_status = NT_STATUS_UNSUCCESSFUL;
1952 d_printf("migrating printer queue for: [%s] / [%s]\n",
1953 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));
1964 /* check for existing dst printer */
1965 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) {
1966 printf ("could not get printer, creating printer.\n");
1968 DEBUG(1,("printer already exists: %s\n", sharename));
1969 /* close printer handle here - dst only, not got src yet. */
1971 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1972 got_hnd_dst = False;
1977 /* now get again src printer ctr via getprinter,
1978 we first need a handle for that */
1980 /* open src printer handle */
1981 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1982 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1987 /* getprinter on the src server */
1988 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &ctr_src))
1991 /* copy each src printer to a dst printer 1:1,
1992 maybe some values have to be changed though */
1993 d_printf("creating printer: %s\n", printername);
1994 result = rpccli_spoolss_addprinterex (pipe_hnd_dst, mem_ctx, level, &ctr_src);
1996 if (W_ERROR_IS_OK(result))
1997 d_printf ("printer [%s] successfully added.\n", printername);
1998 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
1999 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2001 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2005 /* close printer handles here */
2007 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2008 got_hnd_src = False;
2012 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2013 got_hnd_dst = False;
2017 nt_status = NT_STATUS_OK;
2021 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2024 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2027 cli_shutdown(cli_dst);
2033 * Migrate Printer-Settings from a src server to the dst server
2034 * (for this to work, printers and drivers already have to be migrated earlier)
2036 * All parameters are provided by the run_rpc_command function, except for
2037 * argc, argv which are passed through.
2039 * @param domain_sid The domain sid aquired from the remote server
2040 * @param cli A cli_state connected to the server.
2041 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2042 * @param argc Standard main() style argc
2043 * @param argv Standard main() style argv. Initial components are already
2046 * @return Normal NTSTATUS return.
2049 NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid,
2050 const char *domain_name,
2051 struct cli_state *cli,
2052 struct rpc_pipe_client *pipe_hnd,
2053 TALLOC_CTX *mem_ctx,
2058 /* FIXME: Here the nightmare begins */
2061 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2062 uint32 i = 0, p = 0, j = 0;
2063 uint32 num_printers, val_needed, data_needed;
2065 pstring printername = "", sharename = "";
2066 bool got_hnd_src = False;
2067 bool got_hnd_dst = False;
2068 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2069 POLICY_HND hnd_src, hnd_dst;
2070 PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
2071 REGVAL_CTR *reg_ctr;
2072 struct cli_state *cli_dst = NULL;
2073 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2076 uint16 *keylist = NULL, *curkey;
2078 ZERO_STRUCT(ctr_enum);
2080 DEBUG(3,("copying printer settings\n"));
2082 /* connect destination PI_SPOOLSS */
2083 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
2084 if (!NT_STATUS_IS_OK(nt_status))
2088 /* enum src printers */
2089 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2090 nt_status = NT_STATUS_UNSUCCESSFUL;
2094 if (!num_printers) {
2095 printf ("no printers found on server.\n");
2096 nt_status = NT_STATUS_OK;
2101 /* needed for dns-strings in regkeys */
2102 get_mydnsfullname(longname);
2104 /* do something for all printers */
2105 for (i = 0; i < num_printers; i++) {
2107 /* do some initialization */
2108 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
2109 sizeof(printername), -1, STR_TERMINATE);
2110 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
2111 sizeof(sharename), -1, STR_TERMINATE);
2113 /* we can reset NT_STATUS here because we do not
2114 get any real NT_STATUS-codes anymore from now on */
2115 nt_status = NT_STATUS_UNSUCCESSFUL;
2117 d_printf("migrating printer settings for: [%s] / [%s]\n",
2118 printername, sharename);
2121 /* open src printer handle */
2122 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2123 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2129 /* open dst printer handle */
2130 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2131 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2137 /* check for existing dst printer */
2138 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2143 /* STEP 1: COPY DEVICE-MODE and other
2144 PRINTER_INFO_2-attributes
2147 ctr_dst.printers_2 = &ctr_enum.printers_2[i];
2149 /* why is the port always disconnected when the printer
2150 is correctly installed (incl. driver ???) */
2151 init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
2153 /* check if printer is published */
2154 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2156 /* check for existing dst printer */
2157 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
2160 ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
2162 /* ignore False from setprinter due to WERR_IO_PENDING */
2163 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
2165 DEBUG(3,("republished printer\n"));
2168 if (ctr_enum.printers_2[i].devmode != NULL) {
2170 /* copy devmode (info level 2) */
2171 ctr_dst.printers_2->devmode = (DEVICEMODE *)
2172 TALLOC_MEMDUP(mem_ctx,
2173 ctr_enum.printers_2[i].devmode,
2174 sizeof(DEVICEMODE));
2176 /* do not copy security descriptor (we have another
2177 * command for that) */
2178 ctr_dst.printers_2->secdesc = NULL;
2181 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2183 nt_status = NT_STATUS_NO_MEMORY;
2187 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2190 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2194 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2197 /* STEP 2: COPY REGISTRY VALUES */
2199 /* please keep in mind that samba parse_spools gives horribly
2200 crippled results when used to rpccli_spoolss_enumprinterdataex
2201 a win2k3-server. (Bugzilla #1851)
2202 FIXME: IIRC I've seen it too on a win2k-server
2205 /* enumerate data on src handle */
2206 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2207 &val_needed, &data_needed, NULL);
2209 /* loop for all printerdata of "PrinterDriverData" */
2210 while (W_ERROR_IS_OK(result)) {
2212 REGISTRY_VALUE value;
2214 result = rpccli_spoolss_enumprinterdata(
2215 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2216 data_needed, 0, 0, &value);
2218 /* loop for all reg_keys */
2219 if (W_ERROR_IS_OK(result)) {
2223 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2226 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2230 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2235 /* STEP 3: COPY SUBKEY VALUES */
2237 /* here we need to enum all printer_keys and then work
2238 on the result with enum_printer_key_ex. nt4 does not
2239 respond to enumprinterkey, win2k does, so continue
2240 in case of an error */
2242 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2243 printf("got no key-data\n");
2248 /* work on a list of printer keys
2249 each key has to be enumerated to get all required
2250 information. information is then set via setprinterdataex-calls */
2252 if (keylist == NULL)
2256 while (*curkey != 0) {
2259 rpcstr_pull(subkey, curkey, sizeof(subkey), -1, STR_TERMINATE);
2261 curkey += strlen(subkey) + 1;
2263 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2264 return NT_STATUS_NO_MEMORY;
2266 /* enumerate all src subkeys */
2267 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2272 for (j=0; j < reg_ctr->num_values; j++) {
2274 REGISTRY_VALUE value;
2277 /* although samba replies with sane data in most cases we
2278 should try to avoid writing wrong registry data */
2280 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2281 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2282 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2283 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2284 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2286 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2288 /* although windows uses a multi-sz, we use a sz */
2289 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2290 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2293 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2295 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2296 nt_status = NT_STATUS_NO_MEMORY;
2299 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2300 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2303 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2308 /* FIXME: should we really do that ??? */
2309 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2310 nt_status = NT_STATUS_NO_MEMORY;
2313 init_unistr2(&data, url, UNI_STR_TERMINATE);
2314 fstrcpy(value.valuename, SPOOL_REG_URL);
2318 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2320 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2321 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2324 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2326 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2327 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2330 value.type = REG_SZ;
2331 value.size = data.uni_str_len * 2;
2333 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2335 value.data_p = NULL;
2339 display_reg_value(subkey, value);
2341 /* here we have to set all subkeys on the dst server */
2342 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2349 display_reg_value(subkey, *(reg_ctr->values[j]));
2351 /* here we have to set all subkeys on the dst server */
2352 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2353 subkey, reg_ctr->values[j]))
2358 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2359 subkey, reg_ctr->values[j]->valuename));
2363 TALLOC_FREE( reg_ctr );
2368 /* close printer handles here */
2370 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2371 got_hnd_src = False;
2375 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2376 got_hnd_dst = False;
2381 nt_status = NT_STATUS_OK;
2384 SAFE_FREE(devicename);
2386 SAFE_FREE(unc_name);
2389 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2392 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2395 cli_shutdown(cli_dst);