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) && !defined(__GNU__)
52
#include <sys/ndd_var.h>
53
#include <sys/kinfo.h>
55
#include <linux/sockios.h>
61
#include <openssl/ssl.h>
62
#include <openssl/err.h>
63
#include "libclamav/crypto.h"
66
#include "libclamav/others.h"
68
struct device *get_device_entry(struct device *devices, size_t *ndevices, const char *name)
76
for (i = 0; i < *ndevices; i++) {
77
if (!strcmp(devices[i].name, name)) {
84
p = realloc(devices, sizeof(struct device) * (*ndevices + 1));
86
for (i=0; i < *ndevices; i++)
87
free(devices[i].name);
93
memset(devices + *ndevices, 0x00, sizeof(struct device));
94
*ndevices = *ndevices + 1;
97
devices = calloc(1, sizeof(struct device));
104
if (*ndevices && !(devices[*ndevices - 1].name) && name)
105
devices[*ndevices - 1].name = strdup(name);
111
struct device *get_devices(void)
113
struct ifaddrs *addrs=NULL, *addr;
114
struct device *devices=NULL;
115
size_t ndevices=0, i, j;
120
#if defined(SIOCGIFHWADDR) && !defined(__GNU__)
123
struct sockaddr_dl *sdl;
126
if (getifaddrs(&addrs))
129
for (addr = addrs; addr != NULL; addr = addr->ifa_next) {
130
if (!(addr->ifa_addr))
134
* Even though POSIX (BSD) sockets define AF_LINK, Linux decided to be clever
135
* and use AF_PACKET instead.
137
#if defined(AF_PACKET)
138
if (addr->ifa_addr->sa_family != AF_PACKET)
140
#elif defined(AF_LINK)
141
if (addr->ifa_addr->sa_family != AF_LINK)
144
break; /* We don't support anything else */
147
devices = get_device_entry(devices, &ndevices, addr->ifa_name);
154
* Grab the MAC address for all devices that have them.
155
* Linux doesn't support (struct sockaddr_dl) as POSIX (BSD) sockets require.
156
* Instead, Linux uses its own ioctl. This code only runs if we're not Linux,
157
* Windows, or FreeBSD.
159
#if !defined(SIOCGIFHWADDR) || defined(__GNU__)
160
for (i=0; i < ndevices; i++) {
161
if (!(strcmp(devices[i].name, addr->ifa_name))) {
162
sdl = (struct sockaddr_dl *)(addr->ifa_addr);
167
mac = ((uint8_t *)(sdl->sdl_data + sdl->sdl_nlen));
170
snprintf(devices[i].mac+strlen(devices[i].mac), sizeof(devices[i].mac)-strlen(devices[i].mac)-1, "%02x:", mac[j]);
183
/* This is the Linux version of getting the MAC addresses */
184
#if defined(SIOCGIFHWADDR) && !defined(__GNU__)
185
for (i=0; i < ndevices; i++) {
186
if (!(devices[i].name))
189
memset(&ifr, 0x00, sizeof(struct ifreq));
190
memset(devices[i].mac, 0x00, sizeof(devices[i].mac));
192
strcpy(ifr.ifr_name, devices[i].name);
194
sock = socket(AF_INET, SOCK_DGRAM, 0);
198
if (ioctl(sock, SIOCGIFHWADDR, &ifr)) {
204
mac = ((uint8_t *)(ifr.ifr_ifru.ifru_hwaddr.sa_data));
209
snprintf(devices[i].mac+strlen(devices[i].mac), sizeof(devices[i].mac)-strlen(devices[i].mac)-1, "%02x:", mac[j]);
213
p = realloc(devices, sizeof(struct device) * (ndevices + 1));
218
devices[ndevices].name = NULL;
219
memset(devices[ndevices].mac, 0x00, sizeof(devices[ndevices].mac));
225
for (i=0; i < ndevices; i++)
227
free(devices[i].name);
235
struct device *get_devices(void)
239
#endif /* HAVE_GETIFADDRS */
241
#if !HAVE_SYSCTLBYNAME && !defined(_WIN32)
243
* Since we're getting potentially sensitive data (MAC addresses for all devices on the system),
244
* hash all the MAC addresses to provide basic anonymity and security.
246
char *internal_get_host_id(void)
249
unsigned char raw_md5[16];
251
struct device *devices;
254
devices = get_devices();
258
printable_md5 = calloc(1, 37);
259
if (!(printable_md5)) {
264
ctx = cl_hash_init("md5");
266
for (i=0; devices[i].name != NULL; i++)
267
free(devices[i].name);
275
for (i=0; devices[i].name != NULL; i++)
276
cl_update_hash(ctx, devices[i].mac, sizeof(devices[i].mac));
278
cl_finish_hash(ctx, raw_md5);
280
for (i=0; devices[i].name != NULL; i++)
281
free(devices[i].name);
284
for (i=0; i < sizeof(raw_md5); i++) {
285
size_t len = strlen(printable_md5);
291
printable_md5[len++] = '-';
295
sprintf(printable_md5+len, "%02x", raw_md5[i]);
298
return printable_md5;