void WTS::EnumerateProcesses() { HANDLE server; DWORD totalEntries; PWTS_PROCESS_INFO processInfo; DWORD i; if((server = WTSOpenServer(Config::machine)) == NULL) { Util::Error(GetLastError(), L"WTSOpenServer()"); } if(WTSEnumerateProcesses(server, 0, 1, &processInfo, &totalEntries) != 0) { DWORD err = GetLastError(); wprintf(L"ret = %x\n", err); } for(i = 0; i < totalEntries; i++) { wprintf(L"process: %s\n", processInfo[i].pProcessName); wprintf(L"session: %d\n", processInfo[i].SessionId); wprintf(L"SID: %s\n", processInfo[i].pUserSid); } }
BOOL CProcessDlg::OnInitDialog() { CDialog::OnInitDialog(); serverName = g_pDoc->GetServerName(); serverHandle = WTSOpenServer(serverName); if (!WTSEnumerateProcesses(serverHandle,0,1,&pProcessInfo,&count)) AfxMessageBox("Unable to enumerate processes"); PWTS_PROCESS_INFO pProcess = pProcessInfo; CString serverDisplay; serverDisplay = "List of processes on "; if (*serverName == NULL) serverDisplay += "local server"; else serverDisplay += serverName; m_serverName.SetWindowText(serverDisplay); for (DWORD i=0; i < count; i++) { m_processList.AddString(pProcess->pProcessName); pProcess++; } m_processList.SetCurSel(0); return TRUE; }
void CProcessDlg::OnTerminateProcess() { DWORD ProcessId; LPSTR serverName = g_pDoc->GetServerName(); HANDLE serverHandle = WTSOpenServer(serverName); CString tempName; int temp; PWTS_PROCESS_INFO pProcess = pProcessInfo; temp = m_processList.GetCurSel(); m_processList.GetText(temp, tempName); for (DWORD i=0; i < count; i++) { if (tempName == (pProcess->pProcessName)) { ProcessId = pProcess->ProcessId; if (!WTSTerminateProcess(serverHandle,ProcessId,0x0)) AfxMessageBox("Unable to terminate process"); i = count; } pProcess++; } Refresh(); }
bool mod_ts::openServer(HANDLE * phServer, wstring * server, bool testIt) { bool reussite = false; if(reussite = !server) { *phServer = WTS_CURRENT_SERVER_HANDLE; } else { wchar_t * serverName = _wcsdup(server->c_str()); *phServer = WTSOpenServer(serverName); delete[] serverName; reussite = *phServer != NULL; } return reussite; }
// @pymethod <o PyHANDLE>|win32ts|WTSOpenServer|Opens a handle to a terminal server static PyObject *PyWTSOpenServer(PyObject *self, PyObject *args, PyObject *kwargs) { static char *keywords[]={"ServerName",NULL}; HANDLE h; WCHAR *ServerName=NULL; PyObject *obServerName; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:WTSOpenServer", keywords, &obServerName)) // @pyparm <o PyUnicode>|ServerName||Name ot terminal server to be opened return NULL; if (!PyWinObject_AsWCHAR(obServerName, &ServerName, FALSE)) return NULL; h=WTSOpenServer(ServerName); PyWinObject_FreeWCHAR(ServerName); if (h==NULL) return PyWin_SetAPIError("WTSOpenServer"); return new PyTS_HANDLE(h); }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------ VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv){ sprintf( msg_err, "ServiceMain - init." ); DWORD xParam = 1; JOB = 1; // -- hServiceStatus = RegisterServiceCtrlHandlerEx( service_name, (LPHANDLER_FUNCTION_EX)ServiceCtrlHandlerEx, (PVOID)&xParam ); if( !hServiceStatus ){ prn_err( "RegisterServiceCtrlHandlerEx failed.", GetLastError() ); return; } // init service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; service_status.dwCurrentState = SERVICE_START_PENDING; service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_SESSIONCHANGE; service_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR; service_status.dwServiceSpecificExitCode = 0; service_status.dwCheckPoint = 0; service_status.dwWaitHint = 5000; if( !SetServiceStatus( hServiceStatus, &service_status )){ prn_err( "SetServiceStatus 'NULL' failed.", GetLastError() ); return; } // Rabota service_status.dwCurrentState = SERVICE_RUNNING; service_status.dwWin32ExitCode = NO_ERROR; // Set New if( !SetServiceStatus( hServiceStatus, &service_status )){ prn_err( "SetServiceStatus 'SERVICE_START_PENDING' failed.", GetLastError() ); return; } // -- usleep(10000); // ------------------------------------------------------------------------ prn_log( "." ); SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS ); prn_log( "START SERVICE." ); // --------------------------------- if( !init_conf( service_work, service_name, &conf )){ // возврат всегда "1" prn_err( "Init config-data failed. This message can not be", 0 ); }else{ if( strcasecmp( conf.log_debug, "Off" ) == 0 ) DEBUG = 0; else prn_log( "Logger - DEBUG" ); // -- if( strcasecmp( conf.user_proc, "0" ) == 0 ) prn_log( "WARNING! User Process - disabled." ); if( strcasecmp( conf.maintenance, "0" ) == 0 ) prn_log( "WARNING! Maintenances - disabled." ); if( strcasecmp( conf.git_exec, "0" ) == 0 ) prn_log( "WARNING! Git status - disabled." ); } // -- JOB = 2; // Создаем поток логирования cygwin (сокет /dev/log) для ssh-сессий HANDLE hCygWinLogThread = CreateThread( NULL, 0, &CygWinLogThread, (LPVOID)&conf, 0, NULL ); if( !hCygWinLogThread ){ prn_err( "LogSocket: Create CygWinLogThread - failed.", GetLastError() ); } // --------------------------------- JOB = 3; if( !open_send( conf.zbx_server, conf.zbx_port, conf.zbx_host )){ prn_log( "WARNING! Sender not available: Maintenances - will not be available." ); strcpy( conf.maintenance, "0" ); } // --------------------------------- JOB = 22; // потоки обслуживания // HANDLE hGTimeThread = CreateThread( NULL, 0, >imeThread, NULL, 0, NULL );// поток // --------------------------------- usleep(10000); JOB = 101; send_sm_string( "SM;SyMon;START;!" ); // -- // проверка активных сессий DWORD pCount; HANDLE hToken; PWTS_SESSION_INFO ppSessionInfo = NULL; HANDLE hTS = WTSOpenServer( "" ); // -- if( WTSEnumerateSessions(hTS, 0, 1, &ppSessionInfo, &pCount )){ while( pCount ){ if( ppSessionInfo->State == 0 ){ if( WTSQueryUserToken( ppSessionInfo->SessionId, &hToken )) entering_info( ppSessionInfo->SessionId, 5, hToken, NULL ); else prn_err( "WTS Token failed.", GetLastError() ); } ppSessionInfo++; pCount--; } } // ----------------------------------- while( JOB > 100 ){ // главный цикл git_test(); // выполнение отложенных (тормозных) функций sleep(1); } // -- // CloseHandle( hGTimeThread ); sleep( 2 ); close_send(); sprintf( msg_err, "ServiceMain - return." ); // ------------------------------------------------------------------------ }
void CQueryDlg::OnQuery() { LPSTR serverName = g_pDoc->GetServerName(); HANDLE serverHandle = WTSOpenServer(serverName); DWORD sessionID = g_pDoc->GetSessionID(); BytesReturned = 0; int nIndex = m_querySession.GetCurSel(); if (!WTSQuerySessionInformation(serverHandle, sessionID, QueryItems[nIndex].InfoClass, &pSessionInfo, &BytesReturned)) AfxMessageBox("Query failed!"); else { switch ( QueryItems[nIndex].InfoClass ) { #ifdef ICA_STUFF_INCLUDED case WTSVersion: { LPOSVERSIONINFO pVerInfo = (LPOSVERSIONINFO) pSessionInfo; m_sessionInfo.Format("Version: major %u \r\n minor %u \r\n build %u \r\n CSD Version: %s", pVerInfo->dwMajorVersion, pVerInfo->dwMinorVersion, pVerInfo->dwBuildNumber, pVerInfo->szCSDVersion); } break; #endif case WTSInitialProgram: m_sessionInfo.Format("Initial Program:\r\n "); //m_sessionInfo = "Initial Program: "; m_sessionInfo += pSessionInfo; break; case WTSWorkingDirectory: m_sessionInfo.Format("Working Directory:\r\n "); m_sessionInfo += pSessionInfo; break; case WTSOEMId: m_sessionInfo = "OEM ID: "; m_sessionInfo += pSessionInfo; break; case WTSSessionId: m_sessionInfo.Format(TEXT("Session ID: %u"), *(DWORD *) pSessionInfo); break; case WTSUserName: m_sessionInfo = "UserName: "******"WinStation Name: "; m_sessionInfo += pSessionInfo; break; case WTSDomainName: m_sessionInfo = "Domain Name: "; m_sessionInfo += pSessionInfo; break; case WTSConnectState: {CString connectState[10]; connectState[0] = "Active"; connectState[1] = "Connected"; connectState[2] = "ConnectQuery"; connectState[3] = "Shadow"; connectState[4] = "Disconnected"; connectState[5] = "Idle"; connectState[6] = "Listen"; connectState[7] = "Reset"; connectState[8] = "Down"; connectState[9] = "Init"; m_sessionInfo.Format("Connection State: %s", connectState[(*(DWORD *) pSessionInfo)]); } break; case WTSClientBuildNumber : m_sessionInfo.Format("Client Build Number: %d",(*(WORD*) pSessionInfo)); break; case WTSClientName : m_sessionInfo = "Client Name: "; m_sessionInfo += pSessionInfo; break; case WTSClientDirectory : m_sessionInfo.Format("Client Directory:\r\n %s", pSessionInfo); break; case WTSClientProductId : m_sessionInfo.Format("Client Product ID: %u",(*(WORD*) pSessionInfo)); break; case WTSClientAddress: { BYTE * pByte; PWTS_CLIENT_ADDRESS pClientAddress = (PWTS_CLIENT_ADDRESS) pSessionInfo; m_sessionInfo.Format( TEXT("ClientAddress: (%u) "), pClientAddress->AddressFamily ); CString tempString; switch ( pClientAddress->AddressFamily ) { case 0 : tempString.Format( TEXT("%s"), pClientAddress->Address ); m_sessionInfo += tempString; break; case 2 : pByte = &pClientAddress->Address[2]; tempString.Format( TEXT("%u.%u.%u.%u"), pByte[0], pByte[1], pByte[2], pByte[3] ); m_sessionInfo += tempString; break; case 6 : pByte = pClientAddress->Address; for ( int i=0; i<4; i++ ) { tempString.Format( TEXT("%02x"), pByte[i] ); m_sessionInfo += tempString; } m_sessionInfo += ":"; for ( int i=4; i<10; i++ ){ tempString.Format( TEXT("%02x"), pByte[i] ); m_sessionInfo += tempString; } break; } } break; case WTSClientDisplay: { PWTS_CLIENT_DISPLAY SessionInfo = (PWTS_CLIENT_DISPLAY) pSessionInfo; m_sessionInfo.Format("Client Display:\r\n Resolution: %d X %d\r\n Color Depth %d", SessionInfo->HorizontalResolution, SessionInfo->VerticalResolution, SessionInfo->ColorDepth); } break; #ifdef ICA_STUFF_INCLUDED case WTSClientCache: { PWTS_CLIENT_CACHE SessionInfo = (PWTS_CLIENT_CACHE) pSessionInfo; m_sessionInfo.Format("Client Cache:\r\n Tiny Cache %d\r\n Low Memory Cache %d\r\n XMS Cache %d\r\n Disk Cache %d\r\n Cache Size Dim %d\r\n Bitmap Min Dim %d\r\n Signature Level Dim %d\r\n File System Overhead %d", SessionInfo->CacheTiny, SessionInfo->CacheLowMem, SessionInfo->CacheDisk, SessionInfo->DimCacheSize, SessionInfo->DimBitmapMin, SessionInfo->DimSignatureLevel, SessionInfo->DimFilesysOverhead); } break; case WTSClientDrives: { PWTS_CLIENT_DRIVES pClientDrives = (PWTS_CLIENT_DRIVES) pSessionInfo; CString tempString; CString driveType[10]; driveType[0] = "Removeable"; driveType[1] = "Fixed"; driveType[2] = "Remote"; driveType[3] = "CD-Rom"; driveType[4] = "Ram Disk"; driveType[5] = "AutoConnect"; driveType[6] = "Unknown"; m_sessionInfo.Format( TEXT("WTSClientDrives: fAuto %u, count %u \r\n"), pClientDrives->fAutoClientDrives, pClientDrives->Count ); for ( DWORD i=0; i < pClientDrives->Count; i++ ) { switch (pClientDrives->Drives[i].Flags) { case 1: nIndex = 0; break; case 2: nIndex = 1; break; case 4: nIndex = 2; break; case 8: nIndex = 3; break; case 10: nIndex = 4; break; case 20: nIndex = 5; break; default: nIndex = 6; } tempString.Format( TEXT(" %c:%s \r\n"), pClientDrives->Drives[i].DriveLetter, driveType[nIndex]); m_sessionInfo += tempString; } } break; case WTSICABufferLength: m_sessionInfo.Format("ICA Buffer Length: %u", (*(DWORD *) pSessionInfo)); break; case WTSLicenseEnabler: m_sessionInfo.Format("License Enabler: %x", (*(DWORD *) pSessionInfo)); break; #endif case WTSApplicationName: m_sessionInfo.Format("ApplicationName: %s", pSessionInfo ); break; } SetDlgItemText(IDC_SESSION_INFO, m_sessionInfo); WTSFreeMemory( pSessionInfo ); } }
void main(int argc, char *argv[]) { char *helpMsg = "\nUSAGE: users.exe [hostname]\n\nNot specifying a hostname implies localhost.\nThis command will return information about users currently logged onto\na local or remote computer, including the client's hostname and IP.\nUsers.exe was written by Ryan Ries.\n"; char *hostName, *connState = ""; char *addrFamily = ""; HANDLE hHost = NULL; int retVal = 0; unsigned int i = 0; PWTS_SESSION_INFO pSessionInfo = 0; DWORD dwSessionCount, pBytesReturned = 0; LPTSTR pUser, pDomain, pClientName, pClientAddress; PWTS_CLIENT_ADDRESS pClientAddressStruct = NULL; if(argc > 2) { printf(helpMsg); return; } if(argc == 2) { if(strchr(argv[1],'?') || strchr(argv[1],'/') || strchr(argv[1],'\\')) { printf(helpMsg); return; } } if(argc < 2) hostName = "localhost"; else hostName = argv[1]; hHost = WTSOpenServer(hostName); retVal = WTSEnumerateSessions(hHost, 0, 1, &pSessionInfo, &dwSessionCount); if(retVal == 0) { printf("ERROR %d: Could not connect to %s!", GetLastError(), hostName); WTSCloseServer(hHost); return; } printf("\n"); for(i = 0; i < dwSessionCount; i++) { WTS_SESSION_INFO si = pSessionInfo[i]; // To weed out nonsense if(si.SessionId > 2048 || si.SessionId < 0) continue; WTSQuerySessionInformation(hHost, si.SessionId, WTSUserName, &pUser, &pBytesReturned); WTSQuerySessionInformation(hHost, si.SessionId, WTSDomainName, &pDomain, &pBytesReturned); WTSQuerySessionInformation(hHost, si.SessionId, WTSClientName, &pClientName, &pBytesReturned); WTSQuerySessionInformation(hHost, si.SessionId, WTSClientAddress, &pClientAddress, &pBytesReturned); pClientAddressStruct = (PWTS_CLIENT_ADDRESS)pClientAddress; switch(pClientAddressStruct->AddressFamily) { case 0: addrFamily = "AF_UNSPEC"; break; case 2: addrFamily = "AF_INET"; break; case 6: addrFamily = "AF_IPX"; break; case 17: addrFamily = "AF_NETBIOS"; break; default: addrFamily = "Unknown"; break; } printf("Session ID : %i\n", si.SessionId); if(strlen(pUser) < 1) printf("Domain\\User : System\n"); else printf("Domain\\User : %s\\%s\n", pDomain, pUser); if(strlen(pClientName) < 1) printf("Client Name : Local\n"); else printf("Client Name : %s\n", pClientName); if(pClientAddressStruct->Address[2] == 0 || addrFamily == "AF_UNSPEC") printf("Net Address : n/a\n"); else printf("Net Address : %u.%u.%u.%u (%s)\n", pClientAddressStruct->Address[2], pClientAddressStruct->Address[3], pClientAddressStruct->Address[4], pClientAddressStruct->Address[5], addrFamily); switch(si.State) { case 0: connState = "Active"; break; case 1: connState = "Connected"; break; case 4: connState = "Disconnected"; break; case 5: connState = "Idle"; break; default: connState = "Unknown"; break; } printf("Conn. State : %s\n", connState); printf("\n"); WTSFreeMemory(pClientAddress); WTSFreeMemory(pClientName); WTSFreeMemory(pDomain); WTSFreeMemory(pUser); } WTSFreeMemory(pSessionInfo); WTSCloseServer(hHost); }