reran autoconf
[Samba.git] / source / rpc_client / cli_spoolss_notify.c
bloba6ce3341b783aad46191ec3588ca26fa0e5eb3b6
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2000,
5 * Copyright (C) Jean Francois Micouleau 1998-2000,
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #if 0
24 #include "rpc_parse.h"
25 #include "nterr.h"
26 #endif
27 extern pstring global_myname;
29 struct msg_info_table {
30 uint32 msg;
31 uint32 field;
32 const char *name;
33 void (*construct_fn) (int snum, SPOOL_NOTIFY_INFO_DATA *data,
34 print_queue_struct *queue,
35 NT_PRINTER_INFO_LEVEL *printer, TALLOC_CTX *mem_ctx);
38 struct msg_info_table msg_table[] = {
39 { PRINTER_MESSAGE_DRIVER, PRINTER_NOTIFY_DRIVER_NAME, "PRINTER_MESSAGE_DRIVER", spoolss_notify_driver_name },
40 { PRINTER_MESSAGE_ATTRIBUTES, PRINTER_NOTIFY_ATTRIBUTES, "PRINTER_MESSAGE_ATTRIBUTES", spoolss_notify_attributes },
41 { PRINTER_MESSAGE_COMMENT, PRINTER_NOTIFY_COMMENT, "PRINTER_MESSAGE_COMMENT", spoolss_notify_comment },
42 { PRINTER_MESSAGE_LOCATION, PRINTER_NOTIFY_LOCATION, "PRINTER_MESSAGE_LOCATION", spoolss_notify_location },
43 { PRINTER_MESSAGE_PRINTERNAME, PRINTER_NOTIFY_PRINTER_NAME, "PRINTER_MESSAGE_PRINTERNAME", spoolss_notify_printer_name },
44 { PRINTER_MESSAGE_SHARENAME, PRINTER_NOTIFY_SHARE_NAME, "PRINTER_MESSAGE_SHARENAME", spoolss_notify_share_name },
45 { PRINTER_MESSAGE_PORT, PRINTER_NOTIFY_PORT_NAME, "PRINTER_MESSAGE_PORT", spoolss_notify_port_name },
46 { PRINTER_MESSAGE_CJOBS, PRINTER_NOTIFY_CJOBS, "PRINTER_MESSAGE_CJOBS", spoolss_notify_cjobs },
47 { PRINTER_MESSAGE_SEPFILE, PRINTER_NOTIFY_SEPFILE, "PRINTER_MESSAGE_SEPFILE", spoolss_notify_sepfile },
48 { PRINTER_MESSAGE_PARAMS, PRINTER_NOTIFY_PARAMETERS, "PRINTER_MESSAGE_PARAMETERS", spoolss_notify_parameters },
49 { PRINTER_MESSAGE_DATATYPE, PRINTER_NOTIFY_DATATYPE, "PRINTER_MESSAGE_DATATYPE", spoolss_notify_datatype },
50 { PRINTER_MESSAGE_NULL, 0x0, "", NULL },
53 /*********************************************************
54 Disconnect from the client machine.
55 **********************************************************/
56 BOOL spoolss_disconnect_from_client( struct cli_state *cli)
58 cli_nt_session_close(cli);
59 cli_ulogoff(cli);
60 cli_shutdown(cli);
62 return True;
66 /*********************************************************
67 Connect to the client machine.
68 **********************************************************/
70 BOOL spoolss_connect_to_client( struct cli_state *cli, char *remote_machine)
72 ZERO_STRUCTP(cli);
73 if(cli_initialise(cli) == NULL) {
74 DEBUG(0,("connect_to_client: unable to initialize client connection.\n"));
75 return False;
78 if(!resolve_name( remote_machine, &cli->dest_ip, 0x20)) {
79 DEBUG(0,("connect_to_client: Can't resolve address for %s\n", remote_machine));
80 cli_shutdown(cli);
81 return False;
84 if (ismyip(cli->dest_ip)) {
85 DEBUG(0,("connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
86 cli_shutdown(cli);
87 return False;
90 if (!cli_connect(cli, remote_machine, &cli->dest_ip)) {
91 DEBUG(0,("connect_to_client: unable to connect to SMB server on machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
92 cli_shutdown(cli);
93 return False;
96 if (!attempt_netbios_session_request(cli, global_myname, remote_machine, &cli->dest_ip)) {
97 DEBUG(0,("connect_to_client: machine %s rejected the NetBIOS session request.\n",
98 remote_machine));
99 cli_shutdown(cli);
100 return False;
103 cli->protocol = PROTOCOL_NT1;
105 if (!cli_negprot(cli)) {
106 DEBUG(0,("connect_to_client: machine %s rejected the negotiate protocol. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
107 cli_shutdown(cli);
108 return False;
111 if (cli->protocol != PROTOCOL_NT1) {
112 DEBUG(0,("connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
113 cli_shutdown(cli);
114 return False;
118 * Do an anonymous session setup.
121 if (!cli_session_setup(cli, "", "", 0, "", 0, "")) {
122 DEBUG(0,("connect_to_client: machine %s rejected the session setup. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
123 cli_shutdown(cli);
124 return False;
127 if (!(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
128 DEBUG(0,("connect_to_client: machine %s isn't in user level security mode\n", remote_machine));
129 cli_shutdown(cli);
130 return False;
133 if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
134 DEBUG(0,("connect_to_client: machine %s rejected the tconX on the IPC$ share. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
135 cli_shutdown(cli);
136 return False;
140 * Ok - we have an anonymous connection to the IPC$ share.
141 * Now start the NT Domain stuff :-).
144 if(cli_nt_session_open(cli, PIPE_SPOOLSS) == False) {
145 DEBUG(0,("connect_to_client: unable to open the domain client session to machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli)));
146 cli_nt_session_close(cli);
147 cli_ulogoff(cli);
148 cli_shutdown(cli);
149 return False;
152 return True;
156 * SPOOLSS Client RPC's used by servers as the notification
157 * back channel
160 /***************************************************************************
161 do a reply open printer
162 ****************************************************************************/
164 WERROR cli_spoolss_reply_open_printer(struct cli_state *cli, TALLOC_CTX *mem_ctx,
165 char *printer, uint32 localprinter, uint32 type,
166 POLICY_HND *handle)
168 WERROR result = W_ERROR(ERRgeneral);
170 prs_struct rbuf;
171 prs_struct buf;
173 SPOOL_Q_REPLYOPENPRINTER q_s;
174 SPOOL_R_REPLYOPENPRINTER r_s;
176 prs_init(&buf, 1024, mem_ctx, MARSHALL);
177 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL );
179 /* create and send a MSRPC command with api SPOOLSS_REPLYOPENPRINTER */
181 /* store the parameters */
182 make_spoolss_q_replyopenprinter(&q_s, printer, localprinter, type);
184 /* turn parameters into data stream */
185 if(!spoolss_io_q_replyopenprinter("", &q_s, &buf, 0)) {
186 DEBUG(0,("cli_spoolss_reply_open_printer: Error : failed to marshall SPOOL_Q_REPLYOPENPRINTER struct.\n"));
187 goto done;
190 /* send the data on \PIPE\ */
191 if (!rpc_api_pipe_req(cli, SPOOLSS_REPLYOPENPRINTER, &buf, &rbuf))
192 goto done;
194 /* turn data stream into parameters*/
195 if(!spoolss_io_r_replyopenprinter("", &r_s, &rbuf, 0)) {
196 DEBUG(0,("cli_spoolss_reply_open_printer: Error : failed to unmarshall SPOOL_R_REPLYOPENPRINTER struct.\n"));
197 goto done;
200 memcpy(handle, &r_s.handle, sizeof(r_s.handle));
201 result = r_s.status;
203 done:
204 prs_mem_free(&buf);
205 prs_mem_free(&rbuf);
207 return result;
210 /***************************************************************************
211 do a reply open printer
212 ****************************************************************************/
214 WERROR cli_spoolss_reply_close_printer(struct cli_state *cli, TALLOC_CTX *mem_ctx,
215 POLICY_HND *handle)
217 WERROR result = W_ERROR(ERRgeneral);
218 prs_struct rbuf;
219 prs_struct buf;
221 SPOOL_Q_REPLYCLOSEPRINTER q_s;
222 SPOOL_R_REPLYCLOSEPRINTER r_s;
224 prs_init(&buf, 1024, cli->mem_ctx, MARSHALL);
225 prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL );
227 /* create and send a MSRPC command with api */
229 /* store the parameters */
230 make_spoolss_q_reply_closeprinter(&q_s, handle);
232 /* turn parameters into data stream */
233 if(!spoolss_io_q_replycloseprinter("", &q_s, &buf, 0)) {
234 DEBUG(0,("cli_spoolss_reply_close_printer: Error : failed to marshall SPOOL_Q_REPLY_CLOSEPRINTER struct.\n"));
235 goto done;
238 /* send the data on \PIPE\ */
239 if (!rpc_api_pipe_req(cli, SPOOLSS_REPLYCLOSEPRINTER, &buf, &rbuf))
240 goto done;
242 /* turn data stream into parameters*/
243 if(!spoolss_io_r_replycloseprinter("", &r_s, &rbuf, 0)) {
244 DEBUG(0,("cli_spoolss_reply_close_printer: Error : failed to marshall SPOOL_R_REPLY_CLOSEPRINTER struct.\n"));
245 goto done;
249 result = r_s.status;
251 done:
252 prs_mem_free(&buf);
253 prs_mem_free(&rbuf);
255 return result;
259 /*********************************************************************
260 This SPOOLSS_ROUTERREPLYPRINTER function is used to send a change
261 notification event when the registration **did not** use
262 SPOOL_NOTIFY_OPTION_TYPE structure to specify the events to monitor.
263 Also see cli_spolss_reply_rrpcn()
264 *********************************************************************/
266 WERROR cli_spoolss_routerreplyprinter (struct cli_state *cli, TALLOC_CTX *mem_ctx,
267 POLICY_HND *pol, uint32 condition, uint32 changd_id)
269 prs_struct qbuf, rbuf;
270 SPOOL_Q_ROUTERREPLYPRINTER q;
271 SPOOL_R_ROUTERREPLYPRINTER r;
272 WERROR result = W_ERROR(ERRgeneral);
274 ZERO_STRUCT(q);
275 ZERO_STRUCT(r);
278 /* Initialise input parameters */
280 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
281 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
284 /* write the request */
285 make_spoolss_q_routerreplyprinter(&q, pol, condition, changd_id);
287 /* Marshall data and send request */
288 if (!spoolss_io_q_routerreplyprinter ("", &q, &qbuf, 0)) {
289 DEBUG(0,("cli_spoolss_routerreplyprinter: Unable to marshall SPOOL_Q_ROUTERREPLYPRINTER!\n"));
290 goto done;
294 if (!rpc_api_pipe_req (cli, SPOOLSS_ROUTERREPLYPRINTER, &qbuf, &rbuf))
295 goto done;
297 /* Unmarshall response */
298 if (!spoolss_io_r_routerreplyprinter ("", &r, &rbuf, 0)) {
299 DEBUG(0,("cli_spoolss_routerreplyprinter: Unable to unmarshall SPOOL_R_ROUTERREPLYPRINTER!\n"));
300 goto done;
303 /* Return output parameters */
304 result = r.status;
306 done:
307 prs_mem_free(&qbuf);
308 prs_mem_free(&rbuf);
310 return result;
314 /**********************************************************************************
315 Build the SPOOL_NOTIFY_INFO_DATA entries based upon the flags which have been set
316 *********************************************************************************/
318 static int build_notify_data (TALLOC_CTX *ctx, NT_PRINTER_INFO_LEVEL *printer, uint32 flags,
319 SPOOL_NOTIFY_INFO_DATA **notify_data)
321 SPOOL_NOTIFY_INFO_DATA *data;
322 uint32 idx = 0;
323 int i = 0;
325 while ((msg_table[i].msg != PRINTER_MESSAGE_NULL) && flags)
327 if (flags & msg_table[i].msg)
329 DEBUG(10,("build_notify_data: %s set on [%s][%d]\n", msg_table[i].name,
330 printer->info_2->printername, idx));
331 if ((data=Realloc(*notify_data, (idx+1)*sizeof(SPOOL_NOTIFY_INFO_DATA))) == NULL) {
332 DEBUG(0,("build_notify_data: Realloc() failed with size [%d]!\n",
333 (idx+1)*sizeof(SPOOL_NOTIFY_INFO_DATA)));
334 return -1;
336 *notify_data = data;
338 /* clear memory */
339 memset(*notify_data+idx, 0x0, sizeof(SPOOL_NOTIFY_INFO_DATA));
342 * 'id' (last param here) is undefined when type == PRINTER_NOTIFY_TYPE
343 * See PRINTER_NOTIFY_INFO_DATA entries in MSDN
344 * --jerry
346 construct_info_data(*notify_data+idx, PRINTER_NOTIFY_TYPE, msg_table[i].field, 0x00);
348 msg_table[i].construct_fn(-1, *notify_data+idx, NULL, printer, ctx);
349 idx++;
352 i++;
355 return idx;
358 /*********************************************************************
359 This SPOOLSS_ROUTERREPLYPRINTER function is used to send a change
360 notification event when the registration **did** use
361 SPOOL_NOTIFY_OPTION_TYPE structure to specify the events to monitor
362 Also see cli_spoolss_routereplyprinter()
363 *********************************************************************/
365 WERROR cli_spoolss_reply_rrpcn(struct cli_state *cli, TALLOC_CTX *mem_ctx,
366 POLICY_HND *handle, PRINTER_MESSAGE_INFO *info,
367 NT_PRINTER_INFO_LEVEL *printer)
369 prs_struct rbuf;
370 prs_struct buf;
372 SPOOL_NOTIFY_INFO notify_info;
373 SPOOL_NOTIFY_INFO_DATA *notify_data = NULL;
374 uint32 data_len;
376 WERROR result = W_ERROR(ERRgeneral);
378 SPOOL_Q_REPLY_RRPCN q_s;
379 SPOOL_R_REPLY_RRPCN r_s;
381 if (!info) {
382 DEBUG(5,("cli_spoolss_reply_rrpcn: NULL printer message info pointer!\n"));
383 goto done;
386 prs_init(&buf, 1024, mem_ctx, MARSHALL);
387 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL );
389 ZERO_STRUCT(notify_info);
392 * See comments in _spoolss_setprinter() about PRINTER_CHANGE_XXX
393 * events. --jerry
395 DEBUG(10,("cli_spoolss_reply_rrpcn: PRINTER_MESSAGE flags = 0x%8x\n", info->flags));
397 data_len = build_notify_data(mem_ctx, printer, info->flags, &notify_data);
398 if (info->flags && (data_len == -1)) {
399 DEBUG(0,("cli_spoolss_reply_rrpcn: Failed to build SPOOL_NOTIFY_INFO_DATA [flags == 0x%x] for printer [%s]\n",
400 info->flags, info->printer_name));
401 result = WERR_NOMEM;
402 goto done;
404 notify_info.version = 0x2;
405 notify_info.flags = 0x00020000; /* ?? */
406 notify_info.count = data_len;
407 notify_info.data = notify_data;
409 /* create and send a MSRPC command with api */
410 /* store the parameters */
412 make_spoolss_q_reply_rrpcn(&q_s, handle, info->low, info->high, &notify_info);
414 /* turn parameters into data stream */
415 if(!spoolss_io_q_reply_rrpcn("", &q_s, &buf, 0)) {
416 DEBUG(0,("cli_spoolss_reply_rrpcn: Error : failed to marshall SPOOL_Q_REPLY_RRPCN struct.\n"));
417 goto done;
420 /* send the data on \PIPE\ */
421 if (!rpc_api_pipe_req(cli, SPOOLSS_RRPCN, &buf, &rbuf))
422 goto done;
425 /* turn data stream into parameters*/
426 if(!spoolss_io_r_reply_rrpcn("", &r_s, &rbuf, 0)) {
427 DEBUG(0,("cli_spoolss_reply_rrpcn: Error : failed to unmarshall SPOOL_R_REPLY_RRPCN struct.\n"));
428 goto done;
431 if (r_s.unknown0 == 0x00080000) {
432 DEBUG(8,("cli_spoolss_reply_rrpcn: I think the spooler resonded that the notification was ignored.\n"));
435 result = r_s.status;
437 done:
438 prs_mem_free(&buf);
439 prs_mem_free(&rbuf);
441 * The memory allocated in this array is talloc'd so we only need
442 * free the array here. JRA.
444 SAFE_FREE(notify_data);
446 return result;