wscript: Add check for --wrap linker flag
[vlendec/samba-autobuild/.git] / source3 / rpc_client / rpc_transport_np.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC client transport over named pipes
4  *  Copyright (C) Volker Lendecke 2009
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 "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"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_RPC_CLI
30
31 struct rpc_transport_np_init_state {
32         struct rpc_cli_transport *transport;
33         int retries;
34         struct tevent_context *ev;
35         struct smbXcli_conn *conn;
36         int timeout;
37         struct timeval abs_timeout;
38         const char *pipe_name;
39         struct smbXcli_session *session;
40         struct smbXcli_tcon *tcon;
41         uint16_t pid;
42 };
43
44 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
45
46 struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
47                                               struct tevent_context *ev,
48                                               struct cli_state *cli,
49                                               const struct ndr_interface_table *table)
50 {
51         struct tevent_req *req;
52         struct rpc_transport_np_init_state *state;
53         struct tevent_req *subreq;
54
55         req = tevent_req_create(mem_ctx, &state,
56                                 struct rpc_transport_np_init_state);
57         if (req == NULL) {
58                 return NULL;
59         }
60
61         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
62                 state->tcon = cli->smb2.tcon;
63                 state->session = cli->smb2.session;
64         } else {
65                 state->tcon = cli->smb1.tcon;
66                 state->session = cli->smb1.session;
67                 state->pid = cli->smb1.pid;
68         }
69
70         state->ev = ev;
71         state->conn = cli->conn;
72         state->timeout = cli->timeout;
73         state->abs_timeout = timeval_current_ofs_msec(cli->timeout);
74         state->pipe_name = dcerpc_default_transport_endpoint(state, NCACN_NP,
75                                                              table);
76         if (tevent_req_nomem(state->pipe_name, req)) {
77                 return tevent_req_post(req, ev);
78         }
79
80         while (state->pipe_name[0] == '\\') {
81                 state->pipe_name++;
82         }
83
84         subreq = tstream_smbXcli_np_open_send(state, ev, state->conn,
85                                               state->session, state->tcon,
86                                               state->pid, state->timeout,
87                                               state->pipe_name);
88         if (tevent_req_nomem(subreq, req)) {
89                 return tevent_req_post(req, ev);
90         }
91         tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
92
93         return req;
94 }
95
96 static void rpc_transport_np_init_pipe_open_retry(struct tevent_context *ev,
97                                                   struct tevent_timer *te,
98                                                   struct timeval t,
99                                                   void *priv_data)
100 {
101         struct tevent_req *subreq;
102         struct tevent_req *req = talloc_get_type(priv_data, struct tevent_req);
103         struct rpc_transport_np_init_state *state = tevent_req_data(
104                 req, struct rpc_transport_np_init_state);
105
106         subreq = tstream_smbXcli_np_open_send(state, ev,
107                                               state->conn,
108                                               state->session,
109                                               state->tcon,
110                                               state->pid,
111                                               state->timeout,
112                                               state->pipe_name);
113         if (tevent_req_nomem(subreq, req)) {
114                 return;
115         }
116         tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
117         state->retries++;
118 }
119
120 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
121 {
122         struct tevent_req *req = tevent_req_callback_data(
123                 subreq, struct tevent_req);
124         struct rpc_transport_np_init_state *state = tevent_req_data(
125                 req, struct rpc_transport_np_init_state);
126         NTSTATUS status;
127         struct tstream_context *stream;
128
129         status = tstream_smbXcli_np_open_recv(subreq, state, &stream);
130         TALLOC_FREE(subreq);
131         if (NT_STATUS_EQUAL(status, NT_STATUS_PIPE_NOT_AVAILABLE)
132                                 && (!timeval_expired(&state->abs_timeout))) {
133                 struct tevent_timer *te;
134                 /*
135                  * Retry on STATUS_PIPE_NOT_AVAILABLE, Windows starts some
136                  * servers (FssagentRpc) on demand.
137                  */
138                 DEBUG(2, ("RPC pipe %s not available, retry %d\n",
139                           state->pipe_name, state->retries));
140                 te = tevent_add_timer(state->ev, state,
141                                  timeval_current_ofs_msec(100 * state->retries),
142                                  rpc_transport_np_init_pipe_open_retry, req);
143                 if (tevent_req_nomem(te, req)) {
144                         DEBUG(2, ("Failed to create asynchronous "
145                                         "tevent_timer"));
146                 }
147                 return;
148         } else if (!NT_STATUS_IS_OK(status)) {
149                 tevent_req_nterror(req, status);
150                 return;
151         }
152
153         status = rpc_transport_tstream_init(state,
154                                             &stream,
155                                             &state->transport);
156         if (!NT_STATUS_IS_OK(status)) {
157                 tevent_req_nterror(req, status);
158                 return;
159         }
160
161         tevent_req_done(req);
162 }
163
164 NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
165                                     TALLOC_CTX *mem_ctx,
166                                     struct rpc_cli_transport **presult)
167 {
168         struct rpc_transport_np_init_state *state = tevent_req_data(
169                 req, struct rpc_transport_np_init_state);
170         NTSTATUS status;
171
172         if (tevent_req_is_nterror(req, &status)) {
173                 return status;
174         }
175
176         *presult = talloc_move(mem_ctx, &state->transport);
177         return NT_STATUS_OK;
178 }
179
180 NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
181                                const struct ndr_interface_table *table,
182                                struct rpc_cli_transport **presult)
183 {
184         TALLOC_CTX *frame = talloc_stackframe();
185         struct tevent_context *ev;
186         struct tevent_req *req;
187         NTSTATUS status = NT_STATUS_OK;
188
189         ev = samba_tevent_context_init(frame);
190         if (ev == NULL) {
191                 status = NT_STATUS_NO_MEMORY;
192                 goto fail;
193         }
194
195         req = rpc_transport_np_init_send(frame, ev, cli, table);
196         if (req == NULL) {
197                 status = NT_STATUS_NO_MEMORY;
198                 goto fail;
199         }
200
201         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
202                 goto fail;
203         }
204
205         status = rpc_transport_np_init_recv(req, mem_ctx, presult);
206  fail:
207         TALLOC_FREE(frame);
208         return status;
209 }