2
* Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 2 as
8
* published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include "clamav-config.h"
33
#include <sys/types.h>
37
#include <sys/socket.h>
39
#include <sys/ioctl.h>
42
#if defined(HAVE_GETIFADDRS)
44
#if defined(HAVE_NET_IF_DL_H)
45
#include <net/if_dl.h>
50
#if defined(SIOCGIFHWADDR)
52
#include <sys/ndd_var.h>
53
#include <sys/kinfo.h>
55
#include <linux/sockios.h>
63
#include "libclamav/others.h"
65
struct device *get_device_entry(struct device *devices, size_t *ndevices, const char *name);
67
struct device *get_device_entry(struct device *devices, size_t *ndevices, const char *name)
75
for (i = 0; i < *ndevices; i++) {
76
if (!strcmp(devices[i].name, name)) {
83
p = realloc(devices, sizeof(struct device) * (*ndevices + 1));
85
for (i=0; i < *ndevices; i++)
86
free(devices[i].name);
92
memset(devices + *ndevices, 0x00, sizeof(struct device));
93
*ndevices = *ndevices + 1;
96
devices = calloc(1, sizeof(struct device));
103
if (*ndevices && !(devices[*ndevices - 1].name) && name)
104
devices[*ndevices - 1].name = strdup(name);
110
struct device *get_devices(void)
112
struct ifaddrs *addrs=NULL, *addr;
113
struct device *devices=NULL;
114
size_t ndevices=0, i, j;
119
#if defined(SIOCGIFHWADDR)
122
struct sockaddr_dl *sdl;
125
if (getifaddrs(&addrs))
128
for (addr = addrs; addr != NULL; addr = addr->ifa_next) {
129
if (!(addr->ifa_addr))
133
* Even though POSIX (BSD) sockets define AF_LINK, Linux decided to be clever
134
* and use AF_PACKET instead.
136
#if defined(AF_PACKET)
137
if (addr->ifa_addr->sa_family != AF_PACKET)
139
#elif defined(AF_LINK)
140
if (addr->ifa_addr->sa_family != AF_LINK)
143
break; /* We don't support anything else */
146
devices = get_device_entry(devices, &ndevices, addr->ifa_name);
153
* Grab the MAC address for all devices that have them.
154
* Linux doesn't support (struct sockaddr_dl) as POSIX (BSD) sockets require.
155
* Instead, Linux uses its own ioctl. This code only runs if we're not Linux,
156
* Windows, or FreeBSD.
158
#if !defined(SIOCGIFHWADDR)
159
for (i=0; i < ndevices; i++) {
160
if (!(strcmp(devices[i].name, addr->ifa_name))) {
161
sdl = (struct sockaddr_dl *)(addr->ifa_addr);
166
mac = ((uint8_t *)(sdl->sdl_data + sdl->sdl_nlen));
169
snprintf(devices[i].mac+strlen(devices[i].mac), sizeof(devices[i].mac)-strlen(devices[i].mac)-1, "%02x:", mac[j]);
182
/* This is the Linux version of getting the MAC addresses */
183
#if defined(SIOCGIFHWADDR)
184
for (i=0; i < ndevices; i++) {
185
if (!(devices[i].name))
188
memset(&ifr, 0x00, sizeof(struct ifreq));
189
memset(devices[i].mac, 0x00, sizeof(devices[i].mac));
191
strcpy(ifr.ifr_name, devices[i].name);
193
sock = socket(AF_INET, SOCK_DGRAM, 0);
197
if (ioctl(sock, SIOCGIFHWADDR, &ifr)) {
203
mac = ((uint8_t *)(ifr.ifr_ifru.ifru_hwaddr.sa_data));
208
snprintf(devices[i].mac+strlen(devices[i].mac), sizeof(devices[i].mac)-strlen(devices[i].mac)-1, "%02x:", mac[j]);
212
p = realloc(devices, sizeof(struct device) * (ndevices + 1));
217
devices[ndevices].name = NULL;
218
memset(devices[ndevices].mac, 0x00, sizeof(devices[ndevices].mac));
224
for (i=0; i < ndevices; i++)
226
free(devices[i].name);
234
struct device *get_devices(void)
238
#endif /* HAVE_GETIFADDRS */
240
#if !HAVE_SYSCTLBYNAME && !defined(_WIN32)
242
* Since we're getting potentially sensitive data (MAC addresses for all devices on the system),
243
* hash all the MAC addresses to provide basic anonymity and security.
245
char *internal_get_host_id(void)
248
unsigned char raw_md5[16];
250
struct device *devices;
253
devices = get_devices();
257
printable_md5 = calloc(1, 37);
258
if (!(printable_md5)) {
263
ctx = cl_hash_init("md5");
265
for (i=0; devices[i].name != NULL; i++)
266
free(devices[i].name);
274
for (i=0; devices[i].name != NULL; i++)
275
cl_update_hash(ctx, devices[i].mac, sizeof(devices[i].mac));
277
cl_finish_hash(ctx, raw_md5);
279
for (i=0; devices[i].name != NULL; i++)
280
free(devices[i].name);
283
for (i=0; i < sizeof(raw_md5); i++) {
284
size_t len = strlen(printable_md5);
290
printable_md5[len++] = '-';
294
sprintf(printable_md5+len, "%02x", raw_md5[i]);
297
return printable_md5;