s3-net: temporary disable net_spoolss_setprinterdata.
[samba.git] / source3 / utils / net_rpc_printer.c
1 /*
2    Samba Unix/Linux SMB client library
3    Distributed SMB/CIFS Server Management Utility
4    Copyright (C) 2004 Guenther Deschner (gd@samba.org)
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "includes.h"
20 #include "utils/net.h"
21
22 struct table_node {
23         const char *long_archi;
24         const char *short_archi;
25         int version;
26 };
27
28
29 /* support itanium as well */
30 static const struct table_node archi_table[]= {
31
32         {"Windows 4.0",          "WIN40",       0 },
33         {"Windows NT x86",       "W32X86",      2 },
34         {"Windows NT x86",       "W32X86",      3 },
35         {"Windows NT R4000",     "W32MIPS",     2 },
36         {"Windows NT Alpha_AXP", "W32ALPHA",    2 },
37         {"Windows NT PowerPC",   "W32PPC",      2 },
38         {"Windows IA64",         "IA64",        3 },
39         {"Windows x64",          "x64",         3 },
40         {NULL,                   "",            -1 }
41 };
42
43
44 /**
45  * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
46  * It is here for debugging purpose and should be removed later on.
47  **/
48
49 /****************************************************************************
50  Printer info level 3 display function.
51 ****************************************************************************/
52
53 static void display_print_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         /* FIXME - GD */
825         return true;
826
827         /* setprinterdata call */
828         /* result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value); */
829
830         if (!W_ERROR_IS_OK(result)) {
831                 printf ("unable to set printerdata: %s\n", win_errstr(result));
832                 return false;
833         }
834
835         return true;
836 }
837
838
839 static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
840                                         TALLOC_CTX *mem_ctx,
841                                         POLICY_HND *hnd,
842                                         const char *keyname,
843                                         uint16 **keylist)
844 {
845         WERROR result;
846
847         /* enumprinterkey call */
848         result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
849
850         if (!W_ERROR_IS_OK(result)) {
851                 printf("enumprinterkey failed: %s\n", win_errstr(result));
852                 return false;
853         }
854
855         return true;
856 }
857
858 static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
859                                         TALLOC_CTX *mem_ctx,
860                                         uint32 offered,
861                                         POLICY_HND *hnd,
862                                         const char *keyname,
863                                         REGVAL_CTR *ctr)
864 {
865         WERROR result;
866
867         /* enumprinterdataex call */
868         result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
869
870         if (!W_ERROR_IS_OK(result)) {
871                 printf("enumprinterdataex failed: %s\n", win_errstr(result));
872                 return false;
873         }
874
875         return true;
876 }
877
878
879 static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
880                                         TALLOC_CTX *mem_ctx,
881                                         POLICY_HND *hnd,
882                                         char *keyname,
883                                         REGISTRY_VALUE *value)
884 {
885         WERROR result;
886         NTSTATUS status;
887
888         /* setprinterdataex call */
889         status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
890                                                  hnd,
891                                                  keyname,
892                                                  value->valuename,
893                                                  value->type,
894                                                  value->data_p,
895                                                  value->size,
896                                                  &result);
897
898         if (!W_ERROR_IS_OK(result)) {
899                 printf("could not set printerdataex: %s\n", win_errstr(result));
900                 return false;
901         }
902
903         return true;
904 }
905
906 static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
907                                 TALLOC_CTX *mem_ctx,
908                                 POLICY_HND *hnd,
909                                 int level,
910                                 uint32_t *num_forms,
911                                 union spoolss_FormInfo **forms)
912 {
913         WERROR result;
914
915         /* enumforms call */
916         result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx,
917                                           hnd,
918                                           level,
919                                           0,
920                                           num_forms,
921                                           forms);
922         if (!W_ERROR_IS_OK(result)) {
923                 printf("could not enum forms: %s\n", win_errstr(result));
924                 return false;
925         }
926
927         return true;
928 }
929
930 static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
931                                         TALLOC_CTX *mem_ctx,
932                                         uint32 level, const char *env,
933                                         uint32 *count,
934                                         union spoolss_DriverInfo **info)
935 {
936         WERROR result;
937
938         /* enumprinterdrivers call */
939         result = rpccli_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx,
940                                                    pipe_hnd->srv_name_slash,
941                                                    env,
942                                                    level,
943                                                    0,
944                                                    count,
945                                                    info);
946         if (!W_ERROR_IS_OK(result)) {
947                 printf("cannot enum drivers: %s\n", win_errstr(result));
948                 return false;
949         }
950
951         return true;
952 }
953
954 static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
955                              TALLOC_CTX *mem_ctx,
956                              POLICY_HND *hnd, uint32 level,
957                              const char *env, int version,
958                              union spoolss_DriverInfo *info)
959 {
960         WERROR result;
961         uint32_t server_major_version;
962         uint32_t server_minor_version;
963
964         /* getprinterdriver call */
965         result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
966                                                   hnd,
967                                                   env,
968                                                   level,
969                                                   0,
970                                                   version,
971                                                   2,
972                                                   info,
973                                                   &server_major_version,
974                                                   &server_minor_version);
975         if (!W_ERROR_IS_OK(result)) {
976                 DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
977                         env, win_errstr(result)));
978                 if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
979                     W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
980                         printf("cannot get driver: %s\n", win_errstr(result));
981                 }
982                 return false;
983         }
984
985         return true;
986 }
987
988
989 static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
990                              TALLOC_CTX *mem_ctx, uint32 level,
991                              union spoolss_DriverInfo *info)
992 {
993         WERROR result;
994         NTSTATUS status;
995         struct spoolss_AddDriverInfoCtr info_ctr;
996
997         info_ctr.level = level;
998
999         switch (level) {
1000         case 2:
1001                 info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)&info->info2;
1002                 break;
1003         case 3:
1004                 info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)&info->info3;
1005                 break;
1006         default:
1007                 printf("unsupported info level: %d\n", level);
1008                 return false;
1009         }
1010
1011         /* addprinterdriver call */
1012         status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
1013                                                  pipe_hnd->srv_name_slash,
1014                                                  &info_ctr,
1015                                                  &result);
1016         /* be more verbose */
1017         if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
1018                 printf("You are not allowed to add drivers\n");
1019                 return false;
1020         }
1021         if (!W_ERROR_IS_OK(result)) {
1022                 printf("cannot add driver: %s\n", win_errstr(result));
1023                 return false;
1024         }
1025
1026         return true;
1027 }
1028
1029 /**
1030  * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
1031  * for a single printer or for all printers depending on argc/argv
1032  **/
1033
1034 static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
1035                         TALLOC_CTX *mem_ctx,
1036                         int level,
1037                         int argc,
1038                         const char **argv,
1039                         uint32 *num_printers,
1040                         union spoolss_PrinterInfo **info_p)
1041 {
1042         POLICY_HND hnd;
1043
1044         /* no arguments given, enumerate all printers */
1045         if (argc == 0) {
1046
1047                 if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
1048                                 PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
1049                                 level, num_printers, info_p))
1050                         return false;
1051
1052                 goto out;
1053         }
1054
1055         /* argument given, get a single printer by name */
1056         if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
1057                                          MAXIMUM_ALLOWED_ACCESS,
1058                                          pipe_hnd->auth->user_name,
1059                                          &hnd))
1060                 return false;
1061
1062         if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
1063                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1064                 return false;
1065         }
1066
1067         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1068
1069         *num_printers = 1;
1070
1071 out:
1072         DEBUG(3,("got %d printers\n", *num_printers));
1073
1074         return true;
1075
1076 }
1077
1078 /**
1079  * List print-queues (including local printers that are not shared)
1080  *
1081  * All parameters are provided by the run_rpc_command function, except for
1082  * argc, argv which are passed through.
1083  *
1084  * @param c     A net_context structure
1085  * @param domain_sid The domain sid aquired from the remote server
1086  * @param cli A cli_state connected to the server.
1087  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1088  * @param argc  Standard main() style argc
1089  * @param argv  Standard main() style argv.  Initial components are already
1090  *              stripped
1091  *
1092  * @return Normal NTSTATUS return.
1093  **/
1094
1095 NTSTATUS rpc_printer_list_internals(struct net_context *c,
1096                                         const DOM_SID *domain_sid,
1097                                         const char *domain_name,
1098                                         struct cli_state *cli,
1099                                         struct rpc_pipe_client *pipe_hnd,
1100                                         TALLOC_CTX *mem_ctx,
1101                                         int argc,
1102                                         const char **argv)
1103 {
1104         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1105         uint32 i, num_printers;
1106         uint32 level = 2;
1107         const char *printername, *sharename;
1108         union spoolss_PrinterInfo *info;
1109
1110         printf("listing printers\n");
1111
1112         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info))
1113                 return nt_status;
1114
1115         for (i = 0; i < num_printers; i++) {
1116
1117                 /* do some initialization */
1118                 printername = info[i].info2.printername;
1119                 sharename = info[i].info2.sharename;
1120
1121                 if (printername && sharename) {
1122                         d_printf("printer %d: %s, shared as: %s\n",
1123                                 i+1, printername, sharename);
1124                 }
1125         }
1126
1127         return NT_STATUS_OK;
1128 }
1129
1130 /**
1131  * List printer-drivers from a server
1132  *
1133  * All parameters are provided by the run_rpc_command function, except for
1134  * argc, argv which are passed through.
1135  *
1136  * @param c     A net_context structure
1137  * @param domain_sid The domain sid aquired from the remote server
1138  * @param cli A cli_state connected to the server.
1139  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1140  * @param argc  Standard main() style argc
1141  * @param argv  Standard main() style argv.  Initial components are already
1142  *              stripped
1143  *
1144  * @return Normal NTSTATUS return.
1145  **/
1146
1147 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
1148                                                 const DOM_SID *domain_sid,
1149                                                 const char *domain_name,
1150                                                 struct cli_state *cli,
1151                                                 struct rpc_pipe_client *pipe_hnd,
1152                                                 TALLOC_CTX *mem_ctx,
1153                                                 int argc,
1154                                                 const char **argv)
1155 {
1156         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1157         uint32 i;
1158         uint32 level = 3;
1159         union spoolss_DriverInfo *info;
1160         int d;
1161
1162         printf("listing printer-drivers\n");
1163
1164         for (i=0; archi_table[i].long_archi!=NULL; i++) {
1165
1166                 uint32 num_drivers;
1167
1168                 /* enum remote drivers */
1169                 if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
1170                                 archi_table[i].long_archi,
1171                                 &num_drivers, &info)) {
1172                         nt_status = NT_STATUS_UNSUCCESSFUL;
1173                         goto done;
1174                 }
1175
1176                 if (num_drivers == 0) {
1177                         d_printf ("no drivers found on server for architecture: [%s].\n",
1178                                 archi_table[i].long_archi);
1179                         continue;
1180                 }
1181
1182                 d_printf("got %d printer-drivers for architecture: [%s]\n",
1183                         num_drivers, archi_table[i].long_archi);
1184
1185
1186                 /* do something for all drivers for architecture */
1187                 for (d = 0; d < num_drivers; d++) {
1188                         display_print_driver3(&info[d].info3);
1189                 }
1190         }
1191
1192         nt_status = NT_STATUS_OK;
1193
1194 done:
1195         return nt_status;
1196
1197 }
1198
1199 /**
1200  * Publish print-queues with args-wrapper
1201  *
1202  * @param cli A cli_state connected to the server.
1203  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1204  * @param argc  Standard main() style argc
1205  * @param argv  Standard main() style argv.  Initial components are already
1206  *              stripped
1207  * @param action
1208  *
1209  * @return Normal NTSTATUS return.
1210  **/
1211
1212 static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
1213                                         TALLOC_CTX *mem_ctx,
1214                                         int argc,
1215                                         const char **argv,
1216                                         uint32 action)
1217 {
1218         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1219         uint32 i, num_printers;
1220         uint32 level = 7;
1221         const char *printername, *sharename;
1222         union spoolss_PrinterInfo *info_enum;
1223         union spoolss_PrinterInfo info;
1224         struct spoolss_SetPrinterInfoCtr info_ctr;
1225         struct spoolss_DevmodeContainer devmode_ctr;
1226         struct sec_desc_buf secdesc_ctr;
1227         POLICY_HND hnd;
1228         WERROR result;
1229         const char *action_str;
1230
1231         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1232                 return nt_status;
1233
1234         for (i = 0; i < num_printers; i++) {
1235
1236                 /* do some initialization */
1237                 printername = info_enum[i].info2.printername;
1238                 sharename = info_enum[i].info2.sharename;
1239                 if (!printername || !sharename) {
1240                         goto done;
1241                 }
1242
1243                 /* open printer handle */
1244                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1245                         PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
1246                         goto done;
1247
1248                 /* check for existing dst printer */
1249                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1250                         goto done;
1251
1252                 /* check action and set string */
1253                 switch (action) {
1254                 case DSPRINT_PUBLISH:
1255                         action_str = "published";
1256                         break;
1257                 case DSPRINT_UPDATE:
1258                         action_str = "updated";
1259                         break;
1260                 case DSPRINT_UNPUBLISH:
1261                         action_str = "unpublished";
1262                         break;
1263                 default:
1264                         action_str = "unknown action";
1265                         printf("unkown action: %d\n", action);
1266                         break;
1267                 }
1268
1269                 info.info7.action = action;
1270                 info_ctr.level = 7;
1271                 info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info.info7;
1272
1273                 ZERO_STRUCT(devmode_ctr);
1274                 ZERO_STRUCT(secdesc_ctr);
1275
1276                 nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
1277                                                       &hnd,
1278                                                       &info_ctr,
1279                                                       &devmode_ctr,
1280                                                       &secdesc_ctr,
1281                                                       0, /* command */
1282                                                       &result);
1283
1284                 if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
1285                         printf("cannot set printer-info: %s\n", win_errstr(result));
1286                         goto done;
1287                 }
1288
1289                 printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
1290         }
1291
1292         nt_status = NT_STATUS_OK;
1293
1294 done:
1295         if (is_valid_policy_hnd(&hnd))
1296                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1297
1298         return nt_status;
1299 }
1300
1301 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
1302                                                 const DOM_SID *domain_sid,
1303                                                 const char *domain_name,
1304                                                 struct cli_state *cli,
1305                                                 struct rpc_pipe_client *pipe_hnd,
1306                                                 TALLOC_CTX *mem_ctx,
1307                                                 int argc,
1308                                                 const char **argv)
1309 {
1310         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_PUBLISH);
1311 }
1312
1313 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
1314                                                 const DOM_SID *domain_sid,
1315                                                 const char *domain_name,
1316                                                 struct cli_state *cli,
1317                                                 struct rpc_pipe_client *pipe_hnd,
1318                                                 TALLOC_CTX *mem_ctx,
1319                                                 int argc,
1320                                                 const char **argv)
1321 {
1322         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UNPUBLISH);
1323 }
1324
1325 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
1326                                                 const DOM_SID *domain_sid,
1327                                                 const char *domain_name,
1328                                                 struct cli_state *cli,
1329                                                 struct rpc_pipe_client *pipe_hnd,
1330                                                 TALLOC_CTX *mem_ctx,
1331                                                 int argc,
1332                                                 const char **argv)
1333 {
1334         return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, DSPRINT_UPDATE);
1335 }
1336
1337 /**
1338  * List print-queues w.r.t. their publishing state
1339  *
1340  * All parameters are provided by the run_rpc_command function, except for
1341  * argc, argv which are passed through.
1342  *
1343  * @param c     A net_context structure
1344  * @param domain_sid The domain sid aquired from the remote server
1345  * @param cli A cli_state connected to the server.
1346  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1347  * @param argc  Standard main() style argc
1348  * @param argv  Standard main() style argv.  Initial components are already
1349  *              stripped
1350  *
1351  * @return Normal NTSTATUS return.
1352  **/
1353
1354 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
1355                                                 const DOM_SID *domain_sid,
1356                                                 const char *domain_name,
1357                                                 struct cli_state *cli,
1358                                                 struct rpc_pipe_client *pipe_hnd,
1359                                                 TALLOC_CTX *mem_ctx,
1360                                                 int argc,
1361                                                 const char **argv)
1362 {
1363         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1364         uint32 i, num_printers;
1365         uint32 level = 7;
1366         const char *printername, *sharename;
1367         union spoolss_PrinterInfo *info_enum;
1368         union spoolss_PrinterInfo info;
1369         POLICY_HND hnd;
1370         int state;
1371
1372         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
1373                 return nt_status;
1374
1375         for (i = 0; i < num_printers; i++) {
1376
1377                 /* do some initialization */
1378                 printername = info_enum[i].info2.printername;
1379                 sharename = info_enum[i].info2.sharename;
1380
1381                 if (!printername || !sharename) {
1382                         goto done;
1383                 }
1384
1385                 /* open printer handle */
1386                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1387                         PRINTER_ALL_ACCESS, cli->user_name, &hnd))
1388                         goto done;
1389
1390                 /* check for existing dst printer */
1391                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
1392                         goto done;
1393
1394                 if (!info.info7.guid) {
1395                         goto done;
1396                 }
1397                 state = info.info7.action;
1398                 switch (state) {
1399                         case DSPRINT_PUBLISH:
1400                                 printf("printer [%s] is published", sharename);
1401                                 if (c->opt_verbose)
1402                                         printf(", guid: %s", info.info7.guid);
1403                                 printf("\n");
1404                                 break;
1405                         case DSPRINT_UNPUBLISH:
1406                                 printf("printer [%s] is unpublished\n", sharename);
1407                                 break;
1408                         case DSPRINT_UPDATE:
1409                                 printf("printer [%s] is currently updating\n", sharename);
1410                                 break;
1411                         default:
1412                                 printf("unkown state: %d\n", state);
1413                                 break;
1414                 }
1415         }
1416
1417         nt_status = NT_STATUS_OK;
1418
1419 done:
1420         if (is_valid_policy_hnd(&hnd))
1421                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
1422
1423         return nt_status;
1424 }
1425
1426 /**
1427  * Migrate Printer-ACLs from a source server to the destination server
1428  *
1429  * All parameters are provided by the run_rpc_command function, except for
1430  * argc, argv which are passed through.
1431  *
1432  * @param c     A net_context structure
1433  * @param domain_sid The domain sid aquired from the remote server
1434  * @param cli A cli_state connected to the server.
1435  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1436  * @param argc  Standard main() style argc
1437  * @param argv  Standard main() style argv.  Initial components are already
1438  *              stripped
1439  *
1440  * @return Normal NTSTATUS return.
1441  **/
1442
1443 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
1444                                                 const DOM_SID *domain_sid,
1445                                                 const char *domain_name,
1446                                                 struct cli_state *cli,
1447                                                 struct rpc_pipe_client *pipe_hnd,
1448                                                 TALLOC_CTX *mem_ctx,
1449                                                 int argc,
1450                                                 const char **argv)
1451 {
1452         /* TODO: what now, info2 or info3 ?
1453            convince jerry that we should add clientside setacls level 3 at least
1454         */
1455         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1456         uint32 i = 0;
1457         uint32 num_printers;
1458         uint32 level = 2;
1459         const char *printername, *sharename;
1460         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1461         POLICY_HND hnd_src, hnd_dst;
1462         union spoolss_PrinterInfo *info_enum;
1463         struct cli_state *cli_dst = NULL;
1464         union spoolss_PrinterInfo info_src, info_dst;
1465
1466         DEBUG(3,("copying printer ACLs\n"));
1467
1468         /* connect destination PI_SPOOLSS */
1469         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1470                                      &syntax_spoolss);
1471         if (!NT_STATUS_IS_OK(nt_status))
1472                 return nt_status;
1473
1474
1475         /* enum source printers */
1476         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1477                 nt_status = NT_STATUS_UNSUCCESSFUL;
1478                 goto done;
1479         }
1480
1481         if (!num_printers) {
1482                 printf ("no printers found on server.\n");
1483                 nt_status = NT_STATUS_OK;
1484                 goto done;
1485         }
1486
1487         /* do something for all printers */
1488         for (i = 0; i < num_printers; i++) {
1489
1490                 /* do some initialization */
1491                 printername = info_enum[i].info2.printername;
1492                 sharename = info_enum[i].info2.sharename;
1493
1494                 if (!printername || !sharename) {
1495                         nt_status = NT_STATUS_UNSUCCESSFUL;
1496                         goto done;
1497                 }
1498
1499                 /* we can reset NT_STATUS here because we do not
1500                    get any real NT_STATUS-codes anymore from now on */
1501                 nt_status = NT_STATUS_UNSUCCESSFUL;
1502
1503                 d_printf("migrating printer ACLs for:     [%s] / [%s]\n",
1504                         printername, sharename);
1505
1506                 /* according to msdn you have specify these access-rights
1507                    to see the security descriptor
1508                         - READ_CONTROL (DACL)
1509                         - ACCESS_SYSTEM_SECURITY (SACL)
1510                 */
1511
1512                 /* open src printer handle */
1513                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1514                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1515                         goto done;
1516
1517                 /* open dst printer handle */
1518                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1519                         PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
1520                         goto done;
1521
1522                 /* check for existing dst printer */
1523                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1524                         goto done;
1525
1526                 /* check for existing src printer */
1527                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
1528                         goto done;
1529
1530                 /* Copy Security Descriptor */
1531
1532                 /* copy secdesc (info level 2) */
1533                 info_dst.info2.devmode = NULL;
1534                 info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
1535
1536                 if (c->opt_verbose)
1537                         display_sec_desc(info_dst.info2.secdesc);
1538
1539                 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1540                         goto done;
1541
1542                 DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
1543
1544
1545                 /* close printer handles here */
1546                 if (is_valid_policy_hnd(&hnd_src)) {
1547                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1548                 }
1549
1550                 if (is_valid_policy_hnd(&hnd_dst)) {
1551                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1552                 }
1553
1554         }
1555
1556         nt_status = NT_STATUS_OK;
1557
1558 done:
1559
1560         if (is_valid_policy_hnd(&hnd_src)) {
1561                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1562         }
1563
1564         if (is_valid_policy_hnd(&hnd_dst)) {
1565                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1566         }
1567
1568         if (cli_dst) {
1569                 cli_shutdown(cli_dst);
1570         }
1571         return nt_status;
1572 }
1573
1574 /**
1575  * Migrate printer-forms from a src server to the dst server
1576  *
1577  * All parameters are provided by the run_rpc_command function, except for
1578  * argc, argv which are passed through.
1579  *
1580  * @param c     A net_context structure
1581  * @param domain_sid The domain sid aquired from the remote server
1582  * @param cli A cli_state connected to the server.
1583  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1584  * @param argc  Standard main() style argc
1585  * @param argv  Standard main() style argv.  Initial components are already
1586  *              stripped
1587  *
1588  * @return Normal NTSTATUS return.
1589  **/
1590
1591 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
1592                                                 const DOM_SID *domain_sid,
1593                                                 const char *domain_name,
1594                                                 struct cli_state *cli,
1595                                                 struct rpc_pipe_client *pipe_hnd,
1596                                                 TALLOC_CTX *mem_ctx,
1597                                                 int argc,
1598                                                 const char **argv)
1599 {
1600         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1601         WERROR result;
1602         uint32 i, f;
1603         uint32 num_printers;
1604         uint32 level = 1;
1605         const char *printername, *sharename;
1606         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1607         POLICY_HND hnd_src, hnd_dst;
1608         union spoolss_PrinterInfo *info_enum;
1609         union spoolss_PrinterInfo info_dst;
1610         uint32_t num_forms;
1611         union spoolss_FormInfo *forms;
1612         struct cli_state *cli_dst = NULL;
1613
1614         DEBUG(3,("copying forms\n"));
1615
1616         /* connect destination PI_SPOOLSS */
1617         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1618                                      &syntax_spoolss);
1619         if (!NT_STATUS_IS_OK(nt_status))
1620                 return nt_status;
1621
1622         /* enum src printers */
1623         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1624                 nt_status = NT_STATUS_UNSUCCESSFUL;
1625                 goto done;
1626         }
1627
1628         if (!num_printers) {
1629                 printf ("no printers found on server.\n");
1630                 nt_status = NT_STATUS_OK;
1631                 goto done;
1632         }
1633
1634         /* do something for all printers */
1635         for (i = 0; i < num_printers; i++) {
1636
1637                 /* do some initialization */
1638                 printername = info_enum[i].info2.printername;
1639                 sharename = info_enum[i].info2.sharename;
1640
1641                 if (!printername || !sharename) {
1642                         nt_status = NT_STATUS_UNSUCCESSFUL;
1643                         goto done;
1644                 }
1645                 /* we can reset NT_STATUS here because we do not
1646                    get any real NT_STATUS-codes anymore from now on */
1647                 nt_status = NT_STATUS_UNSUCCESSFUL;
1648
1649                 d_printf("migrating printer forms for:    [%s] / [%s]\n",
1650                         printername, sharename);
1651
1652
1653                 /* open src printer handle */
1654                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1655                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
1656                         goto done;
1657
1658                 /* open dst printer handle */
1659                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1660                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1661                         goto done;
1662
1663                 /* check for existing dst printer */
1664                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
1665                         goto done;
1666
1667                 /* finally migrate forms */
1668                 if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
1669                         goto done;
1670
1671                 DEBUG(1,("got %d forms for printer\n", num_forms));
1672
1673
1674                 for (f = 0; f < num_forms; f++) {
1675
1676                         union spoolss_AddFormInfo info;
1677                         NTSTATUS status;
1678
1679                         /* only migrate FORM_PRINTER types, according to jerry
1680                            FORM_BUILTIN-types are hard-coded in samba */
1681                         if (forms[f].info1.flags != SPOOLSS_FORM_PRINTER)
1682                                 continue;
1683
1684                         if (c->opt_verbose)
1685                                 d_printf("\tmigrating form # %d [%s] of type [%d]\n",
1686                                         f, forms[f].info1.form_name,
1687                                         forms[f].info1.flags);
1688
1689                         info.info1 = (struct spoolss_AddFormInfo1 *)&forms[f].info1;
1690
1691                         /* FIXME: there might be something wrong with samba's
1692                            builtin-forms */
1693                         status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
1694                                                         &hnd_dst,
1695                                                         1,
1696                                                         info,
1697                                                         &result);
1698                         if (!W_ERROR_IS_OK(result)) {
1699                                 d_printf("\tAddForm form %d: [%s] refused.\n",
1700                                         f, forms[f].info1.form_name);
1701                                 continue;
1702                         }
1703
1704                         DEBUGADD(1,("\tAddForm of [%s] succeeded\n",
1705                                 forms[f].info1.form_name));
1706                 }
1707
1708
1709                 /* close printer handles here */
1710                 if (is_valid_policy_hnd(&hnd_src)) {
1711                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1712                 }
1713
1714                 if (is_valid_policy_hnd(&hnd_dst)) {
1715                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1716                 }
1717         }
1718
1719         nt_status = NT_STATUS_OK;
1720
1721 done:
1722
1723         if (is_valid_policy_hnd(&hnd_src))
1724                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1725
1726         if (is_valid_policy_hnd(&hnd_dst))
1727                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1728
1729         if (cli_dst) {
1730                 cli_shutdown(cli_dst);
1731         }
1732         return nt_status;
1733 }
1734
1735 /**
1736  * Migrate printer-drivers from a src server to the dst server
1737  *
1738  * All parameters are provided by the run_rpc_command function, except for
1739  * argc, argv which are passed through.
1740  *
1741  * @param c     A net_context structure
1742  * @param domain_sid The domain sid aquired from the remote server
1743  * @param cli A cli_state connected to the server.
1744  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1745  * @param argc  Standard main() style argc
1746  * @param argv  Standard main() style argv.  Initial components are already
1747  *              stripped
1748  *
1749  * @return Normal NTSTATUS return.
1750  **/
1751
1752 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
1753                                                 const DOM_SID *domain_sid,
1754                                                 const char *domain_name,
1755                                                 struct cli_state *cli,
1756                                                 struct rpc_pipe_client *pipe_hnd,
1757                                                 TALLOC_CTX *mem_ctx,
1758                                                 int argc,
1759                                                 const char **argv)
1760 {
1761         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1762         uint32 i, p;
1763         uint32 num_printers;
1764         uint32 level = 3;
1765         const char *printername, *sharename;
1766         bool got_src_driver_share = false;
1767         bool got_dst_driver_share = false;
1768         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1769         POLICY_HND hnd_src, hnd_dst;
1770         union spoolss_DriverInfo drv_info_src;
1771         union spoolss_PrinterInfo *info_enum;
1772         union spoolss_PrinterInfo info_dst;
1773         struct cli_state *cli_dst = NULL;
1774         struct cli_state *cli_share_src = NULL;
1775         struct cli_state *cli_share_dst = NULL;
1776         const char *drivername = NULL;
1777
1778         DEBUG(3,("copying printer-drivers\n"));
1779
1780         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1781                                      &syntax_spoolss);
1782         if (!NT_STATUS_IS_OK(nt_status))
1783                 return nt_status;
1784
1785         /* open print$-share on the src server */
1786         nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
1787                         cli->desthost, "print$", "A:");
1788         if (!NT_STATUS_IS_OK(nt_status))
1789                 goto done;
1790
1791         got_src_driver_share = true;
1792
1793
1794         /* open print$-share on the dst server */
1795         nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
1796                         cli_dst->desthost, "print$", "A:");
1797         if (!NT_STATUS_IS_OK(nt_status))
1798                 return nt_status;
1799
1800         got_dst_driver_share = true;
1801
1802
1803         /* enum src printers */
1804         if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
1805                 nt_status = NT_STATUS_UNSUCCESSFUL;
1806                 goto done;
1807         }
1808
1809         if (num_printers == 0) {
1810                 printf ("no printers found on server.\n");
1811                 nt_status = NT_STATUS_OK;
1812                 goto done;
1813         }
1814
1815
1816         /* do something for all printers */
1817         for (p = 0; p < num_printers; p++) {
1818
1819                 /* do some initialization */
1820                 printername = info_enum[p].info2.printername;
1821                 sharename = info_enum[p].info2.sharename;
1822
1823                 if (!printername || !sharename) {
1824                         nt_status = NT_STATUS_UNSUCCESSFUL;
1825                         goto done;
1826                 }
1827
1828                 /* we can reset NT_STATUS here because we do not
1829                    get any real NT_STATUS-codes anymore from now on */
1830                 nt_status = NT_STATUS_UNSUCCESSFUL;
1831
1832                 d_printf("migrating printer driver for:   [%s] / [%s]\n",
1833                         printername, sharename);
1834
1835                 /* open dst printer handle */
1836                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
1837                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
1838                         goto done;
1839
1840                 /* check for existing dst printer */
1841                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
1842                         goto done;
1843
1844
1845                 /* open src printer handle */
1846                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
1847                                                  MAXIMUM_ALLOWED_ACCESS,
1848                                                  pipe_hnd->auth->user_name,
1849                                                  &hnd_src))
1850                         goto done;
1851
1852                 /* in a first step call getdriver for each shared printer (per arch)
1853                    to get a list of all files that have to be copied */
1854
1855                 for (i=0; archi_table[i].long_archi!=NULL; i++) {
1856
1857                         /* getdriver src */
1858                         if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
1859                                         level, archi_table[i].long_archi,
1860                                         archi_table[i].version, &drv_info_src))
1861                                 continue;
1862
1863                         drivername = drv_info_src.info3.driver_name;
1864
1865                         if (c->opt_verbose)
1866                                 display_print_driver3(&drv_info_src.info3);
1867
1868                         /* check arch dir */
1869                         nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
1870                         if (!NT_STATUS_IS_OK(nt_status))
1871                                 goto done;
1872
1873
1874                         /* copy driver-files */
1875                         nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
1876                                                         archi_table[i].short_archi,
1877                                                         &drv_info_src.info3);
1878                         if (!NT_STATUS_IS_OK(nt_status))
1879                                 goto done;
1880
1881
1882                         /* adddriver dst */
1883                         if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
1884                                 nt_status = NT_STATUS_UNSUCCESSFUL;
1885                                 goto done;
1886                         }
1887
1888                         DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
1889                                 drivername, printername));
1890
1891                 }
1892
1893                 if (strlen(drivername) == 0) {
1894                         DEBUGADD(1,("Did not get driver for printer %s\n",
1895                                     printername));
1896                         goto done;
1897                 }
1898
1899                 /* setdriver dst */
1900                 info_dst.info2.drivername = drivername;
1901
1902                 if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
1903                         nt_status = NT_STATUS_UNSUCCESSFUL;
1904                         goto done;
1905                 }
1906
1907                 DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
1908                         drivername, printername));
1909
1910                 /* close dst */
1911                 if (is_valid_policy_hnd(&hnd_dst)) {
1912                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1913                 }
1914
1915                 /* close src */
1916                 if (is_valid_policy_hnd(&hnd_src)) {
1917                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1918                 }
1919         }
1920
1921         nt_status = NT_STATUS_OK;
1922
1923 done:
1924
1925         if (is_valid_policy_hnd(&hnd_src))
1926                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
1927
1928         if (is_valid_policy_hnd(&hnd_dst))
1929                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
1930
1931         if (cli_dst) {
1932                 cli_shutdown(cli_dst);
1933         }
1934
1935         if (got_src_driver_share)
1936                 cli_shutdown(cli_share_src);
1937
1938         if (got_dst_driver_share)
1939                 cli_shutdown(cli_share_dst);
1940
1941         return nt_status;
1942
1943 }
1944
1945 /**
1946  * Migrate printer-queues from a src to the dst server
1947  * (requires a working "addprinter command" to be installed for the local smbd)
1948  *
1949  * All parameters are provided by the run_rpc_command function, except for
1950  * argc, argv which are passed through.
1951  *
1952  * @param c     A net_context structure
1953  * @param domain_sid The domain sid aquired from the remote server
1954  * @param cli A cli_state connected to the server.
1955  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1956  * @param argc  Standard main() style argc
1957  * @param argv  Standard main() style argv.  Initial components are already
1958  *              stripped
1959  *
1960  * @return Normal NTSTATUS return.
1961  **/
1962
1963 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
1964                                                 const DOM_SID *domain_sid,
1965                                                 const char *domain_name,
1966                                                 struct cli_state *cli,
1967                                                 struct rpc_pipe_client *pipe_hnd,
1968                                                 TALLOC_CTX *mem_ctx,
1969                                                 int argc,
1970                                                 const char **argv)
1971 {
1972         WERROR result;
1973         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
1974         uint32 i = 0, num_printers;
1975         uint32 level = 2;
1976         union spoolss_PrinterInfo info_dst, info_src;
1977         union spoolss_PrinterInfo *info_enum;
1978         struct cli_state *cli_dst = NULL;
1979         POLICY_HND hnd_dst, hnd_src;
1980         const char *printername, *sharename;
1981         struct rpc_pipe_client *pipe_hnd_dst = NULL;
1982         struct spoolss_SetPrinterInfoCtr info_ctr;
1983
1984         DEBUG(3,("copying printers\n"));
1985
1986         /* connect destination PI_SPOOLSS */
1987         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
1988                                      &syntax_spoolss);
1989         if (!NT_STATUS_IS_OK(nt_status))
1990                 return nt_status;
1991
1992         /* enum printers */
1993         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
1994                 nt_status = NT_STATUS_UNSUCCESSFUL;
1995                 goto done;
1996         }
1997
1998         if (!num_printers) {
1999                 printf ("no printers found on server.\n");
2000                 nt_status = NT_STATUS_OK;
2001                 goto done;
2002         }
2003
2004         /* do something for all printers */
2005         for (i = 0; i < num_printers; i++) {
2006
2007                 /* do some initialization */
2008                 printername = info_enum[i].info2.printername;
2009                 sharename = info_enum[i].info2.sharename;
2010
2011                 if (!printername || !sharename) {
2012                         nt_status = NT_STATUS_UNSUCCESSFUL;
2013                         goto done;
2014                 }
2015                 /* we can reset NT_STATUS here because we do not
2016                    get any real NT_STATUS-codes anymore from now on */
2017                 nt_status = NT_STATUS_UNSUCCESSFUL;
2018
2019                 d_printf("migrating printer queue for:    [%s] / [%s]\n",
2020                         printername, sharename);
2021
2022                 /* open dst printer handle */
2023                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2024                         PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
2025
2026                         DEBUG(1,("could not open printer: %s\n", sharename));
2027                 }
2028
2029                 /* check for existing dst printer */
2030                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
2031                         printf ("could not get printer, creating printer.\n");
2032                 } else {
2033                         DEBUG(1,("printer already exists: %s\n", sharename));
2034                         /* close printer handle here - dst only, not got src yet. */
2035                         if (is_valid_policy_hnd(&hnd_dst)) {
2036                                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2037                         }
2038                         continue;
2039                 }
2040
2041                 /* now get again src printer ctr via getprinter,
2042                    we first need a handle for that */
2043
2044                 /* open src printer handle */
2045                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2046                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2047                         goto done;
2048
2049                 /* getprinter on the src server */
2050                 if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
2051                         goto done;
2052
2053                 /* copy each src printer to a dst printer 1:1,
2054                    maybe some values have to be changed though */
2055                 d_printf("creating printer: %s\n", printername);
2056
2057                 info_ctr.level = level;
2058                 info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info_src.info2;
2059
2060                 result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
2061                                                      mem_ctx,
2062                                                      &info_ctr);
2063
2064                 if (W_ERROR_IS_OK(result))
2065                         d_printf ("printer [%s] successfully added.\n", printername);
2066                 else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
2067                         d_fprintf (stderr, "printer [%s] already exists.\n", printername);
2068                 else {
2069                         d_fprintf (stderr, "could not create printer [%s]\n", printername);
2070                         goto done;
2071                 }
2072
2073                 /* close printer handles here */
2074                 if (is_valid_policy_hnd(&hnd_src)) {
2075                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2076                 }
2077
2078                 if (is_valid_policy_hnd(&hnd_dst)) {
2079                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2080                 }
2081         }
2082
2083         nt_status = NT_STATUS_OK;
2084
2085 done:
2086         if (is_valid_policy_hnd(&hnd_src))
2087                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2088
2089         if (is_valid_policy_hnd(&hnd_dst))
2090                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2091
2092         if (cli_dst) {
2093                 cli_shutdown(cli_dst);
2094         }
2095         return nt_status;
2096 }
2097
2098 /**
2099  * Migrate Printer-Settings from a src server to the dst server
2100  * (for this to work, printers and drivers already have to be migrated earlier)
2101  *
2102  * All parameters are provided by the run_rpc_command function, except for
2103  * argc, argv which are passed through.
2104  *
2105  * @param c     A net_context structure
2106  * @param domain_sid The domain sid aquired from the remote server
2107  * @param cli A cli_state connected to the server.
2108  * @param mem_ctx Talloc context, destoyed on compleation of the function.
2109  * @param argc  Standard main() style argc
2110  * @param argv  Standard main() style argv.  Initial components are already
2111  *              stripped
2112  *
2113  * @return Normal NTSTATUS return.
2114  **/
2115
2116 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
2117                                                 const DOM_SID *domain_sid,
2118                                                 const char *domain_name,
2119                                                 struct cli_state *cli,
2120                                                 struct rpc_pipe_client *pipe_hnd,
2121                                                 TALLOC_CTX *mem_ctx,
2122                                                 int argc,
2123                                                 const char **argv)
2124 {
2125
2126         /* FIXME: Here the nightmare begins */
2127
2128         WERROR result;
2129         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2130         uint32 i = 0, p = 0, j = 0;
2131         uint32 num_printers, val_needed, data_needed;
2132         uint32 level = 2;
2133         const char *printername, *sharename;
2134         struct rpc_pipe_client *pipe_hnd_dst = NULL;
2135         POLICY_HND hnd_src, hnd_dst;
2136         union spoolss_PrinterInfo *info_enum;
2137         union spoolss_PrinterInfo info_dst_publish;
2138         union spoolss_PrinterInfo info_dst;
2139         REGVAL_CTR *reg_ctr;
2140         struct cli_state *cli_dst = NULL;
2141         char *devicename = NULL, *unc_name = NULL, *url = NULL;
2142         const char *longname;
2143
2144         uint16 *keylist = NULL, *curkey;
2145
2146         /* FIXME GD */
2147         ZERO_STRUCT(info_dst_publish);
2148
2149         DEBUG(3,("copying printer settings\n"));
2150
2151         /* connect destination PI_SPOOLSS */
2152         nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
2153                                      &syntax_spoolss);
2154         if (!NT_STATUS_IS_OK(nt_status))
2155                 return nt_status;
2156
2157         /* enum src printers */
2158         if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
2159                 nt_status = NT_STATUS_UNSUCCESSFUL;
2160                 goto done;
2161         }
2162
2163         if (!num_printers) {
2164                 printf ("no printers found on server.\n");
2165                 nt_status = NT_STATUS_OK;
2166                 goto done;
2167         }
2168
2169
2170         /* needed for dns-strings in regkeys */
2171         longname = get_mydnsfullname();
2172         if (!longname) {
2173                 nt_status = NT_STATUS_UNSUCCESSFUL;
2174                 goto done;
2175         }
2176
2177         /* do something for all printers */
2178         for (i = 0; i < num_printers; i++) {
2179
2180                 /* do some initialization */
2181                 printername = info_enum[i].info2.printername;
2182                 sharename = info_enum[i].info2.sharename;
2183
2184                 if (!printername || !sharename) {
2185                         nt_status = NT_STATUS_UNSUCCESSFUL;
2186                         goto done;
2187                 }
2188                 /* we can reset NT_STATUS here because we do not
2189                    get any real NT_STATUS-codes anymore from now on */
2190                 nt_status = NT_STATUS_UNSUCCESSFUL;
2191
2192                 d_printf("migrating printer settings for: [%s] / [%s]\n",
2193                         printername, sharename);
2194
2195
2196                 /* open src printer handle */
2197                 if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
2198                         MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
2199                         goto done;
2200
2201                 /* open dst printer handle */
2202                 if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
2203                         PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
2204                         goto done;
2205
2206                 /* check for existing dst printer */
2207                 if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2208                                 level, &info_dst))
2209                         goto done;
2210
2211
2212                 /* STEP 1: COPY DEVICE-MODE and other
2213                            PRINTER_INFO_2-attributes
2214                 */
2215
2216                 info_dst.info2 = info_enum[i].info2;
2217
2218                 /* why is the port always disconnected when the printer
2219                    is correctly installed (incl. driver ???) */
2220                 info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
2221
2222                 /* check if printer is published */
2223                 if (info_enum[i].info2.attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
2224
2225                         /* check for existing dst printer */
2226                         if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
2227                                 goto done;
2228
2229                         info_dst_publish.info7.action = DSPRINT_PUBLISH;
2230
2231                         /* ignore false from setprinter due to WERR_IO_PENDING */
2232                         net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
2233
2234                         DEBUG(3,("republished printer\n"));
2235                 }
2236
2237                 if (info_enum[i].info2.devmode != NULL) {
2238
2239                         /* copy devmode (info level 2) */
2240                         info_dst.info2.devmode = info_enum[i].info2.devmode;
2241
2242                         /* do not copy security descriptor (we have another
2243                          * command for that) */
2244                         info_dst.info2.secdesc = NULL;
2245
2246 #if 0
2247                         if (asprintf(&devicename, "\\\\%s\\%s", longname,
2248                                      printername) < 0) {
2249                                 nt_status = NT_STATUS_NO_MEMORY;
2250                                 goto done;
2251                         }
2252
2253                         init_unistr(&ctr_dst.printers_2->devmode->devicename,
2254                                     devicename);
2255 #endif
2256                         if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
2257                                                     level, &info_dst))
2258                                 goto done;
2259
2260                         DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
2261                 }
2262
2263                 /* STEP 2: COPY REGISTRY VALUES */
2264
2265                 /* please keep in mind that samba parse_spools gives horribly
2266                    crippled results when used to rpccli_spoolss_enumprinterdataex
2267                    a win2k3-server.  (Bugzilla #1851)
2268                    FIXME: IIRC I've seen it too on a win2k-server
2269                 */
2270
2271                 /* enumerate data on src handle */
2272                 result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
2273                         &val_needed, &data_needed, NULL);
2274
2275                 /* loop for all printerdata of "PrinterDriverData" */
2276                 while (W_ERROR_IS_OK(result)) {
2277
2278                         REGISTRY_VALUE value;
2279
2280                         result = rpccli_spoolss_enumprinterdata(
2281                                 pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
2282                                 data_needed, 0, 0, &value);
2283
2284                         /* loop for all reg_keys */
2285                         if (W_ERROR_IS_OK(result)) {
2286
2287                                 /* display_value */
2288                                 if (c->opt_verbose)
2289                                         display_reg_value(SPOOL_PRINTERDATA_KEY, value);
2290
2291                                 /* set_value */
2292                                 if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
2293                                                                 &hnd_dst, &value))
2294                                         goto done;
2295
2296                                 DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
2297                                         value.valuename));
2298                         }
2299                 }
2300
2301                 /* STEP 3: COPY SUBKEY VALUES */
2302
2303                 /* here we need to enum all printer_keys and then work
2304                    on the result with enum_printer_key_ex. nt4 does not
2305                    respond to enumprinterkey, win2k does, so continue
2306                    in case of an error */
2307
2308                 if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
2309                         printf("got no key-data\n");
2310                         continue;
2311                 }
2312
2313
2314                 /* work on a list of printer keys
2315                    each key has to be enumerated to get all required
2316                    information.  information is then set via setprinterdataex-calls */
2317
2318                 if (keylist == NULL)
2319                         continue;
2320
2321                 curkey = keylist;
2322                 while (*curkey != 0) {
2323                         char *subkey;
2324                         rpcstr_pull_talloc(mem_ctx,
2325                                         &subkey,
2326                                         curkey,
2327                                         -1,
2328                                         STR_TERMINATE);
2329                         if (!subkey) {
2330                                 return NT_STATUS_NO_MEMORY;
2331                         }
2332
2333                         curkey += strlen(subkey) + 1;
2334
2335                         if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
2336                                 return NT_STATUS_NO_MEMORY;
2337
2338                         /* enumerate all src subkeys */
2339                         if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
2340                                                            &hnd_src, subkey,
2341                                                            reg_ctr))
2342                                 goto done;
2343
2344                         for (j=0; j < reg_ctr->num_values; j++) {
2345
2346                                 REGISTRY_VALUE value;
2347                                 UNISTR2 data;
2348
2349                                 /* although samba replies with sane data in most cases we
2350                                    should try to avoid writing wrong registry data */
2351
2352                                 if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
2353                                     strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
2354                                     strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
2355                                     strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
2356                                     strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2357
2358                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
2359
2360                                                 /* although windows uses a multi-sz, we use a sz */
2361                                                 init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
2362                                                 fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
2363                                         }
2364
2365                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
2366
2367                                                 if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
2368                                                         nt_status = NT_STATUS_NO_MEMORY;
2369                                                         goto done;
2370                                                 }
2371                                                 init_unistr2(&data, unc_name, UNI_STR_TERMINATE);
2372                                                 fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
2373                                         }
2374
2375                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
2376
2377                                                 continue;
2378
2379 #if 0
2380                                                 /* FIXME: should we really do that ??? */
2381                                                 if (asprintf(&url, "http://%s:631/printers/%s", longname, sharename) < 0) {
2382                                                         nt_status = NT_STATUS_NO_MEMORY;
2383                                                         goto done;
2384                                                 }
2385                                                 init_unistr2(&data, url, UNI_STR_TERMINATE);
2386                                                 fstrcpy(value.valuename, SPOOL_REG_URL);
2387 #endif
2388                                         }
2389
2390                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
2391
2392                                                 init_unistr2(&data, longname, UNI_STR_TERMINATE);
2393                                                 fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
2394                                         }
2395
2396                                         if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
2397
2398                                                 init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
2399                                                 fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
2400                                         }
2401
2402                                         value.type = REG_SZ;
2403                                         value.size = data.uni_str_len * 2;
2404                                         if (value.size) {
2405                                                 value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
2406                                         } else {
2407                                                 value.data_p = NULL;
2408                                         }
2409
2410                                         if (c->opt_verbose)
2411                                                 display_reg_value(subkey, value);
2412
2413                                         /* here we have to set all subkeys on the dst server */
2414                                         if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2415                                                         subkey, &value))
2416                                                 goto done;
2417
2418                                 } else {
2419
2420                                         if (c->opt_verbose)
2421                                                 display_reg_value(subkey, *(reg_ctr->values[j]));
2422
2423                                         /* here we have to set all subkeys on the dst server */
2424                                         if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
2425                                                         subkey, reg_ctr->values[j]))
2426                                                 goto done;
2427
2428                                 }
2429
2430                                 DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
2431                                                 subkey, reg_ctr->values[j]->valuename));
2432
2433                         }
2434
2435                         TALLOC_FREE( reg_ctr );
2436                 }
2437
2438                 SAFE_FREE(keylist);
2439
2440                 /* close printer handles here */
2441                 if (is_valid_policy_hnd(&hnd_src)) {
2442                         rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2443                 }
2444
2445                 if (is_valid_policy_hnd(&hnd_dst)) {
2446                         rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2447                 }
2448
2449         }
2450
2451         nt_status = NT_STATUS_OK;
2452
2453 done:
2454         SAFE_FREE(devicename);
2455         SAFE_FREE(url);
2456         SAFE_FREE(unc_name);
2457
2458         if (is_valid_policy_hnd(&hnd_src))
2459                 rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
2460
2461         if (is_valid_policy_hnd(&hnd_dst))
2462                 rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
2463
2464         if (cli_dst) {
2465                 cli_shutdown(cli_dst);
2466         }
2467         return nt_status;
2468 }