summaryrefslogtreecommitdiff
path: root/src/core/hook/hook-connect.c
blob: f93b902e2eda92e655efacfd15d9fa91188fb027 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * hook-connect.c - WeeChat connect hook
 *
 * Copyright (C) 2003-2024 Sébastien Helleu <flashcode@flashtux.org>
 * Copyright (C) 2012 Simon Arlott
 *
 * This file is part of WeeChat, the extensible chat client.
 *
 * WeeChat 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 3 of the License, or
 * (at your option) any later version.
 *
 * WeeChat 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 WeeChat.  If not, see <https://www.gnu.org/licenses/>.
 */

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

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "../weechat.h"
#include "../core-hook.h"
#include "../core-infolist.h"
#include "../core-log.h"
#include "../core-network.h"
#include "../../plugins/plugin.h"


/*
 * Returns description of hook.
 *
 * Note: result must be freed after use.
 */

char *
hook_connect_get_description (struct t_hook *hook)
{
    char str_desc[1024];

    snprintf (str_desc, sizeof (str_desc),
              "socket: %d, address: %s, port: %d, child pid: %d",
              HOOK_CONNECT(hook, sock),
              HOOK_CONNECT(hook, address),
              HOOK_CONNECT(hook, port),
              HOOK_CONNECT(hook, child_pid));

    return strdup (str_desc);
}

/*
 * Hooks a connection to a peer (using fork).
 *
 * Returns pointer to new hook, NULL if error.
 */

struct t_hook *
hook_connect (struct t_weechat_plugin *plugin, const char *proxy,
              const char *address, int port, int ipv6, int retry,
              void *gnutls_sess, void *gnutls_cb, int gnutls_dhkey_size,
              const char *gnutls_priorities, const char *local_hostname,
              t_hook_callback_connect *callback,
              const void *callback_pointer,
              void *callback_data)
{
    struct t_hook *new_hook;
    struct t_hook_connect *new_hook_connect;
    int i;

    if (!address || (port <= 0) || !callback)
        return NULL;

    new_hook = malloc (sizeof (*new_hook));
    if (!new_hook)
        return NULL;
    new_hook_connect = malloc (sizeof (*new_hook_connect));
    if (!new_hook_connect)
    {
        free (new_hook);
        return NULL;
    }

    hook_init_data (new_hook, plugin, HOOK_TYPE_CONNECT, HOOK_PRIORITY_DEFAULT,
                    callback_pointer, callback_data);

    new_hook->hook_data = new_hook_connect;
    new_hook_connect->callback = callback;
    new_hook_connect->proxy = (proxy) ? strdup (proxy) : NULL;
    new_hook_connect->address = strdup (address);
    new_hook_connect->port = port;
    new_hook_connect->sock = -1;
    new_hook_connect->ipv6 = ipv6;
    new_hook_connect->retry = retry;
    new_hook_connect->gnutls_sess = gnutls_sess;
    new_hook_connect->gnutls_cb = gnutls_cb;
    new_hook_connect->gnutls_dhkey_size = gnutls_dhkey_size;
    new_hook_connect->gnutls_priorities = (gnutls_priorities) ?
        strdup (gnutls_priorities) : NULL;
    new_hook_connect->local_hostname = (local_hostname) ?
        strdup (local_hostname) : NULL;
    new_hook_connect->child_read = -1;
    new_hook_connect->child_write = -1;
    new_hook_connect->child_recv = -1;
    new_hook_connect->child_send = -1;
    new_hook_connect->child_pid = 0;
    new_hook_connect->hook_child_timer = NULL;
    new_hook_connect->hook_fd = NULL;
    new_hook_connect->handshake_hook_fd = NULL;
    new_hook_connect->handshake_hook_timer = NULL;
    new_hook_connect->handshake_fd_flags = 0;
    new_hook_connect->handshake_ip_address = NULL;
    if (!hook_socketpair_ok)
    {
        for (i = 0; i < HOOK_CONNECT_MAX_SOCKETS; i++)
        {
            new_hook_connect->sock_v4[i] = -1;
            new_hook_connect->sock_v6[i] = -1;
        }
    }

    hook_add_to_list (new_hook);

    network_connect_with_fork (new_hook);

    return new_hook;
}

