Annotation of mstools/samples/sdktools/remote/server.c, revision 1.1.1.2

1.1       root        1: /*++
                      2: 
                      3: Copyright (c) 1992  Microsoft Corporation
                      4: 
                      5: Module Name:
                      6: 
                      7:     Server.c
                      8: 
                      9: Abstract:
                     10: 
                     11:     The server component of Remote. It spawns a child process
                     12:     and redirects the stdin/stdout/stderr of child to itself.
                     13:     Waits for connections from clients - passing the
                     14:     output of child process to client and the input from clients
                     15:     to child process.
                     16: 
                     17: Author:
                     18: 
                     19:     Rajivendra Nath (rajnath) 2-Jan-1992
                     20: 
                     21: Environment:
                     22: 
                     23:     Console App. User mode.
                     24: 
                     25: Revision History:
                     26: 
                     27: --*/
                     28: 
                     29: #include <windows.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <io.h>
                     33: #include <string.h>
                     34: #include "Remote.h"
                     35: 
                     36: #define MAX_SESSION   10
                     37: 
                     38: #define COMMANDFORMAT       "%c%-15s    [%-15s %d:%d]\n%c"
                     39: #define LOCALNAME           "Local"
                     40: #define LOCALCLIENT(x)      (strcmp((char *)(x->Name),LOCALNAME)==0)
                     41: #define RemoteInfo(prt,flg) {if (!(flg&&0x80000000)) prt;}
                     42: 
                     43: #define CMDSTRING(OutBuff,InpBuff,Client,sTime) \
                     44:         {                                \
                     45:             /* int xxlen; */             \
                     46:             sprintf                      \
                     47:             (                            \
                     48:                &OutBuff[0],COMMANDFORMAT,\
                     49:                BEGINMARK,InpBuff,        \
                     50:                Client->Name,sTime.wHour, \
                     51:                sTime.wMinute,ENDMARK     \
                     52:             );                           \
                     53:          }                               \
                     54: 
                     55: #define BUFFSIZE      256
                     56: 
                     57: SESSION_TYPE ClientList[MAX_SESSION];
                     58: 
                     59: HANDLE  ChildStdInp;     //Server Writes to  it
                     60: HANDLE  ChildStdOut;     //Server Reads from it
                     61: HANDLE  ChildStdErr;     //Server Reads from it
                     62: 
                     63: HANDLE  SaveFile;       //File containing all that was
                     64:                         //output by child process.
                     65:                         //Each connection opens a handle to this file
                     66:                         //and is sent through PipeWriteH.
                     67: 
                     68: char    SaveFileName[64]; //Name of above file - all new sessions need
                     69: HANDLE  ChldProc;         //Handle to the Child Process
                     70: HANDLE  ListenThreadH;    //Handle to the thread listening for connections
                     71:                           //from Remote Clients.
                     72: 
1.1.1.2 ! root       73: char    *UserName;        // User/Group who can connect to this server.
        !            74:                           // Obtained from /U option. If NULL all access.
        !            75: 
1.1       root       76: HANDLE
                     77: ForkChildProcess(          // Creates a new process
                     78:     char *cmd,             // Redirects its stdin,stdout
                     79:     PHANDLE in,            // and stderr - returns the
                     80:     PHANDLE out,           // corresponding pipe ends. Using currently
                     81:     PHANDLE err
                     82:     );
                     83: 
                     84: HANDLE
                     85: OldForkChildProcess(       //Same as above except different
                     86:     char *cmd,             //method for redirection. Not Used.
                     87:     PHANDLE in,
                     88:     PHANDLE out,
                     89:     PHANDLE err
                     90:     );
                     91: 
                     92: DWORD
                     93: ListenForSession(          //THREAD:Listens for new connections and
                     94:     char* pipe             //spawns of new seesions - Updates the
                     95:     );                     //Status in Client DataStructure. Seperate Thread.
                     96: 
1.1.1.2 ! root       97: BOOL
        !            98: CreateMySecurityDescriptor(                    //
        !            99:     PSECURITY_DESCRIPTOR pSecurityDescriptor,  // Creates a security descriptor
        !           100:     char *Owner                                // with discretionary access for
        !           101:     );                                         // access for Owner.
        !           102: 
        !           103: 
