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

« back to all changes in this revision

Viewing changes to libclamav/json.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
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
 
 
25
#if HAVE_UNISTD_H
 
26
#include <unistd.h>
 
27
#endif
 
28
 
 
29
#include <sys/types.h>
 
30
#include <errno.h>
 
31
 
 
32
#if defined(_WIN32)
 
33
#include <WinSock2.h>
 
34
#include <Windows.h>
 
35
#endif
 
36
 
 
37
#include <openssl/ssl.h>
 
38
#include <openssl/err.h>
 
39
#include "libclamav/crypto.h"
 
40
 
 
41
#include "libclamav/others.h"
 
42
#include "libclamav/clamav.h"
 
43
 
 
44
#define JSON_BUFSZ 512
 
45
#define SAMPLE_PREFIX "sample_"
 
46
 
 
47
char *hex_encode(char *buf, char *data, size_t len)
 
48
{
 
49
    size_t i;
 
50
    char *p;
 
51
    int t;
 
52
 
 
53
    p = (buf != NULL) ? buf : calloc(1, (len*2)+1);
 
54
    if (!(p))
 
55
        return NULL;
 
56
 
 
57
    for (i=0; i<len; i++) {
 
58
        t = data[i] & 0xff;
 
59
        sprintf(p+(i*2), "%02x", t);
 
60
    }
 
61
 
 
62
    return p;
 
63
}
 
64
 
 
65
char *ensure_bufsize(char *buf, size_t *oldsize, size_t used, size_t additional)
 
66
{
 
67
    char *p=buf;
 
68
 
 
69
    if (*oldsize - used < additional) {
 
70
        p = realloc(buf, *oldsize + JSON_BUFSZ);
 
71
        if (!(p)) {
 
72
            cli_errmsg("ensure_bufsize: Could not allocate more memory: %s (errno: %d)\n", strerror(errno), errno);
 
73
            free(buf);
 
74
            return NULL;
 
75
        }
 
76
 
 
77
        *oldsize += JSON_BUFSZ;
 
78
    }
 
79
 
 
80
    return p;
 
81
}
 
82
 
 
83
char *export_stats_to_json(struct cl_engine *engine, cli_intel_t *intel)
 
