aboutsummaryrefslogtreecommitdiffstats
path: root/update.c
blob: f3969f6ccb2d9aa2a3c6d8334c62ebd11160771d (plain)
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
339
340
341
342
343
/* update.c
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <glib.h>
#include <string.h>
#include <stdio.h>

#include <epan/prefs.h>
#include <epan/prefs-int.h>
#include <epan/filesystem.h>

#include "simple_dialog.h"
#include "version_info.h"

#ifdef HAVE_LIBPCAP
#include "capture-pcap-util.h"
#endif

#include <wsutil/file_util.h>

#include <wininet.h>
#include "nio-ie5.h"


/* update information about a single component */
typedef struct update_info_s {
    char *prefix;                   /* prefix of the update file keys */
    gboolean needs_update;          /* does this component need an update */
    char *version_installed;        /* the version currently installed */

    char *title;                    /* the component title (name) */
    char *description;              /* description of the component */
    char *version_recommended;      /* the version recommended */
    char *url;                      /* the URL for an update */
    char *md5;                      /* md5 checksum for that update */
    char *size;                     /* size of that update */
} update_info_t;


/* download a complete file from the internet */
int
download_file(const char *url, const char *filename) {
    netio_ie5_t * conn;
    char buf[100];
    int chunk_len;
    int fd;
    int stream_len;
    int ret = 0;


    /* open output file */
    fd = ws_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
    if(fd == -1) {
        g_warning("Couldn't open output file %s!", filename);
        return -1;
    }

    /* connect to url */
    conn = netio_ie5_connect (url);
    if (conn == NULL) {
        ws_close(fd);
        g_warning("Couldn't connect to %s!", url);
        return -1;
    }

    do {
		/* XXX - maybe add a progress bar here */

        /* read some bytes from the url */
        chunk_len = netio_ie5_read (conn, buf, sizeof(buf));

        /* write bytes to the output file */
        stream_len = ws_write( fd, buf, chunk_len);
        if(stream_len != chunk_len) {
            g_warning("output failed: stream_len %u != chunk_len %u", stream_len, chunk_len);
            ret = -1;
            break;
        }
    } while(chunk_len > 0);

    netio_ie5_disconnect(conn);

    ws_close(fd);

    return ret;
}

update_info_t *
update_info_new(void)
{
    return g_malloc0(sizeof(update_info_t));
}

void
update_info_delete(update_info_t *update_info)
{
    g_free(update_info->prefix);
    g_free(update_info->version_installed);
    g_free(update_info->title);
    g_free(update_info->description);
    g_free(update_info->version_recommended);
    g_free(update_info->url);
    g_free(update_info->md5);
    g_free(update_info->size);

    g_free(update_info);
}

/* check a single key value pair */
static void
update_pref_check(gchar *pref_name, gchar *value, char *check_prefix, char *check_name, char **check_value)
{
    GString *check = g_string_new(check_prefix);

    g_string_append(check, check_name);

    if(strcmp(pref_name, check->str) == 0) {
        if(*check_value)
            /* there shouldn't be a duplicate entry in the update file */
            g_warning("Duplicate of %s: current %s former %s", pref_name, value, *check_value);
        else
            *check_value = g_strdup(value);
    }

    g_string_free(check, TRUE);
}

/* a new key value pair from the update file */
static prefs_set_pref_e
update_pref(gchar *pref_name, gchar *value, void *private_data)
{
    update_info_t *update_info = private_data;

    update_pref_check(pref_name, value, update_info->prefix, "title",       &update_info->title);
    update_pref_check(pref_name, value, update_info->prefix, "description", &update_info->description);
    update_pref_check(pref_name, value, update_info->prefix, "version",     &update_info->version_recommended);
    update_pref_check(pref_name, value, update_info->prefix, "update.url",  &update_info->url);
    update_pref_check(pref_name, value, update_info->prefix, "update.md5",  &update_info->md5);
    update_pref_check(pref_name, value, update_info->prefix, "update.size",  &update_info->size);

    return PREFS_SET_OK;
}

