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 | *****************************************************************************/
|
---|
27 | static 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 | /****************************************************************************
|
---|
47 | call fn() on each entry in a print queue
|
---|
48 | ****************************************************************************/
|
---|
49 |
|
---|
50 | int 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 | /****************************************************************************
|
---|
120 | the same as cli_print_queue, but pass a state on each fn call
|
---|
121 | ***************************************************************************/
|
---|
122 | int cli_print_queue_state(struct cli_state *cli,
|
---|
123 | void (*fn)(struct print_job_info *, void *), void * state)
|
---|
124 | {
|
---|
125 | char *rparam = NULL;
|
---|
126 | char *rdata = NULL;
|
---|
127 | char *p;
|
---|
128 | unsigned int rdrcnt, rprcnt;
|
---|
129 | char param[1024];
|
---|
130 | int result_code=0;
|
---|
131 | int i = -1;
|
---|
132 |
|
---|
133 | memset(param,'\0',sizeof(param));
|
---|
134 |
|
---|
135 | p = param;
|
---|
136 | SSVAL(p,0,76); /* API function number 76 (DosPrintJobEnum) */
|
---|
137 | p += 2;
|
---|
138 | safe_strcpy_base(p,"zWrLeh", param, sizeof(param)); /* parameter description? */
|
---|
139 | p = skip_string(param,sizeof(param),p);
|
---|
140 | safe_strcpy_base(p,"WWzWWDDzz", param, sizeof(param)); /* returned data format */
|
---|
141 | p = skip_string(param,sizeof(param),p);
|
---|
142 | safe_strcpy_base(p,cli->share, param, sizeof(param)); /* name of queue */
|
---|
143 | p = skip_string(param,sizeof(param),p);
|
---|
144 | SSVAL(p,0,2); /* API function level 2, PRJINFO_2 data structure */
|
---|
145 | SSVAL(p,2,1000); /* size of bytes of returned data buffer */
|
---|
146 | p += 4;
|
---|
147 | safe_strcpy_base(p,"", param,sizeof(param)); /* subformat */
|
---|
148 | p = skip_string(param,sizeof(param),p);
|
---|
149 |
|
---|
150 | DEBUG(4,("doing cli_print_queue_state for %s\n", cli->share));
|
---|
151 |
|
---|
152 | if (cli_api(cli,
|
---|
153 | param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
|
---|
154 | NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
|
---|
155 | &rparam, &rprcnt, /* return params, length */
|
---|
156 | &rdata, &rdrcnt)) { /* return data, length */
|
---|
157 | int converter;
|
---|
158 | result_code = SVAL(rparam,0);
|
---|
159 | converter = SVAL(rparam,2); /* conversion factor */
|
---|
160 |
|
---|
161 | if (result_code == 0) {
|
---|
162 | struct print_job_info job;
|
---|
163 |
|
---|
164 | p = rdata;
|
---|
165 |
|
---|
166 | for (i = 0; i < SVAL(rparam,4); ++i) {
|
---|
167 | job.id = SVAL(p,0);
|
---|
168 | job.priority = SVAL(p,2);
|
---|
169 | fstrcpy(job.user,
|
---|
170 | fix_char_ptr(SVAL(p,4), converter,
|
---|
171 | rdata, rdrcnt));
|
---|
172 | job.t = srv_make_unix_date3(p + 12);
|
---|
173 | job.size = IVAL(p,16);
|
---|
174 | fstrcpy(job.name,fix_char_ptr(SVAL(p,24),
|
---|
175 | converter,
|
---|
176 | rdata, rdrcnt));
|
---|
177 | fn(&job, state);
|
---|
178 | p += 28;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /* If any parameters or data were returned, free the storage. */
|
---|
184 | SAFE_FREE(rparam);
|
---|
185 | SAFE_FREE(rdata);
|
---|
186 |
|
---|
187 | return i;
|
---|
188 | }
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | /****************************************************************************
|
---|
192 | cancel a print job
|
---|
193 | ****************************************************************************/
|
---|
194 |
|
---|
195 | int cli_printjob_del(struct cli_state *cli, int job)
|
---|
196 | {
|
---|
197 | char *rparam = NULL;
|
---|
198 | char *rdata = NULL;
|
---|
199 | char *p;
|
---|
200 | unsigned int rdrcnt,rprcnt;
|
---|
201 | int ret = -1;
|
---|
202 | char param[1024];
|
---|
203 |
|
---|
204 | memset(param,'\0',sizeof(param));
|
---|
205 |
|
---|
206 | p = param;
|
---|
207 | SSVAL(p,0,81); /* DosPrintJobDel() */
|
---|
208 | p += 2;
|
---|
209 | safe_strcpy_base(p,"W", param,sizeof(param));
|
---|
210 | p = skip_string(param,sizeof(param),p);
|
---|
211 | safe_strcpy_base(p,"", param,sizeof(param));
|
---|
212 | p = skip_string(param,sizeof(param),p);
|
---|
213 | SSVAL(p,0,job);
|
---|
214 | p += 2;
|
---|
215 |
|
---|
216 | if (cli_api(cli,
|
---|
217 | param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
|
---|
218 | NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
|
---|
219 | &rparam, &rprcnt, /* return params, length */
|
---|
220 | &rdata, &rdrcnt)) { /* return data, length */
|
---|
221 | ret = SVAL(rparam,0);
|
---|
222 | }
|
---|
223 |
|
---|
224 | SAFE_FREE(rparam);
|
---|
225 | SAFE_FREE(rdata);
|
---|
226 |
|
---|
227 | return ret;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /****************************************************************************
|
---|
232 | Open a spool file
|
---|
233 | ****************************************************************************/
|
---|
234 |
|
---|
235 | int cli_spl_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
|
---|
236 | {
|
---|
237 | char *p;
|
---|
238 | unsigned openfn=0;
|
---|
239 | unsigned accessmode=0;
|
---|
240 |
|
---|
241 | if (flags & O_CREAT)
|
---|
242 | openfn |= (1<<4);
|
---|
243 | if (!(flags & O_EXCL)) {
|
---|
244 | if (flags & O_TRUNC)
|
---|
245 | openfn |= (1<<1);
|
---|
246 | else
|
---|
247 | openfn |= (1<<0);
|
---|
248 | }
|
---|
249 |
|
---|
250 | accessmode = (share_mode<<4);
|
---|
251 |
|
---|
252 | if ((flags & O_ACCMODE) == O_RDWR) {
|
---|
253 | accessmode |= 2;
|
---|
254 | } else if ((flags & O_ACCMODE) == O_WRONLY) {
|
---|
255 | accessmode |= 1;
|
---|
256 | }
|
---|
257 |
|
---|
258 | #if defined(O_SYNC)
|
---|
259 | if ((flags & O_SYNC) == O_SYNC) {
|
---|
260 | accessmode |= (1<<14);
|
---|
261 | }
|
---|
262 | #endif /* O_SYNC */
|
---|
263 |
|
---|
264 | if (share_mode == DENY_FCB) {
|
---|
265 | accessmode = 0xFF;
|
---|
266 | }
|
---|
267 |
|
---|
268 | memset(cli->outbuf,'\0',smb_size);
|
---|
269 | memset(cli->inbuf,'\0',smb_size);
|
---|
270 |
|
---|
271 | cli_set_message(cli->outbuf,15,0,True);
|
---|
272 |
|
---|
273 | SCVAL(cli->outbuf,smb_com,SMBsplopen);
|
---|
274 | SSVAL(cli->outbuf,smb_tid,cli->cnum);
|
---|
275 | cli_setup_packet(cli);
|
---|
276 |
|
---|
277 | SSVAL(cli->outbuf,smb_vwv0,0xFF);
|
---|
278 | SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
|
---|
279 | SSVAL(cli->outbuf,smb_vwv3,accessmode);
|
---|
280 | SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
|
---|
281 | SSVAL(cli->outbuf,smb_vwv5,0);
|
---|
282 | SSVAL(cli->outbuf,smb_vwv8,openfn);
|
---|
283 |
|
---|
284 | if (cli->use_oplocks) {
|
---|
285 | /* if using oplocks then ask for a batch oplock via
|
---|
286 | core and extended methods */
|
---|
287 | SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
|
---|
288 | FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
|
---|
289 | SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
|
---|
290 | }
|
---|
291 |
|
---|
292 | p = smb_buf(cli->outbuf);
|
---|
293 | p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
|
---|
294 |
|
---|
295 | cli_setup_bcc(cli, p);
|
---|
296 |
|
---|
297 | cli_send_smb(cli);
|
---|
298 | if (!cli_receive_smb(cli)) {
|
---|
299 | return -1;
|
---|
300 | }
|
---|
301 |
|
---|
302 | if (cli_is_error(cli)) {
|
---|
303 | return -1;
|
---|
304 | }
|
---|
305 |
|
---|
306 | return SVAL(cli->inbuf,smb_vwv2);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /****************************************************************************
|
---|
310 | Close a file.
|
---|
311 | ****************************************************************************/
|
---|
312 |
|
---|
313 | bool cli_spl_close(struct cli_state *cli, int fnum)
|
---|
314 | {
|
---|
315 | memset(cli->outbuf,'\0',smb_size);
|
---|
316 | memset(cli->inbuf,'\0',smb_size);
|
---|
317 |
|
---|
318 | cli_set_message(cli->outbuf,3,0,True);
|
---|
319 |
|
---|
320 | SCVAL(cli->outbuf,smb_com,SMBsplclose);
|
---|
321 | SSVAL(cli->outbuf,smb_tid,cli->cnum);
|
---|
322 | cli_setup_packet(cli);
|
---|
323 |
|
---|
324 | SSVAL(cli->outbuf,smb_vwv0,fnum);
|
---|
325 | SIVALS(cli->outbuf,smb_vwv1,-1);
|
---|
326 |
|
---|
327 | cli_send_smb(cli);
|
---|
328 | if (!cli_receive_smb(cli)) {
|
---|
329 | return False;
|
---|
330 | }
|
---|
331 |
|
---|
332 | return !cli_is_error(cli);
|
---|
333 | }
|
---|