home *** CD-ROM | disk | FTP | other *** search
- /*
- * Author : Ranveer Chandra
- * Directory: VirtualWiFi_Root\Service
- * File Name: VirtualWiFiSvc.cpp
- * Purpose : Implements the body the Service
- */
-
- #include <windows.h>
- #include <sddl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <process.h>
- #include <tchar.h>
- #include "service.h"
-
- FILE *fpConfig;
- // this event is signalled when the
- // service should end
- //
- HANDLE hServerStopEvent = NULL;
-
- extern VOID ServiceLoop(HANDLE *hEvents);
-
- // CreateMyDACL
- // Creates a security descriptor containing the
- // desired DACL. This function uses SDDL to make Deny and Allow ACEs.
- //
- // Parameter:
- // SECURITY_ATTRIBUTES * pSA
- // Address to a SECURITY_ATTRIBUTES structure. It is the caller's
- // responsibility to properly initialize the structure, and to free
- // the structure's lpSecurityDescriptor member when done (by calling
- // the LocalFree function).
- //
- // Return value:
- // FALSE if the address to the structure is NULL.
- // Otherwise, this function returns the value from the
- // ConvertStringSecurityDescriptorToSecurityDescriptor function.
- BOOL CreateMyDACL(SECURITY_ATTRIBUTES * pSA)
- {
- // Define the SDDL for the DACL. This example sets
- // the following access:
- // Built-in guests are denied all access.
- // Anonymous Logon is denied all access.
- // Authenticated Users are allowed read/write/execute access.
- // Administrators are allowed full control.
- // Modify these values as needed to generate the proper
- // DACL for your application.
- TCHAR * szSD = "D:" // Discretionary ACL
- "(D;OICI;GA;;;BG)" // Deny access to Built-in Guests
- "(D;OICI;GA;;;AN)" // Deny access to Anonymous Logon
- "(A;OICI;GRGWGX;;;AU)" // Allow read/write/execute to Authenticated Users
- "(A;OICI;GA;;;BA)"; // Allow full control to Administrators
-
- if (NULL == pSA)
- return FALSE;
-
- return ConvertStringSecurityDescriptorToSecurityDescriptor(
- szSD,
- SDDL_REVISION_1,
- &(pSA->lpSecurityDescriptor),
- NULL);
- }
-
-
- //
- // FUNCTION: ServiceStart
- //
- // PURPOSE: Actual code of the service that does the work.
- //
- // PARAMETERS:
- // dwArgc - number of command line arguments
- // lpszArgv - array of command line arguments
- //
- // RETURN VALUE:
- // none
- //
- // COMMENTS:
- // The default behavior is to open a
- // named pipe, \\.\pipe\simple, and read
- // from it. It the modifies the data and
- // writes it back to the pipe. The service
- // stops when hServerStopEvent is signalled
- //
- VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
- {
- HANDLE hPipe = INVALID_HANDLE_VALUE;
- HANDLE hEvents[2] = {NULL, NULL};
- OVERLAPPED os;
- PSECURITY_DESCRIPTOR pSD = NULL;
- SECURITY_ATTRIBUTES sa;
- TCHAR szIn[80];
- TCHAR szOut[ (sizeof(szIn) / sizeof(TCHAR) ) + 100];
- LPTSTR lpszPipeName = TEXT("\\\\.\\pipe\\simple");
- BOOL bRet;
- DWORD cbRead;
- DWORD cbWritten;
- DWORD dwWait;
- UINT ndx;
-
- ///////////////////////////////////////////////////
- //
- // Service initialization
- //
-
- // report the status to the service control manager.
- //
- if (!ReportStatusToSCMgr(
- SERVICE_START_PENDING, // service state
- NO_ERROR, // exit code
- 3000)) // wait hint
- goto cleanup;
-
- // create the event object. The control handler function signals
- // this event when it receives the "stop" control code.
- //
- hServerStopEvent = CreateEvent(
- NULL, // no security attributes
- TRUE, // manual reset event
- FALSE, // not-signalled
- NULL); // no name
-
- if ( hServerStopEvent == NULL)
- goto cleanup;
-
- hEvents[0] = hServerStopEvent;
-
- // report the status to the service control manager.
- //
- if (!ReportStatusToSCMgr(
- SERVICE_START_PENDING, // service state
- NO_ERROR, // exit code
- 3000)) // wait hint
- goto cleanup;
-
- // create the event object object use in overlapped i/o
- //
- hEvents[1] = CreateEvent(
- NULL, // no security attributes
- TRUE, // manual reset event
- FALSE, // not-signalled
- NULL); // no name
-
- if ( hEvents[1] == NULL)
- goto cleanup;
-
- // report the status to the service control manager.
- //
- if (!ReportStatusToSCMgr(
- SERVICE_START_PENDING, // service state
- NO_ERROR, // exit code
- 3000)) // wait hint
- goto cleanup;
-
- // create a security descriptor that allows anyone to write to
- // the pipe...
- //
- pSD = (PSECURITY_DESCRIPTOR) malloc( SECURITY_DESCRIPTOR_MIN_LENGTH );
-
- if (pSD == NULL)
- goto cleanup;
-
- if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
- goto cleanup;
-
- sa.nLength = sizeof(sa);
- sa.bInheritHandle = TRUE;
- sa.lpSecurityDescriptor = pSD;
-
- if(!CreateMyDACL(&sa) )
- {
- // DACL creation FAILED!!
- return;
- }
-
- // report the status to the service control manager.
- //
- if (!ReportStatusToSCMgr(
- SERVICE_START_PENDING, // service state
- NO_ERROR, // exit code
- 3000)) // wait hint
- goto cleanup;
-
-
-
- // report the status to the service control manager.
- //
- if (!ReportStatusToSCMgr(
- SERVICE_RUNNING, // service state
- NO_ERROR, // exit code
- 0)) // wait hint
- goto cleanup;
-
- //
- // End of initialization
- //
- ////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////
- //
- // Service is now running, perform work until shutdown
- //
-
- ServiceLoop(hEvents);
-
- cleanup:
-
- if (hPipe != INVALID_HANDLE_VALUE )
- CloseHandle(hPipe);
-
- if (hServerStopEvent)
- CloseHandle(hServerStopEvent);
-
- if (hEvents[1]) // overlapped i/o event
- CloseHandle(hEvents[1]);
-
- if ( pSD )
- free( pSD );
-
- }
-
-
- //
- // FUNCTION: ServiceStop
- //
- // PURPOSE: Stops the service
- //
- // PARAMETERS:
- // none
- //
- // RETURN VALUE:
- // none
- //
- // COMMENTS:
- // If a ServiceStop procedure is going to
- // take longer than 3 seconds to execute,
- // it should spawn a thread to execute the
- // stop code, and return. Otherwise, the
- // ServiceControlManager will believe that
- // the service has stopped responding.
- //
- VOID ServiceStop()
- {
- char lpOutputString[50];
- fclose(fpConfig);
- sprintf(lpOutputString, "Sent Stop Event to ServiceLoop\n");
- OutputDebugString(TEXT(lpOutputString));
- if ( hServerStopEvent )
- SetEvent(hServerStopEvent);
- }
-