84
{
 
85
    char *buf=NULL, *p, *hostid, md5[33];
 
86
    cli_flagged_sample_t *sample;
 
87
    size_t bufsz, curused, i, j;
 
88
 
 
89
    if (!(intel->hostid))
 
90
        if ((engine->cb_stats_get_hostid))
 
91
            intel->hostid = engine->cb_stats_get_hostid(engine->stats_data);
 
92
 
 
93
    hostid = (intel->hostid != NULL) ? intel->hostid : STATS_ANON_UUID;
 
94
 
 
95
    buf = calloc(1, JSON_BUFSZ);
 
96
    if (!(buf))
 
97
        return NULL;
 
98
 
 
99
    bufsz = JSON_BUFSZ;
 
100
    sprintf(buf, "{\n\t\"hostid\": \"%s\",\n", hostid);
 
101
    if (intel->host_info)
 
102
        sprintf(buf+strlen(buf), "\t\"host_info\": \"%s\",\n", intel->host_info);
 
103
 
 
104
    sprintf(buf+strlen(buf), "\t\"samples\": [\n");
 
105
    curused = strlen(buf);
 
106
 
 
107
    for (sample = intel->samples; sample != NULL; sample = sample->next) {
 
108
        if (sample->hits == 0)
 
109
            continue;
 
110
 
 
111
        memset(md5, 0x00, sizeof(md5));
 
112
        hex_encode(md5, sample->md5, sizeof(sample->md5));
 
113
 
 
114
        buf = ensure_bufsize(buf, &bufsz, curused, strlen(md5) + sizeof(SAMPLE_PREFIX) + 45);
 
115
        if (!(buf))
 
116
            return NULL;
 
117
 
 
118
        snprintf(buf+curused, bufsz-curused, "\t\t\t{\n");
 
119
        curused += strlen(buf+curused);
 
120
 
 
121
        buf = ensure_bufsize(buf, &bufsz, curused, sizeof("\t\t\t\"hash\": \"\",\n") + strlen(md5) + 1);
 
122
        if (!(buf))
 
123
            return NULL;
 
124
 
 
125
        snprintf(buf+curused, bufsz-curused, "\t\t\t\"hash\": \"%s\",\n", md5);
 
126
        curused += strlen(buf+curused);
 
127
 
 
128
        /* Reuse the md5 variable for serializing the number of hits */
 
129
        snprintf(md5, sizeof(md5), "%u", sample->hits);
 
130
 
 
131
        buf = ensure_bufsize(buf, &bufsz, curused, strlen(md5) + 20);
 
132
        if (!(buf))
 
133
            return NULL;
 
134
 
 
135
        snprintf(buf+curused, bufsz-curused, "\t\t\t\"hits\": %s,\n", md5);
 
136
        curused += strlen(buf+curused);
 
137
 
 
138
        snprintf(md5, sizeof(md5), "%u", sample->size);
 
139
 
 
140
        buf = ensure_bufsize(buf, &bufsz, curused, strlen(md5) + 20);
 
141
        if (!(buf))
 
142
            return NULL;
 
143
 
 
144
        snprintf(buf+curused, bufsz-curused, "\t\t\t\"size\": %s,\n", md5);
 
145
        curused += strlen(buf+curused);
 
146
 
 
147
        buf = ensure_bufsize(buf, &bufsz, curused, 30);
 
148
        if (!(buf))
 
149
            return NULL;
 
150
 
 
151
        if ((sample->sections) && (sample->sections->nsections)) {
 
152
            buf = ensure_bufsize(buf, &bufsz, curused, 30);
 
153
            if (!(buf))
 
154
                return NULL;
 
155
 
 
156
            snprintf(buf+curused, bufsz-curused, "\t\t\t\"sections\": [\n");
 
157
            curused += strlen(buf+curused);
 
158
 
 
159
            for (i=0; i < sample->sections->nsections; i++) {
 
160
                buf = ensure_bufsize(buf, &bufsz, curused, 30);
 
161
                if (!(buf))
 
162
                    return NULL;
 
163
 
 
164
                snprintf(buf+curused, bufsz-curused, "\t\t\t\t%s{\n", (i > 0) ? "," : "");
 
165
                curused += strlen(buf+curused);
 
166
 
 
167
                buf = ensure_bufsize(buf, &bufsz, curused, 65);
 
168
                if (!(buf))
 
169
                    return NULL;
 
170
 
 
171
                memset(md5, 0x00, sizeof(md5));
 
172
                for (j=0; j < 16; j++)
 
173
                    sprintf(md5+(j*2), "%02x", sample->sections->sections[i].md5[j]);
 
174
 
 
175
                snprintf(buf+curused, bufsz-curused, "\t\t\t\t\t\"hash\": \"%s\",\n", md5);
 
176
                curused += strlen(buf+curused);
 
177
 
 
178
                buf = ensure_bufsize(buf, &bufsz, curused, 65);
 
179
                if (!(buf))
 
180
                    return NULL;
 
181
 
 
182
                snprintf(buf+curused, bufsz-curused, "\t\t\t\t\t\"size\": %u\n", sample->sections->sections[i].len);
 
183
                curused += strlen(buf+curused);
 
184
 
 
185
                buf = ensure_bufsize(buf, &bufsz, curused, 30);
 
186
                if (!(buf))
 
187
                    return NULL;
 
188
 
 
189
                snprintf(buf+curused, bufsz-curused, "\t\t\t\t}\n");
 
190
                curused += strlen(buf+curused);
 
191
            }
 
192
 
 
193
            buf = ensure_bufsize(buf, &bufsz, curused, 20);
 
194
            if (!(buf))
 
195
                return NULL;
 
196
 
 
197
            snprintf(buf+curused, bufsz-curused, "\t\t\t],\n");
 
198
            curused += strlen(buf+curused);
 
199
        }
 
200
 
 
201
        snprintf(buf+curused, bufsz-curused, "\t\t\t\"virus_names\": [ ");
 
202
        curused += strlen(buf+curused);
 
203
 
 
204
        for (i=0; sample->virus_name[i] != NULL; i++) {
 
205
            buf = ensure_bufsize(buf, &bufsz, curused, strlen(sample->virus_name[i]) + 5);
 
206
            if (!(buf))
 
207
                return NULL;
 
208
 
 
209
            snprintf(buf+curused, bufsz-curused, "%s\"%s\"", (i > 0) ? ", " : "", sample->virus_name[i]);
 
210
            curused += strlen(buf+curused);
 
211
        }
 
212
 
 
213
        buf = ensure_bufsize(buf, &bufsz, curused, 10);
 
214
        if (!(buf))
 
215
            return NULL;
 
216
 
 
217
        snprintf(buf+curused, bufsz-curused, " ]\n\t\t}%s\n", (sample->next != NULL) ? "," : "");
 
218
        curused += strlen(buf+curused);
 
219
    }
 
220
 
 
221
    buf = ensure_bufsize(buf, &bufsz, curused, 15);
 
222
    if (!(buf))
 
223
        return NULL;
 
224
 
 
225
    snprintf(buf+curused, bufsz-curused, "\t]\n}\n");
 
226
 
 
227
    return buf;
 
228
}