~n-muench/ubuntu/precise/open-vm-tools/open-vm-tools.march-merge

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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*********************************************************
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 *
 * This program 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 and no later version.
 *
 * This program 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 Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * machineID.c --
 *
 *	Obtain "universally unique" identification information.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#if defined(_WIN32)	// Windows
#include <windows.h>
#include <process.h>
#include <iptypes.h>
#endif

#include "vmware.h"
#include "hostinfo.h"
#include "util.h"
#include "log.h"
#include "str.h"
#include "err.h"
#include "vm_product.h"
#include "vm_atomic.h"

#define LOGLEVEL_MODULE main
#include "loglevel_user.h"

#if defined(_WIN32)	// Windows
/*
 *----------------------------------------------------------------------
 *
 * FindWindowsAdapter --
 *
 *      Search the list of networking interfaces for an appropriate choice.
 *
 *      A suitable adapter either must contain or must not contain a
 *      (simple) pattern string as specified by the findPattern argument.
 *
 * Results:
 *      IP_ADAPTER_INFO pointer or NULL if no suitable choice can be made.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------
 */

static IP_ADAPTER_INFO *
FindWindowsAdapter(IP_ADAPTER_INFO *head,  // IN:
                   char *pattern,          // IN:
                   Bool findPattern)       // IN:
{
   IP_ADAPTER_INFO *adapterInfo;
   IP_ADAPTER_INFO *adapterChoice = NULL;

   ASSERT(head);
   ASSERT(pattern);

   for (adapterInfo = head; adapterInfo; adapterInfo = adapterInfo->Next) {
      // Bias - only ethernet adapters; more likely to be local
      if (adapterInfo->AddressLength == 6) {
         Bool takeIt = FALSE;

         if ((adapterInfo->Description == NULL) ||
             (*adapterInfo->Description == '\0')) {
            takeIt = TRUE;
         } else {
            char *p = strstr(adapterInfo->Description, pattern);

            if (((p != NULL) && findPattern) ||
                ((p == NULL) && !findPattern)) {
               takeIt = TRUE;
            }
         }

         if (takeIt) {
            adapterChoice = adapterInfo;
            break;
         }
      }
   }

   return adapterChoice;
}


/*
 *----------------------------------------------------------------------
 *
 * ObtainHardwareID --
 *
 *      Locate and return the hardwareID for this machine.
 *
 * Results:
 *      0       No errors occured
 *      >0      failure (errno)
 *
 * Side effects:
 *      None.
 *
 * Note:
 *      Return the MAC address of a suitable networking interface.
 *      The hardwareID will be zero if nothing suitable could be found.
 *
 *----------------------------------------------------------------------
 */

