~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to libclamav/hostid.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved.
 
3
 *
 
4
 *  Author: Shawn Webb
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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,
 
18
 *  MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
#if HAVE_CONFIG_H
 
22
#include "clamav-config.h"
 
23
#endif
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
 
 
29
#if HAVE_UNISTD_H
 
30
#include <unistd.h>
 
31
#endif
 
32
 
 
33
#include <sys/types.h>
 
34
#include <fcntl.h>
 
35
 
 
36
#if !defined(_WIN32)
 
37
#include <sys/socket.h>
 
38
#include <netdb.h>
 
39
#include <sys/ioctl.h>
 
40
#endif
 
41
 
 
42
#if defined(HAVE_GETIFADDRS)
 
43
#include <net/if.h>
 
44
#if defined(HAVE_NET_IF_DL_H)
 
45
#include <net/if_dl.h>
 
46
#endif
 
47
#include <ifaddrs.h>
 
48
#endif
 
49
 
 
50
#if defined(SIOCGIFHWADDR) && !defined(__GNU__)
 
51
#if defined(_AIX)
 
52
#include <sys/ndd_var.h>
 
53
#include <sys/kinfo.h>
 
54
#else
 
55
#include <linux/sockios.h>
 
56
#endif
 
57
#endif
 
58
 
 
59
#include <errno.h>
 
60
 
 
61
#include <openssl/ssl.h>
 
62
#include <openssl/err.h>
 
63
#include "libclamav/crypto.h"
 
64
 
 
65
#include "hostid.h"
 
66
#include "libclamav/others.h"
 
67
 
 
68
struct device *get_device_entry(struct device *devices, size_t *ndevices, const char *name)
 
69
{
 
70
    void *p;
 
71
    size_t i;
 
72
 
 
73
    if ((devices)) {
 
74
        int found = 0;
 
75
 
 
76
        for (i = 0; i < *ndevices; i++) {
 
77
            if (!strcmp(devices[i].name, name)) {
 
78
                found = 1;
 
79
                break;
 
80
            }
 
81
        }
 
82
 
 
83
        if (!found) {
 
84
            p = realloc(devices, sizeof(struct device) * (*ndevices + 1));
 
85
            if (!(p)) {
 
86
                for (i=0; i < *ndevices; i++)
 
87
                    free(devices[i].name);
 
88
                free(devices);
 
89
                return NULL;
 
90
            }
 
91
            devices = p;
 
92
 
 
93
            memset(devices + *ndevices, 0x00, sizeof(struct device));
 
94
            *ndevices = *ndevices + 1;
 
95
        }
 
96
    } else {
 
97
        devices = calloc(1, sizeof(struct device));
 
98
        if (!(devices))
 
99
            return NULL;
 
100
 
 
101
        *ndevices = 1;
 
102
    }
 
103
 
 
104
    if (*ndevices && !(devices[*ndevices - 1].name) && name)
 
105
        devices[*ndevices - 1].name = strdup(name);
 
106
 
 
107
    return devices;
 
108
}
 
109
 
 
110
#if HAVE_GETIFADDRS
 
111
struct device *get_devices(void)
 
112
{
 
113
    struct ifaddrs *addrs=NULL, *addr;
 
114
    struct device *devices=NULL;
 
115
    size_t ndevices=0, i, j;
 
116
    void *p;
 
117
    uint8_t *mac;
 
118
    int sock;
 
119
 
 
120
#if defined(SIOCGIFHWADDR) && !defined(__GNU__)
 
121
    struct ifreq ifr;
 
122
#else
 
123
    struct sockaddr_dl *sdl;
 
124
#endif
 
125
 
 
126
    if (getifaddrs(&addrs))
 
127
        return NULL;
 
128
 
 
129
    for (addr = addrs; addr != NULL; addr = addr->ifa_next) {
 
130
        if (!(addr->ifa_addr))
 
131
            continue;
 
132
 
 
133
        /*
 
134
         * Even though POSIX (BSD) sockets define AF_LINK, Linux decided to be clever
 
135
         * and use AF_PACKET instead.
 
136
         */
 
137
#if defined(AF_PACKET)
 
138
        if (addr->ifa_addr->sa_family != AF_PACKET)
 
139
            continue;
 
140
#elif defined(AF_LINK)
 
141
        if (addr->ifa_addr->sa_family != AF_LINK)
 
142
            continue;
 
143
#else
 
144
        break; /* We don't support anything else */
 
145
#endif
 
146
 
 
147
        devices = get_device_entry(devices, &ndevices, addr->ifa_name);
 
148
        if (!(devices)) {
 
149
            freeifaddrs(addrs);
 
150
            return NULL;
 
151
        }
 
152
 
 
153
        /*
 
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.
 
158
         */
 
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);
 
163
 
 
164
#if defined(LLADDR)
 
165
                mac = LLADDR(sdl);
 
166
#else
 
167
                mac = ((uint8_t *)(sdl->sdl_data + sdl->sdl_nlen));
 
168
#endif
 
169
                for (j=0; j<6; j++)
 
170
                    snprintf(devices[i].mac+strlen(devices[i].mac), sizeof(devices[i].mac)-strlen(devices[i].mac)-1, "%02x:", mac[j]);
 
171
 
 
172
                break;
 
173
            }
 
