~ubuntu-branches/ubuntu/precise/ettercap/precise

« back to all changes in this revision

Viewing changes to src/ec_grell.c

  • Committer: Bazaar Package Importer
  • Author(s): Murat Demirten
  • Date: 2003-06-21 19:57:18 UTC
  • Revision ID: james.westby@ubuntu.com-20030621195718-qqbbfk18e1djchd9
Tags: upstream-0.6.b
ImportĀ upstreamĀ versionĀ 0.6.b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    ettercap -- Grell -- HTTPS dissector
 
3
 
 
4
    Copyright (C) 2001  ALoR <alor@users.sourceforge.net>, NaGA <crwm@freemail.it>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 
 
20
    $Id: ec_grell.c,v 1.23 2003/07/08 09:37:20 lordnaga Exp $
 
21
*/
 
22
 
 
23
#include "include/ec_main.h"
 
24
 
 
25
#if defined (HAVE_OPENSSL) && defined (PERMIT_HTTPS)  // don't compile if you don't have OpenSSL
 
26
 
 
27
#include <fcntl.h>
 
28
 
 
29
#include <openssl/ssl.h>
 
30
#include <sys/poll.h>
 
31
 
 
32
#include "include/ec_inet_structures.h"
 
33
#include "include/ec_inet_forge.h"
 
34
#include "include/ec_dissector.h"
 
35
#include "include/ec_buffer.h"
 
36
#include "include/ec_decodedata.h"
 
37
#include "include/ec_thread.h"
 
38
 
 
39
int Grell_ProxyIP = 0;
 
40
int Grell_ProxyPort = 8080;
 
41
int proxy_fd, https_fd;
 
42
 
 
43
typedef struct
 
44
{
 
45
    unsigned int    ServerIP;
 
46
    unsigned short  ServerPort;
 
47
    int             client_fd, server_fd, ssl_type;
 
48
    struct          sockaddr_in client_sin, server_sin;
 
49
    SSL_CTX         *ssl_ctx_client, *ssl_ctx_server;
 
50
    SSL             *ssl_client, *ssl_server;
 
51
} public_data;
 
52
 
 
53
public_data father_data;
 
54
 
 
55
pthread_mutex_t father_mutex = PTHREAD_MUTEX_INITIALIZER;
 
56
 
 
57
// protos...
 
58
 
 
59
void Grell_Dissector(char *payload, CONNECTION *data_to_ettercap);
 
60
void Grell_init(void);
 
61
void Grell_fini(void *dummy);
 
62
void * Grell_spawn(void *local_father_data);
 
63
void * Grell_start(void *);
 
64
pthread_t Grell_Run(void);
 
65
int Grell_ToBeSniffed(SNIFFED_DATA *data);
 
66
 
 
67
// ================================
 
68
int Grell_ToBeSniffed(SNIFFED_DATA *data)
 
69
{
 
70
   char s=0, ps=0, d=0, pd=0;
 
71
 
 
72
   if (current_illithid_data.proto != 'A')
 
73
      if (current_illithid_data.proto != data->proto) return 0;
 
74
 
 
75
   if (current_illithid_data.source_port == 0) ps = 1;
 
76
   if (current_illithid_data.dest_port == 0) pd = 1;
 
77
   if (current_illithid_data.source_ip == 0) s = 1;
 
78
   if (current_illithid_data.dest_ip == 0) d = 1;
 
79
 
 
80
   if (s || current_illithid_data.source_ip == data->fast_source_ip)
 
81
      if (ps || current_illithid_data.source_port == data->source_port)
 
82
      {  s = 1;   ps = 1;  }
 
83
 
 
84
   if (s || current_illithid_data.source_ip == data->fast_dest_ip)
 
85
      if (ps || current_illithid_data.source_port == data->dest_port)
 
86
      {  s = 1;   ps = 1;  }
 
87
 
 
88
   if (d || current_illithid_data.dest_ip == data->fast_source_ip)
 
89
      if (pd || current_illithid_data.dest_port == data->source_port)
 
90
      {  d = 1;   pd = 1;  }
 
91
 
 
92
   if (d || current_illithid_data.dest_ip == data->fast_dest_ip)
 
93
      if (pd || current_illithid_data.dest_port == data->dest_port)
 
94
      {  d = 1;   pd = 1;  }
 
95
 
 
96
   return ( Options.reverse ^ (s && ps && d && pd) );
 
97
}
 
