smbXcli: add the possiblilty to negotiate client capabilites in smb >= 2.2
[ira/wip.git] / source3 / libsmb / clioplock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client oplock functions
4    Copyright (C) Andrew Tridgell 2001
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 #include "../lib/util/tevent_ntstatus.h"
22 #include "async_smb.h"
23 #include "libsmb/libsmb.h"
24
25 struct cli_smb_oplock_break_waiter_state {
26         uint16_t fnum;
27         uint8_t level;
28 };
29
30 static void cli_smb_oplock_break_waiter_done(struct tevent_req *subreq);
31
32 struct tevent_req *cli_smb_oplock_break_waiter_send(TALLOC_CTX *mem_ctx,
33                                                     struct event_context *ev,
34                                                     struct cli_state *cli)
35 {
36         struct tevent_req *req, *subreq;
37         struct cli_smb_oplock_break_waiter_state *state;
38
39         req = tevent_req_create(mem_ctx, &state,
40                                 struct cli_smb_oplock_break_waiter_state);
41         if (req == NULL) {
42                 return NULL;
43         }
44
45         /*
46          * Create a fake SMB request that we will never send out. This is only
47          * used to be set into the pending queue with the right mid.
48          */
49         subreq = cli_smb_req_create(mem_ctx, ev, cli, 0, 0, 0, NULL, 0, NULL);
50         if (tevent_req_nomem(subreq, req)) {
51                 return tevent_req_post(req, ev);
52         }
53         cli_smb_req_set_mid(subreq, 0xffff);
54
55         if (!cli_smb_req_set_pending(subreq)) {
56                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
57                 return tevent_req_post(req, ev);
58         }
59         tevent_req_set_callback(subreq, cli_smb_oplock_break_waiter_done, req);
60         return req;
61 }
62
63 static void cli_smb_oplock_break_waiter_done(struct tevent_req *subreq)
64 {
65         struct tevent_req *req = tevent_req_callback_data(
66                 subreq, struct tevent_req);
67         struct cli_smb_oplock_break_waiter_state *state = tevent_req_data(
68                 req, struct cli_smb_oplock_break_waiter_state);
69         uint8_t wct;
70         uint16_t *vwv;
71         uint32_t num_bytes;
72         uint8_t *bytes;
73         uint8_t *inbuf;
74         NTSTATUS status;
75
76         status = cli_smb_recv(subreq, state, &inbuf, 8, &wct, &vwv,
77                               &num_bytes, &bytes);
78         TALLOC_FREE(subreq);
79         if (!NT_STATUS_IS_OK(status)) {
80                 tevent_req_nterror(req, status);
81                 return;
82         }
83         state->fnum = SVAL(vwv+2, 0);
84         state->level = CVAL(vwv+3, 1);
85         tevent_req_done(req);
86 }
87
88 NTSTATUS cli_smb_oplock_break_waiter_recv(struct tevent_req *req,
89                                           uint16_t *pfnum,
90                                           uint8_t *plevel)
91 {
92         struct cli_smb_oplock_break_waiter_state *state = tevent_req_data(
93                 req, struct cli_smb_oplock_break_waiter_state);
94         NTSTATUS status;
95
96         if (tevent_req_is_nterror(req, &status)) {
97                 return status;
98         }
99         *pfnum = state->fnum;
100         *plevel = state->level;
101         return NT_STATUS_OK;
102 }
103
104 /****************************************************************************
105 send an ack for an oplock break request
106 ****************************************************************************/
107
108 struct cli_oplock_ack_state {
109         uint16_t vwv[8];
110 };
111
112 static void cli_oplock_ack_done(struct tevent_req *subreq);
113
114 struct tevent_req *cli_oplock_ack_send(TALLOC_CTX *mem_ctx,
115                                        struct tevent_context *ev,
116                                        struct cli_state *cli,
117                                        uint16_t fnum, uint8_t level)
118 {
119         struct tevent_req *req, *subreq;
120         struct cli_oplock_ack_state *state;
121
122         req = tevent_req_create(mem_ctx, &state, struct cli_oplock_ack_state);
123         if (req == NULL) {
124                 return NULL;
125         }
126         SCVAL(state->vwv+0, 0, 0xff);
127         SCVAL(state->vwv+0, 1, 0);
128         SSVAL(state->vwv+1, 0, 0);
129         SSVAL(state->vwv+2, 0, fnum);
130         SCVAL(state->vwv+3, 0, LOCKING_ANDX_OPLOCK_RELEASE);
131         SCVAL(state->vwv+3, 1, level);
132         SIVAL(state->vwv+4, 0, 0); /* timeout */
133         SSVAL(state->vwv+6, 0, 0); /* unlockcount */
134         SSVAL(state->vwv+7, 0, 0); /* lockcount */
135
136         subreq = cli_smb_send(state, ev, cli, SMBlockingX, 0, 8, state->vwv,
137                               0, NULL);
138         if (tevent_req_nomem(subreq, req)) {
139                 return tevent_req_post(req, ev);
140         }
141         tevent_req_set_callback(subreq, cli_oplock_ack_done, req);
142         return req;
143 }
144
145 static void cli_oplock_ack_done(struct tevent_req *subreq)
146 {
147         struct tevent_req *req = tevent_req_callback_data(
148                 subreq, struct tevent_req);
149         NTSTATUS status;
150
151         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
152         TALLOC_FREE(subreq);
153         if (!NT_STATUS_IS_OK(status)) {
154                 tevent_req_nterror(req, status);
155                 return;
156         }
157         tevent_req_done(req);
158 }
159
160 NTSTATUS cli_oplock_ack_recv(struct tevent_req *req)
161 {
162         return tevent_req_simple_recv_ntstatus(req);
163 }
164