util:datablob: data_blob_pad checks its alignment assumption
[samba.git] / source3 / rpc_client / rpc_transport_np.c
blob48ec425fee854d27dc0587ec72b1fbb38a903d5e
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 "librpc/rpc/dcerpc_util.h"
23 #include "rpc_client/rpc_transport.h"
24 #include "librpc/ndr/ndr_table.h"
25 #include "libcli/smb/smbXcli_base.h"
26 #include "libcli/smb/tstream_smbXcli_np.h"
27 #include "client.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_RPC_CLI
32 struct rpc_transport_np_init_state {
33 struct rpc_cli_transport *transport;
34 int retries;
35 struct tevent_context *ev;
36 struct smbXcli_conn *conn;
37 int timeout;
38 struct timeval abs_timeout;
39 const char *pipe_name;
40 struct smbXcli_session *session;
41 struct smbXcli_tcon *tcon;
42 uint16_t pid;
45 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
47 struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
48 struct tevent_context *ev,
49 struct cli_state *cli,
50 const char *pipe_name)
52 struct tevent_req *req;
53 struct rpc_transport_np_init_state *state;
54 struct tevent_req *subreq;
56 req = tevent_req_create(mem_ctx, &state,
57 struct rpc_transport_np_init_state);
58 if (req == NULL) {
59 return NULL;
62 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
63 state->tcon = cli->smb2.tcon;
64 state->session = cli->smb2.session;
65 } else {
66 state->tcon = cli->smb1.tcon;
67 state->session = cli->smb1.session;
68 state->pid = cli->smb1.pid;
71 state->ev = ev;
72 state->conn = cli->conn;
73 state->timeout = cli->timeout;
74 state->abs_timeout = timeval_current_ofs_msec(cli->timeout);
75 state->pipe_name = talloc_strdup(state, pipe_name);
76 if (tevent_req_nomem(state->pipe_name, req)) {
77 return tevent_req_post(req, ev);
80 while (state->pipe_name[0] == '\\') {
81 state->pipe_name++;
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);
91 tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
93 return req;
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)
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);
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;
116 tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
117 state->retries++;
120 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
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;
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;
135 * Retry on STATUS_PIPE_NOT_AVAILABLE, Windows starts some
136 * servers (FssagentRpc) on demand.
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\n"));
147 return;
150 if (tevent_req_nterror(req, status)) {
151 return;
154 status = rpc_transport_tstream_init(state,
155 &stream,
156 &state->transport);
157 if (tevent_req_nterror(req, status)) {
158 return;
161 tevent_req_done(req);
164 NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
165 TALLOC_CTX *mem_ctx,
166 struct rpc_cli_transport **presult)
168 struct rpc_transport_np_init_state *state = tevent_req_data(
169 req, struct rpc_transport_np_init_state);
170 NTSTATUS status;
172 if (tevent_req_is_nterror(req, &status)) {
173 return status;
176 *presult = talloc_move(mem_ctx, &state->transport);
177 return NT_STATUS_OK;