1 /*
2    Unix SMB/CIFS implementation.
3 
4    smbd-specific dcerpc server code
5 
6    Copyright (C) Andrew Tridgell 2003-2005
7    Copyright (C) Stefan (metze) Metzmacher 2004-2005
8    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2004,2007
9 
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.
14 
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.
19 
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/>.
22 */
23 
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
26 #include "auth/auth.h"
27 #include "../lib/util/dlinklist.h"
28 #include "rpc_server/dcerpc_server.h"
29 #include "rpc_server/dcerpc_server_proto.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "system/filesys.h"
32 #include "lib/messaging/irpc.h"
33 #include "system/network.h"
34 #include "lib/socket/netif.h"
35 #include "param/param.h"
36 #include "../lib/tsocket/tsocket.h"
37 #include "librpc/rpc/dcerpc_proto.h"
38 #include "../lib/util/tevent_ntstatus.h"
39 #include "libcli/raw/smb.h"
40 #include "../libcli/named_pipe_auth/npa_tstream.h"
41 #include "smbd/process_model.h"
42 
43 struct dcesrv_context_callbacks srv_callbacks = {
44 	.log.successful_authz = log_successful_dcesrv_authz_event,
45 	.auth.gensec_prepare = dcesrv_gensec_prepare,
46 	.assoc_group.find = dcesrv_assoc_group_find,
47 };
48 
49 /*
50  * Need to run the majority of the RPC endpoints in a single process to allow
51  * for shared handles, and the sharing of ldb contexts.
52  *
53  * However other endpoints are capable of being run in multiple processes
54  * e.g. NETLOGON.
55  *
56  * To support this the process model is manipulated to force those end points
57  * not supporting multiple processes into the single process model. The code
58  * responsible for this is in dcesrv_init_endpoints
59  *
60  */
61 NTSTATUS server_service_rpc_init(TALLOC_CTX *);
62 
63 /*
64  * Initialise the rpc endpoints.
65  */
dcesrv_init_endpoints(struct task_server * task,struct dcesrv_context * dce_ctx,bool use_single_process)66 static NTSTATUS dcesrv_init_endpoints(struct task_server *task,
67 				      struct dcesrv_context *dce_ctx,
68 				      bool use_single_process)
69 {
70 
71 	struct dcesrv_endpoint *e;
72 	const struct model_ops *model_ops = NULL;
73 
74 	/*
75 	 * For those RPC services that run with shared context we need to
76 	 * ensure that they don't fork a new process on accept (standard_model).
77 	 * And as there is only one process handling these requests we need
78 	 * to handle accept errors in a similar manner to the single process
79 	 * model.
80 	 *
81 	 * To do this we override the process model operations with the single
82 	 * process operations. This is not the most elegant solution, but it is
83 	 * the least ugly, and is confined to the next block of code.
84 	 */
85 	if (use_single_process == true) {
86 		model_ops = process_model_startup("single");
87 		if (model_ops == NULL) {
88 			DBG_ERR("Unable to load single process model");
89 			return NT_STATUS_INTERNAL_ERROR;
90 		}
91 	} else {
92 		model_ops = task->model_ops;
93 	}
94 
95 	for (e = dce_ctx->endpoint_list; e; e = e->next) {
96 
97 		enum dcerpc_transport_t transport =
98 		    dcerpc_binding_get_transport(e->ep_description);
99 
100 		if (transport == NCACN_HTTP) {
101 			/*
102 			 * We don't support ncacn_http yet
103 			 */
104 			continue;
105 		}
106 		if (e->use_single_process == use_single_process) {
107 			NTSTATUS status;
108 			status = dcesrv_add_ep(dce_ctx,
109 					       task->lp_ctx,
110 					       e,
111 					       task->event_ctx,
112 					       model_ops,
113 					       task->process_context);
114 			if (!NT_STATUS_IS_OK(status)) {
115 				return status;
116 			}
117 		}
118 	}
119 	return NT_STATUS_OK;
120 }
121 
122 /*
123  * Initialise the RPC service.
124  * And those end points that can be serviced by multiple processes.
125  * The endpoints that need to be run in a single process are setup in the
126  * post_fork hook.
127 */
dcesrv_task_init(struct task_server * task)128 static NTSTATUS dcesrv_task_init(struct task_server *task)
129 {
130 	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
131 	struct dcesrv_context *dce_ctx;
132 	const char **ep_servers = NULL;
133 
134 	dcerpc_server_init(task->lp_ctx);
135 
136 	task_server_set_title(task, "task[dcesrv]");
137 
138 	status = dcesrv_init_context(task->event_ctx,
139 				     task->lp_ctx,
140 				     &srv_callbacks,
141 				     &dce_ctx);
142 	if (!NT_STATUS_IS_OK(status)) {
143 		return status;
144 	}
145 
146 	ep_servers = lpcfg_dcerpc_endpoint_servers(task->lp_ctx);
147 	status = dcesrv_init_ep_servers(dce_ctx, ep_servers);
148 	if (!NT_STATUS_IS_OK(status)) {
149 		return status;
150 	}
151 
152 	/* Make sure the directory for NCALRPC exists */
153 	if (!directory_exist(lpcfg_ncalrpc_dir(task->lp_ctx))) {
154 		mkdir(lpcfg_ncalrpc_dir(task->lp_ctx), 0755);
155 	}
156 	status = dcesrv_init_endpoints(task, dce_ctx, false);
157 	if (!NT_STATUS_IS_OK(status)) {
158 		return status;
159 	}
160 
161 	task->private_data = dce_ctx;
162 	return NT_STATUS_OK;
163 }
164 
165 /*
166  * Initialise the endpoints that need to run in a single process fork.
167  * The endpoint registration is only done for the first process instance.
168  *
169  */
dcesrv_post_fork(struct task_server * task,struct process_details * pd)170 static void dcesrv_post_fork(struct task_server *task,
171 			     struct process_details *pd)
172 {
173 
174 	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
175 	struct dcesrv_context *dce_ctx;
176 
177 	if (task->private_data == NULL) {
178 		task_server_terminate(task, "dcerpc: No dcesrv_context", true);
179 		return;
180 	}
181 	dce_ctx =
182 	    talloc_get_type_abort(task->private_data, struct dcesrv_context);
183 
184 	/*
185 	 * Ensure the single process endpoints are only available to the
186 	 * first instance.
187 	 */
188 	if (pd->instances == 0) {
189 		status = dcesrv_init_endpoints(task, dce_ctx, true);
190 		if (!NT_STATUS_IS_OK(status)) {
191 			task_server_terminate(
192 			    task,
193 			    "dcerpc: Failed to initialise end points",
194 			    true);
195 			return;
196 		}
197 	}
198 
199 	irpc_add_name(task->msg_ctx, "rpc_server");
200 }
201 
server_service_rpc_init(TALLOC_CTX * ctx)202 NTSTATUS server_service_rpc_init(TALLOC_CTX *ctx)
203 {
204 	static const struct service_details details = {
205 	    .inhibit_fork_on_accept = false,
206 	    .inhibit_pre_fork = false,
207 	    .task_init = dcesrv_task_init,
208 	    .post_fork = dcesrv_post_fork};
209 	return register_server_service(ctx, "rpc", &details);
210 }
211