/*
 * Verifies certificates.
 */

int
hook_connect_gnutls_verify_certificates (gnutls_session_t tls_session)
{
    struct t_hook *ptr_hook;
    int rc;

    rc = -1;
    ptr_hook = weechat_hooks[HOOK_TYPE_CONNECT];
    while (ptr_hook)
    {
        /* looking for the right hook using to the gnutls session pointer */
        if (!ptr_hook->deleted
            && HOOK_CONNECT(ptr_hook, gnutls_sess)
            && (*(HOOK_CONNECT(ptr_hook, gnutls_sess)) == tls_session))
        {
            rc = (int) (HOOK_CONNECT(ptr_hook, gnutls_cb))
                (ptr_hook->callback_pointer,
                 ptr_hook->callback_data,
                 tls_session, NULL, 0,
                 NULL, 0, NULL,
                 WEECHAT_HOOK_CONNECT_GNUTLS_CB_VERIFY_CERT);
            break;
        }
        ptr_hook = ptr_hook->next_hook;
    }

    return rc;
}

/*
 * Sets certificates.
 */

int
hook_connect_gnutls_set_certificates (gnutls_session_t tls_session,
                                      const gnutls_datum_t *req_ca, int nreq,
                                      const gnutls_pk_algorithm_t *pk_algos,
                                      int pk_algos_len,
#if LIBGNUTLS_VERSION_NUMBER >= 0x020b00 /* 2.11.0 */
                                      gnutls_retr2_st *answer)
#else
                                      gnutls_retr_st *answer)
#endif /* LIBGNUTLS_VERSION_NUMBER >= 0x020b00 */
{
    struct t_hook *ptr_hook;
    int rc;

    rc = -1;
    ptr_hook = weechat_hooks[HOOK_TYPE_CONNECT];
    while (ptr_hook)
    {
        /* looking for the right hook using to the gnutls session pointer */
        if (!ptr_hook->deleted
            && HOOK_CONNECT(ptr_hook, gnutls_sess)
            && (*(HOOK_CONNECT(ptr_hook, gnutls_sess)) == tls_session))
        {
            rc = (int) (HOOK_CONNECT(ptr_hook, gnutls_cb))
                (ptr_hook->callback_pointer,
                 ptr_hook->callback_data,
                 tls_session, req_ca, nreq,
                 pk_algos, pk_algos_len, answer,
                 WEECHAT_HOOK_CONNECT_GNUTLS_CB_SET_CERT);
            break;
        }
        ptr_hook = ptr_hook->next_hook;
    }

    return rc;
}

/*
 * Frees data in a connect hook.
 */

