Merge branch 'master' of git://git.samba.org/samba into teventfix
[samba.git] / source3 / utils / net_rpc_printer.c
1 /*
2    Samba Unix/Linux SMB client library
3    Distributed SMB/CIFS Server Management Utility
4    Copyright (C) 2004 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 *num_forms,
959                                 FORM_1 **forms)
960 {
961         WERROR result;
962
963         /* enumforms call */
964         result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
965
966         if (!W_ERROR_IS_OK(result)) {
967                 printf("could not enum forms: %s\n", win_errstr(result));
968                 return false;
969         }
970
971         return true;
972 }
973
974 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
975                                         TALLOC_CTX *mem_ctx,
976                                         uint32 level, const char *env,
977                                         uint32 *num_drivers,
978                                         PRINTER_DRIVER_CTR *ctr)
979 {
980         WERROR result;
981
982         /* enumprinterdrivers call */
983         result = rpccli_spoolss_enumprinterdrivers(
984                         pipe_hnd, mem_ctx, level,
985                         env, num_drivers, ctr);
986
987         if (!W_ERROR_IS_OK(result)) {
988                 printf("cannot enum drivers: %s\n", win_errstr(result));
989                 return false;
990         }
991
992         return true;
993 }
994
995 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
996                              TALLOC_CTX *mem_ctx,
997                              POLICY_HND *hnd, uint32 level,
998                              const char *env, int version,
999                              union spoolss_DriverInfo *info)
1000 {
1001         WERROR result;
1002         uint32_t server_major_version;
1003         uint32_t server_minor_version;
1004
1005         /* getprinterdriver call */
1006         result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
1007                                                   hnd,
1008                                                   env,
1009                                                   level,
1010                                                   0,
1011                                                   version,
1012                                                   2,
1013                                                   info,
1014                                                   &server_major_version,
1015                                                   &server_minor_version);
1016         if (!W_ERROR_IS_OK(result)) {
1017                 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
1018                         env, win_errstr(result)));
1019                 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
1020                     W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
1021                         printf("cannot get driver: %s\n", win_errstr(result));
1022                 }
1023                 return false;
1024         }
1025
1026         return true;
1027 }
1028
1029
1030 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
1031                              TALLOC_CTX *mem_ctx, uint32 level,
1032                              union spoolss_DriverInfo *info)
1033 {
1034         WERROR result;
1035         NTSTATUS status;
1036         struct spoolss_AddDriverInfoCtr info_ctr;
1037
1038         info_ctr.level = level;
1039
1040         switch (level) {
1041         case 2:
1042                 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)&info->info2;
1043                 break;
1044         case 3:
1045                 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)&info->info3;
1046                 break;
1047         default:
1048                 printf("unsupported info level: %d\n", level);
1049                 return false;
1050         }
1051
1052         /* addprinterdriver call */
1053         status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1054                                                  pipe_hnd->srv_name_slash,
1055                                                  &info_ctr,
1056                                                  &result);
1057         /* be more verbose */
1058         if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1059                 printf("You are not allowed to add drivers\n");
1060                 return false;
1061         }
1062         if (!W_ERROR_IS_OK(result)) {
1063                 printf("cannot add driver: %s\n", win_errstr(result));
1064                 return false;
1065         }
1066
1067         return true;
1068 }
1069
1070 /**
1071  * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
1072  * for a single printer or for all printers depending on argc/argv
1073  **/
1074
1075 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1076                         TALLOC_CTX *mem_ctx,
1077                         int level,
1078                         int argc,
1079                         const char **argv,
1080                         uint32 *num_printers,
1081                         PRINTER_INFO_CTR *ctr)
1082 {
1083
1084         POLICY_HND hnd;
1085         union spoolss_PrinterInfo info;
1086
1087         /* no arguments given, enumerate all printers */
1088         if (argc == 0) {
1089
1090                 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1091                                 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1092                                 level, num_printers, ctr))
1093                         return false;
1094
1095                 goto out;
1096         }
1097
1098         /* FIXME GD */
1099         return false;
1100
1101         /* argument given, get a single printer by name */
1102         if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1103                                          MAXIMUM_ALLOWED_ACCESS,
1104                                          pipe_hnd->auth->user_name,
1105                                          &hnd))
1106                 return false;
1107
1108         if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info)) {
1109                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1110                 return false;
1111         }
1112
1113         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1114
1115         *num_printers = 1;
1116
1117 out:
1118         DEBUG(3,("got %d printers\n", *num_printers));
1119
1120         return true;
1121
1122 }
1123
1124 /**
1125  * List print-queues (including local printers that are not shared)
1126  *
1127  * All parameters are provided by the run_rpc_command function, except for
1128  * argc, argv which are passed through.
1129  *
1130  * @param c     A net_context structure
1131  * @param domain_sid The domain sid aquired from the remote server
1132  * @param cli A cli_state connected to the server.
1133  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1134  * @param argc  Standard main() style argc
1135  * @param argv  Standard main() style argv.  Initial components are already
1136  *              stripped
1137  *
1138  * @return Normal NTSTATUS return.
1139  **/
1140
1141 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1142                                         const DOM_SID *domain_sid,
1143                                         const char *domain_name,
1144                                         struct cli_state *cli,
1145                                         struct rpc_pipe_client *pipe_hnd,
1146                                         TALLOC_CTX *mem_ctx,
1147                                         int argc,
1148                                         const char **argv)
1149 {
1150         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1151         uint32 i, num_printers;
1152         uint32 level = 2;
1153         char *printername, *sharename;
1154         PRINTER_INFO_CTR ctr;
1155
1156         printf("listing printers\n");
1157
1158         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
1159                 return nt_status;
1160
1161         for (i = 0; i < num_printers; i++) {
1162                 /* do some initialization */
1163                 rpcstr_pull_talloc(mem_ctx,
1164                                 &printername,
1165                                 ctr.printers_2[i].printername.buffer,
1166                                 -1,
1167                                 STR_TERMINATE);
1168                 rpcstr_pull_talloc(mem_ctx,
1169                                 &sharename,
1170                                 ctr.printers_2[i].sharename.buffer,
1171                                 -1,
1172                                 STR_TERMINATE);
1173
1174                 if (printername && sharename) {
1175                         d_printf("printer %d: %s, shared as: %s\n",
1176                                 i+1, printername, sharename);
1177                 }
1178         }
1179
1180         return NT_STATUS_OK;
1181 }
1182
1183 /**
1184  * List printer-drivers from a server
1185  *
1186  * All parameters are provided by the run_rpc_command function, except for
1187  * argc, argv which are passed through.
1188  *
1189  * @param c     A net_context structure
1190  * @param domain_sid The domain sid aquired from the remote server
1191  * @param cli A cli_state connected to the server.
1192  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1193  * @param argc  Standard main() style argc
1194  * @param argv  Standard main() style argv.  Initial components are already
1195  *              stripped
1196  *
1197  * @return Normal NTSTATUS return.
1198  **/
1199
1200 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1201                                                 const DOM_SID *domain_sid,
1202                                                 const char *domain_name,
1203                                                 struct cli_state *cli,
1204                                                 struct rpc_pipe_client *pipe_hnd,
1205                                                 TALLOC_CTX *mem_ctx,
1206                                                 int argc,
1207                                                 const char **argv)
1208 {
1209         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1210         uint32 i;
1211         uint32 level = 3;
1212         PRINTER_DRIVER_CTR drv_ctr_enum;
1213         int d;
1214
1215         ZERO_STRUCT(drv_ctr_enum);
1216
1217         printf("listing printer-drivers\n");
1218
1219         for (i=0; archi_table[i].long_archi!=NULL; i++) {
1220
1221                 uint32 num_drivers;
1222
1223                 /* enum remote drivers */
1224                 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1225                                 archi_table[i].long_archi,
1226                                 &num_drivers, &drv_ctr_enum)) {
1227                         nt_status = NT_STATUS_UNSUCCESSFUL;
1228                         goto done;
1229                 }
1230
1231                 if (num_drivers == 0) {
1232                         d_printf ("no drivers found on server for architecture: [%s].\n",
1233                                 archi_table[i].long_archi);
1234                         continue;
1235                 }
1236
1237                 d_printf("got %d printer-drivers for architecture: [%s]\n",
1238                         num_drivers, archi_table[i].long_archi);
1239
1240
1241                 /* do something for all drivers for architecture */
1242                 for (d = 0; d < num_drivers; d++) {
1243                         display_print_driver_3(&(drv_ctr_enum.info3[d]));
1244                 }
1245         }
1246
1247         nt_status = NT_STATUS_OK;
1248
1249 done:
1250         return nt_status;
1251
1252 }
1253
1254 /**
1255  * Publish print-queues with args-wrapper
1256  *
1257  * @param cli A cli_state connected to the server.
1258  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1259  * @param argc  Standard main() style argc
1260  * @param argv  Standard main() style argv.  Initial components are already
1261  *              stripped
1262  * @param action
1263  *
1264  * @return Normal NTSTATUS return.
1265  **/
1266
1267 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1268                                         TALLOC_CTX *mem_ctx,
1269                                         int argc,
1270                                         const char **argv,
1271                                         uint32 action)
1272 {
1273         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1274         uint32 i, num_printers;
1275         uint32 level = 7;
1276         char *printername, *sharename;
1277         PRINTER_INFO_CTR ctr;
1278         union spoolss_PrinterInfo info;
1279         struct spoolss_SetPrinterInfoCtr info_ctr;
1280         struct spoolss_DevmodeContainer devmode_ctr;
1281         struct sec_desc_buf secdesc_ctr;
1282         POLICY_HND hnd;
1283         WERROR result;
1284         const char *action_str;
1285
1286         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1287                 return nt_status;
1288
1289         for (i = 0; i < num_printers; i++) {
1290                 /* do some initialization */
1291                 rpcstr_pull_talloc(mem_ctx,
1292                                 &printername,
1293                                 ctr.printers_2[i].printername.buffer,
1294                                 -1,
1295                                 STR_TERMINATE);
1296                 rpcstr_pull_talloc(mem_ctx,
1297                                 &sharename,
1298                                 ctr.printers_2[i].sharename.buffer,
1299                                 -1,
1300                                 STR_TERMINATE);
1301                 if (!printername || !sharename) {
1302                         goto done;
1303                 }
1304
1305                 /* open printer handle */
1306                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1307                         PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1308                         goto done;
1309
1310                 /* check for existing dst printer */
1311                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1312                         goto done;
1313
1314                 /* check action and set string */
1315                 switch (action) {
1316                 case DSPRINT_PUBLISH:
1317                         action_str = "published";
1318                         break;
1319                 case DSPRINT_UPDATE:
1320                         action_str = "updated";
1321                         break;
1322                 case DSPRINT_UNPUBLISH:
1323                         action_str = "unpublished";
1324                         break;
1325                 default:
1326                         action_str = "unknown action";
1327                         printf("unkown action: %d\n", action);
1328                         break;
1329                 }
1330
1331                 info.info7.action = action;
1332                 info_ctr.level = 7;
1333                 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info.info7;
1334
1335                 ZERO_STRUCT(devmode_ctr);
1336                 ZERO_STRUCT(secdesc_ctr);
1337
1338                 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1339                                                       &hnd,
1340                                                       &info_ctr,
1341                                                       &devmode_ctr,
1342                                                       &secdesc_ctr,
1343                                                       0, /* command */
1344                                                       &result);
1345
1346                 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1347                         printf("cannot set printer-info: %s\n", win_errstr(result));
1348                         goto done;
1349                 }
1350
1351                 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1352         }
1353
1354         nt_status = NT_STATUS_OK;
1355
1356 done:
1357         if (is_valid_policy_hnd(&hnd))
1358                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1359
1360         return nt_status;
1361 }
1362
1363 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1364                                                 const DOM_SID *domain_sid,
1365                                                 const char *domain_name,
1366                                                 struct cli_state *cli,
1367                                                 struct rpc_pipe_client *pipe_hnd,
1368                                                 TALLOC_CTX *mem_ctx,
1369                                                 int argc,
1370                                                 const char **argv)
1371 {
1372         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1373 }
1374
1375 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1376                                                 const DOM_SID *domain_sid,
1377                                                 const char *domain_name,
1378                                                 struct cli_state *cli,
1379                                                 struct rpc_pipe_client *pipe_hnd,
1380                                                 TALLOC_CTX *mem_ctx,
1381                                                 int argc,
1382                                                 const char **argv)
1383 {
1384         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1385 }
1386
1387 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1388                                                 const DOM_SID *domain_sid,
1389                                                 const char *domain_name,
1390                                                 struct cli_state *cli,
1391                                                 struct rpc_pipe_client *pipe_hnd,
1392                                                 TALLOC_CTX *mem_ctx,
1393                                                 int argc,
1394                                                 const char **argv)
1395 {
1396         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1397 }
1398
1399 /**
1400  * List print-queues w.r.t. their publishing state
1401  *
1402  * All parameters are provided by the run_rpc_command function, except for
1403  * argc, argv which are passed through.
1404  *
1405  * @param c     A net_context structure
1406  * @param domain_sid The domain sid aquired from the remote server
1407  * @param cli A cli_state connected to the server.
1408  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1409  * @param argc  Standard main() style argc
1410  * @param argv  Standard main() style argv.  Initial components are already
1411  *              stripped
1412  *
1413  * @return Normal NTSTATUS return.
1414  **/
1415
1416 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1417                                                 const DOM_SID *domain_sid,
1418                                                 const char *domain_name,
1419                                                 struct cli_state *cli,
1420                                                 struct rpc_pipe_client *pipe_hnd,
1421                                                 TALLOC_CTX *mem_ctx,
1422                                                 int argc,
1423                                                 const char **argv)
1424 {
1425         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1426         uint32 i, num_printers;
1427         uint32 level = 7;
1428         char *printername, *sharename;
1429         PRINTER_INFO_CTR ctr, ctr_pub;
1430         union spoolss_PrinterInfo info;
1431         POLICY_HND hnd;
1432         int state;
1433
1434         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
1435                 return nt_status;
1436
1437         for (i = 0; i < num_printers; i++) {
1438                 ZERO_STRUCT(ctr_pub);
1439
1440                 /* do some initialization */
1441                 rpcstr_pull_talloc(mem_ctx,
1442                                 &printername,
1443                                 ctr.printers_2[i].printername.buffer,
1444                                 -1,
1445                                 STR_TERMINATE);
1446                 rpcstr_pull_talloc(mem_ctx,
1447                                 &sharename,
1448                                 ctr.printers_2[i].sharename.buffer,
1449                                 -1,
1450                                 STR_TERMINATE);
1451                 if (!printername || !sharename) {
1452                         goto done;
1453                 }
1454
1455                 /* open printer handle */
1456                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1457                         PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1458                         goto done;
1459
1460                 /* check for existing dst printer */
1461                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1462                         goto done;
1463
1464                 if (!info.info7.guid) {
1465                         goto done;
1466                 }
1467                 state = info.info7.action;
1468                 switch (state) {
1469                         case DSPRINT_PUBLISH:
1470                                 printf("printer [%s] is published", sharename);
1471                                 if (c->opt_verbose)
1472                                         printf(", guid: %s", info.info7.guid);
1473                                 printf("\n");
1474                                 break;
1475                         case DSPRINT_UNPUBLISH:
1476                                 printf("printer [%s] is unpublished\n", sharename);
1477                                 break;
1478                         case DSPRINT_UPDATE:
1479                                 printf("printer [%s] is currently updating\n", sharename);
1480                                 break;
1481                         default:
1482                                 printf("unkown state: %d\n", state);
1483                                 break;
1484                 }
1485         }
1486
1487         nt_status = NT_STATUS_OK;
1488
1489 done:
1490         if (is_valid_policy_hnd(&hnd))
1491                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1492
1493         return nt_status;
1494 }
1495
1496 /**
1497  * Migrate Printer-ACLs from a source server to the destination server
1498  *
1499  * All parameters are provided by the run_rpc_command function, except for
1500  * argc, argv which are passed through.
1501  *
1502  * @param c     A net_context structure
1503  * @param domain_sid The domain sid aquired from the remote server
1504  * @param cli A cli_state connected to the server.
1505  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1506  * @param argc  Standard main() style argc
1507  * @param argv  Standard main() style argv.  Initial components are already
1508  *              stripped
1509  *
1510  * @return Normal NTSTATUS return.
1511  **/
1512
1513 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1514                                                 const DOM_SID *domain_sid,
1515                                                 const char *domain_name,
1516                                                 struct cli_state *cli,
1517                                                 struct rpc_pipe_client *pipe_hnd,
1518                                                 TALLOC_CTX *mem_ctx,
1519                                                 int argc,
1520                                                 const char **argv)
1521 {
1522         /* TODO: what now, info2 or info3 ?
1523            convince jerry that we should add clientside setacls level 3 at least
1524         */
1525         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1526         uint32 i = 0;
1527         uint32 num_printers;
1528         uint32 level = 2;
1529         char *printername, *sharename;
1530         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1531         POLICY_HND hnd_src, hnd_dst;
1532         PRINTER_INFO_CTR ctr_src, ctr_enum;
1533         struct cli_state *cli_dst = NULL;
1534         union spoolss_PrinterInfo info_src, info_dst;
1535
1536         ZERO_STRUCT(ctr_src);
1537
1538         DEBUG(3,("copying printer ACLs\n"));
1539
1540         /* connect destination PI_SPOOLSS */
1541         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1542                                      &syntax_spoolss);
1543         if (!NT_STATUS_IS_OK(nt_status))
1544                 return nt_status;
1545
1546
1547         /* enum source printers */
1548         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
1549                 nt_status = NT_STATUS_UNSUCCESSFUL;
1550                 goto done;
1551         }
1552
1553         if (!num_printers) {
1554                 printf ("no printers found on server.\n");
1555                 nt_status = NT_STATUS_OK;
1556                 goto done;
1557         }
1558
1559         /* do something for all printers */
1560         for (i = 0; i < num_printers; i++) {
1561                 /* do some initialization */
1562                 rpcstr_pull_talloc(mem_ctx,
1563                                 &printername,
1564                                 ctr_enum.printers_2[i].printername.buffer,
1565                                 -1,
1566                                 STR_TERMINATE);
1567                 rpcstr_pull_talloc(mem_ctx,
1568                                 &sharename,
1569                                 ctr_enum.printers_2[i].sharename.buffer,
1570                                 -1,
1571                                 STR_TERMINATE);
1572                 if (!printername || !sharename) {
1573                         nt_status = NT_STATUS_UNSUCCESSFUL;
1574                         goto done;
1575                 }
1576
1577                 /* we can reset NT_STATUS here because we do not
1578                    get any real NT_STATUS-codes anymore from now on */
1579                 nt_status = NT_STATUS_UNSUCCESSFUL;
1580
1581                 d_printf("migrating printer ACLs for:     [%s] / [%s]\n",
1582                         printername, sharename);
1583
1584                 /* according to msdn you have specify these access-rights
1585                    to see the security descriptor
1586                         - READ_CONTROL (DACL)
1587                         - ACCESS_SYSTEM_SECURITY (SACL)
1588                 */
1589
1590                 /* open src printer handle */
1591                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1592                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1593                         goto done;
1594
1595                 /* open dst printer handle */
1596                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1597                         PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1598                         goto done;
1599
1600                 /* check for existing dst printer */
1601                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1602                         goto done;
1603
1604                 /* check for existing src printer */
1605                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1606                         goto done;
1607
1608                 /* Copy Security Descriptor */
1609
1610                 /* copy secdesc (info level 2) */
1611                 info_dst.info2.devmode = NULL;
1612                 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1613
1614                 if (c->opt_verbose)
1615                         display_sec_desc(info_dst.info2.secdesc);
1616
1617                 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1618                         goto done;
1619
1620                 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1621
1622
1623                 /* close printer handles here */
1624                 if (is_valid_policy_hnd(&hnd_src)) {
1625                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1626                 }
1627
1628                 if (is_valid_policy_hnd(&hnd_dst)) {
1629                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1630                 }
1631
1632         }
1633
1634         nt_status = NT_STATUS_OK;
1635
1636 done:
1637
1638         if (is_valid_policy_hnd(&hnd_src)) {
1639                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1640         }
1641
1642         if (is_valid_policy_hnd(&hnd_dst)) {
1643                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1644         }
1645
1646         if (cli_dst) {
1647                 cli_shutdown(cli_dst);
1648         }
1649         return nt_status;
1650 }
1651
1652 /**
1653  * Migrate printer-forms from a src server to the dst server
1654  *
1655  * All parameters are provided by the run_rpc_command function, except for
1656  * argc, argv which are passed through.
1657  *
1658  * @param c     A net_context structure
1659  * @param domain_sid The domain sid aquired from the remote server
1660  * @param cli A cli_state connected to the server.
1661  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1662  * @param argc  Standard main() style argc
1663  * @param argv  Standard main() style argv.  Initial components are already
1664  *              stripped
1665  *
1666  * @return Normal NTSTATUS return.
1667  **/
1668
1669 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1670                                                 const DOM_SID *domain_sid,
1671                                                 const char *domain_name,
1672                                                 struct cli_state *cli,
1673                                                 struct rpc_pipe_client *pipe_hnd,
1674                                                 TALLOC_CTX *mem_ctx,
1675                                                 int argc,
1676                                                 const char **argv)
1677 {
1678         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1679         WERROR result;
1680         uint32 i, f;
1681         uint32 num_printers;
1682         uint32 level = 1;
1683         char *printername, *sharename;
1684         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1685         POLICY_HND hnd_src, hnd_dst;
1686         PRINTER_INFO_CTR ctr_enum;
1687         union spoolss_PrinterInfo info_dst;
1688         uint32 num_forms;
1689         FORM_1 *forms;
1690         struct cli_state *cli_dst = NULL;
1691
1692         ZERO_STRUCT(ctr_enum);
1693
1694         DEBUG(3,("copying forms\n"));
1695
1696         /* connect destination PI_SPOOLSS */
1697         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1698                                      &syntax_spoolss);
1699         if (!NT_STATUS_IS_OK(nt_status))
1700                 return nt_status;
1701
1702         /* enum src printers */
1703         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
1704                 nt_status = NT_STATUS_UNSUCCESSFUL;
1705                 goto done;
1706         }
1707
1708         if (!num_printers) {
1709                 printf ("no printers found on server.\n");
1710                 nt_status = NT_STATUS_OK;
1711                 goto done;
1712         }
1713
1714         /* do something for all printers */
1715         for (i = 0; i < num_printers; i++) {
1716                 /* do some initialization */
1717                 rpcstr_pull_talloc(mem_ctx,
1718                                 &printername,
1719                                 ctr_enum.printers_2[i].printername.buffer,
1720                                 -1,
1721                                 STR_TERMINATE);
1722                 rpcstr_pull_talloc(mem_ctx,
1723                                 &sharename,
1724                                 ctr_enum.printers_2[i].sharename.buffer,
1725                                 -1,
1726                                 STR_TERMINATE);
1727                 if (!printername || !sharename) {
1728                         nt_status = NT_STATUS_UNSUCCESSFUL;
1729                         goto done;
1730                 }
1731                 /* we can reset NT_STATUS here because we do not
1732                    get any real NT_STATUS-codes anymore from now on */
1733                 nt_status = NT_STATUS_UNSUCCESSFUL;
1734
1735                 d_printf("migrating printer forms for:    [%s] / [%s]\n",
1736                         printername, sharename);
1737
1738
1739                 /* open src printer handle */
1740                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1741                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1742                         goto done;
1743
1744                 /* open dst printer handle */
1745                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1746                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1747                         goto done;
1748
1749                 /* check for existing dst printer */
1750                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1751                         goto done;
1752
1753                 /* finally migrate forms */
1754                 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1755                         goto done;
1756
1757                 DEBUG(1,("got %d forms for printer\n", num_forms));
1758
1759
1760                 for (f = 0; f < num_forms; f++) {
1761
1762                         union spoolss_AddFormInfo info;
1763                         struct spoolss_AddFormInfo1 info1;
1764                         fstring form_name;
1765                         NTSTATUS status;
1766
1767                         /* only migrate FORM_PRINTER types, according to jerry
1768                            FORM_BUILTIN-types are hard-coded in samba */
1769                         if (forms[f].flag != FORM_PRINTER)
1770                                 continue;
1771
1772                         if (forms[f].name.buffer)
1773                                 rpcstr_pull(form_name, forms[f].name.buffer,
1774                                         sizeof(form_name), -1, STR_TERMINATE);
1775
1776                         if (c->opt_verbose)
1777                                 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1778                                         f, form_name, forms[f].flag);
1779
1780                         /* is there a more elegant way to do that ? */
1781                         info1.flags             = FORM_PRINTER;
1782                         info1.size.width        = forms[f].width;
1783                         info1.size.height       = forms[f].length;
1784                         info1.area.left         = forms[f].left;
1785                         info1.area.top          = forms[f].top;
1786                         info1.area.right        = forms[f].right;
1787                         info1.area.bottom       = forms[f].bottom;
1788                         info1.form_name         = form_name;
1789
1790                         info.info1 = &info1;
1791
1792                         /* FIXME: there might be something wrong with samba's
1793                            builtin-forms */
1794                         status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1795                                                         &hnd_dst,
1796                                                         1,
1797                                                         info,
1798                                                         &result);
1799                         if (!W_ERROR_IS_OK(result)) {
1800                                 d_printf("\tAddForm form %d: [%s] refused.\n",
1801                                         f, form_name);
1802                                 continue;
1803                         }
1804
1805                         DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
1806                 }
1807
1808
1809                 /* close printer handles here */
1810                 if (is_valid_policy_hnd(&hnd_src)) {
1811                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1812                 }
1813
1814                 if (is_valid_policy_hnd(&hnd_dst)) {
1815                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1816                 }
1817         }
1818
1819         nt_status = NT_STATUS_OK;
1820
1821 done:
1822
1823         if (is_valid_policy_hnd(&hnd_src))
1824                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1825
1826         if (is_valid_policy_hnd(&hnd_dst))
1827                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1828
1829         if (cli_dst) {
1830                 cli_shutdown(cli_dst);
1831         }
1832         return nt_status;
1833 }
1834
1835 /**
1836  * Migrate printer-drivers from a src server to the dst server
1837  *
1838  * All parameters are provided by the run_rpc_command function, except for
1839  * argc, argv which are passed through.
1840  *
1841  * @param c     A net_context structure
1842  * @param domain_sid The domain sid aquired from the remote server
1843  * @param cli A cli_state connected to the server.
1844  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1845  * @param argc  Standard main() style argc
1846  * @param argv  Standard main() style argv.  Initial components are already
1847  *              stripped
1848  *
1849  * @return Normal NTSTATUS return.
1850  **/
1851
1852 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1853                                                 const DOM_SID *domain_sid,
1854                                                 const char *domain_name,
1855                                                 struct cli_state *cli,
1856                                                 struct rpc_pipe_client *pipe_hnd,
1857                                                 TALLOC_CTX *mem_ctx,
1858                                                 int argc,
1859                                                 const char **argv)
1860 {
1861         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1862         uint32 i, p;
1863         uint32 num_printers;
1864         uint32 level = 3;
1865         char *printername, *sharename;
1866         bool got_src_driver_share = false;
1867         bool got_dst_driver_share = false;
1868         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1869         POLICY_HND hnd_src, hnd_dst;
1870         union spoolss_DriverInfo drv_info_src;
1871         PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
1872         union spoolss_PrinterInfo info_dst;
1873         struct cli_state *cli_dst = NULL;
1874         struct cli_state *cli_share_src = NULL;
1875         struct cli_state *cli_share_dst = NULL;
1876         const char *drivername = NULL;
1877
1878         ZERO_STRUCT(info_ctr_enum);
1879         ZERO_STRUCT(info_ctr_dst);
1880
1881         DEBUG(3,("copying printer-drivers\n"));
1882
1883         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1884                                      &syntax_spoolss);
1885         if (!NT_STATUS_IS_OK(nt_status))
1886                 return nt_status;
1887
1888         /* open print$-share on the src server */
1889         nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1890                         cli->desthost, "print$", "A:");
1891         if (!NT_STATUS_IS_OK(nt_status))
1892                 goto done;
1893
1894         got_src_driver_share = true;
1895
1896
1897         /* open print$-share on the dst server */
1898         nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1899                         cli_dst->desthost, "print$", "A:");
1900         if (!NT_STATUS_IS_OK(nt_status))
1901                 return nt_status;
1902
1903         got_dst_driver_share = true;
1904
1905
1906         /* enum src printers */
1907         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
1908                 nt_status = NT_STATUS_UNSUCCESSFUL;
1909                 goto done;
1910         }
1911
1912         if (num_printers == 0) {
1913                 printf ("no printers found on server.\n");
1914                 nt_status = NT_STATUS_OK;
1915                 goto done;
1916         }
1917
1918
1919         /* do something for all printers */
1920         for (p = 0; p < num_printers; p++) {
1921                 /* do some initialization */
1922                 rpcstr_pull_talloc(mem_ctx,
1923                                 &printername,
1924                                 info_ctr_enum.printers_2[p].printername.buffer,
1925                                 -1,
1926                                 STR_TERMINATE);
1927                 rpcstr_pull_talloc(mem_ctx,
1928                                 &sharename,
1929                                 info_ctr_enum.printers_2[p].sharename.buffer,
1930                                 -1,
1931                                 STR_TERMINATE);
1932                 if (!printername || !sharename) {
1933                         nt_status = NT_STATUS_UNSUCCESSFUL;
1934                         goto done;
1935                 }
1936
1937                 /* we can reset NT_STATUS here because we do not
1938                    get any real NT_STATUS-codes anymore from now on */
1939                 nt_status = NT_STATUS_UNSUCCESSFUL;
1940
1941                 d_printf("migrating printer driver for:   [%s] / [%s]\n",
1942                         printername, sharename);
1943
1944                 /* open dst printer handle */
1945                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1946                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1947                         goto done;
1948
1949                 /* check for existing dst printer */
1950                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1951                         goto done;
1952
1953
1954                 /* open src printer handle */
1955                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1956                                                  MAXIMUM_ALLOWED_ACCESS,
1957                                                  pipe_hnd->auth->user_name,
1958                                                  &hnd_src))
1959                         goto done;
1960
1961                 /* in a first step call getdriver for each shared printer (per arch)
1962                    to get a list of all files that have to be copied */
1963
1964                 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1965
1966                         /* getdriver src */
1967                         if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1968                                         level, archi_table[i].long_archi,
1969                                         archi_table[i].version, &drv_info_src))
1970                                 continue;
1971
1972                         drivername = drv_info_src.info3.driver_name;
1973
1974                         if (c->opt_verbose)
1975                                 display_print_driver3(&drv_info_src.info3);
1976
1977                         /* check arch dir */
1978                         nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1979                         if (!NT_STATUS_IS_OK(nt_status))
1980                                 goto done;
1981
1982
1983                         /* copy driver-files */
1984                         nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1985                                                         archi_table[i].short_archi,
1986                                                         &drv_info_src.info3);
1987                         if (!NT_STATUS_IS_OK(nt_status))
1988                                 goto done;
1989
1990
1991                         /* adddriver dst */
1992                         if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1993                                 nt_status = NT_STATUS_UNSUCCESSFUL;
1994                                 goto done;
1995                         }
1996
1997                         DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1998                                 drivername, printername));
1999
2000                 }
2001
2002                 if (strlen(drivername) == 0) {
2003                         DEBUGADD(1,("Did not get driver for printer %s\n",
2004                                     printername));
2005                         goto done;
2006                 }
2007
2008                 /* setdriver dst */
2009                 info_dst.info2.drivername = drivername;
2010
2011                 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
2012                         nt_status = NT_STATUS_UNSUCCESSFUL;
2013                         goto done;
2014                 }
2015
2016                 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
2017                         drivername, printername));
2018
2019                 /* close dst */
2020                 if (is_valid_policy_hnd(&hnd_dst)) {
2021                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2022                 }
2023
2024                 /* close src */
2025                 if (is_valid_policy_hnd(&hnd_src)) {
2026                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2027                 }
2028         }
2029
2030         nt_status = NT_STATUS_OK;
2031
2032 done:
2033
2034         if (is_valid_policy_hnd(&hnd_src))
2035                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2036
2037         if (is_valid_policy_hnd(&hnd_dst))
2038                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2039
2040         if (cli_dst) {
2041                 cli_shutdown(cli_dst);
2042         }
2043
2044         if (got_src_driver_share)
2045                 cli_shutdown(cli_share_src);
2046
2047         if (got_dst_driver_share)
2048                 cli_shutdown(cli_share_dst);
2049
2050         return nt_status;
2051
2052 }
2053
2054 /**
2055  * Migrate printer-queues from a src to the dst server
2056  * (requires a working "addprinter command" to be installed for the local smbd)
2057  *
2058  * All parameters are provided by the run_rpc_command function, except for
2059  * argc, argv which are passed through.
2060  *
2061  * @param c     A net_context structure
2062  * @param domain_sid The domain sid aquired from the remote server
2063  * @param cli A cli_state connected to the server.
2064  * @param mem_ctx Talloc context, destoyed on compleation of the function.
2065  * @param argc  Standard main() style argc
2066  * @param argv  Standard main() style argv.  Initial components are already
2067  *              stripped
2068  *
2069  * @return Normal NTSTATUS return.
2070  **/
2071
2072 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
2073                                                 const DOM_SID *domain_sid,
2074                                                 const char *domain_name,
2075                                                 struct cli_state *cli,
2076                                                 struct rpc_pipe_client *pipe_hnd,
2077                                                 TALLOC_CTX *mem_ctx,
2078                                                 int argc,
2079                                                 const char **argv)
2080 {
2081         WERROR result;
2082         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2083         uint32 i = 0, num_printers;
2084         uint32 level = 2;
2085         PRINTER_INFO_CTR ctr_enum;
2086         union spoolss_PrinterInfo info_dst, info_src;
2087         struct cli_state *cli_dst = NULL;
2088         POLICY_HND hnd_dst, hnd_src;
2089         char *printername, *sharename;
2090         struct rpc_pipe_client *pipe_hnd_dst = NULL;
2091         struct spoolss_SetPrinterInfoCtr info_ctr;
2092
2093         DEBUG(3,("copying printers\n"));
2094
2095         /* connect destination PI_SPOOLSS */
2096         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2097                                      &syntax_spoolss);
2098         if (!NT_STATUS_IS_OK(nt_status))
2099                 return nt_status;
2100
2101         /* enum printers */
2102         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2103                 nt_status = NT_STATUS_UNSUCCESSFUL;
2104                 goto done;
2105         }
2106
2107         if (!num_printers) {
2108                 printf ("no printers found on server.\n");
2109                 nt_status = NT_STATUS_OK;
2110                 goto done;
2111         }
2112
2113         /* do something for all printers */
2114         for (i = 0; i < num_printers; i++) {
2115                 /* do some initialization */
2116                 rpcstr_pull_talloc(mem_ctx,
2117                                 &printername,
2118                                 ctr_enum.printers_2[i].printername.buffer,
2119                                 -1,
2120                                 STR_TERMINATE);
2121                 rpcstr_pull_talloc(mem_ctx,
2122                                 &sharename,
2123                                 ctr_enum.printers_2[i].sharename.buffer,
2124                                 -1,
2125                                 STR_TERMINATE);
2126                 if (!printername || !sharename) {
2127                         nt_status = NT_STATUS_UNSUCCESSFUL;
2128                         goto done;
2129                 }
2130                 /* we can reset NT_STATUS here because we do not
2131                    get any real NT_STATUS-codes anymore from now on */
2132                 nt_status = NT_STATUS_UNSUCCESSFUL;
2133
2134                 d_printf("migrating printer queue for:    [%s] / [%s]\n",
2135                         printername, sharename);
2136
2137                 /* open dst printer handle */
2138                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2139                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2140
2141                         DEBUG(1,("could not open printer: %s\n", sharename));
2142                 }
2143
2144                 /* check for existing dst printer */
2145                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2146                         printf ("could not get printer, creating printer.\n");
2147                 } else {
2148                         DEBUG(1,("printer already exists: %s\n", sharename));
2149                         /* close printer handle here - dst only, not got src yet. */
2150                         if (is_valid_policy_hnd(&hnd_dst)) {
2151                                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2152                         }
2153                         continue;
2154                 }
2155
2156                 /* now get again src printer ctr via getprinter,
2157                    we first need a handle for that */
2158
2159                 /* open src printer handle */
2160                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2161                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2162                         goto done;
2163
2164                 /* getprinter on the src server */
2165                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2166                         goto done;
2167
2168                 /* copy each src printer to a dst printer 1:1,
2169                    maybe some values have to be changed though */
2170                 d_printf("creating printer: %s\n", printername);
2171
2172                 info_ctr.level = level;
2173                 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info_src.info2;
2174
2175                 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2176                                                      mem_ctx,
2177                                                      &info_ctr);
2178
2179                 if (W_ERROR_IS_OK(result))
2180                         d_printf ("printer [%s] successfully added.\n", printername);
2181                 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2182                         d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2183                 else {
2184                         d_fprintf (stderr, "could not create printer [%s]\n", printername);
2185                         goto done;
2186                 }
2187
2188                 /* close printer handles here */
2189                 if (is_valid_policy_hnd(&hnd_src)) {
2190                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2191                 }
2192
2193                 if (is_valid_policy_hnd(&hnd_dst)) {
2194                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2195                 }
2196         }
2197
2198         nt_status = NT_STATUS_OK;
2199
2200 done:
2201         if (is_valid_policy_hnd(&hnd_src))
2202                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2203
2204         if (is_valid_policy_hnd(&hnd_dst))
2205                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2206
2207         if (cli_dst) {
2208                 cli_shutdown(cli_dst);
2209         }
2210         return nt_status;
2211 }
2212
2213 /**
2214  * Migrate Printer-Settings from a src server to the dst server
2215  * (for this to work, printers and drivers already have to be migrated earlier)
2216  *
2217  * All parameters are provided by the run_rpc_command function, except for
2218  * argc, argv which are passed through.
2219  *
2220  * @param c     A net_context structure
2221  * @param domain_sid The domain sid aquired from the remote server
2222  * @param cli A cli_state connected to the server.
2223  * @param mem_ctx Talloc context, destoyed on compleation of the function.
2224  * @param argc  Standard main() style argc
2225  * @param argv  Standard main() style argv.  Initial components are already
2226  *              stripped
2227  *
2228  * @return Normal NTSTATUS return.
2229  **/
2230
2231 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2232                                                 const DOM_SID *domain_sid,
2233                                                 const char *domain_name,
2234                                                 struct cli_state *cli,
2235                                                 struct rpc_pipe_client *pipe_hnd,
2236                                                 TALLOC_CTX *mem_ctx,
2237                                                 int argc,
2238                                                 const char **argv)
2239 {
2240
2241         /* FIXME: Here the nightmare begins */
2242
2243         WERROR result;
2244         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2245         uint32 i = 0, p = 0, j = 0;
2246         uint32 num_printers, val_needed, data_needed;
2247         uint32 level = 2;
2248         char *printername, *sharename;
2249         struct rpc_pipe_client *pipe_hnd_dst = NULL;
2250         POLICY_HND hnd_src, hnd_dst;
2251         PRINTER_INFO_CTR ctr_enum;
2252         union spoolss_PrinterInfo info_dst_publish, info_dst;
2253         REGVAL_CTR *reg_ctr;
2254         struct cli_state *cli_dst = NULL;
2255         char *devicename = NULL, *unc_name = NULL, *url = NULL;
2256         const char *longname;
2257
2258         uint16 *keylist = NULL, *curkey;
2259
2260         ZERO_STRUCT(ctr_enum);
2261         /* FIXME GD */
2262         ZERO_STRUCT(info_dst_publish);
2263
2264         DEBUG(3,("copying printer settings\n"));
2265
2266         /* connect destination PI_SPOOLSS */
2267         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2268                                      &syntax_spoolss);
2269         if (!NT_STATUS_IS_OK(nt_status))
2270                 return nt_status;
2271
2272         /* enum src printers */
2273         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
2274                 nt_status = NT_STATUS_UNSUCCESSFUL;
2275                 goto done;
2276         }
2277
2278         if (!num_printers) {
2279                 printf ("no printers found on server.\n");
2280                 nt_status = NT_STATUS_OK;
2281                 goto done;
2282         }
2283
2284
2285         /* needed for dns-strings in regkeys */
2286         longname = get_mydnsfullname();
2287         if (!longname) {
2288                 nt_status = NT_STATUS_UNSUCCESSFUL;
2289                 goto done;
2290         }
2291
2292         /* do something for all printers */
2293         for (i = 0; i < num_printers; i++) {
2294                 /* do some initialization */
2295                 rpcstr_pull_talloc(mem_ctx,
2296                                 &printername,
2297                                 ctr_enum.printers_2[i].printername.buffer,
2298                                 -1,
2299                                 STR_TERMINATE);
2300                 rpcstr_pull_talloc(mem_ctx,
2301                                 &sharename,
2302                                 ctr_enum.printers_2[i].sharename.buffer,
2303                                 -1,
2304                                 STR_TERMINATE);
2305                 if (!printername || !sharename) {
2306                         nt_status = NT_STATUS_UNSUCCESSFUL;
2307                         goto done;
2308                 }
2309                 /* we can reset NT_STATUS here because we do not
2310                    get any real NT_STATUS-codes anymore from now on */
2311                 nt_status = NT_STATUS_UNSUCCESSFUL;
2312
2313                 d_printf("migrating printer settings for: [%s] / [%s]\n",
2314                         printername, sharename);
2315
2316
2317                 /* open src printer handle */
2318                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2319                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2320                         goto done;
2321
2322                 /* open dst printer handle */
2323                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2324                         PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2325                         goto done;
2326
2327                 /* check for existing dst printer */
2328                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2329                                 level, &info_dst))
2330                         goto done;
2331
2332 #if 0 /* FIXME GD */
2333
2334                 /* STEP 1: COPY DEVICE-MODE and other
2335                            PRINTER_INFO_2-attributes
2336                 */
2337
2338                 info_dst.info2 = &ctr_enum.printers_2[i];
2339
2340                 /* why is the port always disconnected when the printer
2341                    is correctly installed (incl. driver ???) */
2342                 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2343
2344                 /* check if printer is published */
2345                 if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2346
2347                         /* check for existing dst printer */
2348                         if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2349                                 goto done;
2350
2351                         info_dst_publish.info7.action = DSPRINT_PUBLISH;
2352
2353                         /* ignore false from setprinter due to WERR_IO_PENDING */
2354                         net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2355
2356                         DEBUG(3,("republished printer\n"));
2357                 }
2358
2359                 if (ctr_enum.printers_2[i].devmode != NULL) {
2360
2361                         /* copy devmode (info level 2) */
2362                         info_dst.info2.devmode = (DEVICEMODE *)
2363                                 TALLOC_MEMDUP(mem_ctx,
2364                                               ctr_enum.printers_2[i].devmode,
2365                                               sizeof(DEVICEMODE));
2366
2367                         /* do not copy security descriptor (we have another
2368                          * command for that) */
2369                         info_dst.info2.secdesc = NULL;
2370
2371 #if 0
2372                         if (asprintf(&devicename, "\\\\%s\\%s", longname,
2373                                      printername) < 0) {
2374                                 nt_status = NT_STATUS_NO_MEMORY;
2375                                 goto done;
2376                         }
2377
2378                         init_unistr(&ctr_dst.printers_2->devmode->devicename,
2379                                     devicename);
2380 #endif
2381                         if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2382                                                     level, &info_dst))
2383                                 goto done;
2384
2385                         DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2386                 }
2387 #endif
2388                 /* STEP 2: COPY REGISTRY VALUES */
2389
2390                 /* please keep in mind that samba parse_spools gives horribly
2391                    crippled results when used to rpccli_spoolss_enumprinterdataex
2392                    a win2k3-server.  (Bugzilla #1851)
2393                    FIXME: IIRC I've seen it too on a win2k-server
2394                 */
2395
2396                 /* enumerate data on src handle */
2397                 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2398                         &val_needed, &data_needed, NULL);
2399
2400                 /* loop for all printerdata of "PrinterDriverData" */
2401                 while (W_ERROR_IS_OK(result)) {
2402
2403                         REGISTRY_VALUE value;
2404
2405                         result = rpccli_spoolss_enumprinterdata(
2406                                 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2407                                 data_needed, 0, 0, &value);
2408
2409                         /* loop for all reg_keys */
2410                         if (W_ERROR_IS_OK(result)) {
2411
2412                                 /* display_value */
2413                                 if (c->opt_verbose)
2414                                         display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2415
2416                                 /* set_value */
2417                                 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2418                                                                 &hnd_dst, &value))
2419                                         goto done;
2420
2421                                 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2422                                         value.valuename));
2423                         }
2424                 }
2425
2426                 /* STEP 3: COPY SUBKEY VALUES */
2427
2428                 /* here we need to enum all printer_keys and then work
2429                    on the result with enum_printer_key_ex. nt4 does not
2430                    respond to enumprinterkey, win2k does, so continue
2431                    in case of an error */
2432
2433                 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2434                         printf("got no key-data\n");
2435                         continue;
2436                 }
2437
2438
2439                 /* work on a list of printer keys
2440                    each key has to be enumerated to get all required
2441                    information.  information is then set via setprinterdataex-calls */
2442
2443                 if (keylist == NULL)
2444                         continue;
2445
2446                 curkey = keylist;
2447                 while (*curkey != 0) {
2448                         char *subkey;
2449                         rpcstr_pull_talloc(mem_ctx,
2450                                         &subkey,
2451                                         curkey,
2452                                         -1,
2453                                         STR_TERMINATE);
2454                         if (!subkey) {
2455                                 return NT_STATUS_NO_MEMORY;
2456                         }
2457
2458                         curkey += strlen(subkey) + 1;
2459
2460                         if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2461                                 return NT_STATUS_NO_MEMORY;
2462
2463                         /* enumerate all src subkeys */
2464                         if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2465                                                            &hnd_src, subkey,
2466                                                            reg_ctr))
2467                                 goto done;
2468
2469                         for (j=0; j < reg_ctr->num_values; j++) {
2470
2471                                 REGISTRY_VALUE value;
2472                                 UNISTR2 data;
2473
2474                                 /* although samba replies with sane data in most cases we
2475                                    should try to avoid writing wrong registry data */
2476
2477                                 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2478                                     strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2479                                     strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2480                                     strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2481                                     strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2482
2483                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2484
2485                                                 /* although windows uses a multi-sz, we use a sz */
2486                                                 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2487                                                 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2488                                         }
2489
2490                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2491
2492                                                 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2493                                                         nt_status = NT_STATUS_NO_MEMORY;
2494                                                         goto done;
2495                                                 }
2496                                                 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2497                                                 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2498                                         }
2499
2500                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2501
2502                                                 continue;
2503
2504 #if 0
2505                                                 /* FIXME: should we really do that ??? */
2506                                                 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2507                                                         nt_status = NT_STATUS_NO_MEMORY;
2508                                                         goto done;
2509                                                 }
2510                                                 init_unistr2(&data, url, UNI_STR_TERMINATE);
2511                                                 fstrcpy(value.valuename, SPOOL_REG_URL);
2512 #endif
2513                                         }
2514
2515                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2516
2517                                                 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2518                                                 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2519                                         }
2520
2521                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2522
2523                                                 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2524                                                 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2525                                         }
2526
2527                                         value.type = REG_SZ;
2528                                         value.size = data.uni_str_len * 2;
2529                                         if (value.size) {
2530                                                 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2531                                         } else {
2532                                                 value.data_p = NULL;
2533                                         }
2534
2535                                         if (c->opt_verbose)
2536                                                 display_reg_value(subkey, value);
2537
2538                                         /* here we have to set all subkeys on the dst server */
2539                                         if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2540                                                         subkey, &value))
2541                                                 goto done;
2542
2543                                 } else {
2544
2545                                         if (c->opt_verbose)
2546                                                 display_reg_value(subkey, *(reg_ctr->values[j]));
2547
2548                                         /* here we have to set all subkeys on the dst server */
2549                                         if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2550                                                         subkey, reg_ctr->values[j]))
2551                                                 goto done;
2552
2553                                 }
2554
2555                                 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2556                                                 subkey, reg_ctr->values[j]->valuename));
2557
2558                         }
2559
2560                         TALLOC_FREE( reg_ctr );
2561                 }
2562
2563                 SAFE_FREE(keylist);
2564
2565                 /* close printer handles here */
2566                 if (is_valid_policy_hnd(&hnd_src)) {
2567                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2568                 }
2569
2570                 if (is_valid_policy_hnd(&hnd_dst)) {
2571                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2572                 }
2573
2574         }
2575
2576         nt_status = NT_STATUS_OK;
2577
2578 done:
2579         SAFE_FREE(devicename);
2580         SAFE_FREE(url);
2581         SAFE_FREE(unc_name);
2582
2583         if (is_valid_policy_hnd(&hnd_src))
2584                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2585
2586         if (is_valid_policy_hnd(&hnd_dst))
2587                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2588
2589         if (cli_dst) {
2590                 cli_shutdown(cli_dst);
2591         }
2592         return nt_status;
2593 }