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)
827 /* setprinterdata call */
828 /* result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value); */
830 if (!W_ERROR_IS_OK(result)) {
831 printf ("unable to set printerdata: %s\n", win_errstr(result));
839 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
847 /* enumprinterkey call */
848 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
850 if (!W_ERROR_IS_OK(result)) {
851 printf("enumprinterkey failed: %s\n", win_errstr(result));
858 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
867 /* enumprinterdataex call */
868 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
870 if (!W_ERROR_IS_OK(result)) {
871 printf("enumprinterdataex failed: %s\n", win_errstr(result));
879 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
883 REGISTRY_VALUE *value)
888 /* setprinterdataex call */
889 status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
898 if (!W_ERROR_IS_OK(result)) {
899 printf("could not set printerdataex: %s\n", win_errstr(result));
906 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
911 union spoolss_FormInfo **forms)
916 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
922 if (!W_ERROR_IS_OK(result)) {
923 printf("could not enum forms: %s\n", win_errstr(result));
930 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
932 uint32 level, const char *env,
934 union spoolss_DriverInfo **info)
938 /* enumprinterdrivers call */
939 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
940 pipe_hnd->srv_name_slash,
946 if (!W_ERROR_IS_OK(result)) {
947 printf("cannot enum drivers: %s\n", win_errstr(result));
954 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
956 POLICY_HND *hnd, uint32 level,
957 const char *env, int version,
958 union spoolss_DriverInfo *info)
961 uint32_t server_major_version;
962 uint32_t server_minor_version;
964 /* getprinterdriver call */
965 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
973 &server_major_version,
974 &server_minor_version);
975 if (!W_ERROR_IS_OK(result)) {
976 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
977 env, win_errstr(result)));
978 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
979 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
980 printf("cannot get driver: %s\n", win_errstr(result));
989 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
990 TALLOC_CTX *mem_ctx, uint32 level,
991 union spoolss_DriverInfo *info)
995 struct spoolss_AddDriverInfoCtr info_ctr;
997 info_ctr.level = level;
1001 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)&info->info2;
1004 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)&info->info3;
1007 printf("unsupported info level: %d\n", level);
1011 /* addprinterdriver call */
1012 status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1013 pipe_hnd->srv_name_slash,
1016 /* be more verbose */
1017 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1018 printf("You are not allowed to add drivers\n");
1021 if (!W_ERROR_IS_OK(result)) {
1022 printf("cannot add driver: %s\n", win_errstr(result));
1030 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
1031 * for a single printer or for all printers depending on argc/argv
1034 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1035 TALLOC_CTX *mem_ctx,
1039 uint32 *num_printers,
1040 union spoolss_PrinterInfo **info_p)
1044 /* no arguments given, enumerate all printers */
1047 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1048 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1049 level, num_printers, info_p))
1055 /* argument given, get a single printer by name */
1056 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1057 MAXIMUM_ALLOWED_ACCESS,
1058 pipe_hnd->auth->user_name,
1062 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1063 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1067 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1072 DEBUG(3,("got %d printers\n", *num_printers));
1079 * List print-queues (including local printers that are not shared)
1081 * All parameters are provided by the run_rpc_command function, except for
1082 * argc, argv which are passed through.
1084 * @param c A net_context structure
1085 * @param domain_sid The domain sid aquired from the remote server
1086 * @param cli A cli_state connected to the server.
1087 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1088 * @param argc Standard main() style argc
1089 * @param argv Standard main() style argv. Initial components are already
1092 * @return Normal NTSTATUS return.
1095 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1096 const DOM_SID *domain_sid,
1097 const char *domain_name,
1098 struct cli_state *cli,
1099 struct rpc_pipe_client *pipe_hnd,
1100 TALLOC_CTX *mem_ctx,
1104 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1105 uint32 i, num_printers;
1107 const char *printername, *sharename;
1108 union spoolss_PrinterInfo *info;
1110 printf("listing printers\n");
1112 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1115 for (i = 0; i < num_printers; i++) {
1117 /* do some initialization */
1118 printername = info[i].info2.printername;
1119 sharename = info[i].info2.sharename;
1121 if (printername && sharename) {
1122 d_printf("printer %d: %s, shared as: %s\n",
1123 i+1, printername, sharename);
1127 return NT_STATUS_OK;
1131 * List printer-drivers from a server
1133 * All parameters are provided by the run_rpc_command function, except for
1134 * argc, argv which are passed through.
1136 * @param c A net_context structure
1137 * @param domain_sid The domain sid aquired from the remote server
1138 * @param cli A cli_state connected to the server.
1139 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1140 * @param argc Standard main() style argc
1141 * @param argv Standard main() style argv. Initial components are already
1144 * @return Normal NTSTATUS return.
1147 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1148 const DOM_SID *domain_sid,
1149 const char *domain_name,
1150 struct cli_state *cli,
1151 struct rpc_pipe_client *pipe_hnd,
1152 TALLOC_CTX *mem_ctx,
1156 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1159 union spoolss_DriverInfo *info;
1162 printf("listing printer-drivers\n");
1164 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1168 /* enum remote drivers */
1169 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1170 archi_table[i].long_archi,
1171 &num_drivers, &info)) {
1172 nt_status = NT_STATUS_UNSUCCESSFUL;
1176 if (num_drivers == 0) {
1177 d_printf ("no drivers found on server for architecture: [%s].\n",
1178 archi_table[i].long_archi);
1182 d_printf("got %d printer-drivers for architecture: [%s]\n",
1183 num_drivers, archi_table[i].long_archi);
1186 /* do something for all drivers for architecture */
1187 for (d = 0; d < num_drivers; d++) {
1188 display_print_driver3(&info[d].info3);
1192 nt_status = NT_STATUS_OK;
1200 * Publish print-queues with args-wrapper
1202 * @param cli A cli_state connected to the server.
1203 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1204 * @param argc Standard main() style argc
1205 * @param argv Standard main() style argv. Initial components are already
1209 * @return Normal NTSTATUS return.
1212 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1213 TALLOC_CTX *mem_ctx,
1218 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1219 uint32 i, num_printers;
1221 const char *printername, *sharename;
1222 union spoolss_PrinterInfo *info_enum;
1223 union spoolss_PrinterInfo info;
1224 struct spoolss_SetPrinterInfoCtr info_ctr;
1225 struct spoolss_DevmodeContainer devmode_ctr;
1226 struct sec_desc_buf secdesc_ctr;
1229 const char *action_str;
1231 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1234 for (i = 0; i < num_printers; i++) {
1236 /* do some initialization */
1237 printername = info_enum[i].info2.printername;
1238 sharename = info_enum[i].info2.sharename;
1239 if (!printername || !sharename) {
1243 /* open printer handle */
1244 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1245 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1248 /* check for existing dst printer */
1249 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1252 /* check action and set string */
1254 case DSPRINT_PUBLISH:
1255 action_str = "published";
1257 case DSPRINT_UPDATE:
1258 action_str = "updated";
1260 case DSPRINT_UNPUBLISH:
1261 action_str = "unpublished";
1264 action_str = "unknown action";
1265 printf("unkown action: %d\n", action);
1269 info.info7.action = action;
1271 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info.info7;
1273 ZERO_STRUCT(devmode_ctr);
1274 ZERO_STRUCT(secdesc_ctr);
1276 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1284 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1285 printf("cannot set printer-info: %s\n", win_errstr(result));
1289 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1292 nt_status = NT_STATUS_OK;
1295 if (is_valid_policy_hnd(&hnd))
1296 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1301 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1302 const DOM_SID *domain_sid,
1303 const char *domain_name,
1304 struct cli_state *cli,
1305 struct rpc_pipe_client *pipe_hnd,
1306 TALLOC_CTX *mem_ctx,
1310 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1313 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1314 const DOM_SID *domain_sid,
1315 const char *domain_name,
1316 struct cli_state *cli,
1317 struct rpc_pipe_client *pipe_hnd,
1318 TALLOC_CTX *mem_ctx,
1322 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1325 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1326 const DOM_SID *domain_sid,
1327 const char *domain_name,
1328 struct cli_state *cli,
1329 struct rpc_pipe_client *pipe_hnd,
1330 TALLOC_CTX *mem_ctx,
1334 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1338 * List print-queues w.r.t. their publishing state
1340 * All parameters are provided by the run_rpc_command function, except for
1341 * argc, argv which are passed through.
1343 * @param c A net_context structure
1344 * @param domain_sid The domain sid aquired from the remote server
1345 * @param cli A cli_state connected to the server.
1346 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1347 * @param argc Standard main() style argc
1348 * @param argv Standard main() style argv. Initial components are already
1351 * @return Normal NTSTATUS return.
1354 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1355 const DOM_SID *domain_sid,
1356 const char *domain_name,
1357 struct cli_state *cli,
1358 struct rpc_pipe_client *pipe_hnd,
1359 TALLOC_CTX *mem_ctx,
1363 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1364 uint32 i, num_printers;
1366 const char *printername, *sharename;
1367 union spoolss_PrinterInfo *info_enum;
1368 union spoolss_PrinterInfo info;
1372 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1375 for (i = 0; i < num_printers; i++) {
1377 /* do some initialization */
1378 printername = info_enum[i].info2.printername;
1379 sharename = info_enum[i].info2.sharename;
1381 if (!printername || !sharename) {
1385 /* open printer handle */
1386 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1387 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1390 /* check for existing dst printer */
1391 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1394 if (!info.info7.guid) {
1397 state = info.info7.action;
1399 case DSPRINT_PUBLISH:
1400 printf("printer [%s] is published", sharename);
1402 printf(", guid: %s", info.info7.guid);
1405 case DSPRINT_UNPUBLISH:
1406 printf("printer [%s] is unpublished\n", sharename);
1408 case DSPRINT_UPDATE:
1409 printf("printer [%s] is currently updating\n", sharename);
1412 printf("unkown state: %d\n", state);
1417 nt_status = NT_STATUS_OK;
1420 if (is_valid_policy_hnd(&hnd))
1421 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1427 * Migrate Printer-ACLs from a source server to the destination server
1429 * All parameters are provided by the run_rpc_command function, except for
1430 * argc, argv which are passed through.
1432 * @param c A net_context structure
1433 * @param domain_sid The domain sid aquired from the remote server
1434 * @param cli A cli_state connected to the server.
1435 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1436 * @param argc Standard main() style argc
1437 * @param argv Standard main() style argv. Initial components are already
1440 * @return Normal NTSTATUS return.
1443 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1444 const DOM_SID *domain_sid,
1445 const char *domain_name,
1446 struct cli_state *cli,
1447 struct rpc_pipe_client *pipe_hnd,
1448 TALLOC_CTX *mem_ctx,
1452 /* TODO: what now, info2 or info3 ?
1453 convince jerry that we should add clientside setacls level 3 at least
1455 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1457 uint32 num_printers;
1459 const char *printername, *sharename;
1460 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1461 POLICY_HND hnd_src, hnd_dst;
1462 union spoolss_PrinterInfo *info_enum;
1463 struct cli_state *cli_dst = NULL;
1464 union spoolss_PrinterInfo info_src, info_dst;
1466 DEBUG(3,("copying printer ACLs\n"));
1468 /* connect destination PI_SPOOLSS */
1469 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1471 if (!NT_STATUS_IS_OK(nt_status))
1475 /* enum source printers */
1476 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1477 nt_status = NT_STATUS_UNSUCCESSFUL;
1481 if (!num_printers) {
1482 printf ("no printers found on server.\n");
1483 nt_status = NT_STATUS_OK;
1487 /* do something for all printers */
1488 for (i = 0; i < num_printers; i++) {
1490 /* do some initialization */
1491 printername = info_enum[i].info2.printername;
1492 sharename = info_enum[i].info2.sharename;
1494 if (!printername || !sharename) {
1495 nt_status = NT_STATUS_UNSUCCESSFUL;
1499 /* we can reset NT_STATUS here because we do not
1500 get any real NT_STATUS-codes anymore from now on */
1501 nt_status = NT_STATUS_UNSUCCESSFUL;
1503 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1504 printername, sharename);
1506 /* according to msdn you have specify these access-rights
1507 to see the security descriptor
1508 - READ_CONTROL (DACL)
1509 - ACCESS_SYSTEM_SECURITY (SACL)
1512 /* open src printer handle */
1513 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1514 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1517 /* open dst printer handle */
1518 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1519 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1522 /* check for existing dst printer */
1523 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1526 /* check for existing src printer */
1527 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1530 /* Copy Security Descriptor */
1532 /* copy secdesc (info level 2) */
1533 info_dst.info2.devmode = NULL;
1534 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1537 display_sec_desc(info_dst.info2.secdesc);
1539 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1542 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1545 /* close printer handles here */
1546 if (is_valid_policy_hnd(&hnd_src)) {
1547 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1550 if (is_valid_policy_hnd(&hnd_dst)) {
1551 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1556 nt_status = NT_STATUS_OK;
1560 if (is_valid_policy_hnd(&hnd_src)) {
1561 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1564 if (is_valid_policy_hnd(&hnd_dst)) {
1565 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1569 cli_shutdown(cli_dst);
1575 * Migrate printer-forms from a src server to the dst server
1577 * All parameters are provided by the run_rpc_command function, except for
1578 * argc, argv which are passed through.
1580 * @param c A net_context structure
1581 * @param domain_sid The domain sid aquired from the remote server
1582 * @param cli A cli_state connected to the server.
1583 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1584 * @param argc Standard main() style argc
1585 * @param argv Standard main() style argv. Initial components are already
1588 * @return Normal NTSTATUS return.
1591 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1592 const DOM_SID *domain_sid,
1593 const char *domain_name,
1594 struct cli_state *cli,
1595 struct rpc_pipe_client *pipe_hnd,
1596 TALLOC_CTX *mem_ctx,
1600 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1603 uint32 num_printers;
1605 const char *printername, *sharename;
1606 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1607 POLICY_HND hnd_src, hnd_dst;
1608 union spoolss_PrinterInfo *info_enum;
1609 union spoolss_PrinterInfo info_dst;
1611 union spoolss_FormInfo *forms;
1612 struct cli_state *cli_dst = NULL;
1614 DEBUG(3,("copying forms\n"));
1616 /* connect destination PI_SPOOLSS */
1617 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1619 if (!NT_STATUS_IS_OK(nt_status))
1622 /* enum src printers */
1623 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1624 nt_status = NT_STATUS_UNSUCCESSFUL;
1628 if (!num_printers) {
1629 printf ("no printers found on server.\n");
1630 nt_status = NT_STATUS_OK;
1634 /* do something for all printers */
1635 for (i = 0; i < num_printers; i++) {
1637 /* do some initialization */
1638 printername = info_enum[i].info2.printername;
1639 sharename = info_enum[i].info2.sharename;
1641 if (!printername || !sharename) {
1642 nt_status = NT_STATUS_UNSUCCESSFUL;
1645 /* we can reset NT_STATUS here because we do not
1646 get any real NT_STATUS-codes anymore from now on */
1647 nt_status = NT_STATUS_UNSUCCESSFUL;
1649 d_printf("migrating printer forms for: [%s] / [%s]\n",
1650 printername, sharename);
1653 /* open src printer handle */
1654 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1655 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1658 /* open dst printer handle */
1659 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1660 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1663 /* check for existing dst printer */
1664 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1667 /* finally migrate forms */
1668 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1671 DEBUG(1,("got %d forms for printer\n", num_forms));
1674 for (f = 0; f < num_forms; f++) {
1676 union spoolss_AddFormInfo info;
1679 /* only migrate FORM_PRINTER types, according to jerry
1680 FORM_BUILTIN-types are hard-coded in samba */
1681 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1685 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1686 f, forms[f].info1.form_name,
1687 forms[f].info1.flags);
1689 info.info1 = (struct spoolss_AddFormInfo1 *)&forms[f].info1;
1691 /* FIXME: there might be something wrong with samba's
1693 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1698 if (!W_ERROR_IS_OK(result)) {
1699 d_printf("\tAddForm form %d: [%s] refused.\n",
1700 f, forms[f].info1.form_name);
1704 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1705 forms[f].info1.form_name));
1709 /* close printer handles here */
1710 if (is_valid_policy_hnd(&hnd_src)) {
1711 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1714 if (is_valid_policy_hnd(&hnd_dst)) {
1715 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1719 nt_status = NT_STATUS_OK;
1723 if (is_valid_policy_hnd(&hnd_src))
1724 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1726 if (is_valid_policy_hnd(&hnd_dst))
1727 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1730 cli_shutdown(cli_dst);
1736 * Migrate printer-drivers from a src server to the dst server
1738 * All parameters are provided by the run_rpc_command function, except for
1739 * argc, argv which are passed through.
1741 * @param c A net_context structure
1742 * @param domain_sid The domain sid aquired from the remote server
1743 * @param cli A cli_state connected to the server.
1744 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1745 * @param argc Standard main() style argc
1746 * @param argv Standard main() style argv. Initial components are already
1749 * @return Normal NTSTATUS return.
1752 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1753 const DOM_SID *domain_sid,
1754 const char *domain_name,
1755 struct cli_state *cli,
1756 struct rpc_pipe_client *pipe_hnd,
1757 TALLOC_CTX *mem_ctx,
1761 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1763 uint32 num_printers;
1765 const char *printername, *sharename;
1766 bool got_src_driver_share = false;
1767 bool got_dst_driver_share = false;
1768 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1769 POLICY_HND hnd_src, hnd_dst;
1770 union spoolss_DriverInfo drv_info_src;
1771 union spoolss_PrinterInfo *info_enum;
1772 union spoolss_PrinterInfo info_dst;
1773 struct cli_state *cli_dst = NULL;
1774 struct cli_state *cli_share_src = NULL;
1775 struct cli_state *cli_share_dst = NULL;
1776 const char *drivername = NULL;
1778 DEBUG(3,("copying printer-drivers\n"));
1780 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1782 if (!NT_STATUS_IS_OK(nt_status))
1785 /* open print$-share on the src server */
1786 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1787 cli->desthost, "print$", "A:");
1788 if (!NT_STATUS_IS_OK(nt_status))
1791 got_src_driver_share = true;
1794 /* open print$-share on the dst server */
1795 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1796 cli_dst->desthost, "print$", "A:");
1797 if (!NT_STATUS_IS_OK(nt_status))
1800 got_dst_driver_share = true;
1803 /* enum src printers */
1804 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1805 nt_status = NT_STATUS_UNSUCCESSFUL;
1809 if (num_printers == 0) {
1810 printf ("no printers found on server.\n");
1811 nt_status = NT_STATUS_OK;
1816 /* do something for all printers */
1817 for (p = 0; p < num_printers; p++) {
1819 /* do some initialization */
1820 printername = info_enum[p].info2.printername;
1821 sharename = info_enum[p].info2.sharename;
1823 if (!printername || !sharename) {
1824 nt_status = NT_STATUS_UNSUCCESSFUL;
1828 /* we can reset NT_STATUS here because we do not
1829 get any real NT_STATUS-codes anymore from now on */
1830 nt_status = NT_STATUS_UNSUCCESSFUL;
1832 d_printf("migrating printer driver for: [%s] / [%s]\n",
1833 printername, sharename);
1835 /* open dst printer handle */
1836 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1837 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1840 /* check for existing dst printer */
1841 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1845 /* open src printer handle */
1846 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1847 MAXIMUM_ALLOWED_ACCESS,
1848 pipe_hnd->auth->user_name,
1852 /* in a first step call getdriver for each shared printer (per arch)
1853 to get a list of all files that have to be copied */
1855 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1858 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1859 level, archi_table[i].long_archi,
1860 archi_table[i].version, &drv_info_src))
1863 drivername = drv_info_src.info3.driver_name;
1866 display_print_driver3(&drv_info_src.info3);
1868 /* check arch dir */
1869 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1870 if (!NT_STATUS_IS_OK(nt_status))
1874 /* copy driver-files */
1875 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1876 archi_table[i].short_archi,
1877 &drv_info_src.info3);
1878 if (!NT_STATUS_IS_OK(nt_status))
1883 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1884 nt_status = NT_STATUS_UNSUCCESSFUL;
1888 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1889 drivername, printername));
1893 if (strlen(drivername) == 0) {
1894 DEBUGADD(1,("Did not get driver for printer %s\n",
1900 info_dst.info2.drivername = drivername;
1902 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1903 nt_status = NT_STATUS_UNSUCCESSFUL;
1907 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1908 drivername, printername));
1911 if (is_valid_policy_hnd(&hnd_dst)) {
1912 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1916 if (is_valid_policy_hnd(&hnd_src)) {
1917 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1921 nt_status = NT_STATUS_OK;
1925 if (is_valid_policy_hnd(&hnd_src))
1926 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1928 if (is_valid_policy_hnd(&hnd_dst))
1929 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1932 cli_shutdown(cli_dst);
1935 if (got_src_driver_share)
1936 cli_shutdown(cli_share_src);
1938 if (got_dst_driver_share)
1939 cli_shutdown(cli_share_dst);
1946 * Migrate printer-queues from a src to the dst server
1947 * (requires a working "addprinter command" to be installed for the local smbd)
1949 * All parameters are provided by the run_rpc_command function, except for
1950 * argc, argv which are passed through.
1952 * @param c A net_context structure
1953 * @param domain_sid The domain sid aquired from the remote server
1954 * @param cli A cli_state connected to the server.
1955 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1956 * @param argc Standard main() style argc
1957 * @param argv Standard main() style argv. Initial components are already
1960 * @return Normal NTSTATUS return.
1963 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
1964 const DOM_SID *domain_sid,
1965 const char *domain_name,
1966 struct cli_state *cli,
1967 struct rpc_pipe_client *pipe_hnd,
1968 TALLOC_CTX *mem_ctx,
1973 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1974 uint32 i = 0, num_printers;
1976 union spoolss_PrinterInfo info_dst, info_src;
1977 union spoolss_PrinterInfo *info_enum;
1978 struct cli_state *cli_dst = NULL;
1979 POLICY_HND hnd_dst, hnd_src;
1980 const char *printername, *sharename;
1981 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1982 struct spoolss_SetPrinterInfoCtr info_ctr;
1984 DEBUG(3,("copying printers\n"));
1986 /* connect destination PI_SPOOLSS */
1987 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1989 if (!NT_STATUS_IS_OK(nt_status))
1993 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1994 nt_status = NT_STATUS_UNSUCCESSFUL;
1998 if (!num_printers) {
1999 printf ("no printers found on server.\n");
2000 nt_status = NT_STATUS_OK;
2004 /* do something for all printers */
2005 for (i = 0; i < num_printers; i++) {
2007 /* do some initialization */
2008 printername = info_enum[i].info2.printername;
2009 sharename = info_enum[i].info2.sharename;
2011 if (!printername || !sharename) {
2012 nt_status = NT_STATUS_UNSUCCESSFUL;
2015 /* we can reset NT_STATUS here because we do not
2016 get any real NT_STATUS-codes anymore from now on */
2017 nt_status = NT_STATUS_UNSUCCESSFUL;
2019 d_printf("migrating printer queue for: [%s] / [%s]\n",
2020 printername, sharename);
2022 /* open dst printer handle */
2023 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2024 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2026 DEBUG(1,("could not open printer: %s\n", sharename));
2029 /* check for existing dst printer */
2030 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2031 printf ("could not get printer, creating printer.\n");
2033 DEBUG(1,("printer already exists: %s\n", sharename));
2034 /* close printer handle here - dst only, not got src yet. */
2035 if (is_valid_policy_hnd(&hnd_dst)) {
2036 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2041 /* now get again src printer ctr via getprinter,
2042 we first need a handle for that */
2044 /* open src printer handle */
2045 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2046 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2049 /* getprinter on the src server */
2050 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2053 /* copy each src printer to a dst printer 1:1,
2054 maybe some values have to be changed though */
2055 d_printf("creating printer: %s\n", printername);
2057 info_ctr.level = level;
2058 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info_src.info2;
2060 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2064 if (W_ERROR_IS_OK(result))
2065 d_printf ("printer [%s] successfully added.\n", printername);
2066 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2067 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2069 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2073 /* close printer handles here */
2074 if (is_valid_policy_hnd(&hnd_src)) {
2075 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2078 if (is_valid_policy_hnd(&hnd_dst)) {
2079 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2083 nt_status = NT_STATUS_OK;
2086 if (is_valid_policy_hnd(&hnd_src))
2087 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2089 if (is_valid_policy_hnd(&hnd_dst))
2090 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2093 cli_shutdown(cli_dst);
2099 * Migrate Printer-Settings from a src server to the dst server
2100 * (for this to work, printers and drivers already have to be migrated earlier)
2102 * All parameters are provided by the run_rpc_command function, except for
2103 * argc, argv which are passed through.
2105 * @param c A net_context structure
2106 * @param domain_sid The domain sid aquired from the remote server
2107 * @param cli A cli_state connected to the server.
2108 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2109 * @param argc Standard main() style argc
2110 * @param argv Standard main() style argv. Initial components are already
2113 * @return Normal NTSTATUS return.
2116 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2117 const DOM_SID *domain_sid,
2118 const char *domain_name,
2119 struct cli_state *cli,
2120 struct rpc_pipe_client *pipe_hnd,
2121 TALLOC_CTX *mem_ctx,
2126 /* FIXME: Here the nightmare begins */
2129 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2130 uint32 i = 0, p = 0, j = 0;
2131 uint32 num_printers, val_needed, data_needed;
2133 const char *printername, *sharename;
2134 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2135 POLICY_HND hnd_src, hnd_dst;
2136 union spoolss_PrinterInfo *info_enum;
2137 union spoolss_PrinterInfo info_dst_publish;
2138 union spoolss_PrinterInfo info_dst;
2139 REGVAL_CTR *reg_ctr;
2140 struct cli_state *cli_dst = NULL;
2141 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2142 const char *longname;
2144 uint16 *keylist = NULL, *curkey;
2147 ZERO_STRUCT(info_dst_publish);
2149 DEBUG(3,("copying printer settings\n"));
2151 /* connect destination PI_SPOOLSS */
2152 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2154 if (!NT_STATUS_IS_OK(nt_status))
2157 /* enum src printers */
2158 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2159 nt_status = NT_STATUS_UNSUCCESSFUL;
2163 if (!num_printers) {
2164 printf ("no printers found on server.\n");
2165 nt_status = NT_STATUS_OK;
2170 /* needed for dns-strings in regkeys */
2171 longname = get_mydnsfullname();
2173 nt_status = NT_STATUS_UNSUCCESSFUL;
2177 /* do something for all printers */
2178 for (i = 0; i < num_printers; i++) {
2180 /* do some initialization */
2181 printername = info_enum[i].info2.printername;
2182 sharename = info_enum[i].info2.sharename;
2184 if (!printername || !sharename) {
2185 nt_status = NT_STATUS_UNSUCCESSFUL;
2188 /* we can reset NT_STATUS here because we do not
2189 get any real NT_STATUS-codes anymore from now on */
2190 nt_status = NT_STATUS_UNSUCCESSFUL;
2192 d_printf("migrating printer settings for: [%s] / [%s]\n",
2193 printername, sharename);
2196 /* open src printer handle */
2197 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2198 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2201 /* open dst printer handle */
2202 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2203 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2206 /* check for existing dst printer */
2207 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2212 /* STEP 1: COPY DEVICE-MODE and other
2213 PRINTER_INFO_2-attributes
2216 info_dst.info2 = info_enum[i].info2;
2218 /* why is the port always disconnected when the printer
2219 is correctly installed (incl. driver ???) */
2220 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2222 /* check if printer is published */
2223 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2225 /* check for existing dst printer */
2226 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2229 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2231 /* ignore false from setprinter due to WERR_IO_PENDING */
2232 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2234 DEBUG(3,("republished printer\n"));
2237 if (info_enum[i].info2.devmode != NULL) {
2239 /* copy devmode (info level 2) */
2240 info_dst.info2.devmode = info_enum[i].info2.devmode;
2242 /* do not copy security descriptor (we have another
2243 * command for that) */
2244 info_dst.info2.secdesc = NULL;
2247 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2249 nt_status = NT_STATUS_NO_MEMORY;
2253 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2256 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2260 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2263 /* STEP 2: COPY REGISTRY VALUES */
2265 /* please keep in mind that samba parse_spools gives horribly
2266 crippled results when used to rpccli_spoolss_enumprinterdataex
2267 a win2k3-server. (Bugzilla #1851)
2268 FIXME: IIRC I've seen it too on a win2k-server
2271 /* enumerate data on src handle */
2272 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2273 &val_needed, &data_needed, NULL);
2275 /* loop for all printerdata of "PrinterDriverData" */
2276 while (W_ERROR_IS_OK(result)) {
2278 REGISTRY_VALUE value;
2280 result = rpccli_spoolss_enumprinterdata(
2281 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2282 data_needed, 0, 0, &value);
2284 /* loop for all reg_keys */
2285 if (W_ERROR_IS_OK(result)) {
2289 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2292 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2296 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2301 /* STEP 3: COPY SUBKEY VALUES */
2303 /* here we need to enum all printer_keys and then work
2304 on the result with enum_printer_key_ex. nt4 does not
2305 respond to enumprinterkey, win2k does, so continue
2306 in case of an error */
2308 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2309 printf("got no key-data\n");
2314 /* work on a list of printer keys
2315 each key has to be enumerated to get all required
2316 information. information is then set via setprinterdataex-calls */
2318 if (keylist == NULL)
2322 while (*curkey != 0) {
2324 rpcstr_pull_talloc(mem_ctx,
2330 return NT_STATUS_NO_MEMORY;
2333 curkey += strlen(subkey) + 1;
2335 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2336 return NT_STATUS_NO_MEMORY;
2338 /* enumerate all src subkeys */
2339 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2344 for (j=0; j < reg_ctr->num_values; j++) {
2346 REGISTRY_VALUE value;
2349 /* although samba replies with sane data in most cases we
2350 should try to avoid writing wrong registry data */
2352 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2353 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2354 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2355 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2356 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2358 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2360 /* although windows uses a multi-sz, we use a sz */
2361 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2362 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2365 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2367 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2368 nt_status = NT_STATUS_NO_MEMORY;
2371 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2372 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2375 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2380 /* FIXME: should we really do that ??? */
2381 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2382 nt_status = NT_STATUS_NO_MEMORY;
2385 init_unistr2(&data, url, UNI_STR_TERMINATE);
2386 fstrcpy(value.valuename, SPOOL_REG_URL);
2390 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2392 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2393 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2396 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2398 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2399 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2402 value.type = REG_SZ;
2403 value.size = data.uni_str_len * 2;
2405 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2407 value.data_p = NULL;
2411 display_reg_value(subkey, value);
2413 /* here we have to set all subkeys on the dst server */
2414 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2421 display_reg_value(subkey, *(reg_ctr->values[j]));
2423 /* here we have to set all subkeys on the dst server */
2424 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2425 subkey, reg_ctr->values[j]))
2430 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2431 subkey, reg_ctr->values[j]->valuename));
2435 TALLOC_FREE( reg_ctr );
2440 /* close printer handles here */
2441 if (is_valid_policy_hnd(&hnd_src)) {
2442 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2445 if (is_valid_policy_hnd(&hnd_dst)) {
2446 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2451 nt_status = NT_STATUS_OK;
2454 SAFE_FREE(devicename);
2456 SAFE_FREE(unc_name);
2458 if (is_valid_policy_hnd(&hnd_src))
2459 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2461 if (is_valid_policy_hnd(&hnd_dst))
2462 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2465 cli_shutdown(cli_dst);