-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClientList.c
289 lines (239 loc) · 6.56 KB
/
ClientList.c
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
#include "ClientList.h"
#include "allocation.h"
#include "Streams.h"
#include "OnInjection.h"
#include "Injection.h"
#include "BinaryTree.h"
#include <windows.h>
#include <stdio.h>
#include "UAC.h"
void validate_clientlist();
BinaryTree * client_by_pid=0;
unsigned int refcount=0;
//ClientList is a passive list of clients
// passive, in that clients register their COM interface themselves with this client list
int pid_compare(unsigned int a, unsigned int b)
{
if(a<b)
return +1;
else if(a>b)
return -1;
else
return 0;
}
void ClientList_constructor(ClientList * pThis)
{
if(refcount==0)
{
//open console for debugging purposes <to be removed>
if(AllocConsole())
{
freopen("CONIN$","rb",stdin);
freopen("CONOUT$","wb",stdout);
freopen("CONOUT$","wb",stderr);
}
printf("clientlist\n");
//client list is maintained as a binary tree, with the client's PID as key
client_by_pid=BT_create((BTCompare)pid_compare);
}
//global refcount
//synchronization is not an issue, since we are on a SINGLE THREADED APPARTMENT
//<- ClientList lives on a seperate process (COM surrogate, typically dllhost.exe)
refcount++;
return;
}
void ClientList_destructor(ClientList * pThis)
{
VARIANT * curclient;
refcount--;
if(refcount==0)
{
while((client_by_pid->root)&&(curclient=(VARIANT *)client_by_pid->root->data))
{
BT_remove(client_by_pid, client_by_pid->root->key);
VariantClear(curclient);
clean(curclient);
}
BT_delete(client_by_pid);
// make sure dll host terminates
ForceUnload();
}
return;
}
COMCALL GetCount(ClientList * pThis, unsigned int * count)
{
//printf("requesting count!\n");
validate_clientlist();
(*count)=client_by_pid->itemcount;
return S_OK;
}
void validate_clientlist()
{
LinkedList * ll_clients;
unsigned int pid;
Process * ps;
VARIANT * curclient;
if(ll_clients = BT_keys(client_by_pid))
{
while(pid = (unsigned int)LL_pop(ll_clients))
{
if(ps=GetProcess(pid, MINIMAL_PROCESS_PERMISSIONS))
clean(ps);
else
{
/* invalid client in list */
curclient = (VARIANT *)BT_remove(client_by_pid, (void *)pid);
VariantClear((VARIANTARG *)curclient);
clean(curclient);
InternalAddRef(); /* client shut down without removing itself from the clientlist
this should imply the clientlist reference was never released
so we make this stale reference internal here to prevent a
memory leak */
}
}
LL_delete(ll_clients);
}
}
COMCALL GetClient(ClientList * pThis, long index, IUnknown ** client)
{
BinaryTreeEnum * btenum;
int i;
VARIANT * curclient;
HRESULT hr;
validate_clientlist();
//printf("finding client?\n");
curclient=0;
btenum=BT_newenum(client_by_pid);
i=0;
while((i<=index)&&(curclient=(VARIANT *)BT_next(btenum)))
i++;
BT_enumdelete(btenum);
//printf("client list looped, index=%u, curclient=%x!, curclient->pUnkVal=%x\n", index, curclient, curclient->punkVal);
if((i>index)&&(curclient))
{
//printf("querying interface\n");
hr=curclient->punkVal->lpVtbl->QueryInterface(curclient->punkVal, &IID_IClient, (void **)client);
//printf("interface query done\n");
/*if(hr!=S_OK)
printf("query interface returned %x\n", hr);*/
return hr;
}
else
{
//printf("client not found?\n");
(*client)=0;
return S_FALSE;
}
return hr;
}
COMCALL ClientListNewEnum(ClientList * pThis, IUnknown ** newenum)
{
LinkedList * clients;
validate_clientlist();
clients=BT_values(client_by_pid);
if((*newenum)=(IUnknown *)CreateLinkedListEnumerator(clients, LL_newenum(clients), 0, 2, (GUID *)&IID_IEnumVARIANT))
{
return S_OK;
}
else
{
(*newenum)=0;
return S_FALSE;
}
}
COMCALL RegisterClient(ClientList * pThis, IUnknown * pClientDisp)
{
DISPPARAMS * curstack;
VARIANT vpid;
VARIANT * prevclient;
COMClass * CLClass;
COMObject * curl;
BinaryTreeEnum * btenum;
curstack=DispStack(0);
//printf("registering client\n");
pClientDisp->lpVtbl->AddRef(pClientDisp);
if(DoPropGet(pClientDisp, 1, curstack, &vpid)==S_OK)
{
if(VariantChangeType(&vpid, &vpid, 0, VT_UI4)==S_OK)
{
//insert client by pid here
if(prevclient=(VARIANT *)BT_find(client_by_pid, (void *)vpid.ulVal))
{
BT_remove(client_by_pid, (void *)vpid.ulVal);
VariantClear(prevclient);
clean(prevclient);
}
BT_insert(client_by_pid, (void *)vpid.ulVal, (void *)VObject(pClientDisp));
//invoke onNewClient
if(CLClass=FindClass((GUID *)&CLSID_ClientList))
{
btenum=BT_newenum(CLClass->instances);
while(curl=(COMObject *)BT_next(btenum))
{
curstack=DispStack(1);
DispPush(curstack, VObject(pClientDisp));
InvokeEvent(curl, 1, curstack, 0);
}
BT_enumdelete(btenum);
}
VariantClear(&vpid);//<- not required
}
else
printf("change type failed!\n");
}
else
printf("do get prop on client object failed!\n");
pClientDisp->lpVtbl->Release(pClientDisp);
return S_OK;
}
COMCALL UnregisterClient(ClientList * pThis, IUnknown * pClientDisp)
{
DISPPARAMS * curstack;
VARIANT vpid;
VARIANT * prevclient;
COMClass * CLClass;
BinaryTreeEnum * btenum;
COMObject * curl;
printf("unregistering client\n");
curstack=DispStack(0);
if(DoPropGet(pClientDisp, 1, curstack, &vpid)==S_OK)
{
if(VariantChangeType(&vpid, &vpid, 0, VT_UI4)==S_OK)
{
//remove client by pid here
if(prevclient=(VARIANT *)BT_find(client_by_pid, (void *)vpid.ulVal))
{
//invoke clientlist->onclientclose event
if(CLClass=FindClass((GUID *)&CLSID_ClientList))
{
btenum=BT_newenum(CLClass->instances);
while(curl=(COMObject *)BT_next(btenum))
{
curstack=DispStack(1);
DispPush(curstack, VObject(pClientDisp));
InvokeEvent(curl, 2, curstack, 0);
}
BT_enumdelete(btenum);
}
BT_remove(client_by_pid, (void *)vpid.ulVal);
VariantClear(prevclient);
clean(prevclient);
}
VariantClear(&vpid);//<- not required
}
}
pClientDisp->lpVtbl->Release(pClientDisp);
return S_OK;
}
COMCALL FindClient(ClientList * pThis, unsigned int pid, IUnknown ** client, VARIANT_BOOL * bSuccess)
{
VARIANT * prevclient;
validate_clientlist();
(*bSuccess)=VARIANT_FALSE;
if(prevclient=(VARIANT *)BT_find(client_by_pid, (void *)pid))
{
if( prevclient->punkVal->lpVtbl->QueryInterface(prevclient->punkVal, &IID_IClient, (void **)client) == S_OK )
(*bSuccess)=VARIANT_TRUE;
}
return S_OK;
}