r12912: patch from Tony Mountifield <tony@softins.co.uk> for BUG 3327 (fix bad access...
[Samba/gbeck.git] / source3 / utils / net_rpc_printer.c
blobd8f3099dec86a53ccd6f129c1ea56da330392d23
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 #include "includes.h"
21 #include "utils/net.h"
23 struct table_node {
24 const char *long_archi;
25 const char *short_archi;
26 int version;
30 /* support itanium as well */
31 static const struct table_node archi_table[]= {
33 {"Windows 4.0", "WIN40", 0 },
34 {"Windows NT x86", "W32X86", 2 },
35 {"Windows NT x86", "W32X86", 3 },
36 {"Windows NT R4000", "W32MIPS", 2 },
37 {"Windows NT Alpha_AXP", "W32ALPHA", 2 },
38 {"Windows NT PowerPC", "W32PPC", 2 },
39 {"Windows IA64", "IA64", 3 },
40 {"Windows x64", "x64", 3 },
41 {NULL, "", -1 }
45 /**
46 * The display-functions for Security-Descriptors were taken from rpcclient
48 * They reside here for debugging purpose and should
49 * possibly be removed later on
51 **/
53 /****************************************************************************
54 Convert a security permissions into a string.
55 ****************************************************************************/
57 char *get_sec_mask_str(uint32 type)
59 static fstring typestr="";
61 typestr[0] = 0;
63 if (type & GENERIC_ALL_ACCESS)
64 fstrcat(typestr, "Generic all access ");
65 if (type & GENERIC_EXECUTE_ACCESS)
66 fstrcat(typestr, "Generic execute access ");
67 if (type & GENERIC_WRITE_ACCESS)
68 fstrcat(typestr, "Generic write access ");
69 if (type & GENERIC_READ_ACCESS)
70 fstrcat(typestr, "Generic read access ");
71 if (type & MAXIMUM_ALLOWED_ACCESS)
72 fstrcat(typestr, "MAXIMUM_ALLOWED_ACCESS ");
73 if (type & SYSTEM_SECURITY_ACCESS)
74 fstrcat(typestr, "SYSTEM_SECURITY_ACCESS ");
75 if (type & SYNCHRONIZE_ACCESS)
76 fstrcat(typestr, "SYNCHRONIZE_ACCESS ");
77 if (type & WRITE_OWNER_ACCESS)
78 fstrcat(typestr, "WRITE_OWNER_ACCESS ");
79 if (type & WRITE_DAC_ACCESS)
80 fstrcat(typestr, "WRITE_DAC_ACCESS ");
81 if (type & READ_CONTROL_ACCESS)
82 fstrcat(typestr, "READ_CONTROL_ACCESS ");
83 if (type & DELETE_ACCESS)
84 fstrcat(typestr, "DELETE_ACCESS ");
86 printf("\t\tSpecific bits: 0x%lx\n", (unsigned long)type&SPECIFIC_RIGHTS_MASK);
88 return typestr;
91 /****************************************************************************
92 Display sec_ace structure.
93 ****************************************************************************/
95 void display_sec_ace(SEC_ACE *ace)
97 fstring sid_str;
99 printf("\tACE\n\t\ttype: ");
100 switch (ace->type) {
101 case SEC_ACE_TYPE_ACCESS_ALLOWED:
102 printf("ACCESS ALLOWED");
103 break;
104 case SEC_ACE_TYPE_ACCESS_DENIED:
105 printf("ACCESS DENIED");
106 break;
107 case SEC_ACE_TYPE_SYSTEM_AUDIT:
108 printf("SYSTEM AUDIT");
109 break;
110 case SEC_ACE_TYPE_SYSTEM_ALARM:
111 printf("SYSTEM ALARM");
112 break;
113 default:
114 printf("????");
115 break;
117 printf(" (%d) flags: %d\n", ace->type, ace->flags);
118 printf("\t\tPermissions: 0x%x: %s\n", ace->info.mask, get_sec_mask_str(ace->info.mask));
120 sid_to_string(sid_str, &ace->trustee);
121 printf("\t\tSID: %s\n\n", sid_str);
124 /****************************************************************************
125 Display sec_acl structure.
126 ****************************************************************************/
128 void display_sec_acl(SEC_ACL *sec_acl)
130 int i;
132 printf("\tACL\tNum ACEs:\t%d\trevision:\t%x\n",
133 sec_acl->num_aces, sec_acl->revision);
134 printf("\t---\n");
136 if (sec_acl->size != 0 && sec_acl->num_aces != 0)
137 for (i = 0; i < sec_acl->num_aces; i++)
138 display_sec_ace(&sec_acl->ace[i]);
142 /****************************************************************************
143 Display sec_desc structure.
144 ****************************************************************************/
146 void display_sec_desc(SEC_DESC *sec)
148 fstring sid_str;
150 if (sec == NULL)
151 return;
153 if (sec->sacl) {
154 printf("SACL\n");
155 display_sec_acl(sec->sacl);
158 if (sec->dacl) {
159 printf("DACL\n");
160 display_sec_acl(sec->dacl);
163 if (sec->owner_sid) {
164 sid_to_string(sid_str, sec->owner_sid);
165 printf("\tOwner SID:\t%s\n", sid_str);
168 if (sec->grp_sid) {
169 sid_to_string(sid_str, sec->grp_sid);
170 printf("\tParent SID:\t%s\n", sid_str);
176 * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
177 * It is here for debugging purpose and should be removed later on.
180 /****************************************************************************
181 Printer info level 3 display function.
182 ****************************************************************************/
184 static void display_print_driver_3(DRIVER_INFO_3 *i1)
186 fstring name = "";
187 fstring architecture = "";
188 fstring driverpath = "";
189 fstring datafile = "";
190 fstring configfile = "";
191 fstring helpfile = "";
192 fstring dependentfiles = "";
193 fstring monitorname = "";
194 fstring defaultdatatype = "";
196 int length=0;
197 BOOL valid = True;
199 if (i1 == NULL)
200 return;
202 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
203 rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), -1, STR_TERMINATE);
204 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
205 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
206 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
207 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
208 rpcstr_pull(monitorname, i1->monitorname.buffer, sizeof(monitorname), -1, STR_TERMINATE);
209 rpcstr_pull(defaultdatatype, i1->defaultdatatype.buffer, sizeof(defaultdatatype), -1, STR_TERMINATE);
211 d_printf ("Printer Driver Info 3:\n");
212 d_printf ("\tVersion: [%x]\n", i1->version);
213 d_printf ("\tDriver Name: [%s]\n",name);
214 d_printf ("\tArchitecture: [%s]\n", architecture);
215 d_printf ("\tDriver Path: [%s]\n", driverpath);
216 d_printf ("\tDatafile: [%s]\n", datafile);
217 d_printf ("\tConfigfile: [%s]\n", configfile);
218 d_printf ("\tHelpfile: [%s]\n\n", helpfile);
220 while (valid) {
221 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
223 length+=strlen(dependentfiles)+1;
225 if (strlen(dependentfiles) > 0) {
226 d_printf ("\tDependentfiles: [%s]\n", dependentfiles);
227 } else {
228 valid = False;
232 printf ("\n");
234 d_printf ("\tMonitorname: [%s]\n", monitorname);
235 d_printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype);
237 return;
240 static void display_reg_value(const char *subkey, REGISTRY_VALUE value)
242 pstring text;
244 switch(value.type) {
245 case REG_DWORD:
246 d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename,
247 *((uint32 *) value.data_p));
248 break;
250 case REG_SZ:
251 rpcstr_pull(text, value.data_p, sizeof(text), value.size,
252 STR_TERMINATE);
253 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
254 break;
256 case REG_BINARY:
257 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
258 subkey, value.valuename);
259 break;
261 case REG_MULTI_SZ: {
262 uint16 *curstr = (uint16 *) value.data_p;
263 uint8 *start = value.data_p;
264 d_printf("\t[%s:%s]: REG_MULTI_SZ:\n", subkey, value.valuename);
265 while ((*curstr != 0) &&
266 ((uint8 *) curstr < start + value.size)) {
267 rpcstr_pull(text, curstr, sizeof(text), -1,
268 STR_TERMINATE);
269 d_printf("%s\n", text);
270 curstr += strlen(text) + 1;
273 break;
275 default:
276 d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
282 * Copies ACLs, DOS-attributes and timestamps from one
283 * file or directory from one connected share to another connected share
285 * @param mem_ctx A talloc-context
286 * @param cli_share_src A connected cli_state
287 * @param cli_share_dst A connected cli_state
288 * @param src_file The source file-name
289 * @param dst_file The destination file-name
290 * @param copy_acls Whether to copy acls
291 * @param copy_attrs Whether to copy DOS attributes
292 * @param copy_timestamps Whether to preserve timestamps
293 * @param is_file Whether this file is a file or a dir
295 * @return Normal NTSTATUS return.
296 **/
298 NTSTATUS net_copy_fileattr(TALLOC_CTX *mem_ctx,
299 struct cli_state *cli_share_src,
300 struct cli_state *cli_share_dst,
301 const char *src_name, const char *dst_name,
302 BOOL copy_acls, BOOL copy_attrs,
303 BOOL copy_timestamps, BOOL is_file)
305 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
306 int fnum_src = 0;
307 int fnum_dst = 0;
308 SEC_DESC *sd = NULL;
309 uint16 attr;
310 time_t f_atime, f_ctime, f_mtime;
313 if (!copy_timestamps && !copy_acls && !copy_attrs)
314 return NT_STATUS_OK;
316 /* open file/dir on the originating server */
318 DEBUGADD(3,("opening %s %s on originating server\n",
319 is_file?"file":"dir", src_name));
321 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
322 if (fnum_src == -1) {
323 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
324 is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
325 nt_status = cli_nt_error(cli_share_src);
326 goto out;
330 if (copy_acls) {
332 /* get the security descriptor */
333 sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
334 if (!sd) {
335 DEBUG(0,("failed to get security descriptor: %s\n",
336 cli_errstr(cli_share_src)));
337 nt_status = cli_nt_error(cli_share_src);
338 goto out;
341 if (opt_verbose && DEBUGLEVEL >= 3)
342 display_sec_desc(sd);
346 if (copy_attrs || copy_timestamps) {
348 /* get file attributes */
349 if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
350 &f_ctime, &f_atime, &f_mtime)) {
351 DEBUG(0,("failed to get file-attrs: %s\n",
352 cli_errstr(cli_share_src)));
353 nt_status = cli_nt_error(cli_share_src);
354 goto out;
359 /* open the file/dir on the destination server */
361 fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
362 if (fnum_dst == -1) {
363 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
364 is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
365 nt_status = cli_nt_error(cli_share_dst);
366 goto out;
369 if (copy_timestamps) {
371 /* set timestamps */
372 if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
373 DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
374 cli_errstr(cli_share_dst)));
375 nt_status = cli_nt_error(cli_share_dst);
376 goto out;
380 if (copy_acls) {
382 /* set acls */
383 if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
384 DEBUG(0,("could not set secdesc on %s: %s\n",
385 dst_name, cli_errstr(cli_share_dst)));
386 nt_status = cli_nt_error(cli_share_dst);
387 goto out;
391 if (copy_attrs) {
393 /* set attrs */
394 if (!cli_setatr(cli_share_dst, dst_name, attr, 0)) {
395 DEBUG(0,("failed to set file-attrs: %s\n",
396 cli_errstr(cli_share_dst)));
397 nt_status = cli_nt_error(cli_share_dst);
398 goto out;
403 /* closing files */
405 if (!cli_close(cli_share_src, fnum_src)) {
406 d_printf("could not close %s on originating server: %s\n",
407 is_file?"file":"dir", cli_errstr(cli_share_src));
408 nt_status = cli_nt_error(cli_share_src);
409 goto out;
412 if (!cli_close(cli_share_dst, fnum_dst)) {
413 d_printf("could not close %s on destination server: %s\n",
414 is_file?"file":"dir", cli_errstr(cli_share_dst));
415 nt_status = cli_nt_error(cli_share_dst);
416 goto out;
420 nt_status = NT_STATUS_OK;
422 out:
424 /* cleaning up */
425 if (fnum_src)
426 cli_close(cli_share_src, fnum_src);
428 if (fnum_dst)
429 cli_close(cli_share_dst, fnum_dst);
431 return nt_status;
435 * Copy a file or directory from a connected share to another connected share
437 * @param mem_ctx A talloc-context
438 * @param cli_share_src A connected cli_state
439 * @param cli_share_dst A connected cli_state
440 * @param src_file The source file-name
441 * @param dst_file The destination file-name
442 * @param copy_acls Whether to copy acls
443 * @param copy_attrs Whether to copy DOS attributes
444 * @param copy_timestamps Whether to preserve timestamps
445 * @param is_file Whether this file is a file or a dir
447 * @return Normal NTSTATUS return.
448 **/
450 NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
451 struct cli_state *cli_share_src,
452 struct cli_state *cli_share_dst,
453 const char *src_name, const char *dst_name,
454 BOOL copy_acls, BOOL copy_attrs,
455 BOOL copy_timestamps, BOOL is_file)
457 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
458 int fnum_src = 0;
459 int fnum_dst = 0;
460 static int io_bufsize = 64512;
461 int read_size = io_bufsize;
462 char *data = NULL;
463 off_t start = 0;
464 off_t nread = 0;
467 if (!src_name || !dst_name)
468 goto out;
470 if (cli_share_src == NULL || cli_share_dst == NULL)
471 goto out;
474 /* open on the originating server */
475 DEBUGADD(3,("opening %s %s on originating server\n",
476 is_file ? "file":"dir", src_name));
477 if (is_file)
478 fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
479 else
480 fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
482 if (fnum_src == -1) {
483 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
484 is_file ? "file":"dir",
485 src_name, cli_errstr(cli_share_src)));
486 nt_status = cli_nt_error(cli_share_src);
487 goto out;
491 if (is_file) {
493 /* open file on the destination server */
494 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
495 fnum_dst = cli_open(cli_share_dst, dst_name,
496 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
498 if (fnum_dst == -1) {
499 DEBUGADD(1,("cannot create file %s on destination server: %s\n",
500 dst_name, cli_errstr(cli_share_dst)));
501 nt_status = cli_nt_error(cli_share_dst);
502 goto out;
505 /* allocate memory */
506 if (!(data = (char *)SMB_MALLOC(read_size))) {
507 d_printf("malloc fail for size %d\n", read_size);
508 nt_status = NT_STATUS_NO_MEMORY;
509 goto out;
515 if (opt_verbose) {
517 d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
518 "%s ACLs and %s DOS Attributes %s\n",
519 cli_share_src->desthost, cli_share_src->share, src_name,
520 cli_share_dst->desthost, cli_share_dst->share, dst_name,
521 copy_acls ? "with" : "without",
522 copy_attrs ? "with" : "without",
523 copy_timestamps ? "(preserving timestamps)" : "" );
527 while (is_file) {
529 /* copying file */
530 int n, ret;
531 n = cli_read(cli_share_src, fnum_src, data, nread + start,
532 read_size);
534 if (n <= 0)
535 break;
537 ret = cli_write(cli_share_dst, fnum_dst, 0, data,
538 nread + start, n);
540 if (n != ret) {
541 d_printf("Error writing file: %s\n",
542 cli_errstr(cli_share_dst));
543 nt_status = cli_nt_error(cli_share_dst);
544 goto out;
547 nread += n;
551 if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
553 /* creating dir */
554 DEBUGADD(3,("creating dir %s on the destination server\n",
555 dst_name));
557 if (!cli_mkdir(cli_share_dst, dst_name)) {
558 DEBUG(0,("cannot create directory %s: %s\n",
559 dst_name, cli_errstr(cli_share_dst)));
560 nt_status = NT_STATUS_NO_SUCH_FILE;
563 if (!cli_chkpath(cli_share_dst, dst_name)) {
564 d_printf("cannot check for directory %s: %s\n",
565 dst_name, cli_errstr(cli_share_dst));
566 goto out;
571 /* closing files */
572 if (!cli_close(cli_share_src, fnum_src)) {
573 d_printf("could not close file on originating server: %s\n",
574 cli_errstr(cli_share_src));
575 nt_status = cli_nt_error(cli_share_src);
576 goto out;
579 if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
580 d_printf("could not close file on destination server: %s\n",
581 cli_errstr(cli_share_dst));
582 nt_status = cli_nt_error(cli_share_dst);
583 goto out;
586 /* possibly we have to copy some file-attributes / acls / sd */
587 nt_status = net_copy_fileattr(mem_ctx, cli_share_src, cli_share_dst,
588 src_name, dst_name, copy_acls,
589 copy_attrs, copy_timestamps, is_file);
590 if (!NT_STATUS_IS_OK(nt_status))
591 goto out;
594 nt_status = NT_STATUS_OK;
596 out:
598 /* cleaning up */
599 if (fnum_src)
600 cli_close(cli_share_src, fnum_src);
602 if (fnum_dst)
603 cli_close(cli_share_dst, fnum_dst);
605 SAFE_FREE(data);
607 return nt_status;
611 * Copy a driverfile from on connected share to another connected share
612 * This silently assumes that a driver-file is picked up from
614 * \\src_server\print$\{arch}\{version}\file
616 * and copied to
618 * \\dst_server\print$\{arch}\file
620 * to be added via setdriver-calls later.
621 * @param mem_ctx A talloc-context
622 * @param cli_share_src A cli_state connected to source print$-share
623 * @param cli_share_dst A cli_state connected to destination print$-share
624 * @param file The file-name to be copied
625 * @param short_archi The name of the driver-architecture (short form)
627 * @return Normal NTSTATUS return.
628 **/
630 static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
631 struct cli_state *cli_share_src,
632 struct cli_state *cli_share_dst,
633 char *file, const char *short_archi) {
635 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
636 const char *p;
637 char *src_name;
638 char *dst_name;
639 fstring version;
640 fstring filename;
641 fstring tok;
643 /* scroll through the file until we have the part
644 beyond archi_table.short_archi */
645 p = file;
646 while (next_token(&p, tok, "\\", sizeof(tok))) {
647 if (strequal(tok, short_archi)) {
648 next_token(&p, version, "\\", sizeof(version));
649 next_token(&p, filename, "\\", sizeof(filename));
653 /* build source file name */
654 if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
655 return NT_STATUS_NO_MEMORY;
658 /* create destination file name */
659 if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
660 return NT_STATUS_NO_MEMORY;
663 /* finally copy the file */
664 nt_status = net_copy_file(mem_ctx, cli_share_src, cli_share_dst,
665 src_name, dst_name, False, False, False, True);
666 if (!NT_STATUS_IS_OK(nt_status))
667 goto out;
669 nt_status = NT_STATUS_OK;
671 out:
672 SAFE_FREE(src_name);
673 SAFE_FREE(dst_name);
675 return nt_status;
679 * Check for existing Architecture directory on a given server
681 * @param cli_share A cli_state connected to a print$-share
682 * @param short_archi The Architecture for the print-driver
684 * @return Normal NTSTATUS return.
687 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
690 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
691 char *dir;
693 if (asprintf(&dir, "\\%s", short_archi) < 0) {
694 return NT_STATUS_NO_MEMORY;
697 DEBUG(10,("creating print-driver dir for architecture: %s\n",
698 short_archi));
700 if (!cli_mkdir(cli_share, dir)) {
701 DEBUG(1,("cannot create directory %s: %s\n",
702 dir, cli_errstr(cli_share)));
703 nt_status = NT_STATUS_NO_SUCH_FILE;
706 if (!cli_chkpath(cli_share, dir)) {
707 d_printf("cannot check %s: %s\n",
708 dir, cli_errstr(cli_share));
709 goto out;
712 nt_status = NT_STATUS_OK;
714 out:
715 SAFE_FREE(dir);
716 return nt_status;
720 * Copy a print-driver (level 3) from one connected print$-share to another
721 * connected print$-share
723 * @param mem_ctx A talloc-context
724 * @param cli_share_src A cli_state connected to a print$-share
725 * @param cli_share_dst A cli_state connected to a print$-share
726 * @param short_archi The Architecture for the print-driver
727 * @param i1 The DRIVER_INFO_3-struct
729 * @return Normal NTSTATUS return.
732 static NTSTATUS copy_print_driver_3(TALLOC_CTX *mem_ctx,
733 struct cli_state *cli_share_src,
734 struct cli_state *cli_share_dst,
735 const char *short_archi, DRIVER_INFO_3 *i1)
737 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
738 int length = 0;
739 BOOL valid = True;
741 fstring name = "";
742 fstring driverpath = "";
743 fstring datafile = "";
744 fstring configfile = "";
745 fstring helpfile = "";
746 fstring dependentfiles = "";
748 if (i1 == NULL)
749 return nt_status;
751 rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
752 rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
753 rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
754 rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
755 rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
758 if (opt_verbose)
759 d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
760 name, short_archi, i1->version);
762 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
763 driverpath, short_archi);
764 if (!NT_STATUS_IS_OK(nt_status))
765 return nt_status;
767 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
768 datafile, short_archi);
769 if (!NT_STATUS_IS_OK(nt_status))
770 return nt_status;
772 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
773 configfile, short_archi);
774 if (!NT_STATUS_IS_OK(nt_status))
775 return nt_status;
777 nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst,
778 helpfile, short_archi);
779 if (!NT_STATUS_IS_OK(nt_status))
780 return nt_status;
782 while (valid) {
784 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
785 length += strlen(dependentfiles)+1;
787 if (strlen(dependentfiles) > 0) {
789 nt_status = net_copy_driverfile(mem_ctx,
790 cli_share_src, cli_share_dst,
791 dependentfiles, short_archi);
792 if (!NT_STATUS_IS_OK(nt_status))
793 return nt_status;
794 } else {
795 valid = False;
799 return NT_STATUS_OK;
803 * net_spoolss-functions
804 * =====================
806 * the net_spoolss-functions aim to simplify spoolss-client-functions
807 * required during the migration-process wrt buffer-sizes, returned
808 * error-codes, etc.
810 * this greatly reduces the complexitiy of the migrate-functions.
814 static BOOL net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
815 TALLOC_CTX *mem_ctx,
816 char *name,
817 uint32 flags,
818 uint32 level,
819 uint32 *num_printers,
820 PRINTER_INFO_CTR *ctr)
822 WERROR result;
824 /* enum printers */
825 result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
826 level, num_printers, ctr);
828 if (!W_ERROR_IS_OK(result)) {
829 printf("cannot enum printers: %s\n", dos_errstr(result));
830 return False;
833 return True;
836 static BOOL net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
837 TALLOC_CTX *mem_ctx,
838 const char *printername,
839 uint32 access_required,
840 const char *username,
841 POLICY_HND *hnd)
843 WERROR result;
844 fstring servername, printername2;
846 slprintf(servername, sizeof(servername)-1, "\\\\%s", pipe_hnd->cli->desthost);
848 fstrcpy(printername2, servername);
849 fstrcat(printername2, "\\");
850 fstrcat(printername2, printername);
852 DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
853 servername, username, printername2, access_required));
855 /* open printer */
856 result = rpccli_spoolss_open_printer_ex(pipe_hnd, mem_ctx, printername2,
857 "", access_required,
858 servername, username, hnd);
860 /* be more verbose */
861 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
862 d_printf("no access to printer [%s] on [%s] for user [%s] granted\n",
863 printername2, servername, username);
864 return False;
867 if (!W_ERROR_IS_OK(result)) {
868 d_printf("cannot open printer %s on server %s: %s\n",
869 printername2, servername, dos_errstr(result));
870 return False;
873 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
874 printername2, servername));
876 return True;
879 static BOOL net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
880 TALLOC_CTX *mem_ctx,
881 POLICY_HND *hnd,
882 uint32 level,
883 PRINTER_INFO_CTR *ctr)
885 WERROR result;
887 /* getprinter call */
888 result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx, hnd, level, ctr);
890 if (!W_ERROR_IS_OK(result)) {
891 printf("cannot get printer-info: %s\n", dos_errstr(result));
892 return False;
895 return True;
898 static BOOL net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
899 TALLOC_CTX *mem_ctx,
900 POLICY_HND *hnd,
901 uint32 level,
902 PRINTER_INFO_CTR *ctr)
904 WERROR result;
906 /* setprinter call */
907 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, hnd, level, ctr, 0);
909 if (!W_ERROR_IS_OK(result)) {
910 printf("cannot set printer-info: %s\n", dos_errstr(result));
911 return False;
914 return True;
918 static BOOL net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
919 TALLOC_CTX *mem_ctx,
920 POLICY_HND *hnd,
921 REGISTRY_VALUE *value)
923 WERROR result;
925 /* setprinterdata call */
926 result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
928 if (!W_ERROR_IS_OK(result)) {
929 printf ("unable to set printerdata: %s\n", dos_errstr(result));
930 return False;
933 return True;
937 static BOOL net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
938 TALLOC_CTX *mem_ctx,
939 POLICY_HND *hnd,
940 const char *keyname,
941 uint16 **keylist)
943 WERROR result;
945 /* enumprinterkey call */
946 result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
948 if (!W_ERROR_IS_OK(result)) {
949 printf("enumprinterkey failed: %s\n", dos_errstr(result));
950 return False;
953 return True;
956 static BOOL net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
957 TALLOC_CTX *mem_ctx,
958 uint32 offered,
959 POLICY_HND *hnd,
960 const char *keyname,
961 REGVAL_CTR *ctr)
963 WERROR result;
965 /* enumprinterdataex call */
966 result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
968 if (!W_ERROR_IS_OK(result)) {
969 printf("enumprinterdataex failed: %s\n", dos_errstr(result));
970 return False;
973 return True;
977 static BOOL net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
978 TALLOC_CTX *mem_ctx,
979 POLICY_HND *hnd,
980 char *keyname,
981 REGISTRY_VALUE *value)
983 WERROR result;
985 /* setprinterdataex call */
986 result = rpccli_spoolss_setprinterdataex(pipe_hnd, mem_ctx, hnd,
987 keyname, value);
989 if (!W_ERROR_IS_OK(result)) {
990 printf("could not set printerdataex: %s\n", dos_errstr(result));
991 return False;
994 return True;
997 static BOOL net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
998 TALLOC_CTX *mem_ctx,
999 POLICY_HND *hnd,
1000 int level,
1001 uint32 *num_forms,
1002 FORM_1 **forms)
1005 WERROR result;
1007 /* enumforms call */
1008 result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
1010 if (!W_ERROR_IS_OK(result)) {
1011 printf("could not enum forms: %s\n", dos_errstr(result));
1012 return False;
1015 return True;
1018 static BOOL net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
1019 TALLOC_CTX *mem_ctx,
1020 uint32 level, const char *env,
1021 uint32 *num_drivers,
1022 PRINTER_DRIVER_CTR *ctr)
1024 WERROR result;
1026 /* enumprinterdrivers call */
1027 result = rpccli_spoolss_enumprinterdrivers(
1028 pipe_hnd, mem_ctx, level,
1029 env, num_drivers, ctr);
1031 if (!W_ERROR_IS_OK(result)) {
1032 printf("cannot enum drivers: %s\n", dos_errstr(result));
1033 return False;
1036 return True;
1039 static BOOL net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
1040 TALLOC_CTX *mem_ctx,
1041 POLICY_HND *hnd, uint32 level,
1042 const char *env, int version,
1043 PRINTER_DRIVER_CTR *ctr)
1045 WERROR result;
1047 /* getprinterdriver call */
1048 result = rpccli_spoolss_getprinterdriver(
1049 pipe_hnd, mem_ctx, hnd, level,
1050 env, version, ctr);
1052 if (!W_ERROR_IS_OK(result)) {
1053 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1054 env, dos_errstr(result)));
1055 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1056 W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1057 printf("cannot get driver: %s\n", dos_errstr(result));
1059 return False;
1062 return True;
1066 static BOOL net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1067 TALLOC_CTX *mem_ctx, uint32 level,
1068 PRINTER_DRIVER_CTR *ctr)
1070 WERROR result;
1072 /* addprinterdriver call */
1073 result = rpccli_spoolss_addprinterdriver(pipe_hnd, mem_ctx, level, ctr);
1075 /* be more verbose */
1076 if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1077 printf("You are not allowed to add drivers\n");
1078 return False;
1080 if (!W_ERROR_IS_OK(result)) {
1081 printf("cannot add driver: %s\n", dos_errstr(result));
1082 return False;
1085 return True;
1089 * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
1090 * for a single printer or for all printers depending on argc/argv
1093 static BOOL get_printer_info(struct rpc_pipe_client *pipe_hnd,
1094 TALLOC_CTX *mem_ctx,
1095 int level,
1096 int argc,
1097 const char **argv,
1098 uint32 *num_printers,
1099 PRINTER_INFO_CTR *ctr)
1102 POLICY_HND hnd;
1104 /* no arguments given, enumerate all printers */
1105 if (argc == 0) {
1107 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1108 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1109 level, num_printers, ctr))
1110 return False;
1112 goto out;
1116 /* argument given, get a single printer by name */
1117 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1118 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd))
1119 return False;
1121 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, ctr)) {
1122 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1123 return False;
1126 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1128 *num_printers = 1;
1130 out:
1131 DEBUG(3,("got %d printers\n", *num_printers));
1133 return True;
1137 /**
1138 * List print-queues (including local printers that are not shared)
1140 * All parameters are provided by the run_rpc_command function, except for
1141 * argc, argv which are passed through.
1143 * @param domain_sid The domain sid aquired from the remote server
1144 * @param cli A cli_state connected to the server.
1145 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1146 * @param argc Standard main() style argc
1147 * @param argv Standard main() style argv. Initial components are already
1148 * stripped
1150 * @return Normal NTSTATUS return.
1153 NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid,
1154 const char *domain_name,
1155 struct cli_state *cli,
1156 struct rpc_pipe_client *pipe_hnd,
1157 TALLOC_CTX *mem_ctx,
1158 int argc,
1159 const char **argv)
1161 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1162 uint32 i, num_printers;
1163 uint32 level = 2;
1164 pstring printername, sharename;
1165 PRINTER_INFO_CTR ctr;
1167 printf("listing printers\n");
1169 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1170 return nt_status;
1172 for (i = 0; i < num_printers; i++) {
1174 /* do some initialization */
1175 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1176 sizeof(printername), -1, STR_TERMINATE);
1177 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1178 sizeof(sharename), -1, STR_TERMINATE);
1180 d_printf("printer %d: %s, shared as: %s\n",
1181 i+1, printername, sharename);
1184 return NT_STATUS_OK;
1187 /**
1188 * List printer-drivers from a server
1190 * All parameters are provided by the run_rpc_command function, except for
1191 * argc, argv which are passed through.
1193 * @param domain_sid The domain sid aquired from the remote server
1194 * @param cli A cli_state connected to the server.
1195 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1196 * @param argc Standard main() style argc
1197 * @param argv Standard main() style argv. Initial components are already
1198 * stripped
1200 * @return Normal NTSTATUS return.
1203 NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid,
1204 const char *domain_name,
1205 struct cli_state *cli,
1206 struct rpc_pipe_client *pipe_hnd,
1207 TALLOC_CTX *mem_ctx,
1208 int argc,
1209 const char **argv)
1211 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1212 uint32 i;
1213 uint32 level = 3;
1214 PRINTER_DRIVER_CTR drv_ctr_enum;
1215 int d;
1217 ZERO_STRUCT(drv_ctr_enum);
1219 printf("listing printer-drivers\n");
1221 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1223 uint32 num_drivers;
1225 /* enum remote drivers */
1226 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1227 archi_table[i].long_archi,
1228 &num_drivers, &drv_ctr_enum)) {
1230 nt_status = NT_STATUS_UNSUCCESSFUL;
1231 goto done;
1234 if (num_drivers == 0) {
1235 d_printf ("no drivers found on server for architecture: [%s].\n",
1236 archi_table[i].long_archi);
1237 continue;
1240 d_printf("got %d printer-drivers for architecture: [%s]\n",
1241 num_drivers, archi_table[i].long_archi);
1244 /* do something for all drivers for architecture */
1245 for (d = 0; d < num_drivers; d++) {
1246 display_print_driver_3(&(drv_ctr_enum.info3[d]));
1250 nt_status = NT_STATUS_OK;
1252 done:
1253 return nt_status;
1257 /**
1258 * Publish print-queues with args-wrapper
1260 * @param cli A cli_state connected to the server.
1261 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1262 * @param argc Standard main() style argc
1263 * @param argv Standard main() style argv. Initial components are already
1264 * stripped
1265 * @param action
1267 * @return Normal NTSTATUS return.
1270 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1271 TALLOC_CTX *mem_ctx,
1272 int argc,
1273 const char **argv,
1274 uint32 action)
1276 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1277 uint32 i, num_printers;
1278 uint32 level = 7;
1279 pstring printername, sharename;
1280 PRINTER_INFO_CTR ctr, ctr_pub;
1281 POLICY_HND hnd;
1282 BOOL got_hnd = False;
1283 WERROR result;
1284 const char *action_str;
1286 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1287 return nt_status;
1289 for (i = 0; i < num_printers; i++) {
1291 /* do some initialization */
1292 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1293 sizeof(printername), -1, STR_TERMINATE);
1294 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1295 sizeof(sharename), -1, STR_TERMINATE);
1297 /* open printer handle */
1298 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1299 PRINTER_ALL_ACCESS, pipe_hnd->cli->user_name, &hnd))
1300 goto done;
1302 got_hnd = True;
1304 /* check for existing dst printer */
1305 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1306 goto done;
1308 /* check action and set string */
1309 switch (action) {
1310 case SPOOL_DS_PUBLISH:
1311 action_str = "published";
1312 break;
1313 case SPOOL_DS_UPDATE:
1314 action_str = "updated";
1315 break;
1316 case SPOOL_DS_UNPUBLISH:
1317 action_str = "unpublished";
1318 break;
1319 default:
1320 action_str = "unknown action";
1321 printf("unkown action: %d\n", action);
1322 break;
1325 ctr_pub.printers_7->action = action;
1327 result = rpccli_spoolss_setprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub, 0);
1328 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1329 printf("cannot set printer-info: %s\n", dos_errstr(result));
1330 goto done;
1333 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1336 nt_status = NT_STATUS_OK;
1338 done:
1339 if (got_hnd)
1340 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1342 return nt_status;
1345 NTSTATUS rpc_printer_publish_publish_internals(const DOM_SID *domain_sid,
1346 const char *domain_name,
1347 struct cli_state *cli,
1348 struct rpc_pipe_client *pipe_hnd,
1349 TALLOC_CTX *mem_ctx,
1350 int argc,
1351 const char **argv)
1353 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_PUBLISH);
1356 NTSTATUS rpc_printer_publish_unpublish_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 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
1367 NTSTATUS rpc_printer_publish_update_internals(const DOM_SID *domain_sid,
1368 const char *domain_name,
1369 struct cli_state *cli,
1370 struct rpc_pipe_client *pipe_hnd,
1371 TALLOC_CTX *mem_ctx,
1372 int argc,
1373 const char **argv)
1375 return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
1378 /**
1379 * List print-queues w.r.t. their publishing state
1381 * All parameters are provided by the run_rpc_command function, except for
1382 * argc, argv which are passed through.
1384 * @param domain_sid The domain sid aquired from the remote server
1385 * @param cli A cli_state connected to the server.
1386 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1387 * @param argc Standard main() style argc
1388 * @param argv Standard main() style argv. Initial components are already
1389 * stripped
1391 * @return Normal NTSTATUS return.
1394 NTSTATUS rpc_printer_publish_list_internals(const DOM_SID *domain_sid,
1395 const char *domain_name,
1396 struct cli_state *cli,
1397 struct rpc_pipe_client *pipe_hnd,
1398 TALLOC_CTX *mem_ctx,
1399 int argc,
1400 const char **argv)
1402 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1403 uint32 i, num_printers;
1404 uint32 level = 7;
1405 pstring printername, sharename;
1406 pstring guid;
1407 PRINTER_INFO_CTR ctr, ctr_pub;
1408 POLICY_HND hnd;
1409 BOOL got_hnd = False;
1410 int state;
1412 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1413 return nt_status;
1415 for (i = 0; i < num_printers; i++) {
1417 ZERO_STRUCT(ctr_pub);
1419 /* do some initialization */
1420 rpcstr_pull(printername, ctr.printers_2[i].printername.buffer,
1421 sizeof(printername), -1, STR_TERMINATE);
1422 rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer,
1423 sizeof(sharename), -1, STR_TERMINATE);
1425 /* open printer handle */
1426 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1427 PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1428 goto done;
1430 got_hnd = True;
1432 /* check for existing dst printer */
1433 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &ctr_pub))
1434 goto done;
1436 rpcstr_pull(guid, ctr_pub.printers_7->guid.buffer, sizeof(guid), -1, STR_TERMINATE);
1438 state = ctr_pub.printers_7->action;
1439 switch (state) {
1440 case SPOOL_DS_PUBLISH:
1441 printf("printer [%s] is published", sharename);
1442 if (opt_verbose)
1443 printf(", guid: %s", guid);
1444 printf("\n");
1445 break;
1446 case SPOOL_DS_UNPUBLISH:
1447 printf("printer [%s] is unpublished\n", sharename);
1448 break;
1449 case SPOOL_DS_UPDATE:
1450 printf("printer [%s] is currently updating\n", sharename);
1451 break;
1452 default:
1453 printf("unkown state: %d\n", state);
1454 break;
1458 nt_status = NT_STATUS_OK;
1460 done:
1461 if (got_hnd)
1462 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd);
1464 return nt_status;
1467 /**
1468 * Migrate Printer-ACLs from a source server to the destination server
1470 * All parameters are provided by the run_rpc_command function, except for
1471 * argc, argv which are passed through.
1473 * @param domain_sid The domain sid aquired from the remote server
1474 * @param cli A cli_state connected to the server.
1475 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1476 * @param argc Standard main() style argc
1477 * @param argv Standard main() style argv. Initial components are already
1478 * stripped
1480 * @return Normal NTSTATUS return.
1483 NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid,
1484 const char *domain_name,
1485 struct cli_state *cli,
1486 struct rpc_pipe_client *pipe_hnd,
1487 TALLOC_CTX *mem_ctx,
1488 int argc,
1489 const char **argv)
1491 /* TODO: what now, info2 or info3 ?
1492 convince jerry that we should add clientside setacls level 3 at least
1494 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1495 uint32 i = 0;
1496 uint32 num_printers;
1497 uint32 level = 2;
1498 pstring printername = "", sharename = "";
1499 BOOL got_hnd_src = False;
1500 BOOL got_hnd_dst = False;
1501 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1502 POLICY_HND hnd_src, hnd_dst;
1503 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
1504 struct cli_state *cli_dst = NULL;
1506 ZERO_STRUCT(ctr_src);
1508 DEBUG(3,("copying printer ACLs\n"));
1510 /* connect destination PI_SPOOLSS */
1511 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1512 if (!NT_STATUS_IS_OK(nt_status))
1513 return nt_status;
1516 /* enum source printers */
1517 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1518 nt_status = NT_STATUS_UNSUCCESSFUL;
1519 goto done;
1522 if (!num_printers) {
1523 printf ("no printers found on server.\n");
1524 nt_status = NT_STATUS_OK;
1525 goto done;
1528 /* do something for all printers */
1529 for (i = 0; i < num_printers; i++) {
1531 /* do some initialization */
1532 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1533 sizeof(printername), -1, STR_TERMINATE);
1534 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1535 sizeof(sharename), -1, STR_TERMINATE);
1536 /* we can reset NT_STATUS here because we do not
1537 get any real NT_STATUS-codes anymore from now on */
1538 nt_status = NT_STATUS_UNSUCCESSFUL;
1540 d_printf("migrating printer ACLs for: [%s] / [%s]\n",
1541 printername, sharename);
1543 /* according to msdn you have specify these access-rights
1544 to see the security descriptor
1545 - READ_CONTROL (DACL)
1546 - ACCESS_SYSTEM_SECURITY (SACL)
1549 /* open src printer handle */
1550 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1551 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1552 goto done;
1554 got_hnd_src = True;
1556 /* open dst printer handle */
1557 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1558 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1559 goto done;
1561 got_hnd_dst = True;
1563 /* check for existing dst printer */
1564 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1565 goto done;
1567 /* check for existing src printer */
1568 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &ctr_src))
1569 goto done;
1571 /* Copy Security Descriptor */
1573 /* copy secdesc (info level 2) */
1574 ctr_dst.printers_2->devmode = NULL;
1575 ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
1577 if (opt_verbose)
1578 display_sec_desc(ctr_dst.printers_2->secdesc);
1580 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &ctr_dst))
1581 goto done;
1583 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1586 /* close printer handles here */
1587 if (got_hnd_src) {
1588 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1589 got_hnd_src = False;
1592 if (got_hnd_dst) {
1593 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1594 got_hnd_dst = False;
1599 nt_status = NT_STATUS_OK;
1601 done:
1603 if (got_hnd_src) {
1604 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1607 if (got_hnd_dst) {
1608 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1611 if (cli_dst) {
1612 cli_shutdown(cli_dst);
1614 return nt_status;
1617 /**
1618 * Migrate printer-forms from a src server to the dst server
1620 * All parameters are provided by the run_rpc_command function, except for
1621 * argc, argv which are passed through.
1623 * @param domain_sid The domain sid aquired from the remote server
1624 * @param cli A cli_state connected to the server.
1625 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1626 * @param argc Standard main() style argc
1627 * @param argv Standard main() style argv. Initial components are already
1628 * stripped
1630 * @return Normal NTSTATUS return.
1633 NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid,
1634 const char *domain_name,
1635 struct cli_state *cli,
1636 struct rpc_pipe_client *pipe_hnd,
1637 TALLOC_CTX *mem_ctx,
1638 int argc,
1639 const char **argv)
1641 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1642 WERROR result;
1643 uint32 i, f;
1644 uint32 num_printers;
1645 uint32 level = 1;
1646 pstring printername = "", sharename = "";
1647 BOOL got_hnd_src = False;
1648 BOOL got_hnd_dst = False;
1649 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1650 POLICY_HND hnd_src, hnd_dst;
1651 PRINTER_INFO_CTR ctr_enum, ctr_dst;
1652 uint32 num_forms;
1653 FORM_1 *forms;
1654 struct cli_state *cli_dst = NULL;
1656 ZERO_STRUCT(ctr_enum);
1658 DEBUG(3,("copying forms\n"));
1660 /* connect destination PI_SPOOLSS */
1661 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1662 if (!NT_STATUS_IS_OK(nt_status))
1663 return nt_status;
1666 /* enum src printers */
1667 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1668 nt_status = NT_STATUS_UNSUCCESSFUL;
1669 goto done;
1672 if (!num_printers) {
1673 printf ("no printers found on server.\n");
1674 nt_status = NT_STATUS_OK;
1675 goto done;
1679 /* do something for all printers */
1680 for (i = 0; i < num_printers; i++) {
1682 /* do some initialization */
1683 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
1684 sizeof(printername), -1, STR_TERMINATE);
1685 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
1686 sizeof(sharename), -1, STR_TERMINATE);
1687 /* we can reset NT_STATUS here because we do not
1688 get any real NT_STATUS-codes anymore from now on */
1689 nt_status = NT_STATUS_UNSUCCESSFUL;
1691 d_printf("migrating printer forms for: [%s] / [%s]\n",
1692 printername, sharename);
1695 /* open src printer handle */
1696 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1697 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1698 goto done;
1700 got_hnd_src = True;
1703 /* open dst printer handle */
1704 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1705 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1706 goto done;
1708 got_hnd_dst = True;
1711 /* check for existing dst printer */
1712 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst))
1713 goto done;
1715 /* finally migrate forms */
1716 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1717 goto done;
1719 DEBUG(1,("got %d forms for printer\n", num_forms));
1722 for (f = 0; f < num_forms; f++) {
1724 FORM form;
1725 fstring form_name;
1727 /* only migrate FORM_PRINTER types, according to jerry
1728 FORM_BUILTIN-types are hard-coded in samba */
1729 if (forms[f].flag != FORM_PRINTER)
1730 continue;
1732 if (forms[f].name.buffer)
1733 rpcstr_pull(form_name, forms[f].name.buffer,
1734 sizeof(form_name), -1, STR_TERMINATE);
1736 if (opt_verbose)
1737 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1738 f, form_name, forms[f].flag);
1740 /* is there a more elegant way to do that ? */
1741 form.flags = FORM_PRINTER;
1742 form.size_x = forms[f].width;
1743 form.size_y = forms[f].length;
1744 form.left = forms[f].left;
1745 form.top = forms[f].top;
1746 form.right = forms[f].right;
1747 form.bottom = forms[f].bottom;
1749 init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
1751 /* FIXME: there might be something wrong with samba's
1752 builtin-forms */
1753 result = rpccli_spoolss_addform(pipe_hnd_dst, mem_ctx,
1754 &hnd_dst, 1, &form);
1755 if (!W_ERROR_IS_OK(result)) {
1756 d_printf("\tAddForm form %d: [%s] refused.\n",
1757 f, form_name);
1758 continue;
1761 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1765 /* close printer handles here */
1766 if (got_hnd_src) {
1767 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1768 got_hnd_src = False;
1771 if (got_hnd_dst) {
1772 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1773 got_hnd_dst = False;
1777 nt_status = NT_STATUS_OK;
1779 done:
1781 if (got_hnd_src)
1782 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1784 if (got_hnd_dst)
1785 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1787 if (cli_dst) {
1788 cli_shutdown(cli_dst);
1790 return nt_status;
1793 /**
1794 * Migrate printer-drivers from a src server to the dst server
1796 * All parameters are provided by the run_rpc_command function, except for
1797 * argc, argv which are passed through.
1799 * @param domain_sid The domain sid aquired from the remote server
1800 * @param cli A cli_state connected to the server.
1801 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1802 * @param argc Standard main() style argc
1803 * @param argv Standard main() style argv. Initial components are already
1804 * stripped
1806 * @return Normal NTSTATUS return.
1809 NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid,
1810 const char *domain_name,
1811 struct cli_state *cli,
1812 struct rpc_pipe_client *pipe_hnd,
1813 TALLOC_CTX *mem_ctx,
1814 int argc,
1815 const char **argv)
1817 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1818 uint32 i, p;
1819 uint32 num_printers;
1820 uint32 level = 3;
1821 pstring printername = "", sharename = "";
1822 BOOL got_hnd_src = False;
1823 BOOL got_hnd_dst = False;
1824 BOOL got_src_driver_share = False;
1825 BOOL got_dst_driver_share = False;
1826 struct rpc_pipe_client *pipe_hnd_dst = NULL;
1827 POLICY_HND hnd_src, hnd_dst;
1828 PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
1829 PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1830 struct cli_state *cli_dst = NULL;
1831 struct cli_state *cli_share_src = NULL;
1832 struct cli_state *cli_share_dst = NULL;
1833 fstring drivername = "";
1835 ZERO_STRUCT(drv_ctr_src);
1836 ZERO_STRUCT(drv_ctr_dst);
1837 ZERO_STRUCT(info_ctr_enum);
1838 ZERO_STRUCT(info_ctr_dst);
1841 DEBUG(3,("copying printer-drivers\n"));
1843 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
1844 if (!NT_STATUS_IS_OK(nt_status))
1845 return nt_status;
1848 /* open print$-share on the src server */
1849 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
1850 cli->desthost, "print$", "A:");
1851 if (!NT_STATUS_IS_OK(nt_status))
1852 goto done;
1854 got_src_driver_share = True;
1857 /* open print$-share on the dst server */
1858 nt_status = connect_to_service(&cli_share_dst, &cli_dst->dest_ip,
1859 cli_dst->desthost, "print$", "A:");
1860 if (!NT_STATUS_IS_OK(nt_status))
1861 return nt_status;
1863 got_dst_driver_share = True;
1866 /* enum src printers */
1867 if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1868 nt_status = NT_STATUS_UNSUCCESSFUL;
1869 goto done;
1872 if (num_printers == 0) {
1873 printf ("no printers found on server.\n");
1874 nt_status = NT_STATUS_OK;
1875 goto done;
1879 /* do something for all printers */
1880 for (p = 0; p < num_printers; p++) {
1882 /* do some initialization */
1883 rpcstr_pull(printername, info_ctr_enum.printers_2[p].printername.buffer,
1884 sizeof(printername), -1, STR_TERMINATE);
1885 rpcstr_pull(sharename, info_ctr_enum.printers_2[p].sharename.buffer,
1886 sizeof(sharename), -1, STR_TERMINATE);
1887 /* we can reset NT_STATUS here because we do not
1888 get any real NT_STATUS-codes anymore from now on */
1889 nt_status = NT_STATUS_UNSUCCESSFUL;
1891 d_printf("migrating printer driver for: [%s] / [%s]\n",
1892 printername, sharename);
1894 /* open dst printer handle */
1895 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1896 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1897 goto done;
1899 got_hnd_dst = True;
1901 /* check for existing dst printer */
1902 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst))
1903 goto done;
1906 /* open src printer handle */
1907 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1908 MAXIMUM_ALLOWED_ACCESS, pipe_hnd->cli->user_name, &hnd_src))
1909 goto done;
1911 got_hnd_src = True;
1914 /* in a first step call getdriver for each shared printer (per arch)
1915 to get a list of all files that have to be copied */
1917 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1919 /* getdriver src */
1920 if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1921 level, archi_table[i].long_archi,
1922 archi_table[i].version, &drv_ctr_src))
1923 continue;
1925 rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer,
1926 sizeof(drivername), -1, STR_TERMINATE);
1928 if (opt_verbose)
1929 display_print_driver_3(drv_ctr_src.info3);
1932 /* check arch dir */
1933 nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1934 if (!NT_STATUS_IS_OK(nt_status))
1935 goto done;
1938 /* copy driver-files */
1939 nt_status = copy_print_driver_3(mem_ctx, cli_share_src, cli_share_dst,
1940 archi_table[i].short_archi,
1941 drv_ctr_src.info3);
1942 if (!NT_STATUS_IS_OK(nt_status))
1943 goto done;
1946 /* adddriver dst */
1947 if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_ctr_src)) {
1948 nt_status = NT_STATUS_UNSUCCESSFUL;
1949 goto done;
1952 DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1953 drivername, printername));
1957 if (strlen(drivername) == 0) {
1958 DEBUGADD(1,("Did not get driver for printer %s\n",
1959 printername));
1960 goto done;
1963 /* setdriver dst */
1964 init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
1966 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) {
1967 nt_status = NT_STATUS_UNSUCCESSFUL;
1968 goto done;
1971 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1972 drivername, printername));
1974 /* close dst */
1975 if (got_hnd_dst) {
1976 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1977 got_hnd_dst = False;
1980 /* close src */
1981 if (got_hnd_src) {
1982 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1983 got_hnd_src = False;
1987 nt_status = NT_STATUS_OK;
1989 done:
1991 if (got_hnd_src)
1992 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
1994 if (got_hnd_dst)
1995 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
1997 if (cli_dst) {
1998 cli_shutdown(cli_dst);
2001 if (got_src_driver_share)
2002 cli_shutdown(cli_share_src);
2004 if (got_dst_driver_share)
2005 cli_shutdown(cli_share_dst);
2007 return nt_status;
2011 /**
2012 * Migrate printer-queues from a src to the dst server
2013 * (requires a working "addprinter command" to be installed for the local smbd)
2015 * All parameters are provided by the run_rpc_command function, except for
2016 * argc, argv which are passed through.
2018 * @param domain_sid The domain sid aquired from the remote server
2019 * @param cli A cli_state connected to the server.
2020 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2021 * @param argc Standard main() style argc
2022 * @param argv Standard main() style argv. Initial components are already
2023 * stripped
2025 * @return Normal NTSTATUS return.
2028 NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid,
2029 const char *domain_name,
2030 struct cli_state *cli,
2031 struct rpc_pipe_client *pipe_hnd,
2032 TALLOC_CTX *mem_ctx,
2033 int argc,
2034 const char **argv)
2036 WERROR result;
2037 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2038 uint32 i = 0, num_printers;
2039 uint32 level = 2;
2040 PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
2041 struct cli_state *cli_dst = NULL;
2042 POLICY_HND hnd_dst, hnd_src;
2043 pstring printername, sharename;
2044 BOOL got_hnd_src = False;
2045 BOOL got_hnd_dst = False;
2046 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2048 DEBUG(3,("copying printers\n"));
2050 /* connect destination PI_SPOOLSS */
2051 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
2052 if (!NT_STATUS_IS_OK(nt_status))
2053 return nt_status;
2056 /* enum printers */
2057 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2058 nt_status = NT_STATUS_UNSUCCESSFUL;
2059 goto done;
2062 if (!num_printers) {
2063 printf ("no printers found on server.\n");
2064 nt_status = NT_STATUS_OK;
2065 goto done;
2069 /* do something for all printers */
2070 for (i = 0; i < num_printers; i++) {
2072 /* do some initialization */
2073 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
2074 sizeof(printername), -1, STR_TERMINATE);
2075 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
2076 sizeof(sharename), -1, STR_TERMINATE);
2077 /* we can reset NT_STATUS here because we do not
2078 get any real NT_STATUS-codes anymore from now on */
2079 nt_status = NT_STATUS_UNSUCCESSFUL;
2081 d_printf("migrating printer queue for: [%s] / [%s]\n",
2082 printername, sharename);
2085 /* open dst printer handle */
2086 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2087 PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2089 DEBUG(1,("could not open printer: %s\n", sharename));
2090 } else {
2091 got_hnd_dst = True;
2095 /* check for existing dst printer */
2096 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) {
2097 printf ("could not get printer, creating printer.\n");
2098 } else {
2099 DEBUG(1,("printer already exists: %s\n", sharename));
2100 /* close printer handles here */
2101 if (got_hnd_src) {
2102 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2103 got_hnd_src = False;
2106 if (got_hnd_dst) {
2107 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2108 got_hnd_dst = False;
2110 continue;
2114 /* now get again src printer ctr via getprinter,
2115 we first need a handle for that */
2117 /* open src printer handle */
2118 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2119 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2120 goto done;
2122 got_hnd_src = True;
2124 /* getprinter on the src server */
2125 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &ctr_src))
2126 goto done;
2129 /* copy each src printer to a dst printer 1:1,
2130 maybe some values have to be changed though */
2131 d_printf("creating printer: %s\n", printername);
2132 result = rpccli_spoolss_addprinterex (pipe_hnd_dst, mem_ctx, level, &ctr_src);
2134 if (W_ERROR_IS_OK(result))
2135 d_printf ("printer [%s] successfully added.\n", printername);
2136 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2137 d_printf ("printer [%s] already exists.\n", printername);
2138 else {
2139 printf ("could not create printer\n");
2140 goto done;
2143 /* close printer handles here */
2144 if (got_hnd_src) {
2145 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2146 got_hnd_src = False;
2149 if (got_hnd_dst) {
2150 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2151 got_hnd_dst = False;
2155 nt_status = NT_STATUS_OK;
2157 done:
2158 if (got_hnd_src)
2159 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2161 if (got_hnd_dst)
2162 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2164 if (cli_dst) {
2165 cli_shutdown(cli_dst);
2167 return nt_status;
2170 /**
2171 * Migrate Printer-Settings from a src server to the dst server
2172 * (for this to work, printers and drivers already have to be migrated earlier)
2174 * All parameters are provided by the run_rpc_command function, except for
2175 * argc, argv which are passed through.
2177 * @param domain_sid The domain sid aquired from the remote server
2178 * @param cli A cli_state connected to the server.
2179 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2180 * @param argc Standard main() style argc
2181 * @param argv Standard main() style argv. Initial components are already
2182 * stripped
2184 * @return Normal NTSTATUS return.
2187 NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid,
2188 const char *domain_name,
2189 struct cli_state *cli,
2190 struct rpc_pipe_client *pipe_hnd,
2191 TALLOC_CTX *mem_ctx,
2192 int argc,
2193 const char **argv)
2196 /* FIXME: Here the nightmare begins */
2198 WERROR result;
2199 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2200 uint32 i = 0, p = 0, j = 0;
2201 uint32 num_printers, val_needed, data_needed;
2202 uint32 level = 2;
2203 pstring printername = "", sharename = "";
2204 BOOL got_hnd_src = False;
2205 BOOL got_hnd_dst = False;
2206 struct rpc_pipe_client *pipe_hnd_dst = NULL;
2207 POLICY_HND hnd_src, hnd_dst;
2208 PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
2209 REGVAL_CTR *reg_ctr;
2210 struct cli_state *cli_dst = NULL;
2211 char *devicename = NULL, *unc_name = NULL, *url = NULL;
2212 fstring longname;
2214 uint16 *keylist = NULL, *curkey;
2216 ZERO_STRUCT(ctr_enum);
2218 DEBUG(3,("copying printer settings\n"));
2220 /* connect destination PI_SPOOLSS */
2221 nt_status = connect_dst_pipe(&cli_dst, &pipe_hnd_dst, PI_SPOOLSS);
2222 if (!NT_STATUS_IS_OK(nt_status))
2223 return nt_status;
2226 /* enum src printers */
2227 if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2228 nt_status = NT_STATUS_UNSUCCESSFUL;
2229 goto done;
2232 if (!num_printers) {
2233 printf ("no printers found on server.\n");
2234 nt_status = NT_STATUS_OK;
2235 goto done;
2239 /* needed for dns-strings in regkeys */
2240 get_mydnsfullname(longname);
2242 /* do something for all printers */
2243 for (i = 0; i < num_printers; i++) {
2245 /* do some initialization */
2246 rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer,
2247 sizeof(printername), -1, STR_TERMINATE);
2248 rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer,
2249 sizeof(sharename), -1, STR_TERMINATE);
2251 /* we can reset NT_STATUS here because we do not
2252 get any real NT_STATUS-codes anymore from now on */
2253 nt_status = NT_STATUS_UNSUCCESSFUL;
2255 d_printf("migrating printer settings for: [%s] / [%s]\n",
2256 printername, sharename);
2259 /* open src printer handle */
2260 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2261 MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2262 goto done;
2264 got_hnd_src = True;
2267 /* open dst printer handle */
2268 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2269 PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2270 goto done;
2272 got_hnd_dst = True;
2275 /* check for existing dst printer */
2276 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2277 level, &ctr_dst))
2278 goto done;
2281 /* STEP 1: COPY DEVICE-MODE and other
2282 PRINTER_INFO_2-attributes
2285 ctr_dst.printers_2 = &ctr_enum.printers_2[i];
2287 /* why is the port always disconnected when the printer
2288 is correctly installed (incl. driver ???) */
2289 init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
2291 /* check if printer is published */
2292 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2294 /* check for existing dst printer */
2295 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
2296 goto done;
2298 ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
2300 /* ignore False from setprinter due to WERR_IO_PENDING */
2301 net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
2303 DEBUG(3,("republished printer\n"));
2306 if (ctr_enum.printers_2[i].devmode != NULL) {
2308 /* copy devmode (info level 2) */
2309 ctr_dst.printers_2->devmode =
2310 TALLOC_MEMDUP(mem_ctx,
2311 ctr_enum.printers_2[i].devmode,
2312 sizeof(DEVICEMODE));
2314 /* do not copy security descriptor (we have another
2315 * command for that) */
2316 ctr_dst.printers_2->secdesc = NULL;
2318 #if 0
2319 if (asprintf(&devicename, "\\\\%s\\%s", longname,
2320 printername) < 0) {
2321 nt_status = NT_STATUS_NO_MEMORY;
2322 goto done;
2325 init_unistr(&ctr_dst.printers_2->devmode->devicename,
2326 devicename);
2327 #endif
2328 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2329 level, &ctr_dst))
2330 goto done;
2332 DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2335 /* STEP 2: COPY REGISTRY VALUES */
2337 /* please keep in mind that samba parse_spools gives horribly
2338 crippled results when used to rpccli_spoolss_enumprinterdataex
2339 a win2k3-server. (Bugzilla #1851)
2340 FIXME: IIRC I've seen it too on a win2k-server
2343 /* enumerate data on src handle */
2344 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2345 &val_needed, &data_needed, NULL);
2347 /* loop for all printerdata of "PrinterDriverData" */
2348 while (W_ERROR_IS_OK(result)) {
2350 REGISTRY_VALUE value;
2352 result = rpccli_spoolss_enumprinterdata(
2353 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2354 data_needed, 0, 0, &value);
2356 /* loop for all reg_keys */
2357 if (W_ERROR_IS_OK(result)) {
2359 /* display_value */
2360 if (opt_verbose)
2361 display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2363 /* set_value */
2364 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2365 &hnd_dst, &value))
2366 goto done;
2368 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2369 value.valuename));
2373 /* STEP 3: COPY SUBKEY VALUES */
2375 /* here we need to enum all printer_keys and then work
2376 on the result with enum_printer_key_ex. nt4 does not
2377 respond to enumprinterkey, win2k does, so continue
2378 in case of an error */
2380 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2381 printf("got no key-data\n");
2382 continue;
2386 /* work on a list of printer keys
2387 each key has to be enumerated to get all required
2388 information. information is then set via setprinterdataex-calls */
2390 if (keylist == NULL)
2391 continue;
2393 curkey = keylist;
2394 while (*curkey != 0) {
2396 pstring subkey;
2397 rpcstr_pull(subkey, curkey, sizeof(subkey), -1, STR_TERMINATE);
2399 curkey += strlen(subkey) + 1;
2401 if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2402 return NT_STATUS_NO_MEMORY;
2404 /* enumerate all src subkeys */
2405 if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2406 &hnd_src, subkey,
2407 reg_ctr))
2408 goto done;
2410 for (j=0; j < reg_ctr->num_values; j++) {
2412 REGISTRY_VALUE value;
2413 UNISTR2 data;
2415 /* although samba replies with sane data in most cases we
2416 should try to avoid writing wrong registry data */
2418 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2419 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2420 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2421 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2422 strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2424 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2426 /* although windows uses a multi-sz, we use a sz */
2427 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2428 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2431 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2433 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2434 nt_status = NT_STATUS_NO_MEMORY;
2435 goto done;
2437 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2438 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2441 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2443 continue;
2445 #if 0
2446 /* FIXME: should we really do that ??? */
2447 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2448 nt_status = NT_STATUS_NO_MEMORY;
2449 goto done;
2451 init_unistr2(&data, url, UNI_STR_TERMINATE);
2452 fstrcpy(value.valuename, SPOOL_REG_URL);
2453 #endif
2456 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2458 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2459 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2462 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2464 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2465 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2468 value.type = REG_SZ;
2469 value.size = data.uni_str_len * 2;
2470 value.data_p = TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2472 if (opt_verbose)
2473 display_reg_value(subkey, value);
2475 /* here we have to set all subkeys on the dst server */
2476 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2477 subkey, &value))
2478 goto done;
2480 } else {
2482 if (opt_verbose)
2483 display_reg_value(subkey, *(reg_ctr->values[j]));
2485 /* here we have to set all subkeys on the dst server */
2486 if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2487 subkey, reg_ctr->values[j]))
2488 goto done;
2492 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2493 subkey, reg_ctr->values[j]->valuename));
2497 TALLOC_FREE( reg_ctr );
2500 safe_free(keylist);
2502 /* close printer handles here */
2503 if (got_hnd_src) {
2504 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2505 got_hnd_src = False;
2508 if (got_hnd_dst) {
2509 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2510 got_hnd_dst = False;
2515 nt_status = NT_STATUS_OK;
2517 done:
2518 SAFE_FREE(devicename);
2519 SAFE_FREE(url);
2520 SAFE_FREE(unc_name);
2522 if (got_hnd_src)
2523 rpccli_spoolss_close_printer(pipe_hnd, mem_ctx, &hnd_src);
2525 if (got_hnd_dst)
2526 rpccli_spoolss_close_printer(pipe_hnd_dst, mem_ctx, &hnd_dst);
2528 if (cli_dst) {
2529 cli_shutdown(cli_dst);
2531 return nt_status;