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 c A net_context structure
165 * @param mem_ctx A talloc-context
166 * @param cli_share_src A connected cli_state
167 * @param cli_share_dst A connected cli_state
168 * @param src_file The source file-name
169 * @param dst_file The destination file-name
170 * @param copy_acls Whether to copy acls
171 * @param copy_attrs Whether to copy DOS attributes
172 * @param copy_timestamps Whether to preserve timestamps
173 * @param is_file Whether this file is a file or a dir
175 * @return Normal NTSTATUS return.
178 NTSTATUS net_copy_fileattr(struct net_context *c,
180 struct cli_state *cli_share_src,
181 struct cli_state *cli_share_dst,
182 const char *src_name, const char *dst_name,
183 bool copy_acls, bool copy_attrs,
184 bool copy_timestamps, bool is_file)
186 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
191 time_t f_atime, f_ctime, f_mtime;
194 if (!copy_timestamps && !copy_acls && !copy_attrs)
197 /* open file/dir on the originating server */
199 DEBUGADD(3,("opening %s %s on originating server\n",
200 is_file?"file":"dir", src_name));
202 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
203 if (fnum_src == -1) {
204 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
205 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
206 nt_status = cli_nt_error(cli_share_src);
213 /* get the security descriptor */
214 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
216 DEBUG(0,("failed to get security descriptor: %s\n",
217 cli_errstr(cli_share_src)));
218 nt_status = cli_nt_error(cli_share_src);
222 if (c->opt_verbose && DEBUGLEVEL >= 3)
223 display_sec_desc(sd);
227 if (copy_attrs || copy_timestamps) {
229 /* get file attributes */
230 if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
231 &f_ctime, &f_atime, &f_mtime)) {
232 DEBUG(0,("failed to get file-attrs: %s\n",
233 cli_errstr(cli_share_src)));
234 nt_status = cli_nt_error(cli_share_src);
240 /* open the file/dir on the destination server */
242 fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
243 if (fnum_dst == -1) {
244 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
245 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
246 nt_status = cli_nt_error(cli_share_dst);
250 if (copy_timestamps) {
253 if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
254 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
255 cli_errstr(cli_share_dst)));
256 nt_status = cli_nt_error(cli_share_dst);
264 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
265 DEBUG(0,("could not set secdesc on %s: %s\n",
266 dst_name, cli_errstr(cli_share_dst)));
267 nt_status = cli_nt_error(cli_share_dst);
275 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
276 DEBUG(0,("failed to set file-attrs: %s\n",
277 cli_errstr(cli_share_dst)));
278 nt_status = cli_nt_error(cli_share_dst);
286 if (!cli_close(cli_share_src, fnum_src)) {
287 d_fprintf(stderr, "could not close %s on originating server: %s\n",
288 is_file?"file":"dir", cli_errstr(cli_share_src));
289 nt_status = cli_nt_error(cli_share_src);
293 if (!cli_close(cli_share_dst, fnum_dst)) {
294 d_fprintf(stderr, "could not close %s on destination server: %s\n",
295 is_file?"file":"dir", cli_errstr(cli_share_dst));
296 nt_status = cli_nt_error(cli_share_dst);
301 nt_status = NT_STATUS_OK;
307 cli_close(cli_share_src, fnum_src);
310 cli_close(cli_share_dst, fnum_dst);
316 * Copy a file or directory from a connected share to another connected share
318 * @param c A net_context structure
319 * @param mem_ctx A talloc-context
320 * @param cli_share_src A connected cli_state
321 * @param cli_share_dst A connected cli_state
322 * @param src_file The source file-name
323 * @param dst_file The destination file-name
324 * @param copy_acls Whether to copy acls
325 * @param copy_attrs Whether to copy DOS attributes
326 * @param copy_timestamps Whether to preserve timestamps
327 * @param is_file Whether this file is a file or a dir
329 * @return Normal NTSTATUS return.
332 NTSTATUS net_copy_file(struct net_context *c,
334 struct cli_state *cli_share_src,
335 struct cli_state *cli_share_dst,
336 const char *src_name, const char *dst_name,
337 bool copy_acls, bool copy_attrs,
338 bool copy_timestamps, bool is_file)
340 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
343 static int io_bufsize = 64512;
344 int read_size = io_bufsize;
349 if (!src_name || !dst_name)
352 if (cli_share_src == NULL || cli_share_dst == NULL)
355 /* open on the originating server */
356 DEBUGADD(3,("opening %s %s on originating server\n",
357 is_file ? "file":"dir", src_name));
359 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
361 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
363 if (fnum_src == -1) {
364 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
365 is_file ? "file":"dir",
366 src_name, cli_errstr(cli_share_src)));
367 nt_status = cli_nt_error(cli_share_src);
374 /* open file on the destination server */
375 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
376 fnum_dst = cli_open(cli_share_dst, dst_name,
377 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
379 if (fnum_dst == -1) {
380 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
381 dst_name, cli_errstr(cli_share_dst)));
382 nt_status = cli_nt_error(cli_share_dst);
386 /* allocate memory */
387 if (!(data = (char *)SMB_MALLOC(read_size))) {
388 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
389 nt_status = NT_STATUS_NO_MEMORY;
396 if (c->opt_verbose) {
398 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
399 "%s ACLs and %s DOS Attributes %s\n",
400 cli_share_src->desthost, cli_share_src->share, src_name,
401 cli_share_dst->desthost, cli_share_dst->share, dst_name,
402 copy_acls ? "with" : "without",
403 copy_attrs ? "with" : "without",
404 copy_timestamps ? "(preserving timestamps)" : "" );
412 n = cli_read(cli_share_src, fnum_src, data, nread,
418 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
422 d_fprintf(stderr, "Error writing file: %s\n",
423 cli_errstr(cli_share_dst));
424 nt_status = cli_nt_error(cli_share_dst);
432 if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
435 DEBUGADD(3,("creating dir %s on the destination server\n",
438 if (!cli_mkdir(cli_share_dst, dst_name)) {
439 DEBUG(0,("cannot create directory %s: %s\n",
440 dst_name, cli_errstr(cli_share_dst)));
441 nt_status = NT_STATUS_NO_SUCH_FILE;
444 if (!cli_chkpath(cli_share_dst, dst_name)) {
445 d_fprintf(stderr, "cannot check for directory %s: %s\n",
446 dst_name, cli_errstr(cli_share_dst));
453 if (!cli_close(cli_share_src, fnum_src)) {
454 d_fprintf(stderr, "could not close file on originating server: %s\n",
455 cli_errstr(cli_share_src));
456 nt_status = cli_nt_error(cli_share_src);
460 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
461 d_fprintf(stderr, "could not close file on destination server: %s\n",
462 cli_errstr(cli_share_dst));
463 nt_status = cli_nt_error(cli_share_dst);
467 /* possibly we have to copy some file-attributes / acls / sd */
468 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
469 src_name, dst_name, copy_acls,
470 copy_attrs, copy_timestamps, is_file);
471 if (!NT_STATUS_IS_OK(nt_status))
475 nt_status = NT_STATUS_OK;
481 cli_close(cli_share_src, fnum_src);
484 cli_close(cli_share_dst, fnum_dst);
492 * Copy a driverfile from on connected share to another connected share
493 * This silently assumes that a driver-file is picked up from
495 * \\src_server\print$\{arch}\{version}\file
499 * \\dst_server\print$\{arch}\file
501 * to be added via setdriver-calls later.
502 * @param c A net_context structure
503 * @param mem_ctx A talloc-context
504 * @param cli_share_src A cli_state connected to source print$-share
505 * @param cli_share_dst A cli_state connected to destination print$-share
506 * @param file The file-name to be copied
507 * @param short_archi The name of the driver-architecture (short form)
509 * @return Normal NTSTATUS return.
512 static NTSTATUS net_copy_driverfile(struct net_context *c,
514 struct cli_state *cli_share_src,
515 struct cli_state *cli_share_dst,
516 char *file, const char *short_archi) {
518 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
526 /* scroll through the file until we have the part
527 beyond archi_table.short_archi */
529 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
530 if (strequal(tok, short_archi)) {
531 next_token_talloc(mem_ctx, &p, &version, "\\");
532 next_token_talloc(mem_ctx, &p, &filename, "\\");
536 /* build source file name */
537 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
538 return NT_STATUS_NO_MEMORY;
541 /* create destination file name */
542 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
543 return NT_STATUS_NO_MEMORY;
546 /* finally copy the file */
547 nt_status = net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
548 src_name, dst_name, false, false, false, true);
549 if (!NT_STATUS_IS_OK(nt_status))
552 nt_status = NT_STATUS_OK;
562 * Check for existing Architecture directory on a given server
564 * @param cli_share A cli_state connected to a print$-share
565 * @param short_archi The Architecture for the print-driver
567 * @return Normal NTSTATUS return.
570 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
573 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
576 if (asprintf(&dir, "\\%s", short_archi) < 0) {
577 return NT_STATUS_NO_MEMORY;
580 DEBUG(10,("creating print-driver dir for architecture: %s\n",
583 if (!cli_mkdir(cli_share, dir)) {
584 DEBUG(1,("cannot create directory %s: %s\n",
585 dir, cli_errstr(cli_share)));
586 nt_status = NT_STATUS_NO_SUCH_FILE;
589 if (!cli_chkpath(cli_share, dir)) {
590 d_fprintf(stderr, "cannot check %s: %s\n",
591 dir, cli_errstr(cli_share));
595 nt_status = NT_STATUS_OK;
603 * Copy a print-driver (level 3) from one connected print$-share to another
604 * connected print$-share
606 * @param c A net_context structure
607 * @param mem_ctx A talloc-context
608 * @param cli_share_src A cli_state connected to a print$-share
609 * @param cli_share_dst A cli_state connected to a print$-share
610 * @param short_archi The Architecture for the print-driver
611 * @param i1 The DRIVER_INFO_3-struct
613 * @return Normal NTSTATUS return.
616 static NTSTATUS copy_print_driver_3(struct net_context *c,
618 struct cli_state *cli_share_src,
619 struct cli_state *cli_share_dst,
620 const char *short_archi, DRIVER_INFO_3 *i1)
622 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
627 fstring driverpath = "";
628 fstring datafile = "";
629 fstring configfile = "";
630 fstring helpfile = "";
631 fstring dependentfiles = "";
636 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
637 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
638 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
639 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
640 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
644 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
645 name, short_archi, i1->version);
647 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
648 driverpath, short_archi);
649 if (!NT_STATUS_IS_OK(nt_status))
652 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
653 datafile, short_archi);
654 if (!NT_STATUS_IS_OK(nt_status))
657 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
658 configfile, short_archi);
659 if (!NT_STATUS_IS_OK(nt_status))
662 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
663 helpfile, short_archi);
664 if (!NT_STATUS_IS_OK(nt_status))
669 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
670 length += strlen(dependentfiles)+1;
672 if (strlen(dependentfiles) > 0) {
674 nt_status = net_copy_driverfile(c, mem_ctx,
675 cli_share_src, cli_share_dst,
676 dependentfiles, short_archi);
677 if (!NT_STATUS_IS_OK(nt_status))
688 * net_spoolss-functions
689 * =====================
691 * the net_spoolss-functions aim to simplify spoolss-client-functions
692 * required during the migration-process wrt buffer-sizes, returned
695 * this greatly reduces the complexitiy of the migrate-functions.
699 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
704 uint32 *num_printers,
705 PRINTER_INFO_CTR *ctr)
710 result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
711 level, num_printers, ctr);
713 if (!W_ERROR_IS_OK(result)) {
714 printf("cannot enum printers: %s\n", win_errstr(result));
721 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
723 const char *printername,
724 uint32 access_required,
725 const char *username,
729 fstring printername2;
731 fstrcpy(printername2, pipe_hnd->srv_name_slash);
732 fstrcat(printername2, "\\");
733 fstrcat(printername2, printername);
735 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
736 pipe_hnd->srv_name_slash, username, printername2, access_required));
739 result = rpccli_spoolss_open_printer_ex(pipe_hnd, mem_ctx, printername2,
741 pipe_hnd->csrv_name_slash, username, hnd);
743 /* be more verbose */
744 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
745 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
746 printername2, pipe_hnd->csrv_name_slash, username);
750 if (!W_ERROR_IS_OK(result)) {
751 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
752 printername2, pipe_hnd->csrv_name_slash, win_errstr(result));
756 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
757 printername2, pipe_hnd->csrv_name_slash));
762 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
766 PRINTER_INFO_CTR *ctr)
770 /* getprinter call */
771 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx, hnd, level, ctr);
773 if (!W_ERROR_IS_OK(result)) {
774 printf("cannot get printer-info: %s\n", win_errstr(result));
781 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
785 PRINTER_INFO_CTR *ctr)
789 /* setprinter call */
790 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, hnd, level, ctr, 0);
792 if (!W_ERROR_IS_OK(result)) {
793 printf("cannot set printer-info: %s\n", win_errstr(result));
801 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
804 REGISTRY_VALUE *value)
808 /* setprinterdata call */
809 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
811 if (!W_ERROR_IS_OK(result)) {
812 printf ("unable to set printerdata: %s\n", win_errstr(result));
820 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
828 /* enumprinterkey call */
829 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
831 if (!W_ERROR_IS_OK(result)) {
832 printf("enumprinterkey failed: %s\n", win_errstr(result));
839 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
848 /* enumprinterdataex call */
849 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
851 if (!W_ERROR_IS_OK(result)) {
852 printf("enumprinterdataex failed: %s\n", win_errstr(result));
860 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
864 REGISTRY_VALUE *value)
868 /* setprinterdataex call */
869 result = rpccli_spoolss_setprinterdataex(pipe_hnd, mem_ctx, hnd,
872 if (!W_ERROR_IS_OK(result)) {
873 printf("could not set printerdataex: %s\n", win_errstr(result));
880 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
890 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
892 if (!W_ERROR_IS_OK(result)) {
893 printf("could not enum forms: %s\n", win_errstr(result));
900 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
902 uint32 level, const char *env,
904 PRINTER_DRIVER_CTR *ctr)
908 /* enumprinterdrivers call */
909 result = rpccli_spoolss_enumprinterdrivers(
910 pipe_hnd, mem_ctx, level,
911 env, num_drivers, ctr);
913 if (!W_ERROR_IS_OK(result)) {
914 printf("cannot enum drivers: %s\n", win_errstr(result));
921 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
923 POLICY_HND *hnd, uint32 level,
924 const char *env, int version,
925 PRINTER_DRIVER_CTR *ctr)
929 /* getprinterdriver call */
930 result = rpccli_spoolss_getprinterdriver(
931 pipe_hnd, mem_ctx, hnd, level,
934 if (!W_ERROR_IS_OK(result)) {
935 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
936 env, win_errstr(result)));
937 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
938 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
939 printf("cannot get driver: %s\n", win_errstr(result));
948 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
949 TALLOC_CTX *mem_ctx, uint32 level,
950 PRINTER_DRIVER_CTR *ctr)
954 /* addprinterdriver call */
955 result = rpccli_spoolss_addprinterdriver(pipe_hnd, mem_ctx, level, ctr);
957 /* be more verbose */
958 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
959 printf("You are not allowed to add drivers\n");
962 if (!W_ERROR_IS_OK(result)) {
963 printf("cannot add driver: %s\n", win_errstr(result));
971 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
972 * for a single printer or for all printers depending on argc/argv
975 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
980 uint32 *num_printers,
981 PRINTER_INFO_CTR *ctr)
986 /* no arguments given, enumerate all printers */
989 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
990 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
991 level, num_printers, ctr))
998 /* argument given, get a single printer by name */
999 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1000 MAXIMUM_ALLOWED_ACCESS,
1001 pipe_hnd->auth->user_name,
1005 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, ctr)) {
1006 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1010 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1015 DEBUG(3,("got %d printers\n", *num_printers));
1022 * List print-queues (including local printers that are not shared)
1024 * All parameters are provided by the run_rpc_command function, except for
1025 * argc, argv which are passed through.
1027 * @param c A net_context structure
1028 * @param domain_sid The domain sid aquired from the remote server
1029 * @param cli A cli_state connected to the server.
1030 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1031 * @param argc Standard main() style argc
1032 * @param argv Standard main() style argv. Initial components are already
1035 * @return Normal NTSTATUS return.
1038 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1039 const DOM_SID *domain_sid,
1040 const char *domain_name,
1041 struct cli_state *cli,
1042 struct rpc_pipe_client *pipe_hnd,
1043 TALLOC_CTX *mem_ctx,
1047 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1048 uint32 i, num_printers;
1050 char *printername, *sharename;
1051 PRINTER_INFO_CTR ctr;
1053 printf("listing printers\n");
1055 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1058 for (i = 0; i < num_printers; i++) {
1059 /* do some initialization */
1060 rpcstr_pull_talloc(mem_ctx,
1062 ctr.printers_2[i].printername.buffer,
1065 rpcstr_pull_talloc(mem_ctx,
1067 ctr.printers_2[i].sharename.buffer,
1071 if (printername && sharename) {
1072 d_printf("printer %d: %s, shared as: %s\n",
1073 i+1, printername, sharename);
1077 return NT_STATUS_OK;
1081 * List printer-drivers from a server
1083 * All parameters are provided by the run_rpc_command function, except for
1084 * argc, argv which are passed through.
1086 * @param c A net_context structure
1087 * @param domain_sid The domain sid aquired from the remote server
1088 * @param cli A cli_state connected to the server.
1089 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1090 * @param argc Standard main() style argc
1091 * @param argv Standard main() style argv. Initial components are already
1094 * @return Normal NTSTATUS return.
1097 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1098 const DOM_SID *domain_sid,
1099 const char *domain_name,
1100 struct cli_state *cli,
1101 struct rpc_pipe_client *pipe_hnd,
1102 TALLOC_CTX *mem_ctx,
1106 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1109 PRINTER_DRIVER_CTR drv_ctr_enum;
1112 ZERO_STRUCT(drv_ctr_enum);
1114 printf("listing printer-drivers\n");
1116 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1120 /* enum remote drivers */
1121 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1122 archi_table[i].long_archi,
1123 &num_drivers, &drv_ctr_enum)) {
1124 nt_status = NT_STATUS_UNSUCCESSFUL;
1128 if (num_drivers == 0) {
1129 d_printf ("no drivers found on server for architecture: [%s].\n",
1130 archi_table[i].long_archi);
1134 d_printf("got %d printer-drivers for architecture: [%s]\n",
1135 num_drivers, archi_table[i].long_archi);
1138 /* do something for all drivers for architecture */
1139 for (d = 0; d < num_drivers; d++) {
1140 display_print_driver_3(&(drv_ctr_enum.info3[d]));
1144 nt_status = NT_STATUS_OK;
1152 * Publish print-queues with args-wrapper
1154 * @param cli A cli_state connected to the server.
1155 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1156 * @param argc Standard main() style argc
1157 * @param argv Standard main() style argv. Initial components are already
1161 * @return Normal NTSTATUS return.
1164 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1165 TALLOC_CTX *mem_ctx,
1170 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1171 uint32 i, num_printers;
1173 char *printername, *sharename;
1174 PRINTER_INFO_CTR ctr, ctr_pub;
1176 bool got_hnd = false;
1178 const char *action_str;
1180 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1183 for (i = 0; i < num_printers; i++) {
1184 /* do some initialization */
1185 rpcstr_pull_talloc(mem_ctx,
1187 ctr.printers_2[i].printername.buffer,
1190 rpcstr_pull_talloc(mem_ctx,
1192 ctr.printers_2[i].sharename.buffer,
1195 if (!printername || !sharename) {
1199 /* open printer handle */
1200 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1201 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1206 /* check for existing dst printer */
1207 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1210 /* check action and set string */
1212 case SPOOL_DS_PUBLISH:
1213 action_str = "published";
1215 case SPOOL_DS_UPDATE:
1216 action_str = "updated";
1218 case SPOOL_DS_UNPUBLISH:
1219 action_str = "unpublished";
1222 action_str = "unknown action";
1223 printf("unkown action: %d\n", action);
1227 ctr_pub.printers_7->action = action;
1229 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub, 0);
1230 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1231 printf("cannot set printer-info: %s\n", win_errstr(result));
1235 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1238 nt_status = NT_STATUS_OK;
1242 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1247 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1248 const DOM_SID *domain_sid,
1249 const char *domain_name,
1250 struct cli_state *cli,
1251 struct rpc_pipe_client *pipe_hnd,
1252 TALLOC_CTX *mem_ctx,
1256 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_PUBLISH);
1259 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1260 const DOM_SID *domain_sid,
1261 const char *domain_name,
1262 struct cli_state *cli,
1263 struct rpc_pipe_client *pipe_hnd,
1264 TALLOC_CTX *mem_ctx,
1268 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
1271 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1272 const DOM_SID *domain_sid,
1273 const char *domain_name,
1274 struct cli_state *cli,
1275 struct rpc_pipe_client *pipe_hnd,
1276 TALLOC_CTX *mem_ctx,
1280 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
1284 * List print-queues w.r.t. their publishing state
1286 * All parameters are provided by the run_rpc_command function, except for
1287 * argc, argv which are passed through.
1289 * @param c A net_context structure
1290 * @param domain_sid The domain sid aquired from the remote server
1291 * @param cli A cli_state connected to the server.
1292 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1293 * @param argc Standard main() style argc
1294 * @param argv Standard main() style argv. Initial components are already
1297 * @return Normal NTSTATUS return.
1300 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1301 const DOM_SID *domain_sid,
1302 const char *domain_name,
1303 struct cli_state *cli,
1304 struct rpc_pipe_client *pipe_hnd,
1305 TALLOC_CTX *mem_ctx,
1309 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1310 uint32 i, num_printers;
1312 char *printername, *sharename;
1314 PRINTER_INFO_CTR ctr, ctr_pub;
1316 bool got_hnd = false;
1319 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1322 for (i = 0; i < num_printers; i++) {
1323 ZERO_STRUCT(ctr_pub);
1325 /* do some initialization */
1326 rpcstr_pull_talloc(mem_ctx,
1328 ctr.printers_2[i].printername.buffer,
1331 rpcstr_pull_talloc(mem_ctx,
1333 ctr.printers_2[i].sharename.buffer,
1336 if (!printername || !sharename) {
1340 /* open printer handle */
1341 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1342 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1347 /* check for existing dst printer */
1348 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1351 rpcstr_pull_talloc(mem_ctx,
1353 ctr_pub.printers_7->guid.buffer,
1359 state = ctr_pub.printers_7->action;
1361 case SPOOL_DS_PUBLISH:
1362 printf("printer [%s] is published", sharename);
1364 printf(", guid: %s", guid);
1367 case SPOOL_DS_UNPUBLISH:
1368 printf("printer [%s] is unpublished\n", sharename);
1370 case SPOOL_DS_UPDATE:
1371 printf("printer [%s] is currently updating\n", sharename);
1374 printf("unkown state: %d\n", state);
1379 nt_status = NT_STATUS_OK;
1383 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1389 * Migrate Printer-ACLs from a source server to the destination server
1391 * All parameters are provided by the run_rpc_command function, except for
1392 * argc, argv which are passed through.
1394 * @param c A net_context structure
1395 * @param domain_sid The domain sid aquired from the remote server
1396 * @param cli A cli_state connected to the server.
1397 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1398 * @param argc Standard main() style argc
1399 * @param argv Standard main() style argv. Initial components are already
1402 * @return Normal NTSTATUS return.
1405 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1406 const DOM_SID *domain_sid,
1407 const char *domain_name,
1408 struct cli_state *cli,
1409 struct rpc_pipe_client *pipe_hnd,
1410 TALLOC_CTX *mem_ctx,
1414 /* TODO: what now, info2 or info3 ?
1415 convince jerry that we should add clientside setacls level 3 at least
1417 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1419 uint32 num_printers;
1421 char *printername, *sharename;
1422 bool got_hnd_src = false;
1423 bool got_hnd_dst = false;
1424 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1425 POLICY_HND hnd_src, hnd_dst;
1426 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1427 struct cli_state *cli_dst = NULL;
1429 ZERO_STRUCT(ctr_src);
1431 DEBUG(3,("copying printer ACLs\n"));
1433 /* connect destination PI_SPOOLSS */
1434 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1436 if (!NT_STATUS_IS_OK(nt_status))
1440 /* enum source printers */
1441 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1442 nt_status = NT_STATUS_UNSUCCESSFUL;
1446 if (!num_printers) {
1447 printf ("no printers found on server.\n");
1448 nt_status = NT_STATUS_OK;
1452 /* do something for all printers */
1453 for (i = 0; i < num_printers; i++) {
1454 /* do some initialization */
1455 rpcstr_pull_talloc(mem_ctx,
1457 ctr_enum.printers_2[i].printername.buffer,
1460 rpcstr_pull_talloc(mem_ctx,
1462 ctr_enum.printers_2[i].sharename.buffer,
1465 if (!printername || !sharename) {
1466 nt_status = NT_STATUS_UNSUCCESSFUL;
1470 /* we can reset NT_STATUS here because we do not
1471 get any real NT_STATUS-codes anymore from now on */
1472 nt_status = NT_STATUS_UNSUCCESSFUL;
1474 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1475 printername, sharename);
1477 /* according to msdn you have specify these access-rights
1478 to see the security descriptor
1479 - READ_CONTROL (DACL)
1480 - ACCESS_SYSTEM_SECURITY (SACL)
1483 /* open src printer handle */
1484 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1485 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1490 /* open dst printer handle */
1491 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1492 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1497 /* check for existing dst printer */
1498 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1501 /* check for existing src printer */
1502 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &ctr_src))
1505 /* Copy Security Descriptor */
1507 /* copy secdesc (info level 2) */
1508 ctr_dst.printers_2->devmode = NULL;
1509 ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
1512 display_sec_desc(ctr_dst.printers_2->secdesc);
1514 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &ctr_dst))
1517 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1520 /* close printer handles here */
1522 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1523 got_hnd_src = false;
1527 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1528 got_hnd_dst = false;
1533 nt_status = NT_STATUS_OK;
1538 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1542 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1546 cli_shutdown(cli_dst);
1552 * Migrate printer-forms from a src server to the dst server
1554 * All parameters are provided by the run_rpc_command function, except for
1555 * argc, argv which are passed through.
1557 * @param c A net_context structure
1558 * @param domain_sid The domain sid aquired from the remote server
1559 * @param cli A cli_state connected to the server.
1560 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1561 * @param argc Standard main() style argc
1562 * @param argv Standard main() style argv. Initial components are already
1565 * @return Normal NTSTATUS return.
1568 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1569 const DOM_SID *domain_sid,
1570 const char *domain_name,
1571 struct cli_state *cli,
1572 struct rpc_pipe_client *pipe_hnd,
1573 TALLOC_CTX *mem_ctx,
1577 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1580 uint32 num_printers;
1582 char *printername, *sharename;
1583 bool got_hnd_src = false;
1584 bool got_hnd_dst = false;
1585 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1586 POLICY_HND hnd_src, hnd_dst;
1587 PRINTER_INFO_CTR ctr_enum, ctr_dst;
1590 struct cli_state *cli_dst = NULL;
1592 ZERO_STRUCT(ctr_enum);
1594 DEBUG(3,("copying forms\n"));
1596 /* connect destination PI_SPOOLSS */
1597 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1599 if (!NT_STATUS_IS_OK(nt_status))
1602 /* enum src printers */
1603 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1604 nt_status = NT_STATUS_UNSUCCESSFUL;
1608 if (!num_printers) {
1609 printf ("no printers found on server.\n");
1610 nt_status = NT_STATUS_OK;
1614 /* do something for all printers */
1615 for (i = 0; i < num_printers; i++) {
1616 /* do some initialization */
1617 rpcstr_pull_talloc(mem_ctx,
1619 ctr_enum.printers_2[i].printername.buffer,
1622 rpcstr_pull_talloc(mem_ctx,
1624 ctr_enum.printers_2[i].sharename.buffer,
1627 if (!printername || !sharename) {
1628 nt_status = NT_STATUS_UNSUCCESSFUL;
1631 /* we can reset NT_STATUS here because we do not
1632 get any real NT_STATUS-codes anymore from now on */
1633 nt_status = NT_STATUS_UNSUCCESSFUL;
1635 d_printf("migrating printer forms for: [%s] / [%s]\n",
1636 printername, sharename);
1639 /* open src printer handle */
1640 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1641 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1647 /* open dst printer handle */
1648 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1649 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1655 /* check for existing dst printer */
1656 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1659 /* finally migrate forms */
1660 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1663 DEBUG(1,("got %d forms for printer\n", num_forms));
1666 for (f = 0; f < num_forms; f++) {
1668 union spoolss_AddFormInfo info;
1669 struct spoolss_AddFormInfo1 info1;
1673 /* only migrate FORM_PRINTER types, according to jerry
1674 FORM_BUILTIN-types are hard-coded in samba */
1675 if (forms[f].flag != FORM_PRINTER)
1678 if (forms[f].name.buffer)
1679 rpcstr_pull(form_name, forms[f].name.buffer,
1680 sizeof(form_name), -1, STR_TERMINATE);
1683 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1684 f, form_name, forms[f].flag);
1686 /* is there a more elegant way to do that ? */
1687 info1.flags = FORM_PRINTER;
1688 info1.size.width = forms[f].width;
1689 info1.size.height = forms[f].length;
1690 info1.area.left = forms[f].left;
1691 info1.area.top = forms[f].top;
1692 info1.area.right = forms[f].right;
1693 info1.area.bottom = forms[f].bottom;
1694 info1.form_name = form_name;
1696 info.info1 = &info1;
1698 /* FIXME: there might be something wrong with samba's
1700 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1705 if (!W_ERROR_IS_OK(result)) {
1706 d_printf("\tAddForm form %d: [%s] refused.\n",
1711 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1715 /* close printer handles here */
1717 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1718 got_hnd_src = false;
1722 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1723 got_hnd_dst = false;
1727 nt_status = NT_STATUS_OK;
1732 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1735 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1738 cli_shutdown(cli_dst);
1744 * Migrate printer-drivers from a src server to the dst server
1746 * All parameters are provided by the run_rpc_command function, except for
1747 * argc, argv which are passed through.
1749 * @param c A net_context structure
1750 * @param domain_sid The domain sid aquired from the remote server
1751 * @param cli A cli_state connected to the server.
1752 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1753 * @param argc Standard main() style argc
1754 * @param argv Standard main() style argv. Initial components are already
1757 * @return Normal NTSTATUS return.
1760 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1761 const DOM_SID *domain_sid,
1762 const char *domain_name,
1763 struct cli_state *cli,
1764 struct rpc_pipe_client *pipe_hnd,
1765 TALLOC_CTX *mem_ctx,
1769 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1771 uint32 num_printers;
1773 char *printername, *sharename;
1774 bool got_hnd_src = false;
1775 bool got_hnd_dst = false;
1776 bool got_src_driver_share = false;
1777 bool got_dst_driver_share = false;
1778 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1779 POLICY_HND hnd_src, hnd_dst;
1780 PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
1781 PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1782 struct cli_state *cli_dst = NULL;
1783 struct cli_state *cli_share_src = NULL;
1784 struct cli_state *cli_share_dst = NULL;
1785 fstring drivername = "";
1787 ZERO_STRUCT(drv_ctr_src);
1788 ZERO_STRUCT(drv_ctr_dst);
1789 ZERO_STRUCT(info_ctr_enum);
1790 ZERO_STRUCT(info_ctr_dst);
1792 DEBUG(3,("copying printer-drivers\n"));
1794 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1796 if (!NT_STATUS_IS_OK(nt_status))
1799 /* open print$-share on the src server */
1800 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1801 cli->desthost, "print$", "A:");
1802 if (!NT_STATUS_IS_OK(nt_status))
1805 got_src_driver_share = true;
1808 /* open print$-share on the dst server */
1809 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1810 cli_dst->desthost, "print$", "A:");
1811 if (!NT_STATUS_IS_OK(nt_status))
1814 got_dst_driver_share = true;
1817 /* enum src printers */
1818 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1819 nt_status = NT_STATUS_UNSUCCESSFUL;
1823 if (num_printers == 0) {
1824 printf ("no printers found on server.\n");
1825 nt_status = NT_STATUS_OK;
1830 /* do something for all printers */
1831 for (p = 0; p < num_printers; p++) {
1832 /* do some initialization */
1833 rpcstr_pull_talloc(mem_ctx,
1835 info_ctr_enum.printers_2[p].printername.buffer,
1838 rpcstr_pull_talloc(mem_ctx,
1840 info_ctr_enum.printers_2[p].sharename.buffer,
1843 if (!printername || !sharename) {
1844 nt_status = NT_STATUS_UNSUCCESSFUL;
1848 /* we can reset NT_STATUS here because we do not
1849 get any real NT_STATUS-codes anymore from now on */
1850 nt_status = NT_STATUS_UNSUCCESSFUL;
1852 d_printf("migrating printer driver for: [%s] / [%s]\n",
1853 printername, sharename);
1855 /* open dst printer handle */
1856 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1857 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1862 /* check for existing dst printer */
1863 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst))
1867 /* open src printer handle */
1868 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1869 MAXIMUM_ALLOWED_ACCESS,
1870 pipe_hnd->auth->user_name,
1877 /* in a first step call getdriver for each shared printer (per arch)
1878 to get a list of all files that have to be copied */
1880 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1883 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1884 level, archi_table[i].long_archi,
1885 archi_table[i].version, &drv_ctr_src))
1888 rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer,
1889 sizeof(drivername), -1, STR_TERMINATE);
1892 display_print_driver_3(drv_ctr_src.info3);
1895 /* check arch dir */
1896 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1897 if (!NT_STATUS_IS_OK(nt_status))
1901 /* copy driver-files */
1902 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1903 archi_table[i].short_archi,
1905 if (!NT_STATUS_IS_OK(nt_status))
1910 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_ctr_src)) {
1911 nt_status = NT_STATUS_UNSUCCESSFUL;
1915 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1916 drivername, printername));
1920 if (strlen(drivername) == 0) {
1921 DEBUGADD(1,("Did not get driver for printer %s\n",
1927 init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
1929 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) {
1930 nt_status = NT_STATUS_UNSUCCESSFUL;
1934 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1935 drivername, printername));
1939 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1940 got_hnd_dst = false;
1945 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1946 got_hnd_src = false;
1950 nt_status = NT_STATUS_OK;
1955 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1958 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1961 cli_shutdown(cli_dst);
1964 if (got_src_driver_share)
1965 cli_shutdown(cli_share_src);
1967 if (got_dst_driver_share)
1968 cli_shutdown(cli_share_dst);
1975 * Migrate printer-queues from a src to the dst server
1976 * (requires a working "addprinter command" to be installed for the local smbd)
1978 * All parameters are provided by the run_rpc_command function, except for
1979 * argc, argv which are passed through.
1981 * @param c A net_context structure
1982 * @param domain_sid The domain sid aquired from the remote server
1983 * @param cli A cli_state connected to the server.
1984 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1985 * @param argc Standard main() style argc
1986 * @param argv Standard main() style argv. Initial components are already
1989 * @return Normal NTSTATUS return.
1992 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
1993 const DOM_SID *domain_sid,
1994 const char *domain_name,
1995 struct cli_state *cli,
1996 struct rpc_pipe_client *pipe_hnd,
1997 TALLOC_CTX *mem_ctx,
2002 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2003 uint32 i = 0, num_printers;
2005 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
2006 struct cli_state *cli_dst = NULL;
2007 POLICY_HND hnd_dst, hnd_src;
2008 char *printername, *sharename;
2009 bool got_hnd_src = false;
2010 bool got_hnd_dst = false;
2011 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2013 DEBUG(3,("copying printers\n"));
2015 /* connect destination PI_SPOOLSS */
2016 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2018 if (!NT_STATUS_IS_OK(nt_status))
2022 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2023 nt_status = NT_STATUS_UNSUCCESSFUL;
2027 if (!num_printers) {
2028 printf ("no printers found on server.\n");
2029 nt_status = NT_STATUS_OK;
2033 /* do something for all printers */
2034 for (i = 0; i < num_printers; i++) {
2035 /* do some initialization */
2036 rpcstr_pull_talloc(mem_ctx,
2038 ctr_enum.printers_2[i].printername.buffer,
2041 rpcstr_pull_talloc(mem_ctx,
2043 ctr_enum.printers_2[i].sharename.buffer,
2046 if (!printername || !sharename) {
2047 nt_status = NT_STATUS_UNSUCCESSFUL;
2050 /* we can reset NT_STATUS here because we do not
2051 get any real NT_STATUS-codes anymore from now on */
2052 nt_status = NT_STATUS_UNSUCCESSFUL;
2054 d_printf("migrating printer queue for: [%s] / [%s]\n",
2055 printername, sharename);
2057 /* open dst printer handle */
2058 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2059 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2061 DEBUG(1,("could not open printer: %s\n", sharename));
2066 /* check for existing dst printer */
2067 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) {
2068 printf ("could not get printer, creating printer.\n");
2070 DEBUG(1,("printer already exists: %s\n", sharename));
2071 /* close printer handle here - dst only, not got src yet. */
2073 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2074 got_hnd_dst = false;
2079 /* now get again src printer ctr via getprinter,
2080 we first need a handle for that */
2082 /* open src printer handle */
2083 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2084 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2089 /* getprinter on the src server */
2090 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &ctr_src))
2093 /* copy each src printer to a dst printer 1:1,
2094 maybe some values have to be changed though */
2095 d_printf("creating printer: %s\n", printername);
2096 result = rpccli_spoolss_addprinterex (pipe_hnd_dst, mem_ctx, level, &ctr_src);
2098 if (W_ERROR_IS_OK(result))
2099 d_printf ("printer [%s] successfully added.\n", printername);
2100 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2101 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2103 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2107 /* close printer handles here */
2109 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2110 got_hnd_src = false;
2114 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2115 got_hnd_dst = false;
2119 nt_status = NT_STATUS_OK;
2123 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2126 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2129 cli_shutdown(cli_dst);
2135 * Migrate Printer-Settings from a src server to the dst server
2136 * (for this to work, printers and drivers already have to be migrated earlier)
2138 * All parameters are provided by the run_rpc_command function, except for
2139 * argc, argv which are passed through.
2141 * @param c A net_context structure
2142 * @param domain_sid The domain sid aquired from the remote server
2143 * @param cli A cli_state connected to the server.
2144 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2145 * @param argc Standard main() style argc
2146 * @param argv Standard main() style argv. Initial components are already
2149 * @return Normal NTSTATUS return.
2152 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2153 const DOM_SID *domain_sid,
2154 const char *domain_name,
2155 struct cli_state *cli,
2156 struct rpc_pipe_client *pipe_hnd,
2157 TALLOC_CTX *mem_ctx,
2162 /* FIXME: Here the nightmare begins */
2165 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2166 uint32 i = 0, p = 0, j = 0;
2167 uint32 num_printers, val_needed, data_needed;
2169 char *printername, *sharename;
2170 bool got_hnd_src = false;
2171 bool got_hnd_dst = false;
2172 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2173 POLICY_HND hnd_src, hnd_dst;
2174 PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
2175 REGVAL_CTR *reg_ctr;
2176 struct cli_state *cli_dst = NULL;
2177 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2178 const char *longname;
2180 uint16 *keylist = NULL, *curkey;
2182 ZERO_STRUCT(ctr_enum);
2184 DEBUG(3,("copying printer settings\n"));
2186 /* connect destination PI_SPOOLSS */
2187 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2189 if (!NT_STATUS_IS_OK(nt_status))
2192 /* enum src printers */
2193 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2194 nt_status = NT_STATUS_UNSUCCESSFUL;
2198 if (!num_printers) {
2199 printf ("no printers found on server.\n");
2200 nt_status = NT_STATUS_OK;
2205 /* needed for dns-strings in regkeys */
2206 longname = get_mydnsfullname();
2208 nt_status = NT_STATUS_UNSUCCESSFUL;
2212 /* do something for all printers */
2213 for (i = 0; i < num_printers; i++) {
2214 /* do some initialization */
2215 rpcstr_pull_talloc(mem_ctx,
2217 ctr_enum.printers_2[i].printername.buffer,
2220 rpcstr_pull_talloc(mem_ctx,
2222 ctr_enum.printers_2[i].sharename.buffer,
2225 if (!printername || !sharename) {
2226 nt_status = NT_STATUS_UNSUCCESSFUL;
2229 /* we can reset NT_STATUS here because we do not
2230 get any real NT_STATUS-codes anymore from now on */
2231 nt_status = NT_STATUS_UNSUCCESSFUL;
2233 d_printf("migrating printer settings for: [%s] / [%s]\n",
2234 printername, sharename);
2237 /* open src printer handle */
2238 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2239 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2245 /* open dst printer handle */
2246 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2247 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2253 /* check for existing dst printer */
2254 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2259 /* STEP 1: COPY DEVICE-MODE and other
2260 PRINTER_INFO_2-attributes
2263 ctr_dst.printers_2 = &ctr_enum.printers_2[i];
2265 /* why is the port always disconnected when the printer
2266 is correctly installed (incl. driver ???) */
2267 init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
2269 /* check if printer is published */
2270 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2272 /* check for existing dst printer */
2273 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
2276 ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
2278 /* ignore false from setprinter due to WERR_IO_PENDING */
2279 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
2281 DEBUG(3,("republished printer\n"));
2284 if (ctr_enum.printers_2[i].devmode != NULL) {
2286 /* copy devmode (info level 2) */
2287 ctr_dst.printers_2->devmode = (DEVICEMODE *)
2288 TALLOC_MEMDUP(mem_ctx,
2289 ctr_enum.printers_2[i].devmode,
2290 sizeof(DEVICEMODE));
2292 /* do not copy security descriptor (we have another
2293 * command for that) */
2294 ctr_dst.printers_2->secdesc = NULL;
2297 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2299 nt_status = NT_STATUS_NO_MEMORY;
2303 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2306 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2310 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2313 /* STEP 2: COPY REGISTRY VALUES */
2315 /* please keep in mind that samba parse_spools gives horribly
2316 crippled results when used to rpccli_spoolss_enumprinterdataex
2317 a win2k3-server. (Bugzilla #1851)
2318 FIXME: IIRC I've seen it too on a win2k-server
2321 /* enumerate data on src handle */
2322 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2323 &val_needed, &data_needed, NULL);
2325 /* loop for all printerdata of "PrinterDriverData" */
2326 while (W_ERROR_IS_OK(result)) {
2328 REGISTRY_VALUE value;
2330 result = rpccli_spoolss_enumprinterdata(
2331 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2332 data_needed, 0, 0, &value);
2334 /* loop for all reg_keys */
2335 if (W_ERROR_IS_OK(result)) {
2339 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2342 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2346 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2351 /* STEP 3: COPY SUBKEY VALUES */
2353 /* here we need to enum all printer_keys and then work
2354 on the result with enum_printer_key_ex. nt4 does not
2355 respond to enumprinterkey, win2k does, so continue
2356 in case of an error */
2358 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2359 printf("got no key-data\n");
2364 /* work on a list of printer keys
2365 each key has to be enumerated to get all required
2366 information. information is then set via setprinterdataex-calls */
2368 if (keylist == NULL)
2372 while (*curkey != 0) {
2374 rpcstr_pull_talloc(mem_ctx,
2380 return NT_STATUS_NO_MEMORY;
2383 curkey += strlen(subkey) + 1;
2385 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2386 return NT_STATUS_NO_MEMORY;
2388 /* enumerate all src subkeys */
2389 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2394 for (j=0; j < reg_ctr->num_values; j++) {
2396 REGISTRY_VALUE value;
2399 /* although samba replies with sane data in most cases we
2400 should try to avoid writing wrong registry data */
2402 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2403 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2404 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2405 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2406 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2408 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2410 /* although windows uses a multi-sz, we use a sz */
2411 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2412 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2415 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2417 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2418 nt_status = NT_STATUS_NO_MEMORY;
2421 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2422 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2425 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2430 /* FIXME: should we really do that ??? */
2431 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2432 nt_status = NT_STATUS_NO_MEMORY;
2435 init_unistr2(&data, url, UNI_STR_TERMINATE);
2436 fstrcpy(value.valuename, SPOOL_REG_URL);
2440 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2442 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2443 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2446 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2448 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2449 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2452 value.type = REG_SZ;
2453 value.size = data.uni_str_len * 2;
2455 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2457 value.data_p = NULL;
2461 display_reg_value(subkey, value);
2463 /* here we have to set all subkeys on the dst server */
2464 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2471 display_reg_value(subkey, *(reg_ctr->values[j]));
2473 /* here we have to set all subkeys on the dst server */
2474 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2475 subkey, reg_ctr->values[j]))
2480 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2481 subkey, reg_ctr->values[j]->valuename));
2485 TALLOC_FREE( reg_ctr );
2490 /* close printer handles here */
2492 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2493 got_hnd_src = false;
2497 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2498 got_hnd_dst = false;
2503 nt_status = NT_STATUS_OK;
2506 SAFE_FREE(devicename);
2508 SAFE_FREE(unc_name);
2511 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2514 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2517 cli_shutdown(cli_dst);