summaryrefslogtreecommitdiffhomepage
path: root/src/daemon/ntapi_dsr_internal_connection.c
blob: 7246eff2889b36af686f04a71d92b4e90145cbd4 (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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2016  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <ntapi/nt_atomic.h>
#include <ntapi/nt_status.h>
#include <ntapi/nt_thread.h>
#include <ntapi/nt_port.h>
#include <ntapi/nt_daemon.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

static int32_t __stdcall __ntapi_dsr_connect_internal_client_exit(
	nt_daemon_params *	params,
	int32_t			status)
{
	at_store_32(
		&params->exit_code_daemon_start,
		status);

	if (status == NT_STATUS_SUCCESS)
		at_store_32(
			&params->exit_code_daemon_loop,
			NT_STATUS_SERVICE_NOTIFICATION);

	return status;
}

static int32_t __stdcall __ntapi_dsr_internal_client_connect_exit(
	nt_daemon_params *	params,
	int32_t			status)
{
	at_store_32(
		&params->exit_code_internal_client,
		status);

	return __ntapi->zw_terminate_thread(
		NT_CURRENT_THREAD_HANDLE,
		status);
}

/* __ntapi_dsr_connect_internal_client executes in the daemon's dedicated thread */
int32_t __stdcall __ntapi_dsr_connect_internal_client(nt_daemon_params * params)
{
	int32_t			status;
	intptr_t		port_id;
	nt_port_message		port_msg;
	nt_large_integer	timeout;
	void *			_hport_client;

	/* report state */
	at_store_32(
		&params->exit_code_daemon_start,
		NT_STATUS_MORE_PROCESSING_REQUIRED);

	/* timeout-enabled first connection */
	timeout.quad = NT_DSR_INIT_MAX_WAIT;

	if ((status = __ntapi->zw_reply_wait_receive_port_ex(
			params->hport_daemon,
			&port_id,
			(nt_port_message *)0,
			(nt_port_message *)&port_msg,
			&timeout)))
		return __ntapi_dsr_connect_internal_client_exit(
			params,status);

	/* the internal client must be first */
	if (port_msg.client_id.process_id != pe_get_current_process_id())
		return NT_STATUS_PORT_CONNECTION_REFUSED;

	/* accept connection request */
	if ((status = __ntapi->zw_accept_connect_port(
			&_hport_client,
			port_msg.client_id.process_id,
			(nt_port_message *)&port_msg,
			NT_LPC_ACCEPT_CONNECTION,
			(nt_port_section_write *)0,
			(nt_port_section_read *)0)))
		return __ntapi_dsr_connect_internal_client_exit(
			params,status);

	/* finalize connection */
	status = __ntapi->zw_complete_connect_port(
		_hport_client);

	return __ntapi_dsr_connect_internal_client_exit(
		params,status);
}


/* __ntapi_dsr_internal_client_connect executes in its own temporary thread */
int32_t __stdcall __ntapi_dsr_internal_client_connect(nt_daemon_params * params)
{
	int32_t				status;
	nt_unicode_string		server_name;
	nt_object_attributes		oa;
	nt_security_quality_of_service	sqos;

	/* report status */
	at_store_32(
		&params->exit_code_internal_client,
		NT_STATUS_MORE_PROCESSING_REQUIRED);

	/* init server_name */
	server_name.strlen = (uint16_t)__ntapi->tt_string_null_offset_short((const int16_t *)params->port_name);
	server_name.maxlen = 0;
	server_name.buffer = (uint16_t *)params->port_name;

	/* init security structure */
	sqos.length 			= sizeof(sqos);
	sqos.impersonation_level	= NT_SECURITY_IMPERSONATION;
	sqos.context_tracking_mode	= NT_SECURITY_TRACKING_DYNAMIC;
	sqos.effective_only		= 1;

	/* init the port's object attributes */
	oa.len		= sizeof(oa);
	oa.root_dir	= (void *)0;
	oa.obj_name	= &server_name;
	oa.obj_attr	= 0;
	oa.sec_desc	= (nt_security_descriptor *)0;
	oa.sec_qos	= &sqos;

	/* establish internal connection */
	if ((status = __ntapi->zw_connect_port(
			&params->hport_internal_client,
			&server_name,
			&sqos,
			0,0,0,0,0)))
		return __ntapi_dsr_internal_client_connect_exit(
			params,status);

	/* return port info */
	if (params->pport_internal_client)
		*(params->pport_internal_client) = params->hport_internal_client;

	/* signal the 'internal-client-is-ready' event */
	status = __ntapi->zw_set_event(
		params->hevent_internal_client_ready,
		0);

	/* exit the task-specific thread */
	return __ntapi_dsr_internal_client_connect_exit(
		params,status);
}