Esempio n. 1
0
int ServiceCommand::executeCommand(SC_HANDLE scm) {
  LOG.LogDebugFormat(L"Opening %s with %s rights...", mySettings->getServiceName(), (CString)mySCRight);

  SC_HANDLE svc = OpenService(scm, mySettings->getServiceName(), mySCRight);

  if (svc == NULL) {
    DWORD err = GetLastError();
    CString msg = CreateFormatted(L"Failed to open service %s.", mySettings->getServiceName());
    switch(err) {
    case ERROR_ACCESS_DENIED:
      LOG.LogWarnFormat(L"%s Access denied", msg);
      return 1;
    
    case ERROR_INVALID_NAME:
      LOG.LogWarnFormat(L"%s Invalid service name", msg);
      return 1;

    case ERROR_SERVICE_DOES_NOT_EXIST:
      LOG.LogWarnFormat(L"%s Service does not exist", msg);
      return 1;
      break;
    case ERROR_INVALID_HANDLE:
    default:
      LOG.LogWarnFormat(L"%s. %s", msg, LOG.GetErrorText(err));
      return 1;
      break;
    }
  }

  int ret = executeCommand(scm, svc);

  CloseServiceHandle(svc);
  return ret;
}
void ProcessInterruptTimeoutHandler::InterruptProcess(PROCESS_INFORMATION& info) {
  if (myActionFailed) {
    OnTimeoutFailed(info);
    return;
  }

  if (myTerminateSent == 0) {
    myActionFailed = !ExecuteInterruptAction(info);
    if (!myActionFailed) myTerminateSent = GetTickCount();
    return;
  } 

  ULONGLONG nowTicks = GetTickCount();
  if (nowTicks < myTerminateSent) {
    //workaround for 49.7 days running windows
    //http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx
    myTerminateSent = nowTicks;
    return;
  } 
  
  if (myTerminateSent + mySettings->getTerminateWaitTimeoutMilliseconds() < nowTicks) {
    //timeout
    LOG.LogWarn(L"Failed to wait for process to exit...");
    myActionFailed = true;    
    return;
  } 

  LOG.LogDebugFormat(L"Waiting for process to exit...");
}
Esempio n. 3
0
int LSAUserCommand::executeCommand(LSA_HANDLE handle) {    
  CString accountName = CreateFormatted(L"%s", mySettings->getUserName());

  const DWORD sz = 2044;
  PSID sid = (PSID)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz + 1);  
  TCHAR foundDomain[sz+1];
  DWORD psz = sz;
  DWORD pdz = sz;
  SID_NAME_USE use = SidTypeUser;

  LOG.LogDebug(L"Call LookupAccountName");
  if (0 == LookupAccountName(NULL, accountName, sid, &psz, foundDomain, &pdz, &use)) {
    LOG.LogErrorFormat(L"Failed to LookupAccountName. %s", LOG.GetLastError());
    return 1;
  }

  foundDomain[pdz] = L'\0';
  LOG.LogDebugFormat(L"LSA Policy opened: Domain %s", foundDomain);
  return executeCommand(handle, sid);
}
int CreateServiceCommand::executeCommand(SC_HANDLE scm) {
  LOG.LogDebugFormat(L"Creating and registering service: name=%s...", mySettings->getServiceName());
  
  CString sUserName = CreateFormatted(L"%s\\%s", mySettings->getDomain(), mySettings->getUserName());
  CString sPassword = mySettings->getPassword();

  LPCWSTR userName = NULL;
  LPCWSTR password = NULL;

  if (!mySettings->runAsSystem()) {
    //use declared variables to avoid strings from distruct
    userName = sUserName;
    password = sPassword;
    LOG.LogInfoFormat(L"Installing service for user: %s", userName);
  }

  SC_HANDLE service = CreateService(
    scm, //ServiceControlManager instance
    mySettings->getServiceName(),
    mySettings->getServiceDisplayName(),
    SERVICE_ALL_ACCESS, //any option here?
    SERVICE_WIN32_OWN_PROCESS, //Consider to have SERVICE_INTERACTIVE_PROCESS
    mySettings->isAutostart() ? SERVICE_AUTO_START : SERVICE_DEMAND_START, 
    SERVICE_ERROR_NORMAL, 
    mySettings->getServiceExecutable(), //executable command    
    NULL, //Start orger group
    NULL, //tag
    NULL, //dependencies
    userName,
    password
  );

  if (service == NULL) {
    DWORD err = GetLastError();
    CString msg = CreateFormatted(L"Failed to create service %s.", mySettings->getServiceName());
    switch(err) {
    case ERROR_ACCESS_DENIED:
      LOG.LogWarnFormat(L"%s Access denied", msg);
      return 1;
    case ERROR_CIRCULAR_DEPENDENCY:
      LOG.LogWarnFormat(L"%s A circular service dependency was specified", msg);
      return 1;
    case ERROR_DUPLICATE_SERVICE_NAME:
      LOG.LogWarnFormat(L"%s Service display name is already used", msg);
      return 1;
    case ERROR_INVALID_NAME:
      LOG.LogWarnFormat(L"%s The specified service name is invalid", msg);
      return 1;
    case ERROR_INVALID_PARAMETER:
      LOG.LogWarnFormat(L"%s A parameter that was specified is invalid", msg);
      return 1;
    case ERROR_INVALID_SERVICE_ACCOUNT:
      LOG.LogWarnFormat(L"%s Specified account does not exist", msg);
      return 1;
    case ERROR_SERVICE_EXISTS:
      LOG.LogWarnFormat(L"%s The service already exists", msg);
      return 1;
    case ERROR_SERVICE_MARKED_FOR_DELETE:
      LOG.LogWarnFormat(L"%s The service already exists and marked for deletion", msg);
      return 1;
    case ERROR_INVALID_HANDLE:
    default:
      LOG.LogErrorFormat(L"%s %s", msg, LOG.GetLastError());
      return 1;
    }    
  }

  LOG.LogInfoFormat(L"Service '%s' was created", mySettings->getServiceName());
  updateServiceDescription();

  CloseServiceHandle(service);

  return 0;
}