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_driver3(struct spoolss_DriverInfo3 *r)
61 printf("Printer Driver Info 3:\n");
62 printf("\tVersion: [%x]\n", r->version);
63 printf("\tDriver Name: [%s]\n", r->driver_name);
64 printf("\tArchitecture: [%s]\n", r->architecture);
65 printf("\tDriver Path: [%s]\n", r->driver_path);
66 printf("\tDatafile: [%s]\n", r->data_file);
67 printf("\tConfigfile: [%s]\n\n", r->config_file);
68 printf("\tHelpfile: [%s]\n\n", r->help_file);
70 for (i=0; r->dependent_files[i] != NULL; i++) {
71 printf("\tDependentfiles: [%s]\n", r->dependent_files[i]);
76 printf("\tMonitorname: [%s]\n", r->monitor_name);
77 printf("\tDefaultdatatype: [%s]\n\n", r->default_datatype);
80 static void display_reg_value(const char *subkey, REGISTRY_VALUE value)
86 d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename,
87 *((uint32 *) value.data_p));
91 rpcstr_pull_talloc(talloc_tos(),
99 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
103 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
104 subkey, value.valuename);
108 uint32 i, num_values;
111 if (!W_ERROR_IS_OK(reg_pull_multi_sz(NULL, value.data_p,
112 value.size, &num_values,
114 d_printf("reg_pull_multi_sz failed\n");
118 for (i=0; i<num_values; i++) {
119 d_printf("%s\n", values[i]);
126 d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
132 * Copies ACLs, DOS-attributes and timestamps from one
133 * file or directory from one connected share to another connected share
135 * @param c A net_context structure
136 * @param mem_ctx A talloc-context
137 * @param cli_share_src A connected cli_state
138 * @param cli_share_dst A connected cli_state
139 * @param src_file The source file-name
140 * @param dst_file The destination file-name
141 * @param copy_acls Whether to copy acls
142 * @param copy_attrs Whether to copy DOS attributes
143 * @param copy_timestamps Whether to preserve timestamps
144 * @param is_file Whether this file is a file or a dir
146 * @return Normal NTSTATUS return.
149 NTSTATUS net_copy_fileattr(struct net_context *c,
151 struct cli_state *cli_share_src,
152 struct cli_state *cli_share_dst,
153 const char *src_name, const char *dst_name,
154 bool copy_acls, bool copy_attrs,
155 bool copy_timestamps, bool is_file)
157 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
162 time_t f_atime, f_ctime, f_mtime;
165 if (!copy_timestamps && !copy_acls && !copy_attrs)
168 /* open file/dir on the originating server */
170 DEBUGADD(3,("opening %s %s on originating server\n",
171 is_file?"file":"dir", src_name));
173 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
174 if (fnum_src == -1) {
175 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
176 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
177 nt_status = cli_nt_error(cli_share_src);
184 /* get the security descriptor */
185 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
187 DEBUG(0,("failed to get security descriptor: %s\n",
188 cli_errstr(cli_share_src)));
189 nt_status = cli_nt_error(cli_share_src);
193 if (c->opt_verbose && DEBUGLEVEL >= 3)
194 display_sec_desc(sd);
198 if (copy_attrs || copy_timestamps) {
200 /* get file attributes */
201 if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
202 &f_ctime, &f_atime, &f_mtime)) {
203 DEBUG(0,("failed to get file-attrs: %s\n",
204 cli_errstr(cli_share_src)));
205 nt_status = cli_nt_error(cli_share_src);
211 /* open the file/dir on the destination server */
213 fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
214 if (fnum_dst == -1) {
215 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
216 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
217 nt_status = cli_nt_error(cli_share_dst);
221 if (copy_timestamps) {
224 if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
225 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
226 cli_errstr(cli_share_dst)));
227 nt_status = cli_nt_error(cli_share_dst);
235 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
236 DEBUG(0,("could not set secdesc on %s: %s\n",
237 dst_name, cli_errstr(cli_share_dst)));
238 nt_status = cli_nt_error(cli_share_dst);
246 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
247 DEBUG(0,("failed to set file-attrs: %s\n",
248 cli_errstr(cli_share_dst)));
249 nt_status = cli_nt_error(cli_share_dst);
257 if (!cli_close(cli_share_src, fnum_src)) {
258 d_fprintf(stderr, "could not close %s on originating server: %s\n",
259 is_file?"file":"dir", cli_errstr(cli_share_src));
260 nt_status = cli_nt_error(cli_share_src);
264 if (!cli_close(cli_share_dst, fnum_dst)) {
265 d_fprintf(stderr, "could not close %s on destination server: %s\n",
266 is_file?"file":"dir", cli_errstr(cli_share_dst));
267 nt_status = cli_nt_error(cli_share_dst);
272 nt_status = NT_STATUS_OK;
278 cli_close(cli_share_src, fnum_src);
281 cli_close(cli_share_dst, fnum_dst);
287 * Copy a file or directory from a connected share to another connected share
289 * @param c A net_context structure
290 * @param mem_ctx A talloc-context
291 * @param cli_share_src A connected cli_state
292 * @param cli_share_dst A connected cli_state
293 * @param src_file The source file-name
294 * @param dst_file The destination file-name
295 * @param copy_acls Whether to copy acls
296 * @param copy_attrs Whether to copy DOS attributes
297 * @param copy_timestamps Whether to preserve timestamps
298 * @param is_file Whether this file is a file or a dir
300 * @return Normal NTSTATUS return.
303 NTSTATUS net_copy_file(struct net_context *c,
305 struct cli_state *cli_share_src,
306 struct cli_state *cli_share_dst,
307 const char *src_name, const char *dst_name,
308 bool copy_acls, bool copy_attrs,
309 bool copy_timestamps, bool is_file)
311 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
314 static int io_bufsize = 64512;
315 int read_size = io_bufsize;
320 if (!src_name || !dst_name)
323 if (cli_share_src == NULL || cli_share_dst == NULL)
326 /* open on the originating server */
327 DEBUGADD(3,("opening %s %s on originating server\n",
328 is_file ? "file":"dir", src_name));
330 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
332 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
334 if (fnum_src == -1) {
335 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
336 is_file ? "file":"dir",
337 src_name, cli_errstr(cli_share_src)));
338 nt_status = cli_nt_error(cli_share_src);
345 /* open file on the destination server */
346 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
347 fnum_dst = cli_open(cli_share_dst, dst_name,
348 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
350 if (fnum_dst == -1) {
351 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
352 dst_name, cli_errstr(cli_share_dst)));
353 nt_status = cli_nt_error(cli_share_dst);
357 /* allocate memory */
358 if (!(data = (char *)SMB_MALLOC(read_size))) {
359 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
360 nt_status = NT_STATUS_NO_MEMORY;
367 if (c->opt_verbose) {
369 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
370 "%s ACLs and %s DOS Attributes %s\n",
371 cli_share_src->desthost, cli_share_src->share, src_name,
372 cli_share_dst->desthost, cli_share_dst->share, dst_name,
373 copy_acls ? "with" : "without",
374 copy_attrs ? "with" : "without",
375 copy_timestamps ? "(preserving timestamps)" : "" );
383 n = cli_read(cli_share_src, fnum_src, data, nread,
389 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
393 d_fprintf(stderr, "Error writing file: %s\n",
394 cli_errstr(cli_share_dst));
395 nt_status = cli_nt_error(cli_share_dst);
403 if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
406 DEBUGADD(3,("creating dir %s on the destination server\n",
409 if (!cli_mkdir(cli_share_dst, dst_name)) {
410 DEBUG(0,("cannot create directory %s: %s\n",
411 dst_name, cli_errstr(cli_share_dst)));
412 nt_status = NT_STATUS_NO_SUCH_FILE;
415 if (!cli_chkpath(cli_share_dst, dst_name)) {
416 d_fprintf(stderr, "cannot check for directory %s: %s\n",
417 dst_name, cli_errstr(cli_share_dst));
424 if (!cli_close(cli_share_src, fnum_src)) {
425 d_fprintf(stderr, "could not close file on originating server: %s\n",
426 cli_errstr(cli_share_src));
427 nt_status = cli_nt_error(cli_share_src);
431 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
432 d_fprintf(stderr, "could not close file on destination server: %s\n",
433 cli_errstr(cli_share_dst));
434 nt_status = cli_nt_error(cli_share_dst);
438 /* possibly we have to copy some file-attributes / acls / sd */
439 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
440 src_name, dst_name, copy_acls,
441 copy_attrs, copy_timestamps, is_file);
442 if (!NT_STATUS_IS_OK(nt_status))
446 nt_status = NT_STATUS_OK;
452 cli_close(cli_share_src, fnum_src);
455 cli_close(cli_share_dst, fnum_dst);
463 * Copy a driverfile from on connected share to another connected share
464 * This silently assumes that a driver-file is picked up from
466 * \\src_server\print$\{arch}\{version}\file
470 * \\dst_server\print$\{arch}\file
472 * to be added via setdriver-calls later.
473 * @param c A net_context structure
474 * @param mem_ctx A talloc-context
475 * @param cli_share_src A cli_state connected to source print$-share
476 * @param cli_share_dst A cli_state connected to destination print$-share
477 * @param file The file-name to be copied
478 * @param short_archi The name of the driver-architecture (short form)
480 * @return Normal NTSTATUS return.
483 static NTSTATUS net_copy_driverfile(struct net_context *c,
485 struct cli_state *cli_share_src,
486 struct cli_state *cli_share_dst,
487 const char *file, const char *short_archi) {
489 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
501 /* scroll through the file until we have the part
502 beyond archi_table.short_archi */
504 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
505 if (strequal(tok, short_archi)) {
506 next_token_talloc(mem_ctx, &p, &version, "\\");
507 next_token_talloc(mem_ctx, &p, &filename, "\\");
511 /* build source file name */
512 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
513 return NT_STATUS_NO_MEMORY;
516 /* create destination file name */
517 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
518 return NT_STATUS_NO_MEMORY;
521 /* finally copy the file */
522 nt_status = net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
523 src_name, dst_name, false, false, false, true);
524 if (!NT_STATUS_IS_OK(nt_status))
527 nt_status = NT_STATUS_OK;
537 * Check for existing Architecture directory on a given server
539 * @param cli_share A cli_state connected to a print$-share
540 * @param short_archi The Architecture for the print-driver
542 * @return Normal NTSTATUS return.
545 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
548 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
551 if (asprintf(&dir, "\\%s", short_archi) < 0) {
552 return NT_STATUS_NO_MEMORY;
555 DEBUG(10,("creating print-driver dir for architecture: %s\n",
558 if (!cli_mkdir(cli_share, dir)) {
559 DEBUG(1,("cannot create directory %s: %s\n",
560 dir, cli_errstr(cli_share)));
561 nt_status = NT_STATUS_NO_SUCH_FILE;
564 if (!cli_chkpath(cli_share, dir)) {
565 d_fprintf(stderr, "cannot check %s: %s\n",
566 dir, cli_errstr(cli_share));
570 nt_status = NT_STATUS_OK;
578 * Copy a print-driver (level 3) from one connected print$-share to another
579 * connected print$-share
581 * @param c A net_context structure
582 * @param mem_ctx A talloc-context
583 * @param cli_share_src A cli_state connected to a print$-share
584 * @param cli_share_dst A cli_state connected to a print$-share
585 * @param short_archi The Architecture for the print-driver
586 * @param i1 The DRIVER_INFO_3-struct
588 * @return Normal NTSTATUS return.
591 static NTSTATUS copy_print_driver_3(struct net_context *c,
593 struct cli_state *cli_share_src,
594 struct cli_state *cli_share_dst,
595 const char *short_archi,
596 struct spoolss_DriverInfo3 *r)
598 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
606 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
607 r->driver_name, short_archi, r->version);
609 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
610 r->driver_path, short_archi);
611 if (!NT_STATUS_IS_OK(nt_status))
614 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
615 r->data_file, short_archi);
616 if (!NT_STATUS_IS_OK(nt_status))
619 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
620 r->config_file, short_archi);
621 if (!NT_STATUS_IS_OK(nt_status))
624 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
625 r->help_file, short_archi);
626 if (!NT_STATUS_IS_OK(nt_status))
629 for (i=0; r->dependent_files[i] != NULL; i++) {
631 nt_status = net_copy_driverfile(c, mem_ctx,
632 cli_share_src, cli_share_dst,
633 r->dependent_files[i], short_archi);
634 if (!NT_STATUS_IS_OK(nt_status)) {
643 * net_spoolss-functions
644 * =====================
646 * the net_spoolss-functions aim to simplify spoolss-client-functions
647 * required during the migration-process wrt buffer-sizes, returned
650 * this greatly reduces the complexitiy of the migrate-functions.
654 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
659 uint32 *num_printers,
660 union spoolss_PrinterInfo **info)
666 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
673 if (!W_ERROR_IS_OK(result)) {
674 printf("cannot enum printers: %s\n", win_errstr(result));
681 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
683 const char *printername,
684 uint32 access_required,
685 const char *username,
689 fstring printername2;
691 fstrcpy(printername2, pipe_hnd->srv_name_slash);
692 fstrcat(printername2, "\\");
693 fstrcat(printername2, printername);
695 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
696 pipe_hnd->srv_name_slash, username, printername2, access_required));
699 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
704 /* be more verbose */
705 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
706 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
707 printername2, pipe_hnd->srv_name_slash, username);
711 if (!W_ERROR_IS_OK(result)) {
712 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
713 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
717 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
718 printername2, pipe_hnd->srv_name_slash));
723 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
727 union spoolss_PrinterInfo *info)
731 /* getprinter call */
732 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
737 if (!W_ERROR_IS_OK(result)) {
738 printf("cannot get printer-info: %s\n", win_errstr(result));
745 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
749 union spoolss_PrinterInfo *info)
753 struct spoolss_SetPrinterInfoCtr info_ctr;
754 struct spoolss_DevmodeContainer devmode_ctr;
755 struct sec_desc_buf secdesc_ctr;
757 ZERO_STRUCT(devmode_ctr);
758 ZERO_STRUCT(secdesc_ctr);
760 /* setprinter call */
762 info_ctr.level = level;
765 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)&info->info0;
768 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)&info->info1;
771 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info->info2;
774 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)&info->info3;
777 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)&info->info4;
780 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)&info->info5;
783 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)&info->info6;
786 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info->info7;
790 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)&info->info8;
793 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)&info->info9;
800 status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
808 if (!W_ERROR_IS_OK(result)) {
809 printf("cannot set printer-info: %s\n", win_errstr(result));
817 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
820 REGISTRY_VALUE *value)
824 /* setprinterdata call */
825 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
827 if (!W_ERROR_IS_OK(result)) {
828 printf ("unable to set printerdata: %s\n", win_errstr(result));
836 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
844 /* enumprinterkey call */
845 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
847 if (!W_ERROR_IS_OK(result)) {
848 printf("enumprinterkey failed: %s\n", win_errstr(result));
855 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
864 /* enumprinterdataex call */
865 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
867 if (!W_ERROR_IS_OK(result)) {
868 printf("enumprinterdataex failed: %s\n", win_errstr(result));
876 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
880 REGISTRY_VALUE *value)
885 /* setprinterdataex call */
886 status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
895 if (!W_ERROR_IS_OK(result)) {
896 printf("could not set printerdataex: %s\n", win_errstr(result));
903 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
908 union spoolss_FormInfo **forms)
913 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
919 if (!W_ERROR_IS_OK(result)) {
920 printf("could not enum forms: %s\n", win_errstr(result));
927 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
929 uint32 level, const char *env,
931 union spoolss_DriverInfo **info)
935 /* enumprinterdrivers call */
936 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
937 pipe_hnd->srv_name_slash,
943 if (!W_ERROR_IS_OK(result)) {
944 printf("cannot enum drivers: %s\n", win_errstr(result));
951 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
953 POLICY_HND *hnd, uint32 level,
954 const char *env, int version,
955 union spoolss_DriverInfo *info)
958 uint32_t server_major_version;
959 uint32_t server_minor_version;
961 /* getprinterdriver call */
962 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
970 &server_major_version,
971 &server_minor_version);
972 if (!W_ERROR_IS_OK(result)) {
973 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
974 env, win_errstr(result)));
975 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
976 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
977 printf("cannot get driver: %s\n", win_errstr(result));
986 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
987 TALLOC_CTX *mem_ctx, uint32 level,
988 union spoolss_DriverInfo *info)
992 struct spoolss_AddDriverInfoCtr info_ctr;
994 info_ctr.level = level;
998 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)&info->info2;
1001 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)&info->info3;
1004 printf("unsupported info level: %d\n", level);
1008 /* addprinterdriver call */
1009 status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1010 pipe_hnd->srv_name_slash,
1013 /* be more verbose */
1014 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1015 printf("You are not allowed to add drivers\n");
1018 if (!W_ERROR_IS_OK(result)) {
1019 printf("cannot add driver: %s\n", win_errstr(result));
1027 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
1028 * for a single printer or for all printers depending on argc/argv
1031 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1032 TALLOC_CTX *mem_ctx,
1036 uint32 *num_printers,
1037 union spoolss_PrinterInfo **info_p)
1041 /* no arguments given, enumerate all printers */
1044 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1045 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1046 level, num_printers, info_p))
1052 /* argument given, get a single printer by name */
1053 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1054 MAXIMUM_ALLOWED_ACCESS,
1055 pipe_hnd->auth->user_name,
1059 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1060 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1064 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1069 DEBUG(3,("got %d printers\n", *num_printers));
1076 * List print-queues (including local printers that are not shared)
1078 * All parameters are provided by the run_rpc_command function, except for
1079 * argc, argv which are passed through.
1081 * @param c A net_context structure
1082 * @param domain_sid The domain sid aquired from the remote server
1083 * @param cli A cli_state connected to the server.
1084 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1085 * @param argc Standard main() style argc
1086 * @param argv Standard main() style argv. Initial components are already
1089 * @return Normal NTSTATUS return.
1092 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1093 const DOM_SID *domain_sid,
1094 const char *domain_name,
1095 struct cli_state *cli,
1096 struct rpc_pipe_client *pipe_hnd,
1097 TALLOC_CTX *mem_ctx,
1101 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1102 uint32 i, num_printers;
1104 const char *printername, *sharename;
1105 union spoolss_PrinterInfo *info;
1107 printf("listing printers\n");
1109 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1112 for (i = 0; i < num_printers; i++) {
1114 /* do some initialization */
1115 printername = info[i].info2.printername;
1116 sharename = info[i].info2.sharename;
1118 if (printername && sharename) {
1119 d_printf("printer %d: %s, shared as: %s\n",
1120 i+1, printername, sharename);
1124 return NT_STATUS_OK;
1128 * List printer-drivers from a server
1130 * All parameters are provided by the run_rpc_command function, except for
1131 * argc, argv which are passed through.
1133 * @param c A net_context structure
1134 * @param domain_sid The domain sid aquired from the remote server
1135 * @param cli A cli_state connected to the server.
1136 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1137 * @param argc Standard main() style argc
1138 * @param argv Standard main() style argv. Initial components are already
1141 * @return Normal NTSTATUS return.
1144 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1145 const DOM_SID *domain_sid,
1146 const char *domain_name,
1147 struct cli_state *cli,
1148 struct rpc_pipe_client *pipe_hnd,
1149 TALLOC_CTX *mem_ctx,
1153 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1156 union spoolss_DriverInfo *info;
1159 printf("listing printer-drivers\n");
1161 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1165 /* enum remote drivers */
1166 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1167 archi_table[i].long_archi,
1168 &num_drivers, &info)) {
1169 nt_status = NT_STATUS_UNSUCCESSFUL;
1173 if (num_drivers == 0) {
1174 d_printf ("no drivers found on server for architecture: [%s].\n",
1175 archi_table[i].long_archi);
1179 d_printf("got %d printer-drivers for architecture: [%s]\n",
1180 num_drivers, archi_table[i].long_archi);
1183 /* do something for all drivers for architecture */
1184 for (d = 0; d < num_drivers; d++) {
1185 display_print_driver3(&info[d].info3);
1189 nt_status = NT_STATUS_OK;
1197 * Publish print-queues with args-wrapper
1199 * @param cli A cli_state connected to the server.
1200 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1201 * @param argc Standard main() style argc
1202 * @param argv Standard main() style argv. Initial components are already
1206 * @return Normal NTSTATUS return.
1209 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1210 TALLOC_CTX *mem_ctx,
1215 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1216 uint32 i, num_printers;
1218 const char *printername, *sharename;
1219 union spoolss_PrinterInfo *info_enum;
1220 union spoolss_PrinterInfo info;
1221 struct spoolss_SetPrinterInfoCtr info_ctr;
1222 struct spoolss_DevmodeContainer devmode_ctr;
1223 struct sec_desc_buf secdesc_ctr;
1226 const char *action_str;
1228 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1231 for (i = 0; i < num_printers; i++) {
1233 /* do some initialization */
1234 printername = info_enum[i].info2.printername;
1235 sharename = info_enum[i].info2.sharename;
1236 if (!printername || !sharename) {
1240 /* open printer handle */
1241 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1242 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1245 /* check for existing dst printer */
1246 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1249 /* check action and set string */
1251 case DSPRINT_PUBLISH:
1252 action_str = "published";
1254 case DSPRINT_UPDATE:
1255 action_str = "updated";
1257 case DSPRINT_UNPUBLISH:
1258 action_str = "unpublished";
1261 action_str = "unknown action";
1262 printf("unkown action: %d\n", action);
1266 info.info7.action = action;
1268 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info.info7;
1270 ZERO_STRUCT(devmode_ctr);
1271 ZERO_STRUCT(secdesc_ctr);
1273 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1281 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1282 printf("cannot set printer-info: %s\n", win_errstr(result));
1286 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1289 nt_status = NT_STATUS_OK;
1292 if (is_valid_policy_hnd(&hnd))
1293 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1298 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1299 const DOM_SID *domain_sid,
1300 const char *domain_name,
1301 struct cli_state *cli,
1302 struct rpc_pipe_client *pipe_hnd,
1303 TALLOC_CTX *mem_ctx,
1307 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1310 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1311 const DOM_SID *domain_sid,
1312 const char *domain_name,
1313 struct cli_state *cli,
1314 struct rpc_pipe_client *pipe_hnd,
1315 TALLOC_CTX *mem_ctx,
1319 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1322 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1323 const DOM_SID *domain_sid,
1324 const char *domain_name,
1325 struct cli_state *cli,
1326 struct rpc_pipe_client *pipe_hnd,
1327 TALLOC_CTX *mem_ctx,
1331 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1335 * List print-queues w.r.t. their publishing state
1337 * All parameters are provided by the run_rpc_command function, except for
1338 * argc, argv which are passed through.
1340 * @param c A net_context structure
1341 * @param domain_sid The domain sid aquired from the remote server
1342 * @param cli A cli_state connected to the server.
1343 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1344 * @param argc Standard main() style argc
1345 * @param argv Standard main() style argv. Initial components are already
1348 * @return Normal NTSTATUS return.
1351 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1352 const DOM_SID *domain_sid,
1353 const char *domain_name,
1354 struct cli_state *cli,
1355 struct rpc_pipe_client *pipe_hnd,
1356 TALLOC_CTX *mem_ctx,
1360 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1361 uint32 i, num_printers;
1363 const char *printername, *sharename;
1364 union spoolss_PrinterInfo *info_enum;
1365 union spoolss_PrinterInfo info;
1369 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1372 for (i = 0; i < num_printers; i++) {
1374 /* do some initialization */
1375 printername = info_enum[i].info2.printername;
1376 sharename = info_enum[i].info2.sharename;
1378 if (!printername || !sharename) {
1382 /* open printer handle */
1383 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1384 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1387 /* check for existing dst printer */
1388 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1391 if (!info.info7.guid) {
1394 state = info.info7.action;
1396 case DSPRINT_PUBLISH:
1397 printf("printer [%s] is published", sharename);
1399 printf(", guid: %s", info.info7.guid);
1402 case DSPRINT_UNPUBLISH:
1403 printf("printer [%s] is unpublished\n", sharename);
1405 case DSPRINT_UPDATE:
1406 printf("printer [%s] is currently updating\n", sharename);
1409 printf("unkown state: %d\n", state);
1414 nt_status = NT_STATUS_OK;
1417 if (is_valid_policy_hnd(&hnd))
1418 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1424 * Migrate Printer-ACLs from a source server to the destination server
1426 * All parameters are provided by the run_rpc_command function, except for
1427 * argc, argv which are passed through.
1429 * @param c A net_context structure
1430 * @param domain_sid The domain sid aquired from the remote server
1431 * @param cli A cli_state connected to the server.
1432 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1433 * @param argc Standard main() style argc
1434 * @param argv Standard main() style argv. Initial components are already
1437 * @return Normal NTSTATUS return.
1440 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1441 const DOM_SID *domain_sid,
1442 const char *domain_name,
1443 struct cli_state *cli,
1444 struct rpc_pipe_client *pipe_hnd,
1445 TALLOC_CTX *mem_ctx,
1449 /* TODO: what now, info2 or info3 ?
1450 convince jerry that we should add clientside setacls level 3 at least
1452 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1454 uint32 num_printers;
1456 const char *printername, *sharename;
1457 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1458 POLICY_HND hnd_src, hnd_dst;
1459 union spoolss_PrinterInfo *info_enum;
1460 struct cli_state *cli_dst = NULL;
1461 union spoolss_PrinterInfo info_src, info_dst;
1463 DEBUG(3,("copying printer ACLs\n"));
1465 /* connect destination PI_SPOOLSS */
1466 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1468 if (!NT_STATUS_IS_OK(nt_status))
1472 /* enum source printers */
1473 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1474 nt_status = NT_STATUS_UNSUCCESSFUL;
1478 if (!num_printers) {
1479 printf ("no printers found on server.\n");
1480 nt_status = NT_STATUS_OK;
1484 /* do something for all printers */
1485 for (i = 0; i < num_printers; i++) {
1487 /* do some initialization */
1488 printername = info_enum[i].info2.printername;
1489 sharename = info_enum[i].info2.sharename;
1491 if (!printername || !sharename) {
1492 nt_status = NT_STATUS_UNSUCCESSFUL;
1496 /* we can reset NT_STATUS here because we do not
1497 get any real NT_STATUS-codes anymore from now on */
1498 nt_status = NT_STATUS_UNSUCCESSFUL;
1500 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1501 printername, sharename);
1503 /* according to msdn you have specify these access-rights
1504 to see the security descriptor
1505 - READ_CONTROL (DACL)
1506 - ACCESS_SYSTEM_SECURITY (SACL)
1509 /* open src printer handle */
1510 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1511 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1514 /* open dst printer handle */
1515 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1516 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1519 /* check for existing dst printer */
1520 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1523 /* check for existing src printer */
1524 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1527 /* Copy Security Descriptor */
1529 /* copy secdesc (info level 2) */
1530 info_dst.info2.devmode = NULL;
1531 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1534 display_sec_desc(info_dst.info2.secdesc);
1536 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1539 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1542 /* close printer handles here */
1543 if (is_valid_policy_hnd(&hnd_src)) {
1544 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1547 if (is_valid_policy_hnd(&hnd_dst)) {
1548 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1553 nt_status = NT_STATUS_OK;
1557 if (is_valid_policy_hnd(&hnd_src)) {
1558 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1561 if (is_valid_policy_hnd(&hnd_dst)) {
1562 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1566 cli_shutdown(cli_dst);
1572 * Migrate printer-forms from a src server to the dst server
1574 * All parameters are provided by the run_rpc_command function, except for
1575 * argc, argv which are passed through.
1577 * @param c A net_context structure
1578 * @param domain_sid The domain sid aquired from the remote server
1579 * @param cli A cli_state connected to the server.
1580 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1581 * @param argc Standard main() style argc
1582 * @param argv Standard main() style argv. Initial components are already
1585 * @return Normal NTSTATUS return.
1588 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1589 const DOM_SID *domain_sid,
1590 const char *domain_name,
1591 struct cli_state *cli,
1592 struct rpc_pipe_client *pipe_hnd,
1593 TALLOC_CTX *mem_ctx,
1597 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1600 uint32 num_printers;
1602 const char *printername, *sharename;
1603 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1604 POLICY_HND hnd_src, hnd_dst;
1605 union spoolss_PrinterInfo *info_enum;
1606 union spoolss_PrinterInfo info_dst;
1608 union spoolss_FormInfo *forms;
1609 struct cli_state *cli_dst = NULL;
1611 DEBUG(3,("copying forms\n"));
1613 /* connect destination PI_SPOOLSS */
1614 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1616 if (!NT_STATUS_IS_OK(nt_status))
1619 /* enum src printers */
1620 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1621 nt_status = NT_STATUS_UNSUCCESSFUL;
1625 if (!num_printers) {
1626 printf ("no printers found on server.\n");
1627 nt_status = NT_STATUS_OK;
1631 /* do something for all printers */
1632 for (i = 0; i < num_printers; i++) {
1634 /* do some initialization */
1635 printername = info_enum[i].info2.printername;
1636 sharename = info_enum[i].info2.sharename;
1638 if (!printername || !sharename) {
1639 nt_status = NT_STATUS_UNSUCCESSFUL;
1642 /* we can reset NT_STATUS here because we do not
1643 get any real NT_STATUS-codes anymore from now on */
1644 nt_status = NT_STATUS_UNSUCCESSFUL;
1646 d_printf("migrating printer forms for: [%s] / [%s]\n",
1647 printername, sharename);
1650 /* open src printer handle */
1651 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1652 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1655 /* open dst printer handle */
1656 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1657 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1660 /* check for existing dst printer */
1661 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1664 /* finally migrate forms */
1665 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1668 DEBUG(1,("got %d forms for printer\n", num_forms));
1671 for (f = 0; f < num_forms; f++) {
1673 union spoolss_AddFormInfo info;
1676 /* only migrate FORM_PRINTER types, according to jerry
1677 FORM_BUILTIN-types are hard-coded in samba */
1678 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1682 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1683 f, forms[f].info1.form_name,
1684 forms[f].info1.flags);
1686 info.info1 = (struct spoolss_AddFormInfo1 *)&forms[f].info1;
1688 /* FIXME: there might be something wrong with samba's
1690 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1695 if (!W_ERROR_IS_OK(result)) {
1696 d_printf("\tAddForm form %d: [%s] refused.\n",
1697 f, forms[f].info1.form_name);
1701 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1702 forms[f].info1.form_name));
1706 /* close printer handles here */
1707 if (is_valid_policy_hnd(&hnd_src)) {
1708 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1711 if (is_valid_policy_hnd(&hnd_dst)) {
1712 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1716 nt_status = NT_STATUS_OK;
1720 if (is_valid_policy_hnd(&hnd_src))
1721 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1723 if (is_valid_policy_hnd(&hnd_dst))
1724 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1727 cli_shutdown(cli_dst);
1733 * Migrate printer-drivers from a src server to the dst server
1735 * All parameters are provided by the run_rpc_command function, except for
1736 * argc, argv which are passed through.
1738 * @param c A net_context structure
1739 * @param domain_sid The domain sid aquired from the remote server
1740 * @param cli A cli_state connected to the server.
1741 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1742 * @param argc Standard main() style argc
1743 * @param argv Standard main() style argv. Initial components are already
1746 * @return Normal NTSTATUS return.
1749 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1750 const DOM_SID *domain_sid,
1751 const char *domain_name,
1752 struct cli_state *cli,
1753 struct rpc_pipe_client *pipe_hnd,
1754 TALLOC_CTX *mem_ctx,
1758 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1760 uint32 num_printers;
1762 const char *printername, *sharename;
1763 bool got_src_driver_share = false;
1764 bool got_dst_driver_share = false;
1765 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1766 POLICY_HND hnd_src, hnd_dst;
1767 union spoolss_DriverInfo drv_info_src;
1768 union spoolss_PrinterInfo *info_enum;
1769 union spoolss_PrinterInfo info_dst;
1770 struct cli_state *cli_dst = NULL;
1771 struct cli_state *cli_share_src = NULL;
1772 struct cli_state *cli_share_dst = NULL;
1773 const char *drivername = NULL;
1775 DEBUG(3,("copying printer-drivers\n"));
1777 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1779 if (!NT_STATUS_IS_OK(nt_status))
1782 /* open print$-share on the src server */
1783 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1784 cli->desthost, "print$", "A:");
1785 if (!NT_STATUS_IS_OK(nt_status))
1788 got_src_driver_share = true;
1791 /* open print$-share on the dst server */
1792 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1793 cli_dst->desthost, "print$", "A:");
1794 if (!NT_STATUS_IS_OK(nt_status))
1797 got_dst_driver_share = true;
1800 /* enum src printers */
1801 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1802 nt_status = NT_STATUS_UNSUCCESSFUL;
1806 if (num_printers == 0) {
1807 printf ("no printers found on server.\n");
1808 nt_status = NT_STATUS_OK;
1813 /* do something for all printers */
1814 for (p = 0; p < num_printers; p++) {
1816 /* do some initialization */
1817 printername = info_enum[p].info2.printername;
1818 sharename = info_enum[p].info2.sharename;
1820 if (!printername || !sharename) {
1821 nt_status = NT_STATUS_UNSUCCESSFUL;
1825 /* we can reset NT_STATUS here because we do not
1826 get any real NT_STATUS-codes anymore from now on */
1827 nt_status = NT_STATUS_UNSUCCESSFUL;
1829 d_printf("migrating printer driver for: [%s] / [%s]\n",
1830 printername, sharename);
1832 /* open dst printer handle */
1833 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1834 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1837 /* check for existing dst printer */
1838 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1842 /* open src printer handle */
1843 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1844 MAXIMUM_ALLOWED_ACCESS,
1845 pipe_hnd->auth->user_name,
1849 /* in a first step call getdriver for each shared printer (per arch)
1850 to get a list of all files that have to be copied */
1852 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1855 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1856 level, archi_table[i].long_archi,
1857 archi_table[i].version, &drv_info_src))
1860 drivername = drv_info_src.info3.driver_name;
1863 display_print_driver3(&drv_info_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(c, mem_ctx, cli_share_src, cli_share_dst,
1873 archi_table[i].short_archi,
1874 &drv_info_src.info3);
1875 if (!NT_STATUS_IS_OK(nt_status))
1880 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_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 info_dst.info2.drivername = drivername;
1899 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1900 nt_status = NT_STATUS_UNSUCCESSFUL;
1904 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1905 drivername, printername));
1908 if (is_valid_policy_hnd(&hnd_dst)) {
1909 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1913 if (is_valid_policy_hnd(&hnd_src)) {
1914 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1918 nt_status = NT_STATUS_OK;
1922 if (is_valid_policy_hnd(&hnd_src))
1923 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1925 if (is_valid_policy_hnd(&hnd_dst))
1926 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1929 cli_shutdown(cli_dst);
1932 if (got_src_driver_share)
1933 cli_shutdown(cli_share_src);
1935 if (got_dst_driver_share)
1936 cli_shutdown(cli_share_dst);
1943 * Migrate printer-queues from a src to the dst server
1944 * (requires a working "addprinter command" to be installed for the local smbd)
1946 * All parameters are provided by the run_rpc_command function, except for
1947 * argc, argv which are passed through.
1949 * @param c A net_context structure
1950 * @param domain_sid The domain sid aquired from the remote server
1951 * @param cli A cli_state connected to the server.
1952 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1953 * @param argc Standard main() style argc
1954 * @param argv Standard main() style argv. Initial components are already
1957 * @return Normal NTSTATUS return.
1960 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
1961 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 union spoolss_PrinterInfo info_dst, info_src;
1974 union spoolss_PrinterInfo *info_enum;
1975 struct cli_state *cli_dst = NULL;
1976 POLICY_HND hnd_dst, hnd_src;
1977 const char *printername, *sharename;
1978 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1979 struct spoolss_SetPrinterInfoCtr info_ctr;
1981 DEBUG(3,("copying printers\n"));
1983 /* connect destination PI_SPOOLSS */
1984 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1986 if (!NT_STATUS_IS_OK(nt_status))
1990 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1991 nt_status = NT_STATUS_UNSUCCESSFUL;
1995 if (!num_printers) {
1996 printf ("no printers found on server.\n");
1997 nt_status = NT_STATUS_OK;
2001 /* do something for all printers */
2002 for (i = 0; i < num_printers; i++) {
2004 /* do some initialization */
2005 printername = info_enum[i].info2.printername;
2006 sharename = info_enum[i].info2.sharename;
2008 if (!printername || !sharename) {
2009 nt_status = NT_STATUS_UNSUCCESSFUL;
2012 /* we can reset NT_STATUS here because we do not
2013 get any real NT_STATUS-codes anymore from now on */
2014 nt_status = NT_STATUS_UNSUCCESSFUL;
2016 d_printf("migrating printer queue for: [%s] / [%s]\n",
2017 printername, sharename);
2019 /* open dst printer handle */
2020 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2021 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2023 DEBUG(1,("could not open printer: %s\n", sharename));
2026 /* check for existing dst printer */
2027 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2028 printf ("could not get printer, creating printer.\n");
2030 DEBUG(1,("printer already exists: %s\n", sharename));
2031 /* close printer handle here - dst only, not got src yet. */
2032 if (is_valid_policy_hnd(&hnd_dst)) {
2033 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2038 /* now get again src printer ctr via getprinter,
2039 we first need a handle for that */
2041 /* open src printer handle */
2042 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2043 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2046 /* getprinter on the src server */
2047 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2050 /* copy each src printer to a dst printer 1:1,
2051 maybe some values have to be changed though */
2052 d_printf("creating printer: %s\n", printername);
2054 info_ctr.level = level;
2055 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info_src.info2;
2057 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2061 if (W_ERROR_IS_OK(result))
2062 d_printf ("printer [%s] successfully added.\n", printername);
2063 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2064 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2066 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2070 /* close printer handles here */
2071 if (is_valid_policy_hnd(&hnd_src)) {
2072 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2075 if (is_valid_policy_hnd(&hnd_dst)) {
2076 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2080 nt_status = NT_STATUS_OK;
2083 if (is_valid_policy_hnd(&hnd_src))
2084 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2086 if (is_valid_policy_hnd(&hnd_dst))
2087 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2090 cli_shutdown(cli_dst);
2096 * Migrate Printer-Settings from a src server to the dst server
2097 * (for this to work, printers and drivers already have to be migrated earlier)
2099 * All parameters are provided by the run_rpc_command function, except for
2100 * argc, argv which are passed through.
2102 * @param c A net_context structure
2103 * @param domain_sid The domain sid aquired from the remote server
2104 * @param cli A cli_state connected to the server.
2105 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2106 * @param argc Standard main() style argc
2107 * @param argv Standard main() style argv. Initial components are already
2110 * @return Normal NTSTATUS return.
2113 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2114 const DOM_SID *domain_sid,
2115 const char *domain_name,
2116 struct cli_state *cli,
2117 struct rpc_pipe_client *pipe_hnd,
2118 TALLOC_CTX *mem_ctx,
2123 /* FIXME: Here the nightmare begins */
2126 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2127 uint32 i = 0, p = 0, j = 0;
2128 uint32 num_printers, val_needed, data_needed;
2130 const char *printername, *sharename;
2131 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2132 POLICY_HND hnd_src, hnd_dst;
2133 union spoolss_PrinterInfo *info_enum;
2134 union spoolss_PrinterInfo info_dst_publish;
2135 union spoolss_PrinterInfo info_dst;
2136 REGVAL_CTR *reg_ctr;
2137 struct cli_state *cli_dst = NULL;
2138 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2139 const char *longname;
2141 uint16 *keylist = NULL, *curkey;
2144 ZERO_STRUCT(info_dst_publish);
2146 DEBUG(3,("copying printer settings\n"));
2148 /* connect destination PI_SPOOLSS */
2149 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2151 if (!NT_STATUS_IS_OK(nt_status))
2154 /* enum src printers */
2155 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2156 nt_status = NT_STATUS_UNSUCCESSFUL;
2160 if (!num_printers) {
2161 printf ("no printers found on server.\n");
2162 nt_status = NT_STATUS_OK;
2167 /* needed for dns-strings in regkeys */
2168 longname = get_mydnsfullname();
2170 nt_status = NT_STATUS_UNSUCCESSFUL;
2174 /* do something for all printers */
2175 for (i = 0; i < num_printers; i++) {
2177 /* do some initialization */
2178 printername = info_enum[i].info2.printername;
2179 sharename = info_enum[i].info2.sharename;
2181 if (!printername || !sharename) {
2182 nt_status = NT_STATUS_UNSUCCESSFUL;
2185 /* we can reset NT_STATUS here because we do not
2186 get any real NT_STATUS-codes anymore from now on */
2187 nt_status = NT_STATUS_UNSUCCESSFUL;
2189 d_printf("migrating printer settings for: [%s] / [%s]\n",
2190 printername, sharename);
2193 /* open src printer handle */
2194 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2195 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2198 /* open dst printer handle */
2199 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2200 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2203 /* check for existing dst printer */
2204 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2209 /* STEP 1: COPY DEVICE-MODE and other
2210 PRINTER_INFO_2-attributes
2213 info_dst.info2 = info_enum[i].info2;
2215 /* why is the port always disconnected when the printer
2216 is correctly installed (incl. driver ???) */
2217 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2219 /* check if printer is published */
2220 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2222 /* check for existing dst printer */
2223 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2226 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2228 /* ignore false from setprinter due to WERR_IO_PENDING */
2229 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2231 DEBUG(3,("republished printer\n"));
2234 if (info_enum[i].info2.devmode != NULL) {
2236 /* copy devmode (info level 2) */
2237 info_dst.info2.devmode = info_enum[i].info2.devmode;
2239 /* do not copy security descriptor (we have another
2240 * command for that) */
2241 info_dst.info2.secdesc = NULL;
2244 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2246 nt_status = NT_STATUS_NO_MEMORY;
2250 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2253 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2257 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2260 /* STEP 2: COPY REGISTRY VALUES */
2262 /* please keep in mind that samba parse_spools gives horribly
2263 crippled results when used to rpccli_spoolss_enumprinterdataex
2264 a win2k3-server. (Bugzilla #1851)
2265 FIXME: IIRC I've seen it too on a win2k-server
2268 /* enumerate data on src handle */
2269 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2270 &val_needed, &data_needed, NULL);
2272 /* loop for all printerdata of "PrinterDriverData" */
2273 while (W_ERROR_IS_OK(result)) {
2275 REGISTRY_VALUE value;
2277 result = rpccli_spoolss_enumprinterdata(
2278 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2279 data_needed, 0, 0, &value);
2281 /* loop for all reg_keys */
2282 if (W_ERROR_IS_OK(result)) {
2286 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2289 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2293 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2298 /* STEP 3: COPY SUBKEY VALUES */
2300 /* here we need to enum all printer_keys and then work
2301 on the result with enum_printer_key_ex. nt4 does not
2302 respond to enumprinterkey, win2k does, so continue
2303 in case of an error */
2305 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2306 printf("got no key-data\n");
2311 /* work on a list of printer keys
2312 each key has to be enumerated to get all required
2313 information. information is then set via setprinterdataex-calls */
2315 if (keylist == NULL)
2319 while (*curkey != 0) {
2321 rpcstr_pull_talloc(mem_ctx,
2327 return NT_STATUS_NO_MEMORY;
2330 curkey += strlen(subkey) + 1;
2332 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2333 return NT_STATUS_NO_MEMORY;
2335 /* enumerate all src subkeys */
2336 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2341 for (j=0; j < reg_ctr->num_values; j++) {
2343 REGISTRY_VALUE value;
2346 /* although samba replies with sane data in most cases we
2347 should try to avoid writing wrong registry data */
2349 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2350 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2351 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2352 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2353 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2355 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2357 /* although windows uses a multi-sz, we use a sz */
2358 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2359 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2362 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2364 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2365 nt_status = NT_STATUS_NO_MEMORY;
2368 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2369 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2372 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2377 /* FIXME: should we really do that ??? */
2378 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2379 nt_status = NT_STATUS_NO_MEMORY;
2382 init_unistr2(&data, url, UNI_STR_TERMINATE);
2383 fstrcpy(value.valuename, SPOOL_REG_URL);
2387 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2389 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2390 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2393 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2395 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2396 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2399 value.type = REG_SZ;
2400 value.size = data.uni_str_len * 2;
2402 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2404 value.data_p = NULL;
2408 display_reg_value(subkey, value);
2410 /* here we have to set all subkeys on the dst server */
2411 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2418 display_reg_value(subkey, *(reg_ctr->values[j]));
2420 /* here we have to set all subkeys on the dst server */
2421 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2422 subkey, reg_ctr->values[j]))
2427 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2428 subkey, reg_ctr->values[j]->valuename));
2432 TALLOC_FREE( reg_ctr );
2437 /* close printer handles here */
2438 if (is_valid_policy_hnd(&hnd_src)) {
2439 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2442 if (is_valid_policy_hnd(&hnd_dst)) {
2443 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2448 nt_status = NT_STATUS_OK;
2451 SAFE_FREE(devicename);
2453 SAFE_FREE(unc_name);
2455 if (is_valid_policy_hnd(&hnd_src))
2456 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2458 if (is_valid_policy_hnd(&hnd_dst))
2459 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2462 cli_shutdown(cli_dst);