first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[ira/wip.git] / source3 / rpcclient / cmd_spoolss.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NT Domain Authentication SMB / MSRPC client
5    Copyright (C) Andrew Tridgell 1994-1999
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23
24 #ifdef SYSLOG
25 #undef SYSLOG
26 #endif
27
28 #include "includes.h"
29 #include "nterr.h"
30
31 extern int DEBUGLEVEL;
32
33 #define DEBUG_TESTING
34
35 extern FILE* out_hnd;
36
37 extern struct ntuser_creds *usr_creds;
38
39 /****************************************************************************
40 nt spoolss query
41 ****************************************************************************/
42 BOOL msrpc_spoolss_enum_printers( const char* srv_name,
43                                 uint32 level,
44                                 uint32 *num,
45                                 void ***ctr,
46                                 PRINT_INFO_FN(fn))
47 {
48         BOOL res = True;
49
50         if (spoolss_enum_printers( 0x40, srv_name, level, num, ctr) &&
51             fn != NULL)
52         {
53                 fn(srv_name, level, *num, *ctr);
54         }
55
56         return res;
57 }
58
59 static void spool_print_info_ctr(const char* srv_name, uint32 level,
60                                 uint32 num, void *const *const ctr)
61 {
62         display_printer_info_ctr(out_hnd, ACTION_HEADER   , level, num, ctr);
63         display_printer_info_ctr(out_hnd, ACTION_ENUMERATE, level, num, ctr);
64         display_printer_info_ctr(out_hnd, ACTION_FOOTER   , level, num, ctr);
65 }
66
67 /****************************************************************************
68 nt spoolss query
69 ****************************************************************************/
70 void cmd_spoolss_enum_printers(struct client_info *info, int argc, char *argv[])
71 {
72         void **ctr = NULL;
73         uint32 num = 0;
74         uint32 level = 1;
75
76         fstring srv_name;
77         fstrcpy(srv_name, "\\\\");
78         fstrcat(srv_name, info->dest_host);
79         strupper(srv_name);
80
81         if (msrpc_spoolss_enum_printers(srv_name, level, &num, &ctr,
82                                  spool_print_info_ctr))
83         {
84                 DEBUG(5,("cmd_spoolss_enum_printer: query succeeded\n"));
85         }
86         else
87         {
88                 report(out_hnd, "FAILED\n");
89         }
90
91         free_void_array(num, ctr, free);
92 }
93
94 /****************************************************************************
95 nt spoolss query
96 ****************************************************************************/
97 void cmd_spoolss_open_printer_ex(struct client_info *info, int argc, char *argv[])
98 {
99         fstring srv_name;
100         fstring station;
101         char *printer_name;
102         POLICY_HND hnd;
103
104         BOOL res = True;
105
106         if (argc < 2)
107         {
108                 report(out_hnd, "spoolopen <printer name>\n");
109                 return;
110         }
111
112         printer_name = argv[1];
113
114         fstrcpy(station, "\\\\");
115         fstrcat(station, info->myhostname);
116         strupper(station);
117
118         fstrcpy(srv_name, "\\\\");
119         fstrcat(srv_name, info->dest_host);
120         strupper(srv_name);
121
122         if (!strnequal("\\\\", printer_name, 2))
123         {
124                 fstrcat(srv_name, "\\");
125                 fstrcat(srv_name, printer_name);
126                 printer_name = srv_name;
127         }
128
129         DEBUG(4,("spoolopen - printer: %s server: %s user: %s\n",
130                 printer_name, station, usr_creds->user_name));
131
132         res = res ? spoolss_open_printer_ex( printer_name,
133                                 0, 0, 0,
134                                 station, usr_creds->user_name,
135                                 &hnd) : False;
136
137         res = res ? spoolss_closeprinter(&hnd) : False;
138
139         if (res)
140         {
141                 DEBUG(5,("cmd_spoolss_open_printer_ex: query succeeded\n"));
142                 report(out_hnd, "OK\n");
143         }
144         else
145         {
146                 DEBUG(5,("cmd_spoolss_open_printer_ex: query failed\n"));
147         }
148 }
149
150 /****************************************************************************
151 nt spoolss query
152 ****************************************************************************/
153 BOOL msrpc_spoolss_enum_jobs( const char* printer_name,
154                                 const char* station, const char* user_name, 
155                                 uint32 level,
156                                 uint32 *num,
157                                 void ***ctr,
158                                 JOB_INFO_FN(fn))
159 {
160         POLICY_HND hnd;
161         uint32 buf_size = 0x0;
162         uint32 status = 0x0;
163
164         BOOL res = True;
165         BOOL res1 = True;
166
167         DEBUG(4,("spoolopen - printer: %s server: %s user: %s\n",
168                 printer_name, station, user_name));
169
170         res = res ? spoolss_open_printer_ex( printer_name,
171                                 0, 0, 0,
172                                 station, user_name,
173                                 &hnd) : False;
174
175         if (status == 0x0)
176         {
177                 status = spoolss_enum_jobs( &hnd,
178                                 0, 1000, level, &buf_size,
179                                 num, ctr);
180         }
181
182         if (status == ERROR_INSUFFICIENT_BUFFER)
183         {
184                 status = spoolss_enum_jobs( &hnd,
185                                 0, 1000, level, &buf_size,
186                                 num, ctr);
187         }
188
189         res1 = (status == 0x0);
190
191         res = res ? spoolss_closeprinter(&hnd) : False;
192
193         if (res1 && fn != NULL)
194         {
195                 fn(printer_name, station, level, *num, *ctr);
196         }
197
198         return res1;
199 }
200
201 static void spool_job_info_ctr( const char* printer_name,
202                                 const char* station,
203                                 uint32 level,
204                                 uint32 num, void *const *const ctr)
205 {
206         display_job_info_ctr(out_hnd, ACTION_HEADER   , level, num, ctr);
207         display_job_info_ctr(out_hnd, ACTION_ENUMERATE, level, num, ctr);
208         display_job_info_ctr(out_hnd, ACTION_FOOTER   , level, num, ctr);
209 }
210
211 /****************************************************************************
212 nt spoolss query
213 ****************************************************************************/
214 void cmd_spoolss_enum_jobs(struct client_info *info, int argc, char *argv[])
215 {
216         fstring srv_name;
217         fstring station;
218         char *printer_name;
219
220         void **ctr = NULL;
221         uint32 num = 0;
222         uint32 level = 1;
223
224         if (argc < 2)
225         {
226                 report(out_hnd, "spoolenum <printer name>\n");
227                 return;
228         }
229
230         printer_name = argv[1];
231
232         fstrcpy(station, "\\\\");
233         fstrcat(station, info->myhostname);
234         strupper(station);
235
236         fstrcpy(srv_name, "\\\\");
237         fstrcat(srv_name, info->dest_host);
238         strupper(srv_name);
239
240         if (!strnequal("\\\\", printer_name, 2))
241         {
242                 fstrcat(srv_name, "\\");
243                 fstrcat(srv_name, printer_name);
244                 printer_name = srv_name;
245         }
246
247         DEBUG(4,("spoolopen - printer: %s station: %s user: %s\n",
248                 printer_name, station, usr_creds->user_name));
249
250         if (msrpc_spoolss_enum_jobs( printer_name, station,
251                                 usr_creds->user_name,
252                                 level, &num, &ctr,
253                                 spool_job_info_ctr))
254         {
255                 DEBUG(5,("cmd_spoolss_enum_jobs: query succeeded\n"));
256         }
257         else
258         {
259                 report(out_hnd, "FAILED\n");
260         }
261
262         free_void_array(num, ctr, free);
263 }
264