98
 
 
99
 
 
100
 
 
101
void Grell_Dissector(char *payload, CONNECTION *data_to_ettercap)
 
102
{
 
103
   char *buf;
 
104
 
 
105
   buf = Inet_Forge_packet(MAX_DATA*5+data_to_ettercap->datalen+10);                         // prepare the packet for the HTTP dissector
 
106
   Inet_Forge_tcp( buf+40, data_to_ettercap->source_port,      // create a fake tcp header
 
107
                        data_to_ettercap->dest_port,
 
108
                        0xabadc0de,
 
109
                        0xabadc0de,
 
110
                        0,
 
111
                        payload,
 
112
                        data_to_ettercap->datalen);
 
113
 
 
114
   Dissector_http(buf+40, data_to_ettercap, NULL, 1, 443);
 
115
 
 
116
   Inet_Forge_packet_destroy( buf );
 
117
 
 
118
}
 
119
 
 
120
static void client_parse(char *buf, int len, public_data *son_data)
 
121
{
 
122
   CONNECTION data_to_ettercap;
 
123
   SNIFFED_DATA sniff_data_to_ettercap;
 
124
   struct in_addr dest;
 
125
 
 
126
   memset(&data_to_ettercap, 0, sizeof(CONNECTION));
 
127
   memset(&sniff_data_to_ettercap, 0, sizeof(SNIFFED_DATA));
 
128
 
 
129
   dest.s_addr = son_data->ServerIP;
 
130
 
 
131
   strncpy(data_to_ettercap.source_ip, inet_ntoa(son_data->client_sin.sin_addr), sizeof(data_to_ettercap.source_ip)-1);
 
132
   data_to_ettercap.source_ip[sizeof(data_to_ettercap.source_ip)-1]='\0';
 
133
   strncpy(data_to_ettercap.dest_ip, inet_ntoa(dest), sizeof(data_to_ettercap.dest_ip)-1);
 
134
   data_to_ettercap.dest_ip[sizeof(data_to_ettercap.dest_ip)-1]='\0';
 
135
 
 
136
   data_to_ettercap.fast_source_ip = ntohl(son_data->client_sin.sin_addr.s_addr);
 
137
   data_to_ettercap.fast_dest_ip = ntohl(dest.s_addr);
 
138
 
 
139
   data_to_ettercap.source_port = ntohs(son_data->client_sin.sin_port);
 
140
   data_to_ettercap.dest_port = ntohs(son_data->ServerPort);
 
141
   data_to_ettercap.source_seq = 0;
 
142
   data_to_ettercap.dest_seq = 0;
 
143
   data_to_ettercap.flags = 0;
 
144
   data_to_ettercap.proto = 'T';
 
145
   data_to_ettercap.datalen = len;
 
146
   Grell_Dissector(buf, &data_to_ettercap);
 
147
 
 
148
   Decodedata_MakeConnectionList(&data_to_ettercap);
 
149
 
 
150
   if (!Connection_Mode || Options.buflen)
 
151
   {
 
152
      strncpy(sniff_data_to_ettercap.source_ip, inet_ntoa(son_data->client_sin.sin_addr), sizeof(sniff_data_to_ettercap.source_ip)-1);
 
153
      sniff_data_to_ettercap.source_ip[sizeof(sniff_data_to_ettercap.source_ip)-1]='\0';
 
154
      strncpy(sniff_data_to_ettercap.dest_ip, inet_ntoa(dest), sizeof(sniff_data_to_ettercap.dest_ip)-1);
 
155
      sniff_data_to_ettercap.dest_ip[sizeof(sniff_data_to_ettercap.dest_ip)-1]='\0';
 
156
 
 
157
      sniff_data_to_ettercap.fast_source_ip = ntohl(son_data->client_sin.sin_addr.s_addr);
 
158
      sniff_data_to_ettercap.fast_dest_ip = ntohl(dest.s_addr);
 
159
 
 
160
      sniff_data_to_ettercap.source_port = ntohs(son_data->client_sin.sin_port);
 
161
      sniff_data_to_ettercap.dest_port = ntohs(son_data->ServerPort);
 
162
      sniff_data_to_ettercap.seq = 0;
 
163
      sniff_data_to_ettercap.ack_seq = 0;
 
164
      sniff_data_to_ettercap.flags = 0;
 
165
      sniff_data_to_ettercap.proto = 'T';
 
166
      len = (len > (MAX_DATA-1)) ? (MAX_DATA-1) : len;
 
167
      sniff_data_to_ettercap.datasize = len;
 
168
      memset(&sniff_data_to_ettercap.data, 0, sizeof(sniff_data_to_ettercap.data));
 
169
      memcpy(&sniff_data_to_ettercap.data, buf, len);
 
170
 
 
171
      if (!Options.buflen && Grell_ToBeSniffed(&sniff_data_to_ettercap))
 
172
         Buffer_Put(pipe_with_illithid_data, &sniff_data_to_ettercap, sizeof(SNIFFED_DATA));
 
173
   
 
174
      if (Options.buflen)
 
175
         Decodedata_PutDataInList(&sniff_data_to_ettercap);
 
176
   }
 
177
}
 
