selftest: export/use CTDB related envvars in order to run the ctdb command
[Samba.git] / source3 / rpc_server / rpc_ncacn_np.c
blob03618df41e0578d18cef91fb30d1143f88d1fc5d
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1998,
5 * Largely re-written : 2005
6 * Copyright (C) Jeremy Allison 1998 - 2005
7 * Copyright (C) Simo Sorce 2010
8 * Copyright (C) Andrew Bartlett 2011
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "rpc_dce.h"
27 #include "../libcli/named_pipe_auth/npa_tstream.h"
28 #include "rpc_server/rpc_ncacn_np.h"
29 #include "librpc/gen_ndr/netlogon.h"
30 #include "librpc/gen_ndr/auth.h"
31 #include "../auth/auth_sam_reply.h"
32 #include "../auth/auth_util.h"
33 #include "auth.h"
34 #include "rpc_server/rpc_pipes.h"
35 #include "../lib/tsocket/tsocket.h"
36 #include "../lib/util/tevent_ntstatus.h"
37 #include "rpc_server/rpc_config.h"
38 #include "librpc/ndr/ndr_table.h"
39 #include "rpc_server/rpc_server.h"
40 #include "librpc/rpc/dcerpc_util.h"
42 #undef DBGC_CLASS
43 #define DBGC_CLASS DBGC_RPC_SRV
45 struct np_proxy_state {
46 uint16_t file_type;
47 uint16_t device_state;
48 uint64_t allocation_size;
49 struct tstream_context *npipe;
50 struct tevent_queue *read_queue;
51 struct tevent_queue *write_queue;
54 struct npa_state *npa_state_init(TALLOC_CTX *mem_ctx)
56 struct npa_state *npa;
58 npa = talloc_zero(mem_ctx, struct npa_state);
59 if (npa == NULL) {
60 return NULL;
63 npa->read_queue = tevent_queue_create(npa, "npa_cli_read");
64 if (npa->read_queue == NULL) {
65 DEBUG(0, ("tevent_queue_create failed\n"));
66 goto fail;
69 npa->write_queue = tevent_queue_create(npa, "npa_cli_write");
70 if (npa->write_queue == NULL) {
71 DEBUG(0, ("tevent_queue_create failed\n"));
72 goto fail;
75 return npa;
76 fail:
77 talloc_free(npa);
78 return NULL;
81 /**
82 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
84 * @param[in] mem_ctx The memory context to use.
86 * @param[in] ndr_table Normally the ndr_table_<name>.
88 * @param[in] remote_address The info about the connected client.
90 * @param[in] serversupplied_info The server supplied authentication function.
92 * @param[in] msg_ctx The messaging context that can be used by the server
94 * @param[out] binding_handle A pointer to store the connected
95 * dcerpc_binding_handle
97 * @return NT_STATUS_OK on success, a corresponding NT status if an
98 * error occurred.
100 * @code
101 * struct dcerpc_binding_handle *winreg_binding;
102 * NTSTATUS status;
104 * status = rpcint_binding_handle(tmp_ctx,
105 * &ndr_table_winreg,
106 * p->remote_address,
107 * p->session_info,
108 * p->msg_ctx
109 * &winreg_binding);
110 * @endcode
112 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
113 const struct ndr_interface_table *ndr_table,
114 const struct tsocket_address *remote_address,
115 const struct tsocket_address *local_address,
116 const struct auth_session_info *session_info,
117 struct messaging_context *msg_ctx,
118 struct dcerpc_binding_handle **binding_handle)
120 struct rpc_pipe_client *rpccli = NULL;
121 NTSTATUS status;
123 status = rpc_pipe_open_local_np(
124 mem_ctx,
125 ndr_table,
126 NULL,
127 remote_address,
128 NULL,
129 local_address,
130 session_info,
131 &rpccli);
132 if (!NT_STATUS_IS_OK(status)) {
133 DBG_DEBUG("rpc_pipe_open_local_np failed: %s\n",
134 nt_errstr(status));
135 goto fail;
138 *binding_handle = rpccli->binding_handle;
139 return NT_STATUS_OK;
140 fail:
141 TALLOC_FREE(rpccli);
142 return status;
146 * @brief Create a new RPC client context which uses a local dispatch function
147 * or a remote transport, depending on rpc_server configuration for the
148 * specific service.
150 * @param[in] mem_ctx The memory context to use.
152 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
153 * ndr_table_<name>.
155 * @param[in] serversupplied_info The server supplied authentication function.
157 * @param[in] remote_address The client address information.
159 * @param[in] msg_ctx The messaging context to use.
161 * @param[out] presult A pointer to store the connected rpc client pipe.
163 * @return NT_STATUS_OK on success, a corresponding NT status if an
164 * error occurred.
166 * @code
167 * struct rpc_pipe_client *winreg_pipe;
168 * NTSTATUS status;
170 * status = rpc_pipe_open_interface(tmp_ctx,
171 * &ndr_table_winreg.syntax_id,
172 * p->session_info,
173 * remote_address,
174 * &winreg_pipe);
175 * @endcode
178 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
179 const struct ndr_interface_table *table,
180 const struct auth_session_info *session_info,
181 const struct tsocket_address *remote_address,
182 const struct tsocket_address *local_address,
183 struct messaging_context *msg_ctx,
184 struct rpc_pipe_client **cli_pipe)
186 struct rpc_pipe_client *cli = NULL;
187 NTSTATUS status;
189 if (cli_pipe != NULL) {
190 if (rpccli_is_connected(*cli_pipe)) {
191 return NT_STATUS_OK;
192 } else {
193 TALLOC_FREE(*cli_pipe);
197 status = rpc_pipe_open_local_np(
198 mem_ctx,
199 table,
200 NULL,
201 remote_address,
202 NULL,
203 local_address,
204 session_info,
205 &cli);
206 if (!NT_STATUS_IS_OK(status)) {
207 DBG_ERR("Could not connect to %s pipe: %s\n",
208 table->name,
209 nt_errstr(status));
210 return status;
213 if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
214 *cli_pipe = cli;
216 return status;