174
        }
 
175
#endif
 
176
    }
 
177
 
 
178
    if (addrs) {
 
179
        freeifaddrs(addrs);
 
180
        addrs = NULL;
 
181
    }
 
182
 
 
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))
 
187
            continue;
 
188
 
 
189
        memset(&ifr, 0x00, sizeof(struct ifreq));
 
190
        memset(devices[i].mac, 0x00, sizeof(devices[i].mac));
 
191
 
 
192
        strcpy(ifr.ifr_name, devices[i].name);
 
193
 
 
194
        sock = socket(AF_INET, SOCK_DGRAM, 0);
 
195
        if (sock < 0)
 
196
            goto err;
 
197
 
 
198
        if (ioctl(sock, SIOCGIFHWADDR, &ifr)) {
 
199
            close(sock);
 
200
            goto err;
 
201
        }
 
202
        close(sock);
 
203
 
 
204
        mac = ((uint8_t *)(ifr.ifr_ifru.ifru_hwaddr.sa_data));
 
205
        if (!(mac))
 
206
            continue;
 
207
 
 
208
        for (j=0; j<6; j++)
 
209
            snprintf(devices[i].mac+strlen(devices[i].mac), sizeof(devices[i].mac)-strlen(devices[i].mac)-1, "%02x:", mac[j]);
 
210
    }
 
211
#endif
 
212
 
 
213
    p = realloc(devices, sizeof(struct device) * (ndevices + 1));
 
214
    if (!(p))
 
215
        goto err;
 
216
 
 
217
    devices = p;
 
218
    devices[ndevices].name =  NULL;
 
219
    memset(devices[ndevices].mac, 0x00, sizeof(devices[ndevices].mac));
 
220
 
 
221
    return devices;
 
222
 
 
223
err:
 
224
    if (devices) {
 
225
        for (i=0; i < ndevices; i++)
 
226
            if (devices[i].name)
 
227
                free(devices[i].name);
 
228
 
 
229
        free(devices);
 
230
    }
 
231
 
 
232
    return NULL;
 
233
}
 
234
#else
 
235
struct device *get_devices(void)
 
236
{
 
237
    return NULL;
 
238
}
 
239
#endif /* HAVE_GETIFADDRS */
 
240
 
 
241
#if !HAVE_SYSCTLBYNAME && !defined(_WIN32)
 
242
/*
 
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.
 
245
 */
 
246
char *internal_get_host_id(void)
 
247
{
 
248
    size_t i;
 
249
    unsigned char raw_md5[16];
 
250
    char *printable_md5;
 
251
    struct device *devices;
 
252
    void *ctx;
 
253
 
 
254
    devices = get_devices();
 
255
    if (!(devices))
 
256
        return NULL;
 
257
 
 
258
    printable_md5 = calloc(1, 37);
 
259
    if (!(printable_md5)) {
 
260
        free(devices);
 
261
        return NULL;
 
262
    }
 
263
 
 
264
    ctx = cl_hash_init("md5");
 
265
    if (!(ctx)) {
 
266
        for (i=0; devices[i].name != NULL; i++)
 
267
            free(devices[i].name);
 
268
 
 
269
        free(devices);
 
270
        free(printable_md5);
 
271
 
 
272
        return NULL;
 
273
    }
 
274
 
 
275
    for (i=0; devices[i].name != NULL; i++)
 
276
        cl_update_hash(ctx, devices[i].mac, sizeof(devices[i].mac));
 
277
 
 
278
    cl_finish_hash(ctx, raw_md5);
 
279
 
 
280
    for (i=0; devices[i].name != NULL; i++)
 
281
        free(devices[i].name);
 
282
    free(devices);
 
283
 
 
284
    for (i=0; i < sizeof(raw_md5); i++) {
 
285
        size_t len = strlen(printable_md5);
 
286
        switch (len) {
 
287
            case 8:
 
288
            case 13:
 
289
            case 18:
 
290
            case 23:
 
291
                printable_md5[len++] = '-';
 
292
                break;
 
293
        }
 
294
 
 
295
        sprintf(printable_md5+len, "%02x", raw_md5[i]);
 
296
    }
 
297
 
 
298
    return printable_md5;
 
299
}
 
300
#endif