summaryrefslogtreecommitdiff
path: root/source/rpc_client/cli_spoolss.c
blob: 8028001657a3b46480a32bee2511e80006cc5e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

/* 
 *  Unix SMB/Netbios implementation.
 *  Version 1.9.
 *  RPC Pipe client / server routines
 *  Copyright (C) Andrew Tridgell              1992-2000,
 *  Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
 *  Copyright (C) Paul Ashton                  1997-2000,
 *  Copyright (C) Jean Francois Micouleau      1998-2000,
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#ifdef SYSLOG
#undef SYSLOG
#endif

#include "includes.h"
#include "rpc_parse.h"
#include "nterr.h"

extern int DEBUGLEVEL;


/****************************************************************************
do a SPOOLSS Enum Printers
****************************************************************************/
uint32 spoolss_enum_printers(uint32 flags, fstring srv_name, uint32 level,
			     NEW_BUFFER *buffer, uint32 offered,
			     uint32 *needed, uint32 *returned)
{
	prs_struct rbuf;
	prs_struct buf; 
	SPOOL_Q_ENUMPRINTERS q_o;
	SPOOL_R_ENUMPRINTERS r_o;
	BOOL valid_pol = False;

	struct cli_connection *con = NULL;

	if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
		return False;

	prs_init(&buf , 0, 4, False);
	prs_init(&rbuf, 0, 4, True );

	/* create and send a MSRPC command with api SPOOLSS_ENUM_PRINTERS */

	DEBUG(5,("SPOOLSS Enum Printers (Server: %s level: %d)\n", srv_name, level));

	make_spoolss_q_enumprinters(&q_o, flags, "", level, buffer, offered);

	/* turn parameters into data stream */
	if (!spoolss_io_q_enumprinters("", &q_o, &buf, 0) ) {
		prs_free_data(&rbuf);
		prs_free_data(&buf );

		cli_connection_unlink(con);
	}
	
	if(!rpc_con_pipe_req(con, SPOOLSS_ENUMPRINTERS, &buf, &rbuf)) {
		prs_free_data(&rbuf);
		prs_free_data(&buf );

		cli_connection_unlink(con);
	}

	prs_free_data(&buf );
	ZERO_STRUCT(r_o);
	
	buffer->prs.io=UNMARSHALL;
	buffer->prs.offset=0;
	r_o.buffer=buffer;
	
	if(!new_spoolss_io_r_enumprinters("", &r_o, &rbuf, 0)) {
		prs_free_data(&rbuf);
		cli_connection_unlink(con);
	}
	
	*needed=r_o.needed;
	*returned=r_o.returned;
	
	prs_free_data(&rbuf);
	prs_free_data(&buf );

	cli_connection_unlink(con);

	return r_o.status;
}

/****************************************************************************
do a SPOOLSS Enum Jobs
****************************************************************************/
uint32 spoolss_enum_jobs(const POLICY_HND *hnd, uint32 firstjob, uint32 numofjobs,
			 uint32 level, NEW_BUFFER *buffer, uint32 offered, 
			 uint32 *needed, uint32 *returned)
{
	prs_struct rbuf;
	prs_struct buf; 
	SPOOL_Q_ENUMJOBS q_o;
	SPOOL_R_ENUMJOBS r_o;

	if (hnd == NULL)
		return NT_STATUS_INVALID_PARAMETER;

	prs_init(&buf , 0, 4, False);
	prs_init(&rbuf, 0, 4, True );

	/* create and send a MSRPC command with api SPOOLSS_ENUMJOBS */

	DEBUG(5,("SPOOLSS Enum Jobs level: %d)\n", level));

	make_spoolss_q_enumjobs(&q_o, hnd, firstjob, numofjobs, level, buffer, offered);

	/* turn parameters into data stream */
	if (!spoolss_io_q_enumjobs("", &q_o, &buf, 0)) {
		prs_free_data(&rbuf);
		prs_free_data(&buf );
	}
	
	if(!rpc_hnd_pipe_req(hnd, SPOOLSS_ENUMJOBS, &buf, &rbuf))
	{
		prs_free_data(&rbuf);
		prs_free_data(&buf );
	}
	
	ZERO_STRUCT(r_o);
	prs_free_data(&buf );

	r_o.buffer=buffer;
	
	if(!spoolss_io_r_enumjobs("", &r_o, &rbuf, 0)) {
		prs_free_data(&rbuf);
	}
	
	*needed=r_o.needed;
	*returned=r_o.returned;

	prs_free_data(&rbuf);
	prs_free_data(&buf );

	return r_o.status;
}

