1 /*
2  * reactos/apps/lpc/creport.c
3  *
4  * To be run in a real WNT 4.0 system to
5  * create an LPC named port.
6  *
7  * Use Russinovich' HandleEx to verify
8  * creport.exe owns the named LPC port
9  * you asked to create.
10  */
11 #include <windows.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #define PROTO_LPC
15 #include <ddk/ntddk.h>
16 #include "dumpinfo.h"
17 
18 #define LPC_CONNECT_FLAG1 0x00000001
19 #define LPC_CONNECT_FLAG2 0x00000010
20 #define LPC_CONNECT_FLAG3 0x00000100
21 #define LPC_CONNECT_FLAG4 0x00001000
22 #define LPC_CONNECT_FLAG5 0x00010000
23 
24 NTSTATUS
25 (WINAPI * CreatePort)(
26 	/*OUT	PHANDLE			PortHandle,*/
27 	PVOID	Buffer,
28 	IN	POBJECT_ATTRIBUTES	PortAttributes	OPTIONAL,
29 	IN	ACCESS_MASK		DesiredAccess,
30 	IN	DWORD			Unknown3,
31 	IN	ULONG			Flags
32 	);
33 
34 NTSTATUS
35 (WINAPI * QueryObject)(
36 	IN	HANDLE	ObjectHandle,
37 	IN	CINT	ObjectInformationClass,
38 	OUT	PVOID	ObjectInformation,
39 	IN	ULONG	Length,
40 	OUT	PULONG	ResultLength
41 	);
42 
43 NTSTATUS
44 (WINAPI * YieldExecution)(VOID);
45 
46 #define BUF_SIZE 1024
47 #define MAXARG   5000000
48 
49 
50 VOID
51 TryCreatePort(char *port_name)
52 {
53 	DWORD			Status = 0;
54 	HANDLE			Port = 0;
55 	int			i;
56 	UNICODE_STRING		PortName;
57 	OBJECT_ATTRIBUTES	ObjectAttributes;
58 	WORD			Name [BUF_SIZE] = {0};
59 	int			dwx = 0;
60 	char			* port_name_save = port_name;
61 
62 	/*
63 	 * Convert the port's name to Unicode.
64 	 */
65 	for (
66 		PortName.Length = 0;
67 		(	*port_name
68 			&& (PortName.Length < BUF_SIZE)
69 			);
70 		)
71 	{
72 		Name[PortName.Length++] = (WORD) *port_name++;
73 	}
74 	Name[PortName.Length] = 0;
75 
76 	PortName.Length = PortName.Length * sizeof (WORD);
77 	PortName.MaximumLength = PortName.Length + sizeof (WORD);
78 	PortName.Buffer = (PWSTR) Name;
79 	/*
80 	 * Prepare the port object attributes.
81 	 */
82 	ObjectAttributes.Length =
83 		sizeof (OBJECT_ATTRIBUTES);
84 	ObjectAttributes.RootDirectory =
85 		NULL;
86 	ObjectAttributes.ObjectName =
87 		& PortName;
88 	ObjectAttributes.Attributes =
89 		0; //OBJ_CASE_INSENSITIVE --> STATUS_INVALID_PARAMETER ==> case sensitive!;
90 	ObjectAttributes.SecurityDescriptor =
91 		NULL;
92 	ObjectAttributes.SecurityQualityOfService =
93 		NULL;
94 	/*
95 	 * Try to issue a connection request.
96 	 */
97 	Port = 0;
98 	Status = CreatePort(
99 			& Port,
100 			& ObjectAttributes,
101 			0, /* ACCESS_MASK? */
102 			0, /* Unknown3 */
103 			LPC_CONNECT_FLAG5
104 			);
105 	if (Status == STATUS_SUCCESS)
106 	{
107 		DumpInfo(
108 			Name,
109 			Status,
110 			"created",
111 			Port
112 			);
113 		/* Hot waiting */
114 		for (dwx=0; dwx<MAXARG; ++dwx)
115 		{
116 			YieldExecution();
117 		}
118 		if (FALSE == CloseHandle(Port))
119 		{
120 			printf(
121 				"Could not close the port handle %08X.\n",
122 				Port
123 				);
124 		}
125 		return;
126 	}
127 	printf(
128 		"Creating port \"%s\" failed (Status = %08X).\n",
129 		port_name_save,
130 		Status
131 		);
132 }
133 
134 
135 main( int argc, char * argv[] )
136 {
137 	HINSTANCE ntdll;
138 
139 	if (argc != 2)
140 	{
141 		printf("WNT LPC Port Creator\n");
142 		printf("Usage: %s [port_name]\n",argv[0]);
143 		exit(EXIT_FAILURE);
144 	}
145 	printf("LoadLibrary(NTDLL)\n");
146 	ntdll = LoadLibrary("NTDLL");
147 	if (ntdll == NULL)
148 	{
149 		printf("Could not load NTDLL\n");
150 		return EXIT_FAILURE;
151 	}
152 	printf("GetProcAddress(NTDLL.NtCreatePort)\n");
153 	CreatePort = (VOID*) GetProcAddress(
154 					ntdll,
155 					"NtCreatePort"
156 					);
157 	if (CreatePort == NULL)
158 	{
159 		FreeLibrary(ntdll);
160 		printf("Could not find NTDLL.NtCreatePort\n");
161 		return EXIT_FAILURE;
162 	}
163 	printf("GetProcAddress(NTDLL.NtQueryObject)\n");
164 	QueryObject = (VOID*) GetProcAddress(
165 					ntdll,
166 					"NtQueryObject"
167 					);
168 	if (QueryObject == NULL)
169 	{
170 		FreeLibrary(ntdll);
171 		printf("Could not find NTDLL.NtQueryObject\n");
172 		return EXIT_FAILURE;
173 	}
174 	printf("GetProcAddress(NTDLL.NtYieldExecution)\n");
175 	YieldExecution = (VOID*) GetProcAddress(
176 					ntdll,
177 					"NtYieldExecution"
178 					);
179 	if (YieldExecution == NULL)
180 	{
181 		FreeLibrary(ntdll);
182 		printf("Could not find NTDLL.NtYieldExecution\n");
183 		return EXIT_FAILURE;
184 	}
185 	printf("TryCreatePort(%s)\n",argv[1]);
186 	TryCreatePort(argv[1]);
187 	printf("Done\n");
188 	return EXIT_SUCCESS;
189 }
190 
191 /* EOF */
192