source: branches/samba-3.5.x/source3/libsmb/cliprint.c@ 1075

Last change on this file since 1075 was 628, checked in by Silvan Scherrer, 14 years ago

Samba Server 3.5: added missing function for our sambaclient

File size: 9.5 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 client print routines
4 Copyright (C) Andrew Tridgell 1994-1998
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "includes.h"
21
22/*****************************************************************************
23 Convert a character pointer in a cli_call_api() response to a form we can use.
24 This function contains code to prevent core dumps if the server returns
25 invalid data.
26*****************************************************************************/
27static const char *fix_char_ptr(unsigned int datap, unsigned int converter,
28 char *rdata, int rdrcnt)
29{
30 if (datap == 0) {
31 /* turn NULL pointers into zero length strings */
32 return "";
33 } else {
34 unsigned int offset = datap - converter;
35
36 if (offset >= rdrcnt) {
37 DEBUG(1,("bad char ptr: datap=%u, converter=%u rdrcnt=%d>",
38 datap, converter, rdrcnt));
39 return "<ERROR>";
40 } else {
41 return &rdata[offset];
42 }
43 }
44}
45
46/****************************************************************************
47call fn() on each entry in a print queue
48****************************************************************************/
49
50int cli_print_queue(struct cli_state *cli,
51 void (*fn)(struct print_job_info *))
52{
53 char *rparam = NULL;
54 char *rdata = NULL;
55 char *p;
56 unsigned int rdrcnt, rprcnt;
57 char param[1024];
58 int result_code=0;
59 int i = -1;
60
61 memset(param,'\0',sizeof(param));
62
63 p = param;
64 SSVAL(p,0,76); /* API function number 76 (DosPrintJobEnum) */
65 p += 2;
66 safe_strcpy_base(p,"zWrLeh", param, sizeof(param)); /* parameter description? */
67 p = skip_string(param,sizeof(param),p);
68 safe_strcpy_base(p,"WWzWWDDzz", param, sizeof(param)); /* returned data format */
69 p = skip_string(param,sizeof(param),p);
70 safe_strcpy_base(p,cli->share, param, sizeof(param)); /* name of queue */
71 p = skip_string(param,sizeof(param),p);
72 SSVAL(p,0,2); /* API function level 2, PRJINFO_2 data structure */
73 SSVAL(p,2,1000); /* size of bytes of returned data buffer */
74 p += 4;
75 safe_strcpy_base(p,"", param,sizeof(param)); /* subformat */
76 p = skip_string(param,sizeof(param),p);
77
78 DEBUG(4,("doing cli_print_queue for %s\n", cli->share));
79
80 if (cli_api(cli,
81 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
82 NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
83 &rparam, &rprcnt, /* return params, length */
84 &rdata, &rdrcnt)) { /* return data, length */
85 int converter;
86 result_code = SVAL(rparam,0);
87 converter = SVAL(rparam,2); /* conversion factor */
88
89 if (result_code == 0) {
90 struct print_job_info job;
91
92 p = rdata;
93
94 for (i = 0; i < SVAL(rparam,4); ++i) {
95 job.id = SVAL(p,0);
96 job.priority = SVAL(p,2);
97 fstrcpy(job.user,
98 fix_char_ptr(SVAL(p,4), converter,
99 rdata, rdrcnt));
100 job.t = cli_make_unix_date3(cli, p + 12);
101 job.size = IVAL(p,16);
102 fstrcpy(job.name,fix_char_ptr(SVAL(p,24),
103 converter,
104 rdata, rdrcnt));
105 fn(&job);
106 p += 28;
107 }
108 }
109 }
110
111 /* If any parameters or data were returned, free the storage. */
112 SAFE_FREE(rparam);
113 SAFE_FREE(rdata);
114
115 return i;
116}
117
118#ifdef __OS2__
119// this is a exact copy of the above cli_print_queue, but we need to pass the
120// state also to the callback, as our client needs that!!
121
122/****************************************************************************
123call fn() on each entry in a print queue
124****************************************************************************/
125
126int cli_print_queue_state(struct cli_state *cli,
127 void (*fn)(struct print_job_info *, void *), void *state)
128{
129 char *rparam = NULL;
130 char *rdata = NULL;
131 char *p;
132 unsigned int rdrcnt, rprcnt;
133 char param[1024];
134 int result_code=0;
135 int i = -1;
136
137 memset(param,'\0',sizeof(param));
138
139 p = param;
140 SSVAL(p,0,76); /* API function number 76 (DosPrintJobEnum) */
141 p += 2;
142 safe_strcpy_base(p,"zWrLeh", param, sizeof(param)); /* parameter description? */
143 p = skip_string(param,sizeof(param),p);
144 safe_strcpy_base(p,"WWzWWDDzz", param, sizeof(param)); /* returned data format */
145 p = skip_string(param,sizeof(param),p);
146 safe_strcpy_base(p,cli->share, param, sizeof(param)); /* name of queue */
147 p = skip_string(param,sizeof(param),p);
148 SSVAL(p,0,2); /* API function level 2, PRJINFO_2 data structure */
149 SSVAL(p,2,1000); /* size of bytes of returned data buffer */
150 p += 4;
151 safe_strcpy_base(p,"", param,sizeof(param)); /* subformat */
152 p = skip_string(param,sizeof(param),p);
153
154 DEBUG(4,("doing cli_print_queue for %s\n", cli->share));
155
156 if (cli_api(cli,
157 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
158 NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
159 &rparam, &rprcnt, /* return params, length */
160 &rdata, &rdrcnt)) { /* return data, length */
161 int converter;
162 result_code = SVAL(rparam,0);
163 converter = SVAL(rparam,2); /* conversion factor */
164
165 if (result_code == 0) {
166 struct print_job_info job;
167
168 p = rdata;
169
170 for (i = 0; i < SVAL(rparam,4); ++i) {
171 job.id = SVAL(p,0);
172 job.priority = SVAL(p,2);
173 fstrcpy(job.user,
174 fix_char_ptr(SVAL(p,4), converter,
175 rdata, rdrcnt));
176 job.t = cli_make_unix_date3(cli, p + 12);
177 job.size = IVAL(p,16);
178 fstrcpy(job.name,fix_char_ptr(SVAL(p,24),
179 converter,
180 rdata, rdrcnt));
181 fn(&job, state);
182 p += 28;
183 }
184 }
185 }
186
187 /* If any parameters or data were returned, free the storage. */
188 SAFE_FREE(rparam);
189 SAFE_FREE(rdata);
190
191 return i;
192}
193#endif
194
195/****************************************************************************
196 cancel a print job
197 ****************************************************************************/
198
199int cli_printjob_del(struct cli_state *cli, int job)
200{
201 char *rparam = NULL;
202 char *rdata = NULL;
203 char *p;
204 unsigned int rdrcnt,rprcnt;
205 int ret = -1;
206 char param[1024];
207
208 memset(param,'\0',sizeof(param));
209
210 p = param;
211 SSVAL(p,0,81); /* DosPrintJobDel() */
212 p += 2;
213 safe_strcpy_base(p,"W", param,sizeof(param));
214 p = skip_string(param,sizeof(param),p);
215 safe_strcpy_base(p,"", param,sizeof(param));
216 p = skip_string(param,sizeof(param),p);
217 SSVAL(p,0,job);
218 p += 2;
219
220 if (cli_api(cli,
221 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
222 NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
223 &rparam, &rprcnt, /* return params, length */
224 &rdata, &rdrcnt)) { /* return data, length */
225 ret = SVAL(rparam,0);
226 }
227
228 SAFE_FREE(rparam);
229 SAFE_FREE(rdata);
230
231 return ret;
232}
233
234
235/****************************************************************************
236 Open a spool file
237****************************************************************************/
238
239int cli_spl_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
240{
241 char *p;
242 unsigned openfn=0;
243 unsigned accessmode=0;
244
245 if (flags & O_CREAT)
246 openfn |= (1<<4);
247 if (!(flags & O_EXCL)) {
248 if (flags & O_TRUNC)
249 openfn |= (1<<1);
250 else
251 openfn |= (1<<0);
252 }
253
254 accessmode = (share_mode<<4);
255
256 if ((flags & O_ACCMODE) == O_RDWR) {
257 accessmode |= 2;
258 } else if ((flags & O_ACCMODE) == O_WRONLY) {
259 accessmode |= 1;
260 }
261
262#if defined(O_SYNC)
263 if ((flags & O_SYNC) == O_SYNC) {
264 accessmode |= (1<<14);
265 }
266#endif /* O_SYNC */
267
268 if (share_mode == DENY_FCB) {
269 accessmode = 0xFF;
270 }
271
272 memset(cli->outbuf,'\0',smb_size);
273 memset(cli->inbuf,'\0',smb_size);
274
275 cli_set_message(cli->outbuf,15,0,True);
276
277 SCVAL(cli->outbuf,smb_com,SMBsplopen);
278 SSVAL(cli->outbuf,smb_tid,cli->cnum);
279 cli_setup_packet(cli);
280
281 SSVAL(cli->outbuf,smb_vwv0,0xFF);
282 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
283 SSVAL(cli->outbuf,smb_vwv3,accessmode);
284 SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
285 SSVAL(cli->outbuf,smb_vwv5,0);
286 SSVAL(cli->outbuf,smb_vwv8,openfn);
287
288 if (cli->use_oplocks) {
289 /* if using oplocks then ask for a batch oplock via
290 core and extended methods */
291 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
292 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
293 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
294 }
295
296 p = smb_buf(cli->outbuf);
297 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
298
299 cli_setup_bcc(cli, p);
300
301 cli_send_smb(cli);
302 if (!cli_receive_smb(cli)) {
303 return -1;
304 }
305
306 if (cli_is_error(cli)) {
307 return -1;
308 }
309
310 return SVAL(cli->inbuf,smb_vwv2);
311}
312
313/****************************************************************************
314 Close a file.
315****************************************************************************/
316
317bool cli_spl_close(struct cli_state *cli, uint16_t fnum)
318{
319 memset(cli->outbuf,'\0',smb_size);
320 memset(cli->inbuf,'\0',smb_size);
321
322 cli_set_message(cli->outbuf,3,0,True);
323
324 SCVAL(cli->outbuf,smb_com,SMBsplclose);
325 SSVAL(cli->outbuf,smb_tid,cli->cnum);
326 cli_setup_packet(cli);
327
328 SSVAL(cli->outbuf,smb_vwv0,fnum);
329 SIVALS(cli->outbuf,smb_vwv1,-1);
330
331 cli_send_smb(cli);
332 if (!cli_receive_smb(cli)) {
333 return False;
334 }
335
336 return !cli_is_error(cli);
337}
Note: See TracBrowser for help on using the repository browser.