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