This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhttpd.c
3770 lines (3244 loc) · 129 KB
/
httpd.c
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
/*
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*
* http://www.ntop.org
*
* Copyright (C) 1998-2012 Luca Deri <[email protected]>
*
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
*/
#include "ntop.h"
#include "globals-report.h"
#if 0
#define URL_DEBUG
#define HTTP_DEBUG
#endif
/* Private structure definitions */
struct _HTTPstatus {
int statusCode;
char *reasonPhrase;
char *longDescription;
};
/*
This is the complete list of "Status Codes" and suggested
"Reason Phrases" for HTTP responses, as stated in RFC 2068
NOTE: the natural order is altered so "200 OK" results the
first item in the table (index = 0)
*/
struct _HTTPstatus HTTPstatus[] = {
{ 200, "OK", NULL },
{ 100, "Continue", NULL },
{ 101, "Switching Protocols", NULL },
{ 201, "Created", NULL },
{ 202, "Accepted", NULL },
{ 203, "Non-Authoritative Information", NULL },
{ 204, "No Content", NULL },
{ 205, "Reset Content", NULL },
{ 206, "Partial Content", NULL },
{ 300, "Multiple Choices", NULL },
{ 301, "Moved Permanently", NULL },
{ 302, "Moved Temporarily", NULL },
{ 303, "See Other", NULL },
{ 304, "Not Modified", NULL },
{ 305, "Use Proxy", NULL },
{ 400, "Bad Request", "The specified request is invalid." },
{ 401, "Authorization Required", "Unauthorized to access the document." },
{ 402, "Payment Required", NULL },
{ 403, "Forbidden", "Server refused to fulfill your request." },
{ 404, "Not Found", "The server cannot find the requested page "
"(page expired or ntop configuration ?)." },
{ 405, "Method Not Allowed", NULL },
{ 406, "Not Acceptable", NULL },
{ 407, "Proxy Authentication Required", NULL },
{ 408, "Request Time-out", "The request was timed-out." },
{ 409, "Conflict", NULL },
{ 410, "Gone", "The page you requested is not available in your current ntop "
"<A HREF=\"/" CONST_INFO_NTOP_HTML "\">configuration</A>. See the ntop "
"<A HREF=\"/" CONST_MAN_NTOP_HTML "\">man page</A> for more information" },
{ 411, "Length Required", NULL },
{ 412, "Precondition Failed", NULL },
{ 413, "Request Entity Too Large", NULL },
{ 414, "Request-URI Too Large", NULL },
{ 415, "Unsupported Media Type", NULL },
{ 500, "Internal Server Error", NULL },
{ 501, "Not Implemented", "The requested method is not implemented by this server." },
{ 502, "Bad Gateway", NULL },
{ 503, "Service Unavailable", NULL },
{ 504, "Gateway Time-out", NULL },
{ 505, "HTTP Version not supported", "This server doesn't support the specified HTTP version." },
};
/*
Note: the numbers below are offsets inside the HTTPstatus table,
they must be corrected every time the table is modified.
*/
#define BITFLAG_HTTP_STATUS_200 ( 0<<8)
#define BITFLAG_HTTP_STATUS_302 (11<<8)
#define BITFLAG_HTTP_STATUS_304 (13<<8)
#define BITFLAG_HTTP_STATUS_400 (15<<8)
#define BITFLAG_HTTP_STATUS_401 (16<<8)
#define BITFLAG_HTTP_STATUS_403 (18<<8)
#define BITFLAG_HTTP_STATUS_404 (19<<8)
#define BITFLAG_HTTP_STATUS_408 (23<<8)
#define BITFLAG_HTTP_STATUS_500 (31<<8)
#define BITFLAG_HTTP_STATUS_501 (32<<8)
#define BITFLAG_HTTP_STATUS_505 (36<<8)
/* ************************* */
#define MAX_NUM_COMMUNITIES 32
static u_int httpBytesSent;
char httpRequestedURL[512], theHttpUser[32], theLastHttpUser[32];
char allowedCommunities[256], *listAllowedCommunities[MAX_NUM_COMMUNITIES];
static HostAddr *requestFrom;
/* ************************* */
/* Forward */
static int readHTTPheader(char* theRequestedURL, int theRequestedURLLen,
char *thePw, int thePwLen,
char *theAgent, int theAgentLen,
char *theReferer, int theRefererLen,
char *theLanguage, int theLanguageLen,
char *ifModificedSince, int ifModificedSinceLen,
int *isPostMethod);
static int returnHTTPPage(char* pageName,
int postLen,
HostAddr *from,
struct timeval *httpRequestedAt,
int *usedFork,
char *agent,
char *referer,
char *requestedLanguage[],
int numLang,
char *ifModificedSince,
int isPostMethod);
static int generateNewInternalPages(char* pageName);
static int decodeString(char *bufcoded, unsigned char *bufplain, int outbufsize);
static void logHTTPaccess(int rc, struct timeval *httpRequestedAt, u_int gzipBytesSent);
static void returnHTTPspecialStatusCode(int statusIdx, char *additionalText);
static int checkURLsecurity(char *url);
static int checkHTTPpassword(char *theRequestedURL, int theRequestedURLLen _UNUSED_, char* thePw, int thePwLen);
static char compressedFilePath[256];
static short compressFile = 0, acceptGzEncoding;
static FILE *compressFileFd=NULL;
#ifdef MAKE_WITH_ZLIB
static void compressAndSendData(u_int*);
#endif
/* ************************* */
#ifdef HAVE_OPENSSL
char* printSSLError(int errorId) {
switch(errorId) {
case SSL_ERROR_NONE: return("SSL_ERROR_NONE");
case SSL_ERROR_SSL: return("SSL_ERROR_SSL");
case SSL_ERROR_WANT_READ: return("SSL_ERROR_WANT_READ");
case SSL_ERROR_WANT_WRITE: return("SSL_ERROR_WANT_WRITE");
case SSL_ERROR_WANT_X509_LOOKUP: return("SSL_ERROR_WANT_X509_LOOKUP");
case SSL_ERROR_SYSCALL: return("SSL_ERROR_SYSCALL");
case SSL_ERROR_ZERO_RETURN: return("SSL_ERROR_ZERO_RETURN");
case SSL_ERROR_WANT_CONNECT: return("SSL_ERROR_WANT_CONNECT");
default: return("???");
}
}
#endif
/* ************************* */
static int readHTTPheader(char* theRequestedURL,
int theRequestedURLLen,
char *thePw, int thePwLen,
char *theAgent, int theAgentLen,
char *theReferer, int theRefererLen,
char *theLanguage, int theLanguageLen,
char *ifModificedSince, int ifModificedSinceLen,
int *isPostMethod) {
#ifdef HAVE_OPENSSL
SSL* ssl = getSSLsocket(-myGlobals.newSock);
#endif
char aChar[8] /* just in case */, lastChar;
char *lineStr;
int rc, idxChar=0, contentLen=-1, numLine=0, topSock;
fd_set mask;
struct timeval wait_time;
int errorCode=0, lineStrLen;
char *tmpStr;
*isPostMethod = FALSE; /* Assume GET Method by default */
thePw[0] = '\0';
lastChar = '\n';
theRequestedURL[0] = '\0';
memset(httpRequestedURL, 0, sizeof(httpRequestedURL));
lineStrLen = 768;
lineStr = (char*)calloc(lineStrLen, sizeof(char));
if(lineStr == NULL) {
traceEvent(CONST_TRACE_WARNING, "Not enough memory");
return(FLAG_HTTP_INVALID_REQUEST);
}
#ifdef HAVE_OPENSSL
topSock = abs(myGlobals.newSock);
#else
topSock = myGlobals.newSock;
#endif
for(;;) {
FD_ZERO(&mask);
FD_SET((unsigned int)topSock, &mask);
/* printf("About to call select()\n"); fflush(stdout); */
if(myGlobals.newSock > 0) {
/*
Call select only for HTTP.
Fix courtesy of Olivier Maul <[email protected]>
*/
/* select returns immediately */
wait_time.tv_sec = 10; wait_time.tv_usec = 0;
rc = select(myGlobals.newSock+1, &mask, 0, 0, &wait_time);
if(rc == 0) {
errorCode = FLAG_HTTP_REQUEST_TIMEOUT; /* Timeout */
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Timeout while reading from socket.");
#endif
break;
}
}
/* printf("About to call recv()\n"); fflush(stdout); */
/* printf("Rcvd %d bytes\n", recv(myGlobals.newSock, aChar, 1, MSG_PEEK)); fflush(stdout); */
#ifdef HAVE_OPENSSL
if(myGlobals.newSock < 0) {
rc = SSL_read(ssl, aChar, 1);
if(rc == -1) {
/* traceEvent(CONST_TRACE_ERROR, "[SSL] [error=%d]", SSL_get_error(ssl, rc)); */
ntop_ssl_error_report("read");
}
} else
rc = (int)recv(myGlobals.newSock, aChar, 1, 0);
#else
rc = (int)recv(myGlobals.newSock, aChar, 1, 0);
#endif
if(rc != 1) {
idxChar = 0;
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Socket read returned %d (errno=%d)", rc, errno);
#endif
/* FIXME (DL): is valid to write to the socket after this condition? */
break; /* Empty line */
} else if((errorCode == 0) && !isprint(aChar[0]) && !isspace(aChar[0])) {
errorCode = FLAG_HTTP_INVALID_REQUEST;
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Rcvd non expected char '%c' [%d/0x%x]",
aChar[0], aChar[0], aChar[0]);
#endif
} else {
#ifdef URL_DEBUG
if(0)
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Rcvd char '%c' [%d/0x%x]",
aChar[0], aChar[0], aChar[0]);
#endif
if(aChar[0] == '\r') {
/* <CR> is ignored as recommended in section 19.3 of RFC 2068 */
continue;
} else if(aChar[0] == '\n') {
if(lastChar == '\n') {
idxChar = 0;
break;
}
numLine++;
lineStr[idxChar] = '\0';
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: read HTTP %s line: %s [%d]",
(numLine>1) ? "header" : "request", lineStr, idxChar);
#endif
if(errorCode != 0) {
; /* skip parsing after an error was detected */
} else if(numLine == 1) {
strncpy(httpRequestedURL, lineStr,
sizeof(httpRequestedURL)-1)[sizeof(httpRequestedURL)-1] = '\0';
if(idxChar < 9) {
errorCode = FLAG_HTTP_INVALID_REQUEST;
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Too short request line.");
#endif
} else if(strncmp(&lineStr[idxChar-9], " HTTP/", 6) != 0) {
errorCode = FLAG_HTTP_INVALID_REQUEST;
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Malformed request line. [%s][idxChar=%d]",
&lineStr[idxChar-9], idxChar);
{
int y;
for(y=0; y<idxChar; y++)
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: lineStr[%d]='%c'", y, lineStr[y]);
}
#endif
} else if((strncmp(&lineStr[idxChar-3], "1.0", 3) != 0) &&
(strncmp(&lineStr[idxChar-3], "1.1", 3) != 0)) {
errorCode = FLAG_HTTP_INVALID_VERSION;
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Unsupported HTTP version.");
#endif
} else {
lineStr[idxChar-9] = '\0'; idxChar -= 9; tmpStr = NULL;
if((idxChar >= 3) && (strncmp(lineStr, "GET ", 4) == 0)) {
tmpStr = &lineStr[4];
} else if((idxChar >= 4) && (strncmp(lineStr, "POST ", 5) == 0)) {
tmpStr = &lineStr[5];
*isPostMethod = TRUE;
/*
HEAD method could be supported with some litle modifications...
} else if((idxChar >= 4) && (strncmp(lineStr, "HEAD ", 5) == 0)) {
tmpStr = &lineStr[5];
*/
} else {
errorCode = FLAG_HTTP_INVALID_METHOD;
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Unrecognized method in request line.");
#endif
}
if(tmpStr) {
int beginIdx;
/*
Before to copy the URL let's check whether
it has been sent through a proxy
*/
if(strncasecmp(tmpStr, "http://", 7) == 0)
beginIdx = 7;
else if(strncasecmp(tmpStr, "https://", 8) == 0)
beginIdx = 8;
else
beginIdx = 0;
if(beginIdx > 0) {
while((tmpStr[beginIdx] != '\0') && (tmpStr[beginIdx] != '/'))
beginIdx++;
}
strncpy(theRequestedURL, &tmpStr[beginIdx],
theRequestedURLLen-1)[theRequestedURLLen-1] = '\0';
}
}
} else if((idxChar >= 21)
&& (strncasecmp(lineStr, "Authorization: Basic ", 21) == 0)) {
strncpy(thePw, &lineStr[21], thePwLen-1)[thePwLen-1] = '\0';
#ifdef MAKE_WITH_ZLIB
} else if((idxChar >= 17)
&& (strncasecmp(lineStr, "Accept-Encoding: ", 17) == 0)) {
if(strstr(&lineStr[17], "gzip"))
acceptGzEncoding = 1;
#endif
} else if((idxChar >= 16)
&& (strncasecmp(lineStr, "Content-Length: ", 16) == 0)) {
contentLen = atoi(&lineStr[16]);
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: len=%d [%s/%s]", contentLen, lineStr, &lineStr[16]);
#endif
} else if((idxChar >= 19)
&& (strncasecmp(lineStr, "If-Modified-Since: ", 19) == 0)) {
strncpy(ifModificedSince, &lineStr[19], ifModificedSinceLen-1)[ifModificedSinceLen-1] = '\0';
} else if((idxChar >= 12)
&& (strncasecmp(lineStr, "User-Agent: ", 12) == 0)) {
strncpy(theAgent, &lineStr[12], theAgentLen-1)[theAgentLen-1] = '\0';
} else if((idxChar >= 9)
&& (strncasecmp(lineStr, "Referer: ", 9) == 0)) {
strncpy(theReferer, &lineStr[9], theRefererLen-1)[theRefererLen-1] = '\0';
}
idxChar=0;
} else if(idxChar > lineStrLen-2) {
if(errorCode == 0) {
errorCode = FLAG_HTTP_INVALID_REQUEST;
#ifdef URL_DEBUG
traceEvent(CONST_TRACE_INFO, "URL_DEBUG: Line too long (hackers ?)");
#endif
}
} else {
lineStr[idxChar++] = aChar[0];
}
}
lastChar = aChar[0];
}
free(lineStr);
return((errorCode) ? errorCode : contentLen);
}
/* ************************* */
char* encodeString(char* in, char* out, u_int out_len) {
int i, out_idx;
out[0] = '\0';
for(i = out_idx = 0; i < strlen(in); i++) {
if(isalnum(in[i])) {
out[out_idx++] = in[i];
if(out_idx >= out_len) return(out);
} else if(in[i] == ' ') {
out[out_idx++] = '+';
if(out_idx >= out_len) return(out);
} else {
char hex_str[8];
out[out_idx++] = '%';
sprintf(hex_str, "%02X", in[i] & 0xFF);
out[out_idx++] = hex_str[0];
if(out_idx >= out_len) return(out);
out[out_idx++] = hex_str[1];
if(out_idx >= out_len) return(out);
}
}
out[out_idx++] = '\0';
return(out);
}
/* ************************* */
static int decodeString(char *bufcoded,
unsigned char *bufplain,
int outbufsize) {
/* single character decode */
#define DEC(c) pr2six[(int)c]
#define MAXVAL 63
unsigned char pr2six[256];
char six2pr[64] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'
};
/* static */ int first = 1;
int nbytesdecoded, j;
char *bufin = bufcoded;
unsigned char *bufout = bufplain;
int nprbytes;
/* If this is the first call, initialize the mapping table.
* This code should work even on non-ASCII machines.
*/
if(first) {
first = 0;
for(j=0; j<256; j++) pr2six[j] = MAXVAL+1;
for(j=0; j<64; j++) pr2six[(int)six2pr[j]] = (unsigned char) j;
}
/* Strip leading whitespace. */
while(*bufcoded==' ' || *bufcoded == '\t') bufcoded++;
/* Figure out how many characters are in the input buffer.
* If this would decode into more bytes than would fit into
* the output buffer, adjust the number of input bytes downwards.
*/
bufin = bufcoded;
while(pr2six[(int)*(bufin++)] <= MAXVAL)
;
nprbytes = (int)(bufin - bufcoded - 1);
nbytesdecoded = ((nprbytes+3)/4) * 3;
if(nbytesdecoded > outbufsize) {
nprbytes = (outbufsize*4)/3;
}
bufin = bufcoded;
while (nprbytes > 0) {
*(bufout++) = (unsigned char) (DEC(*bufin) << 2 | DEC(bufin[1]) >> 4);
*(bufout++) = (unsigned char) (DEC(bufin[1]) << 4 | DEC(bufin[2]) >> 2);
*(bufout++) = (unsigned char) (DEC(bufin[2]) << 6 | DEC(bufin[3]));
bufin += 4;
nprbytes -= 4;
}
if(nprbytes & 03) {
if(pr2six[(int)bufin[-2]] > MAXVAL) {
nbytesdecoded -= 2;
} else {
nbytesdecoded -= 1;
}
}
return(nbytesdecoded);
}
/* ************************* */
static void ssiMenu_Head(void) {
FlowFilterList *flows = myGlobals.flowsList;
short foundAplugin = 0;
char buf[LEN_GENERAL_WORK_BUFFER];
ExtraPage *ep;
memset(&buf, 0, sizeof(buf));
sendStringWOssi(
"<link rel=\"stylesheet\" href=\"/theme.css\" TYPE=\"text/css\">\n"
"<script type=\"text/javascript\" language=\"JavaScript\" src=\"/autosuggest.js\"></script>\n"
"<link rel=\"stylesheet\" href=\"/autosuggest.css\" type=\"text/css\" media=\"screen\" charset=\"utf-8\" />\n"
"<script type=\"text/javascript\" language=\"JavaScript\" src=\"/theme.js\"></script>\n"
"<script type=\"text/javascript\" language=\"JavaScript\" SRC=\"/JSCookMenu.js\"></script>\n"
"<script type=\"text/javascript\" language=\"JavaScript\"><!--\n"
"var ntopMenu =\n"
"[\n"
" [null,'About',null,null,null,\n"
" [null,'What is ntop?','/" CONST_ABTNTOP_HTML "',null,null],\n"
" [null,'ntop blog','http://www.ntop.org/blog/',null,null],\n"
" [null,'Credits','/" CONST_CREDITS_HTML "',null,null],\n"
" [null,'Make a Donation', 'http://shop.ntop.org/',null,null],\n"
" [null,'ntop World',null,null,null,\n"
" [null,'ntop-based Solutions','http://www.ntop.org/solutions.html',null,null],\n"
" [null,'nMon.net Products','http://www.nmon.net/products.html',null,null],\n"
" ],\n"
" [null,'Online Documentation',null,null,null,\n"
" [null,'Man Page','/" CONST_MAN_NTOP_HTML "',null,null],\n");
#ifdef HAVE_PYTHON
sendStringWOssi(
" ['<img src=/icon_python.png>','Python ntop Engine',null,null,null,\n"
" [ null,'Python API','/docs/python/index.html', '_blank',null],\n"
" [ null,'Tutorial','http://www.ntop.org/blog/?p=112', '_blank',null],\n"
" ],\n");
#endif
sendStringWOssi(
" ['<img src=\"/help.png\">','Help','/ntop" CONST_NTOP_HELP_HTML "',null,null],\n"
" [null,'FAQ','/faq.html',null,null],\n"
" ['<img src=\"/Risk_high.gif\">','Risk Flags','/" CONST_NTOP_HELP_HTML "',null,null],\n"
" ],\n"
" [null,'Show Configuration','/" CONST_INFO_NTOP_HTML "',null,null],\n"
" ['<img src=\"/bug.png\">','Report a Problem','/" CONST_PROBLEMRPT_HTML "',null,null],\n"
" ],\n"
" [null,'Summary',null,null,null,\n"
" [null,'Traffic','/" CONST_TRAFFIC_STATS_HTML "',null,null],\n"
" [null,'Hosts','/" CONST_HOSTS_INFO_HTML "',null,null],\n"
" [null,'Network Load','/" CONST_SORT_DATA_THPT_STATS_HTML "',null,null],\n"
" [null,'Traffic Maps',null,null,null,\n"
);
#ifdef HAVE_PYTHON
sendStringWOssi(" [null,'Region Map','/" CONST_REGION_MAP "',null,null],\n");
#endif
sendStringWOssi(
" [null,'Host Map','/" CONST_HOST_MAP "',null,null],\n"
" ],\n"
);
if(myGlobals.haveVLANs == TRUE)
sendStringWOssi(
" [null,'VLAN Info','/" CONST_VLAN_LIST_HTML "',null,null],\n");
sendStringWOssi(
" [null,'Network Flows','/" CONST_NET_FLOWS_HTML "',null,null],\n"
" ],\n"
" [null,'All Protocols',null,null,null,\n"
" [null,'Traffic','/" CONST_SORT_DATA_PROTOS_HTML "',null,null],\n"
" [null,'Throughput','/" CONST_SORT_DATA_THPT_HTML "',null,null],\n"
" [null,'Activity','/" CONST_SORT_DATA_HOST_TRAFFIC_HTML "',null,null],\n"
" [null,'Top Talkers',null,null,null,\n"
" [null,'Last Hour', '/" CONST_LAST_HOUR_TOP_TALKERS_HTML "',null,null],\n"
" [null,'Last Day', '/" CONST_LAST_DAY_TOP_TALKERS_HTML "',null,null],\n"
" [null,'Historical','/" CONST_HISTORICAL_TALKERS_HTML "',null,null],\n"
" ],\n"
" ],\n"
" [null,'IP',null,null,null,\n"
" [null,'Summary',null,null,null,\n"
" [null,'Traffic','/" CONST_SORT_DATA_IP_HTML "',null,null],\n"
" [null,'Multicast','/" CONST_MULTICAST_STATS_HTML "',null,null],\n"
" [null,'Internet Domain','/" CONST_DOMAIN_STATS_HTML "',null,null],\n"
" [null,'Networks','/" CONST_DOMAIN_STATS_HTML "?netmode=1',null,null],\n"
" [null,'ASs','/" CONST_DOMAIN_STATS_HTML "?netmode=2',null,null],\n"
" [null,'Host Communities','/" CONST_COMMUNITIES_STATS_HTML "',null,null],\n"
" [null,'Distribution','/" CONST_IP_PROTO_DISTRIB_HTML "',null,null],\n"
" ],\n"
" [null,'Traffic Directions',null,null,null,\n"
" [null,'Local to Local','/" CONST_IP_L_2_L_HTML "',null,null],\n"
" [null,'Local to Remote','/" CONST_IP_L_2_R_HTML "',null,null],\n"
" [null,'Remote to Local','/" CONST_IP_R_2_L_HTML "',null,null],\n"
" [null,'Remote to Remote','/" CONST_IP_R_2_R_HTML "',null,null],\n"
" ],\n"
" [null,'Local',null,null,null,\n");
sendStringWOssi(
" [null,'Routers','/" CONST_LOCAL_ROUTERS_LIST_HTML "',null,null],\n");
sendStringWOssi(
" [null,'Ports Used','/" CONST_IP_PROTO_USAGE_HTML "',null,null],\n");
if(myGlobals.runningPref.enableSessionHandling)
sendStringWOssi(
" [null,'Active Sessions','/" CONST_ACTIVE_SESSIONS_HTML "',null,null],\n");
sendStringWOssi(
" [null,'Host Fingerprint','/" CONST_HOSTS_LOCAL_FINGERPRINT_HTML "',null,null],\n"
" [null,'Host Characterization','/" CONST_HOSTS_LOCAL_CHARACT_HTML "',null,null],\n");
#ifndef WIN32
sendStringWOssi(
" [null,'Network Traffic Map','/" CONST_NETWORK_MAP_HTML "',null,null],\n");
#endif
sendStringWOssi(
" ],\n"
" ],\n");
#ifdef ENABLE_FC
if(!myGlobals.runningPref.printIpOnly
&& myGlobals.device[myGlobals.actualReportDeviceId].vsanHash /* We have something to show */) {
sendStringWOssi(
" [null,'Media',null,null,null,\n"
" [null,'Fibre Channel',null,null,null,\n"
" [null,'Traffic','/" CONST_FC_DATA_HTML "',null,null],\n"
" [null,'Throughput','/" CONST_FC_THPT_HTML "',null,null],\n"
" [null,'Activity','/" CONST_FC_ACTIVITY_HTML "',null,null],\n"
" [null,'Hosts','/" CONST_FC_HOSTS_INFO_HTML "',null,null],\n"
" [null,'Traffic Per Port','/" CONST_FC_TRAFFIC_HTML "',null,null],\n");
if(myGlobals.runningPref.enableSessionHandling)
sendStringWOssi(
" [null,'Sessions','/" CONST_FC_SESSIONS_HTML "',null,null],\n");
sendStringWOssi(
" [null,'VSANs','/" CONST_VSAN_LIST_HTML "',null,null],\n"
" [null,'VSAN Summary','/" CONST_VSAN_DISTRIB_HTML "',null,null],\n"
" ],\n");
if(myGlobals.runningPref.enableSessionHandling)
sendStringWOssi(
" [null,'SCSI Sessions',null,null,null,\n"
" [null,'Bytes','/" CONST_SCSI_BYTES_HTML "',null,null],\n"
" [null,'Times','/" CONST_SCSI_TIMES_HTML "',null,null],\n"
" [null,'Status','/" CONST_SCSI_STATUS_HTML "',null,null],\n"
" [null,'Task Management','/" CONST_SCSI_TM_HTML "',null,null],\n"
" ],\n");
sendStringWOssi(
" ],\n");
}
#endif
sendStringWOssi(" [null,'Utils',null,null,null,\n");
#ifdef HAVE_PYTHON
sendStringWOssi(" [null,'RRD Alarm',null,null,null,\n"
" [null,'Configure Thresholds','/python/rrdalarm/config.py',null,null],\n"
" [null,'Check Now','/python/rrdalarm/start.py',null,null],\n"
" ],\n");
#endif
sendStringWOssi(" [null,'Data Dump','/dump.html',null,null],\n"
" [null,'View Log','/" CONST_VIEW_LOG_HTML "',null,null],\n"
" ],\n");
while(flows != NULL) {
if((flows->pluginStatus.pluginPtr != NULL) && (flows->pluginStatus.pluginPtr->pluginURLname != NULL)) {
if(foundAplugin == 0) {
sendStringWOssi(
" [null,'Plugins',null,null,null,\n");
foundAplugin = 1;
}
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
" [null,'%s',null,null,null,\n",
flows->pluginStatus.pluginPtr->pluginName);
sendStringWOssi(buf);
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
" [null,'%sctivate','/" CONST_SHOW_PLUGINS_HTML "?%s=%d',null,null],\n",
flows->pluginStatus.activePlugin ? "Dea" : "A",
flows->pluginStatus.pluginPtr->pluginURLname,
flows->pluginStatus.activePlugin ? 0: 1);
sendStringWOssi(buf);
switch(flows->pluginStatus.pluginPtr->viewConfigureFlag) {
case NoViewNoConfigure:
break;
case ViewOnly:
if(flows->pluginStatus.activePlugin) {
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
" [null,'View','/" CONST_PLUGINS_HEADER "%s',null,null],\n",
flows->pluginStatus.pluginPtr->pluginURLname);
sendStringWOssi(buf);
}
break;
case ConfigureOnly:
if((flows->pluginStatus.pluginPtr->inactiveSetup) ||
(flows->pluginStatus.activePlugin)) {
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
" [null,'Configure','/" CONST_PLUGINS_HEADER "%s',null,null],\n",
flows->pluginStatus.pluginPtr->pluginURLname);
sendStringWOssi(buf);
}
break;
default:
if((flows->pluginStatus.pluginPtr->inactiveSetup) ||
(flows->pluginStatus.activePlugin)) {
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
" [null,'View/Configure','/" CONST_PLUGINS_HEADER "%s',null,null],\n",
flows->pluginStatus.pluginPtr->pluginURLname);
sendStringWOssi(buf);
}
break;
}
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
" [null,'Describe','/" CONST_SHOW_PLUGINS_HTML "?%s',null,null],\n",
flows->pluginStatus.pluginPtr->pluginURLname);
sendStringWOssi(buf);
ep = flows->pluginStatus.pluginPtr->extraPages;
while((ep != NULL) && (ep->url != NULL)) {
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
" [%s%s%s,'%s','/" CONST_PLUGINS_HEADER "%s/%s',null,null],\n",
ep->icon != NULL ? "'<img src=\"/" : "",
ep->icon != NULL ? ep->icon : "null",
ep->icon != NULL ? "\">'" : "",
ep->descr,
flows->pluginStatus.pluginPtr->pluginURLname,
ep->url);
sendStringWOssi(buf);
ep++;
}
sendStringWOssi(
" ],\n");
} /* true plugin */
flows = flows->next;
}
if(foundAplugin != 0)
sendStringWOssi(
" [null,'All','/" CONST_SHOW_PLUGINS_HTML "',null,null],\n"
" ],\n");
sendStringWOssi(
" [null,'Admin',null,null,null,\n");
if(!myGlobals.runningPref.mergeInterfaces) {
int i;
sendStringWOssi(
" [null,'Switch NIC',null,null,null,\n");
for(i=0; i<myGlobals.numDevices; i++) {
if(((!myGlobals.device[i].virtualDevice)
|| (myGlobals.device[i].sflowGlobals)
|| (myGlobals.device[i].netflowGlobals))
&& myGlobals.device[i].activeDevice) {
if(myGlobals.actualReportDeviceId != i) {
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
" [null,'%s','/" CONST_SWITCH_NIC_HTML "?interface=%d',null,null],\n",
myGlobals.device[i].humanFriendlyName, i+1);
sendString(buf);
}
}
}
sendString(" ],\n");
}
sendStringWOssi(
" ['<img src=\"/lock.png\">','Configure',null,null,null,\n"
" ['<img src=\"/lock.png\">','Startup Options','/" CONST_CONFIG_NTOP_HTML "',null,null],\n"
" ['<img src=\"/lock.png\">','Preferences','/"CONST_EDIT_PREFS"',null,null],\n"
" ['<img src=\"/lock.png\">','Packet Filter','/" CONST_CHANGE_FILTER_HTML "',null,null],\n"
" ['<img src=\"/lock.png\">','Reset Stats','/" CONST_RESET_STATS_HTML "',null,null],\n"
" ['<img src=\"/lock.png\">','Web Users','/" CONST_SHOW_USERS_HTML "',null,null],\n"
" ['<img src=\"/lock.png\">','Protect URLs','/" CONST_SHOW_URLS_HTML "',null,null],\n"
" ],\n"
" ['<img src=\"/lock.png\">','Shutdown','/" CONST_SHUTDOWN_NTOP_HTML "',null,null],\n"
" ]\n"
"];\n"
"--></script>\n");
}
/* ************************* */
static void ssiMenu_Body(void) {
sendStringWOssi(
"<table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n"
" <tr>\n"
" <td align=\"left\">\n"
" <table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n"
" <tr>\n"
" <td align=\"left\"><div style=\"width: 103px; height: 75px;\">\n");
if(myGlobals.runningPref.instance != NULL) {
sendStringWOssi(
" <table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n"
" <tr>"
" <td><A HREF=http://www.ntop.org><img src=\"/");
if(myGlobals.runningPref.logo != NULL) {
sendStringWOssi(myGlobals.runningPref.logo);
} else {
sendStringWOssi(CONST_NTOP_LOGO);
}
sendStringWOssi("\" class=\"reflect\" alt=\"ntop logo\" border=0></A></td>\n"
" <td valign=\"top\" align=\"right\" class=\"instance\">Instance: ");
sendStringWOssi(myGlobals.runningPref.instance);
sendStringWOssi(
" </td>\n"
" </tr>\n"
" </table>");
} else {
sendStringWOssi(" <A HREF=http://www.ntop.org><img src=\"/" CONST_NTOP_LOGO "\" class=\"reflect\" border=0></A>");
}
sendStringWOssi(
" </div></td><td></td>\n"
" </tr>\n"
" </table></td>\n"
"<td align=right class=\"leftmenuitem\"><b>(C) 1998-2012 - " CONST_MAILTO_LUCA "</b> </td>"
" </tr>\n"
" <tr>\n"
" <th class=\"leftmenuitem\">\n"
" <noscript>Please enable JavaScript support in your browser</noscript>\n"
" <div id=\"ntopMenuID\">Please enable make sure that the ntop html/ directory is properly installed</div>\n"
"<script type=\"text/javascript\" language=\"JavaScript\"><!--\n"
" cmDraw ('ntopMenuID', ntopMenu, 'hbr', cmThemeOffice, 'ThemeOffice');\n"
"-->\n"
"</script></th>\n"
" <td class=\"leftmenuitem\" align=\"right\"><div><form name=myform method=get action=\"\">"
"<label><img border=0 src=/graph_zoom.gif> </label><input placeholder=\"Search ntop...\" style=\"width: 150px\" type=\"text\" id=\"testinput\" value=\"\" />"
"</form></div></td>\n"
" </tr>\n"
"</table>\n"
"<p> </p>\n");
}
/* ************************* */
static void processSSI(const char *ssiRequest) {
int rc;
char *ssiVirtual,
*ssiURIstart,
*ssiURIend,
*ssiPARMstart;
myGlobals.numSSIRequests++;
#ifdef HTTP_DEBUG
traceEvent(CONST_TRACE_INFO, "HTTP_DEBUG: SSI: Request %d = '%s'", myGlobals.numSSIRequests, ssiRequest);
#endif
/* We need to check if it's the ONLY form we support...
* <!--#include virtual="uri" -->
*
* If it is, we need to do some of the processing we normally do in handleHTTPrequest()
* after the receive and before returnHTTPPage(). But not all - this is, after all
* a request made by ntop internally, or from an ntop .html page which you had to
* be root (or the ntop userid) to install in the first place...
*
* 1. We need to check whether the URL is invalid, i.e. it contains '..' or
* similar chars that can be used for reading system files.
* 2. We can't check the password - we no longer have the Authorization header.
* Besides, we already authorized the page request.
* 3. Strip leading /s and trailing white space
* 4. Break the URI into URI and PARM, then
* 5. Actually process it...
*
* Lastly, remember ssiRequest (and pointers to it) is a COPY, so we don't have
* to restore characters we null out. But, once we do so, we can't display the whole
* request!
*/
if((ssiVirtual = strstr(ssiRequest, "virtual=\"")) == NULL) {
myGlobals.numBadSSIRequests++;
traceEvent(CONST_TRACE_WARNING, "SSI: Ignored invalid (form): '%s'", ssiRequest);
#ifdef HTTP_DEBUG
sendString("<!-- BAD SSI: -->");
sendString(ssiRequest);
#endif
return;
}
ssiURIstart = &ssiVirtual[strlen("virtual=\"")];
if((ssiURIend=strchr(ssiURIstart, '"')) == NULL) {
myGlobals.numBadSSIRequests++;
traceEvent(CONST_TRACE_WARNING, "SSI: Ignored invalid (quotes): '%s'", ssiRequest);
#ifdef HTTP_DEBUG
sendString("<!-- BAD SSI: -->");
sendString(ssiRequest);
#endif
return;
}
ssiURIend[0] = '\0';
#ifdef HTTP_DEBUG
traceEvent(CONST_TRACE_INFO, "HTTP_DEBUG: SSI: Requested URI = '%s'", ssiURIstart);
#endif
if((rc = checkURLsecurity(ssiURIstart)) != 0) {
myGlobals.numBadSSIRequests++;
traceEvent(CONST_TRACE_ERROR, "SSI: URL security: '%s' rejected (code=%d)",
ssiURIstart, rc);
return;
}
while(ssiURIstart[0] == '/') { ssiURIstart++; }
while((ssiURIend > ssiURIstart) &&
((ssiURIend[0] == ' ') ||
(ssiURIend[0] == '\n') ||
(ssiURIend[0] == '\r') ||
(ssiURIend[0] == '\t'))) {
ssiURIend[0] = '\0';
ssiURIend--;
}
if((ssiPARMstart = strchr(ssiURIstart, '?')) != NULL) {
/* We have parms! */
ssiPARMstart[0] = '\0';
ssiPARMstart++;
#ifdef HTTP_DEBUG
traceEvent(CONST_TRACE_INFO, "HTTP_DEBUG: SSI: Adjusted URI = '%s' PARM = '%s'",
ssiURIstart, ssiPARMstart);
} else {
traceEvent(CONST_TRACE_INFO, "HTTP_DEBUG: SSI: Adjusted URI = '%s'", ssiURIstart);
#endif
}
if(ssiURIstart[0] == '\0') {
myGlobals.numBadSSIRequests++;
traceEvent(CONST_TRACE_WARNING, "SSI: Invalid - NULL request ignored");
#ifdef HTTP_DEBUG
sendString("<!-- BAD SSI: --><!--#include virtual=\"\" -->");
#endif
return;
}
/* And so begins the actual processing of an SSI */
sendString("\n<!-- BEGIN SSI ");
sendString(ssiURIstart);
if(ssiPARMstart != NULL) {
sendString("Parm '");
sendString(ssiPARMstart);
sendString("'>");
}
sendString(" -->\n\n");
if(strcasecmp(ssiURIstart, CONST_SSI_MENUBODY_HTML) == 0) {
ssiMenu_Body();
} else if(strcasecmp(ssiURIstart, CONST_SSI_MENUHEAD_HTML) == 0) {
ssiMenu_Head();
} else {
/* Not recognized - oh dear */
sendString("<center><p><b>ERROR</b>: Unrecognized SSI request, '");
sendString(ssiURIstart);
sendString("'");
if(ssiPARMstart != NULL) {
sendString(", with parm '");
sendString(ssiPARMstart);
sendString("'");
}
sendString("</p></center>\n");
myGlobals.numBadSSIRequests++;
return;
}
sendString("\n<!-- END SSI ");
sendString(ssiURIstart);
sendString(" -->\n\n");
myGlobals.numHandledSSIRequests++;
}