s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_tcon.c
blob705f8fc99c7eb78557158a16f01df33d626c62cc
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
4 Copyright (C) Volker Lendecke 2011
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.
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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "client.h"
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
24 #include "smb2cli.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_tcon_state {
29 struct cli_state *cli;
30 uint8_t fixed[8];
33 static void smb2cli_tcon_done(struct tevent_req *subreq);
35 struct tevent_req *smb2cli_tcon_send(TALLOC_CTX *mem_ctx,
36 struct tevent_context *ev,
37 struct cli_state *cli,
38 const char *share)
40 struct tevent_req *req, *subreq;
41 struct smb2cli_tcon_state *state;
42 uint8_t *fixed;
43 char srv_ip[INET6_ADDRSTRLEN];
44 const char *tcon_share;
45 uint8_t *dyn;
46 size_t dyn_len;
48 req = tevent_req_create(mem_ctx, &state, struct smb2cli_tcon_state);
49 if (req == NULL) {
50 return NULL;
52 state->cli = cli;
54 print_sockaddr(srv_ip, sizeof(srv_ip), cli_state_remote_sockaddr(cli));
56 tcon_share = talloc_asprintf(talloc_tos(), "\\\\%s\\%s",
57 srv_ip, share);
58 if (tevent_req_nomem(tcon_share, req)) {
59 return tevent_req_post(req, ev);
61 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
62 tcon_share, talloc_get_size(tcon_share),
63 &dyn, &dyn_len)) {
64 tevent_req_oom(req);
65 return tevent_req_post(req, ev);
68 fixed = state->fixed;
69 SSVAL(fixed, 0, 9);
70 SSVAL(fixed, 4, SMB2_HDR_BODY + 8);
71 SSVAL(fixed, 6, dyn_len);
73 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_TCON, 0,
74 state->fixed, sizeof(state->fixed),
75 dyn, dyn_len);
76 if (tevent_req_nomem(subreq, req)) {
77 return tevent_req_post(req, ev);
79 tevent_req_set_callback(subreq, smb2cli_tcon_done, req);
80 return req;
83 static void smb2cli_tcon_done(struct tevent_req *subreq)
85 struct tevent_req *req = tevent_req_callback_data(
86 subreq, struct tevent_req);
87 struct smb2cli_tcon_state *state = tevent_req_data(
88 req, struct smb2cli_tcon_state);
89 struct cli_state *cli = state->cli;
90 NTSTATUS status;
91 struct iovec *iov;
92 uint8_t *body;
94 status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 16);
95 if (!NT_STATUS_IS_OK(status)) {
96 TALLOC_FREE(subreq);
97 tevent_req_nterror(req, status);
98 return;
101 cli->smb2.tid = IVAL(iov[0].iov_base, SMB2_HDR_TID);
103 body = (uint8_t *)iov[1].iov_base;
104 cli->smb2.share_type = CVAL(body, 2);
105 cli->smb2.share_flags = IVAL(body, 4);
106 cli->capabilities = IVAL(body, 8);
107 cli->smb2.maximal_access = IVAL(body, 12);
109 TALLOC_FREE(subreq);
110 tevent_req_done(req);
113 NTSTATUS smb2cli_tcon_recv(struct tevent_req *req)
115 return tevent_req_simple_recv_ntstatus(req);
118 NTSTATUS smb2cli_tcon(struct cli_state *cli, const char *share)
120 TALLOC_CTX *frame = talloc_stackframe();
121 struct event_context *ev;
122 struct tevent_req *req;
123 NTSTATUS status = NT_STATUS_NO_MEMORY;
125 if (cli_has_async_calls(cli)) {
127 * Can't use sync call while an async call is in flight
129 status = NT_STATUS_INVALID_PARAMETER;
130 goto fail;
132 ev = event_context_init(frame);
133 if (ev == NULL) {
134 goto fail;
136 req = smb2cli_tcon_send(frame, ev, cli, share);
137 if (req == NULL) {
138 goto fail;
140 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
141 goto fail;
143 status = smb2cli_tcon_recv(req);
144 fail:
145 TALLOC_FREE(frame);
146 return status;
149 struct smb2cli_tdis_state {
150 uint8_t fixed[4];
153 static void smb2cli_tdis_done(struct tevent_req *subreq);
155 struct tevent_req *smb2cli_tdis_send(TALLOC_CTX *mem_ctx,
156 struct tevent_context *ev,
157 struct cli_state *cli)
159 struct tevent_req *req, *subreq;
160 struct smb2cli_tdis_state *state;
162 req = tevent_req_create(mem_ctx, &state,
163 struct smb2cli_tdis_state);
164 if (req == NULL) {
165 return NULL;
167 SSVAL(state->fixed, 0, 4);
169 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_TDIS, 0,
170 state->fixed, sizeof(state->fixed),
171 NULL, 0);
172 if (tevent_req_nomem(subreq, req)) {
173 return tevent_req_post(req, ev);
175 tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
176 return req;
179 static void smb2cli_tdis_done(struct tevent_req *subreq)
181 struct tevent_req *req =
182 tevent_req_callback_data(subreq,
183 struct tevent_req);
184 NTSTATUS status;
185 struct iovec *iov;
187 status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 4);
188 TALLOC_FREE(subreq);
189 if (tevent_req_nterror(req, status)) {
190 return;
192 tevent_req_done(req);
195 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
197 return tevent_req_simple_recv_ntstatus(req);
200 NTSTATUS smb2cli_tdis(struct cli_state *cli)
202 TALLOC_CTX *frame = talloc_stackframe();
203 struct event_context *ev;
204 struct tevent_req *req;
205 NTSTATUS status = NT_STATUS_NO_MEMORY;
207 if (cli_has_async_calls(cli)) {
209 * Can't use sync call while an async call is in flight
211 status = NT_STATUS_INVALID_PARAMETER;
212 goto fail;
214 ev = event_context_init(frame);
215 if (ev == NULL) {
216 goto fail;
218 req = smb2cli_tdis_send(frame, ev, cli);
219 if (req == NULL) {
220 goto fail;
222 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
223 goto fail;
225 status = smb2cli_tdis_recv(req);
226 fail:
227 TALLOC_FREE(frame);
228 return status;