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

« back to all changes in this revision

Viewing changes to clamsubmit/clamsubmit.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
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <unistd.h>
 
4
#include <string.h>
 
5
 
 
6
#include <openssl/ssl.h>
 
7
#include <openssl/err.h>
 
8
#include "libclamav/crypto.h"
 
9
 
 
10
#include <curl/curl.h>
 
11
 
 
12
#include "libclamav/clamav.h"
 
13
#include "libclamav/others.h"
 
14
#include "shared/misc.h"
 
15
#include "shared/getopt.h"
 
16
 
 
17
#define OPTS "e:p:n:N:H:h?v"
 
18
 
 
19
char *read_stream(void);
 
20
 
 
21
void usage(char *name)
 
22
{
 
23
    fprintf(stderr, "USAGE: %s -hHinp?\n", name);
 
24
    fprintf(stderr, "OPTIONS:\n");
 
25
    fprintf(stderr, "    -e [EMAIL]\tYour email address (required)\n");
 
26
    fprintf(stderr, "    -h or -?\tShow the help text\n");
 
27
    fprintf(stderr, "    -n [FILE]\tSubmit a false negative (FN)\n");
 
28
    fprintf(stderr, "    -N [NAME]\tYour name (required)\n");
 
29
    fprintf(stderr, "    -p [FILE]\tSubmit a fase positive (FP)\n");
 
30
    fprintf(stderr, "    -v\t\tShow version number and exit\n");
 
31
    fprintf(stderr, "You must specify -n or -p. Both are mutually exclusive. Pass in - as the filename for stdin.\n");
 
32
    exit(0);
 
33
}
 
34
 
 
35
void version(void)
 
36
{
 
37
    print_version(NULL);
 
38
    exit(0);
 
39
}
 
40
 
 
41
int main(int argc, char *argv[])
 
42
{
 
43
    CURL *curl;
 
44
    CURLcode res;
 
45
    int ch;
 
46
    struct curl_httppost *post=NULL, *last=NULL;
 
47
    struct curl_slist *slist = NULL;
 
48
    char *type;
 
49
    char *name=NULL, *email=NULL, *filename=NULL;
 
50
    struct cl_engine *engine;
 
51
    int setURL=0, fromStream=0;
 
52
 
 
53
    curl_global_init(CURL_GLOBAL_ALL);
 
54
 
 
55
    curl = curl_easy_init();
 
56
    if (!(curl)) {
 
57
        fprintf(stderr, "ERROR: Could not initialize libcurl\n");
 
58
        exit(1);
 
59
    }
 
60
 
 
61
    while ((ch = my_getopt(argc, argv, OPTS)) > 0) {
 
62
        switch (ch) {
 
63
            case 'v':
 
64
                version();
 
65
            case 'e':
 
66
                email = optarg;
 
67
                break;
 
68
            case 'N':
 
69
                name = optarg;
 
70
                break;
 
71
            case 'p':
 
72
                if (setURL)
 
73
                    usage(argv[0]);
 
74
 
 
75
                filename = optarg;
 
76
 
 
77
                curl_easy_setopt(curl, CURLOPT_URL, "http://cgi.clamav.net/sendfp.cgi");
 
78
                setURL=1;
 
79
                break;
 
80
            case 'n':
 
81
                if (setURL)
 
82
                    usage(argv[0]);
 
83
 
 
84
                filename = optarg;
 
85
 
 
86
                curl_easy_setopt(curl, CURLOPT_URL, "http://cgi.clamav.net/sendmalware.cgi");
 
87
                setURL=1;
 
88
                break;
 
89
            case 'h':
 
90
            case '?':
 
91
            default:
 
92
                usage(argv[0]);
 
93
        }
 
94
    }
 
95
 
 
96
    if (!(name) || !(email) || !(filename))
 
97
        usage(argv[0]);
 
98
 
 
99
    if (strlen(filename) == 1 && filename[0] == '-') {
 
100
        filename = read_stream();
 
101
        if (!(filename)) {
 
102
            fprintf(stderr, "ERROR: Unable to read stream\n");
 
103
            exit(1);
 
104
        }
 
105
 
 
106
        fromStream=1;
 
107
    }
 
108
 
 
109
    slist = curl_slist_append(slist, "Expect:");
 
110
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
 
111
 
 
112
    if (curl_formadd(&post, &last, CURLFORM_COPYNAME, "sendername", CURLFORM_COPYCONTENTS, name, CURLFORM_END)) {
 
113
        fprintf(stderr, "Unable to specify name in libcurl form for file %s\n", optarg);
 
114
        goto end;
 
115
    }
 
116
 
 
117
    if (curl_formadd(&post, &last, CURLFORM_COPYNAME, "email", CURLFORM_COPYCONTENTS, email, CURLFORM_END)) {
 
118
        fprintf(stderr, "Unable to specify email in libcurl form for file %s\n", optarg);
 
119
        goto end;
 
120
    }
 
121
 
 
122
    if (curl_formadd(&post, &last, CURLFORM_COPYNAME, "file", CURLFORM_FILE, filename, CURLFORM_END)) {
 
123
        fprintf(stderr, "Unable to specify file path in libcurl form for file %s\n", optarg);
 
124
        goto end;
 
125
    }
 
126
 
 
127
    curl_formadd(&post, &last, CURLFORM_COPYNAME, "action", CURLFORM_COPYCONTENTS, "submit", CURLFORM_END);
 
128
    curl_formadd(&post, &last, CURLFORM_COPYNAME, "privacy", CURLFORM_COPYCONTENTS, "yes", CURLFORM_END);
 
129
    curl_formadd(&post, &last, CURLFORM_COPYNAME, "notify", CURLFORM_COPYCONTENTS, "yes", CURLFORM_END);
 
130
 
 
131
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
 
132
    res = curl_easy_perform(curl);
 
133
 
 
134
    if (res) {
 
135
        fprintf(stderr, "Error: %s\n", curl_easy_strerror(res));
 
136
    }
 
137
 
 
138
end:
 
139
    curl_easy_cleanup(curl);
 
140
    if (fromStream) {
 
141
        remove(filename);
 
142
        free(filename);
 
143
    }
 
144
 
 
145
    return 0;
 
146
}
 
147
 
 
148
char *read_stream(void)
 
149
{
 
150
    char *filename;
 
151
    char buf[512];
 
152
    size_t nread, nwritten;
 
153
    FILE *fp;
 
154
 
 
155
    filename = cli_gentemp(NULL);
 
156
    if (!(filename)) {
 
157
        return NULL;
 
158
    }
 
159
 
 
160
    fp = fopen(filename, "w");
 
161
    if (!(fp)) {
 
162
        free(filename);
 
163
        return NULL;
 
164
    }
 
165
 
 
166
    while (!feof(stdin)) {
 
167
        nwritten = 0;
 
168
        nread = fread(buf, 1, sizeof(buf), stdin);
 
169
        if (nread == 0) {
 
170
            fclose(fp);
 
171
            remove(filename);
 
172
            free(filename);
 
173
            return NULL;
 
174
        }
 
175
 
 
176
        while (nwritten < nread) {
 
177
            size_t i;
 
178
            i = fwrite(buf, 1, nread, fp);
 
179
            if (i == 0) {
 
180
                fclose(fp);
 
181
                remove(filename);
 
182
                free(filename);
 
183
                return NULL;
 
184
            }
 
185
 
 
186
            nwritten += i;
 
187
        }
 
188
    }
 
189
 
 
190
    fclose(fp);
 
191
 
 
192
    return filename;
 
193
}