s3/torture: use stack buffer for rbtree loop
[Samba.git] / source3 / utils / net_rpc_printer.c
blobaf035a9b52e9f5fff36e07bef1fa7408408f6523
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.h"
28 #include "../libcli/security/security.h"
29 #include "../libcli/registry/util_reg.h"
30 #include "libsmb/libsmb.h"
31 #include "libsmb/clirap.h"
32 #include "../libcli/smb/smbXcli_base.h"
33 #include "auth/gensec/gensec.h"
34 #include "auth/credentials/credentials.h"
36 /* support itanium as well */
37 static const struct print_architecture_table_node archi_table[]= {
39 {"Windows 4.0", "WIN40", 0 },
40 {"Windows NT x86", "W32X86", 2 },
41 {"Windows NT x86", "W32X86", 3 },
42 {"Windows NT R4000", "W32MIPS", 2 },
43 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
44 {"Windows NT PowerPC", "W32PPC", 2 },
45 {"Windows IA64", "IA64", 3 },
46 {"Windows x64", "x64", 3 },
47 {NULL, "", -1 }
51 /**
52 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
53 * It is here for debugging purpose and should be removed later on.
54 **/
56 /****************************************************************************
57 Printer info level 3 display function.
58 ****************************************************************************/
60 static void display_print_driver3(struct spoolss_DriverInfo3 *r)
62 int i;
64 if (!r) {
65 return;
68 printf(_("Printer Driver Info 3:\n"));
69 printf(_("\tVersion: [%x]\n"), r->version);
70 printf(_("\tDriver Name: [%s]\n"), r->driver_name);
71 printf(_("\tArchitecture: [%s]\n"), r->architecture);
72 printf(_("\tDriver Path: [%s]\n"), r->driver_path);
73 printf(_("\tDatafile: [%s]\n"), r->data_file);
74 printf(_("\tConfigfile: [%s]\n\n"), r->config_file);
75 printf(_("\tHelpfile: [%s]\n\n"), r->help_file);
77 for (i=0; r->dependent_files && r->dependent_files[i] != NULL; i++) {
78 printf(_("\tDependentfiles: [%s]\n"), r->dependent_files[i]);
81 printf("\n");
83 printf(_("\tMonitorname: [%s]\n"), r->monitor_name);
84 printf(_("\tDefaultdatatype: [%s]\n\n"), r->default_datatype);
87 static void display_reg_value(const char *subkey, const char *name, struct registry_value *value)
89 const char *text;
91 switch(value->type) {
92 case REG_DWORD:
93 if (value->data.length == sizeof(uint32_t)) {
94 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey,
95 name, IVAL(value->data.data,0));
96 } else {
97 d_printf(_("\t[%s:%s]: REG_DWORD: <invalid>\n"), subkey,
98 name);
100 break;
102 case REG_SZ:
103 pull_reg_sz(talloc_tos(), &value->data, &text);
104 if (!text) {
105 break;
107 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, name, text);
108 break;
110 case REG_BINARY:
111 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
112 "displayed\n"),
113 subkey, name);
114 break;
116 case REG_MULTI_SZ: {
117 uint32_t i;
118 const char **values;
120 if (!pull_reg_multi_sz(NULL, &value->data, &values)) {
121 d_printf("pull_reg_multi_sz failed\n");
122 break;
125 printf("%s: REG_MULTI_SZ: \n", name);
126 for (i=0; values[i] != NULL; i++) {
127 d_printf("%s\n", values[i]);
129 TALLOC_FREE(values);
130 break;
133 default:
134 d_printf(_("\t%s: unknown type %d\n"), name, value->type);
140 * Copies ACLs, DOS-attributes and timestamps from one
141 * file or directory from one connected share to another connected share
143 * @param c A net_context structure
144 * @param mem_ctx A talloc-context
145 * @param cli_share_src A connected cli_state
146 * @param cli_share_dst A connected cli_state
147 * @param src_file The source file-name
148 * @param dst_file The destination file-name
149 * @param copy_acls Whether to copy acls
150 * @param copy_attrs Whether to copy DOS attributes
151 * @param copy_timestamps Whether to preserve timestamps
152 * @param is_file Whether this file is a file or a dir
154 * @return Normal NTSTATUS return.
157 NTSTATUS net_copy_fileattr(struct net_context *c,
158 TALLOC_CTX *mem_ctx,
159 struct cli_state *cli_share_src,
160 struct cli_state *cli_share_dst,
161 const char *src_name, const char *dst_name,
162 bool copy_acls, bool copy_attrs,
163 bool copy_timestamps, bool is_file)
165 NTSTATUS nt_status;
166 uint16_t fnum_src = 0;
167 uint16_t fnum_dst = 0;
168 struct security_descriptor *sd = NULL;
169 uint32_t attr = (uint32_t)-1;
170 struct timespec f_create_time = { .tv_nsec = SAMBA_UTIME_OMIT };
171 struct timespec f_access_time = { .tv_nsec = SAMBA_UTIME_OMIT };
172 struct timespec f_write_time = { .tv_nsec = SAMBA_UTIME_OMIT };
173 struct timespec f_change_time = { .tv_nsec = SAMBA_UTIME_OMIT };
175 if (!copy_timestamps && !copy_acls && !copy_attrs)
176 return NT_STATUS_OK;
178 /* open file/dir on the originating server */
180 DEBUGADD(3,("opening %s %s on originating server\n",
181 is_file?"file":"dir", src_name));
183 nt_status = cli_ntcreate(cli_share_src, src_name, 0,
184 READ_CONTROL_ACCESS, 0,
185 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
186 0x0, 0x0, &fnum_src, NULL);
187 if (!NT_STATUS_IS_OK(nt_status)) {
188 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
189 is_file?"file":"dir", src_name, nt_errstr(nt_status)));
190 goto out;
193 if (copy_acls) {
194 /* get the security descriptor */
195 nt_status = cli_query_secdesc(cli_share_src, fnum_src,
196 mem_ctx, &sd);
197 if (!NT_STATUS_IS_OK(nt_status)) {
198 DEBUG(0,("failed to get security descriptor: %s\n",
199 nt_errstr(nt_status)));
200 goto out;
203 if (c->opt_verbose && DEBUGLEVEL >= 3)
204 display_sec_desc(sd);
207 if (copy_attrs || copy_timestamps) {
209 /* get file attributes */
210 nt_status = cli_qfileinfo_basic(
211 cli_share_src,
212 fnum_src,
213 copy_attrs ? &attr : NULL,
214 NULL, /* size */
215 copy_timestamps ? &f_create_time : NULL,
216 copy_timestamps ? &f_access_time : NULL,
217 copy_timestamps ? &f_write_time : NULL,
218 copy_timestamps ? &f_change_time : NULL,
219 NULL); /* ino */
220 if (!NT_STATUS_IS_OK(nt_status)) {
221 DEBUG(0,("failed to get file-attrs: %s\n",
222 nt_errstr(nt_status)));
223 goto out;
227 /* open the file/dir on the destination server */
228 nt_status = cli_ntcreate(cli_share_dst, dst_name, 0,
229 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
230 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
231 0x0, 0x0, &fnum_dst, NULL);
232 if (!NT_STATUS_IS_OK(nt_status)) {
233 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
234 is_file?"file":"dir", dst_name, nt_errstr(nt_status)));
235 goto out;
238 if (copy_acls) {
239 /* set acls */
240 nt_status = cli_set_secdesc(cli_share_dst, fnum_dst, sd);
241 if (!NT_STATUS_IS_OK(nt_status)) {
242 DEBUG(0, ("could not set secdesc on %s: %s\n",
243 dst_name, nt_errstr(nt_status)));
244 goto out;
248 if (copy_timestamps || copy_attrs) {
250 nt_status = cli_setfileinfo_ext(
251 cli_share_dst,
252 fnum_dst,
253 f_create_time,
254 f_access_time,
255 f_write_time,
256 f_change_time,
257 attr);
258 if (!NT_STATUS_IS_OK(nt_status)) {
259 DBG_ERR("failed to set file-attrs: %s\n",
260 nt_errstr(nt_status));
261 goto out;
266 /* closing files */
267 nt_status = cli_close(cli_share_src, fnum_src);
268 if (!NT_STATUS_IS_OK(nt_status)) {
269 d_fprintf(stderr,
270 _("could not close %s on originating server: %s\n"),
271 is_file?"file":"dir", nt_errstr(nt_status));
272 goto out;
275 nt_status = cli_close(cli_share_dst, fnum_dst);
276 if (!NT_STATUS_IS_OK(nt_status)) {
277 d_fprintf(stderr,
278 _("could not close %s on destination server: %s\n"),
279 is_file?"file":"dir", nt_errstr(nt_status));
280 goto out;
284 nt_status = NT_STATUS_OK;
286 out:
288 /* cleaning up */
289 if (fnum_src)
290 cli_close(cli_share_src, fnum_src);
292 if (fnum_dst)
293 cli_close(cli_share_dst, fnum_dst);
295 return nt_status;
299 * Copy a file or directory from a connected share to another connected share
301 * @param c A net_context structure
302 * @param mem_ctx A talloc-context
303 * @param cli_share_src A connected cli_state
304 * @param cli_share_dst A connected cli_state
305 * @param src_file The source file-name
306 * @param dst_file The destination file-name
307 * @param copy_acls Whether to copy acls
308 * @param copy_attrs Whether to copy DOS attributes
309 * @param copy_timestamps Whether to preserve timestamps
310 * @param is_file Whether this file is a file or a dir
312 * @return Normal NTSTATUS return.
315 NTSTATUS net_copy_file(struct net_context *c,
316 TALLOC_CTX *mem_ctx,
317 struct cli_state *cli_share_src,
318 struct cli_state *cli_share_dst,
319 const char *src_name, const char *dst_name,
320 bool copy_acls, bool copy_attrs,
321 bool copy_timestamps, bool is_file)
323 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
324 uint16_t fnum_src = 0;
325 uint16_t fnum_dst = 0;
326 static int io_bufsize = 64512;
327 int read_size = io_bufsize;
328 char *data = NULL;
329 off_t nread = 0;
332 if (!src_name || !dst_name)
333 goto out;
335 if (cli_share_src == NULL || cli_share_dst == NULL)
336 goto out;
338 /* open on the originating server */
339 DEBUGADD(3,("opening %s %s on originating server\n",
340 is_file ? "file":"dir", src_name));
341 if (is_file)
342 nt_status = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE, &fnum_src);
343 else
344 nt_status = cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
345 FILE_SHARE_READ|FILE_SHARE_WRITE,
346 FILE_OPEN, 0x0, 0x0, &fnum_src, NULL);
348 if (!NT_STATUS_IS_OK(nt_status)) {
349 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
350 is_file ? "file":"dir",
351 src_name, nt_errstr(nt_status)));
352 goto out;
356 if (is_file) {
358 /* open file on the destination server */
359 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
360 nt_status = cli_open(cli_share_dst, dst_name,
361 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum_dst);
363 if (!NT_STATUS_IS_OK(nt_status)) {
364 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
365 dst_name, nt_errstr(nt_status)));
366 goto out;
369 /* allocate memory */
370 if (!(data = (char *)SMB_MALLOC(read_size))) {
371 d_fprintf(stderr, _("malloc fail for size %d\n"),
372 read_size);
373 nt_status = NT_STATUS_NO_MEMORY;
374 goto out;
380 if (c->opt_verbose) {
382 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
383 "%s ACLs and %s DOS Attributes %s\n"),
384 smbXcli_conn_remote_name(cli_share_src->conn),
385 cli_share_src->share, src_name,
386 smbXcli_conn_remote_name(cli_share_dst->conn),
387 cli_share_dst->share, dst_name,
388 copy_acls ? _("with") : _("without"),
389 copy_attrs ? _("with") : _("without"),
390 copy_timestamps ? _("(preserving timestamps)") : "" );
394 while (is_file) {
396 /* copying file */
397 size_t n;
399 nt_status = cli_read(cli_share_src, fnum_src, data, nread,
400 read_size, &n);
401 if (!NT_STATUS_IS_OK(nt_status)) {
402 d_fprintf(stderr,
403 _("Error reading file [\\\\%s\\%s%s]: %s\n"),
404 smbXcli_conn_remote_name(cli_share_src->conn),
405 cli_share_src->share,
406 src_name, nt_errstr(nt_status));
407 goto out;
410 if (n == 0)
411 break;
413 nt_status = cli_writeall(cli_share_dst, fnum_dst, 0,
414 (uint8_t *)data, nread, n, NULL);
416 if (!NT_STATUS_IS_OK(nt_status)) {
417 d_fprintf(stderr,
418 _("Error writing file: [\\\\%s\\%s%s]: %s\n"),
419 smbXcli_conn_remote_name(cli_share_dst->conn),
420 cli_share_dst->share,
421 dst_name, nt_errstr(nt_status));
422 goto out;
425 nread += n;
429 if (!is_file && !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
431 /* creating dir */
432 DEBUGADD(3,("creating dir %s on the destination server\n",
433 dst_name));
435 nt_status = cli_mkdir(cli_share_dst, dst_name);
436 if (!NT_STATUS_IS_OK(nt_status)) {
437 DEBUG(0,("cannot create directory %s: %s\n",
438 dst_name, nt_errstr(nt_status)));
439 nt_status = NT_STATUS_NO_SUCH_FILE;
443 nt_status = cli_chkpath(cli_share_dst, dst_name);
444 if (!NT_STATUS_IS_OK(nt_status)) {
445 d_fprintf(stderr,
446 _("cannot check for directory %s: %s\n"),
447 dst_name, nt_errstr(nt_status));
448 goto out;
453 /* closing files */
454 nt_status = cli_close(cli_share_src, fnum_src);
455 if (!NT_STATUS_IS_OK(nt_status)) {
456 d_fprintf(stderr,
457 _("could not close file on originating server: %s\n"),
458 nt_errstr(nt_status));
459 goto out;
462 if (is_file) {
463 nt_status = cli_close(cli_share_dst, fnum_dst);
464 if (!NT_STATUS_IS_OK(nt_status)) {
465 d_fprintf(stderr,
466 _("could not close file on destination server: %s\n"),
467 nt_errstr(nt_status));
468 goto out;
472 /* possibly we have to copy some file-attributes / acls / sd */
473 nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
474 src_name, dst_name, copy_acls,
475 copy_attrs, copy_timestamps, is_file);
476 if (!NT_STATUS_IS_OK(nt_status))
477 goto out;
480 nt_status = NT_STATUS_OK;
482 out:
484 /* cleaning up */
485 if (fnum_src)
486 cli_close(cli_share_src, fnum_src);
488 if (fnum_dst)
489 cli_close(cli_share_dst, fnum_dst);
491 SAFE_FREE(data);
493 return nt_status;
497 * Copy a driverfile from on connected share to another connected share
498 * This silently assumes that a driver-file is picked up from
500 * \\src_server\print$\{arch}\{version}\file
502 * and copied to
504 * \\dst_server\print$\{arch}\file
506 * to be added via setdriver-calls later.
507 * @param c A net_context structure
508 * @param mem_ctx A talloc-context
509 * @param cli_share_src A cli_state connected to source print$-share
510 * @param cli_share_dst A cli_state connected to destination print$-share
511 * @param file The file-name to be copied
512 * @param short_archi The name of the driver-architecture (short form)
514 * @return Normal NTSTATUS return.
517 static NTSTATUS net_copy_driverfile(struct net_context *c,
518 TALLOC_CTX *mem_ctx,
519 struct cli_state *cli_share_src,
520 struct cli_state *cli_share_dst,
521 const char *file, const char *short_archi) {
523 const char *p;
524 char *src_name;
525 char *dst_name;
526 char *version = NULL;
527 char *filename = NULL;
528 char *tok;
530 if (!file) {
531 return NT_STATUS_OK;
534 /* scroll through the file until we have the part
535 beyond archi_table.short_archi */
536 p = file;
537 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
538 if (strequal(tok, short_archi)) {
539 next_token_talloc(mem_ctx, &p, &version, "\\");
540 next_token_talloc(mem_ctx, &p, &filename, "\\");
544 if (version == NULL || filename == NULL) {
545 return NT_STATUS_UNSUCCESSFUL;
548 /* build source file name */
549 src_name = talloc_asprintf(mem_ctx, "\\%s\\%s\\%s",
550 short_archi, version, filename);
551 if (src_name == NULL) {
552 return NT_STATUS_NO_MEMORY;
555 /* create destination file name */
556 dst_name = talloc_asprintf(mem_ctx, "\\%s\\%s", short_archi, filename);
557 if (dst_name == NULL) {
558 return NT_STATUS_NO_MEMORY;
562 /* finally copy the file */
563 return net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
564 src_name, dst_name, false, false, false, true);
568 * Check for existing Architecture directory on a given server
570 * @param cli_share A cli_state connected to a print$-share
571 * @param short_archi The Architecture for the print-driver
573 * @return Normal NTSTATUS return.
576 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
579 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
580 char *dir;
582 if (asprintf(&dir, "\\%s", short_archi) < 0) {
583 return NT_STATUS_NO_MEMORY;
586 DEBUG(10,("creating print-driver dir for architecture: %s\n",
587 short_archi));
589 nt_status = cli_mkdir(cli_share, dir);
590 if (!NT_STATUS_IS_OK(nt_status)) {
591 DEBUG(1,("cannot create directory %s: %s\n",
592 dir, nt_errstr(nt_status)));
595 nt_status = cli_chkpath(cli_share, dir);
596 if (!NT_STATUS_IS_OK(nt_status)) {
597 d_fprintf(stderr, _("cannot check %s: %s\n"),
598 dir, nt_errstr(nt_status));
599 goto out;
602 nt_status = NT_STATUS_OK;
604 out:
605 SAFE_FREE(dir);
606 return nt_status;
610 * Copy a print-driver (level 3) from one connected print$-share to another
611 * connected print$-share
613 * @param c A net_context structure
614 * @param mem_ctx A talloc-context
615 * @param cli_share_src A cli_state connected to a print$-share
616 * @param cli_share_dst A cli_state connected to a print$-share
617 * @param short_archi The Architecture for the print-driver
618 * @param i1 The DRIVER_INFO_3-struct
620 * @return Normal NTSTATUS return.
623 static NTSTATUS copy_print_driver_3(struct net_context *c,
624 TALLOC_CTX *mem_ctx,
625 struct cli_state *cli_share_src,
626 struct cli_state *cli_share_dst,
627 const char *short_archi,
628 struct spoolss_DriverInfo3 *r)
630 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
631 int i;
633 if (r == NULL) {
634 return nt_status;
637 if (c->opt_verbose)
638 d_printf(_("copying driver: [%s], for architecture: [%s], "
639 "version: [%d]\n"),
640 r->driver_name, short_archi, r->version);
642 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
643 r->driver_path, short_archi);
644 if (!NT_STATUS_IS_OK(nt_status))
645 return nt_status;
647 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
648 r->data_file, short_archi);
649 if (!NT_STATUS_IS_OK(nt_status))
650 return nt_status;
652 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
653 r->config_file, short_archi);
654 if (!NT_STATUS_IS_OK(nt_status))
655 return nt_status;
657 nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
658 r->help_file, short_archi);
659 if (!NT_STATUS_IS_OK(nt_status))
660 return nt_status;
662 for (i=0; r->dependent_files[i] != NULL; i++) {
664 nt_status = net_copy_driverfile(c, mem_ctx,
665 cli_share_src, cli_share_dst,
666 r->dependent_files[i], short_archi);
667 if (!NT_STATUS_IS_OK(nt_status)) {
668 return nt_status;
672 return NT_STATUS_OK;
676 * net_spoolss-functions
677 * =====================
679 * the net_spoolss-functions aim to simplify spoolss-client-functions
680 * required during the migration-process wrt buffer-sizes, returned
681 * error-codes, etc.
683 * this greatly reduces the complexitiy of the migrate-functions.
687 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
688 TALLOC_CTX *mem_ctx,
689 char *name,
690 uint32_t flags,
691 uint32_t level,
692 uint32_t *num_printers,
693 union spoolss_PrinterInfo **info)
695 WERROR result;
697 /* enum printers */
699 result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
700 flags,
701 name,
702 level,
704 num_printers,
705 info);
706 if (!W_ERROR_IS_OK(result)) {
707 printf(_("cannot enum printers: %s\n"), win_errstr(result));
708 return false;
711 return true;
714 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
715 TALLOC_CTX *mem_ctx,
716 const char *printername,
717 uint32_t access_required,
718 struct policy_handle *hnd)
720 struct cli_credentials *creds = gensec_get_credentials(pipe_hnd->auth->auth_ctx);
721 const char *username = cli_credentials_get_username(creds);
722 WERROR result;
723 fstring printername2;
725 fstrcpy(printername2, pipe_hnd->srv_name_slash);
726 fstrcat(printername2, "\\");
727 fstrcat(printername2, printername);
729 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
730 pipe_hnd->srv_name_slash, username, printername2, access_required));
732 /* open printer */
733 result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
734 printername2,
735 access_required,
736 hnd);
738 /* be more verbose */
739 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
740 d_fprintf(stderr,
741 _("no access to printer [%s] on [%s] for user [%s] "
742 "granted\n"),
743 printername2, pipe_hnd->srv_name_slash, username);
744 return false;
747 if (!W_ERROR_IS_OK(result)) {
748 d_fprintf(stderr,_("cannot open printer %s on server %s: %s\n"),
749 printername2, pipe_hnd->srv_name_slash, win_errstr(result));
750 return false;
753 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
754 printername2, pipe_hnd->srv_name_slash));
756 return true;
759 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
760 TALLOC_CTX *mem_ctx,
761 struct policy_handle *hnd,
762 uint32_t level,
763 union spoolss_PrinterInfo *info)
765 WERROR result;
767 /* getprinter call */
768 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
769 hnd,
770 level,
771 0, /* offered */
772 info);
773 if (!W_ERROR_IS_OK(result)) {
774 printf(_("cannot get printer-info: %s\n"), win_errstr(result));
775 return false;
778 return true;
781 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
782 TALLOC_CTX *mem_ctx,
783 struct policy_handle *hnd,
784 uint32_t level,
785 union spoolss_PrinterInfo *info)
787 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
788 WERROR result;
789 NTSTATUS status;
790 struct spoolss_SetPrinterInfoCtr info_ctr;
791 struct spoolss_SetPrinterInfo2 info2;
792 struct spoolss_DevmodeContainer devmode_ctr;
793 struct sec_desc_buf secdesc_ctr;
795 ZERO_STRUCT(devmode_ctr);
796 ZERO_STRUCT(secdesc_ctr);
798 /* setprinter call */
800 info_ctr.level = level;
801 switch (level) {
802 case 0:
803 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)
804 (void *)&info->info0;
805 break;
806 case 1:
807 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)
808 (void *)&info->info1;
809 break;
810 case 2:
811 spoolss_printerinfo2_to_setprinterinfo2(&info->info2, &info2);
812 info_ctr.info.info2 = &info2;
813 break;
814 case 3:
815 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
816 (void *)&info->info3;
817 break;
818 case 4:
819 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)
820 (void *)&info->info4;
821 break;
822 case 5:
823 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)
824 (void *)&info->info5;
825 break;
826 case 6:
827 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)
828 (void *)&info->info6;
829 break;
830 case 7:
831 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
832 (void *)&info->info7;
833 break;
834 #if 0 /* FIXME GD */
835 case 8:
836 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)
837 (void *)&info->info8;
838 break;
839 case 9:
840 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)
841 (void *)&info->info9;
842 break;
843 #endif
844 default:
845 break; /* FIXME */
848 status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
849 hnd,
850 &info_ctr,
851 &devmode_ctr,
852 &secdesc_ctr,
853 0, /* command */
854 &result);
855 if (!NT_STATUS_IS_OK(status)) {
856 printf(_("cannot set printer-info: %s\n"), nt_errstr(status));
857 return false;
859 if (!W_ERROR_IS_OK(result)) {
860 printf(_("cannot set printer-info: %s\n"), win_errstr(result));
861 return false;
864 return true;
868 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
869 TALLOC_CTX *mem_ctx,
870 struct policy_handle *hnd,
871 const char *value_name,
872 enum winreg_Type type,
873 uint8_t *data,
874 uint32_t offered)
876 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
877 WERROR result;
878 NTSTATUS status;
880 /* setprinterdata call */
881 status = dcerpc_spoolss_SetPrinterData(b, mem_ctx,
882 hnd,
883 value_name,
884 type,
885 data,
886 offered,
887 &result);
888 if (!NT_STATUS_IS_OK(status)) {
889 printf (_("unable to set printerdata: %s\n"),
890 nt_errstr(status));
891 return false;
893 if (!W_ERROR_IS_OK(result)) {
894 printf (_("unable to set printerdata: %s\n"),
895 win_errstr(result));
896 return false;
899 return true;
903 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
904 TALLOC_CTX *mem_ctx,
905 struct policy_handle *hnd,
906 const char *keyname,
907 const char ***keylist)
909 WERROR result;
911 /* enumprinterkey call */
912 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
914 if (!W_ERROR_IS_OK(result)) {
915 printf(_("enumprinterkey failed: %s\n"), win_errstr(result));
916 return false;
919 return true;
922 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
923 TALLOC_CTX *mem_ctx,
924 uint32_t offered,
925 struct policy_handle *hnd,
926 const char *keyname,
927 uint32_t *count,
928 struct spoolss_PrinterEnumValues **info)
930 WERROR result;
932 /* enumprinterdataex call */
933 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
934 hnd,
935 keyname,
936 0, /* offered */
937 count,
938 info);
940 if (!W_ERROR_IS_OK(result)) {
941 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result));
942 return false;
945 return true;
949 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
950 TALLOC_CTX *mem_ctx,
951 struct policy_handle *hnd,
952 const char *keyname,
953 const char *name,
954 struct registry_value *value)
956 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
957 WERROR result;
958 NTSTATUS status;
960 /* setprinterdataex call */
961 status = dcerpc_spoolss_SetPrinterDataEx(b, mem_ctx,
962 hnd,
963 keyname,
964 name,
965 value->type,
966 value->data.data,
967 value->data.length,
968 &result);
969 if (!NT_STATUS_IS_OK(status)) {
970 printf(_("could not set printerdataex: %s\n"),
971 nt_errstr(status));
972 return false;
974 if (!W_ERROR_IS_OK(result)) {
975 printf(_("could not set printerdataex: %s\n"),
976 win_errstr(result));
977 return false;
980 return true;
983 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
984 TALLOC_CTX *mem_ctx,
985 struct policy_handle *hnd,
986 int level,
987 uint32_t *num_forms,
988 union spoolss_FormInfo **forms)
990 WERROR result;
992 /* enumforms call */
993 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
994 hnd,
995 level,
997 num_forms,
998 forms);
999 if (!W_ERROR_IS_OK(result)) {
1000 printf(_("could not enum forms: %s\n"), win_errstr(result));
1001 return false;
1004 return true;
1007 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
1008 TALLOC_CTX *mem_ctx,
1009 uint32_t level, const char *env,
1010 uint32_t *count,
1011 union spoolss_DriverInfo **info)
1013 WERROR result;
1015 /* enumprinterdrivers call */
1016 result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
1017 pipe_hnd->srv_name_slash,
1018 env,
1019 level,
1021 count,
1022 info);
1023 if (!W_ERROR_IS_OK(result)) {
1024 if (W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1025 printf(_("cannot enum drivers for environment %s: %s\n"), env,
1026 win_errstr(result));
1027 return false;
1028 } else {
1029 printf(_("Server does not support environment [%s]\n"),
1030 env);
1034 return true;
1037 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
1038 TALLOC_CTX *mem_ctx,
1039 struct policy_handle *hnd, uint32_t level,
1040 const char *env, int version,
1041 union spoolss_DriverInfo *info)
1043 WERROR result;
1044 uint32_t server_major_version;
1045 uint32_t server_minor_version;
1047 /* getprinterdriver call */
1048 result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
1049 hnd,
1050 env,
1051 level,
1053 version,
1055 info,
1056 &server_major_version,
1057 &server_minor_version);
1058 if (!W_ERROR_IS_OK(result)) {
1059 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1060 env, win_errstr(result)));
1061 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1062 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1063 printf(_("cannot get driver: %s\n"),
1064 win_errstr(result));
1066 return false;
1069 return true;
1073 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1074 TALLOC_CTX *mem_ctx, uint32_t level,
1075 union spoolss_DriverInfo *info)
1077 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1078 WERROR result;
1079 NTSTATUS status;
1080 struct spoolss_AddDriverInfoCtr info_ctr;
1082 info_ctr.level = level;
1084 switch (level) {
1085 case 2:
1086 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)
1087 (void *)&info->info2;
1088 break;
1089 case 3:
1090 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)
1091 (void *)&info->info3;
1092 break;
1093 default:
1094 printf(_("unsupported info level: %d\n"), level);
1095 return false;
1098 /* addprinterdriver call */
1099 status = dcerpc_spoolss_AddPrinterDriver(b, mem_ctx,
1100 pipe_hnd->srv_name_slash,
1101 &info_ctr,
1102 &result);
1103 if (!NT_STATUS_IS_OK(status)) {
1104 printf(_("cannot add driver: %s\n"), nt_errstr(status));
1105 return false;
1107 /* be more verbose */
1108 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1109 printf(_("You are not allowed to add drivers\n"));
1110 return false;
1112 if (!W_ERROR_IS_OK(result)) {
1113 printf(_("cannot add driver: %s\n"), win_errstr(result));
1114 return false;
1117 return true;
1121 * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1122 * for a single printer or for all printers depending on argc/argv
1125 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1126 TALLOC_CTX *mem_ctx,
1127 int level,
1128 int argc,
1129 const char **argv,
1130 uint32_t *num_printers,
1131 union spoolss_PrinterInfo **info_p)
1133 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1134 struct policy_handle hnd;
1135 WERROR werr;
1137 /* no arguments given, enumerate all printers */
1138 if (argc == 0) {
1140 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1141 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1142 level, num_printers, info_p))
1143 return false;
1145 goto out;
1148 /* argument given, get a single printer by name */
1149 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1150 MAXIMUM_ALLOWED_ACCESS,
1151 &hnd))
1152 return false;
1154 *info_p = talloc_zero(mem_ctx, union spoolss_PrinterInfo);
1155 if (*info_p == NULL) {
1156 return false;
1159 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1160 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1161 return false;
1164 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1166 *num_printers = 1;
1168 out:
1169 DEBUG(3,("got %d printers\n", *num_printers));
1171 return true;
1176 * List print-queues (including local printers that are not shared)
1178 * All parameters are provided by the run_rpc_command function, except for
1179 * argc, argv which are passed through.
1181 * @param c A net_context structure
1182 * @param domain_sid The domain sid aquired from the remote server
1183 * @param cli A cli_state connected to the server.
1184 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1185 * @param argc Standard main() style argc
1186 * @param argv Standard main() style argv. Initial components are already
1187 * stripped
1189 * @return Normal NTSTATUS return.
1192 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1193 const struct dom_sid *domain_sid,
1194 const char *domain_name,
1195 struct cli_state *cli,
1196 struct rpc_pipe_client *pipe_hnd,
1197 TALLOC_CTX *mem_ctx,
1198 int argc,
1199 const char **argv)
1201 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1202 uint32_t i, num_printers;
1203 uint32_t level = 2;
1204 const char *printername, *sharename;
1205 union spoolss_PrinterInfo *info;
1207 printf("listing printers\n");
1209 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1210 return nt_status;
1212 for (i = 0; i < num_printers; i++) {
1214 /* do some initialization */
1215 printername = info[i].info2.printername;
1216 sharename = info[i].info2.sharename;
1218 if (printername && sharename) {
1219 d_printf(_("printer %d: %s, shared as: %s\n"),
1220 i+1, printername, sharename);
1224 return NT_STATUS_OK;
1228 * List printer-drivers from a server
1230 * All parameters are provided by the run_rpc_command function, except for
1231 * argc, argv which are passed through.
1233 * @param c A net_context structure
1234 * @param domain_sid The domain sid aquired from the remote server
1235 * @param cli A cli_state connected to the server.
1236 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1237 * @param argc Standard main() style argc
1238 * @param argv Standard main() style argv. Initial components are already
1239 * stripped
1241 * @return Normal NTSTATUS return.
1244 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1245 const struct dom_sid *domain_sid,
1246 const char *domain_name,
1247 struct cli_state *cli,
1248 struct rpc_pipe_client *pipe_hnd,
1249 TALLOC_CTX *mem_ctx,
1250 int argc,
1251 const char **argv)
1253 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1254 uint32_t i;
1255 uint32_t level = 3;
1256 union spoolss_DriverInfo *info;
1258 printf(_("listing printer-drivers\n"));
1260 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1262 uint32_t d, num_drivers;
1264 /* enum remote drivers */
1265 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1266 archi_table[i].long_archi,
1267 &num_drivers, &info)) {
1268 nt_status = NT_STATUS_UNSUCCESSFUL;
1269 goto done;
1272 if (num_drivers == 0) {
1273 d_printf(_("no drivers found on server for "
1274 "architecture: [%s].\n"),
1275 archi_table[i].long_archi);
1276 continue;
1279 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1280 num_drivers, archi_table[i].long_archi);
1283 /* do something for all drivers for architecture */
1284 for (d = 0; d < num_drivers; d++) {
1285 display_print_driver3(&info[d].info3);
1289 nt_status = NT_STATUS_OK;
1291 done:
1292 return nt_status;
1297 * Publish print-queues with args-wrapper
1299 * @param cli A cli_state connected to the server.
1300 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1301 * @param argc Standard main() style argc
1302 * @param argv Standard main() style argv. Initial components are already
1303 * stripped
1304 * @param action
1306 * @return Normal NTSTATUS return.
1309 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1310 TALLOC_CTX *mem_ctx,
1311 int argc,
1312 const char **argv,
1313 uint32_t action)
1315 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1316 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1317 uint32_t i, num_printers;
1318 uint32_t level = 7;
1319 const char *printername, *sharename;
1320 union spoolss_PrinterInfo *info_enum;
1321 union spoolss_PrinterInfo info;
1322 struct spoolss_SetPrinterInfoCtr info_ctr;
1323 struct spoolss_DevmodeContainer devmode_ctr;
1324 struct sec_desc_buf secdesc_ctr;
1325 struct policy_handle hnd = { 0, };
1326 WERROR result;
1327 const char *action_str;
1329 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1330 return nt_status;
1332 for (i = 0; i < num_printers; i++) {
1334 /* do some initialization */
1335 printername = info_enum[i].info2.printername;
1336 sharename = info_enum[i].info2.sharename;
1337 if (!printername || !sharename) {
1338 goto done;
1341 /* open printer handle */
1342 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1343 PRINTER_ALL_ACCESS, &hnd))
1344 goto done;
1346 /* check for existing dst printer */
1347 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1348 goto done;
1350 /* check action and set string */
1351 switch (action) {
1352 case DSPRINT_PUBLISH:
1353 action_str = N_("published");
1354 break;
1355 case DSPRINT_UPDATE:
1356 action_str = N_("updated");
1357 break;
1358 case DSPRINT_UNPUBLISH:
1359 action_str = N_("unpublished");
1360 break;
1361 default:
1362 action_str = N_("unknown action");
1363 printf(_("unknown action: %d\n"), action);
1364 break;
1367 info.info7.action = action;
1368 info_ctr.level = 7;
1369 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
1370 (void *)&info.info7;
1372 ZERO_STRUCT(devmode_ctr);
1373 ZERO_STRUCT(secdesc_ctr);
1375 nt_status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
1376 &hnd,
1377 &info_ctr,
1378 &devmode_ctr,
1379 &secdesc_ctr,
1380 0, /* command */
1381 &result);
1382 if (!NT_STATUS_IS_OK(nt_status)) {
1383 printf(_("cannot set printer-info: %s\n"),
1384 nt_errstr(nt_status));
1385 goto done;
1387 if (!W_ERROR_IS_OK(result) && !W_ERROR_EQUAL(result, WERR_IO_PENDING)) {
1388 if ((action == DSPRINT_UPDATE) && W_ERROR_EQUAL(result, W_ERROR(0x80070002))) {
1389 printf(_("printer not published yet\n"));
1390 } else {
1391 printf(_("cannot set printer-info: %s\n"),
1392 win_errstr(result));
1394 nt_status = werror_to_ntstatus(result);
1395 goto done;
1398 printf(_("successfully %s printer %s in Active Directory\n"),
1399 action_str, sharename);
1402 nt_status = NT_STATUS_OK;
1404 done:
1405 if (is_valid_policy_hnd(&hnd)) {
1406 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &result);
1409 return nt_status;
1412 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1413 const struct dom_sid *domain_sid,
1414 const char *domain_name,
1415 struct cli_state *cli,
1416 struct rpc_pipe_client *pipe_hnd,
1417 TALLOC_CTX *mem_ctx,
1418 int argc,
1419 const char **argv)
1421 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1424 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1425 const struct dom_sid *domain_sid,
1426 const char *domain_name,
1427 struct cli_state *cli,
1428 struct rpc_pipe_client *pipe_hnd,
1429 TALLOC_CTX *mem_ctx,
1430 int argc,
1431 const char **argv)
1433 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1436 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1437 const struct dom_sid *domain_sid,
1438 const char *domain_name,
1439 struct cli_state *cli,
1440 struct rpc_pipe_client *pipe_hnd,
1441 TALLOC_CTX *mem_ctx,
1442 int argc,
1443 const char **argv)
1445 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1449 * List print-queues w.r.t. their publishing state
1451 * All parameters are provided by the run_rpc_command function, except for
1452 * argc, argv which are passed through.
1454 * @param c A net_context structure
1455 * @param domain_sid The domain sid aquired from the remote server
1456 * @param cli A cli_state connected to the server.
1457 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1458 * @param argc Standard main() style argc
1459 * @param argv Standard main() style argv. Initial components are already
1460 * stripped
1462 * @return Normal NTSTATUS return.
1465 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1466 const struct dom_sid *domain_sid,
1467 const char *domain_name,
1468 struct cli_state *cli,
1469 struct rpc_pipe_client *pipe_hnd,
1470 TALLOC_CTX *mem_ctx,
1471 int argc,
1472 const char **argv)
1474 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1475 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1476 uint32_t i, num_printers;
1477 uint32_t level = 7;
1478 const char *printername, *sharename;
1479 union spoolss_PrinterInfo *info_enum;
1480 union spoolss_PrinterInfo info;
1481 struct policy_handle hnd = { 0, };
1482 int state;
1483 WERROR werr;
1485 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1486 return nt_status;
1488 for (i = 0; i < num_printers; i++) {
1490 /* do some initialization */
1491 printername = info_enum[i].info2.printername;
1492 sharename = info_enum[i].info2.sharename;
1494 if (!printername || !sharename) {
1495 goto done;
1498 /* open printer handle */
1499 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1500 PRINTER_ALL_ACCESS, &hnd))
1501 goto done;
1503 /* check for existing dst printer */
1504 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1505 goto done;
1507 if (!info.info7.guid) {
1508 goto done;
1510 state = info.info7.action;
1511 switch (state) {
1512 case DSPRINT_PUBLISH:
1513 printf(_("printer [%s] is published"),
1514 sharename);
1515 if (c->opt_verbose)
1516 printf(_(", guid: %s"),info.info7.guid);
1517 printf("\n");
1518 break;
1519 case DSPRINT_UNPUBLISH:
1520 printf(_("printer [%s] is unpublished\n"),
1521 sharename);
1522 break;
1523 case DSPRINT_UPDATE:
1524 printf(_("printer [%s] is currently updating\n"),
1525 sharename);
1526 break;
1527 default:
1528 printf(_("unknown state: %d\n"), state);
1529 break;
1533 nt_status = NT_STATUS_OK;
1535 done:
1536 if (is_valid_policy_hnd(&hnd)) {
1537 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1540 return nt_status;
1544 * Migrate Printer-ACLs from a source server to the destination server
1546 * All parameters are provided by the run_rpc_command function, except for
1547 * argc, argv which are passed through.
1549 * @param c A net_context structure
1550 * @param domain_sid The domain sid aquired from the remote server
1551 * @param cli A cli_state connected to the server.
1552 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1553 * @param argc Standard main() style argc
1554 * @param argv Standard main() style argv. Initial components are already
1555 * stripped
1557 * @return Normal NTSTATUS return.
1560 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1561 const struct dom_sid *domain_sid,
1562 const char *domain_name,
1563 struct cli_state *cli,
1564 struct rpc_pipe_client *pipe_hnd,
1565 TALLOC_CTX *mem_ctx,
1566 int argc,
1567 const char **argv)
1569 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1570 /* TODO: what now, info2 or info3 ?
1571 convince jerry that we should add clientside setacls level 3 at least
1573 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1574 uint32_t i = 0;
1575 uint32_t num_printers;
1576 uint32_t level = 2;
1577 const char *printername, *sharename;
1578 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1579 struct dcerpc_binding_handle *b_dst = NULL;
1580 struct policy_handle hnd_src = { 0, };
1581 struct policy_handle hnd_dst = { 0, };
1582 union spoolss_PrinterInfo *info_enum;
1583 struct cli_state *cli_dst = NULL;
1584 union spoolss_PrinterInfo info_src, info_dst;
1585 WERROR werr;
1587 DEBUG(3,("copying printer ACLs\n"));
1589 /* connect destination PI_SPOOLSS */
1590 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1591 &ndr_table_spoolss);
1592 if (!NT_STATUS_IS_OK(nt_status)) {
1593 return nt_status;
1595 b_dst = pipe_hnd_dst->binding_handle;
1597 /* enum source printers */
1598 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1599 nt_status = NT_STATUS_UNSUCCESSFUL;
1600 goto done;
1603 if (!num_printers) {
1604 printf (_("no printers found on server.\n"));
1605 nt_status = NT_STATUS_OK;
1606 goto done;
1609 /* do something for all printers */
1610 for (i = 0; i < num_printers; i++) {
1612 /* do some initialization */
1613 printername = info_enum[i].info2.printername;
1614 sharename = info_enum[i].info2.sharename;
1616 if (!printername || !sharename) {
1617 nt_status = NT_STATUS_UNSUCCESSFUL;
1618 goto done;
1621 /* we can reset NT_STATUS here because we do not
1622 get any real NT_STATUS-codes anymore from now on */
1623 nt_status = NT_STATUS_UNSUCCESSFUL;
1625 d_printf(_("migrating printer ACLs for: [%s] / [%s]\n"),
1626 printername, sharename);
1628 /* according to msdn you have specify these access-rights
1629 to see the security descriptor
1630 - READ_CONTROL (DACL)
1631 - ACCESS_SYSTEM_SECURITY (SACL)
1634 /* open src printer handle */
1635 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1636 MAXIMUM_ALLOWED_ACCESS, &hnd_src))
1637 goto done;
1639 /* open dst printer handle */
1640 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1641 PRINTER_ALL_ACCESS, &hnd_dst))
1642 goto done;
1644 /* check for existing dst printer */
1645 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1646 goto done;
1648 /* check for existing src printer */
1649 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1650 goto done;
1652 /* Copy Security Descriptor */
1654 /* copy secdesc (info level 2) */
1655 info_dst.info2.devmode = NULL;
1656 if (info_src.info3.secdesc == NULL) {
1657 info_dst.info2.secdesc = NULL;
1658 } else {
1659 info_dst.info2.secdesc
1660 = security_descriptor_copy(mem_ctx,
1661 info_src.info3.secdesc);
1662 if (info_dst.info2.secdesc == NULL) {
1663 nt_status = NT_STATUS_NO_MEMORY;
1664 goto done;
1668 if (c->opt_verbose)
1669 display_sec_desc(info_dst.info2.secdesc);
1671 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1672 goto done;
1674 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1677 /* close printer handles here */
1678 if (is_valid_policy_hnd(&hnd_src)) {
1679 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
1682 if (is_valid_policy_hnd(&hnd_dst)) {
1683 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
1688 nt_status = NT_STATUS_OK;
1690 done:
1692 if (is_valid_policy_hnd(&hnd_src)) {
1693 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
1696 if (is_valid_policy_hnd(&hnd_dst)) {
1697 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
1700 if (cli_dst) {
1701 cli_shutdown(cli_dst);
1703 return nt_status;
1707 * Migrate printer-forms from a src server to the dst server
1709 * All parameters are provided by the run_rpc_command function, except for
1710 * argc, argv which are passed through.
1712 * @param c A net_context structure
1713 * @param domain_sid The domain sid aquired from the remote server
1714 * @param cli A cli_state connected to the server.
1715 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1716 * @param argc Standard main() style argc
1717 * @param argv Standard main() style argv. Initial components are already
1718 * stripped
1720 * @return Normal NTSTATUS return.
1723 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1724 const struct dom_sid *domain_sid,
1725 const char *domain_name,
1726 struct cli_state *cli,
1727 struct rpc_pipe_client *pipe_hnd,
1728 TALLOC_CTX *mem_ctx,
1729 int argc,
1730 const char **argv)
1732 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1733 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1734 WERROR result;
1735 uint32_t i, f;
1736 uint32_t num_printers;
1737 uint32_t level = 1;
1738 const char *printername, *sharename;
1739 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1740 struct dcerpc_binding_handle *b_dst = NULL;
1741 struct policy_handle hnd_src = { 0, };
1742 struct policy_handle hnd_dst = { 0, };
1743 union spoolss_PrinterInfo *info_enum;
1744 union spoolss_PrinterInfo info_dst;
1745 uint32_t num_forms;
1746 union spoolss_FormInfo *forms;
1747 struct cli_state *cli_dst = NULL;
1749 DEBUG(3,("copying forms\n"));
1751 /* connect destination PI_SPOOLSS */
1752 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1753 &ndr_table_spoolss);
1754 if (!NT_STATUS_IS_OK(nt_status)) {
1755 return nt_status;
1757 b_dst = pipe_hnd_dst->binding_handle;
1759 /* enum src printers */
1760 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1761 nt_status = NT_STATUS_UNSUCCESSFUL;
1762 goto done;
1765 if (!num_printers) {
1766 printf (_("no printers found on server.\n"));
1767 nt_status = NT_STATUS_OK;
1768 goto done;
1771 /* do something for all printers */
1772 for (i = 0; i < num_printers; i++) {
1774 /* do some initialization */
1775 printername = info_enum[i].info2.printername;
1776 sharename = info_enum[i].info2.sharename;
1778 if (!printername || !sharename) {
1779 nt_status = NT_STATUS_UNSUCCESSFUL;
1780 goto done;
1782 /* we can reset NT_STATUS here because we do not
1783 get any real NT_STATUS-codes anymore from now on */
1784 nt_status = NT_STATUS_UNSUCCESSFUL;
1786 d_printf(_("migrating printer forms for: [%s] / [%s]\n"),
1787 printername, sharename);
1790 /* open src printer handle */
1791 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1792 MAXIMUM_ALLOWED_ACCESS, &hnd_src))
1793 goto done;
1795 /* open dst printer handle */
1796 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1797 PRINTER_ALL_ACCESS, &hnd_dst))
1798 goto done;
1800 /* check for existing dst printer */
1801 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1802 goto done;
1804 /* finally migrate forms */
1805 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1806 goto done;
1808 DEBUG(1,("got %d forms for printer\n", num_forms));
1811 for (f = 0; f < num_forms; f++) {
1813 struct spoolss_AddFormInfoCtr info_ctr;
1814 NTSTATUS status;
1816 /* only migrate FORM_PRINTER types, according to jerry
1817 FORM_BUILTIN-types are hard-coded in samba */
1818 if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1819 continue;
1821 if (c->opt_verbose)
1822 d_printf(_("\tmigrating form # %d [%s] of type "
1823 "[%d]\n"),
1824 f, forms[f].info1.form_name,
1825 forms[f].info1.flags);
1826 info_ctr.level = 1;
1827 info_ctr.info.info1 = (struct spoolss_AddFormInfo1 *)
1828 (void *)&forms[f].info1;
1830 /* FIXME: there might be something wrong with samba's
1831 builtin-forms */
1832 status = dcerpc_spoolss_AddForm(b_dst, mem_ctx,
1833 &hnd_dst,
1834 &info_ctr,
1835 &result);
1836 if (!NT_STATUS_IS_OK(status)) {
1837 d_printf(_("\tdcerpc_spoolss_AddForm form %d: [%s] - %s\n"),
1838 f, forms[f].info1.form_name, nt_errstr(status));
1839 continue;
1841 if (!W_ERROR_IS_OK(result)) {
1842 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1843 f, forms[f].info1.form_name);
1844 continue;
1847 DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1848 forms[f].info1.form_name));
1852 /* close printer handles here */
1853 if (is_valid_policy_hnd(&hnd_src)) {
1854 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
1857 if (is_valid_policy_hnd(&hnd_dst)) {
1858 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
1862 nt_status = NT_STATUS_OK;
1864 done:
1866 if (is_valid_policy_hnd(&hnd_src)) {
1867 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
1870 if (is_valid_policy_hnd(&hnd_dst)) {
1871 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
1874 if (cli_dst) {
1875 cli_shutdown(cli_dst);
1877 return nt_status;
1881 * Migrate printer-drivers from a src server to the dst server
1883 * All parameters are provided by the run_rpc_command function, except for
1884 * argc, argv which are passed through.
1886 * @param c A net_context structure
1887 * @param domain_sid The domain sid aquired from the remote server
1888 * @param cli A cli_state connected to the server.
1889 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1890 * @param argc Standard main() style argc
1891 * @param argv Standard main() style argv. Initial components are already
1892 * stripped
1894 * @return Normal NTSTATUS return.
1897 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1898 const struct dom_sid *domain_sid,
1899 const char *domain_name,
1900 struct cli_state *cli,
1901 struct rpc_pipe_client *pipe_hnd,
1902 TALLOC_CTX *mem_ctx,
1903 int argc,
1904 const char **argv)
1906 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1907 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1908 uint32_t i, p;
1909 uint32_t num_printers;
1910 uint32_t level = 3;
1911 const char *printername, *sharename;
1912 bool got_src_driver_share = false;
1913 bool got_dst_driver_share = false;
1914 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1915 struct dcerpc_binding_handle *b_dst = NULL;
1916 struct policy_handle hnd_src = { 0, };
1917 struct policy_handle hnd_dst = { 0, };
1918 union spoolss_DriverInfo drv_info_src;
1919 union spoolss_PrinterInfo *info_enum;
1920 union spoolss_PrinterInfo info_dst;
1921 struct cli_state *cli_dst = NULL;
1922 struct cli_state *cli_share_src = NULL;
1923 struct cli_state *cli_share_dst = NULL;
1924 const char *drivername = NULL;
1925 WERROR werr;
1927 DEBUG(3,("copying printer-drivers\n"));
1929 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1930 &ndr_table_spoolss);
1931 if (!NT_STATUS_IS_OK(nt_status)) {
1932 return nt_status;
1934 b_dst = pipe_hnd_dst->binding_handle;
1936 /* open print$-share on the src server */
1937 nt_status = connect_to_service(c, &cli_share_src,
1938 smbXcli_conn_remote_sockaddr(cli->conn),
1939 smbXcli_conn_remote_name(cli->conn),
1940 "print$", "A:");
1941 if (!NT_STATUS_IS_OK(nt_status))
1942 goto done;
1944 got_src_driver_share = true;
1947 /* open print$-share on the dst server */
1948 nt_status = connect_to_service(c, &cli_share_dst,
1949 smbXcli_conn_remote_sockaddr(cli_dst->conn),
1950 smbXcli_conn_remote_name(cli_dst->conn),
1951 "print$", "A:");
1952 if (!NT_STATUS_IS_OK(nt_status))
1953 return nt_status;
1955 got_dst_driver_share = true;
1958 /* enum src printers */
1959 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1960 nt_status = NT_STATUS_UNSUCCESSFUL;
1961 goto done;
1964 if (num_printers == 0) {
1965 printf (_("no printers found on server.\n"));
1966 nt_status = NT_STATUS_OK;
1967 goto done;
1971 /* do something for all printers */
1972 for (p = 0; p < num_printers; p++) {
1974 /* do some initialization */
1975 printername = info_enum[p].info2.printername;
1976 sharename = info_enum[p].info2.sharename;
1978 if (!printername || !sharename) {
1979 nt_status = NT_STATUS_UNSUCCESSFUL;
1980 goto done;
1983 /* we can reset NT_STATUS here because we do not
1984 get any real NT_STATUS-codes anymore from now on */
1985 nt_status = NT_STATUS_UNSUCCESSFUL;
1987 d_printf(_("migrating printer driver for: [%s] / [%s]\n"),
1988 printername, sharename);
1990 /* open dst printer handle */
1991 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1992 PRINTER_ALL_ACCESS, &hnd_dst))
1993 goto done;
1995 /* check for existing dst printer */
1996 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1997 goto done;
2000 /* open src printer handle */
2001 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2002 MAXIMUM_ALLOWED_ACCESS,
2003 &hnd_src))
2004 goto done;
2006 /* in a first step call getdriver for each shared printer (per arch)
2007 to get a list of all files that have to be copied */
2009 for (i=0; archi_table[i].long_archi!=NULL; i++) {
2011 /* getdriver src */
2012 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
2013 level, archi_table[i].long_archi,
2014 archi_table[i].version, &drv_info_src))
2015 continue;
2017 drivername = drv_info_src.info3.driver_name;
2019 if (c->opt_verbose)
2020 display_print_driver3(&drv_info_src.info3);
2022 /* check arch dir */
2023 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
2024 if (!NT_STATUS_IS_OK(nt_status))
2025 goto done;
2028 /* copy driver-files */
2029 nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
2030 archi_table[i].short_archi,
2031 &drv_info_src.info3);
2032 if (!NT_STATUS_IS_OK(nt_status))
2033 goto done;
2036 /* adddriver dst */
2037 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
2038 nt_status = NT_STATUS_UNSUCCESSFUL;
2039 goto done;
2042 DEBUGADD(1,("Successfully added driver [%s] for printer [%s]\n",
2043 drivername, printername));
2047 if (!drivername || strlen(drivername) == 0) {
2048 DEBUGADD(1,("Did not get driver for printer %s\n",
2049 printername));
2050 goto done;
2053 /* setdriver dst */
2054 info_dst.info2.drivername = drivername;
2056 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
2057 nt_status = NT_STATUS_UNSUCCESSFUL;
2058 goto done;
2061 DEBUGADD(1,("Successfully set driver %s for printer %s\n",
2062 drivername, printername));
2064 /* close dst */
2065 if (is_valid_policy_hnd(&hnd_dst)) {
2066 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
2069 /* close src */
2070 if (is_valid_policy_hnd(&hnd_src)) {
2071 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
2075 nt_status = NT_STATUS_OK;
2077 done:
2079 if (is_valid_policy_hnd(&hnd_dst)) {
2080 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
2083 /* close src */
2084 if (is_valid_policy_hnd(&hnd_src)) {
2085 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
2088 if (cli_dst) {
2089 cli_shutdown(cli_dst);
2092 if (got_src_driver_share)
2093 cli_shutdown(cli_share_src);
2095 if (got_dst_driver_share)
2096 cli_shutdown(cli_share_dst);
2098 return nt_status;
2103 * Migrate printer-queues from a src to the dst server
2104 * (requires a working "addprinter command" to be installed for the local smbd)
2106 * All parameters are provided by the run_rpc_command function, except for
2107 * argc, argv which are passed through.
2109 * @param c A net_context structure
2110 * @param domain_sid The domain sid aquired from the remote server
2111 * @param cli A cli_state connected to the server.
2112 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2113 * @param argc Standard main() style argc
2114 * @param argv Standard main() style argv. Initial components are already
2115 * stripped
2117 * @return Normal NTSTATUS return.
2120 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
2121 const struct dom_sid *domain_sid,
2122 const char *domain_name,
2123 struct cli_state *cli,
2124 struct rpc_pipe_client *pipe_hnd,
2125 TALLOC_CTX *mem_ctx,
2126 int argc,
2127 const char **argv)
2129 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
2130 WERROR result;
2131 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2132 uint32_t i = 0, num_printers;
2133 uint32_t level = 2;
2134 union spoolss_PrinterInfo info_dst, info_src;
2135 union spoolss_PrinterInfo *info_enum;
2136 struct cli_state *cli_dst = NULL;
2137 struct policy_handle hnd_src = { 0, };
2138 struct policy_handle hnd_dst = { 0, };
2139 const char *printername, *sharename;
2140 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2141 struct dcerpc_binding_handle *b_dst = NULL;
2142 struct spoolss_SetPrinterInfoCtr info_ctr;
2144 DEBUG(3,("copying printers\n"));
2146 /* connect destination PI_SPOOLSS */
2147 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2148 &ndr_table_spoolss);
2149 if (!NT_STATUS_IS_OK(nt_status)) {
2150 return nt_status;
2152 b_dst = pipe_hnd_dst->binding_handle;
2154 /* enum printers */
2155 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2156 nt_status = NT_STATUS_UNSUCCESSFUL;
2157 goto done;
2160 if (!num_printers) {
2161 printf (_("no printers found on server.\n"));
2162 nt_status = NT_STATUS_OK;
2163 goto done;
2166 /* do something for all printers */
2167 for (i = 0; i < num_printers; i++) {
2169 struct spoolss_SetPrinterInfo2 info2;
2171 /* do some initialization */
2172 printername = info_enum[i].info2.printername;
2173 sharename = info_enum[i].info2.sharename;
2175 if (!printername || !sharename) {
2176 nt_status = NT_STATUS_UNSUCCESSFUL;
2177 goto done;
2179 /* we can reset NT_STATUS here because we do not
2180 get any real NT_STATUS-codes anymore from now on */
2181 nt_status = NT_STATUS_UNSUCCESSFUL;
2183 d_printf(_("migrating printer queue for: [%s] / [%s]\n"),
2184 printername, sharename);
2186 /* open dst printer handle */
2187 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2188 PRINTER_ALL_ACCESS, &hnd_dst)) {
2190 DEBUG(1,("could not open printer: %s\n", sharename));
2193 /* check for existing dst printer */
2194 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2195 printf (_("could not get printer, creating printer.\n"));
2196 } else {
2197 DEBUG(1,("printer already exists: %s\n", sharename));
2198 /* close printer handle here - dst only, not got src yet. */
2199 if (is_valid_policy_hnd(&hnd_dst)) {
2200 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2202 continue;
2205 /* now get again src printer ctr via getprinter,
2206 we first need a handle for that */
2208 /* open src printer handle */
2209 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2210 MAXIMUM_ALLOWED_ACCESS, &hnd_src))
2211 goto done;
2213 /* getprinter on the src server */
2214 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2215 goto done;
2217 /* copy each src printer to a dst printer 1:1,
2218 maybe some values have to be changed though */
2219 d_printf(_("creating printer: %s\n"), printername);
2221 info_ctr.level = level;
2222 spoolss_printerinfo2_to_setprinterinfo2(&info_src.info2, &info2);
2223 info_ctr.info.info2 = &info2;
2225 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2226 mem_ctx,
2227 &info_ctr);
2229 if (W_ERROR_IS_OK(result))
2230 d_printf (_("printer [%s] successfully added.\n"),
2231 printername);
2232 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2233 d_fprintf (stderr, _("printer [%s] already exists.\n"),
2234 printername);
2235 else {
2236 d_fprintf (stderr, _("could not create printer [%s]\n"),
2237 printername);
2238 goto done;
2241 /* close printer handles here */
2242 if (is_valid_policy_hnd(&hnd_src)) {
2243 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2246 if (is_valid_policy_hnd(&hnd_dst)) {
2247 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2251 nt_status = NT_STATUS_OK;
2253 done:
2254 if (is_valid_policy_hnd(&hnd_src)) {
2255 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2258 if (is_valid_policy_hnd(&hnd_dst)) {
2259 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2262 if (cli_dst) {
2263 cli_shutdown(cli_dst);
2265 return nt_status;
2269 * Migrate Printer-Settings from a src server to the dst server
2270 * (for this to work, printers and drivers already have to be migrated earlier)
2272 * All parameters are provided by the run_rpc_command function, except for
2273 * argc, argv which are passed through.
2275 * @param c A net_context structure
2276 * @param domain_sid The domain sid aquired from the remote server
2277 * @param cli A cli_state connected to the server.
2278 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2279 * @param argc Standard main() style argc
2280 * @param argv Standard main() style argv. Initial components are already
2281 * stripped
2283 * @return Normal NTSTATUS return.
2286 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2287 const struct dom_sid *domain_sid,
2288 const char *domain_name,
2289 struct cli_state *cli,
2290 struct rpc_pipe_client *pipe_hnd,
2291 TALLOC_CTX *mem_ctx,
2292 int argc,
2293 const char **argv)
2295 struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
2297 /* FIXME: Here the nightmare begins */
2299 WERROR result;
2300 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2301 uint32_t i = 0, j = 0;
2302 uint32_t num_printers;
2303 uint32_t level = 2;
2304 const char *printername, *sharename;
2305 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2306 struct dcerpc_binding_handle *b_dst = NULL;
2307 struct policy_handle hnd_src = { 0, };
2308 struct policy_handle hnd_dst = { 0, };
2309 union spoolss_PrinterInfo *info_enum;
2310 union spoolss_PrinterInfo info_dst_publish;
2311 union spoolss_PrinterInfo info_dst;
2312 struct cli_state *cli_dst = NULL;
2313 const char *longname;
2314 const char **keylist = NULL;
2316 /* FIXME GD */
2317 ZERO_STRUCT(info_dst_publish);
2319 DEBUG(3,("copying printer settings\n"));
2321 /* connect destination PI_SPOOLSS */
2322 nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2323 &ndr_table_spoolss);
2324 if (!NT_STATUS_IS_OK(nt_status)) {
2325 return nt_status;
2327 b_dst = pipe_hnd_dst->binding_handle;
2329 /* enum src printers */
2330 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2331 nt_status = NT_STATUS_UNSUCCESSFUL;
2332 goto done;
2335 if (!num_printers) {
2336 printf (_("no printers found on server.\n"));
2337 nt_status = NT_STATUS_OK;
2338 goto done;
2342 /* needed for dns-strings in regkeys */
2343 longname = get_mydnsfullname();
2344 if (!longname) {
2345 nt_status = NT_STATUS_UNSUCCESSFUL;
2346 goto done;
2349 /* do something for all printers */
2350 for (i = 0; i < num_printers; i++) {
2352 uint32_t value_needed;
2353 uint32_t data_needed;
2354 enum winreg_Type type;
2355 struct spoolss_EnumPrinterData r;
2357 /* do some initialization */
2358 printername = info_enum[i].info2.printername;
2359 sharename = info_enum[i].info2.sharename;
2361 if (!printername || !sharename) {
2362 nt_status = NT_STATUS_UNSUCCESSFUL;
2363 goto done;
2365 /* we can reset NT_STATUS here because we do not
2366 get any real NT_STATUS-codes anymore from now on */
2367 nt_status = NT_STATUS_UNSUCCESSFUL;
2369 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2370 printername, sharename);
2373 /* open src printer handle */
2374 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2375 MAXIMUM_ALLOWED_ACCESS, &hnd_src))
2376 goto done;
2378 /* open dst printer handle */
2379 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2380 PRINTER_ALL_ACCESS, &hnd_dst))
2381 goto done;
2383 /* check for existing dst printer */
2384 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2385 level, &info_dst))
2386 goto done;
2389 /* STEP 1: COPY DEVICE-MODE and other
2390 PRINTER_INFO_2-attributes
2393 info_dst.info2 = info_enum[i].info2;
2395 /* why is the port always disconnected when the printer
2396 is correctly installed (incl. driver ???) */
2397 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2399 /* check if printer is published */
2400 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2402 /* check for existing dst printer */
2403 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2404 goto done;
2406 info_dst_publish.info7.action = DSPRINT_PUBLISH;
2408 /* ignore false from setprinter due to WERR_IO_PENDING */
2409 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2411 DEBUG(3,("republished printer\n"));
2414 if (info_enum[i].info2.devmode != NULL) {
2416 /* copy devmode (info level 2) */
2417 info_dst.info2.devmode = info_enum[i].info2.devmode;
2419 /* do not copy security descriptor (we have another
2420 * command for that) */
2421 info_dst.info2.secdesc = NULL;
2423 #if 0
2424 info_dst.info2.devmode.devicename =
2425 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2426 longname, printername);
2427 if (!info_dst.info2.devmode.devicename) {
2428 nt_status = NT_STATUS_NO_MEMORY;
2429 goto done;
2431 #endif
2432 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2433 level, &info_dst))
2434 goto done;
2436 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2439 /* STEP 2: COPY REGISTRY VALUES */
2441 /* please keep in mind that samba parse_spools gives horribly
2442 crippled results when used to rpccli_spoolss_enumprinterdataex
2443 a win2k3-server. (Bugzilla #1851)
2444 FIXME: IIRC I've seen it too on a win2k-server
2447 r.in.handle = &hnd_src;
2448 r.in.enum_index = 0;
2449 r.in.value_offered = 0;
2450 r.in.data_offered = 0;
2451 r.out.value_name = NULL;
2452 r.out.value_needed = &value_needed;
2453 r.out.type = &type;
2454 r.out.data = NULL;
2455 r.out.data_needed = &data_needed;
2457 /* enumerate data on src handle */
2458 nt_status = dcerpc_spoolss_EnumPrinterData_r(b_src, mem_ctx, &r);
2460 r.in.data_offered = *r.out.data_needed;
2461 r.in.value_offered = *r.out.value_needed;
2462 r.out.data = talloc_zero_array(mem_ctx, uint8_t, r.in.data_offered);
2463 r.out.value_name = talloc_zero_array(mem_ctx, char, r.in.value_offered);
2465 /* loop for all printerdata of "PrinterDriverData" */
2466 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(r.out.result)) {
2468 r.in.enum_index++;
2470 nt_status = dcerpc_spoolss_EnumPrinterData_r(b_src, mem_ctx, &r);
2472 /* loop for all reg_keys */
2473 if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(r.out.result)) {
2475 /* display_value */
2476 if (c->opt_verbose) {
2477 struct registry_value v;
2478 v.type = *r.out.type;
2479 v.data = data_blob_const(
2480 r.out.data, r.in.data_offered);
2482 display_reg_value(SPOOL_PRINTERDATA_KEY,
2483 r.out.value_name, &v);
2486 /* set_value */
2487 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2488 &hnd_dst, r.out.value_name,
2489 *r.out.type, r.out.data, r.in.data_offered))
2490 goto done;
2492 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2493 r.out.value_name));
2497 /* STEP 3: COPY SUBKEY VALUES */
2499 /* here we need to enum all printer_keys and then work
2500 on the result with enum_printer_key_ex. nt4 does not
2501 respond to enumprinterkey, win2k does, so continue
2502 in case of an error */
2504 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2505 printf(_("got no key-data\n"));
2506 continue;
2510 /* work on a list of printer keys
2511 each key has to be enumerated to get all required
2512 information. information is then set via setprinterdataex-calls */
2514 if (keylist == NULL)
2515 continue;
2517 for (i=0; keylist && keylist[i] != NULL; i++) {
2519 const char *subkey = keylist[i];
2520 uint32_t count;
2521 struct spoolss_PrinterEnumValues *info;
2523 /* enumerate all src subkeys */
2524 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2525 &hnd_src, subkey,
2526 &count, &info)) {
2527 goto done;
2530 for (j=0; j < count; j++) {
2532 struct registry_value value;
2533 const char *value_name = info[j].value_name;
2534 bool ok;
2536 value.type = REG_SZ;
2538 /* although samba replies with sane data in most cases we
2539 should try to avoid writing wrong registry data */
2541 if (strequal(value_name, SPOOL_REG_PORTNAME)) {
2542 /* although windows uses a multi-sz, we use a sz */
2543 ok = push_reg_sz(mem_ctx, &value.data, SAMBA_PRINTER_PORT_NAME);
2544 if (!ok) {
2545 nt_status = NT_STATUS_NO_MEMORY;
2546 goto done;
2549 else if (strequal(value_name, SPOOL_REG_UNCNAME)) {
2550 char *unc_name;
2551 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2552 nt_status = NT_STATUS_NO_MEMORY;
2553 goto done;
2555 ok = push_reg_sz(mem_ctx, &value.data, unc_name);
2556 if (!ok) {
2557 nt_status = NT_STATUS_NO_MEMORY;
2558 goto done;
2560 free(unc_name);
2562 else if (strequal(value_name, SPOOL_REG_URL)) {
2563 continue;
2564 #if 0
2565 /* FIXME: should we really do that ??? */
2566 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2567 nt_status = NT_STATUS_NO_MEMORY;
2568 goto done;
2570 push_reg_sz(mem_ctx, NULL, &value.data, url);
2571 free(url);
2572 #endif
2574 else if (strequal(value_name, SPOOL_REG_SERVERNAME)) {
2575 ok = push_reg_sz(mem_ctx, &value.data, longname);
2576 if (!ok) {
2577 nt_status = NT_STATUS_NO_MEMORY;
2578 goto done;
2581 else if (strequal(value_name, SPOOL_REG_SHORTSERVERNAME)) {
2582 ok = push_reg_sz(mem_ctx, &value.data, lp_netbios_name());
2583 if (!ok) {
2584 nt_status = NT_STATUS_NO_MEMORY;
2585 goto done;
2588 else {
2589 value.type = info[j].type;
2590 value.data = *info[j].data;
2593 if (c->opt_verbose) {
2594 display_reg_value(subkey, value_name, &value);
2597 /* here we have to set all subkeys on the dst server */
2598 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2599 subkey, value_name, &value))
2601 goto done;
2604 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2605 subkey, info[j].value_name));
2610 TALLOC_FREE(keylist);
2612 /* close printer handles here */
2613 if (is_valid_policy_hnd(&hnd_src)) {
2614 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2617 if (is_valid_policy_hnd(&hnd_dst)) {
2618 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2622 nt_status = NT_STATUS_OK;
2624 done:
2625 if (is_valid_policy_hnd(&hnd_src)) {
2626 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2629 if (is_valid_policy_hnd(&hnd_dst)) {
2630 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2633 if (cli_dst) {
2634 cli_shutdown(cli_dst);
2636 return nt_status;