s3: Fix Coverity ID 2727 to 2740 -- UNINIT
[samba.git] / source3 / utils / net_rpc_printer.c
1 /*
2    Samba Unix/Linux SMB client library
3    Distributed SMB/CIFS Server Management Utility
4    Copyright (C) 2004,2009 Guenther Deschner (gd@samba.org)
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
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.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "includes.h"
20 #include "system/filesys.h"
21 #include "utils/net.h"
22 #include "rpc_client/rpc_client.h"
23 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
24 #include "rpc_client/cli_spoolss.h"
25 #include "rpc_client/init_spoolss.h"
26 #include "nt_printing.h"
27 #include "registry/reg_objects.h"
28 #include "../libcli/security/security.h"
29 #include "../libcli/registry/util_reg.h"
30 #include "libsmb/libsmb.h"
31
32 /* support itanium as well */
33 static const struct print_architecture_table_node archi_table[]= {
34
35         {"Windows 4.0",          "WIN40",       0 },
36         {"Windows NT x86",       "W32X86",      2 },
37         {"Windows NT x86",       "W32X86",      3 },
38         {"Windows NT R4000",     "W32MIPS",     2 },
39         {"Windows NT Alpha_AXP", "W32ALPHA",    2 },
40         {"Windows NT PowerPC",   "W32PPC",      2 },
41         {"Windows IA64",         "IA64",        3 },
42         {"Windows x64",          "x64",         3 },
43         {NULL,                   "",            -1 }
44 };
45
46
47 /**
48  * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
49  * It is here for debugging purpose and should be removed later on.
50  **/
51
52 /****************************************************************************
53  Printer info level 3 display function.
54 ****************************************************************************/
55
56 static void display_print_driver3(struct spoolss_DriverInfo3 *r)
57 {
58         int i;
59
60         if (!r) {
61                 return;
62         }
63
64         printf(_("Printer Driver Info 3:\n"));
65         printf(_("\tVersion: [%x]\n"), r->version);
66         printf(_("\tDriver Name: [%s]\n"), r->driver_name);
67         printf(_("\tArchitecture: [%s]\n"), r->architecture);
68         printf(_("\tDriver Path: [%s]\n"), r->driver_path);
69         printf(_("\tDatafile: [%s]\n"), r->data_file);
70         printf(_("\tConfigfile: [%s]\n\n"), r->config_file);
71         printf(_("\tHelpfile: [%s]\n\n"), r->help_file);
72
73         for (i=0; r->dependent_files && r->dependent_files[i] != NULL; i++) {
74                 printf(_("\tDependentfiles: [%s]\n"), r->dependent_files[i]);
75         }
76
77         printf("\n");
78
79         printf(_("\tMonitorname: [%s]\n"), r->monitor_name);
80         printf(_("\tDefaultdatatype: [%s]\n\n"), r->default_datatype);
81 }
82
83 static void display_reg_value(const char *subkey, struct regval_blob *value)
84 {
85         const char *text;
86         DATA_BLOB blob;
87
88         switch(regval_type(value)) {
89         case REG_DWORD:
90                 d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey,
91                         regval_name(value), *((uint32_t *) regval_data_p(value)));
92                 break;
93
94         case REG_SZ:
95                 blob = data_blob_const(regval_data_p(value), regval_size(value));
96                 pull_reg_sz(talloc_tos(), &blob, &text);
97                 if (!text) {
98                         break;
99                 }
100                 d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, regval_name(value),
101                          text);
102                 break;
103
104         case REG_BINARY:
105                 d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
106                            "displayed\n"),
107                          subkey, regval_name(value));
108                 break;
109
110         case REG_MULTI_SZ: {
111                 uint32_t i;
112                 const char **values;
113                 blob = data_blob_const(regval_data_p(value), regval_size(value));
114
115                 if (!pull_reg_multi_sz(NULL, &blob, &values)) {
116                         d_printf("pull_reg_multi_sz failed\n");
117                         break;
118                 }
119
120                 printf("%s: REG_MULTI_SZ: \n", regval_name(value));
121                 for (i=0; values[i] != NULL; i++) {
122                         d_printf("%s\n", values[i]);
123                 }
124                 TALLOC_FREE(values);
125                 break;
126         }
127
128         default:
129                 d_printf(_("\t%s: unknown type %d\n"), regval_name(value),
130                          regval_type(value));
131         }
132
133 }
134
135 /**
136  * Copies ACLs, DOS-attributes and timestamps from one
137  * file or directory from one connected share to another connected share
138  *
139  * @param c                     A net_context structure
140  * @param mem_ctx               A talloc-context
141  * @param cli_share_src         A connected cli_state
142  * @param cli_share_dst         A connected cli_state
143  * @param src_file              The source file-name
144  * @param dst_file              The destination file-name
145  * @param copy_acls             Whether to copy acls
146  * @param copy_attrs            Whether to copy DOS attributes
147  * @param copy_timestamps       Whether to preserve timestamps
148  * @param is_file               Whether this file is a file or a dir
149  *
150  * @return Normal NTSTATUS return.
151  **/
152
153 NTSTATUS net_copy_fileattr(struct net_context *c,
154                   TALLOC_CTX *mem_ctx,
155                   struct cli_state *cli_share_src,
156                   struct cli_state *cli_share_dst,
157                   const char *src_name, const char *dst_name,
158                   bool copy_acls, bool copy_attrs,
159                   bool copy_timestamps, bool is_file)
160 {
161         NTSTATUS nt_status;
162         uint16_t fnum_src = 0;
163         uint16_t fnum_dst = 0;
164         struct security_descriptor *sd = NULL;
165         uint16_t attr;
166         time_t f_atime, f_ctime, f_mtime;
167
168         if (!copy_timestamps && !copy_acls && !copy_attrs)
169                 return NT_STATUS_OK;
170
171         /* open file/dir on the originating server */
172
173         DEBUGADD(3,("opening %s %s on originating server\n",
174                 is_file?"file":"dir", src_name));
175
176         nt_status = cli_ntcreate(cli_share_src, src_name, 0,
177                                  READ_CONTROL_ACCESS, 0,
178                                  FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
179                                  0x0, 0x0, &fnum_src);
180         if (!NT_STATUS_IS_OK(nt_status)) {
181                 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
182                         is_file?"file":"dir", src_name, nt_errstr(nt_status)));
183                 goto out;
184         }
185
186         if (copy_acls) {
187                 /* get the security descriptor */
188                 nt_status = cli_query_secdesc(cli_share_src, fnum_src,
189                                               mem_ctx, &sd);
190                 if (!NT_STATUS_IS_OK(nt_status)) {
191                         DEBUG(0,("failed to get security descriptor: %s\n",
192                                  nt_errstr(nt_status)));
193                         goto out;
194                 }
195
196                 if (c->opt_verbose && DEBUGLEVEL >= 3)
197                         display_sec_desc(sd);
198         }
199
200         if (copy_attrs || copy_timestamps) {
201
202                 /* get file attributes */
203                 nt_status = cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
204                                       &f_ctime, &f_atime, &f_mtime);
205                 if (!NT_STATUS_IS_OK(nt_status)) {
206                         DEBUG(0,("failed to get file-attrs: %s\n",
207                                 nt_errstr(nt_status)));
208                         goto out;
209                 }
210         }
211
212         /* open the file/dir on the destination server */
213         nt_status = cli_ntcreate(cli_share_dst, dst_name, 0,
214                                  WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
215                                  FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
216                                  0x0, 0x0, &fnum_dst);
217         if (!NT_STATUS_IS_OK(nt_status)) {
218                 DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
219                         is_file?"file":"dir", dst_name, nt_errstr(nt_status)));
220                 goto out;
221         }
222
223         if (copy_timestamps) {
224                 /* set timestamps */
225                 nt_status = cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime);
226                 if (!NT_STATUS_IS_OK(nt_status)) {
227                         DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
228                                 nt_errstr(nt_status)));
229                         goto out;
230                 }
231         }
232
233         if (copy_acls) {
234                 /* set acls */
235                 nt_status = cli_set_secdesc(cli_share_dst, fnum_dst, sd);
236                 if (!NT_STATUS_IS_OK(nt_status)) {
237                         DEBUG(0, ("could not set secdesc on %s: %s\n",
238                                   dst_name, nt_errstr(nt_status)));
239                         goto out;
240                 }
241         }
242
243         if (copy_attrs) {
244                 /* set attrs */
245                 nt_status = cli_setatr(cli_share_dst, dst_name, attr, 0);
246                 if (!NT_STATUS_IS_OK(nt_status)) {
247                         DEBUG(0,("failed to set file-attrs: %s\n",
248                                 nt_errstr(nt_status)));
249                         goto out;
250                 }
251         }
252
253
254         /* closing files */
255         nt_status = cli_close(cli_share_src, fnum_src);
256         if (!NT_STATUS_IS_OK(nt_status)) {
257                 d_fprintf(stderr,
258                         _("could not close %s on originating server: %s\n"),
259                         is_file?"file":"dir", nt_errstr(nt_status));
260                 goto out;
261         }
262
263         nt_status = cli_close(cli_share_dst, fnum_dst);
264         if (!NT_STATUS_IS_OK(nt_status)) {
265                 d_fprintf(stderr,
266                         _("could not close %s on destination server: %s\n"),
267                         is_file?"file":"dir", nt_errstr(nt_status));
268                 goto out;
269         }
270
271
272         nt_status = NT_STATUS_OK;
273
274 out:
275
276         /* cleaning up */
277         if (fnum_src)
278                 cli_close(cli_share_src, fnum_src);
279
280         if (fnum_dst)
281                 cli_close(cli_share_dst, fnum_dst);
282
283         return nt_status;
284 }
285
286 /**
287  * Copy a file or directory from a connected share to another connected share
288  *
289  * @param c                     A net_context structure
290  * @param mem_ctx               A talloc-context
291  * @param cli_share_src         A connected cli_state
292  * @param cli_share_dst         A connected cli_state
293  * @param src_file              The source file-name
294  * @param dst_file              The destination file-name
295  * @param copy_acls             Whether to copy acls
296  * @param copy_attrs            Whether to copy DOS attributes
297  * @param copy_timestamps       Whether to preserve timestamps
298  * @param is_file               Whether this file is a file or a dir
299  *
300  * @return Normal NTSTATUS return.
301  **/
302
303 NTSTATUS net_copy_file(struct net_context *c,
304                        TALLOC_CTX *mem_ctx,
305                        struct cli_state *cli_share_src,
306                        struct cli_state *cli_share_dst,
307                        const char *src_name, const char *dst_name,
308                        bool copy_acls, bool copy_attrs,
309                        bool copy_timestamps, bool is_file)
310 {
311         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
312         uint16_t fnum_src = 0;
313         uint16_t fnum_dst = 0;
314         static int io_bufsize = 64512;
315         int read_size = io_bufsize;
316         char *data = NULL;
317         off_t nread = 0;
318
319
320         if (!src_name || !dst_name)
321                 goto out;
322
323         if (cli_share_src == NULL || cli_share_dst == NULL)
324                 goto out;
325
326         /* open on the originating server */
327         DEBUGADD(3,("opening %s %s on originating server\n",
328                 is_file ? "file":"dir", src_name));
329         if (is_file)
330                 nt_status = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE, &fnum_src);
331         else
332                 nt_status = cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
333                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src);
334
335         if (!NT_STATUS_IS_OK(nt_status)) {
336                 DEBUGADD(0,("cannot open %s %s on originating server %s\n",
337                         is_file ? "file":"dir",
338                         src_name, nt_errstr(nt_status)));
339                 goto out;
340         }
341
342
343         if (is_file) {
344
345                 /* open file on the destination server */
346                 DEBUGADD(3,("opening file %s on destination server\n", dst_name));
347                 nt_status = cli_open(cli_share_dst, dst_name,
348                                 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum_dst);
349
350                 if (!NT_STATUS_IS_OK(nt_status)) {
351                         DEBUGADD(1,("cannot create file %s on destination server: %s\n", 
352                                 dst_name, nt_errstr(nt_status)));
353                         goto out;
354                 }
355
356                 /* allocate memory */
357                 if (!(data = (char *)SMB_MALLOC(read_size))) {
358                         d_fprintf(stderr, _("malloc fail for size %d\n"),
359                                   read_size);
360                         nt_status = NT_STATUS_NO_MEMORY;
361                         goto out;
362                 }
363
364         }
365
366
367         if (c->opt_verbose) {
368
369                 d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
370                            "%s ACLs and %s DOS Attributes %s\n"),
371                         cli_state_remote_name(cli_share_src),
372                         cli_share_src->share, src_name,
373                         cli_state_remote_name(cli_share_dst),
374                         cli_share_dst->share, dst_name,
375                         copy_acls ?  _("with") : _("without"),
376                         copy_attrs ? _("with") : _("without"),
377                         copy_timestamps ? _("(preserving timestamps)") : "" );
378         }
379
380
381         while (is_file) {
382
383                 /* copying file */
384                 size_t n;
385
386                 nt_status = cli_read(cli_share_src, fnum_src, data, nread,
387                                      read_size, &n);
388                 if (!NT_STATUS_IS_OK(nt_status)) {
389                         d_fprintf(stderr,
390                                   _("Error reading file [\\\\%s\\%s%s]: %s\n"),
391                                   cli_state_remote_name(cli_share_src),
392                                   cli_share_src->share,
393                                   src_name, nt_errstr(nt_status));
394                         goto out;
395                 }
396
397                 if (n == 0)
398                         break;
399
400                 nt_status = cli_writeall(cli_share_dst, fnum_dst, 0,
401                                          (uint8_t *)data, nread, n, NULL);
402
403                 if (!NT_STATUS_IS_OK(nt_status)) {
404                         d_fprintf(stderr,
405                                   _("Error writing file: [\\\\%s\\%s%s]: %s\n"),
406                                   cli_state_remote_name(cli_share_dst),
407                                   cli_share_dst->share,
408                                   dst_name, nt_errstr(nt_status));
409                         goto out;
410                 }
411
412                 nread += n;
413         }
414
415
416         if (!is_file && !NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
417
418                 /* creating dir */
419                 DEBUGADD(3,("creating dir %s on the destination server\n",
420                         dst_name));
421
422                 nt_status = cli_mkdir(cli_share_dst, dst_name);
423                 if (!NT_STATUS_IS_OK(nt_status)) {
424                         DEBUG(0,("cannot create directory %s: %s\n",
425                                 dst_name, nt_errstr(nt_status)));
426                         nt_status = NT_STATUS_NO_SUCH_FILE;
427                 }
428
429
430                 nt_status = cli_chkpath(cli_share_dst, dst_name);
431                 if (!NT_STATUS_IS_OK(nt_status)) {
432                         d_fprintf(stderr,
433                                 _("cannot check for directory %s: %s\n"),
434                                 dst_name, nt_errstr(nt_status));
435                         goto out;
436                 }
437         }
438
439
440         /* closing files */
441         nt_status = cli_close(cli_share_src, fnum_src);
442         if (!NT_STATUS_IS_OK(nt_status)) {
443                 d_fprintf(stderr,
444                         _("could not close file on originating server: %s\n"),
445                         nt_errstr(nt_status));
446                 goto out;
447         }
448
449         if (is_file) {
450                 nt_status = cli_close(cli_share_dst, fnum_dst);
451                 if (!NT_STATUS_IS_OK(nt_status)) {
452                         d_fprintf(stderr,
453                         _("could not close file on destination server: %s\n"),
454                         nt_errstr(nt_status));
455                         goto out;
456                 }
457         }
458
459         /* possibly we have to copy some file-attributes / acls / sd */
460         nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
461                                       src_name, dst_name, copy_acls,
462                                       copy_attrs, copy_timestamps, is_file);
463         if (!NT_STATUS_IS_OK(nt_status))
464                 goto out;
465
466
467         nt_status = NT_STATUS_OK;
468
469 out:
470
471         /* cleaning up */
472         if (fnum_src)
473                 cli_close(cli_share_src, fnum_src);
474
475         if (fnum_dst)
476                 cli_close(cli_share_dst, fnum_dst);
477
478         SAFE_FREE(data);
479
480         return nt_status;
481 }
482
483 /**
484  * Copy a driverfile from on connected share to another connected share
485  * This silently assumes that a driver-file is picked up from
486  *
487  *      \\src_server\print$\{arch}\{version}\file
488  *
489  * and copied to
490  *
491  *      \\dst_server\print$\{arch}\file
492  *
493  * to be added via setdriver-calls later.
494  * @param c                     A net_context structure
495  * @param mem_ctx               A talloc-context
496  * @param cli_share_src         A cli_state connected to source print$-share
497  * @param cli_share_dst         A cli_state connected to destination print$-share
498  * @param file                  The file-name to be copied
499  * @param short_archi           The name of the driver-architecture (short form)
500  *
501  * @return Normal NTSTATUS return.
502  **/
503
504 static NTSTATUS net_copy_driverfile(struct net_context *c,
505                                     TALLOC_CTX *mem_ctx,
506                                     struct cli_state *cli_share_src,
507                                     struct cli_state *cli_share_dst,
508                                     const char *file, const char *short_archi) {
509
510         const char *p;
511         char *src_name;
512         char *dst_name;
513         char *version = NULL;
514         char *filename = NULL;
515         char *tok;
516
517         if (!file) {
518                 return NT_STATUS_OK;
519         }
520
521         /* scroll through the file until we have the part
522            beyond archi_table.short_archi */
523         p = file;
524         while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
525                 if (strequal(tok, short_archi)) {
526                         next_token_talloc(mem_ctx, &p, &version, "\\");
527                         next_token_talloc(mem_ctx, &p, &filename, "\\");
528                 }
529         }
530
531         if (version == NULL || filename == NULL) {
532                 return NT_STATUS_UNSUCCESSFUL;
533         }
534
535         /* build source file name */
536         src_name = talloc_asprintf(mem_ctx, "\\%s\\%s\\%s",
537                                    short_archi, version, filename);
538         if (src_name == NULL) {
539                 return NT_STATUS_NO_MEMORY;
540         }
541
542         /* create destination file name */
543         dst_name = talloc_asprintf(mem_ctx, "\\%s\\%s", short_archi, filename);
544         if (dst_name == NULL) {
545                 return NT_STATUS_NO_MEMORY;
546         }
547
548
549         /* finally copy the file */
550         return net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
551                              src_name, dst_name, false, false, false, true);
552 }
553
554 /**
555  * Check for existing Architecture directory on a given server
556  *
557  * @param cli_share             A cli_state connected to a print$-share
558  * @param short_archi           The Architecture for the print-driver
559  *
560  * @return Normal NTSTATUS return.
561  **/
562
563 static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
564 {
565
566         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
567         char *dir;
568
569         if (asprintf(&dir, "\\%s", short_archi) < 0) {
570                 return NT_STATUS_NO_MEMORY;
571         }
572
573         DEBUG(10,("creating print-driver dir for architecture: %s\n",
574                 short_archi));
575
576         nt_status = cli_mkdir(cli_share, dir);
577         if (!NT_STATUS_IS_OK(nt_status)) {
578                 DEBUG(1,("cannot create directory %s: %s\n",
579                          dir, nt_errstr(nt_status)));
580         }
581
582         nt_status = cli_chkpath(cli_share, dir);
583         if (!NT_STATUS_IS_OK(nt_status)) {
584                 d_fprintf(stderr, _("cannot check %s: %s\n"),
585                         dir, nt_errstr(nt_status));
586                 goto out;
587         }
588
589         nt_status = NT_STATUS_OK;
590
591 out:
592         SAFE_FREE(dir);
593         return nt_status;
594 }
595
596 /**
597  * Copy a print-driver (level 3) from one connected print$-share to another
598  * connected print$-share
599  *
600  * @param c                     A net_context structure
601  * @param mem_ctx               A talloc-context
602  * @param cli_share_src         A cli_state connected to a print$-share
603  * @param cli_share_dst         A cli_state connected to a print$-share
604  * @param short_archi           The Architecture for the print-driver
605  * @param i1                    The DRIVER_INFO_3-struct
606  *
607  * @return Normal NTSTATUS return.
608  **/
609
610 static NTSTATUS copy_print_driver_3(struct net_context *c,
611                     TALLOC_CTX *mem_ctx,
612                     struct cli_state *cli_share_src,
613                     struct cli_state *cli_share_dst,
614                     const char *short_archi,
615                     struct spoolss_DriverInfo3 *r)
616 {
617         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
618         int i;
619
620         if (r == NULL) {
621                 return nt_status;
622         }
623
624         if (c->opt_verbose)
625                 d_printf(_("copying driver: [%s], for architecture: [%s], "
626                            "version: [%d]\n"),
627                           r->driver_name, short_archi, r->version);
628
629         nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
630                 r->driver_path, short_archi);
631         if (!NT_STATUS_IS_OK(nt_status))
632                 return nt_status;
633
634         nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
635                 r->data_file, short_archi);
636         if (!NT_STATUS_IS_OK(nt_status))
637                 return nt_status;
638
639         nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
640                 r->config_file, short_archi);
641         if (!NT_STATUS_IS_OK(nt_status))
642                 return nt_status;
643
644         nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
645                 r->help_file, short_archi);
646         if (!NT_STATUS_IS_OK(nt_status))
647                 return nt_status;
648
649         for (i=0; r->dependent_files[i] != NULL; i++) {
650
651                 nt_status = net_copy_driverfile(c, mem_ctx,
652                                 cli_share_src, cli_share_dst,
653                                 r->dependent_files[i], short_archi);
654                 if (!NT_STATUS_IS_OK(nt_status)) {
655                         return nt_status;
656                 }
657         }
658
659         return NT_STATUS_OK;
660 }
661
662 /**
663  * net_spoolss-functions
664  * =====================
665  *
666  * the net_spoolss-functions aim to simplify spoolss-client-functions
667  * required during the migration-process wrt buffer-sizes, returned
668  * error-codes, etc.
669  *
670  * this greatly reduces the complexitiy of the migrate-functions.
671  *
672  **/
673
674 static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
675                                         TALLOC_CTX *mem_ctx,
676                                         char *name,
677                                         uint32_t flags,
678                                         uint32_t level,
679                                         uint32_t *num_printers,
680                                         union spoolss_PrinterInfo **info)
681 {
682         WERROR result;
683
684         /* enum printers */
685
686         result = rpccli_spoolss_enumprinters(pipe_hnd, mem_ctx,
687                                              flags,
688                                              name,
689                                              level,
690                                              0,
691                                              num_printers,
692                                              info);
693         if (!W_ERROR_IS_OK(result)) {
694                 printf(_("cannot enum printers: %s\n"), win_errstr(result));
695                 return false;
696         }
697
698         return true;
699 }
700
701 static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
702                                         TALLOC_CTX *mem_ctx,
703                                         const char *printername,
704                                         uint32_t access_required,
705                                         const char *username,
706                                         struct policy_handle *hnd)
707 {
708         WERROR result;
709         fstring printername2;
710
711         fstrcpy(printername2, pipe_hnd->srv_name_slash);
712         fstrcat(printername2, "\\");
713         fstrcat(printername2, printername);
714
715         DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
716                 pipe_hnd->srv_name_slash, username, printername2, access_required));
717
718         /* open printer */
719         result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
720                                                printername2,
721                                                access_required,
722                                                hnd);
723
724         /* be more verbose */
725         if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
726                 d_fprintf(stderr,
727                         _("no access to printer [%s] on [%s] for user [%s] "
728                           "granted\n"),
729                         printername2, pipe_hnd->srv_name_slash, username);
730                 return false;
731         }
732
733         if (!W_ERROR_IS_OK(result)) {
734                 d_fprintf(stderr,_("cannot open printer %s on server %s: %s\n"),
735                         printername2, pipe_hnd->srv_name_slash, win_errstr(result));
736                 return false;
737         }
738
739         DEBUG(2,("got printer handle for printer: %s, server: %s\n",
740                 printername2, pipe_hnd->srv_name_slash));
741
742         return true;
743 }
744
745 static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
746                                 TALLOC_CTX *mem_ctx,
747                                 struct policy_handle *hnd,
748                                 uint32_t level,
749                                 union spoolss_PrinterInfo *info)
750 {
751         WERROR result;
752
753         /* getprinter call */
754         result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
755                                            hnd,
756                                            level,
757                                            0, /* offered */
758                                            info);
759         if (!W_ERROR_IS_OK(result)) {
760                 printf(_("cannot get printer-info: %s\n"), win_errstr(result));
761                 return false;
762         }
763
764         return true;
765 }
766
767 static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
768                                 TALLOC_CTX *mem_ctx,
769                                 struct policy_handle *hnd,
770                                 uint32_t level,
771                                 union spoolss_PrinterInfo *info)
772 {
773         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
774         WERROR result;
775         NTSTATUS status;
776         struct spoolss_SetPrinterInfoCtr info_ctr;
777         struct spoolss_SetPrinterInfo2 info2;
778         struct spoolss_DevmodeContainer devmode_ctr;
779         struct sec_desc_buf secdesc_ctr;
780
781         ZERO_STRUCT(devmode_ctr);
782         ZERO_STRUCT(secdesc_ctr);
783
784         /* setprinter call */
785
786         info_ctr.level = level;
787         switch (level) {
788         case 0:
789                 info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)
790                         (void *)&info->info0;
791                 break;
792         case 1:
793                 info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)
794                         (void *)&info->info1;
795                 break;
796         case 2:
797                 spoolss_printerinfo2_to_setprinterinfo2(&info->info2, &info2);
798                 info_ctr.info.info2 = &info2;
799                 break;
800         case 3:
801                 info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
802                         (void *)&info->info3;
803                 break;
804         case 4:
805                 info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)
806                         (void *)&info->info4;
807                 break;
808         case 5:
809                 info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)
810                         (void *)&info->info5;
811                 break;
812         case 6:
813                 info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)
814                         (void *)&info->info6;
815                 break;
816         case 7:
817                 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
818                         (void *)&info->info7;
819                 break;
820 #if 0 /* FIXME GD */
821         case 8:
822                 info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)
823                         (void *)&info->info8;
824                 break;
825         case 9:
826                 info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)
827                         (void *)&info->info9;
828                 break;
829 #endif
830         default:
831                 break; /* FIXME */
832         }
833
834         status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
835                                            hnd,
836                                            &info_ctr,
837                                            &devmode_ctr,
838                                            &secdesc_ctr,
839                                            0, /* command */
840                                            &result);
841         if (!NT_STATUS_IS_OK(status)) {
842                 printf(_("cannot set printer-info: %s\n"), nt_errstr(status));
843                 return false;
844         }
845         if (!W_ERROR_IS_OK(result)) {
846                 printf(_("cannot set printer-info: %s\n"), win_errstr(result));
847                 return false;
848         }
849
850         return true;
851 }
852
853
854 static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
855                                        TALLOC_CTX *mem_ctx,
856                                        struct policy_handle *hnd,
857                                        const char *value_name,
858                                        enum winreg_Type type,
859                                        uint8_t *data,
860                                        uint32_t offered)
861 {
862         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
863         WERROR result;
864         NTSTATUS status;
865
866         /* setprinterdata call */
867         status = dcerpc_spoolss_SetPrinterData(b, mem_ctx,
868                                                hnd,
869                                                value_name,
870                                                type,
871                                                data,
872                                                offered,
873                                                &result);
874         if (!NT_STATUS_IS_OK(status)) {
875                 printf (_("unable to set printerdata: %s\n"),
876                         nt_errstr(status));
877                 return false;
878         }
879         if (!W_ERROR_IS_OK(result)) {
880                 printf (_("unable to set printerdata: %s\n"),
881                         win_errstr(result));
882                 return false;
883         }
884
885         return true;
886 }
887
888
889 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
890                                         TALLOC_CTX *mem_ctx,
891                                         struct policy_handle *hnd,
892                                         const char *keyname,
893                                         const char ***keylist)
894 {
895         WERROR result;
896
897         /* enumprinterkey call */
898         result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, 0);
899
900         if (!W_ERROR_IS_OK(result)) {
901                 printf(_("enumprinterkey failed: %s\n"), win_errstr(result));
902                 return false;
903         }
904
905         return true;
906 }
907
908 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
909                                         TALLOC_CTX *mem_ctx,
910                                         uint32_t offered,
911                                         struct policy_handle *hnd,
912                                         const char *keyname,
913                                         uint32_t *count,
914                                         struct spoolss_PrinterEnumValues **info)
915 {
916         WERROR result;
917
918         /* enumprinterdataex call */
919         result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx,
920                                                   hnd,
921                                                   keyname,
922                                                   0, /* offered */
923                                                   count,
924                                                   info);
925
926         if (!W_ERROR_IS_OK(result)) {
927                 printf(_("enumprinterdataex failed: %s\n"), win_errstr(result));
928                 return false;
929         }
930
931         return true;
932 }
933
934
935 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
936                                         TALLOC_CTX *mem_ctx,
937                                         struct policy_handle *hnd,
938                                         const char *keyname,
939                                         struct regval_blob *value)
940 {
941         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
942         WERROR result;
943         NTSTATUS status;
944
945         /* setprinterdataex call */
946         status = dcerpc_spoolss_SetPrinterDataEx(b, mem_ctx,
947                                                  hnd,
948                                                  keyname,
949                                                  regval_name(value),
950                                                  regval_type(value),
951                                                  regval_data_p(value),
952                                                  regval_size(value),
953                                                  &result);
954         if (!NT_STATUS_IS_OK(status)) {
955                 printf(_("could not set printerdataex: %s\n"),
956                        nt_errstr(status));
957                 return false;
958         }
959         if (!W_ERROR_IS_OK(result)) {
960                 printf(_("could not set printerdataex: %s\n"),
961                        win_errstr(result));
962                 return false;
963         }
964
965         return true;
966 }
967
968 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
969                                 TALLOC_CTX *mem_ctx,
970                                 struct policy_handle *hnd,
971                                 int level,
972                                 uint32_t *num_forms,
973                                 union spoolss_FormInfo **forms)
974 {
975         WERROR result;
976
977         /* enumforms call */
978         result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
979                                           hnd,
980                                           level,
981                                           0,
982                                           num_forms,
983                                           forms);
984         if (!W_ERROR_IS_OK(result)) {
985                 printf(_("could not enum forms: %s\n"), win_errstr(result));
986                 return false;
987         }
988
989         return true;
990 }
991
992 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
993                                         TALLOC_CTX *mem_ctx,
994                                         uint32_t level, const char *env,
995                                         uint32_t *count,
996                                         union spoolss_DriverInfo **info)
997 {
998         WERROR result;
999
1000         /* enumprinterdrivers call */
1001         result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
1002                                                    pipe_hnd->srv_name_slash,
1003                                                    env,
1004                                                    level,
1005                                                    0,
1006                                                    count,
1007                                                    info);
1008         if (!W_ERROR_IS_OK(result)) {
1009                 if (W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1010                         printf(_("cannot enum drivers for environment %s: %s\n"), env,
1011                                 win_errstr(result));
1012                         return false;
1013                 } else {
1014                         printf(_("Server does not support environment [%s]\n"),
1015                                 env);
1016                 }
1017         }
1018
1019         return true;
1020 }
1021
1022 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
1023                              TALLOC_CTX *mem_ctx,
1024                              struct policy_handle *hnd, uint32_t level,
1025                              const char *env, int version,
1026                              union spoolss_DriverInfo *info)
1027 {
1028         WERROR result;
1029         uint32_t server_major_version;
1030         uint32_t server_minor_version;
1031
1032         /* getprinterdriver call */
1033         result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
1034                                                   hnd,
1035                                                   env,
1036                                                   level,
1037                                                   0,
1038                                                   version,
1039                                                   2,
1040                                                   info,
1041                                                   &server_major_version,
1042                                                   &server_minor_version);
1043         if (!W_ERROR_IS_OK(result)) {
1044                 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1045                         env, win_errstr(result)));
1046                 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1047                     W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1048                         printf(_("cannot get driver: %s\n"),
1049                                win_errstr(result));
1050                 }
1051                 return false;
1052         }
1053
1054         return true;
1055 }
1056
1057
1058 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1059                              TALLOC_CTX *mem_ctx, uint32_t level,
1060                              union spoolss_DriverInfo *info)
1061 {
1062         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1063         WERROR result;
1064         NTSTATUS status;
1065         struct spoolss_AddDriverInfoCtr info_ctr;
1066
1067         info_ctr.level = level;
1068
1069         switch (level) {
1070         case 2:
1071                 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)
1072                         (void *)&info->info2;
1073                 break;
1074         case 3:
1075                 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)
1076                         (void *)&info->info3;
1077                 break;
1078         default:
1079                 printf(_("unsupported info level: %d\n"), level);
1080                 return false;
1081         }
1082
1083         /* addprinterdriver call */
1084         status = dcerpc_spoolss_AddPrinterDriver(b, mem_ctx,
1085                                                  pipe_hnd->srv_name_slash,
1086                                                  &info_ctr,
1087                                                  &result);
1088         if (!NT_STATUS_IS_OK(status)) {
1089                 printf(_("cannot add driver: %s\n"), nt_errstr(status));
1090                 return false;
1091         }
1092         /* be more verbose */
1093         if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1094                 printf(_("You are not allowed to add drivers\n"));
1095                 return false;
1096         }
1097         if (!W_ERROR_IS_OK(result)) {
1098                 printf(_("cannot add driver: %s\n"), win_errstr(result));
1099                 return false;
1100         }
1101
1102         return true;
1103 }
1104
1105 /**
1106  * abstraction function to get uint32_t num_printers and PRINTER_INFO_CTR ctr
1107  * for a single printer or for all printers depending on argc/argv
1108  **/
1109
1110 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1111                         TALLOC_CTX *mem_ctx,
1112                         int level,
1113                         int argc,
1114                         const char **argv,
1115                         uint32_t *num_printers,
1116                         union spoolss_PrinterInfo **info_p)
1117 {
1118         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1119         struct policy_handle hnd;
1120         WERROR werr;
1121
1122         /* no arguments given, enumerate all printers */
1123         if (argc == 0) {
1124
1125                 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1126                                 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1127                                 level, num_printers, info_p))
1128                         return false;
1129
1130                 goto out;
1131         }
1132
1133         /* argument given, get a single printer by name */
1134         if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1135                                          MAXIMUM_ALLOWED_ACCESS,
1136                                          pipe_hnd->auth->user_name,
1137                                          &hnd))
1138                 return false;
1139
1140         *info_p = talloc_zero(mem_ctx, union spoolss_PrinterInfo);
1141         if (*info_p == NULL) {
1142                 return false;
1143         }
1144
1145         if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1146                 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1147                 return false;
1148         }
1149
1150         dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1151
1152         *num_printers = 1;
1153
1154 out:
1155         DEBUG(3,("got %d printers\n", *num_printers));
1156
1157         return true;
1158
1159 }
1160
1161 /**
1162  * List print-queues (including local printers that are not shared)
1163  *
1164  * All parameters are provided by the run_rpc_command function, except for
1165  * argc, argv which are passed through.
1166  *
1167  * @param c     A net_context structure
1168  * @param domain_sid The domain sid aquired from the remote server
1169  * @param cli A cli_state connected to the server.
1170  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1171  * @param argc  Standard main() style argc
1172  * @param argv  Standard main() style argv.  Initial components are already
1173  *              stripped
1174  *
1175  * @return Normal NTSTATUS return.
1176  **/
1177
1178 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1179                                         const struct dom_sid *domain_sid,
1180                                         const char *domain_name,
1181                                         struct cli_state *cli,
1182                                         struct rpc_pipe_client *pipe_hnd,
1183                                         TALLOC_CTX *mem_ctx,
1184                                         int argc,
1185                                         const char **argv)
1186 {
1187         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1188         uint32_t i, num_printers;
1189         uint32_t level = 2;
1190         const char *printername, *sharename;
1191         union spoolss_PrinterInfo *info;
1192
1193         printf("listing printers\n");
1194
1195         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1196                 return nt_status;
1197
1198         for (i = 0; i < num_printers; i++) {
1199
1200                 /* do some initialization */
1201                 printername = info[i].info2.printername;
1202                 sharename = info[i].info2.sharename;
1203
1204                 if (printername && sharename) {
1205                         d_printf(_("printer %d: %s, shared as: %s\n"),
1206                                 i+1, printername, sharename);
1207                 }
1208         }
1209
1210         return NT_STATUS_OK;
1211 }
1212
1213 /**
1214  * List printer-drivers from a server
1215  *
1216  * All parameters are provided by the run_rpc_command function, except for
1217  * argc, argv which are passed through.
1218  *
1219  * @param c     A net_context structure
1220  * @param domain_sid The domain sid aquired from the remote server
1221  * @param cli A cli_state connected to the server.
1222  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1223  * @param argc  Standard main() style argc
1224  * @param argv  Standard main() style argv.  Initial components are already
1225  *              stripped
1226  *
1227  * @return Normal NTSTATUS return.
1228  **/
1229
1230 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1231                                                 const struct dom_sid *domain_sid,
1232                                                 const char *domain_name,
1233                                                 struct cli_state *cli,
1234                                                 struct rpc_pipe_client *pipe_hnd,
1235                                                 TALLOC_CTX *mem_ctx,
1236                                                 int argc,
1237                                                 const char **argv)
1238 {
1239         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1240         uint32_t i;
1241         uint32_t level = 3;
1242         union spoolss_DriverInfo *info;
1243         int d;
1244
1245         printf(_("listing printer-drivers\n"));
1246
1247         for (i=0; archi_table[i].long_archi!=NULL; i++) {
1248
1249                 uint32_t num_drivers;
1250
1251                 /* enum remote drivers */
1252                 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1253                                 archi_table[i].long_archi,
1254                                 &num_drivers, &info)) {
1255                         nt_status = NT_STATUS_UNSUCCESSFUL;
1256                         goto done;
1257                 }
1258
1259                 if (num_drivers == 0) {
1260                         d_printf(_("no drivers found on server for "
1261                                    "architecture: [%s].\n"),
1262                                 archi_table[i].long_archi);
1263                         continue;
1264                 }
1265
1266                 d_printf(_("got %d printer-drivers for architecture: [%s]\n"),
1267                         num_drivers, archi_table[i].long_archi);
1268
1269
1270                 /* do something for all drivers for architecture */
1271                 for (d = 0; d < num_drivers; d++) {
1272                         display_print_driver3(&info[d].info3);
1273                 }
1274         }
1275
1276         nt_status = NT_STATUS_OK;
1277
1278 done:
1279         return nt_status;
1280
1281 }
1282
1283 /**
1284  * Publish print-queues with args-wrapper
1285  *
1286  * @param cli A cli_state connected to the server.
1287  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1288  * @param argc  Standard main() style argc
1289  * @param argv  Standard main() style argv.  Initial components are already
1290  *              stripped
1291  * @param action
1292  *
1293  * @return Normal NTSTATUS return.
1294  **/
1295
1296 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1297                                         TALLOC_CTX *mem_ctx,
1298                                         int argc,
1299                                         const char **argv,
1300                                         uint32_t action)
1301 {
1302         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1303         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1304         uint32_t i, num_printers;
1305         uint32_t level = 7;
1306         const char *printername, *sharename;
1307         union spoolss_PrinterInfo *info_enum;
1308         union spoolss_PrinterInfo info;
1309         struct spoolss_SetPrinterInfoCtr info_ctr;
1310         struct spoolss_DevmodeContainer devmode_ctr;
1311         struct sec_desc_buf secdesc_ctr;
1312         struct policy_handle hnd = { 0, };
1313         WERROR result;
1314         const char *action_str;
1315
1316         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1317                 return nt_status;
1318
1319         for (i = 0; i < num_printers; i++) {
1320
1321                 /* do some initialization */
1322                 printername = info_enum[i].info2.printername;
1323                 sharename = info_enum[i].info2.sharename;
1324                 if (!printername || !sharename) {
1325                         goto done;
1326                 }
1327
1328                 /* open printer handle */
1329                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1330                         PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1331                         goto done;
1332
1333                 /* check for existing dst printer */
1334                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1335                         goto done;
1336
1337                 /* check action and set string */
1338                 switch (action) {
1339                 case DSPRINT_PUBLISH:
1340                         action_str = N_("published");
1341                         break;
1342                 case DSPRINT_UPDATE:
1343                         action_str = N_("updated");
1344                         break;
1345                 case DSPRINT_UNPUBLISH:
1346                         action_str = N_("unpublished");
1347                         break;
1348                 default:
1349                         action_str = N_("unknown action");
1350                         printf(_("unknown action: %d\n"), action);
1351                         break;
1352                 }
1353
1354                 info.info7.action = action;
1355                 info_ctr.level = 7;
1356                 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)
1357                         (void *)&info.info7;
1358
1359                 ZERO_STRUCT(devmode_ctr);
1360                 ZERO_STRUCT(secdesc_ctr);
1361
1362                 nt_status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
1363                                                       &hnd,
1364                                                       &info_ctr,
1365                                                       &devmode_ctr,
1366                                                       &secdesc_ctr,
1367                                                       0, /* command */
1368                                                       &result);
1369                 if (!NT_STATUS_IS_OK(nt_status)) {
1370                         printf(_("cannot set printer-info: %s\n"),
1371                                nt_errstr(nt_status));
1372                         goto done;
1373                 }
1374                 if (!W_ERROR_IS_OK(result) && !W_ERROR_EQUAL(result, WERR_IO_PENDING)) {
1375                         if ((action == DSPRINT_UPDATE) && W_ERROR_EQUAL(result, W_ERROR(0x80070002))) {
1376                                 printf(_("printer not published yet\n"));
1377                         } else {
1378                                 printf(_("cannot set printer-info: %s\n"),
1379                                        win_errstr(result));
1380                         }
1381                         nt_status = werror_to_ntstatus(result);
1382                         goto done;
1383                 }
1384
1385                 printf(_("successfully %s printer %s in Active Directory\n"),
1386                        action_str, sharename);
1387         }
1388
1389         nt_status = NT_STATUS_OK;
1390
1391 done:
1392         if (is_valid_policy_hnd(&hnd)) {
1393                 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &result);
1394         }
1395
1396         return nt_status;
1397 }
1398
1399 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1400                                                 const struct dom_sid *domain_sid,
1401                                                 const char *domain_name,
1402                                                 struct cli_state *cli,
1403                                                 struct rpc_pipe_client *pipe_hnd,
1404                                                 TALLOC_CTX *mem_ctx,
1405                                                 int argc,
1406                                                 const char **argv)
1407 {
1408         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1409 }
1410
1411 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1412                                                 const struct dom_sid *domain_sid,
1413                                                 const char *domain_name,
1414                                                 struct cli_state *cli,
1415                                                 struct rpc_pipe_client *pipe_hnd,
1416                                                 TALLOC_CTX *mem_ctx,
1417                                                 int argc,
1418                                                 const char **argv)
1419 {
1420         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1421 }
1422
1423 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1424                                                 const struct dom_sid *domain_sid,
1425                                                 const char *domain_name,
1426                                                 struct cli_state *cli,
1427                                                 struct rpc_pipe_client *pipe_hnd,
1428                                                 TALLOC_CTX *mem_ctx,
1429                                                 int argc,
1430                                                 const char **argv)
1431 {
1432         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1433 }
1434
1435 /**
1436  * List print-queues w.r.t. their publishing state
1437  *
1438  * All parameters are provided by the run_rpc_command function, except for
1439  * argc, argv which are passed through.
1440  *
1441  * @param c     A net_context structure
1442  * @param domain_sid The domain sid aquired from the remote server
1443  * @param cli A cli_state connected to the server.
1444  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1445  * @param argc  Standard main() style argc
1446  * @param argv  Standard main() style argv.  Initial components are already
1447  *              stripped
1448  *
1449  * @return Normal NTSTATUS return.
1450  **/
1451
1452 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1453                                                 const struct dom_sid *domain_sid,
1454                                                 const char *domain_name,
1455                                                 struct cli_state *cli,
1456                                                 struct rpc_pipe_client *pipe_hnd,
1457                                                 TALLOC_CTX *mem_ctx,
1458                                                 int argc,
1459                                                 const char **argv)
1460 {
1461         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1462         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1463         uint32_t i, num_printers;
1464         uint32_t level = 7;
1465         const char *printername, *sharename;
1466         union spoolss_PrinterInfo *info_enum;
1467         union spoolss_PrinterInfo info;
1468         struct policy_handle hnd = { 0, };
1469         int state;
1470         WERROR werr;
1471
1472         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1473                 return nt_status;
1474
1475         for (i = 0; i < num_printers; i++) {
1476
1477                 /* do some initialization */
1478                 printername = info_enum[i].info2.printername;
1479                 sharename = info_enum[i].info2.sharename;
1480
1481                 if (!printername || !sharename) {
1482                         goto done;
1483                 }
1484
1485                 /* open printer handle */
1486                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1487                         PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1488                         goto done;
1489
1490                 /* check for existing dst printer */
1491                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1492                         goto done;
1493
1494                 if (!info.info7.guid) {
1495                         goto done;
1496                 }
1497                 state = info.info7.action;
1498                 switch (state) {
1499                         case DSPRINT_PUBLISH:
1500                                 printf(_("printer [%s] is published"),
1501                                        sharename);
1502                                 if (c->opt_verbose)
1503                                         printf(_(", guid: %s"),info.info7.guid);
1504                                 printf("\n");
1505                                 break;
1506                         case DSPRINT_UNPUBLISH:
1507                                 printf(_("printer [%s] is unpublished\n"),
1508                                        sharename);
1509                                 break;
1510                         case DSPRINT_UPDATE:
1511                                 printf(_("printer [%s] is currently updating\n"),
1512                                        sharename);
1513                                 break;
1514                         default:
1515                                 printf(_("unknown state: %d\n"), state);
1516                                 break;
1517                 }
1518         }
1519
1520         nt_status = NT_STATUS_OK;
1521
1522 done:
1523         if (is_valid_policy_hnd(&hnd)) {
1524                 dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
1525         }
1526
1527         return nt_status;
1528 }
1529
1530 /**
1531  * Migrate Printer-ACLs from a source server to the destination server
1532  *
1533  * All parameters are provided by the run_rpc_command function, except for
1534  * argc, argv which are passed through.
1535  *
1536  * @param c     A net_context structure
1537  * @param domain_sid The domain sid aquired from the remote server
1538  * @param cli A cli_state connected to the server.
1539  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1540  * @param argc  Standard main() style argc
1541  * @param argv  Standard main() style argv.  Initial components are already
1542  *              stripped
1543  *
1544  * @return Normal NTSTATUS return.
1545  **/
1546
1547 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1548                                                 const struct dom_sid *domain_sid,
1549                                                 const char *domain_name,
1550                                                 struct cli_state *cli,
1551                                                 struct rpc_pipe_client *pipe_hnd,
1552                                                 TALLOC_CTX *mem_ctx,
1553                                                 int argc,
1554                                                 const char **argv)
1555 {
1556         struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1557         /* TODO: what now, info2 or info3 ?
1558            convince jerry that we should add clientside setacls level 3 at least
1559         */
1560         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1561         uint32_t i = 0;
1562         uint32_t num_printers;
1563         uint32_t level = 2;
1564         const char *printername, *sharename;
1565         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1566         struct dcerpc_binding_handle *b_dst = NULL;
1567         struct policy_handle hnd_src = { 0, };
1568         struct policy_handle hnd_dst = { 0, };
1569         union spoolss_PrinterInfo *info_enum;
1570         struct cli_state *cli_dst = NULL;
1571         union spoolss_PrinterInfo info_src, info_dst;
1572         WERROR werr;
1573
1574         DEBUG(3,("copying printer ACLs\n"));
1575
1576         /* connect destination PI_SPOOLSS */
1577         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1578                                      &ndr_table_spoolss.syntax_id);
1579         if (!NT_STATUS_IS_OK(nt_status)) {
1580                 return nt_status;
1581         }
1582         b_dst = pipe_hnd_dst->binding_handle;
1583
1584         /* enum source printers */
1585         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1586                 nt_status = NT_STATUS_UNSUCCESSFUL;
1587                 goto done;
1588         }
1589
1590         if (!num_printers) {
1591                 printf (_("no printers found on server.\n"));
1592                 nt_status = NT_STATUS_OK;
1593                 goto done;
1594         }
1595
1596         /* do something for all printers */
1597         for (i = 0; i < num_printers; i++) {
1598
1599                 /* do some initialization */
1600                 printername = info_enum[i].info2.printername;
1601                 sharename = info_enum[i].info2.sharename;
1602
1603                 if (!printername || !sharename) {
1604                         nt_status = NT_STATUS_UNSUCCESSFUL;
1605                         goto done;
1606                 }
1607
1608                 /* we can reset NT_STATUS here because we do not
1609                    get any real NT_STATUS-codes anymore from now on */
1610                 nt_status = NT_STATUS_UNSUCCESSFUL;
1611
1612                 d_printf(_("migrating printer ACLs for:     [%s] / [%s]\n"),
1613                         printername, sharename);
1614
1615                 /* according to msdn you have specify these access-rights
1616                    to see the security descriptor
1617                         - READ_CONTROL (DACL)
1618                         - ACCESS_SYSTEM_SECURITY (SACL)
1619                 */
1620
1621                 /* open src printer handle */
1622                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1623                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1624                         goto done;
1625
1626                 /* open dst printer handle */
1627                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1628                         PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1629                         goto done;
1630
1631                 /* check for existing dst printer */
1632                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1633                         goto done;
1634
1635                 /* check for existing src printer */
1636                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1637                         goto done;
1638
1639                 /* Copy Security Descriptor */
1640
1641                 /* copy secdesc (info level 2) */
1642                 info_dst.info2.devmode = NULL;
1643                 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1644
1645                 if (c->opt_verbose)
1646                         display_sec_desc(info_dst.info2.secdesc);
1647
1648                 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1649                         goto done;
1650
1651                 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1652
1653
1654                 /* close printer handles here */
1655                 if (is_valid_policy_hnd(&hnd_src)) {
1656                         dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
1657                 }
1658
1659                 if (is_valid_policy_hnd(&hnd_dst)) {
1660                         dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
1661                 }
1662
1663         }
1664
1665         nt_status = NT_STATUS_OK;
1666
1667 done:
1668
1669         if (is_valid_policy_hnd(&hnd_src)) {
1670                 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
1671         }
1672
1673         if (is_valid_policy_hnd(&hnd_dst)) {
1674                 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
1675         }
1676
1677         if (cli_dst) {
1678                 cli_shutdown(cli_dst);
1679         }
1680         return nt_status;
1681 }
1682
1683 /**
1684  * Migrate printer-forms from a src server to the dst server
1685  *
1686  * All parameters are provided by the run_rpc_command function, except for
1687  * argc, argv which are passed through.
1688  *
1689  * @param c     A net_context structure
1690  * @param domain_sid The domain sid aquired from the remote server
1691  * @param cli A cli_state connected to the server.
1692  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1693  * @param argc  Standard main() style argc
1694  * @param argv  Standard main() style argv.  Initial components are already
1695  *              stripped
1696  *
1697  * @return Normal NTSTATUS return.
1698  **/
1699
1700 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1701                                                 const struct dom_sid *domain_sid,
1702                                                 const char *domain_name,
1703                                                 struct cli_state *cli,
1704                                                 struct rpc_pipe_client *pipe_hnd,
1705                                                 TALLOC_CTX *mem_ctx,
1706                                                 int argc,
1707                                                 const char **argv)
1708 {
1709         struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1710         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1711         WERROR result;
1712         uint32_t i, f;
1713         uint32_t num_printers;
1714         uint32_t level = 1;
1715         const char *printername, *sharename;
1716         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1717         struct dcerpc_binding_handle *b_dst = NULL;
1718         struct policy_handle hnd_src = { 0, };
1719         struct policy_handle hnd_dst = { 0, };
1720         union spoolss_PrinterInfo *info_enum;
1721         union spoolss_PrinterInfo info_dst;
1722         uint32_t num_forms;
1723         union spoolss_FormInfo *forms;
1724         struct cli_state *cli_dst = NULL;
1725
1726         DEBUG(3,("copying forms\n"));
1727
1728         /* connect destination PI_SPOOLSS */
1729         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1730                                      &ndr_table_spoolss.syntax_id);
1731         if (!NT_STATUS_IS_OK(nt_status)) {
1732                 return nt_status;
1733         }
1734         b_dst = pipe_hnd_dst->binding_handle;
1735
1736         /* enum src printers */
1737         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1738                 nt_status = NT_STATUS_UNSUCCESSFUL;
1739                 goto done;
1740         }
1741
1742         if (!num_printers) {
1743                 printf (_("no printers found on server.\n"));
1744                 nt_status = NT_STATUS_OK;
1745                 goto done;
1746         }
1747
1748         /* do something for all printers */
1749         for (i = 0; i < num_printers; i++) {
1750
1751                 /* do some initialization */
1752                 printername = info_enum[i].info2.printername;
1753                 sharename = info_enum[i].info2.sharename;
1754
1755                 if (!printername || !sharename) {
1756                         nt_status = NT_STATUS_UNSUCCESSFUL;
1757                         goto done;
1758                 }
1759                 /* we can reset NT_STATUS here because we do not
1760                    get any real NT_STATUS-codes anymore from now on */
1761                 nt_status = NT_STATUS_UNSUCCESSFUL;
1762
1763                 d_printf(_("migrating printer forms for:    [%s] / [%s]\n"),
1764                         printername, sharename);
1765
1766
1767                 /* open src printer handle */
1768                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1769                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1770                         goto done;
1771
1772                 /* open dst printer handle */
1773                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1774                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1775                         goto done;
1776
1777                 /* check for existing dst printer */
1778                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1779                         goto done;
1780
1781                 /* finally migrate forms */
1782                 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1783                         goto done;
1784
1785                 DEBUG(1,("got %d forms for printer\n", num_forms));
1786
1787
1788                 for (f = 0; f < num_forms; f++) {
1789
1790                         union spoolss_AddFormInfo info;
1791                         NTSTATUS status;
1792
1793                         /* only migrate FORM_PRINTER types, according to jerry
1794                            FORM_BUILTIN-types are hard-coded in samba */
1795                         if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1796                                 continue;
1797
1798                         if (c->opt_verbose)
1799                                 d_printf(_("\tmigrating form # %d [%s] of type "
1800                                            "[%d]\n"),
1801                                         f, forms[f].info1.form_name,
1802                                         forms[f].info1.flags);
1803
1804                         info.info1 = (struct spoolss_AddFormInfo1 *)
1805                                 (void *)&forms[f].info1;
1806
1807                         /* FIXME: there might be something wrong with samba's
1808                            builtin-forms */
1809                         status = dcerpc_spoolss_AddForm(b_dst, mem_ctx,
1810                                                         &hnd_dst,
1811                                                         1,
1812                                                         info,
1813                                                         &result);
1814                         if (!NT_STATUS_IS_OK(status)) {
1815                                 d_printf(_("\tdcerpc_spoolss_AddForm form %d: [%s] - %s\n"),
1816                                         f, forms[f].info1.form_name, nt_errstr(status));
1817                                 continue;
1818                         }
1819                         if (!W_ERROR_IS_OK(result)) {
1820                                 d_printf(_("\tAddForm form %d: [%s] refused.\n"),
1821                                         f, forms[f].info1.form_name);
1822                                 continue;
1823                         }
1824
1825                         DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1826                                 forms[f].info1.form_name));
1827                 }
1828
1829
1830                 /* close printer handles here */
1831                 if (is_valid_policy_hnd(&hnd_src)) {
1832                         dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
1833                 }
1834
1835                 if (is_valid_policy_hnd(&hnd_dst)) {
1836                         dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
1837                 }
1838         }
1839
1840         nt_status = NT_STATUS_OK;
1841
1842 done:
1843
1844         if (is_valid_policy_hnd(&hnd_src)) {
1845                 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
1846         }
1847
1848         if (is_valid_policy_hnd(&hnd_dst)) {
1849                 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
1850         }
1851
1852         if (cli_dst) {
1853                 cli_shutdown(cli_dst);
1854         }
1855         return nt_status;
1856 }
1857
1858 /**
1859  * Migrate printer-drivers from a src server to the dst server
1860  *
1861  * All parameters are provided by the run_rpc_command function, except for
1862  * argc, argv which are passed through.
1863  *
1864  * @param c     A net_context structure
1865  * @param domain_sid The domain sid aquired from the remote server
1866  * @param cli A cli_state connected to the server.
1867  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1868  * @param argc  Standard main() style argc
1869  * @param argv  Standard main() style argv.  Initial components are already
1870  *              stripped
1871  *
1872  * @return Normal NTSTATUS return.
1873  **/
1874
1875 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1876                                                 const struct dom_sid *domain_sid,
1877                                                 const char *domain_name,
1878                                                 struct cli_state *cli,
1879                                                 struct rpc_pipe_client *pipe_hnd,
1880                                                 TALLOC_CTX *mem_ctx,
1881                                                 int argc,
1882                                                 const char **argv)
1883 {
1884         struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
1885         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1886         uint32_t i, p;
1887         uint32_t num_printers;
1888         uint32_t level = 3;
1889         const char *printername, *sharename;
1890         bool got_src_driver_share = false;
1891         bool got_dst_driver_share = false;
1892         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1893         struct dcerpc_binding_handle *b_dst = NULL;
1894         struct policy_handle hnd_src = { 0, };
1895         struct policy_handle hnd_dst = { 0, };
1896         union spoolss_DriverInfo drv_info_src;
1897         union spoolss_PrinterInfo *info_enum;
1898         union spoolss_PrinterInfo info_dst;
1899         struct cli_state *cli_dst = NULL;
1900         struct cli_state *cli_share_src = NULL;
1901         struct cli_state *cli_share_dst = NULL;
1902         const char *drivername = NULL;
1903         WERROR werr;
1904
1905         DEBUG(3,("copying printer-drivers\n"));
1906
1907         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1908                                      &ndr_table_spoolss.syntax_id);
1909         if (!NT_STATUS_IS_OK(nt_status)) {
1910                 return nt_status;
1911         }
1912         b_dst = pipe_hnd_dst->binding_handle;
1913
1914         /* open print$-share on the src server */
1915         nt_status = connect_to_service(c, &cli_share_src,
1916                                        cli_state_remote_sockaddr(cli),
1917                                        cli_state_remote_name(cli),
1918                                        "print$", "A:");
1919         if (!NT_STATUS_IS_OK(nt_status))
1920                 goto done;
1921
1922         got_src_driver_share = true;
1923
1924
1925         /* open print$-share on the dst server */
1926         nt_status = connect_to_service(c, &cli_share_dst,
1927                                        cli_state_remote_sockaddr(cli_dst),
1928                                        cli_state_remote_name(cli_dst),
1929                                        "print$", "A:");
1930         if (!NT_STATUS_IS_OK(nt_status))
1931                 return nt_status;
1932
1933         got_dst_driver_share = true;
1934
1935
1936         /* enum src printers */
1937         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1938                 nt_status = NT_STATUS_UNSUCCESSFUL;
1939                 goto done;
1940         }
1941
1942         if (num_printers == 0) {
1943                 printf (_("no printers found on server.\n"));
1944                 nt_status = NT_STATUS_OK;
1945                 goto done;
1946         }
1947
1948
1949         /* do something for all printers */
1950         for (p = 0; p < num_printers; p++) {
1951
1952                 /* do some initialization */
1953                 printername = info_enum[p].info2.printername;
1954                 sharename = info_enum[p].info2.sharename;
1955
1956                 if (!printername || !sharename) {
1957                         nt_status = NT_STATUS_UNSUCCESSFUL;
1958                         goto done;
1959                 }
1960
1961                 /* we can reset NT_STATUS here because we do not
1962                    get any real NT_STATUS-codes anymore from now on */
1963                 nt_status = NT_STATUS_UNSUCCESSFUL;
1964
1965                 d_printf(_("migrating printer driver for:   [%s] / [%s]\n"),
1966                         printername, sharename);
1967
1968                 /* open dst printer handle */
1969                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1970                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1971                         goto done;
1972
1973                 /* check for existing dst printer */
1974                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1975                         goto done;
1976
1977
1978                 /* open src printer handle */
1979                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1980                                                  MAXIMUM_ALLOWED_ACCESS,
1981                                                  pipe_hnd->auth->user_name,
1982                                                  &hnd_src))
1983                         goto done;
1984
1985                 /* in a first step call getdriver for each shared printer (per arch)
1986                    to get a list of all files that have to be copied */
1987
1988                 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1989
1990                         /* getdriver src */
1991                         if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1992                                         level, archi_table[i].long_archi,
1993                                         archi_table[i].version, &drv_info_src))
1994                                 continue;
1995
1996                         drivername = drv_info_src.info3.driver_name;
1997
1998                         if (c->opt_verbose)
1999                                 display_print_driver3(&drv_info_src.info3);
2000
2001                         /* check arch dir */
2002                         nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
2003                         if (!NT_STATUS_IS_OK(nt_status))
2004                                 goto done;
2005
2006
2007                         /* copy driver-files */
2008                         nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
2009                                                         archi_table[i].short_archi,
2010                                                         &drv_info_src.info3);
2011                         if (!NT_STATUS_IS_OK(nt_status))
2012                                 goto done;
2013
2014
2015                         /* adddriver dst */
2016                         if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
2017                                 nt_status = NT_STATUS_UNSUCCESSFUL;
2018                                 goto done;
2019                         }
2020
2021                         DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
2022                                 drivername, printername));
2023
2024                 }
2025
2026                 if (!drivername || strlen(drivername) == 0) {
2027                         DEBUGADD(1,("Did not get driver for printer %s\n",
2028                                     printername));
2029                         goto done;
2030                 }
2031
2032                 /* setdriver dst */
2033                 info_dst.info2.drivername = drivername;
2034
2035                 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
2036                         nt_status = NT_STATUS_UNSUCCESSFUL;
2037                         goto done;
2038                 }
2039
2040                 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
2041                         drivername, printername));
2042
2043                 /* close dst */
2044                 if (is_valid_policy_hnd(&hnd_dst)) {
2045                         dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
2046                 }
2047
2048                 /* close src */
2049                 if (is_valid_policy_hnd(&hnd_src)) {
2050                         dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
2051                 }
2052         }
2053
2054         nt_status = NT_STATUS_OK;
2055
2056 done:
2057
2058         if (is_valid_policy_hnd(&hnd_dst)) {
2059                 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
2060         }
2061
2062         /* close src */
2063         if (is_valid_policy_hnd(&hnd_src)) {
2064                 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
2065         }
2066
2067         if (cli_dst) {
2068                 cli_shutdown(cli_dst);
2069         }
2070
2071         if (got_src_driver_share)
2072                 cli_shutdown(cli_share_src);
2073
2074         if (got_dst_driver_share)
2075                 cli_shutdown(cli_share_dst);
2076
2077         return nt_status;
2078
2079 }
2080
2081 /**
2082  * Migrate printer-queues from a src to the dst server
2083  * (requires a working "addprinter command" to be installed for the local smbd)
2084  *
2085  * All parameters are provided by the run_rpc_command function, except for
2086  * argc, argv which are passed through.
2087  *
2088  * @param c     A net_context structure
2089  * @param domain_sid The domain sid aquired from the remote server
2090  * @param cli A cli_state connected to the server.
2091  * @param mem_ctx Talloc context, destoyed on compleation of the function.
2092  * @param argc  Standard main() style argc
2093  * @param argv  Standard main() style argv.  Initial components are already
2094  *              stripped
2095  *
2096  * @return Normal NTSTATUS return.
2097  **/
2098
2099 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
2100                                                 const struct dom_sid *domain_sid,
2101                                                 const char *domain_name,
2102                                                 struct cli_state *cli,
2103                                                 struct rpc_pipe_client *pipe_hnd,
2104                                                 TALLOC_CTX *mem_ctx,
2105                                                 int argc,
2106                                                 const char **argv)
2107 {
2108         struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
2109         WERROR result;
2110         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2111         uint32_t i = 0, num_printers;
2112         uint32_t level = 2;
2113         union spoolss_PrinterInfo info_dst, info_src;
2114         union spoolss_PrinterInfo *info_enum;
2115         struct cli_state *cli_dst = NULL;
2116         struct policy_handle hnd_src = { 0, };
2117         struct policy_handle hnd_dst = { 0, };
2118         const char *printername, *sharename;
2119         struct rpc_pipe_client *pipe_hnd_dst = NULL;
2120         struct dcerpc_binding_handle *b_dst = NULL;
2121         struct spoolss_SetPrinterInfoCtr info_ctr;
2122
2123         DEBUG(3,("copying printers\n"));
2124
2125         /* connect destination PI_SPOOLSS */
2126         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2127                                      &ndr_table_spoolss.syntax_id);
2128         if (!NT_STATUS_IS_OK(nt_status)) {
2129                 return nt_status;
2130         }
2131         b_dst = pipe_hnd_dst->binding_handle;
2132
2133         /* enum printers */
2134         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2135                 nt_status = NT_STATUS_UNSUCCESSFUL;
2136                 goto done;
2137         }
2138
2139         if (!num_printers) {
2140                 printf (_("no printers found on server.\n"));
2141                 nt_status = NT_STATUS_OK;
2142                 goto done;
2143         }
2144
2145         /* do something for all printers */
2146         for (i = 0; i < num_printers; i++) {
2147
2148                 struct spoolss_SetPrinterInfo2 info2;
2149
2150                 /* do some initialization */
2151                 printername = info_enum[i].info2.printername;
2152                 sharename = info_enum[i].info2.sharename;
2153
2154                 if (!printername || !sharename) {
2155                         nt_status = NT_STATUS_UNSUCCESSFUL;
2156                         goto done;
2157                 }
2158                 /* we can reset NT_STATUS here because we do not
2159                    get any real NT_STATUS-codes anymore from now on */
2160                 nt_status = NT_STATUS_UNSUCCESSFUL;
2161
2162                 d_printf(_("migrating printer queue for:    [%s] / [%s]\n"),
2163                         printername, sharename);
2164
2165                 /* open dst printer handle */
2166                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2167                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2168
2169                         DEBUG(1,("could not open printer: %s\n", sharename));
2170                 }
2171
2172                 /* check for existing dst printer */
2173                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2174                         printf (_("could not get printer, creating printer.\n"));
2175                 } else {
2176                         DEBUG(1,("printer already exists: %s\n", sharename));
2177                         /* close printer handle here - dst only, not got src yet. */
2178                         if (is_valid_policy_hnd(&hnd_dst)) {
2179                                 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2180                         }
2181                         continue;
2182                 }
2183
2184                 /* now get again src printer ctr via getprinter,
2185                    we first need a handle for that */
2186
2187                 /* open src printer handle */
2188                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2189                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2190                         goto done;
2191
2192                 /* getprinter on the src server */
2193                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2194                         goto done;
2195
2196                 /* copy each src printer to a dst printer 1:1,
2197                    maybe some values have to be changed though */
2198                 d_printf(_("creating printer: %s\n"), printername);
2199
2200                 info_ctr.level = level;
2201                 spoolss_printerinfo2_to_setprinterinfo2(&info_src.info2, &info2);
2202                 info_ctr.info.info2 = &info2;
2203
2204                 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2205                                                      mem_ctx,
2206                                                      &info_ctr);
2207
2208                 if (W_ERROR_IS_OK(result))
2209                         d_printf (_("printer [%s] successfully added.\n"),
2210                                   printername);
2211                 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2212                         d_fprintf (stderr, _("printer [%s] already exists.\n"),
2213                                    printername);
2214                 else {
2215                         d_fprintf (stderr, _("could not create printer [%s]\n"),
2216                                    printername);
2217                         goto done;
2218                 }
2219
2220                 /* close printer handles here */
2221                 if (is_valid_policy_hnd(&hnd_src)) {
2222                         dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2223                 }
2224
2225                 if (is_valid_policy_hnd(&hnd_dst)) {
2226                         dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2227                 }
2228         }
2229
2230         nt_status = NT_STATUS_OK;
2231
2232 done:
2233         if (is_valid_policy_hnd(&hnd_src)) {
2234                 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2235         }
2236
2237         if (is_valid_policy_hnd(&hnd_dst)) {
2238                 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2239         }
2240
2241         if (cli_dst) {
2242                 cli_shutdown(cli_dst);
2243         }
2244         return nt_status;
2245 }
2246
2247 /**
2248  * Migrate Printer-Settings from a src server to the dst server
2249  * (for this to work, printers and drivers already have to be migrated earlier)
2250  *
2251  * All parameters are provided by the run_rpc_command function, except for
2252  * argc, argv which are passed through.
2253  *
2254  * @param c     A net_context structure
2255  * @param domain_sid The domain sid aquired from the remote server
2256  * @param cli A cli_state connected to the server.
2257  * @param mem_ctx Talloc context, destoyed on compleation of the function.
2258  * @param argc  Standard main() style argc
2259  * @param argv  Standard main() style argv.  Initial components are already
2260  *              stripped
2261  *
2262  * @return Normal NTSTATUS return.
2263  **/
2264
2265 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2266                                                 const struct dom_sid *domain_sid,
2267                                                 const char *domain_name,
2268                                                 struct cli_state *cli,
2269                                                 struct rpc_pipe_client *pipe_hnd,
2270                                                 TALLOC_CTX *mem_ctx,
2271                                                 int argc,
2272                                                 const char **argv)
2273 {
2274         struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
2275
2276         /* FIXME: Here the nightmare begins */
2277
2278         WERROR result;
2279         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2280         uint32_t i = 0, j = 0;
2281         uint32_t num_printers;
2282         uint32_t level = 2;
2283         const char *printername, *sharename;
2284         struct rpc_pipe_client *pipe_hnd_dst = NULL;
2285         struct dcerpc_binding_handle *b_dst = NULL;
2286         struct policy_handle hnd_src = { 0, };
2287         struct policy_handle hnd_dst = { 0, };
2288         union spoolss_PrinterInfo *info_enum;
2289         union spoolss_PrinterInfo info_dst_publish;
2290         union spoolss_PrinterInfo info_dst;
2291         struct cli_state *cli_dst = NULL;
2292         char *devicename = NULL, *unc_name = NULL, *url = NULL;
2293         const char *longname;
2294         const char **keylist = NULL;
2295
2296         /* FIXME GD */
2297         ZERO_STRUCT(info_dst_publish);
2298
2299         DEBUG(3,("copying printer settings\n"));
2300
2301         /* connect destination PI_SPOOLSS */
2302         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2303                                      &ndr_table_spoolss.syntax_id);
2304         if (!NT_STATUS_IS_OK(nt_status)) {
2305                 return nt_status;
2306         }
2307         b_dst = pipe_hnd_dst->binding_handle;
2308
2309         /* enum src printers */
2310         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2311                 nt_status = NT_STATUS_UNSUCCESSFUL;
2312                 goto done;
2313         }
2314
2315         if (!num_printers) {
2316                 printf (_("no printers found on server.\n"));
2317                 nt_status = NT_STATUS_OK;
2318                 goto done;
2319         }
2320
2321
2322         /* needed for dns-strings in regkeys */
2323         longname = get_mydnsfullname();
2324         if (!longname) {
2325                 nt_status = NT_STATUS_UNSUCCESSFUL;
2326                 goto done;
2327         }
2328
2329         /* do something for all printers */
2330         for (i = 0; i < num_printers; i++) {
2331
2332                 uint32_t value_needed;
2333                 uint32_t data_needed;
2334                 enum winreg_Type type;
2335                 struct spoolss_EnumPrinterData r;
2336
2337                 /* do some initialization */
2338                 printername = info_enum[i].info2.printername;
2339                 sharename = info_enum[i].info2.sharename;
2340
2341                 if (!printername || !sharename) {
2342                         nt_status = NT_STATUS_UNSUCCESSFUL;
2343                         goto done;
2344                 }
2345                 /* we can reset NT_STATUS here because we do not
2346                    get any real NT_STATUS-codes anymore from now on */
2347                 nt_status = NT_STATUS_UNSUCCESSFUL;
2348
2349                 d_printf(_("migrating printer settings for: [%s] / [%s]\n"),
2350                         printername, sharename);
2351
2352
2353                 /* open src printer handle */
2354                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2355                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2356                         goto done;
2357
2358                 /* open dst printer handle */
2359                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2360                         PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2361                         goto done;
2362
2363                 /* check for existing dst printer */
2364                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2365                                 level, &info_dst))
2366                         goto done;
2367
2368
2369                 /* STEP 1: COPY DEVICE-MODE and other
2370                            PRINTER_INFO_2-attributes
2371                 */
2372
2373                 info_dst.info2 = info_enum[i].info2;
2374
2375                 /* why is the port always disconnected when the printer
2376                    is correctly installed (incl. driver ???) */
2377                 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2378
2379                 /* check if printer is published */
2380                 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2381
2382                         /* check for existing dst printer */
2383                         if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2384                                 goto done;
2385
2386                         info_dst_publish.info7.action = DSPRINT_PUBLISH;
2387
2388                         /* ignore false from setprinter due to WERR_IO_PENDING */
2389                         net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2390
2391                         DEBUG(3,("republished printer\n"));
2392                 }
2393
2394                 if (info_enum[i].info2.devmode != NULL) {
2395
2396                         /* copy devmode (info level 2) */
2397                         info_dst.info2.devmode = info_enum[i].info2.devmode;
2398
2399                         /* do not copy security descriptor (we have another
2400                          * command for that) */
2401                         info_dst.info2.secdesc = NULL;
2402
2403 #if 0
2404                         info_dst.info2.devmode.devicename =
2405                                 talloc_asprintf(mem_ctx, "\\\\%s\\%s",
2406                                                 longname, printername);
2407                         if (!info_dst.info2.devmode.devicename) {
2408                                 nt_status = NT_STATUS_NO_MEMORY;
2409                                 goto done;
2410                         }
2411 #endif
2412                         if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2413                                                     level, &info_dst))
2414                                 goto done;
2415
2416                         DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2417                 }
2418
2419                 /* STEP 2: COPY REGISTRY VALUES */
2420
2421                 /* please keep in mind that samba parse_spools gives horribly
2422                    crippled results when used to rpccli_spoolss_enumprinterdataex
2423                    a win2k3-server.  (Bugzilla #1851)
2424                    FIXME: IIRC I've seen it too on a win2k-server
2425                 */
2426
2427                 r.in.handle = &hnd_src;
2428                 r.in.enum_index = 0;
2429                 r.in.value_offered = 0;
2430                 r.in.data_offered = 0;
2431                 r.out.value_name = NULL;
2432                 r.out.value_needed = &value_needed;
2433                 r.out.type = &type;
2434                 r.out.data = NULL;
2435                 r.out.data_needed = &data_needed;
2436
2437                 /* enumerate data on src handle */
2438                 nt_status = dcerpc_spoolss_EnumPrinterData_r(b_src, mem_ctx, &r);
2439
2440                 r.in.data_offered       = *r.out.data_needed;
2441                 r.in.value_offered      = *r.out.value_needed;
2442                 r.out.data              = talloc_zero_array(mem_ctx, uint8_t, r.in.data_offered);
2443                 r.out.value_name        = talloc_zero_array(mem_ctx, char, r.in.value_offered);
2444
2445                 /* loop for all printerdata of "PrinterDriverData" */
2446                 while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(r.out.result)) {
2447
2448                         r.in.enum_index++;
2449
2450                         nt_status = dcerpc_spoolss_EnumPrinterData_r(b_src, mem_ctx, &r);
2451
2452                         /* loop for all reg_keys */
2453                         if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(r.out.result)) {
2454
2455                                 /* display_value */
2456                                 if (c->opt_verbose) {
2457                                         struct regval_blob *v;
2458
2459                                         v = regval_compose(talloc_tos(),
2460                                                            r.out.value_name,
2461                                                            *r.out.type,
2462                                                            r.out.data,
2463                                                            r.in.data_offered);
2464                                         if (v == NULL) {
2465                                                 nt_status = NT_STATUS_NO_MEMORY;
2466                                                 goto done;
2467                                         }
2468
2469                                         display_reg_value(SPOOL_PRINTERDATA_KEY, v);
2470                                         talloc_free(v);
2471                                 }
2472
2473                                 /* set_value */
2474                                 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2475                                                                 &hnd_dst, r.out.value_name,
2476                                                                 *r.out.type, r.out.data, r.in.data_offered))
2477                                         goto done;
2478
2479                                 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2480                                             r.out.value_name));
2481                         }
2482                 }
2483
2484                 /* STEP 3: COPY SUBKEY VALUES */
2485
2486                 /* here we need to enum all printer_keys and then work
2487                    on the result with enum_printer_key_ex. nt4 does not
2488                    respond to enumprinterkey, win2k does, so continue
2489                    in case of an error */
2490
2491                 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2492                         printf(_("got no key-data\n"));
2493                         continue;
2494                 }
2495
2496
2497                 /* work on a list of printer keys
2498                    each key has to be enumerated to get all required
2499                    information.  information is then set via setprinterdataex-calls */
2500
2501                 if (keylist == NULL)
2502                         continue;
2503
2504                 for (i=0; keylist && keylist[i] != NULL; i++) {
2505
2506                         const char *subkey = keylist[i];
2507                         uint32_t count;
2508                         struct spoolss_PrinterEnumValues *info;
2509
2510                         /* enumerate all src subkeys */
2511                         if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2512                                                            &hnd_src, subkey,
2513                                                            &count, &info)) {
2514                                 goto done;
2515                         }
2516
2517                         for (j=0; j < count; j++) {
2518
2519                                 struct regval_blob *value;
2520                                 DATA_BLOB blob;
2521
2522                                 ZERO_STRUCT(blob);
2523
2524                                 /* although samba replies with sane data in most cases we
2525                                    should try to avoid writing wrong registry data */
2526
2527                                 if (strequal(info[j].value_name, SPOOL_REG_PORTNAME) ||
2528                                     strequal(info[j].value_name, SPOOL_REG_UNCNAME) ||
2529                                     strequal(info[j].value_name, SPOOL_REG_URL) ||
2530                                     strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME) ||
2531                                     strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2532
2533                                         if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
2534
2535                                                 /* although windows uses a multi-sz, we use a sz */
2536                                                 push_reg_sz(mem_ctx, &blob, SAMBA_PRINTER_PORT_NAME);
2537                                         }
2538
2539                                         if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
2540
2541                                                 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2542                                                         nt_status = NT_STATUS_NO_MEMORY;
2543                                                         goto done;
2544                                                 }
2545                                                 push_reg_sz(mem_ctx, &blob, unc_name);
2546                                         }
2547
2548                                         if (strequal(info[j].value_name, SPOOL_REG_URL)) {
2549
2550                                                 continue;
2551
2552 #if 0
2553                                                 /* FIXME: should we really do that ??? */
2554                                                 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2555                                                         nt_status = NT_STATUS_NO_MEMORY;
2556                                                         goto done;
2557                                                 }
2558                                                 push_reg_sz(mem_ctx, NULL, &blob, url);
2559                                                 fstrcpy(value.valuename, SPOOL_REG_URL);
2560 #endif
2561                                         }
2562
2563                                         if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
2564
2565                                                 push_reg_sz(mem_ctx, &blob, longname);
2566                                         }
2567
2568                                         if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
2569
2570                                                 push_reg_sz(mem_ctx, &blob, lp_netbios_name());
2571                                         }
2572
2573                                         value = regval_compose(talloc_tos(),
2574                                                                info[j].value_name,
2575                                                                REG_SZ,
2576                                                                blob.length == 0 ? NULL : blob.data,
2577                                                                blob.length);
2578                                         if (value == NULL) {
2579                                                 nt_status = NT_STATUS_NO_MEMORY;
2580                                                 goto done;
2581                                         }
2582
2583                                         if (c->opt_verbose)
2584                                                 display_reg_value(subkey, value);
2585
2586                                         /* here we have to set all subkeys on the dst server */
2587                                         if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2588                                                         subkey, value))
2589                                         {
2590                                                 talloc_free(value);
2591                                                 goto done;
2592                                         }
2593
2594                                         talloc_free(value);
2595                                 } else {
2596
2597                                         struct regval_blob *v;
2598
2599                                         v = regval_compose(talloc_tos(),
2600                                                            info[j].value_name,
2601                                                            info[j].type,
2602                                                            info[j].data->data,
2603                                                            info[j].data->length);
2604                                         if (v == NULL) {
2605                                                 nt_status = NT_STATUS_NO_MEMORY;
2606                                                 goto done;
2607                                         }
2608
2609                                         if (c->opt_verbose) {
2610                                                 display_reg_value(subkey, v);
2611                                         }
2612
2613                                         /* here we have to set all subkeys on the dst server */
2614                                         if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2615                                                         subkey, v)) {
2616                                                 goto done;
2617                                         }
2618
2619                                         talloc_free(v);
2620                                 }
2621
2622                                 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2623                                                 subkey, info[j].value_name));
2624
2625                         }
2626                 }
2627
2628                 TALLOC_FREE(keylist);
2629
2630                 /* close printer handles here */
2631                 if (is_valid_policy_hnd(&hnd_src)) {
2632                         dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2633                 }
2634
2635                 if (is_valid_policy_hnd(&hnd_dst)) {
2636                         dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2637                 }
2638         }
2639
2640         nt_status = NT_STATUS_OK;
2641
2642 done:
2643         SAFE_FREE(devicename);
2644         SAFE_FREE(url);
2645         SAFE_FREE(unc_name);
2646
2647         if (is_valid_policy_hnd(&hnd_src)) {
2648                 dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
2649         }
2650
2651         if (is_valid_policy_hnd(&hnd_dst)) {
2652                 dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
2653         }
2654
2655         if (cli_dst) {
2656                 cli_shutdown(cli_dst);
2657         }
2658         return nt_status;
2659 }