| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | #include "StdAfx.h" |
|---|
| 22 | #include ".\wmiquery.h" |
|---|
| 23 | |
|---|
| 24 | #include <objidl.h> |
|---|
| 25 | #include <Wbemidl.h> |
|---|
| 26 | #include <map> |
|---|
| 27 | |
|---|
| 28 | WMIQuery::WMIQuery(void) : bInitialized(false) |
|---|
| 29 | { |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | WMIQuery::~WMIQuery(void) |
|---|
| 33 | { |
|---|
| 34 | if (bInitialized) |
|---|
| 35 | unInitialize(); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | bool WMIQuery::initialize() |
|---|
| 40 | { |
|---|
| 41 | NSC_LOG_ERROR_STD("Attempting Inialized WMI component"); |
|---|
| 42 | |
|---|
| 43 | HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
|---|
| 44 | if (FAILED(hRes)) { |
|---|
| 45 | NSC_LOG_ERROR_STD("CoInitialize failed: " + error::format::from_system(hRes)); |
|---|
| 46 | return false; |
|---|
| 47 | } |
|---|
| 48 | bInitialized = true; |
|---|
| 49 | hRes = CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL); |
|---|
| 50 | if (FAILED(hRes)) { |
|---|
| 51 | NSC_LOG_ERROR_STD("CoInitializeSecurity failed: " + error::format::from_system(hRes)); |
|---|
| 52 | return false; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | NSC_LOG_ERROR_STD("Inialized WMI component"); |
|---|
| 56 | return true; |
|---|
| 57 | } |
|---|
| 58 | void WMIQuery::unInitialize() |
|---|
| 59 | { |
|---|
| 60 | CoUninitialize(); |
|---|
| 61 | bInitialized = false; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | std::string WMIQuery::sanitize_string(LPTSTR in) { |
|---|
| 65 | TCHAR *p = in; |
|---|
| 66 | while (*p) { |
|---|
| 67 | if (p[0] < ' ' || p[0] > '}') |
|---|
| 68 | p[0] = '.'; |
|---|
| 69 | p++; |
|---|
| 70 | } |
|---|
| 71 | return in; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | WMIQuery::result_type WMIQuery::execute(std::string query) |
|---|
| 75 | { |
|---|
| 76 | if (!bInitialized) { |
|---|
| 77 | initialize(); |
|---|
| 78 | } |
|---|
| 79 | result_type ret; |
|---|
| 80 | |
|---|
| 81 | CComPtr< IWbemLocator > locator; |
|---|
| 82 | HRESULT hr = CoCreateInstance( CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast< void** >( &locator ) ); |
|---|
| 83 | if (FAILED(hr)) { |
|---|
| 84 | throw WMIException("CoCreateInstance for CLSID_WbemAdministrativeLocator failed!", hr); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | BSTR bstrNamespace = (L"root\\cimv2"); |
|---|
| 88 | CComPtr< IWbemServices > service; |
|---|
| 89 | hr = locator->ConnectServer( bstrNamespace, NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &service ); |
|---|
| 90 | if (FAILED(hr)) { |
|---|
| 91 | throw WMIException("ConnectServer failed!", hr); |
|---|
| 92 | } |
|---|
| 93 | CComBSTR strQuery(query.c_str()); |
|---|
| 94 | BSTR strQL = (L"WQL"); |
|---|
| 95 | |
|---|
| 96 | CComPtr< IEnumWbemClassObject > enumerator; |
|---|
| 97 | hr = service->ExecQuery( strQL, strQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &enumerator ); |
|---|
| 98 | if (FAILED(hr)) { |
|---|
| 99 | throw WMIException("ExecQuery failed:" + query + " (reason is: " + ComError::getComError() + ")", hr); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | CComPtr< IWbemClassObject > row = NULL; |
|---|
| 103 | ULONG retcnt; |
|---|
| 104 | int i=0; |
|---|
| 105 | while (hr = enumerator->Next( WBEM_INFINITE, 1L, &row, &retcnt ) == WBEM_S_NO_ERROR) { |
|---|
| 106 | if (SUCCEEDED(hr)) { |
|---|
| 107 | if (retcnt > 0) { |
|---|
| 108 | SAFEARRAY* pstrNames; |
|---|
| 109 | wmi_row returnRow; |
|---|
| 110 | hr = row->GetNames(NULL,WBEM_FLAG_ALWAYS|WBEM_FLAG_NONSYSTEM_ONLY,NULL,&pstrNames); |
|---|
| 111 | if (FAILED(hr)) { |
|---|
| 112 | throw WMIException("GetNames failed:" + query, hr); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | long index = 0, begin, end; |
|---|
| 116 | CComSafeArray<BSTR> arr = pstrNames; |
|---|
| 117 | begin = arr.GetLowerBound(); |
|---|
| 118 | end = arr.GetUpperBound(); |
|---|
| 119 | for ( index = begin; index <= end; index++ ) { |
|---|
| 120 | USES_CONVERSION; |
|---|
| 121 | CComBSTR bColumn = arr.GetAt(index); |
|---|
| 122 | std::string column = OLE2T(bColumn); |
|---|
| 123 | CComVariant vValue; |
|---|
| 124 | hr = row->Get(bColumn, 0, &vValue, 0, 0); |
|---|
| 125 | if (FAILED(hr)) { |
|---|
| 126 | throw WMIException("Failed to get value for " + column + " in query: " + query, hr); |
|---|
| 127 | } |
|---|
| 128 | WMIResult value; |
|---|
| 129 | |
|---|
| 130 | if (vValue.vt == VT_INT) { |
|---|
| 131 | value.setNumeric(column, vValue.intVal); |
|---|
| 132 | } else if (vValue.vt == VT_I4) { |
|---|
| 133 | value.setNumeric(column, vValue.lVal); |
|---|
| 134 | } else if (vValue.vt == VT_UINT) { |
|---|
| 135 | value.setNumeric(column, vValue.uintVal); |
|---|
| 136 | } else if (vValue.vt == VT_BSTR) { |
|---|
| 137 | value.setString(column, OLE2T(vValue.bstrVal)); |
|---|
| 138 | } else if (vValue.vt == VT_NULL) { |
|---|
| 139 | value.setString(column, "NULL"); |
|---|
| 140 | } else if (vValue.vt == VT_BOOL) { |
|---|
| 141 | value.setBoth(column, vValue.iVal, vValue.iVal?"TRUE":"FALSE"); |
|---|
| 142 | } else { |
|---|
| 143 | NSC_LOG_ERROR_STD(column + " is not supported (type-id: " + strEx::itos(vValue.vt) + ")"); |
|---|
| 144 | } |
|---|
| 145 | returnRow.addValue(column, value); |
|---|
| 146 | } |
|---|
| 147 | ret.push_back(returnRow); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | row.Release(); |
|---|
| 151 | } |
|---|
| 152 | return ret; |
|---|
| 153 | } |
|---|