merge from 2.2
[ira/wip.git] / source3 / rpc_client / cli_spoolss_notify.c
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,
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "includes.h"
23 #include "rpc_parse.h"
24 #include "nterr.h"
25
26 extern pstring global_myname;
27
28 /*********************************************************
29  Disconnect from the client machine.
30 **********************************************************/
31 BOOL spoolss_disconnect_from_client( struct cli_state *cli)
32 {
33         cli_nt_session_close(cli);
34         cli_ulogoff(cli);
35         cli_shutdown(cli);
36
37         return True;
38 }
39
40
41 /*********************************************************
42  Connect to the client machine.
43 **********************************************************/
44
45 BOOL spoolss_connect_to_client( struct cli_state *cli, char *remote_machine)
46 {
47         ZERO_STRUCTP(cli);
48         if(cli_initialise(cli) == NULL) {
49                 DEBUG(0,("connect_to_client: unable to initialize client connection.\n"));
50                 return False;
51         }
52
53         if(!resolve_name( remote_machine, &cli->dest_ip, 0x20)) {
54                 DEBUG(0,("connect_to_client: Can't resolve address for %s\n", remote_machine));
55                 cli_shutdown(cli);
56         return False;
57         }
58
59         if (ismyip(cli->dest_ip)) {
60                 DEBUG(0,("connect_to_client: Machine %s is one of our addresses. Cannot add to ourselves.\n", remote_machine));
61                 cli_shutdown(cli);
62                 return False;
63         }
64
65         if (!cli_connect(cli, remote_machine, &cli->dest_ip)) {
66                 DEBUG(0,("connect_to_client: unable to connect to SMB server on machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
67                 cli_shutdown(cli);
68                 return False;
69         }
70   
71         if (!attempt_netbios_session_request(cli, global_myname, remote_machine, &cli->dest_ip)) {
72                 DEBUG(0,("connect_to_client: machine %s rejected the NetBIOS session request.\n", 
73                         remote_machine));
74                 return False;
75         }
76
77         cli->protocol = PROTOCOL_NT1;
78     
79         if (!cli_negprot(cli)) {
80                 DEBUG(0,("connect_to_client: machine %s rejected the negotiate protocol. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
81                 cli_shutdown(cli);
82                 return False;
83         }
84
85         if (cli->protocol != PROTOCOL_NT1) {
86                 DEBUG(0,("connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
87                 cli_shutdown(cli);
88                 return False;
89         }
90     
91         /*
92          * Do an anonymous session setup.
93          */
94     
95         if (!cli_session_setup(cli, "", "", 0, "", 0, "")) {
96                 DEBUG(0,("connect_to_client: machine %s rejected the session setup. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
97                 cli_shutdown(cli);
98                 return False;
99         }
100     
101         if (!(cli->sec_mode & 1)) {
102                 DEBUG(0,("connect_to_client: machine %s isn't in user level security mode\n", remote_machine));
103                 cli_shutdown(cli);
104                 return False;
105         }
106     
107         if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
108                 DEBUG(0,("connect_to_client: machine %s rejected the tconX on the IPC$ share. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
109                 cli_shutdown(cli);
110                 return False;
111         }
112
113         /*
114          * Ok - we have an anonymous connection to the IPC$ share.
115          * Now start the NT Domain stuff :-).
116          */
117
118         if(cli_nt_session_open(cli, PIPE_SPOOLSS) == False) {
119                 DEBUG(0,("connect_to_client: unable to open the domain client session to machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli)));
120                 cli_nt_session_close(cli);
121                 cli_ulogoff(cli);
122                 cli_shutdown(cli);
123                 return False;
124         } 
125
126         return True;
127 }
128
129 /***************************************************************************
130  do a reply open printer
131 ****************************************************************************/
132
133 BOOL cli_spoolss_reply_open_printer(struct cli_state *cli, char *printer, uint32 localprinter, uint32 type, WERROR *status, POLICY_HND *handle)
134 {
135         prs_struct rbuf;
136         prs_struct buf; 
137
138         SPOOL_Q_REPLYOPENPRINTER q_s;
139         SPOOL_R_REPLYOPENPRINTER r_s;
140
141         prs_init(&buf, 1024, cli->mem_ctx, MARSHALL);
142         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL );
143
144         /* create and send a MSRPC command with api SPOOLSS_REPLYOPENPRINTER */
145 /*
146         DEBUG(4,("cli_spoolss_reply_open_printer: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
147            cli->srv_name_slash, cli->mach_acct, sec_chan_type, global_myname,
148            credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
149 */
150         /* store the parameters */
151         make_spoolss_q_replyopenprinter(&q_s, printer, localprinter, type);
152
153         /* turn parameters into data stream */
154         if(!spoolss_io_q_replyopenprinter("", &q_s,  &buf, 0)) {
155                 DEBUG(0,("cli_spoolss_reply_open_printer: Error : failed to marshall NET_Q_SRV_PWSET struct.\n"));
156                 prs_mem_free(&buf);
157                 prs_mem_free(&rbuf);
158                 return False;
159         }
160
161         /* send the data on \PIPE\ */
162         if (!rpc_api_pipe_req(cli, SPOOLSS_REPLYOPENPRINTER, &buf, &rbuf)) {
163                 prs_mem_free(&buf);
164                 prs_mem_free(&rbuf);
165                 return False;
166         }
167
168         prs_mem_free(&buf);
169         
170         /* turn data stream into parameters*/
171         if(!spoolss_io_r_replyopenprinter("", &r_s, &rbuf, 0)) {
172                 prs_mem_free(&rbuf);
173                 return False;
174         }
175         
176         prs_mem_free(&rbuf);
177
178         memcpy(handle, &r_s.handle, sizeof(r_s.handle));
179         *status=r_s.status;
180
181         return True;
182 }
183
184 /***************************************************************************
185  do a reply open printer
186 ****************************************************************************/
187
188 BOOL cli_spoolss_reply_rrpcn(struct cli_state *cli, POLICY_HND *handle, 
189                              uint32 change_low, uint32 change_high, WERROR *status)
190 {
191         prs_struct rbuf;
192         prs_struct buf; 
193
194         SPOOL_Q_REPLY_RRPCN q_s;
195         SPOOL_R_REPLY_RRPCN r_s;
196
197         prs_init(&buf, 1024, cli->mem_ctx, MARSHALL);
198         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL );
199
200         /* create and send a MSRPC command with api  */
201 /*
202         DEBUG(4,("cli_spoolss_reply_open_printer: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
203            cli->srv_name_slash, cli->mach_acct, sec_chan_type, global_myname,
204            credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
205 */
206         /* store the parameters */
207         make_spoolss_q_reply_rrpcn(&q_s, handle, change_low, change_high);
208
209         /* turn parameters into data stream */
210         if(!spoolss_io_q_reply_rrpcn("", &q_s,  &buf, 0)) {
211                 DEBUG(0,("cli_spoolss_reply_rrpcn: Error : failed to marshall SPOOL_Q_REPLY_RRPCN struct.\n"));
212                 prs_mem_free(&buf);
213                 prs_mem_free(&rbuf);
214                 return False;
215         }
216
217         /* send the data on \PIPE\ */
218         if (!rpc_api_pipe_req(cli, SPOOLSS_RRPCN, &buf, &rbuf)) {
219                 prs_mem_free(&buf);
220                 prs_mem_free(&rbuf);
221                 return False;
222         }
223
224         prs_mem_free(&buf);
225         
226         /* turn data stream into parameters*/
227         if(!spoolss_io_r_reply_rrpcn("", &r_s, &rbuf, 0)) {
228                 prs_mem_free(&rbuf);
229                 return False;
230         }
231         
232         prs_mem_free(&rbuf);
233
234         *status=r_s.status;
235
236         return True;
237 }
238
239 /***************************************************************************
240  do a reply open printer
241 ****************************************************************************/
242
243 BOOL cli_spoolss_reply_close_printer(struct cli_state *cli, POLICY_HND *handle, 
244                                      WERROR *status)
245 {
246         prs_struct rbuf;
247         prs_struct buf; 
248
249         SPOOL_Q_REPLYCLOSEPRINTER q_s;
250         SPOOL_R_REPLYCLOSEPRINTER r_s;
251
252         prs_init(&buf, 1024, cli->mem_ctx, MARSHALL);
253         prs_init(&rbuf, 0, cli->mem_ctx, UNMARSHALL );
254
255         /* create and send a MSRPC command with api  */
256 /*
257         DEBUG(4,("cli_spoolss_reply_open_printer: srv:%s acct:%s sc: %d mc: %s clnt %s %x\n",
258            cli->srv_name_slash, cli->mach_acct, sec_chan_type, global_myname,
259            credstr(new_clnt_cred.challenge.data), new_clnt_cred.timestamp.time));
260 */
261         /* store the parameters */
262         make_spoolss_q_reply_closeprinter(&q_s, handle);
263
264         /* turn parameters into data stream */
265         if(!spoolss_io_q_replycloseprinter("", &q_s,  &buf, 0)) {
266                 DEBUG(0,("cli_spoolss_reply_close_printer: Error : failed to marshall SPOOL_Q_REPLY_CLOSEPRINTER struct.\n"));
267                 prs_mem_free(&buf);
268                 prs_mem_free(&rbuf);
269                 return False;
270         }
271
272         /* send the data on \PIPE\ */
273         if (!rpc_api_pipe_req(cli, SPOOLSS_REPLYCLOSEPRINTER, &buf, &rbuf)) {
274                 prs_mem_free(&buf);
275                 prs_mem_free(&rbuf);
276                 return False;
277         }
278
279         prs_mem_free(&buf);
280         
281         /* turn data stream into parameters*/
282         if(!spoolss_io_r_replycloseprinter("", &r_s, &rbuf, 0)) {
283                 prs_mem_free(&rbuf);
284                 return False;
285         }
286         
287         prs_mem_free(&rbuf);
288
289         *status=r_s.status;
290
291         return True;
292 }