178
 
 
179
static void server_parse(char *buf, int len, public_data *son_data)
 
180
{
 
181
   CONNECTION data_to_ettercap;
 
182
   SNIFFED_DATA sniff_data_to_ettercap;
 
183
   struct in_addr source;
 
184
 
 
185
   memset(&data_to_ettercap, 0, sizeof(CONNECTION));
 
186
   memset(&sniff_data_to_ettercap, 0, sizeof(SNIFFED_DATA));
 
187
 
 
188
   source.s_addr = son_data->ServerIP;
 
189
 
 
190
   strncpy(data_to_ettercap.source_ip, inet_ntoa(source), sizeof(data_to_ettercap.source_ip)-1);
 
191
   data_to_ettercap.source_ip[sizeof(data_to_ettercap.source_ip)-1]='\0';
 
192
   strncpy(data_to_ettercap.dest_ip, inet_ntoa(son_data->client_sin.sin_addr), sizeof(data_to_ettercap.dest_ip)-1);
 
193
   data_to_ettercap.dest_ip[sizeof(data_to_ettercap.dest_ip)-1]='\0';
 
194
 
 
195
   data_to_ettercap.fast_source_ip = ntohl(source.s_addr);
 
196
   data_to_ettercap.fast_dest_ip = ntohl(son_data->client_sin.sin_addr.s_addr);
 
197
 
 
198
   data_to_ettercap.source_port = ntohs(son_data->ServerPort);
 
199
   data_to_ettercap.dest_port = ntohs(son_data->client_sin.sin_port);
 
200
   data_to_ettercap.source_seq = 0;
 
201
   data_to_ettercap.dest_seq = 0;
 
202
   data_to_ettercap.flags = 0;
 
203
   data_to_ettercap.proto = 'T';
 
204
   data_to_ettercap.datalen = len;
 
205
 
 
206
   Decodedata_MakeConnectionList(&data_to_ettercap);
 
207
 
 
208
   if (!Connection_Mode || Options.buflen)
 
209
   {
 
210
      strncpy(sniff_data_to_ettercap.source_ip, inet_ntoa(source), sizeof(sniff_data_to_ettercap.source_ip)-1);
 
211
      sniff_data_to_ettercap.source_ip[sizeof(sniff_data_to_ettercap.source_ip)-1]='\0';
 
212
      strncpy(sniff_data_to_ettercap.dest_ip, inet_ntoa(son_data->client_sin.sin_addr), sizeof(sniff_data_to_ettercap.dest_ip)-1);
 
213
      sniff_data_to_ettercap.dest_ip[sizeof(sniff_data_to_ettercap.dest_ip)-1]='\0';
 
214
 
 
215
      sniff_data_to_ettercap.fast_source_ip = ntohl(source.s_addr);
 
216
      sniff_data_to_ettercap.fast_dest_ip = ntohl(son_data->client_sin.sin_addr.s_addr);
 
217
 
 
218
      sniff_data_to_ettercap.source_port = ntohs(son_data->ServerPort);
 
219
      sniff_data_to_ettercap.dest_port = ntohs(son_data->client_sin.sin_port);
 
220
      sniff_data_to_ettercap.seq = 0;
 
221
      sniff_data_to_ettercap.ack_seq = 0;
 
222
      sniff_data_to_ettercap.flags = 0;
 
223
      sniff_data_to_ettercap.proto = 'T';
 
224
      len = (len > (MAX_DATA-1)) ? (MAX_DATA-1) : len;
 
225
      sniff_data_to_ettercap.datasize = len;
 
226
      memset(&sniff_data_to_ettercap.data, 0, sizeof(sniff_data_to_ettercap.data));
 
227
      memcpy(&sniff_data_to_ettercap.data, buf, len);
 
228
 
 
229
      if (!Options.buflen && Grell_ToBeSniffed(&sniff_data_to_ettercap))
 
230
         Buffer_Put(pipe_with_illithid_data, &sniff_data_to_ettercap, sizeof(SNIFFED_DATA));
 
231
   
 
232
      if (Options.buflen)
 
233
         Decodedata_PutDataInList(&sniff_data_to_ettercap);
 
234
   }
 
235
}
 
