source: protocols/proxy.c @ 68b50b5f

Last change on this file since 68b50b5f was 576d6d7, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-07T18:39:57Z

Got rid of some debugging code we tried to use to track the 100% CPU bug.
Turned out it was very suitable DoS code. :-)

  • Property mode set to 100644
File size: 15.1 KB
Line 
1/*
2 * gaim
3 *
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
5 * Copyright (C) 2002-2004, Wilmer van der Gaast, Jelmer Vernooij
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *
21 */
22
23/* this is a little piece of code to handle proxy connection */
24/* it is intended to : 1st handle http proxy, using the CONNECT command
25 , 2nd provide an easy way to add socks support */
26
27#define BITLBEE_CORE
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/types.h>
32#ifndef _WIN32
33#include <sys/socket.h>
34#include <netdb.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <unistd.h>
38#else
39#include "sock.h"
40#define ETIMEDOUT WSAETIMEDOUT
41#define EINPROGRESS WSAEINPROGRESS
42#endif
43#include <fcntl.h>
44#include <errno.h>
45#include "nogaim.h"
46#include "proxy.h"
47
48#define GAIM_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
49#define GAIM_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
50#define GAIM_ERR_COND   (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
51
52/*FIXME*               
53        #ifndef _WIN32
54                if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
55                        closesocket(fd);
56                        g_free(phb);
57                        return -1;
58                }
59                fcntl(fd, F_SETFL, 0);
60#endif*/
61
62char proxyhost[128] = "";
63int proxyport = 0;
64int proxytype = PROXY_NONE;
65char proxyuser[128] = "";
66char proxypass[128] = "";
67
68struct PHB {
69        GaimInputFunction func, proxy_func;
70        gpointer data, proxy_data;
71        char *host;
72        int port;
73        int fd;
74        gint inpa;
75};
76
77typedef struct _GaimIOClosure {
78        GaimInputFunction function;
79        guint result;
80        gpointer data;
81} GaimIOClosure;
82
83
84
85static struct sockaddr_in *gaim_gethostbyname(char *host, int port)
86{
87        static struct sockaddr_in sin;
88
89        if (!inet_aton(host, &sin.sin_addr)) {
90                struct hostent *hp;
91                if (!(hp = gethostbyname(host))) {
92                        return NULL;
93                }
94                memset(&sin, 0, sizeof(struct sockaddr_in));
95                memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
96                sin.sin_family = hp->h_addrtype;
97        } else
98                sin.sin_family = AF_INET;
99        sin.sin_port = htons(port);
100
101        return &sin;
102}
103
104static void gaim_io_destroy(gpointer data)
105{
106        g_free(data);
107}
108
109#ifdef PROXYPROFILER
110struct proxyprofiler
111{
112        GaimInputFunction function;
113        gpointer data;
114       
115        int count;
116       
117        struct proxyprofiler *next;
118} *pp = NULL;
119
120void proxyprofiler_dump()
121{
122        struct proxyprofiler *l;
123        char s[128];
124        FILE *fp;
125       
126        sprintf( s, "proxyprofiler.%d", (int) getpid() );
127        fp = fopen( s, "w" );
128       
129        fprintf( fp, "%-18s  %-18s  %10s\n", "Function", "Data", "Count" );
130        for( l = pp; l; l = l->next )
131                fprintf( fp, "0x%-16x  0x%-16x  %10d\n", (int) l->function, (int) l->data, l->count );
132       
133        fclose( fp );
134}
135#endif
136
137static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
138{
139        GaimIOClosure *closure = data;
140        GaimInputCondition gaim_cond = 0;
141
142#ifdef PROXYPROFILER
143        struct proxyprofiler *l;
144       
145        for( l = pp; l; l = l->next )
146        {
147                if( closure->function == l->function && closure->data == l->data )
148                        break;
149        }
150        if( l )
151        {
152                l->count ++;
153        }
154        else
155        {
156                l = g_new0( struct proxyprofiler, 1 );
157                l->function = closure->function;
158                l->data = closure->data;
159                l->count = 1;
160               
161                l->next = pp;
162                pp = l;
163        }
164#endif
165       
166        if (condition & GAIM_READ_COND)
167                gaim_cond |= GAIM_INPUT_READ;
168        if (condition & GAIM_WRITE_COND)
169                gaim_cond |= GAIM_INPUT_WRITE;
170//      if (condition & GAIM_ERR_COND)
171//              fprintf( stderr, "ERROR! fd=%d\n", g_io_channel_unix_get_fd( source ) );
172
173        closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond);
174
175        return TRUE;
176}
177
178static void no_one_calls(gpointer data, gint source, GaimInputCondition cond)
179{
180        struct PHB *phb = data;
181        unsigned int len;
182        int error = ETIMEDOUT;
183        len = sizeof(error);
184       
185#ifndef _WIN32
186        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
187                closesocket(source);
188                gaim_input_remove(phb->inpa);
189                if( phb->proxy_func )
190                        phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ);
191                else {
192                        phb->func(phb->data, -1, GAIM_INPUT_READ);
193                        g_free(phb);
194                }
195                return;
196        }
197        fcntl(source, F_SETFL, 0);
198#endif
199        gaim_input_remove(phb->inpa);
200        if( phb->proxy_func )
201                phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ);
202        else {
203                phb->func(phb->data, source, GAIM_INPUT_READ);
204                g_free(phb);
205        }
206}
207
208static int proxy_connect_none(char *host, unsigned short port, struct PHB *phb)
209{
210        struct sockaddr_in *sin;
211        int fd = -1;
212
213        if (!(sin = gaim_gethostbyname(host, port))) {
214                g_free(phb);
215                return -1;
216        }
217
218        if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
219                g_free(phb);
220                return -1;
221        }
222
223        sock_make_nonblocking(fd);
224
225        if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
226                if (sockerr_again()) {
227                        phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, no_one_calls, phb);
228                        phb->fd = fd;
229                } else {
230                        closesocket(fd);
231                        g_free(phb);
232                        return -1;
233                }
234        }
235
236        return fd;
237}
238
239
240/* Connecting to HTTP proxies */
241
242#define HTTP_GOODSTRING "HTTP/1.0 200 Connection established"
243#define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established"
244
245static void http_canread(gpointer data, gint source, GaimInputCondition cond)
246{
247        int nlc = 0;
248        int pos = 0;
249        struct PHB *phb = data;
250        char inputline[8192];
251
252        gaim_input_remove(phb->inpa);
253
254        while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) {
255                if (inputline[pos - 1] == '\n')
256                        nlc++;
257                else if (inputline[pos - 1] != '\r')
258                        nlc = 0;
259        }
260        inputline[pos] = '\0';
261
262        if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) ||
263            (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) {
264                phb->func(phb->data, source, GAIM_INPUT_READ);
265                g_free(phb->host);
266                g_free(phb);
267                return;
268        }
269
270        close(source);
271        phb->func(phb->data, -1, GAIM_INPUT_READ);
272        g_free(phb->host);
273        g_free(phb);
274        return;
275}
276
277static void http_canwrite(gpointer data, gint source, GaimInputCondition cond)
278{
279        char cmd[384];
280        struct PHB *phb = data;
281        unsigned int len;
282        int error = ETIMEDOUT;
283        if (phb->inpa > 0)
284                gaim_input_remove(phb->inpa);
285        len = sizeof(error);
286        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
287                close(source);
288                phb->func(phb->data, -1, GAIM_INPUT_READ);
289                g_free(phb->host);
290                g_free(phb);
291                return;
292        }
293#ifdef F_SETFL
294        fcntl(source, F_SETFL, 0);
295#endif
296
297        g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port,
298                   phb->host, phb->port);
299        if (send(source, cmd, strlen(cmd), 0) < 0) {
300                close(source);
301                phb->func(phb->data, -1, GAIM_INPUT_READ);
302                g_free(phb->host);
303                g_free(phb);
304                return;
305        }
306
307        if (proxyuser && *proxyuser) {
308                char *t1, *t2;
309                t1 = g_strdup_printf("%s:%s", proxyuser, proxypass);
310                t2 = tobase64(t1);
311                g_free(t1);
312                g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2);
313                g_free(t2);
314                if (send(source, cmd, strlen(cmd), 0) < 0) {
315                        close(source);
316                        phb->func(phb->data, -1, GAIM_INPUT_READ);
317                        g_free(phb->host);
318                        g_free(phb);
319                        return;
320                }
321        }
322
323        g_snprintf(cmd, sizeof(cmd), "\r\n");
324        if (send(source, cmd, strlen(cmd), 0) < 0) {
325                close(source);
326                phb->func(phb->data, -1, GAIM_INPUT_READ);
327                g_free(phb->host);
328                g_free(phb);
329                return;
330        }
331
332        phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb);
333}
334
335static int proxy_connect_http(char *host, unsigned short port, struct PHB *phb)
336{
337        phb->host = g_strdup(host);
338        phb->port = port;
339        phb->proxy_func = http_canwrite;
340        phb->proxy_data = phb;
341       
342        return( proxy_connect_none( proxyhost, proxyport, phb ) );
343}
344
345
346/* Connecting to SOCKS4 proxies */
347
348static void s4_canread(gpointer data, gint source, GaimInputCondition cond)
349{
350        unsigned char packet[12];
351        struct PHB *phb = data;
352
353        gaim_input_remove(phb->inpa);
354
355        memset(packet, 0, sizeof(packet));
356        if (read(source, packet, 9) >= 4 && packet[1] == 90) {
357                phb->func(phb->data, source, GAIM_INPUT_READ);
358                g_free(phb->host);
359                g_free(phb);
360                return;
361        }
362
363        close(source);
364        phb->func(phb->data, -1, GAIM_INPUT_READ);
365        g_free(phb->host);
366        g_free(phb);
367}
368
369static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond)
370{
371        unsigned char packet[12];
372        struct hostent *hp;
373        struct PHB *phb = data;
374        unsigned int len;
375        int error = ETIMEDOUT;
376        if (phb->inpa > 0)
377                gaim_input_remove(phb->inpa);
378        len = sizeof(error);
379        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
380                close(source);
381                phb->func(phb->data, -1, GAIM_INPUT_READ);
382                g_free(phb->host);
383                g_free(phb);
384                return;
385        }
386#ifdef F_SETFL
387        fcntl(source, F_SETFL, 0);
388#endif
389
390        /* XXX does socks4 not support host name lookups by the proxy? */
391        if (!(hp = gethostbyname(phb->host))) {
392                close(source);
393                phb->func(phb->data, -1, GAIM_INPUT_READ);
394                g_free(phb->host);
395                g_free(phb);
396                return;
397        }
398
399        packet[0] = 4;
400        packet[1] = 1;
401        packet[2] = phb->port >> 8;
402        packet[3] = phb->port & 0xff;
403        packet[4] = (unsigned char)(hp->h_addr_list[0])[0];
404        packet[5] = (unsigned char)(hp->h_addr_list[0])[1];
405        packet[6] = (unsigned char)(hp->h_addr_list[0])[2];
406        packet[7] = (unsigned char)(hp->h_addr_list[0])[3];
407        packet[8] = 0;
408        if (write(source, packet, 9) != 9) {
409                close(source);
410                phb->func(phb->data, -1, GAIM_INPUT_READ);
411                g_free(phb->host);
412                g_free(phb);
413                return;
414        }
415
416        phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb);
417}
418
419static int proxy_connect_socks4(char *host, unsigned short port, struct PHB *phb)
420{
421        phb->host = g_strdup(host);
422        phb->port = port;
423        phb->proxy_func = s4_canwrite;
424        phb->proxy_data = phb;
425       
426        return( proxy_connect_none( proxyhost, proxyport, phb ) );
427}
428
429
430/* Connecting to SOCKS5 proxies */
431
432static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond)
433{
434        unsigned char buf[512];
435        struct PHB *phb = data;
436
437        gaim_input_remove(phb->inpa);
438
439        if (read(source, buf, 10) < 10) {
440                close(source);
441                phb->func(phb->data, -1, GAIM_INPUT_READ);
442                g_free(phb->host);
443                g_free(phb);
444                return;
445        }
446        if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
447                close(source);
448                phb->func(phb->data, -1, GAIM_INPUT_READ);
449                g_free(phb->host);
450                g_free(phb);
451                return;
452        }
453
454        phb->func(phb->data, source, GAIM_INPUT_READ);
455        g_free(phb->host);
456        g_free(phb);
457        return;
458}
459
460static void s5_sendconnect(gpointer data, gint source)
461{
462        unsigned char buf[512];
463        struct PHB *phb = data;
464        int hlen = strlen(phb->host);
465
466        buf[0] = 0x05;
467        buf[1] = 0x01;          /* CONNECT */
468        buf[2] = 0x00;          /* reserved */
469        buf[3] = 0x03;          /* address type -- host name */
470        buf[4] = hlen;
471        memcpy(buf + 5, phb->host, hlen);
472        buf[5 + strlen(phb->host)] = phb->port >> 8;
473        buf[5 + strlen(phb->host) + 1] = phb->port & 0xff;
474
475        if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) {
476                close(source);
477                phb->func(phb->data, -1, GAIM_INPUT_READ);
478                g_free(phb->host);
479                g_free(phb);
480                return;
481        }
482
483        phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb);
484}
485
486static void s5_readauth(gpointer data, gint source, GaimInputCondition cond)
487{
488        unsigned char buf[512];
489        struct PHB *phb = data;
490
491        gaim_input_remove(phb->inpa);
492
493        if (read(source, buf, 2) < 2) {
494                close(source);
495                phb->func(phb->data, -1, GAIM_INPUT_READ);
496                g_free(phb->host);
497                g_free(phb);
498                return;
499        }
500
501        if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
502                close(source);
503                phb->func(phb->data, -1, GAIM_INPUT_READ);
504                g_free(phb->host);
505                g_free(phb);
506                return;
507        }
508
509        s5_sendconnect(phb, source);
510}
511
512static void s5_canread(gpointer data, gint source, GaimInputCondition cond)
513{
514        unsigned char buf[512];
515        struct PHB *phb = data;
516
517        gaim_input_remove(phb->inpa);
518
519        if (read(source, buf, 2) < 2) {
520                close(source);
521                phb->func(phb->data, -1, GAIM_INPUT_READ);
522                g_free(phb->host);
523                g_free(phb);
524                return;
525        }
526
527        if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
528                close(source);
529                phb->func(phb->data, -1, GAIM_INPUT_READ);
530                g_free(phb->host);
531                g_free(phb);
532                return;
533        }
534
535        if (buf[1] == 0x02) {
536                unsigned int i = strlen(proxyuser), j = strlen(proxypass);
537                buf[0] = 0x01;  /* version 1 */
538                buf[1] = i;
539                memcpy(buf + 2, proxyuser, i);
540                buf[2 + i] = j;
541                memcpy(buf + 2 + i + 1, proxypass, j);
542                if (write(source, buf, 3 + i + j) < 3 + i + j) {
543                        close(source);
544                        phb->func(phb->data, -1, GAIM_INPUT_READ);
545                        g_free(phb->host);
546                        g_free(phb);
547                        return;
548                }
549
550                phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);
551        } else {
552                s5_sendconnect(phb, source);
553        }
554}
555
556static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
557{
558        unsigned char buf[512];
559        int i;
560        struct PHB *phb = data;
561        unsigned int len;
562        int error = ETIMEDOUT;
563        if (phb->inpa > 0)
564                gaim_input_remove(phb->inpa);
565        len = sizeof(error);
566        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
567                close(source);
568                phb->func(phb->data, -1, GAIM_INPUT_READ);
569                g_free(phb->host);
570                g_free(phb);
571                return;
572        }
573#ifdef F_SETFL
574        fcntl(source, F_SETFL, 0);
575#endif
576
577        i = 0;
578        buf[0] = 0x05;          /* SOCKS version 5 */
579        if (proxyuser[0]) {
580                buf[1] = 0x02;  /* two methods */
581                buf[2] = 0x00;  /* no authentication */
582                buf[3] = 0x02;  /* username/password authentication */
583                i = 4;
584        } else {
585                buf[1] = 0x01;
586                buf[2] = 0x00;
587                i = 3;
588        }
589
590        if (write(source, buf, i) < i) {
591                close(source);
592                phb->func(phb->data, -1, GAIM_INPUT_READ);
593                g_free(phb->host);
594                g_free(phb);
595                return;
596        }
597
598        phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb);
599}
600
601static int proxy_connect_socks5(char *host, unsigned short port, struct PHB *phb)
602{
603        phb->host = g_strdup(host);
604        phb->port = port;
605        phb->proxy_func = s5_canwrite;
606        phb->proxy_data = phb;
607       
608        return( proxy_connect_none( proxyhost, proxyport, phb ) );
609}
610
611
612/* Export functions */
613
614gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data)
615{
616        GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
617        GIOChannel *channel;
618        GIOCondition cond = 0;
619       
620        closure->function = function;
621        closure->data = data;
622       
623        if (condition & GAIM_INPUT_READ)
624                cond |= GAIM_READ_COND;
625        if (condition & GAIM_INPUT_WRITE)
626                cond |= GAIM_WRITE_COND;
627       
628        channel = g_io_channel_unix_new(source);
629        closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
630                                              gaim_io_invoke, closure, gaim_io_destroy);
631       
632        g_io_channel_unref(channel);
633        return closure->result;
634}
635
636void gaim_input_remove(gint tag)
637{
638        if (tag > 0)
639                g_source_remove(tag);
640}
641
642int proxy_connect(char *host, int port, GaimInputFunction func, gpointer data)
643{
644        struct PHB *phb;
645       
646        if (!host || !port || (port == -1) || !func || strlen(host) > 128) {
647                return -1;
648        }
649       
650        phb = g_new0(struct PHB, 1);
651        phb->func = func;
652        phb->data = data;
653       
654#ifndef _WIN32
655        sethostent(1);
656#endif
657       
658        if ((proxytype == PROXY_NONE) || !proxyhost || !proxyhost[0] || !proxyport || (proxyport == -1))
659                return proxy_connect_none(host, port, phb);
660        else if (proxytype == PROXY_HTTP)
661                return proxy_connect_http(host, port, phb);
662        else if (proxytype == PROXY_SOCKS4)
663                return proxy_connect_socks4(host, port, phb);
664        else if (proxytype == PROXY_SOCKS5)
665                return proxy_connect_socks5(host, port, phb);
666       
667        if (phb->host) g_free(phb);
668        g_free(phb);
669        return -1;
670}
Note: See TracBrowser for help on using the repository browser.