void
hook_connect_free_data (struct t_hook *hook)
{
    int i;

    if (!hook || !hook->hook_data)
        return;

    if (HOOK_CONNECT(hook, proxy))
    {
        free (HOOK_CONNECT(hook, proxy));
        HOOK_CONNECT(hook, proxy) = NULL;
    }
    if (HOOK_CONNECT(hook, address))
    {
        free (HOOK_CONNECT(hook, address));
        HOOK_CONNECT(hook, address) = NULL;
    }
    if (HOOK_CONNECT(hook, gnutls_priorities))
    {
        free (HOOK_CONNECT(hook, gnutls_priorities));
        HOOK_CONNECT(hook, gnutls_priorities) = NULL;
    }
    if (HOOK_CONNECT(hook, local_hostname))
    {
        free (HOOK_CONNECT(hook, local_hostname));
        HOOK_CONNECT(hook, local_hostname) = NULL;
    }
    if (HOOK_CONNECT(hook, hook_child_timer))
    {
        unhook (HOOK_CONNECT(hook, hook_child_timer));
        HOOK_CONNECT(hook, hook_child_timer) = NULL;
    }
    if (HOOK_CONNECT(hook, hook_fd))
    {
        unhook (HOOK_CONNECT(hook, hook_fd));
        HOOK_CONNECT(hook, hook_fd) = NULL;
    }
    if (HOOK_CONNECT(hook, handshake_hook_fd))
    {
        unhook (HOOK_CONNECT(hook, handshake_hook_fd));
        HOOK_CONNECT(hook, handshake_hook_fd) = NULL;
    }
    if (HOOK_CONNECT(hook, handshake_hook_timer))
    {
        unhook (HOOK_CONNECT(hook, handshake_hook_timer));
        HOOK_CONNECT(hook, handshake_hook_timer) = NULL;
    }
    if (HOOK_CONNECT(hook, handshake_ip_address))
    {
        free (HOOK_CONNECT(hook, handshake_ip_address));
        HOOK_CONNECT(hook, handshake_ip_address) = NULL;
    }
    if (HOOK_CONNECT(hook, child_pid) > 0)
    {
        kill (HOOK_CONNECT(hook, child_pid), SIGKILL);
        hook_schedule_clean_process (HOOK_CONNECT(hook, child_pid));
        HOOK_CONNECT(hook, child_pid) = 0;
    }
    if (HOOK_CONNECT(hook, child_read) != -1)
    {
        close (HOOK_CONNECT(hook, child_read));
        HOOK_CONNECT(hook, child_read) = -1;
    }
    if (HOOK_CONNECT(hook, child_write) != -1)
    {
        close (HOOK_CONNECT(hook, child_write));
        HOOK_CONNECT(hook, child_write) = -1;
    }
    if (HOOK_CONNECT(hook, child_recv) != -1)
    {
        close (HOOK_CONNECT(hook, child_recv));
        HOOK_CONNECT(hook, child_recv) = -1;
    }
    if (HOOK_CONNECT(hook, child_send) != -1)
    {
        close (HOOK_CONNECT(hook, child_send));
        HOOK_CONNECT(hook, child_send) = -1;
    }
    if (!hook_socketpair_ok)
    {
        for (i = 0; i < HOOK_CONNECT_MAX_SOCKETS; i++)
        {
            if (HOOK_CONNECT(hook, sock_v4[i]) != -1)
            {
                close (HOOK_CONNECT(hook, sock_v4[i]));
                HOOK_CONNECT(hook, sock_v4[i]) = -1;
            }
            if (HOOK_CONNECT(hook, sock_v6[i]) != -1)
            {
                close (HOOK_CONNECT(hook, sock_v6[i]));
                HOOK_CONNECT(hook, sock_v6[i]) = -1;
            }
        }
    }

    free (hook->hook_data);
    hook->hook_data = NULL;
}

/*
 * Adds connect hook data in the infolist item.
 *
 * Returns:
 *   1: OK
 *   0: error
 */

int
hook_connect_add_to_infolist (struct t_infolist_item *item,
                              struct t_hook *hook)
{
    if (!item || !hook || !hook->hook_data)
        return 0;

    if (!infolist_new_var_pointer (item, "callback", HOOK_CONNECT(hook, callback)))
        return 0;
    if (!infolist_new_var_string (item, "address", HOOK_CONNECT(hook, address)))
        return 0;
    if (!infolist_new_var_integer (item, "port", HOOK_CONNECT(hook, port)))
        return 0;
    if (!infolist_new_var_integer (item, "sock", HOOK_CONNECT(hook, sock)))
        return 0;
    if (!infolist_new_var_integer (item, "ipv6", HOOK_CONNECT(hook, ipv6)))
        return 0;
    if (!infolist_new_var_integer (item, "retry", HOOK_CONNECT(hook, retry)))
        return 0;
    if (!infolist_new_var_pointer (item, "gnutls_sess", HOOK_CONNECT(hook, gnutls_sess)))
        return 0;
    if (!infolist_new_var_pointer (item, "gnutls_cb", HOOK_CONNECT(hook, gnutls_cb)))
        return 0;
    if (!infolist_new_var_integer (item, "gnutls_dhkey_size", HOOK_CONNECT(hook, gnutls_dhkey_size)))
        return 0;
    if (!infolist_new_var_string (item, "local_hostname", HOOK_CONNECT(hook, local_hostname)))
        return 0;
    if (!infolist_new_var_integer (item, "child_read", HOOK_CONNECT(hook, child_read)))
        return 0;
    if (!infolist_new_var_integer (item, "child_write", HOOK_CONNECT(hook, child_write)))
        return 0;
    if (!infolist_new_var_integer (item, "child_recv", HOOK_CONNECT(hook, child_recv)))
        return 0;
    if (!infolist_new_var_integer (item, "child_send", HOOK_CONNECT(hook, child_send)))
        return 0;
    if (!infolist_new_var_integer (item, "child_pid", HOOK_CONNECT(hook, child_pid)))
        return 0;
    if (!infolist_new_var_pointer (item, "hook_child_timer", HOOK_CONNECT(hook, hook_child_timer)))
        return 0;
    if (!infolist_new_var_pointer (item, "hook_fd", HOOK_CONNECT(hook, hook_fd)))
        return 0;
    if (!infolist_new_var_pointer (item, "handshake_hook_fd", HOOK_CONNECT(hook, handshake_hook_fd)))
        return 0;
    if (!infolist_new_var_pointer (item, "handshake_hook_timer", HOOK_CONNECT(hook, handshake_hook_timer)))
        return 0;
    if (!infolist_new_var_integer (item, "handshake_fd_flags", HOOK_CONNECT(hook, handshake_fd_flags)))
        return 0;
    if (!infolist_new_var_string (item, "handshake_ip_address", HOOK_CONNECT(hook, handshake_ip_address)))
        return 0;

