home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / mstools / MSRMesh-VirtualWIFI.MSI / VirtualWiFiSvc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  7.6 KB  |  251 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\Service
  4.  * File Name: VirtualWiFiSvc.cpp
  5.  * Purpose  : Implements the body the Service
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <sddl.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <process.h>
  13. #include <tchar.h>
  14. #include "service.h"
  15.  
  16. FILE *fpConfig;
  17. // this event is signalled when the
  18. // service should end
  19. //
  20. HANDLE  hServerStopEvent = NULL;
  21.  
  22. extern VOID ServiceLoop(HANDLE *hEvents);
  23.  
  24. // CreateMyDACL
  25. // Creates a security descriptor containing the
  26. // desired DACL. This function uses SDDL to make Deny and Allow ACEs.
  27. //
  28. // Parameter:
  29. //     SECURITY_ATTRIBUTES * pSA
  30. // Address to a SECURITY_ATTRIBUTES structure. It is the caller's
  31. // responsibility to properly initialize the structure, and to free 
  32. // the structure's lpSecurityDescriptor member when done (by calling
  33. // the LocalFree function).
  34. // 
  35. // Return value:
  36. //    FALSE if the address to the structure is NULL. 
  37. //    Otherwise, this function returns the value from the
  38. //    ConvertStringSecurityDescriptorToSecurityDescriptor function.
  39. BOOL CreateMyDACL(SECURITY_ATTRIBUTES * pSA)
  40. {
  41.     // Define the SDDL for the DACL. This example sets 
  42.     // the following access:
  43.     //     Built-in guests are denied all access.
  44.     //     Anonymous Logon is denied all access.
  45.     //     Authenticated Users are allowed read/write/execute access.
  46.     //     Administrators are allowed full control.
  47.     // Modify these values as needed to generate the proper
  48.     // DACL for your application. 
  49.     TCHAR * szSD = "D:"                   // Discretionary ACL
  50.                    "(D;OICI;GA;;;BG)"     // Deny access to Built-in Guests
  51.                    "(D;OICI;GA;;;AN)"     // Deny access to Anonymous Logon
  52.                    "(A;OICI;GRGWGX;;;AU)" // Allow read/write/execute to Authenticated Users
  53.                    "(A;OICI;GA;;;BA)";    // Allow full control to Administrators
  54.  
  55.     if (NULL == pSA)
  56.         return FALSE;
  57.  
  58.     return ConvertStringSecurityDescriptorToSecurityDescriptor(
  59.                                                               szSD,
  60.                                                               SDDL_REVISION_1,
  61.                                                               &(pSA->lpSecurityDescriptor),
  62.                                                               NULL);
  63. }
  64.  
  65.  
  66. //
  67. //  FUNCTION: ServiceStart
  68. //
  69. //  PURPOSE: Actual code of the service that does the work.
  70. //
  71. //  PARAMETERS:
  72. //    dwArgc   - number of command line arguments
  73. //    lpszArgv - array of command line arguments
  74. //
  75. //  RETURN VALUE:
  76. //    none
  77. //
  78. //  COMMENTS:
  79. //    The default behavior is to open a
  80. //    named pipe, \\.\pipe\simple, and read
  81. //    from it.  It the modifies the data and
  82. //    writes it back to the pipe.  The service
  83. //    stops when hServerStopEvent is signalled
  84. //
  85. VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
  86. {
  87.    HANDLE                  hPipe = INVALID_HANDLE_VALUE;
  88.    HANDLE                  hEvents[2] = {NULL, NULL};
  89.    OVERLAPPED              os;
  90.    PSECURITY_DESCRIPTOR    pSD = NULL;
  91.    SECURITY_ATTRIBUTES     sa;
  92.    TCHAR                   szIn[80];
  93.    TCHAR                   szOut[ (sizeof(szIn) / sizeof(TCHAR) )  + 100];
  94.    LPTSTR                  lpszPipeName = TEXT("\\\\.\\pipe\\simple");
  95.    BOOL                    bRet;
  96.    DWORD                   cbRead;
  97.    DWORD                   cbWritten;
  98.    DWORD                   dwWait;
  99.    UINT                    ndx;
  100.  
  101.    ///////////////////////////////////////////////////
  102.    //
  103.    // Service initialization
  104.    //
  105.  
  106.    // report the status to the service control manager.
  107.    //
  108.    if (!ReportStatusToSCMgr(
  109.                            SERVICE_START_PENDING, // service state
  110.                            NO_ERROR,              // exit code
  111.                            3000))                 // wait hint
  112.       goto cleanup;
  113.  
  114.    // create the event object. The control handler function signals
  115.    // this event when it receives the "stop" control code.
  116.    //
  117.    hServerStopEvent = CreateEvent(
  118.                                  NULL,    // no security attributes
  119.                                  TRUE,    // manual reset event
  120.                                  FALSE,   // not-signalled
  121.                                  NULL);   // no name
  122.  
  123.    if ( hServerStopEvent == NULL)
  124.       goto cleanup;
  125.  
  126.    hEvents[0] = hServerStopEvent;
  127.  
  128.    // report the status to the service control manager.
  129.    //
  130.    if (!ReportStatusToSCMgr(
  131.                            SERVICE_START_PENDING, // service state
  132.                            NO_ERROR,              // exit code
  133.                            3000))                 // wait hint
  134.       goto cleanup;
  135.  
  136.    // create the event object object use in overlapped i/o
  137.    //
  138.    hEvents[1] = CreateEvent(
  139.                            NULL,    // no security attributes
  140.                            TRUE,    // manual reset event
  141.                            FALSE,   // not-signalled
  142.                            NULL);   // no name
  143.  
  144.    if ( hEvents[1] == NULL)
  145.       goto cleanup;
  146.  
  147.    // report the status to the service control manager.
  148.    //
  149.    if (!ReportStatusToSCMgr(
  150.                            SERVICE_START_PENDING, // service state
  151.                            NO_ERROR,              // exit code
  152.                            3000))                 // wait hint
  153.       goto cleanup;
  154.  
  155.    // create a security descriptor that allows anyone to write to
  156.    //  the pipe...
  157.    //
  158.    pSD = (PSECURITY_DESCRIPTOR) malloc( SECURITY_DESCRIPTOR_MIN_LENGTH );
  159.  
  160.    if (pSD == NULL)
  161.       goto cleanup;
  162.  
  163.    if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
  164.       goto cleanup;
  165.  
  166.    sa.nLength = sizeof(sa);
  167.    sa.bInheritHandle = TRUE;
  168.    sa.lpSecurityDescriptor = pSD;
  169.  
  170.    if(!CreateMyDACL(&sa) )
  171.    {
  172.        // DACL creation FAILED!!
  173.        return;
  174.    }
  175.  
  176.    // report the status to the service control manager.
  177.    //
  178.    if (!ReportStatusToSCMgr(
  179.                            SERVICE_START_PENDING, // service state
  180.                            NO_ERROR,              // exit code
  181.                            3000))                 // wait hint
  182.       goto cleanup;
  183.  
  184.  
  185.  
  186.    // report the status to the service control manager.
  187.    //
  188.    if (!ReportStatusToSCMgr(
  189.                            SERVICE_RUNNING,       // service state
  190.                            NO_ERROR,              // exit code
  191.                            0))                    // wait hint
  192.       goto cleanup;
  193.  
  194.    //
  195.    // End of initialization
  196.    //
  197.    ////////////////////////////////////////////////////////
  198.  
  199.    ////////////////////////////////////////////////////////
  200.    //
  201.    // Service is now running, perform work until shutdown
  202.    //
  203.  
  204.    ServiceLoop(hEvents);
  205.  
  206.    cleanup:
  207.  
  208.    if (hPipe != INVALID_HANDLE_VALUE )
  209.       CloseHandle(hPipe);
  210.  
  211.    if (hServerStopEvent)
  212.       CloseHandle(hServerStopEvent);
  213.  
  214.    if (hEvents[1]) // overlapped i/o event
  215.       CloseHandle(hEvents[1]);
  216.  
  217.    if ( pSD )
  218.       free( pSD );
  219.  
  220. }
  221.  
  222.  
  223. //
  224. //  FUNCTION: ServiceStop
  225. //
  226. //  PURPOSE: Stops the service
  227. //
  228. //  PARAMETERS:
  229. //    none
  230. //
  231. //  RETURN VALUE:
  232. //    none
  233. //
  234. //  COMMENTS:
  235. //    If a ServiceStop procedure is going to
  236. //    take longer than 3 seconds to execute,
  237. //    it should spawn a thread to execute the
  238. //    stop code, and return.  Otherwise, the
  239. //    ServiceControlManager will believe that
  240. //    the service has stopped responding.
  241. //
  242. VOID ServiceStop()
  243. {
  244.     char lpOutputString[50];
  245.     fclose(fpConfig);
  246.     sprintf(lpOutputString, "Sent Stop Event to ServiceLoop\n");
  247.     OutputDebugString(TEXT(lpOutputString));
  248.     if ( hServerStopEvent )
  249.       SetEvent(hServerStopEvent);
  250. }
  251.