Add comment explaining the previous fix.
[Samba.git] / source / utils / net_rpc_printer.c
blobbcfb37713c9265a152ebe9b5fd5d41d407fe2551
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 char *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_talloc(talloc_tos(),
121 &text,
122 value.data_p,
123 value.size,
124 STR_TERMINATE);
125 if (!text) {
126 break;
128 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
129 break;
131 case REG_BINARY:
132 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
133 subkey, value.valuename);
134 break;
136 case REG_MULTI_SZ: {
137 uint32 i, num_values;
138 char **values;
140 if (!W_ERROR_IS_OK(reg_pull_multi_sz(NULL, value.data_p,
141 value.size, &num_values,
142 &values))) {
143 d_printf("reg_pull_multi_sz failed\n");
144 break;
147 for (i=0; i<num_values; i++) {
148 d_printf("%s\n", values[i]);
150 TALLOC_FREE(values);
151 break;
154 default:
155 d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
161 * Copies ACLs, DOS-attributes and timestamps from one
162 * file or directory from one connected share to another connected share
164 * @param mem_ctx A talloc-context
165 * @param cli_share_src A connected cli_state
166 * @param cli_share_dst A connected cli_state
167 * @param src_file The source file-name
168 * @param dst_file The destination file-name
169 * @param copy_acls Whether to copy acls
170 * @param copy_attrs Whether to copy DOS attributes
171 * @param copy_timestamps Whether to preserve timestamps
172 * @param is_file Whether this file is a file or a dir
174 * @return Normal NTSTATUS return.
175 **/
177 NTSTATUS net_copy_fileattr(TALLOC_CTX *mem_ctx,
178 struct cli_state *cli_share_src,
179 struct cli_state *cli_share_dst,
180 const char *src_name, const char *dst_name,
181 bool copy_acls, bool copy_attrs,
182 bool copy_timestamps, bool is_file)
184 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
185 int fnum_src = 0;
186 int fnum_dst = 0;
187 SEC_DESC *sd = NULL;
188 uint16 attr;
189 time_t f_atime, f_ctime, f_mtime;
192 if (!copy_timestamps && !copy_acls && !copy_attrs)
193 return NT_STATUS_OK;
195 /* open file/dir on the originating server */
197 DEBUGADD(3,("opening %s %s on originating server\n",
198 is_file?"file":"dir", src_name));
200 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
201 if (fnum_src == -1) {
202 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
203 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
204 nt_status = cli_nt_error(cli_share_src);
205 goto out;
209 if (copy_acls) {
211 /* get the security descriptor */
212 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
213 if (!sd) {
214 DEBUG(0,("failed to get security descriptor: %s\n",
215 cli_errstr(cli_share_src)));
216 nt_status = cli_nt_error(cli_share_src);
217 goto out;
220 if (opt_verbose && DEBUGLEVEL >= 3)
221 display_sec_desc(sd);
225 if (copy_attrs || copy_timestamps) {
227 /* get file attributes */
228 if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
229 &f_ctime, &f_atime, &f_mtime)) {
230 DEBUG(0,("failed to get file-attrs: %s\n",
231 cli_errstr(cli_share_src)));
232 nt_status = cli_nt_error(cli_share_src);
233 goto out;
238 /* open the file/dir on the destination server */
240 fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
241 if (fnum_dst == -1) {
242 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
243 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
244 nt_status = cli_nt_error(cli_share_dst);
245 goto out;
248 if (copy_timestamps) {
250 /* set timestamps */
251 if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
252 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
253 cli_errstr(cli_share_dst)));
254 nt_status = cli_nt_error(cli_share_dst);
255 goto out;
259 if (copy_acls) {
261 /* set acls */
262 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
263 DEBUG(0,("could not set secdesc on %s: %s\n",
264 dst_name, cli_errstr(cli_share_dst)));
265 nt_status = cli_nt_error(cli_share_dst);
266 goto out;
270 if (copy_attrs) {
272 /* set attrs */
273 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
274 DEBUG(0,("failed to set file-attrs: %s\n",
275 cli_errstr(cli_share_dst)));
276 nt_status = cli_nt_error(cli_share_dst);
277 goto out;
282 /* closing files */
284 if (!cli_close(cli_share_src, fnum_src)) {
285 d_fprintf(stderr, "could not close %s on originating server: %s\n",
286 is_file?"file":"dir", cli_errstr(cli_share_src));
287 nt_status = cli_nt_error(cli_share_src);
288 goto out;
291 if (!cli_close(cli_share_dst, fnum_dst)) {
292 d_fprintf(stderr, "could not close %s on destination server: %s\n",
293 is_file?"file":"dir", cli_errstr(cli_share_dst));
294 nt_status = cli_nt_error(cli_share_dst);
295 goto out;
299 nt_status = NT_STATUS_OK;
301 out:
303 /* cleaning up */
304 if (fnum_src)
305 cli_close(cli_share_src, fnum_src);
307 if (fnum_dst)
308 cli_close(cli_share_dst, fnum_dst);
310 return nt_status;
314 * Copy a file or directory from a connected share to another connected share
316 * @param mem_ctx A talloc-context
317 * @param cli_share_src A connected cli_state
318 * @param cli_share_dst A connected cli_state
319 * @param src_file The source file-name
320 * @param dst_file The destination file-name
321 * @param copy_acls Whether to copy acls
322 * @param copy_attrs Whether to copy DOS attributes
323 * @param copy_timestamps Whether to preserve timestamps
324 * @param is_file Whether this file is a file or a dir
326 * @return Normal NTSTATUS return.
327 **/
329 NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
330 struct cli_state *cli_share_src,
331 struct cli_state *cli_share_dst,
332 const char *src_name, const char *dst_name,
333 bool copy_acls, bool copy_attrs,
334 bool copy_timestamps, bool is_file)
336 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
337 int fnum_src = 0;
338 int fnum_dst = 0;
339 static int io_bufsize = 64512;
340 int read_size = io_bufsize;
341 char *data = NULL;
342 off_t nread = 0;
345 if (!src_name || !dst_name)
346 goto out;
348 if (cli_share_src == NULL || cli_share_dst == NULL)
349 goto out;
352 /* open on the originating server */
353 DEBUGADD(3,("opening %s %s on originating server\n",
354 is_file ? "file":"dir", src_name));
355 if (is_file)
356 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
357 else
358 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
360 if (fnum_src == -1) {
361 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
362 is_file ? "file":"dir",
363 src_name, cli_errstr(cli_share_src)));
364 nt_status = cli_nt_error(cli_share_src);
365 goto out;
369 if (is_file) {
371 /* open file on the destination server */
372 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
373 fnum_dst = cli_open(cli_share_dst, dst_name,
374 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
376 if (fnum_dst == -1) {
377 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
378 dst_name, cli_errstr(cli_share_dst)));
379 nt_status = cli_nt_error(cli_share_dst);
380 goto out;
383 /* allocate memory */
384 if (!(data = (char *)SMB_MALLOC(read_size))) {
385 d_fprintf(stderr, "malloc fail for size %d\n", read_size);
386 nt_status = NT_STATUS_NO_MEMORY;
387 goto out;
393 if (opt_verbose) {
395 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
396 "%s ACLs and %s DOS Attributes %s\n",
397 cli_share_src->desthost, cli_share_src->share, src_name,
398 cli_share_dst->desthost, cli_share_dst->share, dst_name,
399 copy_acls ? "with" : "without",
400 copy_attrs ? "with" : "without",
401 copy_timestamps ? "(preserving timestamps)" : "" );
405 while (is_file) {
407 /* copying file */
408 int n, ret;
409 n = cli_read(cli_share_src, fnum_src, data, nread,
410 read_size);
412 if (n <= 0)
413 break;
415 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
416 nread, n);
418 if (n != ret) {
419 d_fprintf(stderr, "Error writing file: %s\n",
420 cli_errstr(cli_share_dst));
421 nt_status = cli_nt_error(cli_share_dst);
422 goto out;
425 nread += n;
429 if (!is_file && !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 if (!cli_mkdir(cli_share_dst, dst_name)) {
436 DEBUG(0,("cannot create directory %s: %s\n",
437 dst_name, cli_errstr(cli_share_dst)));
438 nt_status = NT_STATUS_NO_SUCH_FILE;
441 if (!cli_chkpath(cli_share_dst, dst_name)) {
442 d_fprintf(stderr, "cannot check for directory %s: %s\n",
443 dst_name, cli_errstr(cli_share_dst));
444 goto out;
449 /* closing files */
450 if (!cli_close(cli_share_src, fnum_src)) {
451 d_fprintf(stderr, "could not close file on originating server: %s\n",
452 cli_errstr(cli_share_src));
453 nt_status = cli_nt_error(cli_share_src);
454 goto out;
457 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
458 d_fprintf(stderr, "could not close file on destination server: %s\n",
459 cli_errstr(cli_share_dst));
460 nt_status = cli_nt_error(cli_share_dst);
461 goto out;
464 /* possibly we have to copy some file-attributes / acls / sd */
465 nt_status = net_copy_fileattr(mem_ctx, cli_share_src, cli_share_dst,
466 src_name, dst_name, copy_acls,
467 copy_attrs, copy_timestamps, is_file);
468 if (!NT_STATUS_IS_OK(nt_status))
469 goto out;
472 nt_status = NT_STATUS_OK;
474 out:
476 /* cleaning up */
477 if (fnum_src)
478 cli_close(cli_share_src, fnum_src);
480 if (fnum_dst)
481 cli_close(cli_share_dst, fnum_dst);
483 SAFE_FREE(data);
485 return nt_status;
489 * Copy a driverfile from on connected share to another connected share
490 * This silently assumes that a driver-file is picked up from
492 * \\src_server\print$\{arch}\{version}\file
494 * and copied to
496 * \\dst_server\print$\{arch}\file
498 * to be added via setdriver-calls later.
499 * @param mem_ctx A talloc-context
500 * @param cli_share_src A cli_state connected to source print$-share
501 * @param cli_share_dst A cli_state connected to destination print$-share
502 * @param file The file-name to be copied
503 * @param short_archi The name of the driver-architecture (short form)
505 * @return Normal NTSTATUS return.
506 **/
508 static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
509 struct cli_state *cli_share_src,
510 struct cli_state *cli_share_dst,
511 char *file, const char *short_archi) {
513 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
514 const char *p;
515 char *src_name;
516 char *dst_name;
517 char *version;
518 char *filename;
519 char *tok;
521 /* scroll through the file until we have the part
522 beyond archi_table.short_archi */
523 p = file;
524 while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
525 if (strequal(tok, short_archi)) {
526 next_token_talloc(mem_ctx, &p, &version, "\\");
527 next_token_talloc(mem_ctx, &p, &filename, "\\");
531 /* build source file name */
532 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
533 return NT_STATUS_NO_MEMORY;
536 /* create destination file name */
537 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
538 return NT_STATUS_NO_MEMORY;
541 /* finally copy the file */
542 nt_status = net_copy_file(mem_ctx, cli_share_src, cli_share_dst,
543 src_name, dst_name, False, False, False, True);
544 if (!NT_STATUS_IS_OK(nt_status))
545 goto out;
547 nt_status = NT_STATUS_OK;
549 out:
550 SAFE_FREE(src_name);
551 SAFE_FREE(dst_name);
553 return nt_status;
557 * Check for existing Architecture directory on a given server
559 * @param cli_share A cli_state connected to a print$-share
560 * @param short_archi The Architecture for the print-driver
562 * @return Normal NTSTATUS return.
565 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
568 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
569 char *dir;
571 if (asprintf(&dir, "\\%s", short_archi) < 0) {
572 return NT_STATUS_NO_MEMORY;
575 DEBUG(10,("creating print-driver dir for architecture: %s\n",
576 short_archi));
578 if (!cli_mkdir(cli_share, dir)) {
579 DEBUG(1,("cannot create directory %s: %s\n",
580 dir, cli_errstr(cli_share)));
581 nt_status = NT_STATUS_NO_SUCH_FILE;
584 if (!cli_chkpath(cli_share, dir)) {
585 d_fprintf(stderr, "cannot check %s: %s\n",
586 dir, cli_errstr(cli_share));
587 goto out;
590 nt_status = NT_STATUS_OK;
592 out:
593 SAFE_FREE(dir);
594 return nt_status;
598 * Copy a print-driver (level 3) from one connected print$-share to another
599 * connected print$-share
601 * @param mem_ctx A talloc-context
602 * @param cli_share_src A cli_state connected to a print$-share
603 * @param cli_share_dst A cli_state connected to a print$-share
604 * @param short_archi The Architecture for the print-driver
605 * @param i1 The DRIVER_INFO_3-struct
607 * @return Normal NTSTATUS return.
610 static NTSTATUS copy_print_driver_3(TALLOC_CTX *mem_ctx,
611 struct cli_state *cli_share_src,
612 struct cli_state *cli_share_dst,
613 const char *short_archi, DRIVER_INFO_3 *i1)
615 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
616 int length = 0;
617 bool valid = True;
619 fstring name = "";
620 fstring driverpath = "";
621 fstring datafile = "";
622 fstring configfile = "";
623 fstring helpfile = "";
624 fstring dependentfiles = "";
626 if (i1 == NULL)
627 return nt_status;
629 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
630 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
631 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
632 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
633 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
636 if (opt_verbose)
637 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
638 name, short_archi, i1->version);
640 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
641 driverpath, 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 datafile, 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 configfile, short_archi);
652 if (!NT_STATUS_IS_OK(nt_status))
653 return nt_status;
655 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
656 helpfile, short_archi);
657 if (!NT_STATUS_IS_OK(nt_status))
658 return nt_status;
660 while (valid) {
662 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
663 length += strlen(dependentfiles)+1;
665 if (strlen(dependentfiles) > 0) {
667 nt_status = net_copy_driverfile(mem_ctx,
668 cli_share_src, cli_share_dst,
669 dependentfiles, short_archi);
670 if (!NT_STATUS_IS_OK(nt_status))
671 return nt_status;
672 } else {
673 valid = False;
677 return NT_STATUS_OK;
681 * net_spoolss-functions
682 * =====================
684 * the net_spoolss-functions aim to simplify spoolss-client-functions
685 * required during the migration-process wrt buffer-sizes, returned
686 * error-codes, etc.
688 * this greatly reduces the complexitiy of the migrate-functions.
692 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
693 TALLOC_CTX *mem_ctx,
694 char *name,
695 uint32 flags,
696 uint32 level,
697 uint32 *num_printers,
698 PRINTER_INFO_CTR *ctr)
700 WERROR result;
702 /* enum printers */
703 result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
704 level, num_printers, ctr);
706 if (!W_ERROR_IS_OK(result)) {
707 printf("cannot enum printers: %s\n", dos_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 access_required,
718 const char *username,
719 POLICY_HND *hnd)
721 WERROR result;
722 fstring servername, printername2;
724 slprintf(servername, sizeof(servername)-1, "\\\\%s", pipe_hnd->cli->desthost);
726 fstrcpy(printername2, servername);
727 fstrcat(printername2, "\\");
728 fstrcat(printername2, printername);
730 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
731 servername, username, printername2, access_required));
733 /* open printer */
734 result = rpccli_spoolss_open_printer_ex(pipe_hnd, mem_ctx, printername2,
735 "", access_required,
736 servername, username, hnd);
738 /* be more verbose */
739 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
740 d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
741 printername2, servername, username);
742 return False;
745 if (!W_ERROR_IS_OK(result)) {
746 d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
747 printername2, servername, dos_errstr(result));
748 return False;
751 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
752 printername2, servername));
754 return True;
757 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
758 TALLOC_CTX *mem_ctx,
759 POLICY_HND *hnd,
760 uint32 level,
761 PRINTER_INFO_CTR *ctr)
763 WERROR result;
765 /* getprinter call */
766 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx, hnd, level, ctr);
768 if (!W_ERROR_IS_OK(result)) {
769 printf("cannot get printer-info: %s\n", dos_errstr(result));
770 return False;
773 return True;
776 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
777 TALLOC_CTX *mem_ctx,
778 POLICY_HND *hnd,
779 uint32 level,
780 PRINTER_INFO_CTR *ctr)
782 WERROR result;
784 /* setprinter call */
785 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, hnd, level, ctr, 0);
787 if (!W_ERROR_IS_OK(result)) {
788 printf("cannot set printer-info: %s\n", dos_errstr(result));
789 return False;
792 return True;
796 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
797 TALLOC_CTX *mem_ctx,
798 POLICY_HND *hnd,
799 REGISTRY_VALUE *value)
801 WERROR result;
803 /* setprinterdata call */
804 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
806 if (!W_ERROR_IS_OK(result)) {
807 printf ("unable to set printerdata: %s\n", dos_errstr(result));
808 return False;
811 return True;
815 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
816 TALLOC_CTX *mem_ctx,
817 POLICY_HND *hnd,
818 const char *keyname,
819 uint16 **keylist)
821 WERROR result;
823 /* enumprinterkey call */
824 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
826 if (!W_ERROR_IS_OK(result)) {
827 printf("enumprinterkey failed: %s\n", dos_errstr(result));
828 return False;
831 return True;
834 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
835 TALLOC_CTX *mem_ctx,
836 uint32 offered,
837 POLICY_HND *hnd,
838 const char *keyname,
839 REGVAL_CTR *ctr)
841 WERROR result;
843 /* enumprinterdataex call */
844 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
846 if (!W_ERROR_IS_OK(result)) {
847 printf("enumprinterdataex failed: %s\n", dos_errstr(result));
848 return False;
851 return True;
855 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
856 TALLOC_CTX *mem_ctx,
857 POLICY_HND *hnd,
858 char *keyname,
859 REGISTRY_VALUE *value)
861 WERROR result;
863 /* setprinterdataex call */
864 result = rpccli_spoolss_setprinterdataex(pipe_hnd, mem_ctx, hnd,
865 keyname, value);
867 if (!W_ERROR_IS_OK(result)) {
868 printf("could not set printerdataex: %s\n", dos_errstr(result));
869 return False;
872 return True;
875 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
876 TALLOC_CTX *mem_ctx,
877 POLICY_HND *hnd,
878 int level,
879 uint32 *num_forms,
880 FORM_1 **forms)
883 WERROR result;
885 /* enumforms call */
886 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
888 if (!W_ERROR_IS_OK(result)) {
889 printf("could not enum forms: %s\n", dos_errstr(result));
890 return False;
893 return True;
896 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
897 TALLOC_CTX *mem_ctx,
898 uint32 level, const char *env,
899 uint32 *num_drivers,
900 PRINTER_DRIVER_CTR *ctr)
902 WERROR result;
904 /* enumprinterdrivers call */
905 result = rpccli_spoolss_enumprinterdrivers(
906 pipe_hnd, mem_ctx, level,
907 env, num_drivers, ctr);
909 if (!W_ERROR_IS_OK(result)) {
910 printf("cannot enum drivers: %s\n", dos_errstr(result));
911 return False;
914 return True;
917 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
918 TALLOC_CTX *mem_ctx,
919 POLICY_HND *hnd, uint32 level,
920 const char *env, int version,
921 PRINTER_DRIVER_CTR *ctr)
923 WERROR result;
925 /* getprinterdriver call */
926 result = rpccli_spoolss_getprinterdriver(
927 pipe_hnd, mem_ctx, hnd, level,
928 env, version, ctr);
930 if (!W_ERROR_IS_OK(result)) {
931 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
932 env, dos_errstr(result)));
933 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
934 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
935 printf("cannot get driver: %s\n", dos_errstr(result));
937 return False;
940 return True;
944 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
945 TALLOC_CTX *mem_ctx, uint32 level,
946 PRINTER_DRIVER_CTR *ctr)
948 WERROR result;
950 /* addprinterdriver call */
951 result = rpccli_spoolss_addprinterdriver(pipe_hnd, mem_ctx, level, ctr);
953 /* be more verbose */
954 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
955 printf("You are not allowed to add drivers\n");
956 return False;
958 if (!W_ERROR_IS_OK(result)) {
959 printf("cannot add driver: %s\n", dos_errstr(result));
960 return False;
963 return True;
967 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
968 * for a single printer or for all printers depending on argc/argv
971 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
972 TALLOC_CTX *mem_ctx,
973 int level,
974 int argc,
975 const char **argv,
976 uint32 *num_printers,
977 PRINTER_INFO_CTR *ctr)
980 POLICY_HND hnd;
982 /* no arguments given, enumerate all printers */
983 if (argc == 0) {
985 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
986 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
987 level, num_printers, ctr))
988 return False;
990 goto out;
994 /* argument given, get a single printer by name */
995 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
996 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd))
997 return False;
999 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, ctr)) {
1000 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1001 return False;
1004 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1006 *num_printers = 1;
1008 out:
1009 DEBUG(3,("got %d printers\n", *num_printers));
1011 return True;
1015 /**
1016 * List print-queues (including local printers that are not shared)
1018 * All parameters are provided by the run_rpc_command function, except for
1019 * argc, argv which are passed through.
1021 * @param domain_sid The domain sid aquired from the remote server
1022 * @param cli A cli_state connected to the server.
1023 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1024 * @param argc Standard main() style argc
1025 * @param argv Standard main() style argv. Initial components are already
1026 * stripped
1028 * @return Normal NTSTATUS return.
1031 NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid,
1032 const char *domain_name,
1033 struct cli_state *cli,
1034 struct rpc_pipe_client *pipe_hnd,
1035 TALLOC_CTX *mem_ctx,
1036 int argc,
1037 const char **argv)
1039 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1040 uint32 i, num_printers;
1041 uint32 level = 2;
1042 char *printername, *sharename;
1043 PRINTER_INFO_CTR ctr;
1045 printf("listing printers\n");
1047 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1048 return nt_status;
1050 for (i = 0; i < num_printers; i++) {
1051 /* do some initialization */
1052 rpcstr_pull_talloc(mem_ctx,
1053 &printername,
1054 ctr.printers_2[i].printername.buffer,
1056 STR_TERMINATE);
1057 rpcstr_pull_talloc(mem_ctx,
1058 &sharename,
1059 ctr.printers_2[i].sharename.buffer,
1061 STR_TERMINATE);
1063 if (printername && sharename) {
1064 d_printf("printer %d: %s, shared as: %s\n",
1065 i+1, printername, sharename);
1069 return NT_STATUS_OK;
1072 /**
1073 * List printer-drivers from a server
1075 * All parameters are provided by the run_rpc_command function, except for
1076 * argc, argv which are passed through.
1078 * @param domain_sid The domain sid aquired from the remote server
1079 * @param cli A cli_state connected to the server.
1080 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1081 * @param argc Standard main() style argc
1082 * @param argv Standard main() style argv. Initial components are already
1083 * stripped
1085 * @return Normal NTSTATUS return.
1088 NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid,
1089 const char *domain_name,
1090 struct cli_state *cli,
1091 struct rpc_pipe_client *pipe_hnd,
1092 TALLOC_CTX *mem_ctx,
1093 int argc,
1094 const char **argv)
1096 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1097 uint32 i;
1098 uint32 level = 3;
1099 PRINTER_DRIVER_CTR drv_ctr_enum;
1100 int d;
1102 ZERO_STRUCT(drv_ctr_enum);
1104 printf("listing printer-drivers\n");
1106 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1108 uint32 num_drivers;
1110 /* enum remote drivers */
1111 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1112 archi_table[i].long_archi,
1113 &num_drivers, &drv_ctr_enum)) {
1115 nt_status = NT_STATUS_UNSUCCESSFUL;
1116 goto done;
1119 if (num_drivers == 0) {
1120 d_printf ("no drivers found on server for architecture: [%s].\n",
1121 archi_table[i].long_archi);
1122 continue;
1125 d_printf("got %d printer-drivers for architecture: [%s]\n",
1126 num_drivers, archi_table[i].long_archi);
1129 /* do something for all drivers for architecture */
1130 for (d = 0; d < num_drivers; d++) {
1131 display_print_driver_3(&(drv_ctr_enum.info3[d]));
1135 nt_status = NT_STATUS_OK;
1137 done:
1138 return nt_status;
1142 /**
1143 * Publish print-queues with args-wrapper
1145 * @param cli A cli_state connected to the server.
1146 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1147 * @param argc Standard main() style argc
1148 * @param argv Standard main() style argv. Initial components are already
1149 * stripped
1150 * @param action
1152 * @return Normal NTSTATUS return.
1155 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1156 TALLOC_CTX *mem_ctx,
1157 int argc,
1158 const char **argv,
1159 uint32 action)
1161 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1162 uint32 i, num_printers;
1163 uint32 level = 7;
1164 char *printername, *sharename;
1165 PRINTER_INFO_CTR ctr, ctr_pub;
1166 POLICY_HND hnd;
1167 bool got_hnd = False;
1168 WERROR result;
1169 const char *action_str;
1171 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1172 return nt_status;
1174 for (i = 0; i < num_printers; i++) {
1175 /* do some initialization */
1176 rpcstr_pull_talloc(mem_ctx,
1177 &printername,
1178 ctr.printers_2[i].printername.buffer,
1180 STR_TERMINATE);
1181 rpcstr_pull_talloc(mem_ctx,
1182 &sharename,
1183 ctr.printers_2[i].sharename.buffer,
1185 STR_TERMINATE);
1186 if (!printername || !sharename) {
1187 goto done;
1190 /* open printer handle */
1191 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1192 PRINTER_ALL_ACCESS, pipe_hnd->cli->user_name, &hnd))
1193 goto done;
1195 got_hnd = True;
1197 /* check for existing dst printer */
1198 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1199 goto done;
1201 /* check action and set string */
1202 switch (action) {
1203 case SPOOL_DS_PUBLISH:
1204 action_str = "published";
1205 break;
1206 case SPOOL_DS_UPDATE:
1207 action_str = "updated";
1208 break;
1209 case SPOOL_DS_UNPUBLISH:
1210 action_str = "unpublished";
1211 break;
1212 default:
1213 action_str = "unknown action";
1214 printf("unkown action: %d\n", action);
1215 break;
1218 ctr_pub.printers_7->action = action;
1220 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub, 0);
1221 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1222 printf("cannot set printer-info: %s\n", dos_errstr(result));
1223 goto done;
1226 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1229 nt_status = NT_STATUS_OK;
1231 done:
1232 if (got_hnd)
1233 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1235 return nt_status;
1238 NTSTATUS rpc_printer_publish_publish_internals(const DOM_SID *domain_sid,
1239 const char *domain_name,
1240 struct cli_state *cli,
1241 struct rpc_pipe_client *pipe_hnd,
1242 TALLOC_CTX *mem_ctx,
1243 int argc,
1244 const char **argv)
1246 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_PUBLISH);
1249 NTSTATUS rpc_printer_publish_unpublish_internals(const DOM_SID *domain_sid,
1250 const char *domain_name,
1251 struct cli_state *cli,
1252 struct rpc_pipe_client *pipe_hnd,
1253 TALLOC_CTX *mem_ctx,
1254 int argc,
1255 const char **argv)
1257 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
1260 NTSTATUS rpc_printer_publish_update_internals(const DOM_SID *domain_sid,
1261 const char *domain_name,
1262 struct cli_state *cli,
1263 struct rpc_pipe_client *pipe_hnd,
1264 TALLOC_CTX *mem_ctx,
1265 int argc,
1266 const char **argv)
1268 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
1271 /**
1272 * List print-queues w.r.t. their publishing state
1274 * All parameters are provided by the run_rpc_command function, except for
1275 * argc, argv which are passed through.
1277 * @param domain_sid The domain sid aquired from the remote server
1278 * @param cli A cli_state connected to the server.
1279 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1280 * @param argc Standard main() style argc
1281 * @param argv Standard main() style argv. Initial components are already
1282 * stripped
1284 * @return Normal NTSTATUS return.
1287 NTSTATUS rpc_printer_publish_list_internals(const DOM_SID *domain_sid,
1288 const char *domain_name,
1289 struct cli_state *cli,
1290 struct rpc_pipe_client *pipe_hnd,
1291 TALLOC_CTX *mem_ctx,
1292 int argc,
1293 const char **argv)
1295 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1296 uint32 i, num_printers;
1297 uint32 level = 7;
1298 char *printername, *sharename;
1299 char *guid;
1300 PRINTER_INFO_CTR ctr, ctr_pub;
1301 POLICY_HND hnd;
1302 bool got_hnd = False;
1303 int state;
1305 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1306 return nt_status;
1308 for (i = 0; i < num_printers; i++) {
1309 ZERO_STRUCT(ctr_pub);
1311 /* do some initialization */
1312 rpcstr_pull_talloc(mem_ctx,
1313 &printername,
1314 ctr.printers_2[i].printername.buffer,
1316 STR_TERMINATE);
1317 rpcstr_pull_talloc(mem_ctx,
1318 &sharename,
1319 ctr.printers_2[i].sharename.buffer,
1321 STR_TERMINATE);
1322 if (!printername || !sharename) {
1323 goto done;
1326 /* open printer handle */
1327 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1328 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1329 goto done;
1331 got_hnd = True;
1333 /* check for existing dst printer */
1334 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1335 goto done;
1337 rpcstr_pull_talloc(mem_ctx,
1338 &guid,
1339 ctr_pub.printers_7->guid.buffer,
1341 STR_TERMINATE);
1342 if (!guid) {
1343 goto done;
1345 state = ctr_pub.printers_7->action;
1346 switch (state) {
1347 case SPOOL_DS_PUBLISH:
1348 printf("printer [%s] is published", sharename);
1349 if (opt_verbose)
1350 printf(", guid: %s", guid);
1351 printf("\n");
1352 break;
1353 case SPOOL_DS_UNPUBLISH:
1354 printf("printer [%s] is unpublished\n", sharename);
1355 break;
1356 case SPOOL_DS_UPDATE:
1357 printf("printer [%s] is currently updating\n", sharename);
1358 break;
1359 default:
1360 printf("unkown state: %d\n", state);
1361 break;
1365 nt_status = NT_STATUS_OK;
1367 done:
1368 if (got_hnd)
1369 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1371 return nt_status;
1374 /**
1375 * Migrate Printer-ACLs from a source server to the destination server
1377 * All parameters are provided by the run_rpc_command function, except for
1378 * argc, argv which are passed through.
1380 * @param domain_sid The domain sid aquired from the remote server
1381 * @param cli A cli_state connected to the server.
1382 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1383 * @param argc Standard main() style argc
1384 * @param argv Standard main() style argv. Initial components are already
1385 * stripped
1387 * @return Normal NTSTATUS return.
1390 NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid,
1391 const char *domain_name,
1392 struct cli_state *cli,
1393 struct rpc_pipe_client *pipe_hnd,
1394 TALLOC_CTX *mem_ctx,
1395 int argc,
1396 const char **argv)
1398 /* TODO: what now, info2 or info3 ?
1399 convince jerry that we should add clientside setacls level 3 at least
1401 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1402 uint32 i = 0;
1403 uint32 num_printers;
1404 uint32 level = 2;
1405 char *printername, *sharename;
1406 bool got_hnd_src = False;
1407 bool got_hnd_dst = False;
1408 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1409 POLICY_HND hnd_src, hnd_dst;
1410 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1411 struct cli_state *cli_dst = NULL;
1413 ZERO_STRUCT(ctr_src);
1415 DEBUG(3,("copying printer ACLs\n"));
1417 /* connect destination PI_SPOOLSS */
1418 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1419 if (!NT_STATUS_IS_OK(nt_status))
1420 return nt_status;
1423 /* enum source printers */
1424 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1425 nt_status = NT_STATUS_UNSUCCESSFUL;
1426 goto done;
1429 if (!num_printers) {
1430 printf ("no printers found on server.\n");
1431 nt_status = NT_STATUS_OK;
1432 goto done;
1435 /* do something for all printers */
1436 for (i = 0; i < num_printers; i++) {
1437 /* do some initialization */
1438 rpcstr_pull_talloc(mem_ctx,
1439 &printername,
1440 ctr_enum.printers_2[i].printername.buffer,
1442 STR_TERMINATE);
1443 rpcstr_pull_talloc(mem_ctx,
1444 &sharename,
1445 ctr_enum.printers_2[i].sharename.buffer,
1447 STR_TERMINATE);
1448 if (!printername || !sharename) {
1449 nt_status = NT_STATUS_UNSUCCESSFUL;
1450 goto done;
1453 /* we can reset NT_STATUS here because we do not
1454 get any real NT_STATUS-codes anymore from now on */
1455 nt_status = NT_STATUS_UNSUCCESSFUL;
1457 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1458 printername, sharename);
1460 /* according to msdn you have specify these access-rights
1461 to see the security descriptor
1462 - READ_CONTROL (DACL)
1463 - ACCESS_SYSTEM_SECURITY (SACL)
1466 /* open src printer handle */
1467 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1468 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1469 goto done;
1471 got_hnd_src = True;
1473 /* open dst printer handle */
1474 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1475 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1476 goto done;
1478 got_hnd_dst = True;
1480 /* check for existing dst printer */
1481 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1482 goto done;
1484 /* check for existing src printer */
1485 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &ctr_src))
1486 goto done;
1488 /* Copy Security Descriptor */
1490 /* copy secdesc (info level 2) */
1491 ctr_dst.printers_2->devmode = NULL;
1492 ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
1494 if (opt_verbose)
1495 display_sec_desc(ctr_dst.printers_2->secdesc);
1497 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &ctr_dst))
1498 goto done;
1500 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1503 /* close printer handles here */
1504 if (got_hnd_src) {
1505 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1506 got_hnd_src = False;
1509 if (got_hnd_dst) {
1510 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1511 got_hnd_dst = False;
1516 nt_status = NT_STATUS_OK;
1518 done:
1520 if (got_hnd_src) {
1521 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1524 if (got_hnd_dst) {
1525 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1528 if (cli_dst) {
1529 cli_shutdown(cli_dst);
1531 return nt_status;
1534 /**
1535 * Migrate printer-forms from a src server to the dst server
1537 * All parameters are provided by the run_rpc_command function, except for
1538 * argc, argv which are passed through.
1540 * @param domain_sid The domain sid aquired from the remote server
1541 * @param cli A cli_state connected to the server.
1542 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1543 * @param argc Standard main() style argc
1544 * @param argv Standard main() style argv. Initial components are already
1545 * stripped
1547 * @return Normal NTSTATUS return.
1550 NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid,
1551 const char *domain_name,
1552 struct cli_state *cli,
1553 struct rpc_pipe_client *pipe_hnd,
1554 TALLOC_CTX *mem_ctx,
1555 int argc,
1556 const char **argv)
1558 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1559 WERROR result;
1560 uint32 i, f;
1561 uint32 num_printers;
1562 uint32 level = 1;
1563 char *printername, *sharename;
1564 bool got_hnd_src = False;
1565 bool got_hnd_dst = False;
1566 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1567 POLICY_HND hnd_src, hnd_dst;
1568 PRINTER_INFO_CTR ctr_enum, ctr_dst;
1569 uint32 num_forms;
1570 FORM_1 *forms;
1571 struct cli_state *cli_dst = NULL;
1573 ZERO_STRUCT(ctr_enum);
1575 DEBUG(3,("copying forms\n"));
1577 /* connect destination PI_SPOOLSS */
1578 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1579 if (!NT_STATUS_IS_OK(nt_status))
1580 return nt_status;
1582 /* enum src printers */
1583 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1584 nt_status = NT_STATUS_UNSUCCESSFUL;
1585 goto done;
1588 if (!num_printers) {
1589 printf ("no printers found on server.\n");
1590 nt_status = NT_STATUS_OK;
1591 goto done;
1594 /* do something for all printers */
1595 for (i = 0; i < num_printers; i++) {
1596 /* do some initialization */
1597 rpcstr_pull_talloc(mem_ctx,
1598 &printername,
1599 ctr_enum.printers_2[i].printername.buffer,
1601 STR_TERMINATE);
1602 rpcstr_pull_talloc(mem_ctx,
1603 &sharename,
1604 ctr_enum.printers_2[i].sharename.buffer,
1606 STR_TERMINATE);
1607 if (!printername || !sharename) {
1608 nt_status = NT_STATUS_UNSUCCESSFUL;
1609 goto done;
1611 /* we can reset NT_STATUS here because we do not
1612 get any real NT_STATUS-codes anymore from now on */
1613 nt_status = NT_STATUS_UNSUCCESSFUL;
1615 d_printf("migrating printer forms for: [%s] / [%s]\n",
1616 printername, sharename);
1619 /* open src printer handle */
1620 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1621 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1622 goto done;
1624 got_hnd_src = True;
1627 /* open dst printer handle */
1628 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1629 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1630 goto done;
1632 got_hnd_dst = True;
1635 /* check for existing dst printer */
1636 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1637 goto done;
1639 /* finally migrate forms */
1640 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1641 goto done;
1643 DEBUG(1,("got %d forms for printer\n", num_forms));
1646 for (f = 0; f < num_forms; f++) {
1648 FORM form;
1649 fstring form_name;
1651 /* only migrate FORM_PRINTER types, according to jerry
1652 FORM_BUILTIN-types are hard-coded in samba */
1653 if (forms[f].flag != FORM_PRINTER)
1654 continue;
1656 if (forms[f].name.buffer)
1657 rpcstr_pull(form_name, forms[f].name.buffer,
1658 sizeof(form_name), -1, STR_TERMINATE);
1660 if (opt_verbose)
1661 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1662 f, form_name, forms[f].flag);
1664 /* is there a more elegant way to do that ? */
1665 form.flags = FORM_PRINTER;
1666 form.size_x = forms[f].width;
1667 form.size_y = forms[f].length;
1668 form.left = forms[f].left;
1669 form.top = forms[f].top;
1670 form.right = forms[f].right;
1671 form.bottom = forms[f].bottom;
1673 init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
1675 /* FIXME: there might be something wrong with samba's
1676 builtin-forms */
1677 result = rpccli_spoolss_addform(pipe_hnd_dst, mem_ctx,
1678 &hnd_dst, 1, &form);
1679 if (!W_ERROR_IS_OK(result)) {
1680 d_printf("\tAddForm form %d: [%s] refused.\n",
1681 f, form_name);
1682 continue;
1685 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1689 /* close printer handles here */
1690 if (got_hnd_src) {
1691 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1692 got_hnd_src = False;
1695 if (got_hnd_dst) {
1696 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1697 got_hnd_dst = False;
1701 nt_status = NT_STATUS_OK;
1703 done:
1705 if (got_hnd_src)
1706 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1708 if (got_hnd_dst)
1709 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1711 if (cli_dst) {
1712 cli_shutdown(cli_dst);
1714 return nt_status;
1717 /**
1718 * Migrate printer-drivers from a src server to the dst server
1720 * All parameters are provided by the run_rpc_command function, except for
1721 * argc, argv which are passed through.
1723 * @param domain_sid The domain sid aquired from the remote server
1724 * @param cli A cli_state connected to the server.
1725 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1726 * @param argc Standard main() style argc
1727 * @param argv Standard main() style argv. Initial components are already
1728 * stripped
1730 * @return Normal NTSTATUS return.
1733 NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid,
1734 const char *domain_name,
1735 struct cli_state *cli,
1736 struct rpc_pipe_client *pipe_hnd,
1737 TALLOC_CTX *mem_ctx,
1738 int argc,
1739 const char **argv)
1741 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1742 uint32 i, p;
1743 uint32 num_printers;
1744 uint32 level = 3;
1745 char *printername, *sharename;
1746 bool got_hnd_src = False;
1747 bool got_hnd_dst = False;
1748 bool got_src_driver_share = False;
1749 bool got_dst_driver_share = False;
1750 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1751 POLICY_HND hnd_src, hnd_dst;
1752 PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
1753 PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1754 struct cli_state *cli_dst = NULL;
1755 struct cli_state *cli_share_src = NULL;
1756 struct cli_state *cli_share_dst = NULL;
1757 fstring drivername = "";
1759 ZERO_STRUCT(drv_ctr_src);
1760 ZERO_STRUCT(drv_ctr_dst);
1761 ZERO_STRUCT(info_ctr_enum);
1762 ZERO_STRUCT(info_ctr_dst);
1764 DEBUG(3,("copying printer-drivers\n"));
1766 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1767 if (!NT_STATUS_IS_OK(nt_status))
1768 return nt_status;
1770 /* open print$-share on the src server */
1771 nt_status = connect_to_service(&cli_share_src, &cli->dest_ss,
1772 cli->desthost, "print$", "A:");
1773 if (!NT_STATUS_IS_OK(nt_status))
1774 goto done;
1776 got_src_driver_share = True;
1779 /* open print$-share on the dst server */
1780 nt_status = connect_to_service(&cli_share_dst, &cli_dst->dest_ss,
1781 cli_dst->desthost, "print$", "A:");
1782 if (!NT_STATUS_IS_OK(nt_status))
1783 return nt_status;
1785 got_dst_driver_share = True;
1788 /* enum src printers */
1789 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1790 nt_status = NT_STATUS_UNSUCCESSFUL;
1791 goto done;
1794 if (num_printers == 0) {
1795 printf ("no printers found on server.\n");
1796 nt_status = NT_STATUS_OK;
1797 goto done;
1801 /* do something for all printers */
1802 for (p = 0; p < num_printers; p++) {
1803 /* do some initialization */
1804 rpcstr_pull_talloc(mem_ctx,
1805 &printername,
1806 info_ctr_enum.printers_2[p].printername.buffer,
1808 STR_TERMINATE);
1809 rpcstr_pull_talloc(mem_ctx,
1810 &sharename,
1811 info_ctr_enum.printers_2[p].sharename.buffer,
1813 STR_TERMINATE);
1814 if (!printername || !sharename) {
1815 nt_status = NT_STATUS_UNSUCCESSFUL;
1816 goto done;
1819 /* we can reset NT_STATUS here because we do not
1820 get any real NT_STATUS-codes anymore from now on */
1821 nt_status = NT_STATUS_UNSUCCESSFUL;
1823 d_printf("migrating printer driver for: [%s] / [%s]\n",
1824 printername, sharename);
1826 /* open dst printer handle */
1827 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1828 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1829 goto done;
1831 got_hnd_dst = True;
1833 /* check for existing dst printer */
1834 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst))
1835 goto done;
1838 /* open src printer handle */
1839 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1840 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd_src))
1841 goto done;
1843 got_hnd_src = True;
1846 /* in a first step call getdriver for each shared printer (per arch)
1847 to get a list of all files that have to be copied */
1849 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1851 /* getdriver src */
1852 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1853 level, archi_table[i].long_archi,
1854 archi_table[i].version, &drv_ctr_src))
1855 continue;
1857 rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer,
1858 sizeof(drivername), -1, STR_TERMINATE);
1860 if (opt_verbose)
1861 display_print_driver_3(drv_ctr_src.info3);
1864 /* check arch dir */
1865 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1866 if (!NT_STATUS_IS_OK(nt_status))
1867 goto done;
1870 /* copy driver-files */
1871 nt_status = copy_print_driver_3(mem_ctx, cli_share_src, cli_share_dst,
1872 archi_table[i].short_archi,
1873 drv_ctr_src.info3);
1874 if (!NT_STATUS_IS_OK(nt_status))
1875 goto done;
1878 /* adddriver dst */
1879 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_ctr_src)) {
1880 nt_status = NT_STATUS_UNSUCCESSFUL;
1881 goto done;
1884 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1885 drivername, printername));
1889 if (strlen(drivername) == 0) {
1890 DEBUGADD(1,("Did not get driver for printer %s\n",
1891 printername));
1892 goto done;
1895 /* setdriver dst */
1896 init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
1898 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) {
1899 nt_status = NT_STATUS_UNSUCCESSFUL;
1900 goto done;
1903 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1904 drivername, printername));
1906 /* close dst */
1907 if (got_hnd_dst) {
1908 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1909 got_hnd_dst = False;
1912 /* close src */
1913 if (got_hnd_src) {
1914 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1915 got_hnd_src = False;
1919 nt_status = NT_STATUS_OK;
1921 done:
1923 if (got_hnd_src)
1924 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1926 if (got_hnd_dst)
1927 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1929 if (cli_dst) {
1930 cli_shutdown(cli_dst);
1933 if (got_src_driver_share)
1934 cli_shutdown(cli_share_src);
1936 if (got_dst_driver_share)
1937 cli_shutdown(cli_share_dst);
1939 return nt_status;
1943 /**
1944 * Migrate printer-queues from a src to the dst server
1945 * (requires a working "addprinter command" to be installed for the local smbd)
1947 * All parameters are provided by the run_rpc_command function, except for
1948 * argc, argv which are passed through.
1950 * @param domain_sid The domain sid aquired from the remote server
1951 * @param cli A cli_state connected to the server.
1952 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1953 * @param argc Standard main() style argc
1954 * @param argv Standard main() style argv. Initial components are already
1955 * stripped
1957 * @return Normal NTSTATUS return.
1960 NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid,
1961 const char *domain_name,
1962 struct cli_state *cli,
1963 struct rpc_pipe_client *pipe_hnd,
1964 TALLOC_CTX *mem_ctx,
1965 int argc,
1966 const char **argv)
1968 WERROR result;
1969 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1970 uint32 i = 0, num_printers;
1971 uint32 level = 2;
1972 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1973 struct cli_state *cli_dst = NULL;
1974 POLICY_HND hnd_dst, hnd_src;
1975 char *printername, *sharename;
1976 bool got_hnd_src = False;
1977 bool got_hnd_dst = False;
1978 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1980 DEBUG(3,("copying printers\n"));
1982 /* connect destination PI_SPOOLSS */
1983 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1984 if (!NT_STATUS_IS_OK(nt_status))
1985 return nt_status;
1987 /* enum printers */
1988 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1989 nt_status = NT_STATUS_UNSUCCESSFUL;
1990 goto done;
1993 if (!num_printers) {
1994 printf ("no printers found on server.\n");
1995 nt_status = NT_STATUS_OK;
1996 goto done;
1999 /* do something for all printers */
2000 for (i = 0; i < num_printers; i++) {
2001 /* do some initialization */
2002 rpcstr_pull_talloc(mem_ctx,
2003 &printername,
2004 ctr_enum.printers_2[i].printername.buffer,
2006 STR_TERMINATE);
2007 rpcstr_pull_talloc(mem_ctx,
2008 &sharename,
2009 ctr_enum.printers_2[i].sharename.buffer,
2011 STR_TERMINATE);
2012 if (!printername || !sharename) {
2013 nt_status = NT_STATUS_UNSUCCESSFUL;
2014 goto done;
2016 /* we can reset NT_STATUS here because we do not
2017 get any real NT_STATUS-codes anymore from now on */
2018 nt_status = NT_STATUS_UNSUCCESSFUL;
2020 d_printf("migrating printer queue for: [%s] / [%s]\n",
2021 printername, sharename);
2023 /* open dst printer handle */
2024 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2025 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2027 DEBUG(1,("could not open printer: %s\n", sharename));
2028 } else {
2029 got_hnd_dst = True;
2032 /* check for existing dst printer */
2033 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) {
2034 printf ("could not get printer, creating printer.\n");
2035 } else {
2036 DEBUG(1,("printer already exists: %s\n", sharename));
2037 /* close printer handle here - dst only, not got src yet. */
2038 if (got_hnd_dst) {
2039 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2040 got_hnd_dst = False;
2042 continue;
2045 /* now get again src printer ctr via getprinter,
2046 we first need a handle for that */
2048 /* open src printer handle */
2049 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2050 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2051 goto done;
2053 got_hnd_src = True;
2055 /* getprinter on the src server */
2056 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &ctr_src))
2057 goto done;
2059 /* copy each src printer to a dst printer 1:1,
2060 maybe some values have to be changed though */
2061 d_printf("creating printer: %s\n", printername);
2062 result = rpccli_spoolss_addprinterex (pipe_hnd_dst, mem_ctx, level, &ctr_src);
2064 if (W_ERROR_IS_OK(result))
2065 d_printf ("printer [%s] successfully added.\n", printername);
2066 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2067 d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2068 else {
2069 d_fprintf (stderr, "could not create printer [%s]\n", printername);
2070 goto done;
2073 /* close printer handles here */
2074 if (got_hnd_src) {
2075 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2076 got_hnd_src = False;
2079 if (got_hnd_dst) {
2080 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2081 got_hnd_dst = False;
2085 nt_status = NT_STATUS_OK;
2087 done:
2088 if (got_hnd_src)
2089 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2091 if (got_hnd_dst)
2092 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2094 if (cli_dst) {
2095 cli_shutdown(cli_dst);
2097 return nt_status;
2100 /**
2101 * Migrate Printer-Settings from a src server to the dst server
2102 * (for this to work, printers and drivers already have to be migrated earlier)
2104 * All parameters are provided by the run_rpc_command function, except for
2105 * argc, argv which are passed through.
2107 * @param domain_sid The domain sid aquired from the remote server
2108 * @param cli A cli_state connected to the server.
2109 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2110 * @param argc Standard main() style argc
2111 * @param argv Standard main() style argv. Initial components are already
2112 * stripped
2114 * @return Normal NTSTATUS return.
2117 NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid,
2118 const char *domain_name,
2119 struct cli_state *cli,
2120 struct rpc_pipe_client *pipe_hnd,
2121 TALLOC_CTX *mem_ctx,
2122 int argc,
2123 const char **argv)
2126 /* FIXME: Here the nightmare begins */
2128 WERROR result;
2129 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2130 uint32 i = 0, p = 0, j = 0;
2131 uint32 num_printers, val_needed, data_needed;
2132 uint32 level = 2;
2133 char *printername, *sharename;
2134 bool got_hnd_src = False;
2135 bool got_hnd_dst = False;
2136 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2137 POLICY_HND hnd_src, hnd_dst;
2138 PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
2139 REGVAL_CTR *reg_ctr;
2140 struct cli_state *cli_dst = NULL;
2141 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2142 const char *longname;
2144 uint16 *keylist = NULL, *curkey;
2146 ZERO_STRUCT(ctr_enum);
2148 DEBUG(3,("copying printer settings\n"));
2150 /* connect destination PI_SPOOLSS */
2151 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
2152 if (!NT_STATUS_IS_OK(nt_status))
2153 return nt_status;
2155 /* enum src printers */
2156 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2157 nt_status = NT_STATUS_UNSUCCESSFUL;
2158 goto done;
2161 if (!num_printers) {
2162 printf ("no printers found on server.\n");
2163 nt_status = NT_STATUS_OK;
2164 goto done;
2168 /* needed for dns-strings in regkeys */
2169 longname = get_mydnsfullname();
2170 if (!longname) {
2171 nt_status = NT_STATUS_UNSUCCESSFUL;
2172 goto done;
2175 /* do something for all printers */
2176 for (i = 0; i < num_printers; i++) {
2177 /* do some initialization */
2178 rpcstr_pull_talloc(mem_ctx,
2179 &printername,
2180 ctr_enum.printers_2[i].printername.buffer,
2182 STR_TERMINATE);
2183 rpcstr_pull_talloc(mem_ctx,
2184 &sharename,
2185 ctr_enum.printers_2[i].sharename.buffer,
2187 STR_TERMINATE);
2188 if (!printername || !sharename) {
2189 nt_status = NT_STATUS_UNSUCCESSFUL;
2190 goto done;
2192 /* we can reset NT_STATUS here because we do not
2193 get any real NT_STATUS-codes anymore from now on */
2194 nt_status = NT_STATUS_UNSUCCESSFUL;
2196 d_printf("migrating printer settings for: [%s] / [%s]\n",
2197 printername, sharename);
2200 /* open src printer handle */
2201 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2202 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2203 goto done;
2205 got_hnd_src = True;
2208 /* open dst printer handle */
2209 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2210 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2211 goto done;
2213 got_hnd_dst = True;
2216 /* check for existing dst printer */
2217 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2218 level, &ctr_dst))
2219 goto done;
2222 /* STEP 1: COPY DEVICE-MODE and other
2223 PRINTER_INFO_2-attributes
2226 ctr_dst.printers_2 = &ctr_enum.printers_2[i];
2228 /* why is the port always disconnected when the printer
2229 is correctly installed (incl. driver ???) */
2230 init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
2232 /* check if printer is published */
2233 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2235 /* check for existing dst printer */
2236 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
2237 goto done;
2239 ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
2241 /* ignore False from setprinter due to WERR_IO_PENDING */
2242 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
2244 DEBUG(3,("republished printer\n"));
2247 if (ctr_enum.printers_2[i].devmode != NULL) {
2249 /* copy devmode (info level 2) */
2250 ctr_dst.printers_2->devmode = (DEVICEMODE *)
2251 TALLOC_MEMDUP(mem_ctx,
2252 ctr_enum.printers_2[i].devmode,
2253 sizeof(DEVICEMODE));
2255 /* do not copy security descriptor (we have another
2256 * command for that) */
2257 ctr_dst.printers_2->secdesc = NULL;
2259 #if 0
2260 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2261 printername) < 0) {
2262 nt_status = NT_STATUS_NO_MEMORY;
2263 goto done;
2266 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2267 devicename);
2268 #endif
2269 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2270 level, &ctr_dst))
2271 goto done;
2273 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2276 /* STEP 2: COPY REGISTRY VALUES */
2278 /* please keep in mind that samba parse_spools gives horribly
2279 crippled results when used to rpccli_spoolss_enumprinterdataex
2280 a win2k3-server. (Bugzilla #1851)
2281 FIXME: IIRC I've seen it too on a win2k-server
2284 /* enumerate data on src handle */
2285 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2286 &val_needed, &data_needed, NULL);
2288 /* loop for all printerdata of "PrinterDriverData" */
2289 while (W_ERROR_IS_OK(result)) {
2291 REGISTRY_VALUE value;
2293 result = rpccli_spoolss_enumprinterdata(
2294 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2295 data_needed, 0, 0, &value);
2297 /* loop for all reg_keys */
2298 if (W_ERROR_IS_OK(result)) {
2300 /* display_value */
2301 if (opt_verbose)
2302 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2304 /* set_value */
2305 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2306 &hnd_dst, &value))
2307 goto done;
2309 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2310 value.valuename));
2314 /* STEP 3: COPY SUBKEY VALUES */
2316 /* here we need to enum all printer_keys and then work
2317 on the result with enum_printer_key_ex. nt4 does not
2318 respond to enumprinterkey, win2k does, so continue
2319 in case of an error */
2321 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2322 printf("got no key-data\n");
2323 continue;
2327 /* work on a list of printer keys
2328 each key has to be enumerated to get all required
2329 information. information is then set via setprinterdataex-calls */
2331 if (keylist == NULL)
2332 continue;
2334 curkey = keylist;
2335 while (*curkey != 0) {
2336 char *subkey;
2337 rpcstr_pull_talloc(mem_ctx,
2338 &subkey,
2339 curkey,
2341 STR_TERMINATE);
2342 if (!subkey) {
2343 return NT_STATUS_NO_MEMORY;
2346 curkey += strlen(subkey) + 1;
2348 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2349 return NT_STATUS_NO_MEMORY;
2351 /* enumerate all src subkeys */
2352 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2353 &hnd_src, subkey,
2354 reg_ctr))
2355 goto done;
2357 for (j=0; j < reg_ctr->num_values; j++) {
2359 REGISTRY_VALUE value;
2360 UNISTR2 data;
2362 /* although samba replies with sane data in most cases we
2363 should try to avoid writing wrong registry data */
2365 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2366 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2367 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2368 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2369 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2371 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2373 /* although windows uses a multi-sz, we use a sz */
2374 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2375 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2378 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2380 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2381 nt_status = NT_STATUS_NO_MEMORY;
2382 goto done;
2384 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2385 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2388 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2390 continue;
2392 #if 0
2393 /* FIXME: should we really do that ??? */
2394 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2395 nt_status = NT_STATUS_NO_MEMORY;
2396 goto done;
2398 init_unistr2(&data, url, UNI_STR_TERMINATE);
2399 fstrcpy(value.valuename, SPOOL_REG_URL);
2400 #endif
2403 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2405 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2406 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2409 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2411 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2412 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2415 value.type = REG_SZ;
2416 value.size = data.uni_str_len * 2;
2417 if (value.size) {
2418 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2419 } else {
2420 value.data_p = NULL;
2423 if (opt_verbose)
2424 display_reg_value(subkey, value);
2426 /* here we have to set all subkeys on the dst server */
2427 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2428 subkey, &value))
2429 goto done;
2431 } else {
2433 if (opt_verbose)
2434 display_reg_value(subkey, *(reg_ctr->values[j]));
2436 /* here we have to set all subkeys on the dst server */
2437 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2438 subkey, reg_ctr->values[j]))
2439 goto done;
2443 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2444 subkey, reg_ctr->values[j]->valuename));
2448 TALLOC_FREE( reg_ctr );
2451 safe_free(keylist);
2453 /* close printer handles here */
2454 if (got_hnd_src) {
2455 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2456 got_hnd_src = False;
2459 if (got_hnd_dst) {
2460 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2461 got_hnd_dst = False;
2466 nt_status = NT_STATUS_OK;
2468 done:
2469 SAFE_FREE(devicename);
2470 SAFE_FREE(url);
2471 SAFE_FREE(unc_name);
2473 if (got_hnd_src)
2474 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2476 if (got_hnd_dst)
2477 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2479 if (cli_dst) {
2480 cli_shutdown(cli_dst);
2482 return nt_status;