home *** CD-ROM | disk | FTP | other *** search
/ DEFCON 15 / DefCon15.bin / Speakers / Jennings / Extras / incognito / incognito_service.c < prev    next >
C/C++ Source or Header  |  2007-03-12  |  4KB  |  137 lines

  1. #define _CRT_SECURE_NO_DEPRECATE 1
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "handle_arguments.h"
  5.  
  6. #define BUFFER_SIZE        500
  7.  
  8. SERVICE_STATUS ServiceStatus; 
  9. SERVICE_STATUS_HANDLE hStatus; 
  10.  
  11. void ServiceMain(int argc, char** argv); 
  12. void ControlHandler(DWORD request); 
  13. BOOL InitializePipe(LPCTSTR lpszPipe);
  14.  
  15. HANDLE hPipeR, hPipeW;
  16.  
  17. void main() 
  18.     SERVICE_TABLE_ENTRY ServiceTable[2];
  19.     ServiceTable[0].lpServiceName = "incognito_service";
  20.     ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
  21.  
  22.     ServiceTable[1].lpServiceName = NULL;
  23.     ServiceTable[1].lpServiceProc = NULL;
  24.  
  25.     // Start the control dispatcher thread for our service
  26.     StartServiceCtrlDispatcher(ServiceTable);  
  27. }
  28.  
  29. void ServiceMain(int argc, char** argv) 
  30.     ServiceStatus.dwServiceType        = SERVICE_WIN32; 
  31.     ServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
  32.     ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  33.     ServiceStatus.dwWin32ExitCode      = 0; 
  34.     ServiceStatus.dwServiceSpecificExitCode = 0; 
  35.     ServiceStatus.dwCheckPoint         = 0; 
  36.     ServiceStatus.dwWaitHint           = 0; 
  37.  
  38.     hStatus = RegisterServiceCtrlHandler(
  39.         "incognito_service", 
  40.         (LPHANDLER_FUNCTION)ControlHandler); 
  41.  
  42.     if (hStatus == (SERVICE_STATUS_HANDLE)0) 
  43.     { 
  44.         // Registering Control Handler failed
  45.         return; 
  46.     }  
  47.  
  48.     // We report the running status to SCM. 
  49.     ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
  50.     SetServiceStatus (hStatus, &ServiceStatus);
  51.  
  52.     InitializePipe(argv[1]);
  53.     Sleep(100);
  54.  
  55.     OUTPUT = hPipeW;
  56.     INPUT = hPipeR;
  57.  
  58.     handle_options(argc-1, ++argv);
  59.  
  60.     ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
  61.     SetServiceStatus (hStatus, &ServiceStatus);
  62.  
  63.     Sleep(100);
  64.     return; 
  65. }
  66.  
  67. // Control handler function
  68. void ControlHandler(DWORD request) 
  69.     switch(request) 
  70.     { 
  71.         case SERVICE_CONTROL_STOP: 
  72.             ServiceStatus.dwWin32ExitCode = 0; 
  73.             ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
  74.             SetServiceStatus (hStatus, &ServiceStatus);
  75.             return; 
  76.  
  77.         case SERVICE_CONTROL_SHUTDOWN: 
  78.             ServiceStatus.dwWin32ExitCode = 0; 
  79.             ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
  80.             SetServiceStatus (hStatus, &ServiceStatus);
  81.             return; 
  82.         
  83.         default:
  84.             break;
  85.     } 
  86.  
  87.     // Report current status
  88.     SetServiceStatus (hStatus,  &ServiceStatus);
  89.  
  90.     return; 
  91.  
  92. BOOL InitializePipe(LPCTSTR lpszPipe)
  93. {
  94.     char szPipeName[MAX_PATH];
  95.  
  96.     memset(szPipeName, 0, MAX_PATH);
  97.     if (_snprintf(szPipeName, MAX_PATH, "\\\\.\\pipe\\%s", lpszPipe) == -1)
  98.         szPipeName[sizeof(szPipeName)-1] = '\0';
  99.     
  100.     hPipeW = CreateNamedPipeA(szPipeName, 
  101.                             PIPE_ACCESS_DUPLEX, // read/write access 
  102.                             PIPE_TYPE_MESSAGE | // message type pipe 
  103.                             PIPE_READMODE_MESSAGE | // message-read mode 
  104.                             PIPE_WAIT, // blocking mode 
  105.                             PIPE_UNLIMITED_INSTANCES, // max. instances 
  106.                             BUFFER_SIZE, // output buffer size 
  107.                             BUFFER_SIZE, // input buffer size 
  108.                             10000, // client time-out 
  109.                             NULL); // no security attribute 
  110.  
  111.     ConnectNamedPipe(hPipeW, NULL);
  112.  
  113.     hPipeR = CreateNamedPipeA(szPipeName, 
  114.                             PIPE_ACCESS_DUPLEX, // read/write access 
  115.                             PIPE_TYPE_MESSAGE | // message type pipe 
  116.                             PIPE_READMODE_MESSAGE | // message-read mode 
  117.                             PIPE_WAIT, // blocking mode 
  118.                             PIPE_UNLIMITED_INSTANCES, // max. instances 
  119.                             BUFFER_SIZE, // output buffer size 
  120.                             BUFFER_SIZE, // input buffer size 
  121.                             10000, // client time-out 
  122.                             NULL); // no security attribute 
  123.  
  124.     ConnectNamedPipe(hPipeR, NULL);
  125.  
  126.     if (hPipeR == INVALID_HANDLE_VALUE || hPipeW == INVALID_HANDLE_VALUE) 
  127.     {
  128.         return FALSE;
  129.     }
  130.     
  131.     return TRUE;
  132. }
  133.