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