1.1       root      104: DWORD
                    105: NewSession(                //Manages the session with a client.
                    106:     SESSION_TYPE* Client
                    107:     );
                    108: 
                    109: DWORD                      //2 THREAD:Each reads either
                    110: GetChldOutput(             //StdOut or StdErr of child and
                    111:     HANDLE rhandle         //writes to SaveFile. Seperate Thread.
                    112:     );
                    113: 
                    114: DWORD
                    115: TransferFileToClient(      //X THREADS:Reads the save
                    116:     SESSION_TYPE* Client   //file and sendsoutput to a client. Seperate Thread
                    117:     );
                    118: 
                    119: 
                    120: DWORD
                    121: GetClientInput(            //Times X THREADS:Gets input from Child pipe
                    122:     SESSION_TYPE* Client   //and sends to childs StdIn. Seperate Thread.
                    123:     );
                    124: 
                    125: 
                    126: BOOL
                    127: FilterCommand(             //Filters input from client
                    128:     SESSION_TYPE *cl,      //for commands intended for REMOTE
                    129:     char *buff,
                    130:     int dread
                    131:     );
                    132: 
                    133: DWORD                      // Manages the IO with user
                    134: LocalSession(              // For the remote server. Seperate Thread.
                    135:     PVOID noarg
                    136:     );
                    137: 
                    138: DWORD                      // Manages the IO with Remote Client.
                    139: RemoteSession(
                    140:     SESSION_TYPE* Client
                    141:     );
                    142: 
                    143: BOOL                       // Ctrl-C handler
                    144: SrvCtrlHand(
                    145:     DWORD event
                    146:     );
                    147: 
                    148: VOID                       // @s command to remote
                    149: SendStatus(
                    150:     HANDLE hClientPipe
                    151:     );
                    152: 
                    153: DWORD                      // @p command to remote
                    154: ShowPopup(
                    155:     char *mssg
                    156:     );
                    157: 
                    158: VOID                       // Removes the command begin and end markers
                    159: RemoveInpMark(             // from the save file.
                    160:     char* Buff,
                    161:     DWORD Size
                    162:     );
                    163: 
                    164: VOID                       // Cleans up the session
                    165: CloseClient(               // once it ends.
                    166:     SESSION_TYPE *Client
                    167:     );
                    168:                            // Initialises the Client datastructs
                    169: VOID
                    170: InitClientList(
                    171:     );
                    172: 
                    173: 
                    174: /*************************************************************/
                    175: /* The main entry point for the Server End of Remote         */
                    176: /*************************************************************/
                    177: VOID
                    178: Server(                    
                    179:     char* ChildCmd,
                    180:     char* PipeName
                    181:     )
                    182: {
                    183:     DWORD  ThreadID ;
                    184:     HANDLE WaitH[3];
                    185:     DWORD  WaitObj;
                    186:     char   tmpdir[32];
                    187: 
                    188:     WRITEF((VBuff,"**************************************\n"));
                    189:     WRITEF((VBuff,"***********     REMOTE    ************\n"));
                    190:     WRITEF((VBuff,"***********     SERVER    ************\n"));
                    191:     WRITEF((VBuff,"**************************************\n"));
                    192:     WRITEF((VBuff,"To Connect: Remote /C %s %s\n\n",HostName,PipeName));
                    193: 
                    194:     InitClientList();
                    195: 
                    196:     //
                    197:     //Start the command as a child process
                    198:     //
                    199: 
                    200:     ChldProc=ForkChildProcess(ChildCmd,&ChildStdInp,&ChildStdOut,&ChildStdErr);
                    201: 
                    202:     //
                    203:     //Create a tempfile for storing Child process output.
                    204:     //
                    205:     {
                    206:          DWORD size=sizeof(tmpdir);
                    207:          if (
                    208:              (GetEnvironmentVariable("TMP" ,tmpdir,size)==0)&&
                    209:              (GetEnvironmentVariable("TEMP",tmpdir,size)==0)
                    210:             )
                    211:          {
                    212:             sprintf(tmpdir,"%s",".");
                    213:          }
                    214:          if (!GetTempFileName(tmpdir,"REMOTE",0,SaveFileName))
                    215:             GetTempFileName(".","REMOTE",0,SaveFileName);
                    216:     }
                    217: 
                    218: 
                    219:     if ((SaveFile=CreateFile
                    220:                   (
                    221:                      (LPCTSTR)SaveFileName,           /* address of name of the file  */
                    222:                      GENERIC_READ|GENERIC_WRITE,      /* access (read/write) mode */
                    223:                      FILE_SHARE_READ|FILE_SHARE_WRITE,/* share mode   */
                    224:                      (LPSECURITY_ATTRIBUTES)NULL,     /* security descriptor  */
                    225:                      CREATE_ALWAYS,                   /* how to create    */
                    226:                      FILE_ATTRIBUTE_NORMAL,           /* File Attribute */
                    227:                      (HANDLE)NULL)
                    228:                   )==NULL)
                    229:     {
                    230:         TerminateProcess(ChldProc,0);
                    231:         ErrorExit("Could not Create Output File");
                    232:     }
                    233: 
                    234: 
                    235:     //
                    236:     //Start 2 threads to save the output from stdout and stderr of cmd to savefile.
                    237:     //
                    238: 
                    239:     if ((WaitH[0]=CreateThread
                    240:                   (
                    241:                      (LPSECURITY_ATTRIBUTES)NULL,           // No security attributes.
                    242:                      (DWORD)0,                              // Use same stack size.
                    243:                      (LPTHREAD_START_ROUTINE)GetChldOutput, // Thread procedure.
                    244:                      (LPVOID)ChildStdErr,                   // Parameter to pass.
                    245:                      (DWORD)0,                              // Run immediately.
                    246:                      (LPDWORD)&ThreadID)
                    247:                    )==NULL)
                    248:     {
                    249: 
                    250:         TerminateProcess(ChldProc,0);
                    251:         ErrorExit("Failed to Create GetGhldOutput#1 Thread");
                    252:     }
                    253: 
                    254: 
                    255:     if ((WaitH[1]=CreateThread
                    256:                   (
                    257:                      (LPSECURITY_ATTRIBUTES)NULL,           // No security attributes.
                    258:                      (DWORD)0,                              // Use same stack size.
                    259:                      (LPTHREAD_START_ROUTINE)GetChldOutput, // Thread procedure.
                    260:                      (LPVOID)ChildStdOut,                   // Parameter to pass.
                    261:                      (DWORD)0,                              // Run immediately.
                    262:                      (LPDWORD)&ThreadID)
                    263:                   )==NULL)
                    264:     {
                    265: 
                    266:         TerminateProcess(ChldProc,0);
                    267:         ErrorExit("Failed to Create GetGhldOutput#2 Thread");
                    268:     }
                    269: 
                    270: 
                    271:     //
                    272:     //Start Thread to listen for new Connections
                    273:     //
                    274: 
                    275:     if ((ListenThreadH=CreateThread
                    276:                        (
                    277:                          (LPSECURITY_ATTRIBUTES)NULL,        // No security attributes.
                    278:                          (DWORD)0,                           // Use same stack size.
                    279:                          (LPTHREAD_START_ROUTINE)ListenForSession, // Thread procedure.
                    280:                          (LPVOID)PipeName,                   // Parameter to pass.
                    281:                          (DWORD)0,                           // Run immediately.
                    282:                          (LPDWORD)&ThreadID)
                    283:                        )==NULL)
                    284:     {
                    285: 
                    286:         TerminateProcess(ChldProc,0);
                    287:         ErrorExit("Failed To Create ListenForSession Thread");
                    288: 
                    289:     }
                    290: 
                    291:     //
                    292:     //Start Local Thread
                    293:     //
                    294: 
                    295:     if ((ClientList[0].hThread=CreateThread
                    296:                                (
                    297:                                     (LPSECURITY_ATTRIBUTES)NULL,        // No security attributes.
                    298:                                     (DWORD)0,                           // Use same stack size.
                    299:                                     (LPTHREAD_START_ROUTINE)LocalSession, // Thread procedure.
                    300:                                     (LPVOID)NULL,                       // Parameter to pass.
                    301:                                     (DWORD)0,                           // Run immediately.
                    302:                                     (LPDWORD)&ThreadID)
                    303:                                )==NULL)
                    304:     {
                    305: 
                    306:         TerminateProcess(ChldProc,0);
                    307:         ErrorExit("Failed To Create ListenForSession Thread");
                    308: 
                    309:     }
                    310: 
                    311:     SetConsoleCtrlHandler((PHANDLER_ROUTINE)SrvCtrlHand,TRUE);
                    312: 
                    313:     WaitH[2]=ChldProc;
                    314: 
                    315:     //
                    316:     // Wait until the child process terminates
                    317:     // or local IO thread terminates
                    318:     // or IO with child process ends
                    319:     //
                    320: 
                    321:     WaitObj=WaitForMultipleObjects(3,WaitH,FALSE,INFINITE);
                    322: 
                    323:     switch (WaitObj-WAIT_OBJECT_0)
                    324:     {
                    325:         case 0:      // Error Writing to savefile
                    326:         case 1:
                    327:             TerminateProcess(ChldProc,0);
                    328:             break;
                    329:         case 2:      // Child Proc Terminated
                    330:             break;
                    331: 
                    332:         default:     // Out of Some Resource
                    333:             printf("Out of Resource Error %d..Terminating\n",GetLastError());
                    334:             break;
                    335: 
                    336:     }
                    337: 
                    338:     TerminateThread(ListenThreadH,0);
                    339: 
                    340:     CloseHandle(ChildStdInp);
                    341:     CloseHandle(ChildStdOut);
                    342:     CloseHandle(ChildStdErr);
                    343: 
                    344:     WRITEF((VBuff,"\nRemote:Parent exiting. Child(%s) dead..\n",ChildCmd));
                    345: 
                    346:     CloseHandle(SaveFile);
                    347: 
                    348:     {
                    349:         int i;
                    350:         for (i=0;i<MAX_SESSION;i++)
                    351:             CloseClient(&ClientList[i]);
                    352:     }
                    353: 
                    354:     if (!DeleteFile(SaveFileName))
                    355:           WRITEF((VBuff,"Temp File %s not deleted..\n",SaveFileName));
                    356: 
                    357:     return;
                    358: }
                    359: /*************************************************************/
                    360: /* Creates the child process and redirects its std.IO handles*/
                    361: /*************************************************************/
                    362: HANDLE
                    363: ForkChildProcess(          // Creates a new process
                    364:     char *cmd,             // Redirects its stdin,stdout
                    365:     PHANDLE inH,           // and stderr - returns the
                    366:     PHANDLE outH,          // corresponding pipe ends.
                    367:     PHANDLE errH
                    368:     )
                    369: {
                    370:     SECURITY_ATTRIBUTES lsa;
                    371:     STARTUPINFO         si;
                    372:     PROCESS_INFORMATION pi;
                    373: 
                    374:     HANDLE ChildIn;
                    375:     HANDLE ChildOut;
                    376:     HANDLE ChildErr;
                    377: 
                    378:     lsa.nLength=sizeof(SECURITY_ATTRIBUTES);
                    379:     lsa.lpSecurityDescriptor=NULL;
                    380:     lsa.bInheritHandle=TRUE;
                    381: 
                    382:     //
                    383:     //Create Parent_Write to ChildStdIn Pipe
                    384:     //
                    385: 
                    386:     if (!CreatePipe(&ChildIn,inH,&lsa,0))
                    387:         ErrorExit("Could Not Create Parent-->Child Pipe");
                    388: 
                    389:     //
                    390:     //Create ChildStdOut to Parent_Read pipe
                    391:     //
                    392: 
                    393:     if (!CreatePipe(outH,&ChildOut,&lsa,0))
                    394:         ErrorExit("Could Not Create Child-->Parent Pipe");
                    395: 
                    396:     //
                    397:     //Create ChildStdOut to Parent_Read pipe
                    398:     //
                    399: 
                    400:     if (!CreatePipe(errH,&ChildErr,&lsa,0))
                    401:         ErrorExit("Could Not Create Child-->Parent Pipe");
                    402: 
                    403:     //
                    404:     // Lets Redirect Console StdHandles - easy enough
                    405:     //
                    406: 
                    407: 
                    408:     si.cb=sizeof(STARTUPINFO);
                    409:     si.lpReserved=NULL;
                    410:     si.lpTitle=NULL;
                    411:     si.lpDesktop=NULL;
                    412:     si.dwX=si.dwY=si.dwYSize=si.dwXSize=0;
                    413:     si.dwFlags=STARTF_USESTDHANDLES;
                    414:     si.hStdInput =ChildIn;
                    415:     si.hStdOutput=ChildOut;
                    416:     si.hStdError =ChildErr;
                    417:     si.wShowWindow=SW_SHOW;
                    418:     si.lpReserved2=NULL;
                    419:     si.cbReserved2=0;
                    420: 
                    421:     //
                    422:     //Create Child Process
                    423:     //
                    424: 
                    425:     if (!CreateProcess
                    426:          (
                    427:             NULL,
                    428:             cmd,
                    429:             NULL,
                    430:             NULL,
                    431:             TRUE,
                    432:             NORMAL_PRIORITY_CLASS,
                    433:             NULL,
                    434:             NULL,
                    435:             &si,
                    436:             &pi)
                    437:          )
                    438:     {
                    439:         if (GetLastError()==2)
                    440:             WRITEF((VBuff,"Executable %s not found\n",cmd));
                    441:         ErrorExit("Could Not Create Child Process");
                    442:     }
                    443: 
                    444:     //
                    445:     //Close unneccesary Handles and Restore the crt handles
                    446:     //
                    447: 
                    448:     CloseHandle(ChildIn);
                    449:     CloseHandle(ChildOut);
                    450:     CloseHandle(ChildErr);
                    451: 
                    452:     return(pi.hProcess);
                    453: }
                    454: /*************************************************************/
                    455: /* Same as above but uses different method of IO redir.      */
                    456: /* Not used now                                              */
                    457: /*************************************************************/
                    458: HANDLE
                    459: OldForkChildProcess(
                    460:     char *cmd,
                    461:     PHANDLE inH,
                    462:     PHANDLE outH,
                    463:     PHANDLE errH
                    464:     )
                    465: {
                    466:     SECURITY_ATTRIBUTES lsa;
                    467:     STARTUPINFO         si;
                    468:     PROCESS_INFORMATION pi;
                    469: 
                    470:     HANDLE OldStdIn =GetStdHandle(STD_INPUT_HANDLE);
                    471:     HANDLE OldStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
                    472:     HANDLE OldStdErr=GetStdHandle(STD_ERROR_HANDLE);
                    473: 
                    474:     HANDLE ChildStdIn;
                    475:     HANDLE ChildStdOut;
                    476:     HANDLE ChildStdErr;
                    477: 
                    478:     lsa.nLength=sizeof(SECURITY_ATTRIBUTES);
                    479:     lsa.lpSecurityDescriptor=NULL;
                    480:     lsa.bInheritHandle=TRUE;
                    481: 
                    482:     //Create Parent_Write to ChildStdIn Pipe
                    483:     if (!CreatePipe(&ChildStdIn,inH,&lsa,0))
                    484:         ErrorExit("Could Not Create Parent-->Child Pipe");
                    485: 
                    486:     //Create ChildStdOut to Parent_Read pipe
                    487:     if (!CreatePipe(outH,&ChildStdOut,&lsa,0))
                    488:         ErrorExit("Could Not Create Child-->Parent Pipe");
                    489: 
                    490:     //Create ChildStdOut to Parent_Read pipe
                    491:     if (!CreatePipe(errH,&ChildStdErr,&lsa,0))
                    492:         ErrorExit("Could Not Create Child-->Parent Pipe");
                    493: 
                    494:     //Make ChildStdIn and Out as standard handles and get it inherited by child
                    495:     if (!SetStdHandle(STD_INPUT_HANDLE,ChildStdIn))
                    496:         ErrorExit("Could not change StdIn");
                    497: 
                    498:     if (!SetStdHandle(STD_OUTPUT_HANDLE,ChildStdOut))
                    499:         ErrorExit("Could Not change StdOut");
                    500: 
                    501:     if (!SetStdHandle(STD_ERROR_HANDLE,ChildStdErr))
                    502:         ErrorExit("Could Not change StdErr");
                    503: 
                    504:     si.cb=sizeof(STARTUPINFO);
                    505:     si.lpReserved=NULL;
                    506:     si.lpTitle=NULL;
                    507:     si.lpDesktop=NULL;
                    508:     si.dwX=si.dwY=si.dwYSize=si.dwXSize=si.dwFlags=0L;
                    509:     si.wShowWindow=SW_SHOW;
                    510:     si.lpReserved2=NULL;
                    511:     si.cbReserved2=0;
                    512: 
                    513:     //Create Child Process
                    514:     if (!CreateProcess
                    515:          (
                    516:             NULL,
                    517:             cmd,
                    518:             NULL,
                    519:             NULL,
                    520:             TRUE,
                    521:             NORMAL_PRIORITY_CLASS,
                    522:             NULL,
                    523:             NULL,
                    524:             &si,
                    525:             &pi)
                    526:          )
                    527:     {
                    528:         ErrorExit("Could Not Create Child Process");
                    529:     }
                    530: 
                    531:     //reset StdIn StdOut
                    532:     if (!SetStdHandle(STD_INPUT_HANDLE,OldStdIn))
                    533:     {
                    534:         TerminateProcess(pi.hProcess,1);
                    535:         ErrorExit("Could not RESET StdIn");
                    536:     }
                    537: 
                    538:     if (!SetStdHandle(STD_OUTPUT_HANDLE,OldStdOut))
                    539:     {
                    540:         TerminateProcess(pi.hProcess,1);
                    541:         ErrorExit("Could not RESET StdIn");
                    542:     }
                    543: 
                    544:     if (!SetStdHandle(STD_ERROR_HANDLE,OldStdErr))
                    545:     {
                    546:         TerminateProcess(pi.hProcess,1);
                    547:         ErrorExit("Could not RESET StdIn");
                    548:     }
                    549: 
                    550:     //Close unneccesary Handles
                    551:     CloseHandle(ChildStdIn);
                    552:     CloseHandle(ChildStdOut);
                    553:     CloseHandle(ChildStdErr);
                    554: 
                    555:     return(pi.hProcess);
                    556: }
