2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2004,2009 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, struct regval_blob value)
86 d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename,
87 *((uint32_t *) 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_t 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;
158 uint16_t fnum_src = 0;
159 uint16_t fnum_dst = 0;
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 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
174 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src))) {
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 if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_dst, dst_name, 0, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
214 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_dst))) {
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;
312 uint16_t fnum_src = 0;
313 uint16_t fnum_dst = 0;
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 nt_status = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE, &fnum_src);
332 nt_status = cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
333 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src);
335 if (!NT_STATUS_IS_OK(nt_status)) {
336 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
337 is_file ? "file":"dir",
338 src_name, cli_errstr(cli_share_src)));
345 /* open file on the destination server */
346 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
347 nt_status = cli_open(cli_share_dst, dst_name,
348 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum_dst);
350 if (!NT_STATUS_IS_OK(nt_status)) {
351 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
352 dst_name, cli_errstr(cli_share_dst)));
356 /* allocate memory */
357 if (!(data = (char *)SMB_MALLOC(read_size))) {
358 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
359 nt_status = NT_STATUS_NO_MEMORY;
366 if (c->opt_verbose) {
368 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
369 "%s ACLs and %s DOS Attributes %s\n",
370 cli_share_src->desthost, cli_share_src->share, src_name,
371 cli_share_dst->desthost, cli_share_dst->share, dst_name,
372 copy_acls ? "with" : "without",
373 copy_attrs ? "with" : "without",
374 copy_timestamps ? "(preserving timestamps)" : "" );
382 n = cli_read(cli_share_src, fnum_src, data, nread,
388 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
392 d_fprintf(stderr, "Error writing file: %s\n",
393 cli_errstr(cli_share_dst));
394 nt_status = cli_nt_error(cli_share_dst);
402 if (!is_file && !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
405 DEBUGADD(3,("creating dir %s on the destination server\n",
408 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share_dst, dst_name))) {
409 DEBUG(0,("cannot create directory %s: %s\n",
410 dst_name, cli_errstr(cli_share_dst)));
411 nt_status = NT_STATUS_NO_SUCH_FILE;
414 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
415 d_fprintf(stderr, "cannot check for directory %s: %s\n",
416 dst_name, cli_errstr(cli_share_dst));
423 if (!cli_close(cli_share_src, fnum_src)) {
424 d_fprintf(stderr, "could not close file on originating server: %s\n",
425 cli_errstr(cli_share_src));
426 nt_status = cli_nt_error(cli_share_src);
430 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
431 d_fprintf(stderr, "could not close file on destination server: %s\n",
432 cli_errstr(cli_share_dst));
433 nt_status = cli_nt_error(cli_share_dst);
437 /* possibly we have to copy some file-attributes / acls / sd */
438 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
439 src_name, dst_name, copy_acls,
440 copy_attrs, copy_timestamps, is_file);
441 if (!NT_STATUS_IS_OK(nt_status))
445 nt_status = NT_STATUS_OK;
451 cli_close(cli_share_src, fnum_src);
454 cli_close(cli_share_dst, fnum_dst);
462 * Copy a driverfile from on connected share to another connected share
463 * This silently assumes that a driver-file is picked up from
465 * \\src_server\print$\{arch}\{version}\file
469 * \\dst_server\print$\{arch}\file
471 * to be added via setdriver-calls later.
472 * @param c A net_context structure
473 * @param mem_ctx A talloc-context
474 * @param cli_share_src A cli_state connected to source print$-share
475 * @param cli_share_dst A cli_state connected to destination print$-share
476 * @param file The file-name to be copied
477 * @param short_archi The name of the driver-architecture (short form)
479 * @return Normal NTSTATUS return.
482 static NTSTATUS net_copy_driverfile(struct net_context *c,
484 struct cli_state *cli_share_src,
485 struct cli_state *cli_share_dst,
486 const char *file, const char *short_archi) {
488 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
500 /* scroll through the file until we have the part
501 beyond archi_table.short_archi */
503 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
504 if (strequal(tok, short_archi)) {
505 next_token_talloc(mem_ctx, &p, &version, "\\");
506 next_token_talloc(mem_ctx, &p, &filename, "\\");
510 /* build source file name */
511 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
512 return NT_STATUS_NO_MEMORY;
515 /* create destination file name */
516 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
517 return NT_STATUS_NO_MEMORY;
520 /* finally copy the file */
521 nt_status = net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
522 src_name, dst_name, false, false, false, true);
523 if (!NT_STATUS_IS_OK(nt_status))
526 nt_status = NT_STATUS_OK;
536 * Check for existing Architecture directory on a given server
538 * @param cli_share A cli_state connected to a print$-share
539 * @param short_archi The Architecture for the print-driver
541 * @return Normal NTSTATUS return.
544 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
547 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
550 if (asprintf(&dir, "\\%s", short_archi) < 0) {
551 return NT_STATUS_NO_MEMORY;
554 DEBUG(10,("creating print-driver dir for architecture: %s\n",
557 if (!NT_STATUS_IS_OK(cli_mkdir(cli_share, dir))) {
558 DEBUG(1,("cannot create directory %s: %s\n",
559 dir, cli_errstr(cli_share)));
560 nt_status = NT_STATUS_NO_SUCH_FILE;
563 if (!NT_STATUS_IS_OK(cli_chkpath(cli_share, dir))) {
564 d_fprintf(stderr, "cannot check %s: %s\n",
565 dir, cli_errstr(cli_share));
569 nt_status = NT_STATUS_OK;
577 * Copy a print-driver (level 3) from one connected print$-share to another
578 * connected print$-share
580 * @param c A net_context structure
581 * @param mem_ctx A talloc-context
582 * @param cli_share_src A cli_state connected to a print$-share
583 * @param cli_share_dst A cli_state connected to a print$-share
584 * @param short_archi The Architecture for the print-driver
585 * @param i1 The DRIVER_INFO_3-struct
587 * @return Normal NTSTATUS return.
590 static NTSTATUS copy_print_driver_3(struct net_context *c,
592 struct cli_state *cli_share_src,
593 struct cli_state *cli_share_dst,
594 const char *short_archi,
595 struct spoolss_DriverInfo3 *r)
597 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
605 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
606 r->driver_name, short_archi, r->version);
608 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
609 r->driver_path, short_archi);
610 if (!NT_STATUS_IS_OK(nt_status))
613 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
614 r->data_file, short_archi);
615 if (!NT_STATUS_IS_OK(nt_status))
618 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
619 r->config_file, short_archi);
620 if (!NT_STATUS_IS_OK(nt_status))
623 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
624 r->help_file, short_archi);
625 if (!NT_STATUS_IS_OK(nt_status))
628 for (i=0; r->dependent_files[i] != NULL; i++) {
630 nt_status = net_copy_driverfile(c, mem_ctx,
631 cli_share_src, cli_share_dst,
632 r->dependent_files[i], short_archi);
633 if (!NT_STATUS_IS_OK(nt_status)) {
642 * net_spoolss-functions
643 * =====================
645 * the net_spoolss-functions aim to simplify spoolss-client-functions
646 * required during the migration-process wrt buffer-sizes, returned
649 * this greatly reduces the complexitiy of the migrate-functions.
653 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
658 uint32_t *num_printers,
659 union spoolss_PrinterInfo **info)
665 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
672 if (!W_ERROR_IS_OK(result)) {
673 printf("cannot enum printers: %s\n", win_errstr(result));
680 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
682 const char *printername,
683 uint32_t access_required,
684 const char *username,
685 struct policy_handle *hnd)
688 fstring printername2;
690 fstrcpy(printername2, pipe_hnd->srv_name_slash);
691 fstrcat(printername2, "\\");
692 fstrcat(printername2, printername);
694 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
695 pipe_hnd->srv_name_slash, username, printername2, access_required));
698 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
703 /* be more verbose */
704 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
705 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
706 printername2, pipe_hnd->srv_name_slash, username);
710 if (!W_ERROR_IS_OK(result)) {
711 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
712 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
716 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
717 printername2, pipe_hnd->srv_name_slash));
722 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
724 struct policy_handle *hnd,
726 union spoolss_PrinterInfo *info)
730 /* getprinter call */
731 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
736 if (!W_ERROR_IS_OK(result)) {
737 printf("cannot get printer-info: %s\n", win_errstr(result));
744 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
746 struct policy_handle *hnd,
748 union spoolss_PrinterInfo *info)
752 struct spoolss_SetPrinterInfoCtr info_ctr;
753 struct spoolss_DevmodeContainer devmode_ctr;
754 struct sec_desc_buf secdesc_ctr;
756 ZERO_STRUCT(devmode_ctr);
757 ZERO_STRUCT(secdesc_ctr);
759 /* setprinter call */
761 info_ctr.level = level;
764 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)&info->info0;
767 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)&info->info1;
770 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info->info2;
773 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)&info->info3;
776 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)&info->info4;
779 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)&info->info5;
782 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)&info->info6;
785 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info->info7;
789 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)&info->info8;
792 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)&info->info9;
799 status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
807 if (!W_ERROR_IS_OK(result)) {
808 printf("cannot set printer-info: %s\n", win_errstr(result));
816 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
818 struct policy_handle *hnd,
819 const char *value_name,
820 enum winreg_Type type,
821 union spoolss_PrinterData data)
826 /* setprinterdata call */
827 status = rpccli_spoolss_SetPrinterData(pipe_hnd, mem_ctx,
832 0, /* autocalculated */
835 if (!W_ERROR_IS_OK(result)) {
836 printf ("unable to set printerdata: %s\n", win_errstr(result));
844 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
846 struct policy_handle *hnd,
848 const char ***keylist)
852 /* enumprinterkey call */
853 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
855 if (!W_ERROR_IS_OK(result)) {
856 printf("enumprinterkey failed: %s\n", win_errstr(result));
863 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
866 struct policy_handle *hnd,
869 struct spoolss_PrinterEnumValues **info)
873 /* enumprinterdataex call */
874 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
881 if (!W_ERROR_IS_OK(result)) {
882 printf("enumprinterdataex failed: %s\n", win_errstr(result));
890 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
892 struct policy_handle *hnd,
894 struct regval_blob *value)
899 /* setprinterdataex call */
900 status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
909 if (!W_ERROR_IS_OK(result)) {
910 printf("could not set printerdataex: %s\n", win_errstr(result));
917 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
919 struct policy_handle *hnd,
922 union spoolss_FormInfo **forms)
927 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
933 if (!W_ERROR_IS_OK(result)) {
934 printf("could not enum forms: %s\n", win_errstr(result));
941 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
943 uint32_t level, const char *env,
945 union spoolss_DriverInfo **info)
949 /* enumprinterdrivers call */
950 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
951 pipe_hnd->srv_name_slash,
957 if (!W_ERROR_IS_OK(result)) {
958 printf("cannot enum drivers: %s\n", win_errstr(result));
965 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
967 struct policy_handle *hnd, uint32_t level,
968 const char *env, int version,
969 union spoolss_DriverInfo *info)
972 uint32_t server_major_version;
973 uint32_t server_minor_version;
975 /* getprinterdriver call */
976 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
984 &server_major_version,
985 &server_minor_version);
986 if (!W_ERROR_IS_OK(result)) {
987 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
988 env, win_errstr(result)));
989 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
990 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
991 printf("cannot get driver: %s\n", win_errstr(result));
1000 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1001 TALLOC_CTX *mem_ctx, uint32_t level,
1002 union spoolss_DriverInfo *info)
1006 struct spoolss_AddDriverInfoCtr info_ctr;
1008 info_ctr.level = level;
1012 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)&info->info2;
1015 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)&info->info3;
1018 printf("unsupported info level: %d\n", level);
1022 /* addprinterdriver call */
1023 status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1024 pipe_hnd->srv_name_slash,
1027 /* be more verbose */
1028 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1029 printf("You are not allowed to add drivers\n");
1032 if (!W_ERROR_IS_OK(result)) {
1033 printf("cannot add driver: %s\n", win_errstr(result));
1041 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1042 * for a single printer or for all printers depending on argc/argv
1045 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1046 TALLOC_CTX *mem_ctx,
1050 uint32_t *num_printers,
1051 union spoolss_PrinterInfo **info_p)
1053 struct policy_handle hnd;
1055 /* no arguments given, enumerate all printers */
1058 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1059 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1060 level, num_printers, info_p))
1066 /* argument given, get a single printer by name */
1067 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1068 MAXIMUM_ALLOWED_ACCESS,
1069 pipe_hnd->auth->user_name,
1073 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1074 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1078 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1083 DEBUG(3,("got %d printers\n", *num_printers));
1090 * List print-queues (including local printers that are not shared)
1092 * All parameters are provided by the run_rpc_command function, except for
1093 * argc, argv which are passed through.
1095 * @param c A net_context structure
1096 * @param domain_sid The domain sid aquired from the remote server
1097 * @param cli A cli_state connected to the server.
1098 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1099 * @param argc Standard main() style argc
1100 * @param argv Standard main() style argv. Initial components are already
1103 * @return Normal NTSTATUS return.
1106 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1107 const DOM_SID *domain_sid,
1108 const char *domain_name,
1109 struct cli_state *cli,
1110 struct rpc_pipe_client *pipe_hnd,
1111 TALLOC_CTX *mem_ctx,
1115 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1116 uint32_t i, num_printers;
1118 const char *printername, *sharename;
1119 union spoolss_PrinterInfo *info;
1121 printf("listing printers\n");
1123 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1126 for (i = 0; i < num_printers; i++) {
1128 /* do some initialization */
1129 printername = info[i].info2.printername;
1130 sharename = info[i].info2.sharename;
1132 if (printername && sharename) {
1133 d_printf("printer %d: %s, shared as: %s\n",
1134 i+1, printername, sharename);
1138 return NT_STATUS_OK;
1142 * List printer-drivers from a server
1144 * All parameters are provided by the run_rpc_command function, except for
1145 * argc, argv which are passed through.
1147 * @param c A net_context structure
1148 * @param domain_sid The domain sid aquired from the remote server
1149 * @param cli A cli_state connected to the server.
1150 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1151 * @param argc Standard main() style argc
1152 * @param argv Standard main() style argv. Initial components are already
1155 * @return Normal NTSTATUS return.
1158 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1159 const DOM_SID *domain_sid,
1160 const char *domain_name,
1161 struct cli_state *cli,
1162 struct rpc_pipe_client *pipe_hnd,
1163 TALLOC_CTX *mem_ctx,
1167 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1170 union spoolss_DriverInfo *info;
1173 printf("listing printer-drivers\n");
1175 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1177 uint32_t num_drivers;
1179 /* enum remote drivers */
1180 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1181 archi_table[i].long_archi,
1182 &num_drivers, &info)) {
1183 nt_status = NT_STATUS_UNSUCCESSFUL;
1187 if (num_drivers == 0) {
1188 d_printf ("no drivers found on server for architecture: [%s].\n",
1189 archi_table[i].long_archi);
1193 d_printf("got %d printer-drivers for architecture: [%s]\n",
1194 num_drivers, archi_table[i].long_archi);
1197 /* do something for all drivers for architecture */
1198 for (d = 0; d < num_drivers; d++) {
1199 display_print_driver3(&info[d].info3);
1203 nt_status = NT_STATUS_OK;
1211 * Publish print-queues with args-wrapper
1213 * @param cli A cli_state connected to the server.
1214 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1215 * @param argc Standard main() style argc
1216 * @param argv Standard main() style argv. Initial components are already
1220 * @return Normal NTSTATUS return.
1223 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1224 TALLOC_CTX *mem_ctx,
1229 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1230 uint32_t i, num_printers;
1232 const char *printername, *sharename;
1233 union spoolss_PrinterInfo *info_enum;
1234 union spoolss_PrinterInfo info;
1235 struct spoolss_SetPrinterInfoCtr info_ctr;
1236 struct spoolss_DevmodeContainer devmode_ctr;
1237 struct sec_desc_buf secdesc_ctr;
1238 struct policy_handle hnd;
1240 const char *action_str;
1242 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1245 for (i = 0; i < num_printers; i++) {
1247 /* do some initialization */
1248 printername = info_enum[i].info2.printername;
1249 sharename = info_enum[i].info2.sharename;
1250 if (!printername || !sharename) {
1254 /* open printer handle */
1255 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1256 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1259 /* check for existing dst printer */
1260 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1263 /* check action and set string */
1265 case DSPRINT_PUBLISH:
1266 action_str = "published";
1268 case DSPRINT_UPDATE:
1269 action_str = "updated";
1271 case DSPRINT_UNPUBLISH:
1272 action_str = "unpublished";
1275 action_str = "unknown action";
1276 printf("unkown action: %d\n", action);
1280 info.info7.action = action;
1282 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info.info7;
1284 ZERO_STRUCT(devmode_ctr);
1285 ZERO_STRUCT(secdesc_ctr);
1287 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1295 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1296 printf("cannot set printer-info: %s\n", win_errstr(result));
1300 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1303 nt_status = NT_STATUS_OK;
1306 if (is_valid_policy_hnd(&hnd))
1307 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1312 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1313 const DOM_SID *domain_sid,
1314 const char *domain_name,
1315 struct cli_state *cli,
1316 struct rpc_pipe_client *pipe_hnd,
1317 TALLOC_CTX *mem_ctx,
1321 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1324 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1325 const DOM_SID *domain_sid,
1326 const char *domain_name,
1327 struct cli_state *cli,
1328 struct rpc_pipe_client *pipe_hnd,
1329 TALLOC_CTX *mem_ctx,
1333 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1336 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1337 const DOM_SID *domain_sid,
1338 const char *domain_name,
1339 struct cli_state *cli,
1340 struct rpc_pipe_client *pipe_hnd,
1341 TALLOC_CTX *mem_ctx,
1345 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1349 * List print-queues w.r.t. their publishing state
1351 * All parameters are provided by the run_rpc_command function, except for
1352 * argc, argv which are passed through.
1354 * @param c A net_context structure
1355 * @param domain_sid The domain sid aquired from the remote server
1356 * @param cli A cli_state connected to the server.
1357 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1358 * @param argc Standard main() style argc
1359 * @param argv Standard main() style argv. Initial components are already
1362 * @return Normal NTSTATUS return.
1365 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1366 const DOM_SID *domain_sid,
1367 const char *domain_name,
1368 struct cli_state *cli,
1369 struct rpc_pipe_client *pipe_hnd,
1370 TALLOC_CTX *mem_ctx,
1374 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1375 uint32_t i, num_printers;
1377 const char *printername, *sharename;
1378 union spoolss_PrinterInfo *info_enum;
1379 union spoolss_PrinterInfo info;
1380 struct policy_handle hnd;
1383 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1386 for (i = 0; i < num_printers; i++) {
1388 /* do some initialization */
1389 printername = info_enum[i].info2.printername;
1390 sharename = info_enum[i].info2.sharename;
1392 if (!printername || !sharename) {
1396 /* open printer handle */
1397 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1398 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1401 /* check for existing dst printer */
1402 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1405 if (!info.info7.guid) {
1408 state = info.info7.action;
1410 case DSPRINT_PUBLISH:
1411 printf("printer [%s] is published", sharename);
1413 printf(", guid: %s", info.info7.guid);
1416 case DSPRINT_UNPUBLISH:
1417 printf("printer [%s] is unpublished\n", sharename);
1419 case DSPRINT_UPDATE:
1420 printf("printer [%s] is currently updating\n", sharename);
1423 printf("unkown state: %d\n", state);
1428 nt_status = NT_STATUS_OK;
1431 if (is_valid_policy_hnd(&hnd))
1432 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1438 * Migrate Printer-ACLs from a source server to the destination server
1440 * All parameters are provided by the run_rpc_command function, except for
1441 * argc, argv which are passed through.
1443 * @param c A net_context structure
1444 * @param domain_sid The domain sid aquired from the remote server
1445 * @param cli A cli_state connected to the server.
1446 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1447 * @param argc Standard main() style argc
1448 * @param argv Standard main() style argv. Initial components are already
1451 * @return Normal NTSTATUS return.
1454 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1455 const DOM_SID *domain_sid,
1456 const char *domain_name,
1457 struct cli_state *cli,
1458 struct rpc_pipe_client *pipe_hnd,
1459 TALLOC_CTX *mem_ctx,
1463 /* TODO: what now, info2 or info3 ?
1464 convince jerry that we should add clientside setacls level 3 at least
1466 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1468 uint32_t num_printers;
1470 const char *printername, *sharename;
1471 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1472 struct policy_handle hnd_src, hnd_dst;
1473 union spoolss_PrinterInfo *info_enum;
1474 struct cli_state *cli_dst = NULL;
1475 union spoolss_PrinterInfo info_src, info_dst;
1477 DEBUG(3,("copying printer ACLs\n"));
1479 /* connect destination PI_SPOOLSS */
1480 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1481 &ndr_table_spoolss.syntax_id);
1482 if (!NT_STATUS_IS_OK(nt_status))
1486 /* enum source printers */
1487 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1488 nt_status = NT_STATUS_UNSUCCESSFUL;
1492 if (!num_printers) {
1493 printf ("no printers found on server.\n");
1494 nt_status = NT_STATUS_OK;
1498 /* do something for all printers */
1499 for (i = 0; i < num_printers; i++) {
1501 /* do some initialization */
1502 printername = info_enum[i].info2.printername;
1503 sharename = info_enum[i].info2.sharename;
1505 if (!printername || !sharename) {
1506 nt_status = NT_STATUS_UNSUCCESSFUL;
1510 /* we can reset NT_STATUS here because we do not
1511 get any real NT_STATUS-codes anymore from now on */
1512 nt_status = NT_STATUS_UNSUCCESSFUL;
1514 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1515 printername, sharename);
1517 /* according to msdn you have specify these access-rights
1518 to see the security descriptor
1519 - READ_CONTROL (DACL)
1520 - ACCESS_SYSTEM_SECURITY (SACL)
1523 /* open src printer handle */
1524 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1525 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1528 /* open dst printer handle */
1529 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1530 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1533 /* check for existing dst printer */
1534 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1537 /* check for existing src printer */
1538 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1541 /* Copy Security Descriptor */
1543 /* copy secdesc (info level 2) */
1544 info_dst.info2.devmode = NULL;
1545 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1548 display_sec_desc(info_dst.info2.secdesc);
1550 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1553 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1556 /* close printer handles here */
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);
1567 nt_status = NT_STATUS_OK;
1571 if (is_valid_policy_hnd(&hnd_src)) {
1572 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1575 if (is_valid_policy_hnd(&hnd_dst)) {
1576 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1580 cli_shutdown(cli_dst);
1586 * Migrate printer-forms from a src server to the dst server
1588 * All parameters are provided by the run_rpc_command function, except for
1589 * argc, argv which are passed through.
1591 * @param c A net_context structure
1592 * @param domain_sid The domain sid aquired from the remote server
1593 * @param cli A cli_state connected to the server.
1594 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1595 * @param argc Standard main() style argc
1596 * @param argv Standard main() style argv. Initial components are already
1599 * @return Normal NTSTATUS return.
1602 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1603 const DOM_SID *domain_sid,
1604 const char *domain_name,
1605 struct cli_state *cli,
1606 struct rpc_pipe_client *pipe_hnd,
1607 TALLOC_CTX *mem_ctx,
1611 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1614 uint32_t num_printers;
1616 const char *printername, *sharename;
1617 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1618 struct policy_handle hnd_src, hnd_dst;
1619 union spoolss_PrinterInfo *info_enum;
1620 union spoolss_PrinterInfo info_dst;
1622 union spoolss_FormInfo *forms;
1623 struct cli_state *cli_dst = NULL;
1625 DEBUG(3,("copying forms\n"));
1627 /* connect destination PI_SPOOLSS */
1628 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1629 &ndr_table_spoolss.syntax_id);
1630 if (!NT_STATUS_IS_OK(nt_status))
1633 /* enum src printers */
1634 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1635 nt_status = NT_STATUS_UNSUCCESSFUL;
1639 if (!num_printers) {
1640 printf ("no printers found on server.\n");
1641 nt_status = NT_STATUS_OK;
1645 /* do something for all printers */
1646 for (i = 0; i < num_printers; i++) {
1648 /* do some initialization */
1649 printername = info_enum[i].info2.printername;
1650 sharename = info_enum[i].info2.sharename;
1652 if (!printername || !sharename) {
1653 nt_status = NT_STATUS_UNSUCCESSFUL;
1656 /* we can reset NT_STATUS here because we do not
1657 get any real NT_STATUS-codes anymore from now on */
1658 nt_status = NT_STATUS_UNSUCCESSFUL;
1660 d_printf("migrating printer forms for: [%s] / [%s]\n",
1661 printername, sharename);
1664 /* open src printer handle */
1665 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1666 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1669 /* open dst printer handle */
1670 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1671 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1674 /* check for existing dst printer */
1675 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1678 /* finally migrate forms */
1679 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1682 DEBUG(1,("got %d forms for printer\n", num_forms));
1685 for (f = 0; f < num_forms; f++) {
1687 union spoolss_AddFormInfo info;
1690 /* only migrate FORM_PRINTER types, according to jerry
1691 FORM_BUILTIN-types are hard-coded in samba */
1692 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1696 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1697 f, forms[f].info1.form_name,
1698 forms[f].info1.flags);
1700 info.info1 = (struct spoolss_AddFormInfo1 *)&forms[f].info1;
1702 /* FIXME: there might be something wrong with samba's
1704 status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1709 if (!W_ERROR_IS_OK(result)) {
1710 d_printf("\tAddForm form %d: [%s] refused.\n",
1711 f, forms[f].info1.form_name);
1715 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1716 forms[f].info1.form_name));
1720 /* close printer handles here */
1721 if (is_valid_policy_hnd(&hnd_src)) {
1722 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1725 if (is_valid_policy_hnd(&hnd_dst)) {
1726 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1730 nt_status = NT_STATUS_OK;
1734 if (is_valid_policy_hnd(&hnd_src))
1735 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1737 if (is_valid_policy_hnd(&hnd_dst))
1738 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1741 cli_shutdown(cli_dst);
1747 * Migrate printer-drivers from a src server to the dst server
1749 * All parameters are provided by the run_rpc_command function, except for
1750 * argc, argv which are passed through.
1752 * @param c A net_context structure
1753 * @param domain_sid The domain sid aquired from the remote server
1754 * @param cli A cli_state connected to the server.
1755 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1756 * @param argc Standard main() style argc
1757 * @param argv Standard main() style argv. Initial components are already
1760 * @return Normal NTSTATUS return.
1763 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1764 const DOM_SID *domain_sid,
1765 const char *domain_name,
1766 struct cli_state *cli,
1767 struct rpc_pipe_client *pipe_hnd,
1768 TALLOC_CTX *mem_ctx,
1772 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1774 uint32_t num_printers;
1776 const char *printername, *sharename;
1777 bool got_src_driver_share = false;
1778 bool got_dst_driver_share = false;
1779 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1780 struct policy_handle hnd_src, hnd_dst;
1781 union spoolss_DriverInfo drv_info_src;
1782 union spoolss_PrinterInfo *info_enum;
1783 union spoolss_PrinterInfo info_dst;
1784 struct cli_state *cli_dst = NULL;
1785 struct cli_state *cli_share_src = NULL;
1786 struct cli_state *cli_share_dst = NULL;
1787 const char *drivername = NULL;
1789 DEBUG(3,("copying printer-drivers\n"));
1791 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1792 &ndr_table_spoolss.syntax_id);
1793 if (!NT_STATUS_IS_OK(nt_status))
1796 /* open print$-share on the src server */
1797 nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1798 cli->desthost, "print$", "A:");
1799 if (!NT_STATUS_IS_OK(nt_status))
1802 got_src_driver_share = true;
1805 /* open print$-share on the dst server */
1806 nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1807 cli_dst->desthost, "print$", "A:");
1808 if (!NT_STATUS_IS_OK(nt_status))
1811 got_dst_driver_share = true;
1814 /* enum src printers */
1815 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1816 nt_status = NT_STATUS_UNSUCCESSFUL;
1820 if (num_printers == 0) {
1821 printf ("no printers found on server.\n");
1822 nt_status = NT_STATUS_OK;
1827 /* do something for all printers */
1828 for (p = 0; p < num_printers; p++) {
1830 /* do some initialization */
1831 printername = info_enum[p].info2.printername;
1832 sharename = info_enum[p].info2.sharename;
1834 if (!printername || !sharename) {
1835 nt_status = NT_STATUS_UNSUCCESSFUL;
1839 /* we can reset NT_STATUS here because we do not
1840 get any real NT_STATUS-codes anymore from now on */
1841 nt_status = NT_STATUS_UNSUCCESSFUL;
1843 d_printf("migrating printer driver for: [%s] / [%s]\n",
1844 printername, sharename);
1846 /* open dst printer handle */
1847 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1848 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1851 /* check for existing dst printer */
1852 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1856 /* open src printer handle */
1857 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1858 MAXIMUM_ALLOWED_ACCESS,
1859 pipe_hnd->auth->user_name,
1863 /* in a first step call getdriver for each shared printer (per arch)
1864 to get a list of all files that have to be copied */
1866 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1869 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1870 level, archi_table[i].long_archi,
1871 archi_table[i].version, &drv_info_src))
1874 drivername = drv_info_src.info3.driver_name;
1877 display_print_driver3(&drv_info_src.info3);
1879 /* check arch dir */
1880 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1881 if (!NT_STATUS_IS_OK(nt_status))
1885 /* copy driver-files */
1886 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1887 archi_table[i].short_archi,
1888 &drv_info_src.info3);
1889 if (!NT_STATUS_IS_OK(nt_status))
1894 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1895 nt_status = NT_STATUS_UNSUCCESSFUL;
1899 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1900 drivername, printername));
1904 if (!drivername || strlen(drivername) == 0) {
1905 DEBUGADD(1,("Did not get driver for printer %s\n",
1911 info_dst.info2.drivername = drivername;
1913 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1914 nt_status = NT_STATUS_UNSUCCESSFUL;
1918 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1919 drivername, printername));
1922 if (is_valid_policy_hnd(&hnd_dst)) {
1923 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1927 if (is_valid_policy_hnd(&hnd_src)) {
1928 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1932 nt_status = NT_STATUS_OK;
1936 if (is_valid_policy_hnd(&hnd_src))
1937 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1939 if (is_valid_policy_hnd(&hnd_dst))
1940 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1943 cli_shutdown(cli_dst);
1946 if (got_src_driver_share)
1947 cli_shutdown(cli_share_src);
1949 if (got_dst_driver_share)
1950 cli_shutdown(cli_share_dst);
1957 * Migrate printer-queues from a src to the dst server
1958 * (requires a working "addprinter command" to be installed for the local smbd)
1960 * All parameters are provided by the run_rpc_command function, except for
1961 * argc, argv which are passed through.
1963 * @param c A net_context structure
1964 * @param domain_sid The domain sid aquired from the remote server
1965 * @param cli A cli_state connected to the server.
1966 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1967 * @param argc Standard main() style argc
1968 * @param argv Standard main() style argv. Initial components are already
1971 * @return Normal NTSTATUS return.
1974 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
1975 const DOM_SID *domain_sid,
1976 const char *domain_name,
1977 struct cli_state *cli,
1978 struct rpc_pipe_client *pipe_hnd,
1979 TALLOC_CTX *mem_ctx,
1984 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1985 uint32_t i = 0, num_printers;
1987 union spoolss_PrinterInfo info_dst, info_src;
1988 union spoolss_PrinterInfo *info_enum;
1989 struct cli_state *cli_dst = NULL;
1990 struct policy_handle hnd_dst, hnd_src;
1991 const char *printername, *sharename;
1992 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1993 struct spoolss_SetPrinterInfoCtr info_ctr;
1995 DEBUG(3,("copying printers\n"));
1997 /* connect destination PI_SPOOLSS */
1998 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1999 &ndr_table_spoolss.syntax_id);
2000 if (!NT_STATUS_IS_OK(nt_status))
2004 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2005 nt_status = NT_STATUS_UNSUCCESSFUL;
2009 if (!num_printers) {
2010 printf ("no printers found on server.\n");
2011 nt_status = NT_STATUS_OK;
2015 /* do something for all printers */
2016 for (i = 0; i < num_printers; i++) {
2018 /* do some initialization */
2019 printername = info_enum[i].info2.printername;
2020 sharename = info_enum[i].info2.sharename;
2022 if (!printername || !sharename) {
2023 nt_status = NT_STATUS_UNSUCCESSFUL;
2026 /* we can reset NT_STATUS here because we do not
2027 get any real NT_STATUS-codes anymore from now on */
2028 nt_status = NT_STATUS_UNSUCCESSFUL;
2030 d_printf("migrating printer queue for: [%s] / [%s]\n",
2031 printername, sharename);
2033 /* open dst printer handle */
2034 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2035 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2037 DEBUG(1,("could not open printer: %s\n", sharename));
2040 /* check for existing dst printer */
2041 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2042 printf ("could not get printer, creating printer.\n");
2044 DEBUG(1,("printer already exists: %s\n", sharename));
2045 /* close printer handle here - dst only, not got src yet. */
2046 if (is_valid_policy_hnd(&hnd_dst)) {
2047 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2052 /* now get again src printer ctr via getprinter,
2053 we first need a handle for that */
2055 /* open src printer handle */
2056 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2057 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2060 /* getprinter on the src server */
2061 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2064 /* copy each src printer to a dst printer 1:1,
2065 maybe some values have to be changed though */
2066 d_printf("creating printer: %s\n", printername);
2068 info_ctr.level = level;
2069 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info_src.info2;
2071 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2075 if (W_ERROR_IS_OK(result))
2076 d_printf ("printer [%s] successfully added.\n", printername);
2077 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2078 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2080 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2084 /* close printer handles here */
2085 if (is_valid_policy_hnd(&hnd_src)) {
2086 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);
2094 nt_status = NT_STATUS_OK;
2097 if (is_valid_policy_hnd(&hnd_src))
2098 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2100 if (is_valid_policy_hnd(&hnd_dst))
2101 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2104 cli_shutdown(cli_dst);
2110 * Migrate Printer-Settings from a src server to the dst server
2111 * (for this to work, printers and drivers already have to be migrated earlier)
2113 * All parameters are provided by the run_rpc_command function, except for
2114 * argc, argv which are passed through.
2116 * @param c A net_context structure
2117 * @param domain_sid The domain sid aquired from the remote server
2118 * @param cli A cli_state connected to the server.
2119 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2120 * @param argc Standard main() style argc
2121 * @param argv Standard main() style argv. Initial components are already
2124 * @return Normal NTSTATUS return.
2127 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2128 const DOM_SID *domain_sid,
2129 const char *domain_name,
2130 struct cli_state *cli,
2131 struct rpc_pipe_client *pipe_hnd,
2132 TALLOC_CTX *mem_ctx,
2137 /* FIXME: Here the nightmare begins */
2140 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2141 uint32_t i = 0, p = 0, j = 0;
2142 uint32_t num_printers;
2144 const char *printername, *sharename;
2145 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2146 struct policy_handle hnd_src, hnd_dst;
2147 union spoolss_PrinterInfo *info_enum;
2148 union spoolss_PrinterInfo info_dst_publish;
2149 union spoolss_PrinterInfo info_dst;
2150 struct cli_state *cli_dst = NULL;
2151 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2152 const char *longname;
2153 const char **keylist = NULL;
2156 ZERO_STRUCT(info_dst_publish);
2158 DEBUG(3,("copying printer settings\n"));
2160 /* connect destination PI_SPOOLSS */
2161 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2162 &ndr_table_spoolss.syntax_id);
2163 if (!NT_STATUS_IS_OK(nt_status))
2166 /* enum src printers */
2167 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2168 nt_status = NT_STATUS_UNSUCCESSFUL;
2172 if (!num_printers) {
2173 printf ("no printers found on server.\n");
2174 nt_status = NT_STATUS_OK;
2179 /* needed for dns-strings in regkeys */
2180 longname = get_mydnsfullname();
2182 nt_status = NT_STATUS_UNSUCCESSFUL;
2186 /* do something for all printers */
2187 for (i = 0; i < num_printers; i++) {
2189 uint32_t value_offered = 0, value_needed;
2190 uint32_t data_offered = 0, data_needed;
2191 enum winreg_Type type;
2192 uint8_t *buffer = NULL;
2193 const char *value_name = NULL;
2195 /* do some initialization */
2196 printername = info_enum[i].info2.printername;
2197 sharename = info_enum[i].info2.sharename;
2199 if (!printername || !sharename) {
2200 nt_status = NT_STATUS_UNSUCCESSFUL;
2203 /* we can reset NT_STATUS here because we do not
2204 get any real NT_STATUS-codes anymore from now on */
2205 nt_status = NT_STATUS_UNSUCCESSFUL;
2207 d_printf("migrating printer settings for: [%s] / [%s]\n",
2208 printername, sharename);
2211 /* open src printer handle */
2212 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2213 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2216 /* open dst printer handle */
2217 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2218 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2221 /* check for existing dst printer */
2222 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2227 /* STEP 1: COPY DEVICE-MODE and other
2228 PRINTER_INFO_2-attributes
2231 info_dst.info2 = info_enum[i].info2;
2233 /* why is the port always disconnected when the printer
2234 is correctly installed (incl. driver ???) */
2235 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2237 /* check if printer is published */
2238 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2240 /* check for existing dst printer */
2241 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2244 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2246 /* ignore false from setprinter due to WERR_IO_PENDING */
2247 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2249 DEBUG(3,("republished printer\n"));
2252 if (info_enum[i].info2.devmode != NULL) {
2254 /* copy devmode (info level 2) */
2255 info_dst.info2.devmode = info_enum[i].info2.devmode;
2257 /* do not copy security descriptor (we have another
2258 * command for that) */
2259 info_dst.info2.secdesc = NULL;
2262 info_dst.info2.devmode.devicename =
2263 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2264 longname, printername);
2265 if (!info_dst.info2.devmode.devicename) {
2266 nt_status = NT_STATUS_NO_MEMORY;
2270 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2274 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2277 /* STEP 2: COPY REGISTRY VALUES */
2279 /* please keep in mind that samba parse_spools gives horribly
2280 crippled results when used to rpccli_spoolss_enumprinterdataex
2281 a win2k3-server. (Bugzilla #1851)
2282 FIXME: IIRC I've seen it too on a win2k-server
2285 /* enumerate data on src handle */
2286 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2298 data_offered = data_needed;
2299 value_offered = value_needed;
2300 buffer = talloc_zero_array(mem_ctx, uint8_t, data_needed);
2301 value_name = talloc_zero_array(mem_ctx, char, value_needed);
2303 /* loop for all printerdata of "PrinterDriverData" */
2304 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2306 nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
2317 /* loop for all reg_keys */
2318 if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
2320 struct regval_blob v;
2322 union spoolss_PrinterData printer_data;
2325 if (c->opt_verbose) {
2326 fstrcpy(v.valuename, value_name);
2328 v.size = data_offered;
2330 display_reg_value(SPOOL_PRINTERDATA_KEY, v);
2333 result = pull_spoolss_PrinterData(mem_ctx,
2337 if (!W_ERROR_IS_OK(result)) {
2342 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2343 &hnd_dst, value_name,
2344 type, printer_data))
2347 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2352 /* STEP 3: COPY SUBKEY VALUES */
2354 /* here we need to enum all printer_keys and then work
2355 on the result with enum_printer_key_ex. nt4 does not
2356 respond to enumprinterkey, win2k does, so continue
2357 in case of an error */
2359 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2360 printf("got no key-data\n");
2365 /* work on a list of printer keys
2366 each key has to be enumerated to get all required
2367 information. information is then set via setprinterdataex-calls */
2369 if (keylist == NULL)
2372 for (i=0; keylist && keylist[i] != NULL; i++) {
2374 const char *subkey = keylist[i];
2376 struct spoolss_PrinterEnumValues *info;
2378 /* enumerate all src subkeys */
2379 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2385 for (j=0; j < count; j++) {
2387 struct regval_blob value;
2390 /* although samba replies with sane data in most cases we
2391 should try to avoid writing wrong registry data */
2393 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME) ||
2394 strequal(info[j].value_name, SPOOL_REG_UNCNAME) ||
2395 strequal(info[j].value_name, SPOOL_REG_URL) ||
2396 strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME) ||
2397 strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2399 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
2401 /* although windows uses a multi-sz, we use a sz */
2402 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2403 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2406 if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
2408 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2409 nt_status = NT_STATUS_NO_MEMORY;
2412 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2413 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2416 if (strequal(info[j].value_name, SPOOL_REG_URL)) {
2421 /* FIXME: should we really do that ??? */
2422 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2423 nt_status = NT_STATUS_NO_MEMORY;
2426 init_unistr2(&data, url, UNI_STR_TERMINATE);
2427 fstrcpy(value.valuename, SPOOL_REG_URL);
2431 if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2433 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2434 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2437 if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
2439 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2440 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2443 value.type = REG_SZ;
2444 value.size = data.uni_str_len * 2;
2446 value.data_p = (uint8_t *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2448 value.data_p = NULL;
2452 display_reg_value(subkey, value);
2454 /* here we have to set all subkeys on the dst server */
2455 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2461 struct regval_blob v;
2464 result = push_spoolss_PrinterData(mem_ctx, &blob,
2467 if (!W_ERROR_IS_OK(result)) {
2471 fstrcpy(v.valuename, info[j].value_name);
2472 v.type = info[j].type;
2473 v.data_p = blob.data;
2474 v.size = blob.length;
2476 if (c->opt_verbose) {
2477 display_reg_value(subkey, v);
2480 /* here we have to set all subkeys on the dst server */
2481 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2488 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2489 subkey, info[j].value_name));
2494 TALLOC_FREE(keylist);
2496 /* close printer handles here */
2497 if (is_valid_policy_hnd(&hnd_src)) {
2498 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2501 if (is_valid_policy_hnd(&hnd_dst)) {
2502 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2507 nt_status = NT_STATUS_OK;
2510 SAFE_FREE(devicename);
2512 SAFE_FREE(unc_name);
2514 if (is_valid_policy_hnd(&hnd_src))
2515 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2517 if (is_valid_policy_hnd(&hnd_dst))
2518 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2521 cli_shutdown(cli_dst);