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