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_talloc(talloc_tos(),
128 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
132 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
133 subkey, value.valuename);
137 uint32 i, num_values;
140 if (!W_ERROR_IS_OK(reg_pull_multi_sz(NULL, value.data_p,
141 value.size, &num_values,
143 d_printf("reg_pull_multi_sz failed\n");
147 for (i=0; i<num_values; i++) {
148 d_printf("%s\n", values[i]);
155 d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
161 * Copies ACLs, DOS-attributes and timestamps from one
162 * file or directory from one connected share to another connected share
164 * @param mem_ctx A talloc-context
165 * @param cli_share_src A connected cli_state
166 * @param cli_share_dst A connected cli_state
167 * @param src_file The source file-name
168 * @param dst_file The destination file-name
169 * @param copy_acls Whether to copy acls
170 * @param copy_attrs Whether to copy DOS attributes
171 * @param copy_timestamps Whether to preserve timestamps
172 * @param is_file Whether this file is a file or a dir
174 * @return Normal NTSTATUS return.
177 NTSTATUS net_copy_fileattr(TALLOC_CTX *mem_ctx,
178 struct cli_state *cli_share_src,
179 struct cli_state *cli_share_dst,
180 const char *src_name, const char *dst_name,
181 bool copy_acls, bool copy_attrs,
182 bool copy_timestamps, bool is_file)
184 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
189 time_t f_atime, f_ctime, f_mtime;
192 if (!copy_timestamps && !copy_acls && !copy_attrs)
195 /* open file/dir on the originating server */
197 DEBUGADD(3,("opening %s %s on originating server\n",
198 is_file?"file":"dir", src_name));
200 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
201 if (fnum_src == -1) {
202 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
203 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
204 nt_status = cli_nt_error(cli_share_src);
211 /* get the security descriptor */
212 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
214 DEBUG(0,("failed to get security descriptor: %s\n",
215 cli_errstr(cli_share_src)));
216 nt_status = cli_nt_error(cli_share_src);
220 if (opt_verbose && DEBUGLEVEL >= 3)
221 display_sec_desc(sd);
225 if (copy_attrs || copy_timestamps) {
227 /* get file attributes */
228 if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
229 &f_ctime, &f_atime, &f_mtime)) {
230 DEBUG(0,("failed to get file-attrs: %s\n",
231 cli_errstr(cli_share_src)));
232 nt_status = cli_nt_error(cli_share_src);
238 /* open the file/dir on the destination server */
240 fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
241 if (fnum_dst == -1) {
242 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
243 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
244 nt_status = cli_nt_error(cli_share_dst);
248 if (copy_timestamps) {
251 if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
252 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
253 cli_errstr(cli_share_dst)));
254 nt_status = cli_nt_error(cli_share_dst);
262 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
263 DEBUG(0,("could not set secdesc on %s: %s\n",
264 dst_name, cli_errstr(cli_share_dst)));
265 nt_status = cli_nt_error(cli_share_dst);
273 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
274 DEBUG(0,("failed to set file-attrs: %s\n",
275 cli_errstr(cli_share_dst)));
276 nt_status = cli_nt_error(cli_share_dst);
284 if (!cli_close(cli_share_src, fnum_src)) {
285 d_fprintf(stderr, "could not close %s on originating server: %s\n",
286 is_file?"file":"dir", cli_errstr(cli_share_src));
287 nt_status = cli_nt_error(cli_share_src);
291 if (!cli_close(cli_share_dst, fnum_dst)) {
292 d_fprintf(stderr, "could not close %s on destination server: %s\n",
293 is_file?"file":"dir", cli_errstr(cli_share_dst));
294 nt_status = cli_nt_error(cli_share_dst);
299 nt_status = NT_STATUS_OK;
305 cli_close(cli_share_src, fnum_src);
308 cli_close(cli_share_dst, fnum_dst);
314 * Copy a file or directory from a connected share to another connected share
316 * @param mem_ctx A talloc-context
317 * @param cli_share_src A connected cli_state
318 * @param cli_share_dst A connected cli_state
319 * @param src_file The source file-name
320 * @param dst_file The destination file-name
321 * @param copy_acls Whether to copy acls
322 * @param copy_attrs Whether to copy DOS attributes
323 * @param copy_timestamps Whether to preserve timestamps
324 * @param is_file Whether this file is a file or a dir
326 * @return Normal NTSTATUS return.
329 NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
330 struct cli_state *cli_share_src,
331 struct cli_state *cli_share_dst,
332 const char *src_name, const char *dst_name,
333 bool copy_acls, bool copy_attrs,
334 bool copy_timestamps, bool is_file)
336 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
339 static int io_bufsize = 64512;
340 int read_size = io_bufsize;
345 if (!src_name || !dst_name)
348 if (cli_share_src == NULL || cli_share_dst == NULL)
352 /* open on the originating server */
353 DEBUGADD(3,("opening %s %s on originating server\n",
354 is_file ? "file":"dir", src_name));
356 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
358 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
360 if (fnum_src == -1) {
361 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
362 is_file ? "file":"dir",
363 src_name, cli_errstr(cli_share_src)));
364 nt_status = cli_nt_error(cli_share_src);
371 /* open file on the destination server */
372 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
373 fnum_dst = cli_open(cli_share_dst, dst_name,
374 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
376 if (fnum_dst == -1) {
377 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
378 dst_name, cli_errstr(cli_share_dst)));
379 nt_status = cli_nt_error(cli_share_dst);
383 /* allocate memory */
384 if (!(data = (char *)SMB_MALLOC(read_size))) {
385 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
386 nt_status = NT_STATUS_NO_MEMORY;
395 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
396 "%s ACLs and %s DOS Attributes %s\n",
397 cli_share_src->desthost, cli_share_src->share, src_name,
398 cli_share_dst->desthost, cli_share_dst->share, dst_name,
399 copy_acls ? "with" : "without",
400 copy_attrs ? "with" : "without",
401 copy_timestamps ? "(preserving timestamps)" : "" );
409 n = cli_read(cli_share_src, fnum_src, data, nread,
415 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
419 d_fprintf(stderr, "Error writing file: %s\n",
420 cli_errstr(cli_share_dst));
421 nt_status = cli_nt_error(cli_share_dst);
429 if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
432 DEBUGADD(3,("creating dir %s on the destination server\n",
435 if (!cli_mkdir(cli_share_dst, dst_name)) {
436 DEBUG(0,("cannot create directory %s: %s\n",
437 dst_name, cli_errstr(cli_share_dst)));
438 nt_status = NT_STATUS_NO_SUCH_FILE;
441 if (!cli_chkpath(cli_share_dst, dst_name)) {
442 d_fprintf(stderr, "cannot check for directory %s: %s\n",
443 dst_name, cli_errstr(cli_share_dst));
450 if (!cli_close(cli_share_src, fnum_src)) {
451 d_fprintf(stderr, "could not close file on originating server: %s\n",
452 cli_errstr(cli_share_src));
453 nt_status = cli_nt_error(cli_share_src);
457 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
458 d_fprintf(stderr, "could not close file on destination server: %s\n",
459 cli_errstr(cli_share_dst));
460 nt_status = cli_nt_error(cli_share_dst);
464 /* possibly we have to copy some file-attributes / acls / sd */
465 nt_status = net_copy_fileattr(mem_ctx, cli_share_src, cli_share_dst,
466 src_name, dst_name, copy_acls,
467 copy_attrs, copy_timestamps, is_file);
468 if (!NT_STATUS_IS_OK(nt_status))
472 nt_status = NT_STATUS_OK;
478 cli_close(cli_share_src, fnum_src);
481 cli_close(cli_share_dst, fnum_dst);
489 * Copy a driverfile from on connected share to another connected share
490 * This silently assumes that a driver-file is picked up from
492 * \\src_server\print$\{arch}\{version}\file
496 * \\dst_server\print$\{arch}\file
498 * to be added via setdriver-calls later.
499 * @param mem_ctx A talloc-context
500 * @param cli_share_src A cli_state connected to source print$-share
501 * @param cli_share_dst A cli_state connected to destination print$-share
502 * @param file The file-name to be copied
503 * @param short_archi The name of the driver-architecture (short form)
505 * @return Normal NTSTATUS return.
508 static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
509 struct cli_state *cli_share_src,
510 struct cli_state *cli_share_dst,
511 char *file, const char *short_archi) {
513 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
521 /* scroll through the file until we have the part
522 beyond archi_table.short_archi */
524 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
525 if (strequal(tok, short_archi)) {
526 next_token_talloc(mem_ctx, &p, &version, "\\");
527 next_token_talloc(mem_ctx, &p, &filename, "\\");
531 /* build source file name */
532 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
533 return NT_STATUS_NO_MEMORY;
536 /* create destination file name */
537 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
538 return NT_STATUS_NO_MEMORY;
541 /* finally copy the file */
542 nt_status = net_copy_file(mem_ctx, cli_share_src, cli_share_dst,
543 src_name, dst_name, False, False, False, True);
544 if (!NT_STATUS_IS_OK(nt_status))
547 nt_status = NT_STATUS_OK;
557 * Check for existing Architecture directory on a given server
559 * @param cli_share A cli_state connected to a print$-share
560 * @param short_archi The Architecture for the print-driver
562 * @return Normal NTSTATUS return.
565 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
568 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
571 if (asprintf(&dir, "\\%s", short_archi) < 0) {
572 return NT_STATUS_NO_MEMORY;
575 DEBUG(10,("creating print-driver dir for architecture: %s\n",
578 if (!cli_mkdir(cli_share, dir)) {
579 DEBUG(1,("cannot create directory %s: %s\n",
580 dir, cli_errstr(cli_share)));
581 nt_status = NT_STATUS_NO_SUCH_FILE;
584 if (!cli_chkpath(cli_share, dir)) {
585 d_fprintf(stderr, "cannot check %s: %s\n",
586 dir, cli_errstr(cli_share));
590 nt_status = NT_STATUS_OK;
598 * Copy a print-driver (level 3) from one connected print$-share to another
599 * connected print$-share
601 * @param mem_ctx A talloc-context
602 * @param cli_share_src A cli_state connected to a print$-share
603 * @param cli_share_dst A cli_state connected to a print$-share
604 * @param short_archi The Architecture for the print-driver
605 * @param i1 The DRIVER_INFO_3-struct
607 * @return Normal NTSTATUS return.
610 static NTSTATUS copy_print_driver_3(TALLOC_CTX *mem_ctx,
611 struct cli_state *cli_share_src,
612 struct cli_state *cli_share_dst,
613 const char *short_archi, DRIVER_INFO_3 *i1)
615 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
620 fstring driverpath = "";
621 fstring datafile = "";
622 fstring configfile = "";
623 fstring helpfile = "";
624 fstring dependentfiles = "";
629 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
630 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
631 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
632 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
633 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
637 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
638 name, short_archi, i1->version);
640 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
641 driverpath, 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 datafile, 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 configfile, short_archi);
652 if (!NT_STATUS_IS_OK(nt_status))
655 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
656 helpfile, short_archi);
657 if (!NT_STATUS_IS_OK(nt_status))
662 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
663 length += strlen(dependentfiles)+1;
665 if (strlen(dependentfiles) > 0) {
667 nt_status = net_copy_driverfile(mem_ctx,
668 cli_share_src, cli_share_dst,
669 dependentfiles, short_archi);
670 if (!NT_STATUS_IS_OK(nt_status))
681 * net_spoolss-functions
682 * =====================
684 * the net_spoolss-functions aim to simplify spoolss-client-functions
685 * required during the migration-process wrt buffer-sizes, returned
688 * this greatly reduces the complexitiy of the migrate-functions.
692 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
697 uint32 *num_printers,
698 PRINTER_INFO_CTR *ctr)
703 result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
704 level, num_printers, ctr);
706 if (!W_ERROR_IS_OK(result)) {
707 printf("cannot enum printers: %s\n", dos_errstr(result));
714 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
716 const char *printername,
717 uint32 access_required,
718 const char *username,
722 fstring servername, printername2;
724 slprintf(servername, sizeof(servername)-1, "\\\\%s",
727 fstrcpy(printername2, servername);
728 fstrcat(printername2, "\\");
729 fstrcat(printername2, printername);
731 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
732 servername, username, printername2, access_required));
735 result = rpccli_spoolss_open_printer_ex(pipe_hnd, mem_ctx, printername2,
737 servername, username, hnd);
739 /* be more verbose */
740 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
741 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
742 printername2, servername, username);
746 if (!W_ERROR_IS_OK(result)) {
747 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
748 printername2, servername, dos_errstr(result));
752 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
753 printername2, servername));
758 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
762 PRINTER_INFO_CTR *ctr)
766 /* getprinter call */
767 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx, hnd, level, ctr);
769 if (!W_ERROR_IS_OK(result)) {
770 printf("cannot get printer-info: %s\n", dos_errstr(result));
777 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
781 PRINTER_INFO_CTR *ctr)
785 /* setprinter call */
786 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, hnd, level, ctr, 0);
788 if (!W_ERROR_IS_OK(result)) {
789 printf("cannot set printer-info: %s\n", dos_errstr(result));
797 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
800 REGISTRY_VALUE *value)
804 /* setprinterdata call */
805 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
807 if (!W_ERROR_IS_OK(result)) {
808 printf ("unable to set printerdata: %s\n", dos_errstr(result));
816 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
824 /* enumprinterkey call */
825 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
827 if (!W_ERROR_IS_OK(result)) {
828 printf("enumprinterkey failed: %s\n", dos_errstr(result));
835 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
844 /* enumprinterdataex call */
845 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
847 if (!W_ERROR_IS_OK(result)) {
848 printf("enumprinterdataex failed: %s\n", dos_errstr(result));
856 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
860 REGISTRY_VALUE *value)
864 /* setprinterdataex call */
865 result = rpccli_spoolss_setprinterdataex(pipe_hnd, mem_ctx, hnd,
868 if (!W_ERROR_IS_OK(result)) {
869 printf("could not set printerdataex: %s\n", dos_errstr(result));
876 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
887 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
889 if (!W_ERROR_IS_OK(result)) {
890 printf("could not enum forms: %s\n", dos_errstr(result));
897 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
899 uint32 level, const char *env,
901 PRINTER_DRIVER_CTR *ctr)
905 /* enumprinterdrivers call */
906 result = rpccli_spoolss_enumprinterdrivers(
907 pipe_hnd, mem_ctx, level,
908 env, num_drivers, ctr);
910 if (!W_ERROR_IS_OK(result)) {
911 printf("cannot enum drivers: %s\n", dos_errstr(result));
918 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
920 POLICY_HND *hnd, uint32 level,
921 const char *env, int version,
922 PRINTER_DRIVER_CTR *ctr)
926 /* getprinterdriver call */
927 result = rpccli_spoolss_getprinterdriver(
928 pipe_hnd, mem_ctx, hnd, level,
931 if (!W_ERROR_IS_OK(result)) {
932 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
933 env, dos_errstr(result)));
934 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
935 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
936 printf("cannot get driver: %s\n", dos_errstr(result));
945 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
946 TALLOC_CTX *mem_ctx, uint32 level,
947 PRINTER_DRIVER_CTR *ctr)
951 /* addprinterdriver call */
952 result = rpccli_spoolss_addprinterdriver(pipe_hnd, mem_ctx, level, ctr);
954 /* be more verbose */
955 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
956 printf("You are not allowed to add drivers\n");
959 if (!W_ERROR_IS_OK(result)) {
960 printf("cannot add driver: %s\n", dos_errstr(result));
968 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
969 * for a single printer or for all printers depending on argc/argv
972 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
977 uint32 *num_printers,
978 PRINTER_INFO_CTR *ctr)
983 /* no arguments given, enumerate all printers */
986 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
987 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
988 level, num_printers, ctr))
995 /* argument given, get a single printer by name */
996 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
997 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->user_name, &hnd))
1000 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, ctr)) {
1001 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1005 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1010 DEBUG(3,("got %d printers\n", *num_printers));
1017 * List print-queues (including local printers that are not shared)
1019 * All parameters are provided by the run_rpc_command function, except for
1020 * argc, argv which are passed through.
1022 * @param domain_sid The domain sid aquired from the remote server
1023 * @param cli A cli_state connected to the server.
1024 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1025 * @param argc Standard main() style argc
1026 * @param argv Standard main() style argv. Initial components are already
1029 * @return Normal NTSTATUS return.
1032 NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid,
1033 const char *domain_name,
1034 struct cli_state *cli,
1035 struct rpc_pipe_client *pipe_hnd,
1036 TALLOC_CTX *mem_ctx,
1040 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1041 uint32 i, num_printers;
1043 char *printername, *sharename;
1044 PRINTER_INFO_CTR ctr;
1046 printf("listing printers\n");
1048 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1051 for (i = 0; i < num_printers; i++) {
1052 /* do some initialization */
1053 rpcstr_pull_talloc(mem_ctx,
1055 ctr.printers_2[i].printername.buffer,
1058 rpcstr_pull_talloc(mem_ctx,
1060 ctr.printers_2[i].sharename.buffer,
1064 if (printername && sharename) {
1065 d_printf("printer %d: %s, shared as: %s\n",
1066 i+1, printername, sharename);
1070 return NT_STATUS_OK;
1074 * List printer-drivers from a server
1076 * All parameters are provided by the run_rpc_command function, except for
1077 * argc, argv which are passed through.
1079 * @param domain_sid The domain sid aquired from the remote server
1080 * @param cli A cli_state connected to the server.
1081 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1082 * @param argc Standard main() style argc
1083 * @param argv Standard main() style argv. Initial components are already
1086 * @return Normal NTSTATUS return.
1089 NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid,
1090 const char *domain_name,
1091 struct cli_state *cli,
1092 struct rpc_pipe_client *pipe_hnd,
1093 TALLOC_CTX *mem_ctx,
1097 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1100 PRINTER_DRIVER_CTR drv_ctr_enum;
1103 ZERO_STRUCT(drv_ctr_enum);
1105 printf("listing printer-drivers\n");
1107 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1111 /* enum remote drivers */
1112 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1113 archi_table[i].long_archi,
1114 &num_drivers, &drv_ctr_enum)) {
1116 nt_status = NT_STATUS_UNSUCCESSFUL;
1120 if (num_drivers == 0) {
1121 d_printf ("no drivers found on server for architecture: [%s].\n",
1122 archi_table[i].long_archi);
1126 d_printf("got %d printer-drivers for architecture: [%s]\n",
1127 num_drivers, archi_table[i].long_archi);
1130 /* do something for all drivers for architecture */
1131 for (d = 0; d < num_drivers; d++) {
1132 display_print_driver_3(&(drv_ctr_enum.info3[d]));
1136 nt_status = NT_STATUS_OK;
1144 * Publish print-queues with args-wrapper
1146 * @param cli A cli_state connected to the server.
1147 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1148 * @param argc Standard main() style argc
1149 * @param argv Standard main() style argv. Initial components are already
1153 * @return Normal NTSTATUS return.
1156 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1157 TALLOC_CTX *mem_ctx,
1162 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1163 uint32 i, num_printers;
1165 char *printername, *sharename;
1166 PRINTER_INFO_CTR ctr, ctr_pub;
1168 bool got_hnd = False;
1170 const char *action_str;
1172 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1175 for (i = 0; i < num_printers; i++) {
1176 /* do some initialization */
1177 rpcstr_pull_talloc(mem_ctx,
1179 ctr.printers_2[i].printername.buffer,
1182 rpcstr_pull_talloc(mem_ctx,
1184 ctr.printers_2[i].sharename.buffer,
1187 if (!printername || !sharename) {
1191 /* open printer handle */
1192 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1193 PRINTER_ALL_ACCESS, pipe_hnd->user_name, &hnd))
1198 /* check for existing dst printer */
1199 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1202 /* check action and set string */
1204 case SPOOL_DS_PUBLISH:
1205 action_str = "published";
1207 case SPOOL_DS_UPDATE:
1208 action_str = "updated";
1210 case SPOOL_DS_UNPUBLISH:
1211 action_str = "unpublished";
1214 action_str = "unknown action";
1215 printf("unkown action: %d\n", action);
1219 ctr_pub.printers_7->action = action;
1221 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub, 0);
1222 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1223 printf("cannot set printer-info: %s\n", dos_errstr(result));
1227 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1230 nt_status = NT_STATUS_OK;
1234 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1239 NTSTATUS rpc_printer_publish_publish_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_PUBLISH);
1250 NTSTATUS rpc_printer_publish_unpublish_internals(const DOM_SID *domain_sid,
1251 const char *domain_name,
1252 struct cli_state *cli,
1253 struct rpc_pipe_client *pipe_hnd,
1254 TALLOC_CTX *mem_ctx,
1258 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
1261 NTSTATUS rpc_printer_publish_update_internals(const DOM_SID *domain_sid,
1262 const char *domain_name,
1263 struct cli_state *cli,
1264 struct rpc_pipe_client *pipe_hnd,
1265 TALLOC_CTX *mem_ctx,
1269 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
1273 * List print-queues w.r.t. their publishing state
1275 * All parameters are provided by the run_rpc_command function, except for
1276 * argc, argv which are passed through.
1278 * @param domain_sid The domain sid aquired from the remote server
1279 * @param cli A cli_state connected to the server.
1280 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1281 * @param argc Standard main() style argc
1282 * @param argv Standard main() style argv. Initial components are already
1285 * @return Normal NTSTATUS return.
1288 NTSTATUS rpc_printer_publish_list_internals(const DOM_SID *domain_sid,
1289 const char *domain_name,
1290 struct cli_state *cli,
1291 struct rpc_pipe_client *pipe_hnd,
1292 TALLOC_CTX *mem_ctx,
1296 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1297 uint32 i, num_printers;
1299 char *printername, *sharename;
1301 PRINTER_INFO_CTR ctr, ctr_pub;
1303 bool got_hnd = False;
1306 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1309 for (i = 0; i < num_printers; i++) {
1310 ZERO_STRUCT(ctr_pub);
1312 /* do some initialization */
1313 rpcstr_pull_talloc(mem_ctx,
1315 ctr.printers_2[i].printername.buffer,
1318 rpcstr_pull_talloc(mem_ctx,
1320 ctr.printers_2[i].sharename.buffer,
1323 if (!printername || !sharename) {
1327 /* open printer handle */
1328 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1329 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1334 /* check for existing dst printer */
1335 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1338 rpcstr_pull_talloc(mem_ctx,
1340 ctr_pub.printers_7->guid.buffer,
1346 state = ctr_pub.printers_7->action;
1348 case SPOOL_DS_PUBLISH:
1349 printf("printer [%s] is published", sharename);
1351 printf(", guid: %s", guid);
1354 case SPOOL_DS_UNPUBLISH:
1355 printf("printer [%s] is unpublished\n", sharename);
1357 case SPOOL_DS_UPDATE:
1358 printf("printer [%s] is currently updating\n", sharename);
1361 printf("unkown state: %d\n", state);
1366 nt_status = NT_STATUS_OK;
1370 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1376 * Migrate Printer-ACLs from a source server to the destination server
1378 * All parameters are provided by the run_rpc_command function, except for
1379 * argc, argv which are passed through.
1381 * @param domain_sid The domain sid aquired from the remote server
1382 * @param cli A cli_state connected to the server.
1383 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1384 * @param argc Standard main() style argc
1385 * @param argv Standard main() style argv. Initial components are already
1388 * @return Normal NTSTATUS return.
1391 NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid,
1392 const char *domain_name,
1393 struct cli_state *cli,
1394 struct rpc_pipe_client *pipe_hnd,
1395 TALLOC_CTX *mem_ctx,
1399 /* TODO: what now, info2 or info3 ?
1400 convince jerry that we should add clientside setacls level 3 at least
1402 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1404 uint32 num_printers;
1406 char *printername, *sharename;
1407 bool got_hnd_src = False;
1408 bool got_hnd_dst = False;
1409 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1410 POLICY_HND hnd_src, hnd_dst;
1411 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1412 struct cli_state *cli_dst = NULL;
1414 ZERO_STRUCT(ctr_src);
1416 DEBUG(3,("copying printer ACLs\n"));
1418 /* connect destination PI_SPOOLSS */
1419 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1420 if (!NT_STATUS_IS_OK(nt_status))
1424 /* enum source printers */
1425 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1426 nt_status = NT_STATUS_UNSUCCESSFUL;
1430 if (!num_printers) {
1431 printf ("no printers found on server.\n");
1432 nt_status = NT_STATUS_OK;
1436 /* do something for all printers */
1437 for (i = 0; i < num_printers; i++) {
1438 /* do some initialization */
1439 rpcstr_pull_talloc(mem_ctx,
1441 ctr_enum.printers_2[i].printername.buffer,
1444 rpcstr_pull_talloc(mem_ctx,
1446 ctr_enum.printers_2[i].sharename.buffer,
1449 if (!printername || !sharename) {
1450 nt_status = NT_STATUS_UNSUCCESSFUL;
1454 /* we can reset NT_STATUS here because we do not
1455 get any real NT_STATUS-codes anymore from now on */
1456 nt_status = NT_STATUS_UNSUCCESSFUL;
1458 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1459 printername, sharename);
1461 /* according to msdn you have specify these access-rights
1462 to see the security descriptor
1463 - READ_CONTROL (DACL)
1464 - ACCESS_SYSTEM_SECURITY (SACL)
1467 /* open src printer handle */
1468 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1469 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1474 /* open dst printer handle */
1475 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1476 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1481 /* check for existing dst printer */
1482 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1485 /* check for existing src printer */
1486 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &ctr_src))
1489 /* Copy Security Descriptor */
1491 /* copy secdesc (info level 2) */
1492 ctr_dst.printers_2->devmode = NULL;
1493 ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
1496 display_sec_desc(ctr_dst.printers_2->secdesc);
1498 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &ctr_dst))
1501 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1504 /* close printer handles here */
1506 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1507 got_hnd_src = False;
1511 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1512 got_hnd_dst = False;
1517 nt_status = NT_STATUS_OK;
1522 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1526 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1530 cli_shutdown(cli_dst);
1536 * Migrate printer-forms from a src server to the dst server
1538 * All parameters are provided by the run_rpc_command function, except for
1539 * argc, argv which are passed through.
1541 * @param domain_sid The domain sid aquired from the remote server
1542 * @param cli A cli_state connected to the server.
1543 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1544 * @param argc Standard main() style argc
1545 * @param argv Standard main() style argv. Initial components are already
1548 * @return Normal NTSTATUS return.
1551 NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid,
1552 const char *domain_name,
1553 struct cli_state *cli,
1554 struct rpc_pipe_client *pipe_hnd,
1555 TALLOC_CTX *mem_ctx,
1559 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1562 uint32 num_printers;
1564 char *printername, *sharename;
1565 bool got_hnd_src = False;
1566 bool got_hnd_dst = False;
1567 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1568 POLICY_HND hnd_src, hnd_dst;
1569 PRINTER_INFO_CTR ctr_enum, ctr_dst;
1572 struct cli_state *cli_dst = NULL;
1574 ZERO_STRUCT(ctr_enum);
1576 DEBUG(3,("copying forms\n"));
1578 /* connect destination PI_SPOOLSS */
1579 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1580 if (!NT_STATUS_IS_OK(nt_status))
1583 /* enum src printers */
1584 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1585 nt_status = NT_STATUS_UNSUCCESSFUL;
1589 if (!num_printers) {
1590 printf ("no printers found on server.\n");
1591 nt_status = NT_STATUS_OK;
1595 /* do something for all printers */
1596 for (i = 0; i < num_printers; i++) {
1597 /* do some initialization */
1598 rpcstr_pull_talloc(mem_ctx,
1600 ctr_enum.printers_2[i].printername.buffer,
1603 rpcstr_pull_talloc(mem_ctx,
1605 ctr_enum.printers_2[i].sharename.buffer,
1608 if (!printername || !sharename) {
1609 nt_status = NT_STATUS_UNSUCCESSFUL;
1612 /* we can reset NT_STATUS here because we do not
1613 get any real NT_STATUS-codes anymore from now on */
1614 nt_status = NT_STATUS_UNSUCCESSFUL;
1616 d_printf("migrating printer forms for: [%s] / [%s]\n",
1617 printername, sharename);
1620 /* open src printer handle */
1621 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1622 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1628 /* open dst printer handle */
1629 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1630 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1636 /* check for existing dst printer */
1637 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1640 /* finally migrate forms */
1641 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1644 DEBUG(1,("got %d forms for printer\n", num_forms));
1647 for (f = 0; f < num_forms; f++) {
1652 /* only migrate FORM_PRINTER types, according to jerry
1653 FORM_BUILTIN-types are hard-coded in samba */
1654 if (forms[f].flag != FORM_PRINTER)
1657 if (forms[f].name.buffer)
1658 rpcstr_pull(form_name, forms[f].name.buffer,
1659 sizeof(form_name), -1, STR_TERMINATE);
1662 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1663 f, form_name, forms[f].flag);
1665 /* is there a more elegant way to do that ? */
1666 form.flags = FORM_PRINTER;
1667 form.size_x = forms[f].width;
1668 form.size_y = forms[f].length;
1669 form.left = forms[f].left;
1670 form.top = forms[f].top;
1671 form.right = forms[f].right;
1672 form.bottom = forms[f].bottom;
1674 init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
1676 /* FIXME: there might be something wrong with samba's
1678 result = rpccli_spoolss_addform(pipe_hnd_dst, mem_ctx,
1679 &hnd_dst, 1, &form);
1680 if (!W_ERROR_IS_OK(result)) {
1681 d_printf("\tAddForm form %d: [%s] refused.\n",
1686 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1690 /* close printer handles here */
1692 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1693 got_hnd_src = False;
1697 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1698 got_hnd_dst = False;
1702 nt_status = NT_STATUS_OK;
1707 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1710 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1713 cli_shutdown(cli_dst);
1719 * Migrate printer-drivers from a src server to the dst server
1721 * All parameters are provided by the run_rpc_command function, except for
1722 * argc, argv which are passed through.
1724 * @param domain_sid The domain sid aquired from the remote server
1725 * @param cli A cli_state connected to the server.
1726 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1727 * @param argc Standard main() style argc
1728 * @param argv Standard main() style argv. Initial components are already
1731 * @return Normal NTSTATUS return.
1734 NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid,
1735 const char *domain_name,
1736 struct cli_state *cli,
1737 struct rpc_pipe_client *pipe_hnd,
1738 TALLOC_CTX *mem_ctx,
1742 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1744 uint32 num_printers;
1746 char *printername, *sharename;
1747 bool got_hnd_src = False;
1748 bool got_hnd_dst = False;
1749 bool got_src_driver_share = False;
1750 bool got_dst_driver_share = False;
1751 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1752 POLICY_HND hnd_src, hnd_dst;
1753 PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
1754 PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1755 struct cli_state *cli_dst = NULL;
1756 struct cli_state *cli_share_src = NULL;
1757 struct cli_state *cli_share_dst = NULL;
1758 fstring drivername = "";
1760 ZERO_STRUCT(drv_ctr_src);
1761 ZERO_STRUCT(drv_ctr_dst);
1762 ZERO_STRUCT(info_ctr_enum);
1763 ZERO_STRUCT(info_ctr_dst);
1765 DEBUG(3,("copying printer-drivers\n"));
1767 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1768 if (!NT_STATUS_IS_OK(nt_status))
1771 /* open print$-share on the src server */
1772 nt_status = connect_to_service(&cli_share_src, &cli->dest_ss,
1773 cli->desthost, "print$", "A:");
1774 if (!NT_STATUS_IS_OK(nt_status))
1777 got_src_driver_share = True;
1780 /* open print$-share on the dst server */
1781 nt_status = connect_to_service(&cli_share_dst, &cli_dst->dest_ss,
1782 cli_dst->desthost, "print$", "A:");
1783 if (!NT_STATUS_IS_OK(nt_status))
1786 got_dst_driver_share = True;
1789 /* enum src printers */
1790 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1791 nt_status = NT_STATUS_UNSUCCESSFUL;
1795 if (num_printers == 0) {
1796 printf ("no printers found on server.\n");
1797 nt_status = NT_STATUS_OK;
1802 /* do something for all printers */
1803 for (p = 0; p < num_printers; p++) {
1804 /* do some initialization */
1805 rpcstr_pull_talloc(mem_ctx,
1807 info_ctr_enum.printers_2[p].printername.buffer,
1810 rpcstr_pull_talloc(mem_ctx,
1812 info_ctr_enum.printers_2[p].sharename.buffer,
1815 if (!printername || !sharename) {
1816 nt_status = NT_STATUS_UNSUCCESSFUL;
1820 /* we can reset NT_STATUS here because we do not
1821 get any real NT_STATUS-codes anymore from now on */
1822 nt_status = NT_STATUS_UNSUCCESSFUL;
1824 d_printf("migrating printer driver for: [%s] / [%s]\n",
1825 printername, sharename);
1827 /* open dst printer handle */
1828 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1829 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1834 /* check for existing dst printer */
1835 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst))
1839 /* open src printer handle */
1840 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1841 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->user_name, &hnd_src))
1847 /* in a first step call getdriver for each shared printer (per arch)
1848 to get a list of all files that have to be copied */
1850 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1853 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1854 level, archi_table[i].long_archi,
1855 archi_table[i].version, &drv_ctr_src))
1858 rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer,
1859 sizeof(drivername), -1, STR_TERMINATE);
1862 display_print_driver_3(drv_ctr_src.info3);
1865 /* check arch dir */
1866 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1867 if (!NT_STATUS_IS_OK(nt_status))
1871 /* copy driver-files */
1872 nt_status = copy_print_driver_3(mem_ctx, cli_share_src, cli_share_dst,
1873 archi_table[i].short_archi,
1875 if (!NT_STATUS_IS_OK(nt_status))
1880 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_ctr_src)) {
1881 nt_status = NT_STATUS_UNSUCCESSFUL;
1885 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1886 drivername, printername));
1890 if (strlen(drivername) == 0) {
1891 DEBUGADD(1,("Did not get driver for printer %s\n",
1897 init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
1899 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) {
1900 nt_status = NT_STATUS_UNSUCCESSFUL;
1904 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1905 drivername, printername));
1909 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1910 got_hnd_dst = False;
1915 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1916 got_hnd_src = False;
1920 nt_status = NT_STATUS_OK;
1925 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1928 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1931 cli_shutdown(cli_dst);
1934 if (got_src_driver_share)
1935 cli_shutdown(cli_share_src);
1937 if (got_dst_driver_share)
1938 cli_shutdown(cli_share_dst);
1945 * Migrate printer-queues from a src to the dst server
1946 * (requires a working "addprinter command" to be installed for the local smbd)
1948 * All parameters are provided by the run_rpc_command function, except for
1949 * argc, argv which are passed through.
1951 * @param domain_sid The domain sid aquired from the remote server
1952 * @param cli A cli_state connected to the server.
1953 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1954 * @param argc Standard main() style argc
1955 * @param argv Standard main() style argv. Initial components are already
1958 * @return Normal NTSTATUS return.
1961 NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid,
1962 const char *domain_name,
1963 struct cli_state *cli,
1964 struct rpc_pipe_client *pipe_hnd,
1965 TALLOC_CTX *mem_ctx,
1970 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1971 uint32 i = 0, num_printers;
1973 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1974 struct cli_state *cli_dst = NULL;
1975 POLICY_HND hnd_dst, hnd_src;
1976 char *printername, *sharename;
1977 bool got_hnd_src = False;
1978 bool got_hnd_dst = False;
1979 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1981 DEBUG(3,("copying printers\n"));
1983 /* connect destination PI_SPOOLSS */
1984 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1985 if (!NT_STATUS_IS_OK(nt_status))
1989 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1990 nt_status = NT_STATUS_UNSUCCESSFUL;
1994 if (!num_printers) {
1995 printf ("no printers found on server.\n");
1996 nt_status = NT_STATUS_OK;
2000 /* do something for all printers */
2001 for (i = 0; i < num_printers; i++) {
2002 /* do some initialization */
2003 rpcstr_pull_talloc(mem_ctx,
2005 ctr_enum.printers_2[i].printername.buffer,
2008 rpcstr_pull_talloc(mem_ctx,
2010 ctr_enum.printers_2[i].sharename.buffer,
2013 if (!printername || !sharename) {
2014 nt_status = NT_STATUS_UNSUCCESSFUL;
2017 /* we can reset NT_STATUS here because we do not
2018 get any real NT_STATUS-codes anymore from now on */
2019 nt_status = NT_STATUS_UNSUCCESSFUL;
2021 d_printf("migrating printer queue for: [%s] / [%s]\n",
2022 printername, sharename);
2024 /* open dst printer handle */
2025 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2026 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2028 DEBUG(1,("could not open printer: %s\n", sharename));
2033 /* check for existing dst printer */
2034 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) {
2035 printf ("could not get printer, creating printer.\n");
2037 DEBUG(1,("printer already exists: %s\n", sharename));
2038 /* close printer handle here - dst only, not got src yet. */
2040 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2041 got_hnd_dst = False;
2046 /* now get again src printer ctr via getprinter,
2047 we first need a handle for that */
2049 /* open src printer handle */
2050 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2051 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2056 /* getprinter on the src server */
2057 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &ctr_src))
2060 /* copy each src printer to a dst printer 1:1,
2061 maybe some values have to be changed though */
2062 d_printf("creating printer: %s\n", printername);
2063 result = rpccli_spoolss_addprinterex (pipe_hnd_dst, mem_ctx, level, &ctr_src);
2065 if (W_ERROR_IS_OK(result))
2066 d_printf ("printer [%s] successfully added.\n", printername);
2067 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2068 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2070 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2074 /* close printer handles here */
2076 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2077 got_hnd_src = False;
2081 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2082 got_hnd_dst = False;
2086 nt_status = NT_STATUS_OK;
2090 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2093 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2096 cli_shutdown(cli_dst);
2102 * Migrate Printer-Settings from a src server to the dst server
2103 * (for this to work, printers and drivers already have to be migrated earlier)
2105 * All parameters are provided by the run_rpc_command function, except for
2106 * argc, argv which are passed through.
2108 * @param domain_sid The domain sid aquired from the remote server
2109 * @param cli A cli_state connected to the server.
2110 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2111 * @param argc Standard main() style argc
2112 * @param argv Standard main() style argv. Initial components are already
2115 * @return Normal NTSTATUS return.
2118 NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid,
2119 const char *domain_name,
2120 struct cli_state *cli,
2121 struct rpc_pipe_client *pipe_hnd,
2122 TALLOC_CTX *mem_ctx,
2127 /* FIXME: Here the nightmare begins */
2130 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2131 uint32 i = 0, p = 0, j = 0;
2132 uint32 num_printers, val_needed, data_needed;
2134 char *printername, *sharename;
2135 bool got_hnd_src = False;
2136 bool got_hnd_dst = False;
2137 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2138 POLICY_HND hnd_src, hnd_dst;
2139 PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
2140 REGVAL_CTR *reg_ctr;
2141 struct cli_state *cli_dst = NULL;
2142 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2143 const char *longname;
2145 uint16 *keylist = NULL, *curkey;
2147 ZERO_STRUCT(ctr_enum);
2149 DEBUG(3,("copying printer settings\n"));
2151 /* connect destination PI_SPOOLSS */
2152 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
2153 if (!NT_STATUS_IS_OK(nt_status))
2156 /* enum src printers */
2157 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2158 nt_status = NT_STATUS_UNSUCCESSFUL;
2162 if (!num_printers) {
2163 printf ("no printers found on server.\n");
2164 nt_status = NT_STATUS_OK;
2169 /* needed for dns-strings in regkeys */
2170 longname = get_mydnsfullname();
2172 nt_status = NT_STATUS_UNSUCCESSFUL;
2176 /* do something for all printers */
2177 for (i = 0; i < num_printers; i++) {
2178 /* do some initialization */
2179 rpcstr_pull_talloc(mem_ctx,
2181 ctr_enum.printers_2[i].printername.buffer,
2184 rpcstr_pull_talloc(mem_ctx,
2186 ctr_enum.printers_2[i].sharename.buffer,
2189 if (!printername || !sharename) {
2190 nt_status = NT_STATUS_UNSUCCESSFUL;
2193 /* we can reset NT_STATUS here because we do not
2194 get any real NT_STATUS-codes anymore from now on */
2195 nt_status = NT_STATUS_UNSUCCESSFUL;
2197 d_printf("migrating printer settings for: [%s] / [%s]\n",
2198 printername, sharename);
2201 /* open src printer handle */
2202 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2203 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2209 /* open dst printer handle */
2210 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2211 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2217 /* check for existing dst printer */
2218 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2223 /* STEP 1: COPY DEVICE-MODE and other
2224 PRINTER_INFO_2-attributes
2227 ctr_dst.printers_2 = &ctr_enum.printers_2[i];
2229 /* why is the port always disconnected when the printer
2230 is correctly installed (incl. driver ???) */
2231 init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
2233 /* check if printer is published */
2234 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2236 /* check for existing dst printer */
2237 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
2240 ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
2242 /* ignore False from setprinter due to WERR_IO_PENDING */
2243 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
2245 DEBUG(3,("republished printer\n"));
2248 if (ctr_enum.printers_2[i].devmode != NULL) {
2250 /* copy devmode (info level 2) */
2251 ctr_dst.printers_2->devmode = (DEVICEMODE *)
2252 TALLOC_MEMDUP(mem_ctx,
2253 ctr_enum.printers_2[i].devmode,
2254 sizeof(DEVICEMODE));
2256 /* do not copy security descriptor (we have another
2257 * command for that) */
2258 ctr_dst.printers_2->secdesc = NULL;
2261 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2263 nt_status = NT_STATUS_NO_MEMORY;
2267 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2270 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2274 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2277 /* STEP 2: COPY REGISTRY VALUES */
2279 /* please keep in mind that samba parse_spools gives horribly
2280 crippled results when used to rpccli_spoolss_enumprinterdataex
2281 a win2k3-server. (Bugzilla #1851)
2282 FIXME: IIRC I've seen it too on a win2k-server
2285 /* enumerate data on src handle */
2286 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2287 &val_needed, &data_needed, NULL);
2289 /* loop for all printerdata of "PrinterDriverData" */
2290 while (W_ERROR_IS_OK(result)) {
2292 REGISTRY_VALUE value;
2294 result = rpccli_spoolss_enumprinterdata(
2295 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2296 data_needed, 0, 0, &value);
2298 /* loop for all reg_keys */
2299 if (W_ERROR_IS_OK(result)) {
2303 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2306 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2310 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2315 /* STEP 3: COPY SUBKEY VALUES */
2317 /* here we need to enum all printer_keys and then work
2318 on the result with enum_printer_key_ex. nt4 does not
2319 respond to enumprinterkey, win2k does, so continue
2320 in case of an error */
2322 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2323 printf("got no key-data\n");
2328 /* work on a list of printer keys
2329 each key has to be enumerated to get all required
2330 information. information is then set via setprinterdataex-calls */
2332 if (keylist == NULL)
2336 while (*curkey != 0) {
2338 rpcstr_pull_talloc(mem_ctx,
2344 return NT_STATUS_NO_MEMORY;
2347 curkey += strlen(subkey) + 1;
2349 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2350 return NT_STATUS_NO_MEMORY;
2352 /* enumerate all src subkeys */
2353 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2358 for (j=0; j < reg_ctr->num_values; j++) {
2360 REGISTRY_VALUE value;
2363 /* although samba replies with sane data in most cases we
2364 should try to avoid writing wrong registry data */
2366 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2367 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2368 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2369 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2370 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2372 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2374 /* although windows uses a multi-sz, we use a sz */
2375 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2376 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2379 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2381 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2382 nt_status = NT_STATUS_NO_MEMORY;
2385 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2386 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2389 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2394 /* FIXME: should we really do that ??? */
2395 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2396 nt_status = NT_STATUS_NO_MEMORY;
2399 init_unistr2(&data, url, UNI_STR_TERMINATE);
2400 fstrcpy(value.valuename, SPOOL_REG_URL);
2404 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2406 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2407 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2410 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2412 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2413 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2416 value.type = REG_SZ;
2417 value.size = data.uni_str_len * 2;
2419 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2421 value.data_p = NULL;
2425 display_reg_value(subkey, value);
2427 /* here we have to set all subkeys on the dst server */
2428 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2435 display_reg_value(subkey, *(reg_ctr->values[j]));
2437 /* here we have to set all subkeys on the dst server */
2438 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2439 subkey, reg_ctr->values[j]))
2444 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2445 subkey, reg_ctr->values[j]->valuename));
2449 TALLOC_FREE( reg_ctr );
2454 /* close printer handles here */
2456 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2457 got_hnd_src = False;
2461 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2462 got_hnd_dst = False;
2467 nt_status = NT_STATUS_OK;
2470 SAFE_FREE(devicename);
2472 SAFE_FREE(unc_name);
2475 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2478 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2481 cli_shutdown(cli_dst);