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