static int
ObtainHardwareID(uint64 *hardwareID) // OUT:
{
   void *buf;
   DWORD status;
   HMODULE dllHandle;
   IP_ADAPTER_INFO *adapterList;
   IP_ADAPTER_INFO *adapterChoice;
   DWORD (WINAPI *getAdaptersFn)(IP_ADAPTER_INFO *, ULONG *);

   ULONG bufLen = 0;

   // Deal with BUG 21643
   dllHandle = LoadLibrary(TEXT("icmp.dll"));
   FreeLibrary(dllHandle);

   if (!dllHandle) {
      Warning("%s Failed to load icmp.dll.\n", __FUNCTION__);

      return EINVAL;
   }

   dllHandle = LoadLibrary(TEXT("IpHlpApi.dll"));

   if (!dllHandle) {
      Warning("%s Failed to load iphlpapi.dll.\n", __FUNCTION__);

      return EINVAL;
   }

   getAdaptersFn = (void *) GetProcAddress(dllHandle, "GetAdaptersInfo");

   if (!getAdaptersFn) {
      FreeLibrary(dllHandle);
      Warning("%s Failed to find GetAdaptersInfo.\n", __FUNCTION__);

      return EINVAL;
   }

   // Force GetAdaptersInfo to provide the amount of memory necessary.

   status = (*getAdaptersFn)(NULL, &bufLen);

   switch (status) {
   case ERROR_NOT_SUPPORTED:
      Warning("%s GetAdaptersInfo is not supported.\n", __FUNCTION__);
      // fall through
   case ERROR_NO_DATA:
      FreeLibrary(dllHandle);
      *hardwareID = 0;
      return 0;
      break;

   case ERROR_BUFFER_OVERFLOW:
   case NO_ERROR:
      break;

   default:
      FreeLibrary(dllHandle);
      Warning("%s GetAdaptersInfo failure %d: %d.\n", __FUNCTION__,
              __LINE__, status);

      return EINVAL;
   }

   buf = malloc(bufLen);

   if (buf == NULL) {
      FreeLibrary(dllHandle);

      return ENOMEM;
   }

   adapterList = (IP_ADAPTER_INFO *) buf;

   status = (*getAdaptersFn)(adapterList, &bufLen);

   FreeLibrary(dllHandle);

   if (status != NO_ERROR) {
      // something is seriously wrong; worked before...
      Warning("%s GetAdaptersInfo failure %d: %d.\n", __FUNCTION__,
              __LINE__, status);

      free(buf);

      return EINVAL;
   }

   // Try to find real hardware.
   adapterChoice = FindWindowsAdapter(adapterList, PRODUCT_GENERIC_NAME,
                                      FALSE);

   if (adapterChoice == NULL) {
      *hardwareID = 0;
   } else {
      union bytes {
         uint64 data;
         char   bytes[8];
      } x;

      x.bytes[0] = adapterChoice->Address[0];
      x.bytes[1] = adapterChoice->Address[1];
      x.bytes[2] = adapterChoice->Address[2];
      x.bytes[3] = adapterChoice->Address[3];
      x.bytes[4] = adapterChoice->Address[4];
      x.bytes[5] = adapterChoice->Address[5];
      x.bytes[6] = '\0';
      x.bytes[7] = '\0';

      *hardwareID = x.data;
   }

   free(buf);

   return 0;
}
#elif defined(__APPLE__)	// MacOS X
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#include <net/ethernet.h>


/*
 *----------------------------------------------------------------------
 *
 * CheckEthernet --
 *
 *      Determine if "en<n>" exists and, if so, return its MAC address.
 *
 * Results:
 *      NULL    interface doesn't exist or isn't usable
 *      !NULL   interface exists and is usable
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------
 */

static struct ifaddrs *
CheckEthernet(struct ifaddrs *ifp,  // IN:
              uint32 n)             // IN:
{
   struct ifaddrs *p;
   char name[8];

   // Construct the interface name
   Str_Sprintf(name, sizeof name, "en%u", n);

   // Go through the list and see if this interface exists and is usable.
   p = ifp;

   while (p != NULL) {
      if ((strcmp(p->ifa_name, name) == 0) &&
          (p->ifa_addr != NULL) &&
          (p->ifa_addr->sa_family == AF_LINK)) {
         break;
      }

      p = p->ifa_next;
   }

   return p;
}


/*
 *----------------------------------------------------------------------
 *
 * ObtainHardwareID --
 *
 *      Locate and return the hardwareID for this machine.
 *
 * Results:
 *      0       No errors occured
 *      >0      failure (errno)
 *
 * Side effects:
 *      None.
 *
 * Note:
 *      Return the MAC address of a suitable networking interface.
 *      The hardwareID will be zero if nothing suitable could be found.
 *
 *----------------------------------------------------------------------
 */