236
 
 
237
 
 
238
static int client_read(char *buf, size_t size, public_data *son_data)
 
239
{
 
240
   if (son_data->ssl_type) return (SSL_read(son_data->ssl_client, buf, size));
 
241
   return (read(son_data->client_fd, buf, size));
 
242
}
 
243
 
 
244
static int client_write(char *buf, size_t size, public_data *son_data)
 
245
{
 
246
   if (son_data->ssl_type) return (SSL_write(son_data->ssl_client, buf, size));
 
247
   return (write(son_data->client_fd, buf, size));
 
248
}
 
249
 
 
250
static void client_init(public_data *son_data)
 
251
{
 
252
   //fcntl(son_data->client_fd, F_SETFL, 0);
 
253
 
 
254
   if (son_data->ssl_type)
 
255
   {
 
256
      son_data->ssl_client = SSL_new(son_data->ssl_ctx_client);
 
257
      SSL_set_fd(son_data->ssl_client, son_data->client_fd);
 
258
      SSL_accept(son_data->ssl_client);
 
259
   }
 
260
}
 
261
 
 
262
static void client_close(public_data *son_data)
 
263
{
 
264
   if (son_data->ssl_type) SSL_free(son_data->ssl_client);
 
265
   close(son_data->client_fd);
 
266
}
 
267
 
 
268
static int server_read(char *buf, size_t size, public_data *son_data)
 
269
{
 
270
   if (son_data->ssl_type) return (SSL_read(son_data->ssl_server, buf, size));
 
271
   return (read(son_data->server_fd, buf, size));
 
272
}
 
273
 
 
274
static int server_write(char *buf, size_t size, public_data *son_data)
 
275
{
 
276
   if (son_data->ssl_type) return (SSL_write(son_data->ssl_server, buf, size));
 
277
   return (write(son_data->server_fd, buf, size));
 
278
}
 
279
 
 
280
static void server_close(public_data *son_data)
 
281
{
 
282
   if (son_data->ssl_type) SSL_free(son_data->ssl_server);
 
283
   close(son_data->server_fd);
 
284
}
 
285
 
 
286
static int server_init(char *buf, size_t size, public_data *son_data)
 
