s3: libsmb: In cli_qpathinfo_send() (SMBtrans2:TRANSACT2_QPATHINFO) check for DFS...
[Samba.git] / source3 / rpc_server / rpc_server.c
blob32fbb78f5ffb398965b6887bee04876ea6529ddc
1 /*
2 Unix SMB/Netbios implementation.
3 Generic infrstructure for RPC Daemons
4 Copyright (C) Simo Sorce 2010
5 Copyright (C) Andrew Bartlett 2011
6 Copyright (C) Andreas Schneider 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "librpc/rpc/dcesrv_core.h"
24 #include "rpc_server/rpc_pipes.h"
25 #include "rpc_server/rpc_server.h"
26 #include "rpc_server/rpc_config.h"
27 #include "rpc_dce.h"
28 #include "librpc/gen_ndr/netlogon.h"
29 #include "librpc/gen_ndr/auth.h"
30 #include "lib/tsocket/tsocket.h"
31 #include "libcli/named_pipe_auth/npa_tstream.h"
32 #include "../auth/auth_sam_reply.h"
33 #include "auth.h"
34 #include "rpc_server/rpc_ncacn_np.h"
35 #include "rpc_server/srv_pipe_hnd.h"
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_RPC_SRV
40 /* Start listening on the appropriate unix socket and setup all is needed to
41 * dispatch requests to the pipes rpc implementation */
43 struct dcerpc_ncacn_listen_state {
44 int fd;
46 struct tevent_context *ev_ctx;
47 struct messaging_context *msg_ctx;
48 struct dcesrv_context *dce_ctx;
49 struct dcesrv_endpoint *endpoint;
50 dcerpc_ncacn_termination_fn termination_fn;
51 void *termination_data;
54 static void ncacn_terminate_connection(struct dcerpc_ncacn_conn *conn,
55 const char *reason);
57 NTSTATUS dcesrv_auth_gensec_prepare(
58 TALLOC_CTX *mem_ctx,
59 struct dcesrv_call_state *call,
60 struct gensec_security **out,
61 void *private_data)
63 struct gensec_security *gensec = NULL;
64 NTSTATUS status;
66 if (out == NULL) {
67 return NT_STATUS_INVALID_PARAMETER;
70 status = auth_generic_prepare(mem_ctx,
71 call->conn->remote_address,
72 call->conn->local_address,
73 "DCE/RPC",
74 &gensec);
75 if (!NT_STATUS_IS_OK(status)) {
76 DBG_ERR("Failed to prepare gensec: %s\n", nt_errstr(status));
77 return status;
80 *out = gensec;
82 return NT_STATUS_OK;
85 void dcesrv_log_successful_authz(
86 struct dcesrv_call_state *call,
87 void *private_data)
89 TALLOC_CTX *frame = talloc_stackframe();
90 struct auth4_context *auth4_context = NULL;
91 struct dcesrv_auth *auth = call->auth_state;
92 enum dcerpc_transport_t transport = dcerpc_binding_get_transport(
93 call->conn->endpoint->ep_description);
94 const char *auth_type = derpc_transport_string_by_transport(transport);
95 const char *transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
96 NTSTATUS status;
98 if (frame == NULL) {
99 DBG_ERR("No memory");
100 return;
103 if (transport == NCACN_NP) {
104 transport_protection = AUTHZ_TRANSPORT_PROTECTION_SMB;
107 become_root();
108 status = make_auth4_context(frame, &auth4_context);
109 unbecome_root();
110 if (!NT_STATUS_IS_OK(status)) {
111 DBG_ERR("Unable to make auth context for authz log.\n");
112 TALLOC_FREE(frame);
113 return;
117 * Log the authorization to this RPC interface. This
118 * covered ncacn_np pass-through auth, and anonymous
119 * DCE/RPC (eg epmapper, netlogon etc)
121 log_successful_authz_event(auth4_context->msg_ctx,
122 auth4_context->lp_ctx,
123 call->conn->remote_address,
124 call->conn->local_address,
125 "DCE/RPC",
126 auth_type,
127 transport_protection,
128 auth->session_info);
130 auth->auth_audited = true;
132 TALLOC_FREE(frame);
135 static int dcesrv_assoc_group_destructor(struct dcesrv_assoc_group *assoc_group)
137 int ret;
138 ret = idr_remove(assoc_group->dce_ctx->assoc_groups_idr,
139 assoc_group->id);
140 if (ret != 0) {
141 DBG_ERR("Failed to remove assoc_group 0x%08x\n",
142 assoc_group->id);
144 return 0;
147 static NTSTATUS dcesrv_assoc_group_new(struct dcesrv_call_state *call)
149 struct dcesrv_connection *conn = call->conn;
150 struct dcesrv_context *dce_ctx = conn->dce_ctx;
151 const struct dcesrv_endpoint *endpoint = conn->endpoint;
152 enum dcerpc_transport_t transport =
153 dcerpc_binding_get_transport(endpoint->ep_description);
154 struct dcesrv_assoc_group *assoc_group = NULL;
155 int id;
157 assoc_group = talloc_zero(conn, struct dcesrv_assoc_group);
158 if (assoc_group == NULL) {
159 return NT_STATUS_NO_MEMORY;
162 id = idr_get_new_random(dce_ctx->assoc_groups_idr,
163 assoc_group,
164 UINT16_MAX);
165 if (id == -1) {
166 TALLOC_FREE(assoc_group);
167 DBG_ERR("Out of association groups!\n");
168 return NT_STATUS_RPC_OUT_OF_RESOURCES;
171 assoc_group->transport = transport;
172 assoc_group->id = id;
173 assoc_group->dce_ctx = dce_ctx;
175 call->conn->assoc_group = assoc_group;
177 talloc_set_destructor(assoc_group, dcesrv_assoc_group_destructor);
179 return NT_STATUS_OK;
182 static NTSTATUS dcesrv_assoc_group_reference(struct dcesrv_call_state *call,
183 uint32_t assoc_group_id)
185 struct dcesrv_connection *conn = call->conn;
186 const struct dcesrv_endpoint *endpoint = conn->endpoint;
187 enum dcerpc_transport_t transport =
188 dcerpc_binding_get_transport(endpoint->ep_description);
189 struct dcesrv_assoc_group *assoc_group = NULL;
190 void *id_ptr = NULL;
192 /* find an association group given a assoc_group_id */
193 id_ptr = idr_find(conn->dce_ctx->assoc_groups_idr, assoc_group_id);
194 if (id_ptr == NULL) {
196 * FIXME If the association group is not found it has
197 * been created in other process (preforking daemons).
198 * Until this is properly fixed we just create a new
199 * association group in this process
201 DBG_NOTICE("Failed to find assoc_group 0x%08x in this "
202 "server process, creating a new one\n",
203 assoc_group_id);
204 return dcesrv_assoc_group_new(call);
206 assoc_group = talloc_get_type_abort(id_ptr, struct dcesrv_assoc_group);
208 if (assoc_group->transport != transport) {
209 const char *at =
210 derpc_transport_string_by_transport(
211 assoc_group->transport);
212 const char *ct =
213 derpc_transport_string_by_transport(
214 transport);
216 DBG_NOTICE("assoc_group 0x%08x (transport %s) "
217 "is not available on transport %s",
218 assoc_group_id, at, ct);
219 return NT_STATUS_UNSUCCESSFUL;
222 conn->assoc_group = talloc_reference(conn, assoc_group);
223 return NT_STATUS_OK;
226 NTSTATUS dcesrv_assoc_group_find(
227 struct dcesrv_call_state *call,
228 void *private_data)
230 uint32_t assoc_group_id = call->pkt.u.bind.assoc_group_id;
232 if (assoc_group_id != 0) {
233 return dcesrv_assoc_group_reference(call, assoc_group_id);
236 /* If not requested by client create a new association group */
237 return dcesrv_assoc_group_new(call);
240 void dcesrv_transport_terminate_connection(struct dcesrv_connection *dce_conn,
241 const char *reason)
243 struct dcerpc_ncacn_conn *ncacn_conn = talloc_get_type_abort(
244 dce_conn->transport.private_data,
245 struct dcerpc_ncacn_conn);
247 ncacn_terminate_connection(ncacn_conn, reason);
250 static void ncacn_terminate_connection(struct dcerpc_ncacn_conn *conn,
251 const char *reason)
253 if (reason == NULL) {
254 reason = "Unknown reason";
257 DBG_NOTICE("Terminating connection - '%s'\n", reason);
259 talloc_free(conn);
262 NTSTATUS dcesrv_endpoint_by_ncacn_np_name(struct dcesrv_context *dce_ctx,
263 const char *pipe_name,
264 struct dcesrv_endpoint **out)
266 struct dcesrv_endpoint *e = NULL;
268 for (e = dce_ctx->endpoint_list; e; e = e->next) {
269 enum dcerpc_transport_t transport =
270 dcerpc_binding_get_transport(e->ep_description);
271 const char *endpoint = NULL;
273 if (transport != NCACN_NP) {
274 continue;
277 endpoint = dcerpc_binding_get_string_option(e->ep_description,
278 "endpoint");
279 if (endpoint == NULL) {
280 continue;
283 if (strncmp(endpoint, "\\pipe\\", 6) == 0) {
284 endpoint += 6;
287 if (strequal(endpoint, pipe_name)) {
288 *out = e;
289 return NT_STATUS_OK;
293 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
296 struct pipes_struct *dcesrv_get_pipes_struct(struct dcesrv_connection *conn)
298 struct dcerpc_ncacn_conn *ncacn_conn = talloc_get_type_abort(
299 conn->transport.private_data,
300 struct dcerpc_ncacn_conn);
302 return &ncacn_conn->p;
305 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */