summaryrefslogtreecommitdiff
path: root/indra/newview/llmachineid.cpp
blob: d57260563590faf4647fd1ecf53b6c941f71377c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
 * @file llmachineid.cpp
 * @brief retrieves unique machine ids
 *
 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"
#include "lluuid.h"
#include "llmachineid.h"
#if LL_WINDOWS
#define _WIN32_DCOM
#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
#elif LL_DARWIN
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#endif
unsigned char static_unique_id[] =  {0,0,0,0,0,0};
unsigned char static_legacy_id[] =  {0,0,0,0,0,0};
bool static has_static_unique_id = false;
bool static has_static_legacy_id = false;

#if LL_WINDOWS

class LLWMIMethods
{
public:
    LLWMIMethods()
    :   pLoc(NULL),
        pSvc(NULL)
    {
        initCOMObjects();
    }

    ~LLWMIMethods()
    {
        if (isInitialized())
        {
            cleanCOMObjects();
        }
    }

    bool isInitialized() { return SUCCEEDED(mHR); }
    bool getWindowsProductNumber(unsigned char *unique_id, size_t len);
    bool getDiskDriveSerialNumber(unsigned char *unique_id, size_t len);
    bool getProcessorSerialNumber(unsigned char *unique_id, size_t len);
    bool getMotherboardSerialNumber(unsigned char *unique_id, size_t len);
    bool getComputerSystemProductUUID(unsigned char *unique_id, size_t len);
    bool getGenericSerialNumber(const BSTR &select, const LPCWSTR &variable, unsigned char *unique_id, size_t len, bool validate_as_uuid = false);

private:
    void initCOMObjects();
    void cleanCOMObjects();

    HRESULT mHR;
    IWbemLocator *pLoc;
    IWbemServices *pSvc;
};


void LLWMIMethods::initCOMObjects()
{
# pragma comment(lib, "wbemuuid.lib")
    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------

    mHR = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
    if (FAILED(mHR))
    {
        // if result S_FALSE, it's already initialized
        LL_DEBUGS("AppInit") << "Failed to initialize COM library. Error code = 0x" << std::hex << mHR << LL_ENDL;
    }

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // Note: If you are using Windows 2000, you need to specify -
    // the default authentication credentials for a user by using
    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    // parameter of CoInitializeSecurity ------------------------

    mHR = CoInitializeSecurity(
        NULL,
        -1,                          // COM authentication
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities
        NULL                         // Reserved
    );

    if (FAILED(mHR))
    {
        LL_WARNS("AppInit") << "Failed to initialize security. Error code = 0x" << std::hex << mHR << LL_ENDL;
        CoUninitialize();
        return;               // Program has failed.
    }

    // Step 3: ---------------------------------------------------
    // Obtain the initial locator to WMI -------------------------

    mHR = CoCreateInstance(
        CLSID_WbemLocator,
        0,
        CLSCTX_INPROC_SERVER,
        IID_IWbemLocator, (LPVOID *)&pLoc);

    if (FAILED(mHR))
    {
        LL_WARNS("AppInit") << "Failed to create IWbemLocator object." << " Err code = 0x" << std::hex << mHR << LL_ENDL;
        CoUninitialize();
        return;               // Program has failed.
    }

    // Step 4: -----------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    mHR = 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 (e.g. Kerberos)
        0,                       // Context object
        &pSvc                    // pointer to IWbemServices proxy
    );

    if (FAILED(mHR))
    {
        LL_WARNS("AppInit") << "Could not connect. Error code = 0x" << std::hex << mHR << LL_ENDL;
        pLoc->Release();
        CoUninitialize();
        return;               // Program has failed.
    }

    LL_DEBUGS("AppInit") << "Connected to ROOT\\CIMV2 WMI namespace" << LL_ENDL;

    // Step 5: --------------------------------------------------
    // Set security levels on the proxy -------------------------

    mHR = 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(mHR))
    {
        LL_WARNS("AppInit") << "Could not set proxy blanket. Error code = 0x" << std::hex << mHR << LL_ENDL;
        cleanCOMObjects();
        return;               // Program has failed.
    }
}


void LLWMIMethods::cleanCOMObjects()
{
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();
}

bool LLWMIMethods::getWindowsProductNumber(unsigned char *unique_id, size_t len)
{
    // wmic path Win32_ComputerSystemProduct get UUID
    return getGenericSerialNumber(bstr_t("SELECT * FROM Win32_OperatingSystem"), L"SerialNumber", unique_id, len);
}

