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