287
{
 
288
   char vhost[501], *i=0;
 
289
   size_t offset = 0, temp;
 
290
   int type_connect=0, addr, j;
 
291
   struct hostent *toresolv=(struct hostent *)1;
 
292
 
 
293
   do
 
294
   {
 
295
      temp = client_read(buf + offset, size - offset, son_data);
 
296
      offset += temp;
 
297
 
 
298
      if (Grell_ProxyIP && !strncasecmp(buf,"CONNECT",7) && !son_data->ssl_type)
 
299
      {
 
300
         type_connect = 1;
 
301
         break;
 
302
      }
 
303
 
 
304
       if (temp<=0) offset=size+1;
 
305
       usleep(5000);
 
306
   } while(size>offset && (i=(char *)memmem(buf, size, "\r\nHost: ",8))==NULL);
 
307
 
 
308
   fcntl(son_data->client_fd, F_SETFL, 0);
 
309
   
 
310
   if (offset>=size) // No virtual host
 
311
   {
 
312
       client_close(son_data);
 
313
       return -1;
 
314
   }
 
315
 
 
316
   memset(&son_data->server_sin, 0, sizeof(son_data->server_sin));
 
317
   son_data->server_sin.sin_family = AF_INET;
 
318
 
 
319
   if (!Grell_ProxyIP || son_data->ssl_type)
 
320
   {
 
321
      memcpy(vhost, i+8, sizeof(vhost));
 
322
      vhost[500] = 0;
 
323
      strtok(vhost, "\r");
 
324
      strtok(vhost, ":");
 
325
      son_data->server_sin.sin_port = son_data->ssl_type ? htons(443) : htons(80);
 
326
 
 
327
      addr = inet_addr(vhost);
 
328
      if (addr == INADDR_NONE)
 
329
      {
 
330
         toresolv = gethostbyname(vhost);
 
331
         if (toresolv) addr = *(unsigned long *)toresolv->h_addr;
 
332
      }
 
333
 
 
334
      if (!toresolv || addr==ntohl(INADDR_LOOPBACK))
 
335
      {
 
336
         client_close(son_data);
 
337
         return -1;
 
338
      }
 
339
   }
 
340
   else
 
341
   {
 
342
       addr = Grell_ProxyIP;
 
343
       son_data->server_sin.sin_port=htons((short)Grell_ProxyPort);
 
344
   }
 
345
 
 
346
   son_data->server_sin.sin_addr.s_addr = addr;
 
347
   son_data->ServerIP = addr;
 
348
   son_data->ServerPort = son_data->server_sin.sin_port;
 
349
 
 
350
   son_data->server_fd = socket(AF_INET, SOCK_STREAM, 0);
 
351
   connect(son_data->server_fd, (struct sockaddr *)&son_data->server_sin, sizeof(son_data->server_sin));
 
352
 
 
353
   if (type_connect)
 
354
   {
 
355
      //fd_set fds;
 
356
      struct pollfd poll_fd[2];
 
357
 
 
358
      server_write(buf,offset, son_data);  // Skip readable messages
 
359
 
 
360
      LOOP {
 
361
         //FD_ZERO(&fds);
 
362
         //FD_SET(son_data->client_fd, &fds);
 
363
         //FD_SET(son_data->server_fd, &fds);
 
364
         poll_fd[0].fd = son_data->client_fd;
 
365
         poll_fd[1].fd = son_data->server_fd;
 
366
         poll_fd[0].events = poll_fd[1].events = POLLIN;
 
367
 
 
368
         //select(FOPEN_MAX, &fds, 0, 0, 0);
 
369
         poll(poll_fd,2,-1);
 
370
 
 
371
         //if (FD_ISSET(son_data->client_fd, &fds))
 
372
         if (poll_fd[0].revents & POLLIN)
 
373
         {
 
374
            if ((j = client_read(buf, MAX_DATA, son_data)) <= 0)
 
375
            {
 
376
               server_close(son_data);
 
377
               client_close(son_data);
 
378
               return -1;    // if it can't handle proxy auth...
 
379
            }
 
380
            buf[j]=0;
 
381
            if (server_write(buf, j, son_data) != j)
 
382
            {
 
383
               server_close(son_data);
 
384
               client_close(son_data);
 
385
               return -1;    // if it can't handle proxy auth...
 
386
            }
 
387
         }
 
388
         else //if (FD_ISSET(son_data->server_fd, &fds))
 
389
         if (poll_fd[1].revents & POLLIN)
 
390
         {
 
391
            char *found;
 
392
            if ((j = server_read(buf, MAX_DATA, son_data)) <= 0)
 
393
            {
 
394
               server_close(son_data);
 
395
               client_close(son_data);
 
396
               return -1;    // if it can't handle proxy auth...
 
397
            }
 
398
            buf[j]=0;
 
399
            if (client_write(buf, j, son_data) != j)
 
400
            {
 
401
               server_close(son_data);
 
402
               client_close(son_data);
 
403
               return -1;    // if it can't handle proxy auth...
 
404
            }
 
405
            found = strstr(buf, "200");
 
406
            if ( found && (unsigned int)(found-buf) < 20 ) 
 
407
            {
 
408
                while (!strstr(buf, "\r\n\r\n") && (buf[0]!='\r' || buf[1]!='\n')) 
 
409
                {
 
410
                    if ( (j=server_read(buf, MAX_DATA, son_data)) <=0) 
 
411
                    {
 
412
                        usleep(3000);
 
413
                        continue;
 
414
                    }
 
415
                    buf[j]=0;
 
416
                    if (client_write(buf, j, son_data) != j)
 
417
                    {
 
418
                       server_close(son_data);
 
419
                       client_close(son_data);
 
420
                       return -1;    // if it can't handle proxy auth...
 
421
                    }
 
422
                }
 
423
                break;  
 
424
            }
 
425
         }
 
426
      }
 
427
 
 
428
      son_data->ssl_type=1;       // Turn on encryption
 
429
      client_init(son_data);
 
430
      offset = client_read(buf,MAX_DATA,son_data);
 
431
 
 
432
      if (offset == -1)
 
433
      {
 
434
          server_close(son_data);
 
435
          client_close(son_data);
 
436
          return -1;    // if it can't handle proxy auth...
 
437
      }
 
438
      buf[offset]=0; // Only for paranoid tests
 
439
   }
 
440
 
 
441
   if (son_data->ssl_type)
 
442
   {
 
443
      son_data->ssl_ctx_server = SSL_CTX_new(SSLv3_client_method());
 
444
      son_data->ssl_server = SSL_new(son_data->ssl_ctx_server);
 
445
      SSL_set_connect_state(son_data->ssl_server);
 
446
      SSL_set_fd(son_data->ssl_server, son_data->server_fd);
 
447
 
 
448
      if (SSL_connect(son_data->ssl_server)!=1)
 
449
      {
 
450
         son_data->ssl_ctx_server = SSL_CTX_new(SSLv2_client_method());
 
451
         son_data->ssl_server = SSL_new(son_data->ssl_ctx_server);
 
452
         SSL_set_connect_state(son_data->ssl_server);
 
453
         SSL_set_fd(son_data->ssl_server, son_data->server_fd);
 
454
         if (SSL_connect(son_data->ssl_server)!=1)
 
455
         {
 
456
            son_data->ssl_ctx_server = SSL_CTX_new(TLSv1_client_method());
 
457
            son_data->ssl_server = SSL_new(son_data->ssl_ctx_server);
 
458
            SSL_set_connect_state(son_data->ssl_server);
 
459
            SSL_set_fd(son_data->ssl_server, son_data->server_fd);
 
460
            if (SSL_connect(son_data->ssl_server)!=1)
 
461
            {
 
462
               server_close(son_data);
 
463
               client_close(son_data);
 
464
               return -1;
 
465
            }
 
466
         }
 
467
      }
 
468
   }
 
469
 
 
470
   return(offset);
 
471
}
 