bool LLWMIMethods::getDiskDriveSerialNumber(unsigned char *unique_id, size_t len)
{
    // wmic path Win32_DiskDrive get DeviceID,SerialNumber
    return getGenericSerialNumber(bstr_t("SELECT * FROM Win32_DiskDrive"), L"SerialNumber", unique_id, len);
}

bool LLWMIMethods::getProcessorSerialNumber(unsigned char *unique_id, size_t len)
{
    // wmic path Win32_Processor get DeviceID,ProcessorId
    return getGenericSerialNumber(bstr_t("SELECT * FROM Win32_Processor"), L"ProcessorId", unique_id, len);
}

bool LLWMIMethods::getMotherboardSerialNumber(unsigned char *unique_id, size_t len)
{
    // wmic path Win32_Processor get DeviceID,ProcessorId
    return getGenericSerialNumber(bstr_t("SELECT * FROM Win32_BaseBoard"), L"SerialNumber", unique_id, len);
}

bool LLWMIMethods::getComputerSystemProductUUID(unsigned char *unique_id, size_t len)
{
    // UUID from Win32_ComputerSystemProduct is motherboard's uuid and is identical to csproduct's uuid
    // wmic csproduct get name,identifyingnumber,uuid
    // wmic path Win32_ComputerSystemProduct get UUID
    return getGenericSerialNumber(bstr_t("SELECT * FROM Win32_ComputerSystemProduct"), L"UUID", unique_id, len, true);
}

bool LLWMIMethods::getGenericSerialNumber(const BSTR &select, const LPCWSTR &variable, unsigned char *unique_id, size_t len, bool validate_as_uuid)
{
    if (!isInitialized())
    {
        return false;
    }

    HRESULT hres;

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // For example, get the name of the operating system
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        select,
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,
        &pEnumerator);

    if (FAILED(hres))
    {
        LL_WARNS("AppInit") << "Query for operating system name failed." << " Error code = 0x" << std::hex << hres << LL_ENDL;
        return false;               // Program has failed.
    }

    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------

    IWbemClassObject *pclsObj = NULL;
    ULONG uReturn = 0;
    bool found = false;

    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(variable, 0, &vtProp, 0, 0);
        if (FAILED(hr))
        {
            LL_WARNS() << "Failed to get SerialNumber. Error code = 0x" << std::hex << hres << LL_ENDL;
            pclsObj->Release();
            pclsObj = NULL;
            continue;
        }

        // use characters in the returned Serial Number to create a byte array of size len
        BSTR serialNumber(vtProp.bstrVal);
        unsigned int serial_size = SysStringLen(serialNumber);
        if (serial_size < 1) // < len?
        {
            VariantClear(&vtProp);
            pclsObj->Release();
            pclsObj = NULL;
            continue;
        }

        if (validate_as_uuid)
        {
            std::wstring ws(serialNumber, serial_size);
            std::string str(ws.begin(), ws.end());

            if (!LLUUID::validate(str))
            {
                VariantClear(&vtProp);
                pclsObj->Release();
                pclsObj = NULL;
                continue;
            }

            static const LLUUID f_uuid("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
            LLUUID id(str);

            if (id.isNull() || id == f_uuid)
            {
                // Not unique id
                VariantClear(&vtProp);
                pclsObj->Release();
                pclsObj = NULL;
                continue;
            }
        }
        LL_INFOS("AppInit") << " Serial Number : " << vtProp.bstrVal << LL_ENDL;

        unsigned int j = 0;

        while (j < serial_size && vtProp.bstrVal[j] != 0)
        {
            for (unsigned int i = 0; i < len; i++)
            {
                if (j >= serial_size || vtProp.bstrVal[j] == 0)
                    break;

                unique_id[i] = (unsigned int)(unique_id[i] + serialNumber[j]);
                j++;
            }
        }
        VariantClear(&vtProp);

        pclsObj->Release();
        pclsObj = NULL;
        found = true;
        break;
    }

    // Cleanup
    // ========

    if (pEnumerator)
        pEnumerator->Release();

    return found;
}
#elif LL_DARWIN
bool getSerialNumber(unsigned char *unique_id, size_t len)
{
    CFStringRef serial_cf_str = NULL;
    io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,
                                                                 IOServiceMatching("IOPlatformExpertDevice"));
    if (platformExpert)
    {
        serial_cf_str = (CFStringRef) IORegistryEntryCreateCFProperty(platformExpert,
                                                                     CFSTR(kIOPlatformSerialNumberKey),
                                                                     kCFAllocatorDefault, 0);
        IOObjectRelease(platformExpert);
    }

    if (serial_cf_str)
    {
        char buffer[64] = {0};
        std::string serial_str("");
        if (CFStringGetCString(serial_cf_str, buffer, 64, kCFStringEncodingUTF8))
        {
            serial_str = buffer;
        }

        S32 serial_size = serial_str.size();

        if(serial_str.size() > 0)
        {
            S32 j = 0;
            while (j < serial_size)
            {
                for (S32 i = 0; i < len; i++)
                {
                    if (j >= serial_size)
                        break;

                    unique_id[i] = (unsigned int)(unique_id[i] + serial_str[j]);
                    j++;
                }
            }
            return true;
        }
    }
    return false;
}
#endif

