File: VtFileDist.c

package info (click to toggle)
c-vtapi 0.0~git20230329.226eda8-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: ansic: 3,924; makefile: 66
file content (338 lines) | stat: -rw-r--r-- 8,360 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright 2014 VirusTotal S.L. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#define _GNU_SOURCE

#ifdef HAVE_CONFIG_H
#include "c-vtapi_config.h"
#endif


#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>

#if !defined(_WIN32) && !defined(_WIN64)
#include <unistd.h>
#endif

#include <time.h>
#include <jansson.h>
#include <stdbool.h>
#include <curl/curl.h>


#include "VtObject.h"
#include "VtApiPage.h"
#include "VtResponse.h"
#include "VtFileDist.h"

#include "vtcapi_common.h"

struct VtFileDist {
  API_OBJECT_COMMON;
  unsigned long long before;
  unsigned long long after;
  bool reports;
  int limit;
};


/**
* @name Constructor and Destructor
* @{
*/

/**
*  VtObjects constructor
*  @arg VtObject that was just allocated
*/
int VtFileDist_constructor(struct VtObject *obj) {
  struct VtFileDist *vt_udist = (struct VtFileDist *)obj;

  DBG(DGB_LEVEL_MEM, " constructor %p\n", vt_udist);

  return 0;
}


/**
*  VtObjects destructor
*  @arg VtObject that is going to be free'd
*/
int VtFileDist_destructor(struct VtObject *obj) {
  struct VtFileDist *vt_udist = (struct VtFileDist *)obj;

  DBG(DGB_LEVEL_MEM, " destructor %p\n", vt_udist);

  // Parent destructor
  return VtApiPage_destructor((struct VtObject *)obj);
}



/** @} */


static struct VtObject_ops obj_ops = {
  .obj_type           = "VtFileDist",
  .obj_size           = sizeof(struct VtFileDist),
  .obj_constructor    = VtFileDist_constructor,
  .obj_destructor     = VtFileDist_destructor,
// 	.obj_from_json      = VtFileDist_objectFromJSON,
};

static struct VtFileDist* VtFileDist_alloc(struct VtObject_ops *ops) {
  struct VtFileDist *FileScan;

  FileScan = (struct VtFileDist*) VtObject_alloc(ops);
  return FileScan;
}


struct VtFileDist* VtFileDist_new(void) {
  struct VtFileDist *FileScan = VtFileDist_alloc(&obj_ops);

  return FileScan;
}

/** Get a reference counter */
void VtFileDist_get(struct VtFileDist *obj) {
  VtObject_get((struct VtObject*) obj);
}

/** put a reference counter */
void VtFileDist_put(struct VtFileDist **obj) {
  VtApiPage_put((struct VtApiPage**) obj);
}

void VtFileDist_setApiKey(struct VtFileDist *vt_udist, const char *api_key) {
  // Call parent function
  return VtApiPage_setApiKey((struct VtApiPage *)vt_udist, api_key);
}

void VtFileDist_setReports(struct VtFileDist *vt_udist, bool value) {
  vt_udist->reports = value;
}

void VtFileDist_setAfter(struct VtFileDist *vt_udist, unsigned long long  value) {
  vt_udist->after = value;
}

void VtFileDist_setBefore(struct VtFileDist *vt_udist, unsigned long long  value) {
  vt_udist->before = value;
}

void VtFileDist_setLimit(struct VtFileDist *vt_udist, int value) {
  vt_udist->limit = value;
}

struct VtResponse * VtFileDist_getResponse(struct VtFileDist *vt_udist) {
  VtResponse_get(vt_udist->response);
  return vt_udist->response;
}

