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