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