r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / utils / net_rpc_printer.c
blob1f0936cf967b408e008fcfb50c1b9a0e9f30973f
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2004 Guenther Deschner (gd@samba.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "includes.h"
20 #include "utils/net.h"
22 struct table_node {
23 const char *long_archi;
24 const char *short_archi;
25 int version;
29 /* support itanium as well */
30 static const struct table_node archi_table[]= {
32 {"Windows 4.0", "WIN40", 0 },
33 {"Windows NT x86", "W32X86", 2 },
34 {"Windows NT x86", "W32X86", 3 },
35 {"Windows NT R4000", "W32MIPS", 2 },
36 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
37 {"Windows NT PowerPC", "W32PPC", 2 },
38 {"Windows IA64", "IA64", 3 },
39 {"Windows x64", "x64", 3 },
40 {NULL, "", -1 }
44 /**
45 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
46 * It is here for debugging purpose and should be removed later on.
47 **/
49 /****************************************************************************
50 Printer info level 3 display function.
51 ****************************************************************************/
53 static void display_print_driver_3(DRIVER_INFO_3 *i1)
55 fstring name = "";
56 fstring architecture = "";
57 fstring driverpath = "";
58 fstring datafile = "";
59 fstring configfile = "";
60 fstring helpfile = "";
61 fstring dependentfiles = "";
62 fstring monitorname = "";
63 fstring defaultdatatype = "";
65 int length=0;
66 BOOL valid = True;
68 if (i1 == NULL)
69 return;
71 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
72 rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), -1, STR_TERMINATE);
73 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
74 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
75 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
76 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
77 rpcstr_pull(monitorname, i1->monitorname.buffer, sizeof(monitorname), -1, STR_TERMINATE);
78 rpcstr_pull(defaultdatatype, i1->defaultdatatype.buffer, sizeof(defaultdatatype), -1, STR_TERMINATE);
80 d_printf ("Printer Driver Info 3:\n");
81 d_printf ("\tVersion: [%x]\n", i1->version);
82 d_printf ("\tDriver Name: [%s]\n",name);
83 d_printf ("\tArchitecture: [%s]\n", architecture);
84 d_printf ("\tDriver Path: [%s]\n", driverpath);
85 d_printf ("\tDatafile: [%s]\n", datafile);
86 d_printf ("\tConfigfile: [%s]\n", configfile);
87 d_printf ("\tHelpfile: [%s]\n\n", helpfile);
89 while (valid) {
90 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
92 length+=strlen(dependentfiles)+1;
94 if (strlen(dependentfiles) > 0) {
95 d_printf ("\tDependentfiles: [%s]\n", dependentfiles);
96 } else {
97 valid = False;
101 printf ("\n");
103 d_printf ("\tMonitorname: [%s]\n", monitorname);
104 d_printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype);
106 return;
109 static void display_reg_value(const char *subkey, REGISTRY_VALUE value)
111 pstring text;
113 switch(value.type) {
114 case REG_DWORD:
115 d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename,
116 *((uint32 *) value.data_p));
117 break;
119 case REG_SZ:
120 rpcstr_pull(text, value.data_p, sizeof(text), value.size,
121 STR_TERMINATE);
122 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
123 break;
125 case REG_BINARY:
126 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
127 subkey, value.valuename);
128 break;
130 case REG_MULTI_SZ: {
131 uint32 i, num_values;
132 char **values;
134 if (!W_ERROR_IS_OK(reg_pull_multi_sz(NULL, value.data_p,
135 value.size, &num_values,
136 &values))) {
137 d_printf("reg_pull_multi_sz failed\n");
138 break;
141 for (i=0; i<num_values; i++) {
142 d_printf("%s\n", values[i]);
144 TALLOC_FREE(values);
145 break;
148 default:
149 d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
155 * Copies ACLs, DOS-attributes and timestamps from one
156 * file or directory from one connected share to another connected share
158 * @param mem_ctx A talloc-context
159 * @param cli_share_src A connected cli_state
160 * @param cli_share_dst A connected cli_state
161 * @param src_file The source file-name
162 * @param dst_file The destination file-name
163 * @param copy_acls Whether to copy acls
164 * @param copy_attrs Whether to copy DOS attributes
165 * @param copy_timestamps Whether to preserve timestamps
166 * @param is_file Whether this file is a file or a dir
168 * @return Normal NTSTATUS return.
169 **/
171 NTSTATUS net_copy_fileattr(TALLOC_CTX *mem_ctx,
172 struct cli_state *cli_share_src,
173 struct cli_state *cli_share_dst,
174 const char *src_name, const char *dst_name,
175 BOOL copy_acls, BOOL copy_attrs,
176 BOOL copy_timestamps, BOOL is_file)
178 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
179 int fnum_src = 0;
180 int fnum_dst = 0;
181 SEC_DESC *sd = NULL;
182 uint16 attr;
183 time_t f_atime, f_ctime, f_mtime;
186 if (!copy_timestamps && !copy_acls && !copy_attrs)
187 return NT_STATUS_OK;
189 /* open file/dir on the originating server */
191 DEBUGADD(3,("opening %s %s on originating server\n",
192 is_file?"file":"dir", src_name));
194 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
195 if (fnum_src == -1) {
196 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
197 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
198 nt_status = cli_nt_error(cli_share_src);
199 goto out;
203 if (copy_acls) {
205 /* get the security descriptor */
206 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
207 if (!sd) {
208 DEBUG(0,("failed to get security descriptor: %s\n",
209 cli_errstr(cli_share_src)));
210 nt_status = cli_nt_error(cli_share_src);
211 goto out;
214 if (opt_verbose && DEBUGLEVEL >= 3)
215 display_sec_desc(sd);
219 if (copy_attrs || copy_timestamps) {
221 /* get file attributes */
222 if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
223 &f_ctime, &f_atime, &f_mtime)) {
224 DEBUG(0,("failed to get file-attrs: %s\n",
225 cli_errstr(cli_share_src)));
226 nt_status = cli_nt_error(cli_share_src);
227 goto out;
232 /* open the file/dir on the destination server */
234 fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
235 if (fnum_dst == -1) {
236 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
237 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
238 nt_status = cli_nt_error(cli_share_dst);
239 goto out;
242 if (copy_timestamps) {
244 /* set timestamps */
245 if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
246 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
247 cli_errstr(cli_share_dst)));
248 nt_status = cli_nt_error(cli_share_dst);
249 goto out;
253 if (copy_acls) {
255 /* set acls */
256 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
257 DEBUG(0,("could not set secdesc on %s: %s\n",
258 dst_name, cli_errstr(cli_share_dst)));
259 nt_status = cli_nt_error(cli_share_dst);
260 goto out;
264 if (copy_attrs) {
266 /* set attrs */
267 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
268 DEBUG(0,("failed to set file-attrs: %s\n",
269 cli_errstr(cli_share_dst)));
270 nt_status = cli_nt_error(cli_share_dst);
271 goto out;
276 /* closing files */
278 if (!cli_close(cli_share_src, fnum_src)) {
279 d_fprintf(stderr, "could not close %s on originating server: %s\n",
280 is_file?"file":"dir", cli_errstr(cli_share_src));
281 nt_status = cli_nt_error(cli_share_src);
282 goto out;
285 if (!cli_close(cli_share_dst, fnum_dst)) {
286 d_fprintf(stderr, "could not close %s on destination server: %s\n",
287 is_file?"file":"dir", cli_errstr(cli_share_dst));
288 nt_status = cli_nt_error(cli_share_dst);
289 goto out;
293 nt_status = NT_STATUS_OK;
295 out:
297 /* cleaning up */
298 if (fnum_src)
299 cli_close(cli_share_src, fnum_src);
301 if (fnum_dst)
302 cli_close(cli_share_dst, fnum_dst);
304 return nt_status;
308 * Copy a file or directory from a connected share to another connected share
310 * @param mem_ctx A talloc-context
311 * @param cli_share_src A connected cli_state
312 * @param cli_share_dst A connected cli_state
313 * @param src_file The source file-name
314 * @param dst_file The destination file-name
315 * @param copy_acls Whether to copy acls
316 * @param copy_attrs Whether to copy DOS attributes
317 * @param copy_timestamps Whether to preserve timestamps
318 * @param is_file Whether this file is a file or a dir
320 * @return Normal NTSTATUS return.
321 **/
323 NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
324 struct cli_state *cli_share_src,
325 struct cli_state *cli_share_dst,
326 const char *src_name, const char *dst_name,
327 BOOL copy_acls, BOOL copy_attrs,
328 BOOL copy_timestamps, BOOL is_file)
330 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
331 int fnum_src = 0;
332 int fnum_dst = 0;
333 static int io_bufsize = 64512;
334 int read_size = io_bufsize;
335 char *data = NULL;
336 off_t start = 0;
337 off_t nread = 0;
340 if (!src_name || !dst_name)
341 goto out;
343 if (cli_share_src == NULL || cli_share_dst == NULL)
344 goto out;
347 /* open on the originating server */
348 DEBUGADD(3,("opening %s %s on originating server\n",
349 is_file ? "file":"dir", src_name));
350 if (is_file)
351 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
352 else
353 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
355 if (fnum_src == -1) {
356 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
357 is_file ? "file":"dir",
358 src_name, cli_errstr(cli_share_src)));
359 nt_status = cli_nt_error(cli_share_src);
360 goto out;
364 if (is_file) {
366 /* open file on the destination server */
367 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
368 fnum_dst = cli_open(cli_share_dst, dst_name,
369 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
371 if (fnum_dst == -1) {
372 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
373 dst_name, cli_errstr(cli_share_dst)));
374 nt_status = cli_nt_error(cli_share_dst);
375 goto out;
378 /* allocate memory */
379 if (!(data = (char *)SMB_MALLOC(read_size))) {
380 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
381 nt_status = NT_STATUS_NO_MEMORY;
382 goto out;
388 if (opt_verbose) {
390 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
391 "%s ACLs and %s DOS Attributes %s\n",
392 cli_share_src->desthost, cli_share_src->share, src_name,
393 cli_share_dst->desthost, cli_share_dst->share, dst_name,
394 copy_acls ? "with" : "without",
395 copy_attrs ? "with" : "without",
396 copy_timestamps ? "(preserving timestamps)" : "" );
400 while (is_file) {
402 /* copying file */
403 int n, ret;
404 n = cli_read(cli_share_src, fnum_src, data, nread + start,
405 read_size);
407 if (n <= 0)
408 break;
410 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
411 nread + start, n);
413 if (n != ret) {
414 d_fprintf(stderr, "Error writing file: %s\n",
415 cli_errstr(cli_share_dst));
416 nt_status = cli_nt_error(cli_share_dst);
417 goto out;
420 nread += n;
424 if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
426 /* creating dir */
427 DEBUGADD(3,("creating dir %s on the destination server\n",
428 dst_name));
430 if (!cli_mkdir(cli_share_dst, dst_name)) {
431 DEBUG(0,("cannot create directory %s: %s\n",
432 dst_name, cli_errstr(cli_share_dst)));
433 nt_status = NT_STATUS_NO_SUCH_FILE;
436 if (!cli_chkpath(cli_share_dst, dst_name)) {
437 d_fprintf(stderr, "cannot check for directory %s: %s\n",
438 dst_name, cli_errstr(cli_share_dst));
439 goto out;
444 /* closing files */
445 if (!cli_close(cli_share_src, fnum_src)) {
446 d_fprintf(stderr, "could not close file on originating server: %s\n",
447 cli_errstr(cli_share_src));
448 nt_status = cli_nt_error(cli_share_src);
449 goto out;
452 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
453 d_fprintf(stderr, "could not close file on destination server: %s\n",
454 cli_errstr(cli_share_dst));
455 nt_status = cli_nt_error(cli_share_dst);
456 goto out;
459 /* possibly we have to copy some file-attributes / acls / sd */
460 nt_status = net_copy_fileattr(mem_ctx, cli_share_src, cli_share_dst,
461 src_name, dst_name, copy_acls,
462 copy_attrs, copy_timestamps, is_file);
463 if (!NT_STATUS_IS_OK(nt_status))
464 goto out;
467 nt_status = NT_STATUS_OK;
469 out:
471 /* cleaning up */
472 if (fnum_src)
473 cli_close(cli_share_src, fnum_src);
475 if (fnum_dst)
476 cli_close(cli_share_dst, fnum_dst);
478 SAFE_FREE(data);
480 return nt_status;
484 * Copy a driverfile from on connected share to another connected share
485 * This silently assumes that a driver-file is picked up from
487 * \\src_server\print$\{arch}\{version}\file
489 * and copied to
491 * \\dst_server\print$\{arch}\file
493 * to be added via setdriver-calls later.
494 * @param mem_ctx A talloc-context
495 * @param cli_share_src A cli_state connected to source print$-share
496 * @param cli_share_dst A cli_state connected to destination print$-share
497 * @param file The file-name to be copied
498 * @param short_archi The name of the driver-architecture (short form)
500 * @return Normal NTSTATUS return.
501 **/
503 static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
504 struct cli_state *cli_share_src,
505 struct cli_state *cli_share_dst,
506 char *file, const char *short_archi) {
508 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
509 const char *p;
510 char *src_name;
511 char *dst_name;
512 fstring version;
513 fstring filename;
514 fstring tok;
516 /* scroll through the file until we have the part
517 beyond archi_table.short_archi */
518 p = file;
519 while (next_token(&p, tok, "\\", sizeof(tok))) {
520 if (strequal(tok, short_archi)) {
521 next_token(&p, version, "\\", sizeof(version));
522 next_token(&p, filename, "\\", sizeof(filename));
526 /* build source file name */
527 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
528 return NT_STATUS_NO_MEMORY;
531 /* create destination file name */
532 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
533 return NT_STATUS_NO_MEMORY;
536 /* finally copy the file */
537 nt_status = net_copy_file(mem_ctx, cli_share_src, cli_share_dst,
538 src_name, dst_name, False, False, False, True);
539 if (!NT_STATUS_IS_OK(nt_status))
540 goto out;
542 nt_status = NT_STATUS_OK;
544 out:
545 SAFE_FREE(src_name);
546 SAFE_FREE(dst_name);
548 return nt_status;
552 * Check for existing Architecture directory on a given server
554 * @param cli_share A cli_state connected to a print$-share
555 * @param short_archi The Architecture for the print-driver
557 * @return Normal NTSTATUS return.
560 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
563 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
564 char *dir;
566 if (asprintf(&dir, "\\%s", short_archi) < 0) {
567 return NT_STATUS_NO_MEMORY;
570 DEBUG(10,("creating print-driver dir for architecture: %s\n",
571 short_archi));
573 if (!cli_mkdir(cli_share, dir)) {
574 DEBUG(1,("cannot create directory %s: %s\n",
575 dir, cli_errstr(cli_share)));
576 nt_status = NT_STATUS_NO_SUCH_FILE;
579 if (!cli_chkpath(cli_share, dir)) {
580 d_fprintf(stderr, "cannot check %s: %s\n",
581 dir, cli_errstr(cli_share));
582 goto out;
585 nt_status = NT_STATUS_OK;
587 out:
588 SAFE_FREE(dir);
589 return nt_status;
593 * Copy a print-driver (level 3) from one connected print$-share to another
594 * connected print$-share
596 * @param mem_ctx A talloc-context
597 * @param cli_share_src A cli_state connected to a print$-share
598 * @param cli_share_dst A cli_state connected to a print$-share
599 * @param short_archi The Architecture for the print-driver
600 * @param i1 The DRIVER_INFO_3-struct
602 * @return Normal NTSTATUS return.
605 static NTSTATUS copy_print_driver_3(TALLOC_CTX *mem_ctx,
606 struct cli_state *cli_share_src,
607 struct cli_state *cli_share_dst,
608 const char *short_archi, DRIVER_INFO_3 *i1)
610 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
611 int length = 0;
612 BOOL valid = True;
614 fstring name = "";
615 fstring driverpath = "";
616 fstring datafile = "";
617 fstring configfile = "";
618 fstring helpfile = "";
619 fstring dependentfiles = "";
621 if (i1 == NULL)
622 return nt_status;
624 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
625 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
626 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
627 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
628 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
631 if (opt_verbose)
632 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
633 name, short_archi, i1->version);
635 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
636 driverpath, short_archi);
637 if (!NT_STATUS_IS_OK(nt_status))
638 return nt_status;
640 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
641 datafile, short_archi);
642 if (!NT_STATUS_IS_OK(nt_status))
643 return nt_status;
645 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
646 configfile, short_archi);
647 if (!NT_STATUS_IS_OK(nt_status))
648 return nt_status;
650 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
651 helpfile, short_archi);
652 if (!NT_STATUS_IS_OK(nt_status))
653 return nt_status;
655 while (valid) {
657 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
658 length += strlen(dependentfiles)+1;
660 if (strlen(dependentfiles) > 0) {
662 nt_status = net_copy_driverfile(mem_ctx,
663 cli_share_src, cli_share_dst,
664 dependentfiles, short_archi);
665 if (!NT_STATUS_IS_OK(nt_status))
666 return nt_status;
667 } else {
668 valid = False;
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 flags,
691 uint32 level,
692 uint32 *num_printers,
693 PRINTER_INFO_CTR *ctr)
695 WERROR result;
697 /* enum printers */
698 result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
699 level, num_printers, ctr);
701 if (!W_ERROR_IS_OK(result)) {
702 printf("cannot enum printers: %s\n", dos_errstr(result));
703 return False;
706 return True;
709 static BOOL net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
710 TALLOC_CTX *mem_ctx,
711 const char *printername,
712 uint32 access_required,
713 const char *username,
714 POLICY_HND *hnd)
716 WERROR result;
717 fstring servername, printername2;
719 slprintf(servername, sizeof(servername)-1, "\\\\%s", pipe_hnd->cli->desthost);
721 fstrcpy(printername2, servername);
722 fstrcat(printername2, "\\");
723 fstrcat(printername2, printername);
725 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
726 servername, username, printername2, access_required));
728 /* open printer */
729 result = rpccli_spoolss_open_printer_ex(pipe_hnd, mem_ctx, printername2,
730 "", access_required,
731 servername, username, hnd);
733 /* be more verbose */
734 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
735 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
736 printername2, servername, username);
737 return False;
740 if (!W_ERROR_IS_OK(result)) {
741 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
742 printername2, servername, dos_errstr(result));
743 return False;
746 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
747 printername2, servername));
749 return True;
752 static BOOL net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
753 TALLOC_CTX *mem_ctx,
754 POLICY_HND *hnd,
755 uint32 level,
756 PRINTER_INFO_CTR *ctr)
758 WERROR result;
760 /* getprinter call */
761 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx, hnd, level, ctr);
763 if (!W_ERROR_IS_OK(result)) {
764 printf("cannot get printer-info: %s\n", dos_errstr(result));
765 return False;
768 return True;
771 static BOOL net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
772 TALLOC_CTX *mem_ctx,
773 POLICY_HND *hnd,
774 uint32 level,
775 PRINTER_INFO_CTR *ctr)
777 WERROR result;
779 /* setprinter call */
780 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, hnd, level, ctr, 0);
782 if (!W_ERROR_IS_OK(result)) {
783 printf("cannot set printer-info: %s\n", dos_errstr(result));
784 return False;
787 return True;
791 static BOOL net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
792 TALLOC_CTX *mem_ctx,
793 POLICY_HND *hnd,
794 REGISTRY_VALUE *value)
796 WERROR result;
798 /* setprinterdata call */
799 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
801 if (!W_ERROR_IS_OK(result)) {
802 printf ("unable to set printerdata: %s\n", dos_errstr(result));
803 return False;
806 return True;
810 static BOOL net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
811 TALLOC_CTX *mem_ctx,
812 POLICY_HND *hnd,
813 const char *keyname,
814 uint16 **keylist)
816 WERROR result;
818 /* enumprinterkey call */
819 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
821 if (!W_ERROR_IS_OK(result)) {
822 printf("enumprinterkey failed: %s\n", dos_errstr(result));
823 return False;
826 return True;
829 static BOOL net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
830 TALLOC_CTX *mem_ctx,
831 uint32 offered,
832 POLICY_HND *hnd,
833 const char *keyname,
834 REGVAL_CTR *ctr)
836 WERROR result;
838 /* enumprinterdataex call */
839 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
841 if (!W_ERROR_IS_OK(result)) {
842 printf("enumprinterdataex failed: %s\n", dos_errstr(result));
843 return False;
846 return True;
850 static BOOL net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
851 TALLOC_CTX *mem_ctx,
852 POLICY_HND *hnd,
853 char *keyname,
854 REGISTRY_VALUE *value)
856 WERROR result;
858 /* setprinterdataex call */
859 result = rpccli_spoolss_setprinterdataex(pipe_hnd, mem_ctx, hnd,
860 keyname, value);
862 if (!W_ERROR_IS_OK(result)) {
863 printf("could not set printerdataex: %s\n", dos_errstr(result));
864 return False;
867 return True;
870 static BOOL net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
871 TALLOC_CTX *mem_ctx,
872 POLICY_HND *hnd,
873 int level,
874 uint32 *num_forms,
875 FORM_1 **forms)
878 WERROR result;
880 /* enumforms call */
881 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
883 if (!W_ERROR_IS_OK(result)) {
884 printf("could not enum forms: %s\n", dos_errstr(result));
885 return False;
888 return True;
891 static BOOL net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
892 TALLOC_CTX *mem_ctx,
893 uint32 level, const char *env,
894 uint32 *num_drivers,
895 PRINTER_DRIVER_CTR *ctr)
897 WERROR result;
899 /* enumprinterdrivers call */
900 result = rpccli_spoolss_enumprinterdrivers(
901 pipe_hnd, mem_ctx, level,
902 env, num_drivers, ctr);
904 if (!W_ERROR_IS_OK(result)) {
905 printf("cannot enum drivers: %s\n", dos_errstr(result));
906 return False;
909 return True;
912 static BOOL net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
913 TALLOC_CTX *mem_ctx,
914 POLICY_HND *hnd, uint32 level,
915 const char *env, int version,
916 PRINTER_DRIVER_CTR *ctr)
918 WERROR result;
920 /* getprinterdriver call */
921 result = rpccli_spoolss_getprinterdriver(
922 pipe_hnd, mem_ctx, hnd, level,
923 env, version, ctr);
925 if (!W_ERROR_IS_OK(result)) {
926 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
927 env, dos_errstr(result)));
928 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
929 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
930 printf("cannot get driver: %s\n", dos_errstr(result));
932 return False;
935 return True;
939 static BOOL net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
940 TALLOC_CTX *mem_ctx, uint32 level,
941 PRINTER_DRIVER_CTR *ctr)
943 WERROR result;
945 /* addprinterdriver call */
946 result = rpccli_spoolss_addprinterdriver(pipe_hnd, mem_ctx, level, ctr);
948 /* be more verbose */
949 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
950 printf("You are not allowed to add drivers\n");
951 return False;
953 if (!W_ERROR_IS_OK(result)) {
954 printf("cannot add driver: %s\n", dos_errstr(result));
955 return False;
958 return True;
962 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
963 * for a single printer or for all printers depending on argc/argv
966 static BOOL get_printer_info(struct rpc_pipe_client *pipe_hnd,
967 TALLOC_CTX *mem_ctx,
968 int level,
969 int argc,
970 const char **argv,
971 uint32 *num_printers,
972 PRINTER_INFO_CTR *ctr)
975 POLICY_HND hnd;
977 /* no arguments given, enumerate all printers */
978 if (argc == 0) {
980 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
981 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
982 level, num_printers, ctr))
983 return False;
985 goto out;
989 /* argument given, get a single printer by name */
990 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
991 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd))
992 return False;
994 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, ctr)) {
995 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
996 return False;
999 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1001 *num_printers = 1;
1003 out:
1004 DEBUG(3,("got %d printers\n", *num_printers));
1006 return True;
1010 /**
1011 * List print-queues (including local printers that are not shared)
1013 * All parameters are provided by the run_rpc_command function, except for
1014 * argc, argv which are passed through.
1016 * @param domain_sid The domain sid aquired from the remote server
1017 * @param cli A cli_state connected to the server.
1018 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1019 * @param argc Standard main() style argc
1020 * @param argv Standard main() style argv. Initial components are already
1021 * stripped
1023 * @return Normal NTSTATUS return.
1026 NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid,
1027 const char *domain_name,
1028 struct cli_state *cli,
1029 struct rpc_pipe_client *pipe_hnd,
1030 TALLOC_CTX *mem_ctx,
1031 int argc,
1032 const char **argv)
1034 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1035 uint32 i, num_printers;
1036 uint32 level = 2;
1037 pstring printername, sharename;
1038 PRINTER_INFO_CTR ctr;
1040 printf("listing printers\n");
1042 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1043 return nt_status;
1045 for (i = 0; i < num_printers; i++) {
1047 /* do some initialization */
1048 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1049 sizeof(printername), -1, STR_TERMINATE);
1050 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1051 sizeof(sharename), -1, STR_TERMINATE);
1053 d_printf("printer %d: %s, shared as: %s\n",
1054 i+1, printername, sharename);
1057 return NT_STATUS_OK;
1060 /**
1061 * List printer-drivers from a server
1063 * All parameters are provided by the run_rpc_command function, except for
1064 * argc, argv which are passed through.
1066 * @param domain_sid The domain sid aquired from the remote server
1067 * @param cli A cli_state connected to the server.
1068 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1069 * @param argc Standard main() style argc
1070 * @param argv Standard main() style argv. Initial components are already
1071 * stripped
1073 * @return Normal NTSTATUS return.
1076 NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid,
1077 const char *domain_name,
1078 struct cli_state *cli,
1079 struct rpc_pipe_client *pipe_hnd,
1080 TALLOC_CTX *mem_ctx,
1081 int argc,
1082 const char **argv)
1084 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1085 uint32 i;
1086 uint32 level = 3;
1087 PRINTER_DRIVER_CTR drv_ctr_enum;
1088 int d;
1090 ZERO_STRUCT(drv_ctr_enum);
1092 printf("listing printer-drivers\n");
1094 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1096 uint32 num_drivers;
1098 /* enum remote drivers */
1099 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1100 archi_table[i].long_archi,
1101 &num_drivers, &drv_ctr_enum)) {
1103 nt_status = NT_STATUS_UNSUCCESSFUL;
1104 goto done;
1107 if (num_drivers == 0) {
1108 d_printf ("no drivers found on server for architecture: [%s].\n",
1109 archi_table[i].long_archi);
1110 continue;
1113 d_printf("got %d printer-drivers for architecture: [%s]\n",
1114 num_drivers, archi_table[i].long_archi);
1117 /* do something for all drivers for architecture */
1118 for (d = 0; d < num_drivers; d++) {
1119 display_print_driver_3(&(drv_ctr_enum.info3[d]));
1123 nt_status = NT_STATUS_OK;
1125 done:
1126 return nt_status;
1130 /**
1131 * Publish print-queues with args-wrapper
1133 * @param cli A cli_state connected to the server.
1134 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1135 * @param argc Standard main() style argc
1136 * @param argv Standard main() style argv. Initial components are already
1137 * stripped
1138 * @param action
1140 * @return Normal NTSTATUS return.
1143 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1144 TALLOC_CTX *mem_ctx,
1145 int argc,
1146 const char **argv,
1147 uint32 action)
1149 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1150 uint32 i, num_printers;
1151 uint32 level = 7;
1152 pstring printername, sharename;
1153 PRINTER_INFO_CTR ctr, ctr_pub;
1154 POLICY_HND hnd;
1155 BOOL got_hnd = False;
1156 WERROR result;
1157 const char *action_str;
1159 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1160 return nt_status;
1162 for (i = 0; i < num_printers; i++) {
1164 /* do some initialization */
1165 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1166 sizeof(printername), -1, STR_TERMINATE);
1167 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1168 sizeof(sharename), -1, STR_TERMINATE);
1170 /* open printer handle */
1171 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1172 PRINTER_ALL_ACCESS, pipe_hnd->cli->user_name, &hnd))
1173 goto done;
1175 got_hnd = True;
1177 /* check for existing dst printer */
1178 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1179 goto done;
1181 /* check action and set string */
1182 switch (action) {
1183 case SPOOL_DS_PUBLISH:
1184 action_str = "published";
1185 break;
1186 case SPOOL_DS_UPDATE:
1187 action_str = "updated";
1188 break;
1189 case SPOOL_DS_UNPUBLISH:
1190 action_str = "unpublished";
1191 break;
1192 default:
1193 action_str = "unknown action";
1194 printf("unkown action: %d\n", action);
1195 break;
1198 ctr_pub.printers_7->action = action;
1200 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub, 0);
1201 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1202 printf("cannot set printer-info: %s\n", dos_errstr(result));
1203 goto done;
1206 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1209 nt_status = NT_STATUS_OK;
1211 done:
1212 if (got_hnd)
1213 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1215 return nt_status;
1218 NTSTATUS rpc_printer_publish_publish_internals(const DOM_SID *domain_sid,
1219 const char *domain_name,
1220 struct cli_state *cli,
1221 struct rpc_pipe_client *pipe_hnd,
1222 TALLOC_CTX *mem_ctx,
1223 int argc,
1224 const char **argv)
1226 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_PUBLISH);
1229 NTSTATUS rpc_printer_publish_unpublish_internals(const DOM_SID *domain_sid,
1230 const char *domain_name,
1231 struct cli_state *cli,
1232 struct rpc_pipe_client *pipe_hnd,
1233 TALLOC_CTX *mem_ctx,
1234 int argc,
1235 const char **argv)
1237 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
1240 NTSTATUS rpc_printer_publish_update_internals(const DOM_SID *domain_sid,
1241 const char *domain_name,
1242 struct cli_state *cli,
1243 struct rpc_pipe_client *pipe_hnd,
1244 TALLOC_CTX *mem_ctx,
1245 int argc,
1246 const char **argv)
1248 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
1251 /**
1252 * List print-queues w.r.t. their publishing state
1254 * All parameters are provided by the run_rpc_command function, except for
1255 * argc, argv which are passed through.
1257 * @param domain_sid The domain sid aquired from the remote server
1258 * @param cli A cli_state connected to the server.
1259 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1260 * @param argc Standard main() style argc
1261 * @param argv Standard main() style argv. Initial components are already
1262 * stripped
1264 * @return Normal NTSTATUS return.
1267 NTSTATUS rpc_printer_publish_list_internals(const DOM_SID *domain_sid,
1268 const char *domain_name,
1269 struct cli_state *cli,
1270 struct rpc_pipe_client *pipe_hnd,
1271 TALLOC_CTX *mem_ctx,
1272 int argc,
1273 const char **argv)
1275 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1276 uint32 i, num_printers;
1277 uint32 level = 7;
1278 pstring printername, sharename;
1279 pstring guid;
1280 PRINTER_INFO_CTR ctr, ctr_pub;
1281 POLICY_HND hnd;
1282 BOOL got_hnd = False;
1283 int state;
1285 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1286 return nt_status;
1288 for (i = 0; i < num_printers; i++) {
1290 ZERO_STRUCT(ctr_pub);
1292 /* do some initialization */
1293 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1294 sizeof(printername), -1, STR_TERMINATE);
1295 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1296 sizeof(sharename), -1, STR_TERMINATE);
1298 /* open printer handle */
1299 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1300 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1301 goto done;
1303 got_hnd = True;
1305 /* check for existing dst printer */
1306 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1307 goto done;
1309 rpcstr_pull(guid, ctr_pub.printers_7->guid.buffer, sizeof(guid), -1, STR_TERMINATE);
1311 state = ctr_pub.printers_7->action;
1312 switch (state) {
1313 case SPOOL_DS_PUBLISH:
1314 printf("printer [%s] is published", sharename);
1315 if (opt_verbose)
1316 printf(", guid: %s", guid);
1317 printf("\n");
1318 break;
1319 case SPOOL_DS_UNPUBLISH:
1320 printf("printer [%s] is unpublished\n", sharename);
1321 break;
1322 case SPOOL_DS_UPDATE:
1323 printf("printer [%s] is currently updating\n", sharename);
1324 break;
1325 default:
1326 printf("unkown state: %d\n", state);
1327 break;
1331 nt_status = NT_STATUS_OK;
1333 done:
1334 if (got_hnd)
1335 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1337 return nt_status;
1340 /**
1341 * Migrate Printer-ACLs from a source server to the destination server
1343 * All parameters are provided by the run_rpc_command function, except for
1344 * argc, argv which are passed through.
1346 * @param domain_sid The domain sid aquired from the remote server
1347 * @param cli A cli_state connected to the server.
1348 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1349 * @param argc Standard main() style argc
1350 * @param argv Standard main() style argv. Initial components are already
1351 * stripped
1353 * @return Normal NTSTATUS return.
1356 NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid,
1357 const char *domain_name,
1358 struct cli_state *cli,
1359 struct rpc_pipe_client *pipe_hnd,
1360 TALLOC_CTX *mem_ctx,
1361 int argc,
1362 const char **argv)
1364 /* TODO: what now, info2 or info3 ?
1365 convince jerry that we should add clientside setacls level 3 at least
1367 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1368 uint32 i = 0;
1369 uint32 num_printers;
1370 uint32 level = 2;
1371 pstring printername = "", sharename = "";
1372 BOOL got_hnd_src = False;
1373 BOOL got_hnd_dst = False;
1374 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1375 POLICY_HND hnd_src, hnd_dst;
1376 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1377 struct cli_state *cli_dst = NULL;
1379 ZERO_STRUCT(ctr_src);
1381 DEBUG(3,("copying printer ACLs\n"));
1383 /* connect destination PI_SPOOLSS */
1384 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1385 if (!NT_STATUS_IS_OK(nt_status))
1386 return nt_status;
1389 /* enum source printers */
1390 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1391 nt_status = NT_STATUS_UNSUCCESSFUL;
1392 goto done;
1395 if (!num_printers) {
1396 printf ("no printers found on server.\n");
1397 nt_status = NT_STATUS_OK;
1398 goto done;
1401 /* do something for all printers */
1402 for (i = 0; i < num_printers; i++) {
1404 /* do some initialization */
1405 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1406 sizeof(printername), -1, STR_TERMINATE);
1407 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1408 sizeof(sharename), -1, STR_TERMINATE);
1409 /* we can reset NT_STATUS here because we do not
1410 get any real NT_STATUS-codes anymore from now on */
1411 nt_status = NT_STATUS_UNSUCCESSFUL;
1413 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1414 printername, sharename);
1416 /* according to msdn you have specify these access-rights
1417 to see the security descriptor
1418 - READ_CONTROL (DACL)
1419 - ACCESS_SYSTEM_SECURITY (SACL)
1422 /* open src printer handle */
1423 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1424 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1425 goto done;
1427 got_hnd_src = True;
1429 /* open dst printer handle */
1430 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1431 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1432 goto done;
1434 got_hnd_dst = True;
1436 /* check for existing dst printer */
1437 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1438 goto done;
1440 /* check for existing src printer */
1441 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &ctr_src))
1442 goto done;
1444 /* Copy Security Descriptor */
1446 /* copy secdesc (info level 2) */
1447 ctr_dst.printers_2->devmode = NULL;
1448 ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
1450 if (opt_verbose)
1451 display_sec_desc(ctr_dst.printers_2->secdesc);
1453 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &ctr_dst))
1454 goto done;
1456 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1459 /* close printer handles here */
1460 if (got_hnd_src) {
1461 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1462 got_hnd_src = False;
1465 if (got_hnd_dst) {
1466 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1467 got_hnd_dst = False;
1472 nt_status = NT_STATUS_OK;
1474 done:
1476 if (got_hnd_src) {
1477 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1480 if (got_hnd_dst) {
1481 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1484 if (cli_dst) {
1485 cli_shutdown(cli_dst);
1487 return nt_status;
1490 /**
1491 * Migrate printer-forms from a src server to the dst server
1493 * All parameters are provided by the run_rpc_command function, except for
1494 * argc, argv which are passed through.
1496 * @param domain_sid The domain sid aquired from the remote server
1497 * @param cli A cli_state connected to the server.
1498 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1499 * @param argc Standard main() style argc
1500 * @param argv Standard main() style argv. Initial components are already
1501 * stripped
1503 * @return Normal NTSTATUS return.
1506 NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid,
1507 const char *domain_name,
1508 struct cli_state *cli,
1509 struct rpc_pipe_client *pipe_hnd,
1510 TALLOC_CTX *mem_ctx,
1511 int argc,
1512 const char **argv)
1514 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1515 WERROR result;
1516 uint32 i, f;
1517 uint32 num_printers;
1518 uint32 level = 1;
1519 pstring printername = "", sharename = "";
1520 BOOL got_hnd_src = False;
1521 BOOL got_hnd_dst = False;
1522 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1523 POLICY_HND hnd_src, hnd_dst;
1524 PRINTER_INFO_CTR ctr_enum, ctr_dst;
1525 uint32 num_forms;
1526 FORM_1 *forms;
1527 struct cli_state *cli_dst = NULL;
1529 ZERO_STRUCT(ctr_enum);
1531 DEBUG(3,("copying forms\n"));
1533 /* connect destination PI_SPOOLSS */
1534 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1535 if (!NT_STATUS_IS_OK(nt_status))
1536 return nt_status;
1539 /* enum src printers */
1540 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1541 nt_status = NT_STATUS_UNSUCCESSFUL;
1542 goto done;
1545 if (!num_printers) {
1546 printf ("no printers found on server.\n");
1547 nt_status = NT_STATUS_OK;
1548 goto done;
1552 /* do something for all printers */
1553 for (i = 0; i < num_printers; i++) {
1555 /* do some initialization */
1556 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1557 sizeof(printername), -1, STR_TERMINATE);
1558 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1559 sizeof(sharename), -1, STR_TERMINATE);
1560 /* we can reset NT_STATUS here because we do not
1561 get any real NT_STATUS-codes anymore from now on */
1562 nt_status = NT_STATUS_UNSUCCESSFUL;
1564 d_printf("migrating printer forms for: [%s] / [%s]\n",
1565 printername, sharename);
1568 /* open src printer handle */
1569 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1570 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1571 goto done;
1573 got_hnd_src = True;
1576 /* open dst printer handle */
1577 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1578 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1579 goto done;
1581 got_hnd_dst = True;
1584 /* check for existing dst printer */
1585 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1586 goto done;
1588 /* finally migrate forms */
1589 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1590 goto done;
1592 DEBUG(1,("got %d forms for printer\n", num_forms));
1595 for (f = 0; f < num_forms; f++) {
1597 FORM form;
1598 fstring form_name;
1600 /* only migrate FORM_PRINTER types, according to jerry
1601 FORM_BUILTIN-types are hard-coded in samba */
1602 if (forms[f].flag != FORM_PRINTER)
1603 continue;
1605 if (forms[f].name.buffer)
1606 rpcstr_pull(form_name, forms[f].name.buffer,
1607 sizeof(form_name), -1, STR_TERMINATE);
1609 if (opt_verbose)
1610 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1611 f, form_name, forms[f].flag);
1613 /* is there a more elegant way to do that ? */
1614 form.flags = FORM_PRINTER;
1615 form.size_x = forms[f].width;
1616 form.size_y = forms[f].length;
1617 form.left = forms[f].left;
1618 form.top = forms[f].top;
1619 form.right = forms[f].right;
1620 form.bottom = forms[f].bottom;
1622 init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
1624 /* FIXME: there might be something wrong with samba's
1625 builtin-forms */
1626 result = rpccli_spoolss_addform(pipe_hnd_dst, mem_ctx,
1627 &hnd_dst, 1, &form);
1628 if (!W_ERROR_IS_OK(result)) {
1629 d_printf("\tAddForm form %d: [%s] refused.\n",
1630 f, form_name);
1631 continue;
1634 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1638 /* close printer handles here */
1639 if (got_hnd_src) {
1640 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1641 got_hnd_src = False;
1644 if (got_hnd_dst) {
1645 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1646 got_hnd_dst = False;
1650 nt_status = NT_STATUS_OK;
1652 done:
1654 if (got_hnd_src)
1655 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1657 if (got_hnd_dst)
1658 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1660 if (cli_dst) {
1661 cli_shutdown(cli_dst);
1663 return nt_status;
1666 /**
1667 * Migrate printer-drivers from a src server to the dst server
1669 * All parameters are provided by the run_rpc_command function, except for
1670 * argc, argv which are passed through.
1672 * @param domain_sid The domain sid aquired from the remote server
1673 * @param cli A cli_state connected to the server.
1674 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1675 * @param argc Standard main() style argc
1676 * @param argv Standard main() style argv. Initial components are already
1677 * stripped
1679 * @return Normal NTSTATUS return.
1682 NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid,
1683 const char *domain_name,
1684 struct cli_state *cli,
1685 struct rpc_pipe_client *pipe_hnd,
1686 TALLOC_CTX *mem_ctx,
1687 int argc,
1688 const char **argv)
1690 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1691 uint32 i, p;
1692 uint32 num_printers;
1693 uint32 level = 3;
1694 pstring printername = "", sharename = "";
1695 BOOL got_hnd_src = False;
1696 BOOL got_hnd_dst = False;
1697 BOOL got_src_driver_share = False;
1698 BOOL got_dst_driver_share = False;
1699 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1700 POLICY_HND hnd_src, hnd_dst;
1701 PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
1702 PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1703 struct cli_state *cli_dst = NULL;
1704 struct cli_state *cli_share_src = NULL;
1705 struct cli_state *cli_share_dst = NULL;
1706 fstring drivername = "";
1708 ZERO_STRUCT(drv_ctr_src);
1709 ZERO_STRUCT(drv_ctr_dst);
1710 ZERO_STRUCT(info_ctr_enum);
1711 ZERO_STRUCT(info_ctr_dst);
1714 DEBUG(3,("copying printer-drivers\n"));
1716 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1717 if (!NT_STATUS_IS_OK(nt_status))
1718 return nt_status;
1721 /* open print$-share on the src server */
1722 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
1723 cli->desthost, "print$", "A:");
1724 if (!NT_STATUS_IS_OK(nt_status))
1725 goto done;
1727 got_src_driver_share = True;
1730 /* open print$-share on the dst server */
1731 nt_status = connect_to_service(&cli_share_dst, &cli_dst->dest_ip,
1732 cli_dst->desthost, "print$", "A:");
1733 if (!NT_STATUS_IS_OK(nt_status))
1734 return nt_status;
1736 got_dst_driver_share = True;
1739 /* enum src printers */
1740 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1741 nt_status = NT_STATUS_UNSUCCESSFUL;
1742 goto done;
1745 if (num_printers == 0) {
1746 printf ("no printers found on server.\n");
1747 nt_status = NT_STATUS_OK;
1748 goto done;
1752 /* do something for all printers */
1753 for (p = 0; p < num_printers; p++) {
1755 /* do some initialization */
1756 rpcstr_pull(printername, info_ctr_enum.printers_2[p].printername.buffer,
1757 sizeof(printername), -1, STR_TERMINATE);
1758 rpcstr_pull(sharename, info_ctr_enum.printers_2[p].sharename.buffer,
1759 sizeof(sharename), -1, STR_TERMINATE);
1760 /* we can reset NT_STATUS here because we do not
1761 get any real NT_STATUS-codes anymore from now on */
1762 nt_status = NT_STATUS_UNSUCCESSFUL;
1764 d_printf("migrating printer driver for: [%s] / [%s]\n",
1765 printername, sharename);
1767 /* open dst printer handle */
1768 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1769 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1770 goto done;
1772 got_hnd_dst = True;
1774 /* check for existing dst printer */
1775 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst))
1776 goto done;
1779 /* open src printer handle */
1780 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1781 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd_src))
1782 goto done;
1784 got_hnd_src = True;
1787 /* in a first step call getdriver for each shared printer (per arch)
1788 to get a list of all files that have to be copied */
1790 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1792 /* getdriver src */
1793 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1794 level, archi_table[i].long_archi,
1795 archi_table[i].version, &drv_ctr_src))
1796 continue;
1798 rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer,
1799 sizeof(drivername), -1, STR_TERMINATE);
1801 if (opt_verbose)
1802 display_print_driver_3(drv_ctr_src.info3);
1805 /* check arch dir */
1806 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1807 if (!NT_STATUS_IS_OK(nt_status))
1808 goto done;
1811 /* copy driver-files */
1812 nt_status = copy_print_driver_3(mem_ctx, cli_share_src, cli_share_dst,
1813 archi_table[i].short_archi,
1814 drv_ctr_src.info3);
1815 if (!NT_STATUS_IS_OK(nt_status))
1816 goto done;
1819 /* adddriver dst */
1820 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_ctr_src)) {
1821 nt_status = NT_STATUS_UNSUCCESSFUL;
1822 goto done;
1825 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1826 drivername, printername));
1830 if (strlen(drivername) == 0) {
1831 DEBUGADD(1,("Did not get driver for printer %s\n",
1832 printername));
1833 goto done;
1836 /* setdriver dst */
1837 init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
1839 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) {
1840 nt_status = NT_STATUS_UNSUCCESSFUL;
1841 goto done;
1844 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1845 drivername, printername));
1847 /* close dst */
1848 if (got_hnd_dst) {
1849 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1850 got_hnd_dst = False;
1853 /* close src */
1854 if (got_hnd_src) {
1855 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1856 got_hnd_src = False;
1860 nt_status = NT_STATUS_OK;
1862 done:
1864 if (got_hnd_src)
1865 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1867 if (got_hnd_dst)
1868 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1870 if (cli_dst) {
1871 cli_shutdown(cli_dst);
1874 if (got_src_driver_share)
1875 cli_shutdown(cli_share_src);
1877 if (got_dst_driver_share)
1878 cli_shutdown(cli_share_dst);
1880 return nt_status;
1884 /**
1885 * Migrate printer-queues from a src to the dst server
1886 * (requires a working "addprinter command" to be installed for the local smbd)
1888 * All parameters are provided by the run_rpc_command function, except for
1889 * argc, argv which are passed through.
1891 * @param domain_sid The domain sid aquired from the remote server
1892 * @param cli A cli_state connected to the server.
1893 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1894 * @param argc Standard main() style argc
1895 * @param argv Standard main() style argv. Initial components are already
1896 * stripped
1898 * @return Normal NTSTATUS return.
1901 NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid,
1902 const char *domain_name,
1903 struct cli_state *cli,
1904 struct rpc_pipe_client *pipe_hnd,
1905 TALLOC_CTX *mem_ctx,
1906 int argc,
1907 const char **argv)
1909 WERROR result;
1910 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1911 uint32 i = 0, num_printers;
1912 uint32 level = 2;
1913 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1914 struct cli_state *cli_dst = NULL;
1915 POLICY_HND hnd_dst, hnd_src;
1916 pstring printername, sharename;
1917 BOOL got_hnd_src = False;
1918 BOOL got_hnd_dst = False;
1919 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1921 DEBUG(3,("copying printers\n"));
1923 /* connect destination PI_SPOOLSS */
1924 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1925 if (!NT_STATUS_IS_OK(nt_status))
1926 return nt_status;
1928 /* enum printers */
1929 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1930 nt_status = NT_STATUS_UNSUCCESSFUL;
1931 goto done;
1934 if (!num_printers) {
1935 printf ("no printers found on server.\n");
1936 nt_status = NT_STATUS_OK;
1937 goto done;
1941 /* do something for all printers */
1942 for (i = 0; i < num_printers; i++) {
1944 /* do some initialization */
1945 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1946 sizeof(printername), -1, STR_TERMINATE);
1947 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1948 sizeof(sharename), -1, STR_TERMINATE);
1949 /* we can reset NT_STATUS here because we do not
1950 get any real NT_STATUS-codes anymore from now on */
1951 nt_status = NT_STATUS_UNSUCCESSFUL;
1953 d_printf("migrating printer queue for: [%s] / [%s]\n",
1954 printername, sharename);
1956 /* open dst printer handle */
1957 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1958 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
1960 DEBUG(1,("could not open printer: %s\n", sharename));
1961 } else {
1962 got_hnd_dst = True;
1965 /* check for existing dst printer */
1966 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) {
1967 printf ("could not get printer, creating printer.\n");
1968 } else {
1969 DEBUG(1,("printer already exists: %s\n", sharename));
1970 /* close printer handle here - dst only, not got src yet. */
1971 if (got_hnd_dst) {
1972 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1973 got_hnd_dst = False;
1975 continue;
1978 /* now get again src printer ctr via getprinter,
1979 we first need a handle for that */
1981 /* open src printer handle */
1982 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1983 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1984 goto done;
1986 got_hnd_src = True;
1988 /* getprinter on the src server */
1989 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &ctr_src))
1990 goto done;
1992 /* copy each src printer to a dst printer 1:1,
1993 maybe some values have to be changed though */
1994 d_printf("creating printer: %s\n", printername);
1995 result = rpccli_spoolss_addprinterex (pipe_hnd_dst, mem_ctx, level, &ctr_src);
1997 if (W_ERROR_IS_OK(result))
1998 d_printf ("printer [%s] successfully added.\n", printername);
1999 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2000 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2001 else {
2002 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2003 goto done;
2006 /* close printer handles here */
2007 if (got_hnd_src) {
2008 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2009 got_hnd_src = False;
2012 if (got_hnd_dst) {
2013 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2014 got_hnd_dst = False;
2018 nt_status = NT_STATUS_OK;
2020 done:
2021 if (got_hnd_src)
2022 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2024 if (got_hnd_dst)
2025 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2027 if (cli_dst) {
2028 cli_shutdown(cli_dst);
2030 return nt_status;
2033 /**
2034 * Migrate Printer-Settings from a src server to the dst server
2035 * (for this to work, printers and drivers already have to be migrated earlier)
2037 * All parameters are provided by the run_rpc_command function, except for
2038 * argc, argv which are passed through.
2040 * @param domain_sid The domain sid aquired from the remote server
2041 * @param cli A cli_state connected to the server.
2042 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2043 * @param argc Standard main() style argc
2044 * @param argv Standard main() style argv. Initial components are already
2045 * stripped
2047 * @return Normal NTSTATUS return.
2050 NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid,
2051 const char *domain_name,
2052 struct cli_state *cli,
2053 struct rpc_pipe_client *pipe_hnd,
2054 TALLOC_CTX *mem_ctx,
2055 int argc,
2056 const char **argv)
2059 /* FIXME: Here the nightmare begins */
2061 WERROR result;
2062 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2063 uint32 i = 0, p = 0, j = 0;
2064 uint32 num_printers, val_needed, data_needed;
2065 uint32 level = 2;
2066 pstring printername = "", sharename = "";
2067 BOOL got_hnd_src = False;
2068 BOOL got_hnd_dst = False;
2069 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2070 POLICY_HND hnd_src, hnd_dst;
2071 PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
2072 REGVAL_CTR *reg_ctr;
2073 struct cli_state *cli_dst = NULL;
2074 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2075 fstring longname;
2077 uint16 *keylist = NULL, *curkey;
2079 ZERO_STRUCT(ctr_enum);
2081 DEBUG(3,("copying printer settings\n"));
2083 /* connect destination PI_SPOOLSS */
2084 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
2085 if (!NT_STATUS_IS_OK(nt_status))
2086 return nt_status;
2089 /* enum src printers */
2090 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2091 nt_status = NT_STATUS_UNSUCCESSFUL;
2092 goto done;
2095 if (!num_printers) {
2096 printf ("no printers found on server.\n");
2097 nt_status = NT_STATUS_OK;
2098 goto done;
2102 /* needed for dns-strings in regkeys */
2103 get_mydnsfullname(longname);
2105 /* do something for all printers */
2106 for (i = 0; i < num_printers; i++) {
2108 /* do some initialization */
2109 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
2110 sizeof(printername), -1, STR_TERMINATE);
2111 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
2112 sizeof(sharename), -1, STR_TERMINATE);
2114 /* we can reset NT_STATUS here because we do not
2115 get any real NT_STATUS-codes anymore from now on */
2116 nt_status = NT_STATUS_UNSUCCESSFUL;
2118 d_printf("migrating printer settings for: [%s] / [%s]\n",
2119 printername, sharename);
2122 /* open src printer handle */
2123 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2124 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2125 goto done;
2127 got_hnd_src = True;
2130 /* open dst printer handle */
2131 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2132 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2133 goto done;
2135 got_hnd_dst = True;
2138 /* check for existing dst printer */
2139 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2140 level, &ctr_dst))
2141 goto done;
2144 /* STEP 1: COPY DEVICE-MODE and other
2145 PRINTER_INFO_2-attributes
2148 ctr_dst.printers_2 = &ctr_enum.printers_2[i];
2150 /* why is the port always disconnected when the printer
2151 is correctly installed (incl. driver ???) */
2152 init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
2154 /* check if printer is published */
2155 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2157 /* check for existing dst printer */
2158 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
2159 goto done;
2161 ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
2163 /* ignore False from setprinter due to WERR_IO_PENDING */
2164 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
2166 DEBUG(3,("republished printer\n"));
2169 if (ctr_enum.printers_2[i].devmode != NULL) {
2171 /* copy devmode (info level 2) */
2172 ctr_dst.printers_2->devmode = (DEVICEMODE *)
2173 TALLOC_MEMDUP(mem_ctx,
2174 ctr_enum.printers_2[i].devmode,
2175 sizeof(DEVICEMODE));
2177 /* do not copy security descriptor (we have another
2178 * command for that) */
2179 ctr_dst.printers_2->secdesc = NULL;
2181 #if 0
2182 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2183 printername) < 0) {
2184 nt_status = NT_STATUS_NO_MEMORY;
2185 goto done;
2188 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2189 devicename);
2190 #endif
2191 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2192 level, &ctr_dst))
2193 goto done;
2195 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2198 /* STEP 2: COPY REGISTRY VALUES */
2200 /* please keep in mind that samba parse_spools gives horribly
2201 crippled results when used to rpccli_spoolss_enumprinterdataex
2202 a win2k3-server. (Bugzilla #1851)
2203 FIXME: IIRC I've seen it too on a win2k-server
2206 /* enumerate data on src handle */
2207 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2208 &val_needed, &data_needed, NULL);
2210 /* loop for all printerdata of "PrinterDriverData" */
2211 while (W_ERROR_IS_OK(result)) {
2213 REGISTRY_VALUE value;
2215 result = rpccli_spoolss_enumprinterdata(
2216 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2217 data_needed, 0, 0, &value);
2219 /* loop for all reg_keys */
2220 if (W_ERROR_IS_OK(result)) {
2222 /* display_value */
2223 if (opt_verbose)
2224 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2226 /* set_value */
2227 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2228 &hnd_dst, &value))
2229 goto done;
2231 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2232 value.valuename));
2236 /* STEP 3: COPY SUBKEY VALUES */
2238 /* here we need to enum all printer_keys and then work
2239 on the result with enum_printer_key_ex. nt4 does not
2240 respond to enumprinterkey, win2k does, so continue
2241 in case of an error */
2243 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2244 printf("got no key-data\n");
2245 continue;
2249 /* work on a list of printer keys
2250 each key has to be enumerated to get all required
2251 information. information is then set via setprinterdataex-calls */
2253 if (keylist == NULL)
2254 continue;
2256 curkey = keylist;
2257 while (*curkey != 0) {
2259 pstring subkey;
2260 rpcstr_pull(subkey, curkey, sizeof(subkey), -1, STR_TERMINATE);
2262 curkey += strlen(subkey) + 1;
2264 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2265 return NT_STATUS_NO_MEMORY;
2267 /* enumerate all src subkeys */
2268 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2269 &hnd_src, subkey,
2270 reg_ctr))
2271 goto done;
2273 for (j=0; j < reg_ctr->num_values; j++) {
2275 REGISTRY_VALUE value;
2276 UNISTR2 data;
2278 /* although samba replies with sane data in most cases we
2279 should try to avoid writing wrong registry data */
2281 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2282 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2283 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2284 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2285 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2287 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2289 /* although windows uses a multi-sz, we use a sz */
2290 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2291 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2294 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2296 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2297 nt_status = NT_STATUS_NO_MEMORY;
2298 goto done;
2300 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2301 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2304 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2306 continue;
2308 #if 0
2309 /* FIXME: should we really do that ??? */
2310 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2311 nt_status = NT_STATUS_NO_MEMORY;
2312 goto done;
2314 init_unistr2(&data, url, UNI_STR_TERMINATE);
2315 fstrcpy(value.valuename, SPOOL_REG_URL);
2316 #endif
2319 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2321 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2322 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2325 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2327 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2328 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2331 value.type = REG_SZ;
2332 value.size = data.uni_str_len * 2;
2333 if (value.size) {
2334 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2335 } else {
2336 value.data_p = NULL;
2339 if (opt_verbose)
2340 display_reg_value(subkey, value);
2342 /* here we have to set all subkeys on the dst server */
2343 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2344 subkey, &value))
2345 goto done;
2347 } else {
2349 if (opt_verbose)
2350 display_reg_value(subkey, *(reg_ctr->values[j]));
2352 /* here we have to set all subkeys on the dst server */
2353 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2354 subkey, reg_ctr->values[j]))
2355 goto done;
2359 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2360 subkey, reg_ctr->values[j]->valuename));
2364 TALLOC_FREE( reg_ctr );
2367 safe_free(keylist);
2369 /* close printer handles here */
2370 if (got_hnd_src) {
2371 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2372 got_hnd_src = False;
2375 if (got_hnd_dst) {
2376 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2377 got_hnd_dst = False;
2382 nt_status = NT_STATUS_OK;
2384 done:
2385 SAFE_FREE(devicename);
2386 SAFE_FREE(url);
2387 SAFE_FREE(unc_name);
2389 if (got_hnd_src)
2390 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2392 if (got_hnd_dst)
2393 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2395 if (cli_dst) {
2396 cli_shutdown(cli_dst);
2398 return nt_status;