Converted enumprinters and enumports cmd functions to use WERROR
[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         WERROR                  result;
302         uint32                  info_level = 1;
303         PRINTER_INFO_CTR        ctr;
304         uint32                  i = 0, num_printers, needed;
305
306         if (argc > 2) 
307         {
308                 printf("Usage: %s [level]\n", argv[0]);
309                 return NT_STATUS_OK;
310         }
311
312         if (argc == 2) {
313                 info_level = atoi(argv[1]);
314         }
315
316         /* Enumerate printers  -- Should we enumerate types other 
317            than PRINTER_ENUM_LOCAL?  Maybe accept as a parameter?  --jerry */
318
319         ZERO_STRUCT(ctr);
320
321         result = cli_spoolss_enum_printers(
322                 cli, mem_ctx, 0, &needed, PRINTER_ENUM_LOCAL, 
323                 info_level, &num_printers, &ctr);
324
325         if (W_ERROR_V(result) == ERRinsufficientbuffer)
326                 result = cli_spoolss_enum_printers(
327                         cli, mem_ctx, needed, NULL, PRINTER_ENUM_LOCAL, 
328                         info_level, &num_printers, &ctr);
329
330         if (W_ERROR_IS_OK(result)) {
331                 if (!num_printers)
332                         printf ("No Printers printers returned.\n");
333         
334                 switch(info_level) {
335                 case 0:
336                         for (i=0; i < num_printers; i++)
337                                 display_print_info_0(&ctr.printers_0[i]);
338                         break;
339                 case 1:
340                         for (i=0; i < num_printers; i++)
341                                 display_print_info_1(&ctr.printers_1[i]);
342                         break;
343                 case 2:
344                         for (i=0; i < num_printers; i++)
345                                 display_print_info_2(&ctr.printers_2[i]);
346                         break;
347                 case 3:
348                         for (i=0; i < num_printers; i++)
349                                 display_print_info_3(&ctr.printers_3[i]);
350                         break;
351                 default:
352                         printf("unknown info level %d\n", info_level);
353                         break;
354                 }
355         }
356
357         return werror_to_ntstatus(result);
358 }
359
360 /****************************************************************************
361 port info level 1 display function
362 ****************************************************************************/
363 static void display_port_info_1(PORT_INFO_1 *i1)
364 {
365         fstring buffer;
366         
367         rpcstr_pull(buffer, i1->port_name.buffer, sizeof(buffer), 0, STR_TERMINATE);
368         printf("\tPort Name:\t[%s]\n", buffer);
369 }
370
371 /****************************************************************************
372 port info level 2 display function
373 ****************************************************************************/
374 static void display_port_info_2(PORT_INFO_2 *i2)
375 {
376         fstring buffer;
377         
378         rpcstr_pull(buffer, i2->port_name.buffer, sizeof(buffer), 0, STR_TERMINATE);
379         printf("\tPort Name:\t[%s]\n", buffer);
380         rpcstr_pull(buffer, i2->monitor_name.buffer, sizeof(buffer), 0, STR_TERMINATE);
381
382         printf("\tMonitor Name:\t[%s]\n", buffer);
383         rpcstr_pull(buffer, i2->description.buffer, sizeof(buffer), 0, STR_TERMINATE);
384
385         printf("\tDescription:\t[%s]\n", buffer);
386         printf("\tPort Type:\t[%d]\n", i2->port_type);
387         printf("\tReserved:\t[%d]\n", i2->reserved);
388         printf("\n");
389 }
390
391 /* Enumerate ports */
392
393 static NTSTATUS cmd_spoolss_enum_ports(struct cli_state *cli, 
394                                        TALLOC_CTX *mem_ctx, int argc, 
395                                        char **argv)
396 {
397         WERROR                  result;
398         uint32                  needed, info_level = 1;
399         PORT_INFO_CTR           ctr;
400         int                     returned;
401         
402         if (argc > 2) {
403                 printf("Usage: %s [level]\n", argv[0]);
404                 return NT_STATUS_OK;
405         }
406         
407         if (argc == 2)
408                 info_level = atoi(argv[1]);
409
410         /* Enumerate ports */
411
412         ZERO_STRUCT(ctr);
413
414         result = cli_spoolss_enum_ports(cli, mem_ctx, 0, &needed, info_level, 
415                                         &returned, &ctr);
416
417         if (W_ERROR_V(result) == ERRinsufficientbuffer)
418                 result = cli_spoolss_enum_ports(cli, mem_ctx, needed, NULL,
419                                                 info_level, &returned, &ctr);
420
421         if (W_ERROR_IS_OK(result)) {
422                 int i;
423
424                 for (i = 0; i < returned; i++) {
425                         switch (info_level) {
426                         case 1:
427                                 display_port_info_1(&ctr.port.info_1[i]);
428                                 break;
429                         case 2:
430                                 display_port_info_2(&ctr.port.info_2[i]);
431                                 break;
432                         default:
433                                 printf("unknown info level %d\n", info_level);
434                                 break;
435                         }
436                 }
437         }
438         
439         return werror_to_ntstatus(result);
440 }
441
442 /***********************************************************************
443  * Get printer information
444  */
445 static NTSTATUS cmd_spoolss_getprinter(struct cli_state *cli, 
446                                        TALLOC_CTX *mem_ctx,
447                                        int argc, char **argv)
448 {
449         POLICY_HND      pol;
450         WERROR          werror;
451         NTSTATUS        result;
452         uint32          info_level = 1;
453         BOOL            opened_hnd = False;
454         PRINTER_INFO_CTR ctr;
455         fstring         printername, 
456                         servername,
457                         user;
458
459         if (argc == 1 || argc > 3) {
460                 printf("Usage: %s <printername> [level]\n", argv[0]);
461                 return NT_STATUS_OK;
462         }
463
464         /* Open a printer handle */
465         if (argc == 3) {
466                 info_level = atoi(argv[2]);
467         }
468
469         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
470         strupper (servername);
471         slprintf (printername, sizeof(fstring)-1, "%s\\%s", servername, argv[1]);
472         fstrcpy  (user, cli->user_name);
473         
474         /* get a printer handle */
475
476         werror = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, 
477                                              "", MAXIMUM_ALLOWED_ACCESS, 
478                                              servername, user, &pol);
479
480         result = werror_to_ntstatus(werror);
481
482         if (!NT_STATUS_IS_OK(result))
483                 goto done;
484  
485         opened_hnd = True;
486
487         /* Get printer info */
488
489         result = cli_spoolss_getprinter(cli, mem_ctx, &pol, info_level, &ctr);
490
491         if (!NT_STATUS_IS_OK(result))
492                 goto done;
493
494         /* Display printer info */
495
496         switch (info_level) {
497         case 0: 
498                 display_print_info_0(ctr.printers_0);
499                 break;
500         case 1:
501                 display_print_info_1(ctr.printers_1);
502                 break;
503         case 2:
504                 display_print_info_2(ctr.printers_2);
505                 break;
506         case 3:
507                 display_print_info_3(ctr.printers_3);
508                 break;
509         default:
510                 printf("unknown info level %d\n", info_level);
511                 break;
512         }
513
514  done: 
515         if (opened_hnd) 
516                 cli_spoolss_close_printer(cli, mem_ctx, &pol);
517
518         return result;
519 }
520
521 /****************************************************************************
522 printer info level 0 display function
523 ****************************************************************************/
524 static void display_print_driver_1(DRIVER_INFO_1 *i1)
525 {
526         fstring name;
527         if (i1 == NULL)
528                 return;
529
530         rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE);
531
532         printf ("Printer Driver Info 1:\n");
533         printf ("\tDriver Name: [%s]\n\n", name);
534         
535         return;
536 }
537
538 /****************************************************************************
539 printer info level 1 display function
540 ****************************************************************************/
541 static void display_print_driver_2(DRIVER_INFO_2 *i1)
542 {
543         fstring name;
544         fstring architecture;
545         fstring driverpath;
546         fstring datafile;
547         fstring configfile;
548         if (i1 == NULL)
549                 return;
550
551         rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE);
552         rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), 0, STR_TERMINATE);
553         rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), 0, STR_TERMINATE);
554         rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), 0, STR_TERMINATE);
555         rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), 0, STR_TERMINATE);
556
557         printf ("Printer Driver Info 2:\n");
558         printf ("\tVersion: [%x]\n", i1->version);
559         printf ("\tDriver Name: [%s]\n", name);
560         printf ("\tArchitecture: [%s]\n", architecture);
561         printf ("\tDriver Path: [%s]\n", driverpath);
562         printf ("\tDatafile: [%s]\n", datafile);
563         printf ("\tConfigfile: [%s]\n\n", configfile);
564
565         return;
566 }
567
568 /****************************************************************************
569 printer info level 2 display function
570 ****************************************************************************/
571 static void display_print_driver_3(DRIVER_INFO_3 *i1)
572 {
573         fstring name;
574         fstring architecture;
575         fstring driverpath;
576         fstring datafile;
577         fstring configfile;
578         fstring helpfile;
579         fstring dependentfiles;
580         fstring monitorname;
581         fstring defaultdatatype;
582         
583         int length=0;
584         BOOL valid = True;
585         
586         if (i1 == NULL)
587                 return;
588
589         rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE);
590         rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), 0, STR_TERMINATE);
591         rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), 0, STR_TERMINATE);
592         rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), 0, STR_TERMINATE);
593         rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), 0, STR_TERMINATE);
594         rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), 0, STR_TERMINATE);
595         rpcstr_pull(monitorname, i1->monitorname.buffer, sizeof(monitorname), 0, STR_TERMINATE);
596         rpcstr_pull(defaultdatatype, i1->defaultdatatype.buffer, sizeof(defaultdatatype), 0, STR_TERMINATE);
597
598         printf ("Printer Driver Info 3:\n");
599         printf ("\tVersion: [%x]\n", i1->version);
600         printf ("\tDriver Name: [%s]\n",name);
601         printf ("\tArchitecture: [%s]\n", architecture);
602         printf ("\tDriver Path: [%s]\n", driverpath);
603         printf ("\tDatafile: [%s]\n", datafile);
604         printf ("\tConfigfile: [%s]\n", configfile);
605         printf ("\tHelpfile: [%s]\n\n", helpfile);
606
607         while (valid)
608         {
609                 rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), 0, STR_TERMINATE);
610                 
611                 length+=strlen(dependentfiles)+1;
612                 
613                 if (strlen(dependentfiles) > 0)
614                 {
615                         printf ("\tDependentfiles: [%s]\n", dependentfiles);
616                 }
617                 else
618                 {
619                         valid = False;
620                 }
621         }
622         
623         printf ("\n");
624
625         printf ("\tMonitorname: [%s]\n", monitorname);
626         printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype);
627
628         return; 
629 }
630
631 /***********************************************************************
632  * Get printer information
633  */
634 static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, 
635                                       TALLOC_CTX *mem_ctx,
636                                       int argc, char **argv)
637 {
638         POLICY_HND      pol;
639         WERROR          werror;
640         NTSTATUS        result;
641         uint32          info_level = 3;
642         BOOL            opened_hnd = False;
643         PRINTER_DRIVER_CTR      ctr;
644         fstring         printername, 
645                         servername, 
646                         user;
647         uint32          i;
648
649         if ((argc == 1) || (argc > 3)) 
650         {
651                 printf("Usage: %s <printername> [level]\n", argv[0]);
652                 return NT_STATUS_OK;
653         }
654
655         /* get the arguments need to open the printer handle */
656         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
657         strupper (servername);
658         fstrcpy  (user, cli->user_name);
659         fstrcpy  (printername, argv[1]);
660         if (argc == 3)
661                 info_level = atoi(argv[2]);
662
663         /* Open a printer handle */
664
665         werror = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", 
666                                              MAXIMUM_ALLOWED_ACCESS, 
667                                              servername, user, &pol);
668
669         result = werror_to_ntstatus(werror);
670
671         if (!NT_STATUS_IS_OK(result)) {
672                 printf("Error opening printer handle for %s!\n", printername);
673                 return result;
674         }
675
676         opened_hnd = True;
677
678         /* loop through and print driver info level for each architecture */
679         for (i=0; archi_table[i].long_archi!=NULL; i++) 
680         {
681                 result = cli_spoolss_getprinterdriver(cli, mem_ctx, &pol, info_level, 
682                                                        archi_table[i].long_archi, &ctr);
683                 if (!NT_STATUS_IS_OK(result)) {
684                         continue;
685                 }
686
687                         
688                 printf ("\n[%s]\n", archi_table[i].long_archi);
689                 switch (info_level) 
690                 {
691                         
692                 case 1:
693                         display_print_driver_1 (ctr.info1);
694                         break;
695                 case 2:
696                         display_print_driver_2 (ctr.info2);
697                         break;
698                 case 3:
699                         display_print_driver_3 (ctr.info3);
700                         break;
701                 default:
702                         printf("unknown info level %d\n", info_level);
703                         break;
704                 }
705         }
706         
707         /* Cleanup */
708
709         if (opened_hnd)
710                 cli_spoolss_close_printer (cli, mem_ctx, &pol);
711         
712         return result;
713 }
714
715 /***********************************************************************
716  * Get printer information
717  */
718 static NTSTATUS cmd_spoolss_enum_drivers(struct cli_state *cli, 
719                                          TALLOC_CTX *mem_ctx,
720                                          int argc, char **argv)
721 {
722         NTSTATUS        result = NT_STATUS_OK;
723         uint32          info_level = 1;
724         PRINTER_DRIVER_CTR      ctr;
725         fstring         servername;
726         uint32          i, j,
727                         returned;
728
729         if (argc > 2) 
730         {
731                 printf("Usage: enumdrivers [level]\n");
732                 return NT_STATUS_OK;
733         }
734
735         /* get the arguments need to open the printer handle */
736         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
737         strupper (servername);
738         if (argc == 2)
739                 info_level = atoi(argv[1]);
740
741
742         /* loop through and print driver info level for each architecture */
743         for (i=0; archi_table[i].long_archi!=NULL; i++) 
744         {
745                 returned = 0;   
746                 result = cli_spoolss_enumprinterdrivers (cli, mem_ctx, info_level, 
747                                 archi_table[i].long_archi, &returned, &ctr);
748
749                 if (returned == 0)
750                         continue;
751                         
752
753                 if (!NT_STATUS_IS_OK(result))
754                 {
755                         printf ("Error getting driver for environment [%s] - %s\n",
756                                 archi_table[i].long_archi, get_nt_error_msg(result));
757                         continue;
758                 }
759                 
760                 printf ("\n[%s]\n", archi_table[i].long_archi);
761                 switch (info_level) 
762                 {
763                         
764                 case 1:
765                         for (j=0; j < returned; j++) {
766                                 display_print_driver_1 (&(ctr.info1[j]));
767                         }
768                         break;
769                 case 2:
770                         for (j=0; j < returned; j++) {
771                                 display_print_driver_2 (&(ctr.info2[j]));
772                         }
773                         break;
774                 case 3:
775                         for (j=0; j < returned; j++) {
776                                 display_print_driver_3 (&(ctr.info3[j]));
777                         }
778                         break;
779                 default:
780                         printf("unknown info level %d\n", info_level);
781                         break;
782                 }
783         }
784         
785         return result;
786 }
787
788 /****************************************************************************
789 printer info level 1 display function
790 ****************************************************************************/
791 static void display_printdriverdir_1(DRIVER_DIRECTORY_1 *i1)
792 {
793         fstring name;
794         if (i1 == NULL)
795                 return;
796  
797         rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE);
798  
799         printf ("\tDirectory Name:[%s]\n", name);
800 }
801
802 /***********************************************************************
803  * Get printer driver directory information
804  */
805 static NTSTATUS cmd_spoolss_getdriverdir(struct cli_state *cli, 
806                                          TALLOC_CTX *mem_ctx,
807                                          int argc, char **argv)
808 {
809         NTSTATUS                result;
810         fstring                 env;
811         DRIVER_DIRECTORY_CTR    ctr;
812
813         if (argc > 2) 
814         {
815                 printf("Usage: %s [environment]\n", argv[0]);
816                 return NT_STATUS_OK;
817         }
818
819         /* get the arguments need to open the printer handle */
820         if (argc == 2)
821                 fstrcpy (env, argv[1]);
822         else
823                 fstrcpy (env, "Windows NT x86");
824
825         /* Get the directory.  Only use Info level 1 */
826         result = cli_spoolss_getprinterdriverdir (cli, mem_ctx, 1, env, &ctr);
827         if (!NT_STATUS_IS_OK(result)) {
828                 return result;
829         }
830
831         
832         display_printdriverdir_1 (ctr.info1);
833
834         return result;
835 }
836
837 /*******************************************************************************
838  set the version and environment fields of a DRIVER_INFO_3 struct
839  ******************************************************************************/
840 void set_drv_info_3_env (DRIVER_INFO_3 *info, const char *arch)
841 {
842
843         int i;
844         
845         for (i=0; archi_table[i].long_archi != NULL; i++) 
846         {
847                 if (strcmp(arch, archi_table[i].short_archi) == 0)
848                 {
849                         info->version = archi_table[i].version;
850                         init_unistr (&info->architecture, archi_table[i].long_archi);
851                         break;
852                 }
853         }
854         
855         if (archi_table[i].long_archi == NULL)
856         {
857                 DEBUG(0, ("set_drv_info_3_env: Unknown arch [%s]\n", arch));
858         }
859         
860         return;
861 }
862
863
864 /**************************************************************************
865  wrapper for strtok to get the next parameter from a delimited list.
866  Needed to handle the empty parameter string denoted by "NULL"
867  *************************************************************************/
868 static char* get_driver_3_param (char* str, char* delim, UNISTR* dest)
869 {
870         char    *ptr;
871
872         /* get the next token */
873         ptr = strtok(str, delim);
874
875         /* a string of 'NULL' is used to represent an empty
876            parameter because two consecutive delimiters
877            will not return an empty string.  See man strtok(3)
878            for details */
879         if (StrCaseCmp(ptr, "NULL") == 0)
880                 ptr = NULL;
881
882         if (dest != NULL)
883                 init_unistr(dest, ptr); 
884
885         return ptr;
886 }
887
888 /********************************************************************************
889  fill in the members of a DRIVER_INFO_3 struct using a character 
890  string in the form of
891          <Long Printer Name>:<Driver File Name>:<Data File Name>:\
892              <Config File Name>:<Help File Name>:<Language Monitor Name>:\
893              <Default Data Type>:<Comma Separated list of Files> 
894  *******************************************************************************/
895 static BOOL init_drv_info_3_members (
896         TALLOC_CTX *mem_ctx, 
897         DRIVER_INFO_3 *info, 
898         char *args
899 )
900 {
901         char    *str, *str2;
902         uint32  len, i;
903         
904         /* fill in the UNISTR fields */
905         str = get_driver_3_param (args, ":", &info->name);
906         str = get_driver_3_param (NULL, ":", &info->driverpath);
907         str = get_driver_3_param (NULL, ":", &info->datafile);
908         str = get_driver_3_param (NULL, ":", &info->configfile);
909         str = get_driver_3_param (NULL, ":", &info->helpfile);
910         str = get_driver_3_param (NULL, ":", &info->monitorname);
911         str = get_driver_3_param (NULL, ":", &info->defaultdatatype);
912
913         /* <Comma Separated List of Dependent Files> */
914         str2 = get_driver_3_param (NULL, ":", NULL); /* save the beginning of the string */
915         str = str2;                     
916
917         /* begin to strip out each filename */
918         str = strtok(str, ",");         
919         len = 0;
920         while (str != NULL)
921         {
922                 /* keep a cumlative count of the str lengths */
923                 len += strlen(str)+1;
924                 str = strtok(NULL, ",");
925         }
926
927         /* allocate the space; add one extra slot for a terminating NULL.
928            Each filename is NULL terminated and the end contains a double
929            NULL */
930         if ((info->dependentfiles=(uint16*)talloc(mem_ctx, (len+1)*sizeof(uint16))) == NULL)
931         {
932                 DEBUG(0,("init_drv_info_3_members: Unable to malloc memory for dependenfiles\n"));
933                 return False;
934         }
935         for (i=0; i<len; i++)
936         {
937                 info->dependentfiles[i] = SSVAL(&info->dependentfiles[i], 0, str2[i]);
938         }
939         info->dependentfiles[len] = '\0';
940
941         return True;
942 }
943
944
945 static NTSTATUS cmd_spoolss_addprinterdriver(struct cli_state *cli, 
946                                              TALLOC_CTX *mem_ctx,
947                                              int argc, char **argv)
948 {
949         NTSTATUS                result;
950         uint32                  level = 3;
951         PRINTER_DRIVER_CTR      ctr;
952         DRIVER_INFO_3           info3;
953         fstring                 arch;
954         fstring                 driver_name;
955
956         /* parse the command arguements */
957         if (argc != 3)
958         {
959                 printf ("Usage: %s <Environment>\\\n", argv[0]);
960                 printf ("\t<Long Printer Name>:<Driver File Name>:<Data File Name>:\\\n");
961                 printf ("\t<Config File Name>:<Help File Name>:<Language Monitor Name>:\\\n");
962                 printf ("\t<Default Data Type>:<Comma Separated list of Files>\n");
963
964                 return NT_STATUS_OK;
965         }
966                 
967         /* Fill in the DRIVER_INFO_3 struct */
968         ZERO_STRUCT(info3);
969         if (!get_short_archi(arch, argv[1]))
970         {
971                 printf ("Error Unknown architechture [%s]\n", argv[1]);
972                 return NT_STATUS_INVALID_PARAMETER;
973         }
974         else
975                 set_drv_info_3_env(&info3, arch);
976
977         if (!init_drv_info_3_members(mem_ctx, &info3, argv[2]))
978         {
979                 printf ("Error Invalid parameter list - %s.\n", argv[2]);
980                 return NT_STATUS_INVALID_PARAMETER;
981         }
982
983
984         ctr.info3 = &info3;
985         result = cli_spoolss_addprinterdriver (cli, mem_ctx, level, &ctr);
986         if (!NT_STATUS_IS_OK(result)) {
987                 return result;
988         }
989
990         rpcstr_pull(driver_name, info3.name.buffer, sizeof(driver_name), 0, STR_TERMINATE);
991         printf ("Printer Driver %s successfully installed.\n", driver_name);
992
993         return result;
994 }
995
996
997 static NTSTATUS cmd_spoolss_addprinterex(struct cli_state *cli, 
998                                          TALLOC_CTX *mem_ctx, 
999                                          int argc, char **argv)
1000 {
1001         NTSTATUS                result;
1002         uint32                  level = 2;
1003         PRINTER_INFO_CTR        ctr;
1004         PRINTER_INFO_2          info2;
1005         fstring                 servername;
1006         
1007         /* parse the command arguements */
1008         if (argc != 5)
1009         {
1010                 printf ("Usage: %s <name> <shared name> <driver> <port>\n", argv[0]);
1011                 return NT_STATUS_OK;
1012         }
1013         
1014         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1015         strupper (servername);
1016
1017         /* Fill in the DRIVER_INFO_3 struct */
1018         ZERO_STRUCT(info2);
1019 #if 0   /* JERRY */
1020         init_unistr( &info2.servername,         servername);
1021 #endif
1022         init_unistr( &info2.printername,        argv[1]);
1023         init_unistr( &info2.sharename,          argv[2]);
1024         init_unistr( &info2.drivername,         argv[3]);
1025         init_unistr( &info2.portname,           argv[4]);
1026         init_unistr( &info2.comment,            "Created by rpcclient");
1027         init_unistr( &info2.printprocessor,     "winprint");
1028         init_unistr( &info2.datatype,           "RAW");
1029         info2.devmode =         NULL;
1030         info2.secdesc =         NULL;
1031         info2.attributes        = PRINTER_ATTRIBUTE_SHARED;
1032         info2.priority          = 0;
1033         info2.defaultpriority   = 0;
1034         info2.starttime         = 0;
1035         info2.untiltime         = 0;
1036         
1037         /* These three fields must not be used by AddPrinter() 
1038            as defined in the MS Platform SDK documentation..  
1039            --jerry
1040         info2.status            = 0;
1041         info2.cjobs             = 0;
1042         info2.averageppm        = 0;
1043         */
1044
1045         ctr.printers_2 = &info2;
1046         result = cli_spoolss_addprinterex (cli, mem_ctx, level, &ctr);
1047         if (!NT_STATUS_IS_OK(result)) {
1048                 return result;
1049         }
1050
1051         printf ("Printer %s successfully installed.\n", argv[1]);
1052
1053         return result;
1054 }
1055
1056 static NTSTATUS cmd_spoolss_setdriver(struct cli_state *cli, 
1057                                       TALLOC_CTX *mem_ctx,
1058                                       int argc, char **argv)
1059 {
1060         POLICY_HND              pol;
1061         WERROR                  result;
1062         NTSTATUS                nt_status;
1063         uint32                  level = 2;
1064         BOOL                    opened_hnd = False;
1065         PRINTER_INFO_CTR        ctr;
1066         PRINTER_INFO_2          info2;
1067         fstring                 servername,
1068                                 printername,
1069                                 user;
1070         
1071         /* parse the command arguements */
1072         if (argc != 3)
1073         {
1074                 printf ("Usage: %s <printer> <driver>\n", argv[0]);
1075                 return NT_STATUS_OK;
1076         }
1077
1078         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1079         strupper (servername);
1080         slprintf (printername, sizeof(fstring)-1, "%s\\%s", servername, argv[1]);
1081         fstrcpy  (user, cli->user_name);
1082
1083         /* Get a printer handle */
1084
1085         result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", 
1086                                              MAXIMUM_ALLOWED_ACCESS, 
1087                                              servername, user, &pol);
1088
1089         nt_status = werror_to_ntstatus(result);
1090
1091         if (!NT_STATUS_IS_OK(nt_status))
1092                 goto done;
1093  
1094         opened_hnd = True;
1095
1096         /* Get printer info */
1097
1098         ZERO_STRUCT (info2);
1099         ctr.printers_2 = &info2;
1100
1101         nt_status = cli_spoolss_getprinter(cli, mem_ctx, &pol, level, &ctr);
1102
1103         if (!NT_STATUS_IS_OK(nt_status)) {
1104                 printf ("Unable to retrieve printer information!\n");
1105                 goto done;
1106         }
1107
1108         /* Set the printer driver */
1109
1110         init_unistr(&ctr.printers_2->drivername, argv[2]);
1111
1112         nt_status = cli_spoolss_setprinter(cli, mem_ctx, &pol, level, &ctr, 0);
1113
1114         if (!NT_STATUS_IS_OK(nt_status)) {
1115                 printf("SetPrinter call failed!\n");
1116                 goto done;;
1117         }
1118
1119         printf("Succesfully set %s to driver %s.\n", argv[1], argv[2]);
1120
1121 done:
1122         /* Cleanup */
1123
1124         if (opened_hnd)
1125                 cli_spoolss_close_printer(cli, mem_ctx, &pol);
1126         
1127         return nt_status;               
1128 }
1129
1130
1131 static NTSTATUS cmd_spoolss_deletedriver(struct cli_state *cli, 
1132                                          TALLOC_CTX *mem_ctx,
1133                                          int argc, char **argv)
1134 {
1135         NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
1136         fstring                 servername;
1137         int                     i;
1138         
1139         /* parse the command arguements */
1140         if (argc != 2)
1141         {
1142                 printf ("Usage: %s <driver>\n", argv[0]);
1143                 return NT_STATUS_OK;
1144         }
1145
1146         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
1147         strupper (servername);
1148
1149         /* delete the driver for all architectures */
1150         for (i=0; archi_table[i].long_archi; i++)
1151         {
1152                 /* make the call to remove the driver */
1153                 result = cli_spoolss_deleteprinterdriver(cli, mem_ctx, 
1154                                                          archi_table[i].long_archi, argv[1]);
1155                 if (!NT_STATUS_IS_OK(result)) {
1156                         printf ("Failed to remove driver %s for arch [%s] - error %s!\n", 
1157                                 argv[1], archi_table[i].long_archi, get_nt_error_msg(result));
1158                 }
1159                 else
1160                         printf ("Driver %s removed for arch [%s].\n", argv[1], archi_table[i].long_archi);
1161         }
1162                 
1163         return NT_STATUS_OK;            
1164 }
1165
1166 static NTSTATUS cmd_spoolss_getprintprocdir(struct cli_state *cli, 
1167                                             TALLOC_CTX *mem_ctx,
1168                                             int argc, char **argv)
1169 {
1170         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1171         char *servername = NULL, *environment = NULL;
1172         fstring procdir;
1173         
1174         /* parse the command arguements */
1175         if (argc < 2 || argc > 3) {
1176                 printf ("Usage: %s <server> [environment]\n", argv[0]);
1177                 return NT_STATUS_OK;
1178         }
1179
1180         asprintf(&servername, "\\\\%s", cli->desthost);
1181         strupper(servername);
1182
1183         asprintf(&environment, "%s", (argc == 3) ? argv[2] : 
1184                  PRINTER_DRIVER_ARCHITECTURE);
1185
1186         result = cli_spoolss_getprintprocessordirectory(
1187                 cli, mem_ctx, servername, environment, procdir);
1188
1189         if (NT_STATUS_IS_OK(result))
1190                 printf("%s", procdir);
1191
1192         SAFE_FREE(servername);
1193         SAFE_FREE(environment);
1194
1195         return result;
1196 }
1197
1198 /* List of commands exported by this module */
1199 struct cmd_set spoolss_commands[] = {
1200
1201         { "SPOOLSS"  },
1202
1203         { "adddriver",          cmd_spoolss_addprinterdriver,   PIPE_SPOOLSS, "Add a print driver",                  "" },
1204         { "addprinter",         cmd_spoolss_addprinterex,       PIPE_SPOOLSS, "Add a printer",                       "" },
1205         { "deldriver",          cmd_spoolss_deletedriver,       PIPE_SPOOLSS, "Delete a printer driver",             "" },
1206         { "enumdata",           cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Enumerate printer data (*)",          "" },
1207         { "enumjobs",           cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Enumerate print jobs (*)",            "" },
1208         { "enumports",          cmd_spoolss_enum_ports,         PIPE_SPOOLSS, "Enumerate printer ports",             "" },
1209         { "enumdrivers",        cmd_spoolss_enum_drivers,       PIPE_SPOOLSS, "Enumerate installed printer drivers", "" },
1210         { "enumprinters",       cmd_spoolss_enum_printers,      PIPE_SPOOLSS, "Enumerate printers",                  "" },
1211         { "getdata",            cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Get print driver data (*)",           "" },
1212         { "getdriver",          cmd_spoolss_getdriver,          PIPE_SPOOLSS, "Get print driver information",        "" },
1213         { "getdriverdir",       cmd_spoolss_getdriverdir,       PIPE_SPOOLSS, "Get print driver upload directory",   "" },
1214         { "getprinter",         cmd_spoolss_getprinter,         PIPE_SPOOLSS, "Get printer info",                    "" },
1215         { "openprinter",        cmd_spoolss_open_printer_ex,    PIPE_SPOOLSS, "Open printer handle",                 "" },
1216         { "setdriver",          cmd_spoolss_setdriver,          PIPE_SPOOLSS, "Set printer driver",                  "" },
1217         { "getprintprocdir",    cmd_spoolss_getprintprocdir, PIPE_SPOOLSS, "Get print processor directory",          "" },
1218
1219         { NULL }
1220 };