    return 1;
}

/*
 * Prints connect hook data in WeeChat log file (usually for crash dump).
 */

void
hook_connect_print_log (struct t_hook *hook)
{
    int i;

    if (!hook || !hook->hook_data)
        return;

    log_printf ("  connect data:");
    log_printf ("    callback. . . . . . . : %p", HOOK_CONNECT(hook, callback));
    log_printf ("    proxy . . . . . . . . : '%s'", HOOK_CONNECT(hook, proxy));
    log_printf ("    address . . . . . . . : '%s'", HOOK_CONNECT(hook, address));
    log_printf ("    port. . . . . . . . . : %d", HOOK_CONNECT(hook, port));
    log_printf ("    sock. . . . . . . . . : %d", HOOK_CONNECT(hook, sock));
    log_printf ("    ipv6. . . . . . . . . : %d", HOOK_CONNECT(hook, ipv6));
    log_printf ("    retry . . . . . . . . : %d", HOOK_CONNECT(hook, retry));
    log_printf ("    gnutls_sess . . . . . : %p", HOOK_CONNECT(hook, gnutls_sess));
    log_printf ("    gnutls_cb . . . . . . : %p", HOOK_CONNECT(hook, gnutls_cb));
    log_printf ("    gnutls_dhkey_size . . : %d", HOOK_CONNECT(hook, gnutls_dhkey_size));
    log_printf ("    gnutls_priorities . . : '%s'", HOOK_CONNECT(hook, gnutls_priorities));
    log_printf ("    local_hostname. . . . : '%s'", HOOK_CONNECT(hook, local_hostname));
    log_printf ("    child_read. . . . . . : %d", HOOK_CONNECT(hook, child_read));
    log_printf ("    child_write . . . . . : %d", HOOK_CONNECT(hook, child_write));
    log_printf ("    child_recv. . . . . . : %d", HOOK_CONNECT(hook, child_recv));
    log_printf ("    child_send. . . . . . : %d", HOOK_CONNECT(hook, child_send));
    log_printf ("    child_pid . . . . . . : %d", HOOK_CONNECT(hook, child_pid));
    log_printf ("    hook_child_timer. . . : %p", HOOK_CONNECT(hook, hook_child_timer));
    log_printf ("    hook_fd . . . . . . . : %p", HOOK_CONNECT(hook, hook_fd));
    log_printf ("    handshake_hook_fd . . : %p", HOOK_CONNECT(hook, handshake_hook_fd));
    log_printf ("    handshake_hook_timer. : %p", HOOK_CONNECT(hook, handshake_hook_timer));
    log_printf ("    handshake_fd_flags. . : %d", HOOK_CONNECT(hook, handshake_fd_flags));
    log_printf ("    handshake_ip_address. : '%s'", HOOK_CONNECT(hook, handshake_ip_address));
    if (!hook_socketpair_ok)
    {
        for (i = 0; i < HOOK_CONNECT_MAX_SOCKETS; i++)
        {
            log_printf ("    sock_v4[%03d]. . . . . : '%d'", i, HOOK_CONNECT(hook, sock_v4[i]));
            log_printf ("    sock_v6[%03d]. . . . . : '%d'", i, HOOK_CONNECT(hook, sock_v6[i]));
        }
    }
}