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