From 643353a31e9adc99828d9aca78fb4e742bfb10e1 Mon Sep 17 00:00:00 2001 From: David Kelly Date: Fri, 2 Dec 2016 15:46:20 -0800 Subject: [PATCH] Initial push Works on windows, have not tried it on mac. Lots more to do, but need to get it pushed to remote just in case. --- interface/src/Application.cpp | 8 +- libraries/networking/src/FingerprintUtils.cpp | 140 ++++++++++++++++++ libraries/networking/src/FingerprintUtils.h | 29 ++++ 3 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 libraries/networking/src/FingerprintUtils.cpp create mode 100644 libraries/networking/src/FingerprintUtils.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 76353104f4..9178a248ef 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -62,6 +62,7 @@ #include #include #include +#include #include #include #include @@ -580,9 +581,12 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo _window->setWindowTitle("Interface"); Model::setAbstractViewStateInterface(this); // The model class will sometimes need to know view state details from us - + + // TODO: This is temporary, while developing + FingerprintUtils::getMachineFingerprint(); + // End TODO auto nodeList = DependencyManager::get(); - + // Set up a watchdog thread to intentionally crash the application on deadlocks _deadlockWatchdogThread = new DeadlockWatchdogThread(); _deadlockWatchdogThread->start(); diff --git a/libraries/networking/src/FingerprintUtils.cpp b/libraries/networking/src/FingerprintUtils.cpp new file mode 100644 index 0000000000..6aa35dd2b6 --- /dev/null +++ b/libraries/networking/src/FingerprintUtils.cpp @@ -0,0 +1,140 @@ +// +// FingerprintUtils.h +// libraries/networking/src +// +// Created by David Kelly on 2016-12-02. +// Copyright 2016 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "FingerprintUtils.h" +#include + +QString FingerprintUtils::getMachineFingerprint() { + QString retval; +#ifdef Q_OS_WIN + HRESULT hres; + IWbemLocator *pLoc = NULL; + + // initialize com + hres = CoCreateInstance( + CLSID_WbemLocator, + 0, + CLSCTX_INPROC_SERVER, + IID_IWbemLocator, (LPVOID *) &pLoc); + + if (FAILED(hres)) { + qDebug() << "Failed to initialize WbemLocator"; + return retval; + } + + // Connect to WMI through the IWbemLocator::ConnectServer method + IWbemServices *pSvc = NULL; + + // Connect to the root\cimv2 namespace with + // the current user and obtain pointer pSvc + // to make IWbemServices calls. + hres = pLoc->ConnectServer( + _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace + NULL, // User name. NULL = current user + NULL, // User password. NULL = current + 0, // Locale. NULL indicates current + NULL, // Security flags. + 0, // Authority (for example, Kerberos) + 0, // Context object + &pSvc // pointer to IWbemServices proxy + ); + + if (FAILED(hres)) { + pLoc->Release(); + qDebug() << "Failed to connect to WMI"; + return retval; + } + + // Set security levels on the proxy + hres = CoSetProxyBlanket( + pSvc, // Indicates the proxy to set + RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx + RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx + NULL, // Server principal name + RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx + RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx + NULL, // client identity + EOAC_NONE // proxy capabilities + ); + + if (FAILED(hres)) { + pSvc->Release(); + pLoc->Release(); + qDebug() << "Failed to set security on proxy blanket"; + return retval; + } + + // Use the IWbemServices pointer to grab the Win32_BIOS stuff + IEnumWbemClassObject* pEnumerator = NULL; + hres = pSvc->ExecQuery( + bstr_t("WQL"), + bstr_t("SELECT * FROM Win32_ComputerSystemProduct"), + WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, + NULL, + &pEnumerator); + + if (FAILED(hres)) { + pSvc->Release(); + pLoc->Release(); + qDebug() << "query to get Win32_ComputerSystemProduct info"; + return retval; + } + + // Get the SerialNumber from the Win32_BIOS data + IWbemClassObject *pclsObj; + ULONG uReturn = 0; + + SHORT sRetStatus = -100; + + while (pEnumerator) { + HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); + + if(0 == uReturn){ + break; + } + + VARIANT vtProp; + + // Get the value of the Name property + hr = pclsObj->Get(L"UUID", 0, &vtProp, 0, 0); + if (!FAILED(hres)) { + switch (vtProp.vt) { + case VT_BSTR: + retval = QString::fromWCharArray(vtProp.bstrVal); + break; + } + } + VariantClear(&vtProp); + + pclsObj->Release(); + } + pEnumerator->Release(); + + // Cleanup + pSvc->Release(); + pLoc->Release(); + + qDebug() << "Windows BIOS UUID: " << retval; +#endif //Q_OS_WIN + +#ifdef Q_OS_MAC + + io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/"); + CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0); + IOObjectRelease(ioRegistryRoot); + retval = QString::fromCFString(uuidCf); + CFRelease(uuidCf); + qDebug() << "Mac serial number: " << retval; +#endif //Q_OS_MAC + + return retval; +} + diff --git a/libraries/networking/src/FingerprintUtils.h b/libraries/networking/src/FingerprintUtils.h new file mode 100644 index 0000000000..84a94f0abc --- /dev/null +++ b/libraries/networking/src/FingerprintUtils.h @@ -0,0 +1,29 @@ +// +// FingerprintUtils.h +// libraries/networking/src +// +// Created by David Kelly on 2016-12-02. +// Copyright 2016 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_FingerprintUtils_h +#define hifi_FingerprintUtils_h + +#include + +#ifdef Q_OS_WIN +#include +#include +#endif //Q_OS_WIN + +class FingerprintUtils { +public: + static QString getMachineFingerprint(); + +}; + +#endif // hifi_FingerprintUtils_h +