forked from negrutiu/nsis-nscurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
601 lines (484 loc) · 13.5 KB
/
main.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//? Marius Negrutiu ([email protected]) :: 2014/01/19
#include "main.h"
#include "curl.h"
#include "utils.h"
#include "crypto.h"
#include "queue.h"
#include "gui.h"
HINSTANCE g_hInst = NULL;
HANDLE g_hTerm = NULL;
extra_parameters *g_ep = NULL;
HWND g_hwndparent = NULL;
//++ PluginInit
//? Called at DLL_PROCESS_ATTACH
BOOL PluginInit( _In_ HINSTANCE hInst )
{
if (!g_hInst) {
TRACE( _T( "%hs\n" ), __FUNCTION__ );
#if defined(_DEBUG)
MessageBox(NULL, TEXT("Waiting debugger to attach..."), TEXT("Waiting debugger to attach..."), MB_OK | MB_ICONEXCLAMATION);
#endif
g_hInst = hInst;
g_hTerm = CreateEvent( NULL, TRUE, FALSE, NULL );
assert( g_hTerm );
UtilsInitialize();
CurlInitialize();
QueueInitialize();
GuiInitialize();
return TRUE;
}
return FALSE;
}
//++ PluginUninit
//? Called at DLL_PROCESS_DETACH
BOOL PluginUninit()
{
if ( g_hInst ) {
TRACE( _T( "%hs\n" ), __FUNCTION__ );
SetEvent( g_hTerm );
GuiDestroy();
QueueDestroy();
CurlDestroy();
UtilsDestroy();
CloseHandle( g_hTerm );
g_hInst = NULL;
return TRUE;
}
return FALSE;
}
//++ UnloadCallback
//+ USAGE: extra->RegisterPluginCallback( g_hInst, UnloadCallback );
//? By registering UnloadCallback, the framework will keep the current plugin locked in memory
//? Otherwise, unless the caller specifies the /NOUNLOAD parameter the plugin gets unloaded when the current call returns
UINT_PTR __cdecl UnloadCallback( enum NSPIM iMessage )
{
switch ( iMessage ) {
case NSPIM_GUIUNLOAD: {
TRACE( _T( "%hs( NSPIM_GUIUNLOAD )\n" ), __FUNCTION__ );
break;
}
case NSPIM_UNLOAD: {
TRACE( _T( "%hs( NSPIM_UNLOAD )\n" ), __FUNCTION__ );
PluginUninit();
break;
}
}
return 0;
}
#if !defined(NO_HASH_EXPORTS)
//++ [exported] md5 [(string|file|memory)] <data>
EXTERN_C __declspec(dllexport)
void __cdecl md5( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
LPTSTR psz;
IDATA data;
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
psz = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) );
assert( psz );
psz[0] = 0;
if (popstring( psz ) == NOERROR) {
UCHAR hash[16];
if (IDataParseParam( psz, string_size, &data )) {
if (Hash( &data, hash, NULL, NULL ) == ERROR_SUCCESS) {
MyFormatBinaryHex( hash, sizeof( hash ), psz, string_size );
} else {
psz[0] = 0;
}
IDataDestroy( &data );
} else {
psz[0] = 0;
}
}
pushstringEx( psz );
MyFree( psz );
}
//++ [exported] sha1 [(string|file|memory)] <data>
EXTERN_C __declspec(dllexport)
void __cdecl sha1( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
LPTSTR psz;
IDATA data;
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
psz = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) );
assert( psz );
psz[0] = 0;
if (popstring( psz ) == NOERROR) {
UCHAR hash[20];
if (IDataParseParam( psz, string_size, &data )) {
if (Hash( &data, NULL, hash, NULL ) == ERROR_SUCCESS) {
MyFormatBinaryHex( hash, sizeof( hash ), psz, string_size );
} else {
psz[0] = 0;
}
IDataDestroy( &data );
} else {
psz[0] = 0;
}
}
pushstringEx( psz );
MyFree( psz );
}
//++ [exported] sha256 [(string|file|memory)] <data>
EXTERN_C __declspec(dllexport)
void __cdecl sha256( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
LPTSTR psz;
IDATA data;
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
psz = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) );
assert( psz );
psz[0] = 0;
if (popstring( psz ) == NOERROR) {
UCHAR hash[32];
if (IDataParseParam( psz, string_size, &data )) {
if (Hash( &data, NULL, NULL, hash ) == ERROR_SUCCESS) {
MyFormatBinaryHex( hash, sizeof( hash ), psz, string_size );
} else {
psz[0] = 0;
}
IDataDestroy( &data );
} else {
psz[0] = 0;
}
}
pushstringEx( psz );
MyFree( psz );
}
#endif
#if !defined(NO_COMPAT)
//++ [exported] echo [param1]..[paramN]
EXTERN_C __declspec(dllexport)
void __cdecl echo( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
LPTSTR psz, psz2;
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
psz = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) );
psz2 = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) );
assert( psz && psz2 );
psz[0] = 0;
for (;;) {
if (popstring( psz2 ) != NOERROR)
break;
if (*psz)
_tcscat( psz, _T( " " ) );
_tcscat( psz, psz2 );
if (lstrcmpi( psz2, _T( "/END" ) ) == 0)
break;
}
pushstringEx( psz );
MyFree( psz );
MyFree( psz2 );
}
#endif
//++ [exported] http <parameters> /END
EXTERN_C __declspec(dllexport)
#if defined(NO_COMPAT)
void __cdecl perform( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
#else
void __cdecl http( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
#endif
{
LPTSTR psz = NULL;
PCURL_REQUEST pReq = NULL;
PGUI_REQUEST pGui = NULL;
EXDLL_INIT();
EXDLL_VALIDATE();
if (!extra) return;
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
// Lock the plugin in memory (against NSIS framework trying to unload it)
extra->RegisterPluginCallback( g_hInst, UnloadCallback );
// Lock the plugin in memory (against Windows itself trying to FreeLibrary it)
// Once curl_global_init gets called, the current module must never unload
CurlInitializeLibcurl();
// Working structures
psz = (LPTSTR)MyAlloc( string_size * sizeof(TCHAR) );
pReq = (PCURL_REQUEST)MyAlloc( sizeof( *pReq ) );
pGui = (PGUI_REQUEST)MyAlloc( sizeof( *pGui ) );
if (psz && pReq && pGui) {
ULONG i;
CurlRequestInit( pReq );
GuiRequestInit( pGui );
// Parameters
for (i = 0; ; i++) {
if (popstring( psz ) != 0)
break;
if (lstrcmpi( psz, _T( "/END" ) ) == 0)
break;
if (!CurlParseRequestParam( i, psz, string_size, pReq ) &&
!GuiParseRequestParam( psz, string_size, pGui ))
{
TRACE( _T( " [!] Unknown parameter \"%s\"\n" ), psz );
assert( !"Unknown parameter" );
}
}
// Append to the queue
if (QueueAdd( pReq ) == ERROR_SUCCESS) {
// Wait for this particular ID
pGui->qsel.iId = pReq->Queue.iId;
pGui->qsel.pszTag = NULL;
// Wait
// In /background mode the call returns immediately
GuiWait( pGui, psz, string_size );
pushstringEx( psz );
// Don't destroy pReq
pReq = NULL;
} else {
pushstringEx( _T( "Error" ) );
}
} else {
pushstringEx( _T( "Error" ) );
}
if (pReq) {
CurlRequestDestroy( pReq );
MyFree( pReq );
}
if (pGui) {
GuiRequestDestroy( pGui );
MyFree( pGui );
}
MyFree( psz );
}
#if !defined(NO_MISC)
//++ [exported] wait [/ID id] [/TAG tag] parameters /END
EXTERN_C __declspec(dllexport)
void __cdecl wait( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
LPTSTR psz = NULL;
PGUI_REQUEST pGui = NULL;
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
// Working structures
psz = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) );
pGui = (PGUI_REQUEST)MyAlloc( sizeof( *pGui ) );
if (psz && pGui) {
// Parameters
GuiRequestInit( pGui );
for (;;) {
if (popstring( psz ) != NOERROR)
break;
if (lstrcmpi( psz, _T( "/END" ) ) == 0)
break;
if (lstrcmpi( psz, _T( "/ID" ) ) == 0) {
pGui->qsel.iId = (ULONG)popintptr();
if (pGui->qsel.iId == 0)
pGui->qsel.iId = 0xffffffff; // id=0 would match all IDs. We want no match
} else if (lstrcmpi( psz, _T( "/TAG" ) ) == 0) {
if (popstring( psz ) == NOERROR) {
MyFree( pGui->qsel.pszTag );
pGui->qsel.pszTag = MyStrDup( eT2A, psz );
}
} else if (!GuiParseRequestParam( psz, string_size, pGui )) {
TRACE( _T( " [!] Unknown parameter \"%s\"\n" ), psz );
assert( !"Unknown parameter" );
}
}
// Wait
GuiWait( pGui, psz, string_size );
}
GuiRequestDestroy( pGui );
MyFree( pGui );
MyFree( psz );
}
//++ [exported] query [/ID id] [/TAG tag] "query_string"
EXTERN_C __declspec(dllexport)
void __cdecl query( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
ULONG e;
LPTSTR psz = NULL;
QUEUE_SELECTION qsel = {0};
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
// Working buffer
psz = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) );
assert( psz );
if (psz) {
while ((e = popstring( psz )) == NOERROR) {
if (e == NOERROR && lstrcmpi( psz, _T( "/ID" ) ) == 0) {
qsel.iId = (ULONG)popintptr();
if (qsel.iId == 0)
qsel.iId = 0xffffffff; // id=0 would match all IDs. We want no match
} else if (e == NOERROR && lstrcmpi( psz, _T( "/TAG" ) ) == 0) {
if ((e = popstring( psz )) == NOERROR) {
MyFree( qsel.pszTag );
qsel.pszTag = MyStrDup( eT2A, psz );
}
} else {
break;
}
}
if (e == NOERROR) {
QueueQuery( &qsel, psz, string_size );
pushstringEx( psz );
} else {
pushstringEx( _T( "" ) );
}
}
MyFree( psz );
MyFree( qsel.pszTag );
}
//++ [exported] cancel [/ID id] [/TAG tag] [/REMOVE]
EXTERN_C __declspec(dllexport)
void __cdecl cancel( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
ULONG e;
LPTSTR psz = NULL;
QUEUE_SELECTION qsel = {0};
BOOLEAN bRemove = FALSE;
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
// Working buffer
psz = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) );
assert( psz );
if (psz) {
while ((e = popstring( psz )) == NOERROR) {
if (e == NOERROR && lstrcmpi( psz, _T( "/ID" ) ) == 0) {
qsel.iId = (ULONG)popintptr();
if (qsel.iId == 0)
qsel.iId = 0xffffffff; // id=0 would match all IDs. We want no match
} else if (e == NOERROR && lstrcmpi( psz, _T( "/TAG" ) ) == 0) {
if ((e = popstring( psz )) == NOERROR) {
MyFree( qsel.pszTag );
qsel.pszTag = MyStrDup( eT2A, psz );
}
} else if (e == NOERROR && lstrcmpi( psz, _T( "/REMOVE" ) ) == 0) {
bRemove = TRUE;
} else {
break;
}
}
if (bRemove) {
// Abort + Wait + Remove
QueueRemove( &qsel );
} else {
// Abort only
QueueAbort( &qsel );
}
}
MyFree( psz );
MyFree( qsel.pszTag );
}
//++ [exported] enumerate [/STATUS s1]..[/STATUS sN] [/TAG tag] /END
EXTERN_C __declspec(dllexport)
void __cdecl enumerate( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
LPTSTR psz = NULL;
QUEUE_SELECTION qsel = {0};
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
if ((psz = (LPTSTR)MyAlloc( string_size * sizeof( TCHAR ) )) != NULL) {
BOOLEAN bWaiting = FALSE, bRunning = FALSE, bComplete = FALSE;
struct curl_slist *sl = NULL;
for (;;)
{
if (popstring( psz ) != NOERROR)
break;
if (lstrcmpi( psz, _T( "/END" ) ) == 0)
break;
if (lstrcmpi( psz, _T( "/STATUS" ) ) == 0) {
if (popstring( psz ) == NOERROR) {
if (lstrcmpi( psz, _T( "Waiting" ) ) == 0) {
bWaiting = TRUE;
} else if (lstrcmpi( psz, _T( "Running" ) ) == 0) {
bRunning = TRUE;
} else if (lstrcmpi( psz, _T( "Complete" ) ) == 0) {
bComplete = TRUE;
}
}
} else if (lstrcmpi( psz, _T( "/TAG" ) ) == 0) {
if (popstring( psz ) == NOERROR) {
MyFree( qsel.pszTag );
qsel.pszTag = MyStrDup( eT2A, psz );
}
}
}
if (!bWaiting && !bRunning && !bComplete)
bWaiting = bRunning = bComplete = TRUE; /// All by default
// Enumerate ID-s
sl = QueueEnumerate( &qsel, bWaiting, bRunning, bComplete );
// Empty string marks the end of enumeration
pushstringEx( _T( "" ) );
// Push them on the stack in reversed order
{
int i, j, n;
struct curl_slist *s;
for (n = 0, s = sl; s; n++, s = s->next); /// List length
for (i = n - 1; i >= 0; i--) {
for (j = 0, s = sl; j < i; j++, s = s->next);
PushStringA( s->data );
}
}
curl_slist_free_all( sl );
MyFree( psz );
MyFree( qsel.pszTag );
}
}
//++ [exported] escape <string>
EXTERN_C __declspec(dllexport)
void __cdecl escape( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
LPTSTR psz = NULL;
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
psz = (LPTSTR)MyAlloc( string_size * sizeof(TCHAR) );
assert( psz );
if (popstring( psz ) == NOERROR) {
LPSTR pszA = MyStrDup( eT2A, psz );
if (pszA) {
LPSTR pszOut = curl_easy_escape( NULL, pszA, 0 );
if (pszOut) {
PushStringA( pszOut );
curl_free( pszOut );
}
MyFree( pszA );
}
}
MyFree( psz );
}
//++ [exported] unescape <string>
EXTERN_C __declspec(dllexport)
void __cdecl unescape( HWND parent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra )
{
LPTSTR psz = NULL;
EXDLL_INIT();
EXDLL_VALIDATE();
TRACE( _T( "%s!%hs\n" ), PLUGINNAME, __FUNCTION__ );
psz = (LPTSTR)MyAlloc( string_size * sizeof(TCHAR) );
assert( psz );
if (popstring( psz ) == NOERROR) {
LPSTR pszA = MyStrDup( eT2A, psz );
if (pszA) {
LPSTR pszOut = curl_easy_unescape( NULL, pszA, 0, NULL );
if (pszOut) {
PushStringA( pszOut );
curl_free( pszOut );
}
MyFree( pszA );
}
}
MyFree( psz );
}
#endif
//++ DllMain
EXTERN_C
BOOL WINAPI DllMain( HMODULE hInst, UINT iReason, LPVOID lpReserved )
{
if ( iReason == DLL_PROCESS_ATTACH ) {
PluginInit( hInst );
} else if ( iReason == DLL_PROCESS_DETACH ) {
PluginUninit();
}
return TRUE;
}