s4:librpc: remove recv_data from transport
[Samba.git] / source4 / librpc / rpc / dcerpc_smb.c
blob0224fdfb194596cbb885002943d99f0a196317d2
1 /*
2 Unix SMB/CIFS implementation.
4 dcerpc over SMB transport
6 Copyright (C) Tim Potter 2003
7 Copyright (C) Andrew Tridgell 2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include <tevent.h>
26 #include "lib/tsocket/tsocket.h"
27 #include "libcli/smb/smb_constants.h"
28 #include "libcli/smb/smbXcli_base.h"
29 #include "libcli/smb/tstream_smbXcli_np.h"
30 #include "libcli/raw/libcliraw.h"
31 #include "libcli/smb2/smb2.h"
32 #include "librpc/rpc/dcerpc.h"
33 #include "librpc/rpc/dcerpc_proto.h"
34 #include "libcli/composite/composite.h"
36 /* transport private information used by SMB pipe transport */
37 struct smb_private {
38 DATA_BLOB session_key;
41 * these are needed to open a secondary connection
43 struct smbXcli_conn *conn;
44 struct smbXcli_session *session;
45 struct smbXcli_tcon *tcon;
46 uint32_t timeout_msec;
50 fetch the user session key
52 static NTSTATUS smb_session_key(struct dcecli_connection *c, DATA_BLOB *session_key)
54 struct smb_private *smb = talloc_get_type_abort(
55 c->transport.private_data, struct smb_private);
57 if (smb == NULL) return NT_STATUS_CONNECTION_DISCONNECTED;
59 if (smb->session_key.length == 0) {
60 return NT_STATUS_NO_USER_SESSION_KEY;
63 *session_key = smb->session_key;
64 return NT_STATUS_OK;
67 struct dcerpc_pipe_open_smb_state {
68 struct dcecli_connection *c;
69 struct composite_context *ctx;
71 const char *fname;
73 struct smb_private *smb;
76 static void dcerpc_pipe_open_smb_done(struct tevent_req *subreq);
78 struct composite_context *dcerpc_pipe_open_smb_send(struct dcecli_connection *c,
79 struct smbXcli_conn *conn,
80 struct smbXcli_session *session,
81 struct smbXcli_tcon *tcon,
82 uint32_t timeout_msec,
83 const char *pipe_name)
85 struct composite_context *ctx;
86 struct dcerpc_pipe_open_smb_state *state;
87 uint16_t pid = 0;
88 struct tevent_req *subreq;
90 ctx = composite_create(c, c->event_ctx);
91 if (ctx == NULL) return NULL;
93 state = talloc(ctx, struct dcerpc_pipe_open_smb_state);
94 if (composite_nomem(state, ctx)) return ctx;
95 ctx->private_data = state;
97 state->c = c;
98 state->ctx = ctx;
100 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
101 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
102 pipe_name += 6;
104 if ((strncasecmp(pipe_name, "/", 1) == 0) ||
105 (strncasecmp(pipe_name, "\\", 1) == 0)) {
106 pipe_name += 1;
108 state->fname = talloc_strdup(state, pipe_name);
109 if (composite_nomem(state->fname, ctx)) return ctx;
111 state->smb = talloc_zero(state, struct smb_private);
112 if (composite_nomem(state->smb, ctx)) return ctx;
114 state->smb->conn = conn;
115 state->smb->session = session;
116 state->smb->tcon = tcon;
117 state->smb->timeout_msec = timeout_msec;
119 state->c->server_name = strupper_talloc(state->c,
120 smbXcli_conn_remote_name(conn));
121 if (composite_nomem(state->c->server_name, ctx)) return ctx;
123 ctx->status = smbXcli_session_application_key(session,
124 state->smb,
125 &state->smb->session_key);
126 if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_NO_USER_SESSION_KEY)) {
127 state->smb->session_key = data_blob_null;
128 ctx->status = NT_STATUS_OK;
130 if (!composite_is_ok(ctx)) return ctx;
132 subreq = tstream_smbXcli_np_open_send(state, c->event_ctx,
133 conn, session, tcon, pid,
134 timeout_msec, state->fname);
135 if (composite_nomem(subreq, ctx)) return ctx;
136 tevent_req_set_callback(subreq, dcerpc_pipe_open_smb_done, state);
138 return ctx;
141 static void dcerpc_pipe_open_smb_done(struct tevent_req *subreq)
143 struct dcerpc_pipe_open_smb_state *state =
144 tevent_req_callback_data(subreq,
145 struct dcerpc_pipe_open_smb_state);
146 struct composite_context *ctx = state->ctx;
147 struct dcecli_connection *c = state->c;
149 ctx->status = tstream_smbXcli_np_open_recv(subreq,
150 state->smb,
151 &state->c->transport.stream);
152 TALLOC_FREE(subreq);
153 if (!composite_is_ok(ctx)) return;
155 state->c->transport.write_queue =
156 tevent_queue_create(state->c, "dcerpc_smb write queue");
157 if (composite_nomem(state->c->transport.write_queue, ctx)) return;
160 fill in the transport methods
162 c->transport.transport = NCACN_NP;
163 c->transport.private_data = NULL;
166 * Windows uses 4280 for ncacn_np,
167 * so we also use it, this is what our
168 * tstream_smbXcli_np code relies on.
170 c->srv_max_xmit_frag = 4280;
171 c->srv_max_recv_frag = 4280;
173 /* Over-ride the default session key with the SMB session key */
174 c->security_state.session_key = smb_session_key;
176 c->transport.private_data = talloc_move(c, &state->smb);
178 composite_done(ctx);
181 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
183 NTSTATUS status = composite_wait(c);
184 talloc_free(c);
185 return status;
188 _PUBLIC_ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
189 struct smbcli_tree *t,
190 const char *pipe_name)
192 struct smbXcli_conn *conn;
193 struct smbXcli_session *session;
194 struct smbXcli_tcon *tcon;
195 uint32_t timeout_msec;
196 struct composite_context *ctx;
198 conn = t->session->transport->conn;
199 session = t->session->smbXcli;
200 tcon = t->smbXcli;
201 smb1cli_tcon_set_id(tcon, t->tid);
202 timeout_msec = t->session->transport->options.request_timeout * 1000;
204 /* if we don't have a binding on this pipe yet, then create one */
205 if (p->binding == NULL) {
206 NTSTATUS status;
207 const char *r = smbXcli_conn_remote_name(conn);
208 char *str;
209 SMB_ASSERT(r != NULL);
210 str = talloc_asprintf(p, "ncacn_np:%s", r);
211 if (str == NULL) {
212 return NT_STATUS_NO_MEMORY;
214 status = dcerpc_parse_binding(p, str,
215 &p->binding);
216 talloc_free(str);
217 if (!NT_STATUS_IS_OK(status)) {
218 return status;
222 ctx = dcerpc_pipe_open_smb_send(p->conn,
223 conn, session,
224 tcon, timeout_msec,
225 pipe_name);
226 if (ctx == NULL) {
227 return NT_STATUS_NO_MEMORY;
230 return dcerpc_pipe_open_smb_recv(ctx);
233 _PUBLIC_ NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_pipe *p,
234 struct smb2_tree *t,
235 const char *pipe_name)
237 struct smbXcli_conn *conn;
238 struct smbXcli_session *session;
239 struct smbXcli_tcon *tcon;
240 uint32_t timeout_msec;
241 struct composite_context *ctx;
243 conn = t->session->transport->conn;
244 session = t->session->smbXcli;
245 tcon = t->smbXcli;
246 timeout_msec = t->session->transport->options.request_timeout * 1000;
248 /* if we don't have a binding on this pipe yet, then create one */
249 if (p->binding == NULL) {
250 NTSTATUS status;
251 const char *r = smbXcli_conn_remote_name(conn);
252 char *str;
253 SMB_ASSERT(r != NULL);
254 str = talloc_asprintf(p, "ncacn_np:%s", r);
255 if (str == NULL) {
256 return NT_STATUS_NO_MEMORY;
258 status = dcerpc_parse_binding(p, str,
259 &p->binding);
260 talloc_free(str);
261 if (!NT_STATUS_IS_OK(status)) {
262 return status;
266 ctx = dcerpc_pipe_open_smb_send(p->conn,
267 conn, session,
268 tcon, timeout_msec,
269 pipe_name);
270 if (ctx == NULL) {
271 return NT_STATUS_NO_MEMORY;
274 return dcerpc_pipe_open_smb_recv(ctx);
277 struct composite_context *dcerpc_secondary_smb_send(struct dcecli_connection *c1,
278 struct dcecli_connection *c2,
279 const char *pipe_name)
281 struct smb_private *smb;
283 if (c1->transport.transport != NCACN_NP) return NULL;
285 smb = talloc_get_type(c1->transport.private_data, struct smb_private);
286 if (!smb) return NULL;
288 return dcerpc_pipe_open_smb_send(c2,
289 smb->conn,
290 smb->session,
291 smb->tcon,
292 smb->timeout_msec,
293 pipe_name);
296 NTSTATUS dcerpc_secondary_smb_recv(struct composite_context *c)
298 return dcerpc_pipe_open_smb_recv(c);