472
 
 
473
 
 
474
void Grell_fini(void *dummy)
 
475
{
 
476
   close(proxy_fd);
 
477
   close(https_fd);
 
478
 
 
479
   DEBUG_MSG("Grell_fini -- grell closed gracefully...");
 
480
}
 
481
 
 
482
 
 
483
void Grell_init(void)
 
484
{
 
485
   struct sockaddr_in sa_in;
 
486
   int i=1;
 
487
 
 
488
   proxy_fd = socket(AF_INET, SOCK_STREAM, 0);
 
489
   https_fd = socket(AF_INET, SOCK_STREAM, 0);
 
490
 
 
491
 
 
492
   setsockopt(proxy_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
493
   setsockopt(https_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
494
 
 
495
   memset(&sa_in, 0, sizeof(sa_in));
 
496
   sa_in.sin_family = AF_INET;
 
497
   sa_in.sin_addr.s_addr = INADDR_ANY;
 
498
 
 
499
   sa_in.sin_port = htons(Proxy_Local_Port);
 
500
 
 
501
   if (bind(proxy_fd, (struct sockaddr *)&sa_in, sizeof(sa_in)) < 0)
 
502
       Error_msg("Can't bind port %d (required for HTTPS dissection)\n", Proxy_Local_Port);
 
503
 
 
504
   sa_in.sin_port = htons(HTTPS_Local_Port);
 
505
 
 
506
   if (bind(https_fd, (struct sockaddr *)&sa_in, sizeof(sa_in)) < 0)
 
507
      Error_msg("Can't bind port %d (required for HTTPS dissection)\n", HTTPS_Local_Port);
 
508
 
 
509
   DEBUG_MSG("Grell_init -- Listening for Proxy redirect on port %d", Proxy_Local_Port);
 
510
   DEBUG_MSG("Grell_init -- Listening for HTTPS redirect on port %d", HTTPS_Local_Port);
 
511
 
 
512
   listen(proxy_fd, 50);
 
513
   listen(https_fd, 50);
 
514
 
 
515
   SSL_library_init();
 
516
 
 
517
   father_data.ssl_ctx_client = SSL_CTX_new(SSLv23_server_method());
 
518
 
 
519
   if (SSL_CTX_use_certificate_file(father_data.ssl_ctx_client, CERT_FILE, SSL_FILETYPE_PEM) == 0)
 
520
   {
 
521
      DEBUG_MSG("Grell_init -- SSL_CTX_use_certificate_file -- %s", DATA_PATH "/" CERT_FILE);
 
522
 
 
523
      if (SSL_CTX_use_certificate_file(father_data.ssl_ctx_client, DATA_PATH "/" CERT_FILE, SSL_FILETYPE_PEM) == 0)
 
524
         Error_msg("Can't open \"%s\" file !!", CERT_FILE);
 
525
   }
 
526
 
 
527
   if (SSL_CTX_use_PrivateKey_file(father_data.ssl_ctx_client, CERT_FILE, SSL_FILETYPE_PEM) == 0)
 
528
   {
 
529
      DEBUG_MSG("Grell_init -- SSL_CTX_use_PrivateKey_file -- %s", DATA_PATH "/" CERT_FILE);
 
530
 
 
531
      if (SSL_CTX_use_PrivateKey_file(father_data.ssl_ctx_client, DATA_PATH "/" CERT_FILE, SSL_FILETYPE_PEM) == 0)
 
532
         Error_msg("Can't open \"%s\" file !!", CERT_FILE);
 
533
   }
 
534
 
 
535
   if (SSL_CTX_check_private_key(father_data.ssl_ctx_client) == 0)
 
536
      Error_msg("Bad SSL Key couple !!");
 
537
 
 
538
}
 
539
 
 
540
void * Grell_spawn(void *local_father_data)
 
541
{
 
542
   u_char buf[MAX_DATA*5];
 
543
   //fd_set fds;
 
544
   struct pollfd poll_fd[2];
 
545
 
 
546
   int i;
 
547
   public_data son_data;
 
548
 
 
549
   DEBUG_MSG("Grell_spawn -- new connection accepted");
 
550
 
 
551
   memset(buf, 0, sizeof(buf));
 
552
   memcpy(&son_data, local_father_data, sizeof(public_data));
 
553
 
 
554
   pthread_mutex_unlock(&father_mutex);
 
555
 
 
556
   client_init(&son_data);
 
557
   i = server_init(buf, sizeof(buf)-2, &son_data);
 
558
 
 
559
   if (i == -1) return NULL;
 
560
 
 
561
   server_write(buf, i, &son_data);
 
562
   client_parse(buf, i, &son_data);
 
563
 
 
564
   LOOP {
 
565
      //FD_ZERO(&fds);
 
566
      //FD_SET(son_data.client_fd, &fds);
 
567
      //FD_SET(son_data.server_fd, &fds);
 
568
 
 
569
 
 
570
      poll_fd[0].fd = son_data.client_fd;
 
571
      poll_fd[1].fd = son_data.server_fd;
 
572
      poll_fd[0].events = poll_fd[1].events = POLLIN;
 
573
 
 
574
      //select(FOPEN_MAX, &fds, 0, 0, 0);
 
575
      poll(poll_fd,2,-1);
 
576
 
 
577
      //if (FD_ISSET(son_data.client_fd, &fds))
 
578
      if (poll_fd[0].revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL))
 
579
      {
 
580
         i = sizeof(buf);
 
581
         if ((i = client_read(buf, i-2, &son_data)) <= 0) break;
 
582
         buf[i]=0;
 
583
         if (server_write(buf, i, &son_data) != i)   break;
 
584
         client_parse(buf, i, &son_data);
 
585
         if (poll_fd[0].revents & (POLLHUP | POLLERR | POLLNVAL)) break;
 
586
      }
 
587
      else //if (FD_ISSET(son_data.server_fd, &fds))
 
588
      if (poll_fd[1].revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL))
 
589
      {
 
590
         i = sizeof(buf);
 
591
         if ((i = server_read(buf, i-2, &son_data)) <= 0) break;
 
592
         buf[i]=0;
 
593
         if (client_write(buf, i, &son_data) != i) break;
 
594
         server_parse(buf, i, &son_data);
 
595
         if (poll_fd[1].revents & (POLLHUP | POLLERR | POLLNVAL)) break;
 
596
      }
 
597
   }
 
598
   server_close(&son_data);
 
599
   client_close(&son_data);
 
600
 
 
601
   return NULL;
 
602
}
 