1.1.1.2 ! root      557: 
1.1       root      558: /*************************************************************/
                    559: /* Listens for sessions from Clients and creates a new thread*/
                    560: /* for each client                                           */
                    561: /*************************************************************/
                    562: 
                    563: DWORD
                    564: ListenForSession(
                    565:    char* pipename
                    566:    )
                    567: {
                    568:     int    i;
                    569:     DWORD  ThreadID;
                    570:     HANDLE PipeH[2];
                    571:     SECURITY_DESCRIPTOR SecurityDescriptor;
                    572:     HANDLE TokenHandle;
                    573:     TOKEN_DEFAULT_DACL DefaultDacl;
                    574:     SECURITY_ATTRIBUTES lsa;
                    575: 
                    576:     char   fullnameIn[BUFFSIZE];
                    577:     char   fullnameOut[BUFFSIZE];
                    578: 
                    579:     sprintf(fullnameIn,SERVER_READ_PIPE  ,".",pipename);
                    580:     sprintf(fullnameOut,SERVER_WRITE_PIPE,".",pipename);
                    581: 
                    582: 
                    583:     DefaultDacl.DefaultDacl = NULL;
                    584: 
                    585:     if (OpenProcessToken
                    586:         (
                    587:             GetCurrentProcess(),
                    588:             TOKEN_ADJUST_DEFAULT,
                    589:             &TokenHandle
                    590:         ))
                    591:     {
                    592: 
                    593:         //
                    594:         // Remove the default DACL on the token
                    595:         //
                    596: 
                    597:         SetTokenInformation
                    598:         (
                    599:             TokenHandle,
                    600:             TokenDefaultDacl,
                    601:             &DefaultDacl,
                    602:             sizeof( TOKEN_DEFAULT_DACL )
                    603:         );
                    604: 
                    605:     }
                    606: 
1.1.1.2 ! root      607: 
        !           608:     if (CreateMySecurityDescriptor(&SecurityDescriptor,UserName)==FALSE)
        !           609:     {
        !           610:         Errormsg("Remote:Could not set security for user");
        !           611:     }
        !           612: 
1.1       root      613:     lsa.nLength=sizeof(SECURITY_ATTRIBUTES);
                    614:     lsa.lpSecurityDescriptor=&SecurityDescriptor;
                    615:     lsa.bInheritHandle=TRUE;
                    616: 
                    617:     while(TRUE)
                    618:     {
                    619:         //
                    620:         // Create New pipe instances for each connection
                    621:         //
                    622: 
                    623:         PipeH[0]=CreateNamedPipe
                    624:                  (
                    625:                     fullnameIn ,
                    626:                     PIPE_ACCESS_INBOUND ,
                    627:                     PIPE_TYPE_BYTE,
                    628:                     PIPE_UNLIMITED_INSTANCES,
                    629:                     0,0,0,&lsa
                    630:                  );
                    631: 
                    632:         PipeH[1]=CreateNamedPipe
                    633:                  (
                    634:                     fullnameOut,
                    635:                     PIPE_ACCESS_OUTBOUND,
                    636:                     PIPE_TYPE_BYTE,
                    637:                     PIPE_UNLIMITED_INSTANCES,
                    638:                     0,0,0,&lsa
                    639:                  );
                    640: 
                    641:         if (!ConnectNamedPipe(PipeH[0],NULL))
                    642:         {
                    643:             if (GetLastError()!=ERROR_PIPE_CONNECTED)
                    644:             {
                    645:                 CloseHandle(PipeH[0]);
                    646:                 CloseHandle(PipeH[1]);
                    647:                 continue;
                    648:             }
                    649: 
                    650:         }
                    651: 
                    652:         if (!ConnectNamedPipe(PipeH[1],NULL))
                    653:         {
                    654:             if (GetLastError()!=ERROR_PIPE_CONNECTED)
                    655:             {
                    656:                 CloseHandle(PipeH[0]);
                    657:                 CloseHandle(PipeH[1]);
                    658:                 continue;
                    659:             }
                    660:         }
                    661: 
                    662:         //
                    663:         //Look For a Free Slot & if not- then terminate connection
                    664:         //
                    665: 
                    666:         for (i=1;i<MAX_SESSION;i++)
                    667:         {
                    668:             //
                    669:             // Locate a Free Client block
                    670:             //
                    671:             if (!ClientList[i].Active)
                    672:                 break;
                    673:         }
                    674: 
                    675:         if (i<MAX_SESSION)
                    676:         {
                    677:             //
                    678:             // Initialize the Client
                    679:             //
                    680:             ClientList[i].PipeReadH=PipeH[0];
                    681:             ClientList[i].PipeWriteH=PipeH[1];
                    682:             ClientList[i].Active=TRUE;
                    683:             ClientList[i].SendOutput=TRUE;
                    684:             ClientList[i].CommandRcvd=FALSE;
                    685: 
                    686:         }
                    687:         else
                    688:         {
                    689:             WRITEF((VBuff,"Remote:Closing New Session - No more slots\n"));
                    690:             CloseHandle(PipeH[0]);
                    691:             CloseHandle(PipeH[1]);
                    692:             continue;
                    693:         }
                    694: 
                    695:         //
                    696:         //start new thread for this connection
                    697:         //
                    698: 
                    699:         if((ClientList[i].hThread=CreateThread
                    700:             (
                    701:                  (LPSECURITY_ATTRIBUTES)NULL,        // No security attributes.
                    702:                  (DWORD)0,                           // Use same stack size.
                    703:                  (LPTHREAD_START_ROUTINE)RemoteSession, // Thread procedure.
                    704:                  (LPVOID)&ClientList[i],             // Parameter to pass.
                    705:                  (DWORD)0,                           // Run immediately.
                    706:                  (LPDWORD)&ThreadID)
                    707:             )==NULL)
                    708:         {
                    709:             CloseClient(&ClientList[i]);
                    710:             continue;
                    711:         }
                    712:     }
                    713:     return(0);
                    714: }
