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