603
 
 
604
// ssl_type = 0 Proxy
 
605
// ssl_type = 1 HTTPS
 
606
 
 
607
void * Grell_start(void *none)
 
608
{
 
609
   //fd_set fds;
 
610
   struct pollfd poll_fd[2];
 
611
 
 
612
   int dummy;
 
613
 
 
614
   Grell_init();
 
615
 
 
616
   fcntl(proxy_fd, F_SETFL, O_NONBLOCK);
 
617
   fcntl(https_fd, F_SETFL, O_NONBLOCK);
 
618
 
 
619
   exit_func(Grell_fini);
 
620
 
 
621
   LOOP {
 
622
      //FD_ZERO(&fds);
 
623
      //FD_SET(proxy_fd, &fds);
 
624
      //FD_SET(https_fd, &fds);
 
625
 
 
626
      poll_fd[0].fd = proxy_fd;
 
627
      poll_fd[1].fd = https_fd;
 
628
      poll_fd[0].events = poll_fd[1].events = POLLIN;
 
629
 
 
630
 
 
631
      pthread_testcancel();
 
632
 
 
633
      //select(FOPEN_MAX, &fds, 0, 0, (struct timeval *)0);
 
634
      poll(poll_fd,2,-1);
 
635
 
 
636
      pthread_testcancel();
 
637
 
 
638
      dummy = sizeof(struct sockaddr);
 
639
 
 
640
      //if (FD_ISSET(proxy_fd, &fds))
 
641
      if (poll_fd[0].revents & POLLIN)
 
642
      {
 
643
         u_long peer;
 
644
 
 
645
         DEBUG_MSG("Grell_start -- got a connection on proxy_fd");
 
646
 
 
647
         pthread_mutex_lock(&father_mutex);
 
648
 
 
649
         father_data.client_fd = accept(proxy_fd, (struct sockaddr *)&father_data.client_sin, &dummy);
 
650
         father_data.ssl_type = 0;
 
651
 
 
652
         memcpy(&peer, &father_data.client_sin.sin_addr, sizeof(u_long));
 
653
         peer &= htonl(0x00FFFFFF);
 
654
         peer |= inet_addr(Host_In_LAN[0].ip) & htonl(0xFF000000);
 
655
         memcpy(&father_data.client_sin.sin_addr, &peer, sizeof(u_long));
 
656
         ECThread_create("grell_son", &Grell_spawn, &father_data);
 
657
      }
 
658
      else //if (FD_ISSET(https_fd, &fds))
 
659
      if (poll_fd[1].revents & POLLIN)
 
660
      {
 
661
         u_long peer;
 
662
 
 
663
         DEBUG_MSG("Grell_start -- got a connection on https_fd");
 
664
 
 
665
         pthread_mutex_lock(&father_mutex);
 
666
 
 
667
         father_data.client_fd = accept(https_fd, (struct sockaddr *)&father_data.client_sin, &dummy);
 
668
         father_data.ssl_type = 1;
 
669
 
 
670
         memcpy(&peer, &father_data.client_sin.sin_addr, sizeof(u_long));
 
671
         peer &= htonl(0x00FFFFFF);
 
672
         peer |= inet_addr(Host_In_LAN[0].ip) & htonl(0xFF000000);
 
673
         memcpy(&father_data.client_sin.sin_addr, &peer, sizeof(u_long));
 
674
         ECThread_create("grell_son", &Grell_spawn, &father_data);
 
675
      }
 
676
   }
 
677
 
 
678
   exit_func_end();
 
679
 
 
680
   return NULL;
 
681
}
 
682
 
 
683
pthread_t Grell_Run(void)
 
684
{
 
685
   extern DISSECTOR Available_Dissectors[];
 
686
   DISSECTOR *ds;
 
687
 
 
688
   for( ds = Available_Dissectors; ds->port != 0; ds++)
 
689
   {
 
690
      if (!strcasecmp("HTTPS", ds->name))
 
691
      {
 
692
         if (ds->active == 0)
 
693
         {
 
694
            DEBUG_MSG("Grell was disabled by conf.file");
 
695
 
 
696
            return 0;
 
697
         }
 
698
      }
 
699
   }
 
700
 
 
701
   DEBUG_MSG("Grell Starts");
 
702
 
 
703
   return ECThread_create("grell", &Grell_start, NULL);
 
704
}
 
705
 
 
706
#endif //HAVE_OPENSSL
 
707
 
 
708
/* EOF */
 
709
 
 
710
// vim:ts=3:expandtab
 
711