1.1.1.2 ! root      715: 
        !           716: /*************************************************************/
        !           717: /* Creates a security descriptor with the discrtionry access */
        !           718: /* for the account specified in the /U switch  if any        */
        !           719: /*************************************************************/
        !           720: 
        !           721: BOOL
        !           722: CreateMySecurityDescriptor(
        !           723:     PSECURITY_DESCRIPTOR pSecurityDescriptor,
        !           724:     char *Owner
        !           725:     )
        !           726: {
        !           727:     PSID pOwnerSid;
        !           728:     PACL pAcl;
        !           729:     BOOL Ret=FALSE;
        !           730: 
        !           731:     //
        !           732:     // Initialize the Security Descriptor struct.
        !           733:     //
        !           734: 
        !           735: 
        !           736:     InitializeSecurityDescriptor
        !           737:     (
        !           738:         pSecurityDescriptor,
        !           739:         SECURITY_DESCRIPTOR_REVISION
        !           740:     );
        !           741: 
        !           742:     if (Owner==NULL)
        !           743:     {
        !           744:         //
        !           745:         // No security required.
        !           746:         //
        !           747: 
        !           748:         SetSecurityDescriptorDacl
        !           749:         (
        !           750:                pSecurityDescriptor,
        !           751:                TRUE,
        !           752:                NULL,
        !           753:                FALSE
        !           754:         );
        !           755: 
        !           756:         return TRUE;
        !           757:     }
        !           758: 
        !           759:     {
        !           760:         //
        !           761:         // Get the SID for the account/Group
        !           762:         //
        !           763: 
        !           764:         DWORD len1=1024,len2=1024;
        !           765:         char RefDomain[1024];
        !           766:         SID_NAME_USE snu=0;        //don't care
        !           767: 
        !           768:         if ((pOwnerSid=(PSID)calloc(len1,1))==NULL)
        !           769:             return FALSE;
        !           770: 
        !           771: 
        !           772:         Ret=
        !           773:         LookupAccountName
        !           774:         (
        !           775:             NULL,
        !           776:             Owner,
        !           777:             pOwnerSid,
        !           778:             &len1,
        !           779:             RefDomain,
        !           780:             &len2,
        !           781:             &snu
        !           782:         );
        !           783: 
        !           784:         if (!Ret)
        !           785:         {
        !           786:             free(pOwnerSid);
        !           787:             return FALSE;
        !           788:         }
        !           789: 
        !           790:     }
        !           791: 
        !           792:     {
        !           793: 
        !           794:         //
        !           795:         // Create the access control list with access for
        !           796:         // the SID obtained above.
        !           797:         //
        !           798: 
        !           799:         DWORD aclsize=sizeof(ACL)+
        !           800:                       sizeof(ACCESS_ALLOWED_ACE)+
        !           801:                       GetLengthSid(pOwnerSid)-
        !           802:                       sizeof(DWORD);
        !           803: 
        !           804:         if ((pAcl=(PACL)calloc(1,aclsize))==NULL)
        !           805:         {
        !           806:             free(pOwnerSid);
        !           807:             return FALSE;
        !           808:         }
        !           809: 
        !           810:         //
        !           811:         // Initialize the acl buffer
        !           812:         //
        !           813: 
        !           814:         Ret=
        !           815:         InitializeAcl
        !           816:         (
        !           817:             pAcl,
        !           818:             aclsize,
        !           819:             ACL_REVISION
        !           820:         );
        !           821: 
        !           822:         if (!Ret)
        !           823:         {
        !           824:             free(pOwnerSid);
        !           825:             free(pAcl);
        !           826:             return FALSE;
        !           827:         }
        !           828: 
        !           829:         //
        !           830:         // Add the sid to the access allowed part in ACL
        !           831:         //
        !           832: 
        !           833:         Ret=
        !           834:         AddAccessAllowedAce
        !           835:         (
        !           836:             pAcl,
        !           837:             ACL_REVISION,
        !           838:             GENERIC_ALL,
        !           839:             pOwnerSid
        !           840:         );
        !           841: 
        !           842:         if (!Ret)
        !           843:         {
        !           844:             free(pOwnerSid);
        !           845:             free(pAcl);
        !           846:             return FALSE;
        !           847:         }
        !           848:     }
        !           849: 
        !           850:     //
        !           851:     // Add the created ACL to the discreationary control list
        !           852:     //
        !           853: 
        !           854:     Ret=
        !           855:     SetSecurityDescriptorDacl
        !           856:     (
        !           857:         pSecurityDescriptor,
        !           858:         TRUE,
        !           859:         pAcl,
        !           860:         FALSE
        !           861:     );
        !           862: 
        !           863:     if (!Ret)
        !           864:     {
        !           865:         free(pOwnerSid);
        !           866:         free(pAcl);
        !           867:         return FALSE;
        !           868:     }
        !           869:     return TRUE;
        !           870: }
        !           871: 
