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