[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / libsmb / cliprint.c
blobcb04e0ddcc70fab49156eaaf5745e4801d77cdbd
1 /*
2 Unix SMB/CIFS implementation.
3 client print routines
4 Copyright (C) Andrew Tridgell 1994-1998
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 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 /*****************************************************************************
24 Convert a character pointer in a cli_call_api() response to a form we can use.
25 This function contains code to prevent core dumps if the server returns
26 invalid data.
27 *****************************************************************************/
28 static const char *fix_char_ptr(unsigned int datap, unsigned int converter,
29 char *rdata, int rdrcnt)
31 if (datap == 0) { /* turn NULL pointers into zero length strings */
32 return "";
33 } else {
34 unsigned int offset = datap - converter;
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];
47 /****************************************************************************
48 call fn() on each entry in a print queue
49 ****************************************************************************/
50 int cli_print_queue(struct cli_state *cli,
51 void (*fn)(struct print_job_info *))
53 char *rparam = NULL;
54 char *rdata = NULL;
55 char *p;
56 unsigned int rdrcnt, rprcnt;
57 pstring param;
58 int result_code=0;
59 int i = -1;
61 memset(param,'\0',sizeof(param));
63 p = param;
64 SSVAL(p,0,76); /* API function number 76 (DosPrintJobEnum) */
65 p += 2;
66 pstrcpy_base(p,"zWrLeh", param); /* parameter description? */
67 p = skip_string(param,sizeof(param),p);
68 pstrcpy_base(p,"WWzWWDDzz", param); /* returned data format */
69 p = skip_string(param,sizeof(param),p);
70 pstrcpy_base(p,cli->share, 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 pstrcpy_base(p,"", param); /* subformat */
76 p = skip_string(param,sizeof(param),p);
78 DEBUG(4,("doing cli_print_queue for %s\n", cli->share));
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 */
89 if (result_code == 0) {
90 struct print_job_info job;
92 p = rdata;
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;
111 /* If any parameters or data were returned, free the storage. */
112 SAFE_FREE(rparam);
113 SAFE_FREE(rdata);
115 return i;
118 /****************************************************************************
119 cancel a print job
120 ****************************************************************************/
121 int cli_printjob_del(struct cli_state *cli, int job)
123 char *rparam = NULL;
124 char *rdata = NULL;
125 char *p;
126 unsigned int rdrcnt,rprcnt;
127 int ret = -1;
128 pstring param;
130 memset(param,'\0',sizeof(param));
132 p = param;
133 SSVAL(p,0,81); /* DosPrintJobDel() */
134 p += 2;
135 pstrcpy_base(p,"W", param);
136 p = skip_string(param,sizeof(param),p);
137 pstrcpy_base(p,"", param);
138 p = skip_string(param,sizeof(param),p);
139 SSVAL(p,0,job);
140 p += 2;
142 if (cli_api(cli,
143 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
144 NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
145 &rparam, &rprcnt, /* return params, length */
146 &rdata, &rdrcnt)) { /* return data, length */
147 ret = SVAL(rparam,0);
150 SAFE_FREE(rparam);
151 SAFE_FREE(rdata);
153 return ret;
157 /****************************************************************************
158 Open a spool file
159 ****************************************************************************/
161 int cli_spl_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
163 char *p;
164 unsigned openfn=0;
165 unsigned accessmode=0;
167 if (flags & O_CREAT)
168 openfn |= (1<<4);
169 if (!(flags & O_EXCL)) {
170 if (flags & O_TRUNC)
171 openfn |= (1<<1);
172 else
173 openfn |= (1<<0);
176 accessmode = (share_mode<<4);
178 if ((flags & O_ACCMODE) == O_RDWR) {
179 accessmode |= 2;
180 } else if ((flags & O_ACCMODE) == O_WRONLY) {
181 accessmode |= 1;
184 #if defined(O_SYNC)
185 if ((flags & O_SYNC) == O_SYNC) {
186 accessmode |= (1<<14);
188 #endif /* O_SYNC */
190 if (share_mode == DENY_FCB) {
191 accessmode = 0xFF;
194 memset(cli->outbuf,'\0',smb_size);
195 memset(cli->inbuf,'\0',smb_size);
197 set_message(cli->outbuf,15,0,True);
199 SCVAL(cli->outbuf,smb_com,SMBsplopen);
200 SSVAL(cli->outbuf,smb_tid,cli->cnum);
201 cli_setup_packet(cli);
203 SSVAL(cli->outbuf,smb_vwv0,0xFF);
204 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
205 SSVAL(cli->outbuf,smb_vwv3,accessmode);
206 SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
207 SSVAL(cli->outbuf,smb_vwv5,0);
208 SSVAL(cli->outbuf,smb_vwv8,openfn);
210 if (cli->use_oplocks) {
211 /* if using oplocks then ask for a batch oplock via
212 core and extended methods */
213 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
214 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
215 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
218 p = smb_buf(cli->outbuf);
219 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
221 cli_setup_bcc(cli, p);
223 cli_send_smb(cli);
224 if (!cli_receive_smb(cli)) {
225 return -1;
228 if (cli_is_error(cli)) {
229 return -1;
232 return SVAL(cli->inbuf,smb_vwv2);
235 /****************************************************************************
236 Close a file.
237 ****************************************************************************/
239 BOOL cli_spl_close(struct cli_state *cli, int fnum)
241 memset(cli->outbuf,'\0',smb_size);
242 memset(cli->inbuf,'\0',smb_size);
244 set_message(cli->outbuf,3,0,True);
246 SCVAL(cli->outbuf,smb_com,SMBsplclose);
247 SSVAL(cli->outbuf,smb_tid,cli->cnum);
248 cli_setup_packet(cli);
250 SSVAL(cli->outbuf,smb_vwv0,fnum);
251 SIVALS(cli->outbuf,smb_vwv1,-1);
253 cli_send_smb(cli);
254 if (!cli_receive_smb(cli)) {
255 return False;
258 return !cli_is_error(cli);