00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 #include <stdio.h>
00191
00192 #include "basext.h"
00193 #include "sym.h"
00194 #include "errcodes.h"
00195 #include "report.h"
00196 #include "lexer.h"
00197 #include "expression.h"
00198 #include "builder.h"
00199 #include "memory.h"
00200 #include "syntax.h"
00201 #include "execute.h"
00202 #include "myalloc.h"
00203 #include "dynlolib.h"
00204 #include "hookers.h"
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 int hook_Init(pExecuteObject pEo,
00234 pHookFunctions *pHookers
00235 ){
00236
00237
00238 *pHookers = alloc_Alloc(sizeof(HookFunctions),pEo->pMemorySegment);
00239 if( *pHookers == NULL )return COMMAND_ERROR_MEMORY_LOW;
00240 #define SET(x) (*pHookers)->HOOK_##x = hook_##x
00241
00242 (*pHookers)->hook_pointer = NULL;
00243
00244 SET(file_access);
00245
00246 SET(fopen);
00247 SET(fclose);
00248 SET(size);
00249 SET(time_accessed);
00250 SET(time_modified);
00251 SET(time_created);
00252 SET(isdir);
00253 SET(isreg);
00254 SET(exists);
00255 SET(truncate);
00256 SET(fgetc);
00257 SET(ferror);
00258 SET(fread);
00259 SET(setmode);
00260 SET(binmode);
00261 SET(textmode);
00262 SET(fwrite);
00263 SET(fputc);
00264 SET(flock);
00265 SET(lock);
00266 SET(feof);
00267 SET(mkdir);
00268 SET(rmdir);
00269 SET(remove);
00270 SET(deltree);
00271 SET(MakeDirectory);
00272 SET(opendir);
00273 SET(readdir);
00274 SET(closedir);
00275 SET(sleep);
00276 SET(curdir);
00277 SET(chdir);
00278 SET(chown);
00279 SET(SetCreateTime);
00280 SET(SetModifyTime);
00281 SET(SetAccessTime);
00282 SET(gethostname);
00283 SET(gethost);
00284 SET(tcpconnect);
00285 SET(tcpsend);
00286 SET(tcprecv);
00287 SET(tcpclose);
00288 SET(killproc);
00289 SET(getowner);
00290 SET(fcrypt);
00291 SET(CreateProcess);
00292 SET(CreateProcessEx);
00293 SET(CallScribaFunction);
00294 SET(waitpid);
00295
00296 (*pHookers)->HOOK_ExecBefore = NULL;
00297 (*pHookers)->HOOK_ExecAfter = NULL;
00298 (*pHookers)->HOOK_ExecCall = NULL;
00299 (*pHookers)->HOOK_ExecReturn = NULL;
00300
00301 return COMMAND_ERROR_SUCCESS;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 int hook_file_access(pExecuteObject pEo,
00337 char *pszFileName
00338 ){
00339
00340
00341 return 3;
00342 }
00343
00344
00345
00346
00347
00348 FILE *hook_fopen(pExecuteObject pEo,
00349 char *pszFileName,
00350 char *pszOpenMode
00351 ){
00352
00353
00354 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00355
00356 if( iAccessPermission == 0 )return NULL;
00357 if( (iAccessPermission&1 && *pszOpenMode == 'r') ||
00358 (iAccessPermission&2 && ( *pszOpenMode == 'w' || *pszOpenMode == 'a' )) )
00359 return file_fopen(pszFileName,pszOpenMode);
00360 return NULL;
00361 }
00362
00363
00364
00365
00366
00367 void hook_fclose(pExecuteObject pEo,
00368 FILE *fp
00369 ){
00370
00371
00372 file_fclose(fp);
00373 }
00374
00375
00376
00377
00378
00379 long hook_size(pExecuteObject pEo,
00380 char *pszFileName
00381 ){
00382
00383
00384 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00385 if( !(iAccessPermission&1) )return -1;
00386 return file_size(pszFileName);
00387 }
00388
00389
00390
00391
00392
00393
00394 long hook_time_accessed(pExecuteObject pEo,
00395 char *pszFileName
00396 ){
00397
00398
00399 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00400 if( !(iAccessPermission&1) )return 0;
00401
00402 return file_time_accessed(pszFileName);
00403 }
00404
00405
00406
00407
00408
00409
00410 long hook_time_modified(pExecuteObject pEo,
00411 char *pszFileName
00412 ){
00413
00414
00415 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00416 if( !(iAccessPermission&1) )return 0;
00417
00418 return file_time_modified(pszFileName);
00419 }
00420
00421
00422
00423
00424
00425
00426 long hook_time_created(pExecuteObject pEo,
00427 char *pszFileName
00428 ){
00429
00430
00431 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00432 if( !(iAccessPermission&1) )return 0;
00433
00434 return file_time_created(pszFileName);
00435 }
00436
00437
00438
00439
00440
00441
00442 int hook_isdir(pExecuteObject pEo,
00443 char *pszFileName
00444 ){
00445
00446
00447 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00448 if( !(iAccessPermission&1) )return 0;
00449
00450 return file_isdir(pszFileName);
00451 }
00452
00453
00454
00455
00456
00457
00458 int hook_isreg(pExecuteObject pEo,
00459 char *pszFileName
00460 ){
00461
00462
00463 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00464 if( !(iAccessPermission&1) )return 0;
00465
00466 return file_isreg(pszFileName);
00467 }
00468
00469
00470
00471
00472
00473
00474 int hook_exists(pExecuteObject pEo,
00475 char *pszFileName
00476 ){
00477
00478
00479 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00480 if( !(iAccessPermission&1) )return 0;
00481
00482 return file_exists(pszFileName);
00483 }
00484
00485
00486
00487
00488
00489 int hook_truncate(pExecuteObject pEo,
00490 FILE *fp,
00491 long lNewFileSize
00492 ){
00493
00494
00495 return file_truncate(fp,lNewFileSize);
00496 }
00497
00498
00499
00500
00501
00502 int hook_fgetc(pExecuteObject pEo,
00503 FILE *fp
00504 ){
00505
00506
00507 return file_fgetc(fp);
00508 }
00509
00510
00511
00512
00513
00514 int hook_ferror(pExecuteObject pEo,
00515 FILE *fp
00516 ){
00517
00518
00519 return file_ferror(fp);
00520 }
00521
00522
00523
00524
00525
00526 int hook_fread(pExecuteObject pEo,
00527 char *buf,
00528 int size,
00529 int count,
00530 FILE *fp
00531 ){
00532
00533
00534 return file_fread(buf,size,count,fp);
00535 }
00536
00537
00538
00539
00540
00541
00542 void hook_setmode(pExecuteObject pEo,
00543 FILE *fp,
00544 int mode
00545 ){
00546
00547
00548 file_setmode(fp,mode);
00549 return;
00550 }
00551
00552
00553
00554
00555
00556 void hook_binmode(pExecuteObject pEo,
00557 FILE *fp
00558 ){
00559
00560
00561 file_binmode(fp);
00562 return;
00563 }
00564
00565
00566
00567
00568
00569 void hook_textmode(pExecuteObject pEo,
00570 FILE *fp
00571 ){
00572
00573
00574 file_textmode(fp);
00575 return;
00576 }
00577
00578
00579
00580
00581
00582 int hook_fwrite(pExecuteObject pEo,
00583 char *buf,
00584 int size,
00585 int count,
00586 FILE *fp
00587 ){
00588
00589
00590 return file_fwrite(buf,size,count,fp);
00591 }
00592
00593
00594
00595
00596
00597
00598 int hook_fputc(pExecuteObject pEo,
00599 int c,
00600 FILE *fp
00601 ){
00602
00603
00604 return file_fputc(c,fp);
00605 }
00606
00607
00608
00609
00610
00611
00612 int hook_flock(pExecuteObject pEo,
00613 FILE *fp,
00614 int iLockType
00615 ){
00616
00617
00618 return file_flock(fp,iLockType);
00619 }
00620
00621
00622
00623
00624
00625
00626 int hook_lock(pExecuteObject pEo,
00627 FILE *fp,
00628 int iLockType,
00629 long lStart,
00630 long lLength
00631 ){
00632 return file_lock(fp,iLockType,lStart,lLength);
00633
00634
00635 }
00636
00637
00638
00639
00640
00641
00642 int hook_feof(pExecuteObject pEo,
00643 FILE *fp
00644 ){
00645
00646
00647 return file_feof(fp);
00648 }
00649
00650
00651
00652
00653
00654
00655 int hook_mkdir(pExecuteObject pEo,
00656 char *pszDirectoryName
00657 ){
00658
00659
00660 return file_mkdir(pszDirectoryName);
00661 }
00662
00663
00664
00665
00666
00667
00668 int hook_rmdir(pExecuteObject pEo,
00669 char *pszDirectoryName
00670 ){
00671
00672
00673 return file_rmdir(pszDirectoryName);
00674 }
00675
00676
00677
00678
00679
00680
00681 int hook_remove(pExecuteObject pEo,
00682 char *pszFileName
00683 ){
00684
00685
00686 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00687 if( !(iAccessPermission&1) )return -1;
00688
00689 return file_remove(pszFileName);
00690 }
00691
00692
00693
00694
00695
00696
00697 int hook_deltree(pExecuteObject pEo,
00698 char *pszDirectoryName
00699 ){
00700
00701
00702 int iAccessPermission = HOOK_FILE_ACCESS(pszDirectoryName);
00703 if( !(iAccessPermission&1) )return -1;
00704
00705 return file_deltree(pszDirectoryName);
00706 }
00707
00708
00709
00710
00711
00712
00713 int hook_MakeDirectory(pExecuteObject pEo,
00714 char *pszDirectoryName
00715 ){
00716
00717
00718 int iAccessPermission = HOOK_FILE_ACCESS(pszDirectoryName);
00719 if( !(iAccessPermission&1) )return -1;
00720
00721 return file_MakeDirectory(pszDirectoryName);
00722 }
00723
00724
00725
00726
00727
00728
00729 DIR *hook_opendir(pExecuteObject pEo,
00730 char *pszDirectoryName,
00731 tDIR *pDirectory
00732 ){
00733
00734
00735 return file_opendir(pszDirectoryName,pDirectory);
00736 }
00737
00738
00739
00740
00741
00742
00743 struct dirent *hook_readdir(pExecuteObject pEo,
00744 DIR *pDirectory
00745 ){
00746
00747
00748 return file_readdir(pDirectory);
00749 }
00750
00751
00752
00753
00754
00755
00756 void hook_closedir(pExecuteObject pEo,
00757 DIR *pDirectory
00758 ){
00759
00760
00761
00762 file_closedir(pDirectory);
00763 }
00764
00765
00766
00767
00768
00769
00770 void hook_sleep(pExecuteObject pEo,
00771 long lSeconds
00772 ){
00773
00774
00775 sys_sleep(lSeconds);
00776 }
00777
00778
00779
00780
00781
00782
00783 int hook_curdir(pExecuteObject pEo,
00784 char *Buffer,
00785 unsigned long cbBuffer
00786 ){
00787
00788
00789 return file_curdir(Buffer,cbBuffer);
00790 }
00791
00792
00793
00794
00795
00796
00797 int hook_chdir(pExecuteObject pEo,
00798 char *Buffer
00799 ){
00800
00801
00802 return file_chdir(Buffer);
00803 }
00804
00805
00806
00807
00808
00809
00810 int hook_chown(pExecuteObject pEo,
00811 char *pszFileName,
00812 char *pszOwner
00813 ){
00814
00815
00816 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00817 if( !(iAccessPermission&1) )return COMMAND_ERROR_CHOWN_SET_OWNER;
00818
00819 return file_chown(pszFileName,pszOwner);
00820 }
00821
00822
00823
00824
00825
00826
00827 int hook_SetCreateTime(pExecuteObject pEo,
00828 char *pszFileName,
00829 long lTime
00830 ){
00831
00832
00833 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00834 if( !(iAccessPermission&1) )return COMMAND_ERROR_CREATIME_FAIL;
00835
00836 return file_SetCreateTime(pszFileName,lTime);
00837 }
00838
00839
00840
00841
00842
00843
00844 int hook_SetModifyTime(pExecuteObject pEo,
00845 char *pszFileName,
00846 long lTime
00847 ){
00848
00849
00850 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00851 if( !(iAccessPermission&1) )return COMMAND_ERROR_MODTIME_FAIL;
00852
00853 return file_SetModifyTime(pszFileName,lTime);
00854 }
00855
00856
00857
00858
00859
00860
00861 int hook_SetAccessTime(pExecuteObject pEo,
00862 char *pszFileName,
00863 long lTime
00864 ){
00865
00866
00867 int iAccessPermission = HOOK_FILE_ACCESS(pszFileName);
00868 if( !(iAccessPermission&1) )return COMMAND_ERROR_ACCTIM_FAIL;
00869
00870 return file_SetAccessTime(pszFileName,lTime);
00871 }
00872
00873
00874
00875
00876
00877
00878 int hook_gethostname(pExecuteObject pEo,
00879 char *pszBuffer,
00880 long cbBuffer
00881 ){
00882
00883
00884 return file_gethostname(pszBuffer,cbBuffer);
00885 }
00886
00887
00888
00889
00890
00891 int hook_gethost(pExecuteObject pEo,
00892 char *pszBuffer,
00893 struct hostent *pHost
00894 ){
00895
00896
00897 return file_gethost(pszBuffer,pHost);
00898 }
00899
00900
00901
00902
00903
00904 int hook_tcpconnect(pExecuteObject pEo,
00905 SOCKET *sClient,
00906 char *pszRemoteSocket
00907 ){
00908
00909
00910 return file_tcpconnect(sClient,pszRemoteSocket);
00911 }
00912
00913
00914
00915
00916
00917 int hook_tcpsend(pExecuteObject pEo,
00918 SOCKET sClient,
00919 char *pszBuffer,
00920 long cbBuffer,
00921 int iFlags
00922 ){
00923
00924
00925 return file_tcpsend(sClient,pszBuffer,cbBuffer,iFlags);
00926 }
00927
00928
00929
00930
00931
00932 int hook_tcprecv(pExecuteObject pEo,
00933 SOCKET sClient,
00934 char *pszBuffer,
00935 long cbBuffer,
00936 int iFlags
00937 ){
00938
00939
00940 return file_tcprecv(sClient,pszBuffer,cbBuffer,iFlags);
00941 }
00942
00943
00944
00945
00946
00947 int hook_tcpclose(pExecuteObject pEo,
00948 SOCKET sClient
00949 ){
00950
00951
00952 return file_tcpclose(sClient);
00953 }
00954
00955
00956
00957
00958
00959 int hook_killproc(pExecuteObject pEo,
00960 long pid
00961 ){
00962
00963
00964 return file_killproc(pid);
00965 }
00966
00967
00968
00969
00970
00971 int hook_getowner(pExecuteObject pEo,
00972 char *pszFileName,
00973 char *pszOwnerBuffer,
00974 long cbOwnerBuffer
00975 ){
00976
00977
00978 return file_getowner(pszFileName,pszOwnerBuffer,cbOwnerBuffer);
00979 }
00980
00981
00982
00983
00984
00985 char *hook_fcrypt(pExecuteObject pEo,
00986 char *buf,
00987 char *salt,
00988 char *buff
00989 ){
00990
00991
00992 return file_fcrypt(buf,salt,buff);
00993 }
00994
00995
00996
00997
00998
00999 long hook_CreateProcess(pExecuteObject pEo,
01000 char *pszCommandLine
01001 ){
01002
01003
01004 return file_CreateProcess(pszCommandLine);
01005 }
01006
01007
01008
01009
01010
01011 long hook_CreateProcessEx(pExecuteObject pEo,
01012 char *pszCommandLine,
01013 long lTimeOut,
01014 unsigned long *plPid,
01015 unsigned long *plExitCode
01016 ){
01017
01018
01019 return file_CreateProcessEx(pszCommandLine,lTimeOut,plPid,plExitCode);
01020 }
01021
01022
01023
01024
01025
01026 int hook_waitpid(pExecuteObject pEo,
01027 long pid,
01028 unsigned long *plExitCode
01029 ){
01030
01031
01032 return file_waitpid(pid,plExitCode);
01033 }
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048 int hook_CallScribaFunction(pExecuteObject pEo,
01049 unsigned long lStartNode,
01050 pFixSizeMemoryObject *pArgument,
01051 unsigned long NumberOfPassedArguments,
01052 pFixSizeMemoryObject *pFunctionResult
01053 ){
01054
01055
01056
01057 int iError;
01058 unsigned long SaveProgramCounter,SaveStepCounter;
01059 unsigned long SavefErrorGoto,SaveErrorGoto,SaveErrorResume;
01060 pFixSizeMemoryObject SaveLocalVariablesPointer;
01061 pFixSizeMemoryObject SaveFunctionResultPointer;
01062 MortalList _ThisCommandMortals=NULL;
01063 pMortalList _pThisCommandMortals = &_ThisCommandMortals;
01064 unsigned long _ActualNode=pEo->ProgramCounter;
01065 int iErrorCode;
01066 NODE nItem;
01067 unsigned long i;
01068 unsigned long NumberOfArguments;
01069 long Opcode;
01070
01071
01072 SaveLocalVariablesPointer = pEo->LocalVariables;
01073 SaveProgramCounter = pEo->ProgramCounter;
01074 pEo->ProgramCounter = lStartNode;
01075 if( pEo->ProgramCounter == 0 )return EXE_ERROR_USERFUN_UNDEFINED;
01076
01077 SaveFunctionResultPointer = pEo->pFunctionResult;
01078 pEo->pFunctionResult = NULL;
01079 SaveStepCounter = pEo->lStepCounter;
01080 pEo->lStepCounter = 0;
01081 SaveErrorGoto = pEo->ErrorGoto;
01082 pEo->ErrorGoto = 0;
01083 SaveErrorResume = pEo->ErrorResume;
01084 pEo->ErrorResume = 0;
01085 SavefErrorGoto = pEo->fErrorGoto;
01086 pEo->fErrorGoto = ONERROR_NOTHING;
01087
01088 nItem = pEo->CommandArray[pEo->ProgramCounter-1].Parameter.NodeList.actualm ;
01089 Opcode = pEo->CommandArray[nItem-1].OpCode;
01090 pEo->cLocalVariables = pEo->CommandArray[nItem-1].Parameter.CommandArgument.Argument.lLongValue;
01091 nItem = pEo->CommandArray[nItem-1].Parameter.CommandArgument.next;
01092 NumberOfArguments = pEo->CommandArray[nItem-1].Parameter.CommandArgument.Argument.lLongValue;
01093 nItem = pEo->CommandArray[nItem-1].Parameter.CommandArgument.next;
01094 nItem = pEo->CommandArray[nItem-1].Parameter.CommandArgument.Argument.lLongValue;
01095
01096 if( pEo->cLocalVariables ){
01097 pEo->LocalVariables = memory_NewArray(pEo->pMo,1,pEo->cLocalVariables);
01098 if( pEo->LocalVariables == NULL )return COMMAND_ERROR_MEMORY_LOW;
01099 }else pEo->LocalVariables = NULL;
01100
01101 for( i=0 ; pArgument && i < NumberOfPassedArguments && i < NumberOfArguments ; i++ ){
01102 pEo->LocalVariables->Value.aValue[i]
01103 = memory_DupVar(pEo->pMo,
01104 pArgument[i],
01105 _pThisCommandMortals,
01106 &iError);
01107 if( iError )return iError;
01108 }
01109 while( i < (unsigned)pEo->cLocalVariables ){
01110 pEo->LocalVariables->Value.aValue[i] = NULL;
01111 i++;
01112 }
01113
01114
01115 pEo->lFunctionLevel++;
01116
01117
01118 pEo->ProgramCounter = pEo->CommandArray[pEo->ProgramCounter-1].Parameter.NodeList.rest;
01119 execute_Execute_r(pEo,&iErrorCode);
01120
01121
01122
01123 pEo->lStepCounter = SaveStepCounter;
01124 if( pEo->LocalVariables )
01125 memory_ReleaseVariable(pEo->pMo,pEo->LocalVariables);
01126 pEo->ProgramCounter = SaveProgramCounter;
01127 pEo->LocalVariables = SaveLocalVariablesPointer;
01128 (*pFunctionResult) = pEo->pFunctionResult;
01129 pEo->pFunctionResult = SaveFunctionResultPointer;
01130
01131 pEo->ErrorGoto = SaveErrorGoto;
01132 pEo->fErrorGoto = SavefErrorGoto;
01133 pEo->ErrorResume = SaveErrorResume;
01134 return iErrorCode;
01135 }