summaryrefslogtreecommitdiff
path: root/librpc/rpc/dcesrv_handles.c
blob: a87921ce740abbe16bd2d592c63b8442f3efd761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
   Unix SMB/CIFS implementation.

   server side dcerpc handle code

   Copyright (C) Andrew Tridgell 2003

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "lib/util/dlinklist.h"
#include "rpc_server/dcerpc_server.h"
#include "libcli/security/security.h"
#include "librpc/gen_ndr/auth.h"

/*
  destroy a rpc handle
*/
static int dcesrv_handle_destructor(struct dcesrv_handle *h)
{
	DLIST_REMOVE(h->assoc_group->handles, h);
	return 0;
}


/*
  allocate a new rpc handle
*/
_PUBLIC_
struct dcesrv_handle *dcesrv_handle_create(struct dcesrv_call_state *call,
					   uint8_t handle_type)
{
	struct dcesrv_connection_context *context = call->context;
	struct auth_session_info *session_info =
		dcesrv_call_session_info(call);
	struct dcesrv_handle *h;
	struct dom_sid *sid;

	/*
	 * For simplicity, ensure we abort here for an interface that
	 * has no handles (programmer error)
	 */
	SMB_ASSERT((context->iface->flags & DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED) == 0);

	sid = &session_info->security_token->sids[PRIMARY_USER_SID_INDEX];

	h = talloc_zero(context->conn->assoc_group, struct dcesrv_handle);
	if (!h) {
		return NULL;
	}
	h->data = NULL;
	h->sid = dom_sid_dup(h, sid);
	if (h->sid == NULL) {
		talloc_free(h);
		return NULL;
	}
	h->min_auth_level = call->auth_state->auth_level;
	h->assoc_group = context->conn->assoc_group;
	h->iface = context->iface;
	h->wire_handle.handle_type = handle_type;
	h->wire_handle.uuid = GUID_random();

	DLIST_ADD(context->conn->assoc_group->handles, h);

	talloc_set_destructor(h, dcesrv_handle_destructor);

	return h;
}

/**
  find an internal handle given a wire handle. If the wire handle is NULL then
  allocate a new handle
*/

_PUBLIC_
struct dcesrv_handle *dcesrv_handle_lookup(struct dcesrv_call_state *call,
					   const struct policy_handle *p,
					   uint8_t handle_type)
{
	struct dcesrv_connection_context *context = call->context;
	struct auth_session_info *session_info =
		dcesrv_call_session_info(call);
	struct dcesrv_handle *h;
	struct dom_sid *sid;

	/*
	 * For simplicity, ensure we abort here for an interface that
	 * has no handles (programmer error)
	 */
	SMB_ASSERT((context->iface->flags & DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED) == 0);

	sid = &session_info->security_token->sids[PRIMARY_USER_SID_INDEX];

	if (ndr_policy_handle_empty(p)) {
		/* TODO: we should probably return a NULL handle here */
		return dcesrv_handle_create(call, handle_type);
	}

	for (h=context->conn->assoc_group->handles; h; h=h->next) {
		if (h->wire_handle.handle_type == p->handle_type &&
		    GUID_equal(&p->uuid, &h->wire_handle.uuid)) {
			if (handle_type != DCESRV_HANDLE_ANY &&
			    p->handle_type != handle_type) {
				DEBUG(0,("client gave us the wrong handle type (%d should be %d)\n",
					 p->handle_type, handle_type));
				return NULL;
			}
			if (!dom_sid_equal(h->sid, sid)) {
				struct dom_sid_buf buf1, buf2;
				DBG_ERR("Attempt to use invalid sid %s - %s\n",
					dom_sid_str_buf(h->sid, &buf1),
					dom_sid_str_buf(sid, &buf2));
				return NULL;
			}
			if (call->auth_state->auth_level < h->min_auth_level) {
				DEBUG(0,(__location__ ": Attempt to use invalid auth_level %u < %u\n",
					 call->auth_state->auth_level,
					 h->min_auth_level));
				return NULL;
			}
			if (h->iface != context->iface) {
				DEBUG(0,(__location__ ": Attempt to use invalid iface\n"));
				return NULL;
			}
			return h;
		}
	}

	return NULL;
}

struct dcesrv_iface_state {
	struct dcesrv_iface_state *prev, *next;
	struct dcesrv_assoc_group *assoc;
	const struct dcesrv_interface *iface;
	struct dom_sid owner;
	const struct dcesrv_connection *conn;
	const struct dcesrv_auth *auth;
	const struct dcesrv_connection_context *pres;
	uint64_t magic;
	void *ptr;
	const char *location;
};

static int dcesrv_iface_state_destructor(struct dcesrv_iface_state *istate)
{
	DLIST_REMOVE(istate->assoc->iface_states, istate);
	return 0;
}