/* display an update_info */
static void
update_info_display(update_info_t *update_info)
{
    GString *overview;


    overview = g_string_new("");

    if(update_info->title) {
        g_string_append_printf(overview, "%s%s%s",
            simple_dialog_primary_start(), update_info->title, simple_dialog_primary_end());
    } else {
        g_string_append_printf(overview, "%sComponent%s",
            simple_dialog_primary_start(), simple_dialog_primary_end());
    }

    g_string_append(overview, "\n\n");

    if(update_info->description)
        g_string_append_printf(overview, "%s\n\n", update_info->description);

    g_string_append_printf(overview, "Installed: %s\n", update_info->version_installed);

    if(update_info->version_recommended)
        g_string_append_printf(overview, "Recommended: %s\n", update_info->version_recommended);
    else
        g_string_append(overview, "Recommenced: unknown\n");

    if(update_info->version_recommended && update_info->url)
        g_string_append_printf(overview, "From: %s\n", update_info->url);

    if(update_info->size)
        g_string_append_printf(overview, "Size: %s", update_info->size);

    simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, overview->str);

    g_string_free(overview, TRUE);

}

/* check the version of the wireshark program */
static update_info_t *
update_check_wireshark(const char *local_file)
{
    FILE *pf;
    update_info_t *update_info = update_info_new();


    update_info->version_installed = g_strdup(VERSION);
    update_info->prefix = "wireshark.setup.";

    pf = ws_fopen(local_file, "r");
    if(pf != NULL) {
        /* read in update_info of Wireshark */
        read_prefs_file(local_file, pf, update_pref, update_info);
        fclose(pf);

        /* check if Wireshark needs an update */
        if(update_info->version_installed && update_info->version_recommended &&
            strcmp(update_info->version_installed, update_info->version_recommended) != 0)
        {
            update_info->needs_update = TRUE;
        }
    } else {
        g_warning("Could not open %s", local_file);
    }

    return update_info;
}

/* check the version of winpcap */
static update_info_t *
update_check_winpcap(const char *local_file)
{
    FILE *pf;
    update_info_t * update_info = update_info_new();
    GString *pcap_version_tmp;
    char *pcap_version = NULL;
    char *pcap_vstart;
    char *pcap_vend;


    update_info->prefix = "winpcap.";

    pf = ws_fopen(local_file, "r");
    if(pf != NULL) {
        /* read in update_info of WinPcap */
        read_prefs_file(local_file, pf, update_pref, update_info);
        fclose(pf);

        /* get WinPcap version */
        /* XXX - what's the "approved" method to get the WinPcap version? */
        pcap_version_tmp = g_string_new("");
        get_runtime_pcap_version(pcap_version_tmp);

        /* cut out real version from "combined" version string */
        pcap_vstart = strstr(pcap_version_tmp->str, "with WinPcap version ");
        if(pcap_vstart != NULL) {
            pcap_vstart += sizeof("with WinPcap version");
            pcap_vend = strstr(pcap_vstart, " ");
            if(pcap_vend != NULL) {
                pcap_vend[0] = 0;
                pcap_version = g_strdup(pcap_vstart);
            }
        }

        update_info->version_installed = g_strdup(pcap_version);

        if(pcap_version && update_info->version_recommended &&
            strcmp(pcap_version, update_info->version_recommended) != 0)
        {
            update_info->needs_update = TRUE;
        }
    } else {
        g_warning("Could not open %s", local_file);
    }

    g_string_free(pcap_version_tmp, TRUE);
    g_free(pcap_version);

    return update_info;
}


/* check for all updates */
void
update_check(gboolean interactive)
{
    char *local_file;
    const char *url_file = "http://127.0.0.1/wsupdate";	/* XXX - build the URL depending on platform, versions, ... */
    update_info_t *update_info_wireshark;
    update_info_t *update_info_winpcap;


    /* build update file name */
    /* XXX - using the personal path, use temp dir instead? */
    local_file = get_persconffile_path("wsupdate", FALSE, TRUE /*for_writing*/);
    if(local_file == NULL) {
        g_warning("Couldn't create output path!");
        return;
    }

    /* download update file */
    if(download_file(url_file, local_file) == -1) {
        g_warning("Couldn't download update file: %s", local_file);
        g_free(local_file);
        return;
    }

    /* check wireshark */
    update_info_wireshark = update_check_wireshark(local_file);

    /* check winpcap */
    update_info_winpcap = update_check_winpcap(local_file);

    /* display results */
    if(update_info_wireshark->needs_update || update_info_winpcap->needs_update) {
        if(update_info_wireshark->needs_update)
            update_info_display(update_info_wireshark);
        if(update_info_winpcap->needs_update)
            update_info_display(update_info_winpcap);
    } else {
        if(interactive) {
            simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, "No updates available");
        }
    }

    /* cleanup */
    update_info_delete(update_info_wireshark);
    update_info_delete(update_info_winpcap);

    g_free(local_file);
}