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