static void *dcesrv_iface_state_find(struct dcesrv_assoc_group *assoc,
			const struct dcesrv_interface *iface,
			const struct dom_sid *owner,
			const struct dcesrv_connection *conn,
			const struct dcesrv_auth *auth,
			const struct dcesrv_connection_context *pres,
			uint64_t magic,
			const void *ptr)
{
	struct dcesrv_iface_state *cur = NULL;

	for (cur = assoc->iface_states; cur != NULL; cur = cur->next) {
		bool match;

		SMB_ASSERT(cur->assoc == assoc);

		if (cur->ptr == ptr) {
			return cur->ptr;
		}

		if (cur->iface != iface) {
			continue;
		}

		match = dom_sid_equal(&cur->owner, owner);
		if (!match) {
			continue;
		}

		if (cur->conn != conn) {
			continue;
		}

		if (cur->auth != auth) {
			continue;
		}

		if (cur->pres != pres) {
			continue;
		}

		if (cur->magic != magic) {
			continue;
		}

		return cur->ptr;
	}

	return NULL;
}

static NTSTATUS dcesrv_iface_state_store(struct dcesrv_assoc_group *assoc,
				const struct dcesrv_interface *iface,
				const struct dom_sid *owner,
				const struct dcesrv_connection *conn,
				const struct dcesrv_auth *auth,
				const struct dcesrv_connection_context *pres,
				uint64_t magic,
				TALLOC_CTX *mem_ctx,
				void *ptr,
				const char *location)
{
	struct dcesrv_iface_state *istate = NULL;
	void *optr = NULL;

	optr = dcesrv_iface_state_find(assoc,
				       iface,
				       owner,
				       conn,
				       auth,
				       pres,
				       magic,
				       ptr);
	if (optr != NULL) {
		return NT_STATUS_OBJECTID_EXISTS;
	}

	istate = talloc_zero(ptr, struct dcesrv_iface_state);
	if (istate == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	*istate = (struct dcesrv_iface_state) {
		.assoc = assoc,
		.iface = iface,
		.owner = *owner,
		.conn  = conn,
		.auth  = auth,
		.pres  = pres,
		.magic = magic,
		.location = location,
	};

	istate->ptr = talloc_steal(mem_ctx, ptr);

	talloc_set_destructor(istate, dcesrv_iface_state_destructor);

	DLIST_ADD_END(assoc->iface_states, istate);

	return NT_STATUS_OK;
}

NTSTATUS _dcesrv_iface_state_store_assoc(struct dcesrv_call_state *call,
				uint64_t magic,
				void *ptr,
				const char *location)
{
	struct auth_session_info *session_info =
		dcesrv_call_session_info(call);
	const struct dom_sid *owner =
		&session_info->security_token->sids[0];
	NTSTATUS status;

	status = dcesrv_iface_state_store(call->conn->assoc_group,
					  call->context->iface,
					  owner,
					  NULL, /* conn */
					  NULL, /* auth */
					  NULL, /* pres */
					  magic,
					  call->conn->assoc_group, /* mem_ctx */
					  ptr,
					  location);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	return NT_STATUS_OK;
}

void *_dcesrv_iface_state_find_assoc(struct dcesrv_call_state *call, uint64_t magic)
{
	struct auth_session_info *session_info =
		dcesrv_call_session_info(call);
	const struct dom_sid *owner =
		&session_info->security_token->sids[0];
	void *ptr = NULL;

	ptr = dcesrv_iface_state_find(call->conn->assoc_group,
				      call->context->iface,
				      owner,
				      NULL, /* conn */
				      NULL, /* auth */
				      NULL, /* pres */
				      magic,
				      NULL); /* ptr */
	if (ptr == NULL) {
		return NULL;
	}

	return ptr;
}

NTSTATUS _dcesrv_iface_state_store_conn(struct dcesrv_call_state *call,
					uint64_t magic,
					void *ptr,
					const char *location)
{
	struct auth_session_info *session_info =
		dcesrv_call_session_info(call);
	const struct dom_sid *owner =
		&session_info->security_token->sids[0];
	NTSTATUS status;

	status = dcesrv_iface_state_store(call->conn->assoc_group,
					  call->context->iface,
					  owner,
					  call->conn,
					  call->auth_state,
					  call->context,
					  magic,
					  call->conn, /* mem_ctx */
					  ptr,
					  location);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	return NT_STATUS_OK;
}

void *_dcesrv_iface_state_find_conn(struct dcesrv_call_state *call, uint64_t magic)
{
	struct auth_session_info *session_info =
		dcesrv_call_session_info(call);
	const struct dom_sid *owner =
		&session_info->security_token->sids[0];
	void *ptr = NULL;

	ptr = dcesrv_iface_state_find(call->conn->assoc_group,
				      call->context->iface,
				      owner,
				      call->conn,
				      call->auth_state,
				      call->context,
				      magic,
				      NULL); /* ptr */
	if (ptr == NULL) {
		return NULL;
	}

	return ptr;
}