DSMPlugin.cpp
12.2 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002 Ultr@VNC Team Members. All Rights Reserved.
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the program is not available from the place from
// which you received this file, check
// http://ultravnc.sourceforge.net/
//
////////////////////////////////////////////////////////////////////////////
//
//
// DSMPlugin.cpp: implementation of the CDSMPlugin class.
//
//////////////////////////////////////////////////////////////////////
//
// This class is the interface between UltraVNC and plugins
// (third party dlls) that may be developed (written, exported and
// provided by authorized individuals - according to the law of their
// country) to alter/modify/process/encrypt rfb data streams between
// vnc viewer and vnc server.
//
// The goal here is not to design and develop an extensive, versatile
// and powerfull plugin system but to provide people a way
// to easely customize the VNC communication data between client and server.
//
// It handles the following tasks:
//
// - Listing of all plugins found in the vnc directory (dlls, with ".dsm" extension)
//
// - Loading of a given plugin
//
// - Interface between vnc and the plugin functions:
// - Init() Initialize the plugin
// - SetParams() Set Password, or key or other. If more than one param, uses ',' as separator
// - GetParams() Give Password, or key or other. If more than one param, uses ',' as separator
// - DescribePlugin() Give the Plugin ID string (Name, Author, Date, Version, FileName)
// - TransformBuffer() Tell the plugin to do it's transformation against the data in the buffer
// - RestoreBuffer() Tell the plugin to restore data to its original state
// - Shutdown() Cleanup and shutdown the plugin
// - FreeBuffer() Free a buffer used in the plugin
// - Reset()
//
// - Unloading of the current loaded plugin
//
// WARNING: For the moment, only ONE instance of this class must exist in Vncviewer and WinVNC
// Consequently, WinVNc will impose all its clients to use the same plugin. Maybe we'll
// improve that soon. It depends on the demand/production of DSM plugins.
#include <memory.h>
#include <stdio.h>
#include <string.h>
#include "DSMPlugin.h"
//
// Utils
//
BOOL MyStrToken(LPSTR szToken, LPSTR lpString, int nTokenNum, char cSep)
{
int i = 1;
while (i < nTokenNum)
{
while ( *lpString && (*lpString != cSep) &&(*lpString != '\0'))
{
lpString++;
}
i++;
lpString++;
}
while ((*lpString != cSep) && (*lpString != '\0'))
{
*szToken = *lpString;
szToken++;
lpString++;
}
*szToken = '\0' ;
if (( ! *lpString ) || (! *szToken)) return NULL;
return FALSE;
}
//
//
//
CDSMPlugin::CDSMPlugin()
{
m_fLoaded = false;
m_fEnabled = false;
m_lPassLen = 0;
m_pTransBuffer = NULL;
m_pRestBuffer = NULL;
sprintf(m_szPluginName, "Unknown");
sprintf(m_szPluginVersion, "0.0.0");
sprintf(m_szPluginDate, "12-12-2002");
sprintf(m_szPluginAuthor, "Someone");
sprintf(m_szPluginFileName, "Plugin.dsm"); // No path, just the filename
m_hPDll = NULL;
// Plugin's functions pointers init
STARTUP m_PStartup = NULL;
SHUTDOWN m_PShutdown = NULL;
SETPARAMS m_PSetParams = NULL;
GETPARAMS m_PGetParams = NULL;
TRANSFORMBUFFER m_PTransformBuffer = NULL;
RESTOREBUFFER m_PRestoreBuffer = NULL;
TRANSFORMBUFFER m_PFreeBuffer = NULL;
RESET m_PReset = NULL;
}
//
//
//
CDSMPlugin::~CDSMPlugin()
{
// TODO: Log events
if (IsLoaded())
UnloadPlugin();
}
//
//
//
void CDSMPlugin::SetEnabled(bool fEnable)
{
m_fEnabled = fEnable;
}
//
//
//
void CDSMPlugin::SetLoaded(bool fEnable)
{
m_fLoaded = fEnable;
}
//
//
//
char* CDSMPlugin::DescribePlugin(void)
{
// TODO: Log events
char* szDescription = NULL;
if (m_PDescription)
{
szDescription = (*m_PDescription)();
if (szDescription != NULL)
{
MyStrToken(m_szPluginName, szDescription, 1, ',');
MyStrToken(m_szPluginAuthor, szDescription, 2, ',');
MyStrToken(m_szPluginDate, szDescription, 3, ',');
MyStrToken(m_szPluginVersion, szDescription, 4, ',');
MyStrToken(m_szPluginFileName, szDescription, 5, ',');
}
return szDescription;
}
else
return "No Plugin loaded";
}
//
// Init the DSMPlugin system
//
bool CDSMPlugin::InitPlugin(void)
{
// TODO: Log events
int nRes = (*m_PStartup)();
if (nRes < 0) return false;
else return true;
}
//
// Reset the DSMPlugin
//
bool CDSMPlugin::ResetPlugin(void)
{
// TODO: Log events
int nRes = (*m_PReset)();
if (nRes < 0) return false;
else return true;
}
//
// szParams is the key (or password) that is transmitted to the loaded DSMplugin
//
bool CDSMPlugin::SetPluginParams(HWND hWnd, char* szParams)
{
// TODO: Log events
int nRes = (*m_PSetParams)(hWnd, szParams);
if (nRes > 0) return true; else return false;
}
//
// Return the loaded DSMplugin current param(s)
//
char* CDSMPlugin::GetPluginParams(void)
{
//
return (*m_PGetParams)();
}
//
// List all the plugins is the current APP directory in the given ComboBox
//
int CDSMPlugin::ListPlugins(HWND hComboBox)
{
// TODO: Log events
WIN32_FIND_DATA fd;
HANDLE ff;
int fRet = 1;
int nFiles = 0;
char szCurrentDir[MAX_PATH];
if (GetModuleFileName(NULL, szCurrentDir, MAX_PATH))
{
char* p = strrchr(szCurrentDir, '\\');
if (p == NULL)
return 0;
*p = '\0';
}
else
return 0;
// MessageBox(NULL, szCurrentDir, "Current directory", MB_OK);
if (szCurrentDir[strlen(szCurrentDir) - 1] != '\\') strcat(szCurrentDir, "\\");
strcat(szCurrentDir, "*.dsm"); // The DSMplugin dlls must have this extension
ff = FindFirstFile(szCurrentDir, &fd);
if (ff == INVALID_HANDLE_VALUE)
{
// Todo: Log error here
return 0;
}
while (fRet != 0)
{
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)(fd.cFileName));
nFiles++;
fRet = FindNextFile(ff, &fd);
}
FindClose(ff);
return nFiles;
}
//
// Load the given DSMplugin and map its functions
//
bool CDSMPlugin::LoadPlugin(char* szPlugin, bool fAllowMulti)
{
// sf@2003 - Multi dll trick
// I really don't like doing this kind of dirty workaround but I have no time to do
// better for now. Used only by a listening viewer.
// Create a numbered temporary copy of the original plugin dll and load it (viewer only, for now)
if (fAllowMulti)
{
bool fDllCopyCreated = false;
int i = 1;
char szDllCopyName[MAX_PATH];
char szCurrentDir[MAX_PATH];
char szCurrentDir_szPlugin[MAX_PATH];
char szCurrentDir_szDllCopyName[MAX_PATH];
while (!fDllCopyCreated)
{
strcpy(szDllCopyName, szPlugin);
szDllCopyName[strlen(szPlugin) - 4] = '\0'; //remove the ".dsm" extension
sprintf(szDllCopyName, "%s-tmp.d%d", szDllCopyName, i++);
fDllCopyCreated = (FALSE != CopyFile(szPlugin, szDllCopyName, false));
// Note: Let's be really dirty; Overwrite if it's possible only (dll not loaded).
// This way if for some reason (abnormal process termination) the dll wasn't previously
// normally deleted we overwrite/clean it with the new one at the same time.
strcpy (szCurrentDir_szDllCopyName,szDllCopyName);
DWORD error=GetLastError();
if (error==2)
{
if (GetModuleFileName(NULL, szCurrentDir, MAX_PATH))
{
char* p = strrchr(szCurrentDir, '\\');
*p = '\0';
}
strcpy (szCurrentDir_szPlugin,szCurrentDir);
strcat (szCurrentDir_szPlugin,"\\");
strcat (szCurrentDir_szPlugin,szPlugin);
strcpy (szCurrentDir_szDllCopyName,szCurrentDir);
strcat (szCurrentDir_szDllCopyName,"\\");
strcat (szCurrentDir_szDllCopyName,szDllCopyName);
fDllCopyCreated = (FALSE != CopyFile(szCurrentDir_szPlugin, szCurrentDir_szDllCopyName, false));
}
if (i > 99) break; // Just in case...
}
strcpy(m_szDllName, szDllCopyName);
m_hPDll = LoadLibrary(m_szDllName);
}
else // Use the original plugin dll
{
ZeroMemory(m_szDllName, strlen(m_szDllName));
m_hPDll = LoadLibrary(szPlugin);
//Try current PATH
if (m_hPDll==NULL)
{
char szCurrentDir[MAX_PATH];
char szCurrentDir_szPlugin[MAX_PATH];
if (GetModuleFileName(NULL, szCurrentDir, MAX_PATH))
{
char* p = strrchr(szCurrentDir, '\\');
*p = '\0';
}
strcpy (szCurrentDir_szPlugin,szCurrentDir);
strcat (szCurrentDir_szPlugin,"\\");
strcat (szCurrentDir_szPlugin,szPlugin);
m_hPDll = LoadLibrary(szCurrentDir_szPlugin);
}
}
if (m_hPDll == NULL) return false;
m_PDescription = (DESCRIPTION) GetProcAddress(m_hPDll, "Description");
m_PStartup = (STARTUP) GetProcAddress(m_hPDll, "Startup");
m_PShutdown = (SHUTDOWN) GetProcAddress(m_hPDll, "Shutdown");
m_PSetParams = (SETPARAMS) GetProcAddress(m_hPDll, "SetParams");
m_PGetParams = (GETPARAMS) GetProcAddress(m_hPDll, "GetParams");
m_PTransformBuffer = (TRANSFORMBUFFER) GetProcAddress(m_hPDll, "TransformBuffer");
m_PRestoreBuffer = (RESTOREBUFFER) GetProcAddress(m_hPDll, "RestoreBuffer");
m_PFreeBuffer = (FREEBUFFER) GetProcAddress(m_hPDll, "FreeBuffer");
m_PReset = (RESET) GetProcAddress(m_hPDll, "Reset");
if (m_PStartup == NULL || m_PShutdown == NULL || m_PSetParams == NULL || m_PGetParams == NULL
|| m_PTransformBuffer == NULL || m_PRestoreBuffer == NULL || m_PFreeBuffer == NULL)
{
FreeLibrary(m_hPDll);
if (*m_szDllName) DeleteFile(m_szDllName);
return false;
}
// return ((*m_PStartup)());
SetLoaded(true);
return true;
}
//
// Unload the current DSMPlugin from memory
//
bool CDSMPlugin::UnloadPlugin(void)
{
// TODO: Log events
// Force the DSMplugin to free the buffers it allocated
if (m_pTransBuffer != NULL) (*m_PFreeBuffer)(m_pTransBuffer);
if (m_pRestBuffer != NULL) (*m_PFreeBuffer)(m_pRestBuffer);
m_pTransBuffer = NULL;
m_pRestBuffer = NULL;
SetLoaded(false);
if ((*m_PShutdown)())
{
bool fFreed = false;
fFreed = (FALSE != FreeLibrary(m_hPDll));
if (*m_szDllName) DeleteFile(m_szDllName);
return fFreed;
}
else
return false;
}
//
// Tell the plugin to do its transformation on the source data buffer
// Return: pointer on the new transformed buffer (allocated by the plugin)
// nTransformedDataLen is the number of bytes contained in the transformed buffer
//
BYTE* CDSMPlugin::TransformBuffer(BYTE* pDataBuffer, int nDataLen, int* pnTransformedDataLen)
{
// FixME: possible pb with this mutex in WinVNC
omni_mutex_lock l(m_TransMutex);
m_pTransBuffer = (*m_PTransformBuffer)(pDataBuffer, nDataLen, pnTransformedDataLen);
return m_pTransBuffer;
}
// - If pRestoredDataBuffer = NULL, the plugin check its local buffer and return the pointer
// - Otherwise, restore data contained in its rest. buffer and put the result in pRestoredDataBuffer
// pnRestoredDataLen is the number bytes put in pRestoredDataBuffers
BYTE* CDSMPlugin::RestoreBufferStep1(BYTE* pRestoredDataBuffer, int nDataLen, int* pnRestoredDataLen)
{
//m_RestMutex.lock();
m_pRestBuffer = (*m_PRestoreBuffer)(pRestoredDataBuffer, nDataLen, pnRestoredDataLen);
return m_pRestBuffer;
}
BYTE* CDSMPlugin::RestoreBufferStep2(BYTE* pRestoredDataBuffer, int nDataLen, int* pnRestoredDataLen)
{
m_pRestBuffer = (*m_PRestoreBuffer)(pRestoredDataBuffer, nDataLen, pnRestoredDataLen);
//m_RestMutex.unlock();
return NULL;
}
void CDSMPlugin::RestoreBufferUnlock()
{
//m_RestMutex.unlock();
}