s3: Remove some pointless wrapper functions
[bbaumbach/samba-autobuild/.git] / source3 / libsmb / cliprint.c
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         unsigned int offset;
31
32         if (datap == 0) {
33                 /* turn NULL pointers into zero length strings */
34                 return "";
35         }
36
37         offset = datap - converter;
38
39         if (offset >= rdrcnt) {
40                 DEBUG(1,("bad char ptr: datap=%u, converter=%u rdrcnt=%d>",
41                          datap, converter, rdrcnt));
42                 return "<ERROR>";
43         }
44         return &rdata[offset];
45 }
46
47 /****************************************************************************
48 call fn() on each entry in a print queue
49 ****************************************************************************/
50
51 int cli_print_queue(struct cli_state *cli,
52                     void (*fn)(struct print_job_info *))
53 {
54         char *rparam = NULL;
55         char *rdata = NULL;
56         char *p;
57         unsigned int rdrcnt, rprcnt;
58         char param[1024];
59         int result_code=0;
60         int i = -1;
61
62         memset(param,'\0',sizeof(param));
63
64         p = param;
65         SSVAL(p,0,76);         /* API function number 76 (DosPrintJobEnum) */
66         p += 2;
67         safe_strcpy_base(p,"zWrLeh", param, sizeof(param));   /* parameter description? */
68         p = skip_string(param,sizeof(param),p);
69         safe_strcpy_base(p,"WWzWWDDzz", param, sizeof(param));  /* returned data format */
70         p = skip_string(param,sizeof(param),p);
71         safe_strcpy_base(p,cli->share, param, sizeof(param));    /* name of queue */
72         p = skip_string(param,sizeof(param),p);
73         SSVAL(p,0,2);   /* API function level 2, PRJINFO_2 data structure */
74         SSVAL(p,2,1000); /* size of bytes of returned data buffer */
75         p += 4;
76         safe_strcpy_base(p,"", param,sizeof(param));   /* subformat */
77         p = skip_string(param,sizeof(param),p);
78
79         DEBUG(4,("doing cli_print_queue for %s\n", cli->share));
80
81         if (cli_api(cli,
82                     param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
83                     NULL, 0, CLI_BUFFER_SIZE,            /* data, length, maxlen */
84                     &rparam, &rprcnt,                /* return params, length */
85                     &rdata, &rdrcnt)) {               /* return data, length */
86                 int converter;
87                 result_code = SVAL(rparam,0);
88                 converter = SVAL(rparam,2);       /* conversion factor */
89
90                 if (result_code == 0) {
91                         struct print_job_info job;
92
93                         p = rdata;
94
95                         for (i = 0; i < SVAL(rparam,4); ++i) {
96                                 job.id = SVAL(p,0);
97                                 job.priority = SVAL(p,2);
98                                 fstrcpy(job.user,
99                                         fix_char_ptr(SVAL(p,4), converter,
100                                                      rdata, rdrcnt));
101                                 job.t = make_unix_date3(
102                                         p + 12, cli->serverzone);
103                                 job.size = IVAL(p,16);
104                                 fstrcpy(job.name,fix_char_ptr(SVAL(p,24),
105                                                               converter,
106                                                               rdata, rdrcnt));
107                                 fn(&job);
108                                 p += 28;
109                         }
110                 }
111         }
112
113         /* If any parameters or data were returned, free the storage. */
114         SAFE_FREE(rparam);
115         SAFE_FREE(rdata);
116
117         return i;
118 }
119
120 /****************************************************************************
121   cancel a print job
122   ****************************************************************************/
123
124 int cli_printjob_del(struct cli_state *cli, int job)
125 {
126         char *rparam = NULL;
127         char *rdata = NULL;
128         char *p;
129         unsigned int rdrcnt,rprcnt;
130         int ret = -1;
131         char param[1024];
132
133         memset(param,'\0',sizeof(param));
134
135         p = param;
136         SSVAL(p,0,81);          /* DosPrintJobDel() */
137         p += 2;
138         safe_strcpy_base(p,"W", param,sizeof(param));
139         p = skip_string(param,sizeof(param),p);
140         safe_strcpy_base(p,"", param,sizeof(param));
141         p = skip_string(param,sizeof(param),p);
142         SSVAL(p,0,job);
143         p += 2;
144
145         if (cli_api(cli,
146                     param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
147                     NULL, 0, CLI_BUFFER_SIZE,            /* data, length, maxlen */
148                     &rparam, &rprcnt,                /* return params, length */
149                     &rdata, &rdrcnt)) {               /* return data, length */
150                 ret = SVAL(rparam,0);
151         }
152
153         SAFE_FREE(rparam);
154         SAFE_FREE(rdata);
155
156         return ret;
157 }
158
159
160 /****************************************************************************
161  Open a spool file
162 ****************************************************************************/
163
164 int cli_spl_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
165 {
166         char *p;
167         unsigned openfn=0;
168         unsigned accessmode=0;
169
170         if (flags & O_CREAT)
171                 openfn |= (1<<4);
172         if (!(flags & O_EXCL)) {
173                 if (flags & O_TRUNC)
174                         openfn |= (1<<1);
175                 else
176                         openfn |= (1<<0);
177         }
178
179         accessmode = (share_mode<<4);
180
181         if ((flags & O_ACCMODE) == O_RDWR) {
182                 accessmode |= 2;
183         } else if ((flags & O_ACCMODE) == O_WRONLY) {
184                 accessmode |= 1;
185         }
186
187 #if defined(O_SYNC)
188         if ((flags & O_SYNC) == O_SYNC) {
189                 accessmode |= (1<<14);
190         }
191 #endif /* O_SYNC */
192
193         if (share_mode == DENY_FCB) {
194                 accessmode = 0xFF;
195         }
196
197         memset(cli->outbuf,'\0',smb_size);
198         memset(cli->inbuf,'\0',smb_size);
199
200         cli_set_message(cli->outbuf,15,0,True);
201
202         SCVAL(cli->outbuf,smb_com,SMBsplopen);
203         SSVAL(cli->outbuf,smb_tid,cli->cnum);
204         cli_setup_packet(cli);
205
206         SSVAL(cli->outbuf,smb_vwv0,0xFF);
207         SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
208         SSVAL(cli->outbuf,smb_vwv3,accessmode);
209         SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
210         SSVAL(cli->outbuf,smb_vwv5,0);
211         SSVAL(cli->outbuf,smb_vwv8,openfn);
212
213         if (cli->use_oplocks) {
214                 /* if using oplocks then ask for a batch oplock via
215                    core and extended methods */
216                 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
217                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
218                 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
219         }
220
221         p = smb_buf(cli->outbuf);
222         p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
223
224         cli_setup_bcc(cli, p);
225
226         cli_send_smb(cli);
227         if (!cli_receive_smb(cli)) {
228                 return -1;
229         }
230
231         if (cli_is_error(cli)) {
232                 return -1;
233         }
234
235         return SVAL(cli->inbuf,smb_vwv2);
236 }
237
238 /****************************************************************************
239  Close a file.
240 ****************************************************************************/
241
242 bool cli_spl_close(struct cli_state *cli, uint16_t fnum)
243 {
244         memset(cli->outbuf,'\0',smb_size);
245         memset(cli->inbuf,'\0',smb_size);
246
247         cli_set_message(cli->outbuf,3,0,True);
248
249         SCVAL(cli->outbuf,smb_com,SMBsplclose);
250         SSVAL(cli->outbuf,smb_tid,cli->cnum);
251         cli_setup_packet(cli);
252
253         SSVAL(cli->outbuf,smb_vwv0,fnum);
254         SIVALS(cli->outbuf,smb_vwv1,-1);
255
256         cli_send_smb(cli);
257         if (!cli_receive_smb(cli)) {
258                 return False;
259         }
260
261         return !cli_is_error(cli);
262 }