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