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