1bdaf813 |
/*
* Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved.
*
* Author: Shawn Webb
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
|
9e0f01d9 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h> |
4a4b84da |
#if HAVE_UNISTD_H |
9e0f01d9 |
#include <unistd.h> |
4a4b84da |
#endif |
9e0f01d9 |
#include <sys/types.h> |
4a4b84da |
#include <fcntl.h>
#if !defined(_WIN32) |
9e0f01d9 |
#include <sys/socket.h>
#include <netdb.h>
#include <sys/ioctl.h> |
4a4b84da |
#endif |
9e0f01d9 |
|
aeafdcc0 |
#if defined(HAVE_GETIFADDRS) |
9e0f01d9 |
#include <net/if.h> |
643c3caa |
#if defined(HAVE_NET_IF_DL_H) |
aeafdcc0 |
#include <net/if_dl.h> |
643c3caa |
#endif |
9e0f01d9 |
#include <ifaddrs.h> |
aeafdcc0 |
#endif
|
db5ee846 |
#if defined(SIOCGIFHWADDR) && !defined(__GNU__) |
c147fd01 |
#if defined(_AIX) |
7401c24d |
#include <sys/ndd_var.h>
#include <sys/kinfo.h>
#else |
9e0f01d9 |
#include <linux/sockios.h>
#endif |
7401c24d |
#endif |
9e0f01d9 |
#include <errno.h>
|
60d8d2c3 |
#include "clamav.h" |
9e0f01d9 |
#include "hostid.h" |
174a7ecc |
#include "libclamav/others.h" |
9e0f01d9 |
|
cd94be7a |
struct device *get_device_entry(struct device *devices, size_t *ndevices, const char *name);
|
9e0f01d9 |
struct device *get_device_entry(struct device *devices, size_t *ndevices, const char *name)
{
void *p;
size_t i;
if ((devices)) {
int found = 0;
|
0882a904 |
for (i = 0; i < *ndevices; i++) {
if (!strcmp(devices[i].name, name)) { |
9e0f01d9 |
found = 1;
break;
}
}
if (!found) {
p = realloc(devices, sizeof(struct device) * (*ndevices + 1));
if (!(p)) { |
288057e9 |
for (i = 0; i < *ndevices; i++) |
9e0f01d9 |
free(devices[i].name);
free(devices);
return NULL;
}
devices = p; |
0882a904 |
memset(devices + *ndevices, 0x00, sizeof(struct device)); |
174a7ecc |
*ndevices = *ndevices + 1; |
9e0f01d9 |
}
} else { |
174a7ecc |
devices = calloc(1, sizeof(struct device)); |
9e0f01d9 |
if (!(devices))
return NULL;
*ndevices = 1;
}
|
174a7ecc |
if (*ndevices && !(devices[*ndevices - 1].name) && name)
devices[*ndevices - 1].name = strdup(name); |
cfeb7723 |
|
9e0f01d9 |
return devices;
}
|
aeafdcc0 |
#if HAVE_GETIFADDRS |
9e0f01d9 |
struct device *get_devices(void)
{ |
288057e9 |
struct ifaddrs *addrs = NULL, *addr;
struct device *devices = NULL;
size_t ndevices = 0, i, j; |
9e0f01d9 |
void *p;
uint8_t *mac;
int sock; |
aeafdcc0 |
|
db5ee846 |
#if defined(SIOCGIFHWADDR) && !defined(__GNU__) |
e9b62aae |
struct ifreq ifr; |
aeafdcc0 |
#else
struct sockaddr_dl *sdl;
#endif |
9e0f01d9 |
if (getifaddrs(&addrs))
return NULL;
for (addr = addrs; addr != NULL; addr = addr->ifa_next) {
if (!(addr->ifa_addr))
continue;
|
288057e9 |
/* |
cfeb7723 |
* Even though POSIX (BSD) sockets define AF_LINK, Linux decided to be clever
* and use AF_PACKET instead.
*/ |
aeafdcc0 |
#if defined(AF_PACKET) |
9e0f01d9 |
if (addr->ifa_addr->sa_family != AF_PACKET)
continue; |
aeafdcc0 |
#elif defined(AF_LINK)
if (addr->ifa_addr->sa_family != AF_LINK)
continue;
#else
break; /* We don't support anything else */
#endif |
9e0f01d9 |
devices = get_device_entry(devices, &ndevices, addr->ifa_name);
if (!(devices)) {
freeifaddrs(addrs);
return NULL;
} |
aeafdcc0 |
|
cfeb7723 |
/*
* Grab the MAC address for all devices that have them.
* Linux doesn't support (struct sockaddr_dl) as POSIX (BSD) sockets require.
* Instead, Linux uses its own ioctl. This code only runs if we're not Linux,
* Windows, or FreeBSD.
*/ |
db5ee846 |
#if !defined(SIOCGIFHWADDR) || defined(__GNU__) |
288057e9 |
for (i = 0; i < ndevices; i++) { |
e43a53ac |
if (!(strcmp(devices[i].name, addr->ifa_name))) { |
aeafdcc0 |
sdl = (struct sockaddr_dl *)(addr->ifa_addr);
#if defined(LLADDR)
mac = LLADDR(sdl);
#else
mac = ((uint8_t *)(sdl->sdl_data + sdl->sdl_nlen));
#endif |
288057e9 |
for (j = 0; j < 6; j++)
snprintf(devices[i].mac + strlen(devices[i].mac), sizeof(devices[i].mac) - strlen(devices[i].mac) - 1, "%02x:", mac[j]); |
aeafdcc0 |
break;
}
}
#endif |
9e0f01d9 |
}
if (addrs) {
freeifaddrs(addrs);
addrs = NULL;
}
|
cfeb7723 |
/* This is the Linux version of getting the MAC addresses */ |
db5ee846 |
#if defined(SIOCGIFHWADDR) && !defined(__GNU__) |
288057e9 |
for (i = 0; i < ndevices; i++) { |
174a7ecc |
if (!(devices[i].name))
continue;
|
9e0f01d9 |
memset(&ifr, 0x00, sizeof(struct ifreq)); |
0882a904 |
memset(devices[i].mac, 0x00, sizeof(devices[i].mac)); |
cfeb7723 |
|
0882a904 |
strcpy(ifr.ifr_name, devices[i].name); |
cfeb7723 |
|
aeafdcc0 |
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
goto err; |
cfeb7723 |
|
9e0f01d9 |
if (ioctl(sock, SIOCGIFHWADDR, &ifr)) {
close(sock);
goto err;
} |
cfeb7723 |
close(sock); |
0882a904 |
|
9e0f01d9 |
mac = ((uint8_t *)(ifr.ifr_ifru.ifru_hwaddr.sa_data)); |
0882a904 |
if (!(mac))
continue; |
aeafdcc0 |
|
288057e9 |
for (j = 0; j < 6; j++)
snprintf(devices[i].mac + strlen(devices[i].mac), sizeof(devices[i].mac) - strlen(devices[i].mac) - 1, "%02x:", mac[j]); |
9e0f01d9 |
} |
aeafdcc0 |
#endif |
9e0f01d9 |
p = realloc(devices, sizeof(struct device) * (ndevices + 1));
if (!(p))
goto err;
|
288057e9 |
devices = p;
devices[ndevices].name = NULL; |
9e0f01d9 |
memset(devices[ndevices].mac, 0x00, sizeof(devices[ndevices].mac));
return devices;
err:
if (devices) { |
288057e9 |
for (i = 0; i < ndevices; i++) |
0882a904 |
if (devices[i].name)
free(devices[i].name); |
9e0f01d9 |
free(devices);
}
return NULL;
}
#else
struct device *get_devices(void)
{
return NULL;
}
#endif /* HAVE_GETIFADDRS */
#if !HAVE_SYSCTLBYNAME && !defined(_WIN32) |
cfeb7723 |
/*
* Since we're getting potentially sensitive data (MAC addresses for all devices on the system),
* hash all the MAC addresses to provide basic anonymity and security.
*/ |
9e0f01d9 |
char *internal_get_host_id(void)
{
size_t i;
unsigned char raw_md5[16];
char *printable_md5;
struct device *devices; |
da6e06dd |
void *ctx; |
9e0f01d9 |
devices = get_devices();
if (!(devices))
return NULL;
|
b11a8465 |
printable_md5 = calloc(1, 37); |
0c58ddd2 |
if (!(printable_md5)) {
free(devices); |
9e0f01d9 |
return NULL; |
0c58ddd2 |
} |
9e0f01d9 |
|
da6e06dd |
ctx = cl_hash_init("md5"); |
f077c617 |
if (!(ctx)) { |
288057e9 |
for (i = 0; devices[i].name != NULL; i++) |
f077c617 |
free(devices[i].name);
free(devices);
free(printable_md5);
return NULL;
}
|
288057e9 |
for (i = 0; devices[i].name != NULL; i++) |
da6e06dd |
cl_update_hash(ctx, devices[i].mac, sizeof(devices[i].mac)); |
9e0f01d9 |
|
da6e06dd |
cl_finish_hash(ctx, raw_md5); |
9e0f01d9 |
|
288057e9 |
for (i = 0; devices[i].name != NULL; i++) |
9e0f01d9 |
free(devices[i].name);
free(devices);
|
288057e9 |
for (i = 0; i < sizeof(raw_md5); i++) { |
b11a8465 |
size_t len = strlen(printable_md5);
switch (len) {
case 8:
case 13:
case 18:
case 23:
printable_md5[len++] = '-';
break;
}
|
288057e9 |
sprintf(printable_md5 + len, "%02x", raw_md5[i]); |
b11a8465 |
} |
9e0f01d9 |
return printable_md5;
}
#endif |