/****************************************************************************
do a SPOOLSS Enum printer datas
****************************************************************************/
uint32 spoolss_enum_printerdata(const POLICY_HND *hnd, uint32 index, 
			uint32 *valuelen, uint16 *value, uint32 *rvaluelen, 
			uint32 *type, 
			uint32 *datalen, uint8 *data, uint32 *rdatalen)
{
	prs_struct rbuf;
	prs_struct buf; 
	SPOOL_Q_ENUMPRINTERDATA q_o;
	SPOOL_R_ENUMPRINTERDATA r_o;

	if (hnd == NULL)
		return NT_STATUS_INVALID_PARAMETER;

	prs_init(&buf , 0, 4, False);
	prs_init(&rbuf, 0, 4, True );

	/* create and send a MSRPC command with api SPOOLSS_ENUMJOBS */

	DEBUG(5,("SPOOLSS Enum Printer data)\n"));

	make_spoolss_q_enumprinterdata(&q_o, hnd, index, *valuelen, *datalen);

	/* turn parameters into data stream */
	if (!spoolss_io_q_enumprinterdata("", &q_o, &buf, 0)) {
		prs_free_data(&rbuf);
		prs_free_data(&buf );
	}
	
	if(!rpc_hnd_pipe_req(hnd, SPOOLSS_ENUMJOBS, &buf, &rbuf)) {
		prs_free_data(&rbuf);
		prs_free_data(&buf );
	}
	
	ZERO_STRUCT(r_o);
	prs_free_data(&buf );

	r_o.data=data;
	r_o.value=value;

	if(!spoolss_io_r_enumprinterdata("", &r_o, &rbuf, 0)) {
		prs_free_data(&rbuf);
	}
	
	*valuelen=r_o.valuesize;
	*rvaluelen=r_o.realvaluesize;
	*type=r_o.type;
	*datalen=r_o.datasize;
	*rdatalen=r_o.realdatasize;

	prs_free_data(&rbuf);
	prs_free_data(&buf );

	return r_o.status;
}

/****************************************************************************
do a SPOOLSS Open Printer Ex
****************************************************************************/
BOOL spoolss_open_printer_ex(  char *printername,
			 char *datatype, uint32 access_required,
			 char *station,  char *username,
			POLICY_HND *hnd)
{
	prs_struct rbuf;
	prs_struct buf; 
	SPOOL_Q_OPEN_PRINTER_EX q_o;
	BOOL valid_pol = False;
	fstring srv_name;
	char *s;

	struct cli_connection *con = NULL;

	memset(srv_name, 0, sizeof(srv_name));
	fstrcpy(srv_name, printername);

	s = strchr(&srv_name[2], '\\');

	if (s != NULL)
		*s = 0;

	if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
		return False;

	if (hnd == NULL) return False;

	prs_init(&buf , 0, 4, False);
	prs_init(&rbuf, 0, 4, True );

	/* create and send a MSRPC command with api SPOOLSS_OPENPRINTEREX */

	DEBUG(5,("SPOOLSS Open Printer Ex\n"));

	make_spoolss_q_open_printer_ex(&q_o, printername, datatype,
	                               access_required, station, username);

	/* turn parameters into data stream */
	if (spoolss_io_q_open_printer_ex("", &q_o, &buf, 0) &&
	    rpc_con_pipe_req(con, SPOOLSS_OPENPRINTEREX, &buf, &rbuf))
	{
		SPOOL_R_OPEN_PRINTER_EX r_o;
		BOOL p;

		spoolss_io_r_open_printer_ex("", &r_o, &rbuf, 0);
		p = rbuf.offset != 0;

		if (p && r_o.status != 0)
		{
			/* report error code */
			DEBUG(5,("SPOOLSS_OPENPRINTEREX: %s\n", get_nt_error_msg(r_o.status)));
			p = False;
		}

		if (p)
		{
			/* ok, at last: we're happy. return the policy handle */
			*hnd = r_o.handle;

			valid_pol = register_policy_hnd(get_global_hnd_cache(),
			                                cli_con_sec_ctx(con),
			                                hnd, access_required) &&
			            set_policy_con(get_global_hnd_cache(),
				               hnd, con, 
			                       cli_connection_unlink);
		}
	}

	prs_free_data(&rbuf);
	prs_free_data(&buf );

	return valid_pol;
}

/****************************************************************************
do a SPOOL Close
****************************************************************************/
BOOL spoolss_closeprinter(POLICY_HND *hnd)
{
	prs_struct rbuf;
	prs_struct buf; 
	SPOOL_Q_CLOSEPRINTER q_c;
	BOOL valid_close = False;

	if (hnd == NULL) return False;

	/* create and send a MSRPC command with api SPOOLSS_CLOSEPRINTER */

	prs_init(&buf , 0, 4, False);
	prs_init(&rbuf, 0, 4, True );

	DEBUG(4,("SPOOL Close Printer\n"));

	/* store the parameters */
	make_spoolss_q_closeprinter(&q_c, hnd);

	/* turn parameters into data stream */
	if (spoolss_io_q_closeprinter("", &q_c, &buf, 0) &&
	    rpc_hnd_pipe_req(hnd, SPOOLSS_CLOSEPRINTER, &buf, &rbuf))
	{
		SPOOL_R_CLOSEPRINTER r_c;
		BOOL p;

		spoolss_io_r_closeprinter("", &r_c, &rbuf, 0);
		p = rbuf.offset != 0;

		if (p && r_c.status != 0)
		{
			/* report error code */
			DEBUG(0,("SPOOL_CLOSEPRINTER: %s\n", get_nt_error_msg(r_c.status)));
			p = False;
		}
	}

	prs_free_data(&rbuf);
	prs_free_data(&buf );

	close_policy_hnd(get_global_hnd_cache(), hnd);

	return valid_close;
}