static int
ObtainHardwareID(uint64 *hardwareID)  // OUT:
{
   uint32 i;
   struct ifaddrs *p;
   struct ifaddrs *ifp;

   // Attempt to get the list of networking interfaces
   if (getifaddrs(&ifp) == -1) {
      int saveErrno = errno;

      Warning("%s getifaddrs failure: %s.\n", __FUNCTION__,
              Err_Errno2String(saveErrno));

      return saveErrno;
   }

   // search through a "reasonable" number of interfaces
   for (i = 0, *hardwareID = 0; i < 8; i++) {
      p = CheckEthernet(ifp, i);

      if (p != NULL) {
         memcpy(hardwareID, LLADDR((struct sockaddr_dl *)p->ifa_addr), ETHER_ADDR_LEN);
         break;
      }
   }

   freeifaddrs(ifp);

   return 0;
}
#elif defined(linux) || defined __ANDROID__
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/time.h>
#if defined __ANDROID__
#include <sys/socket.h>  // For SOCK_DGRAM etc.
#endif


/*
 *----------------------------------------------------------------------
 *
 * CheckEthernet --
 *
 *      Determine if "eth<n>" exists and, if so, return its MAC address
 *
 * Results:
 *      0       No errors occured
 *      >0      failure (errno)
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------
 */

static int
CheckEthernet(uint32 n,        // IN:
              char *machineID) // OUT:
{
   int          fd;
   int          erc;
   struct ifreq ifreq;
   int          saveErrno;

   fd = socket(AF_INET, SOCK_DGRAM, 0);

   if (fd == -1) {
      return errno;
   }

   /* get the MAC address of eth<n> */
   Str_Sprintf(ifreq.ifr_name, IFNAMSIZ, "eth%u", n);

   erc = ioctl(fd, SIOCGIFHWADDR, &ifreq);
   saveErrno = errno;

   close(fd);

   if (erc == -1) {
      return saveErrno;
   }

   machineID[0] = ifreq.ifr_hwaddr.sa_data[0] & 0xFF;
   machineID[1] = ifreq.ifr_hwaddr.sa_data[1] & 0xFF;
   machineID[2] = ifreq.ifr_hwaddr.sa_data[2] & 0xFF;
   machineID[3] = ifreq.ifr_hwaddr.sa_data[3] & 0xFF;
   machineID[4] = ifreq.ifr_hwaddr.sa_data[4] & 0xFF;
   machineID[5] = ifreq.ifr_hwaddr.sa_data[5] & 0xFF;
   machineID[6] = '\0';
   machineID[7] = '\0';

   return 0;
}


/*
 *----------------------------------------------------------------------
 *
 * ObtainHardwareID --
 *
 *      Locate the hardwareID for this machine.
 *
 * Results:
 *      0       No errors occured
 *      >0      failure (errno)
 *
 * Side effects:
 *      None.
 *
 * Note:
 *      Return the MAC address of a suitable networking interface.
 *      The hardwareID will be zero if nothing suitable could be found.
 *
 *      This is a "good enough" initial hack but will need to be revisited.
 *      Ideally checks should be made for sysfs (Linux 2.6 and later) and,
 *      if present, search through the information and try to insure that
 *      the ethernet chosen is on the motherboard.
 *
 *----------------------------------------------------------------------
 */

static int
ObtainHardwareID(uint64 *hardwareID)  // OUT:
{
   uint32 i;

   // search through a "reasonable" number of interfaces
   for (i = 0; i < 8; i++) {
      int erc;

      erc = CheckEthernet(i, (char *) hardwareID);

      if (erc == 0) {
         return 0;
      } else {
         if (erc != ENODEV) {
            Warning("%s unexpected failure: %d.\n", __FUNCTION__, erc);
            return erc;
         }
      }
   }

   *hardwareID = 0;

   return 0;
}
#else				// Not a specifically code OS
#include <unistd.h>


/*
 *----------------------------------------------------------------------
 *
 * ObtainHardwareID --
 *
 *      Locate the hardwareID for this machine.
 *
 * Results:
 *      0       No errors occured
 *      >0      failure (errno)
 *
 * Side effects:
 *      None.
 *
 * Note:
 *      This is a dummy to catch an uncoded OS.
 *----------------------------------------------------------------------
 */

