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