s3-clitar: Improve readabilty of make_remote_path().
[Samba/wip.git] / source3 / rpc_client / rpc_transport_np.c
blob86190c6733870a6000823fceefec7ee92dc4e45c
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC client transport over named pipes
4 * Copyright (C) Volker Lendecke 2009
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 "../lib/util/tevent_ntstatus.h"
22 #include "rpc_client/rpc_transport.h"
23 #include "librpc/ndr/ndr_table.h"
24 #include "libcli/smb/smbXcli_base.h"
25 #include "libcli/smb/tstream_smbXcli_np.h"
26 #include "client.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_RPC_CLI
31 struct rpc_transport_np_init_state {
32 struct rpc_cli_transport *transport;
35 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
37 struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct cli_state *cli,
40 const struct ndr_interface_table *table)
42 struct tevent_req *req;
43 struct rpc_transport_np_init_state *state;
44 const char *pipe_name;
45 struct tevent_req *subreq;
46 struct smbXcli_session *session;
47 struct smbXcli_tcon *tcon;
48 uint16_t pid = 0;
50 req = tevent_req_create(mem_ctx, &state,
51 struct rpc_transport_np_init_state);
52 if (req == NULL) {
53 return NULL;
56 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
57 tcon = cli->smb2.tcon;
58 session = cli->smb2.session;
59 } else {
60 tcon = cli->smb1.tcon;
61 session = cli->smb1.session;
62 pid = cli->smb1.pid;
65 pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
66 if (tevent_req_nomem(pipe_name, req)) {
67 return tevent_req_post(req, ev);
70 while (pipe_name[0] == '\\') {
71 pipe_name++;
74 subreq = tstream_smbXcli_np_open_send(state, ev, cli->conn,
75 session, tcon, pid,
76 cli->timeout, pipe_name);
77 if (tevent_req_nomem(subreq, req)) {
78 return tevent_req_post(req, ev);
80 tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
82 return req;
85 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
87 struct tevent_req *req = tevent_req_callback_data(
88 subreq, struct tevent_req);
89 struct rpc_transport_np_init_state *state = tevent_req_data(
90 req, struct rpc_transport_np_init_state);
91 NTSTATUS status;
92 struct tstream_context *stream;
94 status = tstream_smbXcli_np_open_recv(subreq, state, &stream);
95 TALLOC_FREE(subreq);
96 if (!NT_STATUS_IS_OK(status)) {
97 tevent_req_nterror(req, status);
98 return;
101 status = rpc_transport_tstream_init(state,
102 &stream,
103 &state->transport);
104 if (!NT_STATUS_IS_OK(status)) {
105 tevent_req_nterror(req, status);
106 return;
109 tevent_req_done(req);
112 NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
113 TALLOC_CTX *mem_ctx,
114 struct rpc_cli_transport **presult)
116 struct rpc_transport_np_init_state *state = tevent_req_data(
117 req, struct rpc_transport_np_init_state);
118 NTSTATUS status;
120 if (tevent_req_is_nterror(req, &status)) {
121 return status;
124 *presult = talloc_move(mem_ctx, &state->transport);
125 return NT_STATUS_OK;
128 NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
129 const struct ndr_interface_table *table,
130 struct rpc_cli_transport **presult)
132 TALLOC_CTX *frame = talloc_stackframe();
133 struct tevent_context *ev;
134 struct tevent_req *req;
135 NTSTATUS status = NT_STATUS_OK;
137 ev = samba_tevent_context_init(frame);
138 if (ev == NULL) {
139 status = NT_STATUS_NO_MEMORY;
140 goto fail;
143 req = rpc_transport_np_init_send(frame, ev, cli, table);
144 if (req == NULL) {
145 status = NT_STATUS_NO_MEMORY;
146 goto fail;
149 if (!tevent_req_poll(req, ev)) {
150 status = map_nt_error_from_unix(errno);
151 goto fail;
154 status = rpc_transport_np_init_recv(req, mem_ctx, presult);
155 fail:
156 TALLOC_FREE(frame);
157 return status;