static int
ObtainHardwareID(uint64 *hardwareID)  // OUT:
{
   *hardwareID = gethostid();

   return 0;
}
#endif				// Not a specifically code OS


/*
 *----------------------------------------------------------------------
 *
 * HostNameHash --
 *
 *      Return the hash value of a string.
 *
 * Results:
 *      The hash value of the string.
 *
 * Side effects:
 *      None.
 *
 * Note:
 *      This uses the DBJ2 hash algorithm.
 *
 *----------------------------------------------------------------------
 */

static uint32
HostNameHash(unsigned char *str) // IN:
{
   uint32 c;

   uint32 hash = 5381;

   while ((c = *str++) != '\0') {
      hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
   }

   return hash;
}

 
/*
 *----------------------------------------------------------------------
 *
 * Hostinfo_MachineID --
 *
 *      Return the machine ID information.
 *
 *      There are two pieces of identification returned: a hash of the
 *      hostname and and a hardware identifier. If either of them is
 *      unavailable they will be given a value of zero.
 *
 *      The hardware identifier should be extracted from a piece of hardware
 *      that has individual discrimination - each "part" is uniquely labelled.
 *      The MAC address of an ethernet and the WWN of a FibreChannel HBA are
 *      perfect examples.
 *
 * Results:
 *	The values are returned.
 *
 * Side effects:
 *      The hardware identifier should persistent across system reboots and
 *      resets as long of the hardware is available.
 *
 *      Currently the Windows implementation is used only for file locking -
 *      not system identification - so any acceptable piece of hardware
 *      may be used, even if it changes during a reboot.
 *
 *----------------------------------------------------------------------
 */

void
Hostinfo_MachineID(uint32 *hostNameHash,    // OUT:
                   uint64 *hostHardwareID)  // OUT:
{
   static Atomic_Ptr cachedHardwareID;
   static Atomic_Ptr cachedHostNameHash;
   uint64 *tmpHardwareID;
   uint32 *tmpNameHash;

   ASSERT(hostNameHash);
   ASSERT(hostHardwareID);

   tmpNameHash = Atomic_ReadPtr(&cachedHostNameHash);
   if (!tmpNameHash) {
      char *hostName;

      tmpNameHash = Util_SafeMalloc(sizeof *tmpNameHash);

      // 4 bytes (32 bits) of host name information
      hostName = (char *) Hostinfo_HostName();

      if (hostName == NULL) {
         Warning("%s Hostinfo_HostName failure; providing default.\n",
                __FUNCTION__);

         *tmpNameHash = 0;
      } else {
         *tmpNameHash = HostNameHash((unsigned char *) hostName);
         free(hostName);
      }

      if (Atomic_ReadIfEqualWritePtr(&cachedHostNameHash, NULL, 
                                     tmpNameHash)) {
         free(tmpNameHash);
         tmpNameHash = Atomic_ReadPtr(&cachedHostNameHash);
      }
   }
   *hostNameHash = *tmpNameHash;

   tmpHardwareID = Atomic_ReadPtr(&cachedHardwareID);
   if (!tmpHardwareID) {
      int  erc;

      tmpHardwareID = Util_SafeMalloc(sizeof *tmpHardwareID);

      // 8 bytes (64 bits) of hardware information
      erc = ObtainHardwareID(tmpHardwareID);
      if (erc != 0) {
         Warning("%s ObtainHardwareID failure (%s); providing default.\n",
                 __FUNCTION__, Err_Errno2String(erc));

         *tmpHardwareID = 0;
      }

      if (Atomic_ReadIfEqualWritePtr(&cachedHardwareID, NULL, 
                                     tmpHardwareID)) {
         free(tmpHardwareID);
         tmpHardwareID = Atomic_ReadPtr(&cachedHardwareID);
      }
   }
   *hostHardwareID = *tmpHardwareID;
}