spoolss rpc client cleanup:
[samba.git] / source / rpcclient / cmd_spoolss.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4    RPC pipe client
5
6    Copyright (C) Gerald Carter                     2001
7    Copyright (C) Tim Potter                        2000
8    Copyright (C) Andrew Tridgell              1992-1999
9    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
10  
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27 #include "rpcclient.h"
28
29 struct table_node {
30         char    *long_archi;
31         char    *short_archi;
32         int     version;
33 };
34  
35 struct table_node archi_table[]= {
36
37         {"Windows 4.0",          "WIN40",       0 },
38         {"Windows NT x86",       "W32X86",      2 },
39         {"Windows NT R4000",     "W32MIPS",     2 },
40         {"Windows NT Alpha_AXP", "W32ALPHA",    2 },
41         {"Windows NT PowerPC",   "W32PPC",      2 },
42         {NULL,                   "",            -1 }
43 };
44
45 /****************************************************************************
46 function to do the mapping between the long architecture name and
47 the short one.
48 ****************************************************************************/
49 BOOL get_short_archi(char *short_archi, char *long_archi)
50 {
51         int i=-1;
52
53         DEBUG(107,("Getting architecture dependant directory\n"));
54         do {
55                 i++;
56         } while ( (archi_table[i].long_archi!=NULL ) &&
57                   StrCaseCmp(long_archi, archi_table[i].long_archi) );
58
59         if (archi_table[i].long_archi==NULL) {
60                 DEBUGADD(10,("Unknown architecture [%s] !\n", long_archi));
61                 return False;
62         }
63
64         StrnCpy (short_archi, archi_table[i].short_archi, strlen(archi_table[i].short_archi));
65
66         DEBUGADD(108,("index: [%d]\n", i));
67         DEBUGADD(108,("long architecture: [%s]\n", long_archi));
68         DEBUGADD(108,("short architecture: [%s]\n", short_archi));
69
70         return True;
71 }
72
73
74 /**********************************************************************
75  * dummy function  -- placeholder
76   */
77 static NTSTATUS cmd_spoolss_not_implemented(struct cli_state *cli, 
78                                             TALLOC_CTX *mem_ctx,
79                                             int argc, char **argv)
80 {
81         printf ("(*) This command is not currently implemented.\n");
82         return NT_STATUS_OK;
83 }
84
85 /***********************************************************************
86  * Get printer information
87  */
88 static NTSTATUS cmd_spoolss_open_printer_ex(struct cli_state *cli, 
89                                             TALLOC_CTX *mem_ctx,
90                                             int argc, char **argv)
91 {
92         WERROR          werror;
93         pstring         printername;
94         fstring         servername, user;
95         POLICY_HND      hnd;
96         
97         if (argc != 2) {
98                 printf("Usage: %s <printername>\n", argv[0]);
99                 return NT_STATUS_OK;
100         }
101         
102         if (!cli)
103                 return NT_STATUS_UNSUCCESSFUL;
104
105         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
106         strupper (servername);
107         fstrcpy  (user, cli->user_name);
108         fstrcpy  (printername, argv[1]);
109
110         /* Open the printer handle */
111
112         werror = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, 
113                                              "", MAXIMUM_ALLOWED_ACCESS, 
114                                              servername, user, &hnd);
115
116         if (W_ERROR_IS_OK(werror)) {
117                 printf("Printer %s opened successfully\n", printername);
118                 werror = cli_spoolss_close_printer(cli, mem_ctx, &hnd);
119
120                 if (!W_ERROR_IS_OK(werror)) {
121                         printf("Error closing printer handle! (%s)\n", 
122                                 get_dos_error_msg(werror));
123                 }
124         }
125
126         return werror_to_ntstatus(werror);
127 }
128
129
130 /****************************************************************************
131 printer info level 0 display function
132 ****************************************************************************/
133 static void display_print_info_0(PRINTER_INFO_0 *i0)
134 {
135         fstring         name = "";
136         fstring         servername = "";
137
138         if (i0->printername.buffer)
139                 rpcstr_pull(name, i0->printername.buffer, sizeof(name), 0, STR_TERMINATE);
140
141         if (i0->servername.buffer)
142                 rpcstr_pull(servername, i0->servername.buffer, sizeof(servername), 0,STR_TERMINATE);
143   
144         printf("\tprintername:[%s]\n", name);
145         printf("\tservername:[%s]\n", servername);
146         printf("\tcjobs:[0x%x]\n", i0->cjobs);
147         printf("\ttotal_jobs:[0x%x]\n", i0->total_jobs);
148         
149         printf("\t:date: [%d]-[%d]-[%d] (%d)\n", i0->year, i0->month, 
150                i0->day, i0->dayofweek);
151         printf("\t:time: [%d]-[%d]-[%d]-[%d]\n", i0->hour, i0->minute, 
152                i0->second, i0->milliseconds);
153         
154         printf("\tglobal_counter:[0x%x]\n", i0->global_counter);
155         printf("\ttotal_pages:[0x%x]\n", i0->total_pages);
156         
157         printf("\tmajorversion:[0x%x]\n", i0->major_version);
158         printf("\tbuildversion:[0x%x]\n", i0->build_version);
159         
160         printf("\tunknown7:[0x%x]\n", i0->unknown7);
161         printf("\tunknown8:[0x%x]\n", i0->unknown8);
162         printf("\tunknown9:[0x%x]\n", i0->unknown9);
163         printf("\tsession_counter:[0x%x]\n", i0->session_counter);
164         printf("\tunknown11:[0x%x]\n", i0->unknown11);
165         printf("\tprinter_errors:[0x%x]\n", i0->printer_errors);
166         printf("\tunknown13:[0x%x]\n", i0->unknown13);
167         printf("\tunknown14:[0x%x]\n", i0->unknown14);
168         printf("\tunknown15:[0x%x]\n", i0->unknown15);
169         printf("\tunknown16:[0x%x]\n", i0->unknown16);
170         printf("\tchange_id:[0x%x]\n", i0->change_id);
171         printf("\tunknown18:[0x%x]\n", i0->unknown18);
172         printf("\tstatus:[0x%x]\n", i0->status);
173         printf("\tunknown20:[0x%x]\n", i0->unknown20);
174         printf("\tc_setprinter:[0x%x]\n", i0->c_setprinter);
175         printf("\tunknown22:[0x%x]\n", i0->unknown22);
176         printf("\tunknown23:[0x%x]\n", i0->unknown23);
177         printf("\tunknown24:[0x%x]\n", i0->unknown24);
178         printf("\tunknown25:[0x%x]\n", i0->unknown25);
179         printf("\tunknown26:[0x%x]\n", i0->unknown26);
180         printf("\tunknown27:[0x%x]\n", i0->unknown27);
181         printf("\tunknown28:[0x%x]\n", i0->unknown28);
182         printf("\tunknown29:[0x%x]\n", i0->unknown29);
183 }
184
185 /****************************************************************************
186 printer info level 1 display function
187 ****************************************************************************/
188 static void display_print_info_1(PRINTER_INFO_1 *i1)
189 {
190         fstring desc = "";
191         fstring name = "";
192         fstring comm = "";
193
194         if (i1->description.buffer)
195                 rpcstr_pull(desc, i1->description.buffer, sizeof(desc), 0, 
196                             STR_TERMINATE);
197
198         if (i1->name.buffer)
199                 rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, 
200                             STR_TERMINATE);
201
202         if (i1->comment.buffer)
203                 rpcstr_pull(comm, i1->comment.buffer, sizeof(comm), 0, 
204                             STR_TERMINATE);
205
206         printf("\tflags:[0x%x]\n", i1->flags);
207         printf("\tname:[%s]\n", name);
208         printf("\tdescription:[%s]\n", desc);
209         printf("\tcomment:[%s]\n\n", comm);
210 }
211
212 /****************************************************************************
213 printer info level 2 display function
214 ****************************************************************************/
215 static void display_print_info_2(PRINTER_INFO_2 *i2)
216 {
217         fstring servername = "";
218         fstring printername = "";
219         fstring sharename = "";
220         fstring portname = "";
221         fstring drivername = "";
222         fstring comment = "";
223         fstring location = "";
224         fstring sepfile = "";
225         fstring printprocessor = "";
226         fstring datatype = "";
227         fstring parameters = "";
228         
229         if (i2->servername.buffer)
230                 rpcstr_pull(servername, i2->servername.buffer,sizeof(servername), 0, STR_TERMINATE);
231
232         if (i2->printername.buffer)
233                 rpcstr_pull(printername, i2->printername.buffer,sizeof(printername), 0, STR_TERMINATE);
234
235         if (i2->sharename.buffer)
236                 rpcstr_pull(sharename, i2->sharename.buffer,sizeof(sharename), 0, STR_TERMINATE);
237
238         if (i2->portname.buffer)
239                 rpcstr_pull(portname, i2->portname.buffer,sizeof(portname), 0, STR_TERMINATE);
240
241         if (i2->drivername.buffer)
242                 rpcstr_pull(drivername, i2->drivername.buffer,sizeof(drivername), 0, STR_TERMINATE);
243
244         if (i2->comment.buffer)
245                 rpcstr_pull(comment, i2->comment.buffer,sizeof(comment), 0, STR_TERMINATE);
246
247         if (i2->location.buffer)
248                 rpcstr_pull(location, i2->location.buffer,sizeof(location), 0, STR_TERMINATE);
249
250         if (i2->sepfile.buffer)
251                 rpcstr_pull(sepfile, i2->sepfile.buffer,sizeof(sepfile), 0, STR_TERMINATE);
252
253         if (i2->printprocessor.buffer) 
254                 rpcstr_pull(printprocessor, i2->printprocessor.buffer,sizeof(printprocessor), 0, STR_TERMINATE);
255
256         if (i2->datatype.buffer)
257                 rpcstr_pull(datatype, i2->datatype.buffer,sizeof(datatype), 0, STR_TERMINATE);
258
259         if (i2->parameters.buffer)
260                 rpcstr_pull(parameters, i2->parameters.buffer,sizeof(parameters), 0, STR_TERMINATE);
261
262         printf("\tservername:[%s]\n", servername);
263         printf("\tprintername:[%s]\n", printername);
264         printf("\tsharename:[%s]\n", sharename);
265         printf("\tportname:[%s]\n", portname);
266         printf("\tdrivername:[%s]\n", drivername);
267         printf("\tcomment:[%s]\n", comment);
268         printf("\tlocation:[%s]\n", location);
269         printf("\tsepfile:[%s]\n", sepfile);
270         printf("\tprintprocessor:[%s]\n", printprocessor);
271         printf("\tdatatype:[%s]\n", datatype);
272         printf("\tparameters:[%s]\n", parameters);
273         printf("\tattributes:[0x%x]\n", i2->attributes);
274         printf("\tpriority:[0x%x]\n", i2->priority);
275         printf("\tdefaultpriority:[0x%x]\n", i2->defaultpriority);
276         printf("\tstarttime:[0x%x]\n", i2->starttime);
277         printf("\tuntiltime:[0x%x]\n", i2->untiltime);
278         printf("\tstatus:[0x%x]\n", i2->status);
279         printf("\tcjobs:[0x%x]\n", i2->cjobs);
280         printf("\taverageppm:[0x%x]\n", i2->averageppm);
281
282         if (i2->secdesc) display_sec_desc(i2->secdesc);
283 }
284
285 /****************************************************************************
286 printer info level 3 display function
287 ****************************************************************************/
288 static void display_print_info_3(PRINTER_INFO_3 *i3)
289 {
290         printf("\tflags:[0x%x]\n", i3->flags);
291
292         display_sec_desc(i3->secdesc);
293 }
294
295 /* Enumerate printers */
296
297 static NTSTATUS cmd_spoolss_enum_printers(struct cli_state *cli, 
298                                           TALLOC_CTX *mem_ctx,
299                                           int argc, char **argv)
300 {
301         NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
302         uint32                  info_level = 1;
303         PRINTER_INFO_CTR        ctr;
304         int                     returned;
305         uint32                  i = 0;
306
307         if (argc > 2) 
308         {
309                 printf("Usage: %s [level]\n", argv[0]);
310                 return NT_STATUS_OK;
311         }
312
313         if (argc == 2) {
314                 info_level = atoi(argv[1]);
315         }
316
317         /* Enumerate printers  -- Should we enumerate types other 
318            than PRINTER_ENUM_LOCAL?  Maybe accept as a parameter?  --jerry */
319         ZERO_STRUCT(ctr);
320         result = cli_spoolss_enum_printers(cli, mem_ctx, PRINTER_ENUM_LOCAL, 
321                                            info_level, &returned, &ctr);
322
323         if (NT_STATUS_IS_OK(result)) 
324         {
325                 if (!returned)
326                         printf ("No Printers printers returned.\n");
327         
328                 switch(info_level) {
329                 case 0:
330                         for (i=0; i<returned; i++) {
331                                 display_print_info_0(&(ctr.printers_0[i]));
332                         }
333                         break;
334                 case 1:
335                         for (i=0; i<returned; i++) {
336                                 display_print_info_1(&(ctr.printers_1[i]));
337                         }
338                         break;
339                 case 2:
340                         for (i=0; i<returned; i++) {
341                                 display_print_info_2(&(ctr.printers_2[i]));
342                         }
343                         break;
344                 case 3:
345                         for (i=0; i<returned; i++) {
346                                 display_print_info_3(&(ctr.printers_3[i]));
347                         }
348                         break;
349                 default:
350                         printf("unknown info level %d\n", info_level);
351                         break;
352                 }
353         }
354
355         return result;
356 }
357
358 /****************************************************************************
359 port info level 1 display function
360 ****************************************************************************/
361 static void display_port_info_1(PORT_INFO_1 *i1)
362 {
363         fstring buffer;
364         
365         rpcstr_pull(buffer, i1->port_name.buffer, sizeof(buffer), 0, STR_TERMINATE);
366         printf("\tPort Name:\t[%s]\n", buffer);
367 }
368
369 /****************************************************************************
370 port info level 2 display function
371 ****************************************************************************/
372 static void display_port_info_2(PORT_INFO_2 *i2)
373 {
374         fstring buffer;
375         
376         rpcstr_pull(buffer, i2->port_name.buffer, sizeof(buffer), 0, STR_TERMINATE);
377         printf("\tPort Name:\t[%s]\n", buffer);
378         rpcstr_pull(buffer, i2->monitor_name.buffer, sizeof(buffer), 0, STR_TERMINATE);
379
380         printf("\tMonitor Name:\t[%s]\n", buffer);
381         rpcstr_pull(buffer, i2->description.buffer, sizeof(buffer), 0, STR_TERMINATE);
382
383         printf("\tDescription:\t[%s]\n", buffer);
384         printf("\tPort Type:\t[%d]\n", i2->port_type);
385         printf("\tReserved:\t[%d]\n", i2->reserved);
386         printf("\n");
387 }
388
389 /* Enumerate ports */
390
391 static NTSTATUS cmd_spoolss_enum_ports(struct cli_state *cli, 
392                                        TALLOC_CTX *mem_ctx,
393                                        int argc, char **argv)
394 {
395         NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
396         uint32                  info_level = 1;
397         PORT_INFO_CTR           ctr;
398         int                     returned;
399         
400         if (argc > 2) {
401                 printf("Usage: %s [level]\n", argv[0]);
402                 return NT_STATUS_OK;
403         }
404         
405         if (argc == 2) {
406                 info_level = atoi(argv[1]);
407         }
408
409         /* Enumerate ports */
410         ZERO_STRUCT(ctr);
411
412         result = cli_spoolss_enum_ports(cli, mem_ctx, info_level, &returned, &ctr);
413
414         if (NT_STATUS_IS_OK(result)) {
415                 int i;
416
417                 for (i = 0; i < returned; i++) {
418                         switch (info_level) {
419                         case 1:
420                                 display_port_info_1(&ctr.port.info_1[i]);
421                         break;
422                         case 2:
423                                 display_port_info_2(&ctr.port.info_2[i]);
424                                 break;
425                         default:
426                                 printf("unknown info level %d\n", info_level);
427                                 break;
428                         }
429                 }
430         }
431
432         return result;
433 }
434
435 /***********************************************************************
436  * Get printer information
437  */
438 static NTSTATUS cmd_spoolss_getprinter(struct cli_state *cli, 
439                                        TALLOC_CTX *mem_ctx,
440                                        int argc, char **argv)
441 {
442         POLICY_HND      pol;
443         WERROR          werror;
444         NTSTATUS        result;
445         uint32          info_level = 1;
446         BOOL            opened_hnd = False;
447         PRINTER_INFO_CTR ctr;
448         fstring         printername, 
449                         servername,
450                         user;
451
452         if (argc == 1 || argc > 3) {
453                 printf("Usage: %s <printername> [level]\n", argv[0]);
454                 return NT_STATUS_OK;
455         }
456
457         /* Open a printer handle */
458         if (argc == 3) {
459                 info_level = atoi(argv[2]);
460         }
461
462         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
463         strupper (servername);
464         slprintf (printername, sizeof(fstring)-1, "%s\\%s", servername, argv[1]);
465         fstrcpy  (user, cli->user_name);
466         
467         /* get a printer handle */
468
469         werror = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, 
470                                              "", MAXIMUM_ALLOWED_ACCESS, 
471                                              servername, user, &pol);
472
473         result = werror_to_ntstatus(werror);
474
475         if (!NT_STATUS_IS_OK(result))
476                 goto done;
477  
478         opened_hnd = True;
479
480         /* Get printer info */
481
482         result = cli_spoolss_getprinter(cli, mem_ctx, &pol, info_level, &ctr);
483
484         if (!NT_STATUS_IS_OK(result))
485                 goto done;
486
487         /* Display printer info */
488
489         switch (info_level) {
490         case 0: 
491                 display_print_info_0(ctr.printers_0);
492                 break;
493         case 1:
494                 display_print_info_1(ctr.printers_1);
495                 break;
496         case 2:
497                 display_print_info_2(ctr.printers_2);
498                 break;
499         case 3:
500                 display_print_info_3(ctr.printers_3);
501                 break;
502         default:
503                 printf("unknown info level %d\n", info_level);
504                 break;
505         }
506
507  done: 
508         if (opened_hnd) 
509                 cli_spoolss_close_printer(cli, mem_ctx, &pol);
510
511         return result;
512 }
513
514 /****************************************************************************
515 printer info level 0 display function
516 ****************************************************************************/
517 static void display_print_driver_1(DRIVER_INFO_1 *i1)
518 {
519         fstring name;
520         if (i1 == NULL)
521                 return;
522
523         rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE);
524
525         printf ("Printer Driver Info 1:\n");
526         printf ("\tDriver Name: [%s]\n\n", name);
527         
528         return;
529 }
530
531 /****************************************************************************
532 printer info level 1 display function
533 ****************************************************************************/
534 static void display_print_driver_2(DRIVER_INFO_2 *i1)
535 {
536         fstring name;
537         fstring architecture;
538         fstring driverpath;
539         fstring datafile;
540         fstring configfile;
541         if (i1 == NULL)
542                 return;
543
544         rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE);
545         rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), 0, STR_TERMINATE);
546         rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), 0, STR_TERMINATE);
547         rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), 0, STR_TERMINATE);
548         rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), 0, STR_TERMINATE);
549
550         printf ("Printer Driver Info 2:\n");
551         printf ("\tVersion: [%x]\n", i1->version);
552         printf ("\tDriver Name: [%s]\n", name);
553         printf ("\tArchitecture: [%s]\n", architecture);
554         printf ("\tDriver Path: [%s]\n", driverpath);
555         printf ("\tDatafile: [%s]\n", datafile);
556         printf ("\tConfigfile: [%s]\n\n", configfile);
557
558         return;
559 }
560
561 /****************************************************************************
562 printer info level 2 display function
563 ****************************************************************************/
564 static void display_print_driver_3(DRIVER_INFO_3 *i1)
565 {
566         fstring name;
567         fstring architecture;
568         fstring driverpath;
569         fstring datafile;
570         fstring configfile;
571         fstring helpfile;
572         fstring dependentfiles;
573         fstring monitorname;
574         fstring defaultdatatype;
575         
576         int length=0;
577         BOOL valid = True;
578         
579         if (i1 == NULL)
580                 return;
581
582         rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE);
583         rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), 0, STR_TERMINATE);
584         rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), 0, STR_TERMINATE);
585         rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), 0, STR_TERMINATE);
586         rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), 0, STR_TERMINATE);
587         rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), 0, STR_TERMINATE);
588         rpcstr_pull(monitorname, i1->monitorname.buffer, sizeof(monitorname), 0, STR_TERMINATE);
589         rpcstr_pull(defaultdatatype, i1->defaultdatatype.buffer, sizeof(defaultdatatype), 0, STR_TERMINATE);
590
591         printf ("Printer Driver Info 3:\n");
592         printf ("\tVersion: [%x]\n", i1->version);
593         printf ("\tDriver Name: [%s]\n",name);
594         printf ("\tArchitecture: [%s]\n", architecture);
595         printf ("\tDriver Path: [%s]\n", driverpath);
596         printf ("\tDatafile: [%s]\n", datafile);
597         printf ("\tConfigfile: [%s]\n", configfile);
598         printf ("\tHelpfile: [%s]\n\n", helpfile);
599
600         while (valid)
601         {
602                 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), 0, STR_TERMINATE);
603                 
604                 length+=strlen(dependentfiles)+1;
605                 
606                 if (strlen(dependentfiles) > 0)
607                 {
608                         printf ("\tDependentfiles: [%s]\n", dependentfiles);
609                 }
610                 else
611                 {
612                         valid = False;
613                 }
614         }
615         
616         printf ("\n");
617
618         printf ("\tMonitorname: [%s]\n", monitorname);
619         printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype);
620
621         return; 
622 }
623
624 /***********************************************************************
625  * Get printer information
626  */
627 static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, 
628                                       TALLOC_CTX *mem_ctx,
629                                       int argc, char **argv)
630 {
631         POLICY_HND      pol;
632         WERROR          werror;
633         NTSTATUS        result;
634         uint32          info_level = 3;
635         BOOL            opened_hnd = False;
636         PRINTER_DRIVER_CTR      ctr;
637         fstring         printername, 
638                         servername, 
639                         user;
640         uint32          i;
641
642         if ((argc == 1) || (argc > 3)) 
643         {
644                 printf("Usage: %s <printername> [level]\n", argv[0]);
645                 return NT_STATUS_OK;
646         }
647
648         /* get the arguments need to open the printer handle */
649         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
650         strupper (servername);
651         fstrcpy  (user, cli->user_name);
652         fstrcpy  (printername, argv[1]);
653         if (argc == 3)
654                 info_level = atoi(argv[2]);
655
656         /* Open a printer handle */
657
658         werror = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", 
659                                              MAXIMUM_ALLOWED_ACCESS, 
660                                              servername, user, &pol);
661
662         result = werror_to_ntstatus(werror);
663
664         if (!NT_STATUS_IS_OK(result)) {
665                 printf("Error opening printer handle for %s!\n", printername);
666                 return result;
667         }
668
669         opened_hnd = True;
670
671         /* loop through and print driver info level for each architecture */
672         for (i=0; archi_table[i].long_archi!=NULL; i++) 
673         {
674                 result = cli_spoolss_getprinterdriver(cli, mem_ctx, &pol, info_level, 
675                                                        archi_table[i].long_archi, &ctr);
676                 if (!NT_STATUS_IS_OK(result)) {
677                         continue;
678                 }
679
680                         
681                 printf ("\n[%s]\n", archi_table[i].long_archi);
682                 switch (info_level) 
683                 {
684                         
685                 case 1:
686                         display_print_driver_1 (ctr.info1);
687                         break;
688                 case 2:
689                         display_print_driver_2 (ctr.info2);
690                         break;
691                 case 3:
692                         display_print_driver_3 (ctr.info3);
693                         break;
694                 default:
695                         printf("unknown info level %d\n", info_level);
696                         break;
697                 }
698         }
699         
700         /* Cleanup */
701
702         if (opened_hnd)
703                 cli_spoolss_close_printer (cli, mem_ctx, &pol);
704         
705         return result;
706 }
707
708 /***********************************************************************
709  * Get printer information
710  */
711 static NTSTATUS cmd_spoolss_enum_drivers(struct cli_state *cli, 
712                                          TALLOC_CTX *mem_ctx,
713                                          int argc, char **argv)
714 {
715         NTSTATUS        result = NT_STATUS_OK;
716         uint32          info_level = 1;
717         PRINTER_DRIVER_CTR      ctr;
718         fstring         servername;
719         uint32          i, j,
720                         returned;
721
722         if (argc > 2) 
723         {
724                 printf("Usage: enumdrivers [level]\n");
725                 return NT_STATUS_OK;
726         }
727
728         /* get the arguments need to open the printer handle */
729         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
730         strupper (servername);
731         if (argc == 2)
732                 info_level = atoi(argv[1]);
733
734
735         /* loop through and print driver info level for each architecture */
736         for (i=0; archi_table[i].long_archi!=NULL; i++) 
737         {
738                 returned = 0;   
739                 result = cli_spoolss_enumprinterdrivers (cli, mem_ctx, info_level, 
740                                 archi_table[i].long_archi, &returned, &ctr);
741
742                 if (returned == 0)
743                         continue;
744                         
745
746                 if (!NT_STATUS_IS_OK(result))
747                 {
748                         printf ("Error getting driver for environment [%s] - %s\n",
749                                 archi_table[i].long_archi, get_nt_error_msg(result));
750                         continue;
751                 }
752                 
753                 printf ("\n[%s]\n", archi_table[i].long_archi);
754                 switch (info_level) 
755                 {
756                         
757                 case 1:
758                         for (j=0; j < returned; j++) {
759                                 display_print_driver_1 (&(ctr.info1[j]));
760                         }
761                         break;
762                 case 2:
763                         for (j=0; j < returned; j++) {
764                                 display_print_driver_2 (&(ctr.info2[j]));
765                         }
766                         break;
767                 case 3:
768                         for (j=0; j < returned; j++) {
769                                 display_print_driver_3 (&(ctr.info3[j]));
770                         }
771                         break;
772                 default:
773                         printf("unknown info level %d\n", info_level);
774                         break;
775                 }
776         }
777         
778         return result;
779 }
780
781 /****************************************************************************
782 printer info level 1 display function
783 ****************************************************************************/
784 static void display_printdriverdir_1(DRIVER_DIRECTORY_1 *i1)
785 {
786         fstring name;
787         if (i1 == NULL)
788                 return;
789  
790         rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE);
791  
792         printf ("\tDirectory Name:[%s]\n", name);
793 }
794
795 /***********************************************************************
796  * Get printer driver directory information
797  */
798 static NTSTATUS cmd_spoolss_getdriverdir(struct cli_state *cli, 
799                                          TALLOC_CTX *mem_ctx,
800                                          int argc, char **argv)
801 {
802         NTSTATUS                result;
803         fstring                 env;
804         DRIVER_DIRECTORY_CTR    ctr;
805
806         if (argc > 2) 
807         {
808                 printf("Usage: %s [environment]\n", argv[0]);
809                 return NT_STATUS_OK;
810         }
811
812         /* get the arguments need to open the printer handle */
813         if (argc == 2)
814                 fstrcpy (env, argv[1]);
815         else
816                 fstrcpy (env, "Windows NT x86");
817
818         /* Get the directory.  Only use Info level 1 */
819         result = cli_spoolss_getprinterdriverdir (cli, mem_ctx, 1, env, &ctr);
820         if (!NT_STATUS_IS_OK(result)) {
821                 return result;
822         }
823
824         
825         display_printdriverdir_1 (ctr.info1);
826
827         return result;
828 }
829
830 /*******************************************************************************
831  set the version and environment fields of a DRIVER_INFO_3 struct
832  ******************************************************************************/
833 void set_drv_info_3_env (DRIVER_INFO_3 *info, const char *arch)
834 {
835
836         int i;
837         
838         for (i=0; archi_table[i].long_archi != NULL; i++) 
839         {
840                 if (strcmp(arch, archi_table[i].short_archi) == 0)
841                 {
842                         info->version = archi_table[i].version;
843                         init_unistr (&info->architecture, archi_table[i].long_archi);
844                         break;
845                 }
846         }
847         
848         if (archi_table[i].long_archi == NULL)
849         {
850                 DEBUG(0, ("set_drv_info_3_env: Unknown arch [%s]\n", arch));
851         }
852         
853         return;
854 }
855
856
857 /**************************************************************************
858  wrapper for strtok to get the next parameter from a delimited list.
859  Needed to handle the empty parameter string denoted by "NULL"
860  *************************************************************************/
861 static char* get_driver_3_param (char* str, char* delim, UNISTR* dest)
862 {
863         char    *ptr;
864
865         /* get the next token */
866         ptr = strtok(str, delim);
867
868         /* a string of 'NULL' is used to represent an empty
869            parameter because two consecutive delimiters
870            will not return an empty string.  See man strtok(3)
871            for details */
872         if (StrCaseCmp(ptr, "NULL") == 0)
873                 ptr = NULL;
874
875         if (dest != NULL)
876                 init_unistr(dest, ptr); 
877
878         return ptr;
879 }
880
881 /********************************************************************************
882  fill in the members of a DRIVER_INFO_3 struct using a character 
883  string in the form of
884          <Long Printer Name>:<Driver File Name>:<Data File Name>:\
885              <Config File Name>:<Help File Name>:<Language Monitor Name>:\
886              <Default Data Type>:<Comma Separated list of Files> 
887  *******************************************************************************/
888 static BOOL init_drv_info_3_members (
889         TALLOC_CTX *mem_ctx, 
890         DRIVER_INFO_3 *info, 
891         char *args
892 )
893 {
894         char    *str, *str2;
895         uint32  len, i;
896         
897         /* fill in the UNISTR fields */
898         str = get_driver_3_param (args, ":", &info->name);
899         str = get_driver_3_param (NULL, ":", &info->driverpath);
900         str = get_driver_3_param (NULL, ":", &info->datafile);
901         str = get_driver_3_param (NULL, ":", &info->configfile);
902         str = get_driver_3_param (NULL, ":", &info->helpfile);
903         str = get_driver_3_param (NULL, ":", &info->monitorname);
904         str = get_driver_3_param (NULL, ":", &info->defaultdatatype);
905
906         /* <Comma Separated List of Dependent Files> */
907         str2 = get_driver_3_param (NULL, ":", NULL); /* save the beginning of the string */
908         str = str2;                     
909
910         /* begin to strip out each filename */
911         str = strtok(str, ",");         
912         len = 0;
913         while (str != NULL)
914         {
915                 /* keep a cumlative count of the str lengths */
916                 len += strlen(str)+1;
917                 str = strtok(NULL, ",");
918         }
919
920         /* allocate the space; add one extra slot for a terminating NULL.
921            Each filename is NULL terminated and the end contains a double
922            NULL */
923         if ((info->dependentfiles=(uint16*)talloc(mem_ctx, (len+1)*sizeof(uint16))) == NULL)
924         {
925                 DEBUG(0,("init_drv_info_3_members: Unable to malloc memory for dependenfiles\n"));
926                 return False;
927         }
928         for (i=0; i<len; i++)
929         {
930                 info->dependentfiles[i] = SSVAL(&info->dependentfiles[i], 0, str2[i]);
931         }
932         info->dependentfiles[len] = '\0';
933
934         return True;
935 }
936
937
938 static NTSTATUS cmd_spoolss_addprinterdriver(struct cli_state *cli, 
939                                              TALLOC_CTX *mem_ctx,
940                                              int argc, char **argv)
941 {
942         NTSTATUS                result;
943         uint32                  level = 3;
944         PRINTER_DRIVER_CTR      ctr;
945         DRIVER_INFO_3           info3;
946         fstring                 arch;
947         fstring                 driver_name;
948
949         /* parse the command arguements */
950         if (argc != 3)
951         {
952                 printf ("Usage: %s <Environment>\\\n", argv[0]);
953                 printf ("\t<Long Printer Name>:<Driver File Name>:<Data File Name>:\\\n");
954                 printf ("\t<Config File Name>:<Help File Name>:<Language Monitor Name>:\\\n");
955                 printf ("\t<Default Data Type>:<Comma Separated list of Files>\n");
956
957                 return NT_STATUS_OK;
958         }
959                 
960         /* Fill in the DRIVER_INFO_3 struct */
961         ZERO_STRUCT(info3);
962         if (!get_short_archi(arch, argv[1]))
963         {
964                 printf ("Error Unknown architechture [%s]\n", argv[1]);
965                 return NT_STATUS_INVALID_PARAMETER;
966         }
967         else
968                 set_drv_info_3_env(&info3, arch);
969
970         if (!init_drv_info_3_members(mem_ctx, &info3, argv[2]))
971         {
972                 printf ("Error Invalid parameter list - %s.\n", argv[2]);
973                 return NT_STATUS_INVALID_PARAMETER;
974         }
975
976
977         ctr.info3 = &info3;
978         result = cli_spoolss_addprinterdriver (cli, mem_ctx, level, &ctr);
979         if (!NT_STATUS_IS_OK(result)) {
980                 return result;
981         }
982
983         rpcstr_pull(driver_name, info3.name.buffer, sizeof(driver_name), 0, STR_TERMINATE);
984         printf ("Printer Driver %s successfully installed.\n", driver_name);
985
986         return result;
987 }
988
989
990 static NTSTATUS cmd_spoolss_addprinterex(struct cli_state *cli, 
991                                          TALLOC_CTX *mem_ctx, 
992                                          int argc, char **argv)
993 {
994         NTSTATUS                result;
995         uint32                  level = 2;
996         PRINTER_INFO_CTR        ctr;
997         PRINTER_INFO_2          info2;
998         fstring                 servername;
999         
1000         /* parse the command arguements */
1001         if (argc != 5)
1002         {
1003                 printf ("Usage: %s <name> <shared name> <driver> <port>\n", argv[0]);
1004                 return NT_STATUS_OK;
1005         }
1006         
1007         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1008         strupper (servername);
1009
1010         /* Fill in the DRIVER_INFO_3 struct */
1011         ZERO_STRUCT(info2);
1012 #if 0   /* JERRY */
1013         init_unistr( &info2.servername,         servername);
1014 #endif
1015         init_unistr( &info2.printername,        argv[1]);
1016         init_unistr( &info2.sharename,          argv[2]);
1017         init_unistr( &info2.drivername,         argv[3]);
1018         init_unistr( &info2.portname,           argv[4]);
1019         init_unistr( &info2.comment,            "Created by rpcclient");
1020         init_unistr( &info2.printprocessor,     "winprint");
1021         init_unistr( &info2.datatype,           "RAW");
1022         info2.devmode =         NULL;
1023         info2.secdesc =         NULL;
1024         info2.attributes        = PRINTER_ATTRIBUTE_SHARED;
1025         info2.priority          = 0;
1026         info2.defaultpriority   = 0;
1027         info2.starttime         = 0;
1028         info2.untiltime         = 0;
1029         
1030         /* These three fields must not be used by AddPrinter() 
1031            as defined in the MS Platform SDK documentation..  
1032            --jerry
1033         info2.status            = 0;
1034         info2.cjobs             = 0;
1035         info2.averageppm        = 0;
1036         */
1037
1038         ctr.printers_2 = &info2;
1039         result = cli_spoolss_addprinterex (cli, mem_ctx, level, &ctr);
1040         if (!NT_STATUS_IS_OK(result)) {
1041                 return result;
1042         }
1043
1044         printf ("Printer %s successfully installed.\n", argv[1]);
1045
1046         return result;
1047 }
1048
1049 static NTSTATUS cmd_spoolss_setdriver(struct cli_state *cli, 
1050                                       TALLOC_CTX *mem_ctx,
1051                                       int argc, char **argv)
1052 {
1053         POLICY_HND              pol;
1054         WERROR                  result;
1055         NTSTATUS                nt_status;
1056         uint32                  level = 2;
1057         BOOL                    opened_hnd = False;
1058         PRINTER_INFO_CTR        ctr;
1059         PRINTER_INFO_2          info2;
1060         fstring                 servername,
1061                                 printername,
1062                                 user;
1063         
1064         /* parse the command arguements */
1065         if (argc != 3)
1066         {
1067                 printf ("Usage: %s <printer> <driver>\n", argv[0]);
1068                 return NT_STATUS_OK;
1069         }
1070
1071         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1072         strupper (servername);
1073         slprintf (printername, sizeof(fstring)-1, "%s\\%s", servername, argv[1]);
1074         fstrcpy  (user, cli->user_name);
1075
1076         /* Get a printer handle */
1077
1078         result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", 
1079                                              MAXIMUM_ALLOWED_ACCESS, 
1080                                              servername, user, &pol);
1081
1082         nt_status = werror_to_ntstatus(result);
1083
1084         if (!NT_STATUS_IS_OK(nt_status))
1085                 goto done;
1086  
1087         opened_hnd = True;
1088
1089         /* Get printer info */
1090
1091         ZERO_STRUCT (info2);
1092         ctr.printers_2 = &info2;
1093
1094         nt_status = cli_spoolss_getprinter(cli, mem_ctx, &pol, level, &ctr);
1095
1096         if (!NT_STATUS_IS_OK(nt_status)) {
1097                 printf ("Unable to retrieve printer information!\n");
1098                 goto done;
1099         }
1100
1101         /* Set the printer driver */
1102
1103         init_unistr(&ctr.printers_2->drivername, argv[2]);
1104
1105         nt_status = cli_spoolss_setprinter(cli, mem_ctx, &pol, level, &ctr, 0);
1106
1107         if (!NT_STATUS_IS_OK(nt_status)) {
1108                 printf("SetPrinter call failed!\n");
1109                 goto done;;
1110         }
1111
1112         printf("Succesfully set %s to driver %s.\n", argv[1], argv[2]);
1113
1114 done:
1115         /* Cleanup */
1116
1117         if (opened_hnd)
1118                 cli_spoolss_close_printer(cli, mem_ctx, &pol);
1119         
1120         return nt_status;               
1121 }
1122
1123
1124 static NTSTATUS cmd_spoolss_deletedriver(struct cli_state *cli, 
1125                                          TALLOC_CTX *mem_ctx,
1126                                          int argc, char **argv)
1127 {
1128         NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
1129         fstring                 servername;
1130         int                     i;
1131         
1132         /* parse the command arguements */
1133         if (argc != 2)
1134         {
1135                 printf ("Usage: %s <driver>\n", argv[0]);
1136                 return NT_STATUS_OK;
1137         }
1138
1139         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1140         strupper (servername);
1141
1142         /* delete the driver for all architectures */
1143         for (i=0; archi_table[i].long_archi; i++)
1144         {
1145                 /* make the call to remove the driver */
1146                 result = cli_spoolss_deleteprinterdriver(cli, mem_ctx, 
1147                                                          archi_table[i].long_archi, argv[1]);
1148                 if (!NT_STATUS_IS_OK(result)) {
1149                         printf ("Failed to remove driver %s for arch [%s] - error %s!\n", 
1150                                 argv[1], archi_table[i].long_archi, get_nt_error_msg(result));
1151                 }
1152                 else
1153                         printf ("Driver %s removed for arch [%s].\n", argv[1], archi_table[i].long_archi);
1154         }
1155                 
1156         return NT_STATUS_OK;            
1157 }
1158
1159 static NTSTATUS cmd_spoolss_getprintprocdir(struct cli_state *cli, 
1160                                             TALLOC_CTX *mem_ctx,
1161                                             int argc, char **argv)
1162 {
1163         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1164         char *servername = NULL, *environment = NULL;
1165         fstring procdir;
1166         
1167         /* parse the command arguements */
1168         if (argc < 2 || argc > 3) {
1169                 printf ("Usage: %s <server> [environment]\n", argv[0]);
1170                 return NT_STATUS_OK;
1171         }
1172
1173         asprintf(&servername, "\\\\%s", cli->desthost);
1174         strupper(servername);
1175
1176         asprintf(&environment, "%s", (argc == 3) ? argv[2] : 
1177                  PRINTER_DRIVER_ARCHITECTURE);
1178
1179         result = cli_spoolss_getprintprocessordirectory(
1180                 cli, mem_ctx, servername, environment, procdir);
1181
1182         if (NT_STATUS_IS_OK(result))
1183                 printf("%s", procdir);
1184
1185         SAFE_FREE(servername);
1186         SAFE_FREE(environment);
1187
1188         return result;
1189 }
1190
1191 /* List of commands exported by this module */
1192 struct cmd_set spoolss_commands[] = {
1193
1194         { "SPOOLSS"  },
1195
1196         { "adddriver",          cmd_spoolss_addprinterdriver,   PIPE_SPOOLSS, "Add a print driver",                  "" },
1197         { "addprinter",         cmd_spoolss_addprinterex,       PIPE_SPOOLSS, "Add a printer",                       "" },
1198         { "deldriver",          cmd_spoolss_deletedriver,       PIPE_SPOOLSS, "Delete a printer driver",             "" },
1199         { "enumdata",           cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Enumerate printer data (*)",          "" },
1200         { "enumjobs",           cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Enumerate print jobs (*)",            "" },
1201         { "enumports",          cmd_spoolss_enum_ports,         PIPE_SPOOLSS, "Enumerate printer ports",             "" },
1202         { "enumdrivers",        cmd_spoolss_enum_drivers,       PIPE_SPOOLSS, "Enumerate installed printer drivers", "" },
1203         { "enumprinters",       cmd_spoolss_enum_printers,      PIPE_SPOOLSS, "Enumerate printers",                  "" },
1204         { "getdata",            cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Get print driver data (*)",           "" },
1205         { "getdriver",          cmd_spoolss_getdriver,          PIPE_SPOOLSS, "Get print driver information",        "" },
1206         { "getdriverdir",       cmd_spoolss_getdriverdir,       PIPE_SPOOLSS, "Get print driver upload directory",   "" },
1207         { "getprinter",         cmd_spoolss_getprinter,         PIPE_SPOOLSS, "Get printer info",                    "" },
1208         { "openprinter",        cmd_spoolss_open_printer_ex,    PIPE_SPOOLSS, "Open printer handle",                 "" },
1209         { "setdriver",          cmd_spoolss_setdriver,          PIPE_SPOOLSS, "Set printer driver",                  "" },
1210         { "getprintprocdir",    cmd_spoolss_getprintprocdir, PIPE_SPOOLSS, "Get print processor directory",          "" },
1211
1212         { NULL }
1213 };