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