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