int VtFileDist_getDistribution(struct VtFileDist *vt_udist) {

  CURL *curl;
  CURLcode res;
  int ret = 0;
  char get_url[512];
  int len = 0;
  long http_response_code = 0;


  VtApiPage_resetBuffer((struct VtApiPage *) vt_udist);
  curl = curl_easy_init();
  if (!curl) {
    VT_ERROR("init curl\n");
    goto cleanup;
  }

  DBG(1, "Api Key =  '%s'\n", vt_udist->api_key);

  if (ret)
    VT_ERROR("Adding key\n");

  len = snprintf(get_url, 511, VT_API_BASE_URL "file/distribution?apikey=%s", vt_udist->api_key);
  if (len < 0) {
    VT_ERROR("snprintf\n");
    goto cleanup;
  }

  if (vt_udist->before) {
    len += ret = sprintf(get_url + len, "&before=%lld", vt_udist->before);
    if (ret < 0) {
      VT_ERROR("sprintf before\n");
      goto cleanup;
    }
  }

  if (vt_udist->after) {
    len += ret = sprintf(get_url + len, "&after=%lld", vt_udist->after);
    if (ret < 0) {
      VT_ERROR("sprintf after\n");
      goto cleanup;
    }
  }

  if (vt_udist->reports) {
    len += ret = sprintf(get_url + len, "&reports=true");
    if (ret < 0) {
      VT_ERROR("sprintf after\n");
      goto cleanup;
    }
  }

  if (vt_udist->limit) {
    len += ret = sprintf(get_url + len, "&limit=%d", vt_udist->limit);
    if (ret < 0) {
      VT_ERROR("sprintf after\n");
      goto cleanup;
    }
  }
  DBG(1, "URL=%s\n", get_url);
  curl_easy_setopt(curl, CURLOPT_URL, get_url);

#ifdef DISABLE_HTTPS_VALIDATION
  curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L); // disable validation
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif



  /* enable verbose for easier tracing */
  if (debug_level)
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, __VtApiPage_WriteCb); // callback for data
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, vt_udist); // user arg


  /* Perform the request, res will get the return code */
  res = curl_easy_perform(curl);
  DBG(1, "Perform done\n");
  /* Check for errors */
  if(res != CURLE_OK) {
    VT_ERROR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
    goto cleanup;
  } else {
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_response_code);
    if (http_response_code != 200) {
      VT_ERROR("HTTP Response code: %ld\n", http_response_code);
      ret = http_response_code;
      goto cleanup;
    }
  }
  DBG(1, "Page:\n%s\n",vt_udist->buffer);

  // if a previous response
  if (vt_udist->response)
    VtResponse_put(&vt_udist->response);   // relase reference counter

  vt_udist->response = VtResponse_new(); // new response object

  ret = VtResponse_fromJSONstr(vt_udist->response, vt_udist->buffer);
  if (ret) {
    VT_ERROR("Parsing JSON\n");
    goto cleanup;
  }

cleanup:
  /* always cleanup */
  curl_easy_cleanup(curl);

  return ret;
}

int VtFileDist_parse(struct VtFileDist* url_dist,
                     VtFileDistCb cb,
                     void *user_data) {
  json_t *resp_json, *file_jobj;
  json_t *url_str_json, *timestamp_json, *name_json, *sha256_json;
  int index;

  if (!url_dist || !url_dist->response) {
    VT_ERROR("No data received\n");
    return -1;
  }

  resp_json =  VtResponse_getJanssonObj(url_dist->response);

  if (!json_is_array(resp_json)) {
    VT_ERROR("JSON is not array\n");
    return -1;
  }

  json_array_foreach(resp_json, index, file_jobj) {

    if (!json_is_object(file_jobj)) {
      VT_ERROR("Parse error not a URL object\n");
      return -1;
    }

    url_str_json = json_object_get(file_jobj, "link");
    if (!url_str_json || !json_is_string(url_str_json)) {
      VT_ERROR("Parse error: link string\n");
      return -1;
    }
    name_json = json_object_get(file_jobj, "name");
    if (!name_json || !json_is_string(name_json)) {
      name_json = NULL;
    }
    sha256_json = json_object_get(file_jobj, "sha256");
    if (!sha256_json || !json_is_string(sha256_json)) {
      VT_ERROR("Parse error: sha256 string\n");
      return -1;
    }

    timestamp_json = json_object_get(file_jobj, "timestamp");
    if (!timestamp_json || !json_is_integer(timestamp_json)) {
      VT_ERROR("JSON parse error timestamp\n");
      return -1;
    }

    // set the after value, so if we do another query, we will not repeat the same data
    url_dist->after = json_integer_value(timestamp_json);

    // Call user defined callback function
    if (cb)
      cb(json_string_value(url_str_json), json_integer_value(timestamp_json),
         json_string_value(sha256_json), json_string_value(name_json),
         file_jobj, user_data);

  }

  return 0;
}

int VtFileDist_process(struct VtFileDist* url_dist,
                       void (*cb)(const char *url, unsigned long long timestamp, const char *sha256hash, const char *name, json_t *raw_json, void *data),
                       void *user_data) {
  int ret;

  ret = VtFileDist_getDistribution(url_dist);
  if (ret) {
    return ret;
  }

  return  VtFileDist_parse(url_dist, cb, user_data);
}