-
Notifications
You must be signed in to change notification settings - Fork 0
/
splashd.cc
executable file
·1243 lines (932 loc) · 36.1 KB
/
splashd.cc
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "splashd.h"
gboolean show_socket_pairs(gchar* function_name, http_request *h){
gint fd, r;
struct sockaddr_in local_addr, remote_addr;
unsigned int n = sizeof(struct sockaddr_in);
gchar localaddr_ip[16], remoteaddr_ip[16];
const gchar *r2;
unsigned short int local_port, remote_port;
fd = g_io_channel_unix_get_fd(h->channel);
r = getsockname (fd, (struct sockaddr *)&local_addr, &n );
if (r == -1) { g_debug( "%s: getsockname failed: %m",function_name ); }
local_port = local_addr.sin_port;
r2 = (gchar*)inet_ntop( AF_INET, &local_addr.sin_addr, localaddr_ip, INET_ADDRSTRLEN );
//g_assert( r2 != NULL );
n = sizeof(struct sockaddr_in);
r = getpeername (fd, (struct sockaddr *)&remote_addr, &n );
if (r == -1) { g_debug( "%s: getpeername failed: %m",function_name );}
remote_port = remote_addr.sin_port;
r2 = (gchar*)inet_ntop( AF_INET, &remote_addr.sin_addr, remoteaddr_ip, INET_ADDRSTRLEN );
//g_assert( r2 != NULL );
g_debug( "%s: fd = %d -- remote address = %s:%d -- local address = %s:%d",
function_name , fd, r2, remote_port, localaddr_ip, local_port);
return TRUE;
}
/************ Check peer timeouts **************/
gboolean check_peers( void *dummy ) {
time_t now = time(NULL);
//g_message("Checking peers for expiration");
g_hash_table_foreach_remove( peer_tab, (GHRFunc)check_peer_expire, &now );
//g_hash_table_foreach( peer_tab, (GHFunc)check_peer_expire, &now );
return TRUE;
}
gboolean change_table( void *dummy ) {
if ( strcmp(table,"0") == 0) strcpy(table,"1");
else strcpy(table,"0");
fw_resettable (nocat_conf);
return TRUE;
}
/******************************************************************************************/
/******************************************************************************************/
/********************************* Connection handlers ************************************/
/**********************************************/
/**********************************************/
/************* Http connection handlers *******/
/************* Http Read Input Data Connection handle *******/
gboolean handle_read( GIOChannel *channel, GIOCondition cond, http_request *h ) {
/*
* Este handle es el que se ocupa de atender la petici'on http que lleg'o al servidor en el socket que se deriva
* para eso en el connection handler del puerto 5280 (handle_accept).
*/
g_debug("handle_read: reading request fd = %d",g_io_channel_unix_get_fd (h->channel));
guint r;
r= http_request_read(h);
if (r == 1){
http_request_ok(h);
if (handle_request(h) == 0) {
g_debug("handle_read: leaving without shutting down request fd = %d",g_io_channel_unix_get_fd (h->channel));
return TRUE;
}
}
else if (r == 0) {
g_debug("handle_read: leaving without shutting down request fd = %d",g_io_channel_unix_get_fd (h->channel));
return TRUE;
}
g_debug("handle_read: shutting down request fd = %d",g_io_channel_unix_get_fd (h->channel));
g_io_channel_shutdown(h->channel,TRUE,NULL);
g_io_channel_unref(h->channel );
http_request_free(h);
return FALSE;
}
/************* Http Accept Connection handles *******/
gboolean handle_accept( GIOChannel* channel, GIOCondition cond, void* dummy ) {
/*
* Este es el handle principal para las conexiones que se establecen en el puerto 5280. Aqu'i se crea la estructura
* http_request que representa a la conexi'on del cliente y se acepta la conexi'on, estableciendose esta entre el
* socket original remoto del usuario y un socket nuevo que se guarda en h->outside_fd. Se establece un watch en el
* pool principal del programa para recibir los datos desde el usuario (handle_read). A este watch se le entra con
* una referencia a la estructura http_request de la conexi'on.
*/
GIOChannel* conn;
http_request* req; /* defined in http.h */
int fd,n;
pid_t mypid;
g_debug ("handle_accept: entering on socket # = %d", g_io_channel_unix_get_fd (channel));
fd = accept( g_io_channel_unix_get_fd(channel), NULL, NULL );
n = fcntl( fd, F_GETFL, 0 );
fcntl( fd, F_SETFL, n | O_NONBLOCK);
mypid = getpid();
fcntl( fd, F_SETOWN, mypid);
conn = g_io_channel_unix_new(fd);
//g_io_channel_set_encoding(conn,NULL,&gerror);
g_io_channel_set_close_on_unref(conn,TRUE);
g_io_channel_set_buffer_size(conn,4096);
req = http_request_new(conn,fd);
if (req != NULL){
//show_socket_pairs((char*)"handle_accept", req);
req->source_id = g_io_add_watch(req->channel, G_IO_IN,(GIOFunc)handle_read, req);
}
else {
g_io_channel_shutdown(conn,FALSE,NULL);
g_io_channel_unref(conn);
}
//g_debug ("handle_accept: leaving..");
return TRUE;
}
gboolean handle_accept6( GIOChannel* channel, GIOCondition cond, void* dummy ) {
GIOChannel* conn;
http_request* req; /* defined in http.h */
int fd,n;
pid_t mypid;
fd = accept( g_io_channel_unix_get_fd(channel), NULL, NULL );
/* The line below need to be substituted by other error checking method that don't break daemon execution*/
//g_assert( fd != -1 );
n = fcntl( fd, F_GETFL, 0 );
//if (n == -1) g_error("fcntl F_GETFL on %s: %m", ip );
//g_message("n = %d",n);
//g_message("O_NONBLOCK modified = %d",n & O_NONBLOCK);
fcntl( fd, F_SETFL, n | O_NONBLOCK);
//if (r == -1) g_error("fcntl F_SETFL O_NDELAY on %s: %m", ip );
mypid = getpid();
fcntl( fd, F_SETOWN, mypid);
conn = g_io_channel_unix_new( fd );
//g_io_channel_set_encoding(conn,NULL,&gerror);
//if (gerror != NULL) g_message(gerror->message);
g_io_channel_set_close_on_unref(conn,TRUE);
req = http_request_new6( conn, fd );
//req = requests->add6(conn);
//show_socket_pairs((char*)"handle_accept", req);
if (req != NULL) g_io_add_watch( conn, G_IO_IN, (GIOFunc) handle_read, req );
else {
g_io_channel_shutdown(conn,FALSE,NULL);
g_io_channel_unref( conn );
return FALSE;
}
return TRUE;
}
/**********************************************/
/**********************************************/
/************* Https connection handlers ******/
/************* Https Read Input Data Connection handles *******/
gboolean handle_client_ssl( GIOChannel *channel, GIOCondition cond,ssl_connection* s) {
if ((cond & G_IO_ERR) == G_IO_ERR){
g_debug("handle_client_ssl: s = %u: G_IO_ERR",s->secuence_number);
s->ref_count--;
g_debug("handle_client_ssl: s = %u, ref_count = %u, exitting due to an error",s->secuence_number,s->ref_count);
return FALSE;
}
if ((cond & G_IO_HUP) == G_IO_HUP){
g_debug("handle_client_ssl: s = %u: G_IO_HUP",s->secuence_number);
s->ref_count--;
g_debug("handle_client_ssl: s = %u, ref_count = %u, exitting due to an error",s->secuence_number,s->ref_count);
return FALSE;
}
if ((cond & G_IO_IN) == G_IO_IN){
if (s->Channels_array[0] != NULL ) { // Si el canal no est'a marcado para destrucci'on.
gsize channel_size;
gchar *buf;
GIOStatus r;
guint n;
gint m;
GError* gerror = NULL;
channel_size = g_io_channel_get_buffer_size(channel);
buf = g_new0( gchar, channel_size + 2 );
r = g_io_channel_read_chars(channel, buf, channel_size, &n,&gerror);
if ((gerror != NULL) || (n == 0)) {
if (gerror != NULL) g_debug("handle_client_ssl: s = %u: g_io_channel_read_chars return error: %s"\
,s->secuence_number,gerror->message);
else g_debug("handle_client_ssl: s = %u: g_io_channel_read_chars connection closed by the peer"\
,s->secuence_number);
g_free(buf);
g_io_channel_shutdown(channel,FALSE,NULL);
g_io_channel_unref(channel);
s->Channels_array[0] = NULL;
// Also we must mark every channel to be destroyed
s->Channels_array[1] = NULL;
s->Channels_array[2] = NULL;
s->ref_count--;
g_debug("handle_client_ssl: s = %u, ref_count = %u, exitting",s->secuence_number,s->ref_count);
// s->arreglo_source_ids[0] = 0;
// The next comment is not completely true.
// Here is it's only necessary to return FALSE, because this will order Glib to invoke the GDestroyNotify function, in which everything
// will be destroyed, except the event source of this very GIOChannel because that one is automatically destroyed when we return FALSE.
// That's why it's necessary to cero the varible arreglo_source_ids[0] in the former instruction. The GIOChannel could has been
// left alone, in order to shut it down in the GDestroyNotifiy function, but it's safer to do so here, marking it as shut NULLING
// the s->Channels_array[0] variable.
// This is a fatal error, so we have to mark the ssl_connection for destroy in the GDestroyNotify function
s->mark_destroy = TRUE;
return FALSE;
}
/* Para no volver a cometer el mismo error de interpretación que me tomó varias semanas inventando semáforos, etc, aclaro esto aquí y en español,
// para entenderlo bien clarito: Es imposible que si se está realizando el proceso de chequeo del SNI (capture_status < 2), esta función en la
// que estamos ahora interrumpa para agregar alguna información al buffer. Esto es así pues la secuencia de acciones del chequeo del SNI es
// una secuencia lineal que se comienza desde esta misma función, y mientras no se retorne de esta función el GIOChannel no puede generar un
// evento nuevo de información recibida en el canal.
// Now the previous note but for the people that do not understand spanish: It's imposible that in the middle of the SNI check process
// (capture_status < 2) this function in which we are now interrupt to add information to the buffer. This is so because the secuence of actions
// that take place during the SNI check process is a linear one, that start from this very same function, and until this function has not returned
// the GIOChannel can not generate a new event informing that there is information to receive from the phisical channel.
*/
if (s->capture_status < 2) { // buscando el SNI
llenar_first_buffer(s,buf,n);
m = checkSNI(s);
if (m == -1) {
g_free(buf);
g_io_channel_shutdown(channel,FALSE,NULL);
g_io_channel_unref(channel);
s->Channels_array[0] = NULL;
// Also we must mark every channel to be destroyed
s->Channels_array[1] = NULL;
s->Channels_array[2] = NULL;
s->ref_count--;
g_debug("handle_client_ssl: s = %u, ref_count = %u, exitting",s->secuence_number,s->ref_count);
//s->arreglo_source_ids[0] = 0;
// This is a fatal error, so we have to mark the ssl_connection for destroy in the GDestroyNotify function
s->mark_destroy = TRUE;
return FALSE;
}
}
else {
// The SNI was founded (or not), any way from this moment on anything that enters this channel is included in the queue. How, and when the
// packages of the queue are sended to the internal openssl channel is not of the concern of this function.
struct ssl_buffer* my_buffer = g_new0(struct ssl_buffer,1);
my_buffer->tamanno = n;
my_buffer->buffer = g_strdup(buf);
my_buffer->next = NULL;
my_buffer->previous = NULL;
my_buffer->write_tries = 0;
insert_in_queue(s->ssl_queue,my_buffer);
}
g_free(buf);
return TRUE;
}
else {
s->ref_count--;
g_debug("handle_client_ssl: s = %u, ref_count = %u, exitting",s->secuence_number,s->ref_count);
return FALSE;
}
}
}
gboolean handle_read_openssl( GIOChannel *channel, GIOCondition cond, void* dummy ) {
g_debug("handle_read_openssl: entering");
/*gsize channel_size;
gchar *buf;
GIOStatus r;
guint n,m;
GError* gerror = NULL;
channel_size = g_io_channel_get_buffer_size(channel);
buf = g_new0( gchar, channel_size + 2 );
r = g_io_channel_read_chars(channel, buf, channel_size, &n,&gerror);
if (gerror != NULL) {
g_message("handle_read_openssl: g_io_channel_read_chars return error: %s",gerror->message);
g_free(buf);
return FALSE;
}
m = n;
g_debug("handle_read_openssl: caracteres leidos: %u", n);
g_debug("handle_read_openssl: %s", buf);
g_io_channel_write_chars(channel, buf,m,(guint*)&n,NULL);
g_io_channel_flush(channel,NULL);
g_free(buf);*/
g_debug("handle_read_openssl: exiting");
return TRUE;
}
/************* Https Accept Connection handles *******/
gboolean handle_accept_ssl( GIOChannel* channel, GIOCondition cond, void* dummy ) {
/*
* Este es el handle principal para las conexiones que se establecen en el puerto 5281 (ssl). Aqu'i se crea
* la estructura ssl_connection que representa a la conexi'on del cliente y se acepta la conexi'on,
* estableciéndose ésta entre el socket original remoto del usuario y un socket nuevo que se guarda
* en s->outside_fd.
* Se establece un watch en el pool principal del programa para recibir los datos desde el usuario (handle_client_ssl).
* A este watch se le entra con una referencia a la estructura ssl_connection de la conexi'on.
*/
GIOChannel* client_channel;
ssl_connection* s;
int fd,n;
pid_t mypid;
GError* gerror = NULL;
//g_debug ("handle_accept_ssl: entering on socket # = %d", g_io_channel_unix_get_fd (channel));
fd = accept( g_io_channel_unix_get_fd(channel), NULL, NULL );
n = fcntl( fd, F_GETFL, 0 );
fcntl( fd, F_SETFL, n | O_NONBLOCK);
mypid = getpid();
fcntl( fd, F_SETOWN, mypid);
client_channel = g_io_channel_unix_new(fd);
g_io_channel_set_encoding(client_channel,NULL,&gerror);
g_io_channel_set_close_on_unref(client_channel,TRUE);
g_io_channel_set_buffer_size(client_channel,0);
s = ssl_connection_new(client_channel,ssl_connections_secuence_numbers + 1); // s[0xYYY] is a local variable that is asigned the value
// returned from ssl_connection new (0x001) that is the address
// where stand the ssl_connection.
if (s != NULL){
g_debug("handle_accept_ssl: s = %u, capturada conexión desde el host %s:%d al host %s",\
s->secuence_number,s->peer_ip,s->peer_port,s->remote_ip);
ssl_connections_secuence_numbers++;
s->arreglo_source_ids[0] = g_io_add_watch_full(client_channel,G_PRIORITY_DEFAULT,(GIOCondition)(G_IO_IN|G_IO_ERR|G_IO_HUP),\
(GIOFunc)handle_client_ssl,s,(GDestroyNotify)destroy_ssl_conn);
s->ref_count++;
g_debug("handle_accept_ssl: s = %u, ref_count = %u, adding handle_client_ssl",s->secuence_number,s->ref_count);
s->arreglo_source_ids[5] = g_timeout_add_full(G_PRIORITY_DEFAULT,50,(GSourceFunc)timer_output_client_channel,\
s,(GDestroyNotify)destroy_ssl_conn);
s->ref_count++;
g_debug("handle_accept_ssl: s = %u, ref_count = %u, adding timer_output_client_channel",s->secuence_number,s->ref_count);
}
else {
g_io_channel_shutdown(client_channel,FALSE,NULL);
g_io_channel_unref(client_channel);
}
return TRUE;
}
gboolean handle_accept_openssl( GIOChannel* channel, GIOCondition cond, void* dummy ) {
GIOChannel* conn;
int fd,n;
pid_t mypid;
GError* gerror = NULL;
g_debug ("handle_accept_openssl: entering on socket # = %d", g_io_channel_unix_get_fd (channel));
fd = accept( g_io_channel_unix_get_fd(channel), NULL, NULL );
n = fcntl( fd, F_GETFL, 0 );
fcntl( fd, F_SETFL, n | O_NONBLOCK);
mypid = getpid();
fcntl( fd, F_SETOWN, mypid);
conn = g_io_channel_unix_new( fd );
g_io_channel_set_encoding(conn,NULL,&gerror);
g_io_channel_set_close_on_unref(conn,TRUE);
g_io_channel_set_buffer_size(conn,0);
g_io_add_watch(conn,G_IO_IN,(GIOFunc)handle_read_openssl,NULL);
//g_debug ("handle_accept_ssl: leaving..");
return TRUE;
}
/******************************************************************************************/
/******************************************************************************************/
/******************************************************************************************/
gboolean check_exit_signal( GMainLoop *loop ) {
pid_t my_pid;
gchar* nombre;
gchar* contenido;
gsize length;
gchar *start_memory, *end_memory;
gchar *VmSize;
unsigned int memory_size;
gint64 memory_used;
my_pid = getpid();
nombre = g_new0(gchar,100);
g_sprintf(nombre,"%s%d%s","/proc/",my_pid,"/status");
g_file_get_contents(nombre,&contenido,&length,NULL);
g_free(nombre);
start_memory = g_strstr_len(contenido,-1,"VmSize:");
end_memory = g_strstr_len(contenido,-1,"VmLck:");
memory_size = end_memory - start_memory - 9;
VmSize = g_strndup(start_memory+8,memory_size);
g_strchug(VmSize);
g_strcanon(VmSize,"0123456789",'\0');
memory_used = g_ascii_strtoll(VmSize,NULL,0);
//g_debug("memoria total usada %d",memory_used);
g_free(contenido);
g_free(VmSize);
if (memory_used > CONFd("memlimit")){
g_message("check_exit_signal: memory usage exceded, exiting..");
fclose(log_fd);
g_main_quit( loop );
return TRUE;
}
if (exit_signal) {
g_message("check_exit_signal: Caught exit signal %d!", exit_signal);
if (pid_file != NULL) {
unlink( NC_PID_FILE );
fclose( pid_file );
}
g_remove("/tmp/sicat.tmp");
g_main_quit( loop );
}
return TRUE;
}
void signal_handler( int sig ) {
switch(sig) {
case SIGINT:
/*log_message(LOG_FILE,"interrupt signal caught");*/
exit_signal = sig;
break;
case SIGTERM:
/*log_message(LOG_FILE,"terminate signal caught");*/
exit_signal = sig;
break;
case SIGHUP:
/*log_message(LOG_FILE,"hangup signal caught");*/
break;
/*case SIGURG:
g_message("signal_handler: out of band data arrived");
break;*/
case SIGSEGV:
//longjmp(state,1);
g_message("signal_handler: SIGSEGV signal caught");
exit(0);
break;
case SIGBUS:
//longjmp(state,2);
g_message("signal_handler: SIGBUS signal caught");
exit(0);
break;
}
return;
}
void daemonize(void) {
int f;
pid_t r, sid;
if (getppid() == 1) return; /* already a daemon */
r = fork();
if (r<0)
{
//g_message( "daemonize: fork error");
exit(1); /* fork error */
}
if (r>0) //This is the return of fork for the parent process, the pid of the child
{
exit(0); /* parent exits */
}
/* child (daemon) continues */
sid = setsid(); /* obtain a new process group */
if (sid < 0)
{
//g_message("setsid error");
exit(1);
}
for (f = getdtablesize(); f >= 0; --f)
{
//g_message( "f: %d",f);
close(f); /* close all descriptors */
}
f = open("/dev/null",O_RDWR); dup(f); dup(f); /* handle standard I/O */
if (f < 0)
{
//g_message("error opening file");
exit(1);
}
umask(027); /* set newly created file permissions */
chdir(NC_STATE_DIR);
//chdir("/var");
pid_file = fopen( NC_PID_FILE, "w" );
//pid_file = fopen( "/var/run/splashd.pid", "w" );
if (pid_file == NULL)
{
exit(1); /* can not open */
}
if (lockf( fileno(pid_file), F_TLOCK, 0 ) < 0)
{
exit(0); /* can not lock */
}
/* write PID to lockfile */
if (fprintf(pid_file, "%d\n", getpid()) < 0)
{
//g_message( "error en el fprintf()");
exit(0);
}
// fclose(lfp);
// signal(SIGCHLD,SIG_IGN); /* ignore child */
signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP, signal_handler); /* catch hangup signal */
signal(SIGTERM, signal_handler); /* catch kill signal */
signal(SIGINT, signal_handler);
signal(SIGSEGV, signal_handler);
signal(SIGBUS, signal_handler);
//signal(SIGURG, signal_handler);
}
void g_syslog (const gchar* log_domain, GLogLevelFlags log_level,
const gchar* message, gpointer user_data) {
int priority;
size_t message_len = 0;
div_t result;
unsigned int t = 1;
gchar* message_part = NULL;
unsigned int width = (unsigned int) CONFd("llwidth");
unsigned int mem = (unsigned int) CONFd("lmem");
pid_t my_pid;
gchar* nombre;
gchar* contenido;
gsize length;
gchar *start_memory, *end_memory;
gchar *VmSize, *VmRSS, *VmData, *VmStk, *VmLib;
unsigned int memory_size;
switch (log_level & G_LOG_LEVEL_MASK) {
case G_LOG_LEVEL_ERROR: priority = LOG_ERR; break;
case G_LOG_LEVEL_CRITICAL: priority = LOG_CRIT; break;
case G_LOG_LEVEL_WARNING: priority = LOG_WARNING; break;
case G_LOG_LEVEL_MESSAGE: priority = LOG_NOTICE; break;
case G_LOG_LEVEL_INFO: priority = LOG_INFO; break;
case G_LOG_LEVEL_DEBUG:
default: priority = LOG_DEBUG; break;
}
message_len = strlen(message);
if (mem){
my_pid = getpid();
nombre = g_new0(gchar,100);
g_sprintf(nombre,"%s%d%s","/proc/",my_pid,"/status");
g_file_get_contents(nombre,&contenido,&length,NULL);
g_free(nombre);
start_memory = g_strstr_len(contenido,-1,"VmSize:");
end_memory = g_strstr_len(contenido,-1,"VmLck:");
memory_size = end_memory - start_memory - 9;
VmSize = g_strndup(start_memory+8,memory_size);
g_strchug(VmSize);
g_strcanon(VmSize,"0123456789",'\0');
start_memory = g_strstr_len(contenido,-1,"VmRSS:");
end_memory = g_strstr_len(contenido,-1,"VmData:");
memory_size = end_memory - start_memory - 8;
VmRSS = g_strndup(start_memory+7,memory_size);
g_strchug(VmRSS);
g_strcanon(VmRSS,"0123456789",'\0');
start_memory = g_strstr_len(contenido,-1,"VmData:");
end_memory = g_strstr_len(contenido,-1,"VmStk:");
memory_size = end_memory - start_memory - 9;
VmData = g_strndup(start_memory+8,memory_size);
g_strchug(VmData);
g_strcanon(VmData,"0123456789",'\0');
start_memory = g_strstr_len(contenido,-1,"VmStk:");
end_memory = g_strstr_len(contenido,-1,"VmExe:");
memory_size = end_memory - start_memory - 8;
VmStk = g_strndup(start_memory+7,memory_size);
g_strchug(VmStk);
g_strcanon(VmStk,"0123456789",'\0');
start_memory = g_strstr_len(contenido,-1,"VmLib:");
end_memory = g_strstr_len(contenido,-1,"VmPTE:");
memory_size = end_memory - start_memory - 8;
VmLib = g_strndup(start_memory+7,memory_size);
g_strchug(VmLib);
g_strcanon(VmLib,"0123456789",'\0');
g_free(contenido);
}
if ( message_len > width ) {
result = div(message_len,width);
t = result.quot;
if (result.rem > 0) t = t + 1;
}
for (unsigned int i = 0; i < t; i++){
message_part = g_new0(gchar,width + 2 + 50);
strncpy (message_part,message+(i*width),width);
if (mem){
if ( i == 0) {
syslog( priority | LOG_DAEMON, "%s-%s-%s-%s-%s-- %s",VmSize,VmRSS,VmData,VmStk,VmLib,message_part);
if (log_fd != NULL){
fprintf (log_fd, "%s-%s-%s-%s-%s-- %s",VmSize,VmRSS,VmData,VmStk,VmLib, strcat (message_part, "\n"));
fflush (log_fd);
}
}
else {
syslog( priority | LOG_DAEMON, "%s", message_part);
if (log_fd != NULL){
fprintf (log_fd, "%s",strcat (message_part, "\n"));
fflush (log_fd);
}
}
}
else {
syslog(priority | LOG_DAEMON, "%s", message_part);
if (log_fd != NULL){
fprintf (log_fd, "%s",strcat (message_part, "\n"));
fflush (log_fd);
}
}
g_free(message_part);
}
if (mem){
g_free(VmSize);
g_free(VmRSS);
g_free(VmData);
g_free(VmStk);
g_free(VmLib);
}
//syslog( priority | LOG_DAEMON, message );
if (log_level & G_LOG_FLAG_FATAL) exit_signal = -1;
}
FILE * initialize_log (void)
{
int level;
FILE* fd;
unsigned int wsk_log_level = CONFd("wsk_log_level");
switch (wsk_log_level){
case 1:
level = G_LOG_LEVEL_ERROR;
break;
case 3:
level = (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING);
break;
case 7:
level = (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE);
break;
case 15:
level = (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO);
break;
case 31:
default:
level = (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG);
break;
}
if (strncmp( CONF("LogFacility"), "syslog", 6 ) == 0)
{
openlog(CONF("SyslogIdent"), LOG_CONS | LOG_PID, LOG_DAEMON );
g_log_set_handler( 0,(GLogLevelFlags)(level | G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL),g_syslog,0);
fd = NULL;
}
else {
openlog(CONF("SyslogIdent"), LOG_CONS | LOG_PID, LOG_DAEMON );
g_log_set_handler( 0,(GLogLevelFlags)(level | G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL),g_syslog,0);
fd = fopen (CONF("LogFacility"), "a");
//fprintf (fd, "%s",strcat ("iniciando una corrida de sicat", "\n\n"));
//fflush (fd);
}
return fd;
}
char* peer_arp_dns(gchar* ip_add) {
char* ip = g_new0(char,50);
char* hw = g_new0(char,18);
FILE *arp;
arp = fopen( "/proc/net/arp", "r" );
if ( arp == NULL ){
g_message( "Can't open /proc/net/arp: %m" );
g_free(ip);
g_free(hw);
return NULL;
}
fscanf(arp, "%*s %*s %*s %*s %*s %*s %*s %*s %*s"); // Skip first line
while (fscanf( arp, "%15s %*s %*s %17s %*s %*s\n", ip, hw ) != EOF){
if (strcmp( ip_add, ip) == 0 )
{
//g_strncpy(hw_add, hw, sizeof(hw) );
g_free(ip);
fclose(arp);
return hw;
}
}
g_free(ip);
g_free(hw);
fclose(arp);
return NULL;
}
static int nfq_http_callback(struct nfq_q_handle *qh,struct nfgenmsg *nfmsg,struct nfq_data *nfad, void *data){
int id,n;
unsigned char *payload;
struct nfq_iphdr* ip_header;
struct nfq_tcphdr* tcp_header;
gchar ip_dest[16];
gchar ip_source[16];
gchar* hw = NULL;
peer* p = NULL;
//uint32_t** temp_addresses;
//struct sockaddr_in aa;
struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfad);
id = ntohl(ph->packet_id);
n = nfq_get_payload(nfad,&payload);
ip_header = (struct nfq_iphdr*) payload;
inet_ntop(AF_INET, &ip_header->saddr, ip_source, 16);
hw = peer_arp_dns(ip_source);
if (hw != NULL) {
p = (peer*)g_hash_table_lookup(peer_tab, hw);
g_free(hw);
}
inet_ntop(AF_INET, &ip_header->daddr, ip_dest,16);
g_debug("nfq_http_callback: captured output ip package from %s to %s",ip_source,ip_dest);
if (p != NULL){
if ((p->status == 0) || (p->status == 2) || (p->status == 3)){ // Aqu'i el peer se est'a autentificando en cualquiera de los dos
// statuses para eso (0 o 2) o est'a permitido ya (status == 3)
g_debug("el peer est'a en modo 0, 2 'o 3, se deja pasar el paquete ip..");
nfq_set_verdict2(qh, id, NF_ACCEPT, 3 ,0, NULL);
return 0;
}
else if (p->status == 1) { // El peer est'a castigado, as'i que se devuelve el paquete sin marcar
// para que se atrape.
tcp_header = (struct nfq_tcphdr*) (payload + (ip_header->ihl * 4));
if ( (ntohs(tcp_header->dest) == 80) || (ntohs(tcp_header->dest) == 443) ) {
g_debug("nfq_http_callback: el peer est'a castigado, se deja pasar http / https para que se capture el cliente");
nfq_set_verdict(qh, id, NF_ACCEPT,0, NULL);
return 0;
}
/*else {
g_debug("nfq_http_callback: el peer est'a castigado, no se deja pasar https");
nfq_set_verdict(qh, id, NF_DROP,0, NULL);
return 0;
}*/
}
}
else {
tcp_header = (struct nfq_tcphdr*) (payload + (ip_header->ihl * 4));
if ( (ntohs(tcp_header->dest) == 80) || (ntohs(tcp_header->dest) == 443) ) {
g_debug("nfq_http_callback: el peer no existe, se deja pasar http / https para que se capture el cliente");
nfq_set_verdict(qh, id, NF_ACCEPT,0, NULL);
return 0;
}
/*else {
g_debug("nfq_http_callback: el peer no existe, no se deja pasar https");
nfq_set_verdict(qh, id, NF_DROP,0, NULL);
return 0;
}*/
}
return 0;
}
static int nfq_ssl_callback(struct nfq_q_handle *qh,struct nfgenmsg *nfmsg,struct nfq_data *nfad, void *data){
int id,n;
unsigned char *payload;
struct nfq_iphdr* ip_header;
struct nfq_tcphdr* tcp_header;
gchar ip_source[16];
char* hw = NULL;
struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfad);
id = ntohl(ph->packet_id);
n = nfq_get_payload(nfad,&payload);
ip_header = (struct nfq_iphdr*) payload;
inet_ntop(AF_INET, &ip_header->saddr, ip_source, 16);
hw = peer_arp_dns(ip_source);
gchar* ip_dest = g_new0(gchar,16);
inet_ntop(AF_INET, &ip_header->daddr, ip_dest,16);
//g_debug("nfq_ssl_callback: captured ip package from %s to %s",ip_source,ip_dest);
tcp_header = (struct nfq_tcphdr*) (payload + (ip_header->ihl * 4));
if (ntohs(tcp_header->dest) == 443) {
// Esto es https por lo tanto hay que incluirlo en la hashtable ssl_connected_tab
// El formato de la llave es hw:ip:port, en el valor de la hash table lo que va es
// la direcci'on ip real del servidor al que se quiere conectar el cliente.
gchar* key = g_new0(char,40);
if (hw != NULL) {
strcat(key,hw);
g_free(hw);
}
else strcat(key,"NO_HW_ADDR");
strcat(key,"//");
strcat(key,ip_source);
strcat(key,":");
gchar* puerto_origen = g_new0(gchar,4);
sprintf (puerto_origen, "%d",ntohs(tcp_header->source));
strcat(key,puerto_origen);
g_free(puerto_origen);
if ( g_hash_table_lookup(ssl_connected_tab, key) == NULL) {
g_debug("nfq_ssl_callback: insertando en la hash table ssl_connnected_tab la llave: %s con el valor %s",key,ip_dest);
g_hash_table_insert(ssl_connected_tab,key,ip_dest);
}
else {
g_debug("nfq_ssl_callback: la clave %s ya existe en la hash table ssl_connnected_tab",key);
g_free(key);
}
}
nfq_set_verdict(qh, id, NF_ACCEPT,0, NULL);
return 0;
}
static int nfq_http_input_callback(struct nfq_q_handle *qh,struct nfgenmsg *nfmsg,struct nfq_data *nfad, void *data){
int id,n;
unsigned char *payload;
struct nfq_iphdr* ip_header;
struct nfq_tcphdr* tcp_header;
gchar ip_dest[16];
gchar ip_source[16];
char* hw = NULL;
peer* p = NULL;
//uint32_t** temp_addresses;
//struct sockaddr_in aa;
struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfad);