missed mem_grow_data call
[abartlet/samba.git/.git] / source3 / rpc_client / cli_spoolss.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1997,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
8  *  Copyright (C) Paul Ashton                       1997.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #ifdef SYSLOG
27 #undef SYSLOG
28 #endif
29
30 #include "includes.h"
31
32 extern int DEBUGLEVEL;
33
34
35 /****************************************************************************
36 do a SPOOLSS Enum Printers
37 ****************************************************************************/
38 BOOL spoolss_enum_printers(struct cli_state *cli, uint16 fnum,
39                         uint32 flags, const char *servername,
40                         uint32 level,
41                         uint32 *count,
42                         void ***printers)
43 {
44         prs_struct rbuf;
45         prs_struct buf; 
46         SPOOL_Q_ENUMPRINTERS q_o;
47         BOOL valid_pol = False;
48
49         if (count == NULL || printers == NULL) return False;
50
51         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
52         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
53
54         /* create and send a MSRPC command with api SPOOLSS_ENUM_PRINTERS */
55
56         DEBUG(5,("SPOOLSS Enum Printers (Server: %s level: %d)\n",
57                                 servername, level));
58
59         make_spoolss_q_enumprinters(&q_o, flags, servername, level, 0x800);
60
61         /* turn parameters into data stream */
62         spoolss_io_q_enumprinters("", &q_o, &buf, 0);
63
64         /* send the data on \PIPE\ */
65         if (rpc_api_pipe_req(cli, fnum, SPOOLSS_ENUMPRINTERS, &buf, &rbuf))
66         {
67                 SPOOL_R_ENUMPRINTERS r_o;
68                 BOOL p;
69
70                 ZERO_STRUCT(r_o);
71
72                 r_o.level = level;
73
74                 spoolss_io_r_enumprinters("", &r_o, &rbuf, 0);
75                 p = rbuf.offset != 0;
76
77                 if (p && r_o.status != 0)
78                 {
79                         /* report error code */
80                         DEBUG(5,("SPOOLSS_ENUM_PRINTERS: %s\n", get_nt_error_msg(r_o.status)));
81                         p = False;
82                 }
83
84                 if (p)
85                 {
86                         /* ok, at last: we're happy. return the policy handle */
87                         (*count) = r_o.returned;
88                         (*printers) = r_o.printer.info;
89                         valid_pol = True;
90                 }
91         }
92
93         prs_mem_free(&rbuf);
94         prs_mem_free(&buf );
95
96         return valid_pol;
97 }
98
99 /****************************************************************************
100 do a SPOOLSS Open Printer Ex
101 ****************************************************************************/
102 BOOL spoolss_open_printer_ex(struct cli_state *cli, uint16 fnum,
103                         char *printername,
104                         uint32 cbbuf, uint32 devmod, uint32 des_access,
105                         char *station,
106                         char *username,
107                         PRINTER_HND *hnd)
108 {
109         prs_struct rbuf;
110         prs_struct buf; 
111         SPOOL_Q_OPEN_PRINTER_EX q_o;
112         BOOL valid_pol = False;
113
114         if (hnd == NULL) return False;
115
116         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
117         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
118
119         /* create and send a MSRPC command with api SPOOLSS_OPENPRINTEREX */
120
121         DEBUG(5,("SPOOLSS Open Printer Ex\n"));
122
123         make_spoolss_q_open_printer_ex(&q_o, printername,
124                                        cbbuf, devmod, des_access,
125                                        station, username);
126
127         /* turn parameters into data stream */
128         spoolss_io_q_open_printer_ex("", &q_o, &buf, 0);
129
130         /* send the data on \PIPE\ */
131         if (rpc_api_pipe_req(cli, fnum, SPOOLSS_OPENPRINTEREX, &buf, &rbuf))
132         {
133                 SPOOL_R_OPEN_PRINTER_EX r_o;
134                 BOOL p;
135
136                 spoolss_io_r_open_printer_ex("", &r_o, &rbuf, 0);
137                 p = rbuf.offset != 0;
138
139                 if (p && r_o.status != 0)
140                 {
141                         /* report error code */
142                         DEBUG(5,("SPOOLSS_OPENPRINTEREX: %s\n", get_nt_error_msg(r_o.status)));
143                         p = False;
144                 }
145
146                 if (p)
147                 {
148                         /* ok, at last: we're happy. return the policy handle */
149                         memcpy(hnd, r_o.handle.data, sizeof(hnd->data));
150                         valid_pol = True;
151                 }
152         }
153
154         prs_mem_free(&rbuf);
155         prs_mem_free(&buf );
156
157         return valid_pol;
158 }
159
160 /****************************************************************************
161 do a SPOOL Close
162 ****************************************************************************/
163 BOOL spoolss_closeprinter(struct cli_state *cli, uint16 fnum, PRINTER_HND *hnd)
164 {
165         prs_struct rbuf;
166         prs_struct buf; 
167         SPOOL_Q_CLOSEPRINTER q_c;
168         BOOL valid_close = False;
169
170         if (hnd == NULL) return False;
171
172         /* create and send a MSRPC command with api SPOOLSS_CLOSEPRINTER */
173
174         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
175         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
176
177         DEBUG(4,("SPOOL Close Printer\n"));
178
179         /* store the parameters */
180         make_spoolss_q_closeprinter(&q_c, hnd);
181
182         /* turn parameters into data stream */
183         spoolss_io_q_closeprinter("", &q_c, &buf, 0);
184
185         /* send the data on \PIPE\ */
186         if (rpc_api_pipe_req(cli, fnum, SPOOLSS_CLOSEPRINTER, &buf, &rbuf))
187         {
188                 SPOOL_R_CLOSEPRINTER r_c;
189                 BOOL p;
190
191                 spoolss_io_r_closeprinter("", &r_c, &rbuf, 0);
192                 p = rbuf.offset != 0;
193
194                 if (p && r_c.status != 0)
195                 {
196                         /* report error code */
197                         DEBUG(0,("SPOOL_CLOSEPRINTER: %s\n", get_nt_error_msg(r_c.status)));
198                         p = False;
199                 }
200
201                 if (p)
202                 {
203                         /* check that the returned policy handle is all zeros */
204                         uint32 i;
205                         valid_close = True;
206
207                         for (i = 0; i < sizeof(r_c.handle.data); i++)
208                         {
209                                 if (r_c.handle.data[i] != 0)
210                                 {
211                                         valid_close = False;
212                                         break;
213                                 }
214                         }       
215                         if (!valid_close)
216                         {
217                                 DEBUG(0,("SPOOL_CLOSEPRINTER: non-zero handle returned\n"));
218                         }
219                 }
220         }
221
222         prs_mem_free(&rbuf);
223         prs_mem_free(&buf );
224
225         return valid_close;
226 }
227