1.1       root      872: /*************************************************************/
                    873: /* Manages the Session with a Client - Creates a thread for  */
                    874: /* Inputs from the client and a thread for sending outputs to*/
                    875: /* the client. Could have just as easily done with 1 thread  */
                    876: /* using Asyn IO.                                            */
                    877: /*************************************************************/
                    878: DWORD
                    879: RemoteSession(
                    880:     SESSION_TYPE         *MyClient
                    881:     )
                    882: {
                    883:     DWORD                ReadCnt;
                    884:     SESSION_STARTUPINFO  ssi;
                    885:     char                 *headerbuff;
                    886:     char                 msg[BUFFSIZE];
                    887:     DWORD                tmp;
                    888:     SESSION_STARTREPLY   ssr;
                    889:     SYSTEMTIME           st;
                    890: 
                    891:     GetLocalTime(&st);
                    892:     memset((char *)&ssi,0,sizeof(ssi));
                    893: 
                    894:     //
                    895:     // Open a new handle to the save file ...
                    896:     // contains the saved output from the child process
                    897:     // and the commands already given to it.
                    898:     //
                    899: 
                    900:     if ((MyClient->rSaveFile=CreateFile
                    901:         (
                    902:             SaveFileName,
                    903:             GENERIC_READ|GENERIC_WRITE,
                    904:             FILE_SHARE_READ|FILE_SHARE_WRITE,
                    905:             NULL,OPEN_EXISTING,
                    906:             FILE_ATTRIBUTE_NORMAL,NULL)
                    907:         )==NULL)
                    908: 
                    909:     {
                    910:         CloseClient(MyClient);
                    911:         return(1);
                    912:     }
                    913: 
                    914:     //
                    915:     // Exchange Remote Information with Client.
                    916:     //
                    917: 
                    918:     {
                    919: 
                    920:         DWORD reply=0;
                    921: 
                    922:         ReadFixBytes(MyClient->PipeReadH,(char *)MyClient->Name,HOSTNAMELEN-1,0);
                    923: 
                    924:         //
                    925:         //Last four Bytes contains a code
                    926:         //
                    927: 
                    928:         memcpy((char *)&reply,(char *)&(MyClient->Name[11]),4);
                    929: 
                    930:         if (reply!=MAGICNUMBER)
                    931:         {
                    932:             //
                    933:             // Unknown client
                    934:             //
                    935:             CloseClient(MyClient);
                    936:             return(1);
                    937:         }
                    938: 
                    939:         ssr.MagicNumber=MAGICNUMBER;
                    940:         ssr.Size=sizeof(ssr);
                    941:         ssr.FileSize=GetFileSize( MyClient->rSaveFile, &tmp );
                    942: 
                    943:         WriteFile(MyClient->PipeWriteH,(char *)&ssr,sizeof(ssr),&tmp,NULL);
                    944:     }
                    945: 
                    946:     if (ReadFixBytes(MyClient->PipeReadH,(char *)&(ssi.Size),sizeof(ssi.Size),0)!=0)
                    947:     {
                    948:         CloseClient(MyClient);
                    949:         return(1);
                    950:     }
                    951: 
                    952:     if (ssi.Size>1024)      //Sanity Check
                    953:     {
                    954:         sprintf(msg,"%s","Server:Unknown Header..Terminating session\n");
                    955:         WriteFile(MyClient->PipeWriteH,msg,strlen(msg),&tmp,NULL);
                    956:         CloseClient(MyClient);
                    957:         return(1);
                    958:     }
                    959: 
                    960:     if ((headerbuff=(char *)calloc(ssi.Size,1))==NULL)
                    961:     {
                    962:         sprintf(msg,"%s","Server:Not Enough Memory..Terminating session\n");
                    963:         WriteFile(MyClient->PipeWriteH,msg,strlen(msg),&tmp,NULL);
                    964:         CloseClient(MyClient);
                    965:         return(1);
                    966:     }
                    967: 
                    968:     ReadCnt=ssi.Size-sizeof(ssi.Size);
                    969:     if (ReadFixBytes(MyClient->PipeReadH,(char *)headerbuff,ReadCnt,0)!=0)
                    970:     {
                    971:         CloseClient(MyClient);
                    972:         return(1);
                    973:     }
                    974: 
                    975:     memcpy((char *)&ssi+sizeof(ssi.Size),headerbuff,sizeof(ssi)-sizeof(ssi.Size));
                    976:     free(headerbuff);
                    977: 
                    978:     /* Version  */
                    979:     if (ssi.Version!=VERSION)
                    980:     {
                    981:          sprintf(msg,"Remote Warning:Server Version=%d Client Version=%d\n",VERSION,ssi.Version);
                    982:          WriteFile(MyClient->PipeWriteH,msg,strlen(msg),&tmp,NULL);
                    983: 
                    984:     }
                    985: 
                    986:     /* Name  */
                    987:     {
                    988:         memcpy(MyClient->Name,ssi.ClientName,15);
                    989:         MyClient->Name[14]=0;
                    990: 
                    991:     }
                    992: 
                    993:     /* Lines  */
                    994:     if (ssi.LinesToSend!=-1)
                    995:     {
                    996:         long  PosFromEnd=ssi.LinesToSend*CHARS_PER_LINE;
                    997:         DWORD BytesToSend=MINIMUM((DWORD)PosFromEnd,ssr.FileSize);
                    998:         DWORD BytesRead;
                    999:         char  *buff=(char *)calloc(BytesToSend+1,1);
                   1000: 
                   1001:         if (ssr.FileSize > (DWORD)PosFromEnd)
                   1002:         {
                   1003:             SetFilePointer(
                   1004:                             MyClient->rSaveFile,
                   1005:                             -PosFromEnd,
                   1006:                             (PLONG)NULL,
                   1007:                             FILE_END
                   1008:                           );
                   1009:         }
                   1010: 
                   1011:         if (buff!=NULL)
                   1012:         {
                   1013:             if (!ReadFile(MyClient->rSaveFile,buff,BytesToSend,&BytesRead,NULL))
                   1014:             {
                   1015:                 CloseClient(MyClient);
                   1016:                 return(1);
                   1017:             }
                   1018: 
                   1019:             //
                   1020:             // Don't want the markers to be part of the output display
                   1021:             // at the client end.
                   1022:             //
                   1023:             RemoveInpMark(buff,BytesRead);
                   1024:             if (!WriteFile(MyClient->PipeWriteH,buff,BytesRead,&tmp,NULL))
                   1025:             {
                   1026:                 CloseClient(MyClient);
                   1027:                 return(1);
                   1028:             }
                   1029:         }
                   1030:         free(buff);
                   1031: 
                   1032:     }
                   1033: 
                   1034:     RemoteInfo(printf("\n**Remote:Connected To %s [%02d:%02d]\n",MyClient->Name,st.wHour,st.wMinute),ssi.Flag);
                   1035: 
                   1036:     //
                   1037:     // Start off the new session.
                   1038:     //
                   1039:     NewSession(MyClient);
                   1040: 
                   1041:     RemoteInfo(printf("\n**Remote:Disconnected From %s [%02d:%02d]\n",MyClient->Name,st.wHour,st.wMinute),ssi.Flag);
                   1042:     CloseClient(MyClient);
                   1043:     return(0);
                   1044: }
                   1045: /*************************************************************/
                   1046: DWORD
                   1047: NewSession(
                   1048:     SESSION_TYPE* MyClient
                   1049:     )
                   1050: {
                   1051:     DWORD        ThreadId;
                   1052:     HANDLE       rwThread[3];
                   1053: 
                   1054:     MyClient->MoreData=CreateEvent
                   1055:     (
                   1056:         (LPSECURITY_ATTRIBUTES) NULL,/* address of security attributes */
                   1057:         FALSE,                      /* flag for manual-reset event     */
                   1058:         TRUE,                       /* flag for initial state  */
                   1059:         NULL                        /* address of event-object name    */
                   1060:     );
                   1061: 
                   1062:     if ((rwThread[0]=CreateThread
                   1063:          (
                   1064:             (LPSECURITY_ATTRIBUTES)NULL,         // No security attributes.
                   1065:             (DWORD)0,                            // Use same stack size.
                   1066:             (LPTHREAD_START_ROUTINE)GetClientInput, // Thread procedure.
                   1067:             (LPVOID)MyClient,                    // Parameter to pass.
                   1068:             (DWORD)0,                            // Run immediately.
                   1069:             (LPDWORD)&ThreadId)
                   1070:          )==NULL)
                   1071:     {
                   1072:         return(GetLastError());
                   1073:     }
                   1074: 
                   1075: 
                   1076:     if ((rwThread[1]=CreateThread
                   1077:          (
                   1078:             (LPSECURITY_ATTRIBUTES)NULL,        // No security attributes.
                   1079:             (DWORD)0,                           // Use same stack size.
                   1080:             (LPTHREAD_START_ROUTINE)TransferFileToClient, // Thread procedure.
                   1081:             (LPVOID)MyClient,                    // Parameter to pass.
                   1082:             (DWORD)0,                           // Run immediately.
                   1083:             (LPDWORD)&ThreadId)
                   1084:          )==NULL)
                   1085:     {
                   1086:         CloseHandle(rwThread[0]);
                   1087:         return(GetLastError());
                   1088:     }
                   1089: 
                   1090:     rwThread[2]=ChldProc;
                   1091: 
                   1092:     //
                   1093:     // Wait for either the Input or Output thread
                   1094:     // to terminate and then close the session.
                   1095:     //
                   1096: 
                   1097:     WaitForMultipleObjects(3, rwThread,FALSE, INFINITE);
                   1098: 
                   1099:     TerminateThread(rwThread[0],1);
                   1100:     TerminateThread(rwThread[1],1);
                   1101: 
                   1102:     CloseHandle(rwThread[0]);
                   1103:     CloseHandle(rwThread[0]);
                   1104: 
                   1105:     return(0);
                   1106: }
                   1107: 
                   1108: /*************************************************************/
                   1109: /* Saves the output from the child process into the savefile */
                   1110: /* All the remote client thread and local client thread      */
                   1111: /* open a seperate handle to this and output its content     */
                   1112: /* sequentially.                                             */
                   1113: /*************************************************************/
                   1114: DWORD
                   1115: GetChldOutput(
                   1116:     HANDLE readH
                   1117:     )
                   1118: {
                   1119:     char  buff[BUFFSIZE];
                   1120:     DWORD dread;
                   1121:     DWORD tmp;
                   1122: 
                   1123: 
                   1124:     while(ReadFile(readH,buff,BUFFSIZE-1,&dread,NULL))
                   1125:     {
                   1126:         buff[dread]='\0';
                   1127: 
                   1128:         if (!WriteFile(SaveFile,buff,dread,&tmp,NULL))
                   1129:         {
                   1130:             return(1);
                   1131:         }
                   1132: 
                   1133:         //
                   1134:         // Signal Reader Thread that more data is available
                   1135:         //
                   1136:         {
                   1137:             int i;
                   1138:             for (i=0;i<MAX_SESSION;i++)
                   1139:             {
                   1140:                 if (ClientList[i].Active)
                   1141:                 {
                   1142:                     SetEvent(ClientList[i].MoreData);
                   1143:                 }
                   1144:             }
                   1145:         }
                   1146:     }
                   1147:     return(1);
                   1148: }
                   1149: /*************************************************************/
                   1150: /* A thread for each client connection and one for local IO  */
                   1151: /* Reads the contents of Save file and sends it to client for*/
                   1152: /* display.                                                  */
                   1153: /*************************************************************/
                   1154: DWORD
                   1155: TransferFileToClient(
                   1156:     SESSION_TYPE *MyClient
                   1157:     )
                   1158: {
                   1159: 
                   1160:     char   buffin[BUFFSIZE],buffout[BUFFSIZE],cmdbuff[BUFFSIZE];
                   1161:     DWORD  tmp;
                   1162:     DWORD  dread=0,dwrite=0;
                   1163:     BOOL   incmd=FALSE;
                   1164:     DWORD  cmdP=0;
                   1165:     DWORD  i;
                   1166:     char   MyEchoStr[30];
                   1167: 
                   1168:     sprintf(MyEchoStr,"[%-15s",MyClient->Name);
                   1169: 
                   1170:     while(ReadFile(MyClient->rSaveFile,buffin,BUFFSIZE-1,&dread,NULL))
                   1171:     {
                   1172:         if (dread==0)
                   1173:         {
                   1174:             //
                   1175:             // Event is set by GetChldOutput() func. to signal
                   1176:             // More data is available in save file.
                   1177:             //
                   1178:             WaitForSingleObject(MyClient->MoreData,INFINITE);
                   1179:             continue;
                   1180:         }
                   1181:         dwrite=0;
                   1182: 
                   1183:         //
                   1184:         // This is all to insure that the commands entered
                   1185:         // by clients are not echoed back to them.
                   1186:         // A Beginmark and an Endmark is placed around commands
                   1187:         // sent to the child process from some client.
                   1188:         //
                   1189: 
                   1190:         for(i=0;i<dread;i++)
                   1191:         {
                   1192:             if (incmd)
                   1193:             {
                   1194:                 if ((buffin[i]==ENDMARK)||(cmdP==BUFFSIZE-1))
                   1195:                 {
                   1196:                     incmd=FALSE;
                   1197:                     cmdbuff[cmdP]=0;
                   1198:                     if ((strstr(cmdbuff,MyEchoStr)==NULL)||
                   1199:                         (!MyClient->CommandRcvd))
                   1200:                     {
                   1201:                         if (!WriteFile(
                   1202:                             MyClient->PipeWriteH,
                   1203:                             cmdbuff,cmdP,&tmp,NULL))
                   1204:                         {
                   1205:                             return(1);
                   1206:                         }
                   1207:                     }
                   1208:                     cmdP=0;
                   1209:                 }
                   1210:                 else
                   1211:                 {
                   1212:                     cmdbuff[cmdP++]=buffin[i];
                   1213:                 }
                   1214:             }
                   1215:             else
                   1216:             {
                   1217: 
                   1218:                 if (buffin[i]==BEGINMARK)
                   1219:                 {
                   1220:                     if (dwrite!=0)
                   1221:                     {
                   1222:                         if (!WriteFile(
                   1223:                             MyClient->PipeWriteH,
                   1224:                             buffout,dwrite,&tmp,NULL))
                   1225:                         {
                   1226:                             return(1);
                   1227:                         }
                   1228:                         dwrite=0;
                   1229:                     }
                   1230:                     incmd=TRUE;
                   1231:                     continue;
                   1232:                 }
                   1233:                 else
                   1234:                 {
                   1235:                     buffout[dwrite++]=buffin[i];
                   1236:                 }
                   1237:             }
                   1238:         }
                   1239: 
                   1240:         if (dwrite!=0)
                   1241:         {
                   1242:             if (!WriteFile(
                   1243:                 MyClient->PipeWriteH,
                   1244:                 buffout,dwrite,&tmp,NULL))
                   1245:             {
                   1246:                 return(0);
                   1247:             }
                   1248:         }
                   1249:     }
                   1250:     return(1);
                   1251: }
                   1252: 
                   1253: /*************************************************************/
                   1254: /* Commands from the clients are sent to the child process   */
                   1255: /* and also saved in the SaveFile with Begin and End markers */
                   1256: /* around them to seperate them from the output from child   */
                   1257: /* process.                                                  */
                   1258: /*************************************************************/
                   1259: DWORD
                   1260: GetClientInput(
                   1261:     SESSION_TYPE *MyClient
                   1262:     )
                   1263: {
                   1264:     char buff[BUFFSIZE];
                   1265:     DWORD tmp,dread;
                   1266: 
                   1267:     while(ReadFile(MyClient->PipeReadH,buff,BUFFSIZE,&dread,NULL))
                   1268:     {
                   1269:         buff[dread]=0;
                   1270:         MyClient->CommandRcvd=TRUE;
                   1271: 
                   1272:         if (FilterCommand(MyClient,buff,dread))
                   1273:             continue;
                   1274: 
                   1275: 
                   1276:         if (!WriteFile(ChildStdInp,buff,dread,&tmp,NULL))
                   1277:         {
                   1278:             ExitThread(0);
                   1279:         }
                   1280:     }
                   1281:     return(1);
                   1282: }
                   1283: 
                   1284: /*************************************************************/
                   1285: /* If a client command is intended for the Remote server -   */
                   1286: /* those beginning with COMMANDCHAR (are not intended        */
                   1287: /* for the child process) - they are executed here           */
                   1288: /* and the output sent to the client.                        */
                   1289: /*************************************************************/
                   1290: BOOL
                   1291: FilterCommand(
                   1292:     SESSION_TYPE *cl,
                   1293:     char *buff,
                   1294:     int dread
                   1295:     )
                   1296: {
                   1297:     SYSTEMTIME st;
                   1298:     char       inp_buff[4096];
                   1299:     char       tmpchar;
                   1300:     char       ch[3];
                   1301:     DWORD      tmp;
                   1302:     int        len;
                   1303:     DWORD      ThreadID; //Useless
                   1304: 
                   1305:     if (dread==0)
                   1306:         return(FALSE);
                   1307: 
                   1308:     buff[dread]=0;
                   1309: 
                   1310:     GetLocalTime(&st);
                   1311: 
                   1312: 
                   1313:     if (buff[0]==COMMANDCHAR)
                   1314:     {
                   1315:         switch(buff[1])
                   1316:         {
                   1317:             case 'o':
                   1318:             case 'O':
                   1319:                 cl->SendOutput=!cl->SendOutput;
                   1320:                 break;
                   1321: 
                   1322:             case 'k':
                   1323:             case 'K':
                   1324:                 TerminateProcess(ChldProc,1);
                   1325:                 break;
                   1326: 
                   1327:             case 's':
                   1328:             case 'S':
                   1329:                  SendStatus(cl->PipeWriteH);
                   1330:                  break;
                   1331: 
                   1332:             case 'p':
                   1333:             case 'P':
                   1334:                 {
                   1335:                     char  *mssg=(char *)calloc(4096,1); //Free it in called Proc
                   1336:                     char  *ack="Remote:Popup Shown..\n";
                   1337: 
                   1338:                     if (mssg==NULL)
                   1339:                         break;
                   1340: 
                   1341:                     sprintf(mssg,"From %s [%d:%d]\n\n%s\n",cl->Name,st.wHour,st.wMinute,&buff[2]);
                   1342:                     CreateThread(
                   1343:                           (LPSECURITY_ATTRIBUTES)NULL,         // No security attributes.
                   1344:                           (DWORD)0,              // Use same stack size.
                   1345:                           (LPTHREAD_START_ROUTINE)ShowPopup, // Thread procedure.
                   1346:                           (LPVOID)mssg,          // Parameter to pass.
                   1347:                           (DWORD)0,              // Run immediately.
                   1348:                           (LPDWORD)&ThreadID
                   1349:                          );
                   1350:                     WriteFile(cl->PipeWriteH,ack,strlen(ack),&tmp,NULL);
                   1351:                     break;
                   1352:                  }
                   1353: 
                   1354:             case 'm':
                   1355:             case 'M':
                   1356:                 buff[dread-2]=0;
                   1357:                 CMDSTRING(inp_buff,buff,cl,st);
                   1358:                 len=strlen(inp_buff);
                   1359:                 WriteFile(SaveFile,inp_buff,len,&tmp,NULL);
                   1360:                 break;
                   1361: 
                   1362:             case '@':
                   1363:                 buff[dread-2]=0;
                   1364:                 CMDSTRING(inp_buff,&buff[1],cl,st);
                   1365:                 len=strlen(inp_buff);
                   1366:                 WriteFile(SaveFile,inp_buff,len,&tmp,NULL);
                   1367:                 //
                   1368:                 // Remove the first @ sign
                   1369:                 //
                   1370:                 MoveMemory(buff,&buff[1],dread-1);
                   1371:                 buff[dread-1]=' ';
                   1372:                 return(FALSE); //Send it it to the chile process
                   1373:                 break;
                   1374: 
                   1375: 
                   1376:             default :
                   1377:                 sprintf(inp_buff,"%s","** Unknown Command **\n");
                   1378:                 WriteFile(cl->PipeWriteH,inp_buff,strlen(inp_buff),&tmp,NULL);
                   1379: 
                   1380:             case 'h':
                   1381:             case 'H':
                   1382:                  sprintf(inp_buff,"%cM: To Send Message\n",COMMANDCHAR);
                   1383:                  WriteFile(cl->PipeWriteH,inp_buff,strlen(inp_buff),&tmp,NULL);
                   1384:                  sprintf(inp_buff,"%cP: To Generate popup\n",COMMANDCHAR);
                   1385:                  WriteFile(cl->PipeWriteH,inp_buff,strlen(inp_buff),&tmp,NULL);
                   1386:                  sprintf(inp_buff,"%cK: To kill the server\n",COMMANDCHAR);
                   1387:                  WriteFile(cl->PipeWriteH,inp_buff,strlen(inp_buff),&tmp,NULL);
                   1388:                  sprintf(inp_buff,"%cH: This Help\n",COMMANDCHAR);
                   1389:                  WriteFile(cl->PipeWriteH,inp_buff,strlen(inp_buff),&tmp,NULL);
                   1390:                  break;
                   1391:         }
                   1392:         return(TRUE);
                   1393:     }
                   1394: 
                   1395: 
                   1396:     if ((buff[0]<26))
                   1397:     {
                   1398:         BOOL ret=FALSE;
                   1399: 
                   1400:         sprintf(ch,"^%c",buff[0]+64);
                   1401:         CMDSTRING(inp_buff,ch,cl,st);
                   1402:         len=strlen(inp_buff);
                   1403: 
                   1404:         if (buff[0]==CTRLC)
                   1405:         {
                   1406:             cl->CommandRcvd=FALSE;
                   1407:             GenerateConsoleCtrlEvent(CTRL_C_EVENT,0);
                   1408:             ret=TRUE; //Already sent to child
                   1409:         }
                   1410: 
                   1411:         WriteFile(SaveFile,inp_buff,len,&tmp,NULL);
                   1412:         return(ret); //FALSE:send it to child StdIn
                   1413:     }
                   1414: 
                   1415:     tmpchar=buff[dread-2]; //must be 13;but just incase
                   1416:     buff[dread-2]=0;
                   1417:     CMDSTRING(inp_buff,buff,cl,st);
                   1418:     buff[dread-2]=tmpchar;
                   1419:     len=strlen(inp_buff);
                   1420:     WriteFile(SaveFile,inp_buff,len,&tmp,NULL);
                   1421:     return(FALSE);
                   1422: }
                   1423: /*************************************************************/
                   1424: VOID
                   1425: SendStatus(
                   1426:     HANDLE hClientPipe
                   1427:     )
                   1428: {
                   1429:     char  buff[1024];
                   1430:     int   i;
                   1431:     DWORD tmp;
                   1432:     char  *env=(char *)GetEnvironmentStrings();
                   1433:     DWORD ver=GetVersion();
                   1434: 
                   1435:     sprintf(buff,"Command = %s\n",ChildCmd);
                   1436:     WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1437: 
                   1438:     sprintf(buff,"Server = %s PIPE=%s\n",HostName,PipeName);
                   1439:     WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1440: 
                   1441:     sprintf(buff,"Build = %d \n",((WORD *)&ver)[1]);
                   1442:     WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1443: 
                   1444:     for (i=1;i<MAX_SESSION;i++)
                   1445:     {
                   1446:         if (ClientList[i].Active)
                   1447:         {
                   1448:             sprintf(buff,"ACTIVE SESSION=%s\n",ClientList[i].Name);
                   1449:             WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1450:         }
                   1451:     }
                   1452: 
                   1453:     sprintf(buff,"====================\n",Server,PipeName);
                   1454:     WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1455: 
                   1456:     sprintf(buff,"ENVIRONMENT VARIABLES\n",Server,PipeName);
                   1457:     WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1458: 
                   1459:     sprintf(buff,"====================\n",Server,PipeName);
                   1460:     WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1461: 
                   1462: 
                   1463:     try
                   1464:     {
                   1465:         while (*env!=0)
                   1466:         {
                   1467:             sprintf(buff,"%s\n",env);
                   1468:             WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1469: 
                   1470:             while(*(env++)!=0);
                   1471:         }
                   1472:     }
                   1473:     except(EXCEPTION_EXECUTE_HANDLER)
                   1474:     {
                   1475:         sprintf(buff,"Exception Generated Getting Environment Block\n",env);
                   1476:         WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1477: 
                   1478:     }
                   1479: 
                   1480:     sprintf(buff,"====================\n",Server,PipeName);
                   1481:     WriteFile(hClientPipe,buff,strlen(buff),&tmp,NULL);
                   1482:     return;
                   1483: }
                   1484: /*************************************************************/
                   1485: DWORD
                   1486: ShowPopup(
                   1487:     char *mssg
                   1488:     )
                   1489: {
                   1490:     MessageBox(GetActiveWindow(),mssg,"***REMOTE***",MB_OK|MB_SETFOREGROUND);
                   1491:     free(mssg);
                   1492:     return(0);
                   1493: 
                   1494: }
                   1495: /*************************************************************/
                   1496: BOOL SrvCtrlHand(
                   1497:     DWORD event
                   1498:     )
                   1499: {
                   1500:     if (event==CTRL_BREAK_EVENT)
                   1501:     {
                   1502:         TerminateProcess(ChldProc,1);
                   1503:     }
                   1504:     return(TRUE);
                   1505: }
                   1506: /*************************************************************/
                   1507: 
                   1508: DWORD   LocalSession(PVOID noarg)
                   1509: {
                   1510:     //Local is ClientList[0]
                   1511:     char *name=(char *)ClientList[0].Name;
                   1512: 
                   1513:     strcpy(name,LOCALNAME);
                   1514:     if ((ClientList[0].rSaveFile=CreateFile(SaveFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL))==NULL)
                   1515:     {
                   1516:         WRITEF((VBuff,"Remote:Cannot open ReadHandle to Savefile:%d\n",GetLastError()));
                   1517:         ClientList[0].Active=FALSE;
                   1518:         return(1);
                   1519: 
                   1520:     }
                   1521: 
                   1522:     ClientList[0].PipeReadH=GetStdHandle(STD_INPUT_HANDLE);
                   1523:     ClientList[0].PipeWriteH=GetStdHandle(STD_OUTPUT_HANDLE);
                   1524:     ClientList[0].SendOutput=TRUE;
                   1525:     ClientList[0].Active=TRUE;
                   1526:     NewSession(&ClientList[0]);
                   1527:     CloseClient(&ClientList[0]);
                   1528:     return(0);
                   1529: }
                   1530: 
                   1531: VOID
                   1532: CloseClient(
                   1533:     SESSION_TYPE *Client
                   1534:     )
                   1535: {
                   1536:     ZeroMemory(Client->Name,HOSTNAMELEN);
                   1537: 
                   1538:     if (Client->PipeReadH!=INVALID_HANDLE_VALUE)
                   1539:     {
                   1540:         CloseHandle(Client->PipeReadH);
                   1541:         Client->PipeReadH=INVALID_HANDLE_VALUE;
                   1542:     }
                   1543: 
                   1544:     if (Client->PipeWriteH!=INVALID_HANDLE_VALUE)
                   1545:     {
                   1546:         CloseHandle(Client->PipeWriteH);
                   1547:         Client->PipeWriteH=INVALID_HANDLE_VALUE;
                   1548:     }
                   1549: 
                   1550:     if (Client->rSaveFile!=INVALID_HANDLE_VALUE)
                   1551:     {
                   1552:         CloseHandle(Client->rSaveFile);
                   1553:         Client->rSaveFile=INVALID_HANDLE_VALUE;
                   1554:     }
                   1555:     if (Client->MoreData!=NULL)
                   1556:     {
                   1557:         CloseHandle(Client->MoreData);
                   1558:         Client->MoreData=NULL;
                   1559:     }
                   1560: 
                   1561:     Client->Active=FALSE; //Keep it last else synch problem.
                   1562:     return;
                   1563: }
                   1564: 
                   1565: VOID
                   1566: InitClientList(
                   1567:     )
                   1568: {
                   1569:     int i;
                   1570:     for (i=0;i<MAX_SESSION;i++)
                   1571:     {
                   1572:         ZeroMemory(ClientList[i].Name,HOSTNAMELEN);
                   1573:         ClientList[i].PipeReadH=INVALID_HANDLE_VALUE;
                   1574:         ClientList[i].PipeWriteH=INVALID_HANDLE_VALUE;
                   1575:         ClientList[i].rSaveFile=INVALID_HANDLE_VALUE;
                   1576:         ClientList[i].MoreData=NULL;
                   1577:         ClientList[i].Active=FALSE;
                   1578:         ClientList[i].CommandRcvd=FALSE;
                   1579:         ClientList[i].SendOutput=FALSE;
                   1580:         ClientList[i].hThread=NULL;
                   1581:     }
                   1582:     return;
                   1583: }
                   1584: 
                   1585: 
                   1586: 
                   1587: VOID
                   1588: RemoveInpMark(
                   1589:     char* Buff,
                   1590:     DWORD Size
                   1591:     )
                   1592: 
                   1593: {
                   1594:     DWORD i;
                   1595:     for (i=0;i<Size;i++)
                   1596:     {
                   1597:         switch (Buff[i])
                   1598:         {
                   1599:         case BEGINMARK:
                   1600:             Buff[i]=' ';
                   1601:             break;
                   1602: 
                   1603:         case ENDMARK:
                   1604:             if (i<2)
                   1605:             {
                   1606:                 Buff[i]= ' ';
                   1607:             }
                   1608:             else
                   1609:             {
                   1610:                 Buff[i]  =Buff[i-1];
                   1611:                 Buff[i-1]=Buff[i-2];
                   1612:                 Buff[i-2]=' ';
                   1613:             }
                   1614:             break;
                   1615: 
                   1616:         default:
                   1617:            break;
                   1618:        }
                   1619:     }
                   1620: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.