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