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