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