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. */
21 #include "utils/net.h"
24 const char *long_archi
;
25 const char *short_archi
;
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 },
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
53 /****************************************************************************
54 Convert a security permissions into a string.
55 ****************************************************************************/
57 char *get_sec_mask_str(uint32 type
)
59 static fstring typestr
="";
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
);
91 /****************************************************************************
92 Display sec_ace structure.
93 ****************************************************************************/
95 void display_sec_ace(SEC_ACE
*ace
)
99 printf("\tACE\n\t\ttype: ");
101 case SEC_ACE_TYPE_ACCESS_ALLOWED
:
102 printf("ACCESS ALLOWED");
104 case SEC_ACE_TYPE_ACCESS_DENIED
:
105 printf("ACCESS DENIED");
107 case SEC_ACE_TYPE_SYSTEM_AUDIT
:
108 printf("SYSTEM AUDIT");
110 case SEC_ACE_TYPE_SYSTEM_ALARM
:
111 printf("SYSTEM ALARM");
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
)
132 printf("\tACL\tNum ACEs:\t%d\trevision:\t%x\n",
133 sec_acl
->num_aces
, sec_acl
->revision
);
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
)
155 display_sec_acl(sec
->sacl
);
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
);
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
)
187 fstring architecture
= "";
188 fstring driverpath
= "";
189 fstring datafile
= "";
190 fstring configfile
= "";
191 fstring helpfile
= "";
192 fstring dependentfiles
= "";
193 fstring monitorname
= "";
194 fstring defaultdatatype
= "";
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
);
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
);
234 d_printf ("\tMonitorname: [%s]\n", monitorname
);
235 d_printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype
);
240 static void display_reg_value(const char *subkey
, REGISTRY_VALUE value
)
246 d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey
, value
.valuename
,
247 *((uint32
*) value
.data_p
));
251 rpcstr_pull(text
, value
.data_p
, sizeof(text
), value
.size
,
253 d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey
, value
.valuename
, text
);
257 d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
258 subkey
, value
.valuename
);
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,
269 d_printf("%s\n", text
);
270 curstr
+= strlen(text
) + 1;
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.
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
;
310 time_t f_atime
, f_ctime
, f_mtime
;
313 if (!copy_timestamps
&& !copy_acls
&& !copy_attrs
)
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
);
332 /* get the security descriptor */
333 sd
= cli_query_secdesc(cli_share_src
, fnum_src
, mem_ctx
);
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
);
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
);
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
);
369 if (copy_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
);
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
);
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
);
405 if (!cli_close(cli_share_src
, fnum_src
)) {
406 d_fprintf(stderr
, "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
);
412 if (!cli_close(cli_share_dst
, fnum_dst
)) {
413 d_fprintf(stderr
, "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
);
420 nt_status
= NT_STATUS_OK
;
426 cli_close(cli_share_src
, fnum_src
);
429 cli_close(cli_share_dst
, fnum_dst
);
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.
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
;
460 static int io_bufsize
= 64512;
461 int read_size
= io_bufsize
;
467 if (!src_name
|| !dst_name
)
470 if (cli_share_src
== NULL
|| cli_share_dst
== NULL
)
474 /* open on the originating server */
475 DEBUGADD(3,("opening %s %s on originating server\n",
476 is_file
? "file":"dir", src_name
));
478 fnum_src
= cli_open(cli_share_src
, src_name
, O_RDONLY
, DENY_NONE
);
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
);
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
);
505 /* allocate memory */
506 if (!(data
= (char *)SMB_MALLOC(read_size
))) {
507 d_fprintf(stderr
, "malloc fail for size %d\n", read_size
);
508 nt_status
= NT_STATUS_NO_MEMORY
;
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)" : "" );
531 n
= cli_read(cli_share_src
, fnum_src
, data
, nread
+ start
,
537 ret
= cli_write(cli_share_dst
, fnum_dst
, 0, data
,
541 d_fprintf(stderr
, "Error writing file: %s\n",
542 cli_errstr(cli_share_dst
));
543 nt_status
= cli_nt_error(cli_share_dst
);
551 if (!is_file
&& !cli_chkpath(cli_share_dst
, dst_name
)) {
554 DEBUGADD(3,("creating dir %s on the destination server\n",
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_fprintf(stderr
, "cannot check for directory %s: %s\n",
565 dst_name
, cli_errstr(cli_share_dst
));
572 if (!cli_close(cli_share_src
, fnum_src
)) {
573 d_fprintf(stderr
, "could not close file on originating server: %s\n",
574 cli_errstr(cli_share_src
));
575 nt_status
= cli_nt_error(cli_share_src
);
579 if (is_file
&& !cli_close(cli_share_dst
, fnum_dst
)) {
580 d_fprintf(stderr
, "could not close file on destination server: %s\n",
581 cli_errstr(cli_share_dst
));
582 nt_status
= cli_nt_error(cli_share_dst
);
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
))
594 nt_status
= NT_STATUS_OK
;
600 cli_close(cli_share_src
, fnum_src
);
603 cli_close(cli_share_dst
, fnum_dst
);
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
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.
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
;
643 /* scroll through the file until we have the part
644 beyond archi_table.short_archi */
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
))
669 nt_status
= NT_STATUS_OK
;
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
;
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",
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_fprintf(stderr
, "cannot check %s: %s\n",
708 dir
, cli_errstr(cli_share
));
712 nt_status
= NT_STATUS_OK
;
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
;
742 fstring driverpath
= "";
743 fstring datafile
= "";
744 fstring configfile
= "";
745 fstring helpfile
= "";
746 fstring dependentfiles
= "";
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
);
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
))
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
))
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
))
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
))
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
))
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
810 * this greatly reduces the complexitiy of the migrate-functions.
814 static BOOL
net_spoolss_enum_printers(struct rpc_pipe_client
*pipe_hnd
,
819 uint32
*num_printers
,
820 PRINTER_INFO_CTR
*ctr
)
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
));
836 static BOOL
net_spoolss_open_printer_ex(struct rpc_pipe_client
*pipe_hnd
,
838 const char *printername
,
839 uint32 access_required
,
840 const char *username
,
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
));
856 result
= rpccli_spoolss_open_printer_ex(pipe_hnd
, mem_ctx
, printername2
,
858 servername
, username
, hnd
);
860 /* be more verbose */
861 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ACCESS_DENIED
)) {
862 d_fprintf(stderr
, "no access to printer [%s] on [%s] for user [%s] granted\n",
863 printername2
, servername
, username
);
867 if (!W_ERROR_IS_OK(result
)) {
868 d_fprintf(stderr
, "cannot open printer %s on server %s: %s\n",
869 printername2
, servername
, dos_errstr(result
));
873 DEBUG(2,("got printer handle for printer: %s, server: %s\n",
874 printername2
, servername
));
879 static BOOL
net_spoolss_getprinter(struct rpc_pipe_client
*pipe_hnd
,
883 PRINTER_INFO_CTR
*ctr
)
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
));
898 static BOOL
net_spoolss_setprinter(struct rpc_pipe_client
*pipe_hnd
,
902 PRINTER_INFO_CTR
*ctr
)
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
));
918 static BOOL
net_spoolss_setprinterdata(struct rpc_pipe_client
*pipe_hnd
,
921 REGISTRY_VALUE
*value
)
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
));
937 static BOOL
net_spoolss_enumprinterkey(struct rpc_pipe_client
*pipe_hnd
,
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
));
956 static BOOL
net_spoolss_enumprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
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
));
977 static BOOL
net_spoolss_setprinterdataex(struct rpc_pipe_client
*pipe_hnd
,
981 REGISTRY_VALUE
*value
)
985 /* setprinterdataex call */
986 result
= rpccli_spoolss_setprinterdataex(pipe_hnd
, mem_ctx
, hnd
,
989 if (!W_ERROR_IS_OK(result
)) {
990 printf("could not set printerdataex: %s\n", dos_errstr(result
));
997 static BOOL
net_spoolss_enumforms(struct rpc_pipe_client
*pipe_hnd
,
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
));
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
)
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
));
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
)
1047 /* getprinterdriver call */
1048 result
= rpccli_spoolss_getprinterdriver(
1049 pipe_hnd
, mem_ctx
, hnd
, level
,
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
));
1066 static BOOL
net_spoolss_addprinterdriver(struct rpc_pipe_client
*pipe_hnd
,
1067 TALLOC_CTX
*mem_ctx
, uint32 level
,
1068 PRINTER_DRIVER_CTR
*ctr
)
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");
1080 if (!W_ERROR_IS_OK(result
)) {
1081 printf("cannot add driver: %s\n", dos_errstr(result
));
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
,
1098 uint32
*num_printers
,
1099 PRINTER_INFO_CTR
*ctr
)
1104 /* no arguments given, enumerate all printers */
1107 if (!net_spoolss_enum_printers(pipe_hnd
, mem_ctx
, NULL
,
1108 PRINTER_ENUM_LOCAL
|PRINTER_ENUM_SHARED
,
1109 level
, num_printers
, ctr
))
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
))
1121 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, ctr
)) {
1122 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd
);
1126 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd
);
1131 DEBUG(3,("got %d printers\n", *num_printers
));
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
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
,
1161 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1162 uint32 i
, num_printers
;
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
))
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
;
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
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
,
1211 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1214 PRINTER_DRIVER_CTR drv_ctr_enum
;
1217 ZERO_STRUCT(drv_ctr_enum
);
1219 printf("listing printer-drivers\n");
1221 for (i
=0; archi_table
[i
].long_archi
!=NULL
; i
++) {
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
;
1234 if (num_drivers
== 0) {
1235 d_printf ("no drivers found on server for architecture: [%s].\n",
1236 archi_table
[i
].long_archi
);
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
;
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
1267 * @return Normal NTSTATUS return.
1270 static NTSTATUS
rpc_printer_publish_internals_args(struct rpc_pipe_client
*pipe_hnd
,
1271 TALLOC_CTX
*mem_ctx
,
1276 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1277 uint32 i
, num_printers
;
1279 pstring printername
, sharename
;
1280 PRINTER_INFO_CTR ctr
, ctr_pub
;
1282 BOOL got_hnd
= False
;
1284 const char *action_str
;
1286 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &ctr
))
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
))
1304 /* check for existing dst printer */
1305 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &ctr_pub
))
1308 /* check action and set string */
1310 case SPOOL_DS_PUBLISH
:
1311 action_str
= "published";
1313 case SPOOL_DS_UPDATE
:
1314 action_str
= "updated";
1316 case SPOOL_DS_UNPUBLISH
:
1317 action_str
= "unpublished";
1320 action_str
= "unknown action";
1321 printf("unkown action: %d\n", action
);
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
));
1333 printf("successfully %s printer %s in Active Directory\n", action_str
, sharename
);
1336 nt_status
= NT_STATUS_OK
;
1340 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd
);
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
,
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
,
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
,
1375 return rpc_printer_publish_internals_args(pipe_hnd
, mem_ctx
, argc
, argv
, SPOOL_DS_UPDATE
);
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
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
,
1402 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1403 uint32 i
, num_printers
;
1405 pstring printername
, sharename
;
1407 PRINTER_INFO_CTR ctr
, ctr_pub
;
1409 BOOL got_hnd
= False
;
1412 if (!get_printer_info(pipe_hnd
, mem_ctx
, 2, argc
, argv
, &num_printers
, &ctr
))
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
))
1432 /* check for existing dst printer */
1433 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd
, level
, &ctr_pub
))
1436 rpcstr_pull(guid
, ctr_pub
.printers_7
->guid
.buffer
, sizeof(guid
), -1, STR_TERMINATE
);
1438 state
= ctr_pub
.printers_7
->action
;
1440 case SPOOL_DS_PUBLISH
:
1441 printf("printer [%s] is published", sharename
);
1443 printf(", guid: %s", guid
);
1446 case SPOOL_DS_UNPUBLISH
:
1447 printf("printer [%s] is unpublished\n", sharename
);
1449 case SPOOL_DS_UPDATE
:
1450 printf("printer [%s] is currently updating\n", sharename
);
1453 printf("unkown state: %d\n", state
);
1458 nt_status
= NT_STATUS_OK
;
1462 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd
);
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
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
,
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
;
1496 uint32 num_printers
;
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
))
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
;
1522 if (!num_printers
) {
1523 printf ("no printers found on server.\n");
1524 nt_status
= NT_STATUS_OK
;
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
))
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
))
1563 /* check for existing dst printer */
1564 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &ctr_dst
))
1567 /* check for existing src printer */
1568 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, 3, &ctr_src
))
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
);
1578 display_sec_desc(ctr_dst
.printers_2
->secdesc
);
1580 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &ctr_dst
))
1583 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1586 /* close printer handles here */
1588 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
1589 got_hnd_src
= False
;
1593 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
1594 got_hnd_dst
= False
;
1599 nt_status
= NT_STATUS_OK
;
1604 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
1608 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
1612 cli_shutdown(cli_dst
);
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
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
,
1641 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1644 uint32 num_printers
;
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
;
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
))
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
;
1672 if (!num_printers
) {
1673 printf ("no printers found on server.\n");
1674 nt_status
= NT_STATUS_OK
;
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
))
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
))
1711 /* check for existing dst printer */
1712 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, level
, &ctr_dst
))
1715 /* finally migrate forms */
1716 if (!net_spoolss_enumforms(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &num_forms
, &forms
))
1719 DEBUG(1,("got %d forms for printer\n", num_forms
));
1722 for (f
= 0; f
< num_forms
; f
++) {
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
)
1732 if (forms
[f
].name
.buffer
)
1733 rpcstr_pull(form_name
, forms
[f
].name
.buffer
,
1734 sizeof(form_name
), -1, STR_TERMINATE
);
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
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",
1761 DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name
));
1765 /* close printer handles here */
1767 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
1768 got_hnd_src
= False
;
1772 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
1773 got_hnd_dst
= False
;
1777 nt_status
= NT_STATUS_OK
;
1782 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
1785 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
1788 cli_shutdown(cli_dst
);
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
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
,
1817 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
1819 uint32 num_printers
;
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
))
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
))
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
))
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
;
1872 if (num_printers
== 0) {
1873 printf ("no printers found on server.\n");
1874 nt_status
= NT_STATUS_OK
;
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
))
1901 /* check for existing dst printer */
1902 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
, 2, &info_ctr_dst
))
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
))
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
++) {
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
))
1925 rpcstr_pull(drivername
, drv_ctr_src
.info3
->name
.buffer
,
1926 sizeof(drivername
), -1, STR_TERMINATE
);
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
))
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
,
1942 if (!NT_STATUS_IS_OK(nt_status
))
1947 if (!net_spoolss_addprinterdriver(pipe_hnd_dst
, mem_ctx
, level
, &drv_ctr_src
)) {
1948 nt_status
= NT_STATUS_UNSUCCESSFUL
;
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",
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
;
1971 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1972 drivername
, printername
));
1976 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
1977 got_hnd_dst
= False
;
1982 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
1983 got_hnd_src
= False
;
1987 nt_status
= NT_STATUS_OK
;
1992 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
1995 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_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
);
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
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
,
2037 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2038 uint32 i
= 0, num_printers
;
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
))
2057 if (!get_printer_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &num_printers
, &ctr_enum
)) {
2058 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2062 if (!num_printers
) {
2063 printf ("no printers found on server.\n");
2064 nt_status
= NT_STATUS_OK
;
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
));
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");
2099 DEBUG(1,("printer already exists: %s\n", sharename
));
2100 /* close printer handles here */
2102 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
2103 got_hnd_src
= False
;
2107 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
2108 got_hnd_dst
= False
;
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
))
2124 /* getprinter on the src server */
2125 if (!net_spoolss_getprinter(pipe_hnd
, mem_ctx
, &hnd_src
, level
, &ctr_src
))
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_fprintf (stderr
, "printer [%s] already exists.\n", printername
);
2139 d_fprintf (stderr
, "could not create printer [%s]\n", printername
);
2143 /* close printer handles here */
2145 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
2146 got_hnd_src
= False
;
2150 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
2151 got_hnd_dst
= False
;
2155 nt_status
= NT_STATUS_OK
;
2159 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
2162 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
2165 cli_shutdown(cli_dst
);
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
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
,
2196 /* FIXME: Here the nightmare begins */
2199 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2200 uint32 i
= 0, p
= 0, j
= 0;
2201 uint32 num_printers
, val_needed
, data_needed
;
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
;
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
))
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
;
2232 if (!num_printers
) {
2233 printf ("no printers found on server.\n");
2234 nt_status
= NT_STATUS_OK
;
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
))
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
))
2275 /* check for existing dst printer */
2276 if (!net_spoolss_getprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
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
))
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
;
2319 if (asprintf(&devicename
, "\\\\%s\\%s", longname
,
2321 nt_status
= NT_STATUS_NO_MEMORY
;
2325 init_unistr(&ctr_dst
.printers_2
->devmode
->devicename
,
2328 if (!net_spoolss_setprinter(pipe_hnd_dst
, mem_ctx
, &hnd_dst
,
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
)) {
2361 display_reg_value(SPOOL_PRINTERDATA_KEY
, value
);
2364 if (!net_spoolss_setprinterdata(pipe_hnd_dst
, mem_ctx
,
2368 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
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");
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
)
2394 while (*curkey
!= 0) {
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,
2410 for (j
=0; j
< reg_ctr
->num_values
; j
++) {
2412 REGISTRY_VALUE value
;
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
;
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
)) {
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
;
2451 init_unistr2(&data
, url
, UNI_STR_TERMINATE
);
2452 fstrcpy(value
.valuename
, SPOOL_REG_URL
);
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
);
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
,
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
]))
2492 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2493 subkey
, reg_ctr
->values
[j
]->valuename
));
2497 TALLOC_FREE( reg_ctr
);
2502 /* close printer handles here */
2504 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
2505 got_hnd_src
= False
;
2509 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
2510 got_hnd_dst
= False
;
2515 nt_status
= NT_STATUS_OK
;
2518 SAFE_FREE(devicename
);
2520 SAFE_FREE(unc_name
);
2523 rpccli_spoolss_close_printer(pipe_hnd
, mem_ctx
, &hnd_src
);
2526 rpccli_spoolss_close_printer(pipe_hnd_dst
, mem_ctx
, &hnd_dst
);
2529 cli_shutdown(cli_dst
);