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