CVE-2020-1472(ZeroLogon): libcli/auth: add netlogon_creds_random_challenge()
[Samba.git] / source3 / rpc_server / rpc_handles.c
blobd897e0caabe82d2c2f171b2b223b43de4d7947e6
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Jeremy Allison 2001.
7 *
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 "system/passwd.h" /* uid_wrapper */
24 #include "../librpc/gen_ndr/ndr_lsa.h"
25 #include "../librpc/gen_ndr/ndr_samr.h"
26 #include "auth.h"
27 #include "rpc_server/rpc_pipes.h"
28 #include "../libcli/security/security.h"
29 #include "lib/tsocket/tsocket.h"
30 #include "librpc/ndr/ndr_table.h"
31 #include "librpc/rpc/dcesrv_core.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_RPC_SRV
36 static size_t num_handles = 0;
38 /* TODO
39 * the following prototypes are declared here to avoid
40 * code being moved about too much for a patch to be
41 * disrupted / less obvious.
43 * these functions, and associated functions that they
44 * call, should be moved behind a .so module-loading
45 * system _anyway_. so that's the next step...
48 int make_base_pipes_struct(TALLOC_CTX *mem_ctx,
49 struct messaging_context *msg_ctx,
50 const char *pipe_name,
51 enum dcerpc_transport_t transport,
52 const struct tsocket_address *remote_address,
53 const struct tsocket_address *local_address,
54 struct pipes_struct **_p)
56 struct pipes_struct *p;
58 p = talloc_zero(mem_ctx, struct pipes_struct);
59 if (!p) {
60 return ENOMEM;
63 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
64 if (!p->mem_ctx) {
65 talloc_free(p);
66 return ENOMEM;
69 p->msg_ctx = msg_ctx;
70 p->transport = transport;
72 p->remote_address = tsocket_address_copy(remote_address, p);
73 if (p->remote_address == NULL) {
74 talloc_free(p);
75 return ENOMEM;
78 if (local_address) {
79 p->local_address = tsocket_address_copy(local_address, p);
80 if (p->local_address == NULL) {
81 talloc_free(p);
82 return ENOMEM;
86 *_p = p;
87 return 0;
90 bool check_open_pipes(void)
92 if (num_handles > 0) {
93 return true;
96 return false;
99 size_t num_pipe_handles(void)
101 return num_handles;
104 /****************************************************************************
105 find first available policy slot. creates a policy handle for you.
107 If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
108 talloc_moves this into the handle. If the policy_hnd is closed,
109 data_ptr is TALLOC_FREE()'ed
110 ****************************************************************************/
112 bool create_policy_hnd(struct pipes_struct *p,
113 struct policy_handle *hnd,
114 uint8_t handle_type,
115 void *data_ptr)
117 struct dcesrv_handle *rpc_hnd = NULL;
119 rpc_hnd = dcesrv_handle_create(p->dce_call, handle_type);
120 if (rpc_hnd == NULL) {
121 return false;
124 if (data_ptr != NULL) {
125 rpc_hnd->data = talloc_move(rpc_hnd, &data_ptr);
128 *hnd = rpc_hnd->wire_handle;
130 num_handles++;
132 return true;
135 /****************************************************************************
136 find policy by handle - internal version.
137 ****************************************************************************/
139 static struct dcesrv_handle *find_policy_by_hnd_internal(
140 struct pipes_struct *p,
141 const struct policy_handle *hnd,
142 uint8_t handle_type,
143 void **data_p)
145 struct dcesrv_handle *h = NULL;
147 if (data_p) {
148 *data_p = NULL;
152 * Do not pass handle_type to avoid setting the fault_state in the
153 * pipes_struct if the handle type does not match
155 h = dcesrv_handle_lookup(p->dce_call, hnd, DCESRV_HANDLE_ANY);
156 if (h != NULL) {
157 if (handle_type != DCESRV_HANDLE_ANY &&
158 h->wire_handle.handle_type != handle_type) {
159 /* Just return NULL, do not set a fault
160 * state in pipes_struct */
161 return NULL;
163 if (data_p) {
164 *data_p = h->data;
166 return h;
169 p->fault_state = DCERPC_FAULT_CONTEXT_MISMATCH;
171 return NULL;
174 /****************************************************************************
175 find policy by handle
176 ****************************************************************************/
178 void *_find_policy_by_hnd(struct pipes_struct *p,
179 const struct policy_handle *hnd,
180 uint8_t handle_type,
181 NTSTATUS *pstatus)
183 struct dcesrv_handle *rpc_hnd = NULL;
184 void *data = NULL;
186 rpc_hnd = find_policy_by_hnd_internal(p, hnd, handle_type, &data);
187 if (rpc_hnd == NULL) {
188 *pstatus = NT_STATUS_INVALID_HANDLE;
189 return NULL;
192 *pstatus = NT_STATUS_OK;
193 return data;
196 /****************************************************************************
197 Close a policy.
198 ****************************************************************************/
200 bool close_policy_hnd(struct pipes_struct *p,
201 struct policy_handle *hnd)
203 struct dcesrv_handle *rpc_hnd = NULL;
205 rpc_hnd = find_policy_by_hnd_internal(p, hnd, DCESRV_HANDLE_ANY, NULL);
206 if (rpc_hnd == NULL) {
207 DEBUG(3, ("Error closing policy (policy not found)\n"));
208 return false;
211 TALLOC_FREE(rpc_hnd);
213 num_handles--;
215 return true;
218 /*******************************************************************
219 Shall we allow access to this rpc? Currently this function
220 implements the 'restrict anonymous' setting by denying access to
221 anonymous users if the restrict anonymous level is > 0. Further work
222 will be checking a security descriptor to determine whether a user
223 token has enough access to access the pipe.
224 ********************************************************************/
226 bool pipe_access_check(struct pipes_struct *p)
228 /* Don't let anonymous users access this RPC if restrict
229 anonymous > 0 */
231 if (lp_restrict_anonymous() > 0) {
233 /* schannel, so we must be ok */
234 if (p->pipe_bound &&
235 (p->auth.auth_type == DCERPC_AUTH_TYPE_SCHANNEL)) {
236 return True;
239 if (security_session_user_level(p->session_info, NULL) < SECURITY_USER) {
240 return False;
244 return True;