Browse code

Add basic submission code

Shawn Webb authored on 2014/01/07 08:40:21
Showing 1 changed files
... ...
@@ -1,6 +1,99 @@
1 1
 #include <stdio.h>
2
+#include <stdlib.h>
3
+#include <unistd.h>
4
+#include <string.h>
5
+
6
+#include <curl/curl.h>
7
+
8
+#include "libclamav/clamav.h"
9
+#include "libclamav/others.h"
10
+
11
+void usage(char *name)
12
+{
13
+    fprintf(stderr, "USAGE: %s -pnih?\n", name);
14
+    fprintf(stderr, "OPTIONS:\n");
15
+    fprintf(stderr, "    -p [FILE]\tSubmit a fase positive (FP)\n");
16
+    fprintf(stderr, "    -n [FILE]\tSubmit a false negative (FN)\n");
17
+    fprintf(stderr, "    -i [FILE]\tSubmit an \"interesting\" file\n");
18
+    fprintf(stderr, "    -h or -?\tShow the help text\n");
19
+    exit(0);
20
+}
2 21
 
3 22
 int main(int argc, char *argv[])
4 23
 {
24
+    CURL *curl;
25
+    CURLcode res;
26
+    int ch;
27
+    struct curl_httppost *post=NULL, *last=NULL;
28
+    struct curl_slist *slist = NULL;
29
+    char *type;
30
+    char *hostid;
31
+    struct cl_engine *engine;
32
+
33
+    cl_init(0);
34
+    engine = cl_engine_new();
35
+    if (!(engine)) {
36
+        fprintf(stderr, "Unable to create new ClamAV engine\n");
37
+        return 1;
38
+    }
39
+
40
+    hostid = engine->cb_stats_get_hostid(engine->stats_data);
41
+    printf("HostID: %s\n", hostid);
42
+
43
+    curl_global_init(CURL_GLOBAL_ALL);
44
+
45
+    curl = curl_easy_init();
46
+    curl_easy_setopt(curl, CURLOPT_URL, "http://stats.clamav.dev:8080/clamav/1/submit/file");
47
+
48
+    slist = curl_slist_append(slist, "Expect:");
49
+    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
50
+
51
+    while ((ch = my_getopt(argc, argv, "p:n:i:h?")) > 0) {
52
+        switch (ch) {
53
+            case 'p':
54
+                type = "fp";
55
+                break;
56
+            case 'n':
57
+                type = "fn";
58
+                break;
59
+            case 'i':
60
+                type = "interesting";
61
+                break;
62
+            case '?':
63
+            case 'h':
64
+            default:
65
+                usage(argv[0]);
66
+        }
67
+
68
+        if ((post))
69
+            curl_formfree(post);
70
+
71
+        post = last = NULL;
72
+
73
+        if (curl_formadd(&post, &last, CURLFORM_COPYNAME, "type", CURLFORM_COPYCONTENTS, type, CURLFORM_END)) {
74
+            fprintf(stderr, "Unable to specify type of file in libcurl form for file %s\n", optarg);
75
+            break;
76
+        }
77
+
78
+        if (curl_formadd(&post, &last, CURLFORM_COPYNAME, "file", CURLFORM_FILE, optarg, CURLFORM_END)) {
79
+            fprintf(stderr, "Unable to specify file path in libcurl form for file %s\n", optarg);
80
+            break;
81
+        }
82
+
83
+        if (curl_formadd(&post, &last, CURLFORM_COPYNAME, "hostid", CURLFORM_COPYCONTENTS, hostid, CURLFORM_END)) {
84
+            fprintf(stderr, "Unable to specify HostID in libcurl form for file %s\n", optarg);
85
+            break;
86
+        }
87
+
88
+        curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
89
+        res = curl_easy_perform(curl);
90
+
91
+        if (res) {
92
+            fprintf(stderr, "Error: %s\n", curl_easy_strerror(res));
93
+        }
94
+    }
95
+
96
+    curl_easy_cleanup(curl);
97
+
5 98
     return 0;
6 99
 }