s3-torture/denytest.c: replace cli_read_old() with cli_read()
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc_printer.c
blob8e8b140d37f34e6983bc772653f92baf73f39a4c
1 /*
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/>.
19 #include "includes.h"
20 #include "system/filesys.h"
21 #include "utils/net.h"
22 #include "rpc_client/rpc_client.h"
23 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
24 #include "rpc_client/cli_spoolss.h"
25 #include "rpc_client/init_spoolss.h"
26 #include "nt_printing.h"
27 #include "registry/reg_objects.h"
28 #include "../libcli/security/security.h"
29 #include "../libcli/registry/util_reg.h"
30 #include "libsmb/libsmb.h"
32 /* support itanium as well */
33 static const struct print_architecture_table_node archi_table[]= {
35 {"Windows 4.0", "WIN40", 0 },
36 {"Windows NT x86", "W32X86", 2 },
37 {"Windows NT x86", "W32X86", 3 },
38 {"Windows NT R4000", "W32MIPS", 2 },
39 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
40 {"Windows NT PowerPC", "W32PPC", 2 },
41 {"Windows IA64", "IA64", 3 },
42 {"Windows x64", "x64", 3 },
43 {NULL, "", -1 }
47 /**
48 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
49 * It is here for debugging purpose and should be removed later on.
50 **/
52 /****************************************************************************
53 Printer info level 3 display function.
54 ****************************************************************************/
56 static void display_print_driver3(struct spoolss_DriverInfo3 *r)
58 int i;
60 if (!r) {
61 return;
64 printf(_("Printer Driver Info 3:\n"));
65 printf(_("\tVersion: [%x]\n"), r->version);
66 printf(_("\tDriver Name: [%s]\n"), r->driver_name);
67 printf(_("\tArchitecture: [%s]\n"), r->architecture);
68 printf(_("\tDriver Path: [%s]\n"), r->driver_path);
69 printf(_("\tDatafile: [%s]\n"), r->data_file);
70 printf(_("\tConfigfile: [%s]\n\n"), r->config_file);
71 printf(_("\tHelpfile: [%s]\n\n"), r->help_file);
73 for (i=0; r->dependent_files && r->dependent_files[i] != NULL; i++) {
74 printf(_("\tDependentfiles: [%s]\n"), r->dependent_files[i]);
77 printf("\n");
79 printf(_("\tMonitorname: [%s]\n"), r->monitor_name);
80 printf(_("\tDefaultdatatype: [%s]\n\n"), r->default_datatype);
83 static void display_reg_value(const char *subkey, struct regval_blob *value)
85 const char *text;
86 DATA_BLOB blob;
88 switch(regval_type(value)) {
89 case REG_DWORD:
90 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey,
91 regval_name(value), *((uint32_t *) regval_data_p(value)));
92 break;
94 case REG_SZ:
95 blob = data_blob_const(regval_data_p(value), regval_size(value));
96 pull_reg_sz(talloc_tos(), &blob, &text);
97 if (!text) {
98 break;
100 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, regval_name(value),
101 text);
102 break;
104 case REG_BINARY:
105 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
106 "displayed\n"),
107 subkey, regval_name(value));
108 break;
110 case REG_MULTI_SZ: {
111 uint32_t i;
112 const char **values;
113 blob = data_blob_const(regval_data_p(value), regval_size(value));
115 if (!pull_reg_multi_sz(NULL, &blob, &values)) {
116 d_printf("pull_reg_multi_sz failed\n");
117 break;
120 printf("%s: REG_MULTI_SZ: \n", regval_name(value));
121 for (i=0; values[i] != NULL; i++) {
122 d_printf("%s\n", values[i]);
124 TALLOC_FREE(values);
125 break;
128 default:
129 d_printf(_("\t%s: unknown type %d\n"), regval_name(value),
130 regval_type(value));
136 * Copies ACLs, DOS-attributes and timestamps from one
137 * file or directory from one connected share to another connected share
139 * @param c A net_context structure
140 * @param mem_ctx A talloc-context
141 * @param cli_share_src A connected cli_state
142 * @param cli_share_dst A connected cli_state
143 * @param src_file The source file-name
144 * @param dst_file The destination file-name
145 * @param copy_acls Whether to copy acls
146 * @param copy_attrs Whether to copy DOS attributes
147 * @param copy_timestamps Whether to preserve timestamps
148 * @param is_file Whether this file is a file or a dir
150 * @return Normal NTSTATUS return.
153 NTSTATUS net_copy_fileattr(struct net_context *c,
154 TALLOC_CTX *mem_ctx,
155 struct cli_state *cli_share_src,
156 struct cli_state *cli_share_dst,
157 const char *src_name, const char *dst_name,
158 bool copy_acls, bool copy_attrs,
159 bool copy_timestamps, bool is_file)
161 NTSTATUS nt_status;
162 uint16_t fnum_src = 0;
163 uint16_t fnum_dst = 0;
164 struct security_descriptor *sd = NULL;
165 uint16_t attr;
166 time_t f_atime, f_ctime, f_mtime;
168 if (!copy_timestamps && !copy_acls && !copy_attrs)
169 return NT_STATUS_OK;
171 /* open file/dir on the originating server */
173 DEBUGADD(3,("opening %s %s on originating server\n",
174 is_file?"file":"dir", src_name));
176 nt_status = cli_ntcreate(cli_share_src, src_name, 0,
177 READ_CONTROL_ACCESS, 0,
178 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
179 0x0, 0x0, &fnum_src);
180 if (!NT_STATUS_IS_OK(nt_status)) {
181 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
182 is_file?"file":"dir", src_name, nt_errstr(nt_status)));
183 goto out;
186 if (copy_acls) {
187 /* get the security descriptor */
188 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
189 if (!sd) {
190 DEBUG(0,("failed to get security descriptor: %s\n",
191 cli_errstr(cli_share_src)));
192 nt_status = cli_nt_error(cli_share_src);
193 goto out;
196 if (c->opt_verbose && DEBUGLEVEL >= 3)
197 display_sec_desc(sd);
200 if (copy_attrs || copy_timestamps) {
202 /* get file attributes */
203 nt_status = cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
204 &f_ctime, &f_atime, &f_mtime);
205 if (!NT_STATUS_IS_OK(nt_status)) {
206 DEBUG(0,("failed to get file-attrs: %s\n",
207 nt_errstr(nt_status)));
208 goto out;
212 /* open the file/dir on the destination server */
213 nt_status = cli_ntcreate(cli_share_dst, dst_name, 0,
214 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
215 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
216 0x0, 0x0, &fnum_dst);
217 if (!NT_STATUS_IS_OK(nt_status)) {
218 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
219 is_file?"file":"dir", dst_name, nt_errstr(nt_status)));
220 goto out;
223 if (copy_timestamps) {
224 /* set timestamps */
225 nt_status = cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime);
226 if (!NT_STATUS_IS_OK(nt_status)) {
227 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
228 nt_errstr(nt_status)));
229 goto out;
233 if (copy_acls) {
234 /* set acls */
235 nt_status = cli_set_secdesc(cli_share_dst, fnum_dst, sd);
236 if (!NT_STATUS_IS_OK(nt_status)) {
237 DEBUG(0, ("could not set secdesc on %s: %s\n",
238 dst_name, nt_errstr(nt_status)));
239 goto out;
243 if (copy_attrs) {
244 /* set attrs */
245 nt_status = cli_setatr(cli_share_dst, dst_name, attr, 0);
246 if (!NT_STATUS_IS_OK(nt_status)) {
247 DEBUG(0,("failed to set file-attrs: %s\n",
248 nt_errstr(nt_status)));
249 goto out;
254 /* closing files */
255 nt_status = cli_close(cli_share_src, fnum_src);
256 if (!NT_STATUS_IS_OK(nt_status)) {
257 d_fprintf(stderr,
258 _("could not close %s on originating server: %s\n"),
259 is_file?"file":"dir", nt_errstr(nt_status));
260 goto out;
263 nt_status = cli_close(cli_share_dst, fnum_dst);
264 if (!NT_STATUS_IS_OK(nt_status)) {
265 d_fprintf(stderr,
266 _("could not close %s on destination server: %s\n"),
267 is_file?"file":"dir", nt_errstr(nt_status));
268 goto out;
272 nt_status = NT_STATUS_OK;
274 out:
276 /* cleaning up */
277 if (fnum_src)
278 cli_close(cli_share_src, fnum_src);
280 if (fnum_dst)
281 cli_close(cli_share_dst, fnum_dst);
283 return nt_status;
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,
304 TALLOC_CTX *mem_ctx,
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;
316 char *data = NULL;
317 off_t nread = 0;
320 if (!src_name || !dst_name)
321 goto out;
323 if (cli_share_src == NULL || cli_share_dst == NULL)
324 goto out;
326 /* open on the originating server */
327 DEBUGADD(3,("opening %s %s on originating server\n",
328 is_file ? "file":"dir", src_name));
329 if (is_file)
330 nt_status = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE, &fnum_src);
331 else
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, nt_errstr(nt_status)));
339 goto out;
343 if (is_file) {
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, nt_errstr(nt_status)));
353 goto out;
356 /* allocate memory */
357 if (!(data = (char *)SMB_MALLOC(read_size))) {
358 d_fprintf(stderr, _("malloc fail for size %d\n"),
359 read_size);
360 nt_status = NT_STATUS_NO_MEMORY;
361 goto out;
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_state_remote_name(cli_share_src),
372 cli_share_src->share, src_name,
373 cli_state_remote_name(cli_share_dst),
374 cli_share_dst->share, dst_name,
375 copy_acls ? _("with") : _("without"),
376 copy_attrs ? _("with") : _("without"),
377 copy_timestamps ? _("(preserving timestamps)") : "" );
381 while (is_file) {
383 /* copying file */
384 int n;
385 n = cli_read_old(cli_share_src, fnum_src, data, nread,
386 read_size);
388 if (n <= 0)
389 break;
391 nt_status = cli_writeall(cli_share_dst, fnum_dst, 0,
392 (uint8_t *)data, nread, n, NULL);
394 if (!NT_STATUS_IS_OK(nt_status)) {
395 d_fprintf(stderr, _("Error writing file: %s\n"),
396 nt_errstr(nt_status));
397 goto out;
400 nread += n;
404 if (!is_file && !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
406 /* creating dir */
407 DEBUGADD(3,("creating dir %s on the destination server\n",
408 dst_name));
410 nt_status = cli_mkdir(cli_share_dst, dst_name);
411 if (!NT_STATUS_IS_OK(nt_status)) {
412 DEBUG(0,("cannot create directory %s: %s\n",
413 dst_name, nt_errstr(nt_status)));
414 nt_status = NT_STATUS_NO_SUCH_FILE;
418 nt_status = cli_chkpath(cli_share_dst, dst_name);
419 if (!NT_STATUS_IS_OK(nt_status)) {
420 d_fprintf(stderr,
421 _("cannot check for directory %s: %s\n"),
422 dst_name, nt_errstr(nt_status));
423 goto out;
428 /* closing files */
429 nt_status = cli_close(cli_share_src, fnum_src);
430 if (!NT_STATUS_IS_OK(nt_status)) {
431 d_fprintf(stderr,
432 _("could not close file on originating server: %s\n"),
433 nt_errstr(nt_status));
434 goto out;
437 if (is_file) {
438 nt_status = cli_close(cli_share_dst, fnum_dst);
439 if (!NT_STATUS_IS_OK(nt_status)) {
440 d_fprintf(stderr,
441 _("could not close file on destination server: %s\n"),
442 nt_errstr(nt_status));
443 goto out;
447 /* possibly we have to copy some file-attributes / acls / sd */
448 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
449 src_name, dst_name, copy_acls,
450 copy_attrs, copy_timestamps, is_file);
451 if (!NT_STATUS_IS_OK(nt_status))
452 goto out;
455 nt_status = NT_STATUS_OK;
457 out:
459 /* cleaning up */
460 if (fnum_src)
461 cli_close(cli_share_src, fnum_src);
463 if (fnum_dst)
464 cli_close(cli_share_dst, fnum_dst);
466 SAFE_FREE(data);
468 return nt_status;
472 * Copy a driverfile from on connected share to another connected share
473 * This silently assumes that a driver-file is picked up from
475 * \\src_server\print$\{arch}\{version}\file
477 * and copied to
479 * \\dst_server\print$\{arch}\file
481 * to be added via setdriver-calls later.
482 * @param c A net_context structure
483 * @param mem_ctx A talloc-context
484 * @param cli_share_src A cli_state connected to source print$-share
485 * @param cli_share_dst A cli_state connected to destination print$-share
486 * @param file The file-name to be copied
487 * @param short_archi The name of the driver-architecture (short form)
489 * @return Normal NTSTATUS return.
492 static NTSTATUS net_copy_driverfile(struct net_context *c,
493 TALLOC_CTX *mem_ctx,
494 struct cli_state *cli_share_src,
495 struct cli_state *cli_share_dst,
496 const char *file, const char *short_archi) {
498 const char *p;
499 char *src_name;
500 char *dst_name;
501 char *version = NULL;
502 char *filename = NULL;
503 char *tok;
505 if (!file) {
506 return NT_STATUS_OK;
509 /* scroll through the file until we have the part
510 beyond archi_table.short_archi */
511 p = file;
512 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
513 if (strequal(tok, short_archi)) {
514 next_token_talloc(mem_ctx, &p, &version, "\\");
515 next_token_talloc(mem_ctx, &p, &filename, "\\");
519 if (version == NULL || filename == NULL) {
520 return NT_STATUS_UNSUCCESSFUL;
523 /* build source file name */
524 src_name = talloc_asprintf(mem_ctx, "\\%s\\%s\\%s",
525 short_archi, version, filename);
526 if (src_name == NULL) {
527 return NT_STATUS_NO_MEMORY;
530 /* create destination file name */
531 dst_name = talloc_asprintf(mem_ctx, "\\%s\\%s", short_archi, filename);
532 if (dst_name == NULL) {
533 return NT_STATUS_NO_MEMORY;
537 /* finally copy the file */
538 return net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
539 src_name, dst_name, false, false, false, true);
543 * Check for existing Architecture directory on a given server
545 * @param cli_share A cli_state connected to a print$-share
546 * @param short_archi The Architecture for the print-driver
548 * @return Normal NTSTATUS return.
551 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
554 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
555 char *dir;
557 if (asprintf(&dir, "\\%s", short_archi) < 0) {
558 return NT_STATUS_NO_MEMORY;
561 DEBUG(10,("creating print-driver dir for architecture: %s\n",
562 short_archi));
564 nt_status = cli_mkdir(cli_share, dir);
565 if (!NT_STATUS_IS_OK(nt_status)) {
566 DEBUG(1,("cannot create directory %s: %s\n",
567 dir, nt_errstr(nt_status)));
570 nt_status = cli_chkpath(cli_share, dir);
571 if (!NT_STATUS_IS_OK(nt_status)) {
572 d_fprintf(stderr, _("cannot check %s: %s\n"),
573 dir, nt_errstr(nt_status));
574 goto out;
577 nt_status = NT_STATUS_OK;
579 out:
580 SAFE_FREE(dir);
581 return nt_status;
585 * Copy a print-driver (level 3) from one connected print$-share to another
586 * connected print$-share
588 * @param c A net_context structure
589 * @param mem_ctx A talloc-context
590 * @param cli_share_src A cli_state connected to a print$-share
591 * @param cli_share_dst A cli_state connected to a print$-share
592 * @param short_archi The Architecture for the print-driver
593 * @param i1 The DRIVER_INFO_3-struct
595 * @return Normal NTSTATUS return.
598 static NTSTATUS copy_print_driver_3(struct net_context *c,
599 TALLOC_CTX *mem_ctx,
600 struct cli_state *cli_share_src,
601 struct cli_state *cli_share_dst,
602 const char *short_archi,
603 struct spoolss_DriverInfo3 *r)
605 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
606 int i;
608 if (r == NULL) {
609 return nt_status;
612 if (c->opt_verbose)
613 d_printf(_("copying driver: [%s], for architecture: [%s], "
614 "version: [%d]\n"),
615 r->driver_name, short_archi, r->version);
617 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
618 r->driver_path, short_archi);
619 if (!NT_STATUS_IS_OK(nt_status))
620 return nt_status;
622 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
623 r->data_file, short_archi);
624 if (!NT_STATUS_IS_OK(nt_status))
625 return nt_status;
627 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
628 r->config_file, short_archi);
629 if (!NT_STATUS_IS_OK(nt_status))
630 return nt_status;
632 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
633 r->help_file, short_archi);
634 if (!NT_STATUS_IS_OK(nt_status))
635 return nt_status;
637 for (i=0; r->dependent_files[i] != NULL; i++) {
639 nt_status = net_copy_driverfile(c, mem_ctx,
640 cli_share_src, cli_share_dst,
641 r->dependent_files[i], short_archi);
642 if (!NT_STATUS_IS_OK(nt_status)) {
643 return nt_status;
647 return NT_STATUS_OK;
651 * net_spoolss-functions
652 * =====================
654 * the net_spoolss-functions aim to simplify spoolss-client-functions
655 * required during the migration-process wrt buffer-sizes, returned
656 * error-codes, etc.
658 * this greatly reduces the complexitiy of the migrate-functions.
662 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
663 TALLOC_CTX *mem_ctx,
664 char *name,
665 uint32_t flags,
666 uint32_t level,
667 uint32_t *num_printers,
668 union spoolss_PrinterInfo **info)
670 WERROR result;
672 /* enum printers */
674 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
675 flags,
676 name,
677 level,
679 num_printers,
680 info);
681 if (!W_ERROR_IS_OK(result)) {
682 printf(_("cannot enum printers: %s\n"), win_errstr(result));
683 return false;
686 return true;
689 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
690 TALLOC_CTX *mem_ctx,
691 const char *printername,
692 uint32_t access_required,
693 const char *username,
694 struct policy_handle *hnd)
696 WERROR result;
697 fstring printername2;
699 fstrcpy(printername2, pipe_hnd->srv_name_slash);
700 fstrcat(printername2, "\\");
701 fstrcat(printername2, printername);
703 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
704 pipe_hnd->srv_name_slash, username, printername2, access_required));
706 /* open printer */
707 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
708 printername2,
709 access_required,
710 hnd);
712 /* be more verbose */
713 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
714 d_fprintf(stderr,
715 _("no access to printer [%s] on [%s] for user [%s] "
716 "granted\n"),
717 printername2, pipe_hnd->srv_name_slash, username);
718 return false;
721 if (!W_ERROR_IS_OK(result)) {
722 d_fprintf(stderr,_("cannot open printer %s on server %s: %s\n"),
723 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
724 return false;
727 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
728 printername2, pipe_hnd->srv_name_slash));
730 return true;
733 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
734 TALLOC_CTX *mem_ctx,
735 struct policy_handle *hnd,
736 uint32_t level,
737 union spoolss_PrinterInfo *info)
739 WERROR result;
741 /* getprinter call */
742 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
743 hnd,
744 level,
745 0, /* offered */
746 info);
747 if (!W_ERROR_IS_OK(result)) {
748 printf(_("cannot get printer-info: %s\n"), win_errstr(result));
749 return false;
752 return true;
755 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
756 TALLOC_CTX *mem_ctx,
757 struct policy_handle *hnd,
758 uint32_t level,
759 union spoolss_PrinterInfo *info)
761 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
762 WERROR result;
763 NTSTATUS status;
764 struct spoolss_SetPrinterInfoCtr info_ctr;
765 struct spoolss_SetPrinterInfo2 info2;
766 struct spoolss_DevmodeContainer devmode_ctr;
767 struct sec_desc_buf secdesc_ctr;
769 ZERO_STRUCT(devmode_ctr);
770 ZERO_STRUCT(secdesc_ctr);
772 /* setprinter call */
774 info_ctr.level = level;
775 switch (level) {
776 case 0:
777 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)
778 (void *)&info->info0;
779 break;
780 case 1:
781 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)
782 (void *)&info->info1;
783 break;
784 case 2:
785 spoolss_printerinfo2_to_setprinterinfo2(&info->info2, &info2);
786 info_ctr.info.info2 = &info2;
787 break;
788 case 3:
789 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
790 (void *)&info->info3;
791 break;
792 case 4:
793 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)
794 (void *)&info->info4;
795 break;
796 case 5:
797 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)
798 (void *)&info->info5;
799 break;
800 case 6:
801 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)
802 (void *)&info->info6;
803 break;
804 case 7:
805 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
806 (void *)&info->info7;
807 break;
808 #if 0 /* FIXME GD */
809 case 8:
810 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)
811 (void *)&info->info8;
812 break;
813 case 9:
814 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)
815 (void *)&info->info9;
816 break;
817 #endif
818 default:
819 break; /* FIXME */
822 status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
823 hnd,
824 &info_ctr,
825 &devmode_ctr,
826 &secdesc_ctr,
827 0, /* command */
828 &result);
829 if (!NT_STATUS_IS_OK(status)) {
830 printf(_("cannot set printer-info: %s\n"), nt_errstr(status));
831 return false;
833 if (!W_ERROR_IS_OK(result)) {
834 printf(_("cannot set printer-info: %s\n"), win_errstr(result));
835 return false;
838 return true;
842 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
843 TALLOC_CTX *mem_ctx,
844 struct policy_handle *hnd,
845 const char *value_name,
846 enum winreg_Type type,
847 uint8_t *data,
848 uint32_t offered)
850 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
851 WERROR result;
852 NTSTATUS status;
854 /* setprinterdata call */
855 status = dcerpc_spoolss_SetPrinterData(b, mem_ctx,
856 hnd,
857 value_name,
858 type,
859 data,
860 offered,
861 &result);
862 if (!NT_STATUS_IS_OK(status)) {
863 printf (_("unable to set printerdata: %s\n"),
864 nt_errstr(status));
865 return false;
867 if (!W_ERROR_IS_OK(result)) {
868 printf (_("unable to set printerdata: %s\n"),
869 win_errstr(result));
870 return false;
873 return true;
877 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
878 TALLOC_CTX *mem_ctx,
879 struct policy_handle *hnd,
880 const char *keyname,
881 const char ***keylist)
883 WERROR result;
885 /* enumprinterkey call */
886 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
888 if (!W_ERROR_IS_OK(result)) {
889 printf(_("enumprinterkey failed: %s\n"), win_errstr(result));
890 return false;
893 return true;
896 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
897 TALLOC_CTX *mem_ctx,
898 uint32_t offered,
899 struct policy_handle *hnd,
900 const char *keyname,
901 uint32_t *count,
902 struct spoolss_PrinterEnumValues **info)
904 WERROR result;
906 /* enumprinterdataex call */
907 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
908 hnd,
909 keyname,
910 0, /* offered */
911 count,
912 info);
914 if (!W_ERROR_IS_OK(result)) {
915 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result));
916 return false;
919 return true;
923 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
924 TALLOC_CTX *mem_ctx,
925 struct policy_handle *hnd,
926 const char *keyname,
927 struct regval_blob *value)
929 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
930 WERROR result;
931 NTSTATUS status;
933 /* setprinterdataex call */
934 status = dcerpc_spoolss_SetPrinterDataEx(b, mem_ctx,
935 hnd,
936 keyname,
937 regval_name(value),
938 regval_type(value),
939 regval_data_p(value),
940 regval_size(value),
941 &result);
942 if (!NT_STATUS_IS_OK(status)) {
943 printf(_("could not set printerdataex: %s\n"),
944 nt_errstr(status));
945 return false;
947 if (!W_ERROR_IS_OK(result)) {
948 printf(_("could not set printerdataex: %s\n"),
949 win_errstr(result));
950 return false;
953 return true;
956 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
957 TALLOC_CTX *mem_ctx,
958 struct policy_handle *hnd,
959 int level,
960 uint32_t *num_forms,
961 union spoolss_FormInfo **forms)
963 WERROR result;
965 /* enumforms call */
966 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
967 hnd,
968 level,
970 num_forms,
971 forms);
972 if (!W_ERROR_IS_OK(result)) {
973 printf(_("could not enum forms: %s\n"), win_errstr(result));
974 return false;
977 return true;
980 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
981 TALLOC_CTX *mem_ctx,
982 uint32_t level, const char *env,
983 uint32_t *count,
984 union spoolss_DriverInfo **info)
986 WERROR result;
988 /* enumprinterdrivers call */
989 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
990 pipe_hnd->srv_name_slash,
991 env,
992 level,
994 count,
995 info);
996 if (!W_ERROR_IS_OK(result)) {
997 if (W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
998 printf(_("cannot enum drivers for environment %s: %s\n"), env,
999 win_errstr(result));
1000 return false;
1001 } else {
1002 printf(_("Server does not support environment [%s]\n"),
1003 env);
1007 return true;
1010 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
1011 TALLOC_CTX *mem_ctx,
1012 struct policy_handle *hnd, uint32_t level,
1013 const char *env, int version,
1014 union spoolss_DriverInfo *info)
1016 WERROR result;
1017 uint32_t server_major_version;
1018 uint32_t server_minor_version;
1020 /* getprinterdriver call */
1021 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
1022 hnd,
1023 env,
1024 level,
1026 version,
1028 info,
1029 &server_major_version,
1030 &server_minor_version);
1031 if (!W_ERROR_IS_OK(result)) {
1032 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1033 env, win_errstr(result)));
1034 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1035 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1036 printf(_("cannot get driver: %s\n"),
1037 win_errstr(result));
1039 return false;
1042 return true;
1046 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1047 TALLOC_CTX *mem_ctx, uint32_t level,
1048 union spoolss_DriverInfo *info)
1050 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1051 WERROR result;
1052 NTSTATUS status;
1053 struct spoolss_AddDriverInfoCtr info_ctr;
1055 info_ctr.level = level;
1057 switch (level) {
1058 case 2:
1059 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)
1060 (void *)&info->info2;
1061 break;
1062 case 3:
1063 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)
1064 (void *)&info->info3;
1065 break;
1066 default:
1067 printf(_("unsupported info level: %d\n"), level);
1068 return false;
1071 /* addprinterdriver call */
1072 status = dcerpc_spoolss_AddPrinterDriver(b, mem_ctx,
1073 pipe_hnd->srv_name_slash,
1074 &info_ctr,
1075 &result);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 printf(_("cannot add driver: %s\n"), nt_errstr(status));
1078 return false;
1080 /* be more verbose */
1081 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1082 printf(_("You are not allowed to add drivers\n"));
1083 return false;
1085 if (!W_ERROR_IS_OK(result)) {
1086 printf(_("cannot add driver: %s\n"), win_errstr(result));
1087 return false;
1090 return true;
1094 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1095 * for a single printer or for all printers depending on argc/argv
1098 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1099 TALLOC_CTX *mem_ctx,
1100 int level,
1101 int argc,
1102 const char **argv,
1103 uint32_t *num_printers,
1104 union spoolss_PrinterInfo **info_p)
1106 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1107 struct policy_handle hnd;
1108 WERROR werr;
1110 /* no arguments given, enumerate all printers */
1111 if (argc == 0) {
1113 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1114 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1115 level, num_printers, info_p))
1116 return false;
1118 goto out;
1121 /* argument given, get a single printer by name */
1122 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1123 MAXIMUM_ALLOWED_ACCESS,
1124 pipe_hnd->auth->user_name,
1125 &hnd))
1126 return false;
1128 *info_p = talloc_zero(mem_ctx, union spoolss_PrinterInfo);
1129 if (*info_p == NULL) {
1130 return false;
1133 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1134 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1135 return false;
1138 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1140 *num_printers = 1;
1142 out:
1143 DEBUG(3,("got %d printers\n", *num_printers));
1145 return true;
1150 * List print-queues (including local printers that are not shared)
1152 * All parameters are provided by the run_rpc_command function, except for
1153 * argc, argv which are passed through.
1155 * @param c A net_context structure
1156 * @param domain_sid The domain sid aquired from the remote server
1157 * @param cli A cli_state connected to the server.
1158 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1159 * @param argc Standard main() style argc
1160 * @param argv Standard main() style argv. Initial components are already
1161 * stripped
1163 * @return Normal NTSTATUS return.
1166 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1167 const struct dom_sid *domain_sid,
1168 const char *domain_name,
1169 struct cli_state *cli,
1170 struct rpc_pipe_client *pipe_hnd,
1171 TALLOC_CTX *mem_ctx,
1172 int argc,
1173 const char **argv)
1175 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1176 uint32_t i, num_printers;
1177 uint32_t level = 2;
1178 const char *printername, *sharename;
1179 union spoolss_PrinterInfo *info;
1181 printf("listing printers\n");
1183 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1184 return nt_status;
1186 for (i = 0; i < num_printers; i++) {
1188 /* do some initialization */
1189 printername = info[i].info2.printername;
1190 sharename = info[i].info2.sharename;
1192 if (printername && sharename) {
1193 d_printf(_("printer %d: %s, shared as: %s\n"),
1194 i+1, printername, sharename);
1198 return NT_STATUS_OK;
1202 * List printer-drivers from a server
1204 * All parameters are provided by the run_rpc_command function, except for
1205 * argc, argv which are passed through.
1207 * @param c A net_context structure
1208 * @param domain_sid The domain sid aquired from the remote server
1209 * @param cli A cli_state connected to the server.
1210 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1211 * @param argc Standard main() style argc
1212 * @param argv Standard main() style argv. Initial components are already
1213 * stripped
1215 * @return Normal NTSTATUS return.
1218 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1219 const struct dom_sid *domain_sid,
1220 const char *domain_name,
1221 struct cli_state *cli,
1222 struct rpc_pipe_client *pipe_hnd,
1223 TALLOC_CTX *mem_ctx,
1224 int argc,
1225 const char **argv)
1227 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1228 uint32_t i;
1229 uint32_t level = 3;
1230 union spoolss_DriverInfo *info;
1231 int d;
1233 printf(_("listing printer-drivers\n"));
1235 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1237 uint32_t num_drivers;
1239 /* enum remote drivers */
1240 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1241 archi_table[i].long_archi,
1242 &num_drivers, &info)) {
1243 nt_status = NT_STATUS_UNSUCCESSFUL;
1244 goto done;
1247 if (num_drivers == 0) {
1248 d_printf(_("no drivers found on server for "
1249 "architecture: [%s].\n"),
1250 archi_table[i].long_archi);
1251 continue;
1254 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1255 num_drivers, archi_table[i].long_archi);
1258 /* do something for all drivers for architecture */
1259 for (d = 0; d < num_drivers; d++) {
1260 display_print_driver3(&info[d].info3);
1264 nt_status = NT_STATUS_OK;
1266 done:
1267 return nt_status;
1272 * Publish print-queues with args-wrapper
1274 * @param cli A cli_state connected to the server.
1275 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1276 * @param argc Standard main() style argc
1277 * @param argv Standard main() style argv. Initial components are already
1278 * stripped
1279 * @param action
1281 * @return Normal NTSTATUS return.
1284 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1285 TALLOC_CTX *mem_ctx,
1286 int argc,
1287 const char **argv,
1288 uint32_t action)
1290 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1291 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1292 uint32_t i, num_printers;
1293 uint32_t level = 7;
1294 const char *printername, *sharename;
1295 union spoolss_PrinterInfo *info_enum;
1296 union spoolss_PrinterInfo info;
1297 struct spoolss_SetPrinterInfoCtr info_ctr;
1298 struct spoolss_DevmodeContainer devmode_ctr;
1299 struct sec_desc_buf secdesc_ctr;
1300 struct policy_handle hnd;
1301 WERROR result;
1302 const char *action_str;
1304 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1305 return nt_status;
1307 for (i = 0; i < num_printers; i++) {
1309 /* do some initialization */
1310 printername = info_enum[i].info2.printername;
1311 sharename = info_enum[i].info2.sharename;
1312 if (!printername || !sharename) {
1313 goto done;
1316 /* open printer handle */
1317 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1318 PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1319 goto done;
1321 /* check for existing dst printer */
1322 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1323 goto done;
1325 /* check action and set string */
1326 switch (action) {
1327 case DSPRINT_PUBLISH:
1328 action_str = N_("published");
1329 break;
1330 case DSPRINT_UPDATE:
1331 action_str = N_("updated");
1332 break;
1333 case DSPRINT_UNPUBLISH:
1334 action_str = N_("unpublished");
1335 break;
1336 default:
1337 action_str = N_("unknown action");
1338 printf(_("unknown action: %d\n"), action);
1339 break;
1342 info.info7.action = action;
1343 info_ctr.level = 7;
1344 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
1345 (void *)&info.info7;
1347 ZERO_STRUCT(devmode_ctr);
1348 ZERO_STRUCT(secdesc_ctr);
1350 nt_status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
1351 &hnd,
1352 &info_ctr,
1353 &devmode_ctr,
1354 &secdesc_ctr,
1355 0, /* command */
1356 &result);
1357 if (!NT_STATUS_IS_OK(nt_status)) {
1358 printf(_("cannot set printer-info: %s\n"),
1359 nt_errstr(nt_status));
1360 goto done;
1362 if (!W_ERROR_IS_OK(result) && !W_ERROR_EQUAL(result, WERR_IO_PENDING)) {
1363 if ((action == DSPRINT_UPDATE) && W_ERROR_EQUAL(result, W_ERROR(0x80070002))) {
1364 printf(_("printer not published yet\n"));
1365 } else {
1366 printf(_("cannot set printer-info: %s\n"),
1367 win_errstr(result));
1369 nt_status = werror_to_ntstatus(result);
1370 goto done;
1373 printf(_("successfully %s printer %s in Active Directory\n"),
1374 action_str, sharename);
1377 nt_status = NT_STATUS_OK;
1379 done:
1380 if (is_valid_policy_hnd(&hnd)) {
1381 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &result);
1384 return nt_status;
1387 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1388 const struct dom_sid *domain_sid,
1389 const char *domain_name,
1390 struct cli_state *cli,
1391 struct rpc_pipe_client *pipe_hnd,
1392 TALLOC_CTX *mem_ctx,
1393 int argc,
1394 const char **argv)
1396 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1399 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1400 const struct dom_sid *domain_sid,
1401 const char *domain_name,
1402 struct cli_state *cli,
1403 struct rpc_pipe_client *pipe_hnd,
1404 TALLOC_CTX *mem_ctx,
1405 int argc,
1406 const char **argv)
1408 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1411 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1412 const struct dom_sid *domain_sid,
1413 const char *domain_name,
1414 struct cli_state *cli,
1415 struct rpc_pipe_client *pipe_hnd,
1416 TALLOC_CTX *mem_ctx,
1417 int argc,
1418 const char **argv)
1420 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1424 * List print-queues w.r.t. their publishing state
1426 * All parameters are provided by the run_rpc_command function, except for
1427 * argc, argv which are passed through.
1429 * @param c A net_context structure
1430 * @param domain_sid The domain sid aquired from the remote server
1431 * @param cli A cli_state connected to the server.
1432 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1433 * @param argc Standard main() style argc
1434 * @param argv Standard main() style argv. Initial components are already
1435 * stripped
1437 * @return Normal NTSTATUS return.
1440 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1441 const struct dom_sid *domain_sid,
1442 const char *domain_name,
1443 struct cli_state *cli,
1444 struct rpc_pipe_client *pipe_hnd,
1445 TALLOC_CTX *mem_ctx,
1446 int argc,
1447 const char **argv)
1449 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1450 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1451 uint32_t i, num_printers;
1452 uint32_t level = 7;
1453 const char *printername, *sharename;
1454 union spoolss_PrinterInfo *info_enum;
1455 union spoolss_PrinterInfo info;
1456 struct policy_handle hnd;
1457 int state;
1458 WERROR werr;
1460 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1461 return nt_status;
1463 for (i = 0; i < num_printers; i++) {
1465 /* do some initialization */
1466 printername = info_enum[i].info2.printername;
1467 sharename = info_enum[i].info2.sharename;
1469 if (!printername || !sharename) {
1470 goto done;
1473 /* open printer handle */
1474 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1475 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1476 goto done;
1478 /* check for existing dst printer */
1479 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1480 goto done;
1482 if (!info.info7.guid) {
1483 goto done;
1485 state = info.info7.action;
1486 switch (state) {
1487 case DSPRINT_PUBLISH:
1488 printf(_("printer [%s] is published"),
1489 sharename);
1490 if (c->opt_verbose)
1491 printf(_(", guid: %s"),info.info7.guid);
1492 printf("\n");
1493 break;
1494 case DSPRINT_UNPUBLISH:
1495 printf(_("printer [%s] is unpublished\n"),
1496 sharename);
1497 break;
1498 case DSPRINT_UPDATE:
1499 printf(_("printer [%s] is currently updating\n"),
1500 sharename);
1501 break;
1502 default:
1503 printf(_("unknown state: %d\n"), state);
1504 break;
1508 nt_status = NT_STATUS_OK;
1510 done:
1511 if (is_valid_policy_hnd(&hnd)) {
1512 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1515 return nt_status;
1519 * Migrate Printer-ACLs from a source server to the destination server
1521 * All parameters are provided by the run_rpc_command function, except for
1522 * argc, argv which are passed through.
1524 * @param c A net_context structure
1525 * @param domain_sid The domain sid aquired from the remote server
1526 * @param cli A cli_state connected to the server.
1527 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1528 * @param argc Standard main() style argc
1529 * @param argv Standard main() style argv. Initial components are already
1530 * stripped
1532 * @return Normal NTSTATUS return.
1535 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1536 const struct dom_sid *domain_sid,
1537 const char *domain_name,
1538 struct cli_state *cli,
1539 struct rpc_pipe_client *pipe_hnd,
1540 TALLOC_CTX *mem_ctx,
1541 int argc,
1542 const char **argv)
1544 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1545 /* TODO: what now, info2 or info3 ?
1546 convince jerry that we should add clientside setacls level 3 at least
1548 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1549 uint32_t i = 0;
1550 uint32_t num_printers;
1551 uint32_t level = 2;
1552 const char *printername, *sharename;
1553 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1554 struct dcerpc_binding_handle *b_dst = NULL;
1555 struct policy_handle hnd_src, hnd_dst;
1556 union spoolss_PrinterInfo *info_enum;
1557 struct cli_state *cli_dst = NULL;
1558 union spoolss_PrinterInfo info_src, info_dst;
1559 WERROR werr;
1561 DEBUG(3,("copying printer ACLs\n"));
1563 /* connect destination PI_SPOOLSS */
1564 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1565 &ndr_table_spoolss.syntax_id);
1566 if (!NT_STATUS_IS_OK(nt_status)) {
1567 return nt_status;
1569 b_dst = pipe_hnd_dst->binding_handle;
1571 /* enum source printers */
1572 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1573 nt_status = NT_STATUS_UNSUCCESSFUL;
1574 goto done;
1577 if (!num_printers) {
1578 printf (_("no printers found on server.\n"));
1579 nt_status = NT_STATUS_OK;
1580 goto done;
1583 /* do something for all printers */
1584 for (i = 0; i < num_printers; i++) {
1586 /* do some initialization */
1587 printername = info_enum[i].info2.printername;
1588 sharename = info_enum[i].info2.sharename;
1590 if (!printername || !sharename) {
1591 nt_status = NT_STATUS_UNSUCCESSFUL;
1592 goto done;
1595 /* we can reset NT_STATUS here because we do not
1596 get any real NT_STATUS-codes anymore from now on */
1597 nt_status = NT_STATUS_UNSUCCESSFUL;
1599 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1600 printername, sharename);
1602 /* according to msdn you have specify these access-rights
1603 to see the security descriptor
1604 - READ_CONTROL (DACL)
1605 - ACCESS_SYSTEM_SECURITY (SACL)
1608 /* open src printer handle */
1609 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1610 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1611 goto done;
1613 /* open dst printer handle */
1614 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1615 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1616 goto done;
1618 /* check for existing dst printer */
1619 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1620 goto done;
1622 /* check for existing src printer */
1623 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1624 goto done;
1626 /* Copy Security Descriptor */
1628 /* copy secdesc (info level 2) */
1629 info_dst.info2.devmode = NULL;
1630 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1632 if (c->opt_verbose)
1633 display_sec_desc(info_dst.info2.secdesc);
1635 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1636 goto done;
1638 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1641 /* close printer handles here */
1642 if (is_valid_policy_hnd(&hnd_src)) {
1643 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
1646 if (is_valid_policy_hnd(&hnd_dst)) {
1647 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
1652 nt_status = NT_STATUS_OK;
1654 done:
1656 if (is_valid_policy_hnd(&hnd_src)) {
1657 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
1660 if (is_valid_policy_hnd(&hnd_dst)) {
1661 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
1664 if (cli_dst) {
1665 cli_shutdown(cli_dst);
1667 return nt_status;
1671 * Migrate printer-forms from a src server to the dst server
1673 * All parameters are provided by the run_rpc_command function, except for
1674 * argc, argv which are passed through.
1676 * @param c A net_context structure
1677 * @param domain_sid The domain sid aquired from the remote server
1678 * @param cli A cli_state connected to the server.
1679 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1680 * @param argc Standard main() style argc
1681 * @param argv Standard main() style argv. Initial components are already
1682 * stripped
1684 * @return Normal NTSTATUS return.
1687 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1688 const struct dom_sid *domain_sid,
1689 const char *domain_name,
1690 struct cli_state *cli,
1691 struct rpc_pipe_client *pipe_hnd,
1692 TALLOC_CTX *mem_ctx,
1693 int argc,
1694 const char **argv)
1696 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1697 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1698 WERROR result;
1699 uint32_t i, f;
1700 uint32_t num_printers;
1701 uint32_t level = 1;
1702 const char *printername, *sharename;
1703 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1704 struct dcerpc_binding_handle *b_dst = NULL;
1705 struct policy_handle hnd_src, hnd_dst;
1706 union spoolss_PrinterInfo *info_enum;
1707 union spoolss_PrinterInfo info_dst;
1708 uint32_t num_forms;
1709 union spoolss_FormInfo *forms;
1710 struct cli_state *cli_dst = NULL;
1712 DEBUG(3,("copying forms\n"));
1714 /* connect destination PI_SPOOLSS */
1715 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1716 &ndr_table_spoolss.syntax_id);
1717 if (!NT_STATUS_IS_OK(nt_status)) {
1718 return nt_status;
1720 b_dst = pipe_hnd_dst->binding_handle;
1722 /* enum src printers */
1723 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1724 nt_status = NT_STATUS_UNSUCCESSFUL;
1725 goto done;
1728 if (!num_printers) {
1729 printf (_("no printers found on server.\n"));
1730 nt_status = NT_STATUS_OK;
1731 goto done;
1734 /* do something for all printers */
1735 for (i = 0; i < num_printers; i++) {
1737 /* do some initialization */
1738 printername = info_enum[i].info2.printername;
1739 sharename = info_enum[i].info2.sharename;
1741 if (!printername || !sharename) {
1742 nt_status = NT_STATUS_UNSUCCESSFUL;
1743 goto done;
1745 /* we can reset NT_STATUS here because we do not
1746 get any real NT_STATUS-codes anymore from now on */
1747 nt_status = NT_STATUS_UNSUCCESSFUL;
1749 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1750 printername, sharename);
1753 /* open src printer handle */
1754 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1755 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1756 goto done;
1758 /* open dst printer handle */
1759 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1760 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1761 goto done;
1763 /* check for existing dst printer */
1764 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1765 goto done;
1767 /* finally migrate forms */
1768 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1769 goto done;
1771 DEBUG(1,("got %d forms for printer\n", num_forms));
1774 for (f = 0; f < num_forms; f++) {
1776 union spoolss_AddFormInfo info;
1777 NTSTATUS status;
1779 /* only migrate FORM_PRINTER types, according to jerry
1780 FORM_BUILTIN-types are hard-coded in samba */
1781 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1782 continue;
1784 if (c->opt_verbose)
1785 d_printf(_("\tmigrating form # %d [%s] of type "
1786 "[%d]\n"),
1787 f, forms[f].info1.form_name,
1788 forms[f].info1.flags);
1790 info.info1 = (struct spoolss_AddFormInfo1 *)
1791 (void *)&forms[f].info1;
1793 /* FIXME: there might be something wrong with samba's
1794 builtin-forms */
1795 status = dcerpc_spoolss_AddForm(b_dst, mem_ctx,
1796 &hnd_dst,
1798 info,
1799 &result);
1800 if (!NT_STATUS_IS_OK(status)) {
1801 d_printf(_("\tdcerpc_spoolss_AddForm form %d: [%s] - %s\n"),
1802 f, forms[f].info1.form_name, nt_errstr(status));
1803 continue;
1805 if (!W_ERROR_IS_OK(result)) {
1806 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1807 f, forms[f].info1.form_name);
1808 continue;
1811 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1812 forms[f].info1.form_name));
1816 /* close printer handles here */
1817 if (is_valid_policy_hnd(&hnd_src)) {
1818 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
1821 if (is_valid_policy_hnd(&hnd_dst)) {
1822 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
1826 nt_status = NT_STATUS_OK;
1828 done:
1830 if (is_valid_policy_hnd(&hnd_src)) {
1831 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
1834 if (is_valid_policy_hnd(&hnd_dst)) {
1835 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
1838 if (cli_dst) {
1839 cli_shutdown(cli_dst);
1841 return nt_status;
1845 * Migrate printer-drivers from a src server to the dst server
1847 * All parameters are provided by the run_rpc_command function, except for
1848 * argc, argv which are passed through.
1850 * @param c A net_context structure
1851 * @param domain_sid The domain sid aquired from the remote server
1852 * @param cli A cli_state connected to the server.
1853 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1854 * @param argc Standard main() style argc
1855 * @param argv Standard main() style argv. Initial components are already
1856 * stripped
1858 * @return Normal NTSTATUS return.
1861 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1862 const struct dom_sid *domain_sid,
1863 const char *domain_name,
1864 struct cli_state *cli,
1865 struct rpc_pipe_client *pipe_hnd,
1866 TALLOC_CTX *mem_ctx,
1867 int argc,
1868 const char **argv)
1870 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1871 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1872 uint32_t i, p;
1873 uint32_t num_printers;
1874 uint32_t level = 3;
1875 const char *printername, *sharename;
1876 bool got_src_driver_share = false;
1877 bool got_dst_driver_share = false;
1878 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1879 struct dcerpc_binding_handle *b_dst = NULL;
1880 struct policy_handle hnd_src, hnd_dst;
1881 union spoolss_DriverInfo drv_info_src;
1882 union spoolss_PrinterInfo *info_enum;
1883 union spoolss_PrinterInfo info_dst;
1884 struct cli_state *cli_dst = NULL;
1885 struct cli_state *cli_share_src = NULL;
1886 struct cli_state *cli_share_dst = NULL;
1887 const char *drivername = NULL;
1888 WERROR werr;
1890 DEBUG(3,("copying printer-drivers\n"));
1892 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1893 &ndr_table_spoolss.syntax_id);
1894 if (!NT_STATUS_IS_OK(nt_status)) {
1895 return nt_status;
1897 b_dst = pipe_hnd_dst->binding_handle;
1899 /* open print$-share on the src server */
1900 nt_status = connect_to_service(c, &cli_share_src,
1901 cli_state_remote_sockaddr(cli),
1902 cli_state_remote_name(cli),
1903 "print$", "A:");
1904 if (!NT_STATUS_IS_OK(nt_status))
1905 goto done;
1907 got_src_driver_share = true;
1910 /* open print$-share on the dst server */
1911 nt_status = connect_to_service(c, &cli_share_dst,
1912 cli_state_remote_sockaddr(cli_dst),
1913 cli_state_remote_name(cli_dst),
1914 "print$", "A:");
1915 if (!NT_STATUS_IS_OK(nt_status))
1916 return nt_status;
1918 got_dst_driver_share = true;
1921 /* enum src printers */
1922 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1923 nt_status = NT_STATUS_UNSUCCESSFUL;
1924 goto done;
1927 if (num_printers == 0) {
1928 printf (_("no printers found on server.\n"));
1929 nt_status = NT_STATUS_OK;
1930 goto done;
1934 /* do something for all printers */
1935 for (p = 0; p < num_printers; p++) {
1937 /* do some initialization */
1938 printername = info_enum[p].info2.printername;
1939 sharename = info_enum[p].info2.sharename;
1941 if (!printername || !sharename) {
1942 nt_status = NT_STATUS_UNSUCCESSFUL;
1943 goto done;
1946 /* we can reset NT_STATUS here because we do not
1947 get any real NT_STATUS-codes anymore from now on */
1948 nt_status = NT_STATUS_UNSUCCESSFUL;
1950 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1951 printername, sharename);
1953 /* open dst printer handle */
1954 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1955 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1956 goto done;
1958 /* check for existing dst printer */
1959 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1960 goto done;
1963 /* open src printer handle */
1964 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1965 MAXIMUM_ALLOWED_ACCESS,
1966 pipe_hnd->auth->user_name,
1967 &hnd_src))
1968 goto done;
1970 /* in a first step call getdriver for each shared printer (per arch)
1971 to get a list of all files that have to be copied */
1973 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1975 /* getdriver src */
1976 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1977 level, archi_table[i].long_archi,
1978 archi_table[i].version, &drv_info_src))
1979 continue;
1981 drivername = drv_info_src.info3.driver_name;
1983 if (c->opt_verbose)
1984 display_print_driver3(&drv_info_src.info3);
1986 /* check arch dir */
1987 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1988 if (!NT_STATUS_IS_OK(nt_status))
1989 goto done;
1992 /* copy driver-files */
1993 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1994 archi_table[i].short_archi,
1995 &drv_info_src.info3);
1996 if (!NT_STATUS_IS_OK(nt_status))
1997 goto done;
2000 /* adddriver dst */
2001 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
2002 nt_status = NT_STATUS_UNSUCCESSFUL;
2003 goto done;
2006 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
2007 drivername, printername));
2011 if (!drivername || strlen(drivername) == 0) {
2012 DEBUGADD(1,("Did not get driver for printer %s\n",
2013 printername));
2014 goto done;
2017 /* setdriver dst */
2018 info_dst.info2.drivername = drivername;
2020 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
2021 nt_status = NT_STATUS_UNSUCCESSFUL;
2022 goto done;
2025 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
2026 drivername, printername));
2028 /* close dst */
2029 if (is_valid_policy_hnd(&hnd_dst)) {
2030 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
2033 /* close src */
2034 if (is_valid_policy_hnd(&hnd_src)) {
2035 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
2039 nt_status = NT_STATUS_OK;
2041 done:
2043 if (is_valid_policy_hnd(&hnd_dst)) {
2044 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
2047 /* close src */
2048 if (is_valid_policy_hnd(&hnd_src)) {
2049 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
2052 if (cli_dst) {
2053 cli_shutdown(cli_dst);
2056 if (got_src_driver_share)
2057 cli_shutdown(cli_share_src);
2059 if (got_dst_driver_share)
2060 cli_shutdown(cli_share_dst);
2062 return nt_status;
2067 * Migrate printer-queues from a src to the dst server
2068 * (requires a working "addprinter command" to be installed for the local smbd)
2070 * All parameters are provided by the run_rpc_command function, except for
2071 * argc, argv which are passed through.
2073 * @param c A net_context structure
2074 * @param domain_sid The domain sid aquired from the remote server
2075 * @param cli A cli_state connected to the server.
2076 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2077 * @param argc Standard main() style argc
2078 * @param argv Standard main() style argv. Initial components are already
2079 * stripped
2081 * @return Normal NTSTATUS return.
2084 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
2085 const struct dom_sid *domain_sid,
2086 const char *domain_name,
2087 struct cli_state *cli,
2088 struct rpc_pipe_client *pipe_hnd,
2089 TALLOC_CTX *mem_ctx,
2090 int argc,
2091 const char **argv)
2093 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
2094 WERROR result;
2095 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2096 uint32_t i = 0, num_printers;
2097 uint32_t level = 2;
2098 union spoolss_PrinterInfo info_dst, info_src;
2099 union spoolss_PrinterInfo *info_enum;
2100 struct cli_state *cli_dst = NULL;
2101 struct policy_handle hnd_dst, hnd_src;
2102 const char *printername, *sharename;
2103 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2104 struct dcerpc_binding_handle *b_dst = NULL;
2105 struct spoolss_SetPrinterInfoCtr info_ctr;
2107 DEBUG(3,("copying printers\n"));
2109 /* connect destination PI_SPOOLSS */
2110 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2111 &ndr_table_spoolss.syntax_id);
2112 if (!NT_STATUS_IS_OK(nt_status)) {
2113 return nt_status;
2115 b_dst = pipe_hnd_dst->binding_handle;
2117 /* enum printers */
2118 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2119 nt_status = NT_STATUS_UNSUCCESSFUL;
2120 goto done;
2123 if (!num_printers) {
2124 printf (_("no printers found on server.\n"));
2125 nt_status = NT_STATUS_OK;
2126 goto done;
2129 /* do something for all printers */
2130 for (i = 0; i < num_printers; i++) {
2132 struct spoolss_SetPrinterInfo2 info2;
2134 /* do some initialization */
2135 printername = info_enum[i].info2.printername;
2136 sharename = info_enum[i].info2.sharename;
2138 if (!printername || !sharename) {
2139 nt_status = NT_STATUS_UNSUCCESSFUL;
2140 goto done;
2142 /* we can reset NT_STATUS here because we do not
2143 get any real NT_STATUS-codes anymore from now on */
2144 nt_status = NT_STATUS_UNSUCCESSFUL;
2146 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2147 printername, sharename);
2149 /* open dst printer handle */
2150 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2151 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2153 DEBUG(1,("could not open printer: %s\n", sharename));
2156 /* check for existing dst printer */
2157 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2158 printf (_("could not get printer, creating printer.\n"));
2159 } else {
2160 DEBUG(1,("printer already exists: %s\n", sharename));
2161 /* close printer handle here - dst only, not got src yet. */
2162 if (is_valid_policy_hnd(&hnd_dst)) {
2163 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2165 continue;
2168 /* now get again src printer ctr via getprinter,
2169 we first need a handle for that */
2171 /* open src printer handle */
2172 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2173 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2174 goto done;
2176 /* getprinter on the src server */
2177 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2178 goto done;
2180 /* copy each src printer to a dst printer 1:1,
2181 maybe some values have to be changed though */
2182 d_printf(_("creating printer: %s\n"), printername);
2184 info_ctr.level = level;
2185 spoolss_printerinfo2_to_setprinterinfo2(&info_src.info2, &info2);
2186 info_ctr.info.info2 = &info2;
2188 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2189 mem_ctx,
2190 &info_ctr);
2192 if (W_ERROR_IS_OK(result))
2193 d_printf (_("printer [%s] successfully added.\n"),
2194 printername);
2195 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2196 d_fprintf (stderr, _("printer [%s] already exists.\n"),
2197 printername);
2198 else {
2199 d_fprintf (stderr, _("could not create printer [%s]\n"),
2200 printername);
2201 goto done;
2204 /* close printer handles here */
2205 if (is_valid_policy_hnd(&hnd_src)) {
2206 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2209 if (is_valid_policy_hnd(&hnd_dst)) {
2210 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2214 nt_status = NT_STATUS_OK;
2216 done:
2217 if (is_valid_policy_hnd(&hnd_src)) {
2218 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2221 if (is_valid_policy_hnd(&hnd_dst)) {
2222 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2225 if (cli_dst) {
2226 cli_shutdown(cli_dst);
2228 return nt_status;
2232 * Migrate Printer-Settings from a src server to the dst server
2233 * (for this to work, printers and drivers already have to be migrated earlier)
2235 * All parameters are provided by the run_rpc_command function, except for
2236 * argc, argv which are passed through.
2238 * @param c A net_context structure
2239 * @param domain_sid The domain sid aquired from the remote server
2240 * @param cli A cli_state connected to the server.
2241 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2242 * @param argc Standard main() style argc
2243 * @param argv Standard main() style argv. Initial components are already
2244 * stripped
2246 * @return Normal NTSTATUS return.
2249 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2250 const struct dom_sid *domain_sid,
2251 const char *domain_name,
2252 struct cli_state *cli,
2253 struct rpc_pipe_client *pipe_hnd,
2254 TALLOC_CTX *mem_ctx,
2255 int argc,
2256 const char **argv)
2258 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
2260 /* FIXME: Here the nightmare begins */
2262 WERROR result;
2263 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2264 uint32_t i = 0, j = 0;
2265 uint32_t num_printers;
2266 uint32_t level = 2;
2267 const char *printername, *sharename;
2268 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2269 struct dcerpc_binding_handle *b_dst = NULL;
2270 struct policy_handle hnd_src, hnd_dst;
2271 union spoolss_PrinterInfo *info_enum;
2272 union spoolss_PrinterInfo info_dst_publish;
2273 union spoolss_PrinterInfo info_dst;
2274 struct cli_state *cli_dst = NULL;
2275 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2276 const char *longname;
2277 const char **keylist = NULL;
2279 /* FIXME GD */
2280 ZERO_STRUCT(info_dst_publish);
2282 DEBUG(3,("copying printer settings\n"));
2284 /* connect destination PI_SPOOLSS */
2285 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2286 &ndr_table_spoolss.syntax_id);
2287 if (!NT_STATUS_IS_OK(nt_status)) {
2288 return nt_status;
2290 b_dst = pipe_hnd_dst->binding_handle;
2292 /* enum src printers */
2293 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2294 nt_status = NT_STATUS_UNSUCCESSFUL;
2295 goto done;
2298 if (!num_printers) {
2299 printf (_("no printers found on server.\n"));
2300 nt_status = NT_STATUS_OK;
2301 goto done;
2305 /* needed for dns-strings in regkeys */
2306 longname = get_mydnsfullname();
2307 if (!longname) {
2308 nt_status = NT_STATUS_UNSUCCESSFUL;
2309 goto done;
2312 /* do something for all printers */
2313 for (i = 0; i < num_printers; i++) {
2315 uint32_t value_needed;
2316 uint32_t data_needed;
2317 enum winreg_Type type;
2318 struct spoolss_EnumPrinterData r;
2320 /* do some initialization */
2321 printername = info_enum[i].info2.printername;
2322 sharename = info_enum[i].info2.sharename;
2324 if (!printername || !sharename) {
2325 nt_status = NT_STATUS_UNSUCCESSFUL;
2326 goto done;
2328 /* we can reset NT_STATUS here because we do not
2329 get any real NT_STATUS-codes anymore from now on */
2330 nt_status = NT_STATUS_UNSUCCESSFUL;
2332 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2333 printername, sharename);
2336 /* open src printer handle */
2337 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2338 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2339 goto done;
2341 /* open dst printer handle */
2342 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2343 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2344 goto done;
2346 /* check for existing dst printer */
2347 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2348 level, &info_dst))
2349 goto done;
2352 /* STEP 1: COPY DEVICE-MODE and other
2353 PRINTER_INFO_2-attributes
2356 info_dst.info2 = info_enum[i].info2;
2358 /* why is the port always disconnected when the printer
2359 is correctly installed (incl. driver ???) */
2360 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2362 /* check if printer is published */
2363 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2365 /* check for existing dst printer */
2366 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2367 goto done;
2369 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2371 /* ignore false from setprinter due to WERR_IO_PENDING */
2372 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2374 DEBUG(3,("republished printer\n"));
2377 if (info_enum[i].info2.devmode != NULL) {
2379 /* copy devmode (info level 2) */
2380 info_dst.info2.devmode = info_enum[i].info2.devmode;
2382 /* do not copy security descriptor (we have another
2383 * command for that) */
2384 info_dst.info2.secdesc = NULL;
2386 #if 0
2387 info_dst.info2.devmode.devicename =
2388 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2389 longname, printername);
2390 if (!info_dst.info2.devmode.devicename) {
2391 nt_status = NT_STATUS_NO_MEMORY;
2392 goto done;
2394 #endif
2395 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2396 level, &info_dst))
2397 goto done;
2399 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2402 /* STEP 2: COPY REGISTRY VALUES */
2404 /* please keep in mind that samba parse_spools gives horribly
2405 crippled results when used to rpccli_spoolss_enumprinterdataex
2406 a win2k3-server. (Bugzilla #1851)
2407 FIXME: IIRC I've seen it too on a win2k-server
2410 r.in.handle = &hnd_src;
2411 r.in.enum_index = 0;
2412 r.in.value_offered = 0;
2413 r.in.data_offered = 0;
2414 r.out.value_name = NULL;
2415 r.out.value_needed = &value_needed;
2416 r.out.type = &type;
2417 r.out.data = NULL;
2418 r.out.data_needed = &data_needed;
2420 /* enumerate data on src handle */
2421 nt_status = dcerpc_spoolss_EnumPrinterData_r(b_src, mem_ctx, &r);
2423 r.in.data_offered = *r.out.data_needed;
2424 r.in.value_offered = *r.out.value_needed;
2425 r.out.data = talloc_zero_array(mem_ctx, uint8_t, r.in.data_offered);
2426 r.out.value_name = talloc_zero_array(mem_ctx, char, r.in.value_offered);
2428 /* loop for all printerdata of "PrinterDriverData" */
2429 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(r.out.result)) {
2431 r.in.enum_index++;
2433 nt_status = dcerpc_spoolss_EnumPrinterData_r(b_src, mem_ctx, &r);
2435 /* loop for all reg_keys */
2436 if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(r.out.result)) {
2438 /* display_value */
2439 if (c->opt_verbose) {
2440 struct regval_blob *v;
2442 v = regval_compose(talloc_tos(),
2443 r.out.value_name,
2444 *r.out.type,
2445 r.out.data,
2446 r.in.data_offered);
2447 if (v == NULL) {
2448 nt_status = NT_STATUS_NO_MEMORY;
2449 goto done;
2452 display_reg_value(SPOOL_PRINTERDATA_KEY, v);
2453 talloc_free(v);
2456 /* set_value */
2457 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2458 &hnd_dst, r.out.value_name,
2459 *r.out.type, r.out.data, r.in.data_offered))
2460 goto done;
2462 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2463 r.out.value_name));
2467 /* STEP 3: COPY SUBKEY VALUES */
2469 /* here we need to enum all printer_keys and then work
2470 on the result with enum_printer_key_ex. nt4 does not
2471 respond to enumprinterkey, win2k does, so continue
2472 in case of an error */
2474 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2475 printf(_("got no key-data\n"));
2476 continue;
2480 /* work on a list of printer keys
2481 each key has to be enumerated to get all required
2482 information. information is then set via setprinterdataex-calls */
2484 if (keylist == NULL)
2485 continue;
2487 for (i=0; keylist && keylist[i] != NULL; i++) {
2489 const char *subkey = keylist[i];
2490 uint32_t count;
2491 struct spoolss_PrinterEnumValues *info;
2493 /* enumerate all src subkeys */
2494 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2495 &hnd_src, subkey,
2496 &count, &info)) {
2497 goto done;
2500 for (j=0; j < count; j++) {
2502 struct regval_blob *value;
2503 DATA_BLOB blob;
2505 ZERO_STRUCT(blob);
2507 /* although samba replies with sane data in most cases we
2508 should try to avoid writing wrong registry data */
2510 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME) ||
2511 strequal(info[j].value_name, SPOOL_REG_UNCNAME) ||
2512 strequal(info[j].value_name, SPOOL_REG_URL) ||
2513 strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME) ||
2514 strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2516 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
2518 /* although windows uses a multi-sz, we use a sz */
2519 push_reg_sz(mem_ctx, &blob, SAMBA_PRINTER_PORT_NAME);
2522 if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
2524 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2525 nt_status = NT_STATUS_NO_MEMORY;
2526 goto done;
2528 push_reg_sz(mem_ctx, &blob, unc_name);
2531 if (strequal(info[j].value_name, SPOOL_REG_URL)) {
2533 continue;
2535 #if 0
2536 /* FIXME: should we really do that ??? */
2537 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2538 nt_status = NT_STATUS_NO_MEMORY;
2539 goto done;
2541 push_reg_sz(mem_ctx, NULL, &blob, url);
2542 fstrcpy(value.valuename, SPOOL_REG_URL);
2543 #endif
2546 if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2548 push_reg_sz(mem_ctx, &blob, longname);
2551 if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
2553 push_reg_sz(mem_ctx, &blob, lp_netbios_name());
2556 value = regval_compose(talloc_tos(),
2557 info[j].value_name,
2558 REG_SZ,
2559 blob.length == 0 ? NULL : blob.data,
2560 blob.length);
2561 if (value == NULL) {
2562 nt_status = NT_STATUS_NO_MEMORY;
2563 goto done;
2566 if (c->opt_verbose)
2567 display_reg_value(subkey, value);
2569 /* here we have to set all subkeys on the dst server */
2570 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2571 subkey, value))
2573 talloc_free(value);
2574 goto done;
2577 talloc_free(value);
2578 } else {
2580 struct regval_blob *v;
2582 v = regval_compose(talloc_tos(),
2583 info[j].value_name,
2584 info[j].type,
2585 info[j].data->data,
2586 info[j].data->length);
2587 if (v == NULL) {
2588 nt_status = NT_STATUS_NO_MEMORY;
2589 goto done;
2592 if (c->opt_verbose) {
2593 display_reg_value(subkey, v);
2596 /* here we have to set all subkeys on the dst server */
2597 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2598 subkey, v)) {
2599 goto done;
2602 talloc_free(v);
2605 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2606 subkey, info[j].value_name));
2611 TALLOC_FREE(keylist);
2613 /* close printer handles here */
2614 if (is_valid_policy_hnd(&hnd_src)) {
2615 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2618 if (is_valid_policy_hnd(&hnd_dst)) {
2619 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2623 nt_status = NT_STATUS_OK;
2625 done:
2626 SAFE_FREE(devicename);
2627 SAFE_FREE(url);
2628 SAFE_FREE(unc_name);
2630 if (is_valid_policy_hnd(&hnd_src)) {
2631 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2634 if (is_valid_policy_hnd(&hnd_dst)) {
2635 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2638 if (cli_dst) {
2639 cli_shutdown(cli_dst);
2641 return nt_status;