// get an unique machine id.
// NOT THREAD SAFE - do before setting up threads.
// MAC Address doesn't work for Windows 7 since the first returned hardware MAC address changes with each reboot,  Go figure??

S32 LLMachineID::init()
{
    size_t len = sizeof(static_unique_id);
    memset(static_unique_id, 0, len);
    S32 ret_code = 0;
#if LL_WINDOWS

    LLWMIMethods comInit;

    if (comInit.getWindowsProductNumber(static_legacy_id, len))
    {
        // Bios id can change on windows update, so it is not the best id to use
        // but since old viewer already use them, we might need this id to decode
        // passwords
        has_static_legacy_id = true;
    }

    // Try motherboard/bios id, if it is present it is supposed to be sufficiently unique
    if (comInit.getComputerSystemProductUUID(static_unique_id, len))
    {
        has_static_unique_id = true;
        LL_DEBUGS("AppInit") << "Using product uuid as unique id" << LL_ENDL;
    }

    // Fallback to legacy
    if (!has_static_unique_id)
    {
        if (has_static_legacy_id)
        {
            memcpy(static_unique_id, &static_legacy_id, len);
            // Since ids are identical, mark legacy as not present
            // to not cause retry's in sechandler
            has_static_legacy_id = false;
            has_static_unique_id = true;
            LL_DEBUGS("AppInit") << "Using legacy serial" << LL_ENDL;
        }
        else
        {
            return 1; // Program has failed.
        }
    }

    ret_code=0;
#elif LL_DARWIN
    if (getSerialNumber(static_unique_id, len))
    {
        has_static_unique_id = true;
        LL_DEBUGS("AppInit") << "Using Serial number as unique id" << LL_ENDL;
    }

    {
        unsigned char * staticPtr = (unsigned char *)(&static_legacy_id[0]);
        ret_code = LLUUID::getNodeID(staticPtr);
        has_static_legacy_id = true;
    }

    // Fallback to legacy
    if (!has_static_unique_id)
    {
        if (has_static_legacy_id)
        {
            memcpy(static_unique_id, &static_legacy_id, len);
            // Since ids are identical, mark legacy as not present
            // to not cause retry's in sechandler
            has_static_legacy_id = false;
            has_static_unique_id = true;
            LL_DEBUGS("AppInit") << "Using legacy serial" << LL_ENDL;
        }
    }
#else
    unsigned char * staticPtr = (unsigned char *)(&static_unique_id[0]);
    ret_code = LLUUID::getNodeID(staticPtr);
    has_static_unique_id = true;
    has_static_legacy_id = false;
#endif

        LL_INFOS("AppInit") << "UniqueID: 0x";
        // Code between here and LL_ENDL is not executed unless the LL_DEBUGS
        // actually produces output
        for (size_t i = 0; i < len; ++i)
        {
            // Copy each char to unsigned int to hexify. Sending an unsigned
            // char to a std::ostream tries to represent it as a char, not
            // what we want here.
            unsigned byte = static_unique_id[i];
            LL_CONT << std::hex << std::setw(2) << std::setfill('0') << byte;
        }
        // Reset default output formatting to avoid nasty surprises!
        LL_CONT << std::dec << std::setw(0) << std::setfill(' ') << LL_ENDL;

        return ret_code;
}


S32 LLMachineID::getUniqueID(unsigned char *unique_id, size_t len)
{
    if (has_static_unique_id)
    {
        memcpy ( unique_id, &static_unique_id, len);
        return 1;
    }
    return 0;
}

S32 LLMachineID::getLegacyID(unsigned char *unique_id, size_t len)
{
    if (has_static_legacy_id)
    {
        memcpy(unique_id, &static_legacy_id